1 /* Exception handling semantics and decomposition for trees.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "tree-inline.h"
34 #include "tree-iterator.h"
35 #include "tree-pass.h"
37 #include "langhooks.h"
42 /* In some instances a tree and a gimple need to be stored in a same table,
43 i.e. in hash tables. This is a structure to do this. */
44 typedef union {tree
*tp
; tree t
; gimple g
;} treemple
;
46 /* Nonzero if we are using EH to handle cleanups. */
47 static int using_eh_for_cleanups_p
= 0;
50 using_eh_for_cleanups (void)
52 using_eh_for_cleanups_p
= 1;
55 /* Misc functions used in this file. */
57 /* Compare and hash for any structure which begins with a canonical
58 pointer. Assumes all pointers are interchangeable, which is sort
59 of already assumed by gcc elsewhere IIRC. */
62 struct_ptr_eq (const void *a
, const void *b
)
64 const void * const * x
= (const void * const *) a
;
65 const void * const * y
= (const void * const *) b
;
70 struct_ptr_hash (const void *a
)
72 const void * const * x
= (const void * const *) a
;
73 return (size_t)*x
>> 4;
77 /* Remember and lookup EH region data for arbitrary statements.
78 Really this means any statement that could_throw_p. We could
79 stuff this information into the stmt_ann data structure, but:
81 (1) We absolutely rely on this information being kept until
82 we get to rtl. Once we're done with lowering here, if we lose
83 the information there's no way to recover it!
85 (2) There are many more statements that *cannot* throw as
86 compared to those that can. We should be saving some amount
87 of space by only allocating memory for those that can throw. */
90 record_stmt_eh_region (struct eh_region
*region
, gimple t
)
95 add_stmt_to_eh_region (t
, get_eh_region_number (region
));
99 /* Add statement T in function IFUN to EH region NUM. */
102 add_stmt_to_eh_region_fn (struct function
*ifun
, gimple t
, int num
)
104 struct throw_stmt_node
*n
;
107 gcc_assert (num
>= 0);
108 gcc_assert (gimple_code (t
) != GIMPLE_RESX
);
110 n
= GGC_NEW (struct throw_stmt_node
);
114 if (!get_eh_throw_stmt_table (ifun
))
115 set_eh_throw_stmt_table (ifun
, htab_create_ggc (31, struct_ptr_hash
,
119 slot
= htab_find_slot (get_eh_throw_stmt_table (ifun
), n
, INSERT
);
125 /* Add statement T in the current function (cfun) to EH region number
129 add_stmt_to_eh_region (gimple t
, int num
)
131 add_stmt_to_eh_region_fn (cfun
, t
, num
);
135 /* Remove statement T in function IFUN from the EH region holding it. */
138 remove_stmt_from_eh_region_fn (struct function
*ifun
, gimple t
)
140 struct throw_stmt_node dummy
;
143 if (!get_eh_throw_stmt_table (ifun
))
147 slot
= htab_find_slot (get_eh_throw_stmt_table (ifun
), &dummy
,
151 htab_clear_slot (get_eh_throw_stmt_table (ifun
), slot
);
159 /* Remove statement T in the current function (cfun) from the EH
160 region holding it. */
163 remove_stmt_from_eh_region (gimple t
)
165 return remove_stmt_from_eh_region_fn (cfun
, t
);
168 /* Determine if statement T is inside an EH region in function IFUN.
169 Return the EH region number if found, return -2 if IFUN does not
170 have an EH table and -1 if T could not be found in IFUN's EH region
174 lookup_stmt_eh_region_fn (struct function
*ifun
, gimple t
)
176 struct throw_stmt_node
*p
, n
;
178 if (!get_eh_throw_stmt_table (ifun
))
182 p
= (struct throw_stmt_node
*) htab_find (get_eh_throw_stmt_table (ifun
), &n
);
183 return (p
? p
->region_nr
: -1);
187 /* Determine if statement T is inside an EH region in the current
188 function (cfun). Return the EH region number if found, return -2
189 if cfun does not have an EH table and -1 if T could not be found in
190 cfun's EH region table. */
193 lookup_stmt_eh_region (gimple t
)
195 /* We can get called from initialized data when -fnon-call-exceptions
196 is on; prevent crash. */
200 return lookup_stmt_eh_region_fn (cfun
, t
);
204 /* Determine if expression T is inside an EH region in the current
205 function (cfun). Return the EH region number if found, return -2
206 if IFUN does not have an EH table and -1 if T could not be found in
207 IFUN's EH region table. */
210 lookup_expr_eh_region (tree t
)
212 /* We can get called from initialized data when -fnon-call-exceptions
213 is on; prevent crash. */
217 if (!get_eh_throw_stmt_table (cfun
))
222 tree_ann_common_t ann
= tree_common_ann (t
);
224 return (int) ann
->rn
;
231 /* First pass of EH node decomposition. Build up a tree of GIMPLE_TRY_FINALLY
232 nodes and LABEL_DECL nodes. We will use this during the second phase to
233 determine if a goto leaves the body of a TRY_FINALLY_EXPR node. */
235 struct finally_tree_node
237 /* When storing a GIMPLE_TRY, we have to record a gimple. However
238 when deciding whether a GOTO to a certain LABEL_DECL (which is a
239 tree) leaves the TRY block, its necessary to record a tree in
240 this field. Thus a treemple is used. */
245 /* Note that this table is *not* marked GTY. It is short-lived. */
246 static htab_t finally_tree
;
249 record_in_finally_tree (treemple child
, gimple parent
)
251 struct finally_tree_node
*n
;
254 n
= XNEW (struct finally_tree_node
);
258 slot
= htab_find_slot (finally_tree
, n
, INSERT
);
264 collect_finally_tree (gimple stmt
, gimple region
);
266 /* Go through the gimple sequence. Works with collect_finally_tree to
267 record all GIMPLE_LABEL and GIMPLE_TRY statements. */
270 collect_finally_tree_1 (gimple_seq seq
, gimple region
)
272 gimple_stmt_iterator gsi
;
274 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
275 collect_finally_tree (gsi_stmt (gsi
), region
);
279 collect_finally_tree (gimple stmt
, gimple region
)
283 switch (gimple_code (stmt
))
286 temp
.t
= gimple_label_label (stmt
);
287 record_in_finally_tree (temp
, region
);
291 if (gimple_try_kind (stmt
) == GIMPLE_TRY_FINALLY
)
294 record_in_finally_tree (temp
, region
);
295 collect_finally_tree_1 (gimple_try_eval (stmt
), stmt
);
296 collect_finally_tree_1 (gimple_try_cleanup (stmt
), region
);
298 else if (gimple_try_kind (stmt
) == GIMPLE_TRY_CATCH
)
300 collect_finally_tree_1 (gimple_try_eval (stmt
), region
);
301 collect_finally_tree_1 (gimple_try_cleanup (stmt
), region
);
306 collect_finally_tree_1 (gimple_catch_handler (stmt
), region
);
309 case GIMPLE_EH_FILTER
:
310 collect_finally_tree_1 (gimple_eh_filter_failure (stmt
), region
);
314 /* A type, a decl, or some kind of statement that we're not
315 interested in. Don't walk them. */
321 /* Use the finally tree to determine if a jump from START to TARGET
322 would leave the try_finally node that START lives in. */
325 outside_finally_tree (treemple start
, gimple target
)
327 struct finally_tree_node n
, *p
;
332 p
= (struct finally_tree_node
*) htab_find (finally_tree
, &n
);
337 while (start
.g
!= target
);
342 /* Second pass of EH node decomposition. Actually transform the GIMPLE_TRY
343 nodes into a set of gotos, magic labels, and eh regions.
344 The eh region creation is straight-forward, but frobbing all the gotos
345 and such into shape isn't. */
347 /* State of the world while lowering. */
351 /* What's "current" while constructing the eh region tree. These
352 correspond to variables of the same name in cfun->eh, which we
353 don't have easy access to. */
354 struct eh_region
*cur_region
;
355 struct eh_region
*prev_try
;
357 /* Processing of TRY_FINALLY requires a bit more state. This is
358 split out into a separate structure so that we don't have to
359 copy so much when processing other nodes. */
360 struct leh_tf_state
*tf
;
365 /* Pointer to the GIMPLE_TRY_FINALLY node under discussion. The
366 try_finally_expr is the original GIMPLE_TRY_FINALLY. We need to retain
367 this so that outside_finally_tree can reliably reference the tree used
368 in the collect_finally_tree data structures. */
369 gimple try_finally_expr
;
371 /* While lowering a top_p usually it is expanded into multiple statements,
372 thus we need the following field to store them. */
373 gimple_seq top_p_seq
;
375 /* The state outside this try_finally node. */
376 struct leh_state
*outer
;
378 /* The exception region created for it. */
379 struct eh_region
*region
;
381 /* The GOTO_QUEUE is is an array of GIMPLE_GOTO and GIMPLE_RETURN statements
382 that are seen to escape this GIMPLE_TRY_FINALLY node.
383 The idea is to record a gimple statement for everything except for
384 the conditionals, which get their labels recorded. Since labels are of
385 type 'tree', we need this node to store both gimple and tree objects.
386 REPL_STMT is the sequence used to replace the goto/return statement.
387 CONT_STMT is used to store the statement that allows the return/goto to
388 jump to the original destination. */
389 struct goto_queue_node
{
391 gimple_seq repl_stmt
;
394 /* this is used when index >= 0 to indicate that stmt is a label(as
395 opposed to a goto stmt) */
398 size_t goto_queue_size
;
399 size_t goto_queue_active
;
401 /* Pointer map to help in searching goto_queue when it is large. */
402 struct pointer_map_t
*goto_queue_map
;
404 /* The set of unique labels seen as entries in the goto queue. */
405 VEC(tree
,heap
) *dest_array
;
407 /* A label to be added at the end of the completed transformed
408 sequence. It will be set if may_fallthru was true *at one time*,
409 though subsequent transformations may have cleared that flag. */
412 /* A label that has been registered with except.c to be the
413 landing pad for this try block. */
416 /* True if it is possible to fall out the bottom of the try block.
417 Cleared if the fallthru is converted to a goto. */
420 /* True if any entry in goto_queue is a GIMPLE_RETURN. */
423 /* True if the finally block can receive an exception edge.
424 Cleared if the exception case is handled by code duplication. */
428 static gimple_seq
lower_eh_filter (struct leh_state
*, gimple
);
430 /* Search for STMT in the goto queue. Return the replacement,
431 or null if the statement isn't in the queue. */
433 #define LARGE_GOTO_QUEUE 20
435 static void lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq seq
);
438 find_goto_replacement (struct leh_tf_state
*tf
, treemple stmt
)
443 if (tf
->goto_queue_active
< LARGE_GOTO_QUEUE
)
445 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
446 if ( tf
->goto_queue
[i
].stmt
.g
== stmt
.g
)
447 return tf
->goto_queue
[i
].repl_stmt
;
451 /* If we have a large number of entries in the goto_queue, create a
452 pointer map and use that for searching. */
454 if (!tf
->goto_queue_map
)
456 tf
->goto_queue_map
= pointer_map_create ();
457 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
459 slot
= pointer_map_insert (tf
->goto_queue_map
,
460 tf
->goto_queue
[i
].stmt
.g
);
461 gcc_assert (*slot
== NULL
);
462 *slot
= &tf
->goto_queue
[i
];
466 slot
= pointer_map_contains (tf
->goto_queue_map
, stmt
.g
);
468 return (((struct goto_queue_node
*) *slot
)->repl_stmt
);
473 /* A subroutine of replace_goto_queue_1. Handles the sub-clauses of a
474 lowered GIMPLE_COND. If, by chance, the replacement is a simple goto,
475 then we can just splat it in, otherwise we add the new stmts immediately
476 after the GIMPLE_COND and redirect. */
479 replace_goto_queue_cond_clause (tree
*tp
, struct leh_tf_state
*tf
,
480 gimple_stmt_iterator
*gsi
)
487 new_seq
= find_goto_replacement (tf
, temp
);
491 if (gimple_seq_singleton_p (new_seq
)
492 && gimple_code (gimple_seq_first_stmt (new_seq
)) == GIMPLE_GOTO
)
494 *tp
= gimple_goto_dest (gimple_seq_first_stmt (new_seq
));
498 label
= create_artificial_label ();
499 /* Set the new label for the GIMPLE_COND */
502 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
503 gsi_insert_seq_after (gsi
, gimple_seq_copy (new_seq
), GSI_CONTINUE_LINKING
);
506 /* The real work of replace_goto_queue. Returns with TSI updated to
507 point to the next statement. */
509 static void replace_goto_queue_stmt_list (gimple_seq
, struct leh_tf_state
*);
512 replace_goto_queue_1 (gimple stmt
, struct leh_tf_state
*tf
,
513 gimple_stmt_iterator
*gsi
)
519 switch (gimple_code (stmt
))
524 seq
= find_goto_replacement (tf
, temp
);
527 gsi_insert_seq_before (gsi
, gimple_seq_copy (seq
), GSI_SAME_STMT
);
528 gsi_remove (gsi
, false);
534 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 2), tf
, gsi
);
535 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 3), tf
, gsi
);
539 replace_goto_queue_stmt_list (gimple_try_eval (stmt
), tf
);
540 replace_goto_queue_stmt_list (gimple_try_cleanup (stmt
), tf
);
543 replace_goto_queue_stmt_list (gimple_catch_handler (stmt
), tf
);
545 case GIMPLE_EH_FILTER
:
546 replace_goto_queue_stmt_list (gimple_eh_filter_failure (stmt
), tf
);
550 /* These won't have gotos in them. */
557 /* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
560 replace_goto_queue_stmt_list (gimple_seq seq
, struct leh_tf_state
*tf
)
562 gimple_stmt_iterator gsi
= gsi_start (seq
);
564 while (!gsi_end_p (gsi
))
565 replace_goto_queue_1 (gsi_stmt (gsi
), tf
, &gsi
);
568 /* Replace all goto queue members. */
571 replace_goto_queue (struct leh_tf_state
*tf
)
573 if (tf
->goto_queue_active
== 0)
575 replace_goto_queue_stmt_list (tf
->top_p_seq
, tf
);
578 /* Add a new record to the goto queue contained in TF. NEW_STMT is the
579 data to be added, IS_LABEL indicates whether NEW_STMT is a label or
583 record_in_goto_queue (struct leh_tf_state
*tf
,
589 struct goto_queue_node
*q
;
591 gcc_assert (!tf
->goto_queue_map
);
593 active
= tf
->goto_queue_active
;
594 size
= tf
->goto_queue_size
;
597 size
= (size
? size
* 2 : 32);
598 tf
->goto_queue_size
= size
;
600 = XRESIZEVEC (struct goto_queue_node
, tf
->goto_queue
, size
);
603 q
= &tf
->goto_queue
[active
];
604 tf
->goto_queue_active
= active
+ 1;
606 memset (q
, 0, sizeof (*q
));
609 q
->is_label
= is_label
;
612 /* Record the LABEL label in the goto queue contained in TF.
616 record_in_goto_queue_label (struct leh_tf_state
*tf
, treemple stmt
, tree label
)
619 treemple temp
, new_stmt
;
624 /* Computed and non-local gotos do not get processed. Given
625 their nature we can neither tell whether we've escaped the
626 finally block nor redirect them if we knew. */
627 if (TREE_CODE (label
) != LABEL_DECL
)
630 /* No need to record gotos that don't leave the try block. */
632 if (!outside_finally_tree (temp
, tf
->try_finally_expr
))
635 if (! tf
->dest_array
)
637 tf
->dest_array
= VEC_alloc (tree
, heap
, 10);
638 VEC_quick_push (tree
, tf
->dest_array
, label
);
643 int n
= VEC_length (tree
, tf
->dest_array
);
644 for (index
= 0; index
< n
; ++index
)
645 if (VEC_index (tree
, tf
->dest_array
, index
) == label
)
648 VEC_safe_push (tree
, heap
, tf
->dest_array
, label
);
651 /* In the case of a GOTO we want to record the destination label,
652 since with a GIMPLE_COND we have an easy access to the then/else
655 record_in_goto_queue (tf
, new_stmt
, index
, true);
659 /* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
660 node, and if so record that fact in the goto queue associated with that
664 maybe_record_in_goto_queue (struct leh_state
*state
, gimple stmt
)
666 struct leh_tf_state
*tf
= state
->tf
;
672 switch (gimple_code (stmt
))
675 new_stmt
.tp
= gimple_op_ptr (stmt
, 2);
676 record_in_goto_queue_label (tf
, new_stmt
, gimple_cond_true_label (stmt
));
677 new_stmt
.tp
= gimple_op_ptr (stmt
, 3);
678 record_in_goto_queue_label (tf
, new_stmt
, gimple_cond_false_label (stmt
));
682 record_in_goto_queue_label (tf
, new_stmt
, gimple_goto_dest (stmt
));
686 tf
->may_return
= true;
688 record_in_goto_queue (tf
, new_stmt
, -1, false);
697 #ifdef ENABLE_CHECKING
698 /* We do not process GIMPLE_SWITCHes for now. As long as the original source
699 was in fact structured, and we've not yet done jump threading, then none
700 of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this. */
703 verify_norecord_switch_expr (struct leh_state
*state
, gimple switch_expr
)
705 struct leh_tf_state
*tf
= state
->tf
;
711 n
= gimple_switch_num_labels (switch_expr
);
713 for (i
= 0; i
< n
; ++i
)
716 tree lab
= CASE_LABEL (gimple_switch_label (switch_expr
, i
));
718 gcc_assert (!outside_finally_tree (temp
, tf
->try_finally_expr
));
722 #define verify_norecord_switch_expr(state, switch_expr)
725 /* Redirect a RETURN_EXPR pointed to by STMT_P to FINLAB. Place in CONT_P
726 whatever is needed to finish the return. If MOD is non-null, insert it
727 before the new branch. RETURN_VALUE_P is a cache containing a temporary
728 variable to be used in manipulating the value returned from the function. */
731 do_return_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
,
732 tree
*return_value_p
)
737 /* In the case of a return, the queue node must be a gimple statement. */
738 gcc_assert (!q
->is_label
);
740 ret_expr
= gimple_return_retval (q
->stmt
.g
);
744 if (!*return_value_p
)
745 *return_value_p
= ret_expr
;
747 gcc_assert (*return_value_p
== ret_expr
);
748 q
->cont_stmt
= q
->stmt
.g
;
749 /* The nasty part about redirecting the return value is that the
750 return value itself is to be computed before the FINALLY block
764 should return 0, not 1. Arrange for this to happen by copying
765 computed the return value into a local temporary. This also
766 allows us to redirect multiple return statements through the
767 same destination block; whether this is a net win or not really
768 depends, I guess, but it does make generation of the switch in
769 lower_try_finally_switch easier. */
771 if (TREE_CODE (ret_expr
) == RESULT_DECL
)
773 if (!*return_value_p
)
774 *return_value_p
= ret_expr
;
776 gcc_assert (*return_value_p
== ret_expr
);
777 q
->cont_stmt
= q
->stmt
.g
;
783 /* If we don't return a value, all return statements are the same. */
784 q
->cont_stmt
= q
->stmt
.g
;
787 q
->repl_stmt
= gimple_seq_alloc ();
790 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
792 x
= gimple_build_goto (finlab
);
793 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
796 /* Similar, but easier, for GIMPLE_GOTO. */
799 do_goto_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
,
800 struct leh_tf_state
*tf
)
804 gcc_assert (q
->is_label
);
806 q
->repl_stmt
= gimple_seq_alloc ();
808 q
->cont_stmt
= gimple_build_goto (VEC_index (tree
, tf
->dest_array
,q
->index
));
811 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
813 x
= gimple_build_goto (finlab
);
814 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
817 /* We want to transform
818 try { body; } catch { stuff; }
820 body; goto over; lab: stuff; over:
822 TP is a GIMPLE_TRY node. LAB is the label that
823 should be placed before the second operand, or NULL. OVER is
824 an existing label that should be put at the exit, or NULL. */
827 frob_into_branch_around (gimple tp
, tree lab
, tree over
)
830 gimple_seq cleanup
, result
;
832 cleanup
= gimple_try_cleanup (tp
);
833 result
= gimple_try_eval (tp
);
835 if (gimple_seq_may_fallthru (result
))
838 over
= create_artificial_label ();
839 x
= gimple_build_goto (over
);
840 gimple_seq_add_stmt (&result
, x
);
845 x
= gimple_build_label (lab
);
846 gimple_seq_add_stmt (&result
, x
);
849 gimple_seq_add_seq (&result
, cleanup
);
853 x
= gimple_build_label (over
);
854 gimple_seq_add_stmt (&result
, x
);
859 /* A subroutine of lower_try_finally. Duplicate the tree rooted at T.
860 Make sure to record all new labels found. */
863 lower_try_finally_dup_block (gimple_seq seq
, struct leh_state
*outer_state
)
865 gimple region
= NULL
;
868 new_seq
= copy_gimple_seq_and_replace_locals (seq
);
871 region
= outer_state
->tf
->try_finally_expr
;
872 collect_finally_tree_1 (new_seq
, region
);
877 /* A subroutine of lower_try_finally. Create a fallthru label for
878 the given try_finally state. The only tricky bit here is that
879 we have to make sure to record the label in our outer context. */
882 lower_try_finally_fallthru_label (struct leh_tf_state
*tf
)
884 tree label
= tf
->fallthru_label
;
889 label
= create_artificial_label ();
890 tf
->fallthru_label
= label
;
894 record_in_finally_tree (temp
, tf
->outer
->tf
->try_finally_expr
);
900 /* A subroutine of lower_try_finally. If lang_protect_cleanup_actions
901 returns non-null, then the language requires that the exception path out
902 of a try_finally be treated specially. To wit: the code within the
903 finally block may not itself throw an exception. We have two choices here.
904 First we can duplicate the finally block and wrap it in a must_not_throw
905 region. Second, we can generate code like
910 if (fintmp == eh_edge)
911 protect_cleanup_actions;
914 where "fintmp" is the temporary used in the switch statement generation
915 alternative considered below. For the nonce, we always choose the first
918 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
921 honor_protect_cleanup_actions (struct leh_state
*outer_state
,
922 struct leh_state
*this_state
,
923 struct leh_tf_state
*tf
)
925 gimple protect_cleanup_actions
;
926 gimple_stmt_iterator gsi
;
927 bool finally_may_fallthru
;
931 /* First check for nothing to do. */
932 if (lang_protect_cleanup_actions
)
933 protect_cleanup_actions
= lang_protect_cleanup_actions ();
935 protect_cleanup_actions
= NULL
;
937 finally
= gimple_try_cleanup (tf
->top_p
);
939 /* If the EH case of the finally block can fall through, this may be a
940 structure of the form
953 E.g. with an inline destructor with an embedded try block. In this
954 case we must save the runtime EH data around the nested exception.
956 This complication means that any time the previous runtime data might
957 be used (via fallthru from the finally) we handle the eh case here,
958 whether or not protect_cleanup_actions is active. */
960 finally_may_fallthru
= gimple_seq_may_fallthru (finally
);
961 if (!finally_may_fallthru
&& !protect_cleanup_actions
)
964 /* Duplicate the FINALLY block. Only need to do this for try-finally,
965 and not for cleanups. */
967 finally
= lower_try_finally_dup_block (finally
, outer_state
);
969 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
970 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
971 to be in an enclosing scope, but needs to be implemented at this level
972 to avoid a nesting violation (see wrap_temporary_cleanups in
973 cp/decl.c). Since it's logically at an outer level, we should call
974 terminate before we get to it, so strip it away before adding the
975 MUST_NOT_THROW filter. */
976 gsi
= gsi_start (finally
);
978 if (protect_cleanup_actions
979 && gimple_code (x
) == GIMPLE_TRY
980 && gimple_try_kind (x
) == GIMPLE_TRY_CATCH
981 && gimple_try_catch_is_cleanup (x
))
983 gsi_insert_seq_before (&gsi
, gimple_try_eval (x
), GSI_SAME_STMT
);
984 gsi_remove (&gsi
, false);
987 /* Resume execution after the exception. Adding this now lets
988 lower_eh_filter not add unnecessary gotos, as it is clear that
989 we never fallthru from this copy of the finally block. */
990 if (finally_may_fallthru
)
992 tree save_eptr
, save_filt
;
995 save_eptr
= create_tmp_var (ptr_type_node
, "save_eptr");
996 save_filt
= create_tmp_var (integer_type_node
, "save_filt");
998 gsi
= gsi_start (finally
);
999 tmp
= build0 (EXC_PTR_EXPR
, ptr_type_node
);
1000 x
= gimple_build_assign (save_eptr
, tmp
);
1001 gsi_insert_before (&gsi
, x
, GSI_CONTINUE_LINKING
);
1003 tmp
= build0 (FILTER_EXPR
, integer_type_node
);
1004 x
= gimple_build_assign (save_filt
, tmp
);
1005 gsi_insert_before (&gsi
, x
, GSI_CONTINUE_LINKING
);
1007 gsi
= gsi_last (finally
);
1008 tmp
= build0 (EXC_PTR_EXPR
, ptr_type_node
);
1009 x
= gimple_build_assign (tmp
, save_eptr
);
1010 gsi_insert_after (&gsi
, x
, GSI_CONTINUE_LINKING
);
1012 tmp
= build0 (FILTER_EXPR
, integer_type_node
);
1013 x
= gimple_build_assign (tmp
, save_filt
);
1014 gsi_insert_after (&gsi
, x
, GSI_CONTINUE_LINKING
);
1016 x
= gimple_build_resx (get_eh_region_number (tf
->region
));
1017 gsi_insert_after (&gsi
, x
, GSI_CONTINUE_LINKING
);
1020 /* Wrap the block with protect_cleanup_actions as the action. */
1021 if (protect_cleanup_actions
)
1023 gimple_seq seq
= NULL
, failure
= NULL
;
1025 gimple_seq_add_stmt (&failure
, protect_cleanup_actions
);
1026 x
= gimple_build_eh_filter (NULL
, failure
);
1027 gimple_eh_filter_set_must_not_throw (x
, 1);
1029 gimple_seq_add_stmt (&seq
, x
);
1030 x
= gimple_build_try (finally
, seq
, GIMPLE_TRY_CATCH
);
1031 finally
= lower_eh_filter (outer_state
, x
);
1034 lower_eh_constructs_1 (outer_state
, finally
);
1036 /* Hook this up to the end of the existing try block. If we
1037 previously fell through the end, we'll have to branch around.
1038 This means adding a new goto, and adding it to the queue. */
1040 gsi
= gsi_last (gimple_try_eval (tf
->top_p
));
1042 if (tf
->may_fallthru
)
1045 tmp
= lower_try_finally_fallthru_label (tf
);
1046 x
= gimple_build_goto (tmp
);
1047 gsi_insert_after (&gsi
, x
, GSI_CONTINUE_LINKING
);
1050 maybe_record_in_goto_queue (this_state
, x
);
1052 tf
->may_fallthru
= false;
1055 x
= gimple_build_label (tf
->eh_label
);
1056 gsi_insert_after (&gsi
, x
, GSI_CONTINUE_LINKING
);
1057 gsi_insert_seq_after (&gsi
, finally
, GSI_CONTINUE_LINKING
);
1059 /* Having now been handled, EH isn't to be considered with
1060 the rest of the outgoing edges. */
1061 tf
->may_throw
= false;
1064 /* A subroutine of lower_try_finally. We have determined that there is
1065 no fallthru edge out of the finally block. This means that there is
1066 no outgoing edge corresponding to any incoming edge. Restructure the
1067 try_finally node for this special case. */
1070 lower_try_finally_nofallthru (struct leh_state
*state
,
1071 struct leh_tf_state
*tf
)
1073 tree lab
, return_val
;
1076 struct goto_queue_node
*q
, *qe
;
1081 lab
= create_artificial_label ();
1083 /* We expect that tf->top_p is a GIMPLE_TRY. */
1084 finally
= gimple_try_cleanup (tf
->top_p
);
1085 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1087 x
= gimple_build_label (lab
);
1088 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1092 qe
= q
+ tf
->goto_queue_active
;
1095 do_return_redirection (q
, lab
, NULL
, &return_val
);
1097 do_goto_redirection (q
, lab
, NULL
, tf
);
1099 replace_goto_queue (tf
);
1101 lower_eh_constructs_1 (state
, finally
);
1102 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1105 /* A subroutine of lower_try_finally. We have determined that there is
1106 exactly one destination of the finally block. Restructure the
1107 try_finally node for this special case. */
1110 lower_try_finally_onedest (struct leh_state
*state
, struct leh_tf_state
*tf
)
1112 struct goto_queue_node
*q
, *qe
;
1117 finally
= gimple_try_cleanup (tf
->top_p
);
1118 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1120 lower_eh_constructs_1 (state
, finally
);
1124 /* Only reachable via the exception edge. Add the given label to
1125 the head of the FINALLY block. Append a RESX at the end. */
1127 x
= gimple_build_label (tf
->eh_label
);
1128 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1130 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1132 x
= gimple_build_resx (get_eh_region_number (tf
->region
));
1134 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1139 if (tf
->may_fallthru
)
1141 /* Only reachable via the fallthru edge. Do nothing but let
1142 the two blocks run together; we'll fall out the bottom. */
1143 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1147 finally_label
= create_artificial_label ();
1148 x
= gimple_build_label (finally_label
);
1149 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1151 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1154 qe
= q
+ tf
->goto_queue_active
;
1158 /* Reachable by return expressions only. Redirect them. */
1159 tree return_val
= NULL
;
1161 do_return_redirection (q
, finally_label
, NULL
, &return_val
);
1162 replace_goto_queue (tf
);
1166 /* Reachable by goto expressions only. Redirect them. */
1168 do_goto_redirection (q
, finally_label
, NULL
, tf
);
1169 replace_goto_queue (tf
);
1171 if (VEC_index (tree
, tf
->dest_array
, 0) == tf
->fallthru_label
)
1173 /* Reachable by goto to fallthru label only. Redirect it
1174 to the new label (already created, sadly), and do not
1175 emit the final branch out, or the fallthru label. */
1176 tf
->fallthru_label
= NULL
;
1181 /* Place the original return/goto to the original destination
1182 immediately after the finally block. */
1183 x
= tf
->goto_queue
[0].cont_stmt
;
1184 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1185 maybe_record_in_goto_queue (state
, x
);
1188 /* A subroutine of lower_try_finally. There are multiple edges incoming
1189 and outgoing from the finally block. Implement this by duplicating the
1190 finally block for every destination. */
1193 lower_try_finally_copy (struct leh_state
*state
, struct leh_tf_state
*tf
)
1196 gimple_seq new_stmt
;
1201 finally
= gimple_try_cleanup (tf
->top_p
);
1202 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1205 if (tf
->may_fallthru
)
1207 seq
= lower_try_finally_dup_block (finally
, state
);
1208 lower_eh_constructs_1 (state
, seq
);
1209 gimple_seq_add_seq (&new_stmt
, seq
);
1211 tmp
= lower_try_finally_fallthru_label (tf
);
1212 x
= gimple_build_goto (tmp
);
1213 gimple_seq_add_stmt (&new_stmt
, x
);
1218 x
= gimple_build_label (tf
->eh_label
);
1219 gimple_seq_add_stmt (&new_stmt
, x
);
1221 seq
= lower_try_finally_dup_block (finally
, state
);
1222 lower_eh_constructs_1 (state
, seq
);
1223 gimple_seq_add_seq (&new_stmt
, seq
);
1225 x
= gimple_build_resx (get_eh_region_number (tf
->region
));
1226 gimple_seq_add_stmt (&new_stmt
, x
);
1231 struct goto_queue_node
*q
, *qe
;
1232 tree return_val
= NULL
;
1233 int return_index
, index
;
1236 struct goto_queue_node
*q
;
1240 return_index
= VEC_length (tree
, tf
->dest_array
);
1241 labels
= XCNEWVEC (struct labels_s
, return_index
+ 1);
1244 qe
= q
+ tf
->goto_queue_active
;
1247 index
= q
->index
< 0 ? return_index
: q
->index
;
1249 if (!labels
[index
].q
)
1250 labels
[index
].q
= q
;
1253 for (index
= 0; index
< return_index
+ 1; index
++)
1257 q
= labels
[index
].q
;
1261 lab
= labels
[index
].label
= create_artificial_label ();
1263 if (index
== return_index
)
1264 do_return_redirection (q
, lab
, NULL
, &return_val
);
1266 do_goto_redirection (q
, lab
, NULL
, tf
);
1268 x
= gimple_build_label (lab
);
1269 gimple_seq_add_stmt (&new_stmt
, x
);
1271 seq
= lower_try_finally_dup_block (finally
, state
);
1272 lower_eh_constructs_1 (state
, seq
);
1273 gimple_seq_add_seq (&new_stmt
, seq
);
1275 gimple_seq_add_stmt (&new_stmt
, q
->cont_stmt
);
1276 maybe_record_in_goto_queue (state
, q
->cont_stmt
);
1279 for (q
= tf
->goto_queue
; q
< qe
; q
++)
1283 index
= q
->index
< 0 ? return_index
: q
->index
;
1285 if (labels
[index
].q
== q
)
1288 lab
= labels
[index
].label
;
1290 if (index
== return_index
)
1291 do_return_redirection (q
, lab
, NULL
, &return_val
);
1293 do_goto_redirection (q
, lab
, NULL
, tf
);
1296 replace_goto_queue (tf
);
1300 /* Need to link new stmts after running replace_goto_queue due
1301 to not wanting to process the same goto stmts twice. */
1302 gimple_seq_add_seq (&tf
->top_p_seq
, new_stmt
);
1305 /* A subroutine of lower_try_finally. There are multiple edges incoming
1306 and outgoing from the finally block. Implement this by instrumenting
1307 each incoming edge and creating a switch statement at the end of the
1308 finally block that branches to the appropriate destination. */
1311 lower_try_finally_switch (struct leh_state
*state
, struct leh_tf_state
*tf
)
1313 struct goto_queue_node
*q
, *qe
;
1314 tree return_val
= NULL
;
1315 tree finally_tmp
, finally_label
;
1316 int return_index
, eh_index
, fallthru_index
;
1317 int nlabels
, ndests
, j
, last_case_index
;
1319 VEC (tree
,heap
) *case_label_vec
;
1320 gimple_seq switch_body
;
1325 struct pointer_map_t
*cont_map
= NULL
;
1327 switch_body
= gimple_seq_alloc ();
1329 /* Mash the TRY block to the head of the chain. */
1330 finally
= gimple_try_cleanup (tf
->top_p
);
1331 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1333 /* Lower the finally block itself. */
1334 lower_eh_constructs_1 (state
, finally
);
1336 /* Prepare for switch statement generation. */
1337 nlabels
= VEC_length (tree
, tf
->dest_array
);
1338 return_index
= nlabels
;
1339 eh_index
= return_index
+ tf
->may_return
;
1340 fallthru_index
= eh_index
+ tf
->may_throw
;
1341 ndests
= fallthru_index
+ tf
->may_fallthru
;
1343 finally_tmp
= create_tmp_var (integer_type_node
, "finally_tmp");
1344 finally_label
= create_artificial_label ();
1346 /* We use VEC_quick_push on case_label_vec throughout this function,
1347 since we know the size in advance and allocate precisely as muce
1349 case_label_vec
= VEC_alloc (tree
, heap
, ndests
);
1351 last_case_index
= 0;
1353 /* Begin inserting code for getting to the finally block. Things
1354 are done in this order to correspond to the sequence the code is
1357 if (tf
->may_fallthru
)
1359 x
= gimple_build_assign (finally_tmp
, build_int_cst (integer_type_node
,
1361 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1365 x
= gimple_build_goto (finally_label
);
1366 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1370 last_case
= build3 (CASE_LABEL_EXPR
, void_type_node
,
1371 build_int_cst (NULL_TREE
, fallthru_index
), NULL
,
1372 create_artificial_label ());
1373 VEC_quick_push (tree
, case_label_vec
, last_case
);
1376 x
= gimple_build_label (CASE_LABEL (last_case
));
1377 gimple_seq_add_stmt (&switch_body
, x
);
1379 tmp
= lower_try_finally_fallthru_label (tf
);
1380 x
= gimple_build_goto (tmp
);
1381 gimple_seq_add_stmt (&switch_body
, x
);
1386 x
= gimple_build_label (tf
->eh_label
);
1387 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1389 x
= gimple_build_assign (finally_tmp
, build_int_cst (integer_type_node
,
1391 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1393 last_case
= build3 (CASE_LABEL_EXPR
, void_type_node
,
1394 build_int_cst (NULL_TREE
, eh_index
), NULL
,
1395 create_artificial_label ());
1396 VEC_quick_push (tree
, case_label_vec
, last_case
);
1399 x
= gimple_build_label (CASE_LABEL (last_case
));
1400 gimple_seq_add_stmt (&switch_body
, x
);
1401 x
= gimple_build_resx (get_eh_region_number (tf
->region
));
1402 gimple_seq_add_stmt (&switch_body
, x
);
1405 x
= gimple_build_label (finally_label
);
1406 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1408 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1410 /* Redirect each incoming goto edge. */
1412 qe
= q
+ tf
->goto_queue_active
;
1413 j
= last_case_index
+ tf
->may_return
;
1414 /* Prepare the assignments to finally_tmp that are executed upon the
1415 entrance through a particular edge. */
1420 unsigned int case_index
;
1422 mod
= gimple_seq_alloc ();
1426 x
= gimple_build_assign (finally_tmp
,
1427 build_int_cst (integer_type_node
,
1429 gimple_seq_add_stmt (&mod
, x
);
1430 do_return_redirection (q
, finally_label
, mod
, &return_val
);
1431 switch_id
= return_index
;
1435 x
= gimple_build_assign (finally_tmp
,
1436 build_int_cst (integer_type_node
, q
->index
));
1437 gimple_seq_add_stmt (&mod
, x
);
1438 do_goto_redirection (q
, finally_label
, mod
, tf
);
1439 switch_id
= q
->index
;
1442 case_index
= j
+ q
->index
;
1443 if (VEC_length (tree
, case_label_vec
) <= case_index
1444 || !VEC_index (tree
, case_label_vec
, case_index
))
1448 case_lab
= build3 (CASE_LABEL_EXPR
, void_type_node
,
1449 build_int_cst (NULL_TREE
, switch_id
), NULL
,
1451 /* We store the cont_stmt in the pointer map, so that we can recover
1452 it in the loop below. We don't create the new label while
1453 walking the goto_queue because pointers don't offer a stable
1456 cont_map
= pointer_map_create ();
1457 slot
= pointer_map_insert (cont_map
, case_lab
);
1458 *slot
= q
->cont_stmt
;
1459 VEC_quick_push (tree
, case_label_vec
, case_lab
);
1462 for (j
= last_case_index
; j
< last_case_index
+ nlabels
; j
++)
1468 last_case
= VEC_index (tree
, case_label_vec
, j
);
1470 gcc_assert (last_case
);
1471 gcc_assert (cont_map
);
1473 slot
= pointer_map_contains (cont_map
, last_case
);
1474 /* As the comment above suggests, CASE_LABEL (last_case) was just a
1475 placeholder, it does not store an actual label, yet. */
1477 cont_stmt
= *(gimple
*) slot
;
1479 label
= create_artificial_label ();
1480 CASE_LABEL (last_case
) = label
;
1482 x
= gimple_build_label (label
);
1483 gimple_seq_add_stmt (&switch_body
, x
);
1484 gimple_seq_add_stmt (&switch_body
, cont_stmt
);
1485 maybe_record_in_goto_queue (state
, cont_stmt
);
1488 pointer_map_destroy (cont_map
);
1490 replace_goto_queue (tf
);
1492 /* Make sure that the last case is the default label, as one is required.
1493 Then sort the labels, which is also required in GIMPLE. */
1494 CASE_LOW (last_case
) = NULL
;
1495 sort_case_labels (case_label_vec
);
1497 /* Build the switch statement, setting last_case to be the default
1499 switch_stmt
= gimple_build_switch_vec (finally_tmp
, last_case
,
1502 /* Need to link SWITCH_STMT after running replace_goto_queue
1503 due to not wanting to process the same goto stmts twice. */
1504 gimple_seq_add_stmt (&tf
->top_p_seq
, switch_stmt
);
1505 gimple_seq_add_seq (&tf
->top_p_seq
, switch_body
);
1508 /* Decide whether or not we are going to duplicate the finally block.
1509 There are several considerations.
1511 First, if this is Java, then the finally block contains code
1512 written by the user. It has line numbers associated with it,
1513 so duplicating the block means it's difficult to set a breakpoint.
1514 Since controlling code generation via -g is verboten, we simply
1515 never duplicate code without optimization.
1517 Second, we'd like to prevent egregious code growth. One way to
1518 do this is to estimate the size of the finally block, multiply
1519 that by the number of copies we'd need to make, and compare against
1520 the estimate of the size of the switch machinery we'd have to add. */
1523 decide_copy_try_finally (int ndests
, gimple_seq finally
)
1525 int f_estimate
, sw_estimate
;
1530 /* Finally estimate N times, plus N gotos. */
1531 f_estimate
= count_insns_seq (finally
, &eni_size_weights
);
1532 f_estimate
= (f_estimate
+ 1) * ndests
;
1534 /* Switch statement (cost 10), N variable assignments, N gotos. */
1535 sw_estimate
= 10 + 2 * ndests
;
1537 /* Optimize for size clearly wants our best guess. */
1538 if (optimize_function_for_size_p (cfun
))
1539 return f_estimate
< sw_estimate
;
1541 /* ??? These numbers are completely made up so far. */
1543 return f_estimate
< 100 || f_estimate
< sw_estimate
* 2;
1545 return f_estimate
< 40 || f_estimate
* 2 < sw_estimate
* 3;
1549 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
1550 to a sequence of labels and blocks, plus the exception region trees
1551 that record all the magic. This is complicated by the need to
1552 arrange for the FINALLY block to be executed on all exits. */
1555 lower_try_finally (struct leh_state
*state
, gimple tp
)
1557 struct leh_tf_state this_tf
;
1558 struct leh_state this_state
;
1561 /* Process the try block. */
1563 memset (&this_tf
, 0, sizeof (this_tf
));
1564 this_tf
.try_finally_expr
= tp
;
1566 this_tf
.outer
= state
;
1567 if (using_eh_for_cleanups_p
)
1569 = gen_eh_region_cleanup (state
->cur_region
, state
->prev_try
);
1571 this_tf
.region
= NULL
;
1573 this_state
.cur_region
= this_tf
.region
;
1574 this_state
.prev_try
= state
->prev_try
;
1575 this_state
.tf
= &this_tf
;
1577 lower_eh_constructs_1 (&this_state
, gimple_try_eval(tp
));
1579 /* Determine if the try block is escaped through the bottom. */
1580 this_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1582 /* Determine if any exceptions are possible within the try block. */
1583 if (using_eh_for_cleanups_p
)
1584 this_tf
.may_throw
= get_eh_region_may_contain_throw (this_tf
.region
);
1585 if (this_tf
.may_throw
)
1587 this_tf
.eh_label
= create_artificial_label ();
1588 set_eh_region_tree_label (this_tf
.region
, this_tf
.eh_label
);
1589 honor_protect_cleanup_actions (state
, &this_state
, &this_tf
);
1592 /* Determine how many edges (still) reach the finally block. Or rather,
1593 how many destinations are reached by the finally block. Use this to
1594 determine how we process the finally block itself. */
1596 ndests
= VEC_length (tree
, this_tf
.dest_array
);
1597 ndests
+= this_tf
.may_fallthru
;
1598 ndests
+= this_tf
.may_return
;
1599 ndests
+= this_tf
.may_throw
;
1601 /* If the FINALLY block is not reachable, dike it out. */
1604 gimple_seq_add_seq (&this_tf
.top_p_seq
, gimple_try_eval (tp
));
1605 gimple_try_set_cleanup (tp
, NULL
);
1607 /* If the finally block doesn't fall through, then any destination
1608 we might try to impose there isn't reached either. There may be
1609 some minor amount of cleanup and redirection still needed. */
1610 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp
)))
1611 lower_try_finally_nofallthru (state
, &this_tf
);
1613 /* We can easily special-case redirection to a single destination. */
1614 else if (ndests
== 1)
1615 lower_try_finally_onedest (state
, &this_tf
);
1616 else if (decide_copy_try_finally (ndests
, gimple_try_cleanup (tp
)))
1617 lower_try_finally_copy (state
, &this_tf
);
1619 lower_try_finally_switch (state
, &this_tf
);
1621 /* If someone requested we add a label at the end of the transformed
1623 if (this_tf
.fallthru_label
)
1625 /* This must be reached only if ndests == 0. */
1626 gimple x
= gimple_build_label (this_tf
.fallthru_label
);
1627 gimple_seq_add_stmt (&this_tf
.top_p_seq
, x
);
1630 VEC_free (tree
, heap
, this_tf
.dest_array
);
1631 if (this_tf
.goto_queue
)
1632 free (this_tf
.goto_queue
);
1633 if (this_tf
.goto_queue_map
)
1634 pointer_map_destroy (this_tf
.goto_queue_map
);
1636 return this_tf
.top_p_seq
;
1639 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1640 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1641 exception region trees that records all the magic. */
1644 lower_catch (struct leh_state
*state
, gimple tp
)
1646 struct eh_region
*try_region
;
1647 struct leh_state this_state
;
1648 gimple_stmt_iterator gsi
;
1651 try_region
= gen_eh_region_try (state
->cur_region
);
1652 this_state
.cur_region
= try_region
;
1653 this_state
.prev_try
= try_region
;
1654 this_state
.tf
= state
->tf
;
1656 lower_eh_constructs_1 (&this_state
, gimple_try_eval (tp
));
1658 if (!get_eh_region_may_contain_throw (try_region
))
1660 return gimple_try_eval (tp
);
1664 for (gsi
= gsi_start (gimple_try_cleanup (tp
)); !gsi_end_p (gsi
); )
1666 struct eh_region
*catch_region
;
1670 gcatch
= gsi_stmt (gsi
);
1671 catch_region
= gen_eh_region_catch (try_region
,
1672 gimple_catch_types (gcatch
));
1674 this_state
.cur_region
= catch_region
;
1675 this_state
.prev_try
= state
->prev_try
;
1676 lower_eh_constructs_1 (&this_state
, gimple_catch_handler (gcatch
));
1678 eh_label
= create_artificial_label ();
1679 set_eh_region_tree_label (catch_region
, eh_label
);
1681 x
= gimple_build_label (eh_label
);
1682 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
1684 if (gimple_seq_may_fallthru (gimple_catch_handler (gcatch
)))
1687 out_label
= create_artificial_label ();
1689 x
= gimple_build_goto (out_label
);
1690 gimple_seq_add_stmt (gimple_catch_handler_ptr (gcatch
), x
);
1693 gsi_insert_seq_before (&gsi
, gimple_catch_handler (gcatch
),
1695 gsi_remove (&gsi
, false);
1698 return frob_into_branch_around (tp
, NULL
, out_label
);
1701 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1702 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1703 region trees that record all the magic. */
1706 lower_eh_filter (struct leh_state
*state
, gimple tp
)
1708 struct leh_state this_state
;
1709 struct eh_region
*this_region
;
1713 inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1715 if (gimple_eh_filter_must_not_throw (inner
))
1716 this_region
= gen_eh_region_must_not_throw (state
->cur_region
);
1718 this_region
= gen_eh_region_allowed (state
->cur_region
,
1719 gimple_eh_filter_types (inner
));
1720 this_state
= *state
;
1721 this_state
.cur_region
= this_region
;
1722 /* For must not throw regions any cleanup regions inside it
1723 can't reach outer catch regions. */
1724 if (gimple_eh_filter_must_not_throw (inner
))
1725 this_state
.prev_try
= NULL
;
1727 lower_eh_constructs_1 (&this_state
, gimple_try_eval (tp
));
1729 if (!get_eh_region_may_contain_throw (this_region
))
1731 return gimple_try_eval (tp
);
1734 lower_eh_constructs_1 (state
, gimple_eh_filter_failure (inner
));
1735 gimple_try_set_cleanup (tp
, gimple_eh_filter_failure (inner
));
1737 eh_label
= create_artificial_label ();
1738 set_eh_region_tree_label (this_region
, eh_label
);
1740 return frob_into_branch_around (tp
, eh_label
, NULL
);
1743 /* Implement a cleanup expression. This is similar to try-finally,
1744 except that we only execute the cleanup block for exception edges. */
1747 lower_cleanup (struct leh_state
*state
, gimple tp
)
1749 struct leh_state this_state
;
1750 struct eh_region
*this_region
;
1751 struct leh_tf_state fake_tf
;
1754 /* If not using eh, then exception-only cleanups are no-ops. */
1755 if (!flag_exceptions
)
1757 result
= gimple_try_eval (tp
);
1758 lower_eh_constructs_1 (state
, result
);
1762 this_region
= gen_eh_region_cleanup (state
->cur_region
, state
->prev_try
);
1763 this_state
= *state
;
1764 this_state
.cur_region
= this_region
;
1766 lower_eh_constructs_1 (&this_state
, gimple_try_eval (tp
));
1768 if (!get_eh_region_may_contain_throw (this_region
))
1770 return gimple_try_eval (tp
);
1773 /* Build enough of a try-finally state so that we can reuse
1774 honor_protect_cleanup_actions. */
1775 memset (&fake_tf
, 0, sizeof (fake_tf
));
1777 fake_tf
.outer
= state
;
1778 fake_tf
.region
= this_region
;
1779 fake_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1780 fake_tf
.may_throw
= true;
1782 fake_tf
.eh_label
= create_artificial_label ();
1783 set_eh_region_tree_label (this_region
, fake_tf
.eh_label
);
1785 honor_protect_cleanup_actions (state
, NULL
, &fake_tf
);
1787 if (fake_tf
.may_throw
)
1789 /* In this case honor_protect_cleanup_actions had nothing to do,
1790 and we should process this normally. */
1791 lower_eh_constructs_1 (state
, gimple_try_cleanup (tp
));
1792 result
= frob_into_branch_around (tp
, fake_tf
.eh_label
,
1793 fake_tf
.fallthru_label
);
1797 /* In this case honor_protect_cleanup_actions did nearly all of
1798 the work. All we have left is to append the fallthru_label. */
1800 result
= gimple_try_eval (tp
);
1801 if (fake_tf
.fallthru_label
)
1803 gimple x
= gimple_build_label (fake_tf
.fallthru_label
);
1804 gimple_seq_add_stmt (&result
, x
);
1812 /* Main loop for lowering eh constructs. Also moves gsi to the next
1816 lower_eh_constructs_2 (struct leh_state
*state
, gimple_stmt_iterator
*gsi
)
1820 gimple stmt
= gsi_stmt (*gsi
);
1822 switch (gimple_code (stmt
))
1826 /* Look for things that can throw exceptions, and record them. */
1827 if (state
->cur_region
&& stmt_could_throw_p (stmt
))
1829 record_stmt_eh_region (state
->cur_region
, stmt
);
1830 note_eh_region_may_contain_throw (state
->cur_region
);
1837 maybe_record_in_goto_queue (state
, stmt
);
1841 verify_norecord_switch_expr (state
, stmt
);
1845 if (gimple_try_kind (stmt
) == GIMPLE_TRY_FINALLY
)
1846 replace
= lower_try_finally (state
, stmt
);
1849 x
= gimple_seq_first_stmt (gimple_try_cleanup (stmt
));
1850 switch (gimple_code (x
))
1853 replace
= lower_catch (state
, stmt
);
1855 case GIMPLE_EH_FILTER
:
1856 replace
= lower_eh_filter (state
, stmt
);
1859 replace
= lower_cleanup (state
, stmt
);
1864 /* Remove the old stmt and insert the transformed sequence
1866 gsi_insert_seq_before (gsi
, replace
, GSI_SAME_STMT
);
1867 gsi_remove (gsi
, true);
1869 /* Return since we don't want gsi_next () */
1873 /* A type, a decl, or some kind of statement that we're not
1874 interested in. Don't walk them. */
1881 /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
1884 lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq seq
)
1886 gimple_stmt_iterator gsi
;
1887 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
);)
1888 lower_eh_constructs_2 (state
, &gsi
);
1892 lower_eh_constructs (void)
1894 struct leh_state null_state
;
1896 gimple_seq bodyp
= gimple_body (current_function_decl
);
1898 finally_tree
= htab_create (31, struct_ptr_hash
, struct_ptr_eq
, free
);
1900 collect_finally_tree_1 (bodyp
, NULL
);
1902 memset (&null_state
, 0, sizeof (null_state
));
1903 lower_eh_constructs_1 (&null_state
, bodyp
);
1905 htab_delete (finally_tree
);
1907 collect_eh_region_array ();
1911 struct gimple_opt_pass pass_lower_eh
=
1917 lower_eh_constructs
, /* execute */
1920 0, /* static_pass_number */
1921 TV_TREE_EH
, /* tv_id */
1922 PROP_gimple_lcf
, /* properties_required */
1923 PROP_gimple_leh
, /* properties_provided */
1924 0, /* properties_destroyed */
1925 0, /* todo_flags_start */
1926 TODO_dump_func
/* todo_flags_finish */
1931 /* Construct EH edges for STMT. */
1934 make_eh_edge (struct eh_region
*region
, void *data
)
1938 basic_block src
, dst
;
1940 stmt
= (gimple
) data
;
1941 lab
= get_eh_region_tree_label (region
);
1943 src
= gimple_bb (stmt
);
1944 dst
= label_to_block (lab
);
1946 make_edge (src
, dst
, EDGE_ABNORMAL
| EDGE_EH
);
1949 /* See if STMT is call that might be inlined. */
1952 inlinable_call_p (gimple stmt
)
1955 if (gimple_code (stmt
) != GIMPLE_CALL
)
1957 if (cfun
->after_inlining
)
1959 /* Indirect calls can be propagated to direct call
1961 decl
= gimple_call_fndecl (stmt
);
1964 if (cgraph_function_flags_ready
1965 && cgraph_function_body_availability (cgraph_node (decl
))
1966 < AVAIL_OVERWRITABLE
)
1968 return !DECL_UNINLINABLE (decl
);
1972 make_eh_edges (gimple stmt
)
1976 bool inlinable
= false;
1978 if (gimple_code (stmt
) == GIMPLE_RESX
)
1980 region_nr
= gimple_resx_region (stmt
);
1985 region_nr
= lookup_stmt_eh_region (stmt
);
1989 inlinable
= inlinable_call_p (stmt
);
1992 foreach_reachable_handler (region_nr
, is_resx
, inlinable
, make_eh_edge
, stmt
);
1995 static bool mark_eh_edge_found_error
;
1997 /* Mark edge make_eh_edge would create for given region by setting it aux
1998 field, output error if something goes wrong. */
2001 mark_eh_edge (struct eh_region
*region
, void *data
)
2005 basic_block src
, dst
;
2008 stmt
= (gimple
) data
;
2009 lab
= get_eh_region_tree_label (region
);
2011 src
= gimple_bb (stmt
);
2012 dst
= label_to_block (lab
);
2014 e
= find_edge (src
, dst
);
2017 error ("EH edge %i->%i is missing", src
->index
, dst
->index
);
2018 mark_eh_edge_found_error
= true;
2020 else if (!(e
->flags
& EDGE_EH
))
2022 error ("EH edge %i->%i miss EH flag", src
->index
, dst
->index
);
2023 mark_eh_edge_found_error
= true;
2027 /* ??? might not be mistake. */
2028 error ("EH edge %i->%i has duplicated regions", src
->index
, dst
->index
);
2029 mark_eh_edge_found_error
= true;
2035 /* Verify that BB containing STMT as the last statement, has precisely the
2036 edges that make_eh_edges would create. */
2039 verify_eh_edges (gimple stmt
)
2043 basic_block bb
= gimple_bb (stmt
);
2046 bool inlinable
= false;
2048 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2049 gcc_assert (!e
->aux
);
2050 mark_eh_edge_found_error
= false;
2051 if (gimple_code (stmt
) == GIMPLE_RESX
)
2053 region_nr
= gimple_resx_region (stmt
);
2058 region_nr
= lookup_stmt_eh_region (stmt
);
2061 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2062 if (e
->flags
& EDGE_EH
)
2064 error ("BB %i can not throw but has EH edges", bb
->index
);
2069 if (!stmt_could_throw_p (stmt
))
2071 error ("BB %i last statement has incorrectly set region", bb
->index
);
2074 inlinable
= inlinable_call_p (stmt
);
2078 foreach_reachable_handler (region_nr
, is_resx
, inlinable
, mark_eh_edge
, stmt
);
2079 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2081 if ((e
->flags
& EDGE_EH
) && !e
->aux
)
2083 error ("unnecessary EH edge %i->%i", bb
->index
, e
->dest
->index
);
2084 mark_eh_edge_found_error
= true;
2090 return mark_eh_edge_found_error
;
2094 /* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2097 operation_could_trap_helper_p (enum tree_code op
,
2108 case TRUNC_DIV_EXPR
:
2110 case FLOOR_DIV_EXPR
:
2111 case ROUND_DIV_EXPR
:
2112 case EXACT_DIV_EXPR
:
2114 case FLOOR_MOD_EXPR
:
2115 case ROUND_MOD_EXPR
:
2116 case TRUNC_MOD_EXPR
:
2118 if (honor_snans
|| honor_trapv
)
2121 return flag_trapping_math
;
2122 if (!TREE_CONSTANT (divisor
) || integer_zerop (divisor
))
2131 /* Some floating point comparisons may trap. */
2136 case UNORDERED_EXPR
:
2146 case FIX_TRUNC_EXPR
:
2147 /* Conversion of floating point might trap. */
2153 /* These operations don't trap with floating point. */
2161 /* Any floating arithmetic may trap. */
2162 if (fp_operation
&& flag_trapping_math
)
2169 /* Any floating arithmetic may trap. */
2170 if (fp_operation
&& flag_trapping_math
)
2178 /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2179 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2180 type operands that may trap. If OP is a division operator, DIVISOR contains
2181 the value of the divisor. */
2184 operation_could_trap_p (enum tree_code op
, bool fp_operation
, bool honor_trapv
,
2187 bool honor_nans
= (fp_operation
&& flag_trapping_math
2188 && !flag_finite_math_only
);
2189 bool honor_snans
= fp_operation
&& flag_signaling_nans
!= 0;
2192 if (TREE_CODE_CLASS (op
) != tcc_comparison
2193 && TREE_CODE_CLASS (op
) != tcc_unary
2194 && TREE_CODE_CLASS (op
) != tcc_binary
)
2197 return operation_could_trap_helper_p (op
, fp_operation
, honor_trapv
,
2198 honor_nans
, honor_snans
, divisor
,
2202 /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2203 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2204 This routine expects only GIMPLE lhs or rhs input. */
2207 tree_could_trap_p (tree expr
)
2209 enum tree_code code
;
2210 bool fp_operation
= false;
2211 bool honor_trapv
= false;
2212 tree t
, base
, div
= NULL_TREE
;
2217 code
= TREE_CODE (expr
);
2218 t
= TREE_TYPE (expr
);
2222 if (COMPARISON_CLASS_P (expr
))
2223 fp_operation
= FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2225 fp_operation
= FLOAT_TYPE_P (t
);
2226 honor_trapv
= INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
);
2229 if (TREE_CODE_CLASS (code
) == tcc_binary
)
2230 div
= TREE_OPERAND (expr
, 1);
2231 if (operation_could_trap_p (code
, fp_operation
, honor_trapv
, div
))
2237 case TARGET_MEM_REF
:
2238 /* For TARGET_MEM_REFs use the information based on the original
2240 expr
= TMR_ORIGINAL (expr
);
2241 code
= TREE_CODE (expr
);
2248 case VIEW_CONVERT_EXPR
:
2249 case WITH_SIZE_EXPR
:
2250 expr
= TREE_OPERAND (expr
, 0);
2251 code
= TREE_CODE (expr
);
2254 case ARRAY_RANGE_REF
:
2255 base
= TREE_OPERAND (expr
, 0);
2256 if (tree_could_trap_p (base
))
2259 if (TREE_THIS_NOTRAP (expr
))
2262 return !range_in_array_bounds_p (expr
);
2265 base
= TREE_OPERAND (expr
, 0);
2266 if (tree_could_trap_p (base
))
2269 if (TREE_THIS_NOTRAP (expr
))
2272 return !in_array_bounds_p (expr
);
2275 case ALIGN_INDIRECT_REF
:
2276 case MISALIGNED_INDIRECT_REF
:
2277 return !TREE_THIS_NOTRAP (expr
);
2280 return TREE_THIS_VOLATILE (expr
);
2284 t
= get_callee_fndecl (expr
);
2285 /* Assume that calls to weak functions may trap. */
2286 if (!t
|| !DECL_P (t
) || DECL_WEAK (t
))
2296 /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2297 an assignment or a conditional) may throw. */
2300 stmt_could_throw_1_p (gimple stmt
)
2302 enum tree_code code
= gimple_expr_code (stmt
);
2303 bool honor_nans
= false;
2304 bool honor_snans
= false;
2305 bool fp_operation
= false;
2306 bool honor_trapv
= false;
2311 if (TREE_CODE_CLASS (code
) == tcc_comparison
2312 || TREE_CODE_CLASS (code
) == tcc_unary
2313 || TREE_CODE_CLASS (code
) == tcc_binary
)
2315 t
= gimple_expr_type (stmt
);
2316 fp_operation
= FLOAT_TYPE_P (t
);
2319 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
2320 honor_snans
= flag_signaling_nans
!= 0;
2322 else if (INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
))
2326 /* Check if the main expression may trap. */
2327 t
= is_gimple_assign (stmt
) ? gimple_assign_rhs2 (stmt
) : NULL
;
2328 ret
= operation_could_trap_helper_p (code
, fp_operation
, honor_trapv
,
2329 honor_nans
, honor_snans
, t
,
2334 /* If the expression does not trap, see if any of the individual operands may
2336 for (i
= 0; i
< gimple_num_ops (stmt
); i
++)
2337 if (tree_could_trap_p (gimple_op (stmt
, i
)))
2344 /* Return true if statement STMT could throw an exception. */
2347 stmt_could_throw_p (gimple stmt
)
2349 enum gimple_code code
;
2351 if (!flag_exceptions
)
2354 /* The only statements that can throw an exception are assignments,
2355 conditionals, calls and asms. */
2356 code
= gimple_code (stmt
);
2357 if (code
!= GIMPLE_ASSIGN
2358 && code
!= GIMPLE_COND
2359 && code
!= GIMPLE_CALL
2360 && code
!= GIMPLE_ASM
)
2363 /* If exceptions can only be thrown by function calls and STMT is not a
2364 GIMPLE_CALL, the statement cannot throw. */
2365 if (!flag_non_call_exceptions
&& code
!= GIMPLE_CALL
)
2368 if (code
== GIMPLE_ASSIGN
|| code
== GIMPLE_COND
)
2369 return stmt_could_throw_1_p (stmt
);
2370 else if (is_gimple_call (stmt
))
2371 return (gimple_call_flags (stmt
) & ECF_NOTHROW
) == 0;
2372 else if (gimple_code (stmt
) == GIMPLE_ASM
)
2373 return (gimple_asm_volatile_p (stmt
));
2381 /* Return true if expression T could throw an exception. */
2384 tree_could_throw_p (tree t
)
2386 if (!flag_exceptions
)
2388 if (TREE_CODE (t
) == MODIFY_EXPR
)
2390 if (flag_non_call_exceptions
2391 && tree_could_trap_p (TREE_OPERAND (t
, 0)))
2393 t
= TREE_OPERAND (t
, 1);
2396 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
2397 t
= TREE_OPERAND (t
, 0);
2398 if (TREE_CODE (t
) == CALL_EXPR
)
2399 return (call_expr_flags (t
) & ECF_NOTHROW
) == 0;
2400 if (flag_non_call_exceptions
)
2401 return tree_could_trap_p (t
);
2405 /* Return true if STMT can throw an exception that is not caught within
2406 the current function (CFUN). */
2409 stmt_can_throw_external (gimple stmt
)
2412 bool is_resx
= false;
2413 bool inlinable_call
= false;
2415 if (!stmt_could_throw_p (stmt
))
2418 if (gimple_code (stmt
) == GIMPLE_RESX
)
2420 region_nr
= gimple_resx_region (stmt
);
2424 region_nr
= lookup_stmt_eh_region (stmt
);
2429 return can_throw_external_1 (region_nr
, is_resx
, inlinable_call
);
2432 /* Return true if STMT can throw an exception that is caught within
2433 the current function (CFUN). */
2436 stmt_can_throw_internal (gimple stmt
)
2439 bool is_resx
= false;
2440 bool inlinable_call
= false;
2442 if (gimple_code (stmt
) == GIMPLE_RESX
)
2444 region_nr
= gimple_resx_region (stmt
);
2449 region_nr
= lookup_stmt_eh_region (stmt
);
2450 inlinable_call
= inlinable_call_p (stmt
);
2456 return can_throw_internal_1 (region_nr
, is_resx
, inlinable_call
);
2460 /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
2461 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
2462 in the table if it should be in there. Return TRUE if a replacement was
2463 done that my require an EH edge purge. */
2466 maybe_clean_or_replace_eh_stmt (gimple old_stmt
, gimple new_stmt
)
2468 int region_nr
= lookup_stmt_eh_region (old_stmt
);
2472 bool new_stmt_could_throw
= stmt_could_throw_p (new_stmt
);
2474 if (new_stmt
== old_stmt
&& new_stmt_could_throw
)
2477 remove_stmt_from_eh_region (old_stmt
);
2478 if (new_stmt_could_throw
)
2480 add_stmt_to_eh_region (new_stmt
, region_nr
);
2490 /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
2491 GIMPLE_TRY) that are similar enough to be considered the same. Currently
2492 this only handles handlers consisting of a single call, as that's the
2493 important case for C++: a destructor call for a particular object showing
2494 up in multiple handlers. */
2497 same_handler_p (gimple_seq oneh
, gimple_seq twoh
)
2499 gimple_stmt_iterator gsi
;
2503 gsi
= gsi_start (oneh
);
2504 if (!gsi_one_before_end_p (gsi
))
2506 ones
= gsi_stmt (gsi
);
2508 gsi
= gsi_start (twoh
);
2509 if (!gsi_one_before_end_p (gsi
))
2511 twos
= gsi_stmt (gsi
);
2513 if (!is_gimple_call (ones
)
2514 || !is_gimple_call (twos
)
2515 || gimple_call_lhs (ones
)
2516 || gimple_call_lhs (twos
)
2517 || gimple_call_chain (ones
)
2518 || gimple_call_chain (twos
)
2519 || !operand_equal_p (gimple_call_fn (ones
), gimple_call_fn (twos
), 0)
2520 || gimple_call_num_args (ones
) != gimple_call_num_args (twos
))
2523 for (ai
= 0; ai
< gimple_call_num_args (ones
); ++ai
)
2524 if (!operand_equal_p (gimple_call_arg (ones
, ai
),
2525 gimple_call_arg (twos
, ai
), 0))
2532 try { A() } finally { try { ~B() } catch { ~A() } }
2533 try { ... } finally { ~A() }
2535 try { A() } catch { ~B() }
2536 try { ~B() ... } finally { ~A() }
2538 This occurs frequently in C++, where A is a local variable and B is a
2539 temporary used in the initializer for A. */
2542 optimize_double_finally (gimple one
, gimple two
)
2545 gimple_stmt_iterator gsi
;
2547 gsi
= gsi_start (gimple_try_cleanup (one
));
2548 if (!gsi_one_before_end_p (gsi
))
2551 oneh
= gsi_stmt (gsi
);
2552 if (gimple_code (oneh
) != GIMPLE_TRY
2553 || gimple_try_kind (oneh
) != GIMPLE_TRY_CATCH
)
2556 if (same_handler_p (gimple_try_cleanup (oneh
), gimple_try_cleanup (two
)))
2558 gimple_seq seq
= gimple_try_eval (oneh
);
2560 gimple_try_set_cleanup (one
, seq
);
2561 gimple_try_set_kind (one
, GIMPLE_TRY_CATCH
);
2562 seq
= copy_gimple_seq_and_replace_locals (seq
);
2563 gimple_seq_add_seq (&seq
, gimple_try_eval (two
));
2564 gimple_try_set_eval (two
, seq
);
2568 /* Perform EH refactoring optimizations that are simpler to do when code
2569 flow has been lowered but EH structures haven't. */
2572 refactor_eh_r (gimple_seq seq
)
2574 gimple_stmt_iterator gsi
;
2579 gsi
= gsi_start (seq
);
2583 if (gsi_end_p (gsi
))
2586 two
= gsi_stmt (gsi
);
2589 && gimple_code (one
) == GIMPLE_TRY
2590 && gimple_code (two
) == GIMPLE_TRY
2591 && gimple_try_kind (one
) == GIMPLE_TRY_FINALLY
2592 && gimple_try_kind (two
) == GIMPLE_TRY_FINALLY
)
2593 optimize_double_finally (one
, two
);
2595 switch (gimple_code (one
))
2598 refactor_eh_r (gimple_try_eval (one
));
2599 refactor_eh_r (gimple_try_cleanup (one
));
2602 refactor_eh_r (gimple_catch_handler (one
));
2604 case GIMPLE_EH_FILTER
:
2605 refactor_eh_r (gimple_eh_filter_failure (one
));
2620 refactor_eh_r (gimple_body (current_function_decl
));
2624 struct gimple_opt_pass pass_refactor_eh
=
2630 refactor_eh
, /* execute */
2633 0, /* static_pass_number */
2634 TV_TREE_EH
, /* tv_id */
2635 PROP_gimple_lcf
, /* properties_required */
2636 0, /* properties_provided */
2637 0, /* properties_destroyed */
2638 0, /* todo_flags_start */
2639 TODO_dump_func
/* todo_flags_finish */
2643 /* Walk statements, see what regions are really references and remove unreachable ones. */
2646 tree_remove_unreachable_handlers (void)
2648 sbitmap reachable
, contains_stmt
;
2649 VEC(int,heap
) * label_to_region
;
2652 label_to_region
= label_to_region_map ();
2653 reachable
= sbitmap_alloc (num_eh_regions ());
2654 sbitmap_zero (reachable
);
2655 contains_stmt
= sbitmap_alloc (num_eh_regions ());
2656 sbitmap_zero (contains_stmt
);
2660 gimple_stmt_iterator gsi
;
2662 bool has_eh_preds
= false;
2666 FOR_EACH_EDGE (e
, ei
, bb
->preds
) if (e
->flags
& EDGE_EH
)
2667 has_eh_preds
= true;
2669 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2671 gimple stmt
= gsi_stmt (gsi
);
2673 if (gimple_code (stmt
) == GIMPLE_LABEL
&& has_eh_preds
)
2675 int uid
= LABEL_DECL_UID (gimple_label_label (stmt
));
2676 if (uid
<= cfun
->cfg
->last_label_uid
)
2678 int region
= VEC_index (int, label_to_region
, uid
);
2679 SET_BIT (reachable
, region
);
2682 if (gimple_code (stmt
) == RESX
)
2683 SET_BIT (reachable
, gimple_resx_region (stmt
));
2684 if ((region
= lookup_stmt_eh_region (stmt
)) >= 0)
2685 SET_BIT (contains_stmt
, region
);
2691 fprintf (dump_file
, "Before removal of unreachable regions:\n");
2692 dump_eh_tree (dump_file
, cfun
);
2693 fprintf (dump_file
, "Reachable regions: ");
2694 dump_sbitmap_file (dump_file
, reachable
);
2695 fprintf (dump_file
, "Regions containing insns: ");
2696 dump_sbitmap_file (dump_file
, contains_stmt
);
2699 remove_unreachable_regions (reachable
, contains_stmt
);
2700 sbitmap_free (reachable
);
2701 sbitmap_free (contains_stmt
);
2702 VEC_free (int, heap
, label_to_region
);
2705 fprintf (dump_file
, "\n\nAfter removal of unreachable regions:\n");
2706 dump_eh_tree (dump_file
, cfun
);
2707 fprintf (dump_file
, "\n\n");
2711 /* Pattern match emtpy EH receiver looking like:
2713 save_filt.6352_662 = [filter_expr] <<<filter object>>>;
2714 save_eptr.6351_663 = [exc_ptr_expr] <<<exception object>>>;
2715 <<<exception object>>> = save_eptr.6351_663;
2716 <<<filter object>>> = save_filt.6352_662;
2721 tree_empty_eh_handler_p (basic_block bb
)
2723 gimple_stmt_iterator gsi
;
2726 gsi
= gsi_last_bb (bb
);
2729 if (gsi_end_p (gsi
))
2731 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_RESX
)
2733 region
= gimple_resx_region (gsi_stmt (gsi
));
2735 /* filter_object set. */
2737 if (gsi_end_p (gsi
))
2739 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_ASSIGN
)
2741 if (TREE_CODE (gimple_assign_lhs (gsi_stmt (gsi
))) != FILTER_EXPR
)
2744 /* filter_object set. */
2746 if (gsi_end_p (gsi
))
2748 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_ASSIGN
)
2750 if (TREE_CODE (gimple_assign_lhs (gsi_stmt (gsi
))) != EXC_PTR_EXPR
)
2753 /* filter_object get. */
2755 if (gsi_end_p (gsi
))
2757 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_ASSIGN
)
2759 if (TREE_CODE (gimple_assign_rhs1 (gsi_stmt (gsi
))) != EXC_PTR_EXPR
)
2762 /* filter_object get. */
2764 if (gsi_end_p (gsi
))
2766 if (gimple_code (gsi_stmt (gsi
)) != GIMPLE_ASSIGN
)
2768 if (TREE_CODE (gimple_assign_rhs1 (gsi_stmt (gsi
))) != FILTER_EXPR
)
2773 if (gsi_end_p (gsi
))
2775 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
2781 static bool dominance_info_invalidated
;
2783 /* Look for basic blocks containing empty exception handler and remove them.
2784 This is similar to jump forwarding, just across EH edges. */
2787 cleanup_empty_eh (basic_block bb
)
2791 /* When handler of EH region winds up to be empty, we can safely
2792 remove it. This leads to inner EH regions to be redirected
2793 to outer one, if present in function. So we need to rebuild
2794 EH edges in all sources. */
2795 if ((region
= tree_empty_eh_handler_p (bb
)))
2799 gimple_stmt_iterator si
;
2801 remove_eh_region (region
);
2803 /* It is safe to mark symbol for renaming because we have abnormal PHI
2804 here. Once EH edges are made redirectable we might need to add here
2805 similar updating as jump threading does. */
2807 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
2808 mark_sym_for_renaming (SSA_NAME_VAR (PHI_RESULT (gsi_stmt (si
))));
2810 while ((e
= ei_safe_edge (ei_start (bb
->preds
))))
2812 basic_block src
= e
->src
;
2813 gcc_assert (e
->flags
& EDGE_EH
);
2814 for (ei
= ei_start (src
->succs
); (e
= ei_safe_edge (ei
));)
2816 if (e
->flags
& EDGE_EH
)
2819 dominance_info_invalidated
= true;
2824 if (!stmt_can_throw_internal (last_stmt (src
)))
2826 make_eh_edges (last_stmt (src
));
2827 FOR_EACH_EDGE (e
, ei
, src
->succs
) if (e
->flags
& EDGE_EH
)
2829 dominance_info_invalidated
= true;
2830 for (si
= gsi_start_phis (e
->dest
); !gsi_end_p (si
);
2832 mark_sym_for_renaming (SSA_NAME_VAR
2833 (PHI_RESULT (gsi_stmt (si
))));
2837 fprintf (dump_file
, "Empty EH handler %i removed\n", region
);
2838 delete_basic_block (bb
);
2845 /* Perform cleanups and lowering of exception handling
2846 1) cleanups regions with handlers doing nothing are optimized out
2847 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
2848 3) Info about regions that are containing instructions, and regions
2849 reachable via local EH edges is collected
2850 4) Eh tree is pruned for regions no longer neccesary.
2856 bool changed
= false;
2864 fprintf (dump_file
, "Before cleanups:\n");
2865 dump_eh_tree (dump_file
, cfun
);
2868 dominance_info_invalidated
= false;
2869 /* We cannot use FOR_EACH_BB, since the basic blocks may get removed. */
2870 for (i
= NUM_FIXED_BLOCKS
; i
< last_basic_block
; i
++)
2872 bb
= BASIC_BLOCK (i
);
2874 changed
|= cleanup_empty_eh (bb
);
2876 if (dominance_info_invalidated
)
2878 free_dominance_info (CDI_DOMINATORS
);
2879 free_dominance_info (CDI_POST_DOMINATORS
);
2882 /* Removing contained cleanup can render MUST_NOT_THROW regions empty. */
2884 delete_unreachable_blocks ();
2886 tree_remove_unreachable_handlers ();
2889 fprintf (dump_file
, "After cleanups:\n");
2890 dump_eh_tree (dump_file
, cfun
);
2893 return (changed
? TODO_cleanup_cfg
| TODO_update_ssa
: 0);
2896 struct gimple_opt_pass pass_cleanup_eh
= {
2899 "ehcleanup", /* name */
2901 cleanup_eh
, /* execute */
2904 0, /* static_pass_number */
2905 TV_TREE_EH
, /* tv_id */
2906 PROP_gimple_lcf
, /* properties_required */
2907 0, /* properties_provided */
2908 0, /* properties_destroyed */
2909 0, /* todo_flags_start */
2910 TODO_dump_func
/* todo_flags_finish */