1 /* Exception handling semantics and decomposition for trees.
2 Copyright (C) 2003-2023 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"
28 #include "tree-pass.h"
31 #include "diagnostic-core.h"
32 #include "fold-const.h"
36 #include "cfgcleanup.h"
38 #include "gimple-iterator.h"
40 #include "tree-into-ssa.h"
42 #include "tree-inline.h"
43 #include "langhooks.h"
45 #include "gimple-low.h"
46 #include "stringpool.h"
51 /* In some instances a tree and a gimple need to be stored in a same table,
52 i.e. in hash tables. This is a structure to do this. */
53 typedef union {tree
*tp
; tree t
; gimple
*g
;} treemple
;
55 /* Misc functions used in this file. */
57 /* Remember and lookup EH landing pad data for arbitrary statements.
58 Really this means any statement that could_throw_p. We could
59 stuff this information into the stmt_ann data structure, but:
61 (1) We absolutely rely on this information being kept until
62 we get to rtl. Once we're done with lowering here, if we lose
63 the information there's no way to recover it!
65 (2) There are many more statements that *cannot* throw as
66 compared to those that can. We should be saving some amount
67 of space by only allocating memory for those that can throw. */
69 /* Add statement T in function IFUN to landing pad NUM. */
72 add_stmt_to_eh_lp_fn (struct function
*ifun
, gimple
*t
, int num
)
74 gcc_assert (num
!= 0);
76 if (!get_eh_throw_stmt_table (ifun
))
77 set_eh_throw_stmt_table (ifun
, hash_map
<gimple
*, int>::create_ggc (31));
79 gcc_assert (!get_eh_throw_stmt_table (ifun
)->put (t
, num
));
82 /* Add statement T in the current function (cfun) to EH landing pad NUM. */
85 add_stmt_to_eh_lp (gimple
*t
, int num
)
87 add_stmt_to_eh_lp_fn (cfun
, t
, num
);
90 /* Add statement T to the single EH landing pad in REGION. */
93 record_stmt_eh_region (eh_region region
, gimple
*t
)
97 if (region
->type
== ERT_MUST_NOT_THROW
)
98 add_stmt_to_eh_lp_fn (cfun
, t
, -region
->index
);
101 eh_landing_pad lp
= region
->landing_pads
;
103 lp
= gen_eh_landing_pad (region
);
105 gcc_assert (lp
->next_lp
== NULL
);
106 add_stmt_to_eh_lp_fn (cfun
, t
, lp
->index
);
111 /* Remove statement T in function IFUN from its EH landing pad. */
114 remove_stmt_from_eh_lp_fn (struct function
*ifun
, gimple
*t
)
116 if (!get_eh_throw_stmt_table (ifun
))
119 if (!get_eh_throw_stmt_table (ifun
)->get (t
))
122 get_eh_throw_stmt_table (ifun
)->remove (t
);
127 /* Remove statement T in the current function (cfun) from its
131 remove_stmt_from_eh_lp (gimple
*t
)
133 return remove_stmt_from_eh_lp_fn (cfun
, t
);
136 /* Determine if statement T is inside an EH region in function IFUN.
137 Positive numbers indicate a landing pad index; negative numbers
138 indicate a MUST_NOT_THROW region index; zero indicates that the
139 statement is not recorded in the region table. */
142 lookup_stmt_eh_lp_fn (struct function
*ifun
, const gimple
*t
)
144 if (ifun
->eh
->throw_stmt_table
== NULL
)
147 int *lp_nr
= ifun
->eh
->throw_stmt_table
->get (const_cast <gimple
*> (t
));
148 return lp_nr
? *lp_nr
: 0;
151 /* Likewise, but always use the current function. */
154 lookup_stmt_eh_lp (const gimple
*t
)
156 /* We can get called from initialized data when -fnon-call-exceptions
157 is on; prevent crash. */
160 return lookup_stmt_eh_lp_fn (cfun
, t
);
163 /* First pass of EH node decomposition. Build up a tree of GIMPLE_TRY_FINALLY
164 nodes and LABEL_DECL nodes. We will use this during the second phase to
165 determine if a goto leaves the body of a TRY_FINALLY_EXPR node. */
167 struct finally_tree_node
169 /* When storing a GIMPLE_TRY, we have to record a gimple. However
170 when deciding whether a GOTO to a certain LABEL_DECL (which is a
171 tree) leaves the TRY block, its necessary to record a tree in
172 this field. Thus a treemple is used. */
177 /* Hashtable helpers. */
179 struct finally_tree_hasher
: free_ptr_hash
<finally_tree_node
>
181 static inline hashval_t
hash (const finally_tree_node
*);
182 static inline bool equal (const finally_tree_node
*,
183 const finally_tree_node
*);
187 finally_tree_hasher::hash (const finally_tree_node
*v
)
189 return (intptr_t)v
->child
.t
>> 4;
193 finally_tree_hasher::equal (const finally_tree_node
*v
,
194 const finally_tree_node
*c
)
196 return v
->child
.t
== c
->child
.t
;
199 /* Note that this table is *not* marked GTY. It is short-lived. */
200 static hash_table
<finally_tree_hasher
> *finally_tree
;
203 record_in_finally_tree (treemple child
, gtry
*parent
)
205 struct finally_tree_node
*n
;
206 finally_tree_node
**slot
;
208 n
= XNEW (struct finally_tree_node
);
212 slot
= finally_tree
->find_slot (n
, INSERT
);
218 collect_finally_tree (gimple
*stmt
, gtry
*region
);
220 /* Go through the gimple sequence. Works with collect_finally_tree to
221 record all GIMPLE_LABEL and GIMPLE_TRY statements. */
224 collect_finally_tree_1 (gimple_seq seq
, gtry
*region
)
226 gimple_stmt_iterator gsi
;
228 for (gsi
= gsi_start (seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
229 collect_finally_tree (gsi_stmt (gsi
), region
);
233 collect_finally_tree (gimple
*stmt
, gtry
*region
)
237 switch (gimple_code (stmt
))
240 temp
.t
= gimple_label_label (as_a
<glabel
*> (stmt
));
241 record_in_finally_tree (temp
, region
);
245 if (gimple_try_kind (stmt
) == GIMPLE_TRY_FINALLY
)
248 record_in_finally_tree (temp
, region
);
249 collect_finally_tree_1 (gimple_try_eval (stmt
),
250 as_a
<gtry
*> (stmt
));
251 collect_finally_tree_1 (gimple_try_cleanup (stmt
), region
);
253 else if (gimple_try_kind (stmt
) == GIMPLE_TRY_CATCH
)
255 collect_finally_tree_1 (gimple_try_eval (stmt
), region
);
256 collect_finally_tree_1 (gimple_try_cleanup (stmt
), region
);
261 collect_finally_tree_1 (gimple_catch_handler (
262 as_a
<gcatch
*> (stmt
)),
266 case GIMPLE_EH_FILTER
:
267 collect_finally_tree_1 (gimple_eh_filter_failure (stmt
), region
);
272 geh_else
*eh_else_stmt
= as_a
<geh_else
*> (stmt
);
273 collect_finally_tree_1 (gimple_eh_else_n_body (eh_else_stmt
), region
);
274 collect_finally_tree_1 (gimple_eh_else_e_body (eh_else_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 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
;
360 /* Outer non-clean up region. */
361 eh_region outer_non_cleanup
;
366 /* Pointer to the GIMPLE_TRY_FINALLY node under discussion. The
367 try_finally_expr is the original GIMPLE_TRY_FINALLY. We need to retain
368 this so that outside_finally_tree can reliably reference the tree used
369 in the collect_finally_tree data structures. */
370 gtry
*try_finally_expr
;
373 /* While lowering a top_p usually it is expanded into multiple statements,
374 thus we need the following field to store them. */
375 gimple_seq top_p_seq
;
377 /* The state outside this try_finally node. */
378 struct leh_state
*outer
;
380 /* The exception region created for it. */
383 /* The goto queue. */
384 struct goto_queue_node
*goto_queue
;
385 size_t goto_queue_size
;
386 size_t goto_queue_active
;
388 /* Pointer map to help in searching goto_queue when it is large. */
389 hash_map
<gimple
*, goto_queue_node
*> *goto_queue_map
;
391 /* The set of unique labels seen as entries in the goto queue. */
392 vec
<tree
> dest_array
;
394 /* A label to be added at the end of the completed transformed
395 sequence. It will be set if may_fallthru was true *at one time*,
396 though subsequent transformations may have cleared that flag. */
399 /* True if it is possible to fall out the bottom of the try block.
400 Cleared if the fallthru is converted to a goto. */
403 /* True if any entry in goto_queue is a GIMPLE_RETURN. */
406 /* True if the finally block can receive an exception edge.
407 Cleared if the exception case is handled by code duplication. */
411 static gimple_seq
lower_eh_must_not_throw (struct leh_state
*, gtry
*);
413 /* Search for STMT in the goto queue. Return the replacement,
414 or null if the statement isn't in the queue. */
416 #define LARGE_GOTO_QUEUE 20
418 static void lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*seq
);
421 find_goto_replacement (struct leh_tf_state
*tf
, treemple stmt
)
425 if (tf
->goto_queue_active
< LARGE_GOTO_QUEUE
)
427 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
428 if ( tf
->goto_queue
[i
].stmt
.g
== stmt
.g
)
429 return tf
->goto_queue
[i
].repl_stmt
;
433 /* If we have a large number of entries in the goto_queue, create a
434 pointer map and use that for searching. */
436 if (!tf
->goto_queue_map
)
438 tf
->goto_queue_map
= new hash_map
<gimple
*, goto_queue_node
*>;
439 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
441 bool existed
= tf
->goto_queue_map
->put (tf
->goto_queue
[i
].stmt
.g
,
443 gcc_assert (!existed
);
447 goto_queue_node
**slot
= tf
->goto_queue_map
->get (stmt
.g
);
449 return ((*slot
)->repl_stmt
);
454 /* A subroutine of replace_goto_queue_1. Handles the sub-clauses of a
455 lowered GIMPLE_COND. If, by chance, the replacement is a simple goto,
456 then we can just splat it in, otherwise we add the new stmts immediately
457 after the GIMPLE_COND and redirect. */
460 replace_goto_queue_cond_clause (tree
*tp
, struct leh_tf_state
*tf
,
461 gimple_stmt_iterator
*gsi
)
466 location_t loc
= gimple_location (gsi_stmt (*gsi
));
469 new_seq
= find_goto_replacement (tf
, temp
);
473 if (gimple_seq_singleton_p (new_seq
)
474 && gimple_code (gimple_seq_first_stmt (new_seq
)) == GIMPLE_GOTO
)
476 *tp
= gimple_goto_dest (gimple_seq_first_stmt (new_seq
));
480 label
= create_artificial_label (loc
);
481 /* Set the new label for the GIMPLE_COND */
484 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
485 gsi_insert_seq_after (gsi
, gimple_seq_copy (new_seq
), GSI_CONTINUE_LINKING
);
488 /* The real work of replace_goto_queue. Returns with TSI updated to
489 point to the next statement. */
491 static void replace_goto_queue_stmt_list (gimple_seq
*, struct leh_tf_state
*);
494 replace_goto_queue_1 (gimple
*stmt
, struct leh_tf_state
*tf
,
495 gimple_stmt_iterator
*gsi
)
501 switch (gimple_code (stmt
))
506 seq
= find_goto_replacement (tf
, temp
);
509 gimple_stmt_iterator i
;
510 seq
= gimple_seq_copy (seq
);
511 for (i
= gsi_start (seq
); !gsi_end_p (i
); gsi_next (&i
))
512 gimple_set_location (gsi_stmt (i
), gimple_location (stmt
));
513 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
514 gsi_remove (gsi
, false);
520 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 2), tf
, gsi
);
521 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 3), tf
, gsi
);
525 replace_goto_queue_stmt_list (gimple_try_eval_ptr (stmt
), tf
);
526 replace_goto_queue_stmt_list (gimple_try_cleanup_ptr (stmt
), tf
);
529 replace_goto_queue_stmt_list (gimple_catch_handler_ptr (
530 as_a
<gcatch
*> (stmt
)),
533 case GIMPLE_EH_FILTER
:
534 replace_goto_queue_stmt_list (gimple_eh_filter_failure_ptr (stmt
), tf
);
538 geh_else
*eh_else_stmt
= as_a
<geh_else
*> (stmt
);
539 replace_goto_queue_stmt_list (gimple_eh_else_n_body_ptr (eh_else_stmt
),
541 replace_goto_queue_stmt_list (gimple_eh_else_e_body_ptr (eh_else_stmt
),
547 /* These won't have gotos in them. */
554 /* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
557 replace_goto_queue_stmt_list (gimple_seq
*seq
, struct leh_tf_state
*tf
)
559 gimple_stmt_iterator gsi
= gsi_start (*seq
);
561 while (!gsi_end_p (gsi
))
562 replace_goto_queue_1 (gsi_stmt (gsi
), tf
, &gsi
);
565 /* Replace all goto queue members. */
568 replace_goto_queue (struct leh_tf_state
*tf
)
570 if (tf
->goto_queue_active
== 0)
572 replace_goto_queue_stmt_list (&tf
->top_p_seq
, tf
);
573 replace_goto_queue_stmt_list (&eh_seq
, tf
);
576 /* Add a new record to the goto queue contained in TF. NEW_STMT is the
577 data to be added, IS_LABEL indicates whether NEW_STMT is a label or
581 record_in_goto_queue (struct leh_tf_state
*tf
,
588 struct goto_queue_node
*q
;
590 gcc_assert (!tf
->goto_queue_map
);
592 active
= tf
->goto_queue_active
;
593 size
= tf
->goto_queue_size
;
596 size
= (size
? size
* 2 : 32);
597 tf
->goto_queue_size
= size
;
599 = XRESIZEVEC (struct goto_queue_node
, tf
->goto_queue
, size
);
602 q
= &tf
->goto_queue
[active
];
603 tf
->goto_queue_active
= active
+ 1;
605 memset (q
, 0, sizeof (*q
));
608 q
->location
= location
;
609 q
->is_label
= is_label
;
612 /* Record the LABEL label in the goto queue contained in TF.
616 record_in_goto_queue_label (struct leh_tf_state
*tf
, treemple stmt
, tree label
,
620 treemple temp
, new_stmt
;
625 /* Computed and non-local gotos do not get processed. Given
626 their nature we can neither tell whether we've escaped the
627 finally block nor redirect them if we knew. */
628 if (TREE_CODE (label
) != LABEL_DECL
)
631 /* No need to record gotos that don't leave the try block. */
633 if (!outside_finally_tree (temp
, tf
->try_finally_expr
))
636 if (! tf
->dest_array
.exists ())
638 tf
->dest_array
.create (10);
639 tf
->dest_array
.quick_push (label
);
644 int n
= tf
->dest_array
.length ();
645 for (index
= 0; index
< n
; ++index
)
646 if (tf
->dest_array
[index
] == label
)
649 tf
->dest_array
.safe_push (label
);
652 /* In the case of a GOTO we want to record the destination label,
653 since with a GIMPLE_COND we have an easy access to the then/else
656 record_in_goto_queue (tf
, new_stmt
, index
, true, location
);
659 /* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
660 node, and if so record that fact in the goto queue associated with that
664 maybe_record_in_goto_queue (struct leh_state
*state
, gimple
*stmt
)
666 struct leh_tf_state
*tf
= state
->tf
;
672 switch (gimple_code (stmt
))
676 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
677 new_stmt
.tp
= gimple_op_ptr (cond_stmt
, 2);
678 record_in_goto_queue_label (tf
, new_stmt
,
679 gimple_cond_true_label (cond_stmt
),
680 EXPR_LOCATION (*new_stmt
.tp
));
681 new_stmt
.tp
= gimple_op_ptr (cond_stmt
, 3);
682 record_in_goto_queue_label (tf
, new_stmt
,
683 gimple_cond_false_label (cond_stmt
),
684 EXPR_LOCATION (*new_stmt
.tp
));
689 record_in_goto_queue_label (tf
, new_stmt
, gimple_goto_dest (stmt
),
690 gimple_location (stmt
));
694 tf
->may_return
= true;
696 record_in_goto_queue (tf
, new_stmt
, -1, false, gimple_location (stmt
));
706 /* We do not process GIMPLE_SWITCHes for now. As long as the original source
707 was in fact structured, and we've not yet done jump threading, then none
708 of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this. */
711 verify_norecord_switch_expr (struct leh_state
*state
,
712 gswitch
*switch_expr
)
714 struct leh_tf_state
*tf
= state
->tf
;
720 n
= gimple_switch_num_labels (switch_expr
);
722 for (i
= 0; i
< n
; ++i
)
725 tree lab
= CASE_LABEL (gimple_switch_label (switch_expr
, i
));
727 gcc_assert (!outside_finally_tree (temp
, tf
->try_finally_expr
));
731 #define verify_norecord_switch_expr(state, switch_expr)
734 /* Redirect a RETURN_EXPR pointed to by Q to FINLAB. If MOD is
735 non-null, insert it before the new branch. */
738 do_return_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
)
742 /* In the case of a return, the queue node must be a gimple statement. */
743 gcc_assert (!q
->is_label
);
745 /* Note that the return value may have already been computed, e.g.,
758 should return 0, not 1. We don't have to do anything to make
759 this happens because the return value has been placed in the
760 RESULT_DECL already. */
762 q
->cont_stmt
= q
->stmt
.g
;
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 /* Similar, but easier, for GIMPLE_GOTO. */
775 do_goto_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
,
776 struct leh_tf_state
*tf
)
780 gcc_assert (q
->is_label
);
782 q
->cont_stmt
= gimple_build_goto (tf
->dest_array
[q
->index
]);
785 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
787 x
= gimple_build_goto (finlab
);
788 gimple_set_location (x
, q
->location
);
789 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
792 /* Emit a standard landing pad sequence into SEQ for REGION. */
795 emit_post_landing_pad (gimple_seq
*seq
, eh_region region
)
797 eh_landing_pad lp
= region
->landing_pads
;
801 lp
= gen_eh_landing_pad (region
);
803 lp
->post_landing_pad
= create_artificial_label (UNKNOWN_LOCATION
);
804 EH_LANDING_PAD_NR (lp
->post_landing_pad
) = lp
->index
;
806 x
= gimple_build_label (lp
->post_landing_pad
);
807 gimple_seq_add_stmt (seq
, x
);
810 /* Emit a RESX statement into SEQ for REGION. */
813 emit_resx (gimple_seq
*seq
, eh_region region
)
815 gresx
*x
= gimple_build_resx (region
->index
);
816 gimple_seq_add_stmt (seq
, x
);
818 record_stmt_eh_region (region
->outer
, x
);
821 /* Note that the current EH region may contain a throw, or a
822 call to a function which itself may contain a throw. */
825 note_eh_region_may_contain_throw (eh_region region
)
827 while (bitmap_set_bit (eh_region_may_contain_throw_map
, region
->index
))
829 if (region
->type
== ERT_MUST_NOT_THROW
)
831 region
= region
->outer
;
837 /* Check if REGION has been marked as containing a throw. If REGION is
838 NULL, this predicate is false. */
841 eh_region_may_contain_throw (eh_region r
)
843 return r
&& bitmap_bit_p (eh_region_may_contain_throw_map
, r
->index
);
846 /* We want to transform
847 try { body; } catch { stuff; }
857 TP is a GIMPLE_TRY node. REGION is the region whose post_landing_pad
858 should be placed before the second operand, or NULL. OVER is
859 an existing label that should be put at the exit, or NULL. */
862 frob_into_branch_around (gtry
*tp
, eh_region region
, tree over
)
865 gimple_seq cleanup
, result
;
866 location_t loc
= gimple_location (tp
);
868 cleanup
= gimple_try_cleanup (tp
);
869 result
= gimple_try_eval (tp
);
872 emit_post_landing_pad (&eh_seq
, region
);
874 if (gimple_seq_may_fallthru (cleanup
))
877 over
= create_artificial_label (loc
);
878 x
= gimple_build_goto (over
);
879 gimple_set_location (x
, loc
);
880 gimple_seq_add_stmt (&cleanup
, x
);
882 gimple_seq_add_seq (&eh_seq
, cleanup
);
886 x
= gimple_build_label (over
);
887 gimple_seq_add_stmt (&result
, x
);
892 /* A subroutine of lower_try_finally. Duplicate the tree rooted at T.
893 Make sure to record all new labels found. */
896 lower_try_finally_dup_block (gimple_seq seq
, struct leh_state
*outer_state
,
901 gimple_stmt_iterator gsi
;
903 new_seq
= copy_gimple_seq_and_replace_locals (seq
);
905 for (gsi
= gsi_start (new_seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
907 gimple
*stmt
= gsi_stmt (gsi
);
908 if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
910 tree block
= gimple_block (stmt
);
911 gimple_set_location (stmt
, loc
);
912 gimple_set_block (stmt
, block
);
917 region
= outer_state
->tf
->try_finally_expr
;
918 collect_finally_tree_1 (new_seq
, region
);
923 /* A subroutine of lower_try_finally. Create a fallthru label for
924 the given try_finally state. The only tricky bit here is that
925 we have to make sure to record the label in our outer context. */
928 lower_try_finally_fallthru_label (struct leh_tf_state
*tf
)
930 tree label
= tf
->fallthru_label
;
935 label
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
936 tf
->fallthru_label
= label
;
940 record_in_finally_tree (temp
, tf
->outer
->tf
->try_finally_expr
);
946 /* A subroutine of lower_try_finally. If FINALLY consits of a
947 GIMPLE_EH_ELSE node, return it. */
949 static inline geh_else
*
950 get_eh_else (gimple_seq finally
)
952 gimple
*x
= gimple_seq_first_stmt (finally
);
953 if (gimple_code (x
) == GIMPLE_EH_ELSE
)
955 gcc_assert (gimple_seq_singleton_p (finally
));
956 return as_a
<geh_else
*> (x
);
961 /* A subroutine of lower_try_finally. If the eh_protect_cleanup_actions
962 langhook returns non-null, then the language requires that the exception
963 path out of a try_finally be treated specially. To wit: the code within
964 the finally block may not itself throw an exception. We have two choices
965 here. First we can duplicate the finally block and wrap it in a
966 must_not_throw region. Second, we can generate code like
971 if (fintmp == eh_edge)
972 protect_cleanup_actions;
975 where "fintmp" is the temporary used in the switch statement generation
976 alternative considered below. For the nonce, we always choose the first
979 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
982 honor_protect_cleanup_actions (struct leh_state
*outer_state
,
983 struct leh_state
*this_state
,
984 struct leh_tf_state
*tf
)
986 gimple_seq finally
= gimple_try_cleanup (tf
->top_p
);
988 /* EH_ELSE doesn't come from user code; only compiler generated stuff.
989 It does need to be handled here, so as to separate the (different)
990 EH path from the normal path. But we should not attempt to wrap
991 it with a must-not-throw node (which indeed gets in the way). */
992 if (geh_else
*eh_else
= get_eh_else (finally
))
994 gimple_try_set_cleanup (tf
->top_p
, gimple_eh_else_n_body (eh_else
));
995 finally
= gimple_eh_else_e_body (eh_else
);
997 /* Let the ELSE see the exception that's being processed, but
998 since the cleanup is outside the try block, process it with
999 outer_state, otherwise it may be used as a cleanup for
1000 itself, and Bad Things (TM) ensue. */
1001 eh_region save_ehp
= outer_state
->ehp_region
;
1002 outer_state
->ehp_region
= this_state
->cur_region
;
1003 lower_eh_constructs_1 (outer_state
, &finally
);
1004 outer_state
->ehp_region
= save_ehp
;
1008 /* First check for nothing to do. */
1009 if (lang_hooks
.eh_protect_cleanup_actions
== NULL
)
1011 tree actions
= lang_hooks
.eh_protect_cleanup_actions ();
1012 if (actions
== NULL
)
1016 finally
= lower_try_finally_dup_block (finally
, outer_state
,
1017 gimple_location (tf
->try_finally_expr
));
1019 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
1020 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
1021 to be in an enclosing scope, but needs to be implemented at this level
1022 to avoid a nesting violation (see wrap_temporary_cleanups in
1023 cp/decl.cc). Since it's logically at an outer level, we should call
1024 terminate before we get to it, so strip it away before adding the
1025 MUST_NOT_THROW filter. */
1026 gimple_stmt_iterator gsi
= gsi_start (finally
);
1027 gimple
*x
= gsi_stmt (gsi
);
1028 if (gimple_code (x
) == GIMPLE_TRY
1029 && gimple_try_kind (x
) == GIMPLE_TRY_CATCH
1030 && gimple_try_catch_is_cleanup (x
))
1032 gsi_insert_seq_before (&gsi
, gimple_try_eval (x
), GSI_SAME_STMT
);
1033 gsi_remove (&gsi
, false);
1036 /* Wrap the block with protect_cleanup_actions as the action. */
1037 geh_mnt
*eh_mnt
= gimple_build_eh_must_not_throw (actions
);
1038 gtry
*try_stmt
= gimple_build_try (finally
,
1039 gimple_seq_alloc_with_stmt (eh_mnt
),
1041 finally
= lower_eh_must_not_throw (outer_state
, try_stmt
);
1044 /* Drop all of this into the exception sequence. */
1045 emit_post_landing_pad (&eh_seq
, tf
->region
);
1046 gimple_seq_add_seq (&eh_seq
, finally
);
1047 if (gimple_seq_may_fallthru (finally
))
1048 emit_resx (&eh_seq
, tf
->region
);
1050 /* Having now been handled, EH isn't to be considered with
1051 the rest of the outgoing edges. */
1052 tf
->may_throw
= false;
1055 /* A subroutine of lower_try_finally. We have determined that there is
1056 no fallthru edge out of the finally block. This means that there is
1057 no outgoing edge corresponding to any incoming edge. Restructure the
1058 try_finally node for this special case. */
1061 lower_try_finally_nofallthru (struct leh_state
*state
,
1062 struct leh_tf_state
*tf
)
1068 struct goto_queue_node
*q
, *qe
;
1070 lab
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
1072 /* We expect that tf->top_p is a GIMPLE_TRY. */
1073 finally
= gimple_try_cleanup (tf
->top_p
);
1074 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1076 x
= gimple_build_label (lab
);
1077 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1080 qe
= q
+ tf
->goto_queue_active
;
1083 do_return_redirection (q
, lab
, NULL
);
1085 do_goto_redirection (q
, lab
, NULL
, tf
);
1087 replace_goto_queue (tf
);
1089 /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1090 eh_else
= get_eh_else (finally
);
1093 finally
= gimple_eh_else_n_body (eh_else
);
1094 lower_eh_constructs_1 (state
, &finally
);
1095 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1099 finally
= gimple_eh_else_e_body (eh_else
);
1100 lower_eh_constructs_1 (state
, &finally
);
1102 emit_post_landing_pad (&eh_seq
, tf
->region
);
1103 gimple_seq_add_seq (&eh_seq
, finally
);
1108 lower_eh_constructs_1 (state
, &finally
);
1109 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1113 emit_post_landing_pad (&eh_seq
, tf
->region
);
1115 x
= gimple_build_goto (lab
);
1116 gimple_set_location (x
, gimple_location (tf
->try_finally_expr
));
1117 gimple_seq_add_stmt (&eh_seq
, x
);
1122 /* A subroutine of lower_try_finally. We have determined that there is
1123 exactly one destination of the finally block. Restructure the
1124 try_finally node for this special case. */
1127 lower_try_finally_onedest (struct leh_state
*state
, struct leh_tf_state
*tf
)
1129 struct goto_queue_node
*q
, *qe
;
1134 gimple_stmt_iterator gsi
;
1136 location_t loc
= gimple_location (tf
->try_finally_expr
);
1138 finally
= gimple_try_cleanup (tf
->top_p
);
1139 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1141 /* Since there's only one destination, and the destination edge can only
1142 either be EH or non-EH, that implies that all of our incoming edges
1143 are of the same type. Therefore we can lower EH_ELSE immediately. */
1144 eh_else
= get_eh_else (finally
);
1148 finally
= gimple_eh_else_e_body (eh_else
);
1150 finally
= gimple_eh_else_n_body (eh_else
);
1153 lower_eh_constructs_1 (state
, &finally
);
1155 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1157 gimple
*stmt
= gsi_stmt (gsi
);
1158 if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
1160 tree block
= gimple_block (stmt
);
1161 gimple_set_location (stmt
, gimple_location (tf
->try_finally_expr
));
1162 gimple_set_block (stmt
, block
);
1168 /* Only reachable via the exception edge. Add the given label to
1169 the head of the FINALLY block. Append a RESX at the end. */
1170 emit_post_landing_pad (&eh_seq
, tf
->region
);
1171 gimple_seq_add_seq (&eh_seq
, finally
);
1172 emit_resx (&eh_seq
, tf
->region
);
1176 if (tf
->may_fallthru
)
1178 /* Only reachable via the fallthru edge. Do nothing but let
1179 the two blocks run together; we'll fall out the bottom. */
1180 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1184 finally_label
= create_artificial_label (loc
);
1185 label_stmt
= gimple_build_label (finally_label
);
1186 gimple_seq_add_stmt (&tf
->top_p_seq
, label_stmt
);
1188 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1191 qe
= q
+ tf
->goto_queue_active
;
1195 /* Reachable by return expressions only. Redirect them. */
1197 do_return_redirection (q
, finally_label
, NULL
);
1198 replace_goto_queue (tf
);
1202 /* Reachable by goto expressions only. Redirect them. */
1204 do_goto_redirection (q
, finally_label
, NULL
, tf
);
1205 replace_goto_queue (tf
);
1207 if (tf
->dest_array
[0] == tf
->fallthru_label
)
1209 /* Reachable by goto to fallthru label only. Redirect it
1210 to the new label (already created, sadly), and do not
1211 emit the final branch out, or the fallthru label. */
1212 tf
->fallthru_label
= NULL
;
1217 /* Place the original return/goto to the original destination
1218 immediately after the finally block. */
1219 x
= tf
->goto_queue
[0].cont_stmt
;
1220 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1221 maybe_record_in_goto_queue (state
, x
);
1224 /* A subroutine of lower_try_finally. There are multiple edges incoming
1225 and outgoing from the finally block. Implement this by duplicating the
1226 finally block for every destination. */
1229 lower_try_finally_copy (struct leh_state
*state
, struct leh_tf_state
*tf
)
1232 gimple_seq new_stmt
;
1237 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1239 finally
= gimple_try_cleanup (tf
->top_p
);
1241 /* Notice EH_ELSE, and simplify some of the remaining code
1242 by considering FINALLY to be the normal return path only. */
1243 eh_else
= get_eh_else (finally
);
1245 finally
= gimple_eh_else_n_body (eh_else
);
1247 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1250 if (tf
->may_fallthru
)
1252 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1253 lower_eh_constructs_1 (state
, &seq
);
1254 gimple_seq_add_seq (&new_stmt
, seq
);
1256 tmp
= lower_try_finally_fallthru_label (tf
);
1257 x
= gimple_build_goto (tmp
);
1258 gimple_set_location (x
, tf_loc
);
1259 gimple_seq_add_stmt (&new_stmt
, x
);
1264 /* We don't need to copy the EH path of EH_ELSE,
1265 since it is only emitted once. */
1267 seq
= gimple_eh_else_e_body (eh_else
);
1269 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1270 lower_eh_constructs_1 (state
, &seq
);
1272 emit_post_landing_pad (&eh_seq
, tf
->region
);
1273 gimple_seq_add_seq (&eh_seq
, seq
);
1274 emit_resx (&eh_seq
, tf
->region
);
1279 struct goto_queue_node
*q
, *qe
;
1280 int return_index
, index
;
1283 struct goto_queue_node
*q
;
1287 return_index
= tf
->dest_array
.length ();
1288 labels
= XCNEWVEC (struct labels_s
, return_index
+ 1);
1291 qe
= q
+ tf
->goto_queue_active
;
1294 index
= q
->index
< 0 ? return_index
: q
->index
;
1296 if (!labels
[index
].q
)
1297 labels
[index
].q
= q
;
1300 for (index
= 0; index
< return_index
+ 1; index
++)
1304 q
= labels
[index
].q
;
1308 lab
= labels
[index
].label
1309 = create_artificial_label (tf_loc
);
1311 if (index
== return_index
)
1312 do_return_redirection (q
, lab
, NULL
);
1314 do_goto_redirection (q
, lab
, NULL
, tf
);
1316 x
= gimple_build_label (lab
);
1317 gimple_seq_add_stmt (&new_stmt
, x
);
1319 seq
= lower_try_finally_dup_block (finally
, state
, q
->location
);
1320 lower_eh_constructs_1 (state
, &seq
);
1321 gimple_seq_add_seq (&new_stmt
, seq
);
1323 gimple_seq_add_stmt (&new_stmt
, q
->cont_stmt
);
1324 maybe_record_in_goto_queue (state
, q
->cont_stmt
);
1327 for (q
= tf
->goto_queue
; q
< qe
; q
++)
1331 index
= q
->index
< 0 ? return_index
: q
->index
;
1333 if (labels
[index
].q
== q
)
1336 lab
= labels
[index
].label
;
1338 if (index
== return_index
)
1339 do_return_redirection (q
, lab
, NULL
);
1341 do_goto_redirection (q
, lab
, NULL
, tf
);
1344 replace_goto_queue (tf
);
1348 /* Need to link new stmts after running replace_goto_queue due
1349 to not wanting to process the same goto stmts twice. */
1350 gimple_seq_add_seq (&tf
->top_p_seq
, new_stmt
);
1353 /* A subroutine of lower_try_finally. There are multiple edges incoming
1354 and outgoing from the finally block. Implement this by instrumenting
1355 each incoming edge and creating a switch statement at the end of the
1356 finally block that branches to the appropriate destination. */
1359 lower_try_finally_switch (struct leh_state
*state
, struct leh_tf_state
*tf
)
1361 struct goto_queue_node
*q
, *qe
;
1362 tree finally_tmp
, finally_label
;
1363 int return_index
, eh_index
, fallthru_index
;
1364 int nlabels
, ndests
, j
, last_case_index
;
1366 auto_vec
<tree
> case_label_vec
;
1367 gimple_seq switch_body
= NULL
;
1371 gimple
*switch_stmt
;
1373 hash_map
<tree
, gimple
*> *cont_map
= NULL
;
1374 /* The location of the TRY_FINALLY stmt. */
1375 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1376 /* The location of the finally block. */
1377 location_t finally_loc
;
1379 finally
= gimple_try_cleanup (tf
->top_p
);
1380 eh_else
= get_eh_else (finally
);
1382 /* Mash the TRY block to the head of the chain. */
1383 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1385 /* The location of the finally is either the last stmt in the finally
1386 block or the location of the TRY_FINALLY itself. */
1387 x
= gimple_seq_last_stmt (finally
);
1388 finally_loc
= x
? gimple_location (x
) : tf_loc
;
1390 /* Prepare for switch statement generation. */
1391 nlabels
= tf
->dest_array
.length ();
1392 return_index
= nlabels
;
1393 eh_index
= return_index
+ tf
->may_return
;
1394 fallthru_index
= eh_index
+ (tf
->may_throw
&& !eh_else
);
1395 ndests
= fallthru_index
+ tf
->may_fallthru
;
1397 finally_tmp
= create_tmp_var (integer_type_node
, "finally_tmp");
1398 finally_label
= create_artificial_label (finally_loc
);
1400 /* We use vec::quick_push on case_label_vec throughout this function,
1401 since we know the size in advance and allocate precisely as muce
1403 case_label_vec
.create (ndests
);
1405 last_case_index
= 0;
1407 /* Begin inserting code for getting to the finally block. Things
1408 are done in this order to correspond to the sequence the code is
1411 if (tf
->may_fallthru
)
1413 x
= gimple_build_assign (finally_tmp
,
1414 build_int_cst (integer_type_node
,
1416 gimple_set_location (x
, finally_loc
);
1417 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1419 tmp
= build_int_cst (integer_type_node
, fallthru_index
);
1420 last_case
= build_case_label (tmp
, NULL
,
1421 create_artificial_label (finally_loc
));
1422 case_label_vec
.quick_push (last_case
);
1425 x
= gimple_build_label (CASE_LABEL (last_case
));
1426 gimple_seq_add_stmt (&switch_body
, x
);
1428 tmp
= lower_try_finally_fallthru_label (tf
);
1429 x
= gimple_build_goto (tmp
);
1430 gimple_set_location (x
, finally_loc
);
1431 gimple_seq_add_stmt (&switch_body
, x
);
1434 /* For EH_ELSE, emit the exception path (plus resx) now, then
1435 subsequently we only need consider the normal path. */
1440 finally
= gimple_eh_else_e_body (eh_else
);
1441 lower_eh_constructs_1 (state
, &finally
);
1443 emit_post_landing_pad (&eh_seq
, tf
->region
);
1444 gimple_seq_add_seq (&eh_seq
, finally
);
1445 emit_resx (&eh_seq
, tf
->region
);
1448 finally
= gimple_eh_else_n_body (eh_else
);
1450 else if (tf
->may_throw
)
1452 emit_post_landing_pad (&eh_seq
, tf
->region
);
1454 x
= gimple_build_assign (finally_tmp
,
1455 build_int_cst (integer_type_node
, eh_index
));
1456 gimple_seq_add_stmt (&eh_seq
, x
);
1458 x
= gimple_build_goto (finally_label
);
1459 gimple_set_location (x
, tf_loc
);
1460 gimple_seq_add_stmt (&eh_seq
, x
);
1462 tmp
= build_int_cst (integer_type_node
, eh_index
);
1463 last_case
= build_case_label (tmp
, NULL
,
1464 create_artificial_label (tf_loc
));
1465 case_label_vec
.quick_push (last_case
);
1468 x
= gimple_build_label (CASE_LABEL (last_case
));
1469 gimple_seq_add_stmt (&eh_seq
, x
);
1470 emit_resx (&eh_seq
, tf
->region
);
1473 x
= gimple_build_label (finally_label
);
1474 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1476 lower_eh_constructs_1 (state
, &finally
);
1477 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1479 /* Redirect each incoming goto edge. */
1481 qe
= q
+ tf
->goto_queue_active
;
1482 j
= last_case_index
+ tf
->may_return
;
1483 /* Prepare the assignments to finally_tmp that are executed upon the
1484 entrance through a particular edge. */
1487 gimple_seq mod
= NULL
;
1489 unsigned int case_index
;
1493 x
= gimple_build_assign (finally_tmp
,
1494 build_int_cst (integer_type_node
,
1496 gimple_seq_add_stmt (&mod
, x
);
1497 do_return_redirection (q
, finally_label
, mod
);
1498 switch_id
= return_index
;
1502 x
= gimple_build_assign (finally_tmp
,
1503 build_int_cst (integer_type_node
, q
->index
));
1504 gimple_seq_add_stmt (&mod
, x
);
1505 do_goto_redirection (q
, finally_label
, mod
, tf
);
1506 switch_id
= q
->index
;
1509 case_index
= j
+ q
->index
;
1510 if (case_label_vec
.length () <= case_index
|| !case_label_vec
[case_index
])
1513 tmp
= build_int_cst (integer_type_node
, switch_id
);
1514 case_lab
= build_case_label (tmp
, NULL
,
1515 create_artificial_label (tf_loc
));
1516 /* We store the cont_stmt in the pointer map, so that we can recover
1517 it in the loop below. */
1519 cont_map
= new hash_map
<tree
, gimple
*>;
1520 cont_map
->put (case_lab
, q
->cont_stmt
);
1521 case_label_vec
.quick_push (case_lab
);
1524 for (j
= last_case_index
; j
< last_case_index
+ nlabels
; j
++)
1528 last_case
= case_label_vec
[j
];
1530 gcc_assert (last_case
);
1531 gcc_assert (cont_map
);
1533 cont_stmt
= *cont_map
->get (last_case
);
1535 x
= gimple_build_label (CASE_LABEL (last_case
));
1536 gimple_seq_add_stmt (&switch_body
, x
);
1537 gimple_seq_add_stmt (&switch_body
, cont_stmt
);
1538 maybe_record_in_goto_queue (state
, cont_stmt
);
1543 replace_goto_queue (tf
);
1545 /* Make sure that the last case is the default label, as one is required.
1546 Then sort the labels, which is also required in GIMPLE. */
1547 CASE_LOW (last_case
) = NULL
;
1548 tree tem
= case_label_vec
.pop ();
1549 gcc_assert (tem
== last_case
);
1550 sort_case_labels (case_label_vec
);
1552 /* Build the switch statement, setting last_case to be the default
1554 switch_stmt
= gimple_build_switch (finally_tmp
, last_case
,
1556 gimple_set_location (switch_stmt
, finally_loc
);
1558 /* Need to link SWITCH_STMT after running replace_goto_queue
1559 due to not wanting to process the same goto stmts twice. */
1560 gimple_seq_add_stmt (&tf
->top_p_seq
, switch_stmt
);
1561 gimple_seq_add_seq (&tf
->top_p_seq
, switch_body
);
1564 /* Decide whether or not we are going to duplicate the finally block.
1565 There are several considerations.
1567 Second, we'd like to prevent egregious code growth. One way to
1568 do this is to estimate the size of the finally block, multiply
1569 that by the number of copies we'd need to make, and compare against
1570 the estimate of the size of the switch machinery we'd have to add. */
1573 decide_copy_try_finally (int ndests
, bool may_throw
, gimple_seq finally
)
1575 int f_estimate
, sw_estimate
;
1578 /* If there's an EH_ELSE involved, the exception path is separate
1579 and really doesn't come into play for this computation. */
1580 eh_else
= get_eh_else (finally
);
1583 ndests
-= may_throw
;
1584 finally
= gimple_eh_else_n_body (eh_else
);
1589 gimple_stmt_iterator gsi
;
1594 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1596 /* Duplicate __builtin_stack_restore in the hope of eliminating it
1597 on the EH paths and, consequently, useless cleanups. */
1598 gimple
*stmt
= gsi_stmt (gsi
);
1599 if (!is_gimple_debug (stmt
)
1600 && !gimple_clobber_p (stmt
)
1601 && !gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
1607 /* Finally estimate N times, plus N gotos. */
1608 f_estimate
= estimate_num_insns_seq (finally
, &eni_size_weights
);
1609 f_estimate
= (f_estimate
+ 1) * ndests
;
1611 /* Switch statement (cost 10), N variable assignments, N gotos. */
1612 sw_estimate
= 10 + 2 * ndests
;
1614 /* Optimize for size clearly wants our best guess. */
1615 if (optimize_function_for_size_p (cfun
))
1616 return f_estimate
< sw_estimate
;
1618 /* ??? These numbers are completely made up so far. */
1620 return f_estimate
< 100 || f_estimate
< sw_estimate
* 2;
1622 return f_estimate
< 40 || f_estimate
* 2 < sw_estimate
* 3;
1625 /* REG is current region of a LEH state.
1626 is the enclosing region for a possible cleanup region, or the region
1627 itself. Returns TRUE if such a region would be unreachable.
1629 Cleanup regions within a must-not-throw region aren't actually reachable
1630 even if there are throwing stmts within them, because the personality
1631 routine will call terminate before unwinding. */
1634 cleanup_is_dead_in (leh_state
*state
)
1638 eh_region reg
= state
->cur_region
;
1639 while (reg
&& reg
->type
== ERT_CLEANUP
)
1642 gcc_assert (reg
== state
->outer_non_cleanup
);
1645 eh_region reg
= state
->outer_non_cleanup
;
1646 return (reg
&& reg
->type
== ERT_MUST_NOT_THROW
);
1649 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
1650 to a sequence of labels and blocks, plus the exception region trees
1651 that record all the magic. This is complicated by the need to
1652 arrange for the FINALLY block to be executed on all exits. */
1655 lower_try_finally (struct leh_state
*state
, gtry
*tp
)
1657 struct leh_tf_state this_tf
;
1658 struct leh_state this_state
;
1660 gimple_seq old_eh_seq
;
1662 /* Process the try block. */
1664 memset (&this_tf
, 0, sizeof (this_tf
));
1665 this_tf
.try_finally_expr
= tp
;
1667 this_tf
.outer
= state
;
1668 if (using_eh_for_cleanups_p () && !cleanup_is_dead_in (state
))
1670 this_tf
.region
= gen_eh_region_cleanup (state
->cur_region
);
1671 this_state
.cur_region
= this_tf
.region
;
1675 this_tf
.region
= NULL
;
1676 this_state
.cur_region
= state
->cur_region
;
1679 this_state
.outer_non_cleanup
= state
->outer_non_cleanup
;
1680 this_state
.ehp_region
= state
->ehp_region
;
1681 this_state
.tf
= &this_tf
;
1683 old_eh_seq
= eh_seq
;
1686 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1688 /* Determine if the try block is escaped through the bottom. */
1689 this_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1691 /* Determine if any exceptions are possible within the try block. */
1693 this_tf
.may_throw
= eh_region_may_contain_throw (this_tf
.region
);
1694 if (this_tf
.may_throw
)
1695 honor_protect_cleanup_actions (state
, &this_state
, &this_tf
);
1697 /* Determine how many edges (still) reach the finally block. Or rather,
1698 how many destinations are reached by the finally block. Use this to
1699 determine how we process the finally block itself. */
1701 ndests
= this_tf
.dest_array
.length ();
1702 ndests
+= this_tf
.may_fallthru
;
1703 ndests
+= this_tf
.may_return
;
1704 ndests
+= this_tf
.may_throw
;
1706 /* If the FINALLY block is not reachable, dike it out. */
1709 gimple_seq_add_seq (&this_tf
.top_p_seq
, gimple_try_eval (tp
));
1710 gimple_try_set_cleanup (tp
, NULL
);
1712 /* If the finally block doesn't fall through, then any destination
1713 we might try to impose there isn't reached either. There may be
1714 some minor amount of cleanup and redirection still needed. */
1715 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp
)))
1716 lower_try_finally_nofallthru (state
, &this_tf
);
1718 /* We can easily special-case redirection to a single destination. */
1719 else if (ndests
== 1)
1720 lower_try_finally_onedest (state
, &this_tf
);
1721 else if (decide_copy_try_finally (ndests
, this_tf
.may_throw
,
1722 gimple_try_cleanup (tp
)))
1723 lower_try_finally_copy (state
, &this_tf
);
1725 lower_try_finally_switch (state
, &this_tf
);
1727 /* If someone requested we add a label at the end of the transformed
1729 if (this_tf
.fallthru_label
)
1731 /* This must be reached only if ndests == 0. */
1732 gimple
*x
= gimple_build_label (this_tf
.fallthru_label
);
1733 gimple_seq_add_stmt (&this_tf
.top_p_seq
, x
);
1736 this_tf
.dest_array
.release ();
1737 free (this_tf
.goto_queue
);
1738 if (this_tf
.goto_queue_map
)
1739 delete this_tf
.goto_queue_map
;
1741 /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1742 If there was no old eh_seq, then the append is trivially already done. */
1746 eh_seq
= old_eh_seq
;
1749 gimple_seq new_eh_seq
= eh_seq
;
1750 eh_seq
= old_eh_seq
;
1751 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1755 return this_tf
.top_p_seq
;
1758 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1759 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1760 exception region trees that records all the magic. */
1763 lower_catch (struct leh_state
*state
, gtry
*tp
)
1765 eh_region try_region
= NULL
;
1766 struct leh_state this_state
= *state
;
1767 gimple_stmt_iterator gsi
;
1769 gimple_seq new_seq
, cleanup
;
1771 geh_dispatch
*eh_dispatch
;
1772 location_t try_catch_loc
= gimple_location (tp
);
1773 location_t catch_loc
= UNKNOWN_LOCATION
;
1775 if (flag_exceptions
)
1777 try_region
= gen_eh_region_try (state
->cur_region
);
1778 this_state
.cur_region
= try_region
;
1779 this_state
.outer_non_cleanup
= this_state
.cur_region
;
1782 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1784 if (!eh_region_may_contain_throw (try_region
))
1785 return gimple_try_eval (tp
);
1788 eh_dispatch
= gimple_build_eh_dispatch (try_region
->index
);
1789 gimple_seq_add_stmt (&new_seq
, eh_dispatch
);
1790 emit_resx (&new_seq
, try_region
);
1792 this_state
.cur_region
= state
->cur_region
;
1793 this_state
.outer_non_cleanup
= state
->outer_non_cleanup
;
1794 this_state
.ehp_region
= try_region
;
1796 /* Add eh_seq from lowering EH in the cleanup sequence after the cleanup
1797 itself, so that e.g. for coverage purposes the nested cleanups don't
1798 appear before the cleanup body. See PR64634 for details. */
1799 gimple_seq old_eh_seq
= eh_seq
;
1803 cleanup
= gimple_try_cleanup (tp
);
1804 for (gsi
= gsi_start (cleanup
);
1812 catch_stmt
= as_a
<gcatch
*> (gsi_stmt (gsi
));
1813 if (catch_loc
== UNKNOWN_LOCATION
)
1814 catch_loc
= gimple_location (catch_stmt
);
1815 c
= gen_eh_region_catch (try_region
, gimple_catch_types (catch_stmt
));
1817 handler
= gimple_catch_handler (catch_stmt
);
1818 lower_eh_constructs_1 (&this_state
, &handler
);
1820 c
->label
= create_artificial_label (UNKNOWN_LOCATION
);
1821 x
= gimple_build_label (c
->label
);
1822 gimple_seq_add_stmt (&new_seq
, x
);
1824 gimple_seq_add_seq (&new_seq
, handler
);
1826 if (gimple_seq_may_fallthru (new_seq
))
1829 out_label
= create_artificial_label (try_catch_loc
);
1831 x
= gimple_build_goto (out_label
);
1832 gimple_seq_add_stmt (&new_seq
, x
);
1838 /* Try to set a location on the dispatching construct to avoid inheriting
1839 the location of the previous statement. */
1840 gimple_set_location (eh_dispatch
, catch_loc
);
1842 gimple_try_set_cleanup (tp
, new_seq
);
1844 gimple_seq new_eh_seq
= eh_seq
;
1845 eh_seq
= old_eh_seq
;
1846 gimple_seq ret_seq
= frob_into_branch_around (tp
, try_region
, out_label
);
1847 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1851 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1852 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1853 region trees that record all the magic. */
1856 lower_eh_filter (struct leh_state
*state
, gtry
*tp
)
1858 struct leh_state this_state
= *state
;
1859 eh_region this_region
= NULL
;
1863 inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1865 if (flag_exceptions
)
1867 this_region
= gen_eh_region_allowed (state
->cur_region
,
1868 gimple_eh_filter_types (inner
));
1869 this_state
.cur_region
= this_region
;
1870 this_state
.outer_non_cleanup
= this_state
.cur_region
;
1873 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1875 if (!eh_region_may_contain_throw (this_region
))
1876 return gimple_try_eval (tp
);
1878 this_state
.cur_region
= state
->cur_region
;
1879 this_state
.ehp_region
= this_region
;
1882 x
= gimple_build_eh_dispatch (this_region
->index
);
1883 gimple_set_location (x
, gimple_location (tp
));
1884 gimple_seq_add_stmt (&new_seq
, x
);
1885 emit_resx (&new_seq
, this_region
);
1887 this_region
->u
.allowed
.label
= create_artificial_label (UNKNOWN_LOCATION
);
1888 x
= gimple_build_label (this_region
->u
.allowed
.label
);
1889 gimple_seq_add_stmt (&new_seq
, x
);
1891 lower_eh_constructs_1 (&this_state
, gimple_eh_filter_failure_ptr (inner
));
1892 gimple_seq_add_seq (&new_seq
, gimple_eh_filter_failure (inner
));
1894 gimple_try_set_cleanup (tp
, new_seq
);
1896 return frob_into_branch_around (tp
, this_region
, NULL
);
1899 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with
1900 an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1901 plus the exception region trees that record all the magic. */
1904 lower_eh_must_not_throw (struct leh_state
*state
, gtry
*tp
)
1906 struct leh_state this_state
= *state
;
1908 if (flag_exceptions
)
1910 gimple
*inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1911 eh_region this_region
;
1913 this_region
= gen_eh_region_must_not_throw (state
->cur_region
);
1914 this_region
->u
.must_not_throw
.failure_decl
1915 = gimple_eh_must_not_throw_fndecl (
1916 as_a
<geh_mnt
*> (inner
));
1917 this_region
->u
.must_not_throw
.failure_loc
1918 = LOCATION_LOCUS (gimple_location (tp
));
1920 /* In order to get mangling applied to this decl, we must mark it
1921 used now. Otherwise, pass_ipa_free_lang_data won't think it
1923 TREE_USED (this_region
->u
.must_not_throw
.failure_decl
) = 1;
1925 this_state
.cur_region
= this_region
;
1926 this_state
.outer_non_cleanup
= this_state
.cur_region
;
1929 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1931 return gimple_try_eval (tp
);
1934 /* Implement a cleanup expression. This is similar to try-finally,
1935 except that we only execute the cleanup block for exception edges. */
1938 lower_cleanup (struct leh_state
*state
, gtry
*tp
)
1940 struct leh_state this_state
= *state
;
1941 eh_region this_region
= NULL
;
1942 struct leh_tf_state fake_tf
;
1944 bool cleanup_dead
= cleanup_is_dead_in (state
);
1946 if (flag_exceptions
&& !cleanup_dead
)
1948 this_region
= gen_eh_region_cleanup (state
->cur_region
);
1949 this_state
.cur_region
= this_region
;
1950 this_state
.outer_non_cleanup
= state
->outer_non_cleanup
;
1953 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1955 if (cleanup_dead
|| !eh_region_may_contain_throw (this_region
))
1956 return gimple_try_eval (tp
);
1958 /* Build enough of a try-finally state so that we can reuse
1959 honor_protect_cleanup_actions. */
1960 memset (&fake_tf
, 0, sizeof (fake_tf
));
1961 fake_tf
.top_p
= fake_tf
.try_finally_expr
= tp
;
1962 fake_tf
.outer
= state
;
1963 fake_tf
.region
= this_region
;
1964 fake_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1965 fake_tf
.may_throw
= true;
1967 honor_protect_cleanup_actions (state
, NULL
, &fake_tf
);
1969 if (fake_tf
.may_throw
)
1971 /* In this case honor_protect_cleanup_actions had nothing to do,
1972 and we should process this normally. */
1973 lower_eh_constructs_1 (state
, gimple_try_cleanup_ptr (tp
));
1974 result
= frob_into_branch_around (tp
, this_region
,
1975 fake_tf
.fallthru_label
);
1979 /* In this case honor_protect_cleanup_actions did nearly all of
1980 the work. All we have left is to append the fallthru_label. */
1982 result
= gimple_try_eval (tp
);
1983 if (fake_tf
.fallthru_label
)
1985 gimple
*x
= gimple_build_label (fake_tf
.fallthru_label
);
1986 gimple_seq_add_stmt (&result
, x
);
1992 /* Main loop for lowering eh constructs. Also moves gsi to the next
1996 lower_eh_constructs_2 (struct leh_state
*state
, gimple_stmt_iterator
*gsi
)
2000 gimple
*stmt
= gsi_stmt (*gsi
);
2002 switch (gimple_code (stmt
))
2006 tree fndecl
= gimple_call_fndecl (stmt
);
2009 if (fndecl
&& fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
2010 switch (DECL_FUNCTION_CODE (fndecl
))
2012 case BUILT_IN_EH_POINTER
:
2013 /* The front end may have generated a call to
2014 __builtin_eh_pointer (0) within a catch region. Replace
2015 this zero argument with the current catch region number. */
2016 if (state
->ehp_region
)
2018 tree nr
= build_int_cst (integer_type_node
,
2019 state
->ehp_region
->index
);
2020 gimple_call_set_arg (stmt
, 0, nr
);
2024 /* The user has dome something silly. Remove it. */
2025 rhs
= null_pointer_node
;
2030 case BUILT_IN_EH_FILTER
:
2031 /* ??? This should never appear, but since it's a builtin it
2032 is accessible to abuse by users. Just remove it and
2033 replace the use with the arbitrary value zero. */
2034 rhs
= build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
2036 lhs
= gimple_call_lhs (stmt
);
2037 x
= gimple_build_assign (lhs
, rhs
);
2038 gsi_insert_before (gsi
, x
, GSI_SAME_STMT
);
2041 case BUILT_IN_EH_COPY_VALUES
:
2042 /* Likewise this should not appear. Remove it. */
2043 gsi_remove (gsi
, true);
2053 /* If the stmt can throw, use a new temporary for the assignment
2054 to a LHS. This makes sure the old value of the LHS is
2055 available on the EH edge. Only do so for statements that
2056 potentially fall through (no noreturn calls e.g.), otherwise
2057 this new assignment might create fake fallthru regions. */
2058 if (stmt_could_throw_p (cfun
, stmt
)
2059 && gimple_has_lhs (stmt
)
2060 && gimple_stmt_may_fallthru (stmt
)
2061 && !tree_could_throw_p (gimple_get_lhs (stmt
))
2062 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt
))))
2064 tree lhs
= gimple_get_lhs (stmt
);
2065 tree tmp
= create_tmp_var (TREE_TYPE (lhs
));
2066 gimple
*s
= gimple_build_assign (lhs
, tmp
);
2067 gimple_set_location (s
, gimple_location (stmt
));
2068 gimple_set_block (s
, gimple_block (stmt
));
2069 gimple_set_lhs (stmt
, tmp
);
2070 gsi_insert_after (gsi
, s
, GSI_SAME_STMT
);
2072 /* Look for things that can throw exceptions, and record them. */
2073 if (state
->cur_region
&& stmt_could_throw_p (cfun
, stmt
))
2075 record_stmt_eh_region (state
->cur_region
, stmt
);
2076 note_eh_region_may_contain_throw (state
->cur_region
);
2083 maybe_record_in_goto_queue (state
, stmt
);
2087 verify_norecord_switch_expr (state
, as_a
<gswitch
*> (stmt
));
2092 gtry
*try_stmt
= as_a
<gtry
*> (stmt
);
2093 if (gimple_try_kind (try_stmt
) == GIMPLE_TRY_FINALLY
)
2094 replace
= lower_try_finally (state
, try_stmt
);
2097 x
= gimple_seq_first_stmt (gimple_try_cleanup (try_stmt
));
2100 replace
= gimple_try_eval (try_stmt
);
2101 lower_eh_constructs_1 (state
, &replace
);
2104 switch (gimple_code (x
))
2107 replace
= lower_catch (state
, try_stmt
);
2109 case GIMPLE_EH_FILTER
:
2110 replace
= lower_eh_filter (state
, try_stmt
);
2112 case GIMPLE_EH_MUST_NOT_THROW
:
2113 replace
= lower_eh_must_not_throw (state
, try_stmt
);
2115 case GIMPLE_EH_ELSE
:
2116 /* This code is only valid with GIMPLE_TRY_FINALLY. */
2119 replace
= lower_cleanup (state
, try_stmt
);
2125 /* Remove the old stmt and insert the transformed sequence
2127 gsi_insert_seq_before (gsi
, replace
, GSI_SAME_STMT
);
2128 gsi_remove (gsi
, true);
2130 /* Return since we don't want gsi_next () */
2133 case GIMPLE_EH_ELSE
:
2134 /* We should be eliminating this in lower_try_finally et al. */
2138 /* A type, a decl, or some kind of statement that we're not
2139 interested in. Don't walk them. */
2146 /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2149 lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*pseq
)
2151 gimple_stmt_iterator gsi
;
2152 for (gsi
= gsi_start (*pseq
); !gsi_end_p (gsi
);)
2153 lower_eh_constructs_2 (state
, &gsi
);
2158 const pass_data pass_data_lower_eh
=
2160 GIMPLE_PASS
, /* type */
2162 OPTGROUP_NONE
, /* optinfo_flags */
2163 TV_TREE_EH
, /* tv_id */
2164 PROP_gimple_lcf
, /* properties_required */
2165 PROP_gimple_leh
, /* properties_provided */
2166 0, /* properties_destroyed */
2167 0, /* todo_flags_start */
2168 0, /* todo_flags_finish */
2171 class pass_lower_eh
: public gimple_opt_pass
2174 pass_lower_eh (gcc::context
*ctxt
)
2175 : gimple_opt_pass (pass_data_lower_eh
, ctxt
)
2178 /* opt_pass methods: */
2179 unsigned int execute (function
*) final override
;
2181 }; // class pass_lower_eh
2184 pass_lower_eh::execute (function
*fun
)
2186 struct leh_state null_state
;
2189 bodyp
= gimple_body (current_function_decl
);
2193 finally_tree
= new hash_table
<finally_tree_hasher
> (31);
2194 eh_region_may_contain_throw_map
= BITMAP_ALLOC (NULL
);
2195 memset (&null_state
, 0, sizeof (null_state
));
2197 collect_finally_tree_1 (bodyp
, NULL
);
2198 lower_eh_constructs_1 (&null_state
, &bodyp
);
2199 gimple_set_body (current_function_decl
, bodyp
);
2201 /* We assume there's a return statement, or something, at the end of
2202 the function, and thus ploping the EH sequence afterward won't
2204 gcc_assert (!gimple_seq_may_fallthru (bodyp
));
2205 gimple_seq_add_seq (&bodyp
, eh_seq
);
2207 /* We assume that since BODYP already existed, adding EH_SEQ to it
2208 didn't change its value, and we don't have to re-set the function. */
2209 gcc_assert (bodyp
== gimple_body (current_function_decl
));
2211 delete finally_tree
;
2212 finally_tree
= NULL
;
2213 BITMAP_FREE (eh_region_may_contain_throw_map
);
2216 /* If this function needs a language specific EH personality routine
2217 and the frontend didn't already set one do so now. */
2218 if (function_needs_eh_personality (fun
) == eh_personality_lang
2219 && !DECL_FUNCTION_PERSONALITY (current_function_decl
))
2220 DECL_FUNCTION_PERSONALITY (current_function_decl
)
2221 = lang_hooks
.eh_personality ();
2229 make_pass_lower_eh (gcc::context
*ctxt
)
2231 return new pass_lower_eh (ctxt
);
2234 /* Create the multiple edges from an EH_DISPATCH statement to all of
2235 the possible handlers for its EH region. Return true if there's
2236 no fallthru edge; false if there is. */
2239 make_eh_dispatch_edges (geh_dispatch
*stmt
)
2243 basic_block src
, dst
;
2245 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2246 src
= gimple_bb (stmt
);
2251 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2253 dst
= label_to_block (cfun
, c
->label
);
2254 make_edge (src
, dst
, 0);
2256 /* A catch-all handler doesn't have a fallthru. */
2257 if (c
->type_list
== NULL
)
2262 case ERT_ALLOWED_EXCEPTIONS
:
2263 dst
= label_to_block (cfun
, r
->u
.allowed
.label
);
2264 make_edge (src
, dst
, 0);
2274 /* Create the single EH edge from STMT to its nearest landing pad,
2275 if there is such a landing pad within the current function. */
2278 make_eh_edge (gimple
*stmt
)
2280 basic_block src
, dst
;
2284 lp_nr
= lookup_stmt_eh_lp (stmt
);
2288 lp
= get_eh_landing_pad_from_number (lp_nr
);
2289 gcc_assert (lp
!= NULL
);
2291 src
= gimple_bb (stmt
);
2292 dst
= label_to_block (cfun
, lp
->post_landing_pad
);
2293 return make_edge (src
, dst
, EDGE_EH
);
2296 /* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2297 do not actually perform the final edge redirection.
2299 CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2300 we intend to change the destination EH region as well; this means
2301 EH_LANDING_PAD_NR must already be set on the destination block label.
2302 If false, we're being called from generic cfg manipulation code and we
2303 should preserve our place within the region tree. */
2306 redirect_eh_edge_1 (edge edge_in
, basic_block new_bb
, bool change_region
)
2308 eh_landing_pad old_lp
, new_lp
;
2311 int old_lp_nr
, new_lp_nr
;
2312 tree old_label
, new_label
;
2316 old_bb
= edge_in
->dest
;
2317 old_label
= gimple_block_label (old_bb
);
2318 old_lp_nr
= EH_LANDING_PAD_NR (old_label
);
2319 gcc_assert (old_lp_nr
> 0);
2320 old_lp
= get_eh_landing_pad_from_number (old_lp_nr
);
2322 throw_stmt
= *gsi_last_bb (edge_in
->src
);
2323 gcc_checking_assert (lookup_stmt_eh_lp (throw_stmt
) == old_lp_nr
);
2325 new_label
= gimple_block_label (new_bb
);
2327 /* Look for an existing region that might be using NEW_BB already. */
2328 new_lp_nr
= EH_LANDING_PAD_NR (new_label
);
2331 new_lp
= get_eh_landing_pad_from_number (new_lp_nr
);
2332 gcc_assert (new_lp
);
2334 /* Unless CHANGE_REGION is true, the new and old landing pad
2335 had better be associated with the same EH region. */
2336 gcc_assert (change_region
|| new_lp
->region
== old_lp
->region
);
2341 gcc_assert (!change_region
);
2344 /* Notice when we redirect the last EH edge away from OLD_BB. */
2345 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
2346 if (e
!= edge_in
&& (e
->flags
& EDGE_EH
))
2351 /* NEW_LP already exists. If there are still edges into OLD_LP,
2352 there's nothing to do with the EH tree. If there are no more
2353 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2354 If CHANGE_REGION is true, then our caller is expecting to remove
2356 if (e
== NULL
&& !change_region
)
2357 remove_eh_landing_pad (old_lp
);
2361 /* No correct landing pad exists. If there are no more edges
2362 into OLD_LP, then we can simply re-use the existing landing pad.
2363 Otherwise, we have to create a new landing pad. */
2366 EH_LANDING_PAD_NR (old_lp
->post_landing_pad
) = 0;
2370 new_lp
= gen_eh_landing_pad (old_lp
->region
);
2371 new_lp
->post_landing_pad
= new_label
;
2372 EH_LANDING_PAD_NR (new_label
) = new_lp
->index
;
2375 /* Maybe move the throwing statement to the new region. */
2376 if (old_lp
!= new_lp
)
2378 remove_stmt_from_eh_lp (throw_stmt
);
2379 add_stmt_to_eh_lp (throw_stmt
, new_lp
->index
);
2383 /* Redirect EH edge E to NEW_BB. */
2386 redirect_eh_edge (edge edge_in
, basic_block new_bb
)
2388 redirect_eh_edge_1 (edge_in
, new_bb
, false);
2389 return ssa_redirect_edge (edge_in
, new_bb
);
2392 /* This is a subroutine of gimple_redirect_edge_and_branch. Update the
2393 labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2394 The actual edge update will happen in the caller. */
2397 redirect_eh_dispatch_edge (geh_dispatch
*stmt
, edge e
, basic_block new_bb
)
2399 tree new_lab
= gimple_block_label (new_bb
);
2400 bool any_changed
= false;
2405 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2409 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2411 old_bb
= label_to_block (cfun
, c
->label
);
2412 if (old_bb
== e
->dest
)
2420 case ERT_ALLOWED_EXCEPTIONS
:
2421 old_bb
= label_to_block (cfun
, r
->u
.allowed
.label
);
2422 gcc_assert (old_bb
== e
->dest
);
2423 r
->u
.allowed
.label
= new_lab
;
2431 gcc_assert (any_changed
);
2434 /* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2437 operation_could_trap_helper_p (enum tree_code op
,
2448 case TRUNC_DIV_EXPR
:
2450 case FLOOR_DIV_EXPR
:
2451 case ROUND_DIV_EXPR
:
2452 case EXACT_DIV_EXPR
:
2454 case FLOOR_MOD_EXPR
:
2455 case ROUND_MOD_EXPR
:
2456 case TRUNC_MOD_EXPR
:
2457 if (!TREE_CONSTANT (divisor
) || integer_zerop (divisor
))
2459 if (TREE_CODE (divisor
) == VECTOR_CST
)
2461 /* Inspired by initializer_each_zero_or_onep. */
2462 unsigned HOST_WIDE_INT nelts
= vector_cst_encoded_nelts (divisor
);
2463 if (VECTOR_CST_STEPPED_P (divisor
)
2464 && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (divisor
))
2465 .is_constant (&nelts
))
2467 for (unsigned int i
= 0; i
< nelts
; ++i
)
2469 tree elt
= vector_cst_elt (divisor
, i
);
2470 if (integer_zerop (elt
))
2481 return flag_trapping_math
;
2483 /* Fixed point operations also use RDIV_EXPR. */
2484 if (!TREE_CONSTANT (divisor
) || fixed_zerop (divisor
))
2493 /* MIN/MAX similar as LT/LE/GT/GE. */
2496 /* Some floating point comparisons may trap. */
2501 case UNORDERED_EXPR
:
2513 /* These operations don't trap with floating point. */
2519 /* ABSU_EXPR never traps. */
2525 /* Any floating arithmetic may trap. */
2526 if (fp_operation
&& flag_trapping_math
)
2534 /* Constructing an object cannot trap. */
2539 /* Whether *COND_EXPR can trap depends on whether the
2540 first argument can trap, so signal it as not handled.
2541 Whether lhs is floating or not doesn't matter. */
2546 /* Any floating arithmetic may trap. */
2547 if (fp_operation
&& flag_trapping_math
)
2555 /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2556 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2557 type operands that may trap. If OP is a division operator, DIVISOR contains
2558 the value of the divisor. */
2561 operation_could_trap_p (enum tree_code op
, bool fp_operation
, bool honor_trapv
,
2564 bool honor_nans
= (fp_operation
&& flag_trapping_math
2565 && !flag_finite_math_only
);
2566 bool honor_snans
= fp_operation
&& flag_signaling_nans
!= 0;
2569 /* This function cannot tell whether or not COND_EXPR could trap,
2570 because that depends on its condition op. */
2571 gcc_assert (op
!= COND_EXPR
);
2573 if (TREE_CODE_CLASS (op
) != tcc_comparison
2574 && TREE_CODE_CLASS (op
) != tcc_unary
2575 && TREE_CODE_CLASS (op
) != tcc_binary
)
2578 return operation_could_trap_helper_p (op
, fp_operation
, honor_trapv
,
2579 honor_nans
, honor_snans
, divisor
,
2584 /* Returns true if it is possible to prove that the index of
2585 an array access REF (an ARRAY_REF expression) falls into the
2589 in_array_bounds_p (tree ref
)
2591 tree idx
= TREE_OPERAND (ref
, 1);
2594 if (TREE_CODE (idx
) != INTEGER_CST
)
2597 min
= array_ref_low_bound (ref
);
2598 max
= array_ref_up_bound (ref
);
2601 || TREE_CODE (min
) != INTEGER_CST
2602 || TREE_CODE (max
) != INTEGER_CST
)
2605 if (tree_int_cst_lt (idx
, min
)
2606 || tree_int_cst_lt (max
, idx
))
2612 /* Returns true if it is possible to prove that the range of
2613 an array access REF (an ARRAY_RANGE_REF expression) falls
2614 into the array bounds. */
2617 range_in_array_bounds_p (tree ref
)
2619 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ref
));
2620 tree range_min
, range_max
, min
, max
;
2622 range_min
= TYPE_MIN_VALUE (domain_type
);
2623 range_max
= TYPE_MAX_VALUE (domain_type
);
2626 || TREE_CODE (range_min
) != INTEGER_CST
2627 || TREE_CODE (range_max
) != INTEGER_CST
)
2630 min
= array_ref_low_bound (ref
);
2631 max
= array_ref_up_bound (ref
);
2634 || TREE_CODE (min
) != INTEGER_CST
2635 || TREE_CODE (max
) != INTEGER_CST
)
2638 if (tree_int_cst_lt (range_min
, min
)
2639 || tree_int_cst_lt (max
, range_max
))
2645 /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2646 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2647 This routine expects only GIMPLE lhs or rhs input. */
2650 tree_could_trap_p (tree expr
)
2652 enum tree_code code
;
2653 bool fp_operation
= false;
2654 bool honor_trapv
= false;
2655 tree t
, base
, div
= NULL_TREE
;
2660 /* In COND_EXPR and VEC_COND_EXPR only the condition may trap, but
2661 they won't appear as operands in GIMPLE form, so this is just for the
2662 GENERIC uses where it needs to recurse on the operands and so
2663 *COND_EXPR itself doesn't trap. */
2664 if (TREE_CODE (expr
) == COND_EXPR
|| TREE_CODE (expr
) == VEC_COND_EXPR
)
2667 code
= TREE_CODE (expr
);
2668 t
= TREE_TYPE (expr
);
2672 if (COMPARISON_CLASS_P (expr
))
2673 fp_operation
= FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2675 fp_operation
= FLOAT_TYPE_P (t
);
2676 honor_trapv
= INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
);
2679 if (TREE_CODE_CLASS (code
) == tcc_binary
)
2680 div
= TREE_OPERAND (expr
, 1);
2681 if (operation_could_trap_p (code
, fp_operation
, honor_trapv
, div
))
2691 case VIEW_CONVERT_EXPR
:
2692 case WITH_SIZE_EXPR
:
2693 expr
= TREE_OPERAND (expr
, 0);
2694 code
= TREE_CODE (expr
);
2697 case ARRAY_RANGE_REF
:
2698 base
= TREE_OPERAND (expr
, 0);
2699 if (tree_could_trap_p (base
))
2701 if (TREE_THIS_NOTRAP (expr
))
2703 return !range_in_array_bounds_p (expr
);
2706 base
= TREE_OPERAND (expr
, 0);
2707 if (tree_could_trap_p (base
))
2709 if (TREE_THIS_NOTRAP (expr
))
2711 return !in_array_bounds_p (expr
);
2713 case TARGET_MEM_REF
:
2715 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
2716 && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0)))
2718 if (TREE_THIS_NOTRAP (expr
))
2720 /* We cannot prove that the access is in-bounds when we have
2721 variable-index TARGET_MEM_REFs. */
2722 if (code
== TARGET_MEM_REF
2723 && (TMR_INDEX (expr
) || TMR_INDEX2 (expr
)))
2725 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
)
2727 tree base
= TREE_OPERAND (TREE_OPERAND (expr
, 0), 0);
2728 poly_offset_int off
= mem_ref_offset (expr
);
2729 if (maybe_lt (off
, 0))
2731 if (TREE_CODE (base
) == STRING_CST
)
2732 return maybe_le (TREE_STRING_LENGTH (base
), off
);
2733 tree size
= DECL_SIZE_UNIT (base
);
2734 if (size
== NULL_TREE
2735 || !poly_int_tree_p (size
)
2736 || maybe_le (wi::to_poly_offset (size
), off
))
2738 /* Now we are sure the first byte of the access is inside
2745 return !TREE_THIS_NOTRAP (expr
);
2748 return TREE_THIS_VOLATILE (expr
);
2751 /* Internal function calls do not trap. */
2752 if (CALL_EXPR_FN (expr
) == NULL_TREE
)
2754 t
= get_callee_fndecl (expr
);
2755 /* Assume that indirect and calls to weak functions may trap. */
2756 if (!t
|| !DECL_P (t
))
2759 return tree_could_trap_p (t
);
2763 /* Assume that accesses to weak functions may trap, unless we know
2764 they are certainly defined in current TU or in some other
2766 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
) && DECL_EXTERNAL (expr
))
2768 cgraph_node
*node
= cgraph_node::get (expr
);
2770 node
= node
->function_symbol ();
2771 return !(node
&& node
->in_other_partition
);
2776 /* Assume that accesses to weak vars may trap, unless we know
2777 they are certainly defined in current TU or in some other
2779 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
) && DECL_EXTERNAL (expr
))
2781 varpool_node
*node
= varpool_node::get (expr
);
2783 node
= node
->ultimate_alias_target ();
2784 return !(node
&& node
->in_other_partition
);
2793 /* Return non-NULL if there is an integer operation with trapping overflow
2794 we can rewrite into non-trapping. Called via walk_tree from
2795 rewrite_to_non_trapping_overflow. */
2798 find_trapping_overflow (tree
*tp
, int *walk_subtrees
, void *data
)
2801 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (*tp
))
2802 && !operation_no_trapping_overflow (TREE_TYPE (*tp
), TREE_CODE (*tp
)))
2804 if (IS_TYPE_OR_DECL_P (*tp
)
2805 || (TREE_CODE (*tp
) == SAVE_EXPR
&& data
== NULL
))
2810 /* Rewrite selected operations into unsigned arithmetics, so that they
2811 don't trap on overflow. */
2814 replace_trapping_overflow (tree
*tp
, int *walk_subtrees
, void *data
)
2816 if (find_trapping_overflow (tp
, walk_subtrees
, data
))
2818 tree type
= TREE_TYPE (*tp
);
2819 tree utype
= unsigned_type_for (type
);
2821 int len
= TREE_OPERAND_LENGTH (*tp
);
2822 for (int i
= 0; i
< len
; ++i
)
2823 walk_tree (&TREE_OPERAND (*tp
, i
), replace_trapping_overflow
,
2824 data
, (hash_set
<tree
> *) data
);
2826 if (TREE_CODE (*tp
) == ABS_EXPR
)
2828 TREE_SET_CODE (*tp
, ABSU_EXPR
);
2829 TREE_TYPE (*tp
) = utype
;
2830 *tp
= fold_convert (type
, *tp
);
2834 TREE_TYPE (*tp
) = utype
;
2835 len
= TREE_OPERAND_LENGTH (*tp
);
2836 for (int i
= 0; i
< len
; ++i
)
2837 TREE_OPERAND (*tp
, i
)
2838 = fold_convert (utype
, TREE_OPERAND (*tp
, i
));
2839 *tp
= fold_convert (type
, *tp
);
2845 /* If any subexpression of EXPR can trap due to -ftrapv, rewrite it
2846 using unsigned arithmetics to avoid traps in it. */
2849 rewrite_to_non_trapping_overflow (tree expr
)
2853 hash_set
<tree
> pset
;
2854 if (!walk_tree (&expr
, find_trapping_overflow
, &pset
, &pset
))
2856 expr
= unshare_expr (expr
);
2858 walk_tree (&expr
, replace_trapping_overflow
, &pset
, &pset
);
2862 /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2863 an assignment or a conditional) may throw. */
2866 stmt_could_throw_1_p (gassign
*stmt
)
2868 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2869 bool honor_nans
= false;
2870 bool honor_snans
= false;
2871 bool fp_operation
= false;
2872 bool honor_trapv
= false;
2877 if (TREE_CODE_CLASS (code
) == tcc_comparison
2878 || TREE_CODE_CLASS (code
) == tcc_unary
2879 || TREE_CODE_CLASS (code
) == tcc_binary
)
2881 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2882 t
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
2884 t
= TREE_TYPE (gimple_assign_lhs (stmt
));
2885 fp_operation
= FLOAT_TYPE_P (t
);
2888 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
2889 honor_snans
= flag_signaling_nans
!= 0;
2891 else if (INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
))
2895 /* First check the LHS. */
2896 if (tree_could_trap_p (gimple_assign_lhs (stmt
)))
2899 /* Check if the main expression may trap. */
2900 ret
= operation_could_trap_helper_p (code
, fp_operation
, honor_trapv
,
2901 honor_nans
, honor_snans
,
2902 gimple_assign_rhs2 (stmt
),
2907 /* If the expression does not trap, see if any of the individual operands may
2909 for (i
= 1; i
< gimple_num_ops (stmt
); i
++)
2910 if (tree_could_trap_p (gimple_op (stmt
, i
)))
2917 /* Return true if statement STMT within FUN could throw an exception. */
2920 stmt_could_throw_p (function
*fun
, gimple
*stmt
)
2922 if (!flag_exceptions
)
2925 /* The only statements that can throw an exception are assignments,
2926 conditionals, calls, resx, and asms. */
2927 switch (gimple_code (stmt
))
2933 return !gimple_call_nothrow_p (as_a
<gcall
*> (stmt
));
2937 if (fun
&& !fun
->can_throw_non_call_exceptions
)
2939 gcond
*cond
= as_a
<gcond
*> (stmt
);
2940 tree lhs
= gimple_cond_lhs (cond
);
2941 return operation_could_trap_p (gimple_cond_code (cond
),
2942 FLOAT_TYPE_P (TREE_TYPE (lhs
)),
2947 if ((fun
&& !fun
->can_throw_non_call_exceptions
)
2948 || gimple_clobber_p (stmt
))
2950 return stmt_could_throw_1_p (as_a
<gassign
*> (stmt
));
2953 if (fun
&& !fun
->can_throw_non_call_exceptions
)
2955 return gimple_asm_volatile_p (as_a
<gasm
*> (stmt
));
2962 /* Return true if STMT in function FUN must be assumed necessary because of
2963 non-call exceptions. */
2966 stmt_unremovable_because_of_non_call_eh_p (function
*fun
, gimple
*stmt
)
2968 return (fun
->can_throw_non_call_exceptions
2969 && !fun
->can_delete_dead_exceptions
2970 && stmt_could_throw_p (fun
, stmt
));
2973 /* Return true if expression T could throw an exception. */
2976 tree_could_throw_p (tree t
)
2978 if (!flag_exceptions
)
2980 if (TREE_CODE (t
) == MODIFY_EXPR
)
2982 if (cfun
->can_throw_non_call_exceptions
2983 && tree_could_trap_p (TREE_OPERAND (t
, 0)))
2985 t
= TREE_OPERAND (t
, 1);
2988 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
2989 t
= TREE_OPERAND (t
, 0);
2990 if (TREE_CODE (t
) == CALL_EXPR
)
2991 return (call_expr_flags (t
) & ECF_NOTHROW
) == 0;
2992 if (cfun
->can_throw_non_call_exceptions
)
2993 return tree_could_trap_p (t
);
2997 /* Return true if STMT can throw an exception that is not caught within its
2998 function FUN. FUN can be NULL but the function is extra conservative
3002 stmt_can_throw_external (function
*fun
, gimple
*stmt
)
3006 if (!stmt_could_throw_p (fun
, stmt
))
3011 lp_nr
= lookup_stmt_eh_lp_fn (fun
, stmt
);
3015 /* Return true if STMT can throw an exception that is caught within its
3019 stmt_can_throw_internal (function
*fun
, gimple
*stmt
)
3023 gcc_checking_assert (fun
);
3024 if (!stmt_could_throw_p (fun
, stmt
))
3027 lp_nr
= lookup_stmt_eh_lp_fn (fun
, stmt
);
3031 /* Given a statement STMT in IFUN, if STMT can no longer throw, then
3032 remove any entry it might have from the EH table. Return true if
3033 any change was made. */
3036 maybe_clean_eh_stmt_fn (struct function
*ifun
, gimple
*stmt
)
3038 if (stmt_could_throw_p (ifun
, stmt
))
3040 return remove_stmt_from_eh_lp_fn (ifun
, stmt
);
3043 /* Likewise, but always use the current function. */
3046 maybe_clean_eh_stmt (gimple
*stmt
)
3048 return maybe_clean_eh_stmt_fn (cfun
, stmt
);
3051 /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
3052 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
3053 in the table if it should be in there. Return TRUE if a replacement was
3054 done that my require an EH edge purge. */
3057 maybe_clean_or_replace_eh_stmt (gimple
*old_stmt
, gimple
*new_stmt
)
3059 int lp_nr
= lookup_stmt_eh_lp (old_stmt
);
3063 bool new_stmt_could_throw
= stmt_could_throw_p (cfun
, new_stmt
);
3065 if (new_stmt
== old_stmt
&& new_stmt_could_throw
)
3068 remove_stmt_from_eh_lp (old_stmt
);
3069 if (new_stmt_could_throw
)
3071 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
3081 /* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
3082 in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
3083 operand is the return value of duplicate_eh_regions. */
3086 maybe_duplicate_eh_stmt_fn (struct function
*new_fun
, gimple
*new_stmt
,
3087 struct function
*old_fun
, gimple
*old_stmt
,
3088 hash_map
<void *, void *> *map
,
3091 int old_lp_nr
, new_lp_nr
;
3093 if (!stmt_could_throw_p (new_fun
, new_stmt
))
3096 old_lp_nr
= lookup_stmt_eh_lp_fn (old_fun
, old_stmt
);
3099 if (default_lp_nr
== 0)
3101 new_lp_nr
= default_lp_nr
;
3103 else if (old_lp_nr
> 0)
3105 eh_landing_pad old_lp
, new_lp
;
3107 old_lp
= (*old_fun
->eh
->lp_array
)[old_lp_nr
];
3108 new_lp
= static_cast<eh_landing_pad
> (*map
->get (old_lp
));
3109 new_lp_nr
= new_lp
->index
;
3113 eh_region old_r
, new_r
;
3115 old_r
= (*old_fun
->eh
->region_array
)[-old_lp_nr
];
3116 new_r
= static_cast<eh_region
> (*map
->get (old_r
));
3117 new_lp_nr
= -new_r
->index
;
3120 add_stmt_to_eh_lp_fn (new_fun
, new_stmt
, new_lp_nr
);
3124 /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
3125 and thus no remapping is required. */
3128 maybe_duplicate_eh_stmt (gimple
*new_stmt
, gimple
*old_stmt
)
3132 if (!stmt_could_throw_p (cfun
, new_stmt
))
3135 lp_nr
= lookup_stmt_eh_lp (old_stmt
);
3139 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
3143 /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
3144 GIMPLE_TRY) that are similar enough to be considered the same. Currently
3145 this only handles handlers consisting of a single call, as that's the
3146 important case for C++: a destructor call for a particular object showing
3147 up in multiple handlers. */
3150 same_handler_p (gimple_seq oneh
, gimple_seq twoh
)
3152 gimple_stmt_iterator gsi
;
3153 gimple
*ones
, *twos
;
3156 gsi
= gsi_start (oneh
);
3157 if (!gsi_one_before_end_p (gsi
))
3159 ones
= gsi_stmt (gsi
);
3161 gsi
= gsi_start (twoh
);
3162 if (!gsi_one_before_end_p (gsi
))
3164 twos
= gsi_stmt (gsi
);
3166 if (!is_gimple_call (ones
)
3167 || !is_gimple_call (twos
)
3168 || gimple_call_lhs (ones
)
3169 || gimple_call_lhs (twos
)
3170 || gimple_call_chain (ones
)
3171 || gimple_call_chain (twos
)
3172 || !gimple_call_same_target_p (ones
, twos
)
3173 || gimple_call_num_args (ones
) != gimple_call_num_args (twos
))
3176 for (ai
= 0; ai
< gimple_call_num_args (ones
); ++ai
)
3177 if (!operand_equal_p (gimple_call_arg (ones
, ai
),
3178 gimple_call_arg (twos
, ai
), 0))
3185 try { A() } finally { try { ~B() } catch { ~A() } }
3186 try { ... } finally { ~A() }
3188 try { A() } catch { ~B() }
3189 try { ~B() ... } finally { ~A() }
3191 This occurs frequently in C++, where A is a local variable and B is a
3192 temporary used in the initializer for A. */
3195 optimize_double_finally (gtry
*one
, gtry
*two
)
3198 gimple_stmt_iterator gsi
;
3201 cleanup
= gimple_try_cleanup (one
);
3202 gsi
= gsi_start (cleanup
);
3203 if (!gsi_one_before_end_p (gsi
))
3206 oneh
= gsi_stmt (gsi
);
3207 if (gimple_code (oneh
) != GIMPLE_TRY
3208 || gimple_try_kind (oneh
) != GIMPLE_TRY_CATCH
)
3211 if (same_handler_p (gimple_try_cleanup (oneh
), gimple_try_cleanup (two
)))
3213 gimple_seq seq
= gimple_try_eval (oneh
);
3215 gimple_try_set_cleanup (one
, seq
);
3216 gimple_try_set_kind (one
, GIMPLE_TRY_CATCH
);
3217 seq
= copy_gimple_seq_and_replace_locals (seq
);
3218 gimple_seq_add_seq (&seq
, gimple_try_eval (two
));
3219 gimple_try_set_eval (two
, seq
);
3223 /* Perform EH refactoring optimizations that are simpler to do when code
3224 flow has been lowered but EH structures haven't. */
3227 refactor_eh_r (gimple_seq seq
)
3229 gimple_stmt_iterator gsi
;
3234 gsi
= gsi_start (seq
);
3238 if (gsi_end_p (gsi
))
3241 two
= gsi_stmt (gsi
);
3243 if (gtry
*try_one
= dyn_cast
<gtry
*> (one
))
3244 if (gtry
*try_two
= dyn_cast
<gtry
*> (two
))
3245 if (gimple_try_kind (try_one
) == GIMPLE_TRY_FINALLY
3246 && gimple_try_kind (try_two
) == GIMPLE_TRY_FINALLY
)
3247 optimize_double_finally (try_one
, try_two
);
3249 switch (gimple_code (one
))
3252 refactor_eh_r (gimple_try_eval (one
));
3253 refactor_eh_r (gimple_try_cleanup (one
));
3256 refactor_eh_r (gimple_catch_handler (as_a
<gcatch
*> (one
)));
3258 case GIMPLE_EH_FILTER
:
3259 refactor_eh_r (gimple_eh_filter_failure (one
));
3261 case GIMPLE_EH_ELSE
:
3263 geh_else
*eh_else_stmt
= as_a
<geh_else
*> (one
);
3264 refactor_eh_r (gimple_eh_else_n_body (eh_else_stmt
));
3265 refactor_eh_r (gimple_eh_else_e_body (eh_else_stmt
));
3280 const pass_data pass_data_refactor_eh
=
3282 GIMPLE_PASS
, /* type */
3284 OPTGROUP_NONE
, /* optinfo_flags */
3285 TV_TREE_EH
, /* tv_id */
3286 PROP_gimple_lcf
, /* properties_required */
3287 0, /* properties_provided */
3288 0, /* properties_destroyed */
3289 0, /* todo_flags_start */
3290 0, /* todo_flags_finish */
3293 class pass_refactor_eh
: public gimple_opt_pass
3296 pass_refactor_eh (gcc::context
*ctxt
)
3297 : gimple_opt_pass (pass_data_refactor_eh
, ctxt
)
3300 /* opt_pass methods: */
3301 bool gate (function
*) final override
{ return flag_exceptions
!= 0; }
3302 unsigned int execute (function
*) final override
3304 refactor_eh_r (gimple_body (current_function_decl
));
3308 }; // class pass_refactor_eh
3313 make_pass_refactor_eh (gcc::context
*ctxt
)
3315 return new pass_refactor_eh (ctxt
);
3318 /* At the end of gimple optimization, we can lower RESX. */
3321 lower_resx (basic_block bb
, gresx
*stmt
,
3322 hash_map
<eh_region
, tree
> *mnt_map
)
3325 eh_region src_r
, dst_r
;
3326 gimple_stmt_iterator gsi
;
3331 lp_nr
= lookup_stmt_eh_lp (stmt
);
3333 dst_r
= get_eh_region_from_lp_number (lp_nr
);
3337 src_r
= get_eh_region_from_number (gimple_resx_region (stmt
));
3338 gsi
= gsi_last_bb (bb
);
3342 /* We can wind up with no source region when pass_cleanup_eh shows
3343 that there are no entries into an eh region and deletes it, but
3344 then the block that contains the resx isn't removed. This can
3345 happen without optimization when the switch statement created by
3346 lower_try_finally_switch isn't simplified to remove the eh case.
3348 Resolve this by expanding the resx node to an abort. */
3350 fn
= builtin_decl_implicit (BUILT_IN_TRAP
);
3351 x
= gimple_build_call (fn
, 0);
3352 gimple_call_set_ctrl_altering (x
, true);
3353 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3355 while (EDGE_COUNT (bb
->succs
) > 0)
3356 remove_edge (EDGE_SUCC (bb
, 0));
3360 /* When we have a destination region, we resolve this by copying
3361 the excptr and filter values into place, and changing the edge
3362 to immediately after the landing pad. */
3370 /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3371 the failure decl into a new block, if needed. */
3372 gcc_assert (dst_r
->type
== ERT_MUST_NOT_THROW
);
3374 tree
*slot
= mnt_map
->get (dst_r
);
3377 gimple_stmt_iterator gsi2
;
3379 new_bb
= create_empty_bb (bb
);
3380 new_bb
->count
= bb
->count
;
3381 add_bb_to_loop (new_bb
, bb
->loop_father
);
3382 lab
= gimple_block_label (new_bb
);
3383 gsi2
= gsi_start_bb (new_bb
);
3385 /* Handle failure fns that expect either no arguments or the
3386 exception pointer. */
3387 fn
= dst_r
->u
.must_not_throw
.failure_decl
;
3388 if (TYPE_ARG_TYPES (TREE_TYPE (fn
)) != void_list_node
)
3390 tree epfn
= builtin_decl_implicit (BUILT_IN_EH_POINTER
);
3391 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3392 x
= gimple_build_call (epfn
, 1, src_nr
);
3393 tree var
= create_tmp_var (ptr_type_node
);
3394 var
= make_ssa_name (var
, x
);
3395 gimple_call_set_lhs (x
, var
);
3396 gsi_insert_after (&gsi2
, x
, GSI_CONTINUE_LINKING
);
3397 x
= gimple_build_call (fn
, 1, var
);
3400 x
= gimple_build_call (fn
, 0);
3401 gimple_set_location (x
, dst_r
->u
.must_not_throw
.failure_loc
);
3402 gsi_insert_after (&gsi2
, x
, GSI_CONTINUE_LINKING
);
3404 mnt_map
->put (dst_r
, lab
);
3409 new_bb
= label_to_block (cfun
, lab
);
3412 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3413 e
= make_single_succ_edge (bb
, new_bb
, EDGE_FALLTHRU
);
3418 tree dst_nr
= build_int_cst (integer_type_node
, dst_r
->index
);
3420 fn
= builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES
);
3421 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3422 x
= gimple_build_call (fn
, 2, dst_nr
, src_nr
);
3423 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3425 /* Update the flags for the outgoing edge. */
3426 e
= single_succ_edge (bb
);
3427 gcc_assert (e
->flags
& EDGE_EH
);
3428 e
->flags
= (e
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
3429 e
->probability
= profile_probability::always ();
3431 /* If there are no more EH users of the landing pad, delete it. */
3432 FOR_EACH_EDGE (e
, ei
, e
->dest
->preds
)
3433 if (e
->flags
& EDGE_EH
)
3437 eh_landing_pad lp
= get_eh_landing_pad_from_number (lp_nr
);
3438 remove_eh_landing_pad (lp
);
3448 /* When we don't have a destination region, this exception escapes
3449 up the call chain. We resolve this by generating a call to the
3450 _Unwind_Resume library function. */
3452 /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3453 with no arguments for C++. Check for that. */
3454 if (src_r
->use_cxa_end_cleanup
)
3456 fn
= builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP
);
3457 x
= gimple_build_call (fn
, 0);
3458 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3462 fn
= builtin_decl_implicit (BUILT_IN_EH_POINTER
);
3463 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3464 x
= gimple_build_call (fn
, 1, src_nr
);
3465 var
= create_tmp_var (ptr_type_node
);
3466 var
= make_ssa_name (var
, x
);
3467 gimple_call_set_lhs (x
, var
);
3468 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3470 /* When exception handling is delegated to a caller function, we
3471 have to guarantee that shadow memory variables living on stack
3472 will be cleaner before control is given to a parent function. */
3473 if (sanitize_flags_p (SANITIZE_ADDRESS
))
3476 = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN
);
3477 gimple
*g
= gimple_build_call (decl
, 0);
3478 gimple_set_location (g
, gimple_location (stmt
));
3479 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3482 fn
= builtin_decl_implicit (BUILT_IN_UNWIND_RESUME
);
3483 x
= gimple_build_call (fn
, 1, var
);
3484 gimple_call_set_ctrl_altering (x
, true);
3485 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3488 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3491 gsi_remove (&gsi
, true);
3498 const pass_data pass_data_lower_resx
=
3500 GIMPLE_PASS
, /* type */
3502 OPTGROUP_NONE
, /* optinfo_flags */
3503 TV_TREE_EH
, /* tv_id */
3504 PROP_gimple_lcf
, /* properties_required */
3505 0, /* properties_provided */
3506 0, /* properties_destroyed */
3507 0, /* todo_flags_start */
3508 0, /* todo_flags_finish */
3511 class pass_lower_resx
: public gimple_opt_pass
3514 pass_lower_resx (gcc::context
*ctxt
)
3515 : gimple_opt_pass (pass_data_lower_resx
, ctxt
)
3518 /* opt_pass methods: */
3519 bool gate (function
*) final override
{ return flag_exceptions
!= 0; }
3520 unsigned int execute (function
*) final override
;
3522 }; // class pass_lower_resx
3525 pass_lower_resx::execute (function
*fun
)
3528 bool dominance_invalidated
= false;
3529 bool any_rewritten
= false;
3531 hash_map
<eh_region
, tree
> mnt_map
;
3533 FOR_EACH_BB_FN (bb
, fun
)
3535 if (gresx
*last
= safe_dyn_cast
<gresx
*> (*gsi_last_bb (bb
)))
3537 dominance_invalidated
|= lower_resx (bb
, last
, &mnt_map
);
3538 any_rewritten
= true;
3542 if (dominance_invalidated
)
3544 free_dominance_info (CDI_DOMINATORS
);
3545 free_dominance_info (CDI_POST_DOMINATORS
);
3548 return any_rewritten
? TODO_update_ssa_only_virtuals
: 0;
3554 make_pass_lower_resx (gcc::context
*ctxt
)
3556 return new pass_lower_resx (ctxt
);
3559 /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3563 optimize_clobbers (basic_block bb
)
3565 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
3566 bool any_clobbers
= false;
3567 bool seen_stack_restore
= false;
3571 /* Only optimize anything if the bb contains at least one clobber,
3572 ends with resx (checked by caller), optionally contains some
3573 debug stmts or labels, or at most one __builtin_stack_restore
3574 call, and has an incoming EH edge. */
3575 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3577 gimple
*stmt
= gsi_stmt (gsi
);
3578 if (is_gimple_debug (stmt
))
3580 if (gimple_clobber_p (stmt
))
3582 any_clobbers
= true;
3585 if (!seen_stack_restore
3586 && gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
3588 seen_stack_restore
= true;
3591 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3597 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3598 if (e
->flags
& EDGE_EH
)
3602 gsi
= gsi_last_bb (bb
);
3603 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3605 gimple
*stmt
= gsi_stmt (gsi
);
3606 if (!gimple_clobber_p (stmt
))
3608 unlink_stmt_vdef (stmt
);
3609 gsi_remove (&gsi
, true);
3610 release_defs (stmt
);
3614 /* Try to sink var = {v} {CLOBBER} stmts followed just by
3615 internal throw to successor BB.
3616 SUNK, if not NULL, is an array of sequences indexed by basic-block
3617 index to sink to and to pick up sinking opportunities from.
3618 If FOUND_OPPORTUNITY is not NULL then do not perform the optimization
3619 but set *FOUND_OPPORTUNITY to true. */
3622 sink_clobbers (basic_block bb
,
3623 gimple_seq
*sunk
= NULL
, bool *found_opportunity
= NULL
)
3627 gimple_stmt_iterator gsi
, dgsi
;
3629 bool any_clobbers
= false;
3632 /* Only optimize if BB has a single EH successor and
3633 all predecessor edges are EH too. */
3634 if (!single_succ_p (bb
)
3635 || (single_succ_edge (bb
)->flags
& EDGE_EH
) == 0)
3638 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3640 if ((e
->flags
& EDGE_EH
) == 0)
3644 /* And BB contains only CLOBBER stmts before the final
3646 gsi
= gsi_last_bb (bb
);
3647 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3649 gimple
*stmt
= gsi_stmt (gsi
);
3650 if (is_gimple_debug (stmt
))
3652 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3654 if (!gimple_clobber_p (stmt
))
3656 any_clobbers
= true;
3658 if (!any_clobbers
&& (!sunk
|| gimple_seq_empty_p (sunk
[bb
->index
])))
3661 /* If this was a dry run, tell it we found clobbers to sink. */
3662 if (found_opportunity
)
3664 *found_opportunity
= true;
3668 edge succe
= single_succ_edge (bb
);
3669 succbb
= succe
->dest
;
3671 /* See if there is a virtual PHI node to take an updated virtual
3674 for (gphi_iterator gpi
= gsi_start_phis (succbb
);
3675 !gsi_end_p (gpi
); gsi_next (&gpi
))
3677 tree res
= gimple_phi_result (gpi
.phi ());
3678 if (virtual_operand_p (res
))
3685 gimple
*first_sunk
= NULL
;
3686 gimple
*last_sunk
= NULL
;
3687 if (sunk
&& !(succbb
->flags
& BB_VISITED
))
3688 dgsi
= gsi_start (sunk
[succbb
->index
]);
3690 dgsi
= gsi_after_labels (succbb
);
3691 gsi
= gsi_last_bb (bb
);
3692 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3694 gimple
*stmt
= gsi_stmt (gsi
);
3696 if (is_gimple_debug (stmt
))
3698 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3700 lhs
= gimple_assign_lhs (stmt
);
3701 /* Unfortunately we don't have dominance info updated at this
3702 point, so checking if
3703 dominated_by_p (CDI_DOMINATORS, succbb,
3704 gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0)))
3705 would be too costly. Thus, avoid sinking any clobbers that
3706 refer to non-(D) SSA_NAMEs. */
3707 if (TREE_CODE (lhs
) == MEM_REF
3708 && TREE_CODE (TREE_OPERAND (lhs
, 0)) == SSA_NAME
3709 && !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs
, 0)))
3711 unlink_stmt_vdef (stmt
);
3712 gsi_remove (&gsi
, true);
3713 release_defs (stmt
);
3717 /* As we do not change stmt order when sinking across a
3718 forwarder edge we can keep virtual operands in place. */
3719 gsi_remove (&gsi
, false);
3720 gsi_insert_before (&dgsi
, stmt
, GSI_NEW_STMT
);
3725 if (sunk
&& !gimple_seq_empty_p (sunk
[bb
->index
]))
3728 first_sunk
= gsi_stmt (gsi_last (sunk
[bb
->index
]));
3729 last_sunk
= gsi_stmt (gsi_start (sunk
[bb
->index
]));
3730 gsi_insert_seq_before_without_update (&dgsi
,
3731 sunk
[bb
->index
], GSI_NEW_STMT
);
3732 sunk
[bb
->index
] = NULL
;
3736 /* Adjust virtual operands if we sunk across a virtual PHI. */
3739 imm_use_iterator iter
;
3740 use_operand_p use_p
;
3742 tree phi_def
= gimple_phi_result (vphi
);
3743 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, phi_def
)
3744 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3745 SET_USE (use_p
, gimple_vdef (first_sunk
));
3746 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phi_def
))
3748 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (first_sunk
)) = 1;
3749 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phi_def
) = 0;
3751 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (vphi
, succe
),
3752 gimple_vuse (last_sunk
));
3753 SET_USE (gimple_vuse_op (last_sunk
), phi_def
);
3755 /* If there isn't a single predecessor but no virtual PHI node
3756 arrange for virtual operands to be renamed. */
3757 else if (!single_pred_p (succbb
)
3758 && TREE_CODE (gimple_vuse (last_sunk
)) == SSA_NAME
)
3760 mark_virtual_operand_for_renaming (gimple_vuse (last_sunk
));
3761 todo
|= TODO_update_ssa_only_virtuals
;
3768 /* At the end of inlining, we can lower EH_DISPATCH. Return true when
3769 we have found some duplicate labels and removed some edges. */
3772 lower_eh_dispatch (basic_block src
, geh_dispatch
*stmt
)
3774 gimple_stmt_iterator gsi
;
3779 bool redirected
= false;
3781 region_nr
= gimple_eh_dispatch_region (stmt
);
3782 r
= get_eh_region_from_number (region_nr
);
3784 gsi
= gsi_last_bb (src
);
3790 auto_vec
<tree
> labels
;
3791 tree default_label
= NULL
;
3795 hash_set
<tree
> seen_values
;
3797 /* Collect the labels for a switch. Zero the post_landing_pad
3798 field becase we'll no longer have anything keeping these labels
3799 in existence and the optimizer will be free to merge these
3801 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
3803 tree tp_node
, flt_node
, lab
= c
->label
;
3804 bool have_label
= false;
3807 tp_node
= c
->type_list
;
3808 flt_node
= c
->filter_list
;
3810 if (tp_node
== NULL
)
3812 default_label
= lab
;
3817 /* Filter out duplicate labels that arise when this handler
3818 is shadowed by an earlier one. When no labels are
3819 attached to the handler anymore, we remove
3820 the corresponding edge and then we delete unreachable
3821 blocks at the end of this pass. */
3822 if (! seen_values
.contains (TREE_VALUE (flt_node
)))
3824 tree t
= build_case_label (TREE_VALUE (flt_node
),
3826 labels
.safe_push (t
);
3827 seen_values
.add (TREE_VALUE (flt_node
));
3831 tp_node
= TREE_CHAIN (tp_node
);
3832 flt_node
= TREE_CHAIN (flt_node
);
3837 remove_edge (find_edge (src
, label_to_block (cfun
, lab
)));
3842 /* Clean up the edge flags. */
3843 FOR_EACH_EDGE (e
, ei
, src
->succs
)
3845 if (e
->flags
& EDGE_FALLTHRU
)
3847 /* If there was no catch-all, use the fallthru edge. */
3848 if (default_label
== NULL
)
3849 default_label
= gimple_block_label (e
->dest
);
3850 e
->flags
&= ~EDGE_FALLTHRU
;
3853 gcc_assert (default_label
!= NULL
);
3855 /* Don't generate a switch if there's only a default case.
3856 This is common in the form of try { A; } catch (...) { B; }. */
3857 if (!labels
.exists ())
3859 e
= single_succ_edge (src
);
3860 e
->flags
|= EDGE_FALLTHRU
;
3864 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3865 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3867 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)));
3868 filter
= make_ssa_name (filter
, x
);
3869 gimple_call_set_lhs (x
, filter
);
3870 gimple_set_location (x
, gimple_location (stmt
));
3871 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3873 /* Turn the default label into a default case. */
3874 default_label
= build_case_label (NULL
, NULL
, default_label
);
3875 sort_case_labels (labels
);
3877 x
= gimple_build_switch (filter
, default_label
, labels
);
3878 gimple_set_location (x
, gimple_location (stmt
));
3879 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3884 case ERT_ALLOWED_EXCEPTIONS
:
3886 edge b_e
= BRANCH_EDGE (src
);
3887 edge f_e
= FALLTHRU_EDGE (src
);
3889 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3890 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3892 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)));
3893 filter
= make_ssa_name (filter
, x
);
3894 gimple_call_set_lhs (x
, filter
);
3895 gimple_set_location (x
, gimple_location (stmt
));
3896 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3898 r
->u
.allowed
.label
= NULL
;
3899 x
= gimple_build_cond (EQ_EXPR
, filter
,
3900 build_int_cst (TREE_TYPE (filter
),
3901 r
->u
.allowed
.filter
),
3902 NULL_TREE
, NULL_TREE
);
3903 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3905 b_e
->flags
= b_e
->flags
| EDGE_TRUE_VALUE
;
3906 f_e
->flags
= (f_e
->flags
& ~EDGE_FALLTHRU
) | EDGE_FALSE_VALUE
;
3914 /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3915 gsi_remove (&gsi
, true);
3921 const pass_data pass_data_lower_eh_dispatch
=
3923 GIMPLE_PASS
, /* type */
3924 "ehdisp", /* name */
3925 OPTGROUP_NONE
, /* optinfo_flags */
3926 TV_TREE_EH
, /* tv_id */
3927 PROP_gimple_lcf
, /* properties_required */
3928 0, /* properties_provided */
3929 0, /* properties_destroyed */
3930 0, /* todo_flags_start */
3931 0, /* todo_flags_finish */
3934 class pass_lower_eh_dispatch
: public gimple_opt_pass
3937 pass_lower_eh_dispatch (gcc::context
*ctxt
)
3938 : gimple_opt_pass (pass_data_lower_eh_dispatch
, ctxt
)
3941 /* opt_pass methods: */
3942 bool gate (function
*fun
) final override
3944 return fun
->eh
->region_tree
!= NULL
;
3946 unsigned int execute (function
*) final override
;
3948 }; // class pass_lower_eh_dispatch
3951 pass_lower_eh_dispatch::execute (function
*fun
)
3955 bool redirected
= false;
3956 bool any_resx_to_process
= false;
3958 assign_filter_values ();
3960 FOR_EACH_BB_FN (bb
, fun
)
3962 gimple
*last
= *gsi_last_bb (bb
);
3965 if (gimple_code (last
) == GIMPLE_EH_DISPATCH
)
3967 redirected
|= lower_eh_dispatch (bb
,
3968 as_a
<geh_dispatch
*> (last
));
3969 flags
|= TODO_update_ssa_only_virtuals
;
3971 else if (gimple_code (last
) == GIMPLE_RESX
)
3973 if (stmt_can_throw_external (fun
, last
))
3974 optimize_clobbers (bb
);
3975 else if (!any_resx_to_process
)
3976 sink_clobbers (bb
, NULL
, &any_resx_to_process
);
3978 bb
->flags
&= ~BB_VISITED
;
3982 free_dominance_info (CDI_DOMINATORS
);
3983 delete_unreachable_blocks ();
3986 if (any_resx_to_process
)
3988 /* Make sure to catch all secondary sinking opportunities by processing
3989 blocks in RPO order and after all CFG modifications from lowering
3990 and unreachable block removal. */
3991 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (fun
));
3992 int rpo_n
= pre_and_rev_post_order_compute_fn (fun
, NULL
, rpo
, false);
3993 gimple_seq
*sunk
= XCNEWVEC (gimple_seq
, last_basic_block_for_fn (fun
));
3994 for (int i
= 0; i
< rpo_n
; ++i
)
3996 bb
= BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]);
3997 gimple
*last
= *gsi_last_bb (bb
);
3999 && gimple_code (last
) == GIMPLE_RESX
4000 && !stmt_can_throw_external (fun
, last
))
4001 flags
|= sink_clobbers (bb
, sunk
);
4002 /* If there were any clobbers sunk into this BB, insert them now. */
4003 if (!gimple_seq_empty_p (sunk
[bb
->index
]))
4005 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
4006 gsi_insert_seq_before (&gsi
, sunk
[bb
->index
], GSI_NEW_STMT
);
4007 sunk
[bb
->index
] = NULL
;
4009 bb
->flags
|= BB_VISITED
;
4021 make_pass_lower_eh_dispatch (gcc::context
*ctxt
)
4023 return new pass_lower_eh_dispatch (ctxt
);
4026 /* Walk statements, see what regions and, optionally, landing pads
4027 are really referenced.
4029 Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
4030 and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
4032 Passing NULL for LP_REACHABLE is valid, in this case only reachable
4035 The caller is responsible for freeing the returned sbitmaps. */
4038 mark_reachable_handlers (sbitmap
*r_reachablep
, sbitmap
*lp_reachablep
)
4040 sbitmap r_reachable
, lp_reachable
;
4042 bool mark_landing_pads
= (lp_reachablep
!= NULL
);
4043 gcc_checking_assert (r_reachablep
!= NULL
);
4045 r_reachable
= sbitmap_alloc (cfun
->eh
->region_array
->length ());
4046 bitmap_clear (r_reachable
);
4047 *r_reachablep
= r_reachable
;
4049 if (mark_landing_pads
)
4051 lp_reachable
= sbitmap_alloc (cfun
->eh
->lp_array
->length ());
4052 bitmap_clear (lp_reachable
);
4053 *lp_reachablep
= lp_reachable
;
4056 lp_reachable
= NULL
;
4058 FOR_EACH_BB_FN (bb
, cfun
)
4060 gimple_stmt_iterator gsi
;
4062 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4064 gimple
*stmt
= gsi_stmt (gsi
);
4066 if (mark_landing_pads
)
4068 int lp_nr
= lookup_stmt_eh_lp (stmt
);
4070 /* Negative LP numbers are MUST_NOT_THROW regions which
4071 are not considered BB enders. */
4073 bitmap_set_bit (r_reachable
, -lp_nr
);
4075 /* Positive LP numbers are real landing pads, and BB enders. */
4078 gcc_assert (gsi_one_before_end_p (gsi
));
4079 eh_region region
= get_eh_region_from_lp_number (lp_nr
);
4080 bitmap_set_bit (r_reachable
, region
->index
);
4081 bitmap_set_bit (lp_reachable
, lp_nr
);
4085 /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
4086 switch (gimple_code (stmt
))
4089 bitmap_set_bit (r_reachable
,
4090 gimple_resx_region (as_a
<gresx
*> (stmt
)));
4092 case GIMPLE_EH_DISPATCH
:
4093 bitmap_set_bit (r_reachable
,
4094 gimple_eh_dispatch_region (
4095 as_a
<geh_dispatch
*> (stmt
)));
4098 if (gimple_call_builtin_p (stmt
, BUILT_IN_EH_COPY_VALUES
))
4099 for (int i
= 0; i
< 2; ++i
)
4101 tree rt
= gimple_call_arg (stmt
, i
);
4102 HOST_WIDE_INT ri
= tree_to_shwi (rt
);
4104 gcc_assert (ri
== (int)ri
);
4105 bitmap_set_bit (r_reachable
, ri
);
4115 /* Remove unreachable handlers and unreachable landing pads. */
4118 remove_unreachable_handlers (void)
4120 sbitmap r_reachable
, lp_reachable
;
4125 mark_reachable_handlers (&r_reachable
, &lp_reachable
);
4129 fprintf (dump_file
, "Before removal of unreachable regions:\n");
4130 dump_eh_tree (dump_file
, cfun
);
4131 fprintf (dump_file
, "Reachable regions: ");
4132 dump_bitmap_file (dump_file
, r_reachable
);
4133 fprintf (dump_file
, "Reachable landing pads: ");
4134 dump_bitmap_file (dump_file
, lp_reachable
);
4139 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
4140 if (region
&& !bitmap_bit_p (r_reachable
, region
->index
))
4142 "Removing unreachable region %d\n",
4146 remove_unreachable_eh_regions (r_reachable
);
4148 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
4149 if (lp
&& !bitmap_bit_p (lp_reachable
, lp
->index
))
4153 "Removing unreachable landing pad %d\n",
4155 remove_eh_landing_pad (lp
);
4160 fprintf (dump_file
, "\n\nAfter removal of unreachable regions:\n");
4161 dump_eh_tree (dump_file
, cfun
);
4162 fprintf (dump_file
, "\n\n");
4165 sbitmap_free (r_reachable
);
4166 sbitmap_free (lp_reachable
);
4169 verify_eh_tree (cfun
);
4172 /* Remove unreachable handlers if any landing pads have been removed after
4173 last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
4176 maybe_remove_unreachable_handlers (void)
4181 if (cfun
->eh
== NULL
)
4184 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
4186 && (lp
->post_landing_pad
== NULL_TREE
4187 || label_to_block (cfun
, lp
->post_landing_pad
) == NULL
))
4189 remove_unreachable_handlers ();
4194 /* Remove regions that do not have landing pads. This assumes
4195 that remove_unreachable_handlers has already been run, and
4196 that we've just manipulated the landing pads since then.
4198 Preserve regions with landing pads and regions that prevent
4199 exceptions from propagating further, even if these regions
4200 are not reachable. */
4203 remove_unreachable_handlers_no_lp (void)
4206 sbitmap r_reachable
;
4209 mark_reachable_handlers (&r_reachable
, /*lp_reachablep=*/NULL
);
4211 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
4216 if (region
->landing_pads
!= NULL
4217 || region
->type
== ERT_MUST_NOT_THROW
)
4218 bitmap_set_bit (r_reachable
, region
->index
);
4221 && !bitmap_bit_p (r_reachable
, region
->index
))
4223 "Removing unreachable region %d\n",
4227 remove_unreachable_eh_regions (r_reachable
);
4229 sbitmap_free (r_reachable
);
4232 /* Undo critical edge splitting on an EH landing pad. Earlier, we
4233 optimisticaly split all sorts of edges, including EH edges. The
4234 optimization passes in between may not have needed them; if not,
4235 we should undo the split.
4237 Recognize this case by having one EH edge incoming to the BB and
4238 one normal edge outgoing; BB should be empty apart from the
4239 post_landing_pad label.
4241 Note that this is slightly different from the empty handler case
4242 handled by cleanup_empty_eh, in that the actual handler may yet
4243 have actual code but the landing pad has been separated from the
4244 handler. As such, cleanup_empty_eh relies on this transformation
4245 having been done first. */
4248 unsplit_eh (eh_landing_pad lp
)
4250 basic_block bb
= label_to_block (cfun
, lp
->post_landing_pad
);
4251 gimple_stmt_iterator gsi
;
4254 /* Quickly check the edge counts on BB for singularity. */
4255 if (!single_pred_p (bb
) || !single_succ_p (bb
))
4257 e_in
= single_pred_edge (bb
);
4258 e_out
= single_succ_edge (bb
);
4260 /* Input edge must be EH and output edge must be normal. */
4261 if ((e_in
->flags
& EDGE_EH
) == 0 || (e_out
->flags
& EDGE_EH
) != 0)
4264 /* The block must be empty except for the labels and debug insns. */
4265 gsi
= gsi_after_labels (bb
);
4266 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4267 gsi_next_nondebug (&gsi
);
4268 if (!gsi_end_p (gsi
))
4271 /* The destination block must not already have a landing pad
4272 for a different region. */
4273 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4275 glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (gsi
));
4281 lab
= gimple_label_label (label_stmt
);
4282 lp_nr
= EH_LANDING_PAD_NR (lab
);
4283 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4287 /* The new destination block must not already be a destination of
4288 the source block, lest we merge fallthru and eh edges and get
4289 all sorts of confused. */
4290 if (find_edge (e_in
->src
, e_out
->dest
))
4293 /* ??? We can get degenerate phis due to cfg cleanups. I would have
4294 thought this should have been cleaned up by a phicprop pass, but
4295 that doesn't appear to handle virtuals. Propagate by hand. */
4296 if (!gimple_seq_empty_p (phi_nodes (bb
)))
4298 for (gphi_iterator gpi
= gsi_start_phis (bb
); !gsi_end_p (gpi
); )
4301 gphi
*phi
= gpi
.phi ();
4302 tree lhs
= gimple_phi_result (phi
);
4303 tree rhs
= gimple_phi_arg_def (phi
, 0);
4304 use_operand_p use_p
;
4305 imm_use_iterator iter
;
4307 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
4309 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4310 SET_USE (use_p
, rhs
);
4313 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
4314 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
) = 1;
4316 remove_phi_node (&gpi
, true);
4320 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4321 fprintf (dump_file
, "Unsplit EH landing pad %d to block %i.\n",
4322 lp
->index
, e_out
->dest
->index
);
4324 /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
4325 a successor edge, humor it. But do the real CFG change with the
4326 predecessor of E_OUT in order to preserve the ordering of arguments
4327 to the PHI nodes in E_OUT->DEST. */
4328 redirect_eh_edge_1 (e_in
, e_out
->dest
, false);
4329 redirect_edge_pred (e_out
, e_in
->src
);
4330 e_out
->flags
= e_in
->flags
;
4331 e_out
->probability
= e_in
->probability
;
4337 /* Examine each landing pad block and see if it matches unsplit_eh. */
4340 unsplit_all_eh (void)
4342 bool changed
= false;
4346 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4348 changed
|= unsplit_eh (lp
);
4353 /* Wrapper around unsplit_all_eh that makes it usable everywhere. */
4356 unsplit_eh_edges (void)
4360 /* unsplit_all_eh can die looking up unreachable landing pads. */
4361 maybe_remove_unreachable_handlers ();
4363 changed
= unsplit_all_eh ();
4365 /* If EH edges have been unsplit, delete unreachable forwarder blocks. */
4368 free_dominance_info (CDI_DOMINATORS
);
4369 free_dominance_info (CDI_POST_DOMINATORS
);
4370 delete_unreachable_blocks ();
4374 /* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
4375 to OLD_BB to NEW_BB; return true on success, false on failure.
4377 OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
4378 PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
4379 Virtual PHIs may be deleted and marked for renaming. */
4382 cleanup_empty_eh_merge_phis (basic_block new_bb
, basic_block old_bb
,
4383 edge old_bb_out
, bool change_region
)
4385 gphi_iterator ngsi
, ogsi
;
4388 bitmap ophi_handled
;
4390 /* The destination block must not be a regular successor for any
4391 of the preds of the landing pad. Thus, avoid turning
4401 which CFG verification would choke on. See PR45172 and PR51089. */
4402 if (!single_pred_p (new_bb
))
4403 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4404 if (find_edge (e
->src
, new_bb
))
4407 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4408 redirect_edge_var_map_clear (e
);
4410 ophi_handled
= BITMAP_ALLOC (NULL
);
4412 /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
4413 for the edges we're going to move. */
4414 for (ngsi
= gsi_start_phis (new_bb
); !gsi_end_p (ngsi
); gsi_next (&ngsi
))
4416 gphi
*ophi
, *nphi
= ngsi
.phi ();
4419 nresult
= gimple_phi_result (nphi
);
4420 nop
= gimple_phi_arg_def (nphi
, old_bb_out
->dest_idx
);
4422 /* Find the corresponding PHI in OLD_BB so we can forward-propagate
4423 the source ssa_name. */
4425 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4428 if (gimple_phi_result (ophi
) == nop
)
4433 /* If we did find the corresponding PHI, copy those inputs. */
4436 /* If NOP is used somewhere else beyond phis in new_bb, give up. */
4437 if (!has_single_use (nop
))
4439 imm_use_iterator imm_iter
;
4440 use_operand_p use_p
;
4442 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, nop
)
4444 if (!gimple_debug_bind_p (USE_STMT (use_p
))
4445 && (gimple_code (USE_STMT (use_p
)) != GIMPLE_PHI
4446 || gimple_bb (USE_STMT (use_p
)) != new_bb
))
4450 bitmap_set_bit (ophi_handled
, SSA_NAME_VERSION (nop
));
4451 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4456 if ((e
->flags
& EDGE_EH
) == 0)
4458 oop
= gimple_phi_arg_def (ophi
, e
->dest_idx
);
4459 oloc
= gimple_phi_arg_location (ophi
, e
->dest_idx
);
4460 redirect_edge_var_map_add (e
, nresult
, oop
, oloc
);
4463 /* If we didn't find the PHI, if it's a real variable or a VOP, we know
4464 from the fact that OLD_BB is tree_empty_eh_handler_p that the
4465 variable is unchanged from input to the block and we can simply
4466 re-use the input to NEW_BB from the OLD_BB_OUT edge. */
4470 = gimple_phi_arg_location (nphi
, old_bb_out
->dest_idx
);
4471 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4472 redirect_edge_var_map_add (e
, nresult
, nop
, nloc
);
4476 /* Second, verify that all PHIs from OLD_BB have been handled. If not,
4477 we don't know what values from the other edges into NEW_BB to use. */
4478 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4480 gphi
*ophi
= ogsi
.phi ();
4481 tree oresult
= gimple_phi_result (ophi
);
4482 if (!bitmap_bit_p (ophi_handled
, SSA_NAME_VERSION (oresult
)))
4486 /* Finally, move the edges and update the PHIs. */
4487 for (ei
= ei_start (old_bb
->preds
); (e
= ei_safe_edge (ei
)); )
4488 if (e
->flags
& EDGE_EH
)
4490 /* ??? CFG manipluation routines do not try to update loop
4491 form on edge redirection. Do so manually here for now. */
4492 /* If we redirect a loop entry or latch edge that will either create
4493 a multiple entry loop or rotate the loop. If the loops merge
4494 we may have created a loop with multiple latches.
4495 All of this isn't easily fixed thus cancel the affected loop
4496 and mark the other loop as possibly having multiple latches. */
4497 if (e
->dest
== e
->dest
->loop_father
->header
)
4499 mark_loop_for_removal (e
->dest
->loop_father
);
4500 new_bb
->loop_father
->latch
= NULL
;
4501 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
4503 redirect_eh_edge_1 (e
, new_bb
, change_region
);
4504 redirect_edge_succ (e
, new_bb
);
4505 flush_pending_stmts (e
);
4510 BITMAP_FREE (ophi_handled
);
4514 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4515 redirect_edge_var_map_clear (e
);
4516 BITMAP_FREE (ophi_handled
);
4520 /* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4521 old region to NEW_REGION at BB. */
4524 cleanup_empty_eh_move_lp (basic_block bb
, edge e_out
,
4525 eh_landing_pad lp
, eh_region new_region
)
4527 gimple_stmt_iterator gsi
;
4530 for (pp
= &lp
->region
->landing_pads
; *pp
!= lp
; pp
= &(*pp
)->next_lp
)
4534 lp
->region
= new_region
;
4535 lp
->next_lp
= new_region
->landing_pads
;
4536 new_region
->landing_pads
= lp
;
4538 /* Delete the RESX that was matched within the empty handler block. */
4539 gsi
= gsi_last_bb (bb
);
4540 unlink_stmt_vdef (gsi_stmt (gsi
));
4541 gsi_remove (&gsi
, true);
4543 /* Clean up E_OUT for the fallthru. */
4544 e_out
->flags
= (e_out
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
4545 e_out
->probability
= profile_probability::always ();
4548 /* A subroutine of cleanup_empty_eh. Handle more complex cases of
4549 unsplitting than unsplit_eh was prepared to handle, e.g. when
4550 multiple incoming edges and phis are involved. */
4553 cleanup_empty_eh_unsplit (basic_block bb
, edge e_out
, eh_landing_pad lp
)
4555 gimple_stmt_iterator gsi
;
4558 /* We really ought not have totally lost everything following
4559 a landing pad label. Given that BB is empty, there had better
4561 gcc_assert (e_out
!= NULL
);
4563 /* The destination block must not already have a landing pad
4564 for a different region. */
4566 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4568 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (gsi
));
4573 lab
= gimple_label_label (stmt
);
4574 lp_nr
= EH_LANDING_PAD_NR (lab
);
4575 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4579 /* Attempt to move the PHIs into the successor block. */
4580 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, false))
4582 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4584 "Unsplit EH landing pad %d to block %i "
4585 "(via cleanup_empty_eh).\n",
4586 lp
->index
, e_out
->dest
->index
);
4593 /* Return true if edge E_FIRST is part of an empty infinite loop
4594 or leads to such a loop through a series of single successor
4598 infinite_empty_loop_p (edge e_first
)
4600 bool inf_loop
= false;
4603 if (e_first
->dest
== e_first
->src
)
4606 e_first
->src
->aux
= (void *) 1;
4607 for (e
= e_first
; single_succ_p (e
->dest
); e
= single_succ_edge (e
->dest
))
4609 gimple_stmt_iterator gsi
;
4615 e
->dest
->aux
= (void *) 1;
4616 gsi
= gsi_after_labels (e
->dest
);
4617 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4618 gsi_next_nondebug (&gsi
);
4619 if (!gsi_end_p (gsi
))
4622 e_first
->src
->aux
= NULL
;
4623 for (e
= e_first
; e
->dest
->aux
; e
= single_succ_edge (e
->dest
))
4624 e
->dest
->aux
= NULL
;
4629 /* Examine the block associated with LP to determine if it's an empty
4630 handler for its EH region. If so, attempt to redirect EH edges to
4631 an outer region. Return true the CFG was updated in any way. This
4632 is similar to jump forwarding, just across EH edges. */
4635 cleanup_empty_eh (eh_landing_pad lp
)
4637 basic_block bb
= label_to_block (cfun
, lp
->post_landing_pad
);
4638 gimple_stmt_iterator gsi
;
4640 eh_region new_region
;
4643 bool has_non_eh_pred
;
4647 /* There can be zero or one edges out of BB. This is the quickest test. */
4648 switch (EDGE_COUNT (bb
->succs
))
4654 e_out
= single_succ_edge (bb
);
4660 gsi
= gsi_last_nondebug_bb (bb
);
4661 resx
= gsi_stmt (gsi
);
4662 if (resx
&& is_gimple_resx (resx
))
4664 if (stmt_can_throw_external (cfun
, resx
))
4665 optimize_clobbers (bb
);
4666 else if (sink_clobbers (bb
))
4670 gsi
= gsi_after_labels (bb
);
4672 /* Make sure to skip debug statements. */
4673 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4674 gsi_next_nondebug (&gsi
);
4676 /* If the block is totally empty, look for more unsplitting cases. */
4677 if (gsi_end_p (gsi
))
4679 /* For the degenerate case of an infinite loop bail out.
4680 If bb has no successors and is totally empty, which can happen e.g.
4681 because of incorrect noreturn attribute, bail out too. */
4683 || infinite_empty_loop_p (e_out
))
4686 return ret
| cleanup_empty_eh_unsplit (bb
, e_out
, lp
);
4689 /* The block should consist only of a single RESX statement, modulo a
4690 preceding call to __builtin_stack_restore if there is no outgoing
4691 edge, since the call can be eliminated in this case. */
4692 resx
= gsi_stmt (gsi
);
4693 if (!e_out
&& gimple_call_builtin_p (resx
, BUILT_IN_STACK_RESTORE
))
4695 gsi_next_nondebug (&gsi
);
4696 resx
= gsi_stmt (gsi
);
4698 if (!is_gimple_resx (resx
))
4700 gcc_assert (gsi_one_nondebug_before_end_p (gsi
));
4702 /* Determine if there are non-EH edges, or resx edges into the handler. */
4703 has_non_eh_pred
= false;
4704 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4705 if (!(e
->flags
& EDGE_EH
))
4706 has_non_eh_pred
= true;
4708 /* Find the handler that's outer of the empty handler by looking at
4709 where the RESX instruction was vectored. */
4710 new_lp_nr
= lookup_stmt_eh_lp (resx
);
4711 new_region
= get_eh_region_from_lp_number (new_lp_nr
);
4713 /* If there's no destination region within the current function,
4714 redirection is trivial via removing the throwing statements from
4715 the EH region, removing the EH edges, and allowing the block
4716 to go unreachable. */
4717 if (new_region
== NULL
)
4719 gcc_assert (e_out
== NULL
);
4720 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4721 if (e
->flags
& EDGE_EH
)
4723 gimple
*stmt
= *gsi_last_bb (e
->src
);
4724 remove_stmt_from_eh_lp (stmt
);
4732 /* If the destination region is a MUST_NOT_THROW, allow the runtime
4733 to handle the abort and allow the blocks to go unreachable. */
4734 if (new_region
->type
== ERT_MUST_NOT_THROW
)
4736 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4737 if (e
->flags
& EDGE_EH
)
4739 gimple
*stmt
= *gsi_last_bb (e
->src
);
4740 remove_stmt_from_eh_lp (stmt
);
4741 add_stmt_to_eh_lp (stmt
, new_lp_nr
);
4749 /* Try to redirect the EH edges and merge the PHIs into the destination
4750 landing pad block. If the merge succeeds, we'll already have redirected
4751 all the EH edges. The handler itself will go unreachable if there were
4753 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, true))
4756 /* Finally, if all input edges are EH edges, then we can (potentially)
4757 reduce the number of transfers from the runtime by moving the landing
4758 pad from the original region to the new region. This is a win when
4759 we remove the last CLEANUP region along a particular exception
4760 propagation path. Since nothing changes except for the region with
4761 which the landing pad is associated, the PHI nodes do not need to be
4763 if (!has_non_eh_pred
)
4765 cleanup_empty_eh_move_lp (bb
, e_out
, lp
, new_region
);
4766 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4767 fprintf (dump_file
, "Empty EH handler %i moved to EH region %i.\n",
4768 lp
->index
, new_region
->index
);
4770 /* ??? The CFG didn't change, but we may have rendered the
4771 old EH region unreachable. Trigger a cleanup there. */
4778 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4779 fprintf (dump_file
, "Empty EH handler %i removed.\n", lp
->index
);
4780 remove_eh_landing_pad (lp
);
4784 /* Do a post-order traversal of the EH region tree. Examine each
4785 post_landing_pad block and see if we can eliminate it as empty. */
4788 cleanup_all_empty_eh (void)
4790 bool changed
= false;
4794 /* The post-order traversal may lead to quadraticness in the redirection
4795 of incoming EH edges from inner LPs, so first try to walk the region
4796 tree from inner to outer LPs in order to eliminate these edges. */
4797 for (i
= vec_safe_length (cfun
->eh
->lp_array
) - 1; i
>= 1; --i
)
4799 lp
= (*cfun
->eh
->lp_array
)[i
];
4801 changed
|= cleanup_empty_eh (lp
);
4804 /* Now do the post-order traversal to eliminate outer empty LPs. */
4805 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4807 changed
|= cleanup_empty_eh (lp
);
4812 /* Perform cleanups and lowering of exception handling
4813 1) cleanups regions with handlers doing nothing are optimized out
4814 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4815 3) Info about regions that are containing instructions, and regions
4816 reachable via local EH edges is collected
4817 4) Eh tree is pruned for regions no longer necessary.
4819 TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4820 Unify those that have the same failure decl and locus.
4824 execute_cleanup_eh_1 (void)
4826 /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4827 looking up unreachable landing pads. */
4828 remove_unreachable_handlers ();
4830 /* Watch out for the region tree vanishing due to all unreachable. */
4831 if (cfun
->eh
->region_tree
)
4833 bool changed
= false;
4836 changed
|= unsplit_all_eh ();
4837 changed
|= cleanup_all_empty_eh ();
4841 free_dominance_info (CDI_DOMINATORS
);
4842 free_dominance_info (CDI_POST_DOMINATORS
);
4844 /* We delayed all basic block deletion, as we may have performed
4845 cleanups on EH edges while non-EH edges were still present. */
4846 delete_unreachable_blocks ();
4848 /* We manipulated the landing pads. Remove any region that no
4849 longer has a landing pad. */
4850 remove_unreachable_handlers_no_lp ();
4852 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
4861 const pass_data pass_data_cleanup_eh
=
4863 GIMPLE_PASS
, /* type */
4864 "ehcleanup", /* name */
4865 OPTGROUP_NONE
, /* optinfo_flags */
4866 TV_TREE_EH
, /* tv_id */
4867 PROP_gimple_lcf
, /* properties_required */
4868 0, /* properties_provided */
4869 0, /* properties_destroyed */
4870 0, /* todo_flags_start */
4871 0, /* todo_flags_finish */
4874 class pass_cleanup_eh
: public gimple_opt_pass
4877 pass_cleanup_eh (gcc::context
*ctxt
)
4878 : gimple_opt_pass (pass_data_cleanup_eh
, ctxt
)
4881 /* opt_pass methods: */
4882 opt_pass
* clone () final override
{ return new pass_cleanup_eh (m_ctxt
); }
4883 bool gate (function
*fun
) final override
4885 return fun
->eh
!= NULL
&& fun
->eh
->region_tree
!= NULL
;
4888 unsigned int execute (function
*) final override
;
4890 }; // class pass_cleanup_eh
4893 pass_cleanup_eh::execute (function
*fun
)
4895 int ret
= execute_cleanup_eh_1 ();
4897 /* If the function no longer needs an EH personality routine
4898 clear it. This exposes cross-language inlining opportunities
4899 and avoids references to a never defined personality routine. */
4900 if (DECL_FUNCTION_PERSONALITY (current_function_decl
)
4901 && function_needs_eh_personality (fun
) != eh_personality_lang
)
4902 DECL_FUNCTION_PERSONALITY (current_function_decl
) = NULL_TREE
;
4910 make_pass_cleanup_eh (gcc::context
*ctxt
)
4912 return new pass_cleanup_eh (ctxt
);
4915 /* Disable warnings about missing quoting in GCC diagnostics for
4916 the verification errors. Their format strings don't follow GCC
4917 diagnostic conventions but are only used for debugging. */
4919 # pragma GCC diagnostic push
4920 # pragma GCC diagnostic ignored "-Wformat-diag"
4923 /* Verify that BB containing STMT as the last statement, has precisely the
4924 edge that make_eh_edge would create. */
4927 verify_eh_edges (gimple
*stmt
)
4929 basic_block bb
= gimple_bb (stmt
);
4930 eh_landing_pad lp
= NULL
;
4935 lp_nr
= lookup_stmt_eh_lp (stmt
);
4937 lp
= get_eh_landing_pad_from_number (lp_nr
);
4940 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4942 if (e
->flags
& EDGE_EH
)
4946 error ("BB %i has multiple EH edges", bb
->index
);
4958 error ("BB %i cannot throw but has an EH edge", bb
->index
);
4964 if (!stmt_could_throw_p (cfun
, stmt
))
4966 error ("BB %i last statement has incorrectly set lp", bb
->index
);
4970 if (eh_edge
== NULL
)
4972 error ("BB %i is missing an EH edge", bb
->index
);
4976 if (eh_edge
->dest
!= label_to_block (cfun
, lp
->post_landing_pad
))
4978 error ("Incorrect EH edge %i->%i", bb
->index
, eh_edge
->dest
->index
);
4985 /* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
4988 verify_eh_dispatch_edge (geh_dispatch
*stmt
)
4992 basic_block src
, dst
;
4993 bool want_fallthru
= true;
4997 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
4998 src
= gimple_bb (stmt
);
5000 FOR_EACH_EDGE (e
, ei
, src
->succs
)
5001 gcc_assert (e
->aux
== NULL
);
5006 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
5008 dst
= label_to_block (cfun
, c
->label
);
5009 e
= find_edge (src
, dst
);
5012 error ("BB %i is missing an edge", src
->index
);
5017 /* A catch-all handler doesn't have a fallthru. */
5018 if (c
->type_list
== NULL
)
5020 want_fallthru
= false;
5026 case ERT_ALLOWED_EXCEPTIONS
:
5027 dst
= label_to_block (cfun
, r
->u
.allowed
.label
);
5028 e
= find_edge (src
, dst
);
5031 error ("BB %i is missing an edge", src
->index
);
5042 FOR_EACH_EDGE (e
, ei
, src
->succs
)
5044 if (e
->flags
& EDGE_FALLTHRU
)
5046 if (fall_edge
!= NULL
)
5048 error ("BB %i too many fallthru edges", src
->index
);
5057 error ("BB %i has incorrect edge", src
->index
);
5061 if ((fall_edge
!= NULL
) ^ want_fallthru
)
5063 error ("BB %i has incorrect fallthru edge", src
->index
);
5071 # pragma GCC diagnostic pop