1 /* Exception handling semantics and decomposition for trees.
2 Copyright (C) 2003-2014 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)
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/>. */
22 #include "coretypes.h"
23 #include "hash-table.h"
32 #include "basic-block.h"
33 #include "tree-ssa-alias.h"
34 #include "internal-fn.h"
36 #include "gimple-expr.h"
39 #include "gimple-iterator.h"
40 #include "gimple-ssa.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "tree-into-ssa.h"
49 #include "tree-inline.h"
50 #include "tree-pass.h"
51 #include "langhooks.h"
52 #include "diagnostic-core.h"
55 #include "gimple-low.h"
57 /* In some instances a tree and a gimple need to be stored in a same table,
58 i.e. in hash tables. This is a structure to do this. */
59 typedef union {tree
*tp
; tree t
; gimple g
;} treemple
;
61 /* Misc functions used in this file. */
63 /* Remember and lookup EH landing pad data for arbitrary statements.
64 Really this means any statement that could_throw_p. We could
65 stuff this information into the stmt_ann data structure, but:
67 (1) We absolutely rely on this information being kept until
68 we get to rtl. Once we're done with lowering here, if we lose
69 the information there's no way to recover it!
71 (2) There are many more statements that *cannot* throw as
72 compared to those that can. We should be saving some amount
73 of space by only allocating memory for those that can throw. */
75 /* Add statement T in function IFUN to landing pad NUM. */
78 add_stmt_to_eh_lp_fn (struct function
*ifun
, gimple t
, int num
)
80 gcc_assert (num
!= 0);
82 if (!get_eh_throw_stmt_table (ifun
))
83 set_eh_throw_stmt_table (ifun
, hash_map
<gimple
, int>::create_ggc (31));
85 gcc_assert (!get_eh_throw_stmt_table (ifun
)->put (t
, num
));
88 /* Add statement T in the current function (cfun) to EH landing pad NUM. */
91 add_stmt_to_eh_lp (gimple t
, int num
)
93 add_stmt_to_eh_lp_fn (cfun
, t
, num
);
96 /* Add statement T to the single EH landing pad in REGION. */
99 record_stmt_eh_region (eh_region region
, gimple t
)
103 if (region
->type
== ERT_MUST_NOT_THROW
)
104 add_stmt_to_eh_lp_fn (cfun
, t
, -region
->index
);
107 eh_landing_pad lp
= region
->landing_pads
;
109 lp
= gen_eh_landing_pad (region
);
111 gcc_assert (lp
->next_lp
== NULL
);
112 add_stmt_to_eh_lp_fn (cfun
, t
, lp
->index
);
117 /* Remove statement T in function IFUN from its EH landing pad. */
120 remove_stmt_from_eh_lp_fn (struct function
*ifun
, gimple t
)
122 if (!get_eh_throw_stmt_table (ifun
))
125 if (!get_eh_throw_stmt_table (ifun
)->get (t
))
128 get_eh_throw_stmt_table (ifun
)->remove (t
);
133 /* Remove statement T in the current function (cfun) from its
137 remove_stmt_from_eh_lp (gimple t
)
139 return remove_stmt_from_eh_lp_fn (cfun
, t
);
142 /* Determine if statement T is inside an EH region in function IFUN.
143 Positive numbers indicate a landing pad index; negative numbers
144 indicate a MUST_NOT_THROW region index; zero indicates that the
145 statement is not recorded in the region table. */
148 lookup_stmt_eh_lp_fn (struct function
*ifun
, gimple t
)
150 if (ifun
->eh
->throw_stmt_table
== NULL
)
153 int *lp_nr
= ifun
->eh
->throw_stmt_table
->get (t
);
154 return lp_nr
? *lp_nr
: 0;
157 /* Likewise, but always use the current function. */
160 lookup_stmt_eh_lp (gimple t
)
162 /* We can get called from initialized data when -fnon-call-exceptions
163 is on; prevent crash. */
166 return lookup_stmt_eh_lp_fn (cfun
, t
);
169 /* First pass of EH node decomposition. Build up a tree of GIMPLE_TRY_FINALLY
170 nodes and LABEL_DECL nodes. We will use this during the second phase to
171 determine if a goto leaves the body of a TRY_FINALLY_EXPR node. */
173 struct finally_tree_node
175 /* When storing a GIMPLE_TRY, we have to record a gimple. However
176 when deciding whether a GOTO to a certain LABEL_DECL (which is a
177 tree) leaves the TRY block, its necessary to record a tree in
178 this field. Thus a treemple is used. */
183 /* Hashtable helpers. */
185 struct finally_tree_hasher
: typed_free_remove
<finally_tree_node
>
187 typedef finally_tree_node value_type
;
188 typedef finally_tree_node compare_type
;
189 static inline hashval_t
hash (const value_type
*);
190 static inline bool equal (const value_type
*, const compare_type
*);
194 finally_tree_hasher::hash (const value_type
*v
)
196 return (intptr_t)v
->child
.t
>> 4;
200 finally_tree_hasher::equal (const value_type
*v
, const compare_type
*c
)
202 return v
->child
.t
== c
->child
.t
;
205 /* Note that this table is *not* marked GTY. It is short-lived. */
206 static hash_table
<finally_tree_hasher
> *finally_tree
;
209 record_in_finally_tree (treemple child
, gimple parent
)
211 struct finally_tree_node
*n
;
212 finally_tree_node
**slot
;
214 n
= XNEW (struct finally_tree_node
);
218 slot
= finally_tree
->find_slot (n
, INSERT
);
224 collect_finally_tree (gimple stmt
, gimple region
);
226 /* Go through the gimple sequence. Works with collect_finally_tree to
227 record all GIMPLE_LABEL and GIMPLE_TRY statements. */
230 collect_finally_tree_1 (gimple_seq seq
, gimple region
)
232 gimple_stmt_iterator gsi
;
234 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
235 collect_finally_tree (gsi_stmt (gsi
), region
);
239 collect_finally_tree (gimple stmt
, gimple region
)
243 switch (gimple_code (stmt
))
246 temp
.t
= gimple_label_label (stmt
);
247 record_in_finally_tree (temp
, region
);
251 if (gimple_try_kind (stmt
) == GIMPLE_TRY_FINALLY
)
254 record_in_finally_tree (temp
, region
);
255 collect_finally_tree_1 (gimple_try_eval (stmt
), stmt
);
256 collect_finally_tree_1 (gimple_try_cleanup (stmt
), region
);
258 else if (gimple_try_kind (stmt
) == GIMPLE_TRY_CATCH
)
260 collect_finally_tree_1 (gimple_try_eval (stmt
), region
);
261 collect_finally_tree_1 (gimple_try_cleanup (stmt
), region
);
266 collect_finally_tree_1 (gimple_catch_handler (stmt
), region
);
269 case GIMPLE_EH_FILTER
:
270 collect_finally_tree_1 (gimple_eh_filter_failure (stmt
), region
);
274 collect_finally_tree_1 (gimple_eh_else_n_body (stmt
), region
);
275 collect_finally_tree_1 (gimple_eh_else_e_body (stmt
), region
);
279 /* A type, a decl, or some kind of statement that we're not
280 interested in. Don't walk them. */
286 /* Use the finally tree to determine if a jump from START to TARGET
287 would leave the try_finally node that START lives in. */
290 outside_finally_tree (treemple start
, gimple target
)
292 struct finally_tree_node n
, *p
;
297 p
= finally_tree
->find (&n
);
302 while (start
.g
!= target
);
307 /* Second pass of EH node decomposition. Actually transform the GIMPLE_TRY
308 nodes into a set of gotos, magic labels, and eh regions.
309 The eh region creation is straight-forward, but frobbing all the gotos
310 and such into shape isn't. */
312 /* The sequence into which we record all EH stuff. This will be
313 placed at the end of the function when we're all done. */
314 static gimple_seq eh_seq
;
316 /* Record whether an EH region contains something that can throw,
317 indexed by EH region number. */
318 static bitmap eh_region_may_contain_throw_map
;
320 /* The GOTO_QUEUE is is an array of GIMPLE_GOTO and GIMPLE_RETURN
321 statements that are seen to escape this GIMPLE_TRY_FINALLY node.
322 The idea is to record a gimple statement for everything except for
323 the conditionals, which get their labels recorded. Since labels are
324 of type 'tree', we need this node to store both gimple and tree
325 objects. REPL_STMT is the sequence used to replace the goto/return
326 statement. CONT_STMT is used to store the statement that allows
327 the return/goto to jump to the original destination. */
329 struct goto_queue_node
333 gimple_seq repl_stmt
;
336 /* This is used when index >= 0 to indicate that stmt is a label (as
337 opposed to a goto stmt). */
341 /* State of the world while lowering. */
345 /* What's "current" while constructing the eh region tree. These
346 correspond to variables of the same name in cfun->eh, which we
347 don't have easy access to. */
348 eh_region cur_region
;
350 /* What's "current" for the purposes of __builtin_eh_pointer. For
351 a CATCH, this is the associated TRY. For an EH_FILTER, this is
352 the associated ALLOWED_EXCEPTIONS, etc. */
353 eh_region ehp_region
;
355 /* Processing of TRY_FINALLY requires a bit more state. This is
356 split out into a separate structure so that we don't have to
357 copy so much when processing other nodes. */
358 struct leh_tf_state
*tf
;
363 /* Pointer to the GIMPLE_TRY_FINALLY node under discussion. The
364 try_finally_expr is the original GIMPLE_TRY_FINALLY. We need to retain
365 this so that outside_finally_tree can reliably reference the tree used
366 in the collect_finally_tree data structures. */
367 gimple try_finally_expr
;
370 /* While lowering a top_p usually it is expanded into multiple statements,
371 thus we need the following field to store them. */
372 gimple_seq top_p_seq
;
374 /* The state outside this try_finally node. */
375 struct leh_state
*outer
;
377 /* The exception region created for it. */
380 /* The goto queue. */
381 struct goto_queue_node
*goto_queue
;
382 size_t goto_queue_size
;
383 size_t goto_queue_active
;
385 /* Pointer map to help in searching goto_queue when it is large. */
386 hash_map
<gimple
, goto_queue_node
*> *goto_queue_map
;
388 /* The set of unique labels seen as entries in the goto queue. */
389 vec
<tree
> dest_array
;
391 /* A label to be added at the end of the completed transformed
392 sequence. It will be set if may_fallthru was true *at one time*,
393 though subsequent transformations may have cleared that flag. */
396 /* True if it is possible to fall out the bottom of the try block.
397 Cleared if the fallthru is converted to a goto. */
400 /* True if any entry in goto_queue is a GIMPLE_RETURN. */
403 /* True if the finally block can receive an exception edge.
404 Cleared if the exception case is handled by code duplication. */
408 static gimple_seq
lower_eh_must_not_throw (struct leh_state
*, gimple
);
410 /* Search for STMT in the goto queue. Return the replacement,
411 or null if the statement isn't in the queue. */
413 #define LARGE_GOTO_QUEUE 20
415 static void lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*seq
);
418 find_goto_replacement (struct leh_tf_state
*tf
, treemple stmt
)
422 if (tf
->goto_queue_active
< LARGE_GOTO_QUEUE
)
424 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
425 if ( tf
->goto_queue
[i
].stmt
.g
== stmt
.g
)
426 return tf
->goto_queue
[i
].repl_stmt
;
430 /* If we have a large number of entries in the goto_queue, create a
431 pointer map and use that for searching. */
433 if (!tf
->goto_queue_map
)
435 tf
->goto_queue_map
= new hash_map
<gimple
, goto_queue_node
*>;
436 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
438 bool existed
= tf
->goto_queue_map
->put (tf
->goto_queue
[i
].stmt
.g
,
440 gcc_assert (!existed
);
444 goto_queue_node
**slot
= tf
->goto_queue_map
->get (stmt
.g
);
446 return ((*slot
)->repl_stmt
);
451 /* A subroutine of replace_goto_queue_1. Handles the sub-clauses of a
452 lowered GIMPLE_COND. If, by chance, the replacement is a simple goto,
453 then we can just splat it in, otherwise we add the new stmts immediately
454 after the GIMPLE_COND and redirect. */
457 replace_goto_queue_cond_clause (tree
*tp
, struct leh_tf_state
*tf
,
458 gimple_stmt_iterator
*gsi
)
463 location_t loc
= gimple_location (gsi_stmt (*gsi
));
466 new_seq
= find_goto_replacement (tf
, temp
);
470 if (gimple_seq_singleton_p (new_seq
)
471 && gimple_code (gimple_seq_first_stmt (new_seq
)) == GIMPLE_GOTO
)
473 *tp
= gimple_goto_dest (gimple_seq_first_stmt (new_seq
));
477 label
= create_artificial_label (loc
);
478 /* Set the new label for the GIMPLE_COND */
481 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
482 gsi_insert_seq_after (gsi
, gimple_seq_copy (new_seq
), GSI_CONTINUE_LINKING
);
485 /* The real work of replace_goto_queue. Returns with TSI updated to
486 point to the next statement. */
488 static void replace_goto_queue_stmt_list (gimple_seq
*, struct leh_tf_state
*);
491 replace_goto_queue_1 (gimple stmt
, struct leh_tf_state
*tf
,
492 gimple_stmt_iterator
*gsi
)
498 switch (gimple_code (stmt
))
503 seq
= find_goto_replacement (tf
, temp
);
506 gsi_insert_seq_before (gsi
, gimple_seq_copy (seq
), GSI_SAME_STMT
);
507 gsi_remove (gsi
, false);
513 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 2), tf
, gsi
);
514 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 3), tf
, gsi
);
518 replace_goto_queue_stmt_list (gimple_try_eval_ptr (stmt
), tf
);
519 replace_goto_queue_stmt_list (gimple_try_cleanup_ptr (stmt
), tf
);
522 replace_goto_queue_stmt_list (gimple_catch_handler_ptr (stmt
), tf
);
524 case GIMPLE_EH_FILTER
:
525 replace_goto_queue_stmt_list (gimple_eh_filter_failure_ptr (stmt
), tf
);
528 replace_goto_queue_stmt_list (gimple_eh_else_n_body_ptr (stmt
), tf
);
529 replace_goto_queue_stmt_list (gimple_eh_else_e_body_ptr (stmt
), tf
);
533 /* These won't have gotos in them. */
540 /* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
543 replace_goto_queue_stmt_list (gimple_seq
*seq
, struct leh_tf_state
*tf
)
545 gimple_stmt_iterator gsi
= gsi_start (*seq
);
547 while (!gsi_end_p (gsi
))
548 replace_goto_queue_1 (gsi_stmt (gsi
), tf
, &gsi
);
551 /* Replace all goto queue members. */
554 replace_goto_queue (struct leh_tf_state
*tf
)
556 if (tf
->goto_queue_active
== 0)
558 replace_goto_queue_stmt_list (&tf
->top_p_seq
, tf
);
559 replace_goto_queue_stmt_list (&eh_seq
, tf
);
562 /* Add a new record to the goto queue contained in TF. NEW_STMT is the
563 data to be added, IS_LABEL indicates whether NEW_STMT is a label or
567 record_in_goto_queue (struct leh_tf_state
*tf
,
574 struct goto_queue_node
*q
;
576 gcc_assert (!tf
->goto_queue_map
);
578 active
= tf
->goto_queue_active
;
579 size
= tf
->goto_queue_size
;
582 size
= (size
? size
* 2 : 32);
583 tf
->goto_queue_size
= size
;
585 = XRESIZEVEC (struct goto_queue_node
, tf
->goto_queue
, size
);
588 q
= &tf
->goto_queue
[active
];
589 tf
->goto_queue_active
= active
+ 1;
591 memset (q
, 0, sizeof (*q
));
594 q
->location
= location
;
595 q
->is_label
= is_label
;
598 /* Record the LABEL label in the goto queue contained in TF.
602 record_in_goto_queue_label (struct leh_tf_state
*tf
, treemple stmt
, tree label
,
606 treemple temp
, new_stmt
;
611 /* Computed and non-local gotos do not get processed. Given
612 their nature we can neither tell whether we've escaped the
613 finally block nor redirect them if we knew. */
614 if (TREE_CODE (label
) != LABEL_DECL
)
617 /* No need to record gotos that don't leave the try block. */
619 if (!outside_finally_tree (temp
, tf
->try_finally_expr
))
622 if (! tf
->dest_array
.exists ())
624 tf
->dest_array
.create (10);
625 tf
->dest_array
.quick_push (label
);
630 int n
= tf
->dest_array
.length ();
631 for (index
= 0; index
< n
; ++index
)
632 if (tf
->dest_array
[index
] == label
)
635 tf
->dest_array
.safe_push (label
);
638 /* In the case of a GOTO we want to record the destination label,
639 since with a GIMPLE_COND we have an easy access to the then/else
642 record_in_goto_queue (tf
, new_stmt
, index
, true, location
);
645 /* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
646 node, and if so record that fact in the goto queue associated with that
650 maybe_record_in_goto_queue (struct leh_state
*state
, gimple stmt
)
652 struct leh_tf_state
*tf
= state
->tf
;
658 switch (gimple_code (stmt
))
661 new_stmt
.tp
= gimple_op_ptr (stmt
, 2);
662 record_in_goto_queue_label (tf
, new_stmt
, gimple_cond_true_label (stmt
),
663 EXPR_LOCATION (*new_stmt
.tp
));
664 new_stmt
.tp
= gimple_op_ptr (stmt
, 3);
665 record_in_goto_queue_label (tf
, new_stmt
, gimple_cond_false_label (stmt
),
666 EXPR_LOCATION (*new_stmt
.tp
));
670 record_in_goto_queue_label (tf
, new_stmt
, gimple_goto_dest (stmt
),
671 gimple_location (stmt
));
675 tf
->may_return
= true;
677 record_in_goto_queue (tf
, new_stmt
, -1, false, gimple_location (stmt
));
686 #ifdef ENABLE_CHECKING
687 /* We do not process GIMPLE_SWITCHes for now. As long as the original source
688 was in fact structured, and we've not yet done jump threading, then none
689 of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this. */
692 verify_norecord_switch_expr (struct leh_state
*state
, gimple switch_expr
)
694 struct leh_tf_state
*tf
= state
->tf
;
700 n
= gimple_switch_num_labels (switch_expr
);
702 for (i
= 0; i
< n
; ++i
)
705 tree lab
= CASE_LABEL (gimple_switch_label (switch_expr
, i
));
707 gcc_assert (!outside_finally_tree (temp
, tf
->try_finally_expr
));
711 #define verify_norecord_switch_expr(state, switch_expr)
714 /* Redirect a RETURN_EXPR pointed to by Q to FINLAB. If MOD is
715 non-null, insert it before the new branch. */
718 do_return_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
)
722 /* In the case of a return, the queue node must be a gimple statement. */
723 gcc_assert (!q
->is_label
);
725 /* Note that the return value may have already been computed, e.g.,
738 should return 0, not 1. We don't have to do anything to make
739 this happens because the return value has been placed in the
740 RESULT_DECL already. */
742 q
->cont_stmt
= q
->stmt
.g
;
745 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
747 x
= gimple_build_goto (finlab
);
748 gimple_set_location (x
, q
->location
);
749 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
752 /* Similar, but easier, for GIMPLE_GOTO. */
755 do_goto_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
,
756 struct leh_tf_state
*tf
)
760 gcc_assert (q
->is_label
);
762 q
->cont_stmt
= gimple_build_goto (tf
->dest_array
[q
->index
]);
765 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
767 x
= gimple_build_goto (finlab
);
768 gimple_set_location (x
, q
->location
);
769 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
772 /* Emit a standard landing pad sequence into SEQ for REGION. */
775 emit_post_landing_pad (gimple_seq
*seq
, eh_region region
)
777 eh_landing_pad lp
= region
->landing_pads
;
781 lp
= gen_eh_landing_pad (region
);
783 lp
->post_landing_pad
= create_artificial_label (UNKNOWN_LOCATION
);
784 EH_LANDING_PAD_NR (lp
->post_landing_pad
) = lp
->index
;
786 x
= gimple_build_label (lp
->post_landing_pad
);
787 gimple_seq_add_stmt (seq
, x
);
790 /* Emit a RESX statement into SEQ for REGION. */
793 emit_resx (gimple_seq
*seq
, eh_region region
)
795 gimple x
= gimple_build_resx (region
->index
);
796 gimple_seq_add_stmt (seq
, x
);
798 record_stmt_eh_region (region
->outer
, x
);
801 /* Emit an EH_DISPATCH statement into SEQ for REGION. */
804 emit_eh_dispatch (gimple_seq
*seq
, eh_region region
)
806 gimple x
= gimple_build_eh_dispatch (region
->index
);
807 gimple_seq_add_stmt (seq
, x
);
810 /* Note that the current EH region may contain a throw, or a
811 call to a function which itself may contain a throw. */
814 note_eh_region_may_contain_throw (eh_region region
)
816 while (bitmap_set_bit (eh_region_may_contain_throw_map
, region
->index
))
818 if (region
->type
== ERT_MUST_NOT_THROW
)
820 region
= region
->outer
;
826 /* Check if REGION has been marked as containing a throw. If REGION is
827 NULL, this predicate is false. */
830 eh_region_may_contain_throw (eh_region r
)
832 return r
&& bitmap_bit_p (eh_region_may_contain_throw_map
, r
->index
);
835 /* We want to transform
836 try { body; } catch { stuff; }
846 TP is a GIMPLE_TRY node. REGION is the region whose post_landing_pad
847 should be placed before the second operand, or NULL. OVER is
848 an existing label that should be put at the exit, or NULL. */
851 frob_into_branch_around (gimple tp
, eh_region region
, tree over
)
854 gimple_seq cleanup
, result
;
855 location_t loc
= gimple_location (tp
);
857 cleanup
= gimple_try_cleanup (tp
);
858 result
= gimple_try_eval (tp
);
861 emit_post_landing_pad (&eh_seq
, region
);
863 if (gimple_seq_may_fallthru (cleanup
))
866 over
= create_artificial_label (loc
);
867 x
= gimple_build_goto (over
);
868 gimple_set_location (x
, loc
);
869 gimple_seq_add_stmt (&cleanup
, x
);
871 gimple_seq_add_seq (&eh_seq
, cleanup
);
875 x
= gimple_build_label (over
);
876 gimple_seq_add_stmt (&result
, x
);
881 /* A subroutine of lower_try_finally. Duplicate the tree rooted at T.
882 Make sure to record all new labels found. */
885 lower_try_finally_dup_block (gimple_seq seq
, struct leh_state
*outer_state
,
888 gimple region
= NULL
;
890 gimple_stmt_iterator gsi
;
892 new_seq
= copy_gimple_seq_and_replace_locals (seq
);
894 for (gsi
= gsi_start (new_seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
896 gimple stmt
= gsi_stmt (gsi
);
897 if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
899 tree block
= gimple_block (stmt
);
900 gimple_set_location (stmt
, loc
);
901 gimple_set_block (stmt
, block
);
906 region
= outer_state
->tf
->try_finally_expr
;
907 collect_finally_tree_1 (new_seq
, region
);
912 /* A subroutine of lower_try_finally. Create a fallthru label for
913 the given try_finally state. The only tricky bit here is that
914 we have to make sure to record the label in our outer context. */
917 lower_try_finally_fallthru_label (struct leh_tf_state
*tf
)
919 tree label
= tf
->fallthru_label
;
924 label
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
925 tf
->fallthru_label
= label
;
929 record_in_finally_tree (temp
, tf
->outer
->tf
->try_finally_expr
);
935 /* A subroutine of lower_try_finally. If FINALLY consits of a
936 GIMPLE_EH_ELSE node, return it. */
939 get_eh_else (gimple_seq finally
)
941 gimple x
= gimple_seq_first_stmt (finally
);
942 if (gimple_code (x
) == GIMPLE_EH_ELSE
)
944 gcc_assert (gimple_seq_singleton_p (finally
));
950 /* A subroutine of lower_try_finally. If the eh_protect_cleanup_actions
951 langhook returns non-null, then the language requires that the exception
952 path out of a try_finally be treated specially. To wit: the code within
953 the finally block may not itself throw an exception. We have two choices
954 here. First we can duplicate the finally block and wrap it in a
955 must_not_throw region. Second, we can generate code like
960 if (fintmp == eh_edge)
961 protect_cleanup_actions;
964 where "fintmp" is the temporary used in the switch statement generation
965 alternative considered below. For the nonce, we always choose the first
968 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
971 honor_protect_cleanup_actions (struct leh_state
*outer_state
,
972 struct leh_state
*this_state
,
973 struct leh_tf_state
*tf
)
975 tree protect_cleanup_actions
;
976 gimple_stmt_iterator gsi
;
977 bool finally_may_fallthru
;
981 /* First check for nothing to do. */
982 if (lang_hooks
.eh_protect_cleanup_actions
== NULL
)
984 protect_cleanup_actions
= lang_hooks
.eh_protect_cleanup_actions ();
985 if (protect_cleanup_actions
== NULL
)
988 finally
= gimple_try_cleanup (tf
->top_p
);
989 eh_else
= get_eh_else (finally
);
991 /* Duplicate the FINALLY block. Only need to do this for try-finally,
992 and not for cleanups. If we've got an EH_ELSE, extract it now. */
995 finally
= gimple_eh_else_e_body (eh_else
);
996 gimple_try_set_cleanup (tf
->top_p
, gimple_eh_else_n_body (eh_else
));
999 finally
= lower_try_finally_dup_block (finally
, outer_state
,
1000 gimple_location (tf
->try_finally_expr
));
1001 finally_may_fallthru
= gimple_seq_may_fallthru (finally
);
1003 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
1004 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
1005 to be in an enclosing scope, but needs to be implemented at this level
1006 to avoid a nesting violation (see wrap_temporary_cleanups in
1007 cp/decl.c). Since it's logically at an outer level, we should call
1008 terminate before we get to it, so strip it away before adding the
1009 MUST_NOT_THROW filter. */
1010 gsi
= gsi_start (finally
);
1012 if (gimple_code (x
) == GIMPLE_TRY
1013 && gimple_try_kind (x
) == GIMPLE_TRY_CATCH
1014 && gimple_try_catch_is_cleanup (x
))
1016 gsi_insert_seq_before (&gsi
, gimple_try_eval (x
), GSI_SAME_STMT
);
1017 gsi_remove (&gsi
, false);
1020 /* Wrap the block with protect_cleanup_actions as the action. */
1021 x
= gimple_build_eh_must_not_throw (protect_cleanup_actions
);
1022 x
= gimple_build_try (finally
, gimple_seq_alloc_with_stmt (x
),
1024 finally
= lower_eh_must_not_throw (outer_state
, x
);
1026 /* Drop all of this into the exception sequence. */
1027 emit_post_landing_pad (&eh_seq
, tf
->region
);
1028 gimple_seq_add_seq (&eh_seq
, finally
);
1029 if (finally_may_fallthru
)
1030 emit_resx (&eh_seq
, tf
->region
);
1032 /* Having now been handled, EH isn't to be considered with
1033 the rest of the outgoing edges. */
1034 tf
->may_throw
= false;
1037 /* A subroutine of lower_try_finally. We have determined that there is
1038 no fallthru edge out of the finally block. This means that there is
1039 no outgoing edge corresponding to any incoming edge. Restructure the
1040 try_finally node for this special case. */
1043 lower_try_finally_nofallthru (struct leh_state
*state
,
1044 struct leh_tf_state
*tf
)
1049 struct goto_queue_node
*q
, *qe
;
1051 lab
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
1053 /* We expect that tf->top_p is a GIMPLE_TRY. */
1054 finally
= gimple_try_cleanup (tf
->top_p
);
1055 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1057 x
= gimple_build_label (lab
);
1058 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1061 qe
= q
+ tf
->goto_queue_active
;
1064 do_return_redirection (q
, lab
, NULL
);
1066 do_goto_redirection (q
, lab
, NULL
, tf
);
1068 replace_goto_queue (tf
);
1070 /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1071 eh_else
= get_eh_else (finally
);
1074 finally
= gimple_eh_else_n_body (eh_else
);
1075 lower_eh_constructs_1 (state
, &finally
);
1076 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1080 finally
= gimple_eh_else_e_body (eh_else
);
1081 lower_eh_constructs_1 (state
, &finally
);
1083 emit_post_landing_pad (&eh_seq
, tf
->region
);
1084 gimple_seq_add_seq (&eh_seq
, finally
);
1089 lower_eh_constructs_1 (state
, &finally
);
1090 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1094 emit_post_landing_pad (&eh_seq
, tf
->region
);
1096 x
= gimple_build_goto (lab
);
1097 gimple_set_location (x
, gimple_location (tf
->try_finally_expr
));
1098 gimple_seq_add_stmt (&eh_seq
, x
);
1103 /* A subroutine of lower_try_finally. We have determined that there is
1104 exactly one destination of the finally block. Restructure the
1105 try_finally node for this special case. */
1108 lower_try_finally_onedest (struct leh_state
*state
, struct leh_tf_state
*tf
)
1110 struct goto_queue_node
*q
, *qe
;
1113 gimple_stmt_iterator gsi
;
1115 location_t loc
= gimple_location (tf
->try_finally_expr
);
1117 finally
= gimple_try_cleanup (tf
->top_p
);
1118 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1120 /* Since there's only one destination, and the destination edge can only
1121 either be EH or non-EH, that implies that all of our incoming edges
1122 are of the same type. Therefore we can lower EH_ELSE immediately. */
1123 x
= get_eh_else (finally
);
1127 finally
= gimple_eh_else_e_body (x
);
1129 finally
= gimple_eh_else_n_body (x
);
1132 lower_eh_constructs_1 (state
, &finally
);
1134 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1136 gimple stmt
= gsi_stmt (gsi
);
1137 if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
1139 tree block
= gimple_block (stmt
);
1140 gimple_set_location (stmt
, gimple_location (tf
->try_finally_expr
));
1141 gimple_set_block (stmt
, block
);
1147 /* Only reachable via the exception edge. Add the given label to
1148 the head of the FINALLY block. Append a RESX at the end. */
1149 emit_post_landing_pad (&eh_seq
, tf
->region
);
1150 gimple_seq_add_seq (&eh_seq
, finally
);
1151 emit_resx (&eh_seq
, tf
->region
);
1155 if (tf
->may_fallthru
)
1157 /* Only reachable via the fallthru edge. Do nothing but let
1158 the two blocks run together; we'll fall out the bottom. */
1159 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1163 finally_label
= create_artificial_label (loc
);
1164 x
= gimple_build_label (finally_label
);
1165 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1167 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1170 qe
= q
+ tf
->goto_queue_active
;
1174 /* Reachable by return expressions only. Redirect them. */
1176 do_return_redirection (q
, finally_label
, NULL
);
1177 replace_goto_queue (tf
);
1181 /* Reachable by goto expressions only. Redirect them. */
1183 do_goto_redirection (q
, finally_label
, NULL
, tf
);
1184 replace_goto_queue (tf
);
1186 if (tf
->dest_array
[0] == tf
->fallthru_label
)
1188 /* Reachable by goto to fallthru label only. Redirect it
1189 to the new label (already created, sadly), and do not
1190 emit the final branch out, or the fallthru label. */
1191 tf
->fallthru_label
= NULL
;
1196 /* Place the original return/goto to the original destination
1197 immediately after the finally block. */
1198 x
= tf
->goto_queue
[0].cont_stmt
;
1199 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1200 maybe_record_in_goto_queue (state
, x
);
1203 /* A subroutine of lower_try_finally. There are multiple edges incoming
1204 and outgoing from the finally block. Implement this by duplicating the
1205 finally block for every destination. */
1208 lower_try_finally_copy (struct leh_state
*state
, struct leh_tf_state
*tf
)
1211 gimple_seq new_stmt
;
1215 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1217 finally
= gimple_try_cleanup (tf
->top_p
);
1219 /* Notice EH_ELSE, and simplify some of the remaining code
1220 by considering FINALLY to be the normal return path only. */
1221 eh_else
= get_eh_else (finally
);
1223 finally
= gimple_eh_else_n_body (eh_else
);
1225 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1228 if (tf
->may_fallthru
)
1230 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1231 lower_eh_constructs_1 (state
, &seq
);
1232 gimple_seq_add_seq (&new_stmt
, seq
);
1234 tmp
= lower_try_finally_fallthru_label (tf
);
1235 x
= gimple_build_goto (tmp
);
1236 gimple_set_location (x
, tf_loc
);
1237 gimple_seq_add_stmt (&new_stmt
, x
);
1242 /* We don't need to copy the EH path of EH_ELSE,
1243 since it is only emitted once. */
1245 seq
= gimple_eh_else_e_body (eh_else
);
1247 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1248 lower_eh_constructs_1 (state
, &seq
);
1250 emit_post_landing_pad (&eh_seq
, tf
->region
);
1251 gimple_seq_add_seq (&eh_seq
, seq
);
1252 emit_resx (&eh_seq
, tf
->region
);
1257 struct goto_queue_node
*q
, *qe
;
1258 int return_index
, index
;
1261 struct goto_queue_node
*q
;
1265 return_index
= tf
->dest_array
.length ();
1266 labels
= XCNEWVEC (struct labels_s
, return_index
+ 1);
1269 qe
= q
+ tf
->goto_queue_active
;
1272 index
= q
->index
< 0 ? return_index
: q
->index
;
1274 if (!labels
[index
].q
)
1275 labels
[index
].q
= q
;
1278 for (index
= 0; index
< return_index
+ 1; index
++)
1282 q
= labels
[index
].q
;
1286 lab
= labels
[index
].label
1287 = create_artificial_label (tf_loc
);
1289 if (index
== return_index
)
1290 do_return_redirection (q
, lab
, NULL
);
1292 do_goto_redirection (q
, lab
, NULL
, tf
);
1294 x
= gimple_build_label (lab
);
1295 gimple_seq_add_stmt (&new_stmt
, x
);
1297 seq
= lower_try_finally_dup_block (finally
, state
, q
->location
);
1298 lower_eh_constructs_1 (state
, &seq
);
1299 gimple_seq_add_seq (&new_stmt
, seq
);
1301 gimple_seq_add_stmt (&new_stmt
, q
->cont_stmt
);
1302 maybe_record_in_goto_queue (state
, q
->cont_stmt
);
1305 for (q
= tf
->goto_queue
; q
< qe
; q
++)
1309 index
= q
->index
< 0 ? return_index
: q
->index
;
1311 if (labels
[index
].q
== q
)
1314 lab
= labels
[index
].label
;
1316 if (index
== return_index
)
1317 do_return_redirection (q
, lab
, NULL
);
1319 do_goto_redirection (q
, lab
, NULL
, tf
);
1322 replace_goto_queue (tf
);
1326 /* Need to link new stmts after running replace_goto_queue due
1327 to not wanting to process the same goto stmts twice. */
1328 gimple_seq_add_seq (&tf
->top_p_seq
, new_stmt
);
1331 /* A subroutine of lower_try_finally. There are multiple edges incoming
1332 and outgoing from the finally block. Implement this by instrumenting
1333 each incoming edge and creating a switch statement at the end of the
1334 finally block that branches to the appropriate destination. */
1337 lower_try_finally_switch (struct leh_state
*state
, struct leh_tf_state
*tf
)
1339 struct goto_queue_node
*q
, *qe
;
1340 tree finally_tmp
, finally_label
;
1341 int return_index
, eh_index
, fallthru_index
;
1342 int nlabels
, ndests
, j
, last_case_index
;
1344 vec
<tree
> case_label_vec
;
1345 gimple_seq switch_body
= NULL
;
1350 hash_map
<tree
, gimple
> *cont_map
= NULL
;
1351 /* The location of the TRY_FINALLY stmt. */
1352 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1353 /* The location of the finally block. */
1354 location_t finally_loc
;
1356 finally
= gimple_try_cleanup (tf
->top_p
);
1357 eh_else
= get_eh_else (finally
);
1359 /* Mash the TRY block to the head of the chain. */
1360 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1362 /* The location of the finally is either the last stmt in the finally
1363 block or the location of the TRY_FINALLY itself. */
1364 x
= gimple_seq_last_stmt (finally
);
1365 finally_loc
= x
? gimple_location (x
) : tf_loc
;
1367 /* Prepare for switch statement generation. */
1368 nlabels
= tf
->dest_array
.length ();
1369 return_index
= nlabels
;
1370 eh_index
= return_index
+ tf
->may_return
;
1371 fallthru_index
= eh_index
+ (tf
->may_throw
&& !eh_else
);
1372 ndests
= fallthru_index
+ tf
->may_fallthru
;
1374 finally_tmp
= create_tmp_var (integer_type_node
, "finally_tmp");
1375 finally_label
= create_artificial_label (finally_loc
);
1377 /* We use vec::quick_push on case_label_vec throughout this function,
1378 since we know the size in advance and allocate precisely as muce
1380 case_label_vec
.create (ndests
);
1382 last_case_index
= 0;
1384 /* Begin inserting code for getting to the finally block. Things
1385 are done in this order to correspond to the sequence the code is
1388 if (tf
->may_fallthru
)
1390 x
= gimple_build_assign (finally_tmp
,
1391 build_int_cst (integer_type_node
,
1393 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1395 tmp
= build_int_cst (integer_type_node
, fallthru_index
);
1396 last_case
= build_case_label (tmp
, NULL
,
1397 create_artificial_label (tf_loc
));
1398 case_label_vec
.quick_push (last_case
);
1401 x
= gimple_build_label (CASE_LABEL (last_case
));
1402 gimple_seq_add_stmt (&switch_body
, x
);
1404 tmp
= lower_try_finally_fallthru_label (tf
);
1405 x
= gimple_build_goto (tmp
);
1406 gimple_set_location (x
, tf_loc
);
1407 gimple_seq_add_stmt (&switch_body
, x
);
1410 /* For EH_ELSE, emit the exception path (plus resx) now, then
1411 subsequently we only need consider the normal path. */
1416 finally
= gimple_eh_else_e_body (eh_else
);
1417 lower_eh_constructs_1 (state
, &finally
);
1419 emit_post_landing_pad (&eh_seq
, tf
->region
);
1420 gimple_seq_add_seq (&eh_seq
, finally
);
1421 emit_resx (&eh_seq
, tf
->region
);
1424 finally
= gimple_eh_else_n_body (eh_else
);
1426 else if (tf
->may_throw
)
1428 emit_post_landing_pad (&eh_seq
, tf
->region
);
1430 x
= gimple_build_assign (finally_tmp
,
1431 build_int_cst (integer_type_node
, eh_index
));
1432 gimple_seq_add_stmt (&eh_seq
, x
);
1434 x
= gimple_build_goto (finally_label
);
1435 gimple_set_location (x
, tf_loc
);
1436 gimple_seq_add_stmt (&eh_seq
, x
);
1438 tmp
= build_int_cst (integer_type_node
, eh_index
);
1439 last_case
= build_case_label (tmp
, NULL
,
1440 create_artificial_label (tf_loc
));
1441 case_label_vec
.quick_push (last_case
);
1444 x
= gimple_build_label (CASE_LABEL (last_case
));
1445 gimple_seq_add_stmt (&eh_seq
, x
);
1446 emit_resx (&eh_seq
, tf
->region
);
1449 x
= gimple_build_label (finally_label
);
1450 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1452 lower_eh_constructs_1 (state
, &finally
);
1453 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1455 /* Redirect each incoming goto edge. */
1457 qe
= q
+ tf
->goto_queue_active
;
1458 j
= last_case_index
+ tf
->may_return
;
1459 /* Prepare the assignments to finally_tmp that are executed upon the
1460 entrance through a particular edge. */
1463 gimple_seq mod
= NULL
;
1465 unsigned int case_index
;
1469 x
= gimple_build_assign (finally_tmp
,
1470 build_int_cst (integer_type_node
,
1472 gimple_seq_add_stmt (&mod
, x
);
1473 do_return_redirection (q
, finally_label
, mod
);
1474 switch_id
= return_index
;
1478 x
= gimple_build_assign (finally_tmp
,
1479 build_int_cst (integer_type_node
, q
->index
));
1480 gimple_seq_add_stmt (&mod
, x
);
1481 do_goto_redirection (q
, finally_label
, mod
, tf
);
1482 switch_id
= q
->index
;
1485 case_index
= j
+ q
->index
;
1486 if (case_label_vec
.length () <= case_index
|| !case_label_vec
[case_index
])
1489 tmp
= build_int_cst (integer_type_node
, switch_id
);
1490 case_lab
= build_case_label (tmp
, NULL
,
1491 create_artificial_label (tf_loc
));
1492 /* We store the cont_stmt in the pointer map, so that we can recover
1493 it in the loop below. */
1495 cont_map
= new hash_map
<tree
, gimple
>;
1496 cont_map
->put (case_lab
, q
->cont_stmt
);
1497 case_label_vec
.quick_push (case_lab
);
1500 for (j
= last_case_index
; j
< last_case_index
+ nlabels
; j
++)
1504 last_case
= case_label_vec
[j
];
1506 gcc_assert (last_case
);
1507 gcc_assert (cont_map
);
1509 cont_stmt
= *cont_map
->get (last_case
);
1511 x
= gimple_build_label (CASE_LABEL (last_case
));
1512 gimple_seq_add_stmt (&switch_body
, x
);
1513 gimple_seq_add_stmt (&switch_body
, cont_stmt
);
1514 maybe_record_in_goto_queue (state
, cont_stmt
);
1519 replace_goto_queue (tf
);
1521 /* Make sure that the last case is the default label, as one is required.
1522 Then sort the labels, which is also required in GIMPLE. */
1523 CASE_LOW (last_case
) = NULL
;
1524 tree tem
= case_label_vec
.pop ();
1525 gcc_assert (tem
== last_case
);
1526 sort_case_labels (case_label_vec
);
1528 /* Build the switch statement, setting last_case to be the default
1530 switch_stmt
= gimple_build_switch (finally_tmp
, last_case
,
1532 gimple_set_location (switch_stmt
, finally_loc
);
1534 /* Need to link SWITCH_STMT after running replace_goto_queue
1535 due to not wanting to process the same goto stmts twice. */
1536 gimple_seq_add_stmt (&tf
->top_p_seq
, switch_stmt
);
1537 gimple_seq_add_seq (&tf
->top_p_seq
, switch_body
);
1540 /* Decide whether or not we are going to duplicate the finally block.
1541 There are several considerations.
1543 First, if this is Java, then the finally block contains code
1544 written by the user. It has line numbers associated with it,
1545 so duplicating the block means it's difficult to set a breakpoint.
1546 Since controlling code generation via -g is verboten, we simply
1547 never duplicate code without optimization.
1549 Second, we'd like to prevent egregious code growth. One way to
1550 do this is to estimate the size of the finally block, multiply
1551 that by the number of copies we'd need to make, and compare against
1552 the estimate of the size of the switch machinery we'd have to add. */
1555 decide_copy_try_finally (int ndests
, bool may_throw
, gimple_seq finally
)
1557 int f_estimate
, sw_estimate
;
1560 /* If there's an EH_ELSE involved, the exception path is separate
1561 and really doesn't come into play for this computation. */
1562 eh_else
= get_eh_else (finally
);
1565 ndests
-= may_throw
;
1566 finally
= gimple_eh_else_n_body (eh_else
);
1571 gimple_stmt_iterator gsi
;
1576 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1578 gimple stmt
= gsi_stmt (gsi
);
1579 if (!is_gimple_debug (stmt
) && !gimple_clobber_p (stmt
))
1585 /* Finally estimate N times, plus N gotos. */
1586 f_estimate
= count_insns_seq (finally
, &eni_size_weights
);
1587 f_estimate
= (f_estimate
+ 1) * ndests
;
1589 /* Switch statement (cost 10), N variable assignments, N gotos. */
1590 sw_estimate
= 10 + 2 * ndests
;
1592 /* Optimize for size clearly wants our best guess. */
1593 if (optimize_function_for_size_p (cfun
))
1594 return f_estimate
< sw_estimate
;
1596 /* ??? These numbers are completely made up so far. */
1598 return f_estimate
< 100 || f_estimate
< sw_estimate
* 2;
1600 return f_estimate
< 40 || f_estimate
* 2 < sw_estimate
* 3;
1603 /* REG is the enclosing region for a possible cleanup region, or the region
1604 itself. Returns TRUE if such a region would be unreachable.
1606 Cleanup regions within a must-not-throw region aren't actually reachable
1607 even if there are throwing stmts within them, because the personality
1608 routine will call terminate before unwinding. */
1611 cleanup_is_dead_in (eh_region reg
)
1613 while (reg
&& reg
->type
== ERT_CLEANUP
)
1615 return (reg
&& reg
->type
== ERT_MUST_NOT_THROW
);
1618 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
1619 to a sequence of labels and blocks, plus the exception region trees
1620 that record all the magic. This is complicated by the need to
1621 arrange for the FINALLY block to be executed on all exits. */
1624 lower_try_finally (struct leh_state
*state
, gimple tp
)
1626 struct leh_tf_state this_tf
;
1627 struct leh_state this_state
;
1629 gimple_seq old_eh_seq
;
1631 /* Process the try block. */
1633 memset (&this_tf
, 0, sizeof (this_tf
));
1634 this_tf
.try_finally_expr
= tp
;
1636 this_tf
.outer
= state
;
1637 if (using_eh_for_cleanups_p () && !cleanup_is_dead_in (state
->cur_region
))
1639 this_tf
.region
= gen_eh_region_cleanup (state
->cur_region
);
1640 this_state
.cur_region
= this_tf
.region
;
1644 this_tf
.region
= NULL
;
1645 this_state
.cur_region
= state
->cur_region
;
1648 this_state
.ehp_region
= state
->ehp_region
;
1649 this_state
.tf
= &this_tf
;
1651 old_eh_seq
= eh_seq
;
1654 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1656 /* Determine if the try block is escaped through the bottom. */
1657 this_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1659 /* Determine if any exceptions are possible within the try block. */
1661 this_tf
.may_throw
= eh_region_may_contain_throw (this_tf
.region
);
1662 if (this_tf
.may_throw
)
1663 honor_protect_cleanup_actions (state
, &this_state
, &this_tf
);
1665 /* Determine how many edges (still) reach the finally block. Or rather,
1666 how many destinations are reached by the finally block. Use this to
1667 determine how we process the finally block itself. */
1669 ndests
= this_tf
.dest_array
.length ();
1670 ndests
+= this_tf
.may_fallthru
;
1671 ndests
+= this_tf
.may_return
;
1672 ndests
+= this_tf
.may_throw
;
1674 /* If the FINALLY block is not reachable, dike it out. */
1677 gimple_seq_add_seq (&this_tf
.top_p_seq
, gimple_try_eval (tp
));
1678 gimple_try_set_cleanup (tp
, NULL
);
1680 /* If the finally block doesn't fall through, then any destination
1681 we might try to impose there isn't reached either. There may be
1682 some minor amount of cleanup and redirection still needed. */
1683 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp
)))
1684 lower_try_finally_nofallthru (state
, &this_tf
);
1686 /* We can easily special-case redirection to a single destination. */
1687 else if (ndests
== 1)
1688 lower_try_finally_onedest (state
, &this_tf
);
1689 else if (decide_copy_try_finally (ndests
, this_tf
.may_throw
,
1690 gimple_try_cleanup (tp
)))
1691 lower_try_finally_copy (state
, &this_tf
);
1693 lower_try_finally_switch (state
, &this_tf
);
1695 /* If someone requested we add a label at the end of the transformed
1697 if (this_tf
.fallthru_label
)
1699 /* This must be reached only if ndests == 0. */
1700 gimple x
= gimple_build_label (this_tf
.fallthru_label
);
1701 gimple_seq_add_stmt (&this_tf
.top_p_seq
, x
);
1704 this_tf
.dest_array
.release ();
1705 free (this_tf
.goto_queue
);
1706 if (this_tf
.goto_queue_map
)
1707 delete this_tf
.goto_queue_map
;
1709 /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1710 If there was no old eh_seq, then the append is trivially already done. */
1714 eh_seq
= old_eh_seq
;
1717 gimple_seq new_eh_seq
= eh_seq
;
1718 eh_seq
= old_eh_seq
;
1719 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1723 return this_tf
.top_p_seq
;
1726 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1727 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1728 exception region trees that records all the magic. */
1731 lower_catch (struct leh_state
*state
, gimple tp
)
1733 eh_region try_region
= NULL
;
1734 struct leh_state this_state
= *state
;
1735 gimple_stmt_iterator gsi
;
1737 gimple_seq new_seq
, cleanup
;
1739 location_t try_catch_loc
= gimple_location (tp
);
1741 if (flag_exceptions
)
1743 try_region
= gen_eh_region_try (state
->cur_region
);
1744 this_state
.cur_region
= try_region
;
1747 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1749 if (!eh_region_may_contain_throw (try_region
))
1750 return gimple_try_eval (tp
);
1753 emit_eh_dispatch (&new_seq
, try_region
);
1754 emit_resx (&new_seq
, try_region
);
1756 this_state
.cur_region
= state
->cur_region
;
1757 this_state
.ehp_region
= try_region
;
1760 cleanup
= gimple_try_cleanup (tp
);
1761 for (gsi
= gsi_start (cleanup
);
1769 gcatch
= gsi_stmt (gsi
);
1770 c
= gen_eh_region_catch (try_region
, gimple_catch_types (gcatch
));
1772 handler
= gimple_catch_handler (gcatch
);
1773 lower_eh_constructs_1 (&this_state
, &handler
);
1775 c
->label
= create_artificial_label (UNKNOWN_LOCATION
);
1776 x
= gimple_build_label (c
->label
);
1777 gimple_seq_add_stmt (&new_seq
, x
);
1779 gimple_seq_add_seq (&new_seq
, handler
);
1781 if (gimple_seq_may_fallthru (new_seq
))
1784 out_label
= create_artificial_label (try_catch_loc
);
1786 x
= gimple_build_goto (out_label
);
1787 gimple_seq_add_stmt (&new_seq
, x
);
1793 gimple_try_set_cleanup (tp
, new_seq
);
1795 return frob_into_branch_around (tp
, try_region
, out_label
);
1798 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1799 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1800 region trees that record all the magic. */
1803 lower_eh_filter (struct leh_state
*state
, gimple tp
)
1805 struct leh_state this_state
= *state
;
1806 eh_region this_region
= NULL
;
1810 inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1812 if (flag_exceptions
)
1814 this_region
= gen_eh_region_allowed (state
->cur_region
,
1815 gimple_eh_filter_types (inner
));
1816 this_state
.cur_region
= this_region
;
1819 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1821 if (!eh_region_may_contain_throw (this_region
))
1822 return gimple_try_eval (tp
);
1825 this_state
.cur_region
= state
->cur_region
;
1826 this_state
.ehp_region
= this_region
;
1828 emit_eh_dispatch (&new_seq
, this_region
);
1829 emit_resx (&new_seq
, this_region
);
1831 this_region
->u
.allowed
.label
= create_artificial_label (UNKNOWN_LOCATION
);
1832 x
= gimple_build_label (this_region
->u
.allowed
.label
);
1833 gimple_seq_add_stmt (&new_seq
, x
);
1835 lower_eh_constructs_1 (&this_state
, gimple_eh_filter_failure_ptr (inner
));
1836 gimple_seq_add_seq (&new_seq
, gimple_eh_filter_failure (inner
));
1838 gimple_try_set_cleanup (tp
, new_seq
);
1840 return frob_into_branch_around (tp
, this_region
, NULL
);
1843 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with
1844 an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1845 plus the exception region trees that record all the magic. */
1848 lower_eh_must_not_throw (struct leh_state
*state
, gimple tp
)
1850 struct leh_state this_state
= *state
;
1852 if (flag_exceptions
)
1854 gimple inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1855 eh_region this_region
;
1857 this_region
= gen_eh_region_must_not_throw (state
->cur_region
);
1858 this_region
->u
.must_not_throw
.failure_decl
1859 = gimple_eh_must_not_throw_fndecl (inner
);
1860 this_region
->u
.must_not_throw
.failure_loc
1861 = LOCATION_LOCUS (gimple_location (tp
));
1863 /* In order to get mangling applied to this decl, we must mark it
1864 used now. Otherwise, pass_ipa_free_lang_data won't think it
1866 TREE_USED (this_region
->u
.must_not_throw
.failure_decl
) = 1;
1868 this_state
.cur_region
= this_region
;
1871 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1873 return gimple_try_eval (tp
);
1876 /* Implement a cleanup expression. This is similar to try-finally,
1877 except that we only execute the cleanup block for exception edges. */
1880 lower_cleanup (struct leh_state
*state
, gimple tp
)
1882 struct leh_state this_state
= *state
;
1883 eh_region this_region
= NULL
;
1884 struct leh_tf_state fake_tf
;
1886 bool cleanup_dead
= cleanup_is_dead_in (state
->cur_region
);
1888 if (flag_exceptions
&& !cleanup_dead
)
1890 this_region
= gen_eh_region_cleanup (state
->cur_region
);
1891 this_state
.cur_region
= this_region
;
1894 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1896 if (cleanup_dead
|| !eh_region_may_contain_throw (this_region
))
1897 return gimple_try_eval (tp
);
1899 /* Build enough of a try-finally state so that we can reuse
1900 honor_protect_cleanup_actions. */
1901 memset (&fake_tf
, 0, sizeof (fake_tf
));
1902 fake_tf
.top_p
= fake_tf
.try_finally_expr
= tp
;
1903 fake_tf
.outer
= state
;
1904 fake_tf
.region
= this_region
;
1905 fake_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1906 fake_tf
.may_throw
= true;
1908 honor_protect_cleanup_actions (state
, NULL
, &fake_tf
);
1910 if (fake_tf
.may_throw
)
1912 /* In this case honor_protect_cleanup_actions had nothing to do,
1913 and we should process this normally. */
1914 lower_eh_constructs_1 (state
, gimple_try_cleanup_ptr (tp
));
1915 result
= frob_into_branch_around (tp
, this_region
,
1916 fake_tf
.fallthru_label
);
1920 /* In this case honor_protect_cleanup_actions did nearly all of
1921 the work. All we have left is to append the fallthru_label. */
1923 result
= gimple_try_eval (tp
);
1924 if (fake_tf
.fallthru_label
)
1926 gimple x
= gimple_build_label (fake_tf
.fallthru_label
);
1927 gimple_seq_add_stmt (&result
, x
);
1933 /* Main loop for lowering eh constructs. Also moves gsi to the next
1937 lower_eh_constructs_2 (struct leh_state
*state
, gimple_stmt_iterator
*gsi
)
1941 gimple stmt
= gsi_stmt (*gsi
);
1943 switch (gimple_code (stmt
))
1947 tree fndecl
= gimple_call_fndecl (stmt
);
1950 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
1951 switch (DECL_FUNCTION_CODE (fndecl
))
1953 case BUILT_IN_EH_POINTER
:
1954 /* The front end may have generated a call to
1955 __builtin_eh_pointer (0) within a catch region. Replace
1956 this zero argument with the current catch region number. */
1957 if (state
->ehp_region
)
1959 tree nr
= build_int_cst (integer_type_node
,
1960 state
->ehp_region
->index
);
1961 gimple_call_set_arg (stmt
, 0, nr
);
1965 /* The user has dome something silly. Remove it. */
1966 rhs
= null_pointer_node
;
1971 case BUILT_IN_EH_FILTER
:
1972 /* ??? This should never appear, but since it's a builtin it
1973 is accessible to abuse by users. Just remove it and
1974 replace the use with the arbitrary value zero. */
1975 rhs
= build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
1977 lhs
= gimple_call_lhs (stmt
);
1978 x
= gimple_build_assign (lhs
, rhs
);
1979 gsi_insert_before (gsi
, x
, GSI_SAME_STMT
);
1982 case BUILT_IN_EH_COPY_VALUES
:
1983 /* Likewise this should not appear. Remove it. */
1984 gsi_remove (gsi
, true);
1994 /* If the stmt can throw use a new temporary for the assignment
1995 to a LHS. This makes sure the old value of the LHS is
1996 available on the EH edge. Only do so for statements that
1997 potentially fall through (no noreturn calls e.g.), otherwise
1998 this new assignment might create fake fallthru regions. */
1999 if (stmt_could_throw_p (stmt
)
2000 && gimple_has_lhs (stmt
)
2001 && gimple_stmt_may_fallthru (stmt
)
2002 && !tree_could_throw_p (gimple_get_lhs (stmt
))
2003 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt
))))
2005 tree lhs
= gimple_get_lhs (stmt
);
2006 tree tmp
= create_tmp_var (TREE_TYPE (lhs
), NULL
);
2007 gimple s
= gimple_build_assign (lhs
, tmp
);
2008 gimple_set_location (s
, gimple_location (stmt
));
2009 gimple_set_block (s
, gimple_block (stmt
));
2010 gimple_set_lhs (stmt
, tmp
);
2011 if (TREE_CODE (TREE_TYPE (tmp
)) == COMPLEX_TYPE
2012 || TREE_CODE (TREE_TYPE (tmp
)) == VECTOR_TYPE
)
2013 DECL_GIMPLE_REG_P (tmp
) = 1;
2014 gsi_insert_after (gsi
, s
, GSI_SAME_STMT
);
2016 /* Look for things that can throw exceptions, and record them. */
2017 if (state
->cur_region
&& stmt_could_throw_p (stmt
))
2019 record_stmt_eh_region (state
->cur_region
, stmt
);
2020 note_eh_region_may_contain_throw (state
->cur_region
);
2027 maybe_record_in_goto_queue (state
, stmt
);
2031 verify_norecord_switch_expr (state
, stmt
);
2035 if (gimple_try_kind (stmt
) == GIMPLE_TRY_FINALLY
)
2036 replace
= lower_try_finally (state
, stmt
);
2039 x
= gimple_seq_first_stmt (gimple_try_cleanup (stmt
));
2042 replace
= gimple_try_eval (stmt
);
2043 lower_eh_constructs_1 (state
, &replace
);
2046 switch (gimple_code (x
))
2049 replace
= lower_catch (state
, stmt
);
2051 case GIMPLE_EH_FILTER
:
2052 replace
= lower_eh_filter (state
, stmt
);
2054 case GIMPLE_EH_MUST_NOT_THROW
:
2055 replace
= lower_eh_must_not_throw (state
, stmt
);
2057 case GIMPLE_EH_ELSE
:
2058 /* This code is only valid with GIMPLE_TRY_FINALLY. */
2061 replace
= lower_cleanup (state
, stmt
);
2066 /* Remove the old stmt and insert the transformed sequence
2068 gsi_insert_seq_before (gsi
, replace
, GSI_SAME_STMT
);
2069 gsi_remove (gsi
, true);
2071 /* Return since we don't want gsi_next () */
2074 case GIMPLE_EH_ELSE
:
2075 /* We should be eliminating this in lower_try_finally et al. */
2079 /* A type, a decl, or some kind of statement that we're not
2080 interested in. Don't walk them. */
2087 /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2090 lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*pseq
)
2092 gimple_stmt_iterator gsi
;
2093 for (gsi
= gsi_start (*pseq
); !gsi_end_p (gsi
);)
2094 lower_eh_constructs_2 (state
, &gsi
);
2099 const pass_data pass_data_lower_eh
=
2101 GIMPLE_PASS
, /* type */
2103 OPTGROUP_NONE
, /* optinfo_flags */
2104 TV_TREE_EH
, /* tv_id */
2105 PROP_gimple_lcf
, /* properties_required */
2106 PROP_gimple_leh
, /* properties_provided */
2107 0, /* properties_destroyed */
2108 0, /* todo_flags_start */
2109 0, /* todo_flags_finish */
2112 class pass_lower_eh
: public gimple_opt_pass
2115 pass_lower_eh (gcc::context
*ctxt
)
2116 : gimple_opt_pass (pass_data_lower_eh
, ctxt
)
2119 /* opt_pass methods: */
2120 virtual unsigned int execute (function
*);
2122 }; // class pass_lower_eh
2125 pass_lower_eh::execute (function
*fun
)
2127 struct leh_state null_state
;
2130 bodyp
= gimple_body (current_function_decl
);
2134 finally_tree
= new hash_table
<finally_tree_hasher
> (31);
2135 eh_region_may_contain_throw_map
= BITMAP_ALLOC (NULL
);
2136 memset (&null_state
, 0, sizeof (null_state
));
2138 collect_finally_tree_1 (bodyp
, NULL
);
2139 lower_eh_constructs_1 (&null_state
, &bodyp
);
2140 gimple_set_body (current_function_decl
, bodyp
);
2142 /* We assume there's a return statement, or something, at the end of
2143 the function, and thus ploping the EH sequence afterward won't
2145 gcc_assert (!gimple_seq_may_fallthru (bodyp
));
2146 gimple_seq_add_seq (&bodyp
, eh_seq
);
2148 /* We assume that since BODYP already existed, adding EH_SEQ to it
2149 didn't change its value, and we don't have to re-set the function. */
2150 gcc_assert (bodyp
== gimple_body (current_function_decl
));
2152 delete finally_tree
;
2153 finally_tree
= NULL
;
2154 BITMAP_FREE (eh_region_may_contain_throw_map
);
2157 /* If this function needs a language specific EH personality routine
2158 and the frontend didn't already set one do so now. */
2159 if (function_needs_eh_personality (fun
) == eh_personality_lang
2160 && !DECL_FUNCTION_PERSONALITY (current_function_decl
))
2161 DECL_FUNCTION_PERSONALITY (current_function_decl
)
2162 = lang_hooks
.eh_personality ();
2170 make_pass_lower_eh (gcc::context
*ctxt
)
2172 return new pass_lower_eh (ctxt
);
2175 /* Create the multiple edges from an EH_DISPATCH statement to all of
2176 the possible handlers for its EH region. Return true if there's
2177 no fallthru edge; false if there is. */
2180 make_eh_dispatch_edges (gimple stmt
)
2184 basic_block src
, dst
;
2186 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2187 src
= gimple_bb (stmt
);
2192 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2194 dst
= label_to_block (c
->label
);
2195 make_edge (src
, dst
, 0);
2197 /* A catch-all handler doesn't have a fallthru. */
2198 if (c
->type_list
== NULL
)
2203 case ERT_ALLOWED_EXCEPTIONS
:
2204 dst
= label_to_block (r
->u
.allowed
.label
);
2205 make_edge (src
, dst
, 0);
2215 /* Create the single EH edge from STMT to its nearest landing pad,
2216 if there is such a landing pad within the current function. */
2219 make_eh_edges (gimple stmt
)
2221 basic_block src
, dst
;
2225 lp_nr
= lookup_stmt_eh_lp (stmt
);
2229 lp
= get_eh_landing_pad_from_number (lp_nr
);
2230 gcc_assert (lp
!= NULL
);
2232 src
= gimple_bb (stmt
);
2233 dst
= label_to_block (lp
->post_landing_pad
);
2234 make_edge (src
, dst
, EDGE_EH
);
2237 /* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2238 do not actually perform the final edge redirection.
2240 CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2241 we intend to change the destination EH region as well; this means
2242 EH_LANDING_PAD_NR must already be set on the destination block label.
2243 If false, we're being called from generic cfg manipulation code and we
2244 should preserve our place within the region tree. */
2247 redirect_eh_edge_1 (edge edge_in
, basic_block new_bb
, bool change_region
)
2249 eh_landing_pad old_lp
, new_lp
;
2252 int old_lp_nr
, new_lp_nr
;
2253 tree old_label
, new_label
;
2257 old_bb
= edge_in
->dest
;
2258 old_label
= gimple_block_label (old_bb
);
2259 old_lp_nr
= EH_LANDING_PAD_NR (old_label
);
2260 gcc_assert (old_lp_nr
> 0);
2261 old_lp
= get_eh_landing_pad_from_number (old_lp_nr
);
2263 throw_stmt
= last_stmt (edge_in
->src
);
2264 gcc_assert (lookup_stmt_eh_lp (throw_stmt
) == old_lp_nr
);
2266 new_label
= gimple_block_label (new_bb
);
2268 /* Look for an existing region that might be using NEW_BB already. */
2269 new_lp_nr
= EH_LANDING_PAD_NR (new_label
);
2272 new_lp
= get_eh_landing_pad_from_number (new_lp_nr
);
2273 gcc_assert (new_lp
);
2275 /* Unless CHANGE_REGION is true, the new and old landing pad
2276 had better be associated with the same EH region. */
2277 gcc_assert (change_region
|| new_lp
->region
== old_lp
->region
);
2282 gcc_assert (!change_region
);
2285 /* Notice when we redirect the last EH edge away from OLD_BB. */
2286 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
2287 if (e
!= edge_in
&& (e
->flags
& EDGE_EH
))
2292 /* NEW_LP already exists. If there are still edges into OLD_LP,
2293 there's nothing to do with the EH tree. If there are no more
2294 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2295 If CHANGE_REGION is true, then our caller is expecting to remove
2297 if (e
== NULL
&& !change_region
)
2298 remove_eh_landing_pad (old_lp
);
2302 /* No correct landing pad exists. If there are no more edges
2303 into OLD_LP, then we can simply re-use the existing landing pad.
2304 Otherwise, we have to create a new landing pad. */
2307 EH_LANDING_PAD_NR (old_lp
->post_landing_pad
) = 0;
2311 new_lp
= gen_eh_landing_pad (old_lp
->region
);
2312 new_lp
->post_landing_pad
= new_label
;
2313 EH_LANDING_PAD_NR (new_label
) = new_lp
->index
;
2316 /* Maybe move the throwing statement to the new region. */
2317 if (old_lp
!= new_lp
)
2319 remove_stmt_from_eh_lp (throw_stmt
);
2320 add_stmt_to_eh_lp (throw_stmt
, new_lp
->index
);
2324 /* Redirect EH edge E to NEW_BB. */
2327 redirect_eh_edge (edge edge_in
, basic_block new_bb
)
2329 redirect_eh_edge_1 (edge_in
, new_bb
, false);
2330 return ssa_redirect_edge (edge_in
, new_bb
);
2333 /* This is a subroutine of gimple_redirect_edge_and_branch. Update the
2334 labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2335 The actual edge update will happen in the caller. */
2338 redirect_eh_dispatch_edge (gimple stmt
, edge e
, basic_block new_bb
)
2340 tree new_lab
= gimple_block_label (new_bb
);
2341 bool any_changed
= false;
2346 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2350 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2352 old_bb
= label_to_block (c
->label
);
2353 if (old_bb
== e
->dest
)
2361 case ERT_ALLOWED_EXCEPTIONS
:
2362 old_bb
= label_to_block (r
->u
.allowed
.label
);
2363 gcc_assert (old_bb
== e
->dest
);
2364 r
->u
.allowed
.label
= new_lab
;
2372 gcc_assert (any_changed
);
2375 /* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2378 operation_could_trap_helper_p (enum tree_code op
,
2389 case TRUNC_DIV_EXPR
:
2391 case FLOOR_DIV_EXPR
:
2392 case ROUND_DIV_EXPR
:
2393 case EXACT_DIV_EXPR
:
2395 case FLOOR_MOD_EXPR
:
2396 case ROUND_MOD_EXPR
:
2397 case TRUNC_MOD_EXPR
:
2399 if (honor_snans
|| honor_trapv
)
2402 return flag_trapping_math
;
2403 if (!TREE_CONSTANT (divisor
) || integer_zerop (divisor
))
2412 /* Some floating point comparisons may trap. */
2417 case UNORDERED_EXPR
:
2427 case FIX_TRUNC_EXPR
:
2428 /* Conversion of floating point might trap. */
2434 /* These operations don't trap with floating point. */
2442 /* Any floating arithmetic may trap. */
2443 if (fp_operation
&& flag_trapping_math
)
2451 /* Constructing an object cannot trap. */
2455 /* Any floating arithmetic may trap. */
2456 if (fp_operation
&& flag_trapping_math
)
2464 /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2465 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2466 type operands that may trap. If OP is a division operator, DIVISOR contains
2467 the value of the divisor. */
2470 operation_could_trap_p (enum tree_code op
, bool fp_operation
, bool honor_trapv
,
2473 bool honor_nans
= (fp_operation
&& flag_trapping_math
2474 && !flag_finite_math_only
);
2475 bool honor_snans
= fp_operation
&& flag_signaling_nans
!= 0;
2478 if (TREE_CODE_CLASS (op
) != tcc_comparison
2479 && TREE_CODE_CLASS (op
) != tcc_unary
2480 && TREE_CODE_CLASS (op
) != tcc_binary
)
2483 return operation_could_trap_helper_p (op
, fp_operation
, honor_trapv
,
2484 honor_nans
, honor_snans
, divisor
,
2489 /* Returns true if it is possible to prove that the index of
2490 an array access REF (an ARRAY_REF expression) falls into the
2494 in_array_bounds_p (tree ref
)
2496 tree idx
= TREE_OPERAND (ref
, 1);
2499 if (TREE_CODE (idx
) != INTEGER_CST
)
2502 min
= array_ref_low_bound (ref
);
2503 max
= array_ref_up_bound (ref
);
2506 || TREE_CODE (min
) != INTEGER_CST
2507 || TREE_CODE (max
) != INTEGER_CST
)
2510 if (tree_int_cst_lt (idx
, min
)
2511 || tree_int_cst_lt (max
, idx
))
2517 /* Returns true if it is possible to prove that the range of
2518 an array access REF (an ARRAY_RANGE_REF expression) falls
2519 into the array bounds. */
2522 range_in_array_bounds_p (tree ref
)
2524 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ref
));
2525 tree range_min
, range_max
, min
, max
;
2527 range_min
= TYPE_MIN_VALUE (domain_type
);
2528 range_max
= TYPE_MAX_VALUE (domain_type
);
2531 || TREE_CODE (range_min
) != INTEGER_CST
2532 || TREE_CODE (range_max
) != INTEGER_CST
)
2535 min
= array_ref_low_bound (ref
);
2536 max
= array_ref_up_bound (ref
);
2539 || TREE_CODE (min
) != INTEGER_CST
2540 || TREE_CODE (max
) != INTEGER_CST
)
2543 if (tree_int_cst_lt (range_min
, min
)
2544 || tree_int_cst_lt (max
, range_max
))
2550 /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2551 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2552 This routine expects only GIMPLE lhs or rhs input. */
2555 tree_could_trap_p (tree expr
)
2557 enum tree_code code
;
2558 bool fp_operation
= false;
2559 bool honor_trapv
= false;
2560 tree t
, base
, div
= NULL_TREE
;
2565 code
= TREE_CODE (expr
);
2566 t
= TREE_TYPE (expr
);
2570 if (COMPARISON_CLASS_P (expr
))
2571 fp_operation
= FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2573 fp_operation
= FLOAT_TYPE_P (t
);
2574 honor_trapv
= INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
);
2577 if (TREE_CODE_CLASS (code
) == tcc_binary
)
2578 div
= TREE_OPERAND (expr
, 1);
2579 if (operation_could_trap_p (code
, fp_operation
, honor_trapv
, div
))
2589 case VIEW_CONVERT_EXPR
:
2590 case WITH_SIZE_EXPR
:
2591 expr
= TREE_OPERAND (expr
, 0);
2592 code
= TREE_CODE (expr
);
2595 case ARRAY_RANGE_REF
:
2596 base
= TREE_OPERAND (expr
, 0);
2597 if (tree_could_trap_p (base
))
2599 if (TREE_THIS_NOTRAP (expr
))
2601 return !range_in_array_bounds_p (expr
);
2604 base
= TREE_OPERAND (expr
, 0);
2605 if (tree_could_trap_p (base
))
2607 if (TREE_THIS_NOTRAP (expr
))
2609 return !in_array_bounds_p (expr
);
2611 case TARGET_MEM_REF
:
2613 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
2614 && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0)))
2616 if (TREE_THIS_NOTRAP (expr
))
2618 /* We cannot prove that the access is in-bounds when we have
2619 variable-index TARGET_MEM_REFs. */
2620 if (code
== TARGET_MEM_REF
2621 && (TMR_INDEX (expr
) || TMR_INDEX2 (expr
)))
2623 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
)
2625 tree base
= TREE_OPERAND (TREE_OPERAND (expr
, 0), 0);
2626 offset_int off
= mem_ref_offset (expr
);
2627 if (wi::neg_p (off
, SIGNED
))
2629 if (TREE_CODE (base
) == STRING_CST
)
2630 return wi::leu_p (TREE_STRING_LENGTH (base
), off
);
2631 else if (DECL_SIZE_UNIT (base
) == NULL_TREE
2632 || TREE_CODE (DECL_SIZE_UNIT (base
)) != INTEGER_CST
2633 || wi::leu_p (wi::to_offset (DECL_SIZE_UNIT (base
)), off
))
2635 /* Now we are sure the first byte of the access is inside
2642 return !TREE_THIS_NOTRAP (expr
);
2645 return TREE_THIS_VOLATILE (expr
);
2648 t
= get_callee_fndecl (expr
);
2649 /* Assume that calls to weak functions may trap. */
2650 if (!t
|| !DECL_P (t
))
2653 return tree_could_trap_p (t
);
2657 /* Assume that accesses to weak functions may trap, unless we know
2658 they are certainly defined in current TU or in some other
2660 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
))
2662 struct cgraph_node
*node
;
2663 if (!DECL_EXTERNAL (expr
))
2665 node
= cgraph_node::get (expr
)->function_symbol ();
2666 if (node
&& node
->in_other_partition
)
2673 /* Assume that accesses to weak vars may trap, unless we know
2674 they are certainly defined in current TU or in some other
2676 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
))
2679 if (!DECL_EXTERNAL (expr
))
2681 node
= varpool_node::get (expr
)->ultimate_alias_target ();
2682 if (node
&& node
->in_other_partition
)
2694 /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2695 an assignment or a conditional) may throw. */
2698 stmt_could_throw_1_p (gimple stmt
)
2700 enum tree_code code
= gimple_expr_code (stmt
);
2701 bool honor_nans
= false;
2702 bool honor_snans
= false;
2703 bool fp_operation
= false;
2704 bool honor_trapv
= false;
2709 if (TREE_CODE_CLASS (code
) == tcc_comparison
2710 || TREE_CODE_CLASS (code
) == tcc_unary
2711 || TREE_CODE_CLASS (code
) == tcc_binary
)
2713 if (is_gimple_assign (stmt
)
2714 && TREE_CODE_CLASS (code
) == tcc_comparison
)
2715 t
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
2716 else if (gimple_code (stmt
) == GIMPLE_COND
)
2717 t
= TREE_TYPE (gimple_cond_lhs (stmt
));
2719 t
= gimple_expr_type (stmt
);
2720 fp_operation
= FLOAT_TYPE_P (t
);
2723 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
2724 honor_snans
= flag_signaling_nans
!= 0;
2726 else if (INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
))
2730 /* Check if the main expression may trap. */
2731 t
= is_gimple_assign (stmt
) ? gimple_assign_rhs2 (stmt
) : NULL
;
2732 ret
= operation_could_trap_helper_p (code
, fp_operation
, honor_trapv
,
2733 honor_nans
, honor_snans
, t
,
2738 /* If the expression does not trap, see if any of the individual operands may
2740 for (i
= 0; i
< gimple_num_ops (stmt
); i
++)
2741 if (tree_could_trap_p (gimple_op (stmt
, i
)))
2748 /* Return true if statement STMT could throw an exception. */
2751 stmt_could_throw_p (gimple stmt
)
2753 if (!flag_exceptions
)
2756 /* The only statements that can throw an exception are assignments,
2757 conditionals, calls, resx, and asms. */
2758 switch (gimple_code (stmt
))
2764 return !gimple_call_nothrow_p (stmt
);
2768 if (!cfun
->can_throw_non_call_exceptions
)
2770 return stmt_could_throw_1_p (stmt
);
2773 if (!cfun
->can_throw_non_call_exceptions
)
2775 return gimple_asm_volatile_p (stmt
);
2783 /* Return true if expression T could throw an exception. */
2786 tree_could_throw_p (tree t
)
2788 if (!flag_exceptions
)
2790 if (TREE_CODE (t
) == MODIFY_EXPR
)
2792 if (cfun
->can_throw_non_call_exceptions
2793 && tree_could_trap_p (TREE_OPERAND (t
, 0)))
2795 t
= TREE_OPERAND (t
, 1);
2798 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
2799 t
= TREE_OPERAND (t
, 0);
2800 if (TREE_CODE (t
) == CALL_EXPR
)
2801 return (call_expr_flags (t
) & ECF_NOTHROW
) == 0;
2802 if (cfun
->can_throw_non_call_exceptions
)
2803 return tree_could_trap_p (t
);
2807 /* Return true if STMT can throw an exception that is not caught within
2808 the current function (CFUN). */
2811 stmt_can_throw_external (gimple stmt
)
2815 if (!stmt_could_throw_p (stmt
))
2818 lp_nr
= lookup_stmt_eh_lp (stmt
);
2822 /* Return true if STMT can throw an exception that is caught within
2823 the current function (CFUN). */
2826 stmt_can_throw_internal (gimple stmt
)
2830 if (!stmt_could_throw_p (stmt
))
2833 lp_nr
= lookup_stmt_eh_lp (stmt
);
2837 /* Given a statement STMT in IFUN, if STMT can no longer throw, then
2838 remove any entry it might have from the EH table. Return true if
2839 any change was made. */
2842 maybe_clean_eh_stmt_fn (struct function
*ifun
, gimple stmt
)
2844 if (stmt_could_throw_p (stmt
))
2846 return remove_stmt_from_eh_lp_fn (ifun
, stmt
);
2849 /* Likewise, but always use the current function. */
2852 maybe_clean_eh_stmt (gimple stmt
)
2854 return maybe_clean_eh_stmt_fn (cfun
, stmt
);
2857 /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
2858 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
2859 in the table if it should be in there. Return TRUE if a replacement was
2860 done that my require an EH edge purge. */
2863 maybe_clean_or_replace_eh_stmt (gimple old_stmt
, gimple new_stmt
)
2865 int lp_nr
= lookup_stmt_eh_lp (old_stmt
);
2869 bool new_stmt_could_throw
= stmt_could_throw_p (new_stmt
);
2871 if (new_stmt
== old_stmt
&& new_stmt_could_throw
)
2874 remove_stmt_from_eh_lp (old_stmt
);
2875 if (new_stmt_could_throw
)
2877 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
2887 /* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
2888 in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
2889 operand is the return value of duplicate_eh_regions. */
2892 maybe_duplicate_eh_stmt_fn (struct function
*new_fun
, gimple new_stmt
,
2893 struct function
*old_fun
, gimple old_stmt
,
2894 hash_map
<void *, void *> *map
,
2897 int old_lp_nr
, new_lp_nr
;
2899 if (!stmt_could_throw_p (new_stmt
))
2902 old_lp_nr
= lookup_stmt_eh_lp_fn (old_fun
, old_stmt
);
2905 if (default_lp_nr
== 0)
2907 new_lp_nr
= default_lp_nr
;
2909 else if (old_lp_nr
> 0)
2911 eh_landing_pad old_lp
, new_lp
;
2913 old_lp
= (*old_fun
->eh
->lp_array
)[old_lp_nr
];
2914 new_lp
= static_cast<eh_landing_pad
> (*map
->get (old_lp
));
2915 new_lp_nr
= new_lp
->index
;
2919 eh_region old_r
, new_r
;
2921 old_r
= (*old_fun
->eh
->region_array
)[-old_lp_nr
];
2922 new_r
= static_cast<eh_region
> (*map
->get (old_r
));
2923 new_lp_nr
= -new_r
->index
;
2926 add_stmt_to_eh_lp_fn (new_fun
, new_stmt
, new_lp_nr
);
2930 /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
2931 and thus no remapping is required. */
2934 maybe_duplicate_eh_stmt (gimple new_stmt
, gimple old_stmt
)
2938 if (!stmt_could_throw_p (new_stmt
))
2941 lp_nr
= lookup_stmt_eh_lp (old_stmt
);
2945 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
2949 /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
2950 GIMPLE_TRY) that are similar enough to be considered the same. Currently
2951 this only handles handlers consisting of a single call, as that's the
2952 important case for C++: a destructor call for a particular object showing
2953 up in multiple handlers. */
2956 same_handler_p (gimple_seq oneh
, gimple_seq twoh
)
2958 gimple_stmt_iterator gsi
;
2962 gsi
= gsi_start (oneh
);
2963 if (!gsi_one_before_end_p (gsi
))
2965 ones
= gsi_stmt (gsi
);
2967 gsi
= gsi_start (twoh
);
2968 if (!gsi_one_before_end_p (gsi
))
2970 twos
= gsi_stmt (gsi
);
2972 if (!is_gimple_call (ones
)
2973 || !is_gimple_call (twos
)
2974 || gimple_call_lhs (ones
)
2975 || gimple_call_lhs (twos
)
2976 || gimple_call_chain (ones
)
2977 || gimple_call_chain (twos
)
2978 || !gimple_call_same_target_p (ones
, twos
)
2979 || gimple_call_num_args (ones
) != gimple_call_num_args (twos
))
2982 for (ai
= 0; ai
< gimple_call_num_args (ones
); ++ai
)
2983 if (!operand_equal_p (gimple_call_arg (ones
, ai
),
2984 gimple_call_arg (twos
, ai
), 0))
2991 try { A() } finally { try { ~B() } catch { ~A() } }
2992 try { ... } finally { ~A() }
2994 try { A() } catch { ~B() }
2995 try { ~B() ... } finally { ~A() }
2997 This occurs frequently in C++, where A is a local variable and B is a
2998 temporary used in the initializer for A. */
3001 optimize_double_finally (gimple one
, gimple two
)
3004 gimple_stmt_iterator gsi
;
3007 cleanup
= gimple_try_cleanup (one
);
3008 gsi
= gsi_start (cleanup
);
3009 if (!gsi_one_before_end_p (gsi
))
3012 oneh
= gsi_stmt (gsi
);
3013 if (gimple_code (oneh
) != GIMPLE_TRY
3014 || gimple_try_kind (oneh
) != GIMPLE_TRY_CATCH
)
3017 if (same_handler_p (gimple_try_cleanup (oneh
), gimple_try_cleanup (two
)))
3019 gimple_seq seq
= gimple_try_eval (oneh
);
3021 gimple_try_set_cleanup (one
, seq
);
3022 gimple_try_set_kind (one
, GIMPLE_TRY_CATCH
);
3023 seq
= copy_gimple_seq_and_replace_locals (seq
);
3024 gimple_seq_add_seq (&seq
, gimple_try_eval (two
));
3025 gimple_try_set_eval (two
, seq
);
3029 /* Perform EH refactoring optimizations that are simpler to do when code
3030 flow has been lowered but EH structures haven't. */
3033 refactor_eh_r (gimple_seq seq
)
3035 gimple_stmt_iterator gsi
;
3040 gsi
= gsi_start (seq
);
3044 if (gsi_end_p (gsi
))
3047 two
= gsi_stmt (gsi
);
3050 && gimple_code (one
) == GIMPLE_TRY
3051 && gimple_code (two
) == GIMPLE_TRY
3052 && gimple_try_kind (one
) == GIMPLE_TRY_FINALLY
3053 && gimple_try_kind (two
) == GIMPLE_TRY_FINALLY
)
3054 optimize_double_finally (one
, two
);
3056 switch (gimple_code (one
))
3059 refactor_eh_r (gimple_try_eval (one
));
3060 refactor_eh_r (gimple_try_cleanup (one
));
3063 refactor_eh_r (gimple_catch_handler (one
));
3065 case GIMPLE_EH_FILTER
:
3066 refactor_eh_r (gimple_eh_filter_failure (one
));
3068 case GIMPLE_EH_ELSE
:
3069 refactor_eh_r (gimple_eh_else_n_body (one
));
3070 refactor_eh_r (gimple_eh_else_e_body (one
));
3084 const pass_data pass_data_refactor_eh
=
3086 GIMPLE_PASS
, /* type */
3088 OPTGROUP_NONE
, /* optinfo_flags */
3089 TV_TREE_EH
, /* tv_id */
3090 PROP_gimple_lcf
, /* properties_required */
3091 0, /* properties_provided */
3092 0, /* properties_destroyed */
3093 0, /* todo_flags_start */
3094 0, /* todo_flags_finish */
3097 class pass_refactor_eh
: public gimple_opt_pass
3100 pass_refactor_eh (gcc::context
*ctxt
)
3101 : gimple_opt_pass (pass_data_refactor_eh
, ctxt
)
3104 /* opt_pass methods: */
3105 virtual bool gate (function
*) { return flag_exceptions
!= 0; }
3106 virtual unsigned int execute (function
*)
3108 refactor_eh_r (gimple_body (current_function_decl
));
3112 }; // class pass_refactor_eh
3117 make_pass_refactor_eh (gcc::context
*ctxt
)
3119 return new pass_refactor_eh (ctxt
);
3122 /* At the end of gimple optimization, we can lower RESX. */
3125 lower_resx (basic_block bb
, gimple stmt
, hash_map
<eh_region
, tree
> *mnt_map
)
3128 eh_region src_r
, dst_r
;
3129 gimple_stmt_iterator gsi
;
3134 lp_nr
= lookup_stmt_eh_lp (stmt
);
3136 dst_r
= get_eh_region_from_lp_number (lp_nr
);
3140 src_r
= get_eh_region_from_number (gimple_resx_region (stmt
));
3141 gsi
= gsi_last_bb (bb
);
3145 /* We can wind up with no source region when pass_cleanup_eh shows
3146 that there are no entries into an eh region and deletes it, but
3147 then the block that contains the resx isn't removed. This can
3148 happen without optimization when the switch statement created by
3149 lower_try_finally_switch isn't simplified to remove the eh case.
3151 Resolve this by expanding the resx node to an abort. */
3153 fn
= builtin_decl_implicit (BUILT_IN_TRAP
);
3154 x
= gimple_build_call (fn
, 0);
3155 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3157 while (EDGE_COUNT (bb
->succs
) > 0)
3158 remove_edge (EDGE_SUCC (bb
, 0));
3162 /* When we have a destination region, we resolve this by copying
3163 the excptr and filter values into place, and changing the edge
3164 to immediately after the landing pad. */
3172 /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3173 the failure decl into a new block, if needed. */
3174 gcc_assert (dst_r
->type
== ERT_MUST_NOT_THROW
);
3176 tree
*slot
= mnt_map
->get (dst_r
);
3179 gimple_stmt_iterator gsi2
;
3181 new_bb
= create_empty_bb (bb
);
3182 add_bb_to_loop (new_bb
, bb
->loop_father
);
3183 lab
= gimple_block_label (new_bb
);
3184 gsi2
= gsi_start_bb (new_bb
);
3186 fn
= dst_r
->u
.must_not_throw
.failure_decl
;
3187 x
= gimple_build_call (fn
, 0);
3188 gimple_set_location (x
, dst_r
->u
.must_not_throw
.failure_loc
);
3189 gsi_insert_after (&gsi2
, x
, GSI_CONTINUE_LINKING
);
3191 mnt_map
->put (dst_r
, lab
);
3196 new_bb
= label_to_block (lab
);
3199 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3200 e
= make_edge (bb
, new_bb
, EDGE_FALLTHRU
);
3201 e
->count
= bb
->count
;
3202 e
->probability
= REG_BR_PROB_BASE
;
3207 tree dst_nr
= build_int_cst (integer_type_node
, dst_r
->index
);
3209 fn
= builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES
);
3210 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3211 x
= gimple_build_call (fn
, 2, dst_nr
, src_nr
);
3212 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3214 /* Update the flags for the outgoing edge. */
3215 e
= single_succ_edge (bb
);
3216 gcc_assert (e
->flags
& EDGE_EH
);
3217 e
->flags
= (e
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
3219 /* If there are no more EH users of the landing pad, delete it. */
3220 FOR_EACH_EDGE (e
, ei
, e
->dest
->preds
)
3221 if (e
->flags
& EDGE_EH
)
3225 eh_landing_pad lp
= get_eh_landing_pad_from_number (lp_nr
);
3226 remove_eh_landing_pad (lp
);
3236 /* When we don't have a destination region, this exception escapes
3237 up the call chain. We resolve this by generating a call to the
3238 _Unwind_Resume library function. */
3240 /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3241 with no arguments for C++ and Java. Check for that. */
3242 if (src_r
->use_cxa_end_cleanup
)
3244 fn
= builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP
);
3245 x
= gimple_build_call (fn
, 0);
3246 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3250 fn
= builtin_decl_implicit (BUILT_IN_EH_POINTER
);
3251 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3252 x
= gimple_build_call (fn
, 1, src_nr
);
3253 var
= create_tmp_var (ptr_type_node
, NULL
);
3254 var
= make_ssa_name (var
, x
);
3255 gimple_call_set_lhs (x
, var
);
3256 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3258 fn
= builtin_decl_implicit (BUILT_IN_UNWIND_RESUME
);
3259 x
= gimple_build_call (fn
, 1, var
);
3260 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3263 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3266 gsi_remove (&gsi
, true);
3273 const pass_data pass_data_lower_resx
=
3275 GIMPLE_PASS
, /* type */
3277 OPTGROUP_NONE
, /* optinfo_flags */
3278 TV_TREE_EH
, /* tv_id */
3279 PROP_gimple_lcf
, /* properties_required */
3280 0, /* properties_provided */
3281 0, /* properties_destroyed */
3282 0, /* todo_flags_start */
3283 0, /* todo_flags_finish */
3286 class pass_lower_resx
: public gimple_opt_pass
3289 pass_lower_resx (gcc::context
*ctxt
)
3290 : gimple_opt_pass (pass_data_lower_resx
, ctxt
)
3293 /* opt_pass methods: */
3294 virtual bool gate (function
*) { return flag_exceptions
!= 0; }
3295 virtual unsigned int execute (function
*);
3297 }; // class pass_lower_resx
3300 pass_lower_resx::execute (function
*fun
)
3303 bool dominance_invalidated
= false;
3304 bool any_rewritten
= false;
3306 hash_map
<eh_region
, tree
> mnt_map
;
3308 FOR_EACH_BB_FN (bb
, fun
)
3310 gimple last
= last_stmt (bb
);
3311 if (last
&& is_gimple_resx (last
))
3313 dominance_invalidated
|= lower_resx (bb
, last
, &mnt_map
);
3314 any_rewritten
= true;
3318 if (dominance_invalidated
)
3320 free_dominance_info (CDI_DOMINATORS
);
3321 free_dominance_info (CDI_POST_DOMINATORS
);
3324 return any_rewritten
? TODO_update_ssa_only_virtuals
: 0;
3330 make_pass_lower_resx (gcc::context
*ctxt
)
3332 return new pass_lower_resx (ctxt
);
3335 /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3339 optimize_clobbers (basic_block bb
)
3341 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
3342 bool any_clobbers
= false;
3343 bool seen_stack_restore
= false;
3347 /* Only optimize anything if the bb contains at least one clobber,
3348 ends with resx (checked by caller), optionally contains some
3349 debug stmts or labels, or at most one __builtin_stack_restore
3350 call, and has an incoming EH edge. */
3351 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3353 gimple stmt
= gsi_stmt (gsi
);
3354 if (is_gimple_debug (stmt
))
3356 if (gimple_clobber_p (stmt
))
3358 any_clobbers
= true;
3361 if (!seen_stack_restore
3362 && gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
3364 seen_stack_restore
= true;
3367 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3373 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3374 if (e
->flags
& EDGE_EH
)
3378 gsi
= gsi_last_bb (bb
);
3379 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3381 gimple stmt
= gsi_stmt (gsi
);
3382 if (!gimple_clobber_p (stmt
))
3384 unlink_stmt_vdef (stmt
);
3385 gsi_remove (&gsi
, true);
3386 release_defs (stmt
);
3390 /* Try to sink var = {v} {CLOBBER} stmts followed just by
3391 internal throw to successor BB. */
3394 sink_clobbers (basic_block bb
)
3398 gimple_stmt_iterator gsi
, dgsi
;
3400 bool any_clobbers
= false;
3403 /* Only optimize if BB has a single EH successor and
3404 all predecessor edges are EH too. */
3405 if (!single_succ_p (bb
)
3406 || (single_succ_edge (bb
)->flags
& EDGE_EH
) == 0)
3409 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3411 if ((e
->flags
& EDGE_EH
) == 0)
3415 /* And BB contains only CLOBBER stmts before the final
3417 gsi
= gsi_last_bb (bb
);
3418 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3420 gimple stmt
= gsi_stmt (gsi
);
3421 if (is_gimple_debug (stmt
))
3423 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3425 if (!gimple_clobber_p (stmt
))
3427 any_clobbers
= true;
3432 edge succe
= single_succ_edge (bb
);
3433 succbb
= succe
->dest
;
3435 /* See if there is a virtual PHI node to take an updated virtual
3438 tree vuse
= NULL_TREE
;
3439 for (gsi
= gsi_start_phis (succbb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3441 tree res
= gimple_phi_result (gsi_stmt (gsi
));
3442 if (virtual_operand_p (res
))
3444 vphi
= gsi_stmt (gsi
);
3450 dgsi
= gsi_after_labels (succbb
);
3451 gsi
= gsi_last_bb (bb
);
3452 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3454 gimple stmt
= gsi_stmt (gsi
);
3456 if (is_gimple_debug (stmt
))
3458 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3460 lhs
= gimple_assign_lhs (stmt
);
3461 /* Unfortunately we don't have dominance info updated at this
3462 point, so checking if
3463 dominated_by_p (CDI_DOMINATORS, succbb,
3464 gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0)))
3465 would be too costly. Thus, avoid sinking any clobbers that
3466 refer to non-(D) SSA_NAMEs. */
3467 if (TREE_CODE (lhs
) == MEM_REF
3468 && TREE_CODE (TREE_OPERAND (lhs
, 0)) == SSA_NAME
3469 && !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs
, 0)))
3471 unlink_stmt_vdef (stmt
);
3472 gsi_remove (&gsi
, true);
3473 release_defs (stmt
);
3477 /* As we do not change stmt order when sinking across a
3478 forwarder edge we can keep virtual operands in place. */
3479 gsi_remove (&gsi
, false);
3480 gsi_insert_before (&dgsi
, stmt
, GSI_NEW_STMT
);
3482 /* But adjust virtual operands if we sunk across a PHI node. */
3486 imm_use_iterator iter
;
3487 use_operand_p use_p
;
3488 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, vuse
)
3489 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3490 SET_USE (use_p
, gimple_vdef (stmt
));
3491 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse
))
3493 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (stmt
)) = 1;
3494 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse
) = 0;
3496 /* Adjust the incoming virtual operand. */
3497 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (vphi
, succe
), gimple_vuse (stmt
));
3498 SET_USE (gimple_vuse_op (stmt
), vuse
);
3500 /* If there isn't a single predecessor but no virtual PHI node
3501 arrange for virtual operands to be renamed. */
3502 else if (gimple_vuse_op (stmt
) != NULL_USE_OPERAND_P
3503 && !single_pred_p (succbb
))
3505 /* In this case there will be no use of the VDEF of this stmt.
3506 ??? Unless this is a secondary opportunity and we have not
3507 removed unreachable blocks yet, so we cannot assert this.
3508 Which also means we will end up renaming too many times. */
3509 SET_USE (gimple_vuse_op (stmt
), gimple_vop (cfun
));
3510 mark_virtual_operands_for_renaming (cfun
);
3511 todo
|= TODO_update_ssa_only_virtuals
;
3518 /* At the end of inlining, we can lower EH_DISPATCH. Return true when
3519 we have found some duplicate labels and removed some edges. */
3522 lower_eh_dispatch (basic_block src
, gimple stmt
)
3524 gimple_stmt_iterator gsi
;
3529 bool redirected
= false;
3531 region_nr
= gimple_eh_dispatch_region (stmt
);
3532 r
= get_eh_region_from_number (region_nr
);
3534 gsi
= gsi_last_bb (src
);
3540 auto_vec
<tree
> labels
;
3541 tree default_label
= NULL
;
3545 hash_set
<tree
> seen_values
;
3547 /* Collect the labels for a switch. Zero the post_landing_pad
3548 field becase we'll no longer have anything keeping these labels
3549 in existence and the optimizer will be free to merge these
3551 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
3553 tree tp_node
, flt_node
, lab
= c
->label
;
3554 bool have_label
= false;
3557 tp_node
= c
->type_list
;
3558 flt_node
= c
->filter_list
;
3560 if (tp_node
== NULL
)
3562 default_label
= lab
;
3567 /* Filter out duplicate labels that arise when this handler
3568 is shadowed by an earlier one. When no labels are
3569 attached to the handler anymore, we remove
3570 the corresponding edge and then we delete unreachable
3571 blocks at the end of this pass. */
3572 if (! seen_values
.contains (TREE_VALUE (flt_node
)))
3574 tree t
= build_case_label (TREE_VALUE (flt_node
),
3576 labels
.safe_push (t
);
3577 seen_values
.add (TREE_VALUE (flt_node
));
3581 tp_node
= TREE_CHAIN (tp_node
);
3582 flt_node
= TREE_CHAIN (flt_node
);
3587 remove_edge (find_edge (src
, label_to_block (lab
)));
3592 /* Clean up the edge flags. */
3593 FOR_EACH_EDGE (e
, ei
, src
->succs
)
3595 if (e
->flags
& EDGE_FALLTHRU
)
3597 /* If there was no catch-all, use the fallthru edge. */
3598 if (default_label
== NULL
)
3599 default_label
= gimple_block_label (e
->dest
);
3600 e
->flags
&= ~EDGE_FALLTHRU
;
3603 gcc_assert (default_label
!= NULL
);
3605 /* Don't generate a switch if there's only a default case.
3606 This is common in the form of try { A; } catch (...) { B; }. */
3607 if (!labels
.exists ())
3609 e
= single_succ_edge (src
);
3610 e
->flags
|= EDGE_FALLTHRU
;
3614 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3615 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3617 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)), NULL
);
3618 filter
= make_ssa_name (filter
, x
);
3619 gimple_call_set_lhs (x
, filter
);
3620 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3622 /* Turn the default label into a default case. */
3623 default_label
= build_case_label (NULL
, NULL
, default_label
);
3624 sort_case_labels (labels
);
3626 x
= gimple_build_switch (filter
, default_label
, labels
);
3627 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3632 case ERT_ALLOWED_EXCEPTIONS
:
3634 edge b_e
= BRANCH_EDGE (src
);
3635 edge f_e
= FALLTHRU_EDGE (src
);
3637 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3638 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3640 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)), NULL
);
3641 filter
= make_ssa_name (filter
, x
);
3642 gimple_call_set_lhs (x
, filter
);
3643 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3645 r
->u
.allowed
.label
= NULL
;
3646 x
= gimple_build_cond (EQ_EXPR
, filter
,
3647 build_int_cst (TREE_TYPE (filter
),
3648 r
->u
.allowed
.filter
),
3649 NULL_TREE
, NULL_TREE
);
3650 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3652 b_e
->flags
= b_e
->flags
| EDGE_TRUE_VALUE
;
3653 f_e
->flags
= (f_e
->flags
& ~EDGE_FALLTHRU
) | EDGE_FALSE_VALUE
;
3661 /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3662 gsi_remove (&gsi
, true);
3668 const pass_data pass_data_lower_eh_dispatch
=
3670 GIMPLE_PASS
, /* type */
3671 "ehdisp", /* name */
3672 OPTGROUP_NONE
, /* optinfo_flags */
3673 TV_TREE_EH
, /* tv_id */
3674 PROP_gimple_lcf
, /* properties_required */
3675 0, /* properties_provided */
3676 0, /* properties_destroyed */
3677 0, /* todo_flags_start */
3678 0, /* todo_flags_finish */
3681 class pass_lower_eh_dispatch
: public gimple_opt_pass
3684 pass_lower_eh_dispatch (gcc::context
*ctxt
)
3685 : gimple_opt_pass (pass_data_lower_eh_dispatch
, ctxt
)
3688 /* opt_pass methods: */
3689 virtual bool gate (function
*fun
) { return fun
->eh
->region_tree
!= NULL
; }
3690 virtual unsigned int execute (function
*);
3692 }; // class pass_lower_eh_dispatch
3695 pass_lower_eh_dispatch::execute (function
*fun
)
3699 bool redirected
= false;
3701 assign_filter_values ();
3703 FOR_EACH_BB_FN (bb
, fun
)
3705 gimple last
= last_stmt (bb
);
3708 if (gimple_code (last
) == GIMPLE_EH_DISPATCH
)
3710 redirected
|= lower_eh_dispatch (bb
, last
);
3711 flags
|= TODO_update_ssa_only_virtuals
;
3713 else if (gimple_code (last
) == GIMPLE_RESX
)
3715 if (stmt_can_throw_external (last
))
3716 optimize_clobbers (bb
);
3718 flags
|= sink_clobbers (bb
);
3723 delete_unreachable_blocks ();
3730 make_pass_lower_eh_dispatch (gcc::context
*ctxt
)
3732 return new pass_lower_eh_dispatch (ctxt
);
3735 /* Walk statements, see what regions and, optionally, landing pads
3736 are really referenced.
3738 Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
3739 and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
3741 Passing NULL for LP_REACHABLE is valid, in this case only reachable
3744 The caller is responsible for freeing the returned sbitmaps. */
3747 mark_reachable_handlers (sbitmap
*r_reachablep
, sbitmap
*lp_reachablep
)
3749 sbitmap r_reachable
, lp_reachable
;
3751 bool mark_landing_pads
= (lp_reachablep
!= NULL
);
3752 gcc_checking_assert (r_reachablep
!= NULL
);
3754 r_reachable
= sbitmap_alloc (cfun
->eh
->region_array
->length ());
3755 bitmap_clear (r_reachable
);
3756 *r_reachablep
= r_reachable
;
3758 if (mark_landing_pads
)
3760 lp_reachable
= sbitmap_alloc (cfun
->eh
->lp_array
->length ());
3761 bitmap_clear (lp_reachable
);
3762 *lp_reachablep
= lp_reachable
;
3765 lp_reachable
= NULL
;
3767 FOR_EACH_BB_FN (bb
, cfun
)
3769 gimple_stmt_iterator gsi
;
3771 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3773 gimple stmt
= gsi_stmt (gsi
);
3775 if (mark_landing_pads
)
3777 int lp_nr
= lookup_stmt_eh_lp (stmt
);
3779 /* Negative LP numbers are MUST_NOT_THROW regions which
3780 are not considered BB enders. */
3782 bitmap_set_bit (r_reachable
, -lp_nr
);
3784 /* Positive LP numbers are real landing pads, and BB enders. */
3787 gcc_assert (gsi_one_before_end_p (gsi
));
3788 eh_region region
= get_eh_region_from_lp_number (lp_nr
);
3789 bitmap_set_bit (r_reachable
, region
->index
);
3790 bitmap_set_bit (lp_reachable
, lp_nr
);
3794 /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
3795 switch (gimple_code (stmt
))
3798 bitmap_set_bit (r_reachable
, gimple_resx_region (stmt
));
3800 case GIMPLE_EH_DISPATCH
:
3801 bitmap_set_bit (r_reachable
, gimple_eh_dispatch_region (stmt
));
3810 /* Remove unreachable handlers and unreachable landing pads. */
3813 remove_unreachable_handlers (void)
3815 sbitmap r_reachable
, lp_reachable
;
3820 mark_reachable_handlers (&r_reachable
, &lp_reachable
);
3824 fprintf (dump_file
, "Before removal of unreachable regions:\n");
3825 dump_eh_tree (dump_file
, cfun
);
3826 fprintf (dump_file
, "Reachable regions: ");
3827 dump_bitmap_file (dump_file
, r_reachable
);
3828 fprintf (dump_file
, "Reachable landing pads: ");
3829 dump_bitmap_file (dump_file
, lp_reachable
);
3834 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
3835 if (region
&& !bitmap_bit_p (r_reachable
, region
->index
))
3837 "Removing unreachable region %d\n",
3841 remove_unreachable_eh_regions (r_reachable
);
3843 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
3844 if (lp
&& !bitmap_bit_p (lp_reachable
, lp
->index
))
3848 "Removing unreachable landing pad %d\n",
3850 remove_eh_landing_pad (lp
);
3855 fprintf (dump_file
, "\n\nAfter removal of unreachable regions:\n");
3856 dump_eh_tree (dump_file
, cfun
);
3857 fprintf (dump_file
, "\n\n");
3860 sbitmap_free (r_reachable
);
3861 sbitmap_free (lp_reachable
);
3863 #ifdef ENABLE_CHECKING
3864 verify_eh_tree (cfun
);
3868 /* Remove unreachable handlers if any landing pads have been removed after
3869 last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
3872 maybe_remove_unreachable_handlers (void)
3877 if (cfun
->eh
== NULL
)
3880 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
3881 if (lp
&& lp
->post_landing_pad
)
3883 if (label_to_block (lp
->post_landing_pad
) == NULL
)
3885 remove_unreachable_handlers ();
3891 /* Remove regions that do not have landing pads. This assumes
3892 that remove_unreachable_handlers has already been run, and
3893 that we've just manipulated the landing pads since then.
3895 Preserve regions with landing pads and regions that prevent
3896 exceptions from propagating further, even if these regions
3897 are not reachable. */
3900 remove_unreachable_handlers_no_lp (void)
3903 sbitmap r_reachable
;
3906 mark_reachable_handlers (&r_reachable
, /*lp_reachablep=*/NULL
);
3908 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
3913 if (region
->landing_pads
!= NULL
3914 || region
->type
== ERT_MUST_NOT_THROW
)
3915 bitmap_set_bit (r_reachable
, region
->index
);
3918 && !bitmap_bit_p (r_reachable
, region
->index
))
3920 "Removing unreachable region %d\n",
3924 remove_unreachable_eh_regions (r_reachable
);
3926 sbitmap_free (r_reachable
);
3929 /* Undo critical edge splitting on an EH landing pad. Earlier, we
3930 optimisticaly split all sorts of edges, including EH edges. The
3931 optimization passes in between may not have needed them; if not,
3932 we should undo the split.
3934 Recognize this case by having one EH edge incoming to the BB and
3935 one normal edge outgoing; BB should be empty apart from the
3936 post_landing_pad label.
3938 Note that this is slightly different from the empty handler case
3939 handled by cleanup_empty_eh, in that the actual handler may yet
3940 have actual code but the landing pad has been separated from the
3941 handler. As such, cleanup_empty_eh relies on this transformation
3942 having been done first. */
3945 unsplit_eh (eh_landing_pad lp
)
3947 basic_block bb
= label_to_block (lp
->post_landing_pad
);
3948 gimple_stmt_iterator gsi
;
3951 /* Quickly check the edge counts on BB for singularity. */
3952 if (!single_pred_p (bb
) || !single_succ_p (bb
))
3954 e_in
= single_pred_edge (bb
);
3955 e_out
= single_succ_edge (bb
);
3957 /* Input edge must be EH and output edge must be normal. */
3958 if ((e_in
->flags
& EDGE_EH
) == 0 || (e_out
->flags
& EDGE_EH
) != 0)
3961 /* The block must be empty except for the labels and debug insns. */
3962 gsi
= gsi_after_labels (bb
);
3963 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
3964 gsi_next_nondebug (&gsi
);
3965 if (!gsi_end_p (gsi
))
3968 /* The destination block must not already have a landing pad
3969 for a different region. */
3970 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3972 gimple stmt
= gsi_stmt (gsi
);
3976 if (gimple_code (stmt
) != GIMPLE_LABEL
)
3978 lab
= gimple_label_label (stmt
);
3979 lp_nr
= EH_LANDING_PAD_NR (lab
);
3980 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
3984 /* The new destination block must not already be a destination of
3985 the source block, lest we merge fallthru and eh edges and get
3986 all sorts of confused. */
3987 if (find_edge (e_in
->src
, e_out
->dest
))
3990 /* ??? We can get degenerate phis due to cfg cleanups. I would have
3991 thought this should have been cleaned up by a phicprop pass, but
3992 that doesn't appear to handle virtuals. Propagate by hand. */
3993 if (!gimple_seq_empty_p (phi_nodes (bb
)))
3995 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); )
3997 gimple use_stmt
, phi
= gsi_stmt (gsi
);
3998 tree lhs
= gimple_phi_result (phi
);
3999 tree rhs
= gimple_phi_arg_def (phi
, 0);
4000 use_operand_p use_p
;
4001 imm_use_iterator iter
;
4003 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
4005 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4006 SET_USE (use_p
, rhs
);
4009 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
4010 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
) = 1;
4012 remove_phi_node (&gsi
, true);
4016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4017 fprintf (dump_file
, "Unsplit EH landing pad %d to block %i.\n",
4018 lp
->index
, e_out
->dest
->index
);
4020 /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
4021 a successor edge, humor it. But do the real CFG change with the
4022 predecessor of E_OUT in order to preserve the ordering of arguments
4023 to the PHI nodes in E_OUT->DEST. */
4024 redirect_eh_edge_1 (e_in
, e_out
->dest
, false);
4025 redirect_edge_pred (e_out
, e_in
->src
);
4026 e_out
->flags
= e_in
->flags
;
4027 e_out
->probability
= e_in
->probability
;
4028 e_out
->count
= e_in
->count
;
4034 /* Examine each landing pad block and see if it matches unsplit_eh. */
4037 unsplit_all_eh (void)
4039 bool changed
= false;
4043 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4045 changed
|= unsplit_eh (lp
);
4050 /* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
4051 to OLD_BB to NEW_BB; return true on success, false on failure.
4053 OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
4054 PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
4055 Virtual PHIs may be deleted and marked for renaming. */
4058 cleanup_empty_eh_merge_phis (basic_block new_bb
, basic_block old_bb
,
4059 edge old_bb_out
, bool change_region
)
4061 gimple_stmt_iterator ngsi
, ogsi
;
4064 bitmap ophi_handled
;
4066 /* The destination block must not be a regular successor for any
4067 of the preds of the landing pad. Thus, avoid turning
4077 which CFG verification would choke on. See PR45172 and PR51089. */
4078 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4079 if (find_edge (e
->src
, new_bb
))
4082 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4083 redirect_edge_var_map_clear (e
);
4085 ophi_handled
= BITMAP_ALLOC (NULL
);
4087 /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
4088 for the edges we're going to move. */
4089 for (ngsi
= gsi_start_phis (new_bb
); !gsi_end_p (ngsi
); gsi_next (&ngsi
))
4091 gimple ophi
, nphi
= gsi_stmt (ngsi
);
4094 nresult
= gimple_phi_result (nphi
);
4095 nop
= gimple_phi_arg_def (nphi
, old_bb_out
->dest_idx
);
4097 /* Find the corresponding PHI in OLD_BB so we can forward-propagate
4098 the source ssa_name. */
4100 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4102 ophi
= gsi_stmt (ogsi
);
4103 if (gimple_phi_result (ophi
) == nop
)
4108 /* If we did find the corresponding PHI, copy those inputs. */
4111 /* If NOP is used somewhere else beyond phis in new_bb, give up. */
4112 if (!has_single_use (nop
))
4114 imm_use_iterator imm_iter
;
4115 use_operand_p use_p
;
4117 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, nop
)
4119 if (!gimple_debug_bind_p (USE_STMT (use_p
))
4120 && (gimple_code (USE_STMT (use_p
)) != GIMPLE_PHI
4121 || gimple_bb (USE_STMT (use_p
)) != new_bb
))
4125 bitmap_set_bit (ophi_handled
, SSA_NAME_VERSION (nop
));
4126 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4131 if ((e
->flags
& EDGE_EH
) == 0)
4133 oop
= gimple_phi_arg_def (ophi
, e
->dest_idx
);
4134 oloc
= gimple_phi_arg_location (ophi
, e
->dest_idx
);
4135 redirect_edge_var_map_add (e
, nresult
, oop
, oloc
);
4138 /* If we didn't find the PHI, if it's a real variable or a VOP, we know
4139 from the fact that OLD_BB is tree_empty_eh_handler_p that the
4140 variable is unchanged from input to the block and we can simply
4141 re-use the input to NEW_BB from the OLD_BB_OUT edge. */
4145 = gimple_phi_arg_location (nphi
, old_bb_out
->dest_idx
);
4146 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4147 redirect_edge_var_map_add (e
, nresult
, nop
, nloc
);
4151 /* Second, verify that all PHIs from OLD_BB have been handled. If not,
4152 we don't know what values from the other edges into NEW_BB to use. */
4153 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4155 gimple ophi
= gsi_stmt (ogsi
);
4156 tree oresult
= gimple_phi_result (ophi
);
4157 if (!bitmap_bit_p (ophi_handled
, SSA_NAME_VERSION (oresult
)))
4161 /* Finally, move the edges and update the PHIs. */
4162 for (ei
= ei_start (old_bb
->preds
); (e
= ei_safe_edge (ei
)); )
4163 if (e
->flags
& EDGE_EH
)
4165 /* ??? CFG manipluation routines do not try to update loop
4166 form on edge redirection. Do so manually here for now. */
4167 /* If we redirect a loop entry or latch edge that will either create
4168 a multiple entry loop or rotate the loop. If the loops merge
4169 we may have created a loop with multiple latches.
4170 All of this isn't easily fixed thus cancel the affected loop
4171 and mark the other loop as possibly having multiple latches. */
4172 if (e
->dest
== e
->dest
->loop_father
->header
)
4174 mark_loop_for_removal (e
->dest
->loop_father
);
4175 new_bb
->loop_father
->latch
= NULL
;
4176 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
4178 redirect_eh_edge_1 (e
, new_bb
, change_region
);
4179 redirect_edge_succ (e
, new_bb
);
4180 flush_pending_stmts (e
);
4185 BITMAP_FREE (ophi_handled
);
4189 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4190 redirect_edge_var_map_clear (e
);
4191 BITMAP_FREE (ophi_handled
);
4195 /* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4196 old region to NEW_REGION at BB. */
4199 cleanup_empty_eh_move_lp (basic_block bb
, edge e_out
,
4200 eh_landing_pad lp
, eh_region new_region
)
4202 gimple_stmt_iterator gsi
;
4205 for (pp
= &lp
->region
->landing_pads
; *pp
!= lp
; pp
= &(*pp
)->next_lp
)
4209 lp
->region
= new_region
;
4210 lp
->next_lp
= new_region
->landing_pads
;
4211 new_region
->landing_pads
= lp
;
4213 /* Delete the RESX that was matched within the empty handler block. */
4214 gsi
= gsi_last_bb (bb
);
4215 unlink_stmt_vdef (gsi_stmt (gsi
));
4216 gsi_remove (&gsi
, true);
4218 /* Clean up E_OUT for the fallthru. */
4219 e_out
->flags
= (e_out
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
4220 e_out
->probability
= REG_BR_PROB_BASE
;
4223 /* A subroutine of cleanup_empty_eh. Handle more complex cases of
4224 unsplitting than unsplit_eh was prepared to handle, e.g. when
4225 multiple incoming edges and phis are involved. */
4228 cleanup_empty_eh_unsplit (basic_block bb
, edge e_out
, eh_landing_pad lp
)
4230 gimple_stmt_iterator gsi
;
4233 /* We really ought not have totally lost everything following
4234 a landing pad label. Given that BB is empty, there had better
4236 gcc_assert (e_out
!= NULL
);
4238 /* The destination block must not already have a landing pad
4239 for a different region. */
4241 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4243 gimple stmt
= gsi_stmt (gsi
);
4246 if (gimple_code (stmt
) != GIMPLE_LABEL
)
4248 lab
= gimple_label_label (stmt
);
4249 lp_nr
= EH_LANDING_PAD_NR (lab
);
4250 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4254 /* Attempt to move the PHIs into the successor block. */
4255 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, false))
4257 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4259 "Unsplit EH landing pad %d to block %i "
4260 "(via cleanup_empty_eh).\n",
4261 lp
->index
, e_out
->dest
->index
);
4268 /* Return true if edge E_FIRST is part of an empty infinite loop
4269 or leads to such a loop through a series of single successor
4273 infinite_empty_loop_p (edge e_first
)
4275 bool inf_loop
= false;
4278 if (e_first
->dest
== e_first
->src
)
4281 e_first
->src
->aux
= (void *) 1;
4282 for (e
= e_first
; single_succ_p (e
->dest
); e
= single_succ_edge (e
->dest
))
4284 gimple_stmt_iterator gsi
;
4290 e
->dest
->aux
= (void *) 1;
4291 gsi
= gsi_after_labels (e
->dest
);
4292 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4293 gsi_next_nondebug (&gsi
);
4294 if (!gsi_end_p (gsi
))
4297 e_first
->src
->aux
= NULL
;
4298 for (e
= e_first
; e
->dest
->aux
; e
= single_succ_edge (e
->dest
))
4299 e
->dest
->aux
= NULL
;
4304 /* Examine the block associated with LP to determine if it's an empty
4305 handler for its EH region. If so, attempt to redirect EH edges to
4306 an outer region. Return true the CFG was updated in any way. This
4307 is similar to jump forwarding, just across EH edges. */
4310 cleanup_empty_eh (eh_landing_pad lp
)
4312 basic_block bb
= label_to_block (lp
->post_landing_pad
);
4313 gimple_stmt_iterator gsi
;
4315 eh_region new_region
;
4318 bool has_non_eh_pred
;
4322 /* There can be zero or one edges out of BB. This is the quickest test. */
4323 switch (EDGE_COUNT (bb
->succs
))
4329 e_out
= single_succ_edge (bb
);
4335 resx
= last_stmt (bb
);
4336 if (resx
&& is_gimple_resx (resx
))
4338 if (stmt_can_throw_external (resx
))
4339 optimize_clobbers (bb
);
4340 else if (sink_clobbers (bb
))
4344 gsi
= gsi_after_labels (bb
);
4346 /* Make sure to skip debug statements. */
4347 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4348 gsi_next_nondebug (&gsi
);
4350 /* If the block is totally empty, look for more unsplitting cases. */
4351 if (gsi_end_p (gsi
))
4353 /* For the degenerate case of an infinite loop bail out.
4354 If bb has no successors and is totally empty, which can happen e.g.
4355 because of incorrect noreturn attribute, bail out too. */
4357 || infinite_empty_loop_p (e_out
))
4360 return ret
| cleanup_empty_eh_unsplit (bb
, e_out
, lp
);
4363 /* The block should consist only of a single RESX statement, modulo a
4364 preceding call to __builtin_stack_restore if there is no outgoing
4365 edge, since the call can be eliminated in this case. */
4366 resx
= gsi_stmt (gsi
);
4367 if (!e_out
&& gimple_call_builtin_p (resx
, BUILT_IN_STACK_RESTORE
))
4370 resx
= gsi_stmt (gsi
);
4372 if (!is_gimple_resx (resx
))
4374 gcc_assert (gsi_one_before_end_p (gsi
));
4376 /* Determine if there are non-EH edges, or resx edges into the handler. */
4377 has_non_eh_pred
= false;
4378 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4379 if (!(e
->flags
& EDGE_EH
))
4380 has_non_eh_pred
= true;
4382 /* Find the handler that's outer of the empty handler by looking at
4383 where the RESX instruction was vectored. */
4384 new_lp_nr
= lookup_stmt_eh_lp (resx
);
4385 new_region
= get_eh_region_from_lp_number (new_lp_nr
);
4387 /* If there's no destination region within the current function,
4388 redirection is trivial via removing the throwing statements from
4389 the EH region, removing the EH edges, and allowing the block
4390 to go unreachable. */
4391 if (new_region
== NULL
)
4393 gcc_assert (e_out
== NULL
);
4394 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4395 if (e
->flags
& EDGE_EH
)
4397 gimple stmt
= last_stmt (e
->src
);
4398 remove_stmt_from_eh_lp (stmt
);
4406 /* If the destination region is a MUST_NOT_THROW, allow the runtime
4407 to handle the abort and allow the blocks to go unreachable. */
4408 if (new_region
->type
== ERT_MUST_NOT_THROW
)
4410 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4411 if (e
->flags
& EDGE_EH
)
4413 gimple stmt
= last_stmt (e
->src
);
4414 remove_stmt_from_eh_lp (stmt
);
4415 add_stmt_to_eh_lp (stmt
, new_lp_nr
);
4423 /* Try to redirect the EH edges and merge the PHIs into the destination
4424 landing pad block. If the merge succeeds, we'll already have redirected
4425 all the EH edges. The handler itself will go unreachable if there were
4427 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, true))
4430 /* Finally, if all input edges are EH edges, then we can (potentially)
4431 reduce the number of transfers from the runtime by moving the landing
4432 pad from the original region to the new region. This is a win when
4433 we remove the last CLEANUP region along a particular exception
4434 propagation path. Since nothing changes except for the region with
4435 which the landing pad is associated, the PHI nodes do not need to be
4437 if (!has_non_eh_pred
)
4439 cleanup_empty_eh_move_lp (bb
, e_out
, lp
, new_region
);
4440 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4441 fprintf (dump_file
, "Empty EH handler %i moved to EH region %i.\n",
4442 lp
->index
, new_region
->index
);
4444 /* ??? The CFG didn't change, but we may have rendered the
4445 old EH region unreachable. Trigger a cleanup there. */
4452 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4453 fprintf (dump_file
, "Empty EH handler %i removed.\n", lp
->index
);
4454 remove_eh_landing_pad (lp
);
4458 /* Do a post-order traversal of the EH region tree. Examine each
4459 post_landing_pad block and see if we can eliminate it as empty. */
4462 cleanup_all_empty_eh (void)
4464 bool changed
= false;
4468 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4470 changed
|= cleanup_empty_eh (lp
);
4475 /* Perform cleanups and lowering of exception handling
4476 1) cleanups regions with handlers doing nothing are optimized out
4477 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4478 3) Info about regions that are containing instructions, and regions
4479 reachable via local EH edges is collected
4480 4) Eh tree is pruned for regions no longer necessary.
4482 TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4483 Unify those that have the same failure decl and locus.
4487 execute_cleanup_eh_1 (void)
4489 /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4490 looking up unreachable landing pads. */
4491 remove_unreachable_handlers ();
4493 /* Watch out for the region tree vanishing due to all unreachable. */
4494 if (cfun
->eh
->region_tree
)
4496 bool changed
= false;
4499 changed
|= unsplit_all_eh ();
4500 changed
|= cleanup_all_empty_eh ();
4504 free_dominance_info (CDI_DOMINATORS
);
4505 free_dominance_info (CDI_POST_DOMINATORS
);
4507 /* We delayed all basic block deletion, as we may have performed
4508 cleanups on EH edges while non-EH edges were still present. */
4509 delete_unreachable_blocks ();
4511 /* We manipulated the landing pads. Remove any region that no
4512 longer has a landing pad. */
4513 remove_unreachable_handlers_no_lp ();
4515 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
4524 const pass_data pass_data_cleanup_eh
=
4526 GIMPLE_PASS
, /* type */
4527 "ehcleanup", /* name */
4528 OPTGROUP_NONE
, /* optinfo_flags */
4529 TV_TREE_EH
, /* tv_id */
4530 PROP_gimple_lcf
, /* properties_required */
4531 0, /* properties_provided */
4532 0, /* properties_destroyed */
4533 0, /* todo_flags_start */
4534 0, /* todo_flags_finish */
4537 class pass_cleanup_eh
: public gimple_opt_pass
4540 pass_cleanup_eh (gcc::context
*ctxt
)
4541 : gimple_opt_pass (pass_data_cleanup_eh
, ctxt
)
4544 /* opt_pass methods: */
4545 opt_pass
* clone () { return new pass_cleanup_eh (m_ctxt
); }
4546 virtual bool gate (function
*fun
)
4548 return fun
->eh
!= NULL
&& fun
->eh
->region_tree
!= NULL
;
4551 virtual unsigned int execute (function
*);
4553 }; // class pass_cleanup_eh
4556 pass_cleanup_eh::execute (function
*fun
)
4558 int ret
= execute_cleanup_eh_1 ();
4560 /* If the function no longer needs an EH personality routine
4561 clear it. This exposes cross-language inlining opportunities
4562 and avoids references to a never defined personality routine. */
4563 if (DECL_FUNCTION_PERSONALITY (current_function_decl
)
4564 && function_needs_eh_personality (fun
) != eh_personality_lang
)
4565 DECL_FUNCTION_PERSONALITY (current_function_decl
) = NULL_TREE
;
4573 make_pass_cleanup_eh (gcc::context
*ctxt
)
4575 return new pass_cleanup_eh (ctxt
);
4578 /* Verify that BB containing STMT as the last statement, has precisely the
4579 edge that make_eh_edges would create. */
4582 verify_eh_edges (gimple stmt
)
4584 basic_block bb
= gimple_bb (stmt
);
4585 eh_landing_pad lp
= NULL
;
4590 lp_nr
= lookup_stmt_eh_lp (stmt
);
4592 lp
= get_eh_landing_pad_from_number (lp_nr
);
4595 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4597 if (e
->flags
& EDGE_EH
)
4601 error ("BB %i has multiple EH edges", bb
->index
);
4613 error ("BB %i can not throw but has an EH edge", bb
->index
);
4619 if (!stmt_could_throw_p (stmt
))
4621 error ("BB %i last statement has incorrectly set lp", bb
->index
);
4625 if (eh_edge
== NULL
)
4627 error ("BB %i is missing an EH edge", bb
->index
);
4631 if (eh_edge
->dest
!= label_to_block (lp
->post_landing_pad
))
4633 error ("Incorrect EH edge %i->%i", bb
->index
, eh_edge
->dest
->index
);
4640 /* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
4643 verify_eh_dispatch_edge (gimple stmt
)
4647 basic_block src
, dst
;
4648 bool want_fallthru
= true;
4652 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
4653 src
= gimple_bb (stmt
);
4655 FOR_EACH_EDGE (e
, ei
, src
->succs
)
4656 gcc_assert (e
->aux
== NULL
);
4661 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
4663 dst
= label_to_block (c
->label
);
4664 e
= find_edge (src
, dst
);
4667 error ("BB %i is missing an edge", src
->index
);
4672 /* A catch-all handler doesn't have a fallthru. */
4673 if (c
->type_list
== NULL
)
4675 want_fallthru
= false;
4681 case ERT_ALLOWED_EXCEPTIONS
:
4682 dst
= label_to_block (r
->u
.allowed
.label
);
4683 e
= find_edge (src
, dst
);
4686 error ("BB %i is missing an edge", src
->index
);
4697 FOR_EACH_EDGE (e
, ei
, src
->succs
)
4699 if (e
->flags
& EDGE_FALLTHRU
)
4701 if (fall_edge
!= NULL
)
4703 error ("BB %i too many fallthru edges", src
->index
);
4712 error ("BB %i has incorrect edge", src
->index
);
4716 if ((fall_edge
!= NULL
) ^ want_fallthru
)
4718 error ("BB %i has incorrect fallthru edge", src
->index
);