Mark ChangeLog
[official-gcc.git] / gcc / tree-eh.c
blob902ce452791c601b7448fddc7eaeb755addef490
1 /* Exception handling semantics and decomposition for trees.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "function.h"
27 #include "except.h"
28 #include "pointer-set.h"
29 #include "tree-flow.h"
30 #include "tree-inline.h"
31 #include "tree-pass.h"
32 #include "langhooks.h"
33 #include "ggc.h"
34 #include "diagnostic-core.h"
35 #include "gimple.h"
36 #include "target.h"
37 #include "cfgloop.h"
39 /* In some instances a tree and a gimple need to be stored in a same table,
40 i.e. in hash tables. This is a structure to do this. */
41 typedef union {tree *tp; tree t; gimple g;} treemple;
43 /* Nonzero if we are using EH to handle cleanups. */
44 static int using_eh_for_cleanups_p = 0;
46 void
47 using_eh_for_cleanups (void)
49 using_eh_for_cleanups_p = 1;
52 /* Misc functions used in this file. */
54 /* Remember and lookup EH landing pad data for arbitrary statements.
55 Really this means any statement that could_throw_p. We could
56 stuff this information into the stmt_ann data structure, but:
58 (1) We absolutely rely on this information being kept until
59 we get to rtl. Once we're done with lowering here, if we lose
60 the information there's no way to recover it!
62 (2) There are many more statements that *cannot* throw as
63 compared to those that can. We should be saving some amount
64 of space by only allocating memory for those that can throw. */
66 /* Add statement T in function IFUN to landing pad NUM. */
68 void
69 add_stmt_to_eh_lp_fn (struct function *ifun, gimple t, int num)
71 struct throw_stmt_node *n;
72 void **slot;
74 gcc_assert (num != 0);
76 n = ggc_alloc_throw_stmt_node ();
77 n->stmt = t;
78 n->lp_nr = num;
80 if (!get_eh_throw_stmt_table (ifun))
81 set_eh_throw_stmt_table (ifun, htab_create_ggc (31, struct_ptr_hash,
82 struct_ptr_eq,
83 ggc_free));
85 slot = htab_find_slot (get_eh_throw_stmt_table (ifun), n, INSERT);
86 gcc_assert (!*slot);
87 *slot = n;
90 /* Add statement T in the current function (cfun) to EH landing pad NUM. */
92 void
93 add_stmt_to_eh_lp (gimple t, int num)
95 add_stmt_to_eh_lp_fn (cfun, t, num);
98 /* Add statement T to the single EH landing pad in REGION. */
100 static void
101 record_stmt_eh_region (eh_region region, gimple t)
103 if (region == NULL)
104 return;
105 if (region->type == ERT_MUST_NOT_THROW)
106 add_stmt_to_eh_lp_fn (cfun, t, -region->index);
107 else
109 eh_landing_pad lp = region->landing_pads;
110 if (lp == NULL)
111 lp = gen_eh_landing_pad (region);
112 else
113 gcc_assert (lp->next_lp == NULL);
114 add_stmt_to_eh_lp_fn (cfun, t, lp->index);
119 /* Remove statement T in function IFUN from its EH landing pad. */
121 bool
122 remove_stmt_from_eh_lp_fn (struct function *ifun, gimple t)
124 struct throw_stmt_node dummy;
125 void **slot;
127 if (!get_eh_throw_stmt_table (ifun))
128 return false;
130 dummy.stmt = t;
131 slot = htab_find_slot (get_eh_throw_stmt_table (ifun), &dummy,
132 NO_INSERT);
133 if (slot)
135 htab_clear_slot (get_eh_throw_stmt_table (ifun), slot);
136 return true;
138 else
139 return false;
143 /* Remove statement T in the current function (cfun) from its
144 EH landing pad. */
146 bool
147 remove_stmt_from_eh_lp (gimple t)
149 return remove_stmt_from_eh_lp_fn (cfun, t);
152 /* Determine if statement T is inside an EH region in function IFUN.
153 Positive numbers indicate a landing pad index; negative numbers
154 indicate a MUST_NOT_THROW region index; zero indicates that the
155 statement is not recorded in the region table. */
158 lookup_stmt_eh_lp_fn (struct function *ifun, gimple t)
160 struct throw_stmt_node *p, n;
162 if (ifun->eh->throw_stmt_table == NULL)
163 return 0;
165 n.stmt = t;
166 p = (struct throw_stmt_node *) htab_find (ifun->eh->throw_stmt_table, &n);
167 return p ? p->lp_nr : 0;
170 /* Likewise, but always use the current function. */
173 lookup_stmt_eh_lp (gimple t)
175 /* We can get called from initialized data when -fnon-call-exceptions
176 is on; prevent crash. */
177 if (!cfun)
178 return 0;
179 return lookup_stmt_eh_lp_fn (cfun, t);
182 /* First pass of EH node decomposition. Build up a tree of GIMPLE_TRY_FINALLY
183 nodes and LABEL_DECL nodes. We will use this during the second phase to
184 determine if a goto leaves the body of a TRY_FINALLY_EXPR node. */
186 struct finally_tree_node
188 /* When storing a GIMPLE_TRY, we have to record a gimple. However
189 when deciding whether a GOTO to a certain LABEL_DECL (which is a
190 tree) leaves the TRY block, its necessary to record a tree in
191 this field. Thus a treemple is used. */
192 treemple child;
193 gimple parent;
196 /* Note that this table is *not* marked GTY. It is short-lived. */
197 static htab_t finally_tree;
199 static void
200 record_in_finally_tree (treemple child, gimple parent)
202 struct finally_tree_node *n;
203 void **slot;
205 n = XNEW (struct finally_tree_node);
206 n->child = child;
207 n->parent = parent;
209 slot = htab_find_slot (finally_tree, n, INSERT);
210 gcc_assert (!*slot);
211 *slot = n;
214 static void
215 collect_finally_tree (gimple stmt, gimple region);
217 /* Go through the gimple sequence. Works with collect_finally_tree to
218 record all GIMPLE_LABEL and GIMPLE_TRY statements. */
220 static void
221 collect_finally_tree_1 (gimple_seq seq, gimple region)
223 gimple_stmt_iterator gsi;
225 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
226 collect_finally_tree (gsi_stmt (gsi), region);
229 static void
230 collect_finally_tree (gimple stmt, gimple region)
232 treemple temp;
234 switch (gimple_code (stmt))
236 case GIMPLE_LABEL:
237 temp.t = gimple_label_label (stmt);
238 record_in_finally_tree (temp, region);
239 break;
241 case GIMPLE_TRY:
242 if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
244 temp.g = stmt;
245 record_in_finally_tree (temp, region);
246 collect_finally_tree_1 (gimple_try_eval (stmt), stmt);
247 collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
249 else if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
251 collect_finally_tree_1 (gimple_try_eval (stmt), region);
252 collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
254 break;
256 case GIMPLE_CATCH:
257 collect_finally_tree_1 (gimple_catch_handler (stmt), region);
258 break;
260 case GIMPLE_EH_FILTER:
261 collect_finally_tree_1 (gimple_eh_filter_failure (stmt), region);
262 break;
264 case GIMPLE_EH_ELSE:
265 collect_finally_tree_1 (gimple_eh_else_n_body (stmt), region);
266 collect_finally_tree_1 (gimple_eh_else_e_body (stmt), region);
267 break;
269 default:
270 /* A type, a decl, or some kind of statement that we're not
271 interested in. Don't walk them. */
272 break;
277 /* Use the finally tree to determine if a jump from START to TARGET
278 would leave the try_finally node that START lives in. */
280 static bool
281 outside_finally_tree (treemple start, gimple target)
283 struct finally_tree_node n, *p;
287 n.child = start;
288 p = (struct finally_tree_node *) htab_find (finally_tree, &n);
289 if (!p)
290 return true;
291 start.g = p->parent;
293 while (start.g != target);
295 return false;
298 /* Second pass of EH node decomposition. Actually transform the GIMPLE_TRY
299 nodes into a set of gotos, magic labels, and eh regions.
300 The eh region creation is straight-forward, but frobbing all the gotos
301 and such into shape isn't. */
303 /* The sequence into which we record all EH stuff. This will be
304 placed at the end of the function when we're all done. */
305 static gimple_seq eh_seq;
307 /* Record whether an EH region contains something that can throw,
308 indexed by EH region number. */
309 static bitmap eh_region_may_contain_throw_map;
311 /* The GOTO_QUEUE is is an array of GIMPLE_GOTO and GIMPLE_RETURN
312 statements that are seen to escape this GIMPLE_TRY_FINALLY node.
313 The idea is to record a gimple statement for everything except for
314 the conditionals, which get their labels recorded. Since labels are
315 of type 'tree', we need this node to store both gimple and tree
316 objects. REPL_STMT is the sequence used to replace the goto/return
317 statement. CONT_STMT is used to store the statement that allows
318 the return/goto to jump to the original destination. */
320 struct goto_queue_node
322 treemple stmt;
323 location_t location;
324 gimple_seq repl_stmt;
325 gimple cont_stmt;
326 int index;
327 /* This is used when index >= 0 to indicate that stmt is a label (as
328 opposed to a goto stmt). */
329 int is_label;
332 /* State of the world while lowering. */
334 struct leh_state
336 /* What's "current" while constructing the eh region tree. These
337 correspond to variables of the same name in cfun->eh, which we
338 don't have easy access to. */
339 eh_region cur_region;
341 /* What's "current" for the purposes of __builtin_eh_pointer. For
342 a CATCH, this is the associated TRY. For an EH_FILTER, this is
343 the associated ALLOWED_EXCEPTIONS, etc. */
344 eh_region ehp_region;
346 /* Processing of TRY_FINALLY requires a bit more state. This is
347 split out into a separate structure so that we don't have to
348 copy so much when processing other nodes. */
349 struct leh_tf_state *tf;
352 struct leh_tf_state
354 /* Pointer to the GIMPLE_TRY_FINALLY node under discussion. The
355 try_finally_expr is the original GIMPLE_TRY_FINALLY. We need to retain
356 this so that outside_finally_tree can reliably reference the tree used
357 in the collect_finally_tree data structures. */
358 gimple try_finally_expr;
359 gimple top_p;
361 /* While lowering a top_p usually it is expanded into multiple statements,
362 thus we need the following field to store them. */
363 gimple_seq top_p_seq;
365 /* The state outside this try_finally node. */
366 struct leh_state *outer;
368 /* The exception region created for it. */
369 eh_region region;
371 /* The goto queue. */
372 struct goto_queue_node *goto_queue;
373 size_t goto_queue_size;
374 size_t goto_queue_active;
376 /* Pointer map to help in searching goto_queue when it is large. */
377 struct pointer_map_t *goto_queue_map;
379 /* The set of unique labels seen as entries in the goto queue. */
380 vec<tree> dest_array;
382 /* A label to be added at the end of the completed transformed
383 sequence. It will be set if may_fallthru was true *at one time*,
384 though subsequent transformations may have cleared that flag. */
385 tree fallthru_label;
387 /* True if it is possible to fall out the bottom of the try block.
388 Cleared if the fallthru is converted to a goto. */
389 bool may_fallthru;
391 /* True if any entry in goto_queue is a GIMPLE_RETURN. */
392 bool may_return;
394 /* True if the finally block can receive an exception edge.
395 Cleared if the exception case is handled by code duplication. */
396 bool may_throw;
399 static gimple_seq lower_eh_must_not_throw (struct leh_state *, gimple);
401 /* Search for STMT in the goto queue. Return the replacement,
402 or null if the statement isn't in the queue. */
404 #define LARGE_GOTO_QUEUE 20
406 static void lower_eh_constructs_1 (struct leh_state *state, gimple_seq *seq);
408 static gimple_seq
409 find_goto_replacement (struct leh_tf_state *tf, treemple stmt)
411 unsigned int i;
412 void **slot;
414 if (tf->goto_queue_active < LARGE_GOTO_QUEUE)
416 for (i = 0; i < tf->goto_queue_active; i++)
417 if ( tf->goto_queue[i].stmt.g == stmt.g)
418 return tf->goto_queue[i].repl_stmt;
419 return NULL;
422 /* If we have a large number of entries in the goto_queue, create a
423 pointer map and use that for searching. */
425 if (!tf->goto_queue_map)
427 tf->goto_queue_map = pointer_map_create ();
428 for (i = 0; i < tf->goto_queue_active; i++)
430 slot = pointer_map_insert (tf->goto_queue_map,
431 tf->goto_queue[i].stmt.g);
432 gcc_assert (*slot == NULL);
433 *slot = &tf->goto_queue[i];
437 slot = pointer_map_contains (tf->goto_queue_map, stmt.g);
438 if (slot != NULL)
439 return (((struct goto_queue_node *) *slot)->repl_stmt);
441 return NULL;
444 /* A subroutine of replace_goto_queue_1. Handles the sub-clauses of a
445 lowered GIMPLE_COND. If, by chance, the replacement is a simple goto,
446 then we can just splat it in, otherwise we add the new stmts immediately
447 after the GIMPLE_COND and redirect. */
449 static void
450 replace_goto_queue_cond_clause (tree *tp, struct leh_tf_state *tf,
451 gimple_stmt_iterator *gsi)
453 tree label;
454 gimple_seq new_seq;
455 treemple temp;
456 location_t loc = gimple_location (gsi_stmt (*gsi));
458 temp.tp = tp;
459 new_seq = find_goto_replacement (tf, temp);
460 if (!new_seq)
461 return;
463 if (gimple_seq_singleton_p (new_seq)
464 && gimple_code (gimple_seq_first_stmt (new_seq)) == GIMPLE_GOTO)
466 *tp = gimple_goto_dest (gimple_seq_first_stmt (new_seq));
467 return;
470 label = create_artificial_label (loc);
471 /* Set the new label for the GIMPLE_COND */
472 *tp = label;
474 gsi_insert_after (gsi, gimple_build_label (label), GSI_CONTINUE_LINKING);
475 gsi_insert_seq_after (gsi, gimple_seq_copy (new_seq), GSI_CONTINUE_LINKING);
478 /* The real work of replace_goto_queue. Returns with TSI updated to
479 point to the next statement. */
481 static void replace_goto_queue_stmt_list (gimple_seq *, struct leh_tf_state *);
483 static void
484 replace_goto_queue_1 (gimple stmt, struct leh_tf_state *tf,
485 gimple_stmt_iterator *gsi)
487 gimple_seq seq;
488 treemple temp;
489 temp.g = NULL;
491 switch (gimple_code (stmt))
493 case GIMPLE_GOTO:
494 case GIMPLE_RETURN:
495 temp.g = stmt;
496 seq = find_goto_replacement (tf, temp);
497 if (seq)
499 gsi_insert_seq_before (gsi, gimple_seq_copy (seq), GSI_SAME_STMT);
500 gsi_remove (gsi, false);
501 return;
503 break;
505 case GIMPLE_COND:
506 replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 2), tf, gsi);
507 replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 3), tf, gsi);
508 break;
510 case GIMPLE_TRY:
511 replace_goto_queue_stmt_list (gimple_try_eval_ptr (stmt), tf);
512 replace_goto_queue_stmt_list (gimple_try_cleanup_ptr (stmt), tf);
513 break;
514 case GIMPLE_CATCH:
515 replace_goto_queue_stmt_list (gimple_catch_handler_ptr (stmt), tf);
516 break;
517 case GIMPLE_EH_FILTER:
518 replace_goto_queue_stmt_list (gimple_eh_filter_failure_ptr (stmt), tf);
519 break;
520 case GIMPLE_EH_ELSE:
521 replace_goto_queue_stmt_list (gimple_eh_else_n_body_ptr (stmt), tf);
522 replace_goto_queue_stmt_list (gimple_eh_else_e_body_ptr (stmt), tf);
523 break;
525 default:
526 /* These won't have gotos in them. */
527 break;
530 gsi_next (gsi);
533 /* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
535 static void
536 replace_goto_queue_stmt_list (gimple_seq *seq, struct leh_tf_state *tf)
538 gimple_stmt_iterator gsi = gsi_start (*seq);
540 while (!gsi_end_p (gsi))
541 replace_goto_queue_1 (gsi_stmt (gsi), tf, &gsi);
544 /* Replace all goto queue members. */
546 static void
547 replace_goto_queue (struct leh_tf_state *tf)
549 if (tf->goto_queue_active == 0)
550 return;
551 replace_goto_queue_stmt_list (&tf->top_p_seq, tf);
552 replace_goto_queue_stmt_list (&eh_seq, tf);
555 /* Add a new record to the goto queue contained in TF. NEW_STMT is the
556 data to be added, IS_LABEL indicates whether NEW_STMT is a label or
557 a gimple return. */
559 static void
560 record_in_goto_queue (struct leh_tf_state *tf,
561 treemple new_stmt,
562 int index,
563 bool is_label,
564 location_t location)
566 size_t active, size;
567 struct goto_queue_node *q;
569 gcc_assert (!tf->goto_queue_map);
571 active = tf->goto_queue_active;
572 size = tf->goto_queue_size;
573 if (active >= size)
575 size = (size ? size * 2 : 32);
576 tf->goto_queue_size = size;
577 tf->goto_queue
578 = XRESIZEVEC (struct goto_queue_node, tf->goto_queue, size);
581 q = &tf->goto_queue[active];
582 tf->goto_queue_active = active + 1;
584 memset (q, 0, sizeof (*q));
585 q->stmt = new_stmt;
586 q->index = index;
587 q->location = location;
588 q->is_label = is_label;
591 /* Record the LABEL label in the goto queue contained in TF.
592 TF is not null. */
594 static void
595 record_in_goto_queue_label (struct leh_tf_state *tf, treemple stmt, tree label,
596 location_t location)
598 int index;
599 treemple temp, new_stmt;
601 if (!label)
602 return;
604 /* Computed and non-local gotos do not get processed. Given
605 their nature we can neither tell whether we've escaped the
606 finally block nor redirect them if we knew. */
607 if (TREE_CODE (label) != LABEL_DECL)
608 return;
610 /* No need to record gotos that don't leave the try block. */
611 temp.t = label;
612 if (!outside_finally_tree (temp, tf->try_finally_expr))
613 return;
615 if (! tf->dest_array.exists ())
617 tf->dest_array.create (10);
618 tf->dest_array.quick_push (label);
619 index = 0;
621 else
623 int n = tf->dest_array.length ();
624 for (index = 0; index < n; ++index)
625 if (tf->dest_array[index] == label)
626 break;
627 if (index == n)
628 tf->dest_array.safe_push (label);
631 /* In the case of a GOTO we want to record the destination label,
632 since with a GIMPLE_COND we have an easy access to the then/else
633 labels. */
634 new_stmt = stmt;
635 record_in_goto_queue (tf, new_stmt, index, true, location);
638 /* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
639 node, and if so record that fact in the goto queue associated with that
640 try_finally node. */
642 static void
643 maybe_record_in_goto_queue (struct leh_state *state, gimple stmt)
645 struct leh_tf_state *tf = state->tf;
646 treemple new_stmt;
648 if (!tf)
649 return;
651 switch (gimple_code (stmt))
653 case GIMPLE_COND:
654 new_stmt.tp = gimple_op_ptr (stmt, 2);
655 record_in_goto_queue_label (tf, new_stmt, gimple_cond_true_label (stmt),
656 EXPR_LOCATION (*new_stmt.tp));
657 new_stmt.tp = gimple_op_ptr (stmt, 3);
658 record_in_goto_queue_label (tf, new_stmt, gimple_cond_false_label (stmt),
659 EXPR_LOCATION (*new_stmt.tp));
660 break;
661 case GIMPLE_GOTO:
662 new_stmt.g = stmt;
663 record_in_goto_queue_label (tf, new_stmt, gimple_goto_dest (stmt),
664 gimple_location (stmt));
665 break;
667 case GIMPLE_RETURN:
668 tf->may_return = true;
669 new_stmt.g = stmt;
670 record_in_goto_queue (tf, new_stmt, -1, false, gimple_location (stmt));
671 break;
673 default:
674 gcc_unreachable ();
679 #ifdef ENABLE_CHECKING
680 /* We do not process GIMPLE_SWITCHes for now. As long as the original source
681 was in fact structured, and we've not yet done jump threading, then none
682 of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this. */
684 static void
685 verify_norecord_switch_expr (struct leh_state *state, gimple switch_expr)
687 struct leh_tf_state *tf = state->tf;
688 size_t i, n;
690 if (!tf)
691 return;
693 n = gimple_switch_num_labels (switch_expr);
695 for (i = 0; i < n; ++i)
697 treemple temp;
698 tree lab = CASE_LABEL (gimple_switch_label (switch_expr, i));
699 temp.t = lab;
700 gcc_assert (!outside_finally_tree (temp, tf->try_finally_expr));
703 #else
704 #define verify_norecord_switch_expr(state, switch_expr)
705 #endif
707 /* Redirect a RETURN_EXPR pointed to by Q to FINLAB. If MOD is
708 non-null, insert it before the new branch. */
710 static void
711 do_return_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod)
713 gimple x;
715 /* In the case of a return, the queue node must be a gimple statement. */
716 gcc_assert (!q->is_label);
718 /* Note that the return value may have already been computed, e.g.,
720 int x;
721 int foo (void)
723 x = 0;
724 try {
725 return x;
726 } finally {
727 x++;
731 should return 0, not 1. We don't have to do anything to make
732 this happens because the return value has been placed in the
733 RESULT_DECL already. */
735 q->cont_stmt = q->stmt.g;
737 if (mod)
738 gimple_seq_add_seq (&q->repl_stmt, mod);
740 x = gimple_build_goto (finlab);
741 gimple_set_location (x, q->location);
742 gimple_seq_add_stmt (&q->repl_stmt, x);
745 /* Similar, but easier, for GIMPLE_GOTO. */
747 static void
748 do_goto_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod,
749 struct leh_tf_state *tf)
751 gimple x;
753 gcc_assert (q->is_label);
755 q->cont_stmt = gimple_build_goto (tf->dest_array[q->index]);
757 if (mod)
758 gimple_seq_add_seq (&q->repl_stmt, mod);
760 x = gimple_build_goto (finlab);
761 gimple_set_location (x, q->location);
762 gimple_seq_add_stmt (&q->repl_stmt, x);
765 /* Emit a standard landing pad sequence into SEQ for REGION. */
767 static void
768 emit_post_landing_pad (gimple_seq *seq, eh_region region)
770 eh_landing_pad lp = region->landing_pads;
771 gimple x;
773 if (lp == NULL)
774 lp = gen_eh_landing_pad (region);
776 lp->post_landing_pad = create_artificial_label (UNKNOWN_LOCATION);
777 EH_LANDING_PAD_NR (lp->post_landing_pad) = lp->index;
779 x = gimple_build_label (lp->post_landing_pad);
780 gimple_seq_add_stmt (seq, x);
783 /* Emit a RESX statement into SEQ for REGION. */
785 static void
786 emit_resx (gimple_seq *seq, eh_region region)
788 gimple x = gimple_build_resx (region->index);
789 gimple_seq_add_stmt (seq, x);
790 if (region->outer)
791 record_stmt_eh_region (region->outer, x);
794 /* Emit an EH_DISPATCH statement into SEQ for REGION. */
796 static void
797 emit_eh_dispatch (gimple_seq *seq, eh_region region)
799 gimple x = gimple_build_eh_dispatch (region->index);
800 gimple_seq_add_stmt (seq, x);
803 /* Note that the current EH region may contain a throw, or a
804 call to a function which itself may contain a throw. */
806 static void
807 note_eh_region_may_contain_throw (eh_region region)
809 while (bitmap_set_bit (eh_region_may_contain_throw_map, region->index))
811 if (region->type == ERT_MUST_NOT_THROW)
812 break;
813 region = region->outer;
814 if (region == NULL)
815 break;
819 /* Check if REGION has been marked as containing a throw. If REGION is
820 NULL, this predicate is false. */
822 static inline bool
823 eh_region_may_contain_throw (eh_region r)
825 return r && bitmap_bit_p (eh_region_may_contain_throw_map, r->index);
828 /* We want to transform
829 try { body; } catch { stuff; }
831 normal_seqence:
832 body;
833 over:
834 eh_seqence:
835 landing_pad:
836 stuff;
837 goto over;
839 TP is a GIMPLE_TRY node. REGION is the region whose post_landing_pad
840 should be placed before the second operand, or NULL. OVER is
841 an existing label that should be put at the exit, or NULL. */
843 static gimple_seq
844 frob_into_branch_around (gimple tp, eh_region region, tree over)
846 gimple x;
847 gimple_seq cleanup, result;
848 location_t loc = gimple_location (tp);
850 cleanup = gimple_try_cleanup (tp);
851 result = gimple_try_eval (tp);
853 if (region)
854 emit_post_landing_pad (&eh_seq, region);
856 if (gimple_seq_may_fallthru (cleanup))
858 if (!over)
859 over = create_artificial_label (loc);
860 x = gimple_build_goto (over);
861 gimple_set_location (x, loc);
862 gimple_seq_add_stmt (&cleanup, x);
864 gimple_seq_add_seq (&eh_seq, cleanup);
866 if (over)
868 x = gimple_build_label (over);
869 gimple_seq_add_stmt (&result, x);
871 return result;
874 /* A subroutine of lower_try_finally. Duplicate the tree rooted at T.
875 Make sure to record all new labels found. */
877 static gimple_seq
878 lower_try_finally_dup_block (gimple_seq seq, struct leh_state *outer_state,
879 location_t loc)
881 gimple region = NULL;
882 gimple_seq new_seq;
883 gimple_stmt_iterator gsi;
885 new_seq = copy_gimple_seq_and_replace_locals (seq);
887 for (gsi = gsi_start (new_seq); !gsi_end_p (gsi); gsi_next (&gsi))
889 gimple stmt = gsi_stmt (gsi);
890 if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
892 tree block = gimple_block (stmt);
893 gimple_set_location (stmt, loc);
894 gimple_set_block (stmt, block);
898 if (outer_state->tf)
899 region = outer_state->tf->try_finally_expr;
900 collect_finally_tree_1 (new_seq, region);
902 return new_seq;
905 /* A subroutine of lower_try_finally. Create a fallthru label for
906 the given try_finally state. The only tricky bit here is that
907 we have to make sure to record the label in our outer context. */
909 static tree
910 lower_try_finally_fallthru_label (struct leh_tf_state *tf)
912 tree label = tf->fallthru_label;
913 treemple temp;
915 if (!label)
917 label = create_artificial_label (gimple_location (tf->try_finally_expr));
918 tf->fallthru_label = label;
919 if (tf->outer->tf)
921 temp.t = label;
922 record_in_finally_tree (temp, tf->outer->tf->try_finally_expr);
925 return label;
928 /* A subroutine of lower_try_finally. If FINALLY consits of a
929 GIMPLE_EH_ELSE node, return it. */
931 static inline gimple
932 get_eh_else (gimple_seq finally)
934 gimple x = gimple_seq_first_stmt (finally);
935 if (gimple_code (x) == GIMPLE_EH_ELSE)
937 gcc_assert (gimple_seq_singleton_p (finally));
938 return x;
940 return NULL;
943 /* A subroutine of lower_try_finally. If the eh_protect_cleanup_actions
944 langhook returns non-null, then the language requires that the exception
945 path out of a try_finally be treated specially. To wit: the code within
946 the finally block may not itself throw an exception. We have two choices
947 here. First we can duplicate the finally block and wrap it in a
948 must_not_throw region. Second, we can generate code like
950 try {
951 finally_block;
952 } catch {
953 if (fintmp == eh_edge)
954 protect_cleanup_actions;
957 where "fintmp" is the temporary used in the switch statement generation
958 alternative considered below. For the nonce, we always choose the first
959 option.
961 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
963 static void
964 honor_protect_cleanup_actions (struct leh_state *outer_state,
965 struct leh_state *this_state,
966 struct leh_tf_state *tf)
968 tree protect_cleanup_actions;
969 gimple_stmt_iterator gsi;
970 bool finally_may_fallthru;
971 gimple_seq finally;
972 gimple x, eh_else;
974 /* First check for nothing to do. */
975 if (lang_hooks.eh_protect_cleanup_actions == NULL)
976 return;
977 protect_cleanup_actions = lang_hooks.eh_protect_cleanup_actions ();
978 if (protect_cleanup_actions == NULL)
979 return;
981 finally = gimple_try_cleanup (tf->top_p);
982 eh_else = get_eh_else (finally);
984 /* Duplicate the FINALLY block. Only need to do this for try-finally,
985 and not for cleanups. If we've got an EH_ELSE, extract it now. */
986 if (eh_else)
988 finally = gimple_eh_else_e_body (eh_else);
989 gimple_try_set_cleanup (tf->top_p, gimple_eh_else_n_body (eh_else));
991 else if (this_state)
992 finally = lower_try_finally_dup_block (finally, outer_state,
993 gimple_location (tf->try_finally_expr));
994 finally_may_fallthru = gimple_seq_may_fallthru (finally);
996 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
997 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
998 to be in an enclosing scope, but needs to be implemented at this level
999 to avoid a nesting violation (see wrap_temporary_cleanups in
1000 cp/decl.c). Since it's logically at an outer level, we should call
1001 terminate before we get to it, so strip it away before adding the
1002 MUST_NOT_THROW filter. */
1003 gsi = gsi_start (finally);
1004 x = gsi_stmt (gsi);
1005 if (gimple_code (x) == GIMPLE_TRY
1006 && gimple_try_kind (x) == GIMPLE_TRY_CATCH
1007 && gimple_try_catch_is_cleanup (x))
1009 gsi_insert_seq_before (&gsi, gimple_try_eval (x), GSI_SAME_STMT);
1010 gsi_remove (&gsi, false);
1013 /* Wrap the block with protect_cleanup_actions as the action. */
1014 x = gimple_build_eh_must_not_throw (protect_cleanup_actions);
1015 x = gimple_build_try (finally, gimple_seq_alloc_with_stmt (x),
1016 GIMPLE_TRY_CATCH);
1017 finally = lower_eh_must_not_throw (outer_state, x);
1019 /* Drop all of this into the exception sequence. */
1020 emit_post_landing_pad (&eh_seq, tf->region);
1021 gimple_seq_add_seq (&eh_seq, finally);
1022 if (finally_may_fallthru)
1023 emit_resx (&eh_seq, tf->region);
1025 /* Having now been handled, EH isn't to be considered with
1026 the rest of the outgoing edges. */
1027 tf->may_throw = false;
1030 /* A subroutine of lower_try_finally. We have determined that there is
1031 no fallthru edge out of the finally block. This means that there is
1032 no outgoing edge corresponding to any incoming edge. Restructure the
1033 try_finally node for this special case. */
1035 static void
1036 lower_try_finally_nofallthru (struct leh_state *state,
1037 struct leh_tf_state *tf)
1039 tree lab;
1040 gimple x, eh_else;
1041 gimple_seq finally;
1042 struct goto_queue_node *q, *qe;
1044 lab = create_artificial_label (gimple_location (tf->try_finally_expr));
1046 /* We expect that tf->top_p is a GIMPLE_TRY. */
1047 finally = gimple_try_cleanup (tf->top_p);
1048 tf->top_p_seq = gimple_try_eval (tf->top_p);
1050 x = gimple_build_label (lab);
1051 gimple_seq_add_stmt (&tf->top_p_seq, x);
1053 q = tf->goto_queue;
1054 qe = q + tf->goto_queue_active;
1055 for (; q < qe; ++q)
1056 if (q->index < 0)
1057 do_return_redirection (q, lab, NULL);
1058 else
1059 do_goto_redirection (q, lab, NULL, tf);
1061 replace_goto_queue (tf);
1063 /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1064 eh_else = get_eh_else (finally);
1065 if (eh_else)
1067 finally = gimple_eh_else_n_body (eh_else);
1068 lower_eh_constructs_1 (state, &finally);
1069 gimple_seq_add_seq (&tf->top_p_seq, finally);
1071 if (tf->may_throw)
1073 finally = gimple_eh_else_e_body (eh_else);
1074 lower_eh_constructs_1 (state, &finally);
1076 emit_post_landing_pad (&eh_seq, tf->region);
1077 gimple_seq_add_seq (&eh_seq, finally);
1080 else
1082 lower_eh_constructs_1 (state, &finally);
1083 gimple_seq_add_seq (&tf->top_p_seq, finally);
1085 if (tf->may_throw)
1087 emit_post_landing_pad (&eh_seq, tf->region);
1089 x = gimple_build_goto (lab);
1090 gimple_set_location (x, gimple_location (tf->try_finally_expr));
1091 gimple_seq_add_stmt (&eh_seq, x);
1096 /* A subroutine of lower_try_finally. We have determined that there is
1097 exactly one destination of the finally block. Restructure the
1098 try_finally node for this special case. */
1100 static void
1101 lower_try_finally_onedest (struct leh_state *state, struct leh_tf_state *tf)
1103 struct goto_queue_node *q, *qe;
1104 gimple x;
1105 gimple_seq finally;
1106 gimple_stmt_iterator gsi;
1107 tree finally_label;
1108 location_t loc = gimple_location (tf->try_finally_expr);
1110 finally = gimple_try_cleanup (tf->top_p);
1111 tf->top_p_seq = gimple_try_eval (tf->top_p);
1113 /* Since there's only one destination, and the destination edge can only
1114 either be EH or non-EH, that implies that all of our incoming edges
1115 are of the same type. Therefore we can lower EH_ELSE immediately. */
1116 x = get_eh_else (finally);
1117 if (x)
1119 if (tf->may_throw)
1120 finally = gimple_eh_else_e_body (x);
1121 else
1122 finally = gimple_eh_else_n_body (x);
1125 lower_eh_constructs_1 (state, &finally);
1127 for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
1129 gimple stmt = gsi_stmt (gsi);
1130 if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
1132 tree block = gimple_block (stmt);
1133 gimple_set_location (stmt, gimple_location (tf->try_finally_expr));
1134 gimple_set_block (stmt, block);
1138 if (tf->may_throw)
1140 /* Only reachable via the exception edge. Add the given label to
1141 the head of the FINALLY block. Append a RESX at the end. */
1142 emit_post_landing_pad (&eh_seq, tf->region);
1143 gimple_seq_add_seq (&eh_seq, finally);
1144 emit_resx (&eh_seq, tf->region);
1145 return;
1148 if (tf->may_fallthru)
1150 /* Only reachable via the fallthru edge. Do nothing but let
1151 the two blocks run together; we'll fall out the bottom. */
1152 gimple_seq_add_seq (&tf->top_p_seq, finally);
1153 return;
1156 finally_label = create_artificial_label (loc);
1157 x = gimple_build_label (finally_label);
1158 gimple_seq_add_stmt (&tf->top_p_seq, x);
1160 gimple_seq_add_seq (&tf->top_p_seq, finally);
1162 q = tf->goto_queue;
1163 qe = q + tf->goto_queue_active;
1165 if (tf->may_return)
1167 /* Reachable by return expressions only. Redirect them. */
1168 for (; q < qe; ++q)
1169 do_return_redirection (q, finally_label, NULL);
1170 replace_goto_queue (tf);
1172 else
1174 /* Reachable by goto expressions only. Redirect them. */
1175 for (; q < qe; ++q)
1176 do_goto_redirection (q, finally_label, NULL, tf);
1177 replace_goto_queue (tf);
1179 if (tf->dest_array[0] == tf->fallthru_label)
1181 /* Reachable by goto to fallthru label only. Redirect it
1182 to the new label (already created, sadly), and do not
1183 emit the final branch out, or the fallthru label. */
1184 tf->fallthru_label = NULL;
1185 return;
1189 /* Place the original return/goto to the original destination
1190 immediately after the finally block. */
1191 x = tf->goto_queue[0].cont_stmt;
1192 gimple_seq_add_stmt (&tf->top_p_seq, x);
1193 maybe_record_in_goto_queue (state, x);
1196 /* A subroutine of lower_try_finally. There are multiple edges incoming
1197 and outgoing from the finally block. Implement this by duplicating the
1198 finally block for every destination. */
1200 static void
1201 lower_try_finally_copy (struct leh_state *state, struct leh_tf_state *tf)
1203 gimple_seq finally;
1204 gimple_seq new_stmt;
1205 gimple_seq seq;
1206 gimple x, eh_else;
1207 tree tmp;
1208 location_t tf_loc = gimple_location (tf->try_finally_expr);
1210 finally = gimple_try_cleanup (tf->top_p);
1212 /* Notice EH_ELSE, and simplify some of the remaining code
1213 by considering FINALLY to be the normal return path only. */
1214 eh_else = get_eh_else (finally);
1215 if (eh_else)
1216 finally = gimple_eh_else_n_body (eh_else);
1218 tf->top_p_seq = gimple_try_eval (tf->top_p);
1219 new_stmt = NULL;
1221 if (tf->may_fallthru)
1223 seq = lower_try_finally_dup_block (finally, state, tf_loc);
1224 lower_eh_constructs_1 (state, &seq);
1225 gimple_seq_add_seq (&new_stmt, seq);
1227 tmp = lower_try_finally_fallthru_label (tf);
1228 x = gimple_build_goto (tmp);
1229 gimple_set_location (x, tf_loc);
1230 gimple_seq_add_stmt (&new_stmt, x);
1233 if (tf->may_throw)
1235 /* We don't need to copy the EH path of EH_ELSE,
1236 since it is only emitted once. */
1237 if (eh_else)
1238 seq = gimple_eh_else_e_body (eh_else);
1239 else
1240 seq = lower_try_finally_dup_block (finally, state, tf_loc);
1241 lower_eh_constructs_1 (state, &seq);
1243 emit_post_landing_pad (&eh_seq, tf->region);
1244 gimple_seq_add_seq (&eh_seq, seq);
1245 emit_resx (&eh_seq, tf->region);
1248 if (tf->goto_queue)
1250 struct goto_queue_node *q, *qe;
1251 int return_index, index;
1252 struct labels_s
1254 struct goto_queue_node *q;
1255 tree label;
1256 } *labels;
1258 return_index = tf->dest_array.length ();
1259 labels = XCNEWVEC (struct labels_s, return_index + 1);
1261 q = tf->goto_queue;
1262 qe = q + tf->goto_queue_active;
1263 for (; q < qe; q++)
1265 index = q->index < 0 ? return_index : q->index;
1267 if (!labels[index].q)
1268 labels[index].q = q;
1271 for (index = 0; index < return_index + 1; index++)
1273 tree lab;
1275 q = labels[index].q;
1276 if (! q)
1277 continue;
1279 lab = labels[index].label
1280 = create_artificial_label (tf_loc);
1282 if (index == return_index)
1283 do_return_redirection (q, lab, NULL);
1284 else
1285 do_goto_redirection (q, lab, NULL, tf);
1287 x = gimple_build_label (lab);
1288 gimple_seq_add_stmt (&new_stmt, x);
1290 seq = lower_try_finally_dup_block (finally, state, q->location);
1291 lower_eh_constructs_1 (state, &seq);
1292 gimple_seq_add_seq (&new_stmt, seq);
1294 gimple_seq_add_stmt (&new_stmt, q->cont_stmt);
1295 maybe_record_in_goto_queue (state, q->cont_stmt);
1298 for (q = tf->goto_queue; q < qe; q++)
1300 tree lab;
1302 index = q->index < 0 ? return_index : q->index;
1304 if (labels[index].q == q)
1305 continue;
1307 lab = labels[index].label;
1309 if (index == return_index)
1310 do_return_redirection (q, lab, NULL);
1311 else
1312 do_goto_redirection (q, lab, NULL, tf);
1315 replace_goto_queue (tf);
1316 free (labels);
1319 /* Need to link new stmts after running replace_goto_queue due
1320 to not wanting to process the same goto stmts twice. */
1321 gimple_seq_add_seq (&tf->top_p_seq, new_stmt);
1324 /* A subroutine of lower_try_finally. There are multiple edges incoming
1325 and outgoing from the finally block. Implement this by instrumenting
1326 each incoming edge and creating a switch statement at the end of the
1327 finally block that branches to the appropriate destination. */
1329 static void
1330 lower_try_finally_switch (struct leh_state *state, struct leh_tf_state *tf)
1332 struct goto_queue_node *q, *qe;
1333 tree finally_tmp, finally_label;
1334 int return_index, eh_index, fallthru_index;
1335 int nlabels, ndests, j, last_case_index;
1336 tree last_case;
1337 vec<tree> case_label_vec;
1338 gimple_seq switch_body = NULL;
1339 gimple x, eh_else;
1340 tree tmp;
1341 gimple switch_stmt;
1342 gimple_seq finally;
1343 struct pointer_map_t *cont_map = NULL;
1344 /* The location of the TRY_FINALLY stmt. */
1345 location_t tf_loc = gimple_location (tf->try_finally_expr);
1346 /* The location of the finally block. */
1347 location_t finally_loc;
1349 finally = gimple_try_cleanup (tf->top_p);
1350 eh_else = get_eh_else (finally);
1352 /* Mash the TRY block to the head of the chain. */
1353 tf->top_p_seq = gimple_try_eval (tf->top_p);
1355 /* The location of the finally is either the last stmt in the finally
1356 block or the location of the TRY_FINALLY itself. */
1357 x = gimple_seq_last_stmt (finally);
1358 finally_loc = x ? gimple_location (x) : tf_loc;
1360 /* Prepare for switch statement generation. */
1361 nlabels = tf->dest_array.length ();
1362 return_index = nlabels;
1363 eh_index = return_index + tf->may_return;
1364 fallthru_index = eh_index + (tf->may_throw && !eh_else);
1365 ndests = fallthru_index + tf->may_fallthru;
1367 finally_tmp = create_tmp_var (integer_type_node, "finally_tmp");
1368 finally_label = create_artificial_label (finally_loc);
1370 /* We use vec::quick_push on case_label_vec throughout this function,
1371 since we know the size in advance and allocate precisely as muce
1372 space as needed. */
1373 case_label_vec.create (ndests);
1374 last_case = NULL;
1375 last_case_index = 0;
1377 /* Begin inserting code for getting to the finally block. Things
1378 are done in this order to correspond to the sequence the code is
1379 laid out. */
1381 if (tf->may_fallthru)
1383 x = gimple_build_assign (finally_tmp,
1384 build_int_cst (integer_type_node,
1385 fallthru_index));
1386 gimple_seq_add_stmt (&tf->top_p_seq, x);
1388 tmp = build_int_cst (integer_type_node, fallthru_index);
1389 last_case = build_case_label (tmp, NULL,
1390 create_artificial_label (tf_loc));
1391 case_label_vec.quick_push (last_case);
1392 last_case_index++;
1394 x = gimple_build_label (CASE_LABEL (last_case));
1395 gimple_seq_add_stmt (&switch_body, x);
1397 tmp = lower_try_finally_fallthru_label (tf);
1398 x = gimple_build_goto (tmp);
1399 gimple_set_location (x, tf_loc);
1400 gimple_seq_add_stmt (&switch_body, x);
1403 /* For EH_ELSE, emit the exception path (plus resx) now, then
1404 subsequently we only need consider the normal path. */
1405 if (eh_else)
1407 if (tf->may_throw)
1409 finally = gimple_eh_else_e_body (eh_else);
1410 lower_eh_constructs_1 (state, &finally);
1412 emit_post_landing_pad (&eh_seq, tf->region);
1413 gimple_seq_add_seq (&eh_seq, finally);
1414 emit_resx (&eh_seq, tf->region);
1417 finally = gimple_eh_else_n_body (eh_else);
1419 else if (tf->may_throw)
1421 emit_post_landing_pad (&eh_seq, tf->region);
1423 x = gimple_build_assign (finally_tmp,
1424 build_int_cst (integer_type_node, eh_index));
1425 gimple_seq_add_stmt (&eh_seq, x);
1427 x = gimple_build_goto (finally_label);
1428 gimple_set_location (x, tf_loc);
1429 gimple_seq_add_stmt (&eh_seq, x);
1431 tmp = build_int_cst (integer_type_node, eh_index);
1432 last_case = build_case_label (tmp, NULL,
1433 create_artificial_label (tf_loc));
1434 case_label_vec.quick_push (last_case);
1435 last_case_index++;
1437 x = gimple_build_label (CASE_LABEL (last_case));
1438 gimple_seq_add_stmt (&eh_seq, x);
1439 emit_resx (&eh_seq, tf->region);
1442 x = gimple_build_label (finally_label);
1443 gimple_seq_add_stmt (&tf->top_p_seq, x);
1445 lower_eh_constructs_1 (state, &finally);
1446 gimple_seq_add_seq (&tf->top_p_seq, finally);
1448 /* Redirect each incoming goto edge. */
1449 q = tf->goto_queue;
1450 qe = q + tf->goto_queue_active;
1451 j = last_case_index + tf->may_return;
1452 /* Prepare the assignments to finally_tmp that are executed upon the
1453 entrance through a particular edge. */
1454 for (; q < qe; ++q)
1456 gimple_seq mod = NULL;
1457 int switch_id;
1458 unsigned int case_index;
1460 if (q->index < 0)
1462 x = gimple_build_assign (finally_tmp,
1463 build_int_cst (integer_type_node,
1464 return_index));
1465 gimple_seq_add_stmt (&mod, x);
1466 do_return_redirection (q, finally_label, mod);
1467 switch_id = return_index;
1469 else
1471 x = gimple_build_assign (finally_tmp,
1472 build_int_cst (integer_type_node, q->index));
1473 gimple_seq_add_stmt (&mod, x);
1474 do_goto_redirection (q, finally_label, mod, tf);
1475 switch_id = q->index;
1478 case_index = j + q->index;
1479 if (case_label_vec.length () <= case_index || !case_label_vec[case_index])
1481 tree case_lab;
1482 void **slot;
1483 tmp = build_int_cst (integer_type_node, switch_id);
1484 case_lab = build_case_label (tmp, NULL,
1485 create_artificial_label (tf_loc));
1486 /* We store the cont_stmt in the pointer map, so that we can recover
1487 it in the loop below. */
1488 if (!cont_map)
1489 cont_map = pointer_map_create ();
1490 slot = pointer_map_insert (cont_map, case_lab);
1491 *slot = q->cont_stmt;
1492 case_label_vec.quick_push (case_lab);
1495 for (j = last_case_index; j < last_case_index + nlabels; j++)
1497 gimple cont_stmt;
1498 void **slot;
1500 last_case = case_label_vec[j];
1502 gcc_assert (last_case);
1503 gcc_assert (cont_map);
1505 slot = pointer_map_contains (cont_map, last_case);
1506 gcc_assert (slot);
1507 cont_stmt = *(gimple *) slot;
1509 x = gimple_build_label (CASE_LABEL (last_case));
1510 gimple_seq_add_stmt (&switch_body, x);
1511 gimple_seq_add_stmt (&switch_body, cont_stmt);
1512 maybe_record_in_goto_queue (state, cont_stmt);
1514 if (cont_map)
1515 pointer_map_destroy (cont_map);
1517 replace_goto_queue (tf);
1519 /* Make sure that the last case is the default label, as one is required.
1520 Then sort the labels, which is also required in GIMPLE. */
1521 CASE_LOW (last_case) = NULL;
1522 sort_case_labels (case_label_vec);
1524 /* Build the switch statement, setting last_case to be the default
1525 label. */
1526 switch_stmt = gimple_build_switch (finally_tmp, last_case,
1527 case_label_vec);
1528 gimple_set_location (switch_stmt, finally_loc);
1530 /* Need to link SWITCH_STMT after running replace_goto_queue
1531 due to not wanting to process the same goto stmts twice. */
1532 gimple_seq_add_stmt (&tf->top_p_seq, switch_stmt);
1533 gimple_seq_add_seq (&tf->top_p_seq, switch_body);
1536 /* Decide whether or not we are going to duplicate the finally block.
1537 There are several considerations.
1539 First, if this is Java, then the finally block contains code
1540 written by the user. It has line numbers associated with it,
1541 so duplicating the block means it's difficult to set a breakpoint.
1542 Since controlling code generation via -g is verboten, we simply
1543 never duplicate code without optimization.
1545 Second, we'd like to prevent egregious code growth. One way to
1546 do this is to estimate the size of the finally block, multiply
1547 that by the number of copies we'd need to make, and compare against
1548 the estimate of the size of the switch machinery we'd have to add. */
1550 static bool
1551 decide_copy_try_finally (int ndests, bool may_throw, gimple_seq finally)
1553 int f_estimate, sw_estimate;
1554 gimple eh_else;
1556 /* If there's an EH_ELSE involved, the exception path is separate
1557 and really doesn't come into play for this computation. */
1558 eh_else = get_eh_else (finally);
1559 if (eh_else)
1561 ndests -= may_throw;
1562 finally = gimple_eh_else_n_body (eh_else);
1565 if (!optimize)
1567 gimple_stmt_iterator gsi;
1569 if (ndests == 1)
1570 return true;
1572 for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
1574 gimple stmt = gsi_stmt (gsi);
1575 if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
1576 return false;
1578 return true;
1581 /* Finally estimate N times, plus N gotos. */
1582 f_estimate = count_insns_seq (finally, &eni_size_weights);
1583 f_estimate = (f_estimate + 1) * ndests;
1585 /* Switch statement (cost 10), N variable assignments, N gotos. */
1586 sw_estimate = 10 + 2 * ndests;
1588 /* Optimize for size clearly wants our best guess. */
1589 if (optimize_function_for_size_p (cfun))
1590 return f_estimate < sw_estimate;
1592 /* ??? These numbers are completely made up so far. */
1593 if (optimize > 1)
1594 return f_estimate < 100 || f_estimate < sw_estimate * 2;
1595 else
1596 return f_estimate < 40 || f_estimate * 2 < sw_estimate * 3;
1599 /* REG is the enclosing region for a possible cleanup region, or the region
1600 itself. Returns TRUE if such a region would be unreachable.
1602 Cleanup regions within a must-not-throw region aren't actually reachable
1603 even if there are throwing stmts within them, because the personality
1604 routine will call terminate before unwinding. */
1606 static bool
1607 cleanup_is_dead_in (eh_region reg)
1609 while (reg && reg->type == ERT_CLEANUP)
1610 reg = reg->outer;
1611 return (reg && reg->type == ERT_MUST_NOT_THROW);
1614 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
1615 to a sequence of labels and blocks, plus the exception region trees
1616 that record all the magic. This is complicated by the need to
1617 arrange for the FINALLY block to be executed on all exits. */
1619 static gimple_seq
1620 lower_try_finally (struct leh_state *state, gimple tp)
1622 struct leh_tf_state this_tf;
1623 struct leh_state this_state;
1624 int ndests;
1625 gimple_seq old_eh_seq;
1627 /* Process the try block. */
1629 memset (&this_tf, 0, sizeof (this_tf));
1630 this_tf.try_finally_expr = tp;
1631 this_tf.top_p = tp;
1632 this_tf.outer = state;
1633 if (using_eh_for_cleanups_p && !cleanup_is_dead_in (state->cur_region))
1635 this_tf.region = gen_eh_region_cleanup (state->cur_region);
1636 this_state.cur_region = this_tf.region;
1638 else
1640 this_tf.region = NULL;
1641 this_state.cur_region = state->cur_region;
1644 this_state.ehp_region = state->ehp_region;
1645 this_state.tf = &this_tf;
1647 old_eh_seq = eh_seq;
1648 eh_seq = NULL;
1650 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1652 /* Determine if the try block is escaped through the bottom. */
1653 this_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
1655 /* Determine if any exceptions are possible within the try block. */
1656 if (this_tf.region)
1657 this_tf.may_throw = eh_region_may_contain_throw (this_tf.region);
1658 if (this_tf.may_throw)
1659 honor_protect_cleanup_actions (state, &this_state, &this_tf);
1661 /* Determine how many edges (still) reach the finally block. Or rather,
1662 how many destinations are reached by the finally block. Use this to
1663 determine how we process the finally block itself. */
1665 ndests = this_tf.dest_array.length ();
1666 ndests += this_tf.may_fallthru;
1667 ndests += this_tf.may_return;
1668 ndests += this_tf.may_throw;
1670 /* If the FINALLY block is not reachable, dike it out. */
1671 if (ndests == 0)
1673 gimple_seq_add_seq (&this_tf.top_p_seq, gimple_try_eval (tp));
1674 gimple_try_set_cleanup (tp, NULL);
1676 /* If the finally block doesn't fall through, then any destination
1677 we might try to impose there isn't reached either. There may be
1678 some minor amount of cleanup and redirection still needed. */
1679 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp)))
1680 lower_try_finally_nofallthru (state, &this_tf);
1682 /* We can easily special-case redirection to a single destination. */
1683 else if (ndests == 1)
1684 lower_try_finally_onedest (state, &this_tf);
1685 else if (decide_copy_try_finally (ndests, this_tf.may_throw,
1686 gimple_try_cleanup (tp)))
1687 lower_try_finally_copy (state, &this_tf);
1688 else
1689 lower_try_finally_switch (state, &this_tf);
1691 /* If someone requested we add a label at the end of the transformed
1692 block, do so. */
1693 if (this_tf.fallthru_label)
1695 /* This must be reached only if ndests == 0. */
1696 gimple x = gimple_build_label (this_tf.fallthru_label);
1697 gimple_seq_add_stmt (&this_tf.top_p_seq, x);
1700 this_tf.dest_array.release ();
1701 free (this_tf.goto_queue);
1702 if (this_tf.goto_queue_map)
1703 pointer_map_destroy (this_tf.goto_queue_map);
1705 /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1706 If there was no old eh_seq, then the append is trivially already done. */
1707 if (old_eh_seq)
1709 if (eh_seq == NULL)
1710 eh_seq = old_eh_seq;
1711 else
1713 gimple_seq new_eh_seq = eh_seq;
1714 eh_seq = old_eh_seq;
1715 gimple_seq_add_seq(&eh_seq, new_eh_seq);
1719 return this_tf.top_p_seq;
1722 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1723 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1724 exception region trees that records all the magic. */
1726 static gimple_seq
1727 lower_catch (struct leh_state *state, gimple tp)
1729 eh_region try_region = NULL;
1730 struct leh_state this_state = *state;
1731 gimple_stmt_iterator gsi;
1732 tree out_label;
1733 gimple_seq new_seq, cleanup;
1734 gimple x;
1735 location_t try_catch_loc = gimple_location (tp);
1737 if (flag_exceptions)
1739 try_region = gen_eh_region_try (state->cur_region);
1740 this_state.cur_region = try_region;
1743 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1745 if (!eh_region_may_contain_throw (try_region))
1746 return gimple_try_eval (tp);
1748 new_seq = NULL;
1749 emit_eh_dispatch (&new_seq, try_region);
1750 emit_resx (&new_seq, try_region);
1752 this_state.cur_region = state->cur_region;
1753 this_state.ehp_region = try_region;
1755 out_label = NULL;
1756 cleanup = gimple_try_cleanup (tp);
1757 for (gsi = gsi_start (cleanup);
1758 !gsi_end_p (gsi);
1759 gsi_next (&gsi))
1761 eh_catch c;
1762 gimple gcatch;
1763 gimple_seq handler;
1765 gcatch = gsi_stmt (gsi);
1766 c = gen_eh_region_catch (try_region, gimple_catch_types (gcatch));
1768 handler = gimple_catch_handler (gcatch);
1769 lower_eh_constructs_1 (&this_state, &handler);
1771 c->label = create_artificial_label (UNKNOWN_LOCATION);
1772 x = gimple_build_label (c->label);
1773 gimple_seq_add_stmt (&new_seq, x);
1775 gimple_seq_add_seq (&new_seq, handler);
1777 if (gimple_seq_may_fallthru (new_seq))
1779 if (!out_label)
1780 out_label = create_artificial_label (try_catch_loc);
1782 x = gimple_build_goto (out_label);
1783 gimple_seq_add_stmt (&new_seq, x);
1785 if (!c->type_list)
1786 break;
1789 gimple_try_set_cleanup (tp, new_seq);
1791 return frob_into_branch_around (tp, try_region, out_label);
1794 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1795 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1796 region trees that record all the magic. */
1798 static gimple_seq
1799 lower_eh_filter (struct leh_state *state, gimple tp)
1801 struct leh_state this_state = *state;
1802 eh_region this_region = NULL;
1803 gimple inner, x;
1804 gimple_seq new_seq;
1806 inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1808 if (flag_exceptions)
1810 this_region = gen_eh_region_allowed (state->cur_region,
1811 gimple_eh_filter_types (inner));
1812 this_state.cur_region = this_region;
1815 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1817 if (!eh_region_may_contain_throw (this_region))
1818 return gimple_try_eval (tp);
1820 new_seq = NULL;
1821 this_state.cur_region = state->cur_region;
1822 this_state.ehp_region = this_region;
1824 emit_eh_dispatch (&new_seq, this_region);
1825 emit_resx (&new_seq, this_region);
1827 this_region->u.allowed.label = create_artificial_label (UNKNOWN_LOCATION);
1828 x = gimple_build_label (this_region->u.allowed.label);
1829 gimple_seq_add_stmt (&new_seq, x);
1831 lower_eh_constructs_1 (&this_state, gimple_eh_filter_failure_ptr (inner));
1832 gimple_seq_add_seq (&new_seq, gimple_eh_filter_failure (inner));
1834 gimple_try_set_cleanup (tp, new_seq);
1836 return frob_into_branch_around (tp, this_region, NULL);
1839 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with
1840 an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1841 plus the exception region trees that record all the magic. */
1843 static gimple_seq
1844 lower_eh_must_not_throw (struct leh_state *state, gimple tp)
1846 struct leh_state this_state = *state;
1848 if (flag_exceptions)
1850 gimple inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1851 eh_region this_region;
1853 this_region = gen_eh_region_must_not_throw (state->cur_region);
1854 this_region->u.must_not_throw.failure_decl
1855 = gimple_eh_must_not_throw_fndecl (inner);
1856 this_region->u.must_not_throw.failure_loc
1857 = LOCATION_LOCUS (gimple_location (tp));
1859 /* In order to get mangling applied to this decl, we must mark it
1860 used now. Otherwise, pass_ipa_free_lang_data won't think it
1861 needs to happen. */
1862 TREE_USED (this_region->u.must_not_throw.failure_decl) = 1;
1864 this_state.cur_region = this_region;
1867 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1869 return gimple_try_eval (tp);
1872 /* Implement a cleanup expression. This is similar to try-finally,
1873 except that we only execute the cleanup block for exception edges. */
1875 static gimple_seq
1876 lower_cleanup (struct leh_state *state, gimple tp)
1878 struct leh_state this_state = *state;
1879 eh_region this_region = NULL;
1880 struct leh_tf_state fake_tf;
1881 gimple_seq result;
1882 bool cleanup_dead = cleanup_is_dead_in (state->cur_region);
1884 if (flag_exceptions && !cleanup_dead)
1886 this_region = gen_eh_region_cleanup (state->cur_region);
1887 this_state.cur_region = this_region;
1890 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1892 if (cleanup_dead || !eh_region_may_contain_throw (this_region))
1893 return gimple_try_eval (tp);
1895 /* Build enough of a try-finally state so that we can reuse
1896 honor_protect_cleanup_actions. */
1897 memset (&fake_tf, 0, sizeof (fake_tf));
1898 fake_tf.top_p = fake_tf.try_finally_expr = tp;
1899 fake_tf.outer = state;
1900 fake_tf.region = this_region;
1901 fake_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
1902 fake_tf.may_throw = true;
1904 honor_protect_cleanup_actions (state, NULL, &fake_tf);
1906 if (fake_tf.may_throw)
1908 /* In this case honor_protect_cleanup_actions had nothing to do,
1909 and we should process this normally. */
1910 lower_eh_constructs_1 (state, gimple_try_cleanup_ptr (tp));
1911 result = frob_into_branch_around (tp, this_region,
1912 fake_tf.fallthru_label);
1914 else
1916 /* In this case honor_protect_cleanup_actions did nearly all of
1917 the work. All we have left is to append the fallthru_label. */
1919 result = gimple_try_eval (tp);
1920 if (fake_tf.fallthru_label)
1922 gimple x = gimple_build_label (fake_tf.fallthru_label);
1923 gimple_seq_add_stmt (&result, x);
1926 return result;
1929 /* Main loop for lowering eh constructs. Also moves gsi to the next
1930 statement. */
1932 static void
1933 lower_eh_constructs_2 (struct leh_state *state, gimple_stmt_iterator *gsi)
1935 gimple_seq replace;
1936 gimple x;
1937 gimple stmt = gsi_stmt (*gsi);
1939 switch (gimple_code (stmt))
1941 case GIMPLE_CALL:
1943 tree fndecl = gimple_call_fndecl (stmt);
1944 tree rhs, lhs;
1946 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
1947 switch (DECL_FUNCTION_CODE (fndecl))
1949 case BUILT_IN_EH_POINTER:
1950 /* The front end may have generated a call to
1951 __builtin_eh_pointer (0) within a catch region. Replace
1952 this zero argument with the current catch region number. */
1953 if (state->ehp_region)
1955 tree nr = build_int_cst (integer_type_node,
1956 state->ehp_region->index);
1957 gimple_call_set_arg (stmt, 0, nr);
1959 else
1961 /* The user has dome something silly. Remove it. */
1962 rhs = null_pointer_node;
1963 goto do_replace;
1965 break;
1967 case BUILT_IN_EH_FILTER:
1968 /* ??? This should never appear, but since it's a builtin it
1969 is accessible to abuse by users. Just remove it and
1970 replace the use with the arbitrary value zero. */
1971 rhs = build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
1972 do_replace:
1973 lhs = gimple_call_lhs (stmt);
1974 x = gimple_build_assign (lhs, rhs);
1975 gsi_insert_before (gsi, x, GSI_SAME_STMT);
1976 /* FALLTHRU */
1978 case BUILT_IN_EH_COPY_VALUES:
1979 /* Likewise this should not appear. Remove it. */
1980 gsi_remove (gsi, true);
1981 return;
1983 default:
1984 break;
1987 /* FALLTHRU */
1989 case GIMPLE_ASSIGN:
1990 /* If the stmt can throw use a new temporary for the assignment
1991 to a LHS. This makes sure the old value of the LHS is
1992 available on the EH edge. Only do so for statements that
1993 potentially fall through (no noreturn calls e.g.), otherwise
1994 this new assignment might create fake fallthru regions. */
1995 if (stmt_could_throw_p (stmt)
1996 && gimple_has_lhs (stmt)
1997 && gimple_stmt_may_fallthru (stmt)
1998 && !tree_could_throw_p (gimple_get_lhs (stmt))
1999 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
2001 tree lhs = gimple_get_lhs (stmt);
2002 tree tmp = create_tmp_var (TREE_TYPE (lhs), NULL);
2003 gimple s = gimple_build_assign (lhs, tmp);
2004 gimple_set_location (s, gimple_location (stmt));
2005 gimple_set_block (s, gimple_block (stmt));
2006 gimple_set_lhs (stmt, tmp);
2007 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
2008 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
2009 DECL_GIMPLE_REG_P (tmp) = 1;
2010 gsi_insert_after (gsi, s, GSI_SAME_STMT);
2012 /* Look for things that can throw exceptions, and record them. */
2013 if (state->cur_region && stmt_could_throw_p (stmt))
2015 record_stmt_eh_region (state->cur_region, stmt);
2016 note_eh_region_may_contain_throw (state->cur_region);
2018 break;
2020 case GIMPLE_COND:
2021 case GIMPLE_GOTO:
2022 case GIMPLE_RETURN:
2023 maybe_record_in_goto_queue (state, stmt);
2024 break;
2026 case GIMPLE_SWITCH:
2027 verify_norecord_switch_expr (state, stmt);
2028 break;
2030 case GIMPLE_TRY:
2031 if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
2032 replace = lower_try_finally (state, stmt);
2033 else
2035 x = gimple_seq_first_stmt (gimple_try_cleanup (stmt));
2036 if (!x)
2038 replace = gimple_try_eval (stmt);
2039 lower_eh_constructs_1 (state, &replace);
2041 else
2042 switch (gimple_code (x))
2044 case GIMPLE_CATCH:
2045 replace = lower_catch (state, stmt);
2046 break;
2047 case GIMPLE_EH_FILTER:
2048 replace = lower_eh_filter (state, stmt);
2049 break;
2050 case GIMPLE_EH_MUST_NOT_THROW:
2051 replace = lower_eh_must_not_throw (state, stmt);
2052 break;
2053 case GIMPLE_EH_ELSE:
2054 /* This code is only valid with GIMPLE_TRY_FINALLY. */
2055 gcc_unreachable ();
2056 default:
2057 replace = lower_cleanup (state, stmt);
2058 break;
2062 /* Remove the old stmt and insert the transformed sequence
2063 instead. */
2064 gsi_insert_seq_before (gsi, replace, GSI_SAME_STMT);
2065 gsi_remove (gsi, true);
2067 /* Return since we don't want gsi_next () */
2068 return;
2070 case GIMPLE_EH_ELSE:
2071 /* We should be eliminating this in lower_try_finally et al. */
2072 gcc_unreachable ();
2074 default:
2075 /* A type, a decl, or some kind of statement that we're not
2076 interested in. Don't walk them. */
2077 break;
2080 gsi_next (gsi);
2083 /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2085 static void
2086 lower_eh_constructs_1 (struct leh_state *state, gimple_seq *pseq)
2088 gimple_stmt_iterator gsi;
2089 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi);)
2090 lower_eh_constructs_2 (state, &gsi);
2093 static unsigned int
2094 lower_eh_constructs (void)
2096 struct leh_state null_state;
2097 gimple_seq bodyp;
2099 bodyp = gimple_body (current_function_decl);
2100 if (bodyp == NULL)
2101 return 0;
2103 finally_tree = htab_create (31, struct_ptr_hash, struct_ptr_eq, free);
2104 eh_region_may_contain_throw_map = BITMAP_ALLOC (NULL);
2105 memset (&null_state, 0, sizeof (null_state));
2107 collect_finally_tree_1 (bodyp, NULL);
2108 lower_eh_constructs_1 (&null_state, &bodyp);
2109 gimple_set_body (current_function_decl, bodyp);
2111 /* We assume there's a return statement, or something, at the end of
2112 the function, and thus ploping the EH sequence afterward won't
2113 change anything. */
2114 gcc_assert (!gimple_seq_may_fallthru (bodyp));
2115 gimple_seq_add_seq (&bodyp, eh_seq);
2117 /* We assume that since BODYP already existed, adding EH_SEQ to it
2118 didn't change its value, and we don't have to re-set the function. */
2119 gcc_assert (bodyp == gimple_body (current_function_decl));
2121 htab_delete (finally_tree);
2122 BITMAP_FREE (eh_region_may_contain_throw_map);
2123 eh_seq = NULL;
2125 /* If this function needs a language specific EH personality routine
2126 and the frontend didn't already set one do so now. */
2127 if (function_needs_eh_personality (cfun) == eh_personality_lang
2128 && !DECL_FUNCTION_PERSONALITY (current_function_decl))
2129 DECL_FUNCTION_PERSONALITY (current_function_decl)
2130 = lang_hooks.eh_personality ();
2132 return 0;
2135 struct gimple_opt_pass pass_lower_eh =
2138 GIMPLE_PASS,
2139 "eh", /* name */
2140 OPTGROUP_NONE, /* optinfo_flags */
2141 NULL, /* gate */
2142 lower_eh_constructs, /* execute */
2143 NULL, /* sub */
2144 NULL, /* next */
2145 0, /* static_pass_number */
2146 TV_TREE_EH, /* tv_id */
2147 PROP_gimple_lcf, /* properties_required */
2148 PROP_gimple_leh, /* properties_provided */
2149 0, /* properties_destroyed */
2150 0, /* todo_flags_start */
2151 0 /* todo_flags_finish */
2155 /* Create the multiple edges from an EH_DISPATCH statement to all of
2156 the possible handlers for its EH region. Return true if there's
2157 no fallthru edge; false if there is. */
2159 bool
2160 make_eh_dispatch_edges (gimple stmt)
2162 eh_region r;
2163 eh_catch c;
2164 basic_block src, dst;
2166 r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
2167 src = gimple_bb (stmt);
2169 switch (r->type)
2171 case ERT_TRY:
2172 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
2174 dst = label_to_block (c->label);
2175 make_edge (src, dst, 0);
2177 /* A catch-all handler doesn't have a fallthru. */
2178 if (c->type_list == NULL)
2179 return false;
2181 break;
2183 case ERT_ALLOWED_EXCEPTIONS:
2184 dst = label_to_block (r->u.allowed.label);
2185 make_edge (src, dst, 0);
2186 break;
2188 default:
2189 gcc_unreachable ();
2192 return true;
2195 /* Create the single EH edge from STMT to its nearest landing pad,
2196 if there is such a landing pad within the current function. */
2198 void
2199 make_eh_edges (gimple stmt)
2201 basic_block src, dst;
2202 eh_landing_pad lp;
2203 int lp_nr;
2205 lp_nr = lookup_stmt_eh_lp (stmt);
2206 if (lp_nr <= 0)
2207 return;
2209 lp = get_eh_landing_pad_from_number (lp_nr);
2210 gcc_assert (lp != NULL);
2212 src = gimple_bb (stmt);
2213 dst = label_to_block (lp->post_landing_pad);
2214 make_edge (src, dst, EDGE_EH);
2217 /* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2218 do not actually perform the final edge redirection.
2220 CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2221 we intend to change the destination EH region as well; this means
2222 EH_LANDING_PAD_NR must already be set on the destination block label.
2223 If false, we're being called from generic cfg manipulation code and we
2224 should preserve our place within the region tree. */
2226 static void
2227 redirect_eh_edge_1 (edge edge_in, basic_block new_bb, bool change_region)
2229 eh_landing_pad old_lp, new_lp;
2230 basic_block old_bb;
2231 gimple throw_stmt;
2232 int old_lp_nr, new_lp_nr;
2233 tree old_label, new_label;
2234 edge_iterator ei;
2235 edge e;
2237 old_bb = edge_in->dest;
2238 old_label = gimple_block_label (old_bb);
2239 old_lp_nr = EH_LANDING_PAD_NR (old_label);
2240 gcc_assert (old_lp_nr > 0);
2241 old_lp = get_eh_landing_pad_from_number (old_lp_nr);
2243 throw_stmt = last_stmt (edge_in->src);
2244 gcc_assert (lookup_stmt_eh_lp (throw_stmt) == old_lp_nr);
2246 new_label = gimple_block_label (new_bb);
2248 /* Look for an existing region that might be using NEW_BB already. */
2249 new_lp_nr = EH_LANDING_PAD_NR (new_label);
2250 if (new_lp_nr)
2252 new_lp = get_eh_landing_pad_from_number (new_lp_nr);
2253 gcc_assert (new_lp);
2255 /* Unless CHANGE_REGION is true, the new and old landing pad
2256 had better be associated with the same EH region. */
2257 gcc_assert (change_region || new_lp->region == old_lp->region);
2259 else
2261 new_lp = NULL;
2262 gcc_assert (!change_region);
2265 /* Notice when we redirect the last EH edge away from OLD_BB. */
2266 FOR_EACH_EDGE (e, ei, old_bb->preds)
2267 if (e != edge_in && (e->flags & EDGE_EH))
2268 break;
2270 if (new_lp)
2272 /* NEW_LP already exists. If there are still edges into OLD_LP,
2273 there's nothing to do with the EH tree. If there are no more
2274 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2275 If CHANGE_REGION is true, then our caller is expecting to remove
2276 the landing pad. */
2277 if (e == NULL && !change_region)
2278 remove_eh_landing_pad (old_lp);
2280 else
2282 /* No correct landing pad exists. If there are no more edges
2283 into OLD_LP, then we can simply re-use the existing landing pad.
2284 Otherwise, we have to create a new landing pad. */
2285 if (e == NULL)
2287 EH_LANDING_PAD_NR (old_lp->post_landing_pad) = 0;
2288 new_lp = old_lp;
2290 else
2291 new_lp = gen_eh_landing_pad (old_lp->region);
2292 new_lp->post_landing_pad = new_label;
2293 EH_LANDING_PAD_NR (new_label) = new_lp->index;
2296 /* Maybe move the throwing statement to the new region. */
2297 if (old_lp != new_lp)
2299 remove_stmt_from_eh_lp (throw_stmt);
2300 add_stmt_to_eh_lp (throw_stmt, new_lp->index);
2304 /* Redirect EH edge E to NEW_BB. */
2306 edge
2307 redirect_eh_edge (edge edge_in, basic_block new_bb)
2309 redirect_eh_edge_1 (edge_in, new_bb, false);
2310 return ssa_redirect_edge (edge_in, new_bb);
2313 /* This is a subroutine of gimple_redirect_edge_and_branch. Update the
2314 labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2315 The actual edge update will happen in the caller. */
2317 void
2318 redirect_eh_dispatch_edge (gimple stmt, edge e, basic_block new_bb)
2320 tree new_lab = gimple_block_label (new_bb);
2321 bool any_changed = false;
2322 basic_block old_bb;
2323 eh_region r;
2324 eh_catch c;
2326 r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
2327 switch (r->type)
2329 case ERT_TRY:
2330 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
2332 old_bb = label_to_block (c->label);
2333 if (old_bb == e->dest)
2335 c->label = new_lab;
2336 any_changed = true;
2339 break;
2341 case ERT_ALLOWED_EXCEPTIONS:
2342 old_bb = label_to_block (r->u.allowed.label);
2343 gcc_assert (old_bb == e->dest);
2344 r->u.allowed.label = new_lab;
2345 any_changed = true;
2346 break;
2348 default:
2349 gcc_unreachable ();
2352 gcc_assert (any_changed);
2355 /* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2357 bool
2358 operation_could_trap_helper_p (enum tree_code op,
2359 bool fp_operation,
2360 bool honor_trapv,
2361 bool honor_nans,
2362 bool honor_snans,
2363 tree divisor,
2364 bool *handled)
2366 *handled = true;
2367 switch (op)
2369 case TRUNC_DIV_EXPR:
2370 case CEIL_DIV_EXPR:
2371 case FLOOR_DIV_EXPR:
2372 case ROUND_DIV_EXPR:
2373 case EXACT_DIV_EXPR:
2374 case CEIL_MOD_EXPR:
2375 case FLOOR_MOD_EXPR:
2376 case ROUND_MOD_EXPR:
2377 case TRUNC_MOD_EXPR:
2378 case RDIV_EXPR:
2379 if (honor_snans || honor_trapv)
2380 return true;
2381 if (fp_operation)
2382 return flag_trapping_math;
2383 if (!TREE_CONSTANT (divisor) || integer_zerop (divisor))
2384 return true;
2385 return false;
2387 case LT_EXPR:
2388 case LE_EXPR:
2389 case GT_EXPR:
2390 case GE_EXPR:
2391 case LTGT_EXPR:
2392 /* Some floating point comparisons may trap. */
2393 return honor_nans;
2395 case EQ_EXPR:
2396 case NE_EXPR:
2397 case UNORDERED_EXPR:
2398 case ORDERED_EXPR:
2399 case UNLT_EXPR:
2400 case UNLE_EXPR:
2401 case UNGT_EXPR:
2402 case UNGE_EXPR:
2403 case UNEQ_EXPR:
2404 return honor_snans;
2406 case CONVERT_EXPR:
2407 case FIX_TRUNC_EXPR:
2408 /* Conversion of floating point might trap. */
2409 return honor_nans;
2411 case NEGATE_EXPR:
2412 case ABS_EXPR:
2413 case CONJ_EXPR:
2414 /* These operations don't trap with floating point. */
2415 if (honor_trapv)
2416 return true;
2417 return false;
2419 case PLUS_EXPR:
2420 case MINUS_EXPR:
2421 case MULT_EXPR:
2422 /* Any floating arithmetic may trap. */
2423 if (fp_operation && flag_trapping_math)
2424 return true;
2425 if (honor_trapv)
2426 return true;
2427 return false;
2429 case COMPLEX_EXPR:
2430 case CONSTRUCTOR:
2431 /* Constructing an object cannot trap. */
2432 return false;
2434 default:
2435 /* Any floating arithmetic may trap. */
2436 if (fp_operation && flag_trapping_math)
2437 return true;
2439 *handled = false;
2440 return false;
2444 /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2445 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2446 type operands that may trap. If OP is a division operator, DIVISOR contains
2447 the value of the divisor. */
2449 bool
2450 operation_could_trap_p (enum tree_code op, bool fp_operation, bool honor_trapv,
2451 tree divisor)
2453 bool honor_nans = (fp_operation && flag_trapping_math
2454 && !flag_finite_math_only);
2455 bool honor_snans = fp_operation && flag_signaling_nans != 0;
2456 bool handled;
2458 if (TREE_CODE_CLASS (op) != tcc_comparison
2459 && TREE_CODE_CLASS (op) != tcc_unary
2460 && TREE_CODE_CLASS (op) != tcc_binary)
2461 return false;
2463 return operation_could_trap_helper_p (op, fp_operation, honor_trapv,
2464 honor_nans, honor_snans, divisor,
2465 &handled);
2468 /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2469 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2470 This routine expects only GIMPLE lhs or rhs input. */
2472 bool
2473 tree_could_trap_p (tree expr)
2475 enum tree_code code;
2476 bool fp_operation = false;
2477 bool honor_trapv = false;
2478 tree t, base, div = NULL_TREE;
2480 if (!expr)
2481 return false;
2483 code = TREE_CODE (expr);
2484 t = TREE_TYPE (expr);
2486 if (t)
2488 if (COMPARISON_CLASS_P (expr))
2489 fp_operation = FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
2490 else
2491 fp_operation = FLOAT_TYPE_P (t);
2492 honor_trapv = INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t);
2495 if (TREE_CODE_CLASS (code) == tcc_binary)
2496 div = TREE_OPERAND (expr, 1);
2497 if (operation_could_trap_p (code, fp_operation, honor_trapv, div))
2498 return true;
2500 restart:
2501 switch (code)
2503 case COMPONENT_REF:
2504 case REALPART_EXPR:
2505 case IMAGPART_EXPR:
2506 case BIT_FIELD_REF:
2507 case VIEW_CONVERT_EXPR:
2508 case WITH_SIZE_EXPR:
2509 expr = TREE_OPERAND (expr, 0);
2510 code = TREE_CODE (expr);
2511 goto restart;
2513 case ARRAY_RANGE_REF:
2514 base = TREE_OPERAND (expr, 0);
2515 if (tree_could_trap_p (base))
2516 return true;
2517 if (TREE_THIS_NOTRAP (expr))
2518 return false;
2519 return !range_in_array_bounds_p (expr);
2521 case ARRAY_REF:
2522 base = TREE_OPERAND (expr, 0);
2523 if (tree_could_trap_p (base))
2524 return true;
2525 if (TREE_THIS_NOTRAP (expr))
2526 return false;
2527 return !in_array_bounds_p (expr);
2529 case TARGET_MEM_REF:
2530 case MEM_REF:
2531 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
2532 && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
2533 return true;
2534 if (TREE_THIS_NOTRAP (expr))
2535 return false;
2536 /* We cannot prove that the access is in-bounds when we have
2537 variable-index TARGET_MEM_REFs. */
2538 if (code == TARGET_MEM_REF
2539 && (TMR_INDEX (expr) || TMR_INDEX2 (expr)))
2540 return true;
2541 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
2543 tree base = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2544 double_int off = mem_ref_offset (expr);
2545 if (off.is_negative ())
2546 return true;
2547 if (TREE_CODE (base) == STRING_CST)
2548 return double_int::from_uhwi (TREE_STRING_LENGTH (base)).ule (off);
2549 else if (DECL_SIZE_UNIT (base) == NULL_TREE
2550 || TREE_CODE (DECL_SIZE_UNIT (base)) != INTEGER_CST
2551 || tree_to_double_int (DECL_SIZE_UNIT (base)).ule (off))
2552 return true;
2553 /* Now we are sure the first byte of the access is inside
2554 the object. */
2555 return false;
2557 return true;
2559 case INDIRECT_REF:
2560 return !TREE_THIS_NOTRAP (expr);
2562 case ASM_EXPR:
2563 return TREE_THIS_VOLATILE (expr);
2565 case CALL_EXPR:
2566 t = get_callee_fndecl (expr);
2567 /* Assume that calls to weak functions may trap. */
2568 if (!t || !DECL_P (t))
2569 return true;
2570 if (DECL_WEAK (t))
2571 return tree_could_trap_p (t);
2572 return false;
2574 case FUNCTION_DECL:
2575 /* Assume that accesses to weak functions may trap, unless we know
2576 they are certainly defined in current TU or in some other
2577 LTO partition. */
2578 if (DECL_WEAK (expr))
2580 struct cgraph_node *node;
2581 if (!DECL_EXTERNAL (expr))
2582 return false;
2583 node = cgraph_function_node (cgraph_get_node (expr), NULL);
2584 if (node && node->symbol.in_other_partition)
2585 return false;
2586 return true;
2588 return false;
2590 case VAR_DECL:
2591 /* Assume that accesses to weak vars may trap, unless we know
2592 they are certainly defined in current TU or in some other
2593 LTO partition. */
2594 if (DECL_WEAK (expr))
2596 struct varpool_node *node;
2597 if (!DECL_EXTERNAL (expr))
2598 return false;
2599 node = varpool_variable_node (varpool_get_node (expr), NULL);
2600 if (node && node->symbol.in_other_partition)
2601 return false;
2602 return true;
2604 return false;
2606 default:
2607 return false;
2612 /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2613 an assignment or a conditional) may throw. */
2615 static bool
2616 stmt_could_throw_1_p (gimple stmt)
2618 enum tree_code code = gimple_expr_code (stmt);
2619 bool honor_nans = false;
2620 bool honor_snans = false;
2621 bool fp_operation = false;
2622 bool honor_trapv = false;
2623 tree t;
2624 size_t i;
2625 bool handled, ret;
2627 if (TREE_CODE_CLASS (code) == tcc_comparison
2628 || TREE_CODE_CLASS (code) == tcc_unary
2629 || TREE_CODE_CLASS (code) == tcc_binary)
2631 if (is_gimple_assign (stmt)
2632 && TREE_CODE_CLASS (code) == tcc_comparison)
2633 t = TREE_TYPE (gimple_assign_rhs1 (stmt));
2634 else if (gimple_code (stmt) == GIMPLE_COND)
2635 t = TREE_TYPE (gimple_cond_lhs (stmt));
2636 else
2637 t = gimple_expr_type (stmt);
2638 fp_operation = FLOAT_TYPE_P (t);
2639 if (fp_operation)
2641 honor_nans = flag_trapping_math && !flag_finite_math_only;
2642 honor_snans = flag_signaling_nans != 0;
2644 else if (INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t))
2645 honor_trapv = true;
2648 /* Check if the main expression may trap. */
2649 t = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) : NULL;
2650 ret = operation_could_trap_helper_p (code, fp_operation, honor_trapv,
2651 honor_nans, honor_snans, t,
2652 &handled);
2653 if (handled)
2654 return ret;
2656 /* If the expression does not trap, see if any of the individual operands may
2657 trap. */
2658 for (i = 0; i < gimple_num_ops (stmt); i++)
2659 if (tree_could_trap_p (gimple_op (stmt, i)))
2660 return true;
2662 return false;
2666 /* Return true if statement STMT could throw an exception. */
2668 bool
2669 stmt_could_throw_p (gimple stmt)
2671 if (!flag_exceptions)
2672 return false;
2674 /* The only statements that can throw an exception are assignments,
2675 conditionals, calls, resx, and asms. */
2676 switch (gimple_code (stmt))
2678 case GIMPLE_RESX:
2679 return true;
2681 case GIMPLE_CALL:
2682 return !gimple_call_nothrow_p (stmt);
2684 case GIMPLE_ASSIGN:
2685 case GIMPLE_COND:
2686 if (!cfun->can_throw_non_call_exceptions)
2687 return false;
2688 return stmt_could_throw_1_p (stmt);
2690 case GIMPLE_ASM:
2691 if (!cfun->can_throw_non_call_exceptions)
2692 return false;
2693 return gimple_asm_volatile_p (stmt);
2695 default:
2696 return false;
2701 /* Return true if expression T could throw an exception. */
2703 bool
2704 tree_could_throw_p (tree t)
2706 if (!flag_exceptions)
2707 return false;
2708 if (TREE_CODE (t) == MODIFY_EXPR)
2710 if (cfun->can_throw_non_call_exceptions
2711 && tree_could_trap_p (TREE_OPERAND (t, 0)))
2712 return true;
2713 t = TREE_OPERAND (t, 1);
2716 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2717 t = TREE_OPERAND (t, 0);
2718 if (TREE_CODE (t) == CALL_EXPR)
2719 return (call_expr_flags (t) & ECF_NOTHROW) == 0;
2720 if (cfun->can_throw_non_call_exceptions)
2721 return tree_could_trap_p (t);
2722 return false;
2725 /* Return true if STMT can throw an exception that is not caught within
2726 the current function (CFUN). */
2728 bool
2729 stmt_can_throw_external (gimple stmt)
2731 int lp_nr;
2733 if (!stmt_could_throw_p (stmt))
2734 return false;
2736 lp_nr = lookup_stmt_eh_lp (stmt);
2737 return lp_nr == 0;
2740 /* Return true if STMT can throw an exception that is caught within
2741 the current function (CFUN). */
2743 bool
2744 stmt_can_throw_internal (gimple stmt)
2746 int lp_nr;
2748 if (!stmt_could_throw_p (stmt))
2749 return false;
2751 lp_nr = lookup_stmt_eh_lp (stmt);
2752 return lp_nr > 0;
2755 /* Given a statement STMT in IFUN, if STMT can no longer throw, then
2756 remove any entry it might have from the EH table. Return true if
2757 any change was made. */
2759 bool
2760 maybe_clean_eh_stmt_fn (struct function *ifun, gimple stmt)
2762 if (stmt_could_throw_p (stmt))
2763 return false;
2764 return remove_stmt_from_eh_lp_fn (ifun, stmt);
2767 /* Likewise, but always use the current function. */
2769 bool
2770 maybe_clean_eh_stmt (gimple stmt)
2772 return maybe_clean_eh_stmt_fn (cfun, stmt);
2775 /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
2776 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
2777 in the table if it should be in there. Return TRUE if a replacement was
2778 done that my require an EH edge purge. */
2780 bool
2781 maybe_clean_or_replace_eh_stmt (gimple old_stmt, gimple new_stmt)
2783 int lp_nr = lookup_stmt_eh_lp (old_stmt);
2785 if (lp_nr != 0)
2787 bool new_stmt_could_throw = stmt_could_throw_p (new_stmt);
2789 if (new_stmt == old_stmt && new_stmt_could_throw)
2790 return false;
2792 remove_stmt_from_eh_lp (old_stmt);
2793 if (new_stmt_could_throw)
2795 add_stmt_to_eh_lp (new_stmt, lp_nr);
2796 return false;
2798 else
2799 return true;
2802 return false;
2805 /* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
2806 in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
2807 operand is the return value of duplicate_eh_regions. */
2809 bool
2810 maybe_duplicate_eh_stmt_fn (struct function *new_fun, gimple new_stmt,
2811 struct function *old_fun, gimple old_stmt,
2812 struct pointer_map_t *map, int default_lp_nr)
2814 int old_lp_nr, new_lp_nr;
2815 void **slot;
2817 if (!stmt_could_throw_p (new_stmt))
2818 return false;
2820 old_lp_nr = lookup_stmt_eh_lp_fn (old_fun, old_stmt);
2821 if (old_lp_nr == 0)
2823 if (default_lp_nr == 0)
2824 return false;
2825 new_lp_nr = default_lp_nr;
2827 else if (old_lp_nr > 0)
2829 eh_landing_pad old_lp, new_lp;
2831 old_lp = (*old_fun->eh->lp_array)[old_lp_nr];
2832 slot = pointer_map_contains (map, old_lp);
2833 new_lp = (eh_landing_pad) *slot;
2834 new_lp_nr = new_lp->index;
2836 else
2838 eh_region old_r, new_r;
2840 old_r = (*old_fun->eh->region_array)[-old_lp_nr];
2841 slot = pointer_map_contains (map, old_r);
2842 new_r = (eh_region) *slot;
2843 new_lp_nr = -new_r->index;
2846 add_stmt_to_eh_lp_fn (new_fun, new_stmt, new_lp_nr);
2847 return true;
2850 /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
2851 and thus no remapping is required. */
2853 bool
2854 maybe_duplicate_eh_stmt (gimple new_stmt, gimple old_stmt)
2856 int lp_nr;
2858 if (!stmt_could_throw_p (new_stmt))
2859 return false;
2861 lp_nr = lookup_stmt_eh_lp (old_stmt);
2862 if (lp_nr == 0)
2863 return false;
2865 add_stmt_to_eh_lp (new_stmt, lp_nr);
2866 return true;
2869 /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
2870 GIMPLE_TRY) that are similar enough to be considered the same. Currently
2871 this only handles handlers consisting of a single call, as that's the
2872 important case for C++: a destructor call for a particular object showing
2873 up in multiple handlers. */
2875 static bool
2876 same_handler_p (gimple_seq oneh, gimple_seq twoh)
2878 gimple_stmt_iterator gsi;
2879 gimple ones, twos;
2880 unsigned int ai;
2882 gsi = gsi_start (oneh);
2883 if (!gsi_one_before_end_p (gsi))
2884 return false;
2885 ones = gsi_stmt (gsi);
2887 gsi = gsi_start (twoh);
2888 if (!gsi_one_before_end_p (gsi))
2889 return false;
2890 twos = gsi_stmt (gsi);
2892 if (!is_gimple_call (ones)
2893 || !is_gimple_call (twos)
2894 || gimple_call_lhs (ones)
2895 || gimple_call_lhs (twos)
2896 || gimple_call_chain (ones)
2897 || gimple_call_chain (twos)
2898 || !gimple_call_same_target_p (ones, twos)
2899 || gimple_call_num_args (ones) != gimple_call_num_args (twos))
2900 return false;
2902 for (ai = 0; ai < gimple_call_num_args (ones); ++ai)
2903 if (!operand_equal_p (gimple_call_arg (ones, ai),
2904 gimple_call_arg (twos, ai), 0))
2905 return false;
2907 return true;
2910 /* Optimize
2911 try { A() } finally { try { ~B() } catch { ~A() } }
2912 try { ... } finally { ~A() }
2913 into
2914 try { A() } catch { ~B() }
2915 try { ~B() ... } finally { ~A() }
2917 This occurs frequently in C++, where A is a local variable and B is a
2918 temporary used in the initializer for A. */
2920 static void
2921 optimize_double_finally (gimple one, gimple two)
2923 gimple oneh;
2924 gimple_stmt_iterator gsi;
2925 gimple_seq cleanup;
2927 cleanup = gimple_try_cleanup (one);
2928 gsi = gsi_start (cleanup);
2929 if (!gsi_one_before_end_p (gsi))
2930 return;
2932 oneh = gsi_stmt (gsi);
2933 if (gimple_code (oneh) != GIMPLE_TRY
2934 || gimple_try_kind (oneh) != GIMPLE_TRY_CATCH)
2935 return;
2937 if (same_handler_p (gimple_try_cleanup (oneh), gimple_try_cleanup (two)))
2939 gimple_seq seq = gimple_try_eval (oneh);
2941 gimple_try_set_cleanup (one, seq);
2942 gimple_try_set_kind (one, GIMPLE_TRY_CATCH);
2943 seq = copy_gimple_seq_and_replace_locals (seq);
2944 gimple_seq_add_seq (&seq, gimple_try_eval (two));
2945 gimple_try_set_eval (two, seq);
2949 /* Perform EH refactoring optimizations that are simpler to do when code
2950 flow has been lowered but EH structures haven't. */
2952 static void
2953 refactor_eh_r (gimple_seq seq)
2955 gimple_stmt_iterator gsi;
2956 gimple one, two;
2958 one = NULL;
2959 two = NULL;
2960 gsi = gsi_start (seq);
2961 while (1)
2963 one = two;
2964 if (gsi_end_p (gsi))
2965 two = NULL;
2966 else
2967 two = gsi_stmt (gsi);
2968 if (one
2969 && two
2970 && gimple_code (one) == GIMPLE_TRY
2971 && gimple_code (two) == GIMPLE_TRY
2972 && gimple_try_kind (one) == GIMPLE_TRY_FINALLY
2973 && gimple_try_kind (two) == GIMPLE_TRY_FINALLY)
2974 optimize_double_finally (one, two);
2975 if (one)
2976 switch (gimple_code (one))
2978 case GIMPLE_TRY:
2979 refactor_eh_r (gimple_try_eval (one));
2980 refactor_eh_r (gimple_try_cleanup (one));
2981 break;
2982 case GIMPLE_CATCH:
2983 refactor_eh_r (gimple_catch_handler (one));
2984 break;
2985 case GIMPLE_EH_FILTER:
2986 refactor_eh_r (gimple_eh_filter_failure (one));
2987 break;
2988 case GIMPLE_EH_ELSE:
2989 refactor_eh_r (gimple_eh_else_n_body (one));
2990 refactor_eh_r (gimple_eh_else_e_body (one));
2991 break;
2992 default:
2993 break;
2995 if (two)
2996 gsi_next (&gsi);
2997 else
2998 break;
3002 static unsigned
3003 refactor_eh (void)
3005 refactor_eh_r (gimple_body (current_function_decl));
3006 return 0;
3009 static bool
3010 gate_refactor_eh (void)
3012 return flag_exceptions != 0;
3015 struct gimple_opt_pass pass_refactor_eh =
3018 GIMPLE_PASS,
3019 "ehopt", /* name */
3020 OPTGROUP_NONE, /* optinfo_flags */
3021 gate_refactor_eh, /* gate */
3022 refactor_eh, /* execute */
3023 NULL, /* sub */
3024 NULL, /* next */
3025 0, /* static_pass_number */
3026 TV_TREE_EH, /* tv_id */
3027 PROP_gimple_lcf, /* properties_required */
3028 0, /* properties_provided */
3029 0, /* properties_destroyed */
3030 0, /* todo_flags_start */
3031 0 /* todo_flags_finish */
3035 /* At the end of gimple optimization, we can lower RESX. */
3037 static bool
3038 lower_resx (basic_block bb, gimple stmt, struct pointer_map_t *mnt_map)
3040 int lp_nr;
3041 eh_region src_r, dst_r;
3042 gimple_stmt_iterator gsi;
3043 gimple x;
3044 tree fn, src_nr;
3045 bool ret = false;
3047 lp_nr = lookup_stmt_eh_lp (stmt);
3048 if (lp_nr != 0)
3049 dst_r = get_eh_region_from_lp_number (lp_nr);
3050 else
3051 dst_r = NULL;
3053 src_r = get_eh_region_from_number (gimple_resx_region (stmt));
3054 gsi = gsi_last_bb (bb);
3056 if (src_r == NULL)
3058 /* We can wind up with no source region when pass_cleanup_eh shows
3059 that there are no entries into an eh region and deletes it, but
3060 then the block that contains the resx isn't removed. This can
3061 happen without optimization when the switch statement created by
3062 lower_try_finally_switch isn't simplified to remove the eh case.
3064 Resolve this by expanding the resx node to an abort. */
3066 fn = builtin_decl_implicit (BUILT_IN_TRAP);
3067 x = gimple_build_call (fn, 0);
3068 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3070 while (EDGE_COUNT (bb->succs) > 0)
3071 remove_edge (EDGE_SUCC (bb, 0));
3073 else if (dst_r)
3075 /* When we have a destination region, we resolve this by copying
3076 the excptr and filter values into place, and changing the edge
3077 to immediately after the landing pad. */
3078 edge e;
3080 if (lp_nr < 0)
3082 basic_block new_bb;
3083 void **slot;
3084 tree lab;
3086 /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3087 the failure decl into a new block, if needed. */
3088 gcc_assert (dst_r->type == ERT_MUST_NOT_THROW);
3090 slot = pointer_map_contains (mnt_map, dst_r);
3091 if (slot == NULL)
3093 gimple_stmt_iterator gsi2;
3095 new_bb = create_empty_bb (bb);
3096 if (current_loops)
3097 add_bb_to_loop (new_bb, bb->loop_father);
3098 lab = gimple_block_label (new_bb);
3099 gsi2 = gsi_start_bb (new_bb);
3101 fn = dst_r->u.must_not_throw.failure_decl;
3102 x = gimple_build_call (fn, 0);
3103 gimple_set_location (x, dst_r->u.must_not_throw.failure_loc);
3104 gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
3106 slot = pointer_map_insert (mnt_map, dst_r);
3107 *slot = lab;
3109 else
3111 lab = (tree) *slot;
3112 new_bb = label_to_block (lab);
3115 gcc_assert (EDGE_COUNT (bb->succs) == 0);
3116 e = make_edge (bb, new_bb, EDGE_FALLTHRU);
3117 e->count = bb->count;
3118 e->probability = REG_BR_PROB_BASE;
3120 else
3122 edge_iterator ei;
3123 tree dst_nr = build_int_cst (integer_type_node, dst_r->index);
3125 fn = builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES);
3126 src_nr = build_int_cst (integer_type_node, src_r->index);
3127 x = gimple_build_call (fn, 2, dst_nr, src_nr);
3128 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3130 /* Update the flags for the outgoing edge. */
3131 e = single_succ_edge (bb);
3132 gcc_assert (e->flags & EDGE_EH);
3133 e->flags = (e->flags & ~EDGE_EH) | EDGE_FALLTHRU;
3135 /* If there are no more EH users of the landing pad, delete it. */
3136 FOR_EACH_EDGE (e, ei, e->dest->preds)
3137 if (e->flags & EDGE_EH)
3138 break;
3139 if (e == NULL)
3141 eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
3142 remove_eh_landing_pad (lp);
3146 ret = true;
3148 else
3150 tree var;
3152 /* When we don't have a destination region, this exception escapes
3153 up the call chain. We resolve this by generating a call to the
3154 _Unwind_Resume library function. */
3156 /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3157 with no arguments for C++ and Java. Check for that. */
3158 if (src_r->use_cxa_end_cleanup)
3160 fn = builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP);
3161 x = gimple_build_call (fn, 0);
3162 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3164 else
3166 fn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
3167 src_nr = build_int_cst (integer_type_node, src_r->index);
3168 x = gimple_build_call (fn, 1, src_nr);
3169 var = create_tmp_var (ptr_type_node, NULL);
3170 var = make_ssa_name (var, x);
3171 gimple_call_set_lhs (x, var);
3172 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3174 fn = builtin_decl_implicit (BUILT_IN_UNWIND_RESUME);
3175 x = gimple_build_call (fn, 1, var);
3176 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3179 gcc_assert (EDGE_COUNT (bb->succs) == 0);
3182 gsi_remove (&gsi, true);
3184 return ret;
3187 static unsigned
3188 execute_lower_resx (void)
3190 basic_block bb;
3191 struct pointer_map_t *mnt_map;
3192 bool dominance_invalidated = false;
3193 bool any_rewritten = false;
3195 mnt_map = pointer_map_create ();
3197 FOR_EACH_BB (bb)
3199 gimple last = last_stmt (bb);
3200 if (last && is_gimple_resx (last))
3202 dominance_invalidated |= lower_resx (bb, last, mnt_map);
3203 any_rewritten = true;
3207 pointer_map_destroy (mnt_map);
3209 if (dominance_invalidated)
3211 free_dominance_info (CDI_DOMINATORS);
3212 free_dominance_info (CDI_POST_DOMINATORS);
3215 return any_rewritten ? TODO_update_ssa_only_virtuals : 0;
3218 static bool
3219 gate_lower_resx (void)
3221 return flag_exceptions != 0;
3224 struct gimple_opt_pass pass_lower_resx =
3227 GIMPLE_PASS,
3228 "resx", /* name */
3229 OPTGROUP_NONE, /* optinfo_flags */
3230 gate_lower_resx, /* gate */
3231 execute_lower_resx, /* execute */
3232 NULL, /* sub */
3233 NULL, /* next */
3234 0, /* static_pass_number */
3235 TV_TREE_EH, /* tv_id */
3236 PROP_gimple_lcf, /* properties_required */
3237 0, /* properties_provided */
3238 0, /* properties_destroyed */
3239 0, /* todo_flags_start */
3240 TODO_verify_flow /* todo_flags_finish */
3244 /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3245 external throw. */
3247 static void
3248 optimize_clobbers (basic_block bb)
3250 gimple_stmt_iterator gsi = gsi_last_bb (bb);
3251 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3253 gimple stmt = gsi_stmt (gsi);
3254 if (is_gimple_debug (stmt))
3255 continue;
3256 if (!gimple_clobber_p (stmt)
3257 || TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
3258 return;
3259 unlink_stmt_vdef (stmt);
3260 gsi_remove (&gsi, true);
3261 release_defs (stmt);
3265 /* Try to sink var = {v} {CLOBBER} stmts followed just by
3266 internal throw to successor BB. */
3268 static int
3269 sink_clobbers (basic_block bb)
3271 edge e;
3272 edge_iterator ei;
3273 gimple_stmt_iterator gsi, dgsi;
3274 basic_block succbb;
3275 bool any_clobbers = false;
3277 /* Only optimize if BB has a single EH successor and
3278 all predecessor edges are EH too. */
3279 if (!single_succ_p (bb)
3280 || (single_succ_edge (bb)->flags & EDGE_EH) == 0)
3281 return 0;
3283 FOR_EACH_EDGE (e, ei, bb->preds)
3285 if ((e->flags & EDGE_EH) == 0)
3286 return 0;
3289 /* And BB contains only CLOBBER stmts before the final
3290 RESX. */
3291 gsi = gsi_last_bb (bb);
3292 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3294 gimple stmt = gsi_stmt (gsi);
3295 if (is_gimple_debug (stmt))
3296 continue;
3297 if (gimple_code (stmt) == GIMPLE_LABEL)
3298 break;
3299 if (!gimple_clobber_p (stmt)
3300 || TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME)
3301 return 0;
3302 any_clobbers = true;
3304 if (!any_clobbers)
3305 return 0;
3307 succbb = single_succ (bb);
3308 dgsi = gsi_after_labels (succbb);
3309 gsi = gsi_last_bb (bb);
3310 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3312 gimple stmt = gsi_stmt (gsi);
3313 if (is_gimple_debug (stmt))
3314 continue;
3315 if (gimple_code (stmt) == GIMPLE_LABEL)
3316 break;
3317 unlink_stmt_vdef (stmt);
3318 gsi_remove (&gsi, false);
3319 /* Trigger the operand scanner to cause renaming for virtual
3320 operands for this statement.
3321 ??? Given the simple structure of this code manually
3322 figuring out the reaching definition should not be too hard. */
3323 if (gimple_vuse (stmt))
3324 gimple_set_vuse (stmt, NULL_TREE);
3325 gsi_insert_before (&dgsi, stmt, GSI_SAME_STMT);
3328 return TODO_update_ssa_only_virtuals;
3331 /* At the end of inlining, we can lower EH_DISPATCH. Return true when
3332 we have found some duplicate labels and removed some edges. */
3334 static bool
3335 lower_eh_dispatch (basic_block src, gimple stmt)
3337 gimple_stmt_iterator gsi;
3338 int region_nr;
3339 eh_region r;
3340 tree filter, fn;
3341 gimple x;
3342 bool redirected = false;
3344 region_nr = gimple_eh_dispatch_region (stmt);
3345 r = get_eh_region_from_number (region_nr);
3347 gsi = gsi_last_bb (src);
3349 switch (r->type)
3351 case ERT_TRY:
3353 vec<tree> labels = vNULL;
3354 tree default_label = NULL;
3355 eh_catch c;
3356 edge_iterator ei;
3357 edge e;
3358 struct pointer_set_t *seen_values = pointer_set_create ();
3360 /* Collect the labels for a switch. Zero the post_landing_pad
3361 field becase we'll no longer have anything keeping these labels
3362 in existence and the optimizer will be free to merge these
3363 blocks at will. */
3364 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
3366 tree tp_node, flt_node, lab = c->label;
3367 bool have_label = false;
3369 c->label = NULL;
3370 tp_node = c->type_list;
3371 flt_node = c->filter_list;
3373 if (tp_node == NULL)
3375 default_label = lab;
3376 break;
3380 /* Filter out duplicate labels that arise when this handler
3381 is shadowed by an earlier one. When no labels are
3382 attached to the handler anymore, we remove
3383 the corresponding edge and then we delete unreachable
3384 blocks at the end of this pass. */
3385 if (! pointer_set_contains (seen_values, TREE_VALUE (flt_node)))
3387 tree t = build_case_label (TREE_VALUE (flt_node),
3388 NULL, lab);
3389 labels.safe_push (t);
3390 pointer_set_insert (seen_values, TREE_VALUE (flt_node));
3391 have_label = true;
3394 tp_node = TREE_CHAIN (tp_node);
3395 flt_node = TREE_CHAIN (flt_node);
3397 while (tp_node);
3398 if (! have_label)
3400 remove_edge (find_edge (src, label_to_block (lab)));
3401 redirected = true;
3405 /* Clean up the edge flags. */
3406 FOR_EACH_EDGE (e, ei, src->succs)
3408 if (e->flags & EDGE_FALLTHRU)
3410 /* If there was no catch-all, use the fallthru edge. */
3411 if (default_label == NULL)
3412 default_label = gimple_block_label (e->dest);
3413 e->flags &= ~EDGE_FALLTHRU;
3416 gcc_assert (default_label != NULL);
3418 /* Don't generate a switch if there's only a default case.
3419 This is common in the form of try { A; } catch (...) { B; }. */
3420 if (!labels.exists ())
3422 e = single_succ_edge (src);
3423 e->flags |= EDGE_FALLTHRU;
3425 else
3427 fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
3428 x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3429 region_nr));
3430 filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)), NULL);
3431 filter = make_ssa_name (filter, x);
3432 gimple_call_set_lhs (x, filter);
3433 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3435 /* Turn the default label into a default case. */
3436 default_label = build_case_label (NULL, NULL, default_label);
3437 sort_case_labels (labels);
3439 x = gimple_build_switch (filter, default_label, labels);
3440 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3442 labels.release ();
3444 pointer_set_destroy (seen_values);
3446 break;
3448 case ERT_ALLOWED_EXCEPTIONS:
3450 edge b_e = BRANCH_EDGE (src);
3451 edge f_e = FALLTHRU_EDGE (src);
3453 fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
3454 x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3455 region_nr));
3456 filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)), NULL);
3457 filter = make_ssa_name (filter, x);
3458 gimple_call_set_lhs (x, filter);
3459 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3461 r->u.allowed.label = NULL;
3462 x = gimple_build_cond (EQ_EXPR, filter,
3463 build_int_cst (TREE_TYPE (filter),
3464 r->u.allowed.filter),
3465 NULL_TREE, NULL_TREE);
3466 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3468 b_e->flags = b_e->flags | EDGE_TRUE_VALUE;
3469 f_e->flags = (f_e->flags & ~EDGE_FALLTHRU) | EDGE_FALSE_VALUE;
3471 break;
3473 default:
3474 gcc_unreachable ();
3477 /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3478 gsi_remove (&gsi, true);
3479 return redirected;
3482 static unsigned
3483 execute_lower_eh_dispatch (void)
3485 basic_block bb;
3486 int flags = 0;
3487 bool redirected = false;
3489 assign_filter_values ();
3491 FOR_EACH_BB (bb)
3493 gimple last = last_stmt (bb);
3494 if (last == NULL)
3495 continue;
3496 if (gimple_code (last) == GIMPLE_EH_DISPATCH)
3498 redirected |= lower_eh_dispatch (bb, last);
3499 flags |= TODO_update_ssa_only_virtuals;
3501 else if (gimple_code (last) == GIMPLE_RESX)
3503 if (stmt_can_throw_external (last))
3504 optimize_clobbers (bb);
3505 else
3506 flags |= sink_clobbers (bb);
3510 if (redirected)
3511 delete_unreachable_blocks ();
3512 return flags;
3515 static bool
3516 gate_lower_eh_dispatch (void)
3518 return cfun->eh->region_tree != NULL;
3521 struct gimple_opt_pass pass_lower_eh_dispatch =
3524 GIMPLE_PASS,
3525 "ehdisp", /* name */
3526 OPTGROUP_NONE, /* optinfo_flags */
3527 gate_lower_eh_dispatch, /* gate */
3528 execute_lower_eh_dispatch, /* execute */
3529 NULL, /* sub */
3530 NULL, /* next */
3531 0, /* static_pass_number */
3532 TV_TREE_EH, /* tv_id */
3533 PROP_gimple_lcf, /* properties_required */
3534 0, /* properties_provided */
3535 0, /* properties_destroyed */
3536 0, /* todo_flags_start */
3537 TODO_verify_flow /* todo_flags_finish */
3541 /* Walk statements, see what regions and, optionally, landing pads
3542 are really referenced.
3544 Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
3545 and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
3547 Passing NULL for LP_REACHABLE is valid, in this case only reachable
3548 regions are marked.
3550 The caller is responsible for freeing the returned sbitmaps. */
3552 static void
3553 mark_reachable_handlers (sbitmap *r_reachablep, sbitmap *lp_reachablep)
3555 sbitmap r_reachable, lp_reachable;
3556 basic_block bb;
3557 bool mark_landing_pads = (lp_reachablep != NULL);
3558 gcc_checking_assert (r_reachablep != NULL);
3560 r_reachable = sbitmap_alloc (cfun->eh->region_array->length ());
3561 bitmap_clear (r_reachable);
3562 *r_reachablep = r_reachable;
3564 if (mark_landing_pads)
3566 lp_reachable = sbitmap_alloc (cfun->eh->lp_array->length ());
3567 bitmap_clear (lp_reachable);
3568 *lp_reachablep = lp_reachable;
3570 else
3571 lp_reachable = NULL;
3573 FOR_EACH_BB (bb)
3575 gimple_stmt_iterator gsi;
3577 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3579 gimple stmt = gsi_stmt (gsi);
3581 if (mark_landing_pads)
3583 int lp_nr = lookup_stmt_eh_lp (stmt);
3585 /* Negative LP numbers are MUST_NOT_THROW regions which
3586 are not considered BB enders. */
3587 if (lp_nr < 0)
3588 bitmap_set_bit (r_reachable, -lp_nr);
3590 /* Positive LP numbers are real landing pads, and BB enders. */
3591 else if (lp_nr > 0)
3593 gcc_assert (gsi_one_before_end_p (gsi));
3594 eh_region region = get_eh_region_from_lp_number (lp_nr);
3595 bitmap_set_bit (r_reachable, region->index);
3596 bitmap_set_bit (lp_reachable, lp_nr);
3600 /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
3601 switch (gimple_code (stmt))
3603 case GIMPLE_RESX:
3604 bitmap_set_bit (r_reachable, gimple_resx_region (stmt));
3605 break;
3606 case GIMPLE_EH_DISPATCH:
3607 bitmap_set_bit (r_reachable, gimple_eh_dispatch_region (stmt));
3608 break;
3609 default:
3610 break;
3616 /* Remove unreachable handlers and unreachable landing pads. */
3618 static void
3619 remove_unreachable_handlers (void)
3621 sbitmap r_reachable, lp_reachable;
3622 eh_region region;
3623 eh_landing_pad lp;
3624 unsigned i;
3626 mark_reachable_handlers (&r_reachable, &lp_reachable);
3628 if (dump_file)
3630 fprintf (dump_file, "Before removal of unreachable regions:\n");
3631 dump_eh_tree (dump_file, cfun);
3632 fprintf (dump_file, "Reachable regions: ");
3633 dump_bitmap_file (dump_file, r_reachable);
3634 fprintf (dump_file, "Reachable landing pads: ");
3635 dump_bitmap_file (dump_file, lp_reachable);
3638 if (dump_file)
3640 FOR_EACH_VEC_SAFE_ELT (cfun->eh->region_array, i, region)
3641 if (region && !bitmap_bit_p (r_reachable, region->index))
3642 fprintf (dump_file,
3643 "Removing unreachable region %d\n",
3644 region->index);
3647 remove_unreachable_eh_regions (r_reachable);
3649 FOR_EACH_VEC_SAFE_ELT (cfun->eh->lp_array, i, lp)
3650 if (lp && !bitmap_bit_p (lp_reachable, lp->index))
3652 if (dump_file)
3653 fprintf (dump_file,
3654 "Removing unreachable landing pad %d\n",
3655 lp->index);
3656 remove_eh_landing_pad (lp);
3659 if (dump_file)
3661 fprintf (dump_file, "\n\nAfter removal of unreachable regions:\n");
3662 dump_eh_tree (dump_file, cfun);
3663 fprintf (dump_file, "\n\n");
3666 sbitmap_free (r_reachable);
3667 sbitmap_free (lp_reachable);
3669 #ifdef ENABLE_CHECKING
3670 verify_eh_tree (cfun);
3671 #endif
3674 /* Remove unreachable handlers if any landing pads have been removed after
3675 last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
3677 void
3678 maybe_remove_unreachable_handlers (void)
3680 eh_landing_pad lp;
3681 unsigned i;
3683 if (cfun->eh == NULL)
3684 return;
3686 FOR_EACH_VEC_SAFE_ELT (cfun->eh->lp_array, i, lp)
3687 if (lp && lp->post_landing_pad)
3689 if (label_to_block (lp->post_landing_pad) == NULL)
3691 remove_unreachable_handlers ();
3692 return;
3697 /* Remove regions that do not have landing pads. This assumes
3698 that remove_unreachable_handlers has already been run, and
3699 that we've just manipulated the landing pads since then.
3701 Preserve regions with landing pads and regions that prevent
3702 exceptions from propagating further, even if these regions
3703 are not reachable. */
3705 static void
3706 remove_unreachable_handlers_no_lp (void)
3708 eh_region region;
3709 sbitmap r_reachable;
3710 unsigned i;
3712 mark_reachable_handlers (&r_reachable, /*lp_reachablep=*/NULL);
3714 FOR_EACH_VEC_SAFE_ELT (cfun->eh->region_array, i, region)
3716 if (! region)
3717 continue;
3719 if (region->landing_pads != NULL
3720 || region->type == ERT_MUST_NOT_THROW)
3721 bitmap_set_bit (r_reachable, region->index);
3723 if (dump_file
3724 && !bitmap_bit_p (r_reachable, region->index))
3725 fprintf (dump_file,
3726 "Removing unreachable region %d\n",
3727 region->index);
3730 remove_unreachable_eh_regions (r_reachable);
3732 sbitmap_free (r_reachable);
3735 /* Undo critical edge splitting on an EH landing pad. Earlier, we
3736 optimisticaly split all sorts of edges, including EH edges. The
3737 optimization passes in between may not have needed them; if not,
3738 we should undo the split.
3740 Recognize this case by having one EH edge incoming to the BB and
3741 one normal edge outgoing; BB should be empty apart from the
3742 post_landing_pad label.
3744 Note that this is slightly different from the empty handler case
3745 handled by cleanup_empty_eh, in that the actual handler may yet
3746 have actual code but the landing pad has been separated from the
3747 handler. As such, cleanup_empty_eh relies on this transformation
3748 having been done first. */
3750 static bool
3751 unsplit_eh (eh_landing_pad lp)
3753 basic_block bb = label_to_block (lp->post_landing_pad);
3754 gimple_stmt_iterator gsi;
3755 edge e_in, e_out;
3757 /* Quickly check the edge counts on BB for singularity. */
3758 if (EDGE_COUNT (bb->preds) != 1 || EDGE_COUNT (bb->succs) != 1)
3759 return false;
3760 e_in = EDGE_PRED (bb, 0);
3761 e_out = EDGE_SUCC (bb, 0);
3763 /* Input edge must be EH and output edge must be normal. */
3764 if ((e_in->flags & EDGE_EH) == 0 || (e_out->flags & EDGE_EH) != 0)
3765 return false;
3767 /* The block must be empty except for the labels and debug insns. */
3768 gsi = gsi_after_labels (bb);
3769 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
3770 gsi_next_nondebug (&gsi);
3771 if (!gsi_end_p (gsi))
3772 return false;
3774 /* The destination block must not already have a landing pad
3775 for a different region. */
3776 for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
3778 gimple stmt = gsi_stmt (gsi);
3779 tree lab;
3780 int lp_nr;
3782 if (gimple_code (stmt) != GIMPLE_LABEL)
3783 break;
3784 lab = gimple_label_label (stmt);
3785 lp_nr = EH_LANDING_PAD_NR (lab);
3786 if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
3787 return false;
3790 /* The new destination block must not already be a destination of
3791 the source block, lest we merge fallthru and eh edges and get
3792 all sorts of confused. */
3793 if (find_edge (e_in->src, e_out->dest))
3794 return false;
3796 /* ??? We can get degenerate phis due to cfg cleanups. I would have
3797 thought this should have been cleaned up by a phicprop pass, but
3798 that doesn't appear to handle virtuals. Propagate by hand. */
3799 if (!gimple_seq_empty_p (phi_nodes (bb)))
3801 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
3803 gimple use_stmt, phi = gsi_stmt (gsi);
3804 tree lhs = gimple_phi_result (phi);
3805 tree rhs = gimple_phi_arg_def (phi, 0);
3806 use_operand_p use_p;
3807 imm_use_iterator iter;
3809 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3811 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3812 SET_USE (use_p, rhs);
3815 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
3816 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
3818 remove_phi_node (&gsi, true);
3822 if (dump_file && (dump_flags & TDF_DETAILS))
3823 fprintf (dump_file, "Unsplit EH landing pad %d to block %i.\n",
3824 lp->index, e_out->dest->index);
3826 /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
3827 a successor edge, humor it. But do the real CFG change with the
3828 predecessor of E_OUT in order to preserve the ordering of arguments
3829 to the PHI nodes in E_OUT->DEST. */
3830 redirect_eh_edge_1 (e_in, e_out->dest, false);
3831 redirect_edge_pred (e_out, e_in->src);
3832 e_out->flags = e_in->flags;
3833 e_out->probability = e_in->probability;
3834 e_out->count = e_in->count;
3835 remove_edge (e_in);
3837 return true;
3840 /* Examine each landing pad block and see if it matches unsplit_eh. */
3842 static bool
3843 unsplit_all_eh (void)
3845 bool changed = false;
3846 eh_landing_pad lp;
3847 int i;
3849 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
3850 if (lp)
3851 changed |= unsplit_eh (lp);
3853 return changed;
3856 /* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
3857 to OLD_BB to NEW_BB; return true on success, false on failure.
3859 OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
3860 PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
3861 Virtual PHIs may be deleted and marked for renaming. */
3863 static bool
3864 cleanup_empty_eh_merge_phis (basic_block new_bb, basic_block old_bb,
3865 edge old_bb_out, bool change_region)
3867 gimple_stmt_iterator ngsi, ogsi;
3868 edge_iterator ei;
3869 edge e;
3870 bitmap rename_virts;
3871 bitmap ophi_handled;
3873 /* The destination block must not be a regular successor for any
3874 of the preds of the landing pad. Thus, avoid turning
3875 <..>
3876 | \ EH
3877 | <..>
3879 <..>
3880 into
3881 <..>
3882 | | EH
3883 <..>
3884 which CFG verification would choke on. See PR45172 and PR51089. */
3885 FOR_EACH_EDGE (e, ei, old_bb->preds)
3886 if (find_edge (e->src, new_bb))
3887 return false;
3889 FOR_EACH_EDGE (e, ei, old_bb->preds)
3890 redirect_edge_var_map_clear (e);
3892 ophi_handled = BITMAP_ALLOC (NULL);
3893 rename_virts = BITMAP_ALLOC (NULL);
3895 /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
3896 for the edges we're going to move. */
3897 for (ngsi = gsi_start_phis (new_bb); !gsi_end_p (ngsi); gsi_next (&ngsi))
3899 gimple ophi, nphi = gsi_stmt (ngsi);
3900 tree nresult, nop;
3902 nresult = gimple_phi_result (nphi);
3903 nop = gimple_phi_arg_def (nphi, old_bb_out->dest_idx);
3905 /* Find the corresponding PHI in OLD_BB so we can forward-propagate
3906 the source ssa_name. */
3907 ophi = NULL;
3908 for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
3910 ophi = gsi_stmt (ogsi);
3911 if (gimple_phi_result (ophi) == nop)
3912 break;
3913 ophi = NULL;
3916 /* If we did find the corresponding PHI, copy those inputs. */
3917 if (ophi)
3919 /* If NOP is used somewhere else beyond phis in new_bb, give up. */
3920 if (!has_single_use (nop))
3922 imm_use_iterator imm_iter;
3923 use_operand_p use_p;
3925 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, nop)
3927 if (!gimple_debug_bind_p (USE_STMT (use_p))
3928 && (gimple_code (USE_STMT (use_p)) != GIMPLE_PHI
3929 || gimple_bb (USE_STMT (use_p)) != new_bb))
3930 goto fail;
3933 bitmap_set_bit (ophi_handled, SSA_NAME_VERSION (nop));
3934 FOR_EACH_EDGE (e, ei, old_bb->preds)
3936 location_t oloc;
3937 tree oop;
3939 if ((e->flags & EDGE_EH) == 0)
3940 continue;
3941 oop = gimple_phi_arg_def (ophi, e->dest_idx);
3942 oloc = gimple_phi_arg_location (ophi, e->dest_idx);
3943 redirect_edge_var_map_add (e, nresult, oop, oloc);
3946 /* If we didn't find the PHI, but it's a VOP, remember to rename
3947 it later, assuming all other tests succeed. */
3948 else if (virtual_operand_p (nresult))
3949 bitmap_set_bit (rename_virts, SSA_NAME_VERSION (nresult));
3950 /* If we didn't find the PHI, and it's a real variable, we know
3951 from the fact that OLD_BB is tree_empty_eh_handler_p that the
3952 variable is unchanged from input to the block and we can simply
3953 re-use the input to NEW_BB from the OLD_BB_OUT edge. */
3954 else
3956 location_t nloc
3957 = gimple_phi_arg_location (nphi, old_bb_out->dest_idx);
3958 FOR_EACH_EDGE (e, ei, old_bb->preds)
3959 redirect_edge_var_map_add (e, nresult, nop, nloc);
3963 /* Second, verify that all PHIs from OLD_BB have been handled. If not,
3964 we don't know what values from the other edges into NEW_BB to use. */
3965 for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
3967 gimple ophi = gsi_stmt (ogsi);
3968 tree oresult = gimple_phi_result (ophi);
3969 if (!bitmap_bit_p (ophi_handled, SSA_NAME_VERSION (oresult)))
3970 goto fail;
3973 /* At this point we know that the merge will succeed. Remove the PHI
3974 nodes for the virtuals that we want to rename. */
3975 if (!bitmap_empty_p (rename_virts))
3977 for (ngsi = gsi_start_phis (new_bb); !gsi_end_p (ngsi); )
3979 gimple nphi = gsi_stmt (ngsi);
3980 tree nresult = gimple_phi_result (nphi);
3981 if (bitmap_bit_p (rename_virts, SSA_NAME_VERSION (nresult)))
3983 mark_virtual_phi_result_for_renaming (nphi);
3984 remove_phi_node (&ngsi, true);
3986 else
3987 gsi_next (&ngsi);
3991 /* Finally, move the edges and update the PHIs. */
3992 for (ei = ei_start (old_bb->preds); (e = ei_safe_edge (ei)); )
3993 if (e->flags & EDGE_EH)
3995 /* ??? CFG manipluation routines do not try to update loop
3996 form on edge redirection. Do so manually here for now. */
3997 /* If we redirect a loop entry or latch edge that will either create
3998 a multiple entry loop or rotate the loop. If the loops merge
3999 we may have created a loop with multiple latches.
4000 All of this isn't easily fixed thus cancel the affected loop
4001 and mark the other loop as possibly having multiple latches. */
4002 if (current_loops
4003 && e->dest == e->dest->loop_father->header)
4005 e->dest->loop_father->header = NULL;
4006 e->dest->loop_father->latch = NULL;
4007 new_bb->loop_father->latch = NULL;
4008 loops_state_set (LOOPS_NEED_FIXUP|LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
4010 redirect_eh_edge_1 (e, new_bb, change_region);
4011 redirect_edge_succ (e, new_bb);
4012 flush_pending_stmts (e);
4014 else
4015 ei_next (&ei);
4017 BITMAP_FREE (ophi_handled);
4018 BITMAP_FREE (rename_virts);
4019 return true;
4021 fail:
4022 FOR_EACH_EDGE (e, ei, old_bb->preds)
4023 redirect_edge_var_map_clear (e);
4024 BITMAP_FREE (ophi_handled);
4025 BITMAP_FREE (rename_virts);
4026 return false;
4029 /* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4030 old region to NEW_REGION at BB. */
4032 static void
4033 cleanup_empty_eh_move_lp (basic_block bb, edge e_out,
4034 eh_landing_pad lp, eh_region new_region)
4036 gimple_stmt_iterator gsi;
4037 eh_landing_pad *pp;
4039 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
4040 continue;
4041 *pp = lp->next_lp;
4043 lp->region = new_region;
4044 lp->next_lp = new_region->landing_pads;
4045 new_region->landing_pads = lp;
4047 /* Delete the RESX that was matched within the empty handler block. */
4048 gsi = gsi_last_bb (bb);
4049 unlink_stmt_vdef (gsi_stmt (gsi));
4050 gsi_remove (&gsi, true);
4052 /* Clean up E_OUT for the fallthru. */
4053 e_out->flags = (e_out->flags & ~EDGE_EH) | EDGE_FALLTHRU;
4054 e_out->probability = REG_BR_PROB_BASE;
4057 /* A subroutine of cleanup_empty_eh. Handle more complex cases of
4058 unsplitting than unsplit_eh was prepared to handle, e.g. when
4059 multiple incoming edges and phis are involved. */
4061 static bool
4062 cleanup_empty_eh_unsplit (basic_block bb, edge e_out, eh_landing_pad lp)
4064 gimple_stmt_iterator gsi;
4065 tree lab;
4067 /* We really ought not have totally lost everything following
4068 a landing pad label. Given that BB is empty, there had better
4069 be a successor. */
4070 gcc_assert (e_out != NULL);
4072 /* The destination block must not already have a landing pad
4073 for a different region. */
4074 lab = NULL;
4075 for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
4077 gimple stmt = gsi_stmt (gsi);
4078 int lp_nr;
4080 if (gimple_code (stmt) != GIMPLE_LABEL)
4081 break;
4082 lab = gimple_label_label (stmt);
4083 lp_nr = EH_LANDING_PAD_NR (lab);
4084 if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
4085 return false;
4088 /* Attempt to move the PHIs into the successor block. */
4089 if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, false))
4091 if (dump_file && (dump_flags & TDF_DETAILS))
4092 fprintf (dump_file,
4093 "Unsplit EH landing pad %d to block %i "
4094 "(via cleanup_empty_eh).\n",
4095 lp->index, e_out->dest->index);
4096 return true;
4099 return false;
4102 /* Return true if edge E_FIRST is part of an empty infinite loop
4103 or leads to such a loop through a series of single successor
4104 empty bbs. */
4106 static bool
4107 infinite_empty_loop_p (edge e_first)
4109 bool inf_loop = false;
4110 edge e;
4112 if (e_first->dest == e_first->src)
4113 return true;
4115 e_first->src->aux = (void *) 1;
4116 for (e = e_first; single_succ_p (e->dest); e = single_succ_edge (e->dest))
4118 gimple_stmt_iterator gsi;
4119 if (e->dest->aux)
4121 inf_loop = true;
4122 break;
4124 e->dest->aux = (void *) 1;
4125 gsi = gsi_after_labels (e->dest);
4126 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4127 gsi_next_nondebug (&gsi);
4128 if (!gsi_end_p (gsi))
4129 break;
4131 e_first->src->aux = NULL;
4132 for (e = e_first; e->dest->aux; e = single_succ_edge (e->dest))
4133 e->dest->aux = NULL;
4135 return inf_loop;
4138 /* Examine the block associated with LP to determine if it's an empty
4139 handler for its EH region. If so, attempt to redirect EH edges to
4140 an outer region. Return true the CFG was updated in any way. This
4141 is similar to jump forwarding, just across EH edges. */
4143 static bool
4144 cleanup_empty_eh (eh_landing_pad lp)
4146 basic_block bb = label_to_block (lp->post_landing_pad);
4147 gimple_stmt_iterator gsi;
4148 gimple resx;
4149 eh_region new_region;
4150 edge_iterator ei;
4151 edge e, e_out;
4152 bool has_non_eh_pred;
4153 bool ret = false;
4154 int new_lp_nr;
4156 /* There can be zero or one edges out of BB. This is the quickest test. */
4157 switch (EDGE_COUNT (bb->succs))
4159 case 0:
4160 e_out = NULL;
4161 break;
4162 case 1:
4163 e_out = EDGE_SUCC (bb, 0);
4164 break;
4165 default:
4166 return false;
4169 resx = last_stmt (bb);
4170 if (resx && is_gimple_resx (resx))
4172 if (stmt_can_throw_external (resx))
4173 optimize_clobbers (bb);
4174 else if (sink_clobbers (bb))
4175 ret = true;
4178 gsi = gsi_after_labels (bb);
4180 /* Make sure to skip debug statements. */
4181 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4182 gsi_next_nondebug (&gsi);
4184 /* If the block is totally empty, look for more unsplitting cases. */
4185 if (gsi_end_p (gsi))
4187 /* For the degenerate case of an infinite loop bail out.
4188 If bb has no successors and is totally empty, which can happen e.g.
4189 because of incorrect noreturn attribute, bail out too. */
4190 if (e_out == NULL
4191 || infinite_empty_loop_p (e_out))
4192 return ret;
4194 return ret | cleanup_empty_eh_unsplit (bb, e_out, lp);
4197 /* The block should consist only of a single RESX statement, modulo a
4198 preceding call to __builtin_stack_restore if there is no outgoing
4199 edge, since the call can be eliminated in this case. */
4200 resx = gsi_stmt (gsi);
4201 if (!e_out && gimple_call_builtin_p (resx, BUILT_IN_STACK_RESTORE))
4203 gsi_next (&gsi);
4204 resx = gsi_stmt (gsi);
4206 if (!is_gimple_resx (resx))
4207 return ret;
4208 gcc_assert (gsi_one_before_end_p (gsi));
4210 /* Determine if there are non-EH edges, or resx edges into the handler. */
4211 has_non_eh_pred = false;
4212 FOR_EACH_EDGE (e, ei, bb->preds)
4213 if (!(e->flags & EDGE_EH))
4214 has_non_eh_pred = true;
4216 /* Find the handler that's outer of the empty handler by looking at
4217 where the RESX instruction was vectored. */
4218 new_lp_nr = lookup_stmt_eh_lp (resx);
4219 new_region = get_eh_region_from_lp_number (new_lp_nr);
4221 /* If there's no destination region within the current function,
4222 redirection is trivial via removing the throwing statements from
4223 the EH region, removing the EH edges, and allowing the block
4224 to go unreachable. */
4225 if (new_region == NULL)
4227 gcc_assert (e_out == NULL);
4228 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4229 if (e->flags & EDGE_EH)
4231 gimple stmt = last_stmt (e->src);
4232 remove_stmt_from_eh_lp (stmt);
4233 remove_edge (e);
4235 else
4236 ei_next (&ei);
4237 goto succeed;
4240 /* If the destination region is a MUST_NOT_THROW, allow the runtime
4241 to handle the abort and allow the blocks to go unreachable. */
4242 if (new_region->type == ERT_MUST_NOT_THROW)
4244 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4245 if (e->flags & EDGE_EH)
4247 gimple stmt = last_stmt (e->src);
4248 remove_stmt_from_eh_lp (stmt);
4249 add_stmt_to_eh_lp (stmt, new_lp_nr);
4250 remove_edge (e);
4252 else
4253 ei_next (&ei);
4254 goto succeed;
4257 /* Try to redirect the EH edges and merge the PHIs into the destination
4258 landing pad block. If the merge succeeds, we'll already have redirected
4259 all the EH edges. The handler itself will go unreachable if there were
4260 no normal edges. */
4261 if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, true))
4262 goto succeed;
4264 /* Finally, if all input edges are EH edges, then we can (potentially)
4265 reduce the number of transfers from the runtime by moving the landing
4266 pad from the original region to the new region. This is a win when
4267 we remove the last CLEANUP region along a particular exception
4268 propagation path. Since nothing changes except for the region with
4269 which the landing pad is associated, the PHI nodes do not need to be
4270 adjusted at all. */
4271 if (!has_non_eh_pred)
4273 cleanup_empty_eh_move_lp (bb, e_out, lp, new_region);
4274 if (dump_file && (dump_flags & TDF_DETAILS))
4275 fprintf (dump_file, "Empty EH handler %i moved to EH region %i.\n",
4276 lp->index, new_region->index);
4278 /* ??? The CFG didn't change, but we may have rendered the
4279 old EH region unreachable. Trigger a cleanup there. */
4280 return true;
4283 return ret;
4285 succeed:
4286 if (dump_file && (dump_flags & TDF_DETAILS))
4287 fprintf (dump_file, "Empty EH handler %i removed.\n", lp->index);
4288 remove_eh_landing_pad (lp);
4289 return true;
4292 /* Do a post-order traversal of the EH region tree. Examine each
4293 post_landing_pad block and see if we can eliminate it as empty. */
4295 static bool
4296 cleanup_all_empty_eh (void)
4298 bool changed = false;
4299 eh_landing_pad lp;
4300 int i;
4302 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
4303 if (lp)
4304 changed |= cleanup_empty_eh (lp);
4306 return changed;
4309 /* Perform cleanups and lowering of exception handling
4310 1) cleanups regions with handlers doing nothing are optimized out
4311 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4312 3) Info about regions that are containing instructions, and regions
4313 reachable via local EH edges is collected
4314 4) Eh tree is pruned for regions no longer neccesary.
4316 TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4317 Unify those that have the same failure decl and locus.
4320 static unsigned int
4321 execute_cleanup_eh_1 (void)
4323 /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4324 looking up unreachable landing pads. */
4325 remove_unreachable_handlers ();
4327 /* Watch out for the region tree vanishing due to all unreachable. */
4328 if (cfun->eh->region_tree)
4330 bool changed = false;
4332 if (optimize)
4333 changed |= unsplit_all_eh ();
4334 changed |= cleanup_all_empty_eh ();
4336 if (changed)
4338 free_dominance_info (CDI_DOMINATORS);
4339 free_dominance_info (CDI_POST_DOMINATORS);
4341 /* We delayed all basic block deletion, as we may have performed
4342 cleanups on EH edges while non-EH edges were still present. */
4343 delete_unreachable_blocks ();
4345 /* We manipulated the landing pads. Remove any region that no
4346 longer has a landing pad. */
4347 remove_unreachable_handlers_no_lp ();
4349 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
4353 return 0;
4356 static unsigned int
4357 execute_cleanup_eh (void)
4359 int ret = execute_cleanup_eh_1 ();
4361 /* If the function no longer needs an EH personality routine
4362 clear it. This exposes cross-language inlining opportunities
4363 and avoids references to a never defined personality routine. */
4364 if (DECL_FUNCTION_PERSONALITY (current_function_decl)
4365 && function_needs_eh_personality (cfun) != eh_personality_lang)
4366 DECL_FUNCTION_PERSONALITY (current_function_decl) = NULL_TREE;
4368 return ret;
4371 static bool
4372 gate_cleanup_eh (void)
4374 return cfun->eh != NULL && cfun->eh->region_tree != NULL;
4377 struct gimple_opt_pass pass_cleanup_eh = {
4379 GIMPLE_PASS,
4380 "ehcleanup", /* name */
4381 OPTGROUP_NONE, /* optinfo_flags */
4382 gate_cleanup_eh, /* gate */
4383 execute_cleanup_eh, /* execute */
4384 NULL, /* sub */
4385 NULL, /* next */
4386 0, /* static_pass_number */
4387 TV_TREE_EH, /* tv_id */
4388 PROP_gimple_lcf, /* properties_required */
4389 0, /* properties_provided */
4390 0, /* properties_destroyed */
4391 0, /* todo_flags_start */
4392 0 /* todo_flags_finish */
4396 /* Verify that BB containing STMT as the last statement, has precisely the
4397 edge that make_eh_edges would create. */
4399 DEBUG_FUNCTION bool
4400 verify_eh_edges (gimple stmt)
4402 basic_block bb = gimple_bb (stmt);
4403 eh_landing_pad lp = NULL;
4404 int lp_nr;
4405 edge_iterator ei;
4406 edge e, eh_edge;
4408 lp_nr = lookup_stmt_eh_lp (stmt);
4409 if (lp_nr > 0)
4410 lp = get_eh_landing_pad_from_number (lp_nr);
4412 eh_edge = NULL;
4413 FOR_EACH_EDGE (e, ei, bb->succs)
4415 if (e->flags & EDGE_EH)
4417 if (eh_edge)
4419 error ("BB %i has multiple EH edges", bb->index);
4420 return true;
4422 else
4423 eh_edge = e;
4427 if (lp == NULL)
4429 if (eh_edge)
4431 error ("BB %i can not throw but has an EH edge", bb->index);
4432 return true;
4434 return false;
4437 if (!stmt_could_throw_p (stmt))
4439 error ("BB %i last statement has incorrectly set lp", bb->index);
4440 return true;
4443 if (eh_edge == NULL)
4445 error ("BB %i is missing an EH edge", bb->index);
4446 return true;
4449 if (eh_edge->dest != label_to_block (lp->post_landing_pad))
4451 error ("Incorrect EH edge %i->%i", bb->index, eh_edge->dest->index);
4452 return true;
4455 return false;
4458 /* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
4460 DEBUG_FUNCTION bool
4461 verify_eh_dispatch_edge (gimple stmt)
4463 eh_region r;
4464 eh_catch c;
4465 basic_block src, dst;
4466 bool want_fallthru = true;
4467 edge_iterator ei;
4468 edge e, fall_edge;
4470 r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
4471 src = gimple_bb (stmt);
4473 FOR_EACH_EDGE (e, ei, src->succs)
4474 gcc_assert (e->aux == NULL);
4476 switch (r->type)
4478 case ERT_TRY:
4479 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
4481 dst = label_to_block (c->label);
4482 e = find_edge (src, dst);
4483 if (e == NULL)
4485 error ("BB %i is missing an edge", src->index);
4486 return true;
4488 e->aux = (void *)e;
4490 /* A catch-all handler doesn't have a fallthru. */
4491 if (c->type_list == NULL)
4493 want_fallthru = false;
4494 break;
4497 break;
4499 case ERT_ALLOWED_EXCEPTIONS:
4500 dst = label_to_block (r->u.allowed.label);
4501 e = find_edge (src, dst);
4502 if (e == NULL)
4504 error ("BB %i is missing an edge", src->index);
4505 return true;
4507 e->aux = (void *)e;
4508 break;
4510 default:
4511 gcc_unreachable ();
4514 fall_edge = NULL;
4515 FOR_EACH_EDGE (e, ei, src->succs)
4517 if (e->flags & EDGE_FALLTHRU)
4519 if (fall_edge != NULL)
4521 error ("BB %i too many fallthru edges", src->index);
4522 return true;
4524 fall_edge = e;
4526 else if (e->aux)
4527 e->aux = NULL;
4528 else
4530 error ("BB %i has incorrect edge", src->index);
4531 return true;
4534 if ((fall_edge != NULL) ^ want_fallthru)
4536 error ("BB %i has incorrect fallthru edge", src->index);
4537 return true;
4540 return false;