Mark ChangeLog
[official-gcc.git] / gcc / tree-cfg.c
blob6a69d683f3c3252d31e0f14055c1307ae269004c
1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "errors.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "ggc.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
40 #include "timevar.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "cfgloop.h"
46 #include "cfglayout.h"
47 #include "hashtab.h"
49 /* This file contains functions for building the Control Flow Graph (CFG)
50 for a function tree. */
52 /* Local declarations. */
54 /* Initial capacity for the basic block array. */
55 static const int initial_cfg_capacity = 20;
57 /* Mapping of labels to their associated blocks. This can greatly speed up
58 building of the CFG in code with lots of gotos. */
59 static GTY(()) varray_type label_to_block_map;
61 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
62 which use a particular edge. The CASE_LABEL_EXPRs are chained together
63 via their TREE_CHAIN field, which we clear after we're done with the
64 hash table to prevent problems with duplication of SWITCH_EXPRs.
66 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
67 update the case vector in response to edge redirections.
69 Right now this table is set up and torn down at key points in the
70 compilation process. It would be nice if we could make the table
71 more persistent. The key is getting notification of changes to
72 the CFG (particularly edge removal, creation and redirection). */
74 struct edge_to_cases_elt
76 /* The edge itself. Necessary for hashing and equality tests. */
77 edge e;
79 /* The case labels associated with this edge. We link these up via
80 their TREE_CHAIN field, then we wipe out the TREE_CHAIN fields
81 when we destroy the hash table. This prevents problems when copying
82 SWITCH_EXPRs. */
83 tree case_labels;
86 static htab_t edge_to_cases;
88 /* CFG statistics. */
89 struct cfg_stats_d
91 long num_merged_labels;
94 static struct cfg_stats_d cfg_stats;
96 /* Nonzero if we found a computed goto while building basic blocks. */
97 static bool found_computed_goto;
99 /* Basic blocks and flowgraphs. */
100 static basic_block create_bb (void *, void *, basic_block);
101 static void create_block_annotation (basic_block);
102 static void free_blocks_annotations (void);
103 static void clear_blocks_annotations (void);
104 static void make_blocks (tree);
105 static void factor_computed_gotos (void);
107 /* Edges. */
108 static void make_edges (void);
109 static void make_ctrl_stmt_edges (basic_block);
110 static void make_exit_edges (basic_block);
111 static void make_cond_expr_edges (basic_block);
112 static void make_switch_expr_edges (basic_block);
113 static void make_goto_expr_edges (basic_block);
114 static edge tree_redirect_edge_and_branch (edge, basic_block);
115 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
116 static void split_critical_edges (void);
117 static bool remove_fallthru_edge (VEC(edge) *);
119 /* Various helpers. */
120 static inline bool stmt_starts_bb_p (tree, tree);
121 static int tree_verify_flow_info (void);
122 static void tree_make_forwarder_block (edge);
123 static bool tree_forwarder_block_p (basic_block, bool);
124 static void tree_cfg2vcg (FILE *);
126 /* Flowgraph optimization and cleanup. */
127 static void tree_merge_blocks (basic_block, basic_block);
128 static bool tree_can_merge_blocks_p (basic_block, basic_block);
129 static void remove_bb (basic_block);
130 static bool cleanup_control_flow (void);
131 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
132 static edge find_taken_edge_cond_expr (basic_block, tree);
133 static edge find_taken_edge_switch_expr (basic_block, tree);
134 static tree find_case_label_for_value (tree, tree);
135 static bool phi_alternatives_equal (basic_block, edge, edge);
136 static bool cleanup_forwarder_blocks (void);
139 /*---------------------------------------------------------------------------
140 Create basic blocks
141 ---------------------------------------------------------------------------*/
143 /* Entry point to the CFG builder for trees. TP points to the list of
144 statements to be added to the flowgraph. */
146 static void
147 build_tree_cfg (tree *tp)
149 /* Register specific tree functions. */
150 tree_register_cfg_hooks ();
152 /* Initialize rbi_pool. */
153 alloc_rbi_pool ();
155 /* Initialize the basic block array. */
156 init_flow ();
157 profile_status = PROFILE_ABSENT;
158 n_basic_blocks = 0;
159 last_basic_block = 0;
160 VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info");
161 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
163 /* Build a mapping of labels to their associated blocks. */
164 VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity,
165 "label to block map");
167 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
168 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
170 found_computed_goto = 0;
171 make_blocks (*tp);
173 /* Computed gotos are hell to deal with, especially if there are
174 lots of them with a large number of destinations. So we factor
175 them to a common computed goto location before we build the
176 edge list. After we convert back to normal form, we will un-factor
177 the computed gotos since factoring introduces an unwanted jump. */
178 if (found_computed_goto)
179 factor_computed_gotos ();
181 /* Make sure there is always at least one block, even if it's empty. */
182 if (n_basic_blocks == 0)
183 create_empty_bb (ENTRY_BLOCK_PTR);
185 create_block_annotation (ENTRY_BLOCK_PTR);
186 create_block_annotation (EXIT_BLOCK_PTR);
188 /* Adjust the size of the array. */
189 VARRAY_GROW (basic_block_info, n_basic_blocks);
191 /* To speed up statement iterator walks, we first purge dead labels. */
192 cleanup_dead_labels ();
194 /* Group case nodes to reduce the number of edges.
195 We do this after cleaning up dead labels because otherwise we miss
196 a lot of obvious case merging opportunities. */
197 group_case_labels ();
199 /* Create the edges of the flowgraph. */
200 make_edges ();
202 /* Debugging dumps. */
204 /* Write the flowgraph to a VCG file. */
206 int local_dump_flags;
207 FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags);
208 if (dump_file)
210 tree_cfg2vcg (dump_file);
211 dump_end (TDI_vcg, dump_file);
215 /* Dump a textual representation of the flowgraph. */
216 if (dump_file)
217 dump_tree_cfg (dump_file, dump_flags);
220 static void
221 execute_build_cfg (void)
223 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
226 struct tree_opt_pass pass_build_cfg =
228 "cfg", /* name */
229 NULL, /* gate */
230 execute_build_cfg, /* execute */
231 NULL, /* sub */
232 NULL, /* next */
233 0, /* static_pass_number */
234 TV_TREE_CFG, /* tv_id */
235 PROP_gimple_leh, /* properties_required */
236 PROP_cfg, /* properties_provided */
237 0, /* properties_destroyed */
238 0, /* todo_flags_start */
239 TODO_verify_stmts, /* todo_flags_finish */
240 0 /* letter */
243 /* Search the CFG for any computed gotos. If found, factor them to a
244 common computed goto site. Also record the location of that site so
245 that we can un-factor the gotos after we have converted back to
246 normal form. */
248 static void
249 factor_computed_gotos (void)
251 basic_block bb;
252 tree factored_label_decl = NULL;
253 tree var = NULL;
254 tree factored_computed_goto_label = NULL;
255 tree factored_computed_goto = NULL;
257 /* We know there are one or more computed gotos in this function.
258 Examine the last statement in each basic block to see if the block
259 ends with a computed goto. */
261 FOR_EACH_BB (bb)
263 block_stmt_iterator bsi = bsi_last (bb);
264 tree last;
266 if (bsi_end_p (bsi))
267 continue;
268 last = bsi_stmt (bsi);
270 /* Ignore the computed goto we create when we factor the original
271 computed gotos. */
272 if (last == factored_computed_goto)
273 continue;
275 /* If the last statement is a computed goto, factor it. */
276 if (computed_goto_p (last))
278 tree assignment;
280 /* The first time we find a computed goto we need to create
281 the factored goto block and the variable each original
282 computed goto will use for their goto destination. */
283 if (! factored_computed_goto)
285 basic_block new_bb = create_empty_bb (bb);
286 block_stmt_iterator new_bsi = bsi_start (new_bb);
288 /* Create the destination of the factored goto. Each original
289 computed goto will put its desired destination into this
290 variable and jump to the label we create immediately
291 below. */
292 var = create_tmp_var (ptr_type_node, "gotovar");
294 /* Build a label for the new block which will contain the
295 factored computed goto. */
296 factored_label_decl = create_artificial_label ();
297 factored_computed_goto_label
298 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
299 bsi_insert_after (&new_bsi, factored_computed_goto_label,
300 BSI_NEW_STMT);
302 /* Build our new computed goto. */
303 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
304 bsi_insert_after (&new_bsi, factored_computed_goto,
305 BSI_NEW_STMT);
308 /* Copy the original computed goto's destination into VAR. */
309 assignment = build (MODIFY_EXPR, ptr_type_node,
310 var, GOTO_DESTINATION (last));
311 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
313 /* And re-vector the computed goto to the new destination. */
314 GOTO_DESTINATION (last) = factored_label_decl;
320 /* Create annotations for a single basic block. */
322 static void
323 create_block_annotation (basic_block bb)
325 /* Verify that the tree_annotations field is clear. */
326 gcc_assert (!bb->tree_annotations);
327 bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d));
331 /* Free the annotations for all the basic blocks. */
333 static void free_blocks_annotations (void)
335 clear_blocks_annotations ();
339 /* Clear the annotations for all the basic blocks. */
341 static void
342 clear_blocks_annotations (void)
344 basic_block bb;
346 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
347 bb->tree_annotations = NULL;
351 /* Build a flowgraph for the statement_list STMT_LIST. */
353 static void
354 make_blocks (tree stmt_list)
356 tree_stmt_iterator i = tsi_start (stmt_list);
357 tree stmt = NULL;
358 bool start_new_block = true;
359 bool first_stmt_of_list = true;
360 basic_block bb = ENTRY_BLOCK_PTR;
362 while (!tsi_end_p (i))
364 tree prev_stmt;
366 prev_stmt = stmt;
367 stmt = tsi_stmt (i);
369 /* If the statement starts a new basic block or if we have determined
370 in a previous pass that we need to create a new block for STMT, do
371 so now. */
372 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
374 if (!first_stmt_of_list)
375 stmt_list = tsi_split_statement_list_before (&i);
376 bb = create_basic_block (stmt_list, NULL, bb);
377 start_new_block = false;
380 /* Now add STMT to BB and create the subgraphs for special statement
381 codes. */
382 set_bb_for_stmt (stmt, bb);
384 if (computed_goto_p (stmt))
385 found_computed_goto = true;
387 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
388 next iteration. */
389 if (stmt_ends_bb_p (stmt))
390 start_new_block = true;
392 tsi_next (&i);
393 first_stmt_of_list = false;
398 /* Create and return a new empty basic block after bb AFTER. */
400 static basic_block
401 create_bb (void *h, void *e, basic_block after)
403 basic_block bb;
405 gcc_assert (!e);
407 /* Create and initialize a new basic block. Since alloc_block uses
408 ggc_alloc_cleared to allocate a basic block, we do not have to
409 clear the newly allocated basic block here. */
410 bb = alloc_block ();
412 bb->index = last_basic_block;
413 bb->flags = BB_NEW;
414 bb->stmt_list = h ? h : alloc_stmt_list ();
416 /* Add the new block to the linked list of blocks. */
417 link_block (bb, after);
419 /* Grow the basic block array if needed. */
420 if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info))
422 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
423 VARRAY_GROW (basic_block_info, new_size);
426 /* Add the newly created block to the array. */
427 BASIC_BLOCK (last_basic_block) = bb;
429 create_block_annotation (bb);
431 n_basic_blocks++;
432 last_basic_block++;
434 initialize_bb_rbi (bb);
435 return bb;
439 /*---------------------------------------------------------------------------
440 Edge creation
441 ---------------------------------------------------------------------------*/
443 /* Fold COND_EXPR_COND of each COND_EXPR. */
445 static void
446 fold_cond_expr_cond (void)
448 basic_block bb;
450 FOR_EACH_BB (bb)
452 tree stmt = last_stmt (bb);
454 if (stmt
455 && TREE_CODE (stmt) == COND_EXPR)
457 tree cond = fold (COND_EXPR_COND (stmt));
458 if (integer_zerop (cond))
459 COND_EXPR_COND (stmt) = integer_zero_node;
460 else if (integer_onep (cond))
461 COND_EXPR_COND (stmt) = integer_one_node;
466 /* Join all the blocks in the flowgraph. */
468 static void
469 make_edges (void)
471 basic_block bb;
473 /* Create an edge from entry to the first block with executable
474 statements in it. */
475 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
477 /* Traverse the basic block array placing edges. */
478 FOR_EACH_BB (bb)
480 tree first = first_stmt (bb);
481 tree last = last_stmt (bb);
483 if (first)
485 /* Edges for statements that always alter flow control. */
486 if (is_ctrl_stmt (last))
487 make_ctrl_stmt_edges (bb);
489 /* Edges for statements that sometimes alter flow control. */
490 if (is_ctrl_altering_stmt (last))
491 make_exit_edges (bb);
494 /* Finally, if no edges were created above, this is a regular
495 basic block that only needs a fallthru edge. */
496 if (EDGE_COUNT (bb->succs) == 0)
497 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
500 /* We do not care about fake edges, so remove any that the CFG
501 builder inserted for completeness. */
502 remove_fake_exit_edges ();
504 /* Fold COND_EXPR_COND of each COND_EXPR. */
505 fold_cond_expr_cond ();
507 /* Clean up the graph and warn for unreachable code. */
508 cleanup_tree_cfg ();
512 /* Create edges for control statement at basic block BB. */
514 static void
515 make_ctrl_stmt_edges (basic_block bb)
517 tree last = last_stmt (bb);
519 gcc_assert (last);
520 switch (TREE_CODE (last))
522 case GOTO_EXPR:
523 make_goto_expr_edges (bb);
524 break;
526 case RETURN_EXPR:
527 make_edge (bb, EXIT_BLOCK_PTR, 0);
528 break;
530 case COND_EXPR:
531 make_cond_expr_edges (bb);
532 break;
534 case SWITCH_EXPR:
535 make_switch_expr_edges (bb);
536 break;
538 case RESX_EXPR:
539 make_eh_edges (last);
540 /* Yet another NORETURN hack. */
541 if (EDGE_COUNT (bb->succs) == 0)
542 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
543 break;
545 default:
546 gcc_unreachable ();
551 /* Create exit edges for statements in block BB that alter the flow of
552 control. Statements that alter the control flow are 'goto', 'return'
553 and calls to non-returning functions. */
555 static void
556 make_exit_edges (basic_block bb)
558 tree last = last_stmt (bb), op;
560 gcc_assert (last);
561 switch (TREE_CODE (last))
563 case CALL_EXPR:
564 /* If this function receives a nonlocal goto, then we need to
565 make edges from this call site to all the nonlocal goto
566 handlers. */
567 if (TREE_SIDE_EFFECTS (last)
568 && current_function_has_nonlocal_label)
569 make_goto_expr_edges (bb);
571 /* If this statement has reachable exception handlers, then
572 create abnormal edges to them. */
573 make_eh_edges (last);
575 /* Some calls are known not to return. For such calls we create
576 a fake edge.
578 We really need to revamp how we build edges so that it's not
579 such a bloody pain to avoid creating edges for this case since
580 all we do is remove these edges when we're done building the
581 CFG. */
582 if (call_expr_flags (last) & ECF_NORETURN)
584 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
585 return;
588 /* Don't forget the fall-thru edge. */
589 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
590 break;
592 case MODIFY_EXPR:
593 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
594 may have an abnormal edge. Search the RHS for this case and
595 create any required edges. */
596 op = get_call_expr_in (last);
597 if (op && TREE_SIDE_EFFECTS (op)
598 && current_function_has_nonlocal_label)
599 make_goto_expr_edges (bb);
601 make_eh_edges (last);
602 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
603 break;
605 default:
606 gcc_unreachable ();
611 /* Create the edges for a COND_EXPR starting at block BB.
612 At this point, both clauses must contain only simple gotos. */
614 static void
615 make_cond_expr_edges (basic_block bb)
617 tree entry = last_stmt (bb);
618 basic_block then_bb, else_bb;
619 tree then_label, else_label;
621 gcc_assert (entry);
622 gcc_assert (TREE_CODE (entry) == COND_EXPR);
624 /* Entry basic blocks for each component. */
625 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
626 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
627 then_bb = label_to_block (then_label);
628 else_bb = label_to_block (else_label);
630 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
631 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
634 /* Hashing routine for EDGE_TO_CASES. */
636 static hashval_t
637 edge_to_cases_hash (const void *p)
639 edge e = ((struct edge_to_cases_elt *)p)->e;
641 /* Hash on the edge itself (which is a pointer). */
642 return htab_hash_pointer (e);
645 /* Equality routine for EDGE_TO_CASES, edges are unique, so testing
646 for equality is just a pointer comparison. */
648 static int
649 edge_to_cases_eq (const void *p1, const void *p2)
651 edge e1 = ((struct edge_to_cases_elt *)p1)->e;
652 edge e2 = ((struct edge_to_cases_elt *)p2)->e;
654 return e1 == e2;
657 /* Called for each element in the hash table (P) as we delete the
658 edge to cases hash table.
660 Clear all the TREE_CHAINs to prevent problems with copying of
661 SWITCH_EXPRs and structure sharing rules, then free the hash table
662 element. */
664 static void
665 edge_to_cases_cleanup (void *p)
667 struct edge_to_cases_elt *elt = p;
668 tree t, next;
670 for (t = elt->case_labels; t; t = next)
672 next = TREE_CHAIN (t);
673 TREE_CHAIN (t) = NULL;
675 free (p);
678 /* Start recording information mapping edges to case labels. */
680 static void
681 start_recording_case_labels (void)
683 gcc_assert (edge_to_cases == NULL);
685 edge_to_cases = htab_create (37,
686 edge_to_cases_hash,
687 edge_to_cases_eq,
688 edge_to_cases_cleanup);
691 /* Return nonzero if we are recording information for case labels. */
693 static bool
694 recording_case_labels_p (void)
696 return (edge_to_cases != NULL);
699 /* Stop recording information mapping edges to case labels and
700 remove any information we have recorded. */
701 static void
702 end_recording_case_labels (void)
704 htab_delete (edge_to_cases);
705 edge_to_cases = NULL;
708 /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */
710 static void
711 record_switch_edge (edge e, tree case_label)
713 struct edge_to_cases_elt *elt;
714 void **slot;
716 /* Build a hash table element so we can see if E is already
717 in the table. */
718 elt = xmalloc (sizeof (struct edge_to_cases_elt));
719 elt->e = e;
720 elt->case_labels = case_label;
722 slot = htab_find_slot (edge_to_cases, elt, INSERT);
724 if (*slot == NULL)
726 /* E was not in the hash table. Install E into the hash table. */
727 *slot = (void *)elt;
729 else
731 /* E was already in the hash table. Free ELT as we do not need it
732 anymore. */
733 free (elt);
735 /* Get the entry stored in the hash table. */
736 elt = (struct edge_to_cases_elt *) *slot;
738 /* Add it to the chain of CASE_LABEL_EXPRs referencing E. */
739 TREE_CHAIN (case_label) = elt->case_labels;
740 elt->case_labels = case_label;
744 /* If we are inside a {start,end}_recording_cases block, then return
745 a chain of CASE_LABEL_EXPRs from T which reference E.
747 Otherwise return NULL. */
749 static tree
750 get_cases_for_edge (edge e, tree t)
752 struct edge_to_cases_elt elt, *elt_p;
753 void **slot;
754 size_t i, n;
755 tree vec;
757 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
758 chains available. Return NULL so the caller can detect this case. */
759 if (!recording_case_labels_p ())
760 return NULL;
762 restart:
763 elt.e = e;
764 elt.case_labels = NULL;
765 slot = htab_find_slot (edge_to_cases, &elt, NO_INSERT);
767 if (slot)
769 elt_p = (struct edge_to_cases_elt *)*slot;
770 return elt_p->case_labels;
773 /* If we did not find E in the hash table, then this must be the first
774 time we have been queried for information about E & T. Add all the
775 elements from T to the hash table then perform the query again. */
777 vec = SWITCH_LABELS (t);
778 n = TREE_VEC_LENGTH (vec);
779 for (i = 0; i < n; i++)
781 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
782 basic_block label_bb = label_to_block (lab);
783 record_switch_edge (find_edge (e->src, label_bb), TREE_VEC_ELT (vec, i));
785 goto restart;
788 /* Create the edges for a SWITCH_EXPR starting at block BB.
789 At this point, the switch body has been lowered and the
790 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
792 static void
793 make_switch_expr_edges (basic_block bb)
795 tree entry = last_stmt (bb);
796 size_t i, n;
797 tree vec;
799 vec = SWITCH_LABELS (entry);
800 n = TREE_VEC_LENGTH (vec);
802 for (i = 0; i < n; ++i)
804 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
805 basic_block label_bb = label_to_block (lab);
806 make_edge (bb, label_bb, 0);
811 /* Return the basic block holding label DEST. */
813 basic_block
814 label_to_block (tree dest)
816 int uid = LABEL_DECL_UID (dest);
818 /* We would die hard when faced by an undefined label. Emit a label to
819 the very first basic block. This will hopefully make even the dataflow
820 and undefined variable warnings quite right. */
821 if ((errorcount || sorrycount) && uid < 0)
823 block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0));
824 tree stmt;
826 stmt = build1 (LABEL_EXPR, void_type_node, dest);
827 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
828 uid = LABEL_DECL_UID (dest);
830 return VARRAY_BB (label_to_block_map, uid);
834 /* Create edges for a goto statement at block BB. */
836 static void
837 make_goto_expr_edges (basic_block bb)
839 tree goto_t, dest;
840 basic_block target_bb;
841 int for_call;
842 block_stmt_iterator last = bsi_last (bb);
844 goto_t = bsi_stmt (last);
846 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
847 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
848 from a nonlocal goto. */
849 if (TREE_CODE (goto_t) != GOTO_EXPR)
851 dest = error_mark_node;
852 for_call = 1;
854 else
856 dest = GOTO_DESTINATION (goto_t);
857 for_call = 0;
859 /* A GOTO to a local label creates normal edges. */
860 if (simple_goto_p (goto_t))
862 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
863 #ifdef USE_MAPPED_LOCATION
864 e->goto_locus = EXPR_LOCATION (goto_t);
865 #else
866 e->goto_locus = EXPR_LOCUS (goto_t);
867 #endif
868 bsi_remove (&last);
869 return;
872 /* Nothing more to do for nonlocal gotos. */
873 if (TREE_CODE (dest) == LABEL_DECL)
874 return;
876 /* Computed gotos remain. */
879 /* Look for the block starting with the destination label. In the
880 case of a computed goto, make an edge to any label block we find
881 in the CFG. */
882 FOR_EACH_BB (target_bb)
884 block_stmt_iterator bsi;
886 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
888 tree target = bsi_stmt (bsi);
890 if (TREE_CODE (target) != LABEL_EXPR)
891 break;
893 if (
894 /* Computed GOTOs. Make an edge to every label block that has
895 been marked as a potential target for a computed goto. */
896 (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0)
897 /* Nonlocal GOTO target. Make an edge to every label block
898 that has been marked as a potential target for a nonlocal
899 goto. */
900 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1))
902 make_edge (bb, target_bb, EDGE_ABNORMAL);
903 break;
908 /* Degenerate case of computed goto with no labels. */
909 if (!for_call && EDGE_COUNT (bb->succs) == 0)
910 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
914 /*---------------------------------------------------------------------------
915 Flowgraph analysis
916 ---------------------------------------------------------------------------*/
918 /* Remove unreachable blocks and other miscellaneous clean up work. */
920 bool
921 cleanup_tree_cfg (void)
923 bool retval = false;
925 timevar_push (TV_TREE_CLEANUP_CFG);
927 retval = cleanup_control_flow ();
928 retval |= delete_unreachable_blocks ();
930 /* Forwarder blocks can carry line number information which is
931 useful when debugging, so we only clean them up when
932 optimizing. */
934 if (optimize > 0)
936 /* cleanup_forwarder_blocks can redirect edges out of
937 SWITCH_EXPRs, which can get expensive. So we want to enable
938 recording of edge to CASE_LABEL_EXPR mappings around the call
939 to cleanup_forwarder_blocks. */
940 start_recording_case_labels ();
941 retval |= cleanup_forwarder_blocks ();
942 end_recording_case_labels ();
945 #ifdef ENABLE_CHECKING
946 if (retval)
948 gcc_assert (!cleanup_control_flow ());
949 gcc_assert (!delete_unreachable_blocks ());
950 if (optimize > 0)
951 gcc_assert (!cleanup_forwarder_blocks ());
953 #endif
955 /* Merging the blocks creates no new opportunities for the other
956 optimizations, so do it here. */
957 retval |= merge_seq_blocks ();
959 compact_blocks ();
961 #ifdef ENABLE_CHECKING
962 verify_flow_info ();
963 #endif
964 timevar_pop (TV_TREE_CLEANUP_CFG);
965 return retval;
969 /* Cleanup useless labels in basic blocks. This is something we wish
970 to do early because it allows us to group case labels before creating
971 the edges for the CFG, and it speeds up block statement iterators in
972 all passes later on.
973 We only run this pass once, running it more than once is probably not
974 profitable. */
976 /* A map from basic block index to the leading label of that block. */
977 static tree *label_for_bb;
979 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
980 static void
981 update_eh_label (struct eh_region *region)
983 tree old_label = get_eh_region_tree_label (region);
984 if (old_label)
986 tree new_label;
987 basic_block bb = label_to_block (old_label);
989 /* ??? After optimizing, there may be EH regions with labels
990 that have already been removed from the function body, so
991 there is no basic block for them. */
992 if (! bb)
993 return;
995 new_label = label_for_bb[bb->index];
996 set_eh_region_tree_label (region, new_label);
1000 /* Given LABEL return the first label in the same basic block. */
1001 static tree
1002 main_block_label (tree label)
1004 basic_block bb = label_to_block (label);
1006 /* label_to_block possibly inserted undefined label into the chain. */
1007 if (!label_for_bb[bb->index])
1008 label_for_bb[bb->index] = label;
1009 return label_for_bb[bb->index];
1012 /* Cleanup redundant labels. This is a three-step process:
1013 1) Find the leading label for each block.
1014 2) Redirect all references to labels to the leading labels.
1015 3) Cleanup all useless labels. */
1017 void
1018 cleanup_dead_labels (void)
1020 basic_block bb;
1021 label_for_bb = xcalloc (last_basic_block, sizeof (tree));
1023 /* Find a suitable label for each block. We use the first user-defined
1024 label if there is one, or otherwise just the first label we see. */
1025 FOR_EACH_BB (bb)
1027 block_stmt_iterator i;
1029 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
1031 tree label, stmt = bsi_stmt (i);
1033 if (TREE_CODE (stmt) != LABEL_EXPR)
1034 break;
1036 label = LABEL_EXPR_LABEL (stmt);
1038 /* If we have not yet seen a label for the current block,
1039 remember this one and see if there are more labels. */
1040 if (! label_for_bb[bb->index])
1042 label_for_bb[bb->index] = label;
1043 continue;
1046 /* If we did see a label for the current block already, but it
1047 is an artificially created label, replace it if the current
1048 label is a user defined label. */
1049 if (! DECL_ARTIFICIAL (label)
1050 && DECL_ARTIFICIAL (label_for_bb[bb->index]))
1052 label_for_bb[bb->index] = label;
1053 break;
1058 /* Now redirect all jumps/branches to the selected label.
1059 First do so for each block ending in a control statement. */
1060 FOR_EACH_BB (bb)
1062 tree stmt = last_stmt (bb);
1063 if (!stmt)
1064 continue;
1066 switch (TREE_CODE (stmt))
1068 case COND_EXPR:
1070 tree true_branch, false_branch;
1072 true_branch = COND_EXPR_THEN (stmt);
1073 false_branch = COND_EXPR_ELSE (stmt);
1075 GOTO_DESTINATION (true_branch)
1076 = main_block_label (GOTO_DESTINATION (true_branch));
1077 GOTO_DESTINATION (false_branch)
1078 = main_block_label (GOTO_DESTINATION (false_branch));
1080 break;
1083 case SWITCH_EXPR:
1085 size_t i;
1086 tree vec = SWITCH_LABELS (stmt);
1087 size_t n = TREE_VEC_LENGTH (vec);
1089 /* Replace all destination labels. */
1090 for (i = 0; i < n; ++i)
1092 tree elt = TREE_VEC_ELT (vec, i);
1093 tree label = main_block_label (CASE_LABEL (elt));
1094 CASE_LABEL (elt) = label;
1096 break;
1099 /* We have to handle GOTO_EXPRs until they're removed, and we don't
1100 remove them until after we've created the CFG edges. */
1101 case GOTO_EXPR:
1102 if (! computed_goto_p (stmt))
1104 GOTO_DESTINATION (stmt)
1105 = main_block_label (GOTO_DESTINATION (stmt));
1106 break;
1109 default:
1110 break;
1114 for_each_eh_region (update_eh_label);
1116 /* Finally, purge dead labels. All user-defined labels and labels that
1117 can be the target of non-local gotos are preserved. */
1118 FOR_EACH_BB (bb)
1120 block_stmt_iterator i;
1121 tree label_for_this_bb = label_for_bb[bb->index];
1123 if (! label_for_this_bb)
1124 continue;
1126 for (i = bsi_start (bb); !bsi_end_p (i); )
1128 tree label, stmt = bsi_stmt (i);
1130 if (TREE_CODE (stmt) != LABEL_EXPR)
1131 break;
1133 label = LABEL_EXPR_LABEL (stmt);
1135 if (label == label_for_this_bb
1136 || ! DECL_ARTIFICIAL (label)
1137 || DECL_NONLOCAL (label))
1138 bsi_next (&i);
1139 else
1140 bsi_remove (&i);
1144 free (label_for_bb);
1147 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1148 and scan the sorted vector of cases. Combine the ones jumping to the
1149 same label.
1150 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1152 void
1153 group_case_labels (void)
1155 basic_block bb;
1157 FOR_EACH_BB (bb)
1159 tree stmt = last_stmt (bb);
1160 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1162 tree labels = SWITCH_LABELS (stmt);
1163 int old_size = TREE_VEC_LENGTH (labels);
1164 int i, j, new_size = old_size;
1165 tree default_case = TREE_VEC_ELT (labels, old_size - 1);
1166 tree default_label;
1168 /* The default label is always the last case in a switch
1169 statement after gimplification. */
1170 default_label = CASE_LABEL (default_case);
1172 /* Look for possible opportunities to merge cases.
1173 Ignore the last element of the label vector because it
1174 must be the default case. */
1175 i = 0;
1176 while (i < old_size - 1)
1178 tree base_case, base_label, base_high, type;
1179 base_case = TREE_VEC_ELT (labels, i);
1181 gcc_assert (base_case);
1182 base_label = CASE_LABEL (base_case);
1184 /* Discard cases that have the same destination as the
1185 default case. */
1186 if (base_label == default_label)
1188 TREE_VEC_ELT (labels, i) = NULL_TREE;
1189 i++;
1190 new_size--;
1191 continue;
1194 type = TREE_TYPE (CASE_LOW (base_case));
1195 base_high = CASE_HIGH (base_case) ?
1196 CASE_HIGH (base_case) : CASE_LOW (base_case);
1197 i++;
1198 /* Try to merge case labels. Break out when we reach the end
1199 of the label vector or when we cannot merge the next case
1200 label with the current one. */
1201 while (i < old_size - 1)
1203 tree merge_case = TREE_VEC_ELT (labels, i);
1204 tree merge_label = CASE_LABEL (merge_case);
1205 tree t = int_const_binop (PLUS_EXPR, base_high,
1206 integer_one_node, 1);
1208 /* Merge the cases if they jump to the same place,
1209 and their ranges are consecutive. */
1210 if (merge_label == base_label
1211 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1213 base_high = CASE_HIGH (merge_case) ?
1214 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1215 CASE_HIGH (base_case) = base_high;
1216 TREE_VEC_ELT (labels, i) = NULL_TREE;
1217 new_size--;
1218 i++;
1220 else
1221 break;
1225 /* Compress the case labels in the label vector, and adjust the
1226 length of the vector. */
1227 for (i = 0, j = 0; i < new_size; i++)
1229 while (! TREE_VEC_ELT (labels, j))
1230 j++;
1231 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1233 TREE_VEC_LENGTH (labels) = new_size;
1238 /* Checks whether we can merge block B into block A. */
1240 static bool
1241 tree_can_merge_blocks_p (basic_block a, basic_block b)
1243 tree stmt;
1244 block_stmt_iterator bsi;
1246 if (EDGE_COUNT (a->succs) != 1)
1247 return false;
1249 if (EDGE_SUCC (a, 0)->flags & EDGE_ABNORMAL)
1250 return false;
1252 if (EDGE_SUCC (a, 0)->dest != b)
1253 return false;
1255 if (EDGE_COUNT (b->preds) > 1)
1256 return false;
1258 if (b == EXIT_BLOCK_PTR)
1259 return false;
1261 /* If A ends by a statement causing exceptions or something similar, we
1262 cannot merge the blocks. */
1263 stmt = last_stmt (a);
1264 if (stmt && stmt_ends_bb_p (stmt))
1265 return false;
1267 /* Do not allow a block with only a non-local label to be merged. */
1268 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1269 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1270 return false;
1272 /* There may be no phi nodes at the start of b. Most of these degenerate
1273 phi nodes should be cleaned up by kill_redundant_phi_nodes. */
1274 if (phi_nodes (b))
1275 return false;
1277 /* Do not remove user labels. */
1278 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1280 stmt = bsi_stmt (bsi);
1281 if (TREE_CODE (stmt) != LABEL_EXPR)
1282 break;
1283 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1284 return false;
1287 return true;
1291 /* Merge block B into block A. */
1293 static void
1294 tree_merge_blocks (basic_block a, basic_block b)
1296 block_stmt_iterator bsi;
1297 tree_stmt_iterator last;
1299 if (dump_file)
1300 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1302 /* Ensure that B follows A. */
1303 move_block_after (b, a);
1305 gcc_assert (EDGE_SUCC (a, 0)->flags & EDGE_FALLTHRU);
1306 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1308 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1309 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1311 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1312 bsi_remove (&bsi);
1313 else
1315 set_bb_for_stmt (bsi_stmt (bsi), a);
1316 bsi_next (&bsi);
1320 /* Merge the chains. */
1321 last = tsi_last (a->stmt_list);
1322 tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT);
1323 b->stmt_list = NULL;
1327 /* Walk the function tree removing unnecessary statements.
1329 * Empty statement nodes are removed
1331 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1333 * Unnecessary COND_EXPRs are removed
1335 * Some unnecessary BIND_EXPRs are removed
1337 Clearly more work could be done. The trick is doing the analysis
1338 and removal fast enough to be a net improvement in compile times.
1340 Note that when we remove a control structure such as a COND_EXPR
1341 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1342 to ensure we eliminate all the useless code. */
1344 struct rus_data
1346 tree *last_goto;
1347 bool repeat;
1348 bool may_throw;
1349 bool may_branch;
1350 bool has_label;
1353 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1355 static bool
1356 remove_useless_stmts_warn_notreached (tree stmt)
1358 if (EXPR_HAS_LOCATION (stmt))
1360 location_t loc = EXPR_LOCATION (stmt);
1361 if (LOCATION_LINE (loc) > 0)
1363 warning ("%Hwill never be executed", &loc);
1364 return true;
1368 switch (TREE_CODE (stmt))
1370 case STATEMENT_LIST:
1372 tree_stmt_iterator i;
1373 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1374 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1375 return true;
1377 break;
1379 case COND_EXPR:
1380 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1381 return true;
1382 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1383 return true;
1384 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1385 return true;
1386 break;
1388 case TRY_FINALLY_EXPR:
1389 case TRY_CATCH_EXPR:
1390 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1391 return true;
1392 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1393 return true;
1394 break;
1396 case CATCH_EXPR:
1397 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1398 case EH_FILTER_EXPR:
1399 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1400 case BIND_EXPR:
1401 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1403 default:
1404 /* Not a live container. */
1405 break;
1408 return false;
1411 static void
1412 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1414 tree then_clause, else_clause, cond;
1415 bool save_has_label, then_has_label, else_has_label;
1417 save_has_label = data->has_label;
1418 data->has_label = false;
1419 data->last_goto = NULL;
1421 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1423 then_has_label = data->has_label;
1424 data->has_label = false;
1425 data->last_goto = NULL;
1427 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1429 else_has_label = data->has_label;
1430 data->has_label = save_has_label | then_has_label | else_has_label;
1432 then_clause = COND_EXPR_THEN (*stmt_p);
1433 else_clause = COND_EXPR_ELSE (*stmt_p);
1434 cond = fold (COND_EXPR_COND (*stmt_p));
1436 /* If neither arm does anything at all, we can remove the whole IF. */
1437 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1439 *stmt_p = build_empty_stmt ();
1440 data->repeat = true;
1443 /* If there are no reachable statements in an arm, then we can
1444 zap the entire conditional. */
1445 else if (integer_nonzerop (cond) && !else_has_label)
1447 if (warn_notreached)
1448 remove_useless_stmts_warn_notreached (else_clause);
1449 *stmt_p = then_clause;
1450 data->repeat = true;
1452 else if (integer_zerop (cond) && !then_has_label)
1454 if (warn_notreached)
1455 remove_useless_stmts_warn_notreached (then_clause);
1456 *stmt_p = else_clause;
1457 data->repeat = true;
1460 /* Check a couple of simple things on then/else with single stmts. */
1461 else
1463 tree then_stmt = expr_only (then_clause);
1464 tree else_stmt = expr_only (else_clause);
1466 /* Notice branches to a common destination. */
1467 if (then_stmt && else_stmt
1468 && TREE_CODE (then_stmt) == GOTO_EXPR
1469 && TREE_CODE (else_stmt) == GOTO_EXPR
1470 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1472 *stmt_p = then_stmt;
1473 data->repeat = true;
1476 /* If the THEN/ELSE clause merely assigns a value to a variable or
1477 parameter which is already known to contain that value, then
1478 remove the useless THEN/ELSE clause. */
1479 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1481 if (else_stmt
1482 && TREE_CODE (else_stmt) == MODIFY_EXPR
1483 && TREE_OPERAND (else_stmt, 0) == cond
1484 && integer_zerop (TREE_OPERAND (else_stmt, 1)))
1485 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1487 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1488 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1489 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1490 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1492 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1493 ? then_stmt : else_stmt);
1494 tree *location = (TREE_CODE (cond) == EQ_EXPR
1495 ? &COND_EXPR_THEN (*stmt_p)
1496 : &COND_EXPR_ELSE (*stmt_p));
1498 if (stmt
1499 && TREE_CODE (stmt) == MODIFY_EXPR
1500 && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1501 && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1502 *location = alloc_stmt_list ();
1506 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1507 would be re-introduced during lowering. */
1508 data->last_goto = NULL;
1512 static void
1513 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1515 bool save_may_branch, save_may_throw;
1516 bool this_may_branch, this_may_throw;
1518 /* Collect may_branch and may_throw information for the body only. */
1519 save_may_branch = data->may_branch;
1520 save_may_throw = data->may_throw;
1521 data->may_branch = false;
1522 data->may_throw = false;
1523 data->last_goto = NULL;
1525 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1527 this_may_branch = data->may_branch;
1528 this_may_throw = data->may_throw;
1529 data->may_branch |= save_may_branch;
1530 data->may_throw |= save_may_throw;
1531 data->last_goto = NULL;
1533 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1535 /* If the body is empty, then we can emit the FINALLY block without
1536 the enclosing TRY_FINALLY_EXPR. */
1537 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1539 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1540 data->repeat = true;
1543 /* If the handler is empty, then we can emit the TRY block without
1544 the enclosing TRY_FINALLY_EXPR. */
1545 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1547 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1548 data->repeat = true;
1551 /* If the body neither throws, nor branches, then we can safely
1552 string the TRY and FINALLY blocks together. */
1553 else if (!this_may_branch && !this_may_throw)
1555 tree stmt = *stmt_p;
1556 *stmt_p = TREE_OPERAND (stmt, 0);
1557 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1558 data->repeat = true;
1563 static void
1564 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1566 bool save_may_throw, this_may_throw;
1567 tree_stmt_iterator i;
1568 tree stmt;
1570 /* Collect may_throw information for the body only. */
1571 save_may_throw = data->may_throw;
1572 data->may_throw = false;
1573 data->last_goto = NULL;
1575 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1577 this_may_throw = data->may_throw;
1578 data->may_throw = save_may_throw;
1580 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1581 if (!this_may_throw)
1583 if (warn_notreached)
1584 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1585 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1586 data->repeat = true;
1587 return;
1590 /* Process the catch clause specially. We may be able to tell that
1591 no exceptions propagate past this point. */
1593 this_may_throw = true;
1594 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1595 stmt = tsi_stmt (i);
1596 data->last_goto = NULL;
1598 switch (TREE_CODE (stmt))
1600 case CATCH_EXPR:
1601 for (; !tsi_end_p (i); tsi_next (&i))
1603 stmt = tsi_stmt (i);
1604 /* If we catch all exceptions, then the body does not
1605 propagate exceptions past this point. */
1606 if (CATCH_TYPES (stmt) == NULL)
1607 this_may_throw = false;
1608 data->last_goto = NULL;
1609 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1611 break;
1613 case EH_FILTER_EXPR:
1614 if (EH_FILTER_MUST_NOT_THROW (stmt))
1615 this_may_throw = false;
1616 else if (EH_FILTER_TYPES (stmt) == NULL)
1617 this_may_throw = false;
1618 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1619 break;
1621 default:
1622 /* Otherwise this is a cleanup. */
1623 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1625 /* If the cleanup is empty, then we can emit the TRY block without
1626 the enclosing TRY_CATCH_EXPR. */
1627 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1629 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1630 data->repeat = true;
1632 break;
1634 data->may_throw |= this_may_throw;
1638 static void
1639 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1641 tree block;
1643 /* First remove anything underneath the BIND_EXPR. */
1644 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1646 /* If the BIND_EXPR has no variables, then we can pull everything
1647 up one level and remove the BIND_EXPR, unless this is the toplevel
1648 BIND_EXPR for the current function or an inlined function.
1650 When this situation occurs we will want to apply this
1651 optimization again. */
1652 block = BIND_EXPR_BLOCK (*stmt_p);
1653 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1654 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1655 && (! block
1656 || ! BLOCK_ABSTRACT_ORIGIN (block)
1657 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1658 != FUNCTION_DECL)))
1660 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1661 data->repeat = true;
1666 static void
1667 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1669 tree dest = GOTO_DESTINATION (*stmt_p);
1671 data->may_branch = true;
1672 data->last_goto = NULL;
1674 /* Record the last goto expr, so that we can delete it if unnecessary. */
1675 if (TREE_CODE (dest) == LABEL_DECL)
1676 data->last_goto = stmt_p;
1680 static void
1681 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1683 tree label = LABEL_EXPR_LABEL (*stmt_p);
1685 data->has_label = true;
1687 /* We do want to jump across non-local label receiver code. */
1688 if (DECL_NONLOCAL (label))
1689 data->last_goto = NULL;
1691 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1693 *data->last_goto = build_empty_stmt ();
1694 data->repeat = true;
1697 /* ??? Add something here to delete unused labels. */
1701 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1702 decl. This allows us to eliminate redundant or useless
1703 calls to "const" functions.
1705 Gimplifier already does the same operation, but we may notice functions
1706 being const and pure once their calls has been gimplified, so we need
1707 to update the flag. */
1709 static void
1710 update_call_expr_flags (tree call)
1712 tree decl = get_callee_fndecl (call);
1713 if (!decl)
1714 return;
1715 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1716 TREE_SIDE_EFFECTS (call) = 0;
1717 if (TREE_NOTHROW (decl))
1718 TREE_NOTHROW (call) = 1;
1722 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1724 void
1725 notice_special_calls (tree t)
1727 int flags = call_expr_flags (t);
1729 if (flags & ECF_MAY_BE_ALLOCA)
1730 current_function_calls_alloca = true;
1731 if (flags & ECF_RETURNS_TWICE)
1732 current_function_calls_setjmp = true;
1736 /* Clear flags set by notice_special_calls. Used by dead code removal
1737 to update the flags. */
1739 void
1740 clear_special_calls (void)
1742 current_function_calls_alloca = false;
1743 current_function_calls_setjmp = false;
1747 static void
1748 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1750 tree t = *tp, op;
1752 switch (TREE_CODE (t))
1754 case COND_EXPR:
1755 remove_useless_stmts_cond (tp, data);
1756 break;
1758 case TRY_FINALLY_EXPR:
1759 remove_useless_stmts_tf (tp, data);
1760 break;
1762 case TRY_CATCH_EXPR:
1763 remove_useless_stmts_tc (tp, data);
1764 break;
1766 case BIND_EXPR:
1767 remove_useless_stmts_bind (tp, data);
1768 break;
1770 case GOTO_EXPR:
1771 remove_useless_stmts_goto (tp, data);
1772 break;
1774 case LABEL_EXPR:
1775 remove_useless_stmts_label (tp, data);
1776 break;
1778 case RETURN_EXPR:
1779 fold_stmt (tp);
1780 data->last_goto = NULL;
1781 data->may_branch = true;
1782 break;
1784 case CALL_EXPR:
1785 fold_stmt (tp);
1786 data->last_goto = NULL;
1787 notice_special_calls (t);
1788 update_call_expr_flags (t);
1789 if (tree_could_throw_p (t))
1790 data->may_throw = true;
1791 break;
1793 case MODIFY_EXPR:
1794 data->last_goto = NULL;
1795 fold_stmt (tp);
1796 op = get_call_expr_in (t);
1797 if (op)
1799 update_call_expr_flags (op);
1800 notice_special_calls (op);
1802 if (tree_could_throw_p (t))
1803 data->may_throw = true;
1804 break;
1806 case STATEMENT_LIST:
1808 tree_stmt_iterator i = tsi_start (t);
1809 while (!tsi_end_p (i))
1811 t = tsi_stmt (i);
1812 if (IS_EMPTY_STMT (t))
1814 tsi_delink (&i);
1815 continue;
1818 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1820 t = tsi_stmt (i);
1821 if (TREE_CODE (t) == STATEMENT_LIST)
1823 tsi_link_before (&i, t, TSI_SAME_STMT);
1824 tsi_delink (&i);
1826 else
1827 tsi_next (&i);
1830 break;
1831 case ASM_EXPR:
1832 fold_stmt (tp);
1833 data->last_goto = NULL;
1834 break;
1836 default:
1837 data->last_goto = NULL;
1838 break;
1842 static void
1843 remove_useless_stmts (void)
1845 struct rus_data data;
1847 clear_special_calls ();
1851 memset (&data, 0, sizeof (data));
1852 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1854 while (data.repeat);
1858 struct tree_opt_pass pass_remove_useless_stmts =
1860 "useless", /* name */
1861 NULL, /* gate */
1862 remove_useless_stmts, /* execute */
1863 NULL, /* sub */
1864 NULL, /* next */
1865 0, /* static_pass_number */
1866 0, /* tv_id */
1867 PROP_gimple_any, /* properties_required */
1868 0, /* properties_provided */
1869 0, /* properties_destroyed */
1870 0, /* todo_flags_start */
1871 TODO_dump_func, /* todo_flags_finish */
1872 0 /* letter */
1876 /* Remove obviously useless statements in basic block BB. */
1878 static void
1879 cfg_remove_useless_stmts_bb (basic_block bb)
1881 block_stmt_iterator bsi;
1882 tree stmt = NULL_TREE;
1883 tree cond, var = NULL_TREE, val = NULL_TREE;
1884 struct var_ann_d *ann;
1886 /* Check whether we come here from a condition, and if so, get the
1887 condition. */
1888 if (EDGE_COUNT (bb->preds) != 1
1889 || !(EDGE_PRED (bb, 0)->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
1890 return;
1892 cond = COND_EXPR_COND (last_stmt (EDGE_PRED (bb, 0)->src));
1894 if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1896 var = cond;
1897 val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
1898 ? boolean_false_node : boolean_true_node);
1900 else if (TREE_CODE (cond) == TRUTH_NOT_EXPR
1901 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1902 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL))
1904 var = TREE_OPERAND (cond, 0);
1905 val = (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE
1906 ? boolean_true_node : boolean_false_node);
1908 else
1910 if (EDGE_PRED (bb, 0)->flags & EDGE_FALSE_VALUE)
1911 cond = invert_truthvalue (cond);
1912 if (TREE_CODE (cond) == EQ_EXPR
1913 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1914 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1915 && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL
1916 || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL
1917 || TREE_CONSTANT (TREE_OPERAND (cond, 1))))
1919 var = TREE_OPERAND (cond, 0);
1920 val = TREE_OPERAND (cond, 1);
1922 else
1923 return;
1926 /* Only work for normal local variables. */
1927 ann = var_ann (var);
1928 if (!ann
1929 || ann->may_aliases
1930 || TREE_ADDRESSABLE (var))
1931 return;
1933 if (! TREE_CONSTANT (val))
1935 ann = var_ann (val);
1936 if (!ann
1937 || ann->may_aliases
1938 || TREE_ADDRESSABLE (val))
1939 return;
1942 /* Ignore floating point variables, since comparison behaves weird for
1943 them. */
1944 if (FLOAT_TYPE_P (TREE_TYPE (var)))
1945 return;
1947 for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
1949 stmt = bsi_stmt (bsi);
1951 /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
1952 which is already known to contain that value, then remove the useless
1953 THEN/ELSE clause. */
1954 if (TREE_CODE (stmt) == MODIFY_EXPR
1955 && TREE_OPERAND (stmt, 0) == var
1956 && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0))
1958 bsi_remove (&bsi);
1959 continue;
1962 /* Invalidate the var if we encounter something that could modify it.
1963 Likewise for the value it was previously set to. Note that we only
1964 consider values that are either a VAR_DECL or PARM_DECL so we
1965 can test for conflict very simply. */
1966 if (TREE_CODE (stmt) == ASM_EXPR
1967 || (TREE_CODE (stmt) == MODIFY_EXPR
1968 && (TREE_OPERAND (stmt, 0) == var
1969 || TREE_OPERAND (stmt, 0) == val)))
1970 return;
1972 bsi_next (&bsi);
1977 /* A CFG-aware version of remove_useless_stmts. */
1979 void
1980 cfg_remove_useless_stmts (void)
1982 basic_block bb;
1984 #ifdef ENABLE_CHECKING
1985 verify_flow_info ();
1986 #endif
1988 FOR_EACH_BB (bb)
1990 cfg_remove_useless_stmts_bb (bb);
1995 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
1997 static void
1998 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
2000 tree phi;
2002 /* Since this block is no longer reachable, we can just delete all
2003 of its PHI nodes. */
2004 phi = phi_nodes (bb);
2005 while (phi)
2007 tree next = PHI_CHAIN (phi);
2008 remove_phi_node (phi, NULL_TREE, bb);
2009 phi = next;
2012 /* Remove edges to BB's successors. */
2013 while (EDGE_COUNT (bb->succs) > 0)
2014 remove_edge (EDGE_SUCC (bb, 0));
2018 /* Remove statements of basic block BB. */
2020 static void
2021 remove_bb (basic_block bb)
2023 block_stmt_iterator i;
2024 source_locus loc = 0;
2026 if (dump_file)
2028 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2029 if (dump_flags & TDF_DETAILS)
2031 dump_bb (bb, dump_file, 0);
2032 fprintf (dump_file, "\n");
2036 /* Remove all the instructions in the block. */
2037 for (i = bsi_start (bb); !bsi_end_p (i);)
2039 tree stmt = bsi_stmt (i);
2040 if (TREE_CODE (stmt) == LABEL_EXPR
2041 && (FORCED_LABEL (LABEL_EXPR_LABEL (stmt))
2042 || DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt))))
2044 basic_block new_bb;
2045 block_stmt_iterator new_bsi;
2047 /* A non-reachable non-local label may still be referenced.
2048 But it no longer needs to carry the extra semantics of
2049 non-locality. */
2050 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
2052 DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)) = 0;
2053 FORCED_LABEL (LABEL_EXPR_LABEL (stmt)) = 1;
2056 new_bb = bb->prev_bb;
2057 new_bsi = bsi_start (new_bb);
2058 bsi_remove (&i);
2059 bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
2061 else
2063 release_defs (stmt);
2065 set_bb_for_stmt (stmt, NULL);
2066 bsi_remove (&i);
2069 /* Don't warn for removed gotos. Gotos are often removed due to
2070 jump threading, thus resulting in bogus warnings. Not great,
2071 since this way we lose warnings for gotos in the original
2072 program that are indeed unreachable. */
2073 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
2075 source_locus t;
2077 #ifdef USE_MAPPED_LOCATION
2078 t = EXPR_LOCATION (stmt);
2079 #else
2080 t = EXPR_LOCUS (stmt);
2081 #endif
2082 if (t && LOCATION_LINE (*t) > 0)
2083 loc = t;
2087 /* If requested, give a warning that the first statement in the
2088 block is unreachable. We walk statements backwards in the
2089 loop above, so the last statement we process is the first statement
2090 in the block. */
2091 if (warn_notreached && loc)
2092 #ifdef USE_MAPPED_LOCATION
2093 warning ("%Hwill never be executed", &loc);
2094 #else
2095 warning ("%Hwill never be executed", loc);
2096 #endif
2098 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2101 /* A list of all the noreturn calls passed to modify_stmt.
2102 cleanup_control_flow uses it to detect cases where a mid-block
2103 indirect call has been turned into a noreturn call. When this
2104 happens, all the instructions after the call are no longer
2105 reachable and must be deleted as dead. */
2107 VEC(tree) *modified_noreturn_calls;
2109 /* Try to remove superfluous control structures. */
2111 static bool
2112 cleanup_control_flow (void)
2114 basic_block bb;
2115 block_stmt_iterator bsi;
2116 bool retval = false;
2117 tree stmt;
2119 /* Detect cases where a mid-block call is now known not to return. */
2120 while (VEC_length (tree, modified_noreturn_calls))
2122 stmt = VEC_pop (tree, modified_noreturn_calls);
2123 bb = bb_for_stmt (stmt);
2124 if (bb != NULL && last_stmt (bb) != stmt && noreturn_call_p (stmt))
2125 split_block (bb, stmt);
2128 FOR_EACH_BB (bb)
2130 bsi = bsi_last (bb);
2132 if (bsi_end_p (bsi))
2133 continue;
2135 stmt = bsi_stmt (bsi);
2136 if (TREE_CODE (stmt) == COND_EXPR
2137 || TREE_CODE (stmt) == SWITCH_EXPR)
2138 retval |= cleanup_control_expr_graph (bb, bsi);
2140 /* Check for indirect calls that have been turned into
2141 noreturn calls. */
2142 if (noreturn_call_p (stmt) && remove_fallthru_edge (bb->succs))
2144 free_dominance_info (CDI_DOMINATORS);
2145 retval = true;
2148 return retval;
2152 /* Disconnect an unreachable block in the control expression starting
2153 at block BB. */
2155 static bool
2156 cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
2158 edge taken_edge;
2159 bool retval = false;
2160 tree expr = bsi_stmt (bsi), val;
2162 if (EDGE_COUNT (bb->succs) > 1)
2164 edge e;
2165 edge_iterator ei;
2167 switch (TREE_CODE (expr))
2169 case COND_EXPR:
2170 val = COND_EXPR_COND (expr);
2171 break;
2173 case SWITCH_EXPR:
2174 val = SWITCH_COND (expr);
2175 if (TREE_CODE (val) != INTEGER_CST)
2176 return false;
2177 break;
2179 default:
2180 gcc_unreachable ();
2183 taken_edge = find_taken_edge (bb, val);
2184 if (!taken_edge)
2185 return false;
2187 /* Remove all the edges except the one that is always executed. */
2188 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2190 if (e != taken_edge)
2192 taken_edge->probability += e->probability;
2193 taken_edge->count += e->count;
2194 remove_edge (e);
2195 retval = true;
2197 else
2198 ei_next (&ei);
2200 if (taken_edge->probability > REG_BR_PROB_BASE)
2201 taken_edge->probability = REG_BR_PROB_BASE;
2203 else
2204 taken_edge = EDGE_SUCC (bb, 0);
2206 bsi_remove (&bsi);
2207 taken_edge->flags = EDGE_FALLTHRU;
2209 /* We removed some paths from the cfg. */
2210 free_dominance_info (CDI_DOMINATORS);
2212 return retval;
2215 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
2217 static bool
2218 remove_fallthru_edge (VEC(edge) *ev)
2220 edge_iterator ei;
2221 edge e;
2223 FOR_EACH_EDGE (e, ei, ev)
2224 if ((e->flags & EDGE_FALLTHRU) != 0)
2226 remove_edge (e);
2227 return true;
2229 return false;
2232 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2233 predicate VAL, return the edge that will be taken out of the block.
2234 If VAL does not match a unique edge, NULL is returned. */
2236 edge
2237 find_taken_edge (basic_block bb, tree val)
2239 tree stmt;
2241 stmt = last_stmt (bb);
2243 gcc_assert (stmt);
2244 gcc_assert (is_ctrl_stmt (stmt));
2245 gcc_assert (val);
2247 if (TREE_CODE (val) != INTEGER_CST)
2248 return NULL;
2250 if (TREE_CODE (stmt) == COND_EXPR)
2251 return find_taken_edge_cond_expr (bb, val);
2253 if (TREE_CODE (stmt) == SWITCH_EXPR)
2254 return find_taken_edge_switch_expr (bb, val);
2256 gcc_unreachable ();
2260 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2261 statement, determine which of the two edges will be taken out of the
2262 block. Return NULL if either edge may be taken. */
2264 static edge
2265 find_taken_edge_cond_expr (basic_block bb, tree val)
2267 edge true_edge, false_edge;
2269 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2271 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2272 return (zero_p (val) ? false_edge : true_edge);
2275 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2276 statement, determine which edge will be taken out of the block. Return
2277 NULL if any edge may be taken. */
2279 static edge
2280 find_taken_edge_switch_expr (basic_block bb, tree val)
2282 tree switch_expr, taken_case;
2283 basic_block dest_bb;
2284 edge e;
2286 switch_expr = last_stmt (bb);
2287 taken_case = find_case_label_for_value (switch_expr, val);
2288 dest_bb = label_to_block (CASE_LABEL (taken_case));
2290 e = find_edge (bb, dest_bb);
2291 gcc_assert (e);
2292 return e;
2296 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2297 We can make optimal use here of the fact that the case labels are
2298 sorted: We can do a binary search for a case matching VAL. */
2300 static tree
2301 find_case_label_for_value (tree switch_expr, tree val)
2303 tree vec = SWITCH_LABELS (switch_expr);
2304 size_t low, high, n = TREE_VEC_LENGTH (vec);
2305 tree default_case = TREE_VEC_ELT (vec, n - 1);
2307 for (low = -1, high = n - 1; high - low > 1; )
2309 size_t i = (high + low) / 2;
2310 tree t = TREE_VEC_ELT (vec, i);
2311 int cmp;
2313 /* Cache the result of comparing CASE_LOW and val. */
2314 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2316 if (cmp > 0)
2317 high = i;
2318 else
2319 low = i;
2321 if (CASE_HIGH (t) == NULL)
2323 /* A singe-valued case label. */
2324 if (cmp == 0)
2325 return t;
2327 else
2329 /* A case range. We can only handle integer ranges. */
2330 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2331 return t;
2335 return default_case;
2339 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2340 those alternatives are equal in each of the PHI nodes, then return
2341 true, else return false. */
2343 static bool
2344 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
2346 int n1 = e1->dest_idx;
2347 int n2 = e2->dest_idx;
2348 tree phi;
2350 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2352 tree val1 = PHI_ARG_DEF (phi, n1);
2353 tree val2 = PHI_ARG_DEF (phi, n2);
2355 gcc_assert (val1 != NULL_TREE);
2356 gcc_assert (val2 != NULL_TREE);
2358 if (!operand_equal_for_phi_arg_p (val1, val2))
2359 return false;
2362 return true;
2366 /*---------------------------------------------------------------------------
2367 Debugging functions
2368 ---------------------------------------------------------------------------*/
2370 /* Dump tree-specific information of block BB to file OUTF. */
2372 void
2373 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2375 dump_generic_bb (outf, bb, indent, TDF_VOPS);
2379 /* Dump a basic block on stderr. */
2381 void
2382 debug_tree_bb (basic_block bb)
2384 dump_bb (bb, stderr, 0);
2388 /* Dump basic block with index N on stderr. */
2390 basic_block
2391 debug_tree_bb_n (int n)
2393 debug_tree_bb (BASIC_BLOCK (n));
2394 return BASIC_BLOCK (n);
2398 /* Dump the CFG on stderr.
2400 FLAGS are the same used by the tree dumping functions
2401 (see TDF_* in tree.h). */
2403 void
2404 debug_tree_cfg (int flags)
2406 dump_tree_cfg (stderr, flags);
2410 /* Dump the program showing basic block boundaries on the given FILE.
2412 FLAGS are the same used by the tree dumping functions (see TDF_* in
2413 tree.h). */
2415 void
2416 dump_tree_cfg (FILE *file, int flags)
2418 if (flags & TDF_DETAILS)
2420 const char *funcname
2421 = lang_hooks.decl_printable_name (current_function_decl, 2);
2423 fputc ('\n', file);
2424 fprintf (file, ";; Function %s\n\n", funcname);
2425 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2426 n_basic_blocks, n_edges, last_basic_block);
2428 brief_dump_cfg (file);
2429 fprintf (file, "\n");
2432 if (flags & TDF_STATS)
2433 dump_cfg_stats (file);
2435 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2439 /* Dump CFG statistics on FILE. */
2441 void
2442 dump_cfg_stats (FILE *file)
2444 static long max_num_merged_labels = 0;
2445 unsigned long size, total = 0;
2446 int n_edges;
2447 basic_block bb;
2448 const char * const fmt_str = "%-30s%-13s%12s\n";
2449 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2450 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2451 const char *funcname
2452 = lang_hooks.decl_printable_name (current_function_decl, 2);
2455 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2457 fprintf (file, "---------------------------------------------------------\n");
2458 fprintf (file, fmt_str, "", " Number of ", "Memory");
2459 fprintf (file, fmt_str, "", " instances ", "used ");
2460 fprintf (file, "---------------------------------------------------------\n");
2462 size = n_basic_blocks * sizeof (struct basic_block_def);
2463 total += size;
2464 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2465 SCALE (size), LABEL (size));
2467 n_edges = 0;
2468 FOR_EACH_BB (bb)
2469 n_edges += EDGE_COUNT (bb->succs);
2470 size = n_edges * sizeof (struct edge_def);
2471 total += size;
2472 fprintf (file, fmt_str_1, "Edges", n_edges, SCALE (size), LABEL (size));
2474 size = n_basic_blocks * sizeof (struct bb_ann_d);
2475 total += size;
2476 fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks,
2477 SCALE (size), LABEL (size));
2479 fprintf (file, "---------------------------------------------------------\n");
2480 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2481 LABEL (total));
2482 fprintf (file, "---------------------------------------------------------\n");
2483 fprintf (file, "\n");
2485 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2486 max_num_merged_labels = cfg_stats.num_merged_labels;
2488 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2489 cfg_stats.num_merged_labels, max_num_merged_labels);
2491 fprintf (file, "\n");
2495 /* Dump CFG statistics on stderr. Keep extern so that it's always
2496 linked in the final executable. */
2498 void
2499 debug_cfg_stats (void)
2501 dump_cfg_stats (stderr);
2505 /* Dump the flowgraph to a .vcg FILE. */
2507 static void
2508 tree_cfg2vcg (FILE *file)
2510 edge e;
2511 edge_iterator ei;
2512 basic_block bb;
2513 const char *funcname
2514 = lang_hooks.decl_printable_name (current_function_decl, 2);
2516 /* Write the file header. */
2517 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2518 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2519 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2521 /* Write blocks and edges. */
2522 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2524 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2525 e->dest->index);
2527 if (e->flags & EDGE_FAKE)
2528 fprintf (file, " linestyle: dotted priority: 10");
2529 else
2530 fprintf (file, " linestyle: solid priority: 100");
2532 fprintf (file, " }\n");
2534 fputc ('\n', file);
2536 FOR_EACH_BB (bb)
2538 enum tree_code head_code, end_code;
2539 const char *head_name, *end_name;
2540 int head_line = 0;
2541 int end_line = 0;
2542 tree first = first_stmt (bb);
2543 tree last = last_stmt (bb);
2545 if (first)
2547 head_code = TREE_CODE (first);
2548 head_name = tree_code_name[head_code];
2549 head_line = get_lineno (first);
2551 else
2552 head_name = "no-statement";
2554 if (last)
2556 end_code = TREE_CODE (last);
2557 end_name = tree_code_name[end_code];
2558 end_line = get_lineno (last);
2560 else
2561 end_name = "no-statement";
2563 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2564 bb->index, bb->index, head_name, head_line, end_name,
2565 end_line);
2567 FOR_EACH_EDGE (e, ei, bb->succs)
2569 if (e->dest == EXIT_BLOCK_PTR)
2570 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2571 else
2572 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2574 if (e->flags & EDGE_FAKE)
2575 fprintf (file, " priority: 10 linestyle: dotted");
2576 else
2577 fprintf (file, " priority: 100 linestyle: solid");
2579 fprintf (file, " }\n");
2582 if (bb->next_bb != EXIT_BLOCK_PTR)
2583 fputc ('\n', file);
2586 fputs ("}\n\n", file);
2591 /*---------------------------------------------------------------------------
2592 Miscellaneous helpers
2593 ---------------------------------------------------------------------------*/
2595 /* Return true if T represents a stmt that always transfers control. */
2597 bool
2598 is_ctrl_stmt (tree t)
2600 return (TREE_CODE (t) == COND_EXPR
2601 || TREE_CODE (t) == SWITCH_EXPR
2602 || TREE_CODE (t) == GOTO_EXPR
2603 || TREE_CODE (t) == RETURN_EXPR
2604 || TREE_CODE (t) == RESX_EXPR);
2608 /* Return true if T is a statement that may alter the flow of control
2609 (e.g., a call to a non-returning function). */
2611 bool
2612 is_ctrl_altering_stmt (tree t)
2614 tree call;
2616 gcc_assert (t);
2617 call = get_call_expr_in (t);
2618 if (call)
2620 /* A non-pure/const CALL_EXPR alters flow control if the current
2621 function has nonlocal labels. */
2622 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2623 return true;
2625 /* A CALL_EXPR also alters control flow if it does not return. */
2626 if (call_expr_flags (call) & ECF_NORETURN)
2627 return true;
2630 /* If a statement can throw, it alters control flow. */
2631 return tree_can_throw_internal (t);
2635 /* Return true if T is a computed goto. */
2637 bool
2638 computed_goto_p (tree t)
2640 return (TREE_CODE (t) == GOTO_EXPR
2641 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2645 /* Checks whether EXPR is a simple local goto. */
2647 bool
2648 simple_goto_p (tree expr)
2650 return (TREE_CODE (expr) == GOTO_EXPR
2651 && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL);
2655 /* Return true if T should start a new basic block. PREV_T is the
2656 statement preceding T. It is used when T is a label or a case label.
2657 Labels should only start a new basic block if their previous statement
2658 wasn't a label. Otherwise, sequence of labels would generate
2659 unnecessary basic blocks that only contain a single label. */
2661 static inline bool
2662 stmt_starts_bb_p (tree t, tree prev_t)
2664 enum tree_code code;
2666 if (t == NULL_TREE)
2667 return false;
2669 /* LABEL_EXPRs start a new basic block only if the preceding
2670 statement wasn't a label of the same type. This prevents the
2671 creation of consecutive blocks that have nothing but a single
2672 label. */
2673 code = TREE_CODE (t);
2674 if (code == LABEL_EXPR)
2676 /* Nonlocal and computed GOTO targets always start a new block. */
2677 if (code == LABEL_EXPR
2678 && (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2679 || FORCED_LABEL (LABEL_EXPR_LABEL (t))))
2680 return true;
2682 if (prev_t && TREE_CODE (prev_t) == code)
2684 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2685 return true;
2687 cfg_stats.num_merged_labels++;
2688 return false;
2690 else
2691 return true;
2694 return false;
2698 /* Return true if T should end a basic block. */
2700 bool
2701 stmt_ends_bb_p (tree t)
2703 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2707 /* Add gotos that used to be represented implicitly in the CFG. */
2709 void
2710 disband_implicit_edges (void)
2712 basic_block bb;
2713 block_stmt_iterator last;
2714 edge e;
2715 edge_iterator ei;
2716 tree stmt, label;
2718 FOR_EACH_BB (bb)
2720 last = bsi_last (bb);
2721 stmt = last_stmt (bb);
2723 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2725 /* Remove superfluous gotos from COND_EXPR branches. Moved
2726 from cfg_remove_useless_stmts here since it violates the
2727 invariants for tree--cfg correspondence and thus fits better
2728 here where we do it anyway. */
2729 e = find_edge (bb, bb->next_bb);
2730 if (e)
2732 if (e->flags & EDGE_TRUE_VALUE)
2733 COND_EXPR_THEN (stmt) = build_empty_stmt ();
2734 else if (e->flags & EDGE_FALSE_VALUE)
2735 COND_EXPR_ELSE (stmt) = build_empty_stmt ();
2736 else
2737 gcc_unreachable ();
2738 e->flags |= EDGE_FALLTHRU;
2741 continue;
2744 if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
2746 /* Remove the RETURN_EXPR if we may fall though to the exit
2747 instead. */
2748 gcc_assert (EDGE_COUNT (bb->succs) == 1);
2749 gcc_assert (EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR);
2751 if (bb->next_bb == EXIT_BLOCK_PTR
2752 && !TREE_OPERAND (stmt, 0))
2754 bsi_remove (&last);
2755 EDGE_SUCC (bb, 0)->flags |= EDGE_FALLTHRU;
2757 continue;
2760 /* There can be no fallthru edge if the last statement is a control
2761 one. */
2762 if (stmt && is_ctrl_stmt (stmt))
2763 continue;
2765 /* Find a fallthru edge and emit the goto if necessary. */
2766 FOR_EACH_EDGE (e, ei, bb->succs)
2767 if (e->flags & EDGE_FALLTHRU)
2768 break;
2770 if (!e || e->dest == bb->next_bb)
2771 continue;
2773 gcc_assert (e->dest != EXIT_BLOCK_PTR);
2774 label = tree_block_label (e->dest);
2776 stmt = build1 (GOTO_EXPR, void_type_node, label);
2777 #ifdef USE_MAPPED_LOCATION
2778 SET_EXPR_LOCATION (stmt, e->goto_locus);
2779 #else
2780 SET_EXPR_LOCUS (stmt, e->goto_locus);
2781 #endif
2782 bsi_insert_after (&last, stmt, BSI_NEW_STMT);
2783 e->flags &= ~EDGE_FALLTHRU;
2787 /* Remove block annotations and other datastructures. */
2789 void
2790 delete_tree_cfg_annotations (void)
2792 basic_block bb;
2793 if (n_basic_blocks > 0)
2794 free_blocks_annotations ();
2796 label_to_block_map = NULL;
2797 free_rbi_pool ();
2798 FOR_EACH_BB (bb)
2799 bb->rbi = NULL;
2803 /* Return the first statement in basic block BB. */
2805 tree
2806 first_stmt (basic_block bb)
2808 block_stmt_iterator i = bsi_start (bb);
2809 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2813 /* Return the last statement in basic block BB. */
2815 tree
2816 last_stmt (basic_block bb)
2818 block_stmt_iterator b = bsi_last (bb);
2819 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2823 /* Return a pointer to the last statement in block BB. */
2825 tree *
2826 last_stmt_ptr (basic_block bb)
2828 block_stmt_iterator last = bsi_last (bb);
2829 return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL;
2833 /* Return the last statement of an otherwise empty block. Return NULL
2834 if the block is totally empty, or if it contains more than one
2835 statement. */
2837 tree
2838 last_and_only_stmt (basic_block bb)
2840 block_stmt_iterator i = bsi_last (bb);
2841 tree last, prev;
2843 if (bsi_end_p (i))
2844 return NULL_TREE;
2846 last = bsi_stmt (i);
2847 bsi_prev (&i);
2848 if (bsi_end_p (i))
2849 return last;
2851 /* Empty statements should no longer appear in the instruction stream.
2852 Everything that might have appeared before should be deleted by
2853 remove_useless_stmts, and the optimizers should just bsi_remove
2854 instead of smashing with build_empty_stmt.
2856 Thus the only thing that should appear here in a block containing
2857 one executable statement is a label. */
2858 prev = bsi_stmt (i);
2859 if (TREE_CODE (prev) == LABEL_EXPR)
2860 return last;
2861 else
2862 return NULL_TREE;
2866 /* Mark BB as the basic block holding statement T. */
2868 void
2869 set_bb_for_stmt (tree t, basic_block bb)
2871 if (TREE_CODE (t) == PHI_NODE)
2872 PHI_BB (t) = bb;
2873 else if (TREE_CODE (t) == STATEMENT_LIST)
2875 tree_stmt_iterator i;
2876 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2877 set_bb_for_stmt (tsi_stmt (i), bb);
2879 else
2881 stmt_ann_t ann = get_stmt_ann (t);
2882 ann->bb = bb;
2884 /* If the statement is a label, add the label to block-to-labels map
2885 so that we can speed up edge creation for GOTO_EXPRs. */
2886 if (TREE_CODE (t) == LABEL_EXPR)
2888 int uid;
2890 t = LABEL_EXPR_LABEL (t);
2891 uid = LABEL_DECL_UID (t);
2892 if (uid == -1)
2894 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2895 if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid)
2896 VARRAY_GROW (label_to_block_map, 3 * uid / 2);
2898 else
2899 /* We're moving an existing label. Make sure that we've
2900 removed it from the old block. */
2901 gcc_assert (!bb || !VARRAY_BB (label_to_block_map, uid));
2902 VARRAY_BB (label_to_block_map, uid) = bb;
2907 /* Finds iterator for STMT. */
2909 extern block_stmt_iterator
2910 bsi_for_stmt (tree stmt)
2912 block_stmt_iterator bsi;
2914 for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
2915 if (bsi_stmt (bsi) == stmt)
2916 return bsi;
2918 gcc_unreachable ();
2921 /* Insert statement (or statement list) T before the statement
2922 pointed-to by iterator I. M specifies how to update iterator I
2923 after insertion (see enum bsi_iterator_update). */
2925 void
2926 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2928 set_bb_for_stmt (t, i->bb);
2929 tsi_link_before (&i->tsi, t, m);
2930 modify_stmt (t);
2934 /* Insert statement (or statement list) T after the statement
2935 pointed-to by iterator I. M specifies how to update iterator I
2936 after insertion (see enum bsi_iterator_update). */
2938 void
2939 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
2941 set_bb_for_stmt (t, i->bb);
2942 tsi_link_after (&i->tsi, t, m);
2943 modify_stmt (t);
2947 /* Remove the statement pointed to by iterator I. The iterator is updated
2948 to the next statement. */
2950 void
2951 bsi_remove (block_stmt_iterator *i)
2953 tree t = bsi_stmt (*i);
2954 set_bb_for_stmt (t, NULL);
2955 tsi_delink (&i->tsi);
2959 /* Move the statement at FROM so it comes right after the statement at TO. */
2961 void
2962 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
2964 tree stmt = bsi_stmt (*from);
2965 bsi_remove (from);
2966 bsi_insert_after (to, stmt, BSI_SAME_STMT);
2970 /* Move the statement at FROM so it comes right before the statement at TO. */
2972 void
2973 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
2975 tree stmt = bsi_stmt (*from);
2976 bsi_remove (from);
2977 bsi_insert_before (to, stmt, BSI_SAME_STMT);
2981 /* Move the statement at FROM to the end of basic block BB. */
2983 void
2984 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
2986 block_stmt_iterator last = bsi_last (bb);
2988 /* Have to check bsi_end_p because it could be an empty block. */
2989 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
2990 bsi_move_before (from, &last);
2991 else
2992 bsi_move_after (from, &last);
2996 /* Replace the contents of the statement pointed to by iterator BSI
2997 with STMT. If PRESERVE_EH_INFO is true, the exception handling
2998 information of the original statement is preserved. */
3000 void
3001 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
3003 int eh_region;
3004 tree orig_stmt = bsi_stmt (*bsi);
3006 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
3007 set_bb_for_stmt (stmt, bsi->bb);
3009 /* Preserve EH region information from the original statement, if
3010 requested by the caller. */
3011 if (preserve_eh_info)
3013 eh_region = lookup_stmt_eh_region (orig_stmt);
3014 if (eh_region >= 0)
3015 add_stmt_to_eh_region (stmt, eh_region);
3018 *bsi_stmt_ptr (*bsi) = stmt;
3019 modify_stmt (stmt);
3023 /* Insert the statement pointed-to by BSI into edge E. Every attempt
3024 is made to place the statement in an existing basic block, but
3025 sometimes that isn't possible. When it isn't possible, the edge is
3026 split and the statement is added to the new block.
3028 In all cases, the returned *BSI points to the correct location. The
3029 return value is true if insertion should be done after the location,
3030 or false if it should be done before the location. If new basic block
3031 has to be created, it is stored in *NEW_BB. */
3033 static bool
3034 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
3035 basic_block *new_bb)
3037 basic_block dest, src;
3038 tree tmp;
3040 dest = e->dest;
3041 restart:
3043 /* If the destination has one predecessor which has no PHI nodes,
3044 insert there. Except for the exit block.
3046 The requirement for no PHI nodes could be relaxed. Basically we
3047 would have to examine the PHIs to prove that none of them used
3048 the value set by the statement we want to insert on E. That
3049 hardly seems worth the effort. */
3050 if (EDGE_COUNT (dest->preds) == 1
3051 && ! phi_nodes (dest)
3052 && dest != EXIT_BLOCK_PTR)
3054 *bsi = bsi_start (dest);
3055 if (bsi_end_p (*bsi))
3056 return true;
3058 /* Make sure we insert after any leading labels. */
3059 tmp = bsi_stmt (*bsi);
3060 while (TREE_CODE (tmp) == LABEL_EXPR)
3062 bsi_next (bsi);
3063 if (bsi_end_p (*bsi))
3064 break;
3065 tmp = bsi_stmt (*bsi);
3068 if (bsi_end_p (*bsi))
3070 *bsi = bsi_last (dest);
3071 return true;
3073 else
3074 return false;
3077 /* If the source has one successor, the edge is not abnormal and
3078 the last statement does not end a basic block, insert there.
3079 Except for the entry block. */
3080 src = e->src;
3081 if ((e->flags & EDGE_ABNORMAL) == 0
3082 && EDGE_COUNT (src->succs) == 1
3083 && src != ENTRY_BLOCK_PTR)
3085 *bsi = bsi_last (src);
3086 if (bsi_end_p (*bsi))
3087 return true;
3089 tmp = bsi_stmt (*bsi);
3090 if (!stmt_ends_bb_p (tmp))
3091 return true;
3093 /* Insert code just before returning the value. We may need to decompose
3094 the return in the case it contains non-trivial operand. */
3095 if (TREE_CODE (tmp) == RETURN_EXPR)
3097 tree op = TREE_OPERAND (tmp, 0);
3098 if (!is_gimple_val (op))
3100 gcc_assert (TREE_CODE (op) == MODIFY_EXPR);
3101 bsi_insert_before (bsi, op, BSI_NEW_STMT);
3102 TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0);
3104 bsi_prev (bsi);
3105 return true;
3109 /* Otherwise, create a new basic block, and split this edge. */
3110 dest = split_edge (e);
3111 if (new_bb)
3112 *new_bb = dest;
3113 e = EDGE_PRED (dest, 0);
3114 goto restart;
3118 /* This routine will commit all pending edge insertions, creating any new
3119 basic blocks which are necessary. */
3121 void
3122 bsi_commit_edge_inserts (void)
3124 basic_block bb;
3125 edge e;
3126 edge_iterator ei;
3128 bsi_commit_one_edge_insert (EDGE_SUCC (ENTRY_BLOCK_PTR, 0), NULL);
3130 FOR_EACH_BB (bb)
3131 FOR_EACH_EDGE (e, ei, bb->succs)
3132 bsi_commit_one_edge_insert (e, NULL);
3136 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
3137 to this block, otherwise set it to NULL. */
3139 void
3140 bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
3142 if (new_bb)
3143 *new_bb = NULL;
3144 if (PENDING_STMT (e))
3146 block_stmt_iterator bsi;
3147 tree stmt = PENDING_STMT (e);
3149 PENDING_STMT (e) = NULL_TREE;
3151 if (tree_find_edge_insert_loc (e, &bsi, new_bb))
3152 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3153 else
3154 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3159 /* Add STMT to the pending list of edge E. No actual insertion is
3160 made until a call to bsi_commit_edge_inserts () is made. */
3162 void
3163 bsi_insert_on_edge (edge e, tree stmt)
3165 append_to_statement_list (stmt, &PENDING_STMT (e));
3168 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new
3169 block has to be created, it is returned. */
3171 basic_block
3172 bsi_insert_on_edge_immediate (edge e, tree stmt)
3174 block_stmt_iterator bsi;
3175 basic_block new_bb = NULL;
3177 gcc_assert (!PENDING_STMT (e));
3179 if (tree_find_edge_insert_loc (e, &bsi, &new_bb))
3180 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3181 else
3182 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3184 return new_bb;
3187 /*---------------------------------------------------------------------------
3188 Tree specific functions for CFG manipulation
3189 ---------------------------------------------------------------------------*/
3191 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
3193 static void
3194 reinstall_phi_args (edge new_edge, edge old_edge)
3196 tree var, phi;
3198 if (!PENDING_STMT (old_edge))
3199 return;
3201 for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest);
3202 var && phi;
3203 var = TREE_CHAIN (var), phi = PHI_CHAIN (phi))
3205 tree result = TREE_PURPOSE (var);
3206 tree arg = TREE_VALUE (var);
3208 gcc_assert (result == PHI_RESULT (phi));
3210 add_phi_arg (phi, arg, new_edge);
3213 PENDING_STMT (old_edge) = NULL;
3216 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3217 Abort on abnormal edges. */
3219 static basic_block
3220 tree_split_edge (edge edge_in)
3222 basic_block new_bb, after_bb, dest, src;
3223 edge new_edge, e;
3225 /* Abnormal edges cannot be split. */
3226 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
3228 src = edge_in->src;
3229 dest = edge_in->dest;
3231 /* Place the new block in the block list. Try to keep the new block
3232 near its "logical" location. This is of most help to humans looking
3233 at debugging dumps. */
3234 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
3235 after_bb = edge_in->src;
3236 else
3237 after_bb = dest->prev_bb;
3239 new_bb = create_empty_bb (after_bb);
3240 new_bb->frequency = EDGE_FREQUENCY (edge_in);
3241 new_bb->count = edge_in->count;
3242 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
3243 new_edge->probability = REG_BR_PROB_BASE;
3244 new_edge->count = edge_in->count;
3246 e = redirect_edge_and_branch (edge_in, new_bb);
3247 gcc_assert (e);
3248 reinstall_phi_args (new_edge, e);
3250 return new_bb;
3254 /* Return true when BB has label LABEL in it. */
3256 static bool
3257 has_label_p (basic_block bb, tree label)
3259 block_stmt_iterator bsi;
3261 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3263 tree stmt = bsi_stmt (bsi);
3265 if (TREE_CODE (stmt) != LABEL_EXPR)
3266 return false;
3267 if (LABEL_EXPR_LABEL (stmt) == label)
3268 return true;
3270 return false;
3274 /* Callback for walk_tree, check that all elements with address taken are
3275 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3276 inside a PHI node. */
3278 static tree
3279 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3281 tree t = *tp, x;
3282 bool in_phi = (data != NULL);
3284 if (TYPE_P (t))
3285 *walk_subtrees = 0;
3287 /* Check operand N for being valid GIMPLE and give error MSG if not.
3288 We check for constants explicitly since they are not considered
3289 gimple invariants if they overflowed. */
3290 #define CHECK_OP(N, MSG) \
3291 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3292 && !is_gimple_val (TREE_OPERAND (t, N))) \
3293 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3295 switch (TREE_CODE (t))
3297 case SSA_NAME:
3298 if (SSA_NAME_IN_FREE_LIST (t))
3300 error ("SSA name in freelist but still referenced");
3301 return *tp;
3303 break;
3305 case MODIFY_EXPR:
3306 x = TREE_OPERAND (t, 0);
3307 if (TREE_CODE (x) == BIT_FIELD_REF
3308 && is_gimple_reg (TREE_OPERAND (x, 0)))
3310 error ("GIMPLE register modified with BIT_FIELD_REF");
3311 return t;
3313 break;
3315 case ADDR_EXPR:
3316 /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
3317 dead PHIs that take the address of something. But if the PHI
3318 result is dead, the fact that it takes the address of anything
3319 is irrelevant. Because we can not tell from here if a PHI result
3320 is dead, we just skip this check for PHIs altogether. This means
3321 we may be missing "valid" checks, but what can you do?
3322 This was PR19217. */
3323 if (in_phi)
3324 break;
3326 /* Skip any references (they will be checked when we recurse down the
3327 tree) and ensure that any variable used as a prefix is marked
3328 addressable. */
3329 for (x = TREE_OPERAND (t, 0);
3330 handled_component_p (x);
3331 x = TREE_OPERAND (x, 0))
3334 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3335 return NULL;
3336 if (!TREE_ADDRESSABLE (x))
3338 error ("address taken, but ADDRESSABLE bit not set");
3339 return x;
3341 break;
3343 case COND_EXPR:
3344 x = COND_EXPR_COND (t);
3345 if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3347 error ("non-boolean used in condition");
3348 return x;
3350 break;
3352 case NOP_EXPR:
3353 case CONVERT_EXPR:
3354 case FIX_TRUNC_EXPR:
3355 case FIX_CEIL_EXPR:
3356 case FIX_FLOOR_EXPR:
3357 case FIX_ROUND_EXPR:
3358 case FLOAT_EXPR:
3359 case NEGATE_EXPR:
3360 case ABS_EXPR:
3361 case BIT_NOT_EXPR:
3362 case NON_LVALUE_EXPR:
3363 case TRUTH_NOT_EXPR:
3364 CHECK_OP (0, "Invalid operand to unary operator");
3365 break;
3367 case REALPART_EXPR:
3368 case IMAGPART_EXPR:
3369 case COMPONENT_REF:
3370 case ARRAY_REF:
3371 case ARRAY_RANGE_REF:
3372 case BIT_FIELD_REF:
3373 case VIEW_CONVERT_EXPR:
3374 /* We have a nest of references. Verify that each of the operands
3375 that determine where to reference is either a constant or a variable,
3376 verify that the base is valid, and then show we've already checked
3377 the subtrees. */
3378 while (handled_component_p (t))
3380 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3381 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3382 else if (TREE_CODE (t) == ARRAY_REF
3383 || TREE_CODE (t) == ARRAY_RANGE_REF)
3385 CHECK_OP (1, "Invalid array index.");
3386 if (TREE_OPERAND (t, 2))
3387 CHECK_OP (2, "Invalid array lower bound.");
3388 if (TREE_OPERAND (t, 3))
3389 CHECK_OP (3, "Invalid array stride.");
3391 else if (TREE_CODE (t) == BIT_FIELD_REF)
3393 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3394 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3397 t = TREE_OPERAND (t, 0);
3400 if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t))
3402 error ("Invalid reference prefix.");
3403 return t;
3405 *walk_subtrees = 0;
3406 break;
3408 case LT_EXPR:
3409 case LE_EXPR:
3410 case GT_EXPR:
3411 case GE_EXPR:
3412 case EQ_EXPR:
3413 case NE_EXPR:
3414 case UNORDERED_EXPR:
3415 case ORDERED_EXPR:
3416 case UNLT_EXPR:
3417 case UNLE_EXPR:
3418 case UNGT_EXPR:
3419 case UNGE_EXPR:
3420 case UNEQ_EXPR:
3421 case LTGT_EXPR:
3422 case PLUS_EXPR:
3423 case MINUS_EXPR:
3424 case MULT_EXPR:
3425 case TRUNC_DIV_EXPR:
3426 case CEIL_DIV_EXPR:
3427 case FLOOR_DIV_EXPR:
3428 case ROUND_DIV_EXPR:
3429 case TRUNC_MOD_EXPR:
3430 case CEIL_MOD_EXPR:
3431 case FLOOR_MOD_EXPR:
3432 case ROUND_MOD_EXPR:
3433 case RDIV_EXPR:
3434 case EXACT_DIV_EXPR:
3435 case MIN_EXPR:
3436 case MAX_EXPR:
3437 case LSHIFT_EXPR:
3438 case RSHIFT_EXPR:
3439 case LROTATE_EXPR:
3440 case RROTATE_EXPR:
3441 case BIT_IOR_EXPR:
3442 case BIT_XOR_EXPR:
3443 case BIT_AND_EXPR:
3444 CHECK_OP (0, "Invalid operand to binary operator");
3445 CHECK_OP (1, "Invalid operand to binary operator");
3446 break;
3448 default:
3449 break;
3451 return NULL;
3453 #undef CHECK_OP
3457 /* Verify STMT, return true if STMT is not in GIMPLE form.
3458 TODO: Implement type checking. */
3460 static bool
3461 verify_stmt (tree stmt, bool last_in_block)
3463 tree addr;
3465 if (!is_gimple_stmt (stmt))
3467 error ("Is not a valid GIMPLE statement.");
3468 goto fail;
3471 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
3472 if (addr)
3474 debug_generic_stmt (addr);
3475 return true;
3478 /* If the statement is marked as part of an EH region, then it is
3479 expected that the statement could throw. Verify that when we
3480 have optimizations that simplify statements such that we prove
3481 that they cannot throw, that we update other data structures
3482 to match. */
3483 if (lookup_stmt_eh_region (stmt) >= 0)
3485 if (!tree_could_throw_p (stmt))
3487 error ("Statement marked for throw, but doesn%'t.");
3488 goto fail;
3490 if (!last_in_block && tree_can_throw_internal (stmt))
3492 error ("Statement marked for throw in middle of block.");
3493 goto fail;
3497 return false;
3499 fail:
3500 debug_generic_stmt (stmt);
3501 return true;
3505 /* Return true when the T can be shared. */
3507 static bool
3508 tree_node_can_be_shared (tree t)
3510 if (IS_TYPE_OR_DECL_P (t)
3511 /* We check for constants explicitly since they are not considered
3512 gimple invariants if they overflowed. */
3513 || CONSTANT_CLASS_P (t)
3514 || is_gimple_min_invariant (t)
3515 || TREE_CODE (t) == SSA_NAME
3516 || t == error_mark_node)
3517 return true;
3519 if (TREE_CODE (t) == CASE_LABEL_EXPR)
3520 return true;
3522 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3523 /* We check for constants explicitly since they are not considered
3524 gimple invariants if they overflowed. */
3525 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 1))
3526 || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
3527 || (TREE_CODE (t) == COMPONENT_REF
3528 || TREE_CODE (t) == REALPART_EXPR
3529 || TREE_CODE (t) == IMAGPART_EXPR))
3530 t = TREE_OPERAND (t, 0);
3532 if (DECL_P (t))
3533 return true;
3535 return false;
3539 /* Called via walk_trees. Verify tree sharing. */
3541 static tree
3542 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
3544 htab_t htab = (htab_t) data;
3545 void **slot;
3547 if (tree_node_can_be_shared (*tp))
3549 *walk_subtrees = false;
3550 return NULL;
3553 slot = htab_find_slot (htab, *tp, INSERT);
3554 if (*slot)
3555 return *slot;
3556 *slot = *tp;
3558 return NULL;
3562 /* Verify the GIMPLE statement chain. */
3564 void
3565 verify_stmts (void)
3567 basic_block bb;
3568 block_stmt_iterator bsi;
3569 bool err = false;
3570 htab_t htab;
3571 tree addr;
3573 timevar_push (TV_TREE_STMT_VERIFY);
3574 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3576 FOR_EACH_BB (bb)
3578 tree phi;
3579 int i;
3581 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3583 int phi_num_args = PHI_NUM_ARGS (phi);
3585 for (i = 0; i < phi_num_args; i++)
3587 tree t = PHI_ARG_DEF (phi, i);
3588 tree addr;
3590 /* Addressable variables do have SSA_NAMEs but they
3591 are not considered gimple values. */
3592 if (TREE_CODE (t) != SSA_NAME
3593 && TREE_CODE (t) != FUNCTION_DECL
3594 && !is_gimple_val (t))
3596 error ("PHI def is not a GIMPLE value");
3597 debug_generic_stmt (phi);
3598 debug_generic_stmt (t);
3599 err |= true;
3602 addr = walk_tree (&t, verify_expr, (void *) 1, NULL);
3603 if (addr)
3605 debug_generic_stmt (addr);
3606 err |= true;
3609 addr = walk_tree (&t, verify_node_sharing, htab, NULL);
3610 if (addr)
3612 error ("Incorrect sharing of tree nodes");
3613 debug_generic_stmt (phi);
3614 debug_generic_stmt (addr);
3615 err |= true;
3620 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
3622 tree stmt = bsi_stmt (bsi);
3623 bsi_next (&bsi);
3624 err |= verify_stmt (stmt, bsi_end_p (bsi));
3625 addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
3626 if (addr)
3628 error ("Incorrect sharing of tree nodes");
3629 debug_generic_stmt (stmt);
3630 debug_generic_stmt (addr);
3631 err |= true;
3636 if (err)
3637 internal_error ("verify_stmts failed.");
3639 htab_delete (htab);
3640 timevar_pop (TV_TREE_STMT_VERIFY);
3644 /* Verifies that the flow information is OK. */
3646 static int
3647 tree_verify_flow_info (void)
3649 int err = 0;
3650 basic_block bb;
3651 block_stmt_iterator bsi;
3652 tree stmt;
3653 edge e;
3654 edge_iterator ei;
3656 if (ENTRY_BLOCK_PTR->stmt_list)
3658 error ("ENTRY_BLOCK has a statement list associated with it\n");
3659 err = 1;
3662 if (EXIT_BLOCK_PTR->stmt_list)
3664 error ("EXIT_BLOCK has a statement list associated with it\n");
3665 err = 1;
3668 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3669 if (e->flags & EDGE_FALLTHRU)
3671 error ("Fallthru to exit from bb %d\n", e->src->index);
3672 err = 1;
3675 FOR_EACH_BB (bb)
3677 bool found_ctrl_stmt = false;
3679 stmt = NULL_TREE;
3681 /* Skip labels on the start of basic block. */
3682 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3684 tree prev_stmt = stmt;
3686 stmt = bsi_stmt (bsi);
3688 if (TREE_CODE (stmt) != LABEL_EXPR)
3689 break;
3691 if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
3693 error ("Nonlocal label %s is not first "
3694 "in a sequence of labels in bb %d",
3695 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3696 bb->index);
3697 err = 1;
3700 if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
3702 error ("Label %s to block does not match in bb %d\n",
3703 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3704 bb->index);
3705 err = 1;
3708 if (decl_function_context (LABEL_EXPR_LABEL (stmt))
3709 != current_function_decl)
3711 error ("Label %s has incorrect context in bb %d\n",
3712 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3713 bb->index);
3714 err = 1;
3718 /* Verify that body of basic block BB is free of control flow. */
3719 for (; !bsi_end_p (bsi); bsi_next (&bsi))
3721 tree stmt = bsi_stmt (bsi);
3723 if (found_ctrl_stmt)
3725 error ("Control flow in the middle of basic block %d\n",
3726 bb->index);
3727 err = 1;
3730 if (stmt_ends_bb_p (stmt))
3731 found_ctrl_stmt = true;
3733 if (TREE_CODE (stmt) == LABEL_EXPR)
3735 error ("Label %s in the middle of basic block %d\n",
3736 IDENTIFIER_POINTER (DECL_NAME (stmt)),
3737 bb->index);
3738 err = 1;
3741 bsi = bsi_last (bb);
3742 if (bsi_end_p (bsi))
3743 continue;
3745 stmt = bsi_stmt (bsi);
3747 if (is_ctrl_stmt (stmt))
3749 FOR_EACH_EDGE (e, ei, bb->succs)
3750 if (e->flags & EDGE_FALLTHRU)
3752 error ("Fallthru edge after a control statement in bb %d \n",
3753 bb->index);
3754 err = 1;
3758 switch (TREE_CODE (stmt))
3760 case COND_EXPR:
3762 edge true_edge;
3763 edge false_edge;
3764 if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR
3765 || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR)
3767 error ("Structured COND_EXPR at the end of bb %d\n", bb->index);
3768 err = 1;
3771 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
3773 if (!true_edge || !false_edge
3774 || !(true_edge->flags & EDGE_TRUE_VALUE)
3775 || !(false_edge->flags & EDGE_FALSE_VALUE)
3776 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3777 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3778 || EDGE_COUNT (bb->succs) >= 3)
3780 error ("Wrong outgoing edge flags at end of bb %d\n",
3781 bb->index);
3782 err = 1;
3785 if (!has_label_p (true_edge->dest,
3786 GOTO_DESTINATION (COND_EXPR_THEN (stmt))))
3788 error ("%<then%> label does not match edge at end of bb %d\n",
3789 bb->index);
3790 err = 1;
3793 if (!has_label_p (false_edge->dest,
3794 GOTO_DESTINATION (COND_EXPR_ELSE (stmt))))
3796 error ("%<else%> label does not match edge at end of bb %d\n",
3797 bb->index);
3798 err = 1;
3801 break;
3803 case GOTO_EXPR:
3804 if (simple_goto_p (stmt))
3806 error ("Explicit goto at end of bb %d\n", bb->index);
3807 err = 1;
3809 else
3811 /* FIXME. We should double check that the labels in the
3812 destination blocks have their address taken. */
3813 FOR_EACH_EDGE (e, ei, bb->succs)
3814 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
3815 | EDGE_FALSE_VALUE))
3816 || !(e->flags & EDGE_ABNORMAL))
3818 error ("Wrong outgoing edge flags at end of bb %d\n",
3819 bb->index);
3820 err = 1;
3823 break;
3825 case RETURN_EXPR:
3826 if (EDGE_COUNT (bb->succs) != 1
3827 || (EDGE_SUCC (bb, 0)->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3828 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3830 error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
3831 err = 1;
3833 if (EDGE_SUCC (bb, 0)->dest != EXIT_BLOCK_PTR)
3835 error ("Return edge does not point to exit in bb %d\n",
3836 bb->index);
3837 err = 1;
3839 break;
3841 case SWITCH_EXPR:
3843 tree prev;
3844 edge e;
3845 size_t i, n;
3846 tree vec;
3848 vec = SWITCH_LABELS (stmt);
3849 n = TREE_VEC_LENGTH (vec);
3851 /* Mark all the destination basic blocks. */
3852 for (i = 0; i < n; ++i)
3854 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3855 basic_block label_bb = label_to_block (lab);
3857 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
3858 label_bb->aux = (void *)1;
3861 /* Verify that the case labels are sorted. */
3862 prev = TREE_VEC_ELT (vec, 0);
3863 for (i = 1; i < n - 1; ++i)
3865 tree c = TREE_VEC_ELT (vec, i);
3866 if (! CASE_LOW (c))
3868 error ("Found default case not at end of case vector");
3869 err = 1;
3870 continue;
3872 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
3874 error ("Case labels not sorted:\n ");
3875 print_generic_expr (stderr, prev, 0);
3876 fprintf (stderr," is greater than ");
3877 print_generic_expr (stderr, c, 0);
3878 fprintf (stderr," but comes before it.\n");
3879 err = 1;
3881 prev = c;
3883 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
3885 error ("No default case found at end of case vector");
3886 err = 1;
3889 FOR_EACH_EDGE (e, ei, bb->succs)
3891 if (!e->dest->aux)
3893 error ("Extra outgoing edge %d->%d\n",
3894 bb->index, e->dest->index);
3895 err = 1;
3897 e->dest->aux = (void *)2;
3898 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
3899 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3901 error ("Wrong outgoing edge flags at end of bb %d\n",
3902 bb->index);
3903 err = 1;
3907 /* Check that we have all of them. */
3908 for (i = 0; i < n; ++i)
3910 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3911 basic_block label_bb = label_to_block (lab);
3913 if (label_bb->aux != (void *)2)
3915 error ("Missing edge %i->%i",
3916 bb->index, label_bb->index);
3917 err = 1;
3921 FOR_EACH_EDGE (e, ei, bb->succs)
3922 e->dest->aux = (void *)0;
3925 default: ;
3929 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
3930 verify_dominators (CDI_DOMINATORS);
3932 return err;
3936 /* Updates phi nodes after creating a forwarder block joined
3937 by edge FALLTHRU. */
3939 static void
3940 tree_make_forwarder_block (edge fallthru)
3942 edge e;
3943 edge_iterator ei;
3944 basic_block dummy, bb;
3945 tree phi, new_phi, var;
3947 dummy = fallthru->src;
3948 bb = fallthru->dest;
3950 if (EDGE_COUNT (bb->preds) == 1)
3951 return;
3953 /* If we redirected a branch we must create new phi nodes at the
3954 start of BB. */
3955 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
3957 var = PHI_RESULT (phi);
3958 new_phi = create_phi_node (var, bb);
3959 SSA_NAME_DEF_STMT (var) = new_phi;
3960 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
3961 add_phi_arg (new_phi, PHI_RESULT (phi), fallthru);
3964 /* Ensure that the PHI node chain is in the same order. */
3965 set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
3967 /* Add the arguments we have stored on edges. */
3968 FOR_EACH_EDGE (e, ei, bb->preds)
3970 if (e == fallthru)
3971 continue;
3973 flush_pending_stmts (e);
3978 /* Return true if basic block BB does nothing except pass control
3979 flow to another block and that we can safely insert a label at
3980 the start of the successor block.
3982 As a precondition, we require that BB be not equal to
3983 ENTRY_BLOCK_PTR. */
3985 static bool
3986 tree_forwarder_block_p (basic_block bb, bool phi_wanted)
3988 block_stmt_iterator bsi;
3990 /* BB must have a single outgoing edge. */
3991 if (EDGE_COUNT (bb->succs) != 1
3992 /* If PHI_WANTED is false, BB must not have any PHI nodes.
3993 Otherwise, BB must have PHI nodes. */
3994 || (phi_nodes (bb) != NULL_TREE) != phi_wanted
3995 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
3996 || EDGE_SUCC (bb, 0)->dest == EXIT_BLOCK_PTR
3997 /* Nor should this be an infinite loop. */
3998 || EDGE_SUCC (bb, 0)->dest == bb
3999 /* BB may not have an abnormal outgoing edge. */
4000 || (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL))
4001 return false;
4003 #if ENABLE_CHECKING
4004 gcc_assert (bb != ENTRY_BLOCK_PTR);
4005 #endif
4007 /* Now walk through the statements backward. We can ignore labels,
4008 anything else means this is not a forwarder block. */
4009 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4011 tree stmt = bsi_stmt (bsi);
4013 switch (TREE_CODE (stmt))
4015 case LABEL_EXPR:
4016 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
4017 return false;
4018 break;
4020 default:
4021 return false;
4025 if (find_edge (ENTRY_BLOCK_PTR, bb))
4026 return false;
4028 return true;
4031 /* Return true if BB has at least one abnormal incoming edge. */
4033 static inline bool
4034 has_abnormal_incoming_edge_p (basic_block bb)
4036 edge e;
4037 edge_iterator ei;
4039 FOR_EACH_EDGE (e, ei, bb->preds)
4040 if (e->flags & EDGE_ABNORMAL)
4041 return true;
4043 return false;
4046 /* Removes forwarder block BB. Returns false if this failed. If a new
4047 forwarder block is created due to redirection of edges, it is
4048 stored to worklist. */
4050 static bool
4051 remove_forwarder_block (basic_block bb, basic_block **worklist)
4053 edge succ = EDGE_SUCC (bb, 0), e, s;
4054 basic_block dest = succ->dest;
4055 tree label;
4056 tree phi;
4057 edge_iterator ei;
4058 block_stmt_iterator bsi, bsi_to;
4059 bool seen_abnormal_edge = false;
4061 /* We check for infinite loops already in tree_forwarder_block_p.
4062 However it may happen that the infinite loop is created
4063 afterwards due to removal of forwarders. */
4064 if (dest == bb)
4065 return false;
4067 /* If the destination block consists of a nonlocal label, do not merge
4068 it. */
4069 label = first_stmt (dest);
4070 if (label
4071 && TREE_CODE (label) == LABEL_EXPR
4072 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4073 return false;
4075 /* If there is an abnormal edge to basic block BB, but not into
4076 dest, problems might occur during removal of the phi node at out
4077 of ssa due to overlapping live ranges of registers.
4079 If there is an abnormal edge in DEST, the problems would occur
4080 anyway since cleanup_dead_labels would then merge the labels for
4081 two different eh regions, and rest of exception handling code
4082 does not like it.
4084 So if there is an abnormal edge to BB, proceed only if there is
4085 no abnormal edge to DEST and there are no phi nodes in DEST. */
4086 if (has_abnormal_incoming_edge_p (bb))
4088 seen_abnormal_edge = true;
4090 if (has_abnormal_incoming_edge_p (dest)
4091 || phi_nodes (dest) != NULL_TREE)
4092 return false;
4095 /* If there are phi nodes in DEST, and some of the blocks that are
4096 predecessors of BB are also predecessors of DEST, check that the
4097 phi node arguments match. */
4098 if (phi_nodes (dest))
4100 FOR_EACH_EDGE (e, ei, bb->preds)
4102 s = find_edge (e->src, dest);
4103 if (!s)
4104 continue;
4106 if (!phi_alternatives_equal (dest, succ, s))
4107 return false;
4111 /* Redirect the edges. */
4112 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4114 if (e->flags & EDGE_ABNORMAL)
4116 /* If there is an abnormal edge, redirect it anyway, and
4117 move the labels to the new block to make it legal. */
4118 s = redirect_edge_succ_nodup (e, dest);
4120 else
4121 s = redirect_edge_and_branch (e, dest);
4123 if (s == e)
4125 /* Create arguments for the phi nodes, since the edge was not
4126 here before. */
4127 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4128 add_phi_arg (phi, PHI_ARG_DEF (phi, succ->dest_idx), s);
4130 else
4132 /* The source basic block might become a forwarder. We know
4133 that it was not a forwarder before, since it used to have
4134 at least two outgoing edges, so we may just add it to
4135 worklist. */
4136 if (tree_forwarder_block_p (s->src, false))
4137 *(*worklist)++ = s->src;
4141 if (seen_abnormal_edge)
4143 /* Move the labels to the new block, so that the redirection of
4144 the abnormal edges works. */
4146 bsi_to = bsi_start (dest);
4147 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
4149 label = bsi_stmt (bsi);
4150 gcc_assert (TREE_CODE (label) == LABEL_EXPR);
4151 bsi_remove (&bsi);
4152 bsi_insert_before (&bsi_to, label, BSI_CONTINUE_LINKING);
4156 /* Update the dominators. */
4157 if (dom_info_available_p (CDI_DOMINATORS))
4159 basic_block dom, dombb, domdest;
4161 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4162 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4163 if (domdest == bb)
4165 /* Shortcut to avoid calling (relatively expensive)
4166 nearest_common_dominator unless necessary. */
4167 dom = dombb;
4169 else
4170 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4172 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4175 /* And kill the forwarder block. */
4176 delete_basic_block (bb);
4178 return true;
4181 /* Removes forwarder blocks. */
4183 static bool
4184 cleanup_forwarder_blocks (void)
4186 basic_block bb;
4187 bool changed = false;
4188 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4189 basic_block *current = worklist;
4191 FOR_EACH_BB (bb)
4193 if (tree_forwarder_block_p (bb, false))
4194 *current++ = bb;
4197 while (current != worklist)
4199 bb = *--current;
4200 changed |= remove_forwarder_block (bb, &current);
4203 free (worklist);
4204 return changed;
4207 /* Merge the PHI nodes at BB into those at BB's sole successor. */
4209 static void
4210 remove_forwarder_block_with_phi (basic_block bb)
4212 edge succ = EDGE_SUCC (bb, 0);
4213 basic_block dest = succ->dest;
4214 tree label;
4215 basic_block dombb, domdest, dom;
4217 /* We check for infinite loops already in tree_forwarder_block_p.
4218 However it may happen that the infinite loop is created
4219 afterwards due to removal of forwarders. */
4220 if (dest == bb)
4221 return;
4223 /* If the destination block consists of a nonlocal label, do not
4224 merge it. */
4225 label = first_stmt (dest);
4226 if (label
4227 && TREE_CODE (label) == LABEL_EXPR
4228 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4229 return;
4231 /* Redirect each incoming edge to BB to DEST. */
4232 while (EDGE_COUNT (bb->preds) > 0)
4234 edge e = EDGE_PRED (bb, 0), s;
4235 tree phi;
4237 s = find_edge (e->src, dest);
4238 if (s)
4240 /* We already have an edge S from E->src to DEST. If S and
4241 E->dest's sole successor edge have the same PHI arguments
4242 at DEST, redirect S to DEST. */
4243 if (phi_alternatives_equal (dest, s, succ))
4245 e = redirect_edge_and_branch (e, dest);
4246 PENDING_STMT (e) = NULL_TREE;
4247 continue;
4250 /* PHI arguments are different. Create a forwarder block by
4251 splitting E so that we can merge PHI arguments on E to
4252 DEST. */
4253 e = EDGE_SUCC (split_edge (e), 0);
4256 s = redirect_edge_and_branch (e, dest);
4258 /* redirect_edge_and_branch must not create a new edge. */
4259 gcc_assert (s == e);
4261 /* Add to the PHI nodes at DEST each PHI argument removed at the
4262 destination of E. */
4263 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4265 tree def = PHI_ARG_DEF (phi, succ->dest_idx);
4267 if (TREE_CODE (def) == SSA_NAME)
4269 tree var;
4271 /* If DEF is one of the results of PHI nodes removed during
4272 redirection, replace it with the PHI argument that used
4273 to be on E. */
4274 for (var = PENDING_STMT (e); var; var = TREE_CHAIN (var))
4276 tree old_arg = TREE_PURPOSE (var);
4277 tree new_arg = TREE_VALUE (var);
4279 if (def == old_arg)
4281 def = new_arg;
4282 break;
4287 add_phi_arg (phi, def, s);
4290 PENDING_STMT (e) = NULL;
4293 /* Update the dominators. */
4294 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4295 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4296 if (domdest == bb)
4298 /* Shortcut to avoid calling (relatively expensive)
4299 nearest_common_dominator unless necessary. */
4300 dom = dombb;
4302 else
4303 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4305 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4307 /* Remove BB since all of BB's incoming edges have been redirected
4308 to DEST. */
4309 delete_basic_block (bb);
4312 /* This pass merges PHI nodes if one feeds into another. For example,
4313 suppose we have the following:
4315 goto <bb 9> (<L9>);
4317 <L8>:;
4318 tem_17 = foo ();
4320 # tem_6 = PHI <tem_17(8), tem_23(7)>;
4321 <L9>:;
4323 # tem_3 = PHI <tem_6(9), tem_2(5)>;
4324 <L10>:;
4326 Then we merge the first PHI node into the second one like so:
4328 goto <bb 9> (<L10>);
4330 <L8>:;
4331 tem_17 = foo ();
4333 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
4334 <L10>:;
4337 static void
4338 merge_phi_nodes (void)
4340 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4341 basic_block *current = worklist;
4342 basic_block bb;
4344 calculate_dominance_info (CDI_DOMINATORS);
4346 /* Find all PHI nodes that we may be able to merge. */
4347 FOR_EACH_BB (bb)
4349 basic_block dest;
4351 /* Look for a forwarder block with PHI nodes. */
4352 if (!tree_forwarder_block_p (bb, true))
4353 continue;
4355 dest = EDGE_SUCC (bb, 0)->dest;
4357 /* We have to feed into another basic block with PHI
4358 nodes. */
4359 if (!phi_nodes (dest)
4360 /* We don't want to deal with a basic block with
4361 abnormal edges. */
4362 || has_abnormal_incoming_edge_p (bb))
4363 continue;
4365 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
4367 /* If BB does not dominate DEST, then the PHI nodes at
4368 DEST must be the only users of the results of the PHI
4369 nodes at BB. */
4370 *current++ = bb;
4374 /* Now let's drain WORKLIST. */
4375 while (current != worklist)
4377 bb = *--current;
4378 remove_forwarder_block_with_phi (bb);
4381 free (worklist);
4384 static bool
4385 gate_merge_phi (void)
4387 return 1;
4390 struct tree_opt_pass pass_merge_phi = {
4391 "mergephi", /* name */
4392 gate_merge_phi, /* gate */
4393 merge_phi_nodes, /* execute */
4394 NULL, /* sub */
4395 NULL, /* next */
4396 0, /* static_pass_number */
4397 TV_TREE_MERGE_PHI, /* tv_id */
4398 PROP_cfg | PROP_ssa, /* properties_required */
4399 0, /* properties_provided */
4400 0, /* properties_destroyed */
4401 0, /* todo_flags_start */
4402 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */
4403 | TODO_verify_ssa,
4404 0 /* letter */
4407 /* Return a non-special label in the head of basic block BLOCK.
4408 Create one if it doesn't exist. */
4410 tree
4411 tree_block_label (basic_block bb)
4413 block_stmt_iterator i, s = bsi_start (bb);
4414 bool first = true;
4415 tree label, stmt;
4417 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4419 stmt = bsi_stmt (i);
4420 if (TREE_CODE (stmt) != LABEL_EXPR)
4421 break;
4422 label = LABEL_EXPR_LABEL (stmt);
4423 if (!DECL_NONLOCAL (label))
4425 if (!first)
4426 bsi_move_before (&i, &s);
4427 return label;
4431 label = create_artificial_label ();
4432 stmt = build1 (LABEL_EXPR, void_type_node, label);
4433 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4434 return label;
4438 /* Attempt to perform edge redirection by replacing a possibly complex
4439 jump instruction by a goto or by removing the jump completely.
4440 This can apply only if all edges now point to the same block. The
4441 parameters and return values are equivalent to
4442 redirect_edge_and_branch. */
4444 static edge
4445 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4447 basic_block src = e->src;
4448 block_stmt_iterator b;
4449 tree stmt;
4451 /* We can replace or remove a complex jump only when we have exactly
4452 two edges. */
4453 if (EDGE_COUNT (src->succs) != 2
4454 /* Verify that all targets will be TARGET. Specifically, the
4455 edge that is not E must also go to TARGET. */
4456 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4457 return NULL;
4459 b = bsi_last (src);
4460 if (bsi_end_p (b))
4461 return NULL;
4462 stmt = bsi_stmt (b);
4464 if (TREE_CODE (stmt) == COND_EXPR
4465 || TREE_CODE (stmt) == SWITCH_EXPR)
4467 bsi_remove (&b);
4468 e = ssa_redirect_edge (e, target);
4469 e->flags = EDGE_FALLTHRU;
4470 return e;
4473 return NULL;
4477 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4478 edge representing the redirected branch. */
4480 static edge
4481 tree_redirect_edge_and_branch (edge e, basic_block dest)
4483 basic_block bb = e->src;
4484 block_stmt_iterator bsi;
4485 edge ret;
4486 tree label, stmt;
4488 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4489 return NULL;
4491 if (e->src != ENTRY_BLOCK_PTR
4492 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4493 return ret;
4495 if (e->dest == dest)
4496 return NULL;
4498 label = tree_block_label (dest);
4500 bsi = bsi_last (bb);
4501 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4503 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4505 case COND_EXPR:
4506 stmt = (e->flags & EDGE_TRUE_VALUE
4507 ? COND_EXPR_THEN (stmt)
4508 : COND_EXPR_ELSE (stmt));
4509 GOTO_DESTINATION (stmt) = label;
4510 break;
4512 case GOTO_EXPR:
4513 /* No non-abnormal edges should lead from a non-simple goto, and
4514 simple ones should be represented implicitly. */
4515 gcc_unreachable ();
4517 case SWITCH_EXPR:
4519 tree cases = get_cases_for_edge (e, stmt);
4521 /* If we have a list of cases associated with E, then use it
4522 as it's a lot faster than walking the entire case vector. */
4523 if (cases)
4525 edge e2 = find_edge (e->src, dest);
4526 tree last, first;
4528 first = cases;
4529 while (cases)
4531 last = cases;
4532 CASE_LABEL (cases) = label;
4533 cases = TREE_CHAIN (cases);
4536 /* If there was already an edge in the CFG, then we need
4537 to move all the cases associated with E to E2. */
4538 if (e2)
4540 tree cases2 = get_cases_for_edge (e2, stmt);
4542 TREE_CHAIN (last) = TREE_CHAIN (cases2);
4543 TREE_CHAIN (cases2) = first;
4546 else
4548 tree vec = SWITCH_LABELS (stmt);
4549 size_t i, n = TREE_VEC_LENGTH (vec);
4551 for (i = 0; i < n; i++)
4553 tree elt = TREE_VEC_ELT (vec, i);
4555 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4556 CASE_LABEL (elt) = label;
4560 break;
4563 case RETURN_EXPR:
4564 bsi_remove (&bsi);
4565 e->flags |= EDGE_FALLTHRU;
4566 break;
4568 default:
4569 /* Otherwise it must be a fallthru edge, and we don't need to
4570 do anything besides redirecting it. */
4571 gcc_assert (e->flags & EDGE_FALLTHRU);
4572 break;
4575 /* Update/insert PHI nodes as necessary. */
4577 /* Now update the edges in the CFG. */
4578 e = ssa_redirect_edge (e, dest);
4580 return e;
4584 /* Simple wrapper, as we can always redirect fallthru edges. */
4586 static basic_block
4587 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4589 e = tree_redirect_edge_and_branch (e, dest);
4590 gcc_assert (e);
4592 return NULL;
4596 /* Splits basic block BB after statement STMT (but at least after the
4597 labels). If STMT is NULL, BB is split just after the labels. */
4599 static basic_block
4600 tree_split_block (basic_block bb, void *stmt)
4602 block_stmt_iterator bsi, bsi_tgt;
4603 tree act;
4604 basic_block new_bb;
4605 edge e;
4606 edge_iterator ei;
4608 new_bb = create_empty_bb (bb);
4610 /* Redirect the outgoing edges. */
4611 new_bb->succs = bb->succs;
4612 bb->succs = NULL;
4613 FOR_EACH_EDGE (e, ei, new_bb->succs)
4614 e->src = new_bb;
4616 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4617 stmt = NULL;
4619 /* Move everything from BSI to the new basic block. */
4620 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4622 act = bsi_stmt (bsi);
4623 if (TREE_CODE (act) == LABEL_EXPR)
4624 continue;
4626 if (!stmt)
4627 break;
4629 if (stmt == act)
4631 bsi_next (&bsi);
4632 break;
4636 bsi_tgt = bsi_start (new_bb);
4637 while (!bsi_end_p (bsi))
4639 act = bsi_stmt (bsi);
4640 bsi_remove (&bsi);
4641 bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT);
4644 return new_bb;
4648 /* Moves basic block BB after block AFTER. */
4650 static bool
4651 tree_move_block_after (basic_block bb, basic_block after)
4653 if (bb->prev_bb == after)
4654 return true;
4656 unlink_block (bb);
4657 link_block (bb, after);
4659 return true;
4663 /* Return true if basic_block can be duplicated. */
4665 static bool
4666 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED)
4668 return true;
4671 /* Create a duplicate of the basic block BB. NOTE: This does not
4672 preserve SSA form. */
4674 static basic_block
4675 tree_duplicate_bb (basic_block bb)
4677 basic_block new_bb;
4678 block_stmt_iterator bsi, bsi_tgt;
4679 tree phi, val;
4680 ssa_op_iter op_iter;
4682 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4684 /* First copy the phi nodes. We do not copy phi node arguments here,
4685 since the edges are not ready yet. Keep the chain of phi nodes in
4686 the same order, so that we can add them later. */
4687 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4689 mark_for_rewrite (PHI_RESULT (phi));
4690 create_phi_node (PHI_RESULT (phi), new_bb);
4692 set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb)));
4694 bsi_tgt = bsi_start (new_bb);
4695 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4697 tree stmt = bsi_stmt (bsi);
4698 tree copy;
4700 if (TREE_CODE (stmt) == LABEL_EXPR)
4701 continue;
4703 /* Record the definitions. */
4704 get_stmt_operands (stmt);
4706 FOR_EACH_SSA_TREE_OPERAND (val, stmt, op_iter, SSA_OP_ALL_DEFS)
4707 mark_for_rewrite (val);
4709 copy = unshare_expr (stmt);
4711 /* Copy also the virtual operands. */
4712 get_stmt_ann (copy);
4713 copy_virtual_operands (copy, stmt);
4715 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
4718 return new_bb;
4721 /* Basic block BB_COPY was created by code duplication. Add phi node
4722 arguments for edges going out of BB_COPY. The blocks that were
4723 duplicated have rbi->duplicated set to one. */
4725 void
4726 add_phi_args_after_copy_bb (basic_block bb_copy)
4728 basic_block bb, dest;
4729 edge e, e_copy;
4730 edge_iterator ei;
4731 tree phi, phi_copy, phi_next, def;
4733 bb = bb_copy->rbi->original;
4735 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
4737 if (!phi_nodes (e_copy->dest))
4738 continue;
4740 if (e_copy->dest->rbi->duplicated)
4741 dest = e_copy->dest->rbi->original;
4742 else
4743 dest = e_copy->dest;
4745 e = find_edge (bb, dest);
4746 if (!e)
4748 /* During loop unrolling the target of the latch edge is copied.
4749 In this case we are not looking for edge to dest, but to
4750 duplicated block whose original was dest. */
4751 FOR_EACH_EDGE (e, ei, bb->succs)
4752 if (e->dest->rbi->duplicated
4753 && e->dest->rbi->original == dest)
4754 break;
4756 gcc_assert (e != NULL);
4759 for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
4760 phi;
4761 phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
4763 phi_next = PHI_CHAIN (phi);
4765 gcc_assert (PHI_RESULT (phi) == PHI_RESULT (phi_copy));
4766 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4767 add_phi_arg (phi_copy, def, e_copy);
4772 /* Blocks in REGION_COPY array of length N_REGION were created by
4773 duplication of basic blocks. Add phi node arguments for edges
4774 going from these blocks. */
4776 void
4777 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region)
4779 unsigned i;
4781 for (i = 0; i < n_region; i++)
4782 region_copy[i]->rbi->duplicated = 1;
4784 for (i = 0; i < n_region; i++)
4785 add_phi_args_after_copy_bb (region_copy[i]);
4787 for (i = 0; i < n_region; i++)
4788 region_copy[i]->rbi->duplicated = 0;
4791 /* Maps the old ssa name FROM_NAME to TO_NAME. */
4793 struct ssa_name_map_entry
4795 tree from_name;
4796 tree to_name;
4799 /* Hash function for ssa_name_map_entry. */
4801 static hashval_t
4802 ssa_name_map_entry_hash (const void *entry)
4804 const struct ssa_name_map_entry *en = entry;
4805 return SSA_NAME_VERSION (en->from_name);
4808 /* Equality function for ssa_name_map_entry. */
4810 static int
4811 ssa_name_map_entry_eq (const void *in_table, const void *ssa_name)
4813 const struct ssa_name_map_entry *en = in_table;
4815 return en->from_name == ssa_name;
4818 /* Allocate duplicates of ssa names in list DEFINITIONS and store the mapping
4819 to MAP. */
4821 void
4822 allocate_ssa_names (bitmap definitions, htab_t *map)
4824 tree name;
4825 struct ssa_name_map_entry *entry;
4826 PTR *slot;
4827 unsigned ver;
4828 bitmap_iterator bi;
4830 if (!*map)
4831 *map = htab_create (10, ssa_name_map_entry_hash,
4832 ssa_name_map_entry_eq, free);
4833 EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
4835 name = ssa_name (ver);
4836 slot = htab_find_slot_with_hash (*map, name, SSA_NAME_VERSION (name),
4837 INSERT);
4838 if (*slot)
4839 entry = *slot;
4840 else
4842 entry = xmalloc (sizeof (struct ssa_name_map_entry));
4843 entry->from_name = name;
4844 *slot = entry;
4846 entry->to_name = duplicate_ssa_name (name, SSA_NAME_DEF_STMT (name));
4850 /* Rewrite the definition DEF in statement STMT to new ssa name as specified
4851 by the mapping MAP. */
4853 static void
4854 rewrite_to_new_ssa_names_def (def_operand_p def, tree stmt, htab_t map)
4856 tree name = DEF_FROM_PTR (def);
4857 struct ssa_name_map_entry *entry;
4859 gcc_assert (TREE_CODE (name) == SSA_NAME);
4861 entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name));
4862 if (!entry)
4863 return;
4865 SET_DEF (def, entry->to_name);
4866 SSA_NAME_DEF_STMT (entry->to_name) = stmt;
4869 /* Rewrite the USE to new ssa name as specified by the mapping MAP. */
4871 static void
4872 rewrite_to_new_ssa_names_use (use_operand_p use, htab_t map)
4874 tree name = USE_FROM_PTR (use);
4875 struct ssa_name_map_entry *entry;
4877 if (TREE_CODE (name) != SSA_NAME)
4878 return;
4880 entry = htab_find_with_hash (map, name, SSA_NAME_VERSION (name));
4881 if (!entry)
4882 return;
4884 SET_USE (use, entry->to_name);
4887 /* Rewrite the ssa names in basic block BB to new ones as specified by the
4888 mapping MAP. */
4890 void
4891 rewrite_to_new_ssa_names_bb (basic_block bb, htab_t map)
4893 unsigned i;
4894 edge e;
4895 edge_iterator ei;
4896 tree phi, stmt;
4897 block_stmt_iterator bsi;
4898 use_optype uses;
4899 vuse_optype vuses;
4900 def_optype defs;
4901 v_may_def_optype v_may_defs;
4902 v_must_def_optype v_must_defs;
4903 stmt_ann_t ann;
4905 FOR_EACH_EDGE (e, ei, bb->preds)
4906 if (e->flags & EDGE_ABNORMAL)
4907 break;
4909 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4911 rewrite_to_new_ssa_names_def (PHI_RESULT_PTR (phi), phi, map);
4912 if (e)
4913 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)) = 1;
4916 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4918 stmt = bsi_stmt (bsi);
4919 get_stmt_operands (stmt);
4920 ann = stmt_ann (stmt);
4922 uses = USE_OPS (ann);
4923 for (i = 0; i < NUM_USES (uses); i++)
4924 rewrite_to_new_ssa_names_use (USE_OP_PTR (uses, i), map);
4926 defs = DEF_OPS (ann);
4927 for (i = 0; i < NUM_DEFS (defs); i++)
4928 rewrite_to_new_ssa_names_def (DEF_OP_PTR (defs, i), stmt, map);
4930 vuses = VUSE_OPS (ann);
4931 for (i = 0; i < NUM_VUSES (vuses); i++)
4932 rewrite_to_new_ssa_names_use (VUSE_OP_PTR (vuses, i), map);
4934 v_may_defs = V_MAY_DEF_OPS (ann);
4935 for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++)
4937 rewrite_to_new_ssa_names_use
4938 (V_MAY_DEF_OP_PTR (v_may_defs, i), map);
4939 rewrite_to_new_ssa_names_def
4940 (V_MAY_DEF_RESULT_PTR (v_may_defs, i), stmt, map);
4943 v_must_defs = V_MUST_DEF_OPS (ann);
4944 for (i = 0; i < NUM_V_MUST_DEFS (v_must_defs); i++)
4946 rewrite_to_new_ssa_names_def
4947 (V_MUST_DEF_RESULT_PTR (v_must_defs, i), stmt, map);
4948 rewrite_to_new_ssa_names_use
4949 (V_MUST_DEF_KILL_PTR (v_must_defs, i), map);
4953 FOR_EACH_EDGE (e, ei, bb->succs)
4954 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
4956 rewrite_to_new_ssa_names_use
4957 (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e), map);
4959 if (e->flags & EDGE_ABNORMAL)
4961 tree op = PHI_ARG_DEF_FROM_EDGE (phi, e);
4962 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op) = 1;
4967 /* Rewrite the ssa names in N_REGION blocks REGION to the new ones as specified
4968 by the mapping MAP. */
4970 void
4971 rewrite_to_new_ssa_names (basic_block *region, unsigned n_region, htab_t map)
4973 unsigned r;
4975 for (r = 0; r < n_region; r++)
4976 rewrite_to_new_ssa_names_bb (region[r], map);
4979 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4980 important exit edge EXIT. By important we mean that no SSA name defined
4981 inside region is live over the other exit edges of the region. All entry
4982 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4983 to the duplicate of the region. SSA form, dominance and loop information
4984 is updated. The new basic blocks are stored to REGION_COPY in the same
4985 order as they had in REGION, provided that REGION_COPY is not NULL.
4986 The function returns false if it is unable to copy the region,
4987 true otherwise. */
4989 bool
4990 tree_duplicate_sese_region (edge entry, edge exit,
4991 basic_block *region, unsigned n_region,
4992 basic_block *region_copy)
4994 unsigned i, n_doms, ver;
4995 bool free_region_copy = false, copying_header = false;
4996 struct loop *loop = entry->dest->loop_father;
4997 edge exit_copy;
4998 bitmap definitions;
4999 tree phi;
5000 basic_block *doms;
5001 htab_t ssa_name_map = NULL;
5002 edge redirected;
5003 bitmap_iterator bi;
5005 if (!can_copy_bbs_p (region, n_region))
5006 return false;
5008 /* Some sanity checking. Note that we do not check for all possible
5009 missuses of the functions. I.e. if you ask to copy something weird,
5010 it will work, but the state of structures probably will not be
5011 correct. */
5013 for (i = 0; i < n_region; i++)
5015 /* We do not handle subloops, i.e. all the blocks must belong to the
5016 same loop. */
5017 if (region[i]->loop_father != loop)
5018 return false;
5020 if (region[i] != entry->dest
5021 && region[i] == loop->header)
5022 return false;
5025 loop->copy = loop;
5027 /* In case the function is used for loop header copying (which is the primary
5028 use), ensure that EXIT and its copy will be new latch and entry edges. */
5029 if (loop->header == entry->dest)
5031 copying_header = true;
5032 loop->copy = loop->outer;
5034 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
5035 return false;
5037 for (i = 0; i < n_region; i++)
5038 if (region[i] != exit->src
5039 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
5040 return false;
5043 if (!region_copy)
5045 region_copy = xmalloc (sizeof (basic_block) * n_region);
5046 free_region_copy = true;
5049 gcc_assert (!any_marked_for_rewrite_p ());
5051 /* Record blocks outside the region that are duplicated by something
5052 inside. */
5053 doms = xmalloc (sizeof (basic_block) * n_basic_blocks);
5054 n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms);
5056 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop);
5057 definitions = marked_ssa_names ();
5059 if (copying_header)
5061 loop->header = exit->dest;
5062 loop->latch = exit->src;
5065 /* Redirect the entry and add the phi node arguments. */
5066 redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy);
5067 gcc_assert (redirected != NULL);
5068 flush_pending_stmts (entry);
5070 /* Concerning updating of dominators: We must recount dominators
5071 for entry block and its copy. Anything that is outside of the region, but
5072 was dominated by something inside needs recounting as well. */
5073 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5074 doms[n_doms++] = entry->dest->rbi->original;
5075 iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms);
5076 free (doms);
5078 /* Add the other phi node arguments. */
5079 add_phi_args_after_copy (region_copy, n_region);
5081 /* Add phi nodes for definitions at exit. TODO -- once we have immediate
5082 uses, it should be possible to emit phi nodes just for definitions that
5083 are used outside region. */
5084 EXECUTE_IF_SET_IN_BITMAP (definitions, 0, ver, bi)
5086 tree name = ssa_name (ver);
5088 phi = create_phi_node (name, exit->dest);
5089 add_phi_arg (phi, name, exit);
5090 add_phi_arg (phi, name, exit_copy);
5092 SSA_NAME_DEF_STMT (name) = phi;
5095 /* And create new definitions inside region and its copy. TODO -- once we
5096 have immediate uses, it might be better to leave definitions in region
5097 unchanged, create new ssa names for phi nodes on exit, and rewrite
5098 the uses, to avoid changing the copied region. */
5099 allocate_ssa_names (definitions, &ssa_name_map);
5100 rewrite_to_new_ssa_names (region, n_region, ssa_name_map);
5101 allocate_ssa_names (definitions, &ssa_name_map);
5102 rewrite_to_new_ssa_names (region_copy, n_region, ssa_name_map);
5103 htab_delete (ssa_name_map);
5105 if (free_region_copy)
5106 free (region_copy);
5108 unmark_all_for_rewrite ();
5109 BITMAP_FREE (definitions);
5111 return true;
5114 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
5116 void
5117 dump_function_to_file (tree fn, FILE *file, int flags)
5119 tree arg, vars, var;
5120 bool ignore_topmost_bind = false, any_var = false;
5121 basic_block bb;
5122 tree chain;
5124 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
5126 arg = DECL_ARGUMENTS (fn);
5127 while (arg)
5129 print_generic_expr (file, arg, dump_flags);
5130 if (TREE_CHAIN (arg))
5131 fprintf (file, ", ");
5132 arg = TREE_CHAIN (arg);
5134 fprintf (file, ")\n");
5136 if (flags & TDF_RAW)
5138 dump_node (fn, TDF_SLIM | flags, file);
5139 return;
5142 /* When GIMPLE is lowered, the variables are no longer available in
5143 BIND_EXPRs, so display them separately. */
5144 if (cfun && cfun->unexpanded_var_list)
5146 ignore_topmost_bind = true;
5148 fprintf (file, "{\n");
5149 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
5151 var = TREE_VALUE (vars);
5153 print_generic_decl (file, var, flags);
5154 fprintf (file, "\n");
5156 any_var = true;
5160 if (basic_block_info)
5162 /* Make a CFG based dump. */
5163 check_bb_profile (ENTRY_BLOCK_PTR, file);
5164 if (!ignore_topmost_bind)
5165 fprintf (file, "{\n");
5167 if (any_var && n_basic_blocks)
5168 fprintf (file, "\n");
5170 FOR_EACH_BB (bb)
5171 dump_generic_bb (file, bb, 2, flags);
5173 fprintf (file, "}\n");
5174 check_bb_profile (EXIT_BLOCK_PTR, file);
5176 else
5178 int indent;
5180 /* Make a tree based dump. */
5181 chain = DECL_SAVED_TREE (fn);
5183 if (TREE_CODE (chain) == BIND_EXPR)
5185 if (ignore_topmost_bind)
5187 chain = BIND_EXPR_BODY (chain);
5188 indent = 2;
5190 else
5191 indent = 0;
5193 else
5195 if (!ignore_topmost_bind)
5196 fprintf (file, "{\n");
5197 indent = 2;
5200 if (any_var)
5201 fprintf (file, "\n");
5203 print_generic_stmt_indented (file, chain, flags, indent);
5204 if (ignore_topmost_bind)
5205 fprintf (file, "}\n");
5208 fprintf (file, "\n\n");
5212 /* Pretty print of the loops intermediate representation. */
5213 static void print_loop (FILE *, struct loop *, int);
5214 static void print_pred_bbs (FILE *, basic_block bb);
5215 static void print_succ_bbs (FILE *, basic_block bb);
5218 /* Print the predecessors indexes of edge E on FILE. */
5220 static void
5221 print_pred_bbs (FILE *file, basic_block bb)
5223 edge e;
5224 edge_iterator ei;
5226 FOR_EACH_EDGE (e, ei, bb->preds)
5227 fprintf (file, "bb_%d", e->src->index);
5231 /* Print the successors indexes of edge E on FILE. */
5233 static void
5234 print_succ_bbs (FILE *file, basic_block bb)
5236 edge e;
5237 edge_iterator ei;
5239 FOR_EACH_EDGE (e, ei, bb->succs)
5240 fprintf (file, "bb_%d", e->src->index);
5244 /* Pretty print LOOP on FILE, indented INDENT spaces. */
5246 static void
5247 print_loop (FILE *file, struct loop *loop, int indent)
5249 char *s_indent;
5250 basic_block bb;
5252 if (loop == NULL)
5253 return;
5255 s_indent = (char *) alloca ((size_t) indent + 1);
5256 memset ((void *) s_indent, ' ', (size_t) indent);
5257 s_indent[indent] = '\0';
5259 /* Print the loop's header. */
5260 fprintf (file, "%sloop_%d\n", s_indent, loop->num);
5262 /* Print the loop's body. */
5263 fprintf (file, "%s{\n", s_indent);
5264 FOR_EACH_BB (bb)
5265 if (bb->loop_father == loop)
5267 /* Print the basic_block's header. */
5268 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
5269 print_pred_bbs (file, bb);
5270 fprintf (file, "}, succs = {");
5271 print_succ_bbs (file, bb);
5272 fprintf (file, "})\n");
5274 /* Print the basic_block's body. */
5275 fprintf (file, "%s {\n", s_indent);
5276 tree_dump_bb (bb, file, indent + 4);
5277 fprintf (file, "%s }\n", s_indent);
5280 print_loop (file, loop->inner, indent + 2);
5281 fprintf (file, "%s}\n", s_indent);
5282 print_loop (file, loop->next, indent);
5286 /* Follow a CFG edge from the entry point of the program, and on entry
5287 of a loop, pretty print the loop structure on FILE. */
5289 void
5290 print_loop_ir (FILE *file)
5292 basic_block bb;
5294 bb = BASIC_BLOCK (0);
5295 if (bb && bb->loop_father)
5296 print_loop (file, bb->loop_father, 0);
5300 /* Debugging loops structure at tree level. */
5302 void
5303 debug_loop_ir (void)
5305 print_loop_ir (stderr);
5309 /* Return true if BB ends with a call, possibly followed by some
5310 instructions that must stay with the call. Return false,
5311 otherwise. */
5313 static bool
5314 tree_block_ends_with_call_p (basic_block bb)
5316 block_stmt_iterator bsi = bsi_last (bb);
5317 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
5321 /* Return true if BB ends with a conditional branch. Return false,
5322 otherwise. */
5324 static bool
5325 tree_block_ends_with_condjump_p (basic_block bb)
5327 tree stmt = tsi_stmt (bsi_last (bb).tsi);
5328 return (TREE_CODE (stmt) == COND_EXPR);
5332 /* Return true if we need to add fake edge to exit at statement T.
5333 Helper function for tree_flow_call_edges_add. */
5335 static bool
5336 need_fake_edge_p (tree t)
5338 tree call;
5340 /* NORETURN and LONGJMP calls already have an edge to exit.
5341 CONST, PURE and ALWAYS_RETURN calls do not need one.
5342 We don't currently check for CONST and PURE here, although
5343 it would be a good idea, because those attributes are
5344 figured out from the RTL in mark_constant_function, and
5345 the counter incrementation code from -fprofile-arcs
5346 leads to different results from -fbranch-probabilities. */
5347 call = get_call_expr_in (t);
5348 if (call
5349 && !(call_expr_flags (call) & (ECF_NORETURN | ECF_ALWAYS_RETURN)))
5350 return true;
5352 if (TREE_CODE (t) == ASM_EXPR
5353 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
5354 return true;
5356 return false;
5360 /* Add fake edges to the function exit for any non constant and non
5361 noreturn calls, volatile inline assembly in the bitmap of blocks
5362 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
5363 the number of blocks that were split.
5365 The goal is to expose cases in which entering a basic block does
5366 not imply that all subsequent instructions must be executed. */
5368 static int
5369 tree_flow_call_edges_add (sbitmap blocks)
5371 int i;
5372 int blocks_split = 0;
5373 int last_bb = last_basic_block;
5374 bool check_last_block = false;
5376 if (n_basic_blocks == 0)
5377 return 0;
5379 if (! blocks)
5380 check_last_block = true;
5381 else
5382 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
5384 /* In the last basic block, before epilogue generation, there will be
5385 a fallthru edge to EXIT. Special care is required if the last insn
5386 of the last basic block is a call because make_edge folds duplicate
5387 edges, which would result in the fallthru edge also being marked
5388 fake, which would result in the fallthru edge being removed by
5389 remove_fake_edges, which would result in an invalid CFG.
5391 Moreover, we can't elide the outgoing fake edge, since the block
5392 profiler needs to take this into account in order to solve the minimal
5393 spanning tree in the case that the call doesn't return.
5395 Handle this by adding a dummy instruction in a new last basic block. */
5396 if (check_last_block)
5398 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
5399 block_stmt_iterator bsi = bsi_last (bb);
5400 tree t = NULL_TREE;
5401 if (!bsi_end_p (bsi))
5402 t = bsi_stmt (bsi);
5404 if (need_fake_edge_p (t))
5406 edge e;
5408 e = find_edge (bb, EXIT_BLOCK_PTR);
5409 if (e)
5411 bsi_insert_on_edge (e, build_empty_stmt ());
5412 bsi_commit_edge_inserts ();
5417 /* Now add fake edges to the function exit for any non constant
5418 calls since there is no way that we can determine if they will
5419 return or not... */
5420 for (i = 0; i < last_bb; i++)
5422 basic_block bb = BASIC_BLOCK (i);
5423 block_stmt_iterator bsi;
5424 tree stmt, last_stmt;
5426 if (!bb)
5427 continue;
5429 if (blocks && !TEST_BIT (blocks, i))
5430 continue;
5432 bsi = bsi_last (bb);
5433 if (!bsi_end_p (bsi))
5435 last_stmt = bsi_stmt (bsi);
5438 stmt = bsi_stmt (bsi);
5439 if (need_fake_edge_p (stmt))
5441 edge e;
5442 /* The handling above of the final block before the
5443 epilogue should be enough to verify that there is
5444 no edge to the exit block in CFG already.
5445 Calling make_edge in such case would cause us to
5446 mark that edge as fake and remove it later. */
5447 #ifdef ENABLE_CHECKING
5448 if (stmt == last_stmt)
5450 e = find_edge (bb, EXIT_BLOCK_PTR);
5451 gcc_assert (e == NULL);
5453 #endif
5455 /* Note that the following may create a new basic block
5456 and renumber the existing basic blocks. */
5457 if (stmt != last_stmt)
5459 e = split_block (bb, stmt);
5460 if (e)
5461 blocks_split++;
5463 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
5465 bsi_prev (&bsi);
5467 while (!bsi_end_p (bsi));
5471 if (blocks_split)
5472 verify_flow_info ();
5474 return blocks_split;
5477 bool
5478 tree_purge_dead_eh_edges (basic_block bb)
5480 bool changed = false;
5481 edge e;
5482 edge_iterator ei;
5483 tree stmt = last_stmt (bb);
5485 if (stmt && tree_can_throw_internal (stmt))
5486 return false;
5488 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5490 if (e->flags & EDGE_EH)
5492 remove_edge (e);
5493 changed = true;
5495 else
5496 ei_next (&ei);
5499 /* Removal of dead EH edges might change dominators of not
5500 just immediate successors. E.g. when bb1 is changed so that
5501 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5502 eh edges purged by this function in:
5506 1-->2
5507 / \ |
5508 v v |
5509 3-->4 |
5511 --->5
5514 idom(bb5) must be recomputed. For now just free the dominance
5515 info. */
5516 if (changed)
5517 free_dominance_info (CDI_DOMINATORS);
5519 return changed;
5522 bool
5523 tree_purge_all_dead_eh_edges (bitmap blocks)
5525 bool changed = false;
5526 unsigned i;
5527 bitmap_iterator bi;
5529 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
5531 changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
5534 return changed;
5537 /* This function is called whenever a new edge is created or
5538 redirected. */
5540 static void
5541 tree_execute_on_growing_pred (edge e)
5543 basic_block bb = e->dest;
5545 if (phi_nodes (bb))
5546 reserve_phi_args_for_new_edge (bb);
5549 /* This function is called immediately before edge E is removed from
5550 the edge vector E->dest->preds. */
5552 static void
5553 tree_execute_on_shrinking_pred (edge e)
5555 if (phi_nodes (e->dest))
5556 remove_phi_args (e);
5559 struct cfg_hooks tree_cfg_hooks = {
5560 "tree",
5561 tree_verify_flow_info,
5562 tree_dump_bb, /* dump_bb */
5563 create_bb, /* create_basic_block */
5564 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
5565 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
5566 remove_bb, /* delete_basic_block */
5567 tree_split_block, /* split_block */
5568 tree_move_block_after, /* move_block_after */
5569 tree_can_merge_blocks_p, /* can_merge_blocks_p */
5570 tree_merge_blocks, /* merge_blocks */
5571 tree_predict_edge, /* predict_edge */
5572 tree_predicted_by_p, /* predicted_by_p */
5573 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
5574 tree_duplicate_bb, /* duplicate_block */
5575 tree_split_edge, /* split_edge */
5576 tree_make_forwarder_block, /* make_forward_block */
5577 NULL, /* tidy_fallthru_edge */
5578 tree_block_ends_with_call_p, /* block_ends_with_call_p */
5579 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
5580 tree_flow_call_edges_add, /* flow_call_edges_add */
5581 tree_execute_on_growing_pred, /* execute_on_growing_pred */
5582 tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
5586 /* Split all critical edges. */
5588 static void
5589 split_critical_edges (void)
5591 basic_block bb;
5592 edge e;
5593 edge_iterator ei;
5595 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
5596 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
5597 mappings around the calls to split_edge. */
5598 start_recording_case_labels ();
5599 FOR_ALL_BB (bb)
5601 FOR_EACH_EDGE (e, ei, bb->succs)
5602 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
5604 split_edge (e);
5607 end_recording_case_labels ();
5610 struct tree_opt_pass pass_split_crit_edges =
5612 "crited", /* name */
5613 NULL, /* gate */
5614 split_critical_edges, /* execute */
5615 NULL, /* sub */
5616 NULL, /* next */
5617 0, /* static_pass_number */
5618 TV_TREE_SPLIT_EDGES, /* tv_id */
5619 PROP_cfg, /* properties required */
5620 PROP_no_crit_edges, /* properties_provided */
5621 0, /* properties_destroyed */
5622 0, /* todo_flags_start */
5623 TODO_dump_func, /* todo_flags_finish */
5624 0 /* letter */
5628 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5629 a temporary, make sure and register it to be renamed if necessary,
5630 and finally return the temporary. Put the statements to compute
5631 EXP before the current statement in BSI. */
5633 tree
5634 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
5636 tree t, new_stmt, orig_stmt;
5638 if (is_gimple_val (exp))
5639 return exp;
5641 t = make_rename_temp (type, NULL);
5642 new_stmt = build (MODIFY_EXPR, type, t, exp);
5644 orig_stmt = bsi_stmt (*bsi);
5645 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
5646 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
5648 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
5650 return t;
5653 /* Build a ternary operation and gimplify it. Emit code before BSI.
5654 Return the gimple_val holding the result. */
5656 tree
5657 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
5658 tree type, tree a, tree b, tree c)
5660 tree ret;
5662 ret = fold (build3 (code, type, a, b, c));
5663 STRIP_NOPS (ret);
5665 return gimplify_val (bsi, type, ret);
5668 /* Build a binary operation and gimplify it. Emit code before BSI.
5669 Return the gimple_val holding the result. */
5671 tree
5672 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
5673 tree type, tree a, tree b)
5675 tree ret;
5677 ret = fold (build2 (code, type, a, b));
5678 STRIP_NOPS (ret);
5680 return gimplify_val (bsi, type, ret);
5683 /* Build a unary operation and gimplify it. Emit code before BSI.
5684 Return the gimple_val holding the result. */
5686 tree
5687 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
5688 tree a)
5690 tree ret;
5692 ret = fold (build1 (code, type, a));
5693 STRIP_NOPS (ret);
5695 return gimplify_val (bsi, type, ret);
5700 /* Emit return warnings. */
5702 static void
5703 execute_warn_function_return (void)
5705 #ifdef USE_MAPPED_LOCATION
5706 source_location location;
5707 #else
5708 location_t *locus;
5709 #endif
5710 tree last;
5711 edge e;
5712 edge_iterator ei;
5714 if (warn_missing_noreturn
5715 && !TREE_THIS_VOLATILE (cfun->decl)
5716 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
5717 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
5718 warning ("%Jfunction might be possible candidate for "
5719 "attribute %<noreturn%>",
5720 cfun->decl);
5722 /* If we have a path to EXIT, then we do return. */
5723 if (TREE_THIS_VOLATILE (cfun->decl)
5724 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
5726 #ifdef USE_MAPPED_LOCATION
5727 location = UNKNOWN_LOCATION;
5728 #else
5729 locus = NULL;
5730 #endif
5731 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5733 last = last_stmt (e->src);
5734 if (TREE_CODE (last) == RETURN_EXPR
5735 #ifdef USE_MAPPED_LOCATION
5736 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
5737 #else
5738 && (locus = EXPR_LOCUS (last)) != NULL)
5739 #endif
5740 break;
5742 #ifdef USE_MAPPED_LOCATION
5743 if (location == UNKNOWN_LOCATION)
5744 location = cfun->function_end_locus;
5745 warning ("%H%<noreturn%> function does return", &location);
5746 #else
5747 if (!locus)
5748 locus = &cfun->function_end_locus;
5749 warning ("%H%<noreturn%> function does return", locus);
5750 #endif
5753 /* If we see "return;" in some basic block, then we do reach the end
5754 without returning a value. */
5755 else if (warn_return_type
5756 && !TREE_NO_WARNING (cfun->decl)
5757 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
5758 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
5760 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5762 tree last = last_stmt (e->src);
5763 if (TREE_CODE (last) == RETURN_EXPR
5764 && TREE_OPERAND (last, 0) == NULL
5765 && !TREE_NO_WARNING (last))
5767 #ifdef USE_MAPPED_LOCATION
5768 location = EXPR_LOCATION (last);
5769 if (location == UNKNOWN_LOCATION)
5770 location = cfun->function_end_locus;
5771 warning ("%Hcontrol reaches end of non-void function", &location);
5772 #else
5773 locus = EXPR_LOCUS (last);
5774 if (!locus)
5775 locus = &cfun->function_end_locus;
5776 warning ("%Hcontrol reaches end of non-void function", locus);
5777 #endif
5778 TREE_NO_WARNING (cfun->decl) = 1;
5779 break;
5786 /* Given a basic block B which ends with a conditional and has
5787 precisely two successors, determine which of the edges is taken if
5788 the conditional is true and which is taken if the conditional is
5789 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5791 void
5792 extract_true_false_edges_from_block (basic_block b,
5793 edge *true_edge,
5794 edge *false_edge)
5796 edge e = EDGE_SUCC (b, 0);
5798 if (e->flags & EDGE_TRUE_VALUE)
5800 *true_edge = e;
5801 *false_edge = EDGE_SUCC (b, 1);
5803 else
5805 *false_edge = e;
5806 *true_edge = EDGE_SUCC (b, 1);
5810 struct tree_opt_pass pass_warn_function_return =
5812 NULL, /* name */
5813 NULL, /* gate */
5814 execute_warn_function_return, /* execute */
5815 NULL, /* sub */
5816 NULL, /* next */
5817 0, /* static_pass_number */
5818 0, /* tv_id */
5819 PROP_cfg, /* properties_required */
5820 0, /* properties_provided */
5821 0, /* properties_destroyed */
5822 0, /* todo_flags_start */
5823 0, /* todo_flags_finish */
5824 0 /* letter */
5827 #include "gt-tree-cfg.h"