2005-05-19 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / tree-cfg.c
blob9eeb78392873a64dfafde04f6ab7e6db0b58bbf4
1 /* Control flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "errors.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "ggc.h"
37 #include "langhooks.h"
38 #include "diagnostic.h"
39 #include "tree-flow.h"
40 #include "timevar.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "cfgloop.h"
46 #include "cfglayout.h"
47 #include "hashtab.h"
49 /* This file contains functions for building the Control Flow Graph (CFG)
50 for a function tree. */
52 /* Local declarations. */
54 /* Initial capacity for the basic block array. */
55 static const int initial_cfg_capacity = 20;
57 /* This hash table allows us to efficiently lookup all CASE_LABEL_EXPRs
58 which use a particular edge. The CASE_LABEL_EXPRs are chained together
59 via their TREE_CHAIN field, which we clear after we're done with the
60 hash table to prevent problems with duplication of SWITCH_EXPRs.
62 Access to this list of CASE_LABEL_EXPRs allows us to efficiently
63 update the case vector in response to edge redirections.
65 Right now this table is set up and torn down at key points in the
66 compilation process. It would be nice if we could make the table
67 more persistent. The key is getting notification of changes to
68 the CFG (particularly edge removal, creation and redirection). */
70 struct edge_to_cases_elt
72 /* The edge itself. Necessary for hashing and equality tests. */
73 edge e;
75 /* The case labels associated with this edge. We link these up via
76 their TREE_CHAIN field, then we wipe out the TREE_CHAIN fields
77 when we destroy the hash table. This prevents problems when copying
78 SWITCH_EXPRs. */
79 tree case_labels;
82 static htab_t edge_to_cases;
84 /* CFG statistics. */
85 struct cfg_stats_d
87 long num_merged_labels;
90 static struct cfg_stats_d cfg_stats;
92 /* Nonzero if we found a computed goto while building basic blocks. */
93 static bool found_computed_goto;
95 /* Basic blocks and flowgraphs. */
96 static basic_block create_bb (void *, void *, basic_block);
97 static void create_block_annotation (basic_block);
98 static void free_blocks_annotations (void);
99 static void clear_blocks_annotations (void);
100 static void make_blocks (tree);
101 static void factor_computed_gotos (void);
103 /* Edges. */
104 static void make_edges (void);
105 static void make_ctrl_stmt_edges (basic_block);
106 static void make_exit_edges (basic_block);
107 static void make_cond_expr_edges (basic_block);
108 static void make_switch_expr_edges (basic_block);
109 static void make_goto_expr_edges (basic_block);
110 static edge tree_redirect_edge_and_branch (edge, basic_block);
111 static edge tree_try_redirect_by_replacing_jump (edge, basic_block);
112 static void split_critical_edges (void);
113 static bool remove_fallthru_edge (VEC(edge,gc) *);
115 /* Various helpers. */
116 static inline bool stmt_starts_bb_p (tree, tree);
117 static int tree_verify_flow_info (void);
118 static void tree_make_forwarder_block (edge);
119 static bool tree_forwarder_block_p (basic_block, bool);
120 static void tree_cfg2vcg (FILE *);
122 /* Flowgraph optimization and cleanup. */
123 static void tree_merge_blocks (basic_block, basic_block);
124 static bool tree_can_merge_blocks_p (basic_block, basic_block);
125 static void remove_bb (basic_block);
126 static bool cleanup_control_flow (void);
127 static bool cleanup_control_expr_graph (basic_block, block_stmt_iterator);
128 static edge find_taken_edge_computed_goto (basic_block, tree);
129 static edge find_taken_edge_cond_expr (basic_block, tree);
130 static edge find_taken_edge_switch_expr (basic_block, tree);
131 static tree find_case_label_for_value (tree, tree);
132 static bool phi_alternatives_equal (basic_block, edge, edge);
133 static bool cleanup_forwarder_blocks (void);
135 void
136 init_empty_tree_cfg (void)
138 /* Initialize the basic block array. */
139 init_flow ();
140 profile_status = PROFILE_ABSENT;
141 n_basic_blocks = 0;
142 last_basic_block = 0;
143 VARRAY_BB_INIT (basic_block_info, initial_cfg_capacity, "basic_block_info");
145 /* Build a mapping of labels to their associated blocks. */
146 VARRAY_BB_INIT (label_to_block_map, initial_cfg_capacity,
147 "label to block map");
149 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
150 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
152 create_block_annotation (ENTRY_BLOCK_PTR);
153 create_block_annotation (EXIT_BLOCK_PTR);
156 /*---------------------------------------------------------------------------
157 Create basic blocks
158 ---------------------------------------------------------------------------*/
160 /* Entry point to the CFG builder for trees. TP points to the list of
161 statements to be added to the flowgraph. */
163 static void
164 build_tree_cfg (tree *tp)
166 /* Register specific tree functions. */
167 tree_register_cfg_hooks ();
169 memset ((void *) &cfg_stats, 0, sizeof (cfg_stats));
171 init_empty_tree_cfg ();
173 found_computed_goto = 0;
174 make_blocks (*tp);
176 /* Computed gotos are hell to deal with, especially if there are
177 lots of them with a large number of destinations. So we factor
178 them to a common computed goto location before we build the
179 edge list. After we convert back to normal form, we will un-factor
180 the computed gotos since factoring introduces an unwanted jump. */
181 if (found_computed_goto)
182 factor_computed_gotos ();
184 /* Make sure there is always at least one block, even if it's empty. */
185 if (n_basic_blocks == 0)
186 create_empty_bb (ENTRY_BLOCK_PTR);
188 /* Adjust the size of the array. */
189 VARRAY_GROW (basic_block_info, n_basic_blocks);
191 /* To speed up statement iterator walks, we first purge dead labels. */
192 cleanup_dead_labels ();
194 /* Group case nodes to reduce the number of edges.
195 We do this after cleaning up dead labels because otherwise we miss
196 a lot of obvious case merging opportunities. */
197 group_case_labels ();
199 /* Create the edges of the flowgraph. */
200 make_edges ();
202 /* Debugging dumps. */
204 /* Write the flowgraph to a VCG file. */
206 int local_dump_flags;
207 FILE *dump_file = dump_begin (TDI_vcg, &local_dump_flags);
208 if (dump_file)
210 tree_cfg2vcg (dump_file);
211 dump_end (TDI_vcg, dump_file);
215 #ifdef ENABLE_CHECKING
216 verify_stmts ();
217 #endif
219 /* Dump a textual representation of the flowgraph. */
220 if (dump_file)
221 dump_tree_cfg (dump_file, dump_flags);
224 static void
225 execute_build_cfg (void)
227 build_tree_cfg (&DECL_SAVED_TREE (current_function_decl));
230 struct tree_opt_pass pass_build_cfg =
232 "cfg", /* name */
233 NULL, /* gate */
234 execute_build_cfg, /* execute */
235 NULL, /* sub */
236 NULL, /* next */
237 0, /* static_pass_number */
238 TV_TREE_CFG, /* tv_id */
239 PROP_gimple_leh, /* properties_required */
240 PROP_cfg, /* properties_provided */
241 0, /* properties_destroyed */
242 0, /* todo_flags_start */
243 TODO_verify_stmts, /* todo_flags_finish */
244 0 /* letter */
247 /* Search the CFG for any computed gotos. If found, factor them to a
248 common computed goto site. Also record the location of that site so
249 that we can un-factor the gotos after we have converted back to
250 normal form. */
252 static void
253 factor_computed_gotos (void)
255 basic_block bb;
256 tree factored_label_decl = NULL;
257 tree var = NULL;
258 tree factored_computed_goto_label = NULL;
259 tree factored_computed_goto = NULL;
261 /* We know there are one or more computed gotos in this function.
262 Examine the last statement in each basic block to see if the block
263 ends with a computed goto. */
265 FOR_EACH_BB (bb)
267 block_stmt_iterator bsi = bsi_last (bb);
268 tree last;
270 if (bsi_end_p (bsi))
271 continue;
272 last = bsi_stmt (bsi);
274 /* Ignore the computed goto we create when we factor the original
275 computed gotos. */
276 if (last == factored_computed_goto)
277 continue;
279 /* If the last statement is a computed goto, factor it. */
280 if (computed_goto_p (last))
282 tree assignment;
284 /* The first time we find a computed goto we need to create
285 the factored goto block and the variable each original
286 computed goto will use for their goto destination. */
287 if (! factored_computed_goto)
289 basic_block new_bb = create_empty_bb (bb);
290 block_stmt_iterator new_bsi = bsi_start (new_bb);
292 /* Create the destination of the factored goto. Each original
293 computed goto will put its desired destination into this
294 variable and jump to the label we create immediately
295 below. */
296 var = create_tmp_var (ptr_type_node, "gotovar");
298 /* Build a label for the new block which will contain the
299 factored computed goto. */
300 factored_label_decl = create_artificial_label ();
301 factored_computed_goto_label
302 = build1 (LABEL_EXPR, void_type_node, factored_label_decl);
303 bsi_insert_after (&new_bsi, factored_computed_goto_label,
304 BSI_NEW_STMT);
306 /* Build our new computed goto. */
307 factored_computed_goto = build1 (GOTO_EXPR, void_type_node, var);
308 bsi_insert_after (&new_bsi, factored_computed_goto,
309 BSI_NEW_STMT);
312 /* Copy the original computed goto's destination into VAR. */
313 assignment = build (MODIFY_EXPR, ptr_type_node,
314 var, GOTO_DESTINATION (last));
315 bsi_insert_before (&bsi, assignment, BSI_SAME_STMT);
317 /* And re-vector the computed goto to the new destination. */
318 GOTO_DESTINATION (last) = factored_label_decl;
324 /* Create annotations for a single basic block. */
326 static void
327 create_block_annotation (basic_block bb)
329 /* Verify that the tree_annotations field is clear. */
330 gcc_assert (!bb->tree_annotations);
331 bb->tree_annotations = ggc_alloc_cleared (sizeof (struct bb_ann_d));
335 /* Free the annotations for all the basic blocks. */
337 static void free_blocks_annotations (void)
339 clear_blocks_annotations ();
343 /* Clear the annotations for all the basic blocks. */
345 static void
346 clear_blocks_annotations (void)
348 basic_block bb;
350 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
351 bb->tree_annotations = NULL;
355 /* Build a flowgraph for the statement_list STMT_LIST. */
357 static void
358 make_blocks (tree stmt_list)
360 tree_stmt_iterator i = tsi_start (stmt_list);
361 tree stmt = NULL;
362 bool start_new_block = true;
363 bool first_stmt_of_list = true;
364 basic_block bb = ENTRY_BLOCK_PTR;
366 while (!tsi_end_p (i))
368 tree prev_stmt;
370 prev_stmt = stmt;
371 stmt = tsi_stmt (i);
373 /* If the statement starts a new basic block or if we have determined
374 in a previous pass that we need to create a new block for STMT, do
375 so now. */
376 if (start_new_block || stmt_starts_bb_p (stmt, prev_stmt))
378 if (!first_stmt_of_list)
379 stmt_list = tsi_split_statement_list_before (&i);
380 bb = create_basic_block (stmt_list, NULL, bb);
381 start_new_block = false;
384 /* Now add STMT to BB and create the subgraphs for special statement
385 codes. */
386 set_bb_for_stmt (stmt, bb);
388 if (computed_goto_p (stmt))
389 found_computed_goto = true;
391 /* If STMT is a basic block terminator, set START_NEW_BLOCK for the
392 next iteration. */
393 if (stmt_ends_bb_p (stmt))
394 start_new_block = true;
396 tsi_next (&i);
397 first_stmt_of_list = false;
402 /* Create and return a new empty basic block after bb AFTER. */
404 static basic_block
405 create_bb (void *h, void *e, basic_block after)
407 basic_block bb;
409 gcc_assert (!e);
411 /* Create and initialize a new basic block. Since alloc_block uses
412 ggc_alloc_cleared to allocate a basic block, we do not have to
413 clear the newly allocated basic block here. */
414 bb = alloc_block ();
416 bb->index = last_basic_block;
417 bb->flags = BB_NEW;
418 bb->stmt_list = h ? h : alloc_stmt_list ();
420 /* Add the new block to the linked list of blocks. */
421 link_block (bb, after);
423 /* Grow the basic block array if needed. */
424 if ((size_t) last_basic_block == VARRAY_SIZE (basic_block_info))
426 size_t new_size = last_basic_block + (last_basic_block + 3) / 4;
427 VARRAY_GROW (basic_block_info, new_size);
430 /* Add the newly created block to the array. */
431 BASIC_BLOCK (last_basic_block) = bb;
433 create_block_annotation (bb);
435 n_basic_blocks++;
436 last_basic_block++;
438 initialize_bb_rbi (bb);
439 return bb;
443 /*---------------------------------------------------------------------------
444 Edge creation
445 ---------------------------------------------------------------------------*/
447 /* Fold COND_EXPR_COND of each COND_EXPR. */
449 void
450 fold_cond_expr_cond (void)
452 basic_block bb;
454 FOR_EACH_BB (bb)
456 tree stmt = last_stmt (bb);
458 if (stmt
459 && TREE_CODE (stmt) == COND_EXPR)
461 tree cond = fold (COND_EXPR_COND (stmt));
462 if (integer_zerop (cond))
463 COND_EXPR_COND (stmt) = boolean_false_node;
464 else if (integer_onep (cond))
465 COND_EXPR_COND (stmt) = boolean_true_node;
470 /* Join all the blocks in the flowgraph. */
472 static void
473 make_edges (void)
475 basic_block bb;
477 /* Create an edge from entry to the first block with executable
478 statements in it. */
479 make_edge (ENTRY_BLOCK_PTR, BASIC_BLOCK (0), EDGE_FALLTHRU);
481 /* Traverse the basic block array placing edges. */
482 FOR_EACH_BB (bb)
484 tree first = first_stmt (bb);
485 tree last = last_stmt (bb);
487 if (first)
489 /* Edges for statements that always alter flow control. */
490 if (is_ctrl_stmt (last))
491 make_ctrl_stmt_edges (bb);
493 /* Edges for statements that sometimes alter flow control. */
494 if (is_ctrl_altering_stmt (last))
495 make_exit_edges (bb);
498 /* Finally, if no edges were created above, this is a regular
499 basic block that only needs a fallthru edge. */
500 if (EDGE_COUNT (bb->succs) == 0)
501 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
504 /* We do not care about fake edges, so remove any that the CFG
505 builder inserted for completeness. */
506 remove_fake_exit_edges ();
508 /* Fold COND_EXPR_COND of each COND_EXPR. */
509 fold_cond_expr_cond ();
511 /* Clean up the graph and warn for unreachable code. */
512 cleanup_tree_cfg ();
516 /* Create edges for control statement at basic block BB. */
518 static void
519 make_ctrl_stmt_edges (basic_block bb)
521 tree last = last_stmt (bb);
523 gcc_assert (last);
524 switch (TREE_CODE (last))
526 case GOTO_EXPR:
527 make_goto_expr_edges (bb);
528 break;
530 case RETURN_EXPR:
531 make_edge (bb, EXIT_BLOCK_PTR, 0);
532 break;
534 case COND_EXPR:
535 make_cond_expr_edges (bb);
536 break;
538 case SWITCH_EXPR:
539 make_switch_expr_edges (bb);
540 break;
542 case RESX_EXPR:
543 make_eh_edges (last);
544 /* Yet another NORETURN hack. */
545 if (EDGE_COUNT (bb->succs) == 0)
546 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
547 break;
549 default:
550 gcc_unreachable ();
555 /* Create exit edges for statements in block BB that alter the flow of
556 control. Statements that alter the control flow are 'goto', 'return'
557 and calls to non-returning functions. */
559 static void
560 make_exit_edges (basic_block bb)
562 tree last = last_stmt (bb), op;
564 gcc_assert (last);
565 switch (TREE_CODE (last))
567 case RESX_EXPR:
568 break;
569 case CALL_EXPR:
570 /* If this function receives a nonlocal goto, then we need to
571 make edges from this call site to all the nonlocal goto
572 handlers. */
573 if (TREE_SIDE_EFFECTS (last)
574 && current_function_has_nonlocal_label)
575 make_goto_expr_edges (bb);
577 /* If this statement has reachable exception handlers, then
578 create abnormal edges to them. */
579 make_eh_edges (last);
581 /* Some calls are known not to return. For such calls we create
582 a fake edge.
584 We really need to revamp how we build edges so that it's not
585 such a bloody pain to avoid creating edges for this case since
586 all we do is remove these edges when we're done building the
587 CFG. */
588 if (call_expr_flags (last) & ECF_NORETURN)
590 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
591 return;
594 /* Don't forget the fall-thru edge. */
595 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
596 break;
598 case MODIFY_EXPR:
599 /* A MODIFY_EXPR may have a CALL_EXPR on its RHS and the CALL_EXPR
600 may have an abnormal edge. Search the RHS for this case and
601 create any required edges. */
602 op = get_call_expr_in (last);
603 if (op && TREE_SIDE_EFFECTS (op)
604 && current_function_has_nonlocal_label)
605 make_goto_expr_edges (bb);
607 make_eh_edges (last);
608 make_edge (bb, bb->next_bb, EDGE_FALLTHRU);
609 break;
611 default:
612 gcc_unreachable ();
617 /* Create the edges for a COND_EXPR starting at block BB.
618 At this point, both clauses must contain only simple gotos. */
620 static void
621 make_cond_expr_edges (basic_block bb)
623 tree entry = last_stmt (bb);
624 basic_block then_bb, else_bb;
625 tree then_label, else_label;
627 gcc_assert (entry);
628 gcc_assert (TREE_CODE (entry) == COND_EXPR);
630 /* Entry basic blocks for each component. */
631 then_label = GOTO_DESTINATION (COND_EXPR_THEN (entry));
632 else_label = GOTO_DESTINATION (COND_EXPR_ELSE (entry));
633 then_bb = label_to_block (then_label);
634 else_bb = label_to_block (else_label);
636 make_edge (bb, then_bb, EDGE_TRUE_VALUE);
637 make_edge (bb, else_bb, EDGE_FALSE_VALUE);
640 /* Hashing routine for EDGE_TO_CASES. */
642 static hashval_t
643 edge_to_cases_hash (const void *p)
645 edge e = ((struct edge_to_cases_elt *)p)->e;
647 /* Hash on the edge itself (which is a pointer). */
648 return htab_hash_pointer (e);
651 /* Equality routine for EDGE_TO_CASES, edges are unique, so testing
652 for equality is just a pointer comparison. */
654 static int
655 edge_to_cases_eq (const void *p1, const void *p2)
657 edge e1 = ((struct edge_to_cases_elt *)p1)->e;
658 edge e2 = ((struct edge_to_cases_elt *)p2)->e;
660 return e1 == e2;
663 /* Called for each element in the hash table (P) as we delete the
664 edge to cases hash table.
666 Clear all the TREE_CHAINs to prevent problems with copying of
667 SWITCH_EXPRs and structure sharing rules, then free the hash table
668 element. */
670 static void
671 edge_to_cases_cleanup (void *p)
673 struct edge_to_cases_elt *elt = p;
674 tree t, next;
676 for (t = elt->case_labels; t; t = next)
678 next = TREE_CHAIN (t);
679 TREE_CHAIN (t) = NULL;
681 free (p);
684 /* Start recording information mapping edges to case labels. */
686 static void
687 start_recording_case_labels (void)
689 gcc_assert (edge_to_cases == NULL);
691 edge_to_cases = htab_create (37,
692 edge_to_cases_hash,
693 edge_to_cases_eq,
694 edge_to_cases_cleanup);
697 /* Return nonzero if we are recording information for case labels. */
699 static bool
700 recording_case_labels_p (void)
702 return (edge_to_cases != NULL);
705 /* Stop recording information mapping edges to case labels and
706 remove any information we have recorded. */
707 static void
708 end_recording_case_labels (void)
710 htab_delete (edge_to_cases);
711 edge_to_cases = NULL;
714 /* Record that CASE_LABEL (a CASE_LABEL_EXPR) references edge E. */
716 static void
717 record_switch_edge (edge e, tree case_label)
719 struct edge_to_cases_elt *elt;
720 void **slot;
722 /* Build a hash table element so we can see if E is already
723 in the table. */
724 elt = xmalloc (sizeof (struct edge_to_cases_elt));
725 elt->e = e;
726 elt->case_labels = case_label;
728 slot = htab_find_slot (edge_to_cases, elt, INSERT);
730 if (*slot == NULL)
732 /* E was not in the hash table. Install E into the hash table. */
733 *slot = (void *)elt;
735 else
737 /* E was already in the hash table. Free ELT as we do not need it
738 anymore. */
739 free (elt);
741 /* Get the entry stored in the hash table. */
742 elt = (struct edge_to_cases_elt *) *slot;
744 /* Add it to the chain of CASE_LABEL_EXPRs referencing E. */
745 TREE_CHAIN (case_label) = elt->case_labels;
746 elt->case_labels = case_label;
750 /* If we are inside a {start,end}_recording_cases block, then return
751 a chain of CASE_LABEL_EXPRs from T which reference E.
753 Otherwise return NULL. */
755 static tree
756 get_cases_for_edge (edge e, tree t)
758 struct edge_to_cases_elt elt, *elt_p;
759 void **slot;
760 size_t i, n;
761 tree vec;
763 /* If we are not recording cases, then we do not have CASE_LABEL_EXPR
764 chains available. Return NULL so the caller can detect this case. */
765 if (!recording_case_labels_p ())
766 return NULL;
768 restart:
769 elt.e = e;
770 elt.case_labels = NULL;
771 slot = htab_find_slot (edge_to_cases, &elt, NO_INSERT);
773 if (slot)
775 elt_p = (struct edge_to_cases_elt *)*slot;
776 return elt_p->case_labels;
779 /* If we did not find E in the hash table, then this must be the first
780 time we have been queried for information about E & T. Add all the
781 elements from T to the hash table then perform the query again. */
783 vec = SWITCH_LABELS (t);
784 n = TREE_VEC_LENGTH (vec);
785 for (i = 0; i < n; i++)
787 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
788 basic_block label_bb = label_to_block (lab);
789 record_switch_edge (find_edge (e->src, label_bb), TREE_VEC_ELT (vec, i));
791 goto restart;
794 /* Create the edges for a SWITCH_EXPR starting at block BB.
795 At this point, the switch body has been lowered and the
796 SWITCH_LABELS filled in, so this is in effect a multi-way branch. */
798 static void
799 make_switch_expr_edges (basic_block bb)
801 tree entry = last_stmt (bb);
802 size_t i, n;
803 tree vec;
805 vec = SWITCH_LABELS (entry);
806 n = TREE_VEC_LENGTH (vec);
808 for (i = 0; i < n; ++i)
810 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
811 basic_block label_bb = label_to_block (lab);
812 make_edge (bb, label_bb, 0);
817 /* Return the basic block holding label DEST. */
819 basic_block
820 label_to_block_fn (struct function *ifun, tree dest)
822 int uid = LABEL_DECL_UID (dest);
824 /* We would die hard when faced by an undefined label. Emit a label to
825 the very first basic block. This will hopefully make even the dataflow
826 and undefined variable warnings quite right. */
827 if ((errorcount || sorrycount) && uid < 0)
829 block_stmt_iterator bsi = bsi_start (BASIC_BLOCK (0));
830 tree stmt;
832 stmt = build1 (LABEL_EXPR, void_type_node, dest);
833 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
834 uid = LABEL_DECL_UID (dest);
836 if (VARRAY_SIZE (ifun->cfg->x_label_to_block_map) <= (unsigned int)uid)
837 return NULL;
838 return VARRAY_BB (ifun->cfg->x_label_to_block_map, uid);
841 /* Create edges for a goto statement at block BB. */
843 static void
844 make_goto_expr_edges (basic_block bb)
846 tree goto_t;
847 basic_block target_bb;
848 int for_call;
849 block_stmt_iterator last = bsi_last (bb);
851 goto_t = bsi_stmt (last);
853 /* If the last statement is not a GOTO (i.e., it is a RETURN_EXPR,
854 CALL_EXPR or MODIFY_EXPR), then the edge is an abnormal edge resulting
855 from a nonlocal goto. */
856 if (TREE_CODE (goto_t) != GOTO_EXPR)
857 for_call = 1;
858 else
860 tree dest = GOTO_DESTINATION (goto_t);
861 for_call = 0;
863 /* A GOTO to a local label creates normal edges. */
864 if (simple_goto_p (goto_t))
866 edge e = make_edge (bb, label_to_block (dest), EDGE_FALLTHRU);
867 #ifdef USE_MAPPED_LOCATION
868 e->goto_locus = EXPR_LOCATION (goto_t);
869 #else
870 e->goto_locus = EXPR_LOCUS (goto_t);
871 #endif
872 bsi_remove (&last);
873 return;
876 /* Nothing more to do for nonlocal gotos. */
877 if (TREE_CODE (dest) == LABEL_DECL)
878 return;
880 /* Computed gotos remain. */
883 /* Look for the block starting with the destination label. In the
884 case of a computed goto, make an edge to any label block we find
885 in the CFG. */
886 FOR_EACH_BB (target_bb)
888 block_stmt_iterator bsi;
890 for (bsi = bsi_start (target_bb); !bsi_end_p (bsi); bsi_next (&bsi))
892 tree target = bsi_stmt (bsi);
894 if (TREE_CODE (target) != LABEL_EXPR)
895 break;
897 if (
898 /* Computed GOTOs. Make an edge to every label block that has
899 been marked as a potential target for a computed goto. */
900 (FORCED_LABEL (LABEL_EXPR_LABEL (target)) && for_call == 0)
901 /* Nonlocal GOTO target. Make an edge to every label block
902 that has been marked as a potential target for a nonlocal
903 goto. */
904 || (DECL_NONLOCAL (LABEL_EXPR_LABEL (target)) && for_call == 1))
906 make_edge (bb, target_bb, EDGE_ABNORMAL);
907 break;
912 /* Degenerate case of computed goto with no labels. */
913 if (!for_call && EDGE_COUNT (bb->succs) == 0)
914 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
918 /*---------------------------------------------------------------------------
919 Flowgraph analysis
920 ---------------------------------------------------------------------------*/
922 /* Remove unreachable blocks and other miscellaneous clean up work. */
924 bool
925 cleanup_tree_cfg (void)
927 bool retval = false;
929 timevar_push (TV_TREE_CLEANUP_CFG);
931 retval = cleanup_control_flow ();
932 retval |= delete_unreachable_blocks ();
934 /* cleanup_forwarder_blocks can redirect edges out of SWITCH_EXPRs,
935 which can get expensive. So we want to enable recording of edge
936 to CASE_LABEL_EXPR mappings around the call to
937 cleanup_forwarder_blocks. */
938 start_recording_case_labels ();
939 retval |= cleanup_forwarder_blocks ();
940 end_recording_case_labels ();
942 #ifdef ENABLE_CHECKING
943 if (retval)
945 gcc_assert (!cleanup_control_flow ());
946 gcc_assert (!delete_unreachable_blocks ());
947 gcc_assert (!cleanup_forwarder_blocks ());
949 #endif
951 /* Merging the blocks creates no new opportunities for the other
952 optimizations, so do it here. */
953 retval |= merge_seq_blocks ();
955 compact_blocks ();
957 #ifdef ENABLE_CHECKING
958 verify_flow_info ();
959 #endif
960 timevar_pop (TV_TREE_CLEANUP_CFG);
961 return retval;
965 /* Cleanup cfg and repair loop structures. */
967 void
968 cleanup_tree_cfg_loop (void)
970 bitmap changed_bbs = BITMAP_ALLOC (NULL);
972 cleanup_tree_cfg ();
974 fix_loop_structure (current_loops, changed_bbs);
975 calculate_dominance_info (CDI_DOMINATORS);
977 /* This usually does nothing. But sometimes parts of cfg that originally
978 were inside a loop get out of it due to edge removal (since they
979 become unreachable by back edges from latch). */
980 rewrite_into_loop_closed_ssa (changed_bbs, TODO_update_ssa);
982 BITMAP_FREE (changed_bbs);
984 #ifdef ENABLE_CHECKING
985 verify_loop_structure (current_loops);
986 #endif
989 /* Cleanup useless labels in basic blocks. This is something we wish
990 to do early because it allows us to group case labels before creating
991 the edges for the CFG, and it speeds up block statement iterators in
992 all passes later on.
993 We only run this pass once, running it more than once is probably not
994 profitable. */
996 /* A map from basic block index to the leading label of that block. */
997 static tree *label_for_bb;
999 /* Callback for for_each_eh_region. Helper for cleanup_dead_labels. */
1000 static void
1001 update_eh_label (struct eh_region *region)
1003 tree old_label = get_eh_region_tree_label (region);
1004 if (old_label)
1006 tree new_label;
1007 basic_block bb = label_to_block (old_label);
1009 /* ??? After optimizing, there may be EH regions with labels
1010 that have already been removed from the function body, so
1011 there is no basic block for them. */
1012 if (! bb)
1013 return;
1015 new_label = label_for_bb[bb->index];
1016 set_eh_region_tree_label (region, new_label);
1020 /* Given LABEL return the first label in the same basic block. */
1021 static tree
1022 main_block_label (tree label)
1024 basic_block bb = label_to_block (label);
1026 /* label_to_block possibly inserted undefined label into the chain. */
1027 if (!label_for_bb[bb->index])
1028 label_for_bb[bb->index] = label;
1029 return label_for_bb[bb->index];
1032 /* Cleanup redundant labels. This is a three-step process:
1033 1) Find the leading label for each block.
1034 2) Redirect all references to labels to the leading labels.
1035 3) Cleanup all useless labels. */
1037 void
1038 cleanup_dead_labels (void)
1040 basic_block bb;
1041 label_for_bb = xcalloc (last_basic_block, sizeof (tree));
1043 /* Find a suitable label for each block. We use the first user-defined
1044 label if there is one, or otherwise just the first label we see. */
1045 FOR_EACH_BB (bb)
1047 block_stmt_iterator i;
1049 for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
1051 tree label, stmt = bsi_stmt (i);
1053 if (TREE_CODE (stmt) != LABEL_EXPR)
1054 break;
1056 label = LABEL_EXPR_LABEL (stmt);
1058 /* If we have not yet seen a label for the current block,
1059 remember this one and see if there are more labels. */
1060 if (! label_for_bb[bb->index])
1062 label_for_bb[bb->index] = label;
1063 continue;
1066 /* If we did see a label for the current block already, but it
1067 is an artificially created label, replace it if the current
1068 label is a user defined label. */
1069 if (! DECL_ARTIFICIAL (label)
1070 && DECL_ARTIFICIAL (label_for_bb[bb->index]))
1072 label_for_bb[bb->index] = label;
1073 break;
1078 /* Now redirect all jumps/branches to the selected label.
1079 First do so for each block ending in a control statement. */
1080 FOR_EACH_BB (bb)
1082 tree stmt = last_stmt (bb);
1083 if (!stmt)
1084 continue;
1086 switch (TREE_CODE (stmt))
1088 case COND_EXPR:
1090 tree true_branch, false_branch;
1092 true_branch = COND_EXPR_THEN (stmt);
1093 false_branch = COND_EXPR_ELSE (stmt);
1095 GOTO_DESTINATION (true_branch)
1096 = main_block_label (GOTO_DESTINATION (true_branch));
1097 GOTO_DESTINATION (false_branch)
1098 = main_block_label (GOTO_DESTINATION (false_branch));
1100 break;
1103 case SWITCH_EXPR:
1105 size_t i;
1106 tree vec = SWITCH_LABELS (stmt);
1107 size_t n = TREE_VEC_LENGTH (vec);
1109 /* Replace all destination labels. */
1110 for (i = 0; i < n; ++i)
1112 tree elt = TREE_VEC_ELT (vec, i);
1113 tree label = main_block_label (CASE_LABEL (elt));
1114 CASE_LABEL (elt) = label;
1116 break;
1119 /* We have to handle GOTO_EXPRs until they're removed, and we don't
1120 remove them until after we've created the CFG edges. */
1121 case GOTO_EXPR:
1122 if (! computed_goto_p (stmt))
1124 GOTO_DESTINATION (stmt)
1125 = main_block_label (GOTO_DESTINATION (stmt));
1126 break;
1129 default:
1130 break;
1134 for_each_eh_region (update_eh_label);
1136 /* Finally, purge dead labels. All user-defined labels and labels that
1137 can be the target of non-local gotos are preserved. */
1138 FOR_EACH_BB (bb)
1140 block_stmt_iterator i;
1141 tree label_for_this_bb = label_for_bb[bb->index];
1143 if (! label_for_this_bb)
1144 continue;
1146 for (i = bsi_start (bb); !bsi_end_p (i); )
1148 tree label, stmt = bsi_stmt (i);
1150 if (TREE_CODE (stmt) != LABEL_EXPR)
1151 break;
1153 label = LABEL_EXPR_LABEL (stmt);
1155 if (label == label_for_this_bb
1156 || ! DECL_ARTIFICIAL (label)
1157 || DECL_NONLOCAL (label))
1158 bsi_next (&i);
1159 else
1160 bsi_remove (&i);
1164 free (label_for_bb);
1167 /* Look for blocks ending in a multiway branch (a SWITCH_EXPR in GIMPLE),
1168 and scan the sorted vector of cases. Combine the ones jumping to the
1169 same label.
1170 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
1172 void
1173 group_case_labels (void)
1175 basic_block bb;
1177 FOR_EACH_BB (bb)
1179 tree stmt = last_stmt (bb);
1180 if (stmt && TREE_CODE (stmt) == SWITCH_EXPR)
1182 tree labels = SWITCH_LABELS (stmt);
1183 int old_size = TREE_VEC_LENGTH (labels);
1184 int i, j, new_size = old_size;
1185 tree default_case = TREE_VEC_ELT (labels, old_size - 1);
1186 tree default_label;
1188 /* The default label is always the last case in a switch
1189 statement after gimplification. */
1190 default_label = CASE_LABEL (default_case);
1192 /* Look for possible opportunities to merge cases.
1193 Ignore the last element of the label vector because it
1194 must be the default case. */
1195 i = 0;
1196 while (i < old_size - 1)
1198 tree base_case, base_label, base_high;
1199 base_case = TREE_VEC_ELT (labels, i);
1201 gcc_assert (base_case);
1202 base_label = CASE_LABEL (base_case);
1204 /* Discard cases that have the same destination as the
1205 default case. */
1206 if (base_label == default_label)
1208 TREE_VEC_ELT (labels, i) = NULL_TREE;
1209 i++;
1210 new_size--;
1211 continue;
1214 base_high = CASE_HIGH (base_case) ?
1215 CASE_HIGH (base_case) : CASE_LOW (base_case);
1216 i++;
1217 /* Try to merge case labels. Break out when we reach the end
1218 of the label vector or when we cannot merge the next case
1219 label with the current one. */
1220 while (i < old_size - 1)
1222 tree merge_case = TREE_VEC_ELT (labels, i);
1223 tree merge_label = CASE_LABEL (merge_case);
1224 tree t = int_const_binop (PLUS_EXPR, base_high,
1225 integer_one_node, 1);
1227 /* Merge the cases if they jump to the same place,
1228 and their ranges are consecutive. */
1229 if (merge_label == base_label
1230 && tree_int_cst_equal (CASE_LOW (merge_case), t))
1232 base_high = CASE_HIGH (merge_case) ?
1233 CASE_HIGH (merge_case) : CASE_LOW (merge_case);
1234 CASE_HIGH (base_case) = base_high;
1235 TREE_VEC_ELT (labels, i) = NULL_TREE;
1236 new_size--;
1237 i++;
1239 else
1240 break;
1244 /* Compress the case labels in the label vector, and adjust the
1245 length of the vector. */
1246 for (i = 0, j = 0; i < new_size; i++)
1248 while (! TREE_VEC_ELT (labels, j))
1249 j++;
1250 TREE_VEC_ELT (labels, i) = TREE_VEC_ELT (labels, j++);
1252 TREE_VEC_LENGTH (labels) = new_size;
1257 /* Checks whether we can merge block B into block A. */
1259 static bool
1260 tree_can_merge_blocks_p (basic_block a, basic_block b)
1262 tree stmt;
1263 block_stmt_iterator bsi;
1264 tree phi;
1266 if (!single_succ_p (a))
1267 return false;
1269 if (single_succ_edge (a)->flags & EDGE_ABNORMAL)
1270 return false;
1272 if (single_succ (a) != b)
1273 return false;
1275 if (!single_pred_p (b))
1276 return false;
1278 if (b == EXIT_BLOCK_PTR)
1279 return false;
1281 /* If A ends by a statement causing exceptions or something similar, we
1282 cannot merge the blocks. */
1283 stmt = last_stmt (a);
1284 if (stmt && stmt_ends_bb_p (stmt))
1285 return false;
1287 /* Do not allow a block with only a non-local label to be merged. */
1288 if (stmt && TREE_CODE (stmt) == LABEL_EXPR
1289 && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
1290 return false;
1292 /* It must be possible to eliminate all phi nodes in B. If ssa form
1293 is not up-to-date, we cannot eliminate any phis. */
1294 phi = phi_nodes (b);
1295 if (phi)
1297 if (need_ssa_update_p ())
1298 return false;
1300 for (; phi; phi = PHI_CHAIN (phi))
1301 if (!is_gimple_reg (PHI_RESULT (phi))
1302 && !may_propagate_copy (PHI_RESULT (phi), PHI_ARG_DEF (phi, 0)))
1303 return false;
1306 /* Do not remove user labels. */
1307 for (bsi = bsi_start (b); !bsi_end_p (bsi); bsi_next (&bsi))
1309 stmt = bsi_stmt (bsi);
1310 if (TREE_CODE (stmt) != LABEL_EXPR)
1311 break;
1312 if (!DECL_ARTIFICIAL (LABEL_EXPR_LABEL (stmt)))
1313 return false;
1316 /* Protect the loop latches. */
1317 if (current_loops
1318 && b->loop_father->latch == b)
1319 return false;
1321 return true;
1324 /* Replaces all uses of NAME by VAL. */
1326 void
1327 replace_uses_by (tree name, tree val)
1329 imm_use_iterator imm_iter;
1330 use_operand_p use;
1331 tree stmt;
1332 edge e;
1333 unsigned i;
1334 VEC(tree,heap) *stmts = VEC_alloc (tree, heap, 20);
1336 FOR_EACH_IMM_USE_SAFE (use, imm_iter, name)
1338 stmt = USE_STMT (use);
1340 SET_USE (use, val);
1342 if (TREE_CODE (stmt) == PHI_NODE)
1344 e = PHI_ARG_EDGE (stmt, PHI_ARG_INDEX_FROM_USE (use));
1345 if (e->flags & EDGE_ABNORMAL)
1347 /* This can only occur for virtual operands, since
1348 for the real ones SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1349 would prevent replacement. */
1350 gcc_assert (!is_gimple_reg (name));
1351 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (val) = 1;
1354 else
1355 VEC_safe_push (tree, heap, stmts, stmt);
1358 /* We do not update the statements in the loop above. Consider
1359 x = w * w;
1361 If we performed the update in the first loop, the statement
1362 would be rescanned after first occurrence of w is replaced,
1363 the new uses would be placed to the beginning of the list,
1364 and we would never process them. */
1365 for (i = 0; VEC_iterate (tree, stmts, i, stmt); i++)
1367 fold_stmt_inplace (stmt);
1368 update_stmt (stmt);
1371 VEC_free (tree, heap, stmts);
1374 /* Merge block B into block A. */
1376 static void
1377 tree_merge_blocks (basic_block a, basic_block b)
1379 block_stmt_iterator bsi;
1380 tree_stmt_iterator last;
1381 tree phi;
1383 if (dump_file)
1384 fprintf (dump_file, "Merging blocks %d and %d\n", a->index, b->index);
1386 /* Remove the phi nodes. */
1387 bsi = bsi_last (a);
1388 for (phi = phi_nodes (b); phi; phi = phi_nodes (b))
1390 tree def = PHI_RESULT (phi), use = PHI_ARG_DEF (phi, 0);
1391 tree copy;
1393 if (!may_propagate_copy (def, use)
1394 /* Propagating pointers might cause the set of vops for statements
1395 to be changed, and thus require ssa form update. */
1396 || (is_gimple_reg (def)
1397 && POINTER_TYPE_P (TREE_TYPE (def))))
1399 gcc_assert (is_gimple_reg (def));
1401 /* Note that just emitting the copies is fine -- there is no problem
1402 with ordering of phi nodes. This is because A is the single
1403 predecessor of B, therefore results of the phi nodes cannot
1404 appear as arguments of the phi nodes. */
1405 copy = build2 (MODIFY_EXPR, void_type_node, def, use);
1406 bsi_insert_after (&bsi, copy, BSI_NEW_STMT);
1407 SET_PHI_RESULT (phi, NULL_TREE);
1408 SSA_NAME_DEF_STMT (def) = copy;
1410 else
1411 replace_uses_by (def, use);
1412 remove_phi_node (phi, NULL);
1415 /* Ensure that B follows A. */
1416 move_block_after (b, a);
1418 gcc_assert (single_succ_edge (a)->flags & EDGE_FALLTHRU);
1419 gcc_assert (!last_stmt (a) || !stmt_ends_bb_p (last_stmt (a)));
1421 /* Remove labels from B and set bb_for_stmt to A for other statements. */
1422 for (bsi = bsi_start (b); !bsi_end_p (bsi);)
1424 if (TREE_CODE (bsi_stmt (bsi)) == LABEL_EXPR)
1426 tree label = bsi_stmt (bsi);
1428 bsi_remove (&bsi);
1429 /* Now that we can thread computed gotos, we might have
1430 a situation where we have a forced label in block B
1431 However, the label at the start of block B might still be
1432 used in other ways (think about the runtime checking for
1433 Fortran assigned gotos). So we can not just delete the
1434 label. Instead we move the label to the start of block A. */
1435 if (FORCED_LABEL (LABEL_EXPR_LABEL (label)))
1437 block_stmt_iterator dest_bsi = bsi_start (a);
1438 bsi_insert_before (&dest_bsi, label, BSI_NEW_STMT);
1441 else
1443 set_bb_for_stmt (bsi_stmt (bsi), a);
1444 bsi_next (&bsi);
1448 /* Merge the chains. */
1449 last = tsi_last (a->stmt_list);
1450 tsi_link_after (&last, b->stmt_list, TSI_NEW_STMT);
1451 b->stmt_list = NULL;
1455 /* Walk the function tree removing unnecessary statements.
1457 * Empty statement nodes are removed
1459 * Unnecessary TRY_FINALLY and TRY_CATCH blocks are removed
1461 * Unnecessary COND_EXPRs are removed
1463 * Some unnecessary BIND_EXPRs are removed
1465 Clearly more work could be done. The trick is doing the analysis
1466 and removal fast enough to be a net improvement in compile times.
1468 Note that when we remove a control structure such as a COND_EXPR
1469 BIND_EXPR, or TRY block, we will need to repeat this optimization pass
1470 to ensure we eliminate all the useless code. */
1472 struct rus_data
1474 tree *last_goto;
1475 bool repeat;
1476 bool may_throw;
1477 bool may_branch;
1478 bool has_label;
1481 static void remove_useless_stmts_1 (tree *, struct rus_data *);
1483 static bool
1484 remove_useless_stmts_warn_notreached (tree stmt)
1486 if (EXPR_HAS_LOCATION (stmt))
1488 location_t loc = EXPR_LOCATION (stmt);
1489 if (LOCATION_LINE (loc) > 0)
1491 warning (0, "%Hwill never be executed", &loc);
1492 return true;
1496 switch (TREE_CODE (stmt))
1498 case STATEMENT_LIST:
1500 tree_stmt_iterator i;
1501 for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i))
1502 if (remove_useless_stmts_warn_notreached (tsi_stmt (i)))
1503 return true;
1505 break;
1507 case COND_EXPR:
1508 if (remove_useless_stmts_warn_notreached (COND_EXPR_COND (stmt)))
1509 return true;
1510 if (remove_useless_stmts_warn_notreached (COND_EXPR_THEN (stmt)))
1511 return true;
1512 if (remove_useless_stmts_warn_notreached (COND_EXPR_ELSE (stmt)))
1513 return true;
1514 break;
1516 case TRY_FINALLY_EXPR:
1517 case TRY_CATCH_EXPR:
1518 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 0)))
1519 return true;
1520 if (remove_useless_stmts_warn_notreached (TREE_OPERAND (stmt, 1)))
1521 return true;
1522 break;
1524 case CATCH_EXPR:
1525 return remove_useless_stmts_warn_notreached (CATCH_BODY (stmt));
1526 case EH_FILTER_EXPR:
1527 return remove_useless_stmts_warn_notreached (EH_FILTER_FAILURE (stmt));
1528 case BIND_EXPR:
1529 return remove_useless_stmts_warn_notreached (BIND_EXPR_BLOCK (stmt));
1531 default:
1532 /* Not a live container. */
1533 break;
1536 return false;
1539 static void
1540 remove_useless_stmts_cond (tree *stmt_p, struct rus_data *data)
1542 tree then_clause, else_clause, cond;
1543 bool save_has_label, then_has_label, else_has_label;
1545 save_has_label = data->has_label;
1546 data->has_label = false;
1547 data->last_goto = NULL;
1549 remove_useless_stmts_1 (&COND_EXPR_THEN (*stmt_p), data);
1551 then_has_label = data->has_label;
1552 data->has_label = false;
1553 data->last_goto = NULL;
1555 remove_useless_stmts_1 (&COND_EXPR_ELSE (*stmt_p), data);
1557 else_has_label = data->has_label;
1558 data->has_label = save_has_label | then_has_label | else_has_label;
1560 then_clause = COND_EXPR_THEN (*stmt_p);
1561 else_clause = COND_EXPR_ELSE (*stmt_p);
1562 cond = fold (COND_EXPR_COND (*stmt_p));
1564 /* If neither arm does anything at all, we can remove the whole IF. */
1565 if (!TREE_SIDE_EFFECTS (then_clause) && !TREE_SIDE_EFFECTS (else_clause))
1567 *stmt_p = build_empty_stmt ();
1568 data->repeat = true;
1571 /* If there are no reachable statements in an arm, then we can
1572 zap the entire conditional. */
1573 else if (integer_nonzerop (cond) && !else_has_label)
1575 if (warn_notreached)
1576 remove_useless_stmts_warn_notreached (else_clause);
1577 *stmt_p = then_clause;
1578 data->repeat = true;
1580 else if (integer_zerop (cond) && !then_has_label)
1582 if (warn_notreached)
1583 remove_useless_stmts_warn_notreached (then_clause);
1584 *stmt_p = else_clause;
1585 data->repeat = true;
1588 /* Check a couple of simple things on then/else with single stmts. */
1589 else
1591 tree then_stmt = expr_only (then_clause);
1592 tree else_stmt = expr_only (else_clause);
1594 /* Notice branches to a common destination. */
1595 if (then_stmt && else_stmt
1596 && TREE_CODE (then_stmt) == GOTO_EXPR
1597 && TREE_CODE (else_stmt) == GOTO_EXPR
1598 && (GOTO_DESTINATION (then_stmt) == GOTO_DESTINATION (else_stmt)))
1600 *stmt_p = then_stmt;
1601 data->repeat = true;
1604 /* If the THEN/ELSE clause merely assigns a value to a variable or
1605 parameter which is already known to contain that value, then
1606 remove the useless THEN/ELSE clause. */
1607 else if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
1609 if (else_stmt
1610 && TREE_CODE (else_stmt) == MODIFY_EXPR
1611 && TREE_OPERAND (else_stmt, 0) == cond
1612 && integer_zerop (TREE_OPERAND (else_stmt, 1)))
1613 COND_EXPR_ELSE (*stmt_p) = alloc_stmt_list ();
1615 else if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
1616 && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
1617 || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
1618 && TREE_CONSTANT (TREE_OPERAND (cond, 1)))
1620 tree stmt = (TREE_CODE (cond) == EQ_EXPR
1621 ? then_stmt : else_stmt);
1622 tree *location = (TREE_CODE (cond) == EQ_EXPR
1623 ? &COND_EXPR_THEN (*stmt_p)
1624 : &COND_EXPR_ELSE (*stmt_p));
1626 if (stmt
1627 && TREE_CODE (stmt) == MODIFY_EXPR
1628 && TREE_OPERAND (stmt, 0) == TREE_OPERAND (cond, 0)
1629 && TREE_OPERAND (stmt, 1) == TREE_OPERAND (cond, 1))
1630 *location = alloc_stmt_list ();
1634 /* Protect GOTOs in the arm of COND_EXPRs from being removed. They
1635 would be re-introduced during lowering. */
1636 data->last_goto = NULL;
1640 static void
1641 remove_useless_stmts_tf (tree *stmt_p, struct rus_data *data)
1643 bool save_may_branch, save_may_throw;
1644 bool this_may_branch, this_may_throw;
1646 /* Collect may_branch and may_throw information for the body only. */
1647 save_may_branch = data->may_branch;
1648 save_may_throw = data->may_throw;
1649 data->may_branch = false;
1650 data->may_throw = false;
1651 data->last_goto = NULL;
1653 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1655 this_may_branch = data->may_branch;
1656 this_may_throw = data->may_throw;
1657 data->may_branch |= save_may_branch;
1658 data->may_throw |= save_may_throw;
1659 data->last_goto = NULL;
1661 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1663 /* If the body is empty, then we can emit the FINALLY block without
1664 the enclosing TRY_FINALLY_EXPR. */
1665 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 0)))
1667 *stmt_p = TREE_OPERAND (*stmt_p, 1);
1668 data->repeat = true;
1671 /* If the handler is empty, then we can emit the TRY block without
1672 the enclosing TRY_FINALLY_EXPR. */
1673 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1675 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1676 data->repeat = true;
1679 /* If the body neither throws, nor branches, then we can safely
1680 string the TRY and FINALLY blocks together. */
1681 else if (!this_may_branch && !this_may_throw)
1683 tree stmt = *stmt_p;
1684 *stmt_p = TREE_OPERAND (stmt, 0);
1685 append_to_statement_list (TREE_OPERAND (stmt, 1), stmt_p);
1686 data->repeat = true;
1691 static void
1692 remove_useless_stmts_tc (tree *stmt_p, struct rus_data *data)
1694 bool save_may_throw, this_may_throw;
1695 tree_stmt_iterator i;
1696 tree stmt;
1698 /* Collect may_throw information for the body only. */
1699 save_may_throw = data->may_throw;
1700 data->may_throw = false;
1701 data->last_goto = NULL;
1703 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 0), data);
1705 this_may_throw = data->may_throw;
1706 data->may_throw = save_may_throw;
1708 /* If the body cannot throw, then we can drop the entire TRY_CATCH_EXPR. */
1709 if (!this_may_throw)
1711 if (warn_notreached)
1712 remove_useless_stmts_warn_notreached (TREE_OPERAND (*stmt_p, 1));
1713 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1714 data->repeat = true;
1715 return;
1718 /* Process the catch clause specially. We may be able to tell that
1719 no exceptions propagate past this point. */
1721 this_may_throw = true;
1722 i = tsi_start (TREE_OPERAND (*stmt_p, 1));
1723 stmt = tsi_stmt (i);
1724 data->last_goto = NULL;
1726 switch (TREE_CODE (stmt))
1728 case CATCH_EXPR:
1729 for (; !tsi_end_p (i); tsi_next (&i))
1731 stmt = tsi_stmt (i);
1732 /* If we catch all exceptions, then the body does not
1733 propagate exceptions past this point. */
1734 if (CATCH_TYPES (stmt) == NULL)
1735 this_may_throw = false;
1736 data->last_goto = NULL;
1737 remove_useless_stmts_1 (&CATCH_BODY (stmt), data);
1739 break;
1741 case EH_FILTER_EXPR:
1742 if (EH_FILTER_MUST_NOT_THROW (stmt))
1743 this_may_throw = false;
1744 else if (EH_FILTER_TYPES (stmt) == NULL)
1745 this_may_throw = false;
1746 remove_useless_stmts_1 (&EH_FILTER_FAILURE (stmt), data);
1747 break;
1749 default:
1750 /* Otherwise this is a cleanup. */
1751 remove_useless_stmts_1 (&TREE_OPERAND (*stmt_p, 1), data);
1753 /* If the cleanup is empty, then we can emit the TRY block without
1754 the enclosing TRY_CATCH_EXPR. */
1755 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (*stmt_p, 1)))
1757 *stmt_p = TREE_OPERAND (*stmt_p, 0);
1758 data->repeat = true;
1760 break;
1762 data->may_throw |= this_may_throw;
1766 static void
1767 remove_useless_stmts_bind (tree *stmt_p, struct rus_data *data)
1769 tree block;
1771 /* First remove anything underneath the BIND_EXPR. */
1772 remove_useless_stmts_1 (&BIND_EXPR_BODY (*stmt_p), data);
1774 /* If the BIND_EXPR has no variables, then we can pull everything
1775 up one level and remove the BIND_EXPR, unless this is the toplevel
1776 BIND_EXPR for the current function or an inlined function.
1778 When this situation occurs we will want to apply this
1779 optimization again. */
1780 block = BIND_EXPR_BLOCK (*stmt_p);
1781 if (BIND_EXPR_VARS (*stmt_p) == NULL_TREE
1782 && *stmt_p != DECL_SAVED_TREE (current_function_decl)
1783 && (! block
1784 || ! BLOCK_ABSTRACT_ORIGIN (block)
1785 || (TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block))
1786 != FUNCTION_DECL)))
1788 *stmt_p = BIND_EXPR_BODY (*stmt_p);
1789 data->repeat = true;
1794 static void
1795 remove_useless_stmts_goto (tree *stmt_p, struct rus_data *data)
1797 tree dest = GOTO_DESTINATION (*stmt_p);
1799 data->may_branch = true;
1800 data->last_goto = NULL;
1802 /* Record the last goto expr, so that we can delete it if unnecessary. */
1803 if (TREE_CODE (dest) == LABEL_DECL)
1804 data->last_goto = stmt_p;
1808 static void
1809 remove_useless_stmts_label (tree *stmt_p, struct rus_data *data)
1811 tree label = LABEL_EXPR_LABEL (*stmt_p);
1813 data->has_label = true;
1815 /* We do want to jump across non-local label receiver code. */
1816 if (DECL_NONLOCAL (label))
1817 data->last_goto = NULL;
1819 else if (data->last_goto && GOTO_DESTINATION (*data->last_goto) == label)
1821 *data->last_goto = build_empty_stmt ();
1822 data->repeat = true;
1825 /* ??? Add something here to delete unused labels. */
1829 /* If the function is "const" or "pure", then clear TREE_SIDE_EFFECTS on its
1830 decl. This allows us to eliminate redundant or useless
1831 calls to "const" functions.
1833 Gimplifier already does the same operation, but we may notice functions
1834 being const and pure once their calls has been gimplified, so we need
1835 to update the flag. */
1837 static void
1838 update_call_expr_flags (tree call)
1840 tree decl = get_callee_fndecl (call);
1841 if (!decl)
1842 return;
1843 if (call_expr_flags (call) & (ECF_CONST | ECF_PURE))
1844 TREE_SIDE_EFFECTS (call) = 0;
1845 if (TREE_NOTHROW (decl))
1846 TREE_NOTHROW (call) = 1;
1850 /* T is CALL_EXPR. Set current_function_calls_* flags. */
1852 void
1853 notice_special_calls (tree t)
1855 int flags = call_expr_flags (t);
1857 if (flags & ECF_MAY_BE_ALLOCA)
1858 current_function_calls_alloca = true;
1859 if (flags & ECF_RETURNS_TWICE)
1860 current_function_calls_setjmp = true;
1864 /* Clear flags set by notice_special_calls. Used by dead code removal
1865 to update the flags. */
1867 void
1868 clear_special_calls (void)
1870 current_function_calls_alloca = false;
1871 current_function_calls_setjmp = false;
1875 static void
1876 remove_useless_stmts_1 (tree *tp, struct rus_data *data)
1878 tree t = *tp, op;
1880 switch (TREE_CODE (t))
1882 case COND_EXPR:
1883 remove_useless_stmts_cond (tp, data);
1884 break;
1886 case TRY_FINALLY_EXPR:
1887 remove_useless_stmts_tf (tp, data);
1888 break;
1890 case TRY_CATCH_EXPR:
1891 remove_useless_stmts_tc (tp, data);
1892 break;
1894 case BIND_EXPR:
1895 remove_useless_stmts_bind (tp, data);
1896 break;
1898 case GOTO_EXPR:
1899 remove_useless_stmts_goto (tp, data);
1900 break;
1902 case LABEL_EXPR:
1903 remove_useless_stmts_label (tp, data);
1904 break;
1906 case RETURN_EXPR:
1907 fold_stmt (tp);
1908 data->last_goto = NULL;
1909 data->may_branch = true;
1910 break;
1912 case CALL_EXPR:
1913 fold_stmt (tp);
1914 data->last_goto = NULL;
1915 notice_special_calls (t);
1916 update_call_expr_flags (t);
1917 if (tree_could_throw_p (t))
1918 data->may_throw = true;
1919 break;
1921 case MODIFY_EXPR:
1922 data->last_goto = NULL;
1923 fold_stmt (tp);
1924 op = get_call_expr_in (t);
1925 if (op)
1927 update_call_expr_flags (op);
1928 notice_special_calls (op);
1930 if (tree_could_throw_p (t))
1931 data->may_throw = true;
1932 break;
1934 case STATEMENT_LIST:
1936 tree_stmt_iterator i = tsi_start (t);
1937 while (!tsi_end_p (i))
1939 t = tsi_stmt (i);
1940 if (IS_EMPTY_STMT (t))
1942 tsi_delink (&i);
1943 continue;
1946 remove_useless_stmts_1 (tsi_stmt_ptr (i), data);
1948 t = tsi_stmt (i);
1949 if (TREE_CODE (t) == STATEMENT_LIST)
1951 tsi_link_before (&i, t, TSI_SAME_STMT);
1952 tsi_delink (&i);
1954 else
1955 tsi_next (&i);
1958 break;
1959 case ASM_EXPR:
1960 fold_stmt (tp);
1961 data->last_goto = NULL;
1962 break;
1964 default:
1965 data->last_goto = NULL;
1966 break;
1970 static void
1971 remove_useless_stmts (void)
1973 struct rus_data data;
1975 clear_special_calls ();
1979 memset (&data, 0, sizeof (data));
1980 remove_useless_stmts_1 (&DECL_SAVED_TREE (current_function_decl), &data);
1982 while (data.repeat);
1986 struct tree_opt_pass pass_remove_useless_stmts =
1988 "useless", /* name */
1989 NULL, /* gate */
1990 remove_useless_stmts, /* execute */
1991 NULL, /* sub */
1992 NULL, /* next */
1993 0, /* static_pass_number */
1994 0, /* tv_id */
1995 PROP_gimple_any, /* properties_required */
1996 0, /* properties_provided */
1997 0, /* properties_destroyed */
1998 0, /* todo_flags_start */
1999 TODO_dump_func, /* todo_flags_finish */
2000 0 /* letter */
2003 /* Remove PHI nodes associated with basic block BB and all edges out of BB. */
2005 static void
2006 remove_phi_nodes_and_edges_for_unreachable_block (basic_block bb)
2008 tree phi;
2010 /* Since this block is no longer reachable, we can just delete all
2011 of its PHI nodes. */
2012 phi = phi_nodes (bb);
2013 while (phi)
2015 tree next = PHI_CHAIN (phi);
2016 remove_phi_node (phi, NULL_TREE);
2017 phi = next;
2020 /* Remove edges to BB's successors. */
2021 while (EDGE_COUNT (bb->succs) > 0)
2022 remove_edge (EDGE_SUCC (bb, 0));
2026 /* Remove statements of basic block BB. */
2028 static void
2029 remove_bb (basic_block bb)
2031 block_stmt_iterator i;
2032 #ifdef USE_MAPPED_LOCATION
2033 source_location loc = UNKNOWN_LOCATION;
2034 #else
2035 source_locus loc = 0;
2036 #endif
2038 if (dump_file)
2040 fprintf (dump_file, "Removing basic block %d\n", bb->index);
2041 if (dump_flags & TDF_DETAILS)
2043 dump_bb (bb, dump_file, 0);
2044 fprintf (dump_file, "\n");
2048 /* If we remove the header or the latch of a loop, mark the loop for
2049 removal by setting its header and latch to NULL. */
2050 if (current_loops)
2052 struct loop *loop = bb->loop_father;
2054 if (loop->latch == bb
2055 || loop->header == bb)
2057 loop->latch = NULL;
2058 loop->header = NULL;
2062 /* Remove all the instructions in the block. */
2063 for (i = bsi_start (bb); !bsi_end_p (i);)
2065 tree stmt = bsi_stmt (i);
2066 if (TREE_CODE (stmt) == LABEL_EXPR
2067 && FORCED_LABEL (LABEL_EXPR_LABEL (stmt)))
2069 basic_block new_bb = bb->prev_bb;
2070 block_stmt_iterator new_bsi = bsi_start (new_bb);
2072 bsi_remove (&i);
2073 bsi_insert_before (&new_bsi, stmt, BSI_NEW_STMT);
2075 else
2077 release_defs (stmt);
2079 bsi_remove (&i);
2082 /* Don't warn for removed gotos. Gotos are often removed due to
2083 jump threading, thus resulting in bogus warnings. Not great,
2084 since this way we lose warnings for gotos in the original
2085 program that are indeed unreachable. */
2086 if (TREE_CODE (stmt) != GOTO_EXPR && EXPR_HAS_LOCATION (stmt) && !loc)
2088 #ifdef USE_MAPPED_LOCATION
2089 if (EXPR_HAS_LOCATION (stmt))
2090 loc = EXPR_LOCATION (stmt);
2091 #else
2092 source_locus t;
2093 t = EXPR_LOCUS (stmt);
2094 if (t && LOCATION_LINE (*t) > 0)
2095 loc = t;
2096 #endif
2100 /* If requested, give a warning that the first statement in the
2101 block is unreachable. We walk statements backwards in the
2102 loop above, so the last statement we process is the first statement
2103 in the block. */
2104 #ifdef USE_MAPPED_LOCATION
2105 if (warn_notreached && loc > BUILTINS_LOCATION)
2106 warning (0, "%Hwill never be executed", &loc);
2107 #else
2108 if (warn_notreached && loc)
2109 warning (0, "%Hwill never be executed", loc);
2110 #endif
2112 remove_phi_nodes_and_edges_for_unreachable_block (bb);
2115 /* A list of all the noreturn calls passed to modify_stmt.
2116 cleanup_control_flow uses it to detect cases where a mid-block
2117 indirect call has been turned into a noreturn call. When this
2118 happens, all the instructions after the call are no longer
2119 reachable and must be deleted as dead. */
2121 VEC(tree,gc) *modified_noreturn_calls;
2123 /* Try to remove superfluous control structures. */
2125 static bool
2126 cleanup_control_flow (void)
2128 basic_block bb;
2129 block_stmt_iterator bsi;
2130 bool retval = false;
2131 tree stmt;
2133 /* Detect cases where a mid-block call is now known not to return. */
2134 while (VEC_length (tree, modified_noreturn_calls))
2136 stmt = VEC_pop (tree, modified_noreturn_calls);
2137 bb = bb_for_stmt (stmt);
2138 if (bb != NULL && last_stmt (bb) != stmt && noreturn_call_p (stmt))
2139 split_block (bb, stmt);
2142 FOR_EACH_BB (bb)
2144 bsi = bsi_last (bb);
2146 if (bsi_end_p (bsi))
2147 continue;
2149 stmt = bsi_stmt (bsi);
2150 if (TREE_CODE (stmt) == COND_EXPR
2151 || TREE_CODE (stmt) == SWITCH_EXPR)
2152 retval |= cleanup_control_expr_graph (bb, bsi);
2154 /* If we had a computed goto which has a compile-time determinable
2155 destination, then we can eliminate the goto. */
2156 if (TREE_CODE (stmt) == GOTO_EXPR
2157 && TREE_CODE (GOTO_DESTINATION (stmt)) == ADDR_EXPR
2158 && TREE_CODE (TREE_OPERAND (GOTO_DESTINATION (stmt), 0)) == LABEL_DECL)
2160 edge e;
2161 tree label;
2162 edge_iterator ei;
2163 basic_block target_block;
2164 bool removed_edge = false;
2166 /* First look at all the outgoing edges. Delete any outgoing
2167 edges which do not go to the right block. For the one
2168 edge which goes to the right block, fix up its flags. */
2169 label = TREE_OPERAND (GOTO_DESTINATION (stmt), 0);
2170 target_block = label_to_block (label);
2171 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2173 if (e->dest != target_block)
2175 removed_edge = true;
2176 remove_edge (e);
2178 else
2180 /* Turn off the EDGE_ABNORMAL flag. */
2181 e->flags &= ~EDGE_ABNORMAL;
2183 /* And set EDGE_FALLTHRU. */
2184 e->flags |= EDGE_FALLTHRU;
2185 ei_next (&ei);
2189 /* If we removed one or more edges, then we will need to fix the
2190 dominators. It may be possible to incrementally update them. */
2191 if (removed_edge)
2192 free_dominance_info (CDI_DOMINATORS);
2194 /* Remove the GOTO_EXPR as it is not needed. The CFG has all the
2195 relevant information we need. */
2196 bsi_remove (&bsi);
2197 retval = true;
2200 /* Check for indirect calls that have been turned into
2201 noreturn calls. */
2202 if (noreturn_call_p (stmt) && remove_fallthru_edge (bb->succs))
2204 free_dominance_info (CDI_DOMINATORS);
2205 retval = true;
2208 return retval;
2212 /* Disconnect an unreachable block in the control expression starting
2213 at block BB. */
2215 static bool
2216 cleanup_control_expr_graph (basic_block bb, block_stmt_iterator bsi)
2218 edge taken_edge;
2219 bool retval = false;
2220 tree expr = bsi_stmt (bsi), val;
2222 if (!single_succ_p (bb))
2224 edge e;
2225 edge_iterator ei;
2227 switch (TREE_CODE (expr))
2229 case COND_EXPR:
2230 val = COND_EXPR_COND (expr);
2231 break;
2233 case SWITCH_EXPR:
2234 val = SWITCH_COND (expr);
2235 if (TREE_CODE (val) != INTEGER_CST)
2236 return false;
2237 break;
2239 default:
2240 gcc_unreachable ();
2243 taken_edge = find_taken_edge (bb, val);
2244 if (!taken_edge)
2245 return false;
2247 /* Remove all the edges except the one that is always executed. */
2248 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
2250 if (e != taken_edge)
2252 taken_edge->probability += e->probability;
2253 taken_edge->count += e->count;
2254 remove_edge (e);
2255 retval = true;
2257 else
2258 ei_next (&ei);
2260 if (taken_edge->probability > REG_BR_PROB_BASE)
2261 taken_edge->probability = REG_BR_PROB_BASE;
2263 else
2264 taken_edge = single_succ_edge (bb);
2266 bsi_remove (&bsi);
2267 taken_edge->flags = EDGE_FALLTHRU;
2269 /* We removed some paths from the cfg. */
2270 free_dominance_info (CDI_DOMINATORS);
2272 return retval;
2275 /* Remove any fallthru edge from EV. Return true if an edge was removed. */
2277 static bool
2278 remove_fallthru_edge (VEC(edge,gc) *ev)
2280 edge_iterator ei;
2281 edge e;
2283 FOR_EACH_EDGE (e, ei, ev)
2284 if ((e->flags & EDGE_FALLTHRU) != 0)
2286 remove_edge (e);
2287 return true;
2289 return false;
2292 /* Given a basic block BB ending with COND_EXPR or SWITCH_EXPR, and a
2293 predicate VAL, return the edge that will be taken out of the block.
2294 If VAL does not match a unique edge, NULL is returned. */
2296 edge
2297 find_taken_edge (basic_block bb, tree val)
2299 tree stmt;
2301 stmt = last_stmt (bb);
2303 gcc_assert (stmt);
2304 gcc_assert (is_ctrl_stmt (stmt));
2305 gcc_assert (val);
2307 if (! is_gimple_min_invariant (val))
2308 return NULL;
2310 if (TREE_CODE (stmt) == COND_EXPR)
2311 return find_taken_edge_cond_expr (bb, val);
2313 if (TREE_CODE (stmt) == SWITCH_EXPR)
2314 return find_taken_edge_switch_expr (bb, val);
2316 if (computed_goto_p (stmt))
2317 return find_taken_edge_computed_goto (bb, TREE_OPERAND( val, 0));
2319 gcc_unreachable ();
2322 /* Given a constant value VAL and the entry block BB to a GOTO_EXPR
2323 statement, determine which of the outgoing edges will be taken out of the
2324 block. Return NULL if either edge may be taken. */
2326 static edge
2327 find_taken_edge_computed_goto (basic_block bb, tree val)
2329 basic_block dest;
2330 edge e = NULL;
2332 dest = label_to_block (val);
2333 if (dest)
2335 e = find_edge (bb, dest);
2336 gcc_assert (e != NULL);
2339 return e;
2342 /* Given a constant value VAL and the entry block BB to a COND_EXPR
2343 statement, determine which of the two edges will be taken out of the
2344 block. Return NULL if either edge may be taken. */
2346 static edge
2347 find_taken_edge_cond_expr (basic_block bb, tree val)
2349 edge true_edge, false_edge;
2351 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
2353 gcc_assert (TREE_CODE (val) == INTEGER_CST);
2354 return (zero_p (val) ? false_edge : true_edge);
2357 /* Given an INTEGER_CST VAL and the entry block BB to a SWITCH_EXPR
2358 statement, determine which edge will be taken out of the block. Return
2359 NULL if any edge may be taken. */
2361 static edge
2362 find_taken_edge_switch_expr (basic_block bb, tree val)
2364 tree switch_expr, taken_case;
2365 basic_block dest_bb;
2366 edge e;
2368 switch_expr = last_stmt (bb);
2369 taken_case = find_case_label_for_value (switch_expr, val);
2370 dest_bb = label_to_block (CASE_LABEL (taken_case));
2372 e = find_edge (bb, dest_bb);
2373 gcc_assert (e);
2374 return e;
2378 /* Return the CASE_LABEL_EXPR that SWITCH_EXPR will take for VAL.
2379 We can make optimal use here of the fact that the case labels are
2380 sorted: We can do a binary search for a case matching VAL. */
2382 static tree
2383 find_case_label_for_value (tree switch_expr, tree val)
2385 tree vec = SWITCH_LABELS (switch_expr);
2386 size_t low, high, n = TREE_VEC_LENGTH (vec);
2387 tree default_case = TREE_VEC_ELT (vec, n - 1);
2389 for (low = -1, high = n - 1; high - low > 1; )
2391 size_t i = (high + low) / 2;
2392 tree t = TREE_VEC_ELT (vec, i);
2393 int cmp;
2395 /* Cache the result of comparing CASE_LOW and val. */
2396 cmp = tree_int_cst_compare (CASE_LOW (t), val);
2398 if (cmp > 0)
2399 high = i;
2400 else
2401 low = i;
2403 if (CASE_HIGH (t) == NULL)
2405 /* A singe-valued case label. */
2406 if (cmp == 0)
2407 return t;
2409 else
2411 /* A case range. We can only handle integer ranges. */
2412 if (cmp <= 0 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
2413 return t;
2417 return default_case;
2421 /* If all the PHI nodes in DEST have alternatives for E1 and E2 and
2422 those alternatives are equal in each of the PHI nodes, then return
2423 true, else return false. */
2425 static bool
2426 phi_alternatives_equal (basic_block dest, edge e1, edge e2)
2428 int n1 = e1->dest_idx;
2429 int n2 = e2->dest_idx;
2430 tree phi;
2432 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
2434 tree val1 = PHI_ARG_DEF (phi, n1);
2435 tree val2 = PHI_ARG_DEF (phi, n2);
2437 gcc_assert (val1 != NULL_TREE);
2438 gcc_assert (val2 != NULL_TREE);
2440 if (!operand_equal_for_phi_arg_p (val1, val2))
2441 return false;
2444 return true;
2448 /*---------------------------------------------------------------------------
2449 Debugging functions
2450 ---------------------------------------------------------------------------*/
2452 /* Dump tree-specific information of block BB to file OUTF. */
2454 void
2455 tree_dump_bb (basic_block bb, FILE *outf, int indent)
2457 dump_generic_bb (outf, bb, indent, TDF_VOPS);
2461 /* Dump a basic block on stderr. */
2463 void
2464 debug_tree_bb (basic_block bb)
2466 dump_bb (bb, stderr, 0);
2470 /* Dump basic block with index N on stderr. */
2472 basic_block
2473 debug_tree_bb_n (int n)
2475 debug_tree_bb (BASIC_BLOCK (n));
2476 return BASIC_BLOCK (n);
2480 /* Dump the CFG on stderr.
2482 FLAGS are the same used by the tree dumping functions
2483 (see TDF_* in tree.h). */
2485 void
2486 debug_tree_cfg (int flags)
2488 dump_tree_cfg (stderr, flags);
2492 /* Dump the program showing basic block boundaries on the given FILE.
2494 FLAGS are the same used by the tree dumping functions (see TDF_* in
2495 tree.h). */
2497 void
2498 dump_tree_cfg (FILE *file, int flags)
2500 if (flags & TDF_DETAILS)
2502 const char *funcname
2503 = lang_hooks.decl_printable_name (current_function_decl, 2);
2505 fputc ('\n', file);
2506 fprintf (file, ";; Function %s\n\n", funcname);
2507 fprintf (file, ";; \n%d basic blocks, %d edges, last basic block %d.\n\n",
2508 n_basic_blocks, n_edges, last_basic_block);
2510 brief_dump_cfg (file);
2511 fprintf (file, "\n");
2514 if (flags & TDF_STATS)
2515 dump_cfg_stats (file);
2517 dump_function_to_file (current_function_decl, file, flags | TDF_BLOCKS);
2521 /* Dump CFG statistics on FILE. */
2523 void
2524 dump_cfg_stats (FILE *file)
2526 static long max_num_merged_labels = 0;
2527 unsigned long size, total = 0;
2528 long num_edges;
2529 basic_block bb;
2530 const char * const fmt_str = "%-30s%-13s%12s\n";
2531 const char * const fmt_str_1 = "%-30s%13d%11lu%c\n";
2532 const char * const fmt_str_3 = "%-43s%11lu%c\n";
2533 const char *funcname
2534 = lang_hooks.decl_printable_name (current_function_decl, 2);
2537 fprintf (file, "\nCFG Statistics for %s\n\n", funcname);
2539 fprintf (file, "---------------------------------------------------------\n");
2540 fprintf (file, fmt_str, "", " Number of ", "Memory");
2541 fprintf (file, fmt_str, "", " instances ", "used ");
2542 fprintf (file, "---------------------------------------------------------\n");
2544 size = n_basic_blocks * sizeof (struct basic_block_def);
2545 total += size;
2546 fprintf (file, fmt_str_1, "Basic blocks", n_basic_blocks,
2547 SCALE (size), LABEL (size));
2549 num_edges = 0;
2550 FOR_EACH_BB (bb)
2551 num_edges += EDGE_COUNT (bb->succs);
2552 size = num_edges * sizeof (struct edge_def);
2553 total += size;
2554 fprintf (file, fmt_str_1, "Edges", num_edges, SCALE (size), LABEL (size));
2556 size = n_basic_blocks * sizeof (struct bb_ann_d);
2557 total += size;
2558 fprintf (file, fmt_str_1, "Basic block annotations", n_basic_blocks,
2559 SCALE (size), LABEL (size));
2561 fprintf (file, "---------------------------------------------------------\n");
2562 fprintf (file, fmt_str_3, "Total memory used by CFG data", SCALE (total),
2563 LABEL (total));
2564 fprintf (file, "---------------------------------------------------------\n");
2565 fprintf (file, "\n");
2567 if (cfg_stats.num_merged_labels > max_num_merged_labels)
2568 max_num_merged_labels = cfg_stats.num_merged_labels;
2570 fprintf (file, "Coalesced label blocks: %ld (Max so far: %ld)\n",
2571 cfg_stats.num_merged_labels, max_num_merged_labels);
2573 fprintf (file, "\n");
2577 /* Dump CFG statistics on stderr. Keep extern so that it's always
2578 linked in the final executable. */
2580 void
2581 debug_cfg_stats (void)
2583 dump_cfg_stats (stderr);
2587 /* Dump the flowgraph to a .vcg FILE. */
2589 static void
2590 tree_cfg2vcg (FILE *file)
2592 edge e;
2593 edge_iterator ei;
2594 basic_block bb;
2595 const char *funcname
2596 = lang_hooks.decl_printable_name (current_function_decl, 2);
2598 /* Write the file header. */
2599 fprintf (file, "graph: { title: \"%s\"\n", funcname);
2600 fprintf (file, "node: { title: \"ENTRY\" label: \"ENTRY\" }\n");
2601 fprintf (file, "node: { title: \"EXIT\" label: \"EXIT\" }\n");
2603 /* Write blocks and edges. */
2604 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
2606 fprintf (file, "edge: { sourcename: \"ENTRY\" targetname: \"%d\"",
2607 e->dest->index);
2609 if (e->flags & EDGE_FAKE)
2610 fprintf (file, " linestyle: dotted priority: 10");
2611 else
2612 fprintf (file, " linestyle: solid priority: 100");
2614 fprintf (file, " }\n");
2616 fputc ('\n', file);
2618 FOR_EACH_BB (bb)
2620 enum tree_code head_code, end_code;
2621 const char *head_name, *end_name;
2622 int head_line = 0;
2623 int end_line = 0;
2624 tree first = first_stmt (bb);
2625 tree last = last_stmt (bb);
2627 if (first)
2629 head_code = TREE_CODE (first);
2630 head_name = tree_code_name[head_code];
2631 head_line = get_lineno (first);
2633 else
2634 head_name = "no-statement";
2636 if (last)
2638 end_code = TREE_CODE (last);
2639 end_name = tree_code_name[end_code];
2640 end_line = get_lineno (last);
2642 else
2643 end_name = "no-statement";
2645 fprintf (file, "node: { title: \"%d\" label: \"#%d\\n%s (%d)\\n%s (%d)\"}\n",
2646 bb->index, bb->index, head_name, head_line, end_name,
2647 end_line);
2649 FOR_EACH_EDGE (e, ei, bb->succs)
2651 if (e->dest == EXIT_BLOCK_PTR)
2652 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"EXIT\"", bb->index);
2653 else
2654 fprintf (file, "edge: { sourcename: \"%d\" targetname: \"%d\"", bb->index, e->dest->index);
2656 if (e->flags & EDGE_FAKE)
2657 fprintf (file, " priority: 10 linestyle: dotted");
2658 else
2659 fprintf (file, " priority: 100 linestyle: solid");
2661 fprintf (file, " }\n");
2664 if (bb->next_bb != EXIT_BLOCK_PTR)
2665 fputc ('\n', file);
2668 fputs ("}\n\n", file);
2673 /*---------------------------------------------------------------------------
2674 Miscellaneous helpers
2675 ---------------------------------------------------------------------------*/
2677 /* Return true if T represents a stmt that always transfers control. */
2679 bool
2680 is_ctrl_stmt (tree t)
2682 return (TREE_CODE (t) == COND_EXPR
2683 || TREE_CODE (t) == SWITCH_EXPR
2684 || TREE_CODE (t) == GOTO_EXPR
2685 || TREE_CODE (t) == RETURN_EXPR
2686 || TREE_CODE (t) == RESX_EXPR);
2690 /* Return true if T is a statement that may alter the flow of control
2691 (e.g., a call to a non-returning function). */
2693 bool
2694 is_ctrl_altering_stmt (tree t)
2696 tree call;
2698 gcc_assert (t);
2699 call = get_call_expr_in (t);
2700 if (call)
2702 /* A non-pure/const CALL_EXPR alters flow control if the current
2703 function has nonlocal labels. */
2704 if (TREE_SIDE_EFFECTS (call) && current_function_has_nonlocal_label)
2705 return true;
2707 /* A CALL_EXPR also alters control flow if it does not return. */
2708 if (call_expr_flags (call) & ECF_NORETURN)
2709 return true;
2712 /* If a statement can throw, it alters control flow. */
2713 return tree_can_throw_internal (t);
2717 /* Return true if T is a computed goto. */
2719 bool
2720 computed_goto_p (tree t)
2722 return (TREE_CODE (t) == GOTO_EXPR
2723 && TREE_CODE (GOTO_DESTINATION (t)) != LABEL_DECL);
2727 /* Checks whether EXPR is a simple local goto. */
2729 bool
2730 simple_goto_p (tree expr)
2732 return (TREE_CODE (expr) == GOTO_EXPR
2733 && TREE_CODE (GOTO_DESTINATION (expr)) == LABEL_DECL);
2737 /* Return true if T should start a new basic block. PREV_T is the
2738 statement preceding T. It is used when T is a label or a case label.
2739 Labels should only start a new basic block if their previous statement
2740 wasn't a label. Otherwise, sequence of labels would generate
2741 unnecessary basic blocks that only contain a single label. */
2743 static inline bool
2744 stmt_starts_bb_p (tree t, tree prev_t)
2746 if (t == NULL_TREE)
2747 return false;
2749 /* LABEL_EXPRs start a new basic block only if the preceding
2750 statement wasn't a label of the same type. This prevents the
2751 creation of consecutive blocks that have nothing but a single
2752 label. */
2753 if (TREE_CODE (t) == LABEL_EXPR)
2755 /* Nonlocal and computed GOTO targets always start a new block. */
2756 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (t))
2757 || FORCED_LABEL (LABEL_EXPR_LABEL (t)))
2758 return true;
2760 if (prev_t && TREE_CODE (prev_t) == LABEL_EXPR)
2762 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (prev_t)))
2763 return true;
2765 cfg_stats.num_merged_labels++;
2766 return false;
2768 else
2769 return true;
2772 return false;
2776 /* Return true if T should end a basic block. */
2778 bool
2779 stmt_ends_bb_p (tree t)
2781 return is_ctrl_stmt (t) || is_ctrl_altering_stmt (t);
2785 /* Add gotos that used to be represented implicitly in the CFG. */
2787 void
2788 disband_implicit_edges (void)
2790 basic_block bb;
2791 block_stmt_iterator last;
2792 edge e;
2793 edge_iterator ei;
2794 tree stmt, label;
2796 FOR_EACH_BB (bb)
2798 last = bsi_last (bb);
2799 stmt = last_stmt (bb);
2801 if (stmt && TREE_CODE (stmt) == COND_EXPR)
2803 /* Remove superfluous gotos from COND_EXPR branches. Moved
2804 from cfg_remove_useless_stmts here since it violates the
2805 invariants for tree--cfg correspondence and thus fits better
2806 here where we do it anyway. */
2807 e = find_edge (bb, bb->next_bb);
2808 if (e)
2810 if (e->flags & EDGE_TRUE_VALUE)
2811 COND_EXPR_THEN (stmt) = build_empty_stmt ();
2812 else if (e->flags & EDGE_FALSE_VALUE)
2813 COND_EXPR_ELSE (stmt) = build_empty_stmt ();
2814 else
2815 gcc_unreachable ();
2816 e->flags |= EDGE_FALLTHRU;
2819 continue;
2822 if (stmt && TREE_CODE (stmt) == RETURN_EXPR)
2824 /* Remove the RETURN_EXPR if we may fall though to the exit
2825 instead. */
2826 gcc_assert (single_succ_p (bb));
2827 gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR);
2829 if (bb->next_bb == EXIT_BLOCK_PTR
2830 && !TREE_OPERAND (stmt, 0))
2832 bsi_remove (&last);
2833 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2835 continue;
2838 /* There can be no fallthru edge if the last statement is a control
2839 one. */
2840 if (stmt && is_ctrl_stmt (stmt))
2841 continue;
2843 /* Find a fallthru edge and emit the goto if necessary. */
2844 FOR_EACH_EDGE (e, ei, bb->succs)
2845 if (e->flags & EDGE_FALLTHRU)
2846 break;
2848 if (!e || e->dest == bb->next_bb)
2849 continue;
2851 gcc_assert (e->dest != EXIT_BLOCK_PTR);
2852 label = tree_block_label (e->dest);
2854 stmt = build1 (GOTO_EXPR, void_type_node, label);
2855 #ifdef USE_MAPPED_LOCATION
2856 SET_EXPR_LOCATION (stmt, e->goto_locus);
2857 #else
2858 SET_EXPR_LOCUS (stmt, e->goto_locus);
2859 #endif
2860 bsi_insert_after (&last, stmt, BSI_NEW_STMT);
2861 e->flags &= ~EDGE_FALLTHRU;
2865 /* Remove block annotations and other datastructures. */
2867 void
2868 delete_tree_cfg_annotations (void)
2870 basic_block bb;
2871 if (n_basic_blocks > 0)
2872 free_blocks_annotations ();
2874 label_to_block_map = NULL;
2875 FOR_EACH_BB (bb)
2876 bb->rbi = NULL;
2880 /* Return the first statement in basic block BB. */
2882 tree
2883 first_stmt (basic_block bb)
2885 block_stmt_iterator i = bsi_start (bb);
2886 return !bsi_end_p (i) ? bsi_stmt (i) : NULL_TREE;
2890 /* Return the last statement in basic block BB. */
2892 tree
2893 last_stmt (basic_block bb)
2895 block_stmt_iterator b = bsi_last (bb);
2896 return !bsi_end_p (b) ? bsi_stmt (b) : NULL_TREE;
2900 /* Return a pointer to the last statement in block BB. */
2902 tree *
2903 last_stmt_ptr (basic_block bb)
2905 block_stmt_iterator last = bsi_last (bb);
2906 return !bsi_end_p (last) ? bsi_stmt_ptr (last) : NULL;
2910 /* Return the last statement of an otherwise empty block. Return NULL
2911 if the block is totally empty, or if it contains more than one
2912 statement. */
2914 tree
2915 last_and_only_stmt (basic_block bb)
2917 block_stmt_iterator i = bsi_last (bb);
2918 tree last, prev;
2920 if (bsi_end_p (i))
2921 return NULL_TREE;
2923 last = bsi_stmt (i);
2924 bsi_prev (&i);
2925 if (bsi_end_p (i))
2926 return last;
2928 /* Empty statements should no longer appear in the instruction stream.
2929 Everything that might have appeared before should be deleted by
2930 remove_useless_stmts, and the optimizers should just bsi_remove
2931 instead of smashing with build_empty_stmt.
2933 Thus the only thing that should appear here in a block containing
2934 one executable statement is a label. */
2935 prev = bsi_stmt (i);
2936 if (TREE_CODE (prev) == LABEL_EXPR)
2937 return last;
2938 else
2939 return NULL_TREE;
2943 /* Mark BB as the basic block holding statement T. */
2945 void
2946 set_bb_for_stmt (tree t, basic_block bb)
2948 if (TREE_CODE (t) == PHI_NODE)
2949 PHI_BB (t) = bb;
2950 else if (TREE_CODE (t) == STATEMENT_LIST)
2952 tree_stmt_iterator i;
2953 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2954 set_bb_for_stmt (tsi_stmt (i), bb);
2956 else
2958 stmt_ann_t ann = get_stmt_ann (t);
2959 ann->bb = bb;
2961 /* If the statement is a label, add the label to block-to-labels map
2962 so that we can speed up edge creation for GOTO_EXPRs. */
2963 if (TREE_CODE (t) == LABEL_EXPR)
2965 int uid;
2967 t = LABEL_EXPR_LABEL (t);
2968 uid = LABEL_DECL_UID (t);
2969 if (uid == -1)
2971 LABEL_DECL_UID (t) = uid = cfun->last_label_uid++;
2972 if (VARRAY_SIZE (label_to_block_map) <= (unsigned) uid)
2973 VARRAY_GROW (label_to_block_map, 3 * uid / 2);
2975 else
2976 /* We're moving an existing label. Make sure that we've
2977 removed it from the old block. */
2978 gcc_assert (!bb || !VARRAY_BB (label_to_block_map, uid));
2979 VARRAY_BB (label_to_block_map, uid) = bb;
2984 /* Finds iterator for STMT. */
2986 extern block_stmt_iterator
2987 bsi_for_stmt (tree stmt)
2989 block_stmt_iterator bsi;
2991 for (bsi = bsi_start (bb_for_stmt (stmt)); !bsi_end_p (bsi); bsi_next (&bsi))
2992 if (bsi_stmt (bsi) == stmt)
2993 return bsi;
2995 gcc_unreachable ();
2998 /* Mark statement T as modified, and update it. */
2999 static inline void
3000 update_modified_stmts (tree t)
3002 if (TREE_CODE (t) == STATEMENT_LIST)
3004 tree_stmt_iterator i;
3005 tree stmt;
3006 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3008 stmt = tsi_stmt (i);
3009 update_stmt_if_modified (stmt);
3012 else
3013 update_stmt_if_modified (t);
3016 /* Insert statement (or statement list) T before the statement
3017 pointed-to by iterator I. M specifies how to update iterator I
3018 after insertion (see enum bsi_iterator_update). */
3020 void
3021 bsi_insert_before (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
3023 set_bb_for_stmt (t, i->bb);
3024 update_modified_stmts (t);
3025 tsi_link_before (&i->tsi, t, m);
3029 /* Insert statement (or statement list) T after the statement
3030 pointed-to by iterator I. M specifies how to update iterator I
3031 after insertion (see enum bsi_iterator_update). */
3033 void
3034 bsi_insert_after (block_stmt_iterator *i, tree t, enum bsi_iterator_update m)
3036 set_bb_for_stmt (t, i->bb);
3037 update_modified_stmts (t);
3038 tsi_link_after (&i->tsi, t, m);
3042 /* Remove the statement pointed to by iterator I. The iterator is updated
3043 to the next statement. */
3045 void
3046 bsi_remove (block_stmt_iterator *i)
3048 tree t = bsi_stmt (*i);
3049 set_bb_for_stmt (t, NULL);
3050 delink_stmt_imm_use (t);
3051 tsi_delink (&i->tsi);
3052 mark_stmt_modified (t);
3056 /* Move the statement at FROM so it comes right after the statement at TO. */
3058 void
3059 bsi_move_after (block_stmt_iterator *from, block_stmt_iterator *to)
3061 tree stmt = bsi_stmt (*from);
3062 bsi_remove (from);
3063 bsi_insert_after (to, stmt, BSI_SAME_STMT);
3067 /* Move the statement at FROM so it comes right before the statement at TO. */
3069 void
3070 bsi_move_before (block_stmt_iterator *from, block_stmt_iterator *to)
3072 tree stmt = bsi_stmt (*from);
3073 bsi_remove (from);
3074 bsi_insert_before (to, stmt, BSI_SAME_STMT);
3078 /* Move the statement at FROM to the end of basic block BB. */
3080 void
3081 bsi_move_to_bb_end (block_stmt_iterator *from, basic_block bb)
3083 block_stmt_iterator last = bsi_last (bb);
3085 /* Have to check bsi_end_p because it could be an empty block. */
3086 if (!bsi_end_p (last) && is_ctrl_stmt (bsi_stmt (last)))
3087 bsi_move_before (from, &last);
3088 else
3089 bsi_move_after (from, &last);
3093 /* Replace the contents of the statement pointed to by iterator BSI
3094 with STMT. If PRESERVE_EH_INFO is true, the exception handling
3095 information of the original statement is preserved. */
3097 void
3098 bsi_replace (const block_stmt_iterator *bsi, tree stmt, bool preserve_eh_info)
3100 int eh_region;
3101 tree orig_stmt = bsi_stmt (*bsi);
3103 SET_EXPR_LOCUS (stmt, EXPR_LOCUS (orig_stmt));
3104 set_bb_for_stmt (stmt, bsi->bb);
3106 /* Preserve EH region information from the original statement, if
3107 requested by the caller. */
3108 if (preserve_eh_info)
3110 eh_region = lookup_stmt_eh_region (orig_stmt);
3111 if (eh_region >= 0)
3112 add_stmt_to_eh_region (stmt, eh_region);
3115 delink_stmt_imm_use (orig_stmt);
3116 *bsi_stmt_ptr (*bsi) = stmt;
3117 mark_stmt_modified (stmt);
3118 update_modified_stmts (stmt);
3122 /* Insert the statement pointed-to by BSI into edge E. Every attempt
3123 is made to place the statement in an existing basic block, but
3124 sometimes that isn't possible. When it isn't possible, the edge is
3125 split and the statement is added to the new block.
3127 In all cases, the returned *BSI points to the correct location. The
3128 return value is true if insertion should be done after the location,
3129 or false if it should be done before the location. If new basic block
3130 has to be created, it is stored in *NEW_BB. */
3132 static bool
3133 tree_find_edge_insert_loc (edge e, block_stmt_iterator *bsi,
3134 basic_block *new_bb)
3136 basic_block dest, src;
3137 tree tmp;
3139 dest = e->dest;
3140 restart:
3142 /* If the destination has one predecessor which has no PHI nodes,
3143 insert there. Except for the exit block.
3145 The requirement for no PHI nodes could be relaxed. Basically we
3146 would have to examine the PHIs to prove that none of them used
3147 the value set by the statement we want to insert on E. That
3148 hardly seems worth the effort. */
3149 if (single_pred_p (dest)
3150 && ! phi_nodes (dest)
3151 && dest != EXIT_BLOCK_PTR)
3153 *bsi = bsi_start (dest);
3154 if (bsi_end_p (*bsi))
3155 return true;
3157 /* Make sure we insert after any leading labels. */
3158 tmp = bsi_stmt (*bsi);
3159 while (TREE_CODE (tmp) == LABEL_EXPR)
3161 bsi_next (bsi);
3162 if (bsi_end_p (*bsi))
3163 break;
3164 tmp = bsi_stmt (*bsi);
3167 if (bsi_end_p (*bsi))
3169 *bsi = bsi_last (dest);
3170 return true;
3172 else
3173 return false;
3176 /* If the source has one successor, the edge is not abnormal and
3177 the last statement does not end a basic block, insert there.
3178 Except for the entry block. */
3179 src = e->src;
3180 if ((e->flags & EDGE_ABNORMAL) == 0
3181 && single_succ_p (src)
3182 && src != ENTRY_BLOCK_PTR)
3184 *bsi = bsi_last (src);
3185 if (bsi_end_p (*bsi))
3186 return true;
3188 tmp = bsi_stmt (*bsi);
3189 if (!stmt_ends_bb_p (tmp))
3190 return true;
3192 /* Insert code just before returning the value. We may need to decompose
3193 the return in the case it contains non-trivial operand. */
3194 if (TREE_CODE (tmp) == RETURN_EXPR)
3196 tree op = TREE_OPERAND (tmp, 0);
3197 if (!is_gimple_val (op))
3199 gcc_assert (TREE_CODE (op) == MODIFY_EXPR);
3200 bsi_insert_before (bsi, op, BSI_NEW_STMT);
3201 TREE_OPERAND (tmp, 0) = TREE_OPERAND (op, 0);
3203 bsi_prev (bsi);
3204 return true;
3208 /* Otherwise, create a new basic block, and split this edge. */
3209 dest = split_edge (e);
3210 if (new_bb)
3211 *new_bb = dest;
3212 e = single_pred_edge (dest);
3213 goto restart;
3217 /* This routine will commit all pending edge insertions, creating any new
3218 basic blocks which are necessary. */
3220 void
3221 bsi_commit_edge_inserts (void)
3223 basic_block bb;
3224 edge e;
3225 edge_iterator ei;
3227 bsi_commit_one_edge_insert (single_succ_edge (ENTRY_BLOCK_PTR), NULL);
3229 FOR_EACH_BB (bb)
3230 FOR_EACH_EDGE (e, ei, bb->succs)
3231 bsi_commit_one_edge_insert (e, NULL);
3235 /* Commit insertions pending at edge E. If a new block is created, set NEW_BB
3236 to this block, otherwise set it to NULL. */
3238 void
3239 bsi_commit_one_edge_insert (edge e, basic_block *new_bb)
3241 if (new_bb)
3242 *new_bb = NULL;
3243 if (PENDING_STMT (e))
3245 block_stmt_iterator bsi;
3246 tree stmt = PENDING_STMT (e);
3248 PENDING_STMT (e) = NULL_TREE;
3250 if (tree_find_edge_insert_loc (e, &bsi, new_bb))
3251 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3252 else
3253 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3258 /* Add STMT to the pending list of edge E. No actual insertion is
3259 made until a call to bsi_commit_edge_inserts () is made. */
3261 void
3262 bsi_insert_on_edge (edge e, tree stmt)
3264 append_to_statement_list (stmt, &PENDING_STMT (e));
3267 /* Similar to bsi_insert_on_edge+bsi_commit_edge_inserts. If a new
3268 block has to be created, it is returned. */
3270 basic_block
3271 bsi_insert_on_edge_immediate (edge e, tree stmt)
3273 block_stmt_iterator bsi;
3274 basic_block new_bb = NULL;
3276 gcc_assert (!PENDING_STMT (e));
3278 if (tree_find_edge_insert_loc (e, &bsi, &new_bb))
3279 bsi_insert_after (&bsi, stmt, BSI_NEW_STMT);
3280 else
3281 bsi_insert_before (&bsi, stmt, BSI_NEW_STMT);
3283 return new_bb;
3286 /*---------------------------------------------------------------------------
3287 Tree specific functions for CFG manipulation
3288 ---------------------------------------------------------------------------*/
3290 /* Reinstall those PHI arguments queued in OLD_EDGE to NEW_EDGE. */
3292 static void
3293 reinstall_phi_args (edge new_edge, edge old_edge)
3295 tree var, phi;
3297 if (!PENDING_STMT (old_edge))
3298 return;
3300 for (var = PENDING_STMT (old_edge), phi = phi_nodes (new_edge->dest);
3301 var && phi;
3302 var = TREE_CHAIN (var), phi = PHI_CHAIN (phi))
3304 tree result = TREE_PURPOSE (var);
3305 tree arg = TREE_VALUE (var);
3307 gcc_assert (result == PHI_RESULT (phi));
3309 add_phi_arg (phi, arg, new_edge);
3312 PENDING_STMT (old_edge) = NULL;
3315 /* Split a (typically critical) edge EDGE_IN. Return the new block.
3316 Abort on abnormal edges. */
3318 static basic_block
3319 tree_split_edge (edge edge_in)
3321 basic_block new_bb, after_bb, dest, src;
3322 edge new_edge, e;
3324 /* Abnormal edges cannot be split. */
3325 gcc_assert (!(edge_in->flags & EDGE_ABNORMAL));
3327 src = edge_in->src;
3328 dest = edge_in->dest;
3330 /* Place the new block in the block list. Try to keep the new block
3331 near its "logical" location. This is of most help to humans looking
3332 at debugging dumps. */
3333 if (dest->prev_bb && find_edge (dest->prev_bb, dest))
3334 after_bb = edge_in->src;
3335 else
3336 after_bb = dest->prev_bb;
3338 new_bb = create_empty_bb (after_bb);
3339 new_bb->frequency = EDGE_FREQUENCY (edge_in);
3340 new_bb->count = edge_in->count;
3341 new_edge = make_edge (new_bb, dest, EDGE_FALLTHRU);
3342 new_edge->probability = REG_BR_PROB_BASE;
3343 new_edge->count = edge_in->count;
3345 e = redirect_edge_and_branch (edge_in, new_bb);
3346 gcc_assert (e);
3347 reinstall_phi_args (new_edge, e);
3349 return new_bb;
3353 /* Return true when BB has label LABEL in it. */
3355 static bool
3356 has_label_p (basic_block bb, tree label)
3358 block_stmt_iterator bsi;
3360 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3362 tree stmt = bsi_stmt (bsi);
3364 if (TREE_CODE (stmt) != LABEL_EXPR)
3365 return false;
3366 if (LABEL_EXPR_LABEL (stmt) == label)
3367 return true;
3369 return false;
3373 /* Callback for walk_tree, check that all elements with address taken are
3374 properly noticed as such. The DATA is an int* that is 1 if TP was seen
3375 inside a PHI node. */
3377 static tree
3378 verify_expr (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
3380 tree t = *tp, x;
3381 bool in_phi = (data != NULL);
3383 if (TYPE_P (t))
3384 *walk_subtrees = 0;
3386 /* Check operand N for being valid GIMPLE and give error MSG if not.
3387 We check for constants explicitly since they are not considered
3388 gimple invariants if they overflowed. */
3389 #define CHECK_OP(N, MSG) \
3390 do { if (!CONSTANT_CLASS_P (TREE_OPERAND (t, N)) \
3391 && !is_gimple_val (TREE_OPERAND (t, N))) \
3392 { error (MSG); return TREE_OPERAND (t, N); }} while (0)
3394 switch (TREE_CODE (t))
3396 case SSA_NAME:
3397 if (SSA_NAME_IN_FREE_LIST (t))
3399 error ("SSA name in freelist but still referenced");
3400 return *tp;
3402 break;
3404 case ASSERT_EXPR:
3405 x = fold (ASSERT_EXPR_COND (t));
3406 if (x == boolean_false_node)
3408 error ("ASSERT_EXPR with an always-false condition");
3409 return *tp;
3411 break;
3413 case MODIFY_EXPR:
3414 x = TREE_OPERAND (t, 0);
3415 if (TREE_CODE (x) == BIT_FIELD_REF
3416 && is_gimple_reg (TREE_OPERAND (x, 0)))
3418 error ("GIMPLE register modified with BIT_FIELD_REF");
3419 return t;
3421 break;
3423 case ADDR_EXPR:
3424 /* ??? tree-ssa-alias.c may have overlooked dead PHI nodes, missing
3425 dead PHIs that take the address of something. But if the PHI
3426 result is dead, the fact that it takes the address of anything
3427 is irrelevant. Because we can not tell from here if a PHI result
3428 is dead, we just skip this check for PHIs altogether. This means
3429 we may be missing "valid" checks, but what can you do?
3430 This was PR19217. */
3431 if (in_phi)
3432 break;
3434 /* Skip any references (they will be checked when we recurse down the
3435 tree) and ensure that any variable used as a prefix is marked
3436 addressable. */
3437 for (x = TREE_OPERAND (t, 0);
3438 handled_component_p (x);
3439 x = TREE_OPERAND (x, 0))
3442 if (TREE_CODE (x) != VAR_DECL && TREE_CODE (x) != PARM_DECL)
3443 return NULL;
3444 if (!TREE_ADDRESSABLE (x))
3446 error ("address taken, but ADDRESSABLE bit not set");
3447 return x;
3449 break;
3451 case COND_EXPR:
3452 x = COND_EXPR_COND (t);
3453 if (TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3455 error ("non-boolean used in condition");
3456 return x;
3458 break;
3460 case NOP_EXPR:
3461 case CONVERT_EXPR:
3462 case FIX_TRUNC_EXPR:
3463 case FIX_CEIL_EXPR:
3464 case FIX_FLOOR_EXPR:
3465 case FIX_ROUND_EXPR:
3466 case FLOAT_EXPR:
3467 case NEGATE_EXPR:
3468 case ABS_EXPR:
3469 case BIT_NOT_EXPR:
3470 case NON_LVALUE_EXPR:
3471 case TRUTH_NOT_EXPR:
3472 CHECK_OP (0, "Invalid operand to unary operator");
3473 break;
3475 case REALPART_EXPR:
3476 case IMAGPART_EXPR:
3477 case COMPONENT_REF:
3478 case ARRAY_REF:
3479 case ARRAY_RANGE_REF:
3480 case BIT_FIELD_REF:
3481 case VIEW_CONVERT_EXPR:
3482 /* We have a nest of references. Verify that each of the operands
3483 that determine where to reference is either a constant or a variable,
3484 verify that the base is valid, and then show we've already checked
3485 the subtrees. */
3486 while (handled_component_p (t))
3488 if (TREE_CODE (t) == COMPONENT_REF && TREE_OPERAND (t, 2))
3489 CHECK_OP (2, "Invalid COMPONENT_REF offset operator");
3490 else if (TREE_CODE (t) == ARRAY_REF
3491 || TREE_CODE (t) == ARRAY_RANGE_REF)
3493 CHECK_OP (1, "Invalid array index.");
3494 if (TREE_OPERAND (t, 2))
3495 CHECK_OP (2, "Invalid array lower bound.");
3496 if (TREE_OPERAND (t, 3))
3497 CHECK_OP (3, "Invalid array stride.");
3499 else if (TREE_CODE (t) == BIT_FIELD_REF)
3501 CHECK_OP (1, "Invalid operand to BIT_FIELD_REF");
3502 CHECK_OP (2, "Invalid operand to BIT_FIELD_REF");
3505 t = TREE_OPERAND (t, 0);
3508 if (!CONSTANT_CLASS_P (t) && !is_gimple_lvalue (t))
3510 error ("Invalid reference prefix.");
3511 return t;
3513 *walk_subtrees = 0;
3514 break;
3516 case LT_EXPR:
3517 case LE_EXPR:
3518 case GT_EXPR:
3519 case GE_EXPR:
3520 case EQ_EXPR:
3521 case NE_EXPR:
3522 case UNORDERED_EXPR:
3523 case ORDERED_EXPR:
3524 case UNLT_EXPR:
3525 case UNLE_EXPR:
3526 case UNGT_EXPR:
3527 case UNGE_EXPR:
3528 case UNEQ_EXPR:
3529 case LTGT_EXPR:
3530 case PLUS_EXPR:
3531 case MINUS_EXPR:
3532 case MULT_EXPR:
3533 case TRUNC_DIV_EXPR:
3534 case CEIL_DIV_EXPR:
3535 case FLOOR_DIV_EXPR:
3536 case ROUND_DIV_EXPR:
3537 case TRUNC_MOD_EXPR:
3538 case CEIL_MOD_EXPR:
3539 case FLOOR_MOD_EXPR:
3540 case ROUND_MOD_EXPR:
3541 case RDIV_EXPR:
3542 case EXACT_DIV_EXPR:
3543 case MIN_EXPR:
3544 case MAX_EXPR:
3545 case LSHIFT_EXPR:
3546 case RSHIFT_EXPR:
3547 case LROTATE_EXPR:
3548 case RROTATE_EXPR:
3549 case BIT_IOR_EXPR:
3550 case BIT_XOR_EXPR:
3551 case BIT_AND_EXPR:
3552 CHECK_OP (0, "Invalid operand to binary operator");
3553 CHECK_OP (1, "Invalid operand to binary operator");
3554 break;
3556 default:
3557 break;
3559 return NULL;
3561 #undef CHECK_OP
3565 /* Verify STMT, return true if STMT is not in GIMPLE form.
3566 TODO: Implement type checking. */
3568 static bool
3569 verify_stmt (tree stmt, bool last_in_block)
3571 tree addr;
3573 if (!is_gimple_stmt (stmt))
3575 error ("Is not a valid GIMPLE statement.");
3576 goto fail;
3579 addr = walk_tree (&stmt, verify_expr, NULL, NULL);
3580 if (addr)
3582 debug_generic_stmt (addr);
3583 return true;
3586 /* If the statement is marked as part of an EH region, then it is
3587 expected that the statement could throw. Verify that when we
3588 have optimizations that simplify statements such that we prove
3589 that they cannot throw, that we update other data structures
3590 to match. */
3591 if (lookup_stmt_eh_region (stmt) >= 0)
3593 if (!tree_could_throw_p (stmt))
3595 error ("Statement marked for throw, but doesn%'t.");
3596 goto fail;
3598 if (!last_in_block && tree_can_throw_internal (stmt))
3600 error ("Statement marked for throw in middle of block.");
3601 goto fail;
3605 return false;
3607 fail:
3608 debug_generic_stmt (stmt);
3609 return true;
3613 /* Return true when the T can be shared. */
3615 static bool
3616 tree_node_can_be_shared (tree t)
3618 if (IS_TYPE_OR_DECL_P (t)
3619 /* We check for constants explicitly since they are not considered
3620 gimple invariants if they overflowed. */
3621 || CONSTANT_CLASS_P (t)
3622 || is_gimple_min_invariant (t)
3623 || TREE_CODE (t) == SSA_NAME
3624 || t == error_mark_node)
3625 return true;
3627 if (TREE_CODE (t) == CASE_LABEL_EXPR)
3628 return true;
3630 while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
3631 /* We check for constants explicitly since they are not considered
3632 gimple invariants if they overflowed. */
3633 && (CONSTANT_CLASS_P (TREE_OPERAND (t, 1))
3634 || is_gimple_min_invariant (TREE_OPERAND (t, 1))))
3635 || (TREE_CODE (t) == COMPONENT_REF
3636 || TREE_CODE (t) == REALPART_EXPR
3637 || TREE_CODE (t) == IMAGPART_EXPR))
3638 t = TREE_OPERAND (t, 0);
3640 if (DECL_P (t))
3641 return true;
3643 return false;
3647 /* Called via walk_trees. Verify tree sharing. */
3649 static tree
3650 verify_node_sharing (tree * tp, int *walk_subtrees, void *data)
3652 htab_t htab = (htab_t) data;
3653 void **slot;
3655 if (tree_node_can_be_shared (*tp))
3657 *walk_subtrees = false;
3658 return NULL;
3661 slot = htab_find_slot (htab, *tp, INSERT);
3662 if (*slot)
3663 return *slot;
3664 *slot = *tp;
3666 return NULL;
3670 /* Verify the GIMPLE statement chain. */
3672 void
3673 verify_stmts (void)
3675 basic_block bb;
3676 block_stmt_iterator bsi;
3677 bool err = false;
3678 htab_t htab;
3679 tree addr;
3681 timevar_push (TV_TREE_STMT_VERIFY);
3682 htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
3684 FOR_EACH_BB (bb)
3686 tree phi;
3687 int i;
3689 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
3691 int phi_num_args = PHI_NUM_ARGS (phi);
3693 if (bb_for_stmt (phi) != bb)
3695 error ("bb_for_stmt (phi) is set to a wrong basic block\n");
3696 err |= true;
3699 for (i = 0; i < phi_num_args; i++)
3701 tree t = PHI_ARG_DEF (phi, i);
3702 tree addr;
3704 /* Addressable variables do have SSA_NAMEs but they
3705 are not considered gimple values. */
3706 if (TREE_CODE (t) != SSA_NAME
3707 && TREE_CODE (t) != FUNCTION_DECL
3708 && !is_gimple_val (t))
3710 error ("PHI def is not a GIMPLE value");
3711 debug_generic_stmt (phi);
3712 debug_generic_stmt (t);
3713 err |= true;
3716 addr = walk_tree (&t, verify_expr, (void *) 1, NULL);
3717 if (addr)
3719 debug_generic_stmt (addr);
3720 err |= true;
3723 addr = walk_tree (&t, verify_node_sharing, htab, NULL);
3724 if (addr)
3726 error ("Incorrect sharing of tree nodes");
3727 debug_generic_stmt (phi);
3728 debug_generic_stmt (addr);
3729 err |= true;
3734 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
3736 tree stmt = bsi_stmt (bsi);
3738 if (bb_for_stmt (stmt) != bb)
3740 error ("bb_for_stmt (stmt) is set to a wrong basic block\n");
3741 err |= true;
3744 bsi_next (&bsi);
3745 err |= verify_stmt (stmt, bsi_end_p (bsi));
3746 addr = walk_tree (&stmt, verify_node_sharing, htab, NULL);
3747 if (addr)
3749 error ("Incorrect sharing of tree nodes");
3750 debug_generic_stmt (stmt);
3751 debug_generic_stmt (addr);
3752 err |= true;
3757 if (err)
3758 internal_error ("verify_stmts failed.");
3760 htab_delete (htab);
3761 timevar_pop (TV_TREE_STMT_VERIFY);
3765 /* Verifies that the flow information is OK. */
3767 static int
3768 tree_verify_flow_info (void)
3770 int err = 0;
3771 basic_block bb;
3772 block_stmt_iterator bsi;
3773 tree stmt;
3774 edge e;
3775 edge_iterator ei;
3777 if (ENTRY_BLOCK_PTR->stmt_list)
3779 error ("ENTRY_BLOCK has a statement list associated with it\n");
3780 err = 1;
3783 if (EXIT_BLOCK_PTR->stmt_list)
3785 error ("EXIT_BLOCK has a statement list associated with it\n");
3786 err = 1;
3789 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
3790 if (e->flags & EDGE_FALLTHRU)
3792 error ("Fallthru to exit from bb %d\n", e->src->index);
3793 err = 1;
3796 FOR_EACH_BB (bb)
3798 bool found_ctrl_stmt = false;
3800 stmt = NULL_TREE;
3802 /* Skip labels on the start of basic block. */
3803 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3805 tree prev_stmt = stmt;
3807 stmt = bsi_stmt (bsi);
3809 if (TREE_CODE (stmt) != LABEL_EXPR)
3810 break;
3812 if (prev_stmt && DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
3814 error ("Nonlocal label %s is not first "
3815 "in a sequence of labels in bb %d",
3816 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3817 bb->index);
3818 err = 1;
3821 if (label_to_block (LABEL_EXPR_LABEL (stmt)) != bb)
3823 error ("Label %s to block does not match in bb %d\n",
3824 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3825 bb->index);
3826 err = 1;
3829 if (decl_function_context (LABEL_EXPR_LABEL (stmt))
3830 != current_function_decl)
3832 error ("Label %s has incorrect context in bb %d\n",
3833 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3834 bb->index);
3835 err = 1;
3839 /* Verify that body of basic block BB is free of control flow. */
3840 for (; !bsi_end_p (bsi); bsi_next (&bsi))
3842 tree stmt = bsi_stmt (bsi);
3844 if (found_ctrl_stmt)
3846 error ("Control flow in the middle of basic block %d\n",
3847 bb->index);
3848 err = 1;
3851 if (stmt_ends_bb_p (stmt))
3852 found_ctrl_stmt = true;
3854 if (TREE_CODE (stmt) == LABEL_EXPR)
3856 error ("Label %s in the middle of basic block %d\n",
3857 IDENTIFIER_POINTER (DECL_NAME (LABEL_EXPR_LABEL (stmt))),
3858 bb->index);
3859 err = 1;
3862 bsi = bsi_last (bb);
3863 if (bsi_end_p (bsi))
3864 continue;
3866 stmt = bsi_stmt (bsi);
3868 err |= verify_eh_edges (stmt);
3870 if (is_ctrl_stmt (stmt))
3872 FOR_EACH_EDGE (e, ei, bb->succs)
3873 if (e->flags & EDGE_FALLTHRU)
3875 error ("Fallthru edge after a control statement in bb %d \n",
3876 bb->index);
3877 err = 1;
3881 switch (TREE_CODE (stmt))
3883 case COND_EXPR:
3885 edge true_edge;
3886 edge false_edge;
3887 if (TREE_CODE (COND_EXPR_THEN (stmt)) != GOTO_EXPR
3888 || TREE_CODE (COND_EXPR_ELSE (stmt)) != GOTO_EXPR)
3890 error ("Structured COND_EXPR at the end of bb %d\n", bb->index);
3891 err = 1;
3894 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
3896 if (!true_edge || !false_edge
3897 || !(true_edge->flags & EDGE_TRUE_VALUE)
3898 || !(false_edge->flags & EDGE_FALSE_VALUE)
3899 || (true_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3900 || (false_edge->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL))
3901 || EDGE_COUNT (bb->succs) >= 3)
3903 error ("Wrong outgoing edge flags at end of bb %d\n",
3904 bb->index);
3905 err = 1;
3908 if (!has_label_p (true_edge->dest,
3909 GOTO_DESTINATION (COND_EXPR_THEN (stmt))))
3911 error ("%<then%> label does not match edge at end of bb %d\n",
3912 bb->index);
3913 err = 1;
3916 if (!has_label_p (false_edge->dest,
3917 GOTO_DESTINATION (COND_EXPR_ELSE (stmt))))
3919 error ("%<else%> label does not match edge at end of bb %d\n",
3920 bb->index);
3921 err = 1;
3924 break;
3926 case GOTO_EXPR:
3927 if (simple_goto_p (stmt))
3929 error ("Explicit goto at end of bb %d\n", bb->index);
3930 err = 1;
3932 else
3934 /* FIXME. We should double check that the labels in the
3935 destination blocks have their address taken. */
3936 FOR_EACH_EDGE (e, ei, bb->succs)
3937 if ((e->flags & (EDGE_FALLTHRU | EDGE_TRUE_VALUE
3938 | EDGE_FALSE_VALUE))
3939 || !(e->flags & EDGE_ABNORMAL))
3941 error ("Wrong outgoing edge flags at end of bb %d\n",
3942 bb->index);
3943 err = 1;
3946 break;
3948 case RETURN_EXPR:
3949 if (!single_succ_p (bb)
3950 || (single_succ_edge (bb)->flags
3951 & (EDGE_FALLTHRU | EDGE_ABNORMAL
3952 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
3954 error ("Wrong outgoing edge flags at end of bb %d\n", bb->index);
3955 err = 1;
3957 if (single_succ (bb) != EXIT_BLOCK_PTR)
3959 error ("Return edge does not point to exit in bb %d\n",
3960 bb->index);
3961 err = 1;
3963 break;
3965 case SWITCH_EXPR:
3967 tree prev;
3968 edge e;
3969 size_t i, n;
3970 tree vec;
3972 vec = SWITCH_LABELS (stmt);
3973 n = TREE_VEC_LENGTH (vec);
3975 /* Mark all the destination basic blocks. */
3976 for (i = 0; i < n; ++i)
3978 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
3979 basic_block label_bb = label_to_block (lab);
3981 gcc_assert (!label_bb->aux || label_bb->aux == (void *)1);
3982 label_bb->aux = (void *)1;
3985 /* Verify that the case labels are sorted. */
3986 prev = TREE_VEC_ELT (vec, 0);
3987 for (i = 1; i < n - 1; ++i)
3989 tree c = TREE_VEC_ELT (vec, i);
3990 if (! CASE_LOW (c))
3992 error ("Found default case not at end of case vector");
3993 err = 1;
3994 continue;
3996 if (! tree_int_cst_lt (CASE_LOW (prev), CASE_LOW (c)))
3998 error ("Case labels not sorted:\n ");
3999 print_generic_expr (stderr, prev, 0);
4000 fprintf (stderr," is greater than ");
4001 print_generic_expr (stderr, c, 0);
4002 fprintf (stderr," but comes before it.\n");
4003 err = 1;
4005 prev = c;
4007 if (CASE_LOW (TREE_VEC_ELT (vec, n - 1)))
4009 error ("No default case found at end of case vector");
4010 err = 1;
4013 FOR_EACH_EDGE (e, ei, bb->succs)
4015 if (!e->dest->aux)
4017 error ("Extra outgoing edge %d->%d\n",
4018 bb->index, e->dest->index);
4019 err = 1;
4021 e->dest->aux = (void *)2;
4022 if ((e->flags & (EDGE_FALLTHRU | EDGE_ABNORMAL
4023 | EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
4025 error ("Wrong outgoing edge flags at end of bb %d\n",
4026 bb->index);
4027 err = 1;
4031 /* Check that we have all of them. */
4032 for (i = 0; i < n; ++i)
4034 tree lab = CASE_LABEL (TREE_VEC_ELT (vec, i));
4035 basic_block label_bb = label_to_block (lab);
4037 if (label_bb->aux != (void *)2)
4039 error ("Missing edge %i->%i",
4040 bb->index, label_bb->index);
4041 err = 1;
4045 FOR_EACH_EDGE (e, ei, bb->succs)
4046 e->dest->aux = (void *)0;
4049 default: ;
4053 if (dom_computed[CDI_DOMINATORS] >= DOM_NO_FAST_QUERY)
4054 verify_dominators (CDI_DOMINATORS);
4056 return err;
4060 /* Updates phi nodes after creating a forwarder block joined
4061 by edge FALLTHRU. */
4063 static void
4064 tree_make_forwarder_block (edge fallthru)
4066 edge e;
4067 edge_iterator ei;
4068 basic_block dummy, bb;
4069 tree phi, new_phi, var;
4071 dummy = fallthru->src;
4072 bb = fallthru->dest;
4074 if (single_pred_p (bb))
4075 return;
4077 /* If we redirected a branch we must create new phi nodes at the
4078 start of BB. */
4079 for (phi = phi_nodes (dummy); phi; phi = PHI_CHAIN (phi))
4081 var = PHI_RESULT (phi);
4082 new_phi = create_phi_node (var, bb);
4083 SSA_NAME_DEF_STMT (var) = new_phi;
4084 SET_PHI_RESULT (phi, make_ssa_name (SSA_NAME_VAR (var), phi));
4085 add_phi_arg (new_phi, PHI_RESULT (phi), fallthru);
4088 /* Ensure that the PHI node chain is in the same order. */
4089 set_phi_nodes (bb, phi_reverse (phi_nodes (bb)));
4091 /* Add the arguments we have stored on edges. */
4092 FOR_EACH_EDGE (e, ei, bb->preds)
4094 if (e == fallthru)
4095 continue;
4097 flush_pending_stmts (e);
4102 /* Return true if basic block BB does nothing except pass control
4103 flow to another block and that we can safely insert a label at
4104 the start of the successor block.
4106 As a precondition, we require that BB be not equal to
4107 ENTRY_BLOCK_PTR. */
4109 static bool
4110 tree_forwarder_block_p (basic_block bb, bool phi_wanted)
4112 block_stmt_iterator bsi;
4114 /* BB must have a single outgoing edge. */
4115 if (single_succ_p (bb) != 1
4116 /* If PHI_WANTED is false, BB must not have any PHI nodes.
4117 Otherwise, BB must have PHI nodes. */
4118 || (phi_nodes (bb) != NULL_TREE) != phi_wanted
4119 /* BB may not be a predecessor of EXIT_BLOCK_PTR. */
4120 || single_succ (bb) == EXIT_BLOCK_PTR
4121 /* Nor should this be an infinite loop. */
4122 || single_succ (bb) == bb
4123 /* BB may not have an abnormal outgoing edge. */
4124 || (single_succ_edge (bb)->flags & EDGE_ABNORMAL))
4125 return false;
4127 #if ENABLE_CHECKING
4128 gcc_assert (bb != ENTRY_BLOCK_PTR);
4129 #endif
4131 /* Now walk through the statements backward. We can ignore labels,
4132 anything else means this is not a forwarder block. */
4133 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi))
4135 tree stmt = bsi_stmt (bsi);
4137 switch (TREE_CODE (stmt))
4139 case LABEL_EXPR:
4140 if (DECL_NONLOCAL (LABEL_EXPR_LABEL (stmt)))
4141 return false;
4142 break;
4144 default:
4145 return false;
4149 if (find_edge (ENTRY_BLOCK_PTR, bb))
4150 return false;
4152 if (current_loops)
4154 basic_block dest;
4155 /* Protect loop latches, headers and preheaders. */
4156 if (bb->loop_father->header == bb)
4157 return false;
4158 dest = EDGE_SUCC (bb, 0)->dest;
4160 if (dest->loop_father->header == dest)
4161 return false;
4164 return true;
4167 /* Return true if BB has at least one abnormal incoming edge. */
4169 static inline bool
4170 has_abnormal_incoming_edge_p (basic_block bb)
4172 edge e;
4173 edge_iterator ei;
4175 FOR_EACH_EDGE (e, ei, bb->preds)
4176 if (e->flags & EDGE_ABNORMAL)
4177 return true;
4179 return false;
4182 /* Removes forwarder block BB. Returns false if this failed. If a new
4183 forwarder block is created due to redirection of edges, it is
4184 stored to worklist. */
4186 static bool
4187 remove_forwarder_block (basic_block bb, basic_block **worklist)
4189 edge succ = single_succ_edge (bb), e, s;
4190 basic_block dest = succ->dest;
4191 tree label;
4192 tree phi;
4193 edge_iterator ei;
4194 block_stmt_iterator bsi, bsi_to;
4195 bool seen_abnormal_edge = false;
4197 /* We check for infinite loops already in tree_forwarder_block_p.
4198 However it may happen that the infinite loop is created
4199 afterwards due to removal of forwarders. */
4200 if (dest == bb)
4201 return false;
4203 /* If the destination block consists of a nonlocal label, do not merge
4204 it. */
4205 label = first_stmt (dest);
4206 if (label
4207 && TREE_CODE (label) == LABEL_EXPR
4208 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4209 return false;
4211 /* If there is an abnormal edge to basic block BB, but not into
4212 dest, problems might occur during removal of the phi node at out
4213 of ssa due to overlapping live ranges of registers.
4215 If there is an abnormal edge in DEST, the problems would occur
4216 anyway since cleanup_dead_labels would then merge the labels for
4217 two different eh regions, and rest of exception handling code
4218 does not like it.
4220 So if there is an abnormal edge to BB, proceed only if there is
4221 no abnormal edge to DEST and there are no phi nodes in DEST. */
4222 if (has_abnormal_incoming_edge_p (bb))
4224 seen_abnormal_edge = true;
4226 if (has_abnormal_incoming_edge_p (dest)
4227 || phi_nodes (dest) != NULL_TREE)
4228 return false;
4231 /* If there are phi nodes in DEST, and some of the blocks that are
4232 predecessors of BB are also predecessors of DEST, check that the
4233 phi node arguments match. */
4234 if (phi_nodes (dest))
4236 FOR_EACH_EDGE (e, ei, bb->preds)
4238 s = find_edge (e->src, dest);
4239 if (!s)
4240 continue;
4242 if (!phi_alternatives_equal (dest, succ, s))
4243 return false;
4247 /* Redirect the edges. */
4248 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4250 if (e->flags & EDGE_ABNORMAL)
4252 /* If there is an abnormal edge, redirect it anyway, and
4253 move the labels to the new block to make it legal. */
4254 s = redirect_edge_succ_nodup (e, dest);
4256 else
4257 s = redirect_edge_and_branch (e, dest);
4259 if (s == e)
4261 /* Create arguments for the phi nodes, since the edge was not
4262 here before. */
4263 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4264 add_phi_arg (phi, PHI_ARG_DEF (phi, succ->dest_idx), s);
4266 else
4268 /* The source basic block might become a forwarder. We know
4269 that it was not a forwarder before, since it used to have
4270 at least two outgoing edges, so we may just add it to
4271 worklist. */
4272 if (tree_forwarder_block_p (s->src, false))
4273 *(*worklist)++ = s->src;
4277 if (seen_abnormal_edge)
4279 /* Move the labels to the new block, so that the redirection of
4280 the abnormal edges works. */
4282 bsi_to = bsi_start (dest);
4283 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
4285 label = bsi_stmt (bsi);
4286 gcc_assert (TREE_CODE (label) == LABEL_EXPR);
4287 bsi_remove (&bsi);
4288 bsi_insert_before (&bsi_to, label, BSI_CONTINUE_LINKING);
4292 /* Update the dominators. */
4293 if (dom_info_available_p (CDI_DOMINATORS))
4295 basic_block dom, dombb, domdest;
4297 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4298 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4299 if (domdest == bb)
4301 /* Shortcut to avoid calling (relatively expensive)
4302 nearest_common_dominator unless necessary. */
4303 dom = dombb;
4305 else
4306 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4308 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4311 /* And kill the forwarder block. */
4312 delete_basic_block (bb);
4314 return true;
4317 /* Removes forwarder blocks. */
4319 static bool
4320 cleanup_forwarder_blocks (void)
4322 basic_block bb;
4323 bool changed = false;
4324 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4325 basic_block *current = worklist;
4327 FOR_EACH_BB (bb)
4329 if (tree_forwarder_block_p (bb, false))
4330 *current++ = bb;
4333 while (current != worklist)
4335 bb = *--current;
4336 changed |= remove_forwarder_block (bb, &current);
4339 free (worklist);
4340 return changed;
4343 /* Merge the PHI nodes at BB into those at BB's sole successor. */
4345 static void
4346 remove_forwarder_block_with_phi (basic_block bb)
4348 edge succ = single_succ_edge (bb);
4349 basic_block dest = succ->dest;
4350 tree label;
4351 basic_block dombb, domdest, dom;
4353 /* We check for infinite loops already in tree_forwarder_block_p.
4354 However it may happen that the infinite loop is created
4355 afterwards due to removal of forwarders. */
4356 if (dest == bb)
4357 return;
4359 /* If the destination block consists of a nonlocal label, do not
4360 merge it. */
4361 label = first_stmt (dest);
4362 if (label
4363 && TREE_CODE (label) == LABEL_EXPR
4364 && DECL_NONLOCAL (LABEL_EXPR_LABEL (label)))
4365 return;
4367 /* Redirect each incoming edge to BB to DEST. */
4368 while (EDGE_COUNT (bb->preds) > 0)
4370 edge e = EDGE_PRED (bb, 0), s;
4371 tree phi;
4373 s = find_edge (e->src, dest);
4374 if (s)
4376 /* We already have an edge S from E->src to DEST. If S and
4377 E->dest's sole successor edge have the same PHI arguments
4378 at DEST, redirect S to DEST. */
4379 if (phi_alternatives_equal (dest, s, succ))
4381 e = redirect_edge_and_branch (e, dest);
4382 PENDING_STMT (e) = NULL_TREE;
4383 continue;
4386 /* PHI arguments are different. Create a forwarder block by
4387 splitting E so that we can merge PHI arguments on E to
4388 DEST. */
4389 e = single_succ_edge (split_edge (e));
4392 s = redirect_edge_and_branch (e, dest);
4394 /* redirect_edge_and_branch must not create a new edge. */
4395 gcc_assert (s == e);
4397 /* Add to the PHI nodes at DEST each PHI argument removed at the
4398 destination of E. */
4399 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
4401 tree def = PHI_ARG_DEF (phi, succ->dest_idx);
4403 if (TREE_CODE (def) == SSA_NAME)
4405 tree var;
4407 /* If DEF is one of the results of PHI nodes removed during
4408 redirection, replace it with the PHI argument that used
4409 to be on E. */
4410 for (var = PENDING_STMT (e); var; var = TREE_CHAIN (var))
4412 tree old_arg = TREE_PURPOSE (var);
4413 tree new_arg = TREE_VALUE (var);
4415 if (def == old_arg)
4417 def = new_arg;
4418 break;
4423 add_phi_arg (phi, def, s);
4426 PENDING_STMT (e) = NULL;
4429 /* Update the dominators. */
4430 dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
4431 domdest = get_immediate_dominator (CDI_DOMINATORS, dest);
4432 if (domdest == bb)
4434 /* Shortcut to avoid calling (relatively expensive)
4435 nearest_common_dominator unless necessary. */
4436 dom = dombb;
4438 else
4439 dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb);
4441 set_immediate_dominator (CDI_DOMINATORS, dest, dom);
4443 /* Remove BB since all of BB's incoming edges have been redirected
4444 to DEST. */
4445 delete_basic_block (bb);
4448 /* This pass merges PHI nodes if one feeds into another. For example,
4449 suppose we have the following:
4451 goto <bb 9> (<L9>);
4453 <L8>:;
4454 tem_17 = foo ();
4456 # tem_6 = PHI <tem_17(8), tem_23(7)>;
4457 <L9>:;
4459 # tem_3 = PHI <tem_6(9), tem_2(5)>;
4460 <L10>:;
4462 Then we merge the first PHI node into the second one like so:
4464 goto <bb 9> (<L10>);
4466 <L8>:;
4467 tem_17 = foo ();
4469 # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;
4470 <L10>:;
4473 static void
4474 merge_phi_nodes (void)
4476 basic_block *worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
4477 basic_block *current = worklist;
4478 basic_block bb;
4480 calculate_dominance_info (CDI_DOMINATORS);
4482 /* Find all PHI nodes that we may be able to merge. */
4483 FOR_EACH_BB (bb)
4485 basic_block dest;
4487 /* Look for a forwarder block with PHI nodes. */
4488 if (!tree_forwarder_block_p (bb, true))
4489 continue;
4491 dest = single_succ (bb);
4493 /* We have to feed into another basic block with PHI
4494 nodes. */
4495 if (!phi_nodes (dest)
4496 /* We don't want to deal with a basic block with
4497 abnormal edges. */
4498 || has_abnormal_incoming_edge_p (bb))
4499 continue;
4501 if (!dominated_by_p (CDI_DOMINATORS, dest, bb))
4503 /* If BB does not dominate DEST, then the PHI nodes at
4504 DEST must be the only users of the results of the PHI
4505 nodes at BB. */
4506 *current++ = bb;
4510 /* Now let's drain WORKLIST. */
4511 while (current != worklist)
4513 bb = *--current;
4514 remove_forwarder_block_with_phi (bb);
4517 free (worklist);
4520 static bool
4521 gate_merge_phi (void)
4523 return 1;
4526 struct tree_opt_pass pass_merge_phi = {
4527 "mergephi", /* name */
4528 gate_merge_phi, /* gate */
4529 merge_phi_nodes, /* execute */
4530 NULL, /* sub */
4531 NULL, /* next */
4532 0, /* static_pass_number */
4533 TV_TREE_MERGE_PHI, /* tv_id */
4534 PROP_cfg | PROP_ssa, /* properties_required */
4535 0, /* properties_provided */
4536 0, /* properties_destroyed */
4537 0, /* todo_flags_start */
4538 TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */
4539 | TODO_verify_ssa,
4540 0 /* letter */
4543 /* Return a non-special label in the head of basic block BLOCK.
4544 Create one if it doesn't exist. */
4546 tree
4547 tree_block_label (basic_block bb)
4549 block_stmt_iterator i, s = bsi_start (bb);
4550 bool first = true;
4551 tree label, stmt;
4553 for (i = s; !bsi_end_p (i); first = false, bsi_next (&i))
4555 stmt = bsi_stmt (i);
4556 if (TREE_CODE (stmt) != LABEL_EXPR)
4557 break;
4558 label = LABEL_EXPR_LABEL (stmt);
4559 if (!DECL_NONLOCAL (label))
4561 if (!first)
4562 bsi_move_before (&i, &s);
4563 return label;
4567 label = create_artificial_label ();
4568 stmt = build1 (LABEL_EXPR, void_type_node, label);
4569 bsi_insert_before (&s, stmt, BSI_NEW_STMT);
4570 return label;
4574 /* Attempt to perform edge redirection by replacing a possibly complex
4575 jump instruction by a goto or by removing the jump completely.
4576 This can apply only if all edges now point to the same block. The
4577 parameters and return values are equivalent to
4578 redirect_edge_and_branch. */
4580 static edge
4581 tree_try_redirect_by_replacing_jump (edge e, basic_block target)
4583 basic_block src = e->src;
4584 block_stmt_iterator b;
4585 tree stmt;
4587 /* We can replace or remove a complex jump only when we have exactly
4588 two edges. */
4589 if (EDGE_COUNT (src->succs) != 2
4590 /* Verify that all targets will be TARGET. Specifically, the
4591 edge that is not E must also go to TARGET. */
4592 || EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)
4593 return NULL;
4595 b = bsi_last (src);
4596 if (bsi_end_p (b))
4597 return NULL;
4598 stmt = bsi_stmt (b);
4600 if (TREE_CODE (stmt) == COND_EXPR
4601 || TREE_CODE (stmt) == SWITCH_EXPR)
4603 bsi_remove (&b);
4604 e = ssa_redirect_edge (e, target);
4605 e->flags = EDGE_FALLTHRU;
4606 return e;
4609 return NULL;
4613 /* Redirect E to DEST. Return NULL on failure. Otherwise, return the
4614 edge representing the redirected branch. */
4616 static edge
4617 tree_redirect_edge_and_branch (edge e, basic_block dest)
4619 basic_block bb = e->src;
4620 block_stmt_iterator bsi;
4621 edge ret;
4622 tree label, stmt;
4624 if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
4625 return NULL;
4627 if (e->src != ENTRY_BLOCK_PTR
4628 && (ret = tree_try_redirect_by_replacing_jump (e, dest)))
4629 return ret;
4631 if (e->dest == dest)
4632 return NULL;
4634 label = tree_block_label (dest);
4636 bsi = bsi_last (bb);
4637 stmt = bsi_end_p (bsi) ? NULL : bsi_stmt (bsi);
4639 switch (stmt ? TREE_CODE (stmt) : ERROR_MARK)
4641 case COND_EXPR:
4642 stmt = (e->flags & EDGE_TRUE_VALUE
4643 ? COND_EXPR_THEN (stmt)
4644 : COND_EXPR_ELSE (stmt));
4645 GOTO_DESTINATION (stmt) = label;
4646 break;
4648 case GOTO_EXPR:
4649 /* No non-abnormal edges should lead from a non-simple goto, and
4650 simple ones should be represented implicitly. */
4651 gcc_unreachable ();
4653 case SWITCH_EXPR:
4655 tree cases = get_cases_for_edge (e, stmt);
4657 /* If we have a list of cases associated with E, then use it
4658 as it's a lot faster than walking the entire case vector. */
4659 if (cases)
4661 edge e2 = find_edge (e->src, dest);
4662 tree last, first;
4664 first = cases;
4665 while (cases)
4667 last = cases;
4668 CASE_LABEL (cases) = label;
4669 cases = TREE_CHAIN (cases);
4672 /* If there was already an edge in the CFG, then we need
4673 to move all the cases associated with E to E2. */
4674 if (e2)
4676 tree cases2 = get_cases_for_edge (e2, stmt);
4678 TREE_CHAIN (last) = TREE_CHAIN (cases2);
4679 TREE_CHAIN (cases2) = first;
4682 else
4684 tree vec = SWITCH_LABELS (stmt);
4685 size_t i, n = TREE_VEC_LENGTH (vec);
4687 for (i = 0; i < n; i++)
4689 tree elt = TREE_VEC_ELT (vec, i);
4691 if (label_to_block (CASE_LABEL (elt)) == e->dest)
4692 CASE_LABEL (elt) = label;
4696 break;
4699 case RETURN_EXPR:
4700 bsi_remove (&bsi);
4701 e->flags |= EDGE_FALLTHRU;
4702 break;
4704 default:
4705 /* Otherwise it must be a fallthru edge, and we don't need to
4706 do anything besides redirecting it. */
4707 gcc_assert (e->flags & EDGE_FALLTHRU);
4708 break;
4711 /* Update/insert PHI nodes as necessary. */
4713 /* Now update the edges in the CFG. */
4714 e = ssa_redirect_edge (e, dest);
4716 return e;
4720 /* Simple wrapper, as we can always redirect fallthru edges. */
4722 static basic_block
4723 tree_redirect_edge_and_branch_force (edge e, basic_block dest)
4725 e = tree_redirect_edge_and_branch (e, dest);
4726 gcc_assert (e);
4728 return NULL;
4732 /* Splits basic block BB after statement STMT (but at least after the
4733 labels). If STMT is NULL, BB is split just after the labels. */
4735 static basic_block
4736 tree_split_block (basic_block bb, void *stmt)
4738 block_stmt_iterator bsi, bsi_tgt;
4739 tree act;
4740 basic_block new_bb;
4741 edge e;
4742 edge_iterator ei;
4744 new_bb = create_empty_bb (bb);
4746 /* Redirect the outgoing edges. */
4747 new_bb->succs = bb->succs;
4748 bb->succs = NULL;
4749 FOR_EACH_EDGE (e, ei, new_bb->succs)
4750 e->src = new_bb;
4752 if (stmt && TREE_CODE ((tree) stmt) == LABEL_EXPR)
4753 stmt = NULL;
4755 /* Move everything from BSI to the new basic block. */
4756 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4758 act = bsi_stmt (bsi);
4759 if (TREE_CODE (act) == LABEL_EXPR)
4760 continue;
4762 if (!stmt)
4763 break;
4765 if (stmt == act)
4767 bsi_next (&bsi);
4768 break;
4772 bsi_tgt = bsi_start (new_bb);
4773 while (!bsi_end_p (bsi))
4775 act = bsi_stmt (bsi);
4776 bsi_remove (&bsi);
4777 bsi_insert_after (&bsi_tgt, act, BSI_NEW_STMT);
4780 return new_bb;
4784 /* Moves basic block BB after block AFTER. */
4786 static bool
4787 tree_move_block_after (basic_block bb, basic_block after)
4789 if (bb->prev_bb == after)
4790 return true;
4792 unlink_block (bb);
4793 link_block (bb, after);
4795 return true;
4799 /* Return true if basic_block can be duplicated. */
4801 static bool
4802 tree_can_duplicate_bb_p (basic_block bb ATTRIBUTE_UNUSED)
4804 return true;
4808 /* Create a duplicate of the basic block BB. NOTE: This does not
4809 preserve SSA form. */
4811 static basic_block
4812 tree_duplicate_bb (basic_block bb)
4814 basic_block new_bb;
4815 block_stmt_iterator bsi, bsi_tgt;
4816 tree phi;
4818 new_bb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
4820 /* Copy the PHI nodes. We ignore PHI node arguments here because
4821 the incoming edges have not been setup yet. */
4822 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
4824 tree copy = create_phi_node (PHI_RESULT (phi), new_bb);
4825 create_new_def_for (PHI_RESULT (copy), copy, PHI_RESULT_PTR (copy));
4828 /* Keep the chain of PHI nodes in the same order so that they can be
4829 updated by ssa_redirect_edge. */
4830 set_phi_nodes (new_bb, phi_reverse (phi_nodes (new_bb)));
4832 bsi_tgt = bsi_start (new_bb);
4833 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4835 def_operand_p def_p;
4836 ssa_op_iter op_iter;
4837 tree stmt, copy;
4838 int region;
4840 stmt = bsi_stmt (bsi);
4841 if (TREE_CODE (stmt) == LABEL_EXPR)
4842 continue;
4844 /* Create a new copy of STMT and duplicate STMT's virtual
4845 operands. */
4846 copy = unshare_expr (stmt);
4847 bsi_insert_after (&bsi_tgt, copy, BSI_NEW_STMT);
4848 copy_virtual_operands (copy, stmt);
4849 region = lookup_stmt_eh_region (stmt);
4850 if (region >= 0)
4851 add_stmt_to_eh_region (copy, region);
4853 /* Create new names for all the definitions created by COPY and
4854 add replacement mappings for each new name. */
4855 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
4856 create_new_def_for (DEF_FROM_PTR (def_p), copy, def_p);
4859 return new_bb;
4863 /* Basic block BB_COPY was created by code duplication. Add phi node
4864 arguments for edges going out of BB_COPY. The blocks that were
4865 duplicated have rbi->duplicated set to one. */
4867 void
4868 add_phi_args_after_copy_bb (basic_block bb_copy)
4870 basic_block bb, dest;
4871 edge e, e_copy;
4872 edge_iterator ei;
4873 tree phi, phi_copy, phi_next, def;
4875 bb = bb_copy->rbi->original;
4877 FOR_EACH_EDGE (e_copy, ei, bb_copy->succs)
4879 if (!phi_nodes (e_copy->dest))
4880 continue;
4882 if (e_copy->dest->rbi->duplicated)
4883 dest = e_copy->dest->rbi->original;
4884 else
4885 dest = e_copy->dest;
4887 e = find_edge (bb, dest);
4888 if (!e)
4890 /* During loop unrolling the target of the latch edge is copied.
4891 In this case we are not looking for edge to dest, but to
4892 duplicated block whose original was dest. */
4893 FOR_EACH_EDGE (e, ei, bb->succs)
4894 if (e->dest->rbi->duplicated
4895 && e->dest->rbi->original == dest)
4896 break;
4898 gcc_assert (e != NULL);
4901 for (phi = phi_nodes (e->dest), phi_copy = phi_nodes (e_copy->dest);
4902 phi;
4903 phi = phi_next, phi_copy = PHI_CHAIN (phi_copy))
4905 phi_next = PHI_CHAIN (phi);
4906 def = PHI_ARG_DEF_FROM_EDGE (phi, e);
4907 add_phi_arg (phi_copy, def, e_copy);
4912 /* Blocks in REGION_COPY array of length N_REGION were created by
4913 duplication of basic blocks. Add phi node arguments for edges
4914 going from these blocks. */
4916 void
4917 add_phi_args_after_copy (basic_block *region_copy, unsigned n_region)
4919 unsigned i;
4921 for (i = 0; i < n_region; i++)
4922 region_copy[i]->rbi->duplicated = 1;
4924 for (i = 0; i < n_region; i++)
4925 add_phi_args_after_copy_bb (region_copy[i]);
4927 for (i = 0; i < n_region; i++)
4928 region_copy[i]->rbi->duplicated = 0;
4931 /* Duplicates a REGION (set of N_REGION basic blocks) with just a single
4932 important exit edge EXIT. By important we mean that no SSA name defined
4933 inside region is live over the other exit edges of the region. All entry
4934 edges to the region must go to ENTRY->dest. The edge ENTRY is redirected
4935 to the duplicate of the region. SSA form, dominance and loop information
4936 is updated. The new basic blocks are stored to REGION_COPY in the same
4937 order as they had in REGION, provided that REGION_COPY is not NULL.
4938 The function returns false if it is unable to copy the region,
4939 true otherwise. */
4941 bool
4942 tree_duplicate_sese_region (edge entry, edge exit,
4943 basic_block *region, unsigned n_region,
4944 basic_block *region_copy)
4946 unsigned i, n_doms;
4947 bool free_region_copy = false, copying_header = false;
4948 struct loop *loop = entry->dest->loop_father;
4949 edge exit_copy;
4950 basic_block *doms;
4951 edge redirected;
4952 int total_freq, entry_freq;
4954 if (!can_copy_bbs_p (region, n_region))
4955 return false;
4957 /* Some sanity checking. Note that we do not check for all possible
4958 missuses of the functions. I.e. if you ask to copy something weird,
4959 it will work, but the state of structures probably will not be
4960 correct. */
4961 for (i = 0; i < n_region; i++)
4963 /* We do not handle subloops, i.e. all the blocks must belong to the
4964 same loop. */
4965 if (region[i]->loop_father != loop)
4966 return false;
4968 if (region[i] != entry->dest
4969 && region[i] == loop->header)
4970 return false;
4973 loop->copy = loop;
4975 /* In case the function is used for loop header copying (which is the primary
4976 use), ensure that EXIT and its copy will be new latch and entry edges. */
4977 if (loop->header == entry->dest)
4979 copying_header = true;
4980 loop->copy = loop->outer;
4982 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src))
4983 return false;
4985 for (i = 0; i < n_region; i++)
4986 if (region[i] != exit->src
4987 && dominated_by_p (CDI_DOMINATORS, region[i], exit->src))
4988 return false;
4991 if (!region_copy)
4993 region_copy = xmalloc (sizeof (basic_block) * n_region);
4994 free_region_copy = true;
4997 gcc_assert (!need_ssa_update_p ());
4999 /* Record blocks outside the region that are dominated by something
5000 inside. */
5001 doms = xmalloc (sizeof (basic_block) * n_basic_blocks);
5002 n_doms = get_dominated_by_region (CDI_DOMINATORS, region, n_region, doms);
5004 total_freq = entry->dest->frequency;
5005 entry_freq = EDGE_FREQUENCY (entry);
5006 /* Fix up corner cases, to avoid division by zero or creation of negative
5007 frequencies. */
5008 if (total_freq == 0)
5009 total_freq = 1;
5010 else if (entry_freq > total_freq)
5011 entry_freq = total_freq;
5013 copy_bbs (region, n_region, region_copy, &exit, 1, &exit_copy, loop);
5014 scale_bbs_frequencies_int (region, n_region, total_freq - entry_freq,
5015 total_freq);
5016 scale_bbs_frequencies_int (region_copy, n_region, entry_freq, total_freq);
5018 if (copying_header)
5020 loop->header = exit->dest;
5021 loop->latch = exit->src;
5024 /* Redirect the entry and add the phi node arguments. */
5025 redirected = redirect_edge_and_branch (entry, entry->dest->rbi->copy);
5026 gcc_assert (redirected != NULL);
5027 flush_pending_stmts (entry);
5029 /* Concerning updating of dominators: We must recount dominators
5030 for entry block and its copy. Anything that is outside of the
5031 region, but was dominated by something inside needs recounting as
5032 well. */
5033 set_immediate_dominator (CDI_DOMINATORS, entry->dest, entry->src);
5034 doms[n_doms++] = entry->dest->rbi->original;
5035 iterate_fix_dominators (CDI_DOMINATORS, doms, n_doms);
5036 free (doms);
5038 /* Add the other PHI node arguments. */
5039 add_phi_args_after_copy (region_copy, n_region);
5041 /* Update the SSA web. */
5042 update_ssa (TODO_update_ssa);
5044 if (free_region_copy)
5045 free (region_copy);
5047 return true;
5051 /* Dump FUNCTION_DECL FN to file FILE using FLAGS (see TDF_* in tree.h) */
5053 void
5054 dump_function_to_file (tree fn, FILE *file, int flags)
5056 tree arg, vars, var;
5057 bool ignore_topmost_bind = false, any_var = false;
5058 basic_block bb;
5059 tree chain;
5061 fprintf (file, "%s (", lang_hooks.decl_printable_name (fn, 2));
5063 arg = DECL_ARGUMENTS (fn);
5064 while (arg)
5066 print_generic_expr (file, arg, dump_flags);
5067 if (TREE_CHAIN (arg))
5068 fprintf (file, ", ");
5069 arg = TREE_CHAIN (arg);
5071 fprintf (file, ")\n");
5073 if (flags & TDF_DETAILS)
5074 dump_eh_tree (file, DECL_STRUCT_FUNCTION (fn));
5075 if (flags & TDF_RAW)
5077 dump_node (fn, TDF_SLIM | flags, file);
5078 return;
5081 /* When GIMPLE is lowered, the variables are no longer available in
5082 BIND_EXPRs, so display them separately. */
5083 if (cfun && cfun->decl == fn && cfun->unexpanded_var_list)
5085 ignore_topmost_bind = true;
5087 fprintf (file, "{\n");
5088 for (vars = cfun->unexpanded_var_list; vars; vars = TREE_CHAIN (vars))
5090 var = TREE_VALUE (vars);
5092 print_generic_decl (file, var, flags);
5093 fprintf (file, "\n");
5095 any_var = true;
5099 if (cfun && cfun->decl == fn && cfun->cfg && basic_block_info)
5101 /* Make a CFG based dump. */
5102 check_bb_profile (ENTRY_BLOCK_PTR, file);
5103 if (!ignore_topmost_bind)
5104 fprintf (file, "{\n");
5106 if (any_var && n_basic_blocks)
5107 fprintf (file, "\n");
5109 FOR_EACH_BB (bb)
5110 dump_generic_bb (file, bb, 2, flags);
5112 fprintf (file, "}\n");
5113 check_bb_profile (EXIT_BLOCK_PTR, file);
5115 else
5117 int indent;
5119 /* Make a tree based dump. */
5120 chain = DECL_SAVED_TREE (fn);
5122 if (TREE_CODE (chain) == BIND_EXPR)
5124 if (ignore_topmost_bind)
5126 chain = BIND_EXPR_BODY (chain);
5127 indent = 2;
5129 else
5130 indent = 0;
5132 else
5134 if (!ignore_topmost_bind)
5135 fprintf (file, "{\n");
5136 indent = 2;
5139 if (any_var)
5140 fprintf (file, "\n");
5142 print_generic_stmt_indented (file, chain, flags, indent);
5143 if (ignore_topmost_bind)
5144 fprintf (file, "}\n");
5147 fprintf (file, "\n\n");
5151 /* Pretty print of the loops intermediate representation. */
5152 static void print_loop (FILE *, struct loop *, int);
5153 static void print_pred_bbs (FILE *, basic_block bb);
5154 static void print_succ_bbs (FILE *, basic_block bb);
5157 /* Print the predecessors indexes of edge E on FILE. */
5159 static void
5160 print_pred_bbs (FILE *file, basic_block bb)
5162 edge e;
5163 edge_iterator ei;
5165 FOR_EACH_EDGE (e, ei, bb->preds)
5166 fprintf (file, "bb_%d", e->src->index);
5170 /* Print the successors indexes of edge E on FILE. */
5172 static void
5173 print_succ_bbs (FILE *file, basic_block bb)
5175 edge e;
5176 edge_iterator ei;
5178 FOR_EACH_EDGE (e, ei, bb->succs)
5179 fprintf (file, "bb_%d", e->src->index);
5183 /* Pretty print LOOP on FILE, indented INDENT spaces. */
5185 static void
5186 print_loop (FILE *file, struct loop *loop, int indent)
5188 char *s_indent;
5189 basic_block bb;
5191 if (loop == NULL)
5192 return;
5194 s_indent = (char *) alloca ((size_t) indent + 1);
5195 memset ((void *) s_indent, ' ', (size_t) indent);
5196 s_indent[indent] = '\0';
5198 /* Print the loop's header. */
5199 fprintf (file, "%sloop_%d\n", s_indent, loop->num);
5201 /* Print the loop's body. */
5202 fprintf (file, "%s{\n", s_indent);
5203 FOR_EACH_BB (bb)
5204 if (bb->loop_father == loop)
5206 /* Print the basic_block's header. */
5207 fprintf (file, "%s bb_%d (preds = {", s_indent, bb->index);
5208 print_pred_bbs (file, bb);
5209 fprintf (file, "}, succs = {");
5210 print_succ_bbs (file, bb);
5211 fprintf (file, "})\n");
5213 /* Print the basic_block's body. */
5214 fprintf (file, "%s {\n", s_indent);
5215 tree_dump_bb (bb, file, indent + 4);
5216 fprintf (file, "%s }\n", s_indent);
5219 print_loop (file, loop->inner, indent + 2);
5220 fprintf (file, "%s}\n", s_indent);
5221 print_loop (file, loop->next, indent);
5225 /* Follow a CFG edge from the entry point of the program, and on entry
5226 of a loop, pretty print the loop structure on FILE. */
5228 void
5229 print_loop_ir (FILE *file)
5231 basic_block bb;
5233 bb = BASIC_BLOCK (0);
5234 if (bb && bb->loop_father)
5235 print_loop (file, bb->loop_father, 0);
5239 /* Debugging loops structure at tree level. */
5241 void
5242 debug_loop_ir (void)
5244 print_loop_ir (stderr);
5248 /* Return true if BB ends with a call, possibly followed by some
5249 instructions that must stay with the call. Return false,
5250 otherwise. */
5252 static bool
5253 tree_block_ends_with_call_p (basic_block bb)
5255 block_stmt_iterator bsi = bsi_last (bb);
5256 return get_call_expr_in (bsi_stmt (bsi)) != NULL;
5260 /* Return true if BB ends with a conditional branch. Return false,
5261 otherwise. */
5263 static bool
5264 tree_block_ends_with_condjump_p (basic_block bb)
5266 tree stmt = last_stmt (bb);
5267 return (stmt && TREE_CODE (stmt) == COND_EXPR);
5271 /* Return true if we need to add fake edge to exit at statement T.
5272 Helper function for tree_flow_call_edges_add. */
5274 static bool
5275 need_fake_edge_p (tree t)
5277 tree call;
5279 /* NORETURN and LONGJMP calls already have an edge to exit.
5280 CONST and PURE calls do not need one.
5281 We don't currently check for CONST and PURE here, although
5282 it would be a good idea, because those attributes are
5283 figured out from the RTL in mark_constant_function, and
5284 the counter incrementation code from -fprofile-arcs
5285 leads to different results from -fbranch-probabilities. */
5286 call = get_call_expr_in (t);
5287 if (call
5288 && !(call_expr_flags (call) & ECF_NORETURN))
5289 return true;
5291 if (TREE_CODE (t) == ASM_EXPR
5292 && (ASM_VOLATILE_P (t) || ASM_INPUT_P (t)))
5293 return true;
5295 return false;
5299 /* Add fake edges to the function exit for any non constant and non
5300 noreturn calls, volatile inline assembly in the bitmap of blocks
5301 specified by BLOCKS or to the whole CFG if BLOCKS is zero. Return
5302 the number of blocks that were split.
5304 The goal is to expose cases in which entering a basic block does
5305 not imply that all subsequent instructions must be executed. */
5307 static int
5308 tree_flow_call_edges_add (sbitmap blocks)
5310 int i;
5311 int blocks_split = 0;
5312 int last_bb = last_basic_block;
5313 bool check_last_block = false;
5315 if (n_basic_blocks == 0)
5316 return 0;
5318 if (! blocks)
5319 check_last_block = true;
5320 else
5321 check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index);
5323 /* In the last basic block, before epilogue generation, there will be
5324 a fallthru edge to EXIT. Special care is required if the last insn
5325 of the last basic block is a call because make_edge folds duplicate
5326 edges, which would result in the fallthru edge also being marked
5327 fake, which would result in the fallthru edge being removed by
5328 remove_fake_edges, which would result in an invalid CFG.
5330 Moreover, we can't elide the outgoing fake edge, since the block
5331 profiler needs to take this into account in order to solve the minimal
5332 spanning tree in the case that the call doesn't return.
5334 Handle this by adding a dummy instruction in a new last basic block. */
5335 if (check_last_block)
5337 basic_block bb = EXIT_BLOCK_PTR->prev_bb;
5338 block_stmt_iterator bsi = bsi_last (bb);
5339 tree t = NULL_TREE;
5340 if (!bsi_end_p (bsi))
5341 t = bsi_stmt (bsi);
5343 if (need_fake_edge_p (t))
5345 edge e;
5347 e = find_edge (bb, EXIT_BLOCK_PTR);
5348 if (e)
5350 bsi_insert_on_edge (e, build_empty_stmt ());
5351 bsi_commit_edge_inserts ();
5356 /* Now add fake edges to the function exit for any non constant
5357 calls since there is no way that we can determine if they will
5358 return or not... */
5359 for (i = 0; i < last_bb; i++)
5361 basic_block bb = BASIC_BLOCK (i);
5362 block_stmt_iterator bsi;
5363 tree stmt, last_stmt;
5365 if (!bb)
5366 continue;
5368 if (blocks && !TEST_BIT (blocks, i))
5369 continue;
5371 bsi = bsi_last (bb);
5372 if (!bsi_end_p (bsi))
5374 last_stmt = bsi_stmt (bsi);
5377 stmt = bsi_stmt (bsi);
5378 if (need_fake_edge_p (stmt))
5380 edge e;
5381 /* The handling above of the final block before the
5382 epilogue should be enough to verify that there is
5383 no edge to the exit block in CFG already.
5384 Calling make_edge in such case would cause us to
5385 mark that edge as fake and remove it later. */
5386 #ifdef ENABLE_CHECKING
5387 if (stmt == last_stmt)
5389 e = find_edge (bb, EXIT_BLOCK_PTR);
5390 gcc_assert (e == NULL);
5392 #endif
5394 /* Note that the following may create a new basic block
5395 and renumber the existing basic blocks. */
5396 if (stmt != last_stmt)
5398 e = split_block (bb, stmt);
5399 if (e)
5400 blocks_split++;
5402 make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE);
5404 bsi_prev (&bsi);
5406 while (!bsi_end_p (bsi));
5410 if (blocks_split)
5411 verify_flow_info ();
5413 return blocks_split;
5416 bool
5417 tree_purge_dead_eh_edges (basic_block bb)
5419 bool changed = false;
5420 edge e;
5421 edge_iterator ei;
5422 tree stmt = last_stmt (bb);
5424 if (stmt && tree_can_throw_internal (stmt))
5425 return false;
5427 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
5429 if (e->flags & EDGE_EH)
5431 remove_edge (e);
5432 changed = true;
5434 else
5435 ei_next (&ei);
5438 /* Removal of dead EH edges might change dominators of not
5439 just immediate successors. E.g. when bb1 is changed so that
5440 it no longer can throw and bb1->bb3 and bb1->bb4 are dead
5441 eh edges purged by this function in:
5445 1-->2
5446 / \ |
5447 v v |
5448 3-->4 |
5450 --->5
5453 idom(bb5) must be recomputed. For now just free the dominance
5454 info. */
5455 if (changed)
5456 free_dominance_info (CDI_DOMINATORS);
5458 return changed;
5461 bool
5462 tree_purge_all_dead_eh_edges (bitmap blocks)
5464 bool changed = false;
5465 unsigned i;
5466 bitmap_iterator bi;
5468 EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
5470 changed |= tree_purge_dead_eh_edges (BASIC_BLOCK (i));
5473 return changed;
5476 /* This function is called whenever a new edge is created or
5477 redirected. */
5479 static void
5480 tree_execute_on_growing_pred (edge e)
5482 basic_block bb = e->dest;
5484 if (phi_nodes (bb))
5485 reserve_phi_args_for_new_edge (bb);
5488 /* This function is called immediately before edge E is removed from
5489 the edge vector E->dest->preds. */
5491 static void
5492 tree_execute_on_shrinking_pred (edge e)
5494 if (phi_nodes (e->dest))
5495 remove_phi_args (e);
5498 /*---------------------------------------------------------------------------
5499 Helper functions for Loop versioning
5500 ---------------------------------------------------------------------------*/
5502 /* Adjust phi nodes for 'first' basic block. 'second' basic block is a copy
5503 of 'first'. Both of them are dominated by 'new_head' basic block. When
5504 'new_head' was created by 'second's incoming edge it received phi arguments
5505 on the edge by split_edge(). Later, additional edge 'e' was created to
5506 connect 'new_head' and 'first'. Now this routine adds phi args on this
5507 additional edge 'e' that new_head to second edge received as part of edge
5508 splitting.
5511 static void
5512 tree_lv_adjust_loop_header_phi (basic_block first, basic_block second,
5513 basic_block new_head, edge e)
5515 tree phi1, phi2;
5516 edge e2 = find_edge (new_head, second);
5518 /* Because NEW_HEAD has been created by splitting SECOND's incoming
5519 edge, we should always have an edge from NEW_HEAD to SECOND. */
5520 gcc_assert (e2 != NULL);
5522 /* Browse all 'second' basic block phi nodes and add phi args to
5523 edge 'e' for 'first' head. PHI args are always in correct order. */
5525 for (phi2 = phi_nodes (second), phi1 = phi_nodes (first);
5526 phi2 && phi1;
5527 phi2 = PHI_CHAIN (phi2), phi1 = PHI_CHAIN (phi1))
5529 tree def = PHI_ARG_DEF (phi2, e2->dest_idx);
5530 add_phi_arg (phi1, def, e);
5534 /* Adds a if else statement to COND_BB with condition COND_EXPR.
5535 SECOND_HEAD is the destination of the THEN and FIRST_HEAD is
5536 the destination of the ELSE part. */
5537 static void
5538 tree_lv_add_condition_to_bb (basic_block first_head, basic_block second_head,
5539 basic_block cond_bb, void *cond_e)
5541 block_stmt_iterator bsi;
5542 tree goto1 = NULL_TREE;
5543 tree goto2 = NULL_TREE;
5544 tree new_cond_expr = NULL_TREE;
5545 tree cond_expr = (tree) cond_e;
5546 edge e0;
5548 /* Build new conditional expr */
5549 goto1 = build1 (GOTO_EXPR, void_type_node, tree_block_label (first_head));
5550 goto2 = build1 (GOTO_EXPR, void_type_node, tree_block_label (second_head));
5551 new_cond_expr = build3 (COND_EXPR, void_type_node, cond_expr, goto1, goto2);
5553 /* Add new cond in cond_bb. */
5554 bsi = bsi_start (cond_bb);
5555 bsi_insert_after (&bsi, new_cond_expr, BSI_NEW_STMT);
5556 /* Adjust edges appropriately to connect new head with first head
5557 as well as second head. */
5558 e0 = single_succ_edge (cond_bb);
5559 e0->flags &= ~EDGE_FALLTHRU;
5560 e0->flags |= EDGE_FALSE_VALUE;
5563 struct cfg_hooks tree_cfg_hooks = {
5564 "tree",
5565 tree_verify_flow_info,
5566 tree_dump_bb, /* dump_bb */
5567 create_bb, /* create_basic_block */
5568 tree_redirect_edge_and_branch,/* redirect_edge_and_branch */
5569 tree_redirect_edge_and_branch_force,/* redirect_edge_and_branch_force */
5570 remove_bb, /* delete_basic_block */
5571 tree_split_block, /* split_block */
5572 tree_move_block_after, /* move_block_after */
5573 tree_can_merge_blocks_p, /* can_merge_blocks_p */
5574 tree_merge_blocks, /* merge_blocks */
5575 tree_predict_edge, /* predict_edge */
5576 tree_predicted_by_p, /* predicted_by_p */
5577 tree_can_duplicate_bb_p, /* can_duplicate_block_p */
5578 tree_duplicate_bb, /* duplicate_block */
5579 tree_split_edge, /* split_edge */
5580 tree_make_forwarder_block, /* make_forward_block */
5581 NULL, /* tidy_fallthru_edge */
5582 tree_block_ends_with_call_p, /* block_ends_with_call_p */
5583 tree_block_ends_with_condjump_p, /* block_ends_with_condjump_p */
5584 tree_flow_call_edges_add, /* flow_call_edges_add */
5585 tree_execute_on_growing_pred, /* execute_on_growing_pred */
5586 tree_execute_on_shrinking_pred, /* execute_on_shrinking_pred */
5587 tree_duplicate_loop_to_header_edge, /* duplicate loop for trees */
5588 tree_lv_add_condition_to_bb, /* lv_add_condition_to_bb */
5589 tree_lv_adjust_loop_header_phi, /* lv_adjust_loop_header_phi*/
5590 extract_true_false_edges_from_block, /* extract_cond_bb_edges */
5591 flush_pending_stmts /* flush_pending_stmts */
5595 /* Split all critical edges. */
5597 static void
5598 split_critical_edges (void)
5600 basic_block bb;
5601 edge e;
5602 edge_iterator ei;
5604 /* split_edge can redirect edges out of SWITCH_EXPRs, which can get
5605 expensive. So we want to enable recording of edge to CASE_LABEL_EXPR
5606 mappings around the calls to split_edge. */
5607 start_recording_case_labels ();
5608 FOR_ALL_BB (bb)
5610 FOR_EACH_EDGE (e, ei, bb->succs)
5611 if (EDGE_CRITICAL_P (e) && !(e->flags & EDGE_ABNORMAL))
5613 split_edge (e);
5616 end_recording_case_labels ();
5619 struct tree_opt_pass pass_split_crit_edges =
5621 "crited", /* name */
5622 NULL, /* gate */
5623 split_critical_edges, /* execute */
5624 NULL, /* sub */
5625 NULL, /* next */
5626 0, /* static_pass_number */
5627 TV_TREE_SPLIT_EDGES, /* tv_id */
5628 PROP_cfg, /* properties required */
5629 PROP_no_crit_edges, /* properties_provided */
5630 0, /* properties_destroyed */
5631 0, /* todo_flags_start */
5632 TODO_dump_func, /* todo_flags_finish */
5633 0 /* letter */
5637 /* Return EXP if it is a valid GIMPLE rvalue, else gimplify it into
5638 a temporary, make sure and register it to be renamed if necessary,
5639 and finally return the temporary. Put the statements to compute
5640 EXP before the current statement in BSI. */
5642 tree
5643 gimplify_val (block_stmt_iterator *bsi, tree type, tree exp)
5645 tree t, new_stmt, orig_stmt;
5647 if (is_gimple_val (exp))
5648 return exp;
5650 t = make_rename_temp (type, NULL);
5651 new_stmt = build (MODIFY_EXPR, type, t, exp);
5653 orig_stmt = bsi_stmt (*bsi);
5654 SET_EXPR_LOCUS (new_stmt, EXPR_LOCUS (orig_stmt));
5655 TREE_BLOCK (new_stmt) = TREE_BLOCK (orig_stmt);
5657 bsi_insert_before (bsi, new_stmt, BSI_SAME_STMT);
5659 return t;
5662 /* Build a ternary operation and gimplify it. Emit code before BSI.
5663 Return the gimple_val holding the result. */
5665 tree
5666 gimplify_build3 (block_stmt_iterator *bsi, enum tree_code code,
5667 tree type, tree a, tree b, tree c)
5669 tree ret;
5671 ret = fold (build3 (code, type, a, b, c));
5672 STRIP_NOPS (ret);
5674 return gimplify_val (bsi, type, ret);
5677 /* Build a binary operation and gimplify it. Emit code before BSI.
5678 Return the gimple_val holding the result. */
5680 tree
5681 gimplify_build2 (block_stmt_iterator *bsi, enum tree_code code,
5682 tree type, tree a, tree b)
5684 tree ret;
5686 ret = fold (build2 (code, type, a, b));
5687 STRIP_NOPS (ret);
5689 return gimplify_val (bsi, type, ret);
5692 /* Build a unary operation and gimplify it. Emit code before BSI.
5693 Return the gimple_val holding the result. */
5695 tree
5696 gimplify_build1 (block_stmt_iterator *bsi, enum tree_code code, tree type,
5697 tree a)
5699 tree ret;
5701 ret = fold (build1 (code, type, a));
5702 STRIP_NOPS (ret);
5704 return gimplify_val (bsi, type, ret);
5709 /* Emit return warnings. */
5711 static void
5712 execute_warn_function_return (void)
5714 #ifdef USE_MAPPED_LOCATION
5715 source_location location;
5716 #else
5717 location_t *locus;
5718 #endif
5719 tree last;
5720 edge e;
5721 edge_iterator ei;
5723 /* If we have a path to EXIT, then we do return. */
5724 if (TREE_THIS_VOLATILE (cfun->decl)
5725 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0)
5727 #ifdef USE_MAPPED_LOCATION
5728 location = UNKNOWN_LOCATION;
5729 #else
5730 locus = NULL;
5731 #endif
5732 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5734 last = last_stmt (e->src);
5735 if (TREE_CODE (last) == RETURN_EXPR
5736 #ifdef USE_MAPPED_LOCATION
5737 && (location = EXPR_LOCATION (last)) != UNKNOWN_LOCATION)
5738 #else
5739 && (locus = EXPR_LOCUS (last)) != NULL)
5740 #endif
5741 break;
5743 #ifdef USE_MAPPED_LOCATION
5744 if (location == UNKNOWN_LOCATION)
5745 location = cfun->function_end_locus;
5746 warning (0, "%H%<noreturn%> function does return", &location);
5747 #else
5748 if (!locus)
5749 locus = &cfun->function_end_locus;
5750 warning (0, "%H%<noreturn%> function does return", locus);
5751 #endif
5754 /* If we see "return;" in some basic block, then we do reach the end
5755 without returning a value. */
5756 else if (warn_return_type
5757 && !TREE_NO_WARNING (cfun->decl)
5758 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) > 0
5759 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (cfun->decl))))
5761 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
5763 tree last = last_stmt (e->src);
5764 if (TREE_CODE (last) == RETURN_EXPR
5765 && TREE_OPERAND (last, 0) == NULL)
5767 #ifdef USE_MAPPED_LOCATION
5768 location = EXPR_LOCATION (last);
5769 if (location == UNKNOWN_LOCATION)
5770 location = cfun->function_end_locus;
5771 warning (0, "%Hcontrol reaches end of non-void function", &location);
5772 #else
5773 locus = EXPR_LOCUS (last);
5774 if (!locus)
5775 locus = &cfun->function_end_locus;
5776 warning (0, "%Hcontrol reaches end of non-void function", locus);
5777 #endif
5778 TREE_NO_WARNING (cfun->decl) = 1;
5779 break;
5786 /* Given a basic block B which ends with a conditional and has
5787 precisely two successors, determine which of the edges is taken if
5788 the conditional is true and which is taken if the conditional is
5789 false. Set TRUE_EDGE and FALSE_EDGE appropriately. */
5791 void
5792 extract_true_false_edges_from_block (basic_block b,
5793 edge *true_edge,
5794 edge *false_edge)
5796 edge e = EDGE_SUCC (b, 0);
5798 if (e->flags & EDGE_TRUE_VALUE)
5800 *true_edge = e;
5801 *false_edge = EDGE_SUCC (b, 1);
5803 else
5805 *false_edge = e;
5806 *true_edge = EDGE_SUCC (b, 1);
5810 struct tree_opt_pass pass_warn_function_return =
5812 NULL, /* name */
5813 NULL, /* gate */
5814 execute_warn_function_return, /* execute */
5815 NULL, /* sub */
5816 NULL, /* next */
5817 0, /* static_pass_number */
5818 0, /* tv_id */
5819 PROP_cfg, /* properties_required */
5820 0, /* properties_provided */
5821 0, /* properties_destroyed */
5822 0, /* todo_flags_start */
5823 0, /* todo_flags_finish */
5824 0 /* letter */
5827 /* Emit noreturn warnings. */
5829 static void
5830 execute_warn_function_noreturn (void)
5832 if (warn_missing_noreturn
5833 && !TREE_THIS_VOLATILE (cfun->decl)
5834 && EDGE_COUNT (EXIT_BLOCK_PTR->preds) == 0
5835 && !lang_hooks.function.missing_noreturn_ok_p (cfun->decl))
5836 warning (0, "%Jfunction might be possible candidate for "
5837 "attribute %<noreturn%>",
5838 cfun->decl);
5841 struct tree_opt_pass pass_warn_function_noreturn =
5843 NULL, /* name */
5844 NULL, /* gate */
5845 execute_warn_function_noreturn, /* execute */
5846 NULL, /* sub */
5847 NULL, /* next */
5848 0, /* static_pass_number */
5849 0, /* tv_id */
5850 PROP_cfg, /* properties_required */
5851 0, /* properties_provided */
5852 0, /* properties_destroyed */
5853 0, /* todo_flags_start */
5854 0, /* todo_flags_finish */
5855 0 /* letter */