* c-common.c (unsigned_conversion_warning): Move warning control
[official-gcc.git] / gcc / tree-cfg.c
blob22215a391277561f6aa65c060f64b620c88f928e
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"
48 #include "tree-ssa-propagate.h"
50 /* This file contains functions for building the Control Flow Graph (CFG)
51 for a function tree. */
53 /* Local declarations. */
55 /* Initial capacity for the basic block array. */
56 static const int initial_cfg_capacity = 20;
58 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
59 which use a particular edge. The CASE_LABEL_EXPRs are chained together
60 via their TREE_CHAIN field, which we clear after we're done with the
61 hash table to prevent problems with duplication of SWITCH_EXPRs.
63 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
64 update the case vector in response to edge redirections.
66 Right now this table is set up and torn down at key points in the
67 compilation process. It would be nice if we could make the table
68 more persistent. The key is getting notification of changes to
69 the CFG (particularly edge removal, creation and redirection). */
71 struct edge_to_cases_elt
73 /* The edge itself. Necessary for hashing and equality tests. */
74 edge e;
76 /* The case labels associated with this edge. We link these up via
77 their TREE_CHAIN field, then we wipe out the TREE_CHAIN fields
78 when we destroy the hash table. This prevents problems when copying
79 SWITCH_EXPRs. */
80 tree case_labels;
83 static htab_t edge_to_cases;
85 /* CFG statistics. */
86 struct cfg_stats_d
88 long num_merged_labels;
91 static struct cfg_stats_d cfg_stats;
93 /* Nonzero if we found a computed goto while building basic blocks. */
94 static bool found_computed_goto;
96 /* Basic blocks and flowgraphs. */
97 static basic_block create_bb (void *, void *, basic_block);
98 static void create_block_annotation (basic_block);
99 static void free_blocks_annotations (void);
100 static void clear_blocks_annotations (void);
101 static void make_blocks (tree);
102 static void factor_computed_gotos (void);
104 /* Edges. */
105 static void make_edges (void);
106 static void make_ctrl_stmt_edges (basic_block);
107 static void make_exit_edges (basic_block);
108 static void make_cond_expr_edges (basic_block);
109 static void make_switch_expr_edges (basic_block);
110 static void make_goto_expr_edges (basic_block);
111 static edge tree_redirect_edge_and_branch (edge, basic_block);
112 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
113 static void split_critical_edges (void);
114 static bool remove_fallthru_edge (VEC(edge,gc) *);
116 /* Various helpers. */
117 static inline bool stmt_starts_bb_p (tree, tree);
118 static int tree_verify_flow_info (void);
119 static void tree_make_forwarder_block (edge);
120 static bool tree_forwarder_block_p (basic_block, bool);
121 static void tree_cfg2vcg (FILE *);
123 /* Flowgraph optimization and cleanup. */
124 static void tree_merge_blocks (basic_block, basic_block);
125 static bool tree_can_merge_blocks_p (basic_block, basic_block);
126 static void remove_bb (basic_block);
127 static bool cleanup_control_flow (void);
128 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
129 static edge find_taken_edge_computed_goto (basic_block, tree);
130 static edge find_taken_edge_cond_expr (basic_block, tree);
131 static edge find_taken_edge_switch_expr (basic_block, tree);
132 static tree find_case_label_for_value (tree, tree);
133 static bool phi_alternatives_equal (basic_block, edge, edge);
134 static bool cleanup_forwarder_blocks (void);
136 void
137 init_empty_tree_cfg (void)
139 /* Initialize the basic block array. */
140 init_flow ();
141 profile_status = PROFILE_ABSENT;
142 n_basic_blocks = 0;
143 last_basic_block = 0;
144 VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info");
146 /* Build a mapping of labels to their associated blocks. */
147 VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity,
148 "label to block map");
150 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
151 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
153 create_block_annotation (ENTRY_BLOCK_PTR);
154 create_block_annotation (EXIT_BLOCK_PTR);
157 /*---------------------------------------------------------------------------
158 Create basic blocks
159 ---------------------------------------------------------------------------*/
161 /* Entry point to the CFG builder for trees. TP points to the list of
162 statements to be added to the flowgraph. */
164 static void
165 build_tree_cfg (tree *tp)
167 /* Register specific tree functions. */
168 tree_register_cfg_hooks ();
170 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
172 init_empty_tree_cfg ();
174 found_computed_goto = 0;
175 make_blocks (*tp);
177 /* Computed gotos are hell to deal with, especially if there are
178 lots of them with a large number of destinations. So we factor
179 them to a common computed goto location before we build the
180 edge list. After we convert back to normal form, we will un-factor
181 the computed gotos since factoring introduces an unwanted jump. */
182 if (found_computed_goto)
183 factor_computed_gotos ();
185 /* Make sure there is always at least one block, even if it's empty. */
186 if (n_basic_blocks == 0)
187 create_empty_bb (ENTRY_BLOCK_PTR);
189 /* Adjust the size of the array. */
190 VARRAY_GROW (basic_block_info, n_basic_blocks);
192 /* To speed up statement iterator walks, we first purge dead labels. */
193 cleanup_dead_labels ();
195 /* Group case nodes to reduce the number of edges.
196 We do this after cleaning up dead labels because otherwise we miss
197 a lot of obvious case merging opportunities. */
198 group_case_labels ();
200 /* Create the edges of the flowgraph. */
201 make_edges ();
203 /* Debugging dumps. */
205 /* Write the flowgraph to a VCG file. */
207 int local_dump_flags;
208 FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags);
209 if (dump_file)
211 tree_cfg2vcg (dump_file);
212 dump_end (TDI_vcg, dump_file);
216 #ifdef ENABLE_CHECKING
217 verify_stmts ();
218 #endif
220 /* Dump a textual representation of the flowgraph. */
221 if (dump_file)
222 dump_tree_cfg (dump_file, dump_flags);
225 static void
226 execute_build_cfg (void)
228 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
231 struct tree_opt_pass pass_build_cfg =
233 "cfg", /* name */
234 NULL, /* gate */
235 execute_build_cfg, /* execute */
236 NULL, /* sub */
237 NULL, /* next */
238 0, /* static_pass_number */
239 TV_TREE_CFG, /* tv_id */
240 PROP_gimple_leh, /* properties_required */
241 PROP_cfg, /* properties_provided */
242 0, /* properties_destroyed */
243 0, /* todo_flags_start */
244 TODO_verify_stmts, /* todo_flags_finish */
245 0 /* letter */
248 /* Search the CFG for any computed gotos. If found, factor them to a
249 common computed goto site. Also record the location of that site so
250 that we can un-factor the gotos after we have converted back to
251 normal form. */
253 static void
254 factor_computed_gotos (void)
256 basic_block bb;
257 tree factored_label_decl = NULL;
258 tree var = NULL;
259 tree factored_computed_goto_label = NULL;
260 tree factored_computed_goto = NULL;
262 /* We know there are one or more computed gotos in this function.
263 Examine the last statement in each basic block to see if the block
264 ends with a computed goto. */
266 FOR_EACH_BB (bb)
268 block_stmt_iterator bsi = bsi_last (bb);
269 tree last;
271 if (bsi_end_p (bsi))
272 continue;
273 last = bsi_stmt (bsi);
275 /* Ignore the computed goto we create when we factor the original
276 computed gotos. */
277 if (last == factored_computed_goto)
278 continue;
280 /* If the last statement is a computed goto, factor it. */
281 if (computed_goto_p (last))
283 tree assignment;
285 /* The first time we find a computed goto we need to create
286 the factored goto block and the variable each original
287 computed goto will use for their goto destination. */
288 if (! factored_computed_goto)
290 basic_block new_bb = create_empty_bb (bb);
291 block_stmt_iterator new_bsi = bsi_start (new_bb);
293 /* Create the destination of the factored goto. Each original
294 computed goto will put its desired destination into this
295 variable and jump to the label we create immediately
296 below. */
297 var = create_tmp_var (ptr_type_node, "gotovar");
299 /* Build a label for the new block which will contain the
300 factored computed goto. */
301 factored_label_decl = create_artificial_label ();
302 factored_computed_goto_label
303 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
304 bsi_insert_after (&new_bsi, factored_computed_goto_label,
305 BSI_NEW_STMT);
307 /* Build our new computed goto. */
308 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
309 bsi_insert_after (&new_bsi, factored_computed_goto,
310 BSI_NEW_STMT);
313 /* Copy the original computed goto's destination into VAR. */
314 assignment = build (MODIFY_EXPR, ptr_type_node,
315 var, GOTO_DESTINATION (last));
316 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
318 /* And re-vector the computed goto to the new destination. */
319 GOTO_DESTINATION (last) = factored_label_decl;
325 /* Create annotations for a single basic block. */
327 static void
328 create_block_annotation (basic_block bb)
330 /* Verify that the tree_annotations field is clear. */
331 gcc_assert (!bb->tree_annotations);
332 bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d));
336 /* Free the annotations for all the basic blocks. */
338 static void free_blocks_annotations (void)
340 clear_blocks_annotations ();
344 /* Clear the annotations for all the basic blocks. */
346 static void
347 clear_blocks_annotations (void)
349 basic_block bb;
351 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
352 bb->tree_annotations = NULL;
356 /* Build a flowgraph for the statement_list STMT_LIST. */
358 static void
359 make_blocks (tree stmt_list)
361 tree_stmt_iterator i = tsi_start (stmt_list);
362 tree stmt = NULL;
363 bool start_new_block = true;
364 bool first_stmt_of_list = true;
365 basic_block bb = ENTRY_BLOCK_PTR;
367 while (!tsi_end_p (i))
369 tree prev_stmt;
371 prev_stmt = stmt;
372 stmt = tsi_stmt (i);
374 /* If the statement starts a new basic block or if we have determined
375 in a previous pass that we need to create a new block for STMT, do
376 so now. */
377 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
379 if (!first_stmt_of_list)
380 stmt_list = tsi_split_statement_list_before (&i);
381 bb = create_basic_block (stmt_list, NULL, bb);
382 start_new_block = false;
385 /* Now add STMT to BB and create the subgraphs for special statement
386 codes. */
387 set_bb_for_stmt (stmt, bb);
389 if (computed_goto_p (stmt))
390 found_computed_goto = true;
392 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
393 next iteration. */
394 if (stmt_ends_bb_p (stmt))
395 start_new_block = true;
397 tsi_next (&i);
398 first_stmt_of_list = false;
403 /* Create and return a new empty basic block after bb AFTER. */
405 static basic_block
406 create_bb (void *h, void *e, basic_block after)
408 basic_block bb;
410 gcc_assert (!e);
412 /* Create and initialize a new basic block. Since alloc_block uses
413 ggc_alloc_cleared to allocate a basic block, we do not have to
414 clear the newly allocated basic block here. */
415 bb = alloc_block ();
417 bb->index = last_basic_block;
418 bb->flags = BB_NEW;
419 bb->stmt_list = h ? h : alloc_stmt_list ();
421 /* Add the new block to the linked list of blocks. */
422 link_block (bb, after);
424 /* Grow the basic block array if needed. */
425 if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info))
427 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
428 VARRAY_GROW (basic_block_info, new_size);
431 /* Add the newly created block to the array. */
432 BASIC_BLOCK (last_basic_block) = bb;
434 create_block_annotation (bb);
436 n_basic_blocks++;
437 last_basic_block++;
439 initialize_bb_rbi (bb);
440 return bb;
444 /*---------------------------------------------------------------------------
445 Edge creation
446 ---------------------------------------------------------------------------*/
448 /* Fold COND_EXPR_COND of each COND_EXPR. */
450 void
451 fold_cond_expr_cond (void)
453 basic_block bb;
455 FOR_EACH_BB (bb)
457 tree stmt = last_stmt (bb);
459 if (stmt
460 && TREE_CODE (stmt) == COND_EXPR)
462 tree cond = fold (COND_EXPR_COND (stmt));
463 if (integer_zerop (cond))
464 COND_EXPR_COND (stmt) = boolean_false_node;
465 else if (integer_onep (cond))
466 COND_EXPR_COND (stmt) = boolean_true_node;
471 /* Join all the blocks in the flowgraph. */
473 static void
474 make_edges (void)
476 basic_block bb;
478 /* Create an edge from entry to the first block with executable
479 statements in it. */
480 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
482 /* Traverse the basic block array placing edges. */
483 FOR_EACH_BB (bb)
485 tree first = first_stmt (bb);
486 tree last = last_stmt (bb);
488 if (first)
490 /* Edges for statements that always alter flow control. */
491 if (is_ctrl_stmt (last))
492 make_ctrl_stmt_edges (bb);
494 /* Edges for statements that sometimes alter flow control. */
495 if (is_ctrl_altering_stmt (last))
496 make_exit_edges (bb);
499 /* Finally, if no edges were created above, this is a regular
500 basic block that only needs a fallthru edge. */
501 if (EDGE_COUNT (bb->succs) == 0)
502 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
505 /* We do not care about fake edges, so remove any that the CFG
506 builder inserted for completeness. */
507 remove_fake_exit_edges ();
509 /* Fold COND_EXPR_COND of each COND_EXPR. */
510 fold_cond_expr_cond ();
512 /* Clean up the graph and warn for unreachable code. */
513 cleanup_tree_cfg ();
517 /* Create edges for control statement at basic block BB. */
519 static void
520 make_ctrl_stmt_edges (basic_block bb)
522 tree last = last_stmt (bb);
524 gcc_assert (last);
525 switch (TREE_CODE (last))
527 case GOTO_EXPR:
528 make_goto_expr_edges (bb);
529 break;
531 case RETURN_EXPR:
532 make_edge (bb, EXIT_BLOCK_PTR, 0);
533 break;
535 case COND_EXPR:
536 make_cond_expr_edges (bb);
537 break;
539 case SWITCH_EXPR:
540 make_switch_expr_edges (bb);
541 break;
543 case RESX_EXPR:
544 make_eh_edges (last);
545 /* Yet another NORETURN hack. */
546 if (EDGE_COUNT (bb->succs) == 0)
547 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
548 break;
550 default:
551 gcc_unreachable ();
556 /* Create exit edges for statements in block BB that alter the flow of
557 control. Statements that alter the control flow are 'goto', 'return'
558 and calls to non-returning functions. */
560 static void
561 make_exit_edges (basic_block bb)
563 tree last = last_stmt (bb), op;
565 gcc_assert (last);
566 switch (TREE_CODE (last))
568 case RESX_EXPR:
569 break;
570 case CALL_EXPR:
571 /* If this function receives a nonlocal goto, then we need to
572 make edges from this call site to all the nonlocal goto
573 handlers. */
574 if (TREE_SIDE_EFFECTS (last)
575 && current_function_has_nonlocal_label)
576 make_goto_expr_edges (bb);
578 /* If this statement has reachable exception handlers, then
579 create abnormal edges to them. */
580 make_eh_edges (last);
582 /* Some calls are known not to return. For such calls we create
583 a fake edge.
585 We really need to revamp how we build edges so that it's not
586 such a bloody pain to avoid creating edges for this case since
587 all we do is remove these edges when we're done building the
588 CFG. */
589 if (call_expr_flags (last) & ECF_NORETURN)
591 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
592 return;
595 /* Don't forget the fall-thru edge. */
596 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
597 break;
599 case MODIFY_EXPR:
600 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
601 may have an abnormal edge. Search the RHS for this case and
602 create any required edges. */
603 op = get_call_expr_in (last);
604 if (op && TREE_SIDE_EFFECTS (op)
605 && current_function_has_nonlocal_label)
606 make_goto_expr_edges (bb);
608 make_eh_edges (last);
609 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
610 break;
612 default:
613 gcc_unreachable ();
618 /* Create the edges for a COND_EXPR starting at block BB.
619 At this point, both clauses must contain only simple gotos. */
621 static void
622 make_cond_expr_edges (basic_block bb)
624 tree entry = last_stmt (bb);
625 basic_block then_bb, else_bb;
626 tree then_label, else_label;
628 gcc_assert (entry);
629 gcc_assert (TREE_CODE (entry) == COND_EXPR);
631 /* Entry basic blocks for each component. */
632 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
633 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
634 then_bb = label_to_block (then_label);
635 else_bb = label_to_block (else_label);
637 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
638 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
641 /* Hashing routine for EDGE_TO_CASES. */
643 static hashval_t
644 edge_to_cases_hash (const void *p)
646 edge e = ((struct edge_to_cases_elt *)p)->e;
648 /* Hash on the edge itself (which is a pointer). */
649 return htab_hash_pointer (e);
652 /* Equality routine for EDGE_TO_CASES, edges are unique, so testing
653 for equality is just a pointer comparison. */
655 static int
656 edge_to_cases_eq (const void *p1, const void *p2)
658 edge e1 = ((struct edge_to_cases_elt *)p1)->e;
659 edge e2 = ((struct edge_to_cases_elt *)p2)->e;
661 return e1 == e2;
664 /* Called for each element in the hash table (P) as we delete the
665 edge to cases hash table.
667 Clear all the TREE_CHAINs to prevent problems with copying of
668 SWITCH_EXPRs and structure sharing rules, then free the hash table
669 element. */
671 static void
672 edge_to_cases_cleanup (void *p)
674 struct edge_to_cases_elt *elt = p;
675 tree t, next;
677 for (t = elt->case_labels; t; t = next)
679 next = TREE_CHAIN (t);
680 TREE_CHAIN (t) = NULL;
682 free (p);
685 /* Start recording information mapping edges to case labels. */
687 static void
688 start_recording_case_labels (void)
690 gcc_assert (edge_to_cases == NULL);
692 edge_to_cases = htab_create (37,
693 edge_to_cases_hash,
694 edge_to_cases_eq,
695 edge_to_cases_cleanup);
698 /* Return nonzero if we are recording information for case labels. */
700 static bool
701 recording_case_labels_p (void)
703 return (edge_to_cases != NULL);
706 /* Stop recording information mapping edges to case labels and
707 remove any information we have recorded. */
708 static void
709 end_recording_case_labels (void)
711 htab_delete (edge_to_cases);
712 edge_to_cases = NULL;
715 /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */
717 static void
718 record_switch_edge (edge e, tree case_label)
720 struct edge_to_cases_elt *elt;
721 void **slot;
723 /* Build a hash table element so we can see if E is already
724 in the table. */
725 elt = xmalloc (sizeof (struct edge_to_cases_elt));
726 elt->e = e;
727 elt->case_labels = case_label;
729 slot = htab_find_slot (edge_to_cases, elt, INSERT);
731 if (*slot == NULL)
733 /* E was not in the hash table. Install E into the hash table. */
734 *slot = (void *)elt;
736 else
738 /* E was already in the hash table. Free ELT as we do not need it
739 anymore. */
740 free (elt);
742 /* Get the entry stored in the hash table. */
743 elt = (struct edge_to_cases_elt *) *slot;
745 /* Add it to the chain of CASE_LABEL_EXPRs referencing E. */
746 TREE_CHAIN (case_label) = elt->case_labels;
747 elt->case_labels = case_label;
751 /* If we are inside a {start,end}_recording_cases block, then return
752 a chain of CASE_LABEL_EXPRs from T which reference E.
754 Otherwise return NULL. */
756 static tree
757 get_cases_for_edge (edge e, tree t)
759 struct edge_to_cases_elt elt, *elt_p;
760 void **slot;
761 size_t i, n;
762 tree vec;
764 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
765 chains available. Return NULL so the caller can detect this case. */
766 if (!recording_case_labels_p ())
767 return NULL;
769 restart:
770 elt.e = e;
771 elt.case_labels = NULL;
772 slot = htab_find_slot (edge_to_cases, &elt, NO_INSERT);
774 if (slot)
776 elt_p = (struct edge_to_cases_elt *)*slot;
777 return elt_p->case_labels;
780 /* If we did not find E in the hash table, then this must be the first
781 time we have been queried for information about E & T. Add all the
782 elements from T to the hash table then perform the query again. */
784 vec = SWITCH_LABELS (t);
785 n = TREE_VEC_LENGTH (vec);
786 for (i = 0; i < n; i++)
788 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
789 basic_block label_bb = label_to_block (lab);
790 record_switch_edge (find_edge (e->src, label_bb), TREE_VEC_ELT (vec, i));
792 goto restart;
795 /* Create the edges for a SWITCH_EXPR starting at block BB.
796 At this point, the switch body has been lowered and the
797 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
799 static void
800 make_switch_expr_edges (basic_block bb)
802 tree entry = last_stmt (bb);
803 size_t i, n;
804 tree vec;
806 vec = SWITCH_LABELS (entry);
807 n = TREE_VEC_LENGTH (vec);
809 for (i = 0; i < n; ++i)
811 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
812 basic_block label_bb = label_to_block (lab);
813 make_edge (bb, label_bb, 0);
818 /* Return the basic block holding label DEST. */
820 basic_block
821 label_to_block_fn (struct function *ifun, tree dest)
823 int uid = LABEL_DECL_UID (dest);
825 /* We would die hard when faced by an undefined label. Emit a label to
826 the very first basic block. This will hopefully make even the dataflow
827 and undefined variable warnings quite right. */
828 if ((errorcount || sorrycount) && uid < 0)
830 block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0));
831 tree stmt;
833 stmt = build1 (LABEL_EXPR, void_type_node, dest);
834 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
835 uid = LABEL_DECL_UID (dest);
837 if (VARRAY_SIZE (ifun->cfg->x_label_to_block_map) <= (unsigned int)uid)
838 return NULL;
839 return VARRAY_BB (ifun->cfg->x_label_to_block_map, uid);
842 /* Create edges for a goto statement at block BB. */
844 static void
845 make_goto_expr_edges (basic_block bb)
847 tree goto_t;
848 basic_block target_bb;
849 int for_call;
850 block_stmt_iterator last = bsi_last (bb);
852 goto_t = bsi_stmt (last);
854 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
855 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
856 from a nonlocal goto. */
857 if (TREE_CODE (goto_t) != GOTO_EXPR)
858 for_call = 1;
859 else
861 tree dest = GOTO_DESTINATION (goto_t);
862 for_call = 0;
864 /* A GOTO to a local label creates normal edges. */
865 if (simple_goto_p (goto_t))
867 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
868 #ifdef USE_MAPPED_LOCATION
869 e->goto_locus = EXPR_LOCATION (goto_t);
870 #else
871 e->goto_locus = EXPR_LOCUS (goto_t);
872 #endif
873 bsi_remove (&last);
874 return;
877 /* Nothing more to do for nonlocal gotos. */
878 if (TREE_CODE (dest) == LABEL_DECL)
879 return;
881 /* Computed gotos remain. */
884 /* Look for the block starting with the destination label. In the
885 case of a computed goto, make an edge to any label block we find
886 in the CFG. */
887 FOR_EACH_BB (target_bb)
889 block_stmt_iterator bsi;
891 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
893 tree target = bsi_stmt (bsi);
895 if (TREE_CODE (target) != LABEL_EXPR)
896 break;
898 if (
899 /* Computed GOTOs. Make an edge to every label block that has
900 been marked as a potential target for a computed goto. */
901 (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0)
902 /* Nonlocal GOTO target. Make an edge to every label block
903 that has been marked as a potential target for a nonlocal
904 goto. */
905 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1))
907 make_edge (bb, target_bb, EDGE_ABNORMAL);
908 break;
913 /* Degenerate case of computed goto with no labels. */
914 if (!for_call && EDGE_COUNT (bb->succs) == 0)
915 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
919 /*---------------------------------------------------------------------------
920 Flowgraph analysis
921 ---------------------------------------------------------------------------*/
923 /* Remove unreachable blocks and other miscellaneous clean up work. */
925 bool
926 cleanup_tree_cfg (void)
928 bool retval = false;
930 timevar_push (TV_TREE_CLEANUP_CFG);
932 retval = cleanup_control_flow ();
933 retval |= delete_unreachable_blocks ();
935 /* cleanup_forwarder_blocks can redirect edges out of SWITCH_EXPRs,
936 which can get expensive. So we want to enable recording of edge
937 to CASE_LABEL_EXPR mappings around the call to
938 cleanup_forwarder_blocks. */
939 start_recording_case_labels ();
940 retval |= cleanup_forwarder_blocks ();
941 end_recording_case_labels ();
943 #ifdef ENABLE_CHECKING
944 if (retval)
946 gcc_assert (!cleanup_control_flow ());
947 gcc_assert (!delete_unreachable_blocks ());
948 gcc_assert (!cleanup_forwarder_blocks ());
950 #endif
952 /* Merging the blocks creates no new opportunities for the other
953 optimizations, so do it here. */
954 retval |= merge_seq_blocks ();
956 compact_blocks ();
958 #ifdef ENABLE_CHECKING
959 verify_flow_info ();
960 #endif
961 timevar_pop (TV_TREE_CLEANUP_CFG);
962 return retval;
966 /* Cleanup cfg and repair loop structures. */
968 void
969 cleanup_tree_cfg_loop (void)
971 bitmap changed_bbs = BITMAP_ALLOC (NULL);
973 cleanup_tree_cfg ();
975 fix_loop_structure (current_loops, changed_bbs);
976 calculate_dominance_info (CDI_DOMINATORS);
978 /* This usually does nothing. But sometimes parts of cfg that originally
979 were inside a loop get out of it due to edge removal (since they
980 become unreachable by back edges from latch). */
981 rewrite_into_loop_closed_ssa (changed_bbs, TODO_update_ssa);
983 BITMAP_FREE (changed_bbs);
985 #ifdef ENABLE_CHECKING
986 verify_loop_structure (current_loops);
987 #endif
990 /* Cleanup useless labels in basic blocks. This is something we wish
991 to do early because it allows us to group case labels before creating
992 the edges for the CFG, and it speeds up block statement iterators in
993 all passes later on.
994 We only run this pass once, running it more than once is probably not
995 profitable. */
997 /* A map from basic block index to the leading label of that block. */
998 static tree *label_for_bb;
1000 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
1001 static void
1002 update_eh_label (struct eh_region *region)
1004 tree old_label = get_eh_region_tree_label (region);
1005 if (old_label)
1007 tree new_label;
1008 basic_block bb = label_to_block (old_label);
1010 /* ??? After optimizing, there may be EH regions with labels
1011 that have already been removed from the function body, so
1012 there is no basic block for them. */
1013 if (! bb)
1014 return;
1016 new_label = label_for_bb[bb->index];
1017 set_eh_region_tree_label (region, new_label);
1021 /* Given LABEL return the first label in the same basic block. */
1022 static tree
1023 main_block_label (tree label)
1025 basic_block bb = label_to_block (label);
1027 /* label_to_block possibly inserted undefined label into the chain. */
1028 if (!label_for_bb[bb->index])
1029 label_for_bb[bb->index] = label;
1030 return label_for_bb[bb->index];
1033 /* Cleanup redundant labels. This is a three-step process:
1034 1) Find the leading label for each block.
1035 2) Redirect all references to labels to the leading labels.
1036 3) Cleanup all useless labels. */
1038 void
1039 cleanup_dead_labels (void)
1041 basic_block bb;
1042 label_for_bb = xcalloc (last_basic_block, sizeof (tree));
1044 /* Find a suitable label for each block. We use the first user-defined
1045 label if there is one, or otherwise just the first label we see. */
1046 FOR_EACH_BB (bb)
1048 block_stmt_iterator i;
1050 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
1052 tree label, stmt = bsi_stmt (i);
1054 if (TREE_CODE (stmt) != LABEL_EXPR)
1055 break;
1057 label = LABEL_EXPR_LABEL (stmt);
1059 /* If we have not yet seen a label for the current block,
1060 remember this one and see if there are more labels. */
1061 if (! label_for_bb[bb->index])
1063 label_for_bb[bb->index] = label;
1064 continue;
1067 /* If we did see a label for the current block already, but it
1068 is an artificially created label, replace it if the current
1069 label is a user defined label. */
1070 if (! DECL_ARTIFICIAL (label)
1071 && DECL_ARTIFICIAL (label_for_bb[bb->index]))
1073 label_for_bb[bb->index] = label;
1074 break;
1079 /* Now redirect all jumps/branches to the selected label.
1080 First do so for each block ending in a control statement. */
1081 FOR_EACH_BB (bb)
1083 tree stmt = last_stmt (bb);
1084 if (!stmt)
1085 continue;
1087 switch (TREE_CODE (stmt))
1089 case COND_EXPR:
1091 tree true_branch, false_branch;
1093 true_branch = COND_EXPR_THEN (stmt);
1094 false_branch = COND_EXPR_ELSE (stmt);
1096 GOTO_DESTINATION (true_branch)
1097 = main_block_label (GOTO_DESTINATION (true_branch));
1098 GOTO_DESTINATION (false_branch)
1099 = main_block_label (GOTO_DESTINATION (false_branch));
1101 break;
1104 case SWITCH_EXPR:
1106 size_t i;
1107 tree vec = SWITCH_LABELS (stmt);
1108 size_t n = TREE_VEC_LENGTH (vec);
1110 /* Replace all destination labels. */
1111 for (i = 0; i < n; ++i)
1113 tree elt = TREE_VEC_ELT (vec, i);
1114 tree label = main_block_label (CASE_LABEL (elt));
1115 CASE_LABEL (elt) = label;
1117 break;
1120 /* We have to handle GOTO_EXPRs until they're removed, and we don't
1121 remove them until after we've created the CFG edges. */
1122 case GOTO_EXPR:
1123 if (! computed_goto_p (stmt))
1125 GOTO_DESTINATION (stmt)
1126 = main_block_label (GOTO_DESTINATION (stmt));
1127 break;
1130 default:
1131 break;
1135 for_each_eh_region (update_eh_label);
1137 /* Finally, purge dead labels. All user-defined labels and labels that
1138 can be the target of non-local gotos are preserved. */
1139 FOR_EACH_BB (bb)
1141 block_stmt_iterator i;
1142 tree label_for_this_bb = label_for_bb[bb->index];
1144 if (! label_for_this_bb)
1145 continue;
1147 for (i = bsi_start (bb); !bsi_end_p (i); )
1149 tree label, stmt = bsi_stmt (i);
1151 if (TREE_CODE (stmt) != LABEL_EXPR)
1152 break;
1154 label = LABEL_EXPR_LABEL (stmt);
1156 if (label == label_for_this_bb
1157 || ! DECL_ARTIFICIAL (label)
1158 || DECL_NONLOCAL (label))
1159 bsi_next (&i);
1160 else
1161 bsi_remove (&i);
1165 free (label_for_bb);
1168 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1169 and scan the sorted vector of cases. Combine the ones jumping to the
1170 same label.
1171 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1173 void
1174 group_case_labels (void)
1176 basic_block bb;
1178 FOR_EACH_BB (bb)
1180 tree stmt = last_stmt (bb);
1181 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1183 tree labels = SWITCH_LABELS (stmt);
1184 int old_size = TREE_VEC_LENGTH (labels);
1185 int i, j, new_size = old_size;
1186 tree default_case = TREE_VEC_ELT (labels, old_size - 1);
1187 tree default_label;
1189 /* The default label is always the last case in a switch
1190 statement after gimplification. */
1191 default_label = CASE_LABEL (default_case);
1193 /* Look for possible opportunities to merge cases.
1194 Ignore the last element of the label vector because it
1195 must be the default case. */
1196 i = 0;
1197 while (i < old_size - 1)
1199 tree base_case, base_label, base_high;
1200 base_case = TREE_VEC_ELT (labels, i);
1202 gcc_assert (base_case);
1203 base_label = CASE_LABEL (base_case);
1205 /* Discard cases that have the same destination as the
1206 default case. */
1207 if (base_label == default_label)
1209 TREE_VEC_ELT (labels, i) = NULL_TREE;
1210 i++;
1211 new_size--;
1212 continue;
1215 base_high = CASE_HIGH (base_case) ?
1216 CASE_HIGH (base_case) : CASE_LOW (base_case);
1217 i++;
1218 /* Try to merge case labels. Break out when we reach the end
1219 of the label vector or when we cannot merge the next case
1220 label with the current one. */
1221 while (i < old_size - 1)
1223 tree merge_case = TREE_VEC_ELT (labels, i);
1224 tree merge_label = CASE_LABEL (merge_case);
1225 tree t = int_const_binop (PLUS_EXPR, base_high,
1226 integer_one_node, 1);
1228 /* Merge the cases if they jump to the same place,
1229 and their ranges are consecutive. */
1230 if (merge_label == base_label
1231 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1233 base_high = CASE_HIGH (merge_case) ?
1234 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1235 CASE_HIGH (base_case) = base_high;
1236 TREE_VEC_ELT (labels, i) = NULL_TREE;
1237 new_size--;
1238 i++;
1240 else
1241 break;
1245 /* Compress the case labels in the label vector, and adjust the
1246 length of the vector. */
1247 for (i = 0, j = 0; i < new_size; i++)
1249 while (! TREE_VEC_ELT (labels, j))
1250 j++;
1251 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1253 TREE_VEC_LENGTH (labels) = new_size;
1258 /* Checks whether we can merge block B into block A. */
1260 static bool
1261 tree_can_merge_blocks_p (basic_block a, basic_block b)
1263 tree stmt;
1264 block_stmt_iterator bsi;
1265 tree phi;
1267 if (!single_succ_p (a))
1268 return false;
1270 if (single_succ_edge (a)->flags & EDGE_ABNORMAL)
1271 return false;
1273 if (single_succ (a) != b)
1274 return false;
1276 if (!single_pred_p (b))
1277 return false;
1279 if (b == EXIT_BLOCK_PTR)
1280 return false;
1282 /* If A ends by a statement causing exceptions or something similar, we
1283 cannot merge the blocks. */
1284 stmt = last_stmt (a);
1285 if (stmt && stmt_ends_bb_p (stmt))
1286 return false;
1288 /* Do not allow a block with only a non-local label to be merged. */
1289 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1290 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1291 return false;
1293 /* It must be possible to eliminate all phi nodes in B. If ssa form
1294 is not up-to-date, we cannot eliminate any phis. */
1295 phi = phi_nodes (b);
1296 if (phi)
1298 if (need_ssa_update_p ())
1299 return false;
1301 for (; phi; phi = PHI_CHAIN (phi))
1302 if (!is_gimple_reg (PHI_RESULT (phi))
1303 && !may_propagate_copy (PHI_RESULT (phi), PHI_ARG_DEF (phi, 0)))
1304 return false;
1307 /* Do not remove user labels. */
1308 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1310 stmt = bsi_stmt (bsi);
1311 if (TREE_CODE (stmt) != LABEL_EXPR)
1312 break;
1313 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1314 return false;
1317 /* Protect the loop latches. */
1318 if (current_loops
1319 && b->loop_father->latch == b)
1320 return false;
1322 return true;
1325 /* Replaces all uses of NAME by VAL. */
1327 void
1328 replace_uses_by (tree name, tree val)
1330 imm_use_iterator imm_iter;
1331 use_operand_p use;
1332 tree stmt;
1333 edge e;
1334 unsigned i;
1335 VEC(tree,heap) *stmts = VEC_alloc (tree, heap, 20);
1337 FOR_EACH_IMM_USE_SAFE (use, imm_iter, name)
1339 stmt = USE_STMT (use);
1341 SET_USE (use, val);
1343 if (TREE_CODE (stmt) == PHI_NODE)
1345 e = PHI_ARG_EDGE (stmt, PHI_ARG_INDEX_FROM_USE (use));
1346 if (e->flags & EDGE_ABNORMAL)
1348 /* This can only occur for virtual operands, since
1349 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1350 would prevent replacement. */
1351 gcc_assert (!is_gimple_reg (name));
1352 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1355 else
1356 VEC_safe_push (tree, heap, stmts, stmt);
1359 /* We do not update the statements in the loop above. Consider
1360 x = w * w;
1362 If we performed the update in the first loop, the statement
1363 would be rescanned after first occurrence of w is replaced,
1364 the new uses would be placed to the beginning of the list,
1365 and we would never process them. */
1366 for (i = 0; VEC_iterate (tree, stmts, i, stmt); i++)
1368 tree rhs;
1370 fold_stmt_inplace (stmt);
1372 rhs = get_rhs (stmt);
1373 if (TREE_CODE (rhs) == ADDR_EXPR)
1374 recompute_tree_invarant_for_addr_expr (rhs);
1376 update_stmt (stmt);
1379 VEC_free (tree, heap, stmts);
1382 /* Merge block B into block A. */
1384 static void
1385 tree_merge_blocks (basic_block a, basic_block b)
1387 block_stmt_iterator bsi;
1388 tree_stmt_iterator last;
1389 tree phi;
1391 if (dump_file)
1392 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1394 /* Remove the phi nodes. */
1395 bsi = bsi_last (a);
1396 for (phi = phi_nodes (b); phi; phi = phi_nodes (b))
1398 tree def = PHI_RESULT (phi), use = PHI_ARG_DEF (phi, 0);
1399 tree copy;
1401 if (!may_propagate_copy (def, use)
1402 /* Propagating pointers might cause the set of vops for statements
1403 to be changed, and thus require ssa form update. */
1404 || (is_gimple_reg (def)
1405 && POINTER_TYPE_P (TREE_TYPE (def))))
1407 gcc_assert (is_gimple_reg (def));
1409 /* Note that just emitting the copies is fine -- there is no problem
1410 with ordering of phi nodes. This is because A is the single
1411 predecessor of B, therefore results of the phi nodes cannot
1412 appear as arguments of the phi nodes. */
1413 copy = build2 (MODIFY_EXPR, void_type_node, def, use);
1414 bsi_insert_after (&bsi, copy, BSI_NEW_STMT);
1415 SET_PHI_RESULT (phi, NULL_TREE);
1416 SSA_NAME_DEF_STMT (def) = copy;
1418 else
1419 replace_uses_by (def, use);
1420 remove_phi_node (phi, NULL);
1423 /* Ensure that B follows A. */
1424 move_block_after (b, a);
1426 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1427 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1429 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1430 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1432 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1434 tree label = bsi_stmt (bsi);
1436 bsi_remove (&bsi);
1437 /* Now that we can thread computed gotos, we might have
1438 a situation where we have a forced label in block B
1439 However, the label at the start of block B might still be
1440 used in other ways (think about the runtime checking for
1441 Fortran assigned gotos). So we can not just delete the
1442 label. Instead we move the label to the start of block A. */
1443 if (FORCED_LABEL (LABEL_EXPR_LABEL (label)))
1445 block_stmt_iterator dest_bsi = bsi_start (a);
1446 bsi_insert_before (&dest_bsi, label, BSI_NEW_STMT);
1449 else
1451 set_bb_for_stmt (bsi_stmt (bsi), a);
1452 bsi_next (&bsi);
1456 /* Merge the chains. */
1457 last = tsi_last (a->stmt_list);
1458 tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT);
1459 b->stmt_list = NULL;
1463 /* Walk the function tree removing unnecessary statements.
1465 * Empty statement nodes are removed
1467 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1469 * Unnecessary COND_EXPRs are removed
1471 * Some unnecessary BIND_EXPRs are removed
1473 Clearly more work could be done. The trick is doing the analysis
1474 and removal fast enough to be a net improvement in compile times.
1476 Note that when we remove a control structure such as a COND_EXPR
1477 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1478 to ensure we eliminate all the useless code. */
1480 struct rus_data
1482 tree *last_goto;
1483 bool repeat;
1484 bool may_throw;
1485 bool may_branch;
1486 bool has_label;
1489 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1491 static bool
1492 remove_useless_stmts_warn_notreached (tree stmt)
1494 if (EXPR_HAS_LOCATION (stmt))
1496 location_t loc = EXPR_LOCATION (stmt);
1497 if (LOCATION_LINE (loc) > 0)
1499 warning (0, "%Hwill never be executed", &loc);
1500 return true;
1504 switch (TREE_CODE (stmt))
1506 case STATEMENT_LIST:
1508 tree_stmt_iterator i;
1509 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1510 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1511 return true;
1513 break;
1515 case COND_EXPR:
1516 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1517 return true;
1518 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1519 return true;
1520 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1521 return true;
1522 break;
1524 case TRY_FINALLY_EXPR:
1525 case TRY_CATCH_EXPR:
1526 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1527 return true;
1528 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1529 return true;
1530 break;
1532 case CATCH_EXPR:
1533 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1534 case EH_FILTER_EXPR:
1535 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1536 case BIND_EXPR:
1537 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1539 default:
1540 /* Not a live container. */
1541 break;
1544 return false;
1547 static void
1548 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1550 tree then_clause, else_clause, cond;
1551 bool save_has_label, then_has_label, else_has_label;
1553 save_has_label = data->has_label;
1554 data->has_label = false;
1555 data->last_goto = NULL;
1557 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1559 then_has_label = data->has_label;
1560 data->has_label = false;
1561 data->last_goto = NULL;
1563 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1565 else_has_label = data->has_label;
1566 data->has_label = save_has_label | then_has_label | else_has_label;
1568 then_clause = COND_EXPR_THEN (*stmt_p);
1569 else_clause = COND_EXPR_ELSE (*stmt_p);
1570 cond = fold (COND_EXPR_COND (*stmt_p));
1572 /* If neither arm does anything at all, we can remove the whole IF. */
1573 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1575 *stmt_p = build_empty_stmt ();
1576 data->repeat = true;
1579 /* If there are no reachable statements in an arm, then we can
1580 zap the entire conditional. */
1581 else if (integer_nonzerop (cond) && !else_has_label)
1583 if (warn_notreached)
1584 remove_useless_stmts_warn_notreached (else_clause);
1585 *stmt_p = then_clause;
1586 data->repeat = true;
1588 else if (integer_zerop (cond) && !then_has_label)
1590 if (warn_notreached)
1591 remove_useless_stmts_warn_notreached (then_clause);
1592 *stmt_p = else_clause;
1593 data->repeat = true;
1596 /* Check a couple of simple things on then/else with single stmts. */
1597 else
1599 tree then_stmt = expr_only (then_clause);
1600 tree else_stmt = expr_only (else_clause);
1602 /* Notice branches to a common destination. */
1603 if (then_stmt && else_stmt
1604 && TREE_CODE (then_stmt) == GOTO_EXPR
1605 && TREE_CODE (else_stmt) == GOTO_EXPR
1606 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1608 *stmt_p = then_stmt;
1609 data->repeat = true;
1612 /* If the THEN/ELSE clause merely assigns a value to a variable or
1613 parameter which is already known to contain that value, then
1614 remove the useless THEN/ELSE clause. */
1615 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1617 if (else_stmt
1618 && TREE_CODE (else_stmt) == MODIFY_EXPR
1619 && TREE_OPERAND (else_stmt, 0) == cond
1620 && integer_zerop (TREE_OPERAND (else_stmt, 1)))
1621 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1623 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1624 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1625 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1626 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1628 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1629 ? then_stmt : else_stmt);
1630 tree *location = (TREE_CODE (cond) == EQ_EXPR
1631 ? &COND_EXPR_THEN (*stmt_p)
1632 : &COND_EXPR_ELSE (*stmt_p));
1634 if (stmt
1635 && TREE_CODE (stmt) == MODIFY_EXPR
1636 && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1637 && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1638 *location = alloc_stmt_list ();
1642 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1643 would be re-introduced during lowering. */
1644 data->last_goto = NULL;
1648 static void
1649 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1651 bool save_may_branch, save_may_throw;
1652 bool this_may_branch, this_may_throw;
1654 /* Collect may_branch and may_throw information for the body only. */
1655 save_may_branch = data->may_branch;
1656 save_may_throw = data->may_throw;
1657 data->may_branch = false;
1658 data->may_throw = false;
1659 data->last_goto = NULL;
1661 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1663 this_may_branch = data->may_branch;
1664 this_may_throw = data->may_throw;
1665 data->may_branch |= save_may_branch;
1666 data->may_throw |= save_may_throw;
1667 data->last_goto = NULL;
1669 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1671 /* If the body is empty, then we can emit the FINALLY block without
1672 the enclosing TRY_FINALLY_EXPR. */
1673 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1675 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1676 data->repeat = true;
1679 /* If the handler is empty, then we can emit the TRY block without
1680 the enclosing TRY_FINALLY_EXPR. */
1681 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1683 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1684 data->repeat = true;
1687 /* If the body neither throws, nor branches, then we can safely
1688 string the TRY and FINALLY blocks together. */
1689 else if (!this_may_branch && !this_may_throw)
1691 tree stmt = *stmt_p;
1692 *stmt_p = TREE_OPERAND (stmt, 0);
1693 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1694 data->repeat = true;
1699 static void
1700 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1702 bool save_may_throw, this_may_throw;
1703 tree_stmt_iterator i;
1704 tree stmt;
1706 /* Collect may_throw information for the body only. */
1707 save_may_throw = data->may_throw;
1708 data->may_throw = false;
1709 data->last_goto = NULL;
1711 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1713 this_may_throw = data->may_throw;
1714 data->may_throw = save_may_throw;
1716 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1717 if (!this_may_throw)
1719 if (warn_notreached)
1720 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1721 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1722 data->repeat = true;
1723 return;
1726 /* Process the catch clause specially. We may be able to tell that
1727 no exceptions propagate past this point. */
1729 this_may_throw = true;
1730 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1731 stmt = tsi_stmt (i);
1732 data->last_goto = NULL;
1734 switch (TREE_CODE (stmt))
1736 case CATCH_EXPR:
1737 for (; !tsi_end_p (i); tsi_next (&i))
1739 stmt = tsi_stmt (i);
1740 /* If we catch all exceptions, then the body does not
1741 propagate exceptions past this point. */
1742 if (CATCH_TYPES (stmt) == NULL)
1743 this_may_throw = false;
1744 data->last_goto = NULL;
1745 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1747 break;
1749 case EH_FILTER_EXPR:
1750 if (EH_FILTER_MUST_NOT_THROW (stmt))
1751 this_may_throw = false;
1752 else if (EH_FILTER_TYPES (stmt) == NULL)
1753 this_may_throw = false;
1754 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1755 break;
1757 default:
1758 /* Otherwise this is a cleanup. */
1759 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1761 /* If the cleanup is empty, then we can emit the TRY block without
1762 the enclosing TRY_CATCH_EXPR. */
1763 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1765 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1766 data->repeat = true;
1768 break;
1770 data->may_throw |= this_may_throw;
1774 static void
1775 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1777 tree block;
1779 /* First remove anything underneath the BIND_EXPR. */
1780 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1782 /* If the BIND_EXPR has no variables, then we can pull everything
1783 up one level and remove the BIND_EXPR, unless this is the toplevel
1784 BIND_EXPR for the current function or an inlined function.
1786 When this situation occurs we will want to apply this
1787 optimization again. */
1788 block = BIND_EXPR_BLOCK (*stmt_p);
1789 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1790 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1791 && (! block
1792 || ! BLOCK_ABSTRACT_ORIGIN (block)
1793 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1794 != FUNCTION_DECL)))
1796 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1797 data->repeat = true;
1802 static void
1803 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1805 tree dest = GOTO_DESTINATION (*stmt_p);
1807 data->may_branch = true;
1808 data->last_goto = NULL;
1810 /* Record the last goto expr, so that we can delete it if unnecessary. */
1811 if (TREE_CODE (dest) == LABEL_DECL)
1812 data->last_goto = stmt_p;
1816 static void
1817 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1819 tree label = LABEL_EXPR_LABEL (*stmt_p);
1821 data->has_label = true;
1823 /* We do want to jump across non-local label receiver code. */
1824 if (DECL_NONLOCAL (label))
1825 data->last_goto = NULL;
1827 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1829 *data->last_goto = build_empty_stmt ();
1830 data->repeat = true;
1833 /* ??? Add something here to delete unused labels. */
1837 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1838 decl. This allows us to eliminate redundant or useless
1839 calls to "const" functions.
1841 Gimplifier already does the same operation, but we may notice functions
1842 being const and pure once their calls has been gimplified, so we need
1843 to update the flag. */
1845 static void
1846 update_call_expr_flags (tree call)
1848 tree decl = get_callee_fndecl (call);
1849 if (!decl)
1850 return;
1851 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1852 TREE_SIDE_EFFECTS (call) = 0;
1853 if (TREE_NOTHROW (decl))
1854 TREE_NOTHROW (call) = 1;
1858 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1860 void
1861 notice_special_calls (tree t)
1863 int flags = call_expr_flags (t);
1865 if (flags & ECF_MAY_BE_ALLOCA)
1866 current_function_calls_alloca = true;
1867 if (flags & ECF_RETURNS_TWICE)
1868 current_function_calls_setjmp = true;
1872 /* Clear flags set by notice_special_calls. Used by dead code removal
1873 to update the flags. */
1875 void
1876 clear_special_calls (void)
1878 current_function_calls_alloca = false;
1879 current_function_calls_setjmp = false;
1883 static void
1884 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1886 tree t = *tp, op;
1888 switch (TREE_CODE (t))
1890 case COND_EXPR:
1891 remove_useless_stmts_cond (tp, data);
1892 break;
1894 case TRY_FINALLY_EXPR:
1895 remove_useless_stmts_tf (tp, data);
1896 break;
1898 case TRY_CATCH_EXPR:
1899 remove_useless_stmts_tc (tp, data);
1900 break;
1902 case BIND_EXPR:
1903 remove_useless_stmts_bind (tp, data);
1904 break;
1906 case GOTO_EXPR:
1907 remove_useless_stmts_goto (tp, data);
1908 break;
1910 case LABEL_EXPR:
1911 remove_useless_stmts_label (tp, data);
1912 break;
1914 case RETURN_EXPR:
1915 fold_stmt (tp);
1916 data->last_goto = NULL;
1917 data->may_branch = true;
1918 break;
1920 case CALL_EXPR:
1921 fold_stmt (tp);
1922 data->last_goto = NULL;
1923 notice_special_calls (t);
1924 update_call_expr_flags (t);
1925 if (tree_could_throw_p (t))
1926 data->may_throw = true;
1927 break;
1929 case MODIFY_EXPR:
1930 data->last_goto = NULL;
1931 fold_stmt (tp);
1932 op = get_call_expr_in (t);
1933 if (op)
1935 update_call_expr_flags (op);
1936 notice_special_calls (op);
1938 if (tree_could_throw_p (t))
1939 data->may_throw = true;
1940 break;
1942 case STATEMENT_LIST:
1944 tree_stmt_iterator i = tsi_start (t);
1945 while (!tsi_end_p (i))
1947 t = tsi_stmt (i);
1948 if (IS_EMPTY_STMT (t))
1950 tsi_delink (&i);
1951 continue;
1954 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1956 t = tsi_stmt (i);
1957 if (TREE_CODE (t) == STATEMENT_LIST)
1959 tsi_link_before (&i, t, TSI_SAME_STMT);
1960 tsi_delink (&i);
1962 else
1963 tsi_next (&i);
1966 break;
1967 case ASM_EXPR:
1968 fold_stmt (tp);
1969 data->last_goto = NULL;
1970 break;
1972 default:
1973 data->last_goto = NULL;
1974 break;
1978 static void
1979 remove_useless_stmts (void)
1981 struct rus_data data;
1983 clear_special_calls ();
1987 memset (&data, 0, sizeof (data));
1988 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1990 while (data.repeat);
1994 struct tree_opt_pass pass_remove_useless_stmts =
1996 "useless", /* name */
1997 NULL, /* gate */
1998 remove_useless_stmts, /* execute */
1999 NULL, /* sub */
2000 NULL, /* next */
2001 0, /* static_pass_number */
2002 0, /* tv_id */
2003 PROP_gimple_any, /* properties_required */
2004 0, /* properties_provided */
2005 0, /* properties_destroyed */
2006 0, /* todo_flags_start */
2007 TODO_dump_func, /* todo_flags_finish */
2008 0 /* letter */
2011 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
2013 static void
2014 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
2016 tree phi;
2018 /* Since this block is no longer reachable, we can just delete all
2019 of its PHI nodes. */
2020 phi = phi_nodes (bb);
2021 while (phi)
2023 tree next = PHI_CHAIN (phi);
2024 remove_phi_node (phi, NULL_TREE);
2025 phi = next;
2028 /* Remove edges to BB's successors. */
2029 while (EDGE_COUNT (bb->succs) > 0)
2030 remove_edge (EDGE_SUCC (bb, 0));
2034 /* Remove statements of basic block BB. */
2036 static void
2037 remove_bb (basic_block bb)
2039 block_stmt_iterator i;
2040 #ifdef USE_MAPPED_LOCATION
2041 source_location loc = UNKNOWN_LOCATION;
2042 #else
2043 source_locus loc = 0;
2044 #endif
2046 if (dump_file)
2048 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2049 if (dump_flags & TDF_DETAILS)
2051 dump_bb (bb, dump_file, 0);
2052 fprintf (dump_file, "\n");
2056 /* If we remove the header or the latch of a loop, mark the loop for
2057 removal by setting its header and latch to NULL. */
2058 if (current_loops)
2060 struct loop *loop = bb->loop_father;
2062 if (loop->latch == bb
2063 || loop->header == bb)
2065 loop->latch = NULL;
2066 loop->header = NULL;
2070 /* Remove all the instructions in the block. */
2071 for (i = bsi_start (bb); !bsi_end_p (i);)
2073 tree stmt = bsi_stmt (i);
2074 if (TREE_CODE (stmt) == LABEL_EXPR
2075 && FORCED_LABEL (LABEL_EXPR_LABEL (stmt)))
2077 basic_block new_bb = bb->prev_bb;
2078 block_stmt_iterator new_bsi = bsi_start (new_bb);
2080 bsi_remove (&i);
2081 bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
2083 else
2085 release_defs (stmt);
2087 bsi_remove (&i);
2090 /* Don't warn for removed gotos. Gotos are often removed due to
2091 jump threading, thus resulting in bogus warnings. Not great,
2092 since this way we lose warnings for gotos in the original
2093 program that are indeed unreachable. */
2094 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
2096 #ifdef USE_MAPPED_LOCATION
2097 if (EXPR_HAS_LOCATION (stmt))
2098 loc = EXPR_LOCATION (stmt);
2099 #else
2100 source_locus t;
2101 t = EXPR_LOCUS (stmt);
2102 if (t && LOCATION_LINE (*t) > 0)
2103 loc = t;
2104 #endif
2108 /* If requested, give a warning that the first statement in the
2109 block is unreachable. We walk statements backwards in the
2110 loop above, so the last statement we process is the first statement
2111 in the block. */
2112 #ifdef USE_MAPPED_LOCATION
2113 if (loc > BUILTINS_LOCATION)
2114 warning (OPT_Wunreachable_code, "%Hwill never be executed", &loc);
2115 #else
2116 if (loc)
2117 warning (OPT_Wunreachable_code, "%Hwill never be executed", loc);
2118 #endif
2120 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2123 /* A list of all the noreturn calls passed to modify_stmt.
2124 cleanup_control_flow uses it to detect cases where a mid-block
2125 indirect call has been turned into a noreturn call. When this
2126 happens, all the instructions after the call are no longer
2127 reachable and must be deleted as dead. */
2129 VEC(tree,gc) *modified_noreturn_calls;
2131 /* Try to remove superfluous control structures. */
2133 static bool
2134 cleanup_control_flow (void)
2136 basic_block bb;
2137 block_stmt_iterator bsi;
2138 bool retval = false;
2139 tree stmt;
2141 /* Detect cases where a mid-block call is now known not to return. */
2142 while (VEC_length (tree, modified_noreturn_calls))
2144 stmt = VEC_pop (tree, modified_noreturn_calls);
2145 bb = bb_for_stmt (stmt);
2146 if (bb != NULL && last_stmt (bb) != stmt && noreturn_call_p (stmt))
2147 split_block (bb, stmt);
2150 FOR_EACH_BB (bb)
2152 bsi = bsi_last (bb);
2154 if (bsi_end_p (bsi))
2155 continue;
2157 stmt = bsi_stmt (bsi);
2158 if (TREE_CODE (stmt) == COND_EXPR
2159 || TREE_CODE (stmt) == SWITCH_EXPR)
2160 retval |= cleanup_control_expr_graph (bb, bsi);
2162 /* If we had a computed goto which has a compile-time determinable
2163 destination, then we can eliminate the goto. */
2164 if (TREE_CODE (stmt) == GOTO_EXPR
2165 && TREE_CODE (GOTO_DESTINATION (stmt)) == ADDR_EXPR
2166 && TREE_CODE (TREE_OPERAND (GOTO_DESTINATION (stmt), 0)) == LABEL_DECL)
2168 edge e;
2169 tree label;
2170 edge_iterator ei;
2171 basic_block target_block;
2172 bool removed_edge = false;
2174 /* First look at all the outgoing edges. Delete any outgoing
2175 edges which do not go to the right block. For the one
2176 edge which goes to the right block, fix up its flags. */
2177 label = TREE_OPERAND (GOTO_DESTINATION (stmt), 0);
2178 target_block = label_to_block (label);
2179 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2181 if (e->dest != target_block)
2183 removed_edge = true;
2184 remove_edge (e);
2186 else
2188 /* Turn off the EDGE_ABNORMAL flag. */
2189 e->flags &= ~EDGE_ABNORMAL;
2191 /* And set EDGE_FALLTHRU. */
2192 e->flags |= EDGE_FALLTHRU;
2193 ei_next (&ei);
2197 /* If we removed one or more edges, then we will need to fix the
2198 dominators. It may be possible to incrementally update them. */
2199 if (removed_edge)
2200 free_dominance_info (CDI_DOMINATORS);
2202 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
2203 relevant information we need. */
2204 bsi_remove (&bsi);
2205 retval = true;
2208 /* Check for indirect calls that have been turned into
2209 noreturn calls. */
2210 if (noreturn_call_p (stmt) && remove_fallthru_edge (bb->succs))
2212 free_dominance_info (CDI_DOMINATORS);
2213 retval = true;
2216 return retval;
2220 /* Disconnect an unreachable block in the control expression starting
2221 at block BB. */
2223 static bool
2224 cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
2226 edge taken_edge;
2227 bool retval = false;
2228 tree expr = bsi_stmt (bsi), val;
2230 if (!single_succ_p (bb))
2232 edge e;
2233 edge_iterator ei;
2235 switch (TREE_CODE (expr))
2237 case COND_EXPR:
2238 val = COND_EXPR_COND (expr);
2239 break;
2241 case SWITCH_EXPR:
2242 val = SWITCH_COND (expr);
2243 if (TREE_CODE (val) != INTEGER_CST)
2244 return false;
2245 break;
2247 default:
2248 gcc_unreachable ();
2251 taken_edge = find_taken_edge (bb, val);
2252 if (!taken_edge)
2253 return false;
2255 /* Remove all the edges except the one that is always executed. */
2256 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2258 if (e != taken_edge)
2260 taken_edge->probability += e->probability;
2261 taken_edge->count += e->count;
2262 remove_edge (e);
2263 retval = true;
2265 else
2266 ei_next (&ei);
2268 if (taken_edge->probability > REG_BR_PROB_BASE)
2269 taken_edge->probability = REG_BR_PROB_BASE;
2271 else
2272 taken_edge = single_succ_edge (bb);
2274 bsi_remove (&bsi);
2275 taken_edge->flags = EDGE_FALLTHRU;
2277 /* We removed some paths from the cfg. */
2278 free_dominance_info (CDI_DOMINATORS);
2280 return retval;
2283 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
2285 static bool
2286 remove_fallthru_edge (VEC(edge,gc) *ev)
2288 edge_iterator ei;
2289 edge e;
2291 FOR_EACH_EDGE (e, ei, ev)
2292 if ((e->flags & EDGE_FALLTHRU) != 0)
2294 remove_edge (e);
2295 return true;
2297 return false;
2300 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2301 predicate VAL, return the edge that will be taken out of the block.
2302 If VAL does not match a unique edge, NULL is returned. */
2304 edge
2305 find_taken_edge (basic_block bb, tree val)
2307 tree stmt;
2309 stmt = last_stmt (bb);
2311 gcc_assert (stmt);
2312 gcc_assert (is_ctrl_stmt (stmt));
2313 gcc_assert (val);
2315 if (! is_gimple_min_invariant (val))
2316 return NULL;
2318 if (TREE_CODE (stmt) == COND_EXPR)
2319 return find_taken_edge_cond_expr (bb, val);
2321 if (TREE_CODE (stmt) == SWITCH_EXPR)
2322 return find_taken_edge_switch_expr (bb, val);
2324 if (computed_goto_p (stmt))
2325 return find_taken_edge_computed_goto (bb, TREE_OPERAND( val, 0));
2327 gcc_unreachable ();
2330 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2331 statement, determine which of the outgoing edges will be taken out of the
2332 block. Return NULL if either edge may be taken. */
2334 static edge
2335 find_taken_edge_computed_goto (basic_block bb, tree val)
2337 basic_block dest;
2338 edge e = NULL;
2340 dest = label_to_block (val);
2341 if (dest)
2343 e = find_edge (bb, dest);
2344 gcc_assert (e != NULL);
2347 return e;
2350 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2351 statement, determine which of the two edges will be taken out of the
2352 block. Return NULL if either edge may be taken. */
2354 static edge
2355 find_taken_edge_cond_expr (basic_block bb, tree val)
2357 edge true_edge, false_edge;
2359 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2361 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2362 return (zero_p (val) ? false_edge : true_edge);
2365 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2366 statement, determine which edge will be taken out of the block. Return
2367 NULL if any edge may be taken. */
2369 static edge
2370 find_taken_edge_switch_expr (basic_block bb, tree val)
2372 tree switch_expr, taken_case;
2373 basic_block dest_bb;
2374 edge e;
2376 switch_expr = last_stmt (bb);
2377 taken_case = find_case_label_for_value (switch_expr, val);
2378 dest_bb = label_to_block (CASE_LABEL (taken_case));
2380 e = find_edge (bb, dest_bb);
2381 gcc_assert (e);
2382 return e;
2386 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2387 We can make optimal use here of the fact that the case labels are
2388 sorted: We can do a binary search for a case matching VAL. */
2390 static tree
2391 find_case_label_for_value (tree switch_expr, tree val)
2393 tree vec = SWITCH_LABELS (switch_expr);
2394 size_t low, high, n = TREE_VEC_LENGTH (vec);
2395 tree default_case = TREE_VEC_ELT (vec, n - 1);
2397 for (low = -1, high = n - 1; high - low > 1; )
2399 size_t i = (high + low) / 2;
2400 tree t = TREE_VEC_ELT (vec, i);
2401 int cmp;
2403 /* Cache the result of comparing CASE_LOW and val. */
2404 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2406 if (cmp > 0)
2407 high = i;
2408 else
2409 low = i;
2411 if (CASE_HIGH (t) == NULL)
2413 /* A singe-valued case label. */
2414 if (cmp == 0)
2415 return t;
2417 else
2419 /* A case range. We can only handle integer ranges. */
2420 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2421 return t;
2425 return default_case;
2429 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2430 those alternatives are equal in each of the PHI nodes, then return
2431 true, else return false. */
2433 static bool
2434 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
2436 int n1 = e1->dest_idx;
2437 int n2 = e2->dest_idx;
2438 tree phi;
2440 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2442 tree val1 = PHI_ARG_DEF (phi, n1);
2443 tree val2 = PHI_ARG_DEF (phi, n2);
2445 gcc_assert (val1 != NULL_TREE);
2446 gcc_assert (val2 != NULL_TREE);
2448 if (!operand_equal_for_phi_arg_p (val1, val2))
2449 return false;
2452 return true;
2456 /*---------------------------------------------------------------------------
2457 Debugging functions
2458 ---------------------------------------------------------------------------*/
2460 /* Dump tree-specific information of block BB to file OUTF. */
2462 void
2463 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2465 dump_generic_bb (outf, bb, indent, TDF_VOPS);
2469 /* Dump a basic block on stderr. */
2471 void
2472 debug_tree_bb (basic_block bb)
2474 dump_bb (bb, stderr, 0);
2478 /* Dump basic block with index N on stderr. */
2480 basic_block
2481 debug_tree_bb_n (int n)
2483 debug_tree_bb (BASIC_BLOCK (n));
2484 return BASIC_BLOCK (n);
2488 /* Dump the CFG on stderr.
2490 FLAGS are the same used by the tree dumping functions
2491 (see TDF_* in tree.h). */
2493 void
2494 debug_tree_cfg (int flags)
2496 dump_tree_cfg (stderr, flags);
2500 /* Dump the program showing basic block boundaries on the given FILE.
2502 FLAGS are the same used by the tree dumping functions (see TDF_* in
2503 tree.h). */
2505 void
2506 dump_tree_cfg (FILE *file, int flags)
2508 if (flags & TDF_DETAILS)
2510 const char *funcname
2511 = lang_hooks.decl_printable_name (current_function_decl, 2);
2513 fputc ('\n', file);
2514 fprintf (file, ";; Function %s\n\n", funcname);
2515 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2516 n_basic_blocks, n_edges, last_basic_block);
2518 brief_dump_cfg (file);
2519 fprintf (file, "\n");
2522 if (flags & TDF_STATS)
2523 dump_cfg_stats (file);
2525 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2529 /* Dump CFG statistics on FILE. */
2531 void
2532 dump_cfg_stats (FILE *file)
2534 static long max_num_merged_labels = 0;
2535 unsigned long size, total = 0;
2536 long num_edges;
2537 basic_block bb;
2538 const char * const fmt_str = "%-30s%-13s%12s\n";
2539 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2540 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2541 const char *funcname
2542 = lang_hooks.decl_printable_name (current_function_decl, 2);
2545 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2547 fprintf (file, "---------------------------------------------------------\n");
2548 fprintf (file, fmt_str, "", " Number of ", "Memory");
2549 fprintf (file, fmt_str, "", " instances ", "used ");
2550 fprintf (file, "---------------------------------------------------------\n");
2552 size = n_basic_blocks * sizeof (struct basic_block_def);
2553 total += size;
2554 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2555 SCALE (size), LABEL (size));
2557 num_edges = 0;
2558 FOR_EACH_BB (bb)
2559 num_edges += EDGE_COUNT (bb->succs);
2560 size = num_edges * sizeof (struct edge_def);
2561 total += size;
2562 fprintf (file, fmt_str_1, "Edges", num_edges, SCALE (size), LABEL (size));
2564 size = n_basic_blocks * sizeof (struct bb_ann_d);
2565 total += size;
2566 fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks,
2567 SCALE (size), LABEL (size));
2569 fprintf (file, "---------------------------------------------------------\n");
2570 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2571 LABEL (total));
2572 fprintf (file, "---------------------------------------------------------\n");
2573 fprintf (file, "\n");
2575 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2576 max_num_merged_labels = cfg_stats.num_merged_labels;
2578 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2579 cfg_stats.num_merged_labels, max_num_merged_labels);
2581 fprintf (file, "\n");
2585 /* Dump CFG statistics on stderr. Keep extern so that it's always
2586 linked in the final executable. */
2588 void
2589 debug_cfg_stats (void)
2591 dump_cfg_stats (stderr);
2595 /* Dump the flowgraph to a .vcg FILE. */
2597 static void
2598 tree_cfg2vcg (FILE *file)
2600 edge e;
2601 edge_iterator ei;
2602 basic_block bb;
2603 const char *funcname
2604 = lang_hooks.decl_printable_name (current_function_decl, 2);
2606 /* Write the file header. */
2607 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2608 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2609 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2611 /* Write blocks and edges. */
2612 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2614 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2615 e->dest->index);
2617 if (e->flags & EDGE_FAKE)
2618 fprintf (file, " linestyle: dotted priority: 10");
2619 else
2620 fprintf (file, " linestyle: solid priority: 100");
2622 fprintf (file, " }\n");
2624 fputc ('\n', file);
2626 FOR_EACH_BB (bb)
2628 enum tree_code head_code, end_code;
2629 const char *head_name, *end_name;
2630 int head_line = 0;
2631 int end_line = 0;
2632 tree first = first_stmt (bb);
2633 tree last = last_stmt (bb);
2635 if (first)
2637 head_code = TREE_CODE (first);
2638 head_name = tree_code_name[head_code];
2639 head_line = get_lineno (first);
2641 else
2642 head_name = "no-statement";
2644 if (last)
2646 end_code = TREE_CODE (last);
2647 end_name = tree_code_name[end_code];
2648 end_line = get_lineno (last);
2650 else
2651 end_name = "no-statement";
2653 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2654 bb->index, bb->index, head_name, head_line, end_name,
2655 end_line);
2657 FOR_EACH_EDGE (e, ei, bb->succs)
2659 if (e->dest == EXIT_BLOCK_PTR)
2660 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2661 else
2662 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2664 if (e->flags & EDGE_FAKE)
2665 fprintf (file, " priority: 10 linestyle: dotted");
2666 else
2667 fprintf (file, " priority: 100 linestyle: solid");
2669 fprintf (file, " }\n");
2672 if (bb->next_bb != EXIT_BLOCK_PTR)
2673 fputc ('\n', file);
2676 fputs ("}\n\n", file);
2681 /*---------------------------------------------------------------------------
2682 Miscellaneous helpers
2683 ---------------------------------------------------------------------------*/
2685 /* Return true if T represents a stmt that always transfers control. */
2687 bool
2688 is_ctrl_stmt (tree t)
2690 return (TREE_CODE (t) == COND_EXPR
2691 || TREE_CODE (t) == SWITCH_EXPR
2692 || TREE_CODE (t) == GOTO_EXPR
2693 || TREE_CODE (t) == RETURN_EXPR
2694 || TREE_CODE (t) == RESX_EXPR);
2698 /* Return true if T is a statement that may alter the flow of control
2699 (e.g., a call to a non-returning function). */
2701 bool
2702 is_ctrl_altering_stmt (tree t)
2704 tree call;
2706 gcc_assert (t);
2707 call = get_call_expr_in (t);
2708 if (call)
2710 /* A non-pure/const CALL_EXPR alters flow control if the current
2711 function has nonlocal labels. */
2712 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2713 return true;
2715 /* A CALL_EXPR also alters control flow if it does not return. */
2716 if (call_expr_flags (call) & ECF_NORETURN)
2717 return true;
2720 /* If a statement can throw, it alters control flow. */
2721 return tree_can_throw_internal (t);
2725 /* Return true if T is a computed goto. */
2727 bool
2728 computed_goto_p (tree t)
2730 return (TREE_CODE (t) == GOTO_EXPR
2731 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2735 /* Checks whether EXPR is a simple local goto. */
2737 bool
2738 simple_goto_p (tree expr)
2740 return (TREE_CODE (expr) == GOTO_EXPR
2741 && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL);
2745 /* Return true if T should start a new basic block. PREV_T is the
2746 statement preceding T. It is used when T is a label or a case label.
2747 Labels should only start a new basic block if their previous statement
2748 wasn't a label. Otherwise, sequence of labels would generate
2749 unnecessary basic blocks that only contain a single label. */
2751 static inline bool
2752 stmt_starts_bb_p (tree t, tree prev_t)
2754 if (t == NULL_TREE)
2755 return false;
2757 /* LABEL_EXPRs start a new basic block only if the preceding
2758 statement wasn't a label of the same type. This prevents the
2759 creation of consecutive blocks that have nothing but a single
2760 label. */
2761 if (TREE_CODE (t) == LABEL_EXPR)
2763 /* Nonlocal and computed GOTO targets always start a new block. */
2764 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2765 || FORCED_LABEL (LABEL_EXPR_LABEL (t)))
2766 return true;
2768 if (prev_t && TREE_CODE (prev_t) == LABEL_EXPR)
2770 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2771 return true;
2773 cfg_stats.num_merged_labels++;
2774 return false;
2776 else
2777 return true;
2780 return false;
2784 /* Return true if T should end a basic block. */
2786 bool
2787 stmt_ends_bb_p (tree t)
2789 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2793 /* Add gotos that used to be represented implicitly in the CFG. */
2795 void
2796 disband_implicit_edges (void)
2798 basic_block bb;
2799 block_stmt_iterator last;
2800 edge e;
2801 edge_iterator ei;
2802 tree stmt, label;
2804 FOR_EACH_BB (bb)
2806 last = bsi_last (bb);
2807 stmt = last_stmt (bb);
2809 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2811 /* Remove superfluous gotos from COND_EXPR branches. Moved
2812 from cfg_remove_useless_stmts here since it violates the
2813 invariants for tree--cfg correspondence and thus fits better
2814 here where we do it anyway. */
2815 e = find_edge (bb, bb->next_bb);
2816 if (e)
2818 if (e->flags & EDGE_TRUE_VALUE)
2819 COND_EXPR_THEN (stmt) = build_empty_stmt ();
2820 else if (e->flags & EDGE_FALSE_VALUE)
2821 COND_EXPR_ELSE (stmt) = build_empty_stmt ();
2822 else
2823 gcc_unreachable ();
2824 e->flags |= EDGE_FALLTHRU;
2827 continue;
2830 if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
2832 /* Remove the RETURN_EXPR if we may fall though to the exit
2833 instead. */
2834 gcc_assert (single_succ_p (bb));
2835 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
2837 if (bb->next_bb == EXIT_BLOCK_PTR
2838 && !TREE_OPERAND (stmt, 0))
2840 bsi_remove (&last);
2841 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2843 continue;
2846 /* There can be no fallthru edge if the last statement is a control
2847 one. */
2848 if (stmt && is_ctrl_stmt (stmt))
2849 continue;
2851 /* Find a fallthru edge and emit the goto if necessary. */
2852 FOR_EACH_EDGE (e, ei, bb->succs)
2853 if (e->flags & EDGE_FALLTHRU)
2854 break;
2856 if (!e || e->dest == bb->next_bb)
2857 continue;
2859 gcc_assert (e->dest != EXIT_BLOCK_PTR);
2860 label = tree_block_label (e->dest);
2862 stmt = build1 (GOTO_EXPR, void_type_node, label);
2863 #ifdef USE_MAPPED_LOCATION
2864 SET_EXPR_LOCATION (stmt, e->goto_locus);
2865 #else
2866 SET_EXPR_LOCUS (stmt, e->goto_locus);
2867 #endif
2868 bsi_insert_after (&last, stmt, BSI_NEW_STMT);
2869 e->flags &= ~EDGE_FALLTHRU;
2873 /* Remove block annotations and other datastructures. */
2875 void
2876 delete_tree_cfg_annotations (void)
2878 basic_block bb;
2879 if (n_basic_blocks > 0)
2880 free_blocks_annotations ();
2882 label_to_block_map = NULL;
2883 FOR_EACH_BB (bb)
2884 bb->rbi = NULL;
2888 /* Return the first statement in basic block BB. */
2890 tree
2891 first_stmt (basic_block bb)
2893 block_stmt_iterator i = bsi_start (bb);
2894 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2898 /* Return the last statement in basic block BB. */
2900 tree
2901 last_stmt (basic_block bb)
2903 block_stmt_iterator b = bsi_last (bb);
2904 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2908 /* Return a pointer to the last statement in block BB. */
2910 tree *
2911 last_stmt_ptr (basic_block bb)
2913 block_stmt_iterator last = bsi_last (bb);
2914 return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL;
2918 /* Return the last statement of an otherwise empty block. Return NULL
2919 if the block is totally empty, or if it contains more than one
2920 statement. */
2922 tree
2923 last_and_only_stmt (basic_block bb)
2925 block_stmt_iterator i = bsi_last (bb);
2926 tree last, prev;
2928 if (bsi_end_p (i))
2929 return NULL_TREE;
2931 last = bsi_stmt (i);
2932 bsi_prev (&i);
2933 if (bsi_end_p (i))
2934 return last;
2936 /* Empty statements should no longer appear in the instruction stream.
2937 Everything that might have appeared before should be deleted by
2938 remove_useless_stmts, and the optimizers should just bsi_remove
2939 instead of smashing with build_empty_stmt.
2941 Thus the only thing that should appear here in a block containing
2942 one executable statement is a label. */
2943 prev = bsi_stmt (i);
2944 if (TREE_CODE (prev) == LABEL_EXPR)
2945 return last;
2946 else
2947 return NULL_TREE;
2951 /* Mark BB as the basic block holding statement T. */
2953 void
2954 set_bb_for_stmt (tree t, basic_block bb)
2956 if (TREE_CODE (t) == PHI_NODE)
2957 PHI_BB (t) = bb;
2958 else if (TREE_CODE (t) == STATEMENT_LIST)
2960 tree_stmt_iterator i;
2961 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2962 set_bb_for_stmt (tsi_stmt (i), bb);
2964 else
2966 stmt_ann_t ann = get_stmt_ann (t);
2967 ann->bb = bb;
2969 /* If the statement is a label, add the label to block-to-labels map
2970 so that we can speed up edge creation for GOTO_EXPRs. */
2971 if (TREE_CODE (t) == LABEL_EXPR)
2973 int uid;
2975 t = LABEL_EXPR_LABEL (t);
2976 uid = LABEL_DECL_UID (t);
2977 if (uid == -1)
2979 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2980 if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid)
2981 VARRAY_GROW (label_to_block_map, 3 * uid / 2);
2983 else
2984 /* We're moving an existing label. Make sure that we've
2985 removed it from the old block. */
2986 gcc_assert (!bb || !VARRAY_BB (label_to_block_map, uid));
2987 VARRAY_BB (label_to_block_map, uid) = bb;
2992 /* Finds iterator for STMT. */
2994 extern block_stmt_iterator
2995 bsi_for_stmt (tree stmt)
2997 block_stmt_iterator bsi;
2999 for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
3000 if (bsi_stmt (bsi) == stmt)
3001 return bsi;
3003 gcc_unreachable ();
3006 /* Mark statement T as modified, and update it. */
3007 static inline void
3008 update_modified_stmts (tree t)
3010 if (TREE_CODE (t) == STATEMENT_LIST)
3012 tree_stmt_iterator i;
3013 tree stmt;
3014 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3016 stmt = tsi_stmt (i);
3017 update_stmt_if_modified (stmt);
3020 else
3021 update_stmt_if_modified (t);
3024 /* Insert statement (or statement list) T before the statement
3025 pointed-to by iterator I. M specifies how to update iterator I
3026 after insertion (see enum bsi_iterator_update). */
3028 void
3029 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
3031 set_bb_for_stmt (t, i->bb);
3032 update_modified_stmts (t);
3033 tsi_link_before (&i->tsi, t, m);
3037 /* Insert statement (or statement list) T after the statement
3038 pointed-to by iterator I. M specifies how to update iterator I
3039 after insertion (see enum bsi_iterator_update). */
3041 void
3042 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
3044 set_bb_for_stmt (t, i->bb);
3045 update_modified_stmts (t);
3046 tsi_link_after (&i->tsi, t, m);
3050 /* Remove the statement pointed to by iterator I. The iterator is updated
3051 to the next statement. */
3053 void
3054 bsi_remove (block_stmt_iterator *i)
3056 tree t = bsi_stmt (*i);
3057 set_bb_for_stmt (t, NULL);
3058 delink_stmt_imm_use (t);
3059 tsi_delink (&i->tsi);
3060 mark_stmt_modified (t);
3064 /* Move the statement at FROM so it comes right after the statement at TO. */
3066 void
3067 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
3069 tree stmt = bsi_stmt (*from);
3070 bsi_remove (from);
3071 bsi_insert_after (to, stmt, BSI_SAME_STMT);
3075 /* Move the statement at FROM so it comes right before the statement at TO. */
3077 void
3078 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
3080 tree stmt = bsi_stmt (*from);
3081 bsi_remove (from);
3082 bsi_insert_before (to, stmt, BSI_SAME_STMT);
3086 /* Move the statement at FROM to the end of basic block BB. */
3088 void
3089 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
3091 block_stmt_iterator last = bsi_last (bb);
3093 /* Have to check bsi_end_p because it could be an empty block. */
3094 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
3095 bsi_move_before (from, &last);
3096 else
3097 bsi_move_after (from, &last);
3101 /* Replace the contents of the statement pointed to by iterator BSI
3102 with STMT. If PRESERVE_EH_INFO is true, the exception handling
3103 information of the original statement is preserved. */
3105 void
3106 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
3108 int eh_region;
3109 tree orig_stmt = bsi_stmt (*bsi);
3111 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
3112 set_bb_for_stmt (stmt, bsi->bb);
3114 /* Preserve EH region information from the original statement, if
3115 requested by the caller. */
3116 if (preserve_eh_info)
3118 eh_region = lookup_stmt_eh_region (orig_stmt);
3119 if (eh_region >= 0)
3120 add_stmt_to_eh_region (stmt, eh_region);
3123 delink_stmt_imm_use (orig_stmt);
3124 *bsi_stmt_ptr (*bsi) = stmt;
3125 mark_stmt_modified (stmt);
3126 update_modified_stmts (stmt);
3130 /* Insert the statement pointed-to by BSI into edge E. Every attempt
3131 is made to place the statement in an existing basic block, but
3132 sometimes that isn't possible. When it isn't possible, the edge is
3133 split and the statement is added to the new block.
3135 In all cases, the returned *BSI points to the correct location. The
3136 return value is true if insertion should be done after the location,
3137 or false if it should be done before the location. If new basic block
3138 has to be created, it is stored in *NEW_BB. */
3140 static bool
3141 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
3142 basic_block *new_bb)
3144 basic_block dest, src;
3145 tree tmp;
3147 dest = e->dest;
3148 restart:
3150 /* If the destination has one predecessor which has no PHI nodes,
3151 insert there. Except for the exit block.
3153 The requirement for no PHI nodes could be relaxed. Basically we
3154 would have to examine the PHIs to prove that none of them used
3155 the value set by the statement we want to insert on E. That
3156 hardly seems worth the effort. */
3157 if (single_pred_p (dest)
3158 && ! phi_nodes (dest)
3159 && dest != EXIT_BLOCK_PTR)
3161 *bsi = bsi_start (dest);
3162 if (bsi_end_p (*bsi))
3163 return true;
3165 /* Make sure we insert after any leading labels. */
3166 tmp = bsi_stmt (*bsi);
3167 while (TREE_CODE (tmp) == LABEL_EXPR)
3169 bsi_next (bsi);
3170 if (bsi_end_p (*bsi))
3171 break;
3172 tmp = bsi_stmt (*bsi);
3175 if (bsi_end_p (*bsi))
3177 *bsi = bsi_last (dest);
3178 return true;
3180 else
3181 return false;
3184 /* If the source has one successor, the edge is not abnormal and
3185 the last statement does not end a basic block, insert there.
3186 Except for the entry block. */
3187 src = e->src;
3188 if ((e->flags & EDGE_ABNORMAL) == 0
3189 && single_succ_p (src)
3190 && src != ENTRY_BLOCK_PTR)
3192 *bsi = bsi_last (src);
3193 if (bsi_end_p (*bsi))
3194 return true;
3196 tmp = bsi_stmt (*bsi);
3197 if (!stmt_ends_bb_p (tmp))
3198 return true;
3200 /* Insert code just before returning the value. We may need to decompose
3201 the return in the case it contains non-trivial operand. */
3202 if (TREE_CODE (tmp) == RETURN_EXPR)
3204 tree op = TREE_OPERAND (tmp, 0);
3205 if (!is_gimple_val (op))
3207 gcc_assert (TREE_CODE (op) == MODIFY_EXPR);
3208 bsi_insert_before (bsi, op, BSI_NEW_STMT);
3209 TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0);
3211 bsi_prev (bsi);
3212 return true;
3216 /* Otherwise, create a new basic block, and split this edge. */
3217 dest = split_edge (e);
3218 if (new_bb)
3219 *new_bb = dest;
3220 e = single_pred_edge (dest);
3221 goto restart;
3225 /* This routine will commit all pending edge insertions, creating any new
3226 basic blocks which are necessary. */
3228 void
3229 bsi_commit_edge_inserts (void)
3231 basic_block bb;
3232 edge e;
3233 edge_iterator ei;
3235 bsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
3237 FOR_EACH_BB (bb)
3238 FOR_EACH_EDGE (e, ei, bb->succs)
3239 bsi_commit_one_edge_insert (e, NULL);
3243 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
3244 to this block, otherwise set it to NULL. */
3246 void
3247 bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
3249 if (new_bb)
3250 *new_bb = NULL;
3251 if (PENDING_STMT (e))
3253 block_stmt_iterator bsi;
3254 tree stmt = PENDING_STMT (e);
3256 PENDING_STMT (e) = NULL_TREE;
3258 if (tree_find_edge_insert_loc (e, &bsi, new_bb))
3259 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3260 else
3261 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3266 /* Add STMT to the pending list of edge E. No actual insertion is
3267 made until a call to bsi_commit_edge_inserts () is made. */
3269 void
3270 bsi_insert_on_edge (edge e, tree stmt)
3272 append_to_statement_list (stmt, &PENDING_STMT (e));
3275 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new
3276 block has to be created, it is returned. */
3278 basic_block
3279 bsi_insert_on_edge_immediate (edge e, tree stmt)
3281 block_stmt_iterator bsi;
3282 basic_block new_bb = NULL;
3284 gcc_assert (!PENDING_STMT (e));
3286 if (tree_find_edge_insert_loc (e, &bsi, &new_bb))
3287 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3288 else
3289 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3291 return new_bb;
3294 /*---------------------------------------------------------------------------
3295 Tree specific functions for CFG manipulation
3296 ---------------------------------------------------------------------------*/
3298 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
3300 static void
3301 reinstall_phi_args (edge new_edge, edge old_edge)
3303 tree var, phi;
3305 if (!PENDING_STMT (old_edge))
3306 return;
3308 for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest);
3309 var && phi;
3310 var = TREE_CHAIN (var), phi = PHI_CHAIN (phi))
3312 tree result = TREE_PURPOSE (var);
3313 tree arg = TREE_VALUE (var);
3315 gcc_assert (result == PHI_RESULT (phi));
3317 add_phi_arg (phi, arg, new_edge);
3320 PENDING_STMT (old_edge) = NULL;
3323 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3324 Abort on abnormal edges. */
3326 static basic_block
3327 tree_split_edge (edge edge_in)
3329 basic_block new_bb, after_bb, dest, src;
3330 edge new_edge, e;
3332 /* Abnormal edges cannot be split. */
3333 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
3335 src = edge_in->src;
3336 dest = edge_in->dest;
3338 /* Place the new block in the block list. Try to keep the new block
3339 near its "logical" location. This is of most help to humans looking
3340 at debugging dumps. */
3341 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
3342 after_bb = edge_in->src;
3343 else
3344 after_bb = dest->prev_bb;
3346 new_bb = create_empty_bb (after_bb);
3347 new_bb->frequency = EDGE_FREQUENCY (edge_in);
3348 new_bb->count = edge_in->count;
3349 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
3350 new_edge->probability = REG_BR_PROB_BASE;
3351 new_edge->count = edge_in->count;
3353 e = redirect_edge_and_branch (edge_in, new_bb);
3354 gcc_assert (e);
3355 reinstall_phi_args (new_edge, e);
3357 return new_bb;
3361 /* Return true when BB has label LABEL in it. */
3363 static bool
3364 has_label_p (basic_block bb, tree label)
3366 block_stmt_iterator bsi;
3368 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3370 tree stmt = bsi_stmt (bsi);
3372 if (TREE_CODE (stmt) != LABEL_EXPR)
3373 return false;
3374 if (LABEL_EXPR_LABEL (stmt) == label)
3375 return true;
3377 return false;
3381 /* Callback for walk_tree, check that all elements with address taken are
3382 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3383 inside a PHI node. */
3385 static tree
3386 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3388 tree t = *tp, x;
3389 bool in_phi = (data != NULL);
3391 if (TYPE_P (t))
3392 *walk_subtrees = 0;
3394 /* Check operand N for being valid GIMPLE and give error MSG if not.
3395 We check for constants explicitly since they are not considered
3396 gimple invariants if they overflowed. */
3397 #define CHECK_OP(N, MSG) \
3398 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3399 && !is_gimple_val (TREE_OPERAND (t, N))) \
3400 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3402 switch (TREE_CODE (t))
3404 case SSA_NAME:
3405 if (SSA_NAME_IN_FREE_LIST (t))
3407 error ("SSA name in freelist but still referenced");
3408 return *tp;
3410 break;
3412 case ASSERT_EXPR:
3413 x = fold (ASSERT_EXPR_COND (t));
3414 if (x == boolean_false_node)
3416 error ("ASSERT_EXPR with an always-false condition");
3417 return *tp;
3419 break;
3421 case MODIFY_EXPR:
3422 x = TREE_OPERAND (t, 0);
3423 if (TREE_CODE (x) == BIT_FIELD_REF
3424 && is_gimple_reg (TREE_OPERAND (x, 0)))
3426 error ("GIMPLE register modified with BIT_FIELD_REF");
3427 return t;
3429 break;
3431 case ADDR_EXPR:
3432 /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
3433 dead PHIs that take the address of something. But if the PHI
3434 result is dead, the fact that it takes the address of anything
3435 is irrelevant. Because we can not tell from here if a PHI result
3436 is dead, we just skip this check for PHIs altogether. This means
3437 we may be missing "valid" checks, but what can you do?
3438 This was PR19217. */
3439 if (in_phi)
3440 break;
3442 /* Skip any references (they will be checked when we recurse down the
3443 tree) and ensure that any variable used as a prefix is marked
3444 addressable. */
3445 for (x = TREE_OPERAND (t, 0);
3446 handled_component_p (x);
3447 x = TREE_OPERAND (x, 0))
3450 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3451 return NULL;
3452 if (!TREE_ADDRESSABLE (x))
3454 error ("address taken, but ADDRESSABLE bit not set");
3455 return x;
3457 break;
3459 case COND_EXPR:
3460 x = COND_EXPR_COND (t);
3461 if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3463 error ("non-boolean used in condition");
3464 return x;
3466 break;
3468 case NOP_EXPR:
3469 case CONVERT_EXPR:
3470 case FIX_TRUNC_EXPR:
3471 case FIX_CEIL_EXPR:
3472 case FIX_FLOOR_EXPR:
3473 case FIX_ROUND_EXPR:
3474 case FLOAT_EXPR:
3475 case NEGATE_EXPR:
3476 case ABS_EXPR:
3477 case BIT_NOT_EXPR:
3478 case NON_LVALUE_EXPR:
3479 case TRUTH_NOT_EXPR:
3480 CHECK_OP (0, "Invalid operand to unary operator");
3481 break;
3483 case REALPART_EXPR:
3484 case IMAGPART_EXPR:
3485 case COMPONENT_REF:
3486 case ARRAY_REF:
3487 case ARRAY_RANGE_REF:
3488 case BIT_FIELD_REF:
3489 case VIEW_CONVERT_EXPR:
3490 /* We have a nest of references. Verify that each of the operands
3491 that determine where to reference is either a constant or a variable,
3492 verify that the base is valid, and then show we've already checked
3493 the subtrees. */
3494 while (handled_component_p (t))
3496 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3497 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3498 else if (TREE_CODE (t) == ARRAY_REF
3499 || TREE_CODE (t) == ARRAY_RANGE_REF)
3501 CHECK_OP (1, "Invalid array index.");
3502 if (TREE_OPERAND (t, 2))
3503 CHECK_OP (2, "Invalid array lower bound.");
3504 if (TREE_OPERAND (t, 3))
3505 CHECK_OP (3, "Invalid array stride.");
3507 else if (TREE_CODE (t) == BIT_FIELD_REF)
3509 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3510 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3513 t = TREE_OPERAND (t, 0);
3516 if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t))
3518 error ("Invalid reference prefix.");
3519 return t;
3521 *walk_subtrees = 0;
3522 break;
3524 case LT_EXPR:
3525 case LE_EXPR:
3526 case GT_EXPR:
3527 case GE_EXPR:
3528 case EQ_EXPR:
3529 case NE_EXPR:
3530 case UNORDERED_EXPR:
3531 case ORDERED_EXPR:
3532 case UNLT_EXPR:
3533 case UNLE_EXPR:
3534 case UNGT_EXPR:
3535 case UNGE_EXPR:
3536 case UNEQ_EXPR:
3537 case LTGT_EXPR:
3538 case PLUS_EXPR:
3539 case MINUS_EXPR:
3540 case MULT_EXPR:
3541 case TRUNC_DIV_EXPR:
3542 case CEIL_DIV_EXPR:
3543 case FLOOR_DIV_EXPR:
3544 case ROUND_DIV_EXPR:
3545 case TRUNC_MOD_EXPR:
3546 case CEIL_MOD_EXPR:
3547 case FLOOR_MOD_EXPR:
3548 case ROUND_MOD_EXPR:
3549 case RDIV_EXPR:
3550 case EXACT_DIV_EXPR:
3551 case MIN_EXPR:
3552 case MAX_EXPR:
3553 case LSHIFT_EXPR:
3554 case RSHIFT_EXPR:
3555 case LROTATE_EXPR:
3556 case RROTATE_EXPR:
3557 case BIT_IOR_EXPR:
3558 case BIT_XOR_EXPR:
3559 case BIT_AND_EXPR:
3560 CHECK_OP (0, "Invalid operand to binary operator");
3561 CHECK_OP (1, "Invalid operand to binary operator");
3562 break;
3564 default:
3565 break;
3567 return NULL;
3569 #undef CHECK_OP
3573 /* Verify STMT, return true if STMT is not in GIMPLE form.
3574 TODO: Implement type checking. */
3576 static bool
3577 verify_stmt (tree stmt, bool last_in_block)
3579 tree addr;
3581 if (!is_gimple_stmt (stmt))
3583 error ("Is not a valid GIMPLE statement.");
3584 goto fail;
3587 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
3588 if (addr)
3590 debug_generic_stmt (addr);
3591 return true;
3594 /* If the statement is marked as part of an EH region, then it is
3595 expected that the statement could throw. Verify that when we
3596 have optimizations that simplify statements such that we prove
3597 that they cannot throw, that we update other data structures
3598 to match. */
3599 if (lookup_stmt_eh_region (stmt) >= 0)
3601 if (!tree_could_throw_p (stmt))
3603 error ("Statement marked for throw, but doesn%'t.");
3604 goto fail;
3606 if (!last_in_block && tree_can_throw_internal (stmt))
3608 error ("Statement marked for throw in middle of block.");
3609 goto fail;
3613 return false;
3615 fail:
3616 debug_generic_stmt (stmt);
3617 return true;
3621 /* Return true when the T can be shared. */
3623 static bool
3624 tree_node_can_be_shared (tree t)
3626 if (IS_TYPE_OR_DECL_P (t)
3627 /* We check for constants explicitly since they are not considered
3628 gimple invariants if they overflowed. */
3629 || CONSTANT_CLASS_P (t)
3630 || is_gimple_min_invariant (t)
3631 || TREE_CODE (t) == SSA_NAME
3632 || t == error_mark_node)
3633 return true;
3635 if (TREE_CODE (t) == CASE_LABEL_EXPR)
3636 return true;
3638 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3639 /* We check for constants explicitly since they are not considered
3640 gimple invariants if they overflowed. */
3641 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 1))
3642 || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
3643 || (TREE_CODE (t) == COMPONENT_REF
3644 || TREE_CODE (t) == REALPART_EXPR
3645 || TREE_CODE (t) == IMAGPART_EXPR))
3646 t = TREE_OPERAND (t, 0);
3648 if (DECL_P (t))
3649 return true;
3651 return false;
3655 /* Called via walk_trees. Verify tree sharing. */
3657 static tree
3658 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
3660 htab_t htab = (htab_t) data;
3661 void **slot;
3663 if (tree_node_can_be_shared (*tp))
3665 *walk_subtrees = false;
3666 return NULL;
3669 slot = htab_find_slot (htab, *tp, INSERT);
3670 if (*slot)
3671 return *slot;
3672 *slot = *tp;
3674 return NULL;
3678 /* Verify the GIMPLE statement chain. */
3680 void
3681 verify_stmts (void)
3683 basic_block bb;
3684 block_stmt_iterator bsi;
3685 bool err = false;
3686 htab_t htab;
3687 tree addr;
3689 timevar_push (TV_TREE_STMT_VERIFY);
3690 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3692 FOR_EACH_BB (bb)
3694 tree phi;
3695 int i;
3697 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3699 int phi_num_args = PHI_NUM_ARGS (phi);
3701 if (bb_for_stmt (phi) != bb)
3703 error ("bb_for_stmt (phi) is set to a wrong basic block\n");
3704 err |= true;
3707 for (i = 0; i < phi_num_args; i++)
3709 tree t = PHI_ARG_DEF (phi, i);
3710 tree addr;
3712 /* Addressable variables do have SSA_NAMEs but they
3713 are not considered gimple values. */
3714 if (TREE_CODE (t) != SSA_NAME
3715 && TREE_CODE (t) != FUNCTION_DECL
3716 && !is_gimple_val (t))
3718 error ("PHI def is not a GIMPLE value");
3719 debug_generic_stmt (phi);
3720 debug_generic_stmt (t);
3721 err |= true;
3724 addr = walk_tree (&t, verify_expr, (void *) 1, NULL);
3725 if (addr)
3727 debug_generic_stmt (addr);
3728 err |= true;
3731 addr = walk_tree (&t, verify_node_sharing, htab, NULL);
3732 if (addr)
3734 error ("Incorrect sharing of tree nodes");
3735 debug_generic_stmt (phi);
3736 debug_generic_stmt (addr);
3737 err |= true;
3742 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
3744 tree stmt = bsi_stmt (bsi);
3746 if (bb_for_stmt (stmt) != bb)
3748 error ("bb_for_stmt (stmt) is set to a wrong basic block\n");
3749 err |= true;
3752 bsi_next (&bsi);
3753 err |= verify_stmt (stmt, bsi_end_p (bsi));
3754 addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
3755 if (addr)
3757 error ("Incorrect sharing of tree nodes");
3758 debug_generic_stmt (stmt);
3759 debug_generic_stmt (addr);
3760 err |= true;
3765 if (err)
3766 internal_error ("verify_stmts failed.");
3768 htab_delete (htab);
3769 timevar_pop (TV_TREE_STMT_VERIFY);
3773 /* Verifies that the flow information is OK. */
3775 static int
3776 tree_verify_flow_info (void)
3778 int err = 0;
3779 basic_block bb;
3780 block_stmt_iterator bsi;
3781 tree stmt;
3782 edge e;
3783 edge_iterator ei;
3785 if (ENTRY_BLOCK_PTR->stmt_list)
3787 error ("ENTRY_BLOCK has a statement list associated with it\n");
3788 err = 1;
3791 if (EXIT_BLOCK_PTR->stmt_list)
3793 error ("EXIT_BLOCK has a statement list associated with it\n");
3794 err = 1;
3797 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3798 if (e->flags & EDGE_FALLTHRU)
3800 error ("Fallthru to exit from bb %d\n", e->src->index);
3801 err = 1;
3804 FOR_EACH_BB (bb)
3806 bool found_ctrl_stmt = false;
3808 stmt = NULL_TREE;
3810 /* Skip labels on the start of basic block. */
3811 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3813 tree prev_stmt = stmt;
3815 stmt = bsi_stmt (bsi);
3817 if (TREE_CODE (stmt) != LABEL_EXPR)
3818 break;
3820 if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
3822 error ("Nonlocal label %s is not first "
3823 "in a sequence of labels in bb %d",
3824 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3825 bb->index);
3826 err = 1;
3829 if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
3831 error ("Label %s to block does not match in bb %d\n",
3832 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3833 bb->index);
3834 err = 1;
3837 if (decl_function_context (LABEL_EXPR_LABEL (stmt))
3838 != current_function_decl)
3840 error ("Label %s has incorrect context in bb %d\n",
3841 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3842 bb->index);
3843 err = 1;
3847 /* Verify that body of basic block BB is free of control flow. */
3848 for (; !bsi_end_p (bsi); bsi_next (&bsi))
3850 tree stmt = bsi_stmt (bsi);
3852 if (found_ctrl_stmt)
3854 error ("Control flow in the middle of basic block %d\n",
3855 bb->index);
3856 err = 1;
3859 if (stmt_ends_bb_p (stmt))
3860 found_ctrl_stmt = true;
3862 if (TREE_CODE (stmt) == LABEL_EXPR)
3864 error ("Label %s in the middle of basic block %d\n",
3865 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3866 bb->index);
3867 err = 1;
3870 bsi = bsi_last (bb);
3871 if (bsi_end_p (bsi))
3872 continue;
3874 stmt = bsi_stmt (bsi);
3876 err |= verify_eh_edges (stmt);
3878 if (is_ctrl_stmt (stmt))
3880 FOR_EACH_EDGE (e, ei, bb->succs)
3881 if (e->flags & EDGE_FALLTHRU)
3883 error ("Fallthru edge after a control statement in bb %d \n",
3884 bb->index);
3885 err = 1;
3889 switch (TREE_CODE (stmt))
3891 case COND_EXPR:
3893 edge true_edge;
3894 edge false_edge;
3895 if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR
3896 || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR)
3898 error ("Structured COND_EXPR at the end of bb %d\n", bb->index);
3899 err = 1;
3902 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
3904 if (!true_edge || !false_edge
3905 || !(true_edge->flags & EDGE_TRUE_VALUE)
3906 || !(false_edge->flags & EDGE_FALSE_VALUE)
3907 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3908 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3909 || EDGE_COUNT (bb->succs) >= 3)
3911 error ("Wrong outgoing edge flags at end of bb %d\n",
3912 bb->index);
3913 err = 1;
3916 if (!has_label_p (true_edge->dest,
3917 GOTO_DESTINATION (COND_EXPR_THEN (stmt))))
3919 error ("%<then%> label does not match edge at end of bb %d\n",
3920 bb->index);
3921 err = 1;
3924 if (!has_label_p (false_edge->dest,
3925 GOTO_DESTINATION (COND_EXPR_ELSE (stmt))))
3927 error ("%<else%> label does not match edge at end of bb %d\n",
3928 bb->index);
3929 err = 1;
3932 break;
3934 case GOTO_EXPR:
3935 if (simple_goto_p (stmt))
3937 error ("Explicit goto at end of bb %d\n", bb->index);
3938 err = 1;
3940 else
3942 /* FIXME. We should double check that the labels in the
3943 destination blocks have their address taken. */
3944 FOR_EACH_EDGE (e, ei, bb->succs)
3945 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
3946 | EDGE_FALSE_VALUE))
3947 || !(e->flags & EDGE_ABNORMAL))
3949 error ("Wrong outgoing edge flags at end of bb %d\n",
3950 bb->index);
3951 err = 1;
3954 break;
3956 case RETURN_EXPR:
3957 if (!single_succ_p (bb)
3958 || (single_succ_edge (bb)->flags
3959 & (EDGE_FALLTHRU | EDGE_ABNORMAL
3960 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3962 error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
3963 err = 1;
3965 if (single_succ (bb) != EXIT_BLOCK_PTR)
3967 error ("Return edge does not point to exit in bb %d\n",
3968 bb->index);
3969 err = 1;
3971 break;
3973 case SWITCH_EXPR:
3975 tree prev;
3976 edge e;
3977 size_t i, n;
3978 tree vec;
3980 vec = SWITCH_LABELS (stmt);
3981 n = TREE_VEC_LENGTH (vec);
3983 /* Mark all the destination basic blocks. */
3984 for (i = 0; i < n; ++i)
3986 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3987 basic_block label_bb = label_to_block (lab);
3989 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
3990 label_bb->aux = (void *)1;
3993 /* Verify that the case labels are sorted. */
3994 prev = TREE_VEC_ELT (vec, 0);
3995 for (i = 1; i < n - 1; ++i)
3997 tree c = TREE_VEC_ELT (vec, i);
3998 if (! CASE_LOW (c))
4000 error ("Found default case not at end of case vector");
4001 err = 1;
4002 continue;
4004 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
4006 error ("Case labels not sorted:\n ");
4007 print_generic_expr (stderr, prev, 0);
4008 fprintf (stderr," is greater than ");
4009 print_generic_expr (stderr, c, 0);
4010 fprintf (stderr," but comes before it.\n");
4011 err = 1;
4013 prev = c;
4015 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
4017 error ("No default case found at end of case vector");
4018 err = 1;
4021 FOR_EACH_EDGE (e, ei, bb->succs)
4023 if (!e->dest->aux)
4025 error ("Extra outgoing edge %d->%d\n",
4026 bb->index, e->dest->index);
4027 err = 1;
4029 e->dest->aux = (void *)2;
4030 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
4031 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4033 error ("Wrong outgoing edge flags at end of bb %d\n",
4034 bb->index);
4035 err = 1;
4039 /* Check that we have all of them. */
4040 for (i = 0; i < n; ++i)
4042 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
4043 basic_block label_bb = label_to_block (lab);
4045 if (label_bb->aux != (void *)2)
4047 error ("Missing edge %i->%i",
4048 bb->index, label_bb->index);
4049 err = 1;
4053 FOR_EACH_EDGE (e, ei, bb->succs)
4054 e->dest->aux = (void *)0;
4057 default: ;
4061 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
4062 verify_dominators (CDI_DOMINATORS);
4064 return err;
4068 /* Updates phi nodes after creating a forwarder block joined
4069 by edge FALLTHRU. */
4071 static void
4072 tree_make_forwarder_block (edge fallthru)
4074 edge e;
4075 edge_iterator ei;
4076 basic_block dummy, bb;
4077 tree phi, new_phi, var;
4079 dummy = fallthru->src;
4080 bb = fallthru->dest;
4082 if (single_pred_p (bb))
4083 return;
4085 /* If we redirected a branch we must create new phi nodes at the
4086 start of BB. */
4087 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
4089 var = PHI_RESULT (phi);
4090 new_phi = create_phi_node (var, bb);
4091 SSA_NAME_DEF_STMT (var) = new_phi;
4092 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
4093 add_phi_arg (new_phi, PHI_RESULT (phi), fallthru);
4096 /* Ensure that the PHI node chain is in the same order. */
4097 set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
4099 /* Add the arguments we have stored on edges. */
4100 FOR_EACH_EDGE (e, ei, bb->preds)
4102 if (e == fallthru)
4103 continue;
4105 flush_pending_stmts (e);
4110 /* Return true if basic block BB does nothing except pass control
4111 flow to another block and that we can safely insert a label at
4112 the start of the successor block.
4114 As a precondition, we require that BB be not equal to
4115 ENTRY_BLOCK_PTR. */
4117 static bool
4118 tree_forwarder_block_p (basic_block bb, bool phi_wanted)
4120 block_stmt_iterator bsi;
4122 /* BB must have a single outgoing edge. */
4123 if (single_succ_p (bb) != 1
4124 /* If PHI_WANTED is false, BB must not have any PHI nodes.
4125 Otherwise, BB must have PHI nodes. */
4126 || (phi_nodes (bb) != NULL_TREE) != phi_wanted
4127 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
4128 || single_succ (bb) == EXIT_BLOCK_PTR
4129 /* Nor should this be an infinite loop. */
4130 || single_succ (bb) == bb
4131 /* BB may not have an abnormal outgoing edge. */
4132 || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
4133 return false;
4135 #if ENABLE_CHECKING
4136 gcc_assert (bb != ENTRY_BLOCK_PTR);
4137 #endif
4139 /* Now walk through the statements backward. We can ignore labels,
4140 anything else means this is not a forwarder block. */
4141 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
4143 tree stmt = bsi_stmt (bsi);
4145 switch (TREE_CODE (stmt))
4147 case LABEL_EXPR:
4148 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
4149 return false;
4150 break;
4152 default:
4153 return false;
4157 if (find_edge (ENTRY_BLOCK_PTR, bb))
4158 return false;
4160 if (current_loops)
4162 basic_block dest;
4163 /* Protect loop latches, headers and preheaders. */
4164 if (bb->loop_father->header == bb)
4165 return false;
4166 dest = EDGE_SUCC (bb, 0)->dest;
4168 if (dest->loop_father->header == dest)
4169 return false;
4172 return true;
4175 /* Return true if BB has at least one abnormal incoming edge. */
4177 static inline bool
4178 has_abnormal_incoming_edge_p (basic_block bb)
4180 edge e;
4181 edge_iterator ei;
4183 FOR_EACH_EDGE (e, ei, bb->preds)
4184 if (e->flags & EDGE_ABNORMAL)
4185 return true;
4187 return false;
4190 /* Removes forwarder block BB. Returns false if this failed. If a new
4191 forwarder block is created due to redirection of edges, it is
4192 stored to worklist. */
4194 static bool
4195 remove_forwarder_block (basic_block bb, basic_block **worklist)
4197 edge succ = single_succ_edge (bb), e, s;
4198 basic_block dest = succ->dest;
4199 tree label;
4200 tree phi;
4201 edge_iterator ei;
4202 block_stmt_iterator bsi, bsi_to;
4203 bool seen_abnormal_edge = false;
4205 /* We check for infinite loops already in tree_forwarder_block_p.
4206 However it may happen that the infinite loop is created
4207 afterwards due to removal of forwarders. */
4208 if (dest == bb)
4209 return false;
4211 /* If the destination block consists of a nonlocal label, do not merge
4212 it. */
4213 label = first_stmt (dest);
4214 if (label
4215 && TREE_CODE (label) == LABEL_EXPR
4216 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4217 return false;
4219 /* If there is an abnormal edge to basic block BB, but not into
4220 dest, problems might occur during removal of the phi node at out
4221 of ssa due to overlapping live ranges of registers.
4223 If there is an abnormal edge in DEST, the problems would occur
4224 anyway since cleanup_dead_labels would then merge the labels for
4225 two different eh regions, and rest of exception handling code
4226 does not like it.
4228 So if there is an abnormal edge to BB, proceed only if there is
4229 no abnormal edge to DEST and there are no phi nodes in DEST. */
4230 if (has_abnormal_incoming_edge_p (bb))
4232 seen_abnormal_edge = true;
4234 if (has_abnormal_incoming_edge_p (dest)
4235 || phi_nodes (dest) != NULL_TREE)
4236 return false;
4239 /* If there are phi nodes in DEST, and some of the blocks that are
4240 predecessors of BB are also predecessors of DEST, check that the
4241 phi node arguments match. */
4242 if (phi_nodes (dest))
4244 FOR_EACH_EDGE (e, ei, bb->preds)
4246 s = find_edge (e->src, dest);
4247 if (!s)
4248 continue;
4250 if (!phi_alternatives_equal (dest, succ, s))
4251 return false;
4255 /* Redirect the edges. */
4256 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4258 if (e->flags & EDGE_ABNORMAL)
4260 /* If there is an abnormal edge, redirect it anyway, and
4261 move the labels to the new block to make it legal. */
4262 s = redirect_edge_succ_nodup (e, dest);
4264 else
4265 s = redirect_edge_and_branch (e, dest);
4267 if (s == e)
4269 /* Create arguments for the phi nodes, since the edge was not
4270 here before. */
4271 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4272 add_phi_arg (phi, PHI_ARG_DEF (phi, succ->dest_idx), s);
4274 else
4276 /* The source basic block might become a forwarder. We know
4277 that it was not a forwarder before, since it used to have
4278 at least two outgoing edges, so we may just add it to
4279 worklist. */
4280 if (tree_forwarder_block_p (s->src, false))
4281 *(*worklist)++ = s->src;
4285 if (seen_abnormal_edge)
4287 /* Move the labels to the new block, so that the redirection of
4288 the abnormal edges works. */
4290 bsi_to = bsi_start (dest);
4291 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
4293 label = bsi_stmt (bsi);
4294 gcc_assert (TREE_CODE (label) == LABEL_EXPR);
4295 bsi_remove (&bsi);
4296 bsi_insert_before (&bsi_to, label, BSI_CONTINUE_LINKING);
4300 /* Update the dominators. */
4301 if (dom_info_available_p (CDI_DOMINATORS))
4303 basic_block dom, dombb, domdest;
4305 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4306 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4307 if (domdest == bb)
4309 /* Shortcut to avoid calling (relatively expensive)
4310 nearest_common_dominator unless necessary. */
4311 dom = dombb;
4313 else
4314 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4316 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4319 /* And kill the forwarder block. */
4320 delete_basic_block (bb);
4322 return true;
4325 /* Removes forwarder blocks. */
4327 static bool
4328 cleanup_forwarder_blocks (void)
4330 basic_block bb;
4331 bool changed = false;
4332 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4333 basic_block *current = worklist;
4335 FOR_EACH_BB (bb)
4337 if (tree_forwarder_block_p (bb, false))
4338 *current++ = bb;
4341 while (current != worklist)
4343 bb = *--current;
4344 changed |= remove_forwarder_block (bb, &current);
4347 free (worklist);
4348 return changed;
4351 /* Merge the PHI nodes at BB into those at BB's sole successor. */
4353 static void
4354 remove_forwarder_block_with_phi (basic_block bb)
4356 edge succ = single_succ_edge (bb);
4357 basic_block dest = succ->dest;
4358 tree label;
4359 basic_block dombb, domdest, dom;
4361 /* We check for infinite loops already in tree_forwarder_block_p.
4362 However it may happen that the infinite loop is created
4363 afterwards due to removal of forwarders. */
4364 if (dest == bb)
4365 return;
4367 /* If the destination block consists of a nonlocal label, do not
4368 merge it. */
4369 label = first_stmt (dest);
4370 if (label
4371 && TREE_CODE (label) == LABEL_EXPR
4372 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4373 return;
4375 /* Redirect each incoming edge to BB to DEST. */
4376 while (EDGE_COUNT (bb->preds) > 0)
4378 edge e = EDGE_PRED (bb, 0), s;
4379 tree phi;
4381 s = find_edge (e->src, dest);
4382 if (s)
4384 /* We already have an edge S from E->src to DEST. If S and
4385 E->dest's sole successor edge have the same PHI arguments
4386 at DEST, redirect S to DEST. */
4387 if (phi_alternatives_equal (dest, s, succ))
4389 e = redirect_edge_and_branch (e, dest);
4390 PENDING_STMT (e) = NULL_TREE;
4391 continue;
4394 /* PHI arguments are different. Create a forwarder block by
4395 splitting E so that we can merge PHI arguments on E to
4396 DEST. */
4397 e = single_succ_edge (split_edge (e));
4400 s = redirect_edge_and_branch (e, dest);
4402 /* redirect_edge_and_branch must not create a new edge. */
4403 gcc_assert (s == e);
4405 /* Add to the PHI nodes at DEST each PHI argument removed at the
4406 destination of E. */
4407 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4409 tree def = PHI_ARG_DEF (phi, succ->dest_idx);
4411 if (TREE_CODE (def) == SSA_NAME)
4413 tree var;
4415 /* If DEF is one of the results of PHI nodes removed during
4416 redirection, replace it with the PHI argument that used
4417 to be on E. */
4418 for (var = PENDING_STMT (e); var; var = TREE_CHAIN (var))
4420 tree old_arg = TREE_PURPOSE (var);
4421 tree new_arg = TREE_VALUE (var);
4423 if (def == old_arg)
4425 def = new_arg;
4426 break;
4431 add_phi_arg (phi, def, s);
4434 PENDING_STMT (e) = NULL;
4437 /* Update the dominators. */
4438 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4439 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4440 if (domdest == bb)
4442 /* Shortcut to avoid calling (relatively expensive)
4443 nearest_common_dominator unless necessary. */
4444 dom = dombb;
4446 else
4447 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4449 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4451 /* Remove BB since all of BB's incoming edges have been redirected
4452 to DEST. */
4453 delete_basic_block (bb);
4456 /* This pass merges PHI nodes if one feeds into another. For example,
4457 suppose we have the following:
4459 goto <bb 9> (<L9>);
4461 <L8>:;
4462 tem_17 = foo ();
4464 # tem_6 = PHI <tem_17(8), tem_23(7)>;
4465 <L9>:;
4467 # tem_3 = PHI <tem_6(9), tem_2(5)>;
4468 <L10>:;
4470 Then we merge the first PHI node into the second one like so:
4472 goto <bb 9> (<L10>);
4474 <L8>:;
4475 tem_17 = foo ();
4477 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
4478 <L10>:;
4481 static void
4482 merge_phi_nodes (void)
4484 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4485 basic_block *current = worklist;
4486 basic_block bb;
4488 calculate_dominance_info (CDI_DOMINATORS);
4490 /* Find all PHI nodes that we may be able to merge. */
4491 FOR_EACH_BB (bb)
4493 basic_block dest;
4495 /* Look for a forwarder block with PHI nodes. */
4496 if (!tree_forwarder_block_p (bb, true))
4497 continue;
4499 dest = single_succ (bb);
4501 /* We have to feed into another basic block with PHI
4502 nodes. */
4503 if (!phi_nodes (dest)
4504 /* We don't want to deal with a basic block with
4505 abnormal edges. */
4506 || has_abnormal_incoming_edge_p (bb))
4507 continue;
4509 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
4511 /* If BB does not dominate DEST, then the PHI nodes at
4512 DEST must be the only users of the results of the PHI
4513 nodes at BB. */
4514 *current++ = bb;
4518 /* Now let's drain WORKLIST. */
4519 while (current != worklist)
4521 bb = *--current;
4522 remove_forwarder_block_with_phi (bb);
4525 free (worklist);
4528 static bool
4529 gate_merge_phi (void)
4531 return 1;
4534 struct tree_opt_pass pass_merge_phi = {
4535 "mergephi", /* name */
4536 gate_merge_phi, /* gate */
4537 merge_phi_nodes, /* execute */
4538 NULL, /* sub */
4539 NULL, /* next */
4540 0, /* static_pass_number */
4541 TV_TREE_MERGE_PHI, /* tv_id */
4542 PROP_cfg | PROP_ssa, /* properties_required */
4543 0, /* properties_provided */
4544 0, /* properties_destroyed */
4545 0, /* todo_flags_start */
4546 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */
4547 | TODO_verify_ssa,
4548 0 /* letter */
4551 /* Return a non-special label in the head of basic block BLOCK.
4552 Create one if it doesn't exist. */
4554 tree
4555 tree_block_label (basic_block bb)
4557 block_stmt_iterator i, s = bsi_start (bb);
4558 bool first = true;
4559 tree label, stmt;
4561 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4563 stmt = bsi_stmt (i);
4564 if (TREE_CODE (stmt) != LABEL_EXPR)
4565 break;
4566 label = LABEL_EXPR_LABEL (stmt);
4567 if (!DECL_NONLOCAL (label))
4569 if (!first)
4570 bsi_move_before (&i, &s);
4571 return label;
4575 label = create_artificial_label ();
4576 stmt = build1 (LABEL_EXPR, void_type_node, label);
4577 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4578 return label;
4582 /* Attempt to perform edge redirection by replacing a possibly complex
4583 jump instruction by a goto or by removing the jump completely.
4584 This can apply only if all edges now point to the same block. The
4585 parameters and return values are equivalent to
4586 redirect_edge_and_branch. */
4588 static edge
4589 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4591 basic_block src = e->src;
4592 block_stmt_iterator b;
4593 tree stmt;
4595 /* We can replace or remove a complex jump only when we have exactly
4596 two edges. */
4597 if (EDGE_COUNT (src->succs) != 2
4598 /* Verify that all targets will be TARGET. Specifically, the
4599 edge that is not E must also go to TARGET. */
4600 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4601 return NULL;
4603 b = bsi_last (src);
4604 if (bsi_end_p (b))
4605 return NULL;
4606 stmt = bsi_stmt (b);
4608 if (TREE_CODE (stmt) == COND_EXPR
4609 || TREE_CODE (stmt) == SWITCH_EXPR)
4611 bsi_remove (&b);
4612 e = ssa_redirect_edge (e, target);
4613 e->flags = EDGE_FALLTHRU;
4614 return e;
4617 return NULL;
4621 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4622 edge representing the redirected branch. */
4624 static edge
4625 tree_redirect_edge_and_branch (edge e, basic_block dest)
4627 basic_block bb = e->src;
4628 block_stmt_iterator bsi;
4629 edge ret;
4630 tree label, stmt;
4632 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4633 return NULL;
4635 if (e->src != ENTRY_BLOCK_PTR
4636 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4637 return ret;
4639 if (e->dest == dest)
4640 return NULL;
4642 label = tree_block_label (dest);
4644 bsi = bsi_last (bb);
4645 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4647 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4649 case COND_EXPR:
4650 stmt = (e->flags & EDGE_TRUE_VALUE
4651 ? COND_EXPR_THEN (stmt)
4652 : COND_EXPR_ELSE (stmt));
4653 GOTO_DESTINATION (stmt) = label;
4654 break;
4656 case GOTO_EXPR:
4657 /* No non-abnormal edges should lead from a non-simple goto, and
4658 simple ones should be represented implicitly. */
4659 gcc_unreachable ();
4661 case SWITCH_EXPR:
4663 tree cases = get_cases_for_edge (e, stmt);
4665 /* If we have a list of cases associated with E, then use it
4666 as it's a lot faster than walking the entire case vector. */
4667 if (cases)
4669 edge e2 = find_edge (e->src, dest);
4670 tree last, first;
4672 first = cases;
4673 while (cases)
4675 last = cases;
4676 CASE_LABEL (cases) = label;
4677 cases = TREE_CHAIN (cases);
4680 /* If there was already an edge in the CFG, then we need
4681 to move all the cases associated with E to E2. */
4682 if (e2)
4684 tree cases2 = get_cases_for_edge (e2, stmt);
4686 TREE_CHAIN (last) = TREE_CHAIN (cases2);
4687 TREE_CHAIN (cases2) = first;
4690 else
4692 tree vec = SWITCH_LABELS (stmt);
4693 size_t i, n = TREE_VEC_LENGTH (vec);
4695 for (i = 0; i < n; i++)
4697 tree elt = TREE_VEC_ELT (vec, i);
4699 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4700 CASE_LABEL (elt) = label;
4704 break;
4707 case RETURN_EXPR:
4708 bsi_remove (&bsi);
4709 e->flags |= EDGE_FALLTHRU;
4710 break;
4712 default:
4713 /* Otherwise it must be a fallthru edge, and we don't need to
4714 do anything besides redirecting it. */
4715 gcc_assert (e->flags & EDGE_FALLTHRU);
4716 break;
4719 /* Update/insert PHI nodes as necessary. */
4721 /* Now update the edges in the CFG. */
4722 e = ssa_redirect_edge (e, dest);
4724 return e;
4728 /* Simple wrapper, as we can always redirect fallthru edges. */
4730 static basic_block
4731 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4733 e = tree_redirect_edge_and_branch (e, dest);
4734 gcc_assert (e);
4736 return NULL;
4740 /* Splits basic block BB after statement STMT (but at least after the
4741 labels). If STMT is NULL, BB is split just after the labels. */
4743 static basic_block
4744 tree_split_block (basic_block bb, void *stmt)
4746 block_stmt_iterator bsi, bsi_tgt;
4747 tree act;
4748 basic_block new_bb;
4749 edge e;
4750 edge_iterator ei;
4752 new_bb = create_empty_bb (bb);
4754 /* Redirect the outgoing edges. */
4755 new_bb->succs = bb->succs;
4756 bb->succs = NULL;
4757 FOR_EACH_EDGE (e, ei, new_bb->succs)
4758 e->src = new_bb;
4760 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4761 stmt = NULL;
4763 /* Move everything from BSI to the new basic block. */
4764 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4766 act = bsi_stmt (bsi);
4767 if (TREE_CODE (act) == LABEL_EXPR)
4768 continue;
4770 if (!stmt)
4771 break;
4773 if (stmt == act)
4775 bsi_next (&bsi);
4776 break;
4780 bsi_tgt = bsi_start (new_bb);
4781 while (!bsi_end_p (bsi))
4783 act = bsi_stmt (bsi);
4784 bsi_remove (&bsi);
4785 bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT);
4788 return new_bb;
4792 /* Moves basic block BB after block AFTER. */
4794 static bool
4795 tree_move_block_after (basic_block bb, basic_block after)
4797 if (bb->prev_bb == after)
4798 return true;
4800 unlink_block (bb);
4801 link_block (bb, after);
4803 return true;
4807 /* Return true if basic_block can be duplicated. */
4809 static bool
4810 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED)
4812 return true;
4816 /* Create a duplicate of the basic block BB. NOTE: This does not
4817 preserve SSA form. */
4819 static basic_block
4820 tree_duplicate_bb (basic_block bb)
4822 basic_block new_bb;
4823 block_stmt_iterator bsi, bsi_tgt;
4824 tree phi;
4826 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4828 /* Copy the PHI nodes. We ignore PHI node arguments here because
4829 the incoming edges have not been setup yet. */
4830 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4832 tree copy = create_phi_node (PHI_RESULT (phi), new_bb);
4833 create_new_def_for (PHI_RESULT (copy), copy, PHI_RESULT_PTR (copy));
4836 /* Keep the chain of PHI nodes in the same order so that they can be
4837 updated by ssa_redirect_edge. */
4838 set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb)));
4840 bsi_tgt = bsi_start (new_bb);
4841 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4843 def_operand_p def_p;
4844 ssa_op_iter op_iter;
4845 tree stmt, copy;
4846 int region;
4848 stmt = bsi_stmt (bsi);
4849 if (TREE_CODE (stmt) == LABEL_EXPR)
4850 continue;
4852 /* Create a new copy of STMT and duplicate STMT's virtual
4853 operands. */
4854 copy = unshare_expr (stmt);
4855 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
4856 copy_virtual_operands (copy, stmt);
4857 region = lookup_stmt_eh_region (stmt);
4858 if (region >= 0)
4859 add_stmt_to_eh_region (copy, region);
4861 /* Create new names for all the definitions created by COPY and
4862 add replacement mappings for each new name. */
4863 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
4864 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
4867 return new_bb;
4871 /* Basic block BB_COPY was created by code duplication. Add phi node
4872 arguments for edges going out of BB_COPY. The blocks that were
4873 duplicated have rbi->duplicated set to one. */
4875 void
4876 add_phi_args_after_copy_bb (basic_block bb_copy)
4878 basic_block bb, dest;
4879 edge e, e_copy;
4880 edge_iterator ei;
4881 tree phi, phi_copy, phi_next, def;
4883 bb = bb_copy->rbi->original;
4885 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
4887 if (!phi_nodes (e_copy->dest))
4888 continue;
4890 if (e_copy->dest->rbi->duplicated)
4891 dest = e_copy->dest->rbi->original;
4892 else
4893 dest = e_copy->dest;
4895 e = find_edge (bb, dest);
4896 if (!e)
4898 /* During loop unrolling the target of the latch edge is copied.
4899 In this case we are not looking for edge to dest, but to
4900 duplicated block whose original was dest. */
4901 FOR_EACH_EDGE (e, ei, bb->succs)
4902 if (e->dest->rbi->duplicated
4903 && e->dest->rbi->original == dest)
4904 break;
4906 gcc_assert (e != NULL);
4909 for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
4910 phi;
4911 phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
4913 phi_next = PHI_CHAIN (phi);
4914 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4915 add_phi_arg (phi_copy, def, e_copy);
4920 /* Blocks in REGION_COPY array of length N_REGION were created by
4921 duplication of basic blocks. Add phi node arguments for edges
4922 going from these blocks. */
4924 void
4925 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region)
4927 unsigned i;
4929 for (i = 0; i < n_region; i++)
4930 region_copy[i]->rbi->duplicated = 1;
4932 for (i = 0; i < n_region; i++)
4933 add_phi_args_after_copy_bb (region_copy[i]);
4935 for (i = 0; i < n_region; i++)
4936 region_copy[i]->rbi->duplicated = 0;
4939 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4940 important exit edge EXIT. By important we mean that no SSA name defined
4941 inside region is live over the other exit edges of the region. All entry
4942 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4943 to the duplicate of the region. SSA form, dominance and loop information
4944 is updated. The new basic blocks are stored to REGION_COPY in the same
4945 order as they had in REGION, provided that REGION_COPY is not NULL.
4946 The function returns false if it is unable to copy the region,
4947 true otherwise. */
4949 bool
4950 tree_duplicate_sese_region (edge entry, edge exit,
4951 basic_block *region, unsigned n_region,
4952 basic_block *region_copy)
4954 unsigned i, n_doms;
4955 bool free_region_copy = false, copying_header = false;
4956 struct loop *loop = entry->dest->loop_father;
4957 edge exit_copy;
4958 basic_block *doms;
4959 edge redirected;
4960 int total_freq, entry_freq;
4962 if (!can_copy_bbs_p (region, n_region))
4963 return false;
4965 /* Some sanity checking. Note that we do not check for all possible
4966 missuses of the functions. I.e. if you ask to copy something weird,
4967 it will work, but the state of structures probably will not be
4968 correct. */
4969 for (i = 0; i < n_region; i++)
4971 /* We do not handle subloops, i.e. all the blocks must belong to the
4972 same loop. */
4973 if (region[i]->loop_father != loop)
4974 return false;
4976 if (region[i] != entry->dest
4977 && region[i] == loop->header)
4978 return false;
4981 loop->copy = loop;
4983 /* In case the function is used for loop header copying (which is the primary
4984 use), ensure that EXIT and its copy will be new latch and entry edges. */
4985 if (loop->header == entry->dest)
4987 copying_header = true;
4988 loop->copy = loop->outer;
4990 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
4991 return false;
4993 for (i = 0; i < n_region; i++)
4994 if (region[i] != exit->src
4995 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
4996 return false;
4999 if (!region_copy)
5001 region_copy = xmalloc (sizeof (basic_block) * n_region);
5002 free_region_copy = true;
5005 gcc_assert (!need_ssa_update_p ());
5007 /* Record blocks outside the region that are dominated by something
5008 inside. */
5009 doms = xmalloc (sizeof (basic_block) * n_basic_blocks);
5010 n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms);
5012 total_freq = entry->dest->frequency;
5013 entry_freq = EDGE_FREQUENCY (entry);
5014 /* Fix up corner cases, to avoid division by zero or creation of negative
5015 frequencies. */
5016 if (total_freq == 0)
5017 total_freq = 1;
5018 else if (entry_freq > total_freq)
5019 entry_freq = total_freq;
5021 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop);
5022 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
5023 total_freq);
5024 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
5026 if (copying_header)
5028 loop->header = exit->dest;
5029 loop->latch = exit->src;
5032 /* Redirect the entry and add the phi node arguments. */
5033 redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy);
5034 gcc_assert (redirected != NULL);
5035 flush_pending_stmts (entry);
5037 /* Concerning updating of dominators: We must recount dominators
5038 for entry block and its copy. Anything that is outside of the
5039 region, but was dominated by something inside needs recounting as
5040 well. */
5041 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5042 doms[n_doms++] = entry->dest->rbi->original;
5043 iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms);
5044 free (doms);
5046 /* Add the other PHI node arguments. */
5047 add_phi_args_after_copy (region_copy, n_region);
5049 /* Update the SSA web. */
5050 update_ssa (TODO_update_ssa);
5052 if (free_region_copy)
5053 free (region_copy);
5055 return true;
5059 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
5061 void
5062 dump_function_to_file (tree fn, FILE *file, int flags)
5064 tree arg, vars, var;
5065 bool ignore_topmost_bind = false, any_var = false;
5066 basic_block bb;
5067 tree chain;
5069 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
5071 arg = DECL_ARGUMENTS (fn);
5072 while (arg)
5074 print_generic_expr (file, arg, dump_flags);
5075 if (TREE_CHAIN (arg))
5076 fprintf (file, ", ");
5077 arg = TREE_CHAIN (arg);
5079 fprintf (file, ")\n");
5081 if (flags & TDF_DETAILS)
5082 dump_eh_tree (file, DECL_STRUCT_FUNCTION (fn));
5083 if (flags & TDF_RAW)
5085 dump_node (fn, TDF_SLIM | flags, file);
5086 return;
5089 /* When GIMPLE is lowered, the variables are no longer available in
5090 BIND_EXPRs, so display them separately. */
5091 if (cfun && cfun->decl == fn && cfun->unexpanded_var_list)
5093 ignore_topmost_bind = true;
5095 fprintf (file, "{\n");
5096 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
5098 var = TREE_VALUE (vars);
5100 print_generic_decl (file, var, flags);
5101 fprintf (file, "\n");
5103 any_var = true;
5107 if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
5109 /* Make a CFG based dump. */
5110 check_bb_profile (ENTRY_BLOCK_PTR, file);
5111 if (!ignore_topmost_bind)
5112 fprintf (file, "{\n");
5114 if (any_var && n_basic_blocks)
5115 fprintf (file, "\n");
5117 FOR_EACH_BB (bb)
5118 dump_generic_bb (file, bb, 2, flags);
5120 fprintf (file, "}\n");
5121 check_bb_profile (EXIT_BLOCK_PTR, file);
5123 else
5125 int indent;
5127 /* Make a tree based dump. */
5128 chain = DECL_SAVED_TREE (fn);
5130 if (TREE_CODE (chain) == BIND_EXPR)
5132 if (ignore_topmost_bind)
5134 chain = BIND_EXPR_BODY (chain);
5135 indent = 2;
5137 else
5138 indent = 0;
5140 else
5142 if (!ignore_topmost_bind)
5143 fprintf (file, "{\n");
5144 indent = 2;
5147 if (any_var)
5148 fprintf (file, "\n");
5150 print_generic_stmt_indented (file, chain, flags, indent);
5151 if (ignore_topmost_bind)
5152 fprintf (file, "}\n");
5155 fprintf (file, "\n\n");
5159 /* Pretty print of the loops intermediate representation. */
5160 static void print_loop (FILE *, struct loop *, int);
5161 static void print_pred_bbs (FILE *, basic_block bb);
5162 static void print_succ_bbs (FILE *, basic_block bb);
5165 /* Print the predecessors indexes of edge E on FILE. */
5167 static void
5168 print_pred_bbs (FILE *file, basic_block bb)
5170 edge e;
5171 edge_iterator ei;
5173 FOR_EACH_EDGE (e, ei, bb->preds)
5174 fprintf (file, "bb_%d", e->src->index);
5178 /* Print the successors indexes of edge E on FILE. */
5180 static void
5181 print_succ_bbs (FILE *file, basic_block bb)
5183 edge e;
5184 edge_iterator ei;
5186 FOR_EACH_EDGE (e, ei, bb->succs)
5187 fprintf (file, "bb_%d", e->src->index);
5191 /* Pretty print LOOP on FILE, indented INDENT spaces. */
5193 static void
5194 print_loop (FILE *file, struct loop *loop, int indent)
5196 char *s_indent;
5197 basic_block bb;
5199 if (loop == NULL)
5200 return;
5202 s_indent = (char *) alloca ((size_t) indent + 1);
5203 memset ((void *) s_indent, ' ', (size_t) indent);
5204 s_indent[indent] = '\0';
5206 /* Print the loop's header. */
5207 fprintf (file, "%sloop_%d\n", s_indent, loop->num);
5209 /* Print the loop's body. */
5210 fprintf (file, "%s{\n", s_indent);
5211 FOR_EACH_BB (bb)
5212 if (bb->loop_father == loop)
5214 /* Print the basic_block's header. */
5215 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
5216 print_pred_bbs (file, bb);
5217 fprintf (file, "}, succs = {");
5218 print_succ_bbs (file, bb);
5219 fprintf (file, "})\n");
5221 /* Print the basic_block's body. */
5222 fprintf (file, "%s {\n", s_indent);
5223 tree_dump_bb (bb, file, indent + 4);
5224 fprintf (file, "%s }\n", s_indent);
5227 print_loop (file, loop->inner, indent + 2);
5228 fprintf (file, "%s}\n", s_indent);
5229 print_loop (file, loop->next, indent);
5233 /* Follow a CFG edge from the entry point of the program, and on entry
5234 of a loop, pretty print the loop structure on FILE. */
5236 void
5237 print_loop_ir (FILE *file)
5239 basic_block bb;
5241 bb = BASIC_BLOCK (0);
5242 if (bb && bb->loop_father)
5243 print_loop (file, bb->loop_father, 0);
5247 /* Debugging loops structure at tree level. */
5249 void
5250 debug_loop_ir (void)
5252 print_loop_ir (stderr);
5256 /* Return true if BB ends with a call, possibly followed by some
5257 instructions that must stay with the call. Return false,
5258 otherwise. */
5260 static bool
5261 tree_block_ends_with_call_p (basic_block bb)
5263 block_stmt_iterator bsi = bsi_last (bb);
5264 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
5268 /* Return true if BB ends with a conditional branch. Return false,
5269 otherwise. */
5271 static bool
5272 tree_block_ends_with_condjump_p (basic_block bb)
5274 tree stmt = last_stmt (bb);
5275 return (stmt && TREE_CODE (stmt) == COND_EXPR);
5279 /* Return true if we need to add fake edge to exit at statement T.
5280 Helper function for tree_flow_call_edges_add. */
5282 static bool
5283 need_fake_edge_p (tree t)
5285 tree call;
5287 /* NORETURN and LONGJMP calls already have an edge to exit.
5288 CONST and PURE calls do not need one.
5289 We don't currently check for CONST and PURE here, although
5290 it would be a good idea, because those attributes are
5291 figured out from the RTL in mark_constant_function, and
5292 the counter incrementation code from -fprofile-arcs
5293 leads to different results from -fbranch-probabilities. */
5294 call = get_call_expr_in (t);
5295 if (call
5296 && !(call_expr_flags (call) & ECF_NORETURN))
5297 return true;
5299 if (TREE_CODE (t) == ASM_EXPR
5300 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
5301 return true;
5303 return false;
5307 /* Add fake edges to the function exit for any non constant and non
5308 noreturn calls, volatile inline assembly in the bitmap of blocks
5309 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
5310 the number of blocks that were split.
5312 The goal is to expose cases in which entering a basic block does
5313 not imply that all subsequent instructions must be executed. */
5315 static int
5316 tree_flow_call_edges_add (sbitmap blocks)
5318 int i;
5319 int blocks_split = 0;
5320 int last_bb = last_basic_block;
5321 bool check_last_block = false;
5323 if (n_basic_blocks == 0)
5324 return 0;
5326 if (! blocks)
5327 check_last_block = true;
5328 else
5329 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
5331 /* In the last basic block, before epilogue generation, there will be
5332 a fallthru edge to EXIT. Special care is required if the last insn
5333 of the last basic block is a call because make_edge folds duplicate
5334 edges, which would result in the fallthru edge also being marked
5335 fake, which would result in the fallthru edge being removed by
5336 remove_fake_edges, which would result in an invalid CFG.
5338 Moreover, we can't elide the outgoing fake edge, since the block
5339 profiler needs to take this into account in order to solve the minimal
5340 spanning tree in the case that the call doesn't return.
5342 Handle this by adding a dummy instruction in a new last basic block. */
5343 if (check_last_block)
5345 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
5346 block_stmt_iterator bsi = bsi_last (bb);
5347 tree t = NULL_TREE;
5348 if (!bsi_end_p (bsi))
5349 t = bsi_stmt (bsi);
5351 if (need_fake_edge_p (t))
5353 edge e;
5355 e = find_edge (bb, EXIT_BLOCK_PTR);
5356 if (e)
5358 bsi_insert_on_edge (e, build_empty_stmt ());
5359 bsi_commit_edge_inserts ();
5364 /* Now add fake edges to the function exit for any non constant
5365 calls since there is no way that we can determine if they will
5366 return or not... */
5367 for (i = 0; i < last_bb; i++)
5369 basic_block bb = BASIC_BLOCK (i);
5370 block_stmt_iterator bsi;
5371 tree stmt, last_stmt;
5373 if (!bb)
5374 continue;
5376 if (blocks && !TEST_BIT (blocks, i))
5377 continue;
5379 bsi = bsi_last (bb);
5380 if (!bsi_end_p (bsi))
5382 last_stmt = bsi_stmt (bsi);
5385 stmt = bsi_stmt (bsi);
5386 if (need_fake_edge_p (stmt))
5388 edge e;
5389 /* The handling above of the final block before the
5390 epilogue should be enough to verify that there is
5391 no edge to the exit block in CFG already.
5392 Calling make_edge in such case would cause us to
5393 mark that edge as fake and remove it later. */
5394 #ifdef ENABLE_CHECKING
5395 if (stmt == last_stmt)
5397 e = find_edge (bb, EXIT_BLOCK_PTR);
5398 gcc_assert (e == NULL);
5400 #endif
5402 /* Note that the following may create a new basic block
5403 and renumber the existing basic blocks. */
5404 if (stmt != last_stmt)
5406 e = split_block (bb, stmt);
5407 if (e)
5408 blocks_split++;
5410 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
5412 bsi_prev (&bsi);
5414 while (!bsi_end_p (bsi));
5418 if (blocks_split)
5419 verify_flow_info ();
5421 return blocks_split;
5424 bool
5425 tree_purge_dead_eh_edges (basic_block bb)
5427 bool changed = false;
5428 edge e;
5429 edge_iterator ei;
5430 tree stmt = last_stmt (bb);
5432 if (stmt && tree_can_throw_internal (stmt))
5433 return false;
5435 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5437 if (e->flags & EDGE_EH)
5439 remove_edge (e);
5440 changed = true;
5442 else
5443 ei_next (&ei);
5446 /* Removal of dead EH edges might change dominators of not
5447 just immediate successors. E.g. when bb1 is changed so that
5448 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5449 eh edges purged by this function in:
5453 1-->2
5454 / \ |
5455 v v |
5456 3-->4 |
5458 --->5
5461 idom(bb5) must be recomputed. For now just free the dominance
5462 info. */
5463 if (changed)
5464 free_dominance_info (CDI_DOMINATORS);
5466 return changed;
5469 bool
5470 tree_purge_all_dead_eh_edges (bitmap blocks)
5472 bool changed = false;
5473 unsigned i;
5474 bitmap_iterator bi;
5476 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
5478 changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
5481 return changed;
5484 /* This function is called whenever a new edge is created or
5485 redirected. */
5487 static void
5488 tree_execute_on_growing_pred (edge e)
5490 basic_block bb = e->dest;
5492 if (phi_nodes (bb))
5493 reserve_phi_args_for_new_edge (bb);
5496 /* This function is called immediately before edge E is removed from
5497 the edge vector E->dest->preds. */
5499 static void
5500 tree_execute_on_shrinking_pred (edge e)
5502 if (phi_nodes (e->dest))
5503 remove_phi_args (e);
5506 /*---------------------------------------------------------------------------
5507 Helper functions for Loop versioning
5508 ---------------------------------------------------------------------------*/
5510 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
5511 of 'first'. Both of them are dominated by 'new_head' basic block. When
5512 'new_head' was created by 'second's incoming edge it received phi arguments
5513 on the edge by split_edge(). Later, additional edge 'e' was created to
5514 connect 'new_head' and 'first'. Now this routine adds phi args on this
5515 additional edge 'e' that new_head to second edge received as part of edge
5516 splitting.
5519 static void
5520 tree_lv_adjust_loop_header_phi (basic_block first, basic_block second,
5521 basic_block new_head, edge e)
5523 tree phi1, phi2;
5524 edge e2 = find_edge (new_head, second);
5526 /* Because NEW_HEAD has been created by splitting SECOND's incoming
5527 edge, we should always have an edge from NEW_HEAD to SECOND. */
5528 gcc_assert (e2 != NULL);
5530 /* Browse all 'second' basic block phi nodes and add phi args to
5531 edge 'e' for 'first' head. PHI args are always in correct order. */
5533 for (phi2 = phi_nodes (second), phi1 = phi_nodes (first);
5534 phi2 && phi1;
5535 phi2 = PHI_CHAIN (phi2), phi1 = PHI_CHAIN (phi1))
5537 tree def = PHI_ARG_DEF (phi2, e2->dest_idx);
5538 add_phi_arg (phi1, def, e);
5542 /* Adds a if else statement to COND_BB with condition COND_EXPR.
5543 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
5544 the destination of the ELSE part. */
5545 static void
5546 tree_lv_add_condition_to_bb (basic_block first_head, basic_block second_head,
5547 basic_block cond_bb, void *cond_e)
5549 block_stmt_iterator bsi;
5550 tree goto1 = NULL_TREE;
5551 tree goto2 = NULL_TREE;
5552 tree new_cond_expr = NULL_TREE;
5553 tree cond_expr = (tree) cond_e;
5554 edge e0;
5556 /* Build new conditional expr */
5557 goto1 = build1 (GOTO_EXPR, void_type_node, tree_block_label (first_head));
5558 goto2 = build1 (GOTO_EXPR, void_type_node, tree_block_label (second_head));
5559 new_cond_expr = build3 (COND_EXPR, void_type_node, cond_expr, goto1, goto2);
5561 /* Add new cond in cond_bb. */
5562 bsi = bsi_start (cond_bb);
5563 bsi_insert_after (&bsi, new_cond_expr, BSI_NEW_STMT);
5564 /* Adjust edges appropriately to connect new head with first head
5565 as well as second head. */
5566 e0 = single_succ_edge (cond_bb);
5567 e0->flags &= ~EDGE_FALLTHRU;
5568 e0->flags |= EDGE_FALSE_VALUE;
5571 struct cfg_hooks tree_cfg_hooks = {
5572 "tree",
5573 tree_verify_flow_info,
5574 tree_dump_bb, /* dump_bb */
5575 create_bb, /* create_basic_block */
5576 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
5577 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
5578 remove_bb, /* delete_basic_block */
5579 tree_split_block, /* split_block */
5580 tree_move_block_after, /* move_block_after */
5581 tree_can_merge_blocks_p, /* can_merge_blocks_p */
5582 tree_merge_blocks, /* merge_blocks */
5583 tree_predict_edge, /* predict_edge */
5584 tree_predicted_by_p, /* predicted_by_p */
5585 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
5586 tree_duplicate_bb, /* duplicate_block */
5587 tree_split_edge, /* split_edge */
5588 tree_make_forwarder_block, /* make_forward_block */
5589 NULL, /* tidy_fallthru_edge */
5590 tree_block_ends_with_call_p, /* block_ends_with_call_p */
5591 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
5592 tree_flow_call_edges_add, /* flow_call_edges_add */
5593 tree_execute_on_growing_pred, /* execute_on_growing_pred */
5594 tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
5595 tree_duplicate_loop_to_header_edge, /* duplicate loop for trees */
5596 tree_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5597 tree_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
5598 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
5599 flush_pending_stmts /* flush_pending_stmts */
5603 /* Split all critical edges. */
5605 static void
5606 split_critical_edges (void)
5608 basic_block bb;
5609 edge e;
5610 edge_iterator ei;
5612 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
5613 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
5614 mappings around the calls to split_edge. */
5615 start_recording_case_labels ();
5616 FOR_ALL_BB (bb)
5618 FOR_EACH_EDGE (e, ei, bb->succs)
5619 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
5621 split_edge (e);
5624 end_recording_case_labels ();
5627 struct tree_opt_pass pass_split_crit_edges =
5629 "crited", /* name */
5630 NULL, /* gate */
5631 split_critical_edges, /* execute */
5632 NULL, /* sub */
5633 NULL, /* next */
5634 0, /* static_pass_number */
5635 TV_TREE_SPLIT_EDGES, /* tv_id */
5636 PROP_cfg, /* properties required */
5637 PROP_no_crit_edges, /* properties_provided */
5638 0, /* properties_destroyed */
5639 0, /* todo_flags_start */
5640 TODO_dump_func, /* todo_flags_finish */
5641 0 /* letter */
5645 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5646 a temporary, make sure and register it to be renamed if necessary,
5647 and finally return the temporary. Put the statements to compute
5648 EXP before the current statement in BSI. */
5650 tree
5651 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
5653 tree t, new_stmt, orig_stmt;
5655 if (is_gimple_val (exp))
5656 return exp;
5658 t = make_rename_temp (type, NULL);
5659 new_stmt = build (MODIFY_EXPR, type, t, exp);
5661 orig_stmt = bsi_stmt (*bsi);
5662 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
5663 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
5665 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
5667 return t;
5670 /* Build a ternary operation and gimplify it. Emit code before BSI.
5671 Return the gimple_val holding the result. */
5673 tree
5674 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
5675 tree type, tree a, tree b, tree c)
5677 tree ret;
5679 ret = fold (build3 (code, type, a, b, c));
5680 STRIP_NOPS (ret);
5682 return gimplify_val (bsi, type, ret);
5685 /* Build a binary operation and gimplify it. Emit code before BSI.
5686 Return the gimple_val holding the result. */
5688 tree
5689 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
5690 tree type, tree a, tree b)
5692 tree ret;
5694 ret = fold (build2 (code, type, a, b));
5695 STRIP_NOPS (ret);
5697 return gimplify_val (bsi, type, ret);
5700 /* Build a unary operation and gimplify it. Emit code before BSI.
5701 Return the gimple_val holding the result. */
5703 tree
5704 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
5705 tree a)
5707 tree ret;
5709 ret = fold (build1 (code, type, a));
5710 STRIP_NOPS (ret);
5712 return gimplify_val (bsi, type, ret);
5717 /* Emit return warnings. */
5719 static void
5720 execute_warn_function_return (void)
5722 #ifdef USE_MAPPED_LOCATION
5723 source_location location;
5724 #else
5725 location_t *locus;
5726 #endif
5727 tree last;
5728 edge e;
5729 edge_iterator ei;
5731 /* If we have a path to EXIT, then we do return. */
5732 if (TREE_THIS_VOLATILE (cfun->decl)
5733 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
5735 #ifdef USE_MAPPED_LOCATION
5736 location = UNKNOWN_LOCATION;
5737 #else
5738 locus = NULL;
5739 #endif
5740 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5742 last = last_stmt (e->src);
5743 if (TREE_CODE (last) == RETURN_EXPR
5744 #ifdef USE_MAPPED_LOCATION
5745 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
5746 #else
5747 && (locus = EXPR_LOCUS (last)) != NULL)
5748 #endif
5749 break;
5751 #ifdef USE_MAPPED_LOCATION
5752 if (location == UNKNOWN_LOCATION)
5753 location = cfun->function_end_locus;
5754 warning (0, "%H%<noreturn%> function does return", &location);
5755 #else
5756 if (!locus)
5757 locus = &cfun->function_end_locus;
5758 warning (0, "%H%<noreturn%> function does return", locus);
5759 #endif
5762 /* If we see "return;" in some basic block, then we do reach the end
5763 without returning a value. */
5764 else if (warn_return_type
5765 && !TREE_NO_WARNING (cfun->decl)
5766 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
5767 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
5769 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5771 tree last = last_stmt (e->src);
5772 if (TREE_CODE (last) == RETURN_EXPR
5773 && TREE_OPERAND (last, 0) == NULL)
5775 #ifdef USE_MAPPED_LOCATION
5776 location = EXPR_LOCATION (last);
5777 if (location == UNKNOWN_LOCATION)
5778 location = cfun->function_end_locus;
5779 warning (0, "%Hcontrol reaches end of non-void function", &location);
5780 #else
5781 locus = EXPR_LOCUS (last);
5782 if (!locus)
5783 locus = &cfun->function_end_locus;
5784 warning (0, "%Hcontrol reaches end of non-void function", locus);
5785 #endif
5786 TREE_NO_WARNING (cfun->decl) = 1;
5787 break;
5794 /* Given a basic block B which ends with a conditional and has
5795 precisely two successors, determine which of the edges is taken if
5796 the conditional is true and which is taken if the conditional is
5797 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5799 void
5800 extract_true_false_edges_from_block (basic_block b,
5801 edge *true_edge,
5802 edge *false_edge)
5804 edge e = EDGE_SUCC (b, 0);
5806 if (e->flags & EDGE_TRUE_VALUE)
5808 *true_edge = e;
5809 *false_edge = EDGE_SUCC (b, 1);
5811 else
5813 *false_edge = e;
5814 *true_edge = EDGE_SUCC (b, 1);
5818 struct tree_opt_pass pass_warn_function_return =
5820 NULL, /* name */
5821 NULL, /* gate */
5822 execute_warn_function_return, /* execute */
5823 NULL, /* sub */
5824 NULL, /* next */
5825 0, /* static_pass_number */
5826 0, /* tv_id */
5827 PROP_cfg, /* properties_required */
5828 0, /* properties_provided */
5829 0, /* properties_destroyed */
5830 0, /* todo_flags_start */
5831 0, /* todo_flags_finish */
5832 0 /* letter */
5835 /* Emit noreturn warnings. */
5837 static void
5838 execute_warn_function_noreturn (void)
5840 if (warn_missing_noreturn
5841 && !TREE_THIS_VOLATILE (cfun->decl)
5842 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
5843 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
5844 warning (0, "%Jfunction might be possible candidate for "
5845 "attribute %<noreturn%>",
5846 cfun->decl);
5849 struct tree_opt_pass pass_warn_function_noreturn =
5851 NULL, /* name */
5852 NULL, /* gate */
5853 execute_warn_function_noreturn, /* execute */
5854 NULL, /* sub */
5855 NULL, /* next */
5856 0, /* static_pass_number */
5857 0, /* tv_id */
5858 PROP_cfg, /* properties_required */
5859 0, /* properties_provided */
5860 0, /* properties_destroyed */
5861 0, /* todo_flags_start */
5862 0, /* todo_flags_finish */
5863 0 /* letter */