1 /* Exception handling semantics and decomposition for trees.
2 Copyright (C) 2003-2020 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 /* We duplicate __builtin_stack_restore at -O0 in the hope of eliminating
909 it on the EH paths. When it is not eliminated, make it transparent in
911 if (gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
912 gimple_set_location (stmt
, UNKNOWN_LOCATION
);
913 else if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
915 tree block
= gimple_block (stmt
);
916 gimple_set_location (stmt
, loc
);
917 gimple_set_block (stmt
, block
);
922 region
= outer_state
->tf
->try_finally_expr
;
923 collect_finally_tree_1 (new_seq
, region
);
928 /* A subroutine of lower_try_finally. Create a fallthru label for
929 the given try_finally state. The only tricky bit here is that
930 we have to make sure to record the label in our outer context. */
933 lower_try_finally_fallthru_label (struct leh_tf_state
*tf
)
935 tree label
= tf
->fallthru_label
;
940 label
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
941 tf
->fallthru_label
= label
;
945 record_in_finally_tree (temp
, tf
->outer
->tf
->try_finally_expr
);
951 /* A subroutine of lower_try_finally. If FINALLY consits of a
952 GIMPLE_EH_ELSE node, return it. */
954 static inline geh_else
*
955 get_eh_else (gimple_seq finally
)
957 gimple
*x
= gimple_seq_first_stmt (finally
);
958 if (gimple_code (x
) == GIMPLE_EH_ELSE
)
960 gcc_assert (gimple_seq_singleton_p (finally
));
961 return as_a
<geh_else
*> (x
);
966 /* A subroutine of lower_try_finally. If the eh_protect_cleanup_actions
967 langhook returns non-null, then the language requires that the exception
968 path out of a try_finally be treated specially. To wit: the code within
969 the finally block may not itself throw an exception. We have two choices
970 here. First we can duplicate the finally block and wrap it in a
971 must_not_throw region. Second, we can generate code like
976 if (fintmp == eh_edge)
977 protect_cleanup_actions;
980 where "fintmp" is the temporary used in the switch statement generation
981 alternative considered below. For the nonce, we always choose the first
984 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
987 honor_protect_cleanup_actions (struct leh_state
*outer_state
,
988 struct leh_state
*this_state
,
989 struct leh_tf_state
*tf
)
991 gimple_seq finally
= gimple_try_cleanup (tf
->top_p
);
993 /* EH_ELSE doesn't come from user code; only compiler generated stuff.
994 It does need to be handled here, so as to separate the (different)
995 EH path from the normal path. But we should not attempt to wrap
996 it with a must-not-throw node (which indeed gets in the way). */
997 if (geh_else
*eh_else
= get_eh_else (finally
))
999 gimple_try_set_cleanup (tf
->top_p
, gimple_eh_else_n_body (eh_else
));
1000 finally
= gimple_eh_else_e_body (eh_else
);
1002 /* Let the ELSE see the exception that's being processed, but
1003 since the cleanup is outside the try block, process it with
1004 outer_state, otherwise it may be used as a cleanup for
1005 itself, and Bad Things (TM) ensue. */
1006 eh_region save_ehp
= outer_state
->ehp_region
;
1007 outer_state
->ehp_region
= this_state
->cur_region
;
1008 lower_eh_constructs_1 (outer_state
, &finally
);
1009 outer_state
->ehp_region
= save_ehp
;
1013 /* First check for nothing to do. */
1014 if (lang_hooks
.eh_protect_cleanup_actions
== NULL
)
1016 tree actions
= lang_hooks
.eh_protect_cleanup_actions ();
1017 if (actions
== NULL
)
1021 finally
= lower_try_finally_dup_block (finally
, outer_state
,
1022 gimple_location (tf
->try_finally_expr
));
1024 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
1025 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
1026 to be in an enclosing scope, but needs to be implemented at this level
1027 to avoid a nesting violation (see wrap_temporary_cleanups in
1028 cp/decl.c). Since it's logically at an outer level, we should call
1029 terminate before we get to it, so strip it away before adding the
1030 MUST_NOT_THROW filter. */
1031 gimple_stmt_iterator gsi
= gsi_start (finally
);
1032 gimple
*x
= gsi_stmt (gsi
);
1033 if (gimple_code (x
) == GIMPLE_TRY
1034 && gimple_try_kind (x
) == GIMPLE_TRY_CATCH
1035 && gimple_try_catch_is_cleanup (x
))
1037 gsi_insert_seq_before (&gsi
, gimple_try_eval (x
), GSI_SAME_STMT
);
1038 gsi_remove (&gsi
, false);
1041 /* Wrap the block with protect_cleanup_actions as the action. */
1042 geh_mnt
*eh_mnt
= gimple_build_eh_must_not_throw (actions
);
1043 gtry
*try_stmt
= gimple_build_try (finally
,
1044 gimple_seq_alloc_with_stmt (eh_mnt
),
1046 finally
= lower_eh_must_not_throw (outer_state
, try_stmt
);
1049 /* Drop all of this into the exception sequence. */
1050 emit_post_landing_pad (&eh_seq
, tf
->region
);
1051 gimple_seq_add_seq (&eh_seq
, finally
);
1052 if (gimple_seq_may_fallthru (finally
))
1053 emit_resx (&eh_seq
, tf
->region
);
1055 /* Having now been handled, EH isn't to be considered with
1056 the rest of the outgoing edges. */
1057 tf
->may_throw
= false;
1060 /* A subroutine of lower_try_finally. We have determined that there is
1061 no fallthru edge out of the finally block. This means that there is
1062 no outgoing edge corresponding to any incoming edge. Restructure the
1063 try_finally node for this special case. */
1066 lower_try_finally_nofallthru (struct leh_state
*state
,
1067 struct leh_tf_state
*tf
)
1073 struct goto_queue_node
*q
, *qe
;
1075 lab
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
1077 /* We expect that tf->top_p is a GIMPLE_TRY. */
1078 finally
= gimple_try_cleanup (tf
->top_p
);
1079 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1081 x
= gimple_build_label (lab
);
1082 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1085 qe
= q
+ tf
->goto_queue_active
;
1088 do_return_redirection (q
, lab
, NULL
);
1090 do_goto_redirection (q
, lab
, NULL
, tf
);
1092 replace_goto_queue (tf
);
1094 /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1095 eh_else
= get_eh_else (finally
);
1098 finally
= gimple_eh_else_n_body (eh_else
);
1099 lower_eh_constructs_1 (state
, &finally
);
1100 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1104 finally
= gimple_eh_else_e_body (eh_else
);
1105 lower_eh_constructs_1 (state
, &finally
);
1107 emit_post_landing_pad (&eh_seq
, tf
->region
);
1108 gimple_seq_add_seq (&eh_seq
, finally
);
1113 lower_eh_constructs_1 (state
, &finally
);
1114 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1118 emit_post_landing_pad (&eh_seq
, tf
->region
);
1120 x
= gimple_build_goto (lab
);
1121 gimple_set_location (x
, gimple_location (tf
->try_finally_expr
));
1122 gimple_seq_add_stmt (&eh_seq
, x
);
1127 /* A subroutine of lower_try_finally. We have determined that there is
1128 exactly one destination of the finally block. Restructure the
1129 try_finally node for this special case. */
1132 lower_try_finally_onedest (struct leh_state
*state
, struct leh_tf_state
*tf
)
1134 struct goto_queue_node
*q
, *qe
;
1139 gimple_stmt_iterator gsi
;
1141 location_t loc
= gimple_location (tf
->try_finally_expr
);
1143 finally
= gimple_try_cleanup (tf
->top_p
);
1144 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1146 /* Since there's only one destination, and the destination edge can only
1147 either be EH or non-EH, that implies that all of our incoming edges
1148 are of the same type. Therefore we can lower EH_ELSE immediately. */
1149 eh_else
= get_eh_else (finally
);
1153 finally
= gimple_eh_else_e_body (eh_else
);
1155 finally
= gimple_eh_else_n_body (eh_else
);
1158 lower_eh_constructs_1 (state
, &finally
);
1160 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1162 gimple
*stmt
= gsi_stmt (gsi
);
1163 if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
1165 tree block
= gimple_block (stmt
);
1166 gimple_set_location (stmt
, gimple_location (tf
->try_finally_expr
));
1167 gimple_set_block (stmt
, block
);
1173 /* Only reachable via the exception edge. Add the given label to
1174 the head of the FINALLY block. Append a RESX at the end. */
1175 emit_post_landing_pad (&eh_seq
, tf
->region
);
1176 gimple_seq_add_seq (&eh_seq
, finally
);
1177 emit_resx (&eh_seq
, tf
->region
);
1181 if (tf
->may_fallthru
)
1183 /* Only reachable via the fallthru edge. Do nothing but let
1184 the two blocks run together; we'll fall out the bottom. */
1185 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1189 finally_label
= create_artificial_label (loc
);
1190 label_stmt
= gimple_build_label (finally_label
);
1191 gimple_seq_add_stmt (&tf
->top_p_seq
, label_stmt
);
1193 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1196 qe
= q
+ tf
->goto_queue_active
;
1200 /* Reachable by return expressions only. Redirect them. */
1202 do_return_redirection (q
, finally_label
, NULL
);
1203 replace_goto_queue (tf
);
1207 /* Reachable by goto expressions only. Redirect them. */
1209 do_goto_redirection (q
, finally_label
, NULL
, tf
);
1210 replace_goto_queue (tf
);
1212 if (tf
->dest_array
[0] == tf
->fallthru_label
)
1214 /* Reachable by goto to fallthru label only. Redirect it
1215 to the new label (already created, sadly), and do not
1216 emit the final branch out, or the fallthru label. */
1217 tf
->fallthru_label
= NULL
;
1222 /* Place the original return/goto to the original destination
1223 immediately after the finally block. */
1224 x
= tf
->goto_queue
[0].cont_stmt
;
1225 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1226 maybe_record_in_goto_queue (state
, x
);
1229 /* A subroutine of lower_try_finally. There are multiple edges incoming
1230 and outgoing from the finally block. Implement this by duplicating the
1231 finally block for every destination. */
1234 lower_try_finally_copy (struct leh_state
*state
, struct leh_tf_state
*tf
)
1237 gimple_seq new_stmt
;
1242 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1244 finally
= gimple_try_cleanup (tf
->top_p
);
1246 /* Notice EH_ELSE, and simplify some of the remaining code
1247 by considering FINALLY to be the normal return path only. */
1248 eh_else
= get_eh_else (finally
);
1250 finally
= gimple_eh_else_n_body (eh_else
);
1252 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1255 if (tf
->may_fallthru
)
1257 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1258 lower_eh_constructs_1 (state
, &seq
);
1259 gimple_seq_add_seq (&new_stmt
, seq
);
1261 tmp
= lower_try_finally_fallthru_label (tf
);
1262 x
= gimple_build_goto (tmp
);
1263 gimple_set_location (x
, tf_loc
);
1264 gimple_seq_add_stmt (&new_stmt
, x
);
1269 /* We don't need to copy the EH path of EH_ELSE,
1270 since it is only emitted once. */
1272 seq
= gimple_eh_else_e_body (eh_else
);
1274 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1275 lower_eh_constructs_1 (state
, &seq
);
1277 emit_post_landing_pad (&eh_seq
, tf
->region
);
1278 gimple_seq_add_seq (&eh_seq
, seq
);
1279 emit_resx (&eh_seq
, tf
->region
);
1284 struct goto_queue_node
*q
, *qe
;
1285 int return_index
, index
;
1288 struct goto_queue_node
*q
;
1292 return_index
= tf
->dest_array
.length ();
1293 labels
= XCNEWVEC (struct labels_s
, return_index
+ 1);
1296 qe
= q
+ tf
->goto_queue_active
;
1299 index
= q
->index
< 0 ? return_index
: q
->index
;
1301 if (!labels
[index
].q
)
1302 labels
[index
].q
= q
;
1305 for (index
= 0; index
< return_index
+ 1; index
++)
1309 q
= labels
[index
].q
;
1313 lab
= labels
[index
].label
1314 = create_artificial_label (tf_loc
);
1316 if (index
== return_index
)
1317 do_return_redirection (q
, lab
, NULL
);
1319 do_goto_redirection (q
, lab
, NULL
, tf
);
1321 x
= gimple_build_label (lab
);
1322 gimple_seq_add_stmt (&new_stmt
, x
);
1324 seq
= lower_try_finally_dup_block (finally
, state
, q
->location
);
1325 lower_eh_constructs_1 (state
, &seq
);
1326 gimple_seq_add_seq (&new_stmt
, seq
);
1328 gimple_seq_add_stmt (&new_stmt
, q
->cont_stmt
);
1329 maybe_record_in_goto_queue (state
, q
->cont_stmt
);
1332 for (q
= tf
->goto_queue
; q
< qe
; q
++)
1336 index
= q
->index
< 0 ? return_index
: q
->index
;
1338 if (labels
[index
].q
== q
)
1341 lab
= labels
[index
].label
;
1343 if (index
== return_index
)
1344 do_return_redirection (q
, lab
, NULL
);
1346 do_goto_redirection (q
, lab
, NULL
, tf
);
1349 replace_goto_queue (tf
);
1353 /* Need to link new stmts after running replace_goto_queue due
1354 to not wanting to process the same goto stmts twice. */
1355 gimple_seq_add_seq (&tf
->top_p_seq
, new_stmt
);
1358 /* A subroutine of lower_try_finally. There are multiple edges incoming
1359 and outgoing from the finally block. Implement this by instrumenting
1360 each incoming edge and creating a switch statement at the end of the
1361 finally block that branches to the appropriate destination. */
1364 lower_try_finally_switch (struct leh_state
*state
, struct leh_tf_state
*tf
)
1366 struct goto_queue_node
*q
, *qe
;
1367 tree finally_tmp
, finally_label
;
1368 int return_index
, eh_index
, fallthru_index
;
1369 int nlabels
, ndests
, j
, last_case_index
;
1371 auto_vec
<tree
> case_label_vec
;
1372 gimple_seq switch_body
= NULL
;
1376 gimple
*switch_stmt
;
1378 hash_map
<tree
, gimple
*> *cont_map
= NULL
;
1379 /* The location of the TRY_FINALLY stmt. */
1380 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1381 /* The location of the finally block. */
1382 location_t finally_loc
;
1384 finally
= gimple_try_cleanup (tf
->top_p
);
1385 eh_else
= get_eh_else (finally
);
1387 /* Mash the TRY block to the head of the chain. */
1388 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1390 /* The location of the finally is either the last stmt in the finally
1391 block or the location of the TRY_FINALLY itself. */
1392 x
= gimple_seq_last_stmt (finally
);
1393 finally_loc
= x
? gimple_location (x
) : tf_loc
;
1395 /* Prepare for switch statement generation. */
1396 nlabels
= tf
->dest_array
.length ();
1397 return_index
= nlabels
;
1398 eh_index
= return_index
+ tf
->may_return
;
1399 fallthru_index
= eh_index
+ (tf
->may_throw
&& !eh_else
);
1400 ndests
= fallthru_index
+ tf
->may_fallthru
;
1402 finally_tmp
= create_tmp_var (integer_type_node
, "finally_tmp");
1403 finally_label
= create_artificial_label (finally_loc
);
1405 /* We use vec::quick_push on case_label_vec throughout this function,
1406 since we know the size in advance and allocate precisely as muce
1408 case_label_vec
.create (ndests
);
1410 last_case_index
= 0;
1412 /* Begin inserting code for getting to the finally block. Things
1413 are done in this order to correspond to the sequence the code is
1416 if (tf
->may_fallthru
)
1418 x
= gimple_build_assign (finally_tmp
,
1419 build_int_cst (integer_type_node
,
1421 gimple_set_location (x
, finally_loc
);
1422 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1424 tmp
= build_int_cst (integer_type_node
, fallthru_index
);
1425 last_case
= build_case_label (tmp
, NULL
,
1426 create_artificial_label (finally_loc
));
1427 case_label_vec
.quick_push (last_case
);
1430 x
= gimple_build_label (CASE_LABEL (last_case
));
1431 gimple_seq_add_stmt (&switch_body
, x
);
1433 tmp
= lower_try_finally_fallthru_label (tf
);
1434 x
= gimple_build_goto (tmp
);
1435 gimple_set_location (x
, finally_loc
);
1436 gimple_seq_add_stmt (&switch_body
, x
);
1439 /* For EH_ELSE, emit the exception path (plus resx) now, then
1440 subsequently we only need consider the normal path. */
1445 finally
= gimple_eh_else_e_body (eh_else
);
1446 lower_eh_constructs_1 (state
, &finally
);
1448 emit_post_landing_pad (&eh_seq
, tf
->region
);
1449 gimple_seq_add_seq (&eh_seq
, finally
);
1450 emit_resx (&eh_seq
, tf
->region
);
1453 finally
= gimple_eh_else_n_body (eh_else
);
1455 else if (tf
->may_throw
)
1457 emit_post_landing_pad (&eh_seq
, tf
->region
);
1459 x
= gimple_build_assign (finally_tmp
,
1460 build_int_cst (integer_type_node
, eh_index
));
1461 gimple_seq_add_stmt (&eh_seq
, x
);
1463 x
= gimple_build_goto (finally_label
);
1464 gimple_set_location (x
, tf_loc
);
1465 gimple_seq_add_stmt (&eh_seq
, x
);
1467 tmp
= build_int_cst (integer_type_node
, eh_index
);
1468 last_case
= build_case_label (tmp
, NULL
,
1469 create_artificial_label (tf_loc
));
1470 case_label_vec
.quick_push (last_case
);
1473 x
= gimple_build_label (CASE_LABEL (last_case
));
1474 gimple_seq_add_stmt (&eh_seq
, x
);
1475 emit_resx (&eh_seq
, tf
->region
);
1478 x
= gimple_build_label (finally_label
);
1479 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1481 lower_eh_constructs_1 (state
, &finally
);
1482 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1484 /* Redirect each incoming goto edge. */
1486 qe
= q
+ tf
->goto_queue_active
;
1487 j
= last_case_index
+ tf
->may_return
;
1488 /* Prepare the assignments to finally_tmp that are executed upon the
1489 entrance through a particular edge. */
1492 gimple_seq mod
= NULL
;
1494 unsigned int case_index
;
1498 x
= gimple_build_assign (finally_tmp
,
1499 build_int_cst (integer_type_node
,
1501 gimple_seq_add_stmt (&mod
, x
);
1502 do_return_redirection (q
, finally_label
, mod
);
1503 switch_id
= return_index
;
1507 x
= gimple_build_assign (finally_tmp
,
1508 build_int_cst (integer_type_node
, q
->index
));
1509 gimple_seq_add_stmt (&mod
, x
);
1510 do_goto_redirection (q
, finally_label
, mod
, tf
);
1511 switch_id
= q
->index
;
1514 case_index
= j
+ q
->index
;
1515 if (case_label_vec
.length () <= case_index
|| !case_label_vec
[case_index
])
1518 tmp
= build_int_cst (integer_type_node
, switch_id
);
1519 case_lab
= build_case_label (tmp
, NULL
,
1520 create_artificial_label (tf_loc
));
1521 /* We store the cont_stmt in the pointer map, so that we can recover
1522 it in the loop below. */
1524 cont_map
= new hash_map
<tree
, gimple
*>;
1525 cont_map
->put (case_lab
, q
->cont_stmt
);
1526 case_label_vec
.quick_push (case_lab
);
1529 for (j
= last_case_index
; j
< last_case_index
+ nlabels
; j
++)
1533 last_case
= case_label_vec
[j
];
1535 gcc_assert (last_case
);
1536 gcc_assert (cont_map
);
1538 cont_stmt
= *cont_map
->get (last_case
);
1540 x
= gimple_build_label (CASE_LABEL (last_case
));
1541 gimple_seq_add_stmt (&switch_body
, x
);
1542 gimple_seq_add_stmt (&switch_body
, cont_stmt
);
1543 maybe_record_in_goto_queue (state
, cont_stmt
);
1548 replace_goto_queue (tf
);
1550 /* Make sure that the last case is the default label, as one is required.
1551 Then sort the labels, which is also required in GIMPLE. */
1552 CASE_LOW (last_case
) = NULL
;
1553 tree tem
= case_label_vec
.pop ();
1554 gcc_assert (tem
== last_case
);
1555 sort_case_labels (case_label_vec
);
1557 /* Build the switch statement, setting last_case to be the default
1559 switch_stmt
= gimple_build_switch (finally_tmp
, last_case
,
1561 gimple_set_location (switch_stmt
, finally_loc
);
1563 /* Need to link SWITCH_STMT after running replace_goto_queue
1564 due to not wanting to process the same goto stmts twice. */
1565 gimple_seq_add_stmt (&tf
->top_p_seq
, switch_stmt
);
1566 gimple_seq_add_seq (&tf
->top_p_seq
, switch_body
);
1569 /* Decide whether or not we are going to duplicate the finally block.
1570 There are several considerations.
1572 Second, we'd like to prevent egregious code growth. One way to
1573 do this is to estimate the size of the finally block, multiply
1574 that by the number of copies we'd need to make, and compare against
1575 the estimate of the size of the switch machinery we'd have to add. */
1578 decide_copy_try_finally (int ndests
, bool may_throw
, gimple_seq finally
)
1580 int f_estimate
, sw_estimate
;
1583 /* If there's an EH_ELSE involved, the exception path is separate
1584 and really doesn't come into play for this computation. */
1585 eh_else
= get_eh_else (finally
);
1588 ndests
-= may_throw
;
1589 finally
= gimple_eh_else_n_body (eh_else
);
1594 gimple_stmt_iterator gsi
;
1599 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1601 /* Duplicate __builtin_stack_restore in the hope of eliminating it
1602 on the EH paths and, consequently, useless cleanups. */
1603 gimple
*stmt
= gsi_stmt (gsi
);
1604 if (!is_gimple_debug (stmt
)
1605 && !gimple_clobber_p (stmt
)
1606 && !gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
1612 /* Finally estimate N times, plus N gotos. */
1613 f_estimate
= estimate_num_insns_seq (finally
, &eni_size_weights
);
1614 f_estimate
= (f_estimate
+ 1) * ndests
;
1616 /* Switch statement (cost 10), N variable assignments, N gotos. */
1617 sw_estimate
= 10 + 2 * ndests
;
1619 /* Optimize for size clearly wants our best guess. */
1620 if (optimize_function_for_size_p (cfun
))
1621 return f_estimate
< sw_estimate
;
1623 /* ??? These numbers are completely made up so far. */
1625 return f_estimate
< 100 || f_estimate
< sw_estimate
* 2;
1627 return f_estimate
< 40 || f_estimate
* 2 < sw_estimate
* 3;
1630 /* REG is current region of a LEH state.
1631 is the enclosing region for a possible cleanup region, or the region
1632 itself. Returns TRUE if such a region would be unreachable.
1634 Cleanup regions within a must-not-throw region aren't actually reachable
1635 even if there are throwing stmts within them, because the personality
1636 routine will call terminate before unwinding. */
1639 cleanup_is_dead_in (leh_state
*state
)
1643 eh_region reg
= state
->cur_region
;
1644 while (reg
&& reg
->type
== ERT_CLEANUP
)
1647 gcc_assert (reg
== state
->outer_non_cleanup
);
1650 eh_region reg
= state
->outer_non_cleanup
;
1651 return (reg
&& reg
->type
== ERT_MUST_NOT_THROW
);
1654 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
1655 to a sequence of labels and blocks, plus the exception region trees
1656 that record all the magic. This is complicated by the need to
1657 arrange for the FINALLY block to be executed on all exits. */
1660 lower_try_finally (struct leh_state
*state
, gtry
*tp
)
1662 struct leh_tf_state this_tf
;
1663 struct leh_state this_state
;
1665 gimple_seq old_eh_seq
;
1667 /* Process the try block. */
1669 memset (&this_tf
, 0, sizeof (this_tf
));
1670 this_tf
.try_finally_expr
= tp
;
1672 this_tf
.outer
= state
;
1673 if (using_eh_for_cleanups_p () && !cleanup_is_dead_in (state
))
1675 this_tf
.region
= gen_eh_region_cleanup (state
->cur_region
);
1676 this_state
.cur_region
= this_tf
.region
;
1680 this_tf
.region
= NULL
;
1681 this_state
.cur_region
= state
->cur_region
;
1684 this_state
.outer_non_cleanup
= state
->outer_non_cleanup
;
1685 this_state
.ehp_region
= state
->ehp_region
;
1686 this_state
.tf
= &this_tf
;
1688 old_eh_seq
= eh_seq
;
1691 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1693 /* Determine if the try block is escaped through the bottom. */
1694 this_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1696 /* Determine if any exceptions are possible within the try block. */
1698 this_tf
.may_throw
= eh_region_may_contain_throw (this_tf
.region
);
1699 if (this_tf
.may_throw
)
1700 honor_protect_cleanup_actions (state
, &this_state
, &this_tf
);
1702 /* Determine how many edges (still) reach the finally block. Or rather,
1703 how many destinations are reached by the finally block. Use this to
1704 determine how we process the finally block itself. */
1706 ndests
= this_tf
.dest_array
.length ();
1707 ndests
+= this_tf
.may_fallthru
;
1708 ndests
+= this_tf
.may_return
;
1709 ndests
+= this_tf
.may_throw
;
1711 /* If the FINALLY block is not reachable, dike it out. */
1714 gimple_seq_add_seq (&this_tf
.top_p_seq
, gimple_try_eval (tp
));
1715 gimple_try_set_cleanup (tp
, NULL
);
1717 /* If the finally block doesn't fall through, then any destination
1718 we might try to impose there isn't reached either. There may be
1719 some minor amount of cleanup and redirection still needed. */
1720 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp
)))
1721 lower_try_finally_nofallthru (state
, &this_tf
);
1723 /* We can easily special-case redirection to a single destination. */
1724 else if (ndests
== 1)
1725 lower_try_finally_onedest (state
, &this_tf
);
1726 else if (decide_copy_try_finally (ndests
, this_tf
.may_throw
,
1727 gimple_try_cleanup (tp
)))
1728 lower_try_finally_copy (state
, &this_tf
);
1730 lower_try_finally_switch (state
, &this_tf
);
1732 /* If someone requested we add a label at the end of the transformed
1734 if (this_tf
.fallthru_label
)
1736 /* This must be reached only if ndests == 0. */
1737 gimple
*x
= gimple_build_label (this_tf
.fallthru_label
);
1738 gimple_seq_add_stmt (&this_tf
.top_p_seq
, x
);
1741 this_tf
.dest_array
.release ();
1742 free (this_tf
.goto_queue
);
1743 if (this_tf
.goto_queue_map
)
1744 delete this_tf
.goto_queue_map
;
1746 /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1747 If there was no old eh_seq, then the append is trivially already done. */
1751 eh_seq
= old_eh_seq
;
1754 gimple_seq new_eh_seq
= eh_seq
;
1755 eh_seq
= old_eh_seq
;
1756 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1760 return this_tf
.top_p_seq
;
1763 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1764 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1765 exception region trees that records all the magic. */
1768 lower_catch (struct leh_state
*state
, gtry
*tp
)
1770 eh_region try_region
= NULL
;
1771 struct leh_state this_state
= *state
;
1772 gimple_stmt_iterator gsi
;
1774 gimple_seq new_seq
, cleanup
;
1776 geh_dispatch
*eh_dispatch
;
1777 location_t try_catch_loc
= gimple_location (tp
);
1778 location_t catch_loc
= UNKNOWN_LOCATION
;
1780 if (flag_exceptions
)
1782 try_region
= gen_eh_region_try (state
->cur_region
);
1783 this_state
.cur_region
= try_region
;
1784 this_state
.outer_non_cleanup
= this_state
.cur_region
;
1787 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1789 if (!eh_region_may_contain_throw (try_region
))
1790 return gimple_try_eval (tp
);
1793 eh_dispatch
= gimple_build_eh_dispatch (try_region
->index
);
1794 gimple_seq_add_stmt (&new_seq
, eh_dispatch
);
1795 emit_resx (&new_seq
, try_region
);
1797 this_state
.cur_region
= state
->cur_region
;
1798 this_state
.outer_non_cleanup
= state
->outer_non_cleanup
;
1799 this_state
.ehp_region
= try_region
;
1801 /* Add eh_seq from lowering EH in the cleanup sequence after the cleanup
1802 itself, so that e.g. for coverage purposes the nested cleanups don't
1803 appear before the cleanup body. See PR64634 for details. */
1804 gimple_seq old_eh_seq
= eh_seq
;
1808 cleanup
= gimple_try_cleanup (tp
);
1809 for (gsi
= gsi_start (cleanup
);
1817 catch_stmt
= as_a
<gcatch
*> (gsi_stmt (gsi
));
1818 if (catch_loc
== UNKNOWN_LOCATION
)
1819 catch_loc
= gimple_location (catch_stmt
);
1820 c
= gen_eh_region_catch (try_region
, gimple_catch_types (catch_stmt
));
1822 handler
= gimple_catch_handler (catch_stmt
);
1823 lower_eh_constructs_1 (&this_state
, &handler
);
1825 c
->label
= create_artificial_label (UNKNOWN_LOCATION
);
1826 x
= gimple_build_label (c
->label
);
1827 gimple_seq_add_stmt (&new_seq
, x
);
1829 gimple_seq_add_seq (&new_seq
, handler
);
1831 if (gimple_seq_may_fallthru (new_seq
))
1834 out_label
= create_artificial_label (try_catch_loc
);
1836 x
= gimple_build_goto (out_label
);
1837 gimple_seq_add_stmt (&new_seq
, x
);
1843 /* Try to set a location on the dispatching construct to avoid inheriting
1844 the location of the previous statement. */
1845 gimple_set_location (eh_dispatch
, catch_loc
);
1847 gimple_try_set_cleanup (tp
, new_seq
);
1849 gimple_seq new_eh_seq
= eh_seq
;
1850 eh_seq
= old_eh_seq
;
1851 gimple_seq ret_seq
= frob_into_branch_around (tp
, try_region
, out_label
);
1852 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1856 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1857 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1858 region trees that record all the magic. */
1861 lower_eh_filter (struct leh_state
*state
, gtry
*tp
)
1863 struct leh_state this_state
= *state
;
1864 eh_region this_region
= NULL
;
1868 inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1870 if (flag_exceptions
)
1872 this_region
= gen_eh_region_allowed (state
->cur_region
,
1873 gimple_eh_filter_types (inner
));
1874 this_state
.cur_region
= this_region
;
1875 this_state
.outer_non_cleanup
= this_state
.cur_region
;
1878 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1880 if (!eh_region_may_contain_throw (this_region
))
1881 return gimple_try_eval (tp
);
1883 this_state
.cur_region
= state
->cur_region
;
1884 this_state
.ehp_region
= this_region
;
1887 x
= gimple_build_eh_dispatch (this_region
->index
);
1888 gimple_set_location (x
, gimple_location (tp
));
1889 gimple_seq_add_stmt (&new_seq
, x
);
1890 emit_resx (&new_seq
, this_region
);
1892 this_region
->u
.allowed
.label
= create_artificial_label (UNKNOWN_LOCATION
);
1893 x
= gimple_build_label (this_region
->u
.allowed
.label
);
1894 gimple_seq_add_stmt (&new_seq
, x
);
1896 lower_eh_constructs_1 (&this_state
, gimple_eh_filter_failure_ptr (inner
));
1897 gimple_seq_add_seq (&new_seq
, gimple_eh_filter_failure (inner
));
1899 gimple_try_set_cleanup (tp
, new_seq
);
1901 return frob_into_branch_around (tp
, this_region
, NULL
);
1904 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with
1905 an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1906 plus the exception region trees that record all the magic. */
1909 lower_eh_must_not_throw (struct leh_state
*state
, gtry
*tp
)
1911 struct leh_state this_state
= *state
;
1913 if (flag_exceptions
)
1915 gimple
*inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1916 eh_region this_region
;
1918 this_region
= gen_eh_region_must_not_throw (state
->cur_region
);
1919 this_region
->u
.must_not_throw
.failure_decl
1920 = gimple_eh_must_not_throw_fndecl (
1921 as_a
<geh_mnt
*> (inner
));
1922 this_region
->u
.must_not_throw
.failure_loc
1923 = LOCATION_LOCUS (gimple_location (tp
));
1925 /* In order to get mangling applied to this decl, we must mark it
1926 used now. Otherwise, pass_ipa_free_lang_data won't think it
1928 TREE_USED (this_region
->u
.must_not_throw
.failure_decl
) = 1;
1930 this_state
.cur_region
= this_region
;
1931 this_state
.outer_non_cleanup
= this_state
.cur_region
;
1934 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1936 return gimple_try_eval (tp
);
1939 /* Implement a cleanup expression. This is similar to try-finally,
1940 except that we only execute the cleanup block for exception edges. */
1943 lower_cleanup (struct leh_state
*state
, gtry
*tp
)
1945 struct leh_state this_state
= *state
;
1946 eh_region this_region
= NULL
;
1947 struct leh_tf_state fake_tf
;
1949 bool cleanup_dead
= cleanup_is_dead_in (state
);
1951 if (flag_exceptions
&& !cleanup_dead
)
1953 this_region
= gen_eh_region_cleanup (state
->cur_region
);
1954 this_state
.cur_region
= this_region
;
1955 this_state
.outer_non_cleanup
= state
->outer_non_cleanup
;
1958 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1960 if (cleanup_dead
|| !eh_region_may_contain_throw (this_region
))
1961 return gimple_try_eval (tp
);
1963 /* Build enough of a try-finally state so that we can reuse
1964 honor_protect_cleanup_actions. */
1965 memset (&fake_tf
, 0, sizeof (fake_tf
));
1966 fake_tf
.top_p
= fake_tf
.try_finally_expr
= tp
;
1967 fake_tf
.outer
= state
;
1968 fake_tf
.region
= this_region
;
1969 fake_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1970 fake_tf
.may_throw
= true;
1972 honor_protect_cleanup_actions (state
, NULL
, &fake_tf
);
1974 if (fake_tf
.may_throw
)
1976 /* In this case honor_protect_cleanup_actions had nothing to do,
1977 and we should process this normally. */
1978 lower_eh_constructs_1 (state
, gimple_try_cleanup_ptr (tp
));
1979 result
= frob_into_branch_around (tp
, this_region
,
1980 fake_tf
.fallthru_label
);
1984 /* In this case honor_protect_cleanup_actions did nearly all of
1985 the work. All we have left is to append the fallthru_label. */
1987 result
= gimple_try_eval (tp
);
1988 if (fake_tf
.fallthru_label
)
1990 gimple
*x
= gimple_build_label (fake_tf
.fallthru_label
);
1991 gimple_seq_add_stmt (&result
, x
);
1997 /* Main loop for lowering eh constructs. Also moves gsi to the next
2001 lower_eh_constructs_2 (struct leh_state
*state
, gimple_stmt_iterator
*gsi
)
2005 gimple
*stmt
= gsi_stmt (*gsi
);
2007 switch (gimple_code (stmt
))
2011 tree fndecl
= gimple_call_fndecl (stmt
);
2014 if (fndecl
&& fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
2015 switch (DECL_FUNCTION_CODE (fndecl
))
2017 case BUILT_IN_EH_POINTER
:
2018 /* The front end may have generated a call to
2019 __builtin_eh_pointer (0) within a catch region. Replace
2020 this zero argument with the current catch region number. */
2021 if (state
->ehp_region
)
2023 tree nr
= build_int_cst (integer_type_node
,
2024 state
->ehp_region
->index
);
2025 gimple_call_set_arg (stmt
, 0, nr
);
2029 /* The user has dome something silly. Remove it. */
2030 rhs
= null_pointer_node
;
2035 case BUILT_IN_EH_FILTER
:
2036 /* ??? This should never appear, but since it's a builtin it
2037 is accessible to abuse by users. Just remove it and
2038 replace the use with the arbitrary value zero. */
2039 rhs
= build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
2041 lhs
= gimple_call_lhs (stmt
);
2042 x
= gimple_build_assign (lhs
, rhs
);
2043 gsi_insert_before (gsi
, x
, GSI_SAME_STMT
);
2046 case BUILT_IN_EH_COPY_VALUES
:
2047 /* Likewise this should not appear. Remove it. */
2048 gsi_remove (gsi
, true);
2058 /* If the stmt can throw, use a new temporary for the assignment
2059 to a LHS. This makes sure the old value of the LHS is
2060 available on the EH edge. Only do so for statements that
2061 potentially fall through (no noreturn calls e.g.), otherwise
2062 this new assignment might create fake fallthru regions. */
2063 if (stmt_could_throw_p (cfun
, stmt
)
2064 && gimple_has_lhs (stmt
)
2065 && gimple_stmt_may_fallthru (stmt
)
2066 && !tree_could_throw_p (gimple_get_lhs (stmt
))
2067 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt
))))
2069 tree lhs
= gimple_get_lhs (stmt
);
2070 tree tmp
= create_tmp_var (TREE_TYPE (lhs
));
2071 gimple
*s
= gimple_build_assign (lhs
, tmp
);
2072 gimple_set_location (s
, gimple_location (stmt
));
2073 gimple_set_block (s
, gimple_block (stmt
));
2074 gimple_set_lhs (stmt
, tmp
);
2075 gsi_insert_after (gsi
, s
, GSI_SAME_STMT
);
2077 /* Look for things that can throw exceptions, and record them. */
2078 if (state
->cur_region
&& stmt_could_throw_p (cfun
, stmt
))
2080 record_stmt_eh_region (state
->cur_region
, stmt
);
2081 note_eh_region_may_contain_throw (state
->cur_region
);
2088 maybe_record_in_goto_queue (state
, stmt
);
2092 verify_norecord_switch_expr (state
, as_a
<gswitch
*> (stmt
));
2097 gtry
*try_stmt
= as_a
<gtry
*> (stmt
);
2098 if (gimple_try_kind (try_stmt
) == GIMPLE_TRY_FINALLY
)
2099 replace
= lower_try_finally (state
, try_stmt
);
2102 x
= gimple_seq_first_stmt (gimple_try_cleanup (try_stmt
));
2105 replace
= gimple_try_eval (try_stmt
);
2106 lower_eh_constructs_1 (state
, &replace
);
2109 switch (gimple_code (x
))
2112 replace
= lower_catch (state
, try_stmt
);
2114 case GIMPLE_EH_FILTER
:
2115 replace
= lower_eh_filter (state
, try_stmt
);
2117 case GIMPLE_EH_MUST_NOT_THROW
:
2118 replace
= lower_eh_must_not_throw (state
, try_stmt
);
2120 case GIMPLE_EH_ELSE
:
2121 /* This code is only valid with GIMPLE_TRY_FINALLY. */
2124 replace
= lower_cleanup (state
, try_stmt
);
2130 /* Remove the old stmt and insert the transformed sequence
2132 gsi_insert_seq_before (gsi
, replace
, GSI_SAME_STMT
);
2133 gsi_remove (gsi
, true);
2135 /* Return since we don't want gsi_next () */
2138 case GIMPLE_EH_ELSE
:
2139 /* We should be eliminating this in lower_try_finally et al. */
2143 /* A type, a decl, or some kind of statement that we're not
2144 interested in. Don't walk them. */
2151 /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2154 lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*pseq
)
2156 gimple_stmt_iterator gsi
;
2157 for (gsi
= gsi_start (*pseq
); !gsi_end_p (gsi
);)
2158 lower_eh_constructs_2 (state
, &gsi
);
2163 const pass_data pass_data_lower_eh
=
2165 GIMPLE_PASS
, /* type */
2167 OPTGROUP_NONE
, /* optinfo_flags */
2168 TV_TREE_EH
, /* tv_id */
2169 PROP_gimple_lcf
, /* properties_required */
2170 PROP_gimple_leh
, /* properties_provided */
2171 0, /* properties_destroyed */
2172 0, /* todo_flags_start */
2173 0, /* todo_flags_finish */
2176 class pass_lower_eh
: public gimple_opt_pass
2179 pass_lower_eh (gcc::context
*ctxt
)
2180 : gimple_opt_pass (pass_data_lower_eh
, ctxt
)
2183 /* opt_pass methods: */
2184 virtual unsigned int execute (function
*);
2186 }; // class pass_lower_eh
2189 pass_lower_eh::execute (function
*fun
)
2191 struct leh_state null_state
;
2194 bodyp
= gimple_body (current_function_decl
);
2198 finally_tree
= new hash_table
<finally_tree_hasher
> (31);
2199 eh_region_may_contain_throw_map
= BITMAP_ALLOC (NULL
);
2200 memset (&null_state
, 0, sizeof (null_state
));
2202 collect_finally_tree_1 (bodyp
, NULL
);
2203 lower_eh_constructs_1 (&null_state
, &bodyp
);
2204 gimple_set_body (current_function_decl
, bodyp
);
2206 /* We assume there's a return statement, or something, at the end of
2207 the function, and thus ploping the EH sequence afterward won't
2209 gcc_assert (!gimple_seq_may_fallthru (bodyp
));
2210 gimple_seq_add_seq (&bodyp
, eh_seq
);
2212 /* We assume that since BODYP already existed, adding EH_SEQ to it
2213 didn't change its value, and we don't have to re-set the function. */
2214 gcc_assert (bodyp
== gimple_body (current_function_decl
));
2216 delete finally_tree
;
2217 finally_tree
= NULL
;
2218 BITMAP_FREE (eh_region_may_contain_throw_map
);
2221 /* If this function needs a language specific EH personality routine
2222 and the frontend didn't already set one do so now. */
2223 if (function_needs_eh_personality (fun
) == eh_personality_lang
2224 && !DECL_FUNCTION_PERSONALITY (current_function_decl
))
2225 DECL_FUNCTION_PERSONALITY (current_function_decl
)
2226 = lang_hooks
.eh_personality ();
2234 make_pass_lower_eh (gcc::context
*ctxt
)
2236 return new pass_lower_eh (ctxt
);
2239 /* Create the multiple edges from an EH_DISPATCH statement to all of
2240 the possible handlers for its EH region. Return true if there's
2241 no fallthru edge; false if there is. */
2244 make_eh_dispatch_edges (geh_dispatch
*stmt
)
2248 basic_block src
, dst
;
2250 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2251 src
= gimple_bb (stmt
);
2256 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2258 dst
= label_to_block (cfun
, c
->label
);
2259 make_edge (src
, dst
, 0);
2261 /* A catch-all handler doesn't have a fallthru. */
2262 if (c
->type_list
== NULL
)
2267 case ERT_ALLOWED_EXCEPTIONS
:
2268 dst
= label_to_block (cfun
, r
->u
.allowed
.label
);
2269 make_edge (src
, dst
, 0);
2279 /* Create the single EH edge from STMT to its nearest landing pad,
2280 if there is such a landing pad within the current function. */
2283 make_eh_edges (gimple
*stmt
)
2285 basic_block src
, dst
;
2289 lp_nr
= lookup_stmt_eh_lp (stmt
);
2293 lp
= get_eh_landing_pad_from_number (lp_nr
);
2294 gcc_assert (lp
!= NULL
);
2296 src
= gimple_bb (stmt
);
2297 dst
= label_to_block (cfun
, lp
->post_landing_pad
);
2298 make_edge (src
, dst
, EDGE_EH
);
2301 /* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2302 do not actually perform the final edge redirection.
2304 CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2305 we intend to change the destination EH region as well; this means
2306 EH_LANDING_PAD_NR must already be set on the destination block label.
2307 If false, we're being called from generic cfg manipulation code and we
2308 should preserve our place within the region tree. */
2311 redirect_eh_edge_1 (edge edge_in
, basic_block new_bb
, bool change_region
)
2313 eh_landing_pad old_lp
, new_lp
;
2316 int old_lp_nr
, new_lp_nr
;
2317 tree old_label
, new_label
;
2321 old_bb
= edge_in
->dest
;
2322 old_label
= gimple_block_label (old_bb
);
2323 old_lp_nr
= EH_LANDING_PAD_NR (old_label
);
2324 gcc_assert (old_lp_nr
> 0);
2325 old_lp
= get_eh_landing_pad_from_number (old_lp_nr
);
2327 throw_stmt
= last_stmt (edge_in
->src
);
2328 gcc_checking_assert (lookup_stmt_eh_lp (throw_stmt
) == old_lp_nr
);
2330 new_label
= gimple_block_label (new_bb
);
2332 /* Look for an existing region that might be using NEW_BB already. */
2333 new_lp_nr
= EH_LANDING_PAD_NR (new_label
);
2336 new_lp
= get_eh_landing_pad_from_number (new_lp_nr
);
2337 gcc_assert (new_lp
);
2339 /* Unless CHANGE_REGION is true, the new and old landing pad
2340 had better be associated with the same EH region. */
2341 gcc_assert (change_region
|| new_lp
->region
== old_lp
->region
);
2346 gcc_assert (!change_region
);
2349 /* Notice when we redirect the last EH edge away from OLD_BB. */
2350 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
2351 if (e
!= edge_in
&& (e
->flags
& EDGE_EH
))
2356 /* NEW_LP already exists. If there are still edges into OLD_LP,
2357 there's nothing to do with the EH tree. If there are no more
2358 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2359 If CHANGE_REGION is true, then our caller is expecting to remove
2361 if (e
== NULL
&& !change_region
)
2362 remove_eh_landing_pad (old_lp
);
2366 /* No correct landing pad exists. If there are no more edges
2367 into OLD_LP, then we can simply re-use the existing landing pad.
2368 Otherwise, we have to create a new landing pad. */
2371 EH_LANDING_PAD_NR (old_lp
->post_landing_pad
) = 0;
2375 new_lp
= gen_eh_landing_pad (old_lp
->region
);
2376 new_lp
->post_landing_pad
= new_label
;
2377 EH_LANDING_PAD_NR (new_label
) = new_lp
->index
;
2380 /* Maybe move the throwing statement to the new region. */
2381 if (old_lp
!= new_lp
)
2383 remove_stmt_from_eh_lp (throw_stmt
);
2384 add_stmt_to_eh_lp (throw_stmt
, new_lp
->index
);
2388 /* Redirect EH edge E to NEW_BB. */
2391 redirect_eh_edge (edge edge_in
, basic_block new_bb
)
2393 redirect_eh_edge_1 (edge_in
, new_bb
, false);
2394 return ssa_redirect_edge (edge_in
, new_bb
);
2397 /* This is a subroutine of gimple_redirect_edge_and_branch. Update the
2398 labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2399 The actual edge update will happen in the caller. */
2402 redirect_eh_dispatch_edge (geh_dispatch
*stmt
, edge e
, basic_block new_bb
)
2404 tree new_lab
= gimple_block_label (new_bb
);
2405 bool any_changed
= false;
2410 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2414 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2416 old_bb
= label_to_block (cfun
, c
->label
);
2417 if (old_bb
== e
->dest
)
2425 case ERT_ALLOWED_EXCEPTIONS
:
2426 old_bb
= label_to_block (cfun
, r
->u
.allowed
.label
);
2427 gcc_assert (old_bb
== e
->dest
);
2428 r
->u
.allowed
.label
= new_lab
;
2436 gcc_assert (any_changed
);
2439 /* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2442 operation_could_trap_helper_p (enum tree_code op
,
2453 case TRUNC_DIV_EXPR
:
2455 case FLOOR_DIV_EXPR
:
2456 case ROUND_DIV_EXPR
:
2457 case EXACT_DIV_EXPR
:
2459 case FLOOR_MOD_EXPR
:
2460 case ROUND_MOD_EXPR
:
2461 case TRUNC_MOD_EXPR
:
2466 return flag_trapping_math
;
2467 if (!TREE_CONSTANT (divisor
) || integer_zerop (divisor
))
2476 /* Some floating point comparisons may trap. */
2481 case UNORDERED_EXPR
:
2493 /* These operations don't trap with floating point. */
2499 /* ABSU_EXPR never traps. */
2505 /* Any floating arithmetic may trap. */
2506 if (fp_operation
&& flag_trapping_math
)
2514 /* Constructing an object cannot trap. */
2519 /* Whether *COND_EXPR can trap depends on whether the
2520 first argument can trap, so signal it as not handled.
2521 Whether lhs is floating or not doesn't matter. */
2526 /* Any floating arithmetic may trap. */
2527 if (fp_operation
&& flag_trapping_math
)
2535 /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2536 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2537 type operands that may trap. If OP is a division operator, DIVISOR contains
2538 the value of the divisor. */
2541 operation_could_trap_p (enum tree_code op
, bool fp_operation
, bool honor_trapv
,
2544 bool honor_nans
= (fp_operation
&& flag_trapping_math
2545 && !flag_finite_math_only
);
2546 bool honor_snans
= fp_operation
&& flag_signaling_nans
!= 0;
2549 /* This function cannot tell whether or not COND_EXPR and VEC_COND_EXPR could
2550 trap, because that depends on the respective condition op. */
2551 gcc_assert (op
!= COND_EXPR
&& op
!= VEC_COND_EXPR
);
2553 if (TREE_CODE_CLASS (op
) != tcc_comparison
2554 && TREE_CODE_CLASS (op
) != tcc_unary
2555 && TREE_CODE_CLASS (op
) != tcc_binary
)
2558 return operation_could_trap_helper_p (op
, fp_operation
, honor_trapv
,
2559 honor_nans
, honor_snans
, divisor
,
2564 /* Returns true if it is possible to prove that the index of
2565 an array access REF (an ARRAY_REF expression) falls into the
2569 in_array_bounds_p (tree ref
)
2571 tree idx
= TREE_OPERAND (ref
, 1);
2574 if (TREE_CODE (idx
) != INTEGER_CST
)
2577 min
= array_ref_low_bound (ref
);
2578 max
= array_ref_up_bound (ref
);
2581 || TREE_CODE (min
) != INTEGER_CST
2582 || TREE_CODE (max
) != INTEGER_CST
)
2585 if (tree_int_cst_lt (idx
, min
)
2586 || tree_int_cst_lt (max
, idx
))
2592 /* Returns true if it is possible to prove that the range of
2593 an array access REF (an ARRAY_RANGE_REF expression) falls
2594 into the array bounds. */
2597 range_in_array_bounds_p (tree ref
)
2599 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ref
));
2600 tree range_min
, range_max
, min
, max
;
2602 range_min
= TYPE_MIN_VALUE (domain_type
);
2603 range_max
= TYPE_MAX_VALUE (domain_type
);
2606 || TREE_CODE (range_min
) != INTEGER_CST
2607 || TREE_CODE (range_max
) != INTEGER_CST
)
2610 min
= array_ref_low_bound (ref
);
2611 max
= array_ref_up_bound (ref
);
2614 || TREE_CODE (min
) != INTEGER_CST
2615 || TREE_CODE (max
) != INTEGER_CST
)
2618 if (tree_int_cst_lt (range_min
, min
)
2619 || tree_int_cst_lt (max
, range_max
))
2625 /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2626 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2627 This routine expects only GIMPLE lhs or rhs input. */
2630 tree_could_trap_p (tree expr
)
2632 enum tree_code code
;
2633 bool fp_operation
= false;
2634 bool honor_trapv
= false;
2635 tree t
, base
, div
= NULL_TREE
;
2640 /* In COND_EXPR and VEC_COND_EXPR only the condition may trap, but
2641 they won't appear as operands in GIMPLE form, so this is just for the
2642 GENERIC uses where it needs to recurse on the operands and so
2643 *COND_EXPR itself doesn't trap. */
2644 if (TREE_CODE (expr
) == COND_EXPR
|| TREE_CODE (expr
) == VEC_COND_EXPR
)
2647 code
= TREE_CODE (expr
);
2648 t
= TREE_TYPE (expr
);
2652 if (COMPARISON_CLASS_P (expr
))
2653 fp_operation
= FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2655 fp_operation
= FLOAT_TYPE_P (t
);
2656 honor_trapv
= INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
);
2659 if (TREE_CODE_CLASS (code
) == tcc_binary
)
2660 div
= TREE_OPERAND (expr
, 1);
2661 if (operation_could_trap_p (code
, fp_operation
, honor_trapv
, div
))
2671 case VIEW_CONVERT_EXPR
:
2672 case WITH_SIZE_EXPR
:
2673 expr
= TREE_OPERAND (expr
, 0);
2674 code
= TREE_CODE (expr
);
2677 case ARRAY_RANGE_REF
:
2678 base
= TREE_OPERAND (expr
, 0);
2679 if (tree_could_trap_p (base
))
2681 if (TREE_THIS_NOTRAP (expr
))
2683 return !range_in_array_bounds_p (expr
);
2686 base
= TREE_OPERAND (expr
, 0);
2687 if (tree_could_trap_p (base
))
2689 if (TREE_THIS_NOTRAP (expr
))
2691 return !in_array_bounds_p (expr
);
2693 case TARGET_MEM_REF
:
2695 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
2696 && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0)))
2698 if (TREE_THIS_NOTRAP (expr
))
2700 /* We cannot prove that the access is in-bounds when we have
2701 variable-index TARGET_MEM_REFs. */
2702 if (code
== TARGET_MEM_REF
2703 && (TMR_INDEX (expr
) || TMR_INDEX2 (expr
)))
2705 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
)
2707 tree base
= TREE_OPERAND (TREE_OPERAND (expr
, 0), 0);
2708 poly_offset_int off
= mem_ref_offset (expr
);
2709 if (maybe_lt (off
, 0))
2711 if (TREE_CODE (base
) == STRING_CST
)
2712 return maybe_le (TREE_STRING_LENGTH (base
), off
);
2713 tree size
= DECL_SIZE_UNIT (base
);
2714 if (size
== NULL_TREE
2715 || !poly_int_tree_p (size
)
2716 || maybe_le (wi::to_poly_offset (size
), off
))
2718 /* Now we are sure the first byte of the access is inside
2725 return !TREE_THIS_NOTRAP (expr
);
2728 return TREE_THIS_VOLATILE (expr
);
2731 t
= get_callee_fndecl (expr
);
2732 /* Assume that calls to weak functions may trap. */
2733 if (!t
|| !DECL_P (t
))
2736 return tree_could_trap_p (t
);
2740 /* Assume that accesses to weak functions may trap, unless we know
2741 they are certainly defined in current TU or in some other
2743 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
) && DECL_EXTERNAL (expr
))
2745 cgraph_node
*node
= cgraph_node::get (expr
);
2747 node
= node
->function_symbol ();
2748 return !(node
&& node
->in_other_partition
);
2753 /* Assume that accesses to weak vars may trap, unless we know
2754 they are certainly defined in current TU or in some other
2756 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
) && DECL_EXTERNAL (expr
))
2758 varpool_node
*node
= varpool_node::get (expr
);
2760 node
= node
->ultimate_alias_target ();
2761 return !(node
&& node
->in_other_partition
);
2770 /* Return non-NULL if there is an integer operation with trapping overflow
2771 we can rewrite into non-trapping. Called via walk_tree from
2772 rewrite_to_non_trapping_overflow. */
2775 find_trapping_overflow (tree
*tp
, int *walk_subtrees
, void *data
)
2778 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (*tp
))
2779 && !operation_no_trapping_overflow (TREE_TYPE (*tp
), TREE_CODE (*tp
)))
2781 if (IS_TYPE_OR_DECL_P (*tp
)
2782 || (TREE_CODE (*tp
) == SAVE_EXPR
&& data
== NULL
))
2787 /* Rewrite selected operations into unsigned arithmetics, so that they
2788 don't trap on overflow. */
2791 replace_trapping_overflow (tree
*tp
, int *walk_subtrees
, void *data
)
2793 if (find_trapping_overflow (tp
, walk_subtrees
, data
))
2795 tree type
= TREE_TYPE (*tp
);
2796 tree utype
= unsigned_type_for (type
);
2798 int len
= TREE_OPERAND_LENGTH (*tp
);
2799 for (int i
= 0; i
< len
; ++i
)
2800 walk_tree (&TREE_OPERAND (*tp
, i
), replace_trapping_overflow
,
2801 data
, (hash_set
<tree
> *) data
);
2803 if (TREE_CODE (*tp
) == ABS_EXPR
)
2805 TREE_SET_CODE (*tp
, ABSU_EXPR
);
2806 TREE_TYPE (*tp
) = utype
;
2807 *tp
= fold_convert (type
, *tp
);
2811 TREE_TYPE (*tp
) = utype
;
2812 len
= TREE_OPERAND_LENGTH (*tp
);
2813 for (int i
= 0; i
< len
; ++i
)
2814 TREE_OPERAND (*tp
, i
)
2815 = fold_convert (utype
, TREE_OPERAND (*tp
, i
));
2816 *tp
= fold_convert (type
, *tp
);
2822 /* If any subexpression of EXPR can trap due to -ftrapv, rewrite it
2823 using unsigned arithmetics to avoid traps in it. */
2826 rewrite_to_non_trapping_overflow (tree expr
)
2830 hash_set
<tree
> pset
;
2831 if (!walk_tree (&expr
, find_trapping_overflow
, &pset
, &pset
))
2833 expr
= unshare_expr (expr
);
2835 walk_tree (&expr
, replace_trapping_overflow
, &pset
, &pset
);
2839 /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2840 an assignment or a conditional) may throw. */
2843 stmt_could_throw_1_p (gassign
*stmt
)
2845 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2846 bool honor_nans
= false;
2847 bool honor_snans
= false;
2848 bool fp_operation
= false;
2849 bool honor_trapv
= false;
2854 if (TREE_CODE_CLASS (code
) == tcc_comparison
2855 || TREE_CODE_CLASS (code
) == tcc_unary
2856 || TREE_CODE_CLASS (code
) == tcc_binary
)
2858 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2859 t
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
2861 t
= gimple_expr_type (stmt
);
2862 fp_operation
= FLOAT_TYPE_P (t
);
2865 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
2866 honor_snans
= flag_signaling_nans
!= 0;
2868 else if (INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
))
2872 /* First check the LHS. */
2873 if (tree_could_trap_p (gimple_assign_lhs (stmt
)))
2876 /* Check if the main expression may trap. */
2877 ret
= operation_could_trap_helper_p (code
, fp_operation
, honor_trapv
,
2878 honor_nans
, honor_snans
,
2879 gimple_assign_rhs2 (stmt
),
2884 /* If the expression does not trap, see if any of the individual operands may
2886 for (i
= 1; i
< gimple_num_ops (stmt
); i
++)
2887 if (tree_could_trap_p (gimple_op (stmt
, i
)))
2894 /* Return true if statement STMT within FUN could throw an exception. */
2897 stmt_could_throw_p (function
*fun
, gimple
*stmt
)
2899 if (!flag_exceptions
)
2902 /* The only statements that can throw an exception are assignments,
2903 conditionals, calls, resx, and asms. */
2904 switch (gimple_code (stmt
))
2910 return !gimple_call_nothrow_p (as_a
<gcall
*> (stmt
));
2914 if (fun
&& !fun
->can_throw_non_call_exceptions
)
2916 gcond
*cond
= as_a
<gcond
*> (stmt
);
2917 tree lhs
= gimple_cond_lhs (cond
);
2918 return operation_could_trap_p (gimple_cond_code (cond
),
2919 FLOAT_TYPE_P (TREE_TYPE (lhs
)),
2924 if ((fun
&& !fun
->can_throw_non_call_exceptions
)
2925 || gimple_clobber_p (stmt
))
2927 return stmt_could_throw_1_p (as_a
<gassign
*> (stmt
));
2930 if (fun
&& !fun
->can_throw_non_call_exceptions
)
2932 return gimple_asm_volatile_p (as_a
<gasm
*> (stmt
));
2940 /* Return true if expression T could throw an exception. */
2943 tree_could_throw_p (tree t
)
2945 if (!flag_exceptions
)
2947 if (TREE_CODE (t
) == MODIFY_EXPR
)
2949 if (cfun
->can_throw_non_call_exceptions
2950 && tree_could_trap_p (TREE_OPERAND (t
, 0)))
2952 t
= TREE_OPERAND (t
, 1);
2955 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
2956 t
= TREE_OPERAND (t
, 0);
2957 if (TREE_CODE (t
) == CALL_EXPR
)
2958 return (call_expr_flags (t
) & ECF_NOTHROW
) == 0;
2959 if (cfun
->can_throw_non_call_exceptions
)
2960 return tree_could_trap_p (t
);
2964 /* Return true if STMT can throw an exception that is not caught within its
2965 function FUN. FUN can be NULL but the function is extra conservative
2969 stmt_can_throw_external (function
*fun
, gimple
*stmt
)
2973 if (!stmt_could_throw_p (fun
, stmt
))
2978 lp_nr
= lookup_stmt_eh_lp_fn (fun
, stmt
);
2982 /* Return true if STMT can throw an exception that is caught within its
2986 stmt_can_throw_internal (function
*fun
, gimple
*stmt
)
2990 gcc_checking_assert (fun
);
2991 if (!stmt_could_throw_p (fun
, stmt
))
2994 lp_nr
= lookup_stmt_eh_lp_fn (fun
, stmt
);
2998 /* Given a statement STMT in IFUN, if STMT can no longer throw, then
2999 remove any entry it might have from the EH table. Return true if
3000 any change was made. */
3003 maybe_clean_eh_stmt_fn (struct function
*ifun
, gimple
*stmt
)
3005 if (stmt_could_throw_p (ifun
, stmt
))
3007 return remove_stmt_from_eh_lp_fn (ifun
, stmt
);
3010 /* Likewise, but always use the current function. */
3013 maybe_clean_eh_stmt (gimple
*stmt
)
3015 return maybe_clean_eh_stmt_fn (cfun
, stmt
);
3018 /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
3019 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
3020 in the table if it should be in there. Return TRUE if a replacement was
3021 done that my require an EH edge purge. */
3024 maybe_clean_or_replace_eh_stmt (gimple
*old_stmt
, gimple
*new_stmt
)
3026 int lp_nr
= lookup_stmt_eh_lp (old_stmt
);
3030 bool new_stmt_could_throw
= stmt_could_throw_p (cfun
, new_stmt
);
3032 if (new_stmt
== old_stmt
&& new_stmt_could_throw
)
3035 remove_stmt_from_eh_lp (old_stmt
);
3036 if (new_stmt_could_throw
)
3038 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
3048 /* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
3049 in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
3050 operand is the return value of duplicate_eh_regions. */
3053 maybe_duplicate_eh_stmt_fn (struct function
*new_fun
, gimple
*new_stmt
,
3054 struct function
*old_fun
, gimple
*old_stmt
,
3055 hash_map
<void *, void *> *map
,
3058 int old_lp_nr
, new_lp_nr
;
3060 if (!stmt_could_throw_p (new_fun
, new_stmt
))
3063 old_lp_nr
= lookup_stmt_eh_lp_fn (old_fun
, old_stmt
);
3066 if (default_lp_nr
== 0)
3068 new_lp_nr
= default_lp_nr
;
3070 else if (old_lp_nr
> 0)
3072 eh_landing_pad old_lp
, new_lp
;
3074 old_lp
= (*old_fun
->eh
->lp_array
)[old_lp_nr
];
3075 new_lp
= static_cast<eh_landing_pad
> (*map
->get (old_lp
));
3076 new_lp_nr
= new_lp
->index
;
3080 eh_region old_r
, new_r
;
3082 old_r
= (*old_fun
->eh
->region_array
)[-old_lp_nr
];
3083 new_r
= static_cast<eh_region
> (*map
->get (old_r
));
3084 new_lp_nr
= -new_r
->index
;
3087 add_stmt_to_eh_lp_fn (new_fun
, new_stmt
, new_lp_nr
);
3091 /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
3092 and thus no remapping is required. */
3095 maybe_duplicate_eh_stmt (gimple
*new_stmt
, gimple
*old_stmt
)
3099 if (!stmt_could_throw_p (cfun
, new_stmt
))
3102 lp_nr
= lookup_stmt_eh_lp (old_stmt
);
3106 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
3110 /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
3111 GIMPLE_TRY) that are similar enough to be considered the same. Currently
3112 this only handles handlers consisting of a single call, as that's the
3113 important case for C++: a destructor call for a particular object showing
3114 up in multiple handlers. */
3117 same_handler_p (gimple_seq oneh
, gimple_seq twoh
)
3119 gimple_stmt_iterator gsi
;
3120 gimple
*ones
, *twos
;
3123 gsi
= gsi_start (oneh
);
3124 if (!gsi_one_before_end_p (gsi
))
3126 ones
= gsi_stmt (gsi
);
3128 gsi
= gsi_start (twoh
);
3129 if (!gsi_one_before_end_p (gsi
))
3131 twos
= gsi_stmt (gsi
);
3133 if (!is_gimple_call (ones
)
3134 || !is_gimple_call (twos
)
3135 || gimple_call_lhs (ones
)
3136 || gimple_call_lhs (twos
)
3137 || gimple_call_chain (ones
)
3138 || gimple_call_chain (twos
)
3139 || !gimple_call_same_target_p (ones
, twos
)
3140 || gimple_call_num_args (ones
) != gimple_call_num_args (twos
))
3143 for (ai
= 0; ai
< gimple_call_num_args (ones
); ++ai
)
3144 if (!operand_equal_p (gimple_call_arg (ones
, ai
),
3145 gimple_call_arg (twos
, ai
), 0))
3152 try { A() } finally { try { ~B() } catch { ~A() } }
3153 try { ... } finally { ~A() }
3155 try { A() } catch { ~B() }
3156 try { ~B() ... } finally { ~A() }
3158 This occurs frequently in C++, where A is a local variable and B is a
3159 temporary used in the initializer for A. */
3162 optimize_double_finally (gtry
*one
, gtry
*two
)
3165 gimple_stmt_iterator gsi
;
3168 cleanup
= gimple_try_cleanup (one
);
3169 gsi
= gsi_start (cleanup
);
3170 if (!gsi_one_before_end_p (gsi
))
3173 oneh
= gsi_stmt (gsi
);
3174 if (gimple_code (oneh
) != GIMPLE_TRY
3175 || gimple_try_kind (oneh
) != GIMPLE_TRY_CATCH
)
3178 if (same_handler_p (gimple_try_cleanup (oneh
), gimple_try_cleanup (two
)))
3180 gimple_seq seq
= gimple_try_eval (oneh
);
3182 gimple_try_set_cleanup (one
, seq
);
3183 gimple_try_set_kind (one
, GIMPLE_TRY_CATCH
);
3184 seq
= copy_gimple_seq_and_replace_locals (seq
);
3185 gimple_seq_add_seq (&seq
, gimple_try_eval (two
));
3186 gimple_try_set_eval (two
, seq
);
3190 /* Perform EH refactoring optimizations that are simpler to do when code
3191 flow has been lowered but EH structures haven't. */
3194 refactor_eh_r (gimple_seq seq
)
3196 gimple_stmt_iterator gsi
;
3201 gsi
= gsi_start (seq
);
3205 if (gsi_end_p (gsi
))
3208 two
= gsi_stmt (gsi
);
3210 if (gtry
*try_one
= dyn_cast
<gtry
*> (one
))
3211 if (gtry
*try_two
= dyn_cast
<gtry
*> (two
))
3212 if (gimple_try_kind (try_one
) == GIMPLE_TRY_FINALLY
3213 && gimple_try_kind (try_two
) == GIMPLE_TRY_FINALLY
)
3214 optimize_double_finally (try_one
, try_two
);
3216 switch (gimple_code (one
))
3219 refactor_eh_r (gimple_try_eval (one
));
3220 refactor_eh_r (gimple_try_cleanup (one
));
3223 refactor_eh_r (gimple_catch_handler (as_a
<gcatch
*> (one
)));
3225 case GIMPLE_EH_FILTER
:
3226 refactor_eh_r (gimple_eh_filter_failure (one
));
3228 case GIMPLE_EH_ELSE
:
3230 geh_else
*eh_else_stmt
= as_a
<geh_else
*> (one
);
3231 refactor_eh_r (gimple_eh_else_n_body (eh_else_stmt
));
3232 refactor_eh_r (gimple_eh_else_e_body (eh_else_stmt
));
3247 const pass_data pass_data_refactor_eh
=
3249 GIMPLE_PASS
, /* type */
3251 OPTGROUP_NONE
, /* optinfo_flags */
3252 TV_TREE_EH
, /* tv_id */
3253 PROP_gimple_lcf
, /* properties_required */
3254 0, /* properties_provided */
3255 0, /* properties_destroyed */
3256 0, /* todo_flags_start */
3257 0, /* todo_flags_finish */
3260 class pass_refactor_eh
: public gimple_opt_pass
3263 pass_refactor_eh (gcc::context
*ctxt
)
3264 : gimple_opt_pass (pass_data_refactor_eh
, ctxt
)
3267 /* opt_pass methods: */
3268 virtual bool gate (function
*) { return flag_exceptions
!= 0; }
3269 virtual unsigned int execute (function
*)
3271 refactor_eh_r (gimple_body (current_function_decl
));
3275 }; // class pass_refactor_eh
3280 make_pass_refactor_eh (gcc::context
*ctxt
)
3282 return new pass_refactor_eh (ctxt
);
3285 /* At the end of gimple optimization, we can lower RESX. */
3288 lower_resx (basic_block bb
, gresx
*stmt
,
3289 hash_map
<eh_region
, tree
> *mnt_map
)
3292 eh_region src_r
, dst_r
;
3293 gimple_stmt_iterator gsi
;
3298 lp_nr
= lookup_stmt_eh_lp (stmt
);
3300 dst_r
= get_eh_region_from_lp_number (lp_nr
);
3304 src_r
= get_eh_region_from_number (gimple_resx_region (stmt
));
3305 gsi
= gsi_last_bb (bb
);
3309 /* We can wind up with no source region when pass_cleanup_eh shows
3310 that there are no entries into an eh region and deletes it, but
3311 then the block that contains the resx isn't removed. This can
3312 happen without optimization when the switch statement created by
3313 lower_try_finally_switch isn't simplified to remove the eh case.
3315 Resolve this by expanding the resx node to an abort. */
3317 fn
= builtin_decl_implicit (BUILT_IN_TRAP
);
3318 x
= gimple_build_call (fn
, 0);
3319 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3321 while (EDGE_COUNT (bb
->succs
) > 0)
3322 remove_edge (EDGE_SUCC (bb
, 0));
3326 /* When we have a destination region, we resolve this by copying
3327 the excptr and filter values into place, and changing the edge
3328 to immediately after the landing pad. */
3336 /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3337 the failure decl into a new block, if needed. */
3338 gcc_assert (dst_r
->type
== ERT_MUST_NOT_THROW
);
3340 tree
*slot
= mnt_map
->get (dst_r
);
3343 gimple_stmt_iterator gsi2
;
3345 new_bb
= create_empty_bb (bb
);
3346 new_bb
->count
= bb
->count
;
3347 add_bb_to_loop (new_bb
, bb
->loop_father
);
3348 lab
= gimple_block_label (new_bb
);
3349 gsi2
= gsi_start_bb (new_bb
);
3351 fn
= dst_r
->u
.must_not_throw
.failure_decl
;
3352 x
= gimple_build_call (fn
, 0);
3353 gimple_set_location (x
, dst_r
->u
.must_not_throw
.failure_loc
);
3354 gsi_insert_after (&gsi2
, x
, GSI_CONTINUE_LINKING
);
3356 mnt_map
->put (dst_r
, lab
);
3361 new_bb
= label_to_block (cfun
, lab
);
3364 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3365 e
= make_single_succ_edge (bb
, new_bb
, EDGE_FALLTHRU
);
3370 tree dst_nr
= build_int_cst (integer_type_node
, dst_r
->index
);
3372 fn
= builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES
);
3373 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3374 x
= gimple_build_call (fn
, 2, dst_nr
, src_nr
);
3375 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3377 /* Update the flags for the outgoing edge. */
3378 e
= single_succ_edge (bb
);
3379 gcc_assert (e
->flags
& EDGE_EH
);
3380 e
->flags
= (e
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
3381 e
->probability
= profile_probability::always ();
3383 /* If there are no more EH users of the landing pad, delete it. */
3384 FOR_EACH_EDGE (e
, ei
, e
->dest
->preds
)
3385 if (e
->flags
& EDGE_EH
)
3389 eh_landing_pad lp
= get_eh_landing_pad_from_number (lp_nr
);
3390 remove_eh_landing_pad (lp
);
3400 /* When we don't have a destination region, this exception escapes
3401 up the call chain. We resolve this by generating a call to the
3402 _Unwind_Resume library function. */
3404 /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3405 with no arguments for C++. Check for that. */
3406 if (src_r
->use_cxa_end_cleanup
)
3408 fn
= builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP
);
3409 x
= gimple_build_call (fn
, 0);
3410 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3414 fn
= builtin_decl_implicit (BUILT_IN_EH_POINTER
);
3415 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3416 x
= gimple_build_call (fn
, 1, src_nr
);
3417 var
= create_tmp_var (ptr_type_node
);
3418 var
= make_ssa_name (var
, x
);
3419 gimple_call_set_lhs (x
, var
);
3420 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3422 /* When exception handling is delegated to a caller function, we
3423 have to guarantee that shadow memory variables living on stack
3424 will be cleaner before control is given to a parent function. */
3425 if (sanitize_flags_p (SANITIZE_ADDRESS
))
3428 = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN
);
3429 gimple
*g
= gimple_build_call (decl
, 0);
3430 gimple_set_location (g
, gimple_location (stmt
));
3431 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3434 fn
= builtin_decl_implicit (BUILT_IN_UNWIND_RESUME
);
3435 x
= gimple_build_call (fn
, 1, var
);
3436 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3439 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3442 gsi_remove (&gsi
, true);
3449 const pass_data pass_data_lower_resx
=
3451 GIMPLE_PASS
, /* type */
3453 OPTGROUP_NONE
, /* optinfo_flags */
3454 TV_TREE_EH
, /* tv_id */
3455 PROP_gimple_lcf
, /* properties_required */
3456 0, /* properties_provided */
3457 0, /* properties_destroyed */
3458 0, /* todo_flags_start */
3459 0, /* todo_flags_finish */
3462 class pass_lower_resx
: public gimple_opt_pass
3465 pass_lower_resx (gcc::context
*ctxt
)
3466 : gimple_opt_pass (pass_data_lower_resx
, ctxt
)
3469 /* opt_pass methods: */
3470 virtual bool gate (function
*) { return flag_exceptions
!= 0; }
3471 virtual unsigned int execute (function
*);
3473 }; // class pass_lower_resx
3476 pass_lower_resx::execute (function
*fun
)
3479 bool dominance_invalidated
= false;
3480 bool any_rewritten
= false;
3482 hash_map
<eh_region
, tree
> mnt_map
;
3484 FOR_EACH_BB_FN (bb
, fun
)
3486 gimple
*last
= last_stmt (bb
);
3487 if (last
&& is_gimple_resx (last
))
3489 dominance_invalidated
|=
3490 lower_resx (bb
, as_a
<gresx
*> (last
), &mnt_map
);
3491 any_rewritten
= true;
3495 if (dominance_invalidated
)
3497 free_dominance_info (CDI_DOMINATORS
);
3498 free_dominance_info (CDI_POST_DOMINATORS
);
3501 return any_rewritten
? TODO_update_ssa_only_virtuals
: 0;
3507 make_pass_lower_resx (gcc::context
*ctxt
)
3509 return new pass_lower_resx (ctxt
);
3512 /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3516 optimize_clobbers (basic_block bb
)
3518 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
3519 bool any_clobbers
= false;
3520 bool seen_stack_restore
= false;
3524 /* Only optimize anything if the bb contains at least one clobber,
3525 ends with resx (checked by caller), optionally contains some
3526 debug stmts or labels, or at most one __builtin_stack_restore
3527 call, and has an incoming EH edge. */
3528 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3530 gimple
*stmt
= gsi_stmt (gsi
);
3531 if (is_gimple_debug (stmt
))
3533 if (gimple_clobber_p (stmt
))
3535 any_clobbers
= true;
3538 if (!seen_stack_restore
3539 && gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
3541 seen_stack_restore
= true;
3544 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3550 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3551 if (e
->flags
& EDGE_EH
)
3555 gsi
= gsi_last_bb (bb
);
3556 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3558 gimple
*stmt
= gsi_stmt (gsi
);
3559 if (!gimple_clobber_p (stmt
))
3561 unlink_stmt_vdef (stmt
);
3562 gsi_remove (&gsi
, true);
3563 release_defs (stmt
);
3567 /* Try to sink var = {v} {CLOBBER} stmts followed just by
3568 internal throw to successor BB.
3569 SUNK, if not NULL, is an array of sequences indexed by basic-block
3570 index to sink to and to pick up sinking opportunities from.
3571 If FOUND_OPPORTUNITY is not NULL then do not perform the optimization
3572 but set *FOUND_OPPORTUNITY to true. */
3575 sink_clobbers (basic_block bb
,
3576 gimple_seq
*sunk
= NULL
, bool *found_opportunity
= NULL
)
3580 gimple_stmt_iterator gsi
, dgsi
;
3582 bool any_clobbers
= false;
3585 /* Only optimize if BB has a single EH successor and
3586 all predecessor edges are EH too. */
3587 if (!single_succ_p (bb
)
3588 || (single_succ_edge (bb
)->flags
& EDGE_EH
) == 0)
3591 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3593 if ((e
->flags
& EDGE_EH
) == 0)
3597 /* And BB contains only CLOBBER stmts before the final
3599 gsi
= gsi_last_bb (bb
);
3600 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3602 gimple
*stmt
= gsi_stmt (gsi
);
3603 if (is_gimple_debug (stmt
))
3605 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3607 if (!gimple_clobber_p (stmt
))
3609 any_clobbers
= true;
3611 if (!any_clobbers
&& (!sunk
|| gimple_seq_empty_p (sunk
[bb
->index
])))
3614 /* If this was a dry run, tell it we found clobbers to sink. */
3615 if (found_opportunity
)
3617 *found_opportunity
= true;
3621 edge succe
= single_succ_edge (bb
);
3622 succbb
= succe
->dest
;
3624 /* See if there is a virtual PHI node to take an updated virtual
3627 for (gphi_iterator gpi
= gsi_start_phis (succbb
);
3628 !gsi_end_p (gpi
); gsi_next (&gpi
))
3630 tree res
= gimple_phi_result (gpi
.phi ());
3631 if (virtual_operand_p (res
))
3638 gimple
*first_sunk
= NULL
;
3639 gimple
*last_sunk
= NULL
;
3640 if (sunk
&& !(succbb
->flags
& BB_VISITED
))
3641 dgsi
= gsi_start (sunk
[succbb
->index
]);
3643 dgsi
= gsi_after_labels (succbb
);
3644 gsi
= gsi_last_bb (bb
);
3645 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3647 gimple
*stmt
= gsi_stmt (gsi
);
3649 if (is_gimple_debug (stmt
))
3651 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3653 lhs
= gimple_assign_lhs (stmt
);
3654 /* Unfortunately we don't have dominance info updated at this
3655 point, so checking if
3656 dominated_by_p (CDI_DOMINATORS, succbb,
3657 gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0)))
3658 would be too costly. Thus, avoid sinking any clobbers that
3659 refer to non-(D) SSA_NAMEs. */
3660 if (TREE_CODE (lhs
) == MEM_REF
3661 && TREE_CODE (TREE_OPERAND (lhs
, 0)) == SSA_NAME
3662 && !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs
, 0)))
3664 unlink_stmt_vdef (stmt
);
3665 gsi_remove (&gsi
, true);
3666 release_defs (stmt
);
3670 /* As we do not change stmt order when sinking across a
3671 forwarder edge we can keep virtual operands in place. */
3672 gsi_remove (&gsi
, false);
3673 gsi_insert_before (&dgsi
, stmt
, GSI_NEW_STMT
);
3678 if (sunk
&& !gimple_seq_empty_p (sunk
[bb
->index
]))
3681 first_sunk
= gsi_stmt (gsi_last (sunk
[bb
->index
]));
3682 last_sunk
= gsi_stmt (gsi_start (sunk
[bb
->index
]));
3683 gsi_insert_seq_before_without_update (&dgsi
,
3684 sunk
[bb
->index
], GSI_NEW_STMT
);
3685 sunk
[bb
->index
] = NULL
;
3689 /* Adjust virtual operands if we sunk across a virtual PHI. */
3692 imm_use_iterator iter
;
3693 use_operand_p use_p
;
3695 tree phi_def
= gimple_phi_result (vphi
);
3696 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, phi_def
)
3697 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3698 SET_USE (use_p
, gimple_vdef (first_sunk
));
3699 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phi_def
))
3701 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (first_sunk
)) = 1;
3702 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phi_def
) = 0;
3704 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (vphi
, succe
),
3705 gimple_vuse (last_sunk
));
3706 SET_USE (gimple_vuse_op (last_sunk
), phi_def
);
3708 /* If there isn't a single predecessor but no virtual PHI node
3709 arrange for virtual operands to be renamed. */
3710 else if (!single_pred_p (succbb
)
3711 && TREE_CODE (gimple_vuse (last_sunk
)) == SSA_NAME
)
3713 mark_virtual_operand_for_renaming (gimple_vuse (last_sunk
));
3714 todo
|= TODO_update_ssa_only_virtuals
;
3721 /* At the end of inlining, we can lower EH_DISPATCH. Return true when
3722 we have found some duplicate labels and removed some edges. */
3725 lower_eh_dispatch (basic_block src
, geh_dispatch
*stmt
)
3727 gimple_stmt_iterator gsi
;
3732 bool redirected
= false;
3734 region_nr
= gimple_eh_dispatch_region (stmt
);
3735 r
= get_eh_region_from_number (region_nr
);
3737 gsi
= gsi_last_bb (src
);
3743 auto_vec
<tree
> labels
;
3744 tree default_label
= NULL
;
3748 hash_set
<tree
> seen_values
;
3750 /* Collect the labels for a switch. Zero the post_landing_pad
3751 field becase we'll no longer have anything keeping these labels
3752 in existence and the optimizer will be free to merge these
3754 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
3756 tree tp_node
, flt_node
, lab
= c
->label
;
3757 bool have_label
= false;
3760 tp_node
= c
->type_list
;
3761 flt_node
= c
->filter_list
;
3763 if (tp_node
== NULL
)
3765 default_label
= lab
;
3770 /* Filter out duplicate labels that arise when this handler
3771 is shadowed by an earlier one. When no labels are
3772 attached to the handler anymore, we remove
3773 the corresponding edge and then we delete unreachable
3774 blocks at the end of this pass. */
3775 if (! seen_values
.contains (TREE_VALUE (flt_node
)))
3777 tree t
= build_case_label (TREE_VALUE (flt_node
),
3779 labels
.safe_push (t
);
3780 seen_values
.add (TREE_VALUE (flt_node
));
3784 tp_node
= TREE_CHAIN (tp_node
);
3785 flt_node
= TREE_CHAIN (flt_node
);
3790 remove_edge (find_edge (src
, label_to_block (cfun
, lab
)));
3795 /* Clean up the edge flags. */
3796 FOR_EACH_EDGE (e
, ei
, src
->succs
)
3798 if (e
->flags
& EDGE_FALLTHRU
)
3800 /* If there was no catch-all, use the fallthru edge. */
3801 if (default_label
== NULL
)
3802 default_label
= gimple_block_label (e
->dest
);
3803 e
->flags
&= ~EDGE_FALLTHRU
;
3806 gcc_assert (default_label
!= NULL
);
3808 /* Don't generate a switch if there's only a default case.
3809 This is common in the form of try { A; } catch (...) { B; }. */
3810 if (!labels
.exists ())
3812 e
= single_succ_edge (src
);
3813 e
->flags
|= EDGE_FALLTHRU
;
3817 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3818 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3820 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)));
3821 filter
= make_ssa_name (filter
, x
);
3822 gimple_call_set_lhs (x
, filter
);
3823 gimple_set_location (x
, gimple_location (stmt
));
3824 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3826 /* Turn the default label into a default case. */
3827 default_label
= build_case_label (NULL
, NULL
, default_label
);
3828 sort_case_labels (labels
);
3830 x
= gimple_build_switch (filter
, default_label
, labels
);
3831 gimple_set_location (x
, gimple_location (stmt
));
3832 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3837 case ERT_ALLOWED_EXCEPTIONS
:
3839 edge b_e
= BRANCH_EDGE (src
);
3840 edge f_e
= FALLTHRU_EDGE (src
);
3842 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3843 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3845 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)));
3846 filter
= make_ssa_name (filter
, x
);
3847 gimple_call_set_lhs (x
, filter
);
3848 gimple_set_location (x
, gimple_location (stmt
));
3849 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3851 r
->u
.allowed
.label
= NULL
;
3852 x
= gimple_build_cond (EQ_EXPR
, filter
,
3853 build_int_cst (TREE_TYPE (filter
),
3854 r
->u
.allowed
.filter
),
3855 NULL_TREE
, NULL_TREE
);
3856 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3858 b_e
->flags
= b_e
->flags
| EDGE_TRUE_VALUE
;
3859 f_e
->flags
= (f_e
->flags
& ~EDGE_FALLTHRU
) | EDGE_FALSE_VALUE
;
3867 /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3868 gsi_remove (&gsi
, true);
3874 const pass_data pass_data_lower_eh_dispatch
=
3876 GIMPLE_PASS
, /* type */
3877 "ehdisp", /* name */
3878 OPTGROUP_NONE
, /* optinfo_flags */
3879 TV_TREE_EH
, /* tv_id */
3880 PROP_gimple_lcf
, /* properties_required */
3881 0, /* properties_provided */
3882 0, /* properties_destroyed */
3883 0, /* todo_flags_start */
3884 0, /* todo_flags_finish */
3887 class pass_lower_eh_dispatch
: public gimple_opt_pass
3890 pass_lower_eh_dispatch (gcc::context
*ctxt
)
3891 : gimple_opt_pass (pass_data_lower_eh_dispatch
, ctxt
)
3894 /* opt_pass methods: */
3895 virtual bool gate (function
*fun
) { return fun
->eh
->region_tree
!= NULL
; }
3896 virtual unsigned int execute (function
*);
3898 }; // class pass_lower_eh_dispatch
3901 pass_lower_eh_dispatch::execute (function
*fun
)
3905 bool redirected
= false;
3906 bool any_resx_to_process
= false;
3908 assign_filter_values ();
3910 FOR_EACH_BB_FN (bb
, fun
)
3912 gimple
*last
= last_stmt (bb
);
3915 if (gimple_code (last
) == GIMPLE_EH_DISPATCH
)
3917 redirected
|= lower_eh_dispatch (bb
,
3918 as_a
<geh_dispatch
*> (last
));
3919 flags
|= TODO_update_ssa_only_virtuals
;
3921 else if (gimple_code (last
) == GIMPLE_RESX
)
3923 if (stmt_can_throw_external (fun
, last
))
3924 optimize_clobbers (bb
);
3925 else if (!any_resx_to_process
)
3926 sink_clobbers (bb
, NULL
, &any_resx_to_process
);
3928 bb
->flags
&= ~BB_VISITED
;
3932 free_dominance_info (CDI_DOMINATORS
);
3933 delete_unreachable_blocks ();
3936 if (any_resx_to_process
)
3938 /* Make sure to catch all secondary sinking opportunities by processing
3939 blocks in RPO order and after all CFG modifications from lowering
3940 and unreachable block removal. */
3941 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (fun
));
3942 int rpo_n
= pre_and_rev_post_order_compute_fn (fun
, NULL
, rpo
, false);
3943 gimple_seq
*sunk
= XCNEWVEC (gimple_seq
, last_basic_block_for_fn (fun
));
3944 for (int i
= 0; i
< rpo_n
; ++i
)
3946 bb
= BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]);
3947 gimple
*last
= last_stmt (bb
);
3949 && gimple_code (last
) == GIMPLE_RESX
3950 && !stmt_can_throw_external (fun
, last
))
3951 flags
|= sink_clobbers (bb
, sunk
);
3952 /* If there were any clobbers sunk into this BB, insert them now. */
3953 if (!gimple_seq_empty_p (sunk
[bb
->index
]))
3955 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
3956 gsi_insert_seq_before (&gsi
, sunk
[bb
->index
], GSI_NEW_STMT
);
3957 sunk
[bb
->index
] = NULL
;
3959 bb
->flags
|= BB_VISITED
;
3971 make_pass_lower_eh_dispatch (gcc::context
*ctxt
)
3973 return new pass_lower_eh_dispatch (ctxt
);
3976 /* Walk statements, see what regions and, optionally, landing pads
3977 are really referenced.
3979 Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
3980 and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
3982 Passing NULL for LP_REACHABLE is valid, in this case only reachable
3985 The caller is responsible for freeing the returned sbitmaps. */
3988 mark_reachable_handlers (sbitmap
*r_reachablep
, sbitmap
*lp_reachablep
)
3990 sbitmap r_reachable
, lp_reachable
;
3992 bool mark_landing_pads
= (lp_reachablep
!= NULL
);
3993 gcc_checking_assert (r_reachablep
!= NULL
);
3995 r_reachable
= sbitmap_alloc (cfun
->eh
->region_array
->length ());
3996 bitmap_clear (r_reachable
);
3997 *r_reachablep
= r_reachable
;
3999 if (mark_landing_pads
)
4001 lp_reachable
= sbitmap_alloc (cfun
->eh
->lp_array
->length ());
4002 bitmap_clear (lp_reachable
);
4003 *lp_reachablep
= lp_reachable
;
4006 lp_reachable
= NULL
;
4008 FOR_EACH_BB_FN (bb
, cfun
)
4010 gimple_stmt_iterator gsi
;
4012 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4014 gimple
*stmt
= gsi_stmt (gsi
);
4016 if (mark_landing_pads
)
4018 int lp_nr
= lookup_stmt_eh_lp (stmt
);
4020 /* Negative LP numbers are MUST_NOT_THROW regions which
4021 are not considered BB enders. */
4023 bitmap_set_bit (r_reachable
, -lp_nr
);
4025 /* Positive LP numbers are real landing pads, and BB enders. */
4028 gcc_assert (gsi_one_before_end_p (gsi
));
4029 eh_region region
= get_eh_region_from_lp_number (lp_nr
);
4030 bitmap_set_bit (r_reachable
, region
->index
);
4031 bitmap_set_bit (lp_reachable
, lp_nr
);
4035 /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
4036 switch (gimple_code (stmt
))
4039 bitmap_set_bit (r_reachable
,
4040 gimple_resx_region (as_a
<gresx
*> (stmt
)));
4042 case GIMPLE_EH_DISPATCH
:
4043 bitmap_set_bit (r_reachable
,
4044 gimple_eh_dispatch_region (
4045 as_a
<geh_dispatch
*> (stmt
)));
4048 if (gimple_call_builtin_p (stmt
, BUILT_IN_EH_COPY_VALUES
))
4049 for (int i
= 0; i
< 2; ++i
)
4051 tree rt
= gimple_call_arg (stmt
, i
);
4052 HOST_WIDE_INT ri
= tree_to_shwi (rt
);
4054 gcc_assert (ri
== (int)ri
);
4055 bitmap_set_bit (r_reachable
, ri
);
4065 /* Remove unreachable handlers and unreachable landing pads. */
4068 remove_unreachable_handlers (void)
4070 sbitmap r_reachable
, lp_reachable
;
4075 mark_reachable_handlers (&r_reachable
, &lp_reachable
);
4079 fprintf (dump_file
, "Before removal of unreachable regions:\n");
4080 dump_eh_tree (dump_file
, cfun
);
4081 fprintf (dump_file
, "Reachable regions: ");
4082 dump_bitmap_file (dump_file
, r_reachable
);
4083 fprintf (dump_file
, "Reachable landing pads: ");
4084 dump_bitmap_file (dump_file
, lp_reachable
);
4089 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
4090 if (region
&& !bitmap_bit_p (r_reachable
, region
->index
))
4092 "Removing unreachable region %d\n",
4096 remove_unreachable_eh_regions (r_reachable
);
4098 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
4099 if (lp
&& !bitmap_bit_p (lp_reachable
, lp
->index
))
4103 "Removing unreachable landing pad %d\n",
4105 remove_eh_landing_pad (lp
);
4110 fprintf (dump_file
, "\n\nAfter removal of unreachable regions:\n");
4111 dump_eh_tree (dump_file
, cfun
);
4112 fprintf (dump_file
, "\n\n");
4115 sbitmap_free (r_reachable
);
4116 sbitmap_free (lp_reachable
);
4119 verify_eh_tree (cfun
);
4122 /* Remove unreachable handlers if any landing pads have been removed after
4123 last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
4126 maybe_remove_unreachable_handlers (void)
4131 if (cfun
->eh
== NULL
)
4134 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
4136 && (lp
->post_landing_pad
== NULL_TREE
4137 || label_to_block (cfun
, lp
->post_landing_pad
) == NULL
))
4139 remove_unreachable_handlers ();
4144 /* Remove regions that do not have landing pads. This assumes
4145 that remove_unreachable_handlers has already been run, and
4146 that we've just manipulated the landing pads since then.
4148 Preserve regions with landing pads and regions that prevent
4149 exceptions from propagating further, even if these regions
4150 are not reachable. */
4153 remove_unreachable_handlers_no_lp (void)
4156 sbitmap r_reachable
;
4159 mark_reachable_handlers (&r_reachable
, /*lp_reachablep=*/NULL
);
4161 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
4166 if (region
->landing_pads
!= NULL
4167 || region
->type
== ERT_MUST_NOT_THROW
)
4168 bitmap_set_bit (r_reachable
, region
->index
);
4171 && !bitmap_bit_p (r_reachable
, region
->index
))
4173 "Removing unreachable region %d\n",
4177 remove_unreachable_eh_regions (r_reachable
);
4179 sbitmap_free (r_reachable
);
4182 /* Undo critical edge splitting on an EH landing pad. Earlier, we
4183 optimisticaly split all sorts of edges, including EH edges. The
4184 optimization passes in between may not have needed them; if not,
4185 we should undo the split.
4187 Recognize this case by having one EH edge incoming to the BB and
4188 one normal edge outgoing; BB should be empty apart from the
4189 post_landing_pad label.
4191 Note that this is slightly different from the empty handler case
4192 handled by cleanup_empty_eh, in that the actual handler may yet
4193 have actual code but the landing pad has been separated from the
4194 handler. As such, cleanup_empty_eh relies on this transformation
4195 having been done first. */
4198 unsplit_eh (eh_landing_pad lp
)
4200 basic_block bb
= label_to_block (cfun
, lp
->post_landing_pad
);
4201 gimple_stmt_iterator gsi
;
4204 /* Quickly check the edge counts on BB for singularity. */
4205 if (!single_pred_p (bb
) || !single_succ_p (bb
))
4207 e_in
= single_pred_edge (bb
);
4208 e_out
= single_succ_edge (bb
);
4210 /* Input edge must be EH and output edge must be normal. */
4211 if ((e_in
->flags
& EDGE_EH
) == 0 || (e_out
->flags
& EDGE_EH
) != 0)
4214 /* The block must be empty except for the labels and debug insns. */
4215 gsi
= gsi_after_labels (bb
);
4216 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4217 gsi_next_nondebug (&gsi
);
4218 if (!gsi_end_p (gsi
))
4221 /* The destination block must not already have a landing pad
4222 for a different region. */
4223 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4225 glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (gsi
));
4231 lab
= gimple_label_label (label_stmt
);
4232 lp_nr
= EH_LANDING_PAD_NR (lab
);
4233 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4237 /* The new destination block must not already be a destination of
4238 the source block, lest we merge fallthru and eh edges and get
4239 all sorts of confused. */
4240 if (find_edge (e_in
->src
, e_out
->dest
))
4243 /* ??? We can get degenerate phis due to cfg cleanups. I would have
4244 thought this should have been cleaned up by a phicprop pass, but
4245 that doesn't appear to handle virtuals. Propagate by hand. */
4246 if (!gimple_seq_empty_p (phi_nodes (bb
)))
4248 for (gphi_iterator gpi
= gsi_start_phis (bb
); !gsi_end_p (gpi
); )
4251 gphi
*phi
= gpi
.phi ();
4252 tree lhs
= gimple_phi_result (phi
);
4253 tree rhs
= gimple_phi_arg_def (phi
, 0);
4254 use_operand_p use_p
;
4255 imm_use_iterator iter
;
4257 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
4259 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4260 SET_USE (use_p
, rhs
);
4263 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
4264 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
) = 1;
4266 remove_phi_node (&gpi
, true);
4270 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4271 fprintf (dump_file
, "Unsplit EH landing pad %d to block %i.\n",
4272 lp
->index
, e_out
->dest
->index
);
4274 /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
4275 a successor edge, humor it. But do the real CFG change with the
4276 predecessor of E_OUT in order to preserve the ordering of arguments
4277 to the PHI nodes in E_OUT->DEST. */
4278 redirect_eh_edge_1 (e_in
, e_out
->dest
, false);
4279 redirect_edge_pred (e_out
, e_in
->src
);
4280 e_out
->flags
= e_in
->flags
;
4281 e_out
->probability
= e_in
->probability
;
4287 /* Examine each landing pad block and see if it matches unsplit_eh. */
4290 unsplit_all_eh (void)
4292 bool changed
= false;
4296 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4298 changed
|= unsplit_eh (lp
);
4303 /* Wrapper around unsplit_all_eh that makes it usable everywhere. */
4306 unsplit_eh_edges (void)
4310 /* unsplit_all_eh can die looking up unreachable landing pads. */
4311 maybe_remove_unreachable_handlers ();
4313 changed
= unsplit_all_eh ();
4315 /* If EH edges have been unsplit, delete unreachable forwarder blocks. */
4318 free_dominance_info (CDI_DOMINATORS
);
4319 free_dominance_info (CDI_POST_DOMINATORS
);
4320 delete_unreachable_blocks ();
4324 /* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
4325 to OLD_BB to NEW_BB; return true on success, false on failure.
4327 OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
4328 PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
4329 Virtual PHIs may be deleted and marked for renaming. */
4332 cleanup_empty_eh_merge_phis (basic_block new_bb
, basic_block old_bb
,
4333 edge old_bb_out
, bool change_region
)
4335 gphi_iterator ngsi
, ogsi
;
4338 bitmap ophi_handled
;
4340 /* The destination block must not be a regular successor for any
4341 of the preds of the landing pad. Thus, avoid turning
4351 which CFG verification would choke on. See PR45172 and PR51089. */
4352 if (!single_pred_p (new_bb
))
4353 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4354 if (find_edge (e
->src
, new_bb
))
4357 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4358 redirect_edge_var_map_clear (e
);
4360 ophi_handled
= BITMAP_ALLOC (NULL
);
4362 /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
4363 for the edges we're going to move. */
4364 for (ngsi
= gsi_start_phis (new_bb
); !gsi_end_p (ngsi
); gsi_next (&ngsi
))
4366 gphi
*ophi
, *nphi
= ngsi
.phi ();
4369 nresult
= gimple_phi_result (nphi
);
4370 nop
= gimple_phi_arg_def (nphi
, old_bb_out
->dest_idx
);
4372 /* Find the corresponding PHI in OLD_BB so we can forward-propagate
4373 the source ssa_name. */
4375 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4378 if (gimple_phi_result (ophi
) == nop
)
4383 /* If we did find the corresponding PHI, copy those inputs. */
4386 /* If NOP is used somewhere else beyond phis in new_bb, give up. */
4387 if (!has_single_use (nop
))
4389 imm_use_iterator imm_iter
;
4390 use_operand_p use_p
;
4392 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, nop
)
4394 if (!gimple_debug_bind_p (USE_STMT (use_p
))
4395 && (gimple_code (USE_STMT (use_p
)) != GIMPLE_PHI
4396 || gimple_bb (USE_STMT (use_p
)) != new_bb
))
4400 bitmap_set_bit (ophi_handled
, SSA_NAME_VERSION (nop
));
4401 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4406 if ((e
->flags
& EDGE_EH
) == 0)
4408 oop
= gimple_phi_arg_def (ophi
, e
->dest_idx
);
4409 oloc
= gimple_phi_arg_location (ophi
, e
->dest_idx
);
4410 redirect_edge_var_map_add (e
, nresult
, oop
, oloc
);
4413 /* If we didn't find the PHI, if it's a real variable or a VOP, we know
4414 from the fact that OLD_BB is tree_empty_eh_handler_p that the
4415 variable is unchanged from input to the block and we can simply
4416 re-use the input to NEW_BB from the OLD_BB_OUT edge. */
4420 = gimple_phi_arg_location (nphi
, old_bb_out
->dest_idx
);
4421 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4422 redirect_edge_var_map_add (e
, nresult
, nop
, nloc
);
4426 /* Second, verify that all PHIs from OLD_BB have been handled. If not,
4427 we don't know what values from the other edges into NEW_BB to use. */
4428 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4430 gphi
*ophi
= ogsi
.phi ();
4431 tree oresult
= gimple_phi_result (ophi
);
4432 if (!bitmap_bit_p (ophi_handled
, SSA_NAME_VERSION (oresult
)))
4436 /* Finally, move the edges and update the PHIs. */
4437 for (ei
= ei_start (old_bb
->preds
); (e
= ei_safe_edge (ei
)); )
4438 if (e
->flags
& EDGE_EH
)
4440 /* ??? CFG manipluation routines do not try to update loop
4441 form on edge redirection. Do so manually here for now. */
4442 /* If we redirect a loop entry or latch edge that will either create
4443 a multiple entry loop or rotate the loop. If the loops merge
4444 we may have created a loop with multiple latches.
4445 All of this isn't easily fixed thus cancel the affected loop
4446 and mark the other loop as possibly having multiple latches. */
4447 if (e
->dest
== e
->dest
->loop_father
->header
)
4449 mark_loop_for_removal (e
->dest
->loop_father
);
4450 new_bb
->loop_father
->latch
= NULL
;
4451 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
4453 redirect_eh_edge_1 (e
, new_bb
, change_region
);
4454 redirect_edge_succ (e
, new_bb
);
4455 flush_pending_stmts (e
);
4460 BITMAP_FREE (ophi_handled
);
4464 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4465 redirect_edge_var_map_clear (e
);
4466 BITMAP_FREE (ophi_handled
);
4470 /* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4471 old region to NEW_REGION at BB. */
4474 cleanup_empty_eh_move_lp (basic_block bb
, edge e_out
,
4475 eh_landing_pad lp
, eh_region new_region
)
4477 gimple_stmt_iterator gsi
;
4480 for (pp
= &lp
->region
->landing_pads
; *pp
!= lp
; pp
= &(*pp
)->next_lp
)
4484 lp
->region
= new_region
;
4485 lp
->next_lp
= new_region
->landing_pads
;
4486 new_region
->landing_pads
= lp
;
4488 /* Delete the RESX that was matched within the empty handler block. */
4489 gsi
= gsi_last_bb (bb
);
4490 unlink_stmt_vdef (gsi_stmt (gsi
));
4491 gsi_remove (&gsi
, true);
4493 /* Clean up E_OUT for the fallthru. */
4494 e_out
->flags
= (e_out
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
4495 e_out
->probability
= profile_probability::always ();
4498 /* A subroutine of cleanup_empty_eh. Handle more complex cases of
4499 unsplitting than unsplit_eh was prepared to handle, e.g. when
4500 multiple incoming edges and phis are involved. */
4503 cleanup_empty_eh_unsplit (basic_block bb
, edge e_out
, eh_landing_pad lp
)
4505 gimple_stmt_iterator gsi
;
4508 /* We really ought not have totally lost everything following
4509 a landing pad label. Given that BB is empty, there had better
4511 gcc_assert (e_out
!= NULL
);
4513 /* The destination block must not already have a landing pad
4514 for a different region. */
4516 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4518 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (gsi
));
4523 lab
= gimple_label_label (stmt
);
4524 lp_nr
= EH_LANDING_PAD_NR (lab
);
4525 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4529 /* Attempt to move the PHIs into the successor block. */
4530 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, false))
4532 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4534 "Unsplit EH landing pad %d to block %i "
4535 "(via cleanup_empty_eh).\n",
4536 lp
->index
, e_out
->dest
->index
);
4543 /* Return true if edge E_FIRST is part of an empty infinite loop
4544 or leads to such a loop through a series of single successor
4548 infinite_empty_loop_p (edge e_first
)
4550 bool inf_loop
= false;
4553 if (e_first
->dest
== e_first
->src
)
4556 e_first
->src
->aux
= (void *) 1;
4557 for (e
= e_first
; single_succ_p (e
->dest
); e
= single_succ_edge (e
->dest
))
4559 gimple_stmt_iterator gsi
;
4565 e
->dest
->aux
= (void *) 1;
4566 gsi
= gsi_after_labels (e
->dest
);
4567 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4568 gsi_next_nondebug (&gsi
);
4569 if (!gsi_end_p (gsi
))
4572 e_first
->src
->aux
= NULL
;
4573 for (e
= e_first
; e
->dest
->aux
; e
= single_succ_edge (e
->dest
))
4574 e
->dest
->aux
= NULL
;
4579 /* Examine the block associated with LP to determine if it's an empty
4580 handler for its EH region. If so, attempt to redirect EH edges to
4581 an outer region. Return true the CFG was updated in any way. This
4582 is similar to jump forwarding, just across EH edges. */
4585 cleanup_empty_eh (eh_landing_pad lp
)
4587 basic_block bb
= label_to_block (cfun
, lp
->post_landing_pad
);
4588 gimple_stmt_iterator gsi
;
4590 eh_region new_region
;
4593 bool has_non_eh_pred
;
4597 /* There can be zero or one edges out of BB. This is the quickest test. */
4598 switch (EDGE_COUNT (bb
->succs
))
4604 e_out
= single_succ_edge (bb
);
4610 gsi
= gsi_last_nondebug_bb (bb
);
4611 resx
= gsi_stmt (gsi
);
4612 if (resx
&& is_gimple_resx (resx
))
4614 if (stmt_can_throw_external (cfun
, resx
))
4615 optimize_clobbers (bb
);
4616 else if (sink_clobbers (bb
))
4620 gsi
= gsi_after_labels (bb
);
4622 /* Make sure to skip debug statements. */
4623 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4624 gsi_next_nondebug (&gsi
);
4626 /* If the block is totally empty, look for more unsplitting cases. */
4627 if (gsi_end_p (gsi
))
4629 /* For the degenerate case of an infinite loop bail out.
4630 If bb has no successors and is totally empty, which can happen e.g.
4631 because of incorrect noreturn attribute, bail out too. */
4633 || infinite_empty_loop_p (e_out
))
4636 return ret
| cleanup_empty_eh_unsplit (bb
, e_out
, lp
);
4639 /* The block should consist only of a single RESX statement, modulo a
4640 preceding call to __builtin_stack_restore if there is no outgoing
4641 edge, since the call can be eliminated in this case. */
4642 resx
= gsi_stmt (gsi
);
4643 if (!e_out
&& gimple_call_builtin_p (resx
, BUILT_IN_STACK_RESTORE
))
4645 gsi_next_nondebug (&gsi
);
4646 resx
= gsi_stmt (gsi
);
4648 if (!is_gimple_resx (resx
))
4650 gcc_assert (gsi_one_nondebug_before_end_p (gsi
));
4652 /* Determine if there are non-EH edges, or resx edges into the handler. */
4653 has_non_eh_pred
= false;
4654 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4655 if (!(e
->flags
& EDGE_EH
))
4656 has_non_eh_pred
= true;
4658 /* Find the handler that's outer of the empty handler by looking at
4659 where the RESX instruction was vectored. */
4660 new_lp_nr
= lookup_stmt_eh_lp (resx
);
4661 new_region
= get_eh_region_from_lp_number (new_lp_nr
);
4663 /* If there's no destination region within the current function,
4664 redirection is trivial via removing the throwing statements from
4665 the EH region, removing the EH edges, and allowing the block
4666 to go unreachable. */
4667 if (new_region
== NULL
)
4669 gcc_assert (e_out
== NULL
);
4670 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4671 if (e
->flags
& EDGE_EH
)
4673 gimple
*stmt
= last_stmt (e
->src
);
4674 remove_stmt_from_eh_lp (stmt
);
4682 /* If the destination region is a MUST_NOT_THROW, allow the runtime
4683 to handle the abort and allow the blocks to go unreachable. */
4684 if (new_region
->type
== ERT_MUST_NOT_THROW
)
4686 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4687 if (e
->flags
& EDGE_EH
)
4689 gimple
*stmt
= last_stmt (e
->src
);
4690 remove_stmt_from_eh_lp (stmt
);
4691 add_stmt_to_eh_lp (stmt
, new_lp_nr
);
4699 /* Try to redirect the EH edges and merge the PHIs into the destination
4700 landing pad block. If the merge succeeds, we'll already have redirected
4701 all the EH edges. The handler itself will go unreachable if there were
4703 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, true))
4706 /* Finally, if all input edges are EH edges, then we can (potentially)
4707 reduce the number of transfers from the runtime by moving the landing
4708 pad from the original region to the new region. This is a win when
4709 we remove the last CLEANUP region along a particular exception
4710 propagation path. Since nothing changes except for the region with
4711 which the landing pad is associated, the PHI nodes do not need to be
4713 if (!has_non_eh_pred
)
4715 cleanup_empty_eh_move_lp (bb
, e_out
, lp
, new_region
);
4716 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4717 fprintf (dump_file
, "Empty EH handler %i moved to EH region %i.\n",
4718 lp
->index
, new_region
->index
);
4720 /* ??? The CFG didn't change, but we may have rendered the
4721 old EH region unreachable. Trigger a cleanup there. */
4728 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4729 fprintf (dump_file
, "Empty EH handler %i removed.\n", lp
->index
);
4730 remove_eh_landing_pad (lp
);
4734 /* Do a post-order traversal of the EH region tree. Examine each
4735 post_landing_pad block and see if we can eliminate it as empty. */
4738 cleanup_all_empty_eh (void)
4740 bool changed
= false;
4744 /* Ideally we'd walk the region tree and process LPs inner to outer
4745 to avoid quadraticness in EH redirection. Walking the LP array
4746 in reverse seems to be an approximation of that. */
4747 for (i
= vec_safe_length (cfun
->eh
->lp_array
) - 1; i
>= 1; --i
)
4749 lp
= (*cfun
->eh
->lp_array
)[i
];
4751 changed
|= cleanup_empty_eh (lp
);
4757 /* Perform cleanups and lowering of exception handling
4758 1) cleanups regions with handlers doing nothing are optimized out
4759 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4760 3) Info about regions that are containing instructions, and regions
4761 reachable via local EH edges is collected
4762 4) Eh tree is pruned for regions no longer necessary.
4764 TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4765 Unify those that have the same failure decl and locus.
4769 execute_cleanup_eh_1 (void)
4771 /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4772 looking up unreachable landing pads. */
4773 remove_unreachable_handlers ();
4775 /* Watch out for the region tree vanishing due to all unreachable. */
4776 if (cfun
->eh
->region_tree
)
4778 bool changed
= false;
4781 changed
|= unsplit_all_eh ();
4782 changed
|= cleanup_all_empty_eh ();
4786 free_dominance_info (CDI_DOMINATORS
);
4787 free_dominance_info (CDI_POST_DOMINATORS
);
4789 /* We delayed all basic block deletion, as we may have performed
4790 cleanups on EH edges while non-EH edges were still present. */
4791 delete_unreachable_blocks ();
4793 /* We manipulated the landing pads. Remove any region that no
4794 longer has a landing pad. */
4795 remove_unreachable_handlers_no_lp ();
4797 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
4806 const pass_data pass_data_cleanup_eh
=
4808 GIMPLE_PASS
, /* type */
4809 "ehcleanup", /* name */
4810 OPTGROUP_NONE
, /* optinfo_flags */
4811 TV_TREE_EH
, /* tv_id */
4812 PROP_gimple_lcf
, /* properties_required */
4813 0, /* properties_provided */
4814 0, /* properties_destroyed */
4815 0, /* todo_flags_start */
4816 0, /* todo_flags_finish */
4819 class pass_cleanup_eh
: public gimple_opt_pass
4822 pass_cleanup_eh (gcc::context
*ctxt
)
4823 : gimple_opt_pass (pass_data_cleanup_eh
, ctxt
)
4826 /* opt_pass methods: */
4827 opt_pass
* clone () { return new pass_cleanup_eh (m_ctxt
); }
4828 virtual bool gate (function
*fun
)
4830 return fun
->eh
!= NULL
&& fun
->eh
->region_tree
!= NULL
;
4833 virtual unsigned int execute (function
*);
4835 }; // class pass_cleanup_eh
4838 pass_cleanup_eh::execute (function
*fun
)
4840 int ret
= execute_cleanup_eh_1 ();
4842 /* If the function no longer needs an EH personality routine
4843 clear it. This exposes cross-language inlining opportunities
4844 and avoids references to a never defined personality routine. */
4845 if (DECL_FUNCTION_PERSONALITY (current_function_decl
)
4846 && function_needs_eh_personality (fun
) != eh_personality_lang
)
4847 DECL_FUNCTION_PERSONALITY (current_function_decl
) = NULL_TREE
;
4855 make_pass_cleanup_eh (gcc::context
*ctxt
)
4857 return new pass_cleanup_eh (ctxt
);
4860 /* Disable warnings about missing quoting in GCC diagnostics for
4861 the verification errors. Their format strings don't follow GCC
4862 diagnostic conventions but are only used for debugging. */
4864 # pragma GCC diagnostic push
4865 # pragma GCC diagnostic ignored "-Wformat-diag"
4868 /* Verify that BB containing STMT as the last statement, has precisely the
4869 edge that make_eh_edges would create. */
4872 verify_eh_edges (gimple
*stmt
)
4874 basic_block bb
= gimple_bb (stmt
);
4875 eh_landing_pad lp
= NULL
;
4880 lp_nr
= lookup_stmt_eh_lp (stmt
);
4882 lp
= get_eh_landing_pad_from_number (lp_nr
);
4885 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4887 if (e
->flags
& EDGE_EH
)
4891 error ("BB %i has multiple EH edges", bb
->index
);
4903 error ("BB %i cannot throw but has an EH edge", bb
->index
);
4909 if (!stmt_could_throw_p (cfun
, stmt
))
4911 error ("BB %i last statement has incorrectly set lp", bb
->index
);
4915 if (eh_edge
== NULL
)
4917 error ("BB %i is missing an EH edge", bb
->index
);
4921 if (eh_edge
->dest
!= label_to_block (cfun
, lp
->post_landing_pad
))
4923 error ("Incorrect EH edge %i->%i", bb
->index
, eh_edge
->dest
->index
);
4930 /* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
4933 verify_eh_dispatch_edge (geh_dispatch
*stmt
)
4937 basic_block src
, dst
;
4938 bool want_fallthru
= true;
4942 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
4943 src
= gimple_bb (stmt
);
4945 FOR_EACH_EDGE (e
, ei
, src
->succs
)
4946 gcc_assert (e
->aux
== NULL
);
4951 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
4953 dst
= label_to_block (cfun
, c
->label
);
4954 e
= find_edge (src
, dst
);
4957 error ("BB %i is missing an edge", src
->index
);
4962 /* A catch-all handler doesn't have a fallthru. */
4963 if (c
->type_list
== NULL
)
4965 want_fallthru
= false;
4971 case ERT_ALLOWED_EXCEPTIONS
:
4972 dst
= label_to_block (cfun
, r
->u
.allowed
.label
);
4973 e
= find_edge (src
, dst
);
4976 error ("BB %i is missing an edge", src
->index
);
4987 FOR_EACH_EDGE (e
, ei
, src
->succs
)
4989 if (e
->flags
& EDGE_FALLTHRU
)
4991 if (fall_edge
!= NULL
)
4993 error ("BB %i too many fallthru edges", src
->index
);
5002 error ("BB %i has incorrect edge", src
->index
);
5006 if ((fall_edge
!= NULL
) ^ want_fallthru
)
5008 error ("BB %i has incorrect fallthru edge", src
->index
);
5016 # pragma GCC diagnostic pop