1 /* Exception handling semantics and decomposition for trees.
2 Copyright (C) 2003-2019 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
, gimple
*t
)
144 if (ifun
->eh
->throw_stmt_table
== NULL
)
147 int *lp_nr
= ifun
->eh
->throw_stmt_table
->get (t
);
148 return lp_nr
? *lp_nr
: 0;
151 /* Likewise, but always use the current function. */
154 lookup_stmt_eh_lp (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
;
363 /* Pointer to the GIMPLE_TRY_FINALLY node under discussion. The
364 try_finally_expr is the original GIMPLE_TRY_FINALLY. We need to retain
365 this so that outside_finally_tree can reliably reference the tree used
366 in the collect_finally_tree data structures. */
367 gtry
*try_finally_expr
;
370 /* While lowering a top_p usually it is expanded into multiple statements,
371 thus we need the following field to store them. */
372 gimple_seq top_p_seq
;
374 /* The state outside this try_finally node. */
375 struct leh_state
*outer
;
377 /* The exception region created for it. */
380 /* The goto queue. */
381 struct goto_queue_node
*goto_queue
;
382 size_t goto_queue_size
;
383 size_t goto_queue_active
;
385 /* Pointer map to help in searching goto_queue when it is large. */
386 hash_map
<gimple
*, goto_queue_node
*> *goto_queue_map
;
388 /* The set of unique labels seen as entries in the goto queue. */
389 vec
<tree
> dest_array
;
391 /* A label to be added at the end of the completed transformed
392 sequence. It will be set if may_fallthru was true *at one time*,
393 though subsequent transformations may have cleared that flag. */
396 /* True if it is possible to fall out the bottom of the try block.
397 Cleared if the fallthru is converted to a goto. */
400 /* True if any entry in goto_queue is a GIMPLE_RETURN. */
403 /* True if the finally block can receive an exception edge.
404 Cleared if the exception case is handled by code duplication. */
408 static gimple_seq
lower_eh_must_not_throw (struct leh_state
*, gtry
*);
410 /* Search for STMT in the goto queue. Return the replacement,
411 or null if the statement isn't in the queue. */
413 #define LARGE_GOTO_QUEUE 20
415 static void lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*seq
);
418 find_goto_replacement (struct leh_tf_state
*tf
, treemple stmt
)
422 if (tf
->goto_queue_active
< LARGE_GOTO_QUEUE
)
424 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
425 if ( tf
->goto_queue
[i
].stmt
.g
== stmt
.g
)
426 return tf
->goto_queue
[i
].repl_stmt
;
430 /* If we have a large number of entries in the goto_queue, create a
431 pointer map and use that for searching. */
433 if (!tf
->goto_queue_map
)
435 tf
->goto_queue_map
= new hash_map
<gimple
*, goto_queue_node
*>;
436 for (i
= 0; i
< tf
->goto_queue_active
; i
++)
438 bool existed
= tf
->goto_queue_map
->put (tf
->goto_queue
[i
].stmt
.g
,
440 gcc_assert (!existed
);
444 goto_queue_node
**slot
= tf
->goto_queue_map
->get (stmt
.g
);
446 return ((*slot
)->repl_stmt
);
451 /* A subroutine of replace_goto_queue_1. Handles the sub-clauses of a
452 lowered GIMPLE_COND. If, by chance, the replacement is a simple goto,
453 then we can just splat it in, otherwise we add the new stmts immediately
454 after the GIMPLE_COND and redirect. */
457 replace_goto_queue_cond_clause (tree
*tp
, struct leh_tf_state
*tf
,
458 gimple_stmt_iterator
*gsi
)
463 location_t loc
= gimple_location (gsi_stmt (*gsi
));
466 new_seq
= find_goto_replacement (tf
, temp
);
470 if (gimple_seq_singleton_p (new_seq
)
471 && gimple_code (gimple_seq_first_stmt (new_seq
)) == GIMPLE_GOTO
)
473 *tp
= gimple_goto_dest (gimple_seq_first_stmt (new_seq
));
477 label
= create_artificial_label (loc
);
478 /* Set the new label for the GIMPLE_COND */
481 gsi_insert_after (gsi
, gimple_build_label (label
), GSI_CONTINUE_LINKING
);
482 gsi_insert_seq_after (gsi
, gimple_seq_copy (new_seq
), GSI_CONTINUE_LINKING
);
485 /* The real work of replace_goto_queue. Returns with TSI updated to
486 point to the next statement. */
488 static void replace_goto_queue_stmt_list (gimple_seq
*, struct leh_tf_state
*);
491 replace_goto_queue_1 (gimple
*stmt
, struct leh_tf_state
*tf
,
492 gimple_stmt_iterator
*gsi
)
498 switch (gimple_code (stmt
))
503 seq
= find_goto_replacement (tf
, temp
);
506 gimple_stmt_iterator i
;
507 seq
= gimple_seq_copy (seq
);
508 for (i
= gsi_start (seq
); !gsi_end_p (i
); gsi_next (&i
))
509 gimple_set_location (gsi_stmt (i
), gimple_location (stmt
));
510 gsi_insert_seq_before (gsi
, seq
, GSI_SAME_STMT
);
511 gsi_remove (gsi
, false);
517 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 2), tf
, gsi
);
518 replace_goto_queue_cond_clause (gimple_op_ptr (stmt
, 3), tf
, gsi
);
522 replace_goto_queue_stmt_list (gimple_try_eval_ptr (stmt
), tf
);
523 replace_goto_queue_stmt_list (gimple_try_cleanup_ptr (stmt
), tf
);
526 replace_goto_queue_stmt_list (gimple_catch_handler_ptr (
527 as_a
<gcatch
*> (stmt
)),
530 case GIMPLE_EH_FILTER
:
531 replace_goto_queue_stmt_list (gimple_eh_filter_failure_ptr (stmt
), tf
);
535 geh_else
*eh_else_stmt
= as_a
<geh_else
*> (stmt
);
536 replace_goto_queue_stmt_list (gimple_eh_else_n_body_ptr (eh_else_stmt
),
538 replace_goto_queue_stmt_list (gimple_eh_else_e_body_ptr (eh_else_stmt
),
544 /* These won't have gotos in them. */
551 /* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
554 replace_goto_queue_stmt_list (gimple_seq
*seq
, struct leh_tf_state
*tf
)
556 gimple_stmt_iterator gsi
= gsi_start (*seq
);
558 while (!gsi_end_p (gsi
))
559 replace_goto_queue_1 (gsi_stmt (gsi
), tf
, &gsi
);
562 /* Replace all goto queue members. */
565 replace_goto_queue (struct leh_tf_state
*tf
)
567 if (tf
->goto_queue_active
== 0)
569 replace_goto_queue_stmt_list (&tf
->top_p_seq
, tf
);
570 replace_goto_queue_stmt_list (&eh_seq
, tf
);
573 /* Add a new record to the goto queue contained in TF. NEW_STMT is the
574 data to be added, IS_LABEL indicates whether NEW_STMT is a label or
578 record_in_goto_queue (struct leh_tf_state
*tf
,
585 struct goto_queue_node
*q
;
587 gcc_assert (!tf
->goto_queue_map
);
589 active
= tf
->goto_queue_active
;
590 size
= tf
->goto_queue_size
;
593 size
= (size
? size
* 2 : 32);
594 tf
->goto_queue_size
= size
;
596 = XRESIZEVEC (struct goto_queue_node
, tf
->goto_queue
, size
);
599 q
= &tf
->goto_queue
[active
];
600 tf
->goto_queue_active
= active
+ 1;
602 memset (q
, 0, sizeof (*q
));
605 q
->location
= location
;
606 q
->is_label
= is_label
;
609 /* Record the LABEL label in the goto queue contained in TF.
613 record_in_goto_queue_label (struct leh_tf_state
*tf
, treemple stmt
, tree label
,
617 treemple temp
, new_stmt
;
622 /* Computed and non-local gotos do not get processed. Given
623 their nature we can neither tell whether we've escaped the
624 finally block nor redirect them if we knew. */
625 if (TREE_CODE (label
) != LABEL_DECL
)
628 /* No need to record gotos that don't leave the try block. */
630 if (!outside_finally_tree (temp
, tf
->try_finally_expr
))
633 if (! tf
->dest_array
.exists ())
635 tf
->dest_array
.create (10);
636 tf
->dest_array
.quick_push (label
);
641 int n
= tf
->dest_array
.length ();
642 for (index
= 0; index
< n
; ++index
)
643 if (tf
->dest_array
[index
] == label
)
646 tf
->dest_array
.safe_push (label
);
649 /* In the case of a GOTO we want to record the destination label,
650 since with a GIMPLE_COND we have an easy access to the then/else
653 record_in_goto_queue (tf
, new_stmt
, index
, true, location
);
656 /* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
657 node, and if so record that fact in the goto queue associated with that
661 maybe_record_in_goto_queue (struct leh_state
*state
, gimple
*stmt
)
663 struct leh_tf_state
*tf
= state
->tf
;
669 switch (gimple_code (stmt
))
673 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
674 new_stmt
.tp
= gimple_op_ptr (cond_stmt
, 2);
675 record_in_goto_queue_label (tf
, new_stmt
,
676 gimple_cond_true_label (cond_stmt
),
677 EXPR_LOCATION (*new_stmt
.tp
));
678 new_stmt
.tp
= gimple_op_ptr (cond_stmt
, 3);
679 record_in_goto_queue_label (tf
, new_stmt
,
680 gimple_cond_false_label (cond_stmt
),
681 EXPR_LOCATION (*new_stmt
.tp
));
686 record_in_goto_queue_label (tf
, new_stmt
, gimple_goto_dest (stmt
),
687 gimple_location (stmt
));
691 tf
->may_return
= true;
693 record_in_goto_queue (tf
, new_stmt
, -1, false, gimple_location (stmt
));
703 /* We do not process GIMPLE_SWITCHes for now. As long as the original source
704 was in fact structured, and we've not yet done jump threading, then none
705 of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this. */
708 verify_norecord_switch_expr (struct leh_state
*state
,
709 gswitch
*switch_expr
)
711 struct leh_tf_state
*tf
= state
->tf
;
717 n
= gimple_switch_num_labels (switch_expr
);
719 for (i
= 0; i
< n
; ++i
)
722 tree lab
= CASE_LABEL (gimple_switch_label (switch_expr
, i
));
724 gcc_assert (!outside_finally_tree (temp
, tf
->try_finally_expr
));
728 #define verify_norecord_switch_expr(state, switch_expr)
731 /* Redirect a RETURN_EXPR pointed to by Q to FINLAB. If MOD is
732 non-null, insert it before the new branch. */
735 do_return_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
)
739 /* In the case of a return, the queue node must be a gimple statement. */
740 gcc_assert (!q
->is_label
);
742 /* Note that the return value may have already been computed, e.g.,
755 should return 0, not 1. We don't have to do anything to make
756 this happens because the return value has been placed in the
757 RESULT_DECL already. */
759 q
->cont_stmt
= q
->stmt
.g
;
762 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
764 x
= gimple_build_goto (finlab
);
765 gimple_set_location (x
, q
->location
);
766 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
769 /* Similar, but easier, for GIMPLE_GOTO. */
772 do_goto_redirection (struct goto_queue_node
*q
, tree finlab
, gimple_seq mod
,
773 struct leh_tf_state
*tf
)
777 gcc_assert (q
->is_label
);
779 q
->cont_stmt
= gimple_build_goto (tf
->dest_array
[q
->index
]);
782 gimple_seq_add_seq (&q
->repl_stmt
, mod
);
784 x
= gimple_build_goto (finlab
);
785 gimple_set_location (x
, q
->location
);
786 gimple_seq_add_stmt (&q
->repl_stmt
, x
);
789 /* Emit a standard landing pad sequence into SEQ for REGION. */
792 emit_post_landing_pad (gimple_seq
*seq
, eh_region region
)
794 eh_landing_pad lp
= region
->landing_pads
;
798 lp
= gen_eh_landing_pad (region
);
800 lp
->post_landing_pad
= create_artificial_label (UNKNOWN_LOCATION
);
801 EH_LANDING_PAD_NR (lp
->post_landing_pad
) = lp
->index
;
803 x
= gimple_build_label (lp
->post_landing_pad
);
804 gimple_seq_add_stmt (seq
, x
);
807 /* Emit a RESX statement into SEQ for REGION. */
810 emit_resx (gimple_seq
*seq
, eh_region region
)
812 gresx
*x
= gimple_build_resx (region
->index
);
813 gimple_seq_add_stmt (seq
, x
);
815 record_stmt_eh_region (region
->outer
, x
);
818 /* Note that the current EH region may contain a throw, or a
819 call to a function which itself may contain a throw. */
822 note_eh_region_may_contain_throw (eh_region region
)
824 while (bitmap_set_bit (eh_region_may_contain_throw_map
, region
->index
))
826 if (region
->type
== ERT_MUST_NOT_THROW
)
828 region
= region
->outer
;
834 /* Check if REGION has been marked as containing a throw. If REGION is
835 NULL, this predicate is false. */
838 eh_region_may_contain_throw (eh_region r
)
840 return r
&& bitmap_bit_p (eh_region_may_contain_throw_map
, r
->index
);
843 /* We want to transform
844 try { body; } catch { stuff; }
854 TP is a GIMPLE_TRY node. REGION is the region whose post_landing_pad
855 should be placed before the second operand, or NULL. OVER is
856 an existing label that should be put at the exit, or NULL. */
859 frob_into_branch_around (gtry
*tp
, eh_region region
, tree over
)
862 gimple_seq cleanup
, result
;
863 location_t loc
= gimple_location (tp
);
865 cleanup
= gimple_try_cleanup (tp
);
866 result
= gimple_try_eval (tp
);
869 emit_post_landing_pad (&eh_seq
, region
);
871 if (gimple_seq_may_fallthru (cleanup
))
874 over
= create_artificial_label (loc
);
875 x
= gimple_build_goto (over
);
876 gimple_set_location (x
, loc
);
877 gimple_seq_add_stmt (&cleanup
, x
);
879 gimple_seq_add_seq (&eh_seq
, cleanup
);
883 x
= gimple_build_label (over
);
884 gimple_seq_add_stmt (&result
, x
);
889 /* A subroutine of lower_try_finally. Duplicate the tree rooted at T.
890 Make sure to record all new labels found. */
893 lower_try_finally_dup_block (gimple_seq seq
, struct leh_state
*outer_state
,
898 gimple_stmt_iterator gsi
;
900 new_seq
= copy_gimple_seq_and_replace_locals (seq
);
902 for (gsi
= gsi_start (new_seq
); !gsi_end_p (gsi
); gsi_next (&gsi
))
904 gimple
*stmt
= gsi_stmt (gsi
);
905 /* We duplicate __builtin_stack_restore at -O0 in the hope of eliminating
906 it on the EH paths. When it is not eliminated, make it transparent in
908 if (gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
909 gimple_set_location (stmt
, UNKNOWN_LOCATION
);
910 else if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
912 tree block
= gimple_block (stmt
);
913 gimple_set_location (stmt
, loc
);
914 gimple_set_block (stmt
, block
);
919 region
= outer_state
->tf
->try_finally_expr
;
920 collect_finally_tree_1 (new_seq
, region
);
925 /* A subroutine of lower_try_finally. Create a fallthru label for
926 the given try_finally state. The only tricky bit here is that
927 we have to make sure to record the label in our outer context. */
930 lower_try_finally_fallthru_label (struct leh_tf_state
*tf
)
932 tree label
= tf
->fallthru_label
;
937 label
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
938 tf
->fallthru_label
= label
;
942 record_in_finally_tree (temp
, tf
->outer
->tf
->try_finally_expr
);
948 /* A subroutine of lower_try_finally. If FINALLY consits of a
949 GIMPLE_EH_ELSE node, return it. */
951 static inline geh_else
*
952 get_eh_else (gimple_seq finally
)
954 gimple
*x
= gimple_seq_first_stmt (finally
);
955 if (gimple_code (x
) == GIMPLE_EH_ELSE
)
957 gcc_assert (gimple_seq_singleton_p (finally
));
958 return as_a
<geh_else
*> (x
);
963 /* A subroutine of lower_try_finally. If the eh_protect_cleanup_actions
964 langhook returns non-null, then the language requires that the exception
965 path out of a try_finally be treated specially. To wit: the code within
966 the finally block may not itself throw an exception. We have two choices
967 here. First we can duplicate the finally block and wrap it in a
968 must_not_throw region. Second, we can generate code like
973 if (fintmp == eh_edge)
974 protect_cleanup_actions;
977 where "fintmp" is the temporary used in the switch statement generation
978 alternative considered below. For the nonce, we always choose the first
981 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
984 honor_protect_cleanup_actions (struct leh_state
*outer_state
,
985 struct leh_state
*this_state
,
986 struct leh_tf_state
*tf
)
988 gimple_seq finally
= gimple_try_cleanup (tf
->top_p
);
990 /* EH_ELSE doesn't come from user code; only compiler generated stuff.
991 It does need to be handled here, so as to separate the (different)
992 EH path from the normal path. But we should not attempt to wrap
993 it with a must-not-throw node (which indeed gets in the way). */
994 if (geh_else
*eh_else
= get_eh_else (finally
))
996 gimple_try_set_cleanup (tf
->top_p
, gimple_eh_else_n_body (eh_else
));
997 finally
= gimple_eh_else_e_body (eh_else
);
999 /* Let the ELSE see the exception that's being processed, but
1000 since the cleanup is outside the try block, process it with
1001 outer_state, otherwise it may be used as a cleanup for
1002 itself, and Bad Things (TM) ensue. */
1003 eh_region save_ehp
= outer_state
->ehp_region
;
1004 outer_state
->ehp_region
= this_state
->cur_region
;
1005 lower_eh_constructs_1 (outer_state
, &finally
);
1006 outer_state
->ehp_region
= save_ehp
;
1010 /* First check for nothing to do. */
1011 if (lang_hooks
.eh_protect_cleanup_actions
== NULL
)
1013 tree actions
= lang_hooks
.eh_protect_cleanup_actions ();
1014 if (actions
== NULL
)
1018 finally
= lower_try_finally_dup_block (finally
, outer_state
,
1019 gimple_location (tf
->try_finally_expr
));
1021 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
1022 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
1023 to be in an enclosing scope, but needs to be implemented at this level
1024 to avoid a nesting violation (see wrap_temporary_cleanups in
1025 cp/decl.c). Since it's logically at an outer level, we should call
1026 terminate before we get to it, so strip it away before adding the
1027 MUST_NOT_THROW filter. */
1028 gimple_stmt_iterator gsi
= gsi_start (finally
);
1029 gimple
*x
= gsi_stmt (gsi
);
1030 if (gimple_code (x
) == GIMPLE_TRY
1031 && gimple_try_kind (x
) == GIMPLE_TRY_CATCH
1032 && gimple_try_catch_is_cleanup (x
))
1034 gsi_insert_seq_before (&gsi
, gimple_try_eval (x
), GSI_SAME_STMT
);
1035 gsi_remove (&gsi
, false);
1038 /* Wrap the block with protect_cleanup_actions as the action. */
1039 geh_mnt
*eh_mnt
= gimple_build_eh_must_not_throw (actions
);
1040 gtry
*try_stmt
= gimple_build_try (finally
,
1041 gimple_seq_alloc_with_stmt (eh_mnt
),
1043 finally
= lower_eh_must_not_throw (outer_state
, try_stmt
);
1046 /* Drop all of this into the exception sequence. */
1047 emit_post_landing_pad (&eh_seq
, tf
->region
);
1048 gimple_seq_add_seq (&eh_seq
, finally
);
1049 if (gimple_seq_may_fallthru (finally
))
1050 emit_resx (&eh_seq
, tf
->region
);
1052 /* Having now been handled, EH isn't to be considered with
1053 the rest of the outgoing edges. */
1054 tf
->may_throw
= false;
1057 /* A subroutine of lower_try_finally. We have determined that there is
1058 no fallthru edge out of the finally block. This means that there is
1059 no outgoing edge corresponding to any incoming edge. Restructure the
1060 try_finally node for this special case. */
1063 lower_try_finally_nofallthru (struct leh_state
*state
,
1064 struct leh_tf_state
*tf
)
1070 struct goto_queue_node
*q
, *qe
;
1072 lab
= create_artificial_label (gimple_location (tf
->try_finally_expr
));
1074 /* We expect that tf->top_p is a GIMPLE_TRY. */
1075 finally
= gimple_try_cleanup (tf
->top_p
);
1076 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1078 x
= gimple_build_label (lab
);
1079 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1082 qe
= q
+ tf
->goto_queue_active
;
1085 do_return_redirection (q
, lab
, NULL
);
1087 do_goto_redirection (q
, lab
, NULL
, tf
);
1089 replace_goto_queue (tf
);
1091 /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1092 eh_else
= get_eh_else (finally
);
1095 finally
= gimple_eh_else_n_body (eh_else
);
1096 lower_eh_constructs_1 (state
, &finally
);
1097 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1101 finally
= gimple_eh_else_e_body (eh_else
);
1102 lower_eh_constructs_1 (state
, &finally
);
1104 emit_post_landing_pad (&eh_seq
, tf
->region
);
1105 gimple_seq_add_seq (&eh_seq
, finally
);
1110 lower_eh_constructs_1 (state
, &finally
);
1111 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1115 emit_post_landing_pad (&eh_seq
, tf
->region
);
1117 x
= gimple_build_goto (lab
);
1118 gimple_set_location (x
, gimple_location (tf
->try_finally_expr
));
1119 gimple_seq_add_stmt (&eh_seq
, x
);
1124 /* A subroutine of lower_try_finally. We have determined that there is
1125 exactly one destination of the finally block. Restructure the
1126 try_finally node for this special case. */
1129 lower_try_finally_onedest (struct leh_state
*state
, struct leh_tf_state
*tf
)
1131 struct goto_queue_node
*q
, *qe
;
1136 gimple_stmt_iterator gsi
;
1138 location_t loc
= gimple_location (tf
->try_finally_expr
);
1140 finally
= gimple_try_cleanup (tf
->top_p
);
1141 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1143 /* Since there's only one destination, and the destination edge can only
1144 either be EH or non-EH, that implies that all of our incoming edges
1145 are of the same type. Therefore we can lower EH_ELSE immediately. */
1146 eh_else
= get_eh_else (finally
);
1150 finally
= gimple_eh_else_e_body (eh_else
);
1152 finally
= gimple_eh_else_n_body (eh_else
);
1155 lower_eh_constructs_1 (state
, &finally
);
1157 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1159 gimple
*stmt
= gsi_stmt (gsi
);
1160 if (LOCATION_LOCUS (gimple_location (stmt
)) == UNKNOWN_LOCATION
)
1162 tree block
= gimple_block (stmt
);
1163 gimple_set_location (stmt
, gimple_location (tf
->try_finally_expr
));
1164 gimple_set_block (stmt
, block
);
1170 /* Only reachable via the exception edge. Add the given label to
1171 the head of the FINALLY block. Append a RESX at the end. */
1172 emit_post_landing_pad (&eh_seq
, tf
->region
);
1173 gimple_seq_add_seq (&eh_seq
, finally
);
1174 emit_resx (&eh_seq
, tf
->region
);
1178 if (tf
->may_fallthru
)
1180 /* Only reachable via the fallthru edge. Do nothing but let
1181 the two blocks run together; we'll fall out the bottom. */
1182 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1186 finally_label
= create_artificial_label (loc
);
1187 label_stmt
= gimple_build_label (finally_label
);
1188 gimple_seq_add_stmt (&tf
->top_p_seq
, label_stmt
);
1190 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1193 qe
= q
+ tf
->goto_queue_active
;
1197 /* Reachable by return expressions only. Redirect them. */
1199 do_return_redirection (q
, finally_label
, NULL
);
1200 replace_goto_queue (tf
);
1204 /* Reachable by goto expressions only. Redirect them. */
1206 do_goto_redirection (q
, finally_label
, NULL
, tf
);
1207 replace_goto_queue (tf
);
1209 if (tf
->dest_array
[0] == tf
->fallthru_label
)
1211 /* Reachable by goto to fallthru label only. Redirect it
1212 to the new label (already created, sadly), and do not
1213 emit the final branch out, or the fallthru label. */
1214 tf
->fallthru_label
= NULL
;
1219 /* Place the original return/goto to the original destination
1220 immediately after the finally block. */
1221 x
= tf
->goto_queue
[0].cont_stmt
;
1222 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1223 maybe_record_in_goto_queue (state
, x
);
1226 /* A subroutine of lower_try_finally. There are multiple edges incoming
1227 and outgoing from the finally block. Implement this by duplicating the
1228 finally block for every destination. */
1231 lower_try_finally_copy (struct leh_state
*state
, struct leh_tf_state
*tf
)
1234 gimple_seq new_stmt
;
1239 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1241 finally
= gimple_try_cleanup (tf
->top_p
);
1243 /* Notice EH_ELSE, and simplify some of the remaining code
1244 by considering FINALLY to be the normal return path only. */
1245 eh_else
= get_eh_else (finally
);
1247 finally
= gimple_eh_else_n_body (eh_else
);
1249 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1252 if (tf
->may_fallthru
)
1254 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1255 lower_eh_constructs_1 (state
, &seq
);
1256 gimple_seq_add_seq (&new_stmt
, seq
);
1258 tmp
= lower_try_finally_fallthru_label (tf
);
1259 x
= gimple_build_goto (tmp
);
1260 gimple_set_location (x
, tf_loc
);
1261 gimple_seq_add_stmt (&new_stmt
, x
);
1266 /* We don't need to copy the EH path of EH_ELSE,
1267 since it is only emitted once. */
1269 seq
= gimple_eh_else_e_body (eh_else
);
1271 seq
= lower_try_finally_dup_block (finally
, state
, tf_loc
);
1272 lower_eh_constructs_1 (state
, &seq
);
1274 emit_post_landing_pad (&eh_seq
, tf
->region
);
1275 gimple_seq_add_seq (&eh_seq
, seq
);
1276 emit_resx (&eh_seq
, tf
->region
);
1281 struct goto_queue_node
*q
, *qe
;
1282 int return_index
, index
;
1285 struct goto_queue_node
*q
;
1289 return_index
= tf
->dest_array
.length ();
1290 labels
= XCNEWVEC (struct labels_s
, return_index
+ 1);
1293 qe
= q
+ tf
->goto_queue_active
;
1296 index
= q
->index
< 0 ? return_index
: q
->index
;
1298 if (!labels
[index
].q
)
1299 labels
[index
].q
= q
;
1302 for (index
= 0; index
< return_index
+ 1; index
++)
1306 q
= labels
[index
].q
;
1310 lab
= labels
[index
].label
1311 = create_artificial_label (tf_loc
);
1313 if (index
== return_index
)
1314 do_return_redirection (q
, lab
, NULL
);
1316 do_goto_redirection (q
, lab
, NULL
, tf
);
1318 x
= gimple_build_label (lab
);
1319 gimple_seq_add_stmt (&new_stmt
, x
);
1321 seq
= lower_try_finally_dup_block (finally
, state
, q
->location
);
1322 lower_eh_constructs_1 (state
, &seq
);
1323 gimple_seq_add_seq (&new_stmt
, seq
);
1325 gimple_seq_add_stmt (&new_stmt
, q
->cont_stmt
);
1326 maybe_record_in_goto_queue (state
, q
->cont_stmt
);
1329 for (q
= tf
->goto_queue
; q
< qe
; q
++)
1333 index
= q
->index
< 0 ? return_index
: q
->index
;
1335 if (labels
[index
].q
== q
)
1338 lab
= labels
[index
].label
;
1340 if (index
== return_index
)
1341 do_return_redirection (q
, lab
, NULL
);
1343 do_goto_redirection (q
, lab
, NULL
, tf
);
1346 replace_goto_queue (tf
);
1350 /* Need to link new stmts after running replace_goto_queue due
1351 to not wanting to process the same goto stmts twice. */
1352 gimple_seq_add_seq (&tf
->top_p_seq
, new_stmt
);
1355 /* A subroutine of lower_try_finally. There are multiple edges incoming
1356 and outgoing from the finally block. Implement this by instrumenting
1357 each incoming edge and creating a switch statement at the end of the
1358 finally block that branches to the appropriate destination. */
1361 lower_try_finally_switch (struct leh_state
*state
, struct leh_tf_state
*tf
)
1363 struct goto_queue_node
*q
, *qe
;
1364 tree finally_tmp
, finally_label
;
1365 int return_index
, eh_index
, fallthru_index
;
1366 int nlabels
, ndests
, j
, last_case_index
;
1368 auto_vec
<tree
> case_label_vec
;
1369 gimple_seq switch_body
= NULL
;
1373 gimple
*switch_stmt
;
1375 hash_map
<tree
, gimple
*> *cont_map
= NULL
;
1376 /* The location of the TRY_FINALLY stmt. */
1377 location_t tf_loc
= gimple_location (tf
->try_finally_expr
);
1378 /* The location of the finally block. */
1379 location_t finally_loc
;
1381 finally
= gimple_try_cleanup (tf
->top_p
);
1382 eh_else
= get_eh_else (finally
);
1384 /* Mash the TRY block to the head of the chain. */
1385 tf
->top_p_seq
= gimple_try_eval (tf
->top_p
);
1387 /* The location of the finally is either the last stmt in the finally
1388 block or the location of the TRY_FINALLY itself. */
1389 x
= gimple_seq_last_stmt (finally
);
1390 finally_loc
= x
? gimple_location (x
) : tf_loc
;
1392 /* Prepare for switch statement generation. */
1393 nlabels
= tf
->dest_array
.length ();
1394 return_index
= nlabels
;
1395 eh_index
= return_index
+ tf
->may_return
;
1396 fallthru_index
= eh_index
+ (tf
->may_throw
&& !eh_else
);
1397 ndests
= fallthru_index
+ tf
->may_fallthru
;
1399 finally_tmp
= create_tmp_var (integer_type_node
, "finally_tmp");
1400 finally_label
= create_artificial_label (finally_loc
);
1402 /* We use vec::quick_push on case_label_vec throughout this function,
1403 since we know the size in advance and allocate precisely as muce
1405 case_label_vec
.create (ndests
);
1407 last_case_index
= 0;
1409 /* Begin inserting code for getting to the finally block. Things
1410 are done in this order to correspond to the sequence the code is
1413 if (tf
->may_fallthru
)
1415 x
= gimple_build_assign (finally_tmp
,
1416 build_int_cst (integer_type_node
,
1418 gimple_set_location (x
, finally_loc
);
1419 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1421 tmp
= build_int_cst (integer_type_node
, fallthru_index
);
1422 last_case
= build_case_label (tmp
, NULL
,
1423 create_artificial_label (finally_loc
));
1424 case_label_vec
.quick_push (last_case
);
1427 x
= gimple_build_label (CASE_LABEL (last_case
));
1428 gimple_seq_add_stmt (&switch_body
, x
);
1430 tmp
= lower_try_finally_fallthru_label (tf
);
1431 x
= gimple_build_goto (tmp
);
1432 gimple_set_location (x
, finally_loc
);
1433 gimple_seq_add_stmt (&switch_body
, x
);
1436 /* For EH_ELSE, emit the exception path (plus resx) now, then
1437 subsequently we only need consider the normal path. */
1442 finally
= gimple_eh_else_e_body (eh_else
);
1443 lower_eh_constructs_1 (state
, &finally
);
1445 emit_post_landing_pad (&eh_seq
, tf
->region
);
1446 gimple_seq_add_seq (&eh_seq
, finally
);
1447 emit_resx (&eh_seq
, tf
->region
);
1450 finally
= gimple_eh_else_n_body (eh_else
);
1452 else if (tf
->may_throw
)
1454 emit_post_landing_pad (&eh_seq
, tf
->region
);
1456 x
= gimple_build_assign (finally_tmp
,
1457 build_int_cst (integer_type_node
, eh_index
));
1458 gimple_seq_add_stmt (&eh_seq
, x
);
1460 x
= gimple_build_goto (finally_label
);
1461 gimple_set_location (x
, tf_loc
);
1462 gimple_seq_add_stmt (&eh_seq
, x
);
1464 tmp
= build_int_cst (integer_type_node
, eh_index
);
1465 last_case
= build_case_label (tmp
, NULL
,
1466 create_artificial_label (tf_loc
));
1467 case_label_vec
.quick_push (last_case
);
1470 x
= gimple_build_label (CASE_LABEL (last_case
));
1471 gimple_seq_add_stmt (&eh_seq
, x
);
1472 emit_resx (&eh_seq
, tf
->region
);
1475 x
= gimple_build_label (finally_label
);
1476 gimple_seq_add_stmt (&tf
->top_p_seq
, x
);
1478 lower_eh_constructs_1 (state
, &finally
);
1479 gimple_seq_add_seq (&tf
->top_p_seq
, finally
);
1481 /* Redirect each incoming goto edge. */
1483 qe
= q
+ tf
->goto_queue_active
;
1484 j
= last_case_index
+ tf
->may_return
;
1485 /* Prepare the assignments to finally_tmp that are executed upon the
1486 entrance through a particular edge. */
1489 gimple_seq mod
= NULL
;
1491 unsigned int case_index
;
1495 x
= gimple_build_assign (finally_tmp
,
1496 build_int_cst (integer_type_node
,
1498 gimple_seq_add_stmt (&mod
, x
);
1499 do_return_redirection (q
, finally_label
, mod
);
1500 switch_id
= return_index
;
1504 x
= gimple_build_assign (finally_tmp
,
1505 build_int_cst (integer_type_node
, q
->index
));
1506 gimple_seq_add_stmt (&mod
, x
);
1507 do_goto_redirection (q
, finally_label
, mod
, tf
);
1508 switch_id
= q
->index
;
1511 case_index
= j
+ q
->index
;
1512 if (case_label_vec
.length () <= case_index
|| !case_label_vec
[case_index
])
1515 tmp
= build_int_cst (integer_type_node
, switch_id
);
1516 case_lab
= build_case_label (tmp
, NULL
,
1517 create_artificial_label (tf_loc
));
1518 /* We store the cont_stmt in the pointer map, so that we can recover
1519 it in the loop below. */
1521 cont_map
= new hash_map
<tree
, gimple
*>;
1522 cont_map
->put (case_lab
, q
->cont_stmt
);
1523 case_label_vec
.quick_push (case_lab
);
1526 for (j
= last_case_index
; j
< last_case_index
+ nlabels
; j
++)
1530 last_case
= case_label_vec
[j
];
1532 gcc_assert (last_case
);
1533 gcc_assert (cont_map
);
1535 cont_stmt
= *cont_map
->get (last_case
);
1537 x
= gimple_build_label (CASE_LABEL (last_case
));
1538 gimple_seq_add_stmt (&switch_body
, x
);
1539 gimple_seq_add_stmt (&switch_body
, cont_stmt
);
1540 maybe_record_in_goto_queue (state
, cont_stmt
);
1545 replace_goto_queue (tf
);
1547 /* Make sure that the last case is the default label, as one is required.
1548 Then sort the labels, which is also required in GIMPLE. */
1549 CASE_LOW (last_case
) = NULL
;
1550 tree tem
= case_label_vec
.pop ();
1551 gcc_assert (tem
== last_case
);
1552 sort_case_labels (case_label_vec
);
1554 /* Build the switch statement, setting last_case to be the default
1556 switch_stmt
= gimple_build_switch (finally_tmp
, last_case
,
1558 gimple_set_location (switch_stmt
, finally_loc
);
1560 /* Need to link SWITCH_STMT after running replace_goto_queue
1561 due to not wanting to process the same goto stmts twice. */
1562 gimple_seq_add_stmt (&tf
->top_p_seq
, switch_stmt
);
1563 gimple_seq_add_seq (&tf
->top_p_seq
, switch_body
);
1566 /* Decide whether or not we are going to duplicate the finally block.
1567 There are several considerations.
1569 Second, we'd like to prevent egregious code growth. One way to
1570 do this is to estimate the size of the finally block, multiply
1571 that by the number of copies we'd need to make, and compare against
1572 the estimate of the size of the switch machinery we'd have to add. */
1575 decide_copy_try_finally (int ndests
, bool may_throw
, gimple_seq finally
)
1577 int f_estimate
, sw_estimate
;
1580 /* If there's an EH_ELSE involved, the exception path is separate
1581 and really doesn't come into play for this computation. */
1582 eh_else
= get_eh_else (finally
);
1585 ndests
-= may_throw
;
1586 finally
= gimple_eh_else_n_body (eh_else
);
1591 gimple_stmt_iterator gsi
;
1596 for (gsi
= gsi_start (finally
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1598 /* Duplicate __builtin_stack_restore in the hope of eliminating it
1599 on the EH paths and, consequently, useless cleanups. */
1600 gimple
*stmt
= gsi_stmt (gsi
);
1601 if (!is_gimple_debug (stmt
)
1602 && !gimple_clobber_p (stmt
)
1603 && !gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
1609 /* Finally estimate N times, plus N gotos. */
1610 f_estimate
= estimate_num_insns_seq (finally
, &eni_size_weights
);
1611 f_estimate
= (f_estimate
+ 1) * ndests
;
1613 /* Switch statement (cost 10), N variable assignments, N gotos. */
1614 sw_estimate
= 10 + 2 * ndests
;
1616 /* Optimize for size clearly wants our best guess. */
1617 if (optimize_function_for_size_p (cfun
))
1618 return f_estimate
< sw_estimate
;
1620 /* ??? These numbers are completely made up so far. */
1622 return f_estimate
< 100 || f_estimate
< sw_estimate
* 2;
1624 return f_estimate
< 40 || f_estimate
* 2 < sw_estimate
* 3;
1627 /* REG is the enclosing region for a possible cleanup region, or the region
1628 itself. Returns TRUE if such a region would be unreachable.
1630 Cleanup regions within a must-not-throw region aren't actually reachable
1631 even if there are throwing stmts within them, because the personality
1632 routine will call terminate before unwinding. */
1635 cleanup_is_dead_in (eh_region reg
)
1637 while (reg
&& reg
->type
== ERT_CLEANUP
)
1639 return (reg
&& reg
->type
== ERT_MUST_NOT_THROW
);
1642 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
1643 to a sequence of labels and blocks, plus the exception region trees
1644 that record all the magic. This is complicated by the need to
1645 arrange for the FINALLY block to be executed on all exits. */
1648 lower_try_finally (struct leh_state
*state
, gtry
*tp
)
1650 struct leh_tf_state this_tf
;
1651 struct leh_state this_state
;
1653 gimple_seq old_eh_seq
;
1655 /* Process the try block. */
1657 memset (&this_tf
, 0, sizeof (this_tf
));
1658 this_tf
.try_finally_expr
= tp
;
1660 this_tf
.outer
= state
;
1661 if (using_eh_for_cleanups_p () && !cleanup_is_dead_in (state
->cur_region
))
1663 this_tf
.region
= gen_eh_region_cleanup (state
->cur_region
);
1664 this_state
.cur_region
= this_tf
.region
;
1668 this_tf
.region
= NULL
;
1669 this_state
.cur_region
= state
->cur_region
;
1672 this_state
.ehp_region
= state
->ehp_region
;
1673 this_state
.tf
= &this_tf
;
1675 old_eh_seq
= eh_seq
;
1678 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1680 /* Determine if the try block is escaped through the bottom. */
1681 this_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1683 /* Determine if any exceptions are possible within the try block. */
1685 this_tf
.may_throw
= eh_region_may_contain_throw (this_tf
.region
);
1686 if (this_tf
.may_throw
)
1687 honor_protect_cleanup_actions (state
, &this_state
, &this_tf
);
1689 /* Determine how many edges (still) reach the finally block. Or rather,
1690 how many destinations are reached by the finally block. Use this to
1691 determine how we process the finally block itself. */
1693 ndests
= this_tf
.dest_array
.length ();
1694 ndests
+= this_tf
.may_fallthru
;
1695 ndests
+= this_tf
.may_return
;
1696 ndests
+= this_tf
.may_throw
;
1698 /* If the FINALLY block is not reachable, dike it out. */
1701 gimple_seq_add_seq (&this_tf
.top_p_seq
, gimple_try_eval (tp
));
1702 gimple_try_set_cleanup (tp
, NULL
);
1704 /* If the finally block doesn't fall through, then any destination
1705 we might try to impose there isn't reached either. There may be
1706 some minor amount of cleanup and redirection still needed. */
1707 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp
)))
1708 lower_try_finally_nofallthru (state
, &this_tf
);
1710 /* We can easily special-case redirection to a single destination. */
1711 else if (ndests
== 1)
1712 lower_try_finally_onedest (state
, &this_tf
);
1713 else if (decide_copy_try_finally (ndests
, this_tf
.may_throw
,
1714 gimple_try_cleanup (tp
)))
1715 lower_try_finally_copy (state
, &this_tf
);
1717 lower_try_finally_switch (state
, &this_tf
);
1719 /* If someone requested we add a label at the end of the transformed
1721 if (this_tf
.fallthru_label
)
1723 /* This must be reached only if ndests == 0. */
1724 gimple
*x
= gimple_build_label (this_tf
.fallthru_label
);
1725 gimple_seq_add_stmt (&this_tf
.top_p_seq
, x
);
1728 this_tf
.dest_array
.release ();
1729 free (this_tf
.goto_queue
);
1730 if (this_tf
.goto_queue_map
)
1731 delete this_tf
.goto_queue_map
;
1733 /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1734 If there was no old eh_seq, then the append is trivially already done. */
1738 eh_seq
= old_eh_seq
;
1741 gimple_seq new_eh_seq
= eh_seq
;
1742 eh_seq
= old_eh_seq
;
1743 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1747 return this_tf
.top_p_seq
;
1750 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1751 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1752 exception region trees that records all the magic. */
1755 lower_catch (struct leh_state
*state
, gtry
*tp
)
1757 eh_region try_region
= NULL
;
1758 struct leh_state this_state
= *state
;
1759 gimple_stmt_iterator gsi
;
1761 gimple_seq new_seq
, cleanup
;
1763 geh_dispatch
*eh_dispatch
;
1764 location_t try_catch_loc
= gimple_location (tp
);
1765 location_t catch_loc
= UNKNOWN_LOCATION
;
1767 if (flag_exceptions
)
1769 try_region
= gen_eh_region_try (state
->cur_region
);
1770 this_state
.cur_region
= try_region
;
1773 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1775 if (!eh_region_may_contain_throw (try_region
))
1776 return gimple_try_eval (tp
);
1779 eh_dispatch
= gimple_build_eh_dispatch (try_region
->index
);
1780 gimple_seq_add_stmt (&new_seq
, eh_dispatch
);
1781 emit_resx (&new_seq
, try_region
);
1783 this_state
.cur_region
= state
->cur_region
;
1784 this_state
.ehp_region
= try_region
;
1786 /* Add eh_seq from lowering EH in the cleanup sequence after the cleanup
1787 itself, so that e.g. for coverage purposes the nested cleanups don't
1788 appear before the cleanup body. See PR64634 for details. */
1789 gimple_seq old_eh_seq
= eh_seq
;
1793 cleanup
= gimple_try_cleanup (tp
);
1794 for (gsi
= gsi_start (cleanup
);
1802 catch_stmt
= as_a
<gcatch
*> (gsi_stmt (gsi
));
1803 if (catch_loc
== UNKNOWN_LOCATION
)
1804 catch_loc
= gimple_location (catch_stmt
);
1805 c
= gen_eh_region_catch (try_region
, gimple_catch_types (catch_stmt
));
1807 handler
= gimple_catch_handler (catch_stmt
);
1808 lower_eh_constructs_1 (&this_state
, &handler
);
1810 c
->label
= create_artificial_label (UNKNOWN_LOCATION
);
1811 x
= gimple_build_label (c
->label
);
1812 gimple_seq_add_stmt (&new_seq
, x
);
1814 gimple_seq_add_seq (&new_seq
, handler
);
1816 if (gimple_seq_may_fallthru (new_seq
))
1819 out_label
= create_artificial_label (try_catch_loc
);
1821 x
= gimple_build_goto (out_label
);
1822 gimple_seq_add_stmt (&new_seq
, x
);
1828 /* Try to set a location on the dispatching construct to avoid inheriting
1829 the location of the previous statement. */
1830 gimple_set_location (eh_dispatch
, catch_loc
);
1832 gimple_try_set_cleanup (tp
, new_seq
);
1834 gimple_seq new_eh_seq
= eh_seq
;
1835 eh_seq
= old_eh_seq
;
1836 gimple_seq ret_seq
= frob_into_branch_around (tp
, try_region
, out_label
);
1837 gimple_seq_add_seq (&eh_seq
, new_eh_seq
);
1841 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1842 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
1843 region trees that record all the magic. */
1846 lower_eh_filter (struct leh_state
*state
, gtry
*tp
)
1848 struct leh_state this_state
= *state
;
1849 eh_region this_region
= NULL
;
1853 inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1855 if (flag_exceptions
)
1857 this_region
= gen_eh_region_allowed (state
->cur_region
,
1858 gimple_eh_filter_types (inner
));
1859 this_state
.cur_region
= this_region
;
1862 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1864 if (!eh_region_may_contain_throw (this_region
))
1865 return gimple_try_eval (tp
);
1867 this_state
.cur_region
= state
->cur_region
;
1868 this_state
.ehp_region
= this_region
;
1871 x
= gimple_build_eh_dispatch (this_region
->index
);
1872 gimple_set_location (x
, gimple_location (tp
));
1873 gimple_seq_add_stmt (&new_seq
, x
);
1874 emit_resx (&new_seq
, this_region
);
1876 this_region
->u
.allowed
.label
= create_artificial_label (UNKNOWN_LOCATION
);
1877 x
= gimple_build_label (this_region
->u
.allowed
.label
);
1878 gimple_seq_add_stmt (&new_seq
, x
);
1880 lower_eh_constructs_1 (&this_state
, gimple_eh_filter_failure_ptr (inner
));
1881 gimple_seq_add_seq (&new_seq
, gimple_eh_filter_failure (inner
));
1883 gimple_try_set_cleanup (tp
, new_seq
);
1885 return frob_into_branch_around (tp
, this_region
, NULL
);
1888 /* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with
1889 an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1890 plus the exception region trees that record all the magic. */
1893 lower_eh_must_not_throw (struct leh_state
*state
, gtry
*tp
)
1895 struct leh_state this_state
= *state
;
1897 if (flag_exceptions
)
1899 gimple
*inner
= gimple_seq_first_stmt (gimple_try_cleanup (tp
));
1900 eh_region this_region
;
1902 this_region
= gen_eh_region_must_not_throw (state
->cur_region
);
1903 this_region
->u
.must_not_throw
.failure_decl
1904 = gimple_eh_must_not_throw_fndecl (
1905 as_a
<geh_mnt
*> (inner
));
1906 this_region
->u
.must_not_throw
.failure_loc
1907 = LOCATION_LOCUS (gimple_location (tp
));
1909 /* In order to get mangling applied to this decl, we must mark it
1910 used now. Otherwise, pass_ipa_free_lang_data won't think it
1912 TREE_USED (this_region
->u
.must_not_throw
.failure_decl
) = 1;
1914 this_state
.cur_region
= this_region
;
1917 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1919 return gimple_try_eval (tp
);
1922 /* Implement a cleanup expression. This is similar to try-finally,
1923 except that we only execute the cleanup block for exception edges. */
1926 lower_cleanup (struct leh_state
*state
, gtry
*tp
)
1928 struct leh_state this_state
= *state
;
1929 eh_region this_region
= NULL
;
1930 struct leh_tf_state fake_tf
;
1932 bool cleanup_dead
= cleanup_is_dead_in (state
->cur_region
);
1934 if (flag_exceptions
&& !cleanup_dead
)
1936 this_region
= gen_eh_region_cleanup (state
->cur_region
);
1937 this_state
.cur_region
= this_region
;
1940 lower_eh_constructs_1 (&this_state
, gimple_try_eval_ptr (tp
));
1942 if (cleanup_dead
|| !eh_region_may_contain_throw (this_region
))
1943 return gimple_try_eval (tp
);
1945 /* Build enough of a try-finally state so that we can reuse
1946 honor_protect_cleanup_actions. */
1947 memset (&fake_tf
, 0, sizeof (fake_tf
));
1948 fake_tf
.top_p
= fake_tf
.try_finally_expr
= tp
;
1949 fake_tf
.outer
= state
;
1950 fake_tf
.region
= this_region
;
1951 fake_tf
.may_fallthru
= gimple_seq_may_fallthru (gimple_try_eval (tp
));
1952 fake_tf
.may_throw
= true;
1954 honor_protect_cleanup_actions (state
, NULL
, &fake_tf
);
1956 if (fake_tf
.may_throw
)
1958 /* In this case honor_protect_cleanup_actions had nothing to do,
1959 and we should process this normally. */
1960 lower_eh_constructs_1 (state
, gimple_try_cleanup_ptr (tp
));
1961 result
= frob_into_branch_around (tp
, this_region
,
1962 fake_tf
.fallthru_label
);
1966 /* In this case honor_protect_cleanup_actions did nearly all of
1967 the work. All we have left is to append the fallthru_label. */
1969 result
= gimple_try_eval (tp
);
1970 if (fake_tf
.fallthru_label
)
1972 gimple
*x
= gimple_build_label (fake_tf
.fallthru_label
);
1973 gimple_seq_add_stmt (&result
, x
);
1979 /* Main loop for lowering eh constructs. Also moves gsi to the next
1983 lower_eh_constructs_2 (struct leh_state
*state
, gimple_stmt_iterator
*gsi
)
1987 gimple
*stmt
= gsi_stmt (*gsi
);
1989 switch (gimple_code (stmt
))
1993 tree fndecl
= gimple_call_fndecl (stmt
);
1996 if (fndecl
&& fndecl_built_in_p (fndecl
, BUILT_IN_NORMAL
))
1997 switch (DECL_FUNCTION_CODE (fndecl
))
1999 case BUILT_IN_EH_POINTER
:
2000 /* The front end may have generated a call to
2001 __builtin_eh_pointer (0) within a catch region. Replace
2002 this zero argument with the current catch region number. */
2003 if (state
->ehp_region
)
2005 tree nr
= build_int_cst (integer_type_node
,
2006 state
->ehp_region
->index
);
2007 gimple_call_set_arg (stmt
, 0, nr
);
2011 /* The user has dome something silly. Remove it. */
2012 rhs
= null_pointer_node
;
2017 case BUILT_IN_EH_FILTER
:
2018 /* ??? This should never appear, but since it's a builtin it
2019 is accessible to abuse by users. Just remove it and
2020 replace the use with the arbitrary value zero. */
2021 rhs
= build_int_cst (TREE_TYPE (TREE_TYPE (fndecl
)), 0);
2023 lhs
= gimple_call_lhs (stmt
);
2024 x
= gimple_build_assign (lhs
, rhs
);
2025 gsi_insert_before (gsi
, x
, GSI_SAME_STMT
);
2028 case BUILT_IN_EH_COPY_VALUES
:
2029 /* Likewise this should not appear. Remove it. */
2030 gsi_remove (gsi
, true);
2040 /* If the stmt can throw, use a new temporary for the assignment
2041 to a LHS. This makes sure the old value of the LHS is
2042 available on the EH edge. Only do so for statements that
2043 potentially fall through (no noreturn calls e.g.), otherwise
2044 this new assignment might create fake fallthru regions. */
2045 if (stmt_could_throw_p (cfun
, stmt
)
2046 && gimple_has_lhs (stmt
)
2047 && gimple_stmt_may_fallthru (stmt
)
2048 && !tree_could_throw_p (gimple_get_lhs (stmt
))
2049 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt
))))
2051 tree lhs
= gimple_get_lhs (stmt
);
2052 tree tmp
= create_tmp_var (TREE_TYPE (lhs
));
2053 gimple
*s
= gimple_build_assign (lhs
, tmp
);
2054 gimple_set_location (s
, gimple_location (stmt
));
2055 gimple_set_block (s
, gimple_block (stmt
));
2056 gimple_set_lhs (stmt
, tmp
);
2057 if (TREE_CODE (TREE_TYPE (tmp
)) == COMPLEX_TYPE
2058 || TREE_CODE (TREE_TYPE (tmp
)) == VECTOR_TYPE
)
2059 DECL_GIMPLE_REG_P (tmp
) = 1;
2060 gsi_insert_after (gsi
, s
, GSI_SAME_STMT
);
2062 /* Look for things that can throw exceptions, and record them. */
2063 if (state
->cur_region
&& stmt_could_throw_p (cfun
, stmt
))
2065 record_stmt_eh_region (state
->cur_region
, stmt
);
2066 note_eh_region_may_contain_throw (state
->cur_region
);
2073 maybe_record_in_goto_queue (state
, stmt
);
2077 verify_norecord_switch_expr (state
, as_a
<gswitch
*> (stmt
));
2082 gtry
*try_stmt
= as_a
<gtry
*> (stmt
);
2083 if (gimple_try_kind (try_stmt
) == GIMPLE_TRY_FINALLY
)
2084 replace
= lower_try_finally (state
, try_stmt
);
2087 x
= gimple_seq_first_stmt (gimple_try_cleanup (try_stmt
));
2090 replace
= gimple_try_eval (try_stmt
);
2091 lower_eh_constructs_1 (state
, &replace
);
2094 switch (gimple_code (x
))
2097 replace
= lower_catch (state
, try_stmt
);
2099 case GIMPLE_EH_FILTER
:
2100 replace
= lower_eh_filter (state
, try_stmt
);
2102 case GIMPLE_EH_MUST_NOT_THROW
:
2103 replace
= lower_eh_must_not_throw (state
, try_stmt
);
2105 case GIMPLE_EH_ELSE
:
2106 /* This code is only valid with GIMPLE_TRY_FINALLY. */
2109 replace
= lower_cleanup (state
, try_stmt
);
2115 /* Remove the old stmt and insert the transformed sequence
2117 gsi_insert_seq_before (gsi
, replace
, GSI_SAME_STMT
);
2118 gsi_remove (gsi
, true);
2120 /* Return since we don't want gsi_next () */
2123 case GIMPLE_EH_ELSE
:
2124 /* We should be eliminating this in lower_try_finally et al. */
2128 /* A type, a decl, or some kind of statement that we're not
2129 interested in. Don't walk them. */
2136 /* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2139 lower_eh_constructs_1 (struct leh_state
*state
, gimple_seq
*pseq
)
2141 gimple_stmt_iterator gsi
;
2142 for (gsi
= gsi_start (*pseq
); !gsi_end_p (gsi
);)
2143 lower_eh_constructs_2 (state
, &gsi
);
2148 const pass_data pass_data_lower_eh
=
2150 GIMPLE_PASS
, /* type */
2152 OPTGROUP_NONE
, /* optinfo_flags */
2153 TV_TREE_EH
, /* tv_id */
2154 PROP_gimple_lcf
, /* properties_required */
2155 PROP_gimple_leh
, /* properties_provided */
2156 0, /* properties_destroyed */
2157 0, /* todo_flags_start */
2158 0, /* todo_flags_finish */
2161 class pass_lower_eh
: public gimple_opt_pass
2164 pass_lower_eh (gcc::context
*ctxt
)
2165 : gimple_opt_pass (pass_data_lower_eh
, ctxt
)
2168 /* opt_pass methods: */
2169 virtual unsigned int execute (function
*);
2171 }; // class pass_lower_eh
2174 pass_lower_eh::execute (function
*fun
)
2176 struct leh_state null_state
;
2179 bodyp
= gimple_body (current_function_decl
);
2183 finally_tree
= new hash_table
<finally_tree_hasher
> (31);
2184 eh_region_may_contain_throw_map
= BITMAP_ALLOC (NULL
);
2185 memset (&null_state
, 0, sizeof (null_state
));
2187 collect_finally_tree_1 (bodyp
, NULL
);
2188 lower_eh_constructs_1 (&null_state
, &bodyp
);
2189 gimple_set_body (current_function_decl
, bodyp
);
2191 /* We assume there's a return statement, or something, at the end of
2192 the function, and thus ploping the EH sequence afterward won't
2194 gcc_assert (!gimple_seq_may_fallthru (bodyp
));
2195 gimple_seq_add_seq (&bodyp
, eh_seq
);
2197 /* We assume that since BODYP already existed, adding EH_SEQ to it
2198 didn't change its value, and we don't have to re-set the function. */
2199 gcc_assert (bodyp
== gimple_body (current_function_decl
));
2201 delete finally_tree
;
2202 finally_tree
= NULL
;
2203 BITMAP_FREE (eh_region_may_contain_throw_map
);
2206 /* If this function needs a language specific EH personality routine
2207 and the frontend didn't already set one do so now. */
2208 if (function_needs_eh_personality (fun
) == eh_personality_lang
2209 && !DECL_FUNCTION_PERSONALITY (current_function_decl
))
2210 DECL_FUNCTION_PERSONALITY (current_function_decl
)
2211 = lang_hooks
.eh_personality ();
2219 make_pass_lower_eh (gcc::context
*ctxt
)
2221 return new pass_lower_eh (ctxt
);
2224 /* Create the multiple edges from an EH_DISPATCH statement to all of
2225 the possible handlers for its EH region. Return true if there's
2226 no fallthru edge; false if there is. */
2229 make_eh_dispatch_edges (geh_dispatch
*stmt
)
2233 basic_block src
, dst
;
2235 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2236 src
= gimple_bb (stmt
);
2241 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2243 dst
= label_to_block (cfun
, c
->label
);
2244 make_edge (src
, dst
, 0);
2246 /* A catch-all handler doesn't have a fallthru. */
2247 if (c
->type_list
== NULL
)
2252 case ERT_ALLOWED_EXCEPTIONS
:
2253 dst
= label_to_block (cfun
, r
->u
.allowed
.label
);
2254 make_edge (src
, dst
, 0);
2264 /* Create the single EH edge from STMT to its nearest landing pad,
2265 if there is such a landing pad within the current function. */
2268 make_eh_edges (gimple
*stmt
)
2270 basic_block src
, dst
;
2274 lp_nr
= lookup_stmt_eh_lp (stmt
);
2278 lp
= get_eh_landing_pad_from_number (lp_nr
);
2279 gcc_assert (lp
!= NULL
);
2281 src
= gimple_bb (stmt
);
2282 dst
= label_to_block (cfun
, lp
->post_landing_pad
);
2283 make_edge (src
, dst
, EDGE_EH
);
2286 /* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2287 do not actually perform the final edge redirection.
2289 CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2290 we intend to change the destination EH region as well; this means
2291 EH_LANDING_PAD_NR must already be set on the destination block label.
2292 If false, we're being called from generic cfg manipulation code and we
2293 should preserve our place within the region tree. */
2296 redirect_eh_edge_1 (edge edge_in
, basic_block new_bb
, bool change_region
)
2298 eh_landing_pad old_lp
, new_lp
;
2301 int old_lp_nr
, new_lp_nr
;
2302 tree old_label
, new_label
;
2306 old_bb
= edge_in
->dest
;
2307 old_label
= gimple_block_label (old_bb
);
2308 old_lp_nr
= EH_LANDING_PAD_NR (old_label
);
2309 gcc_assert (old_lp_nr
> 0);
2310 old_lp
= get_eh_landing_pad_from_number (old_lp_nr
);
2312 throw_stmt
= last_stmt (edge_in
->src
);
2313 gcc_assert (lookup_stmt_eh_lp (throw_stmt
) == old_lp_nr
);
2315 new_label
= gimple_block_label (new_bb
);
2317 /* Look for an existing region that might be using NEW_BB already. */
2318 new_lp_nr
= EH_LANDING_PAD_NR (new_label
);
2321 new_lp
= get_eh_landing_pad_from_number (new_lp_nr
);
2322 gcc_assert (new_lp
);
2324 /* Unless CHANGE_REGION is true, the new and old landing pad
2325 had better be associated with the same EH region. */
2326 gcc_assert (change_region
|| new_lp
->region
== old_lp
->region
);
2331 gcc_assert (!change_region
);
2334 /* Notice when we redirect the last EH edge away from OLD_BB. */
2335 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
2336 if (e
!= edge_in
&& (e
->flags
& EDGE_EH
))
2341 /* NEW_LP already exists. If there are still edges into OLD_LP,
2342 there's nothing to do with the EH tree. If there are no more
2343 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2344 If CHANGE_REGION is true, then our caller is expecting to remove
2346 if (e
== NULL
&& !change_region
)
2347 remove_eh_landing_pad (old_lp
);
2351 /* No correct landing pad exists. If there are no more edges
2352 into OLD_LP, then we can simply re-use the existing landing pad.
2353 Otherwise, we have to create a new landing pad. */
2356 EH_LANDING_PAD_NR (old_lp
->post_landing_pad
) = 0;
2360 new_lp
= gen_eh_landing_pad (old_lp
->region
);
2361 new_lp
->post_landing_pad
= new_label
;
2362 EH_LANDING_PAD_NR (new_label
) = new_lp
->index
;
2365 /* Maybe move the throwing statement to the new region. */
2366 if (old_lp
!= new_lp
)
2368 remove_stmt_from_eh_lp (throw_stmt
);
2369 add_stmt_to_eh_lp (throw_stmt
, new_lp
->index
);
2373 /* Redirect EH edge E to NEW_BB. */
2376 redirect_eh_edge (edge edge_in
, basic_block new_bb
)
2378 redirect_eh_edge_1 (edge_in
, new_bb
, false);
2379 return ssa_redirect_edge (edge_in
, new_bb
);
2382 /* This is a subroutine of gimple_redirect_edge_and_branch. Update the
2383 labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2384 The actual edge update will happen in the caller. */
2387 redirect_eh_dispatch_edge (geh_dispatch
*stmt
, edge e
, basic_block new_bb
)
2389 tree new_lab
= gimple_block_label (new_bb
);
2390 bool any_changed
= false;
2395 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
2399 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
2401 old_bb
= label_to_block (cfun
, c
->label
);
2402 if (old_bb
== e
->dest
)
2410 case ERT_ALLOWED_EXCEPTIONS
:
2411 old_bb
= label_to_block (cfun
, r
->u
.allowed
.label
);
2412 gcc_assert (old_bb
== e
->dest
);
2413 r
->u
.allowed
.label
= new_lab
;
2421 gcc_assert (any_changed
);
2424 /* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2427 operation_could_trap_helper_p (enum tree_code op
,
2438 case TRUNC_DIV_EXPR
:
2440 case FLOOR_DIV_EXPR
:
2441 case ROUND_DIV_EXPR
:
2442 case EXACT_DIV_EXPR
:
2444 case FLOOR_MOD_EXPR
:
2445 case ROUND_MOD_EXPR
:
2446 case TRUNC_MOD_EXPR
:
2451 return flag_trapping_math
;
2452 if (!TREE_CONSTANT (divisor
) || integer_zerop (divisor
))
2461 /* Some floating point comparisons may trap. */
2466 case UNORDERED_EXPR
:
2478 /* These operations don't trap with floating point. */
2484 /* ABSU_EXPR never traps. */
2490 /* Any floating arithmetic may trap. */
2491 if (fp_operation
&& flag_trapping_math
)
2499 /* Constructing an object cannot trap. */
2503 /* Any floating arithmetic may trap. */
2504 if (fp_operation
&& flag_trapping_math
)
2512 /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2513 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2514 type operands that may trap. If OP is a division operator, DIVISOR contains
2515 the value of the divisor. */
2518 operation_could_trap_p (enum tree_code op
, bool fp_operation
, bool honor_trapv
,
2521 bool honor_nans
= (fp_operation
&& flag_trapping_math
2522 && !flag_finite_math_only
);
2523 bool honor_snans
= fp_operation
&& flag_signaling_nans
!= 0;
2526 if (TREE_CODE_CLASS (op
) != tcc_comparison
2527 && TREE_CODE_CLASS (op
) != tcc_unary
2528 && TREE_CODE_CLASS (op
) != tcc_binary
)
2531 return operation_could_trap_helper_p (op
, fp_operation
, honor_trapv
,
2532 honor_nans
, honor_snans
, divisor
,
2537 /* Returns true if it is possible to prove that the index of
2538 an array access REF (an ARRAY_REF expression) falls into the
2542 in_array_bounds_p (tree ref
)
2544 tree idx
= TREE_OPERAND (ref
, 1);
2547 if (TREE_CODE (idx
) != INTEGER_CST
)
2550 min
= array_ref_low_bound (ref
);
2551 max
= array_ref_up_bound (ref
);
2554 || TREE_CODE (min
) != INTEGER_CST
2555 || TREE_CODE (max
) != INTEGER_CST
)
2558 if (tree_int_cst_lt (idx
, min
)
2559 || tree_int_cst_lt (max
, idx
))
2565 /* Returns true if it is possible to prove that the range of
2566 an array access REF (an ARRAY_RANGE_REF expression) falls
2567 into the array bounds. */
2570 range_in_array_bounds_p (tree ref
)
2572 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (ref
));
2573 tree range_min
, range_max
, min
, max
;
2575 range_min
= TYPE_MIN_VALUE (domain_type
);
2576 range_max
= TYPE_MAX_VALUE (domain_type
);
2579 || TREE_CODE (range_min
) != INTEGER_CST
2580 || TREE_CODE (range_max
) != INTEGER_CST
)
2583 min
= array_ref_low_bound (ref
);
2584 max
= array_ref_up_bound (ref
);
2587 || TREE_CODE (min
) != INTEGER_CST
2588 || TREE_CODE (max
) != INTEGER_CST
)
2591 if (tree_int_cst_lt (range_min
, min
)
2592 || tree_int_cst_lt (max
, range_max
))
2598 /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2599 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2600 This routine expects only GIMPLE lhs or rhs input. */
2603 tree_could_trap_p (tree expr
)
2605 enum tree_code code
;
2606 bool fp_operation
= false;
2607 bool honor_trapv
= false;
2608 tree t
, base
, div
= NULL_TREE
;
2613 code
= TREE_CODE (expr
);
2614 t
= TREE_TYPE (expr
);
2618 if (COMPARISON_CLASS_P (expr
))
2619 fp_operation
= FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0)));
2621 fp_operation
= FLOAT_TYPE_P (t
);
2622 honor_trapv
= INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
);
2625 if (TREE_CODE_CLASS (code
) == tcc_binary
)
2626 div
= TREE_OPERAND (expr
, 1);
2627 if (operation_could_trap_p (code
, fp_operation
, honor_trapv
, div
))
2637 case VIEW_CONVERT_EXPR
:
2638 case WITH_SIZE_EXPR
:
2639 expr
= TREE_OPERAND (expr
, 0);
2640 code
= TREE_CODE (expr
);
2643 case ARRAY_RANGE_REF
:
2644 base
= TREE_OPERAND (expr
, 0);
2645 if (tree_could_trap_p (base
))
2647 if (TREE_THIS_NOTRAP (expr
))
2649 return !range_in_array_bounds_p (expr
);
2652 base
= TREE_OPERAND (expr
, 0);
2653 if (tree_could_trap_p (base
))
2655 if (TREE_THIS_NOTRAP (expr
))
2657 return !in_array_bounds_p (expr
);
2659 case TARGET_MEM_REF
:
2661 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
2662 && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr
, 0), 0)))
2664 if (TREE_THIS_NOTRAP (expr
))
2666 /* We cannot prove that the access is in-bounds when we have
2667 variable-index TARGET_MEM_REFs. */
2668 if (code
== TARGET_MEM_REF
2669 && (TMR_INDEX (expr
) || TMR_INDEX2 (expr
)))
2671 if (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
)
2673 tree base
= TREE_OPERAND (TREE_OPERAND (expr
, 0), 0);
2674 poly_offset_int off
= mem_ref_offset (expr
);
2675 if (maybe_lt (off
, 0))
2677 if (TREE_CODE (base
) == STRING_CST
)
2678 return maybe_le (TREE_STRING_LENGTH (base
), off
);
2679 tree size
= DECL_SIZE_UNIT (base
);
2680 if (size
== NULL_TREE
2681 || !poly_int_tree_p (size
)
2682 || maybe_le (wi::to_poly_offset (size
), off
))
2684 /* Now we are sure the first byte of the access is inside
2691 return !TREE_THIS_NOTRAP (expr
);
2694 return TREE_THIS_VOLATILE (expr
);
2697 t
= get_callee_fndecl (expr
);
2698 /* Assume that calls to weak functions may trap. */
2699 if (!t
|| !DECL_P (t
))
2702 return tree_could_trap_p (t
);
2706 /* Assume that accesses to weak functions may trap, unless we know
2707 they are certainly defined in current TU or in some other
2709 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
) && DECL_EXTERNAL (expr
))
2711 cgraph_node
*node
= cgraph_node::get (expr
);
2713 node
= node
->function_symbol ();
2714 return !(node
&& node
->in_other_partition
);
2719 /* Assume that accesses to weak vars may trap, unless we know
2720 they are certainly defined in current TU or in some other
2722 if (DECL_WEAK (expr
) && !DECL_COMDAT (expr
) && DECL_EXTERNAL (expr
))
2724 varpool_node
*node
= varpool_node::get (expr
);
2726 node
= node
->ultimate_alias_target ();
2727 return !(node
&& node
->in_other_partition
);
2736 /* Return non-NULL if there is an integer operation with trapping overflow
2737 we can rewrite into non-trapping. Called via walk_tree from
2738 rewrite_to_non_trapping_overflow. */
2741 find_trapping_overflow (tree
*tp
, int *walk_subtrees
, void *data
)
2744 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (*tp
))
2745 && !operation_no_trapping_overflow (TREE_TYPE (*tp
), TREE_CODE (*tp
)))
2747 if (IS_TYPE_OR_DECL_P (*tp
)
2748 || (TREE_CODE (*tp
) == SAVE_EXPR
&& data
== NULL
))
2753 /* Rewrite selected operations into unsigned arithmetics, so that they
2754 don't trap on overflow. */
2757 replace_trapping_overflow (tree
*tp
, int *walk_subtrees
, void *data
)
2759 if (find_trapping_overflow (tp
, walk_subtrees
, data
))
2761 tree type
= TREE_TYPE (*tp
);
2762 tree utype
= unsigned_type_for (type
);
2764 int len
= TREE_OPERAND_LENGTH (*tp
);
2765 for (int i
= 0; i
< len
; ++i
)
2766 walk_tree (&TREE_OPERAND (*tp
, i
), replace_trapping_overflow
,
2767 data
, (hash_set
<tree
> *) data
);
2769 if (TREE_CODE (*tp
) == ABS_EXPR
)
2771 TREE_SET_CODE (*tp
, ABSU_EXPR
);
2772 TREE_TYPE (*tp
) = utype
;
2773 *tp
= fold_convert (type
, *tp
);
2777 TREE_TYPE (*tp
) = utype
;
2778 len
= TREE_OPERAND_LENGTH (*tp
);
2779 for (int i
= 0; i
< len
; ++i
)
2780 TREE_OPERAND (*tp
, i
)
2781 = fold_convert (utype
, TREE_OPERAND (*tp
, i
));
2782 *tp
= fold_convert (type
, *tp
);
2788 /* If any subexpression of EXPR can trap due to -ftrapv, rewrite it
2789 using unsigned arithmetics to avoid traps in it. */
2792 rewrite_to_non_trapping_overflow (tree expr
)
2796 hash_set
<tree
> pset
;
2797 if (!walk_tree (&expr
, find_trapping_overflow
, &pset
, &pset
))
2799 expr
= unshare_expr (expr
);
2801 walk_tree (&expr
, replace_trapping_overflow
, &pset
, &pset
);
2805 /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2806 an assignment or a conditional) may throw. */
2809 stmt_could_throw_1_p (gassign
*stmt
)
2811 enum tree_code code
= gimple_assign_rhs_code (stmt
);
2812 bool honor_nans
= false;
2813 bool honor_snans
= false;
2814 bool fp_operation
= false;
2815 bool honor_trapv
= false;
2820 if (TREE_CODE_CLASS (code
) == tcc_comparison
2821 || TREE_CODE_CLASS (code
) == tcc_unary
2822 || TREE_CODE_CLASS (code
) == tcc_binary
)
2824 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2825 t
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
2827 t
= gimple_expr_type (stmt
);
2828 fp_operation
= FLOAT_TYPE_P (t
);
2831 honor_nans
= flag_trapping_math
&& !flag_finite_math_only
;
2832 honor_snans
= flag_signaling_nans
!= 0;
2834 else if (INTEGRAL_TYPE_P (t
) && TYPE_OVERFLOW_TRAPS (t
))
2838 /* First check the LHS. */
2839 if (tree_could_trap_p (gimple_assign_lhs (stmt
)))
2842 /* Check if the main expression may trap. */
2843 ret
= operation_could_trap_helper_p (code
, fp_operation
, honor_trapv
,
2844 honor_nans
, honor_snans
,
2845 gimple_assign_rhs2 (stmt
),
2850 /* If the expression does not trap, see if any of the individual operands may
2852 for (i
= 1; i
< gimple_num_ops (stmt
); i
++)
2853 if (tree_could_trap_p (gimple_op (stmt
, i
)))
2860 /* Return true if statement STMT within FUN could throw an exception. */
2863 stmt_could_throw_p (function
*fun
, gimple
*stmt
)
2865 if (!flag_exceptions
)
2868 /* The only statements that can throw an exception are assignments,
2869 conditionals, calls, resx, and asms. */
2870 switch (gimple_code (stmt
))
2876 return !gimple_call_nothrow_p (as_a
<gcall
*> (stmt
));
2880 if (fun
&& !fun
->can_throw_non_call_exceptions
)
2882 gcond
*cond
= as_a
<gcond
*> (stmt
);
2883 tree lhs
= gimple_cond_lhs (cond
);
2884 return operation_could_trap_p (gimple_cond_code (cond
),
2885 FLOAT_TYPE_P (TREE_TYPE (lhs
)),
2890 if ((fun
&& !fun
->can_throw_non_call_exceptions
)
2891 || gimple_clobber_p (stmt
))
2893 return stmt_could_throw_1_p (as_a
<gassign
*> (stmt
));
2896 if (fun
&& !fun
->can_throw_non_call_exceptions
)
2898 return gimple_asm_volatile_p (as_a
<gasm
*> (stmt
));
2906 /* Return true if expression T could throw an exception. */
2909 tree_could_throw_p (tree t
)
2911 if (!flag_exceptions
)
2913 if (TREE_CODE (t
) == MODIFY_EXPR
)
2915 if (cfun
->can_throw_non_call_exceptions
2916 && tree_could_trap_p (TREE_OPERAND (t
, 0)))
2918 t
= TREE_OPERAND (t
, 1);
2921 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
2922 t
= TREE_OPERAND (t
, 0);
2923 if (TREE_CODE (t
) == CALL_EXPR
)
2924 return (call_expr_flags (t
) & ECF_NOTHROW
) == 0;
2925 if (cfun
->can_throw_non_call_exceptions
)
2926 return tree_could_trap_p (t
);
2930 /* Return true if STMT can throw an exception that is not caught within its
2931 function FUN. FUN can be NULL but the function is extra conservative
2935 stmt_can_throw_external (function
*fun
, gimple
*stmt
)
2939 if (!stmt_could_throw_p (fun
, stmt
))
2944 lp_nr
= lookup_stmt_eh_lp_fn (fun
, stmt
);
2948 /* Return true if STMT can throw an exception that is caught within its
2952 stmt_can_throw_internal (function
*fun
, gimple
*stmt
)
2956 gcc_checking_assert (fun
);
2957 if (!stmt_could_throw_p (fun
, stmt
))
2960 lp_nr
= lookup_stmt_eh_lp_fn (fun
, stmt
);
2964 /* Given a statement STMT in IFUN, if STMT can no longer throw, then
2965 remove any entry it might have from the EH table. Return true if
2966 any change was made. */
2969 maybe_clean_eh_stmt_fn (struct function
*ifun
, gimple
*stmt
)
2971 if (stmt_could_throw_p (ifun
, stmt
))
2973 return remove_stmt_from_eh_lp_fn (ifun
, stmt
);
2976 /* Likewise, but always use the current function. */
2979 maybe_clean_eh_stmt (gimple
*stmt
)
2981 return maybe_clean_eh_stmt_fn (cfun
, stmt
);
2984 /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
2985 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
2986 in the table if it should be in there. Return TRUE if a replacement was
2987 done that my require an EH edge purge. */
2990 maybe_clean_or_replace_eh_stmt (gimple
*old_stmt
, gimple
*new_stmt
)
2992 int lp_nr
= lookup_stmt_eh_lp (old_stmt
);
2996 bool new_stmt_could_throw
= stmt_could_throw_p (cfun
, new_stmt
);
2998 if (new_stmt
== old_stmt
&& new_stmt_could_throw
)
3001 remove_stmt_from_eh_lp (old_stmt
);
3002 if (new_stmt_could_throw
)
3004 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
3014 /* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
3015 in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
3016 operand is the return value of duplicate_eh_regions. */
3019 maybe_duplicate_eh_stmt_fn (struct function
*new_fun
, gimple
*new_stmt
,
3020 struct function
*old_fun
, gimple
*old_stmt
,
3021 hash_map
<void *, void *> *map
,
3024 int old_lp_nr
, new_lp_nr
;
3026 if (!stmt_could_throw_p (new_fun
, new_stmt
))
3029 old_lp_nr
= lookup_stmt_eh_lp_fn (old_fun
, old_stmt
);
3032 if (default_lp_nr
== 0)
3034 new_lp_nr
= default_lp_nr
;
3036 else if (old_lp_nr
> 0)
3038 eh_landing_pad old_lp
, new_lp
;
3040 old_lp
= (*old_fun
->eh
->lp_array
)[old_lp_nr
];
3041 new_lp
= static_cast<eh_landing_pad
> (*map
->get (old_lp
));
3042 new_lp_nr
= new_lp
->index
;
3046 eh_region old_r
, new_r
;
3048 old_r
= (*old_fun
->eh
->region_array
)[-old_lp_nr
];
3049 new_r
= static_cast<eh_region
> (*map
->get (old_r
));
3050 new_lp_nr
= -new_r
->index
;
3053 add_stmt_to_eh_lp_fn (new_fun
, new_stmt
, new_lp_nr
);
3057 /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
3058 and thus no remapping is required. */
3061 maybe_duplicate_eh_stmt (gimple
*new_stmt
, gimple
*old_stmt
)
3065 if (!stmt_could_throw_p (cfun
, new_stmt
))
3068 lp_nr
= lookup_stmt_eh_lp (old_stmt
);
3072 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
3076 /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
3077 GIMPLE_TRY) that are similar enough to be considered the same. Currently
3078 this only handles handlers consisting of a single call, as that's the
3079 important case for C++: a destructor call for a particular object showing
3080 up in multiple handlers. */
3083 same_handler_p (gimple_seq oneh
, gimple_seq twoh
)
3085 gimple_stmt_iterator gsi
;
3086 gimple
*ones
, *twos
;
3089 gsi
= gsi_start (oneh
);
3090 if (!gsi_one_before_end_p (gsi
))
3092 ones
= gsi_stmt (gsi
);
3094 gsi
= gsi_start (twoh
);
3095 if (!gsi_one_before_end_p (gsi
))
3097 twos
= gsi_stmt (gsi
);
3099 if (!is_gimple_call (ones
)
3100 || !is_gimple_call (twos
)
3101 || gimple_call_lhs (ones
)
3102 || gimple_call_lhs (twos
)
3103 || gimple_call_chain (ones
)
3104 || gimple_call_chain (twos
)
3105 || !gimple_call_same_target_p (ones
, twos
)
3106 || gimple_call_num_args (ones
) != gimple_call_num_args (twos
))
3109 for (ai
= 0; ai
< gimple_call_num_args (ones
); ++ai
)
3110 if (!operand_equal_p (gimple_call_arg (ones
, ai
),
3111 gimple_call_arg (twos
, ai
), 0))
3118 try { A() } finally { try { ~B() } catch { ~A() } }
3119 try { ... } finally { ~A() }
3121 try { A() } catch { ~B() }
3122 try { ~B() ... } finally { ~A() }
3124 This occurs frequently in C++, where A is a local variable and B is a
3125 temporary used in the initializer for A. */
3128 optimize_double_finally (gtry
*one
, gtry
*two
)
3131 gimple_stmt_iterator gsi
;
3134 cleanup
= gimple_try_cleanup (one
);
3135 gsi
= gsi_start (cleanup
);
3136 if (!gsi_one_before_end_p (gsi
))
3139 oneh
= gsi_stmt (gsi
);
3140 if (gimple_code (oneh
) != GIMPLE_TRY
3141 || gimple_try_kind (oneh
) != GIMPLE_TRY_CATCH
)
3144 if (same_handler_p (gimple_try_cleanup (oneh
), gimple_try_cleanup (two
)))
3146 gimple_seq seq
= gimple_try_eval (oneh
);
3148 gimple_try_set_cleanup (one
, seq
);
3149 gimple_try_set_kind (one
, GIMPLE_TRY_CATCH
);
3150 seq
= copy_gimple_seq_and_replace_locals (seq
);
3151 gimple_seq_add_seq (&seq
, gimple_try_eval (two
));
3152 gimple_try_set_eval (two
, seq
);
3156 /* Perform EH refactoring optimizations that are simpler to do when code
3157 flow has been lowered but EH structures haven't. */
3160 refactor_eh_r (gimple_seq seq
)
3162 gimple_stmt_iterator gsi
;
3167 gsi
= gsi_start (seq
);
3171 if (gsi_end_p (gsi
))
3174 two
= gsi_stmt (gsi
);
3176 if (gtry
*try_one
= dyn_cast
<gtry
*> (one
))
3177 if (gtry
*try_two
= dyn_cast
<gtry
*> (two
))
3178 if (gimple_try_kind (try_one
) == GIMPLE_TRY_FINALLY
3179 && gimple_try_kind (try_two
) == GIMPLE_TRY_FINALLY
)
3180 optimize_double_finally (try_one
, try_two
);
3182 switch (gimple_code (one
))
3185 refactor_eh_r (gimple_try_eval (one
));
3186 refactor_eh_r (gimple_try_cleanup (one
));
3189 refactor_eh_r (gimple_catch_handler (as_a
<gcatch
*> (one
)));
3191 case GIMPLE_EH_FILTER
:
3192 refactor_eh_r (gimple_eh_filter_failure (one
));
3194 case GIMPLE_EH_ELSE
:
3196 geh_else
*eh_else_stmt
= as_a
<geh_else
*> (one
);
3197 refactor_eh_r (gimple_eh_else_n_body (eh_else_stmt
));
3198 refactor_eh_r (gimple_eh_else_e_body (eh_else_stmt
));
3213 const pass_data pass_data_refactor_eh
=
3215 GIMPLE_PASS
, /* type */
3217 OPTGROUP_NONE
, /* optinfo_flags */
3218 TV_TREE_EH
, /* tv_id */
3219 PROP_gimple_lcf
, /* properties_required */
3220 0, /* properties_provided */
3221 0, /* properties_destroyed */
3222 0, /* todo_flags_start */
3223 0, /* todo_flags_finish */
3226 class pass_refactor_eh
: public gimple_opt_pass
3229 pass_refactor_eh (gcc::context
*ctxt
)
3230 : gimple_opt_pass (pass_data_refactor_eh
, ctxt
)
3233 /* opt_pass methods: */
3234 virtual bool gate (function
*) { return flag_exceptions
!= 0; }
3235 virtual unsigned int execute (function
*)
3237 refactor_eh_r (gimple_body (current_function_decl
));
3241 }; // class pass_refactor_eh
3246 make_pass_refactor_eh (gcc::context
*ctxt
)
3248 return new pass_refactor_eh (ctxt
);
3251 /* At the end of gimple optimization, we can lower RESX. */
3254 lower_resx (basic_block bb
, gresx
*stmt
,
3255 hash_map
<eh_region
, tree
> *mnt_map
)
3258 eh_region src_r
, dst_r
;
3259 gimple_stmt_iterator gsi
;
3264 lp_nr
= lookup_stmt_eh_lp (stmt
);
3266 dst_r
= get_eh_region_from_lp_number (lp_nr
);
3270 src_r
= get_eh_region_from_number (gimple_resx_region (stmt
));
3271 gsi
= gsi_last_bb (bb
);
3275 /* We can wind up with no source region when pass_cleanup_eh shows
3276 that there are no entries into an eh region and deletes it, but
3277 then the block that contains the resx isn't removed. This can
3278 happen without optimization when the switch statement created by
3279 lower_try_finally_switch isn't simplified to remove the eh case.
3281 Resolve this by expanding the resx node to an abort. */
3283 fn
= builtin_decl_implicit (BUILT_IN_TRAP
);
3284 x
= gimple_build_call (fn
, 0);
3285 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3287 while (EDGE_COUNT (bb
->succs
) > 0)
3288 remove_edge (EDGE_SUCC (bb
, 0));
3292 /* When we have a destination region, we resolve this by copying
3293 the excptr and filter values into place, and changing the edge
3294 to immediately after the landing pad. */
3302 /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3303 the failure decl into a new block, if needed. */
3304 gcc_assert (dst_r
->type
== ERT_MUST_NOT_THROW
);
3306 tree
*slot
= mnt_map
->get (dst_r
);
3309 gimple_stmt_iterator gsi2
;
3311 new_bb
= create_empty_bb (bb
);
3312 new_bb
->count
= bb
->count
;
3313 add_bb_to_loop (new_bb
, bb
->loop_father
);
3314 lab
= gimple_block_label (new_bb
);
3315 gsi2
= gsi_start_bb (new_bb
);
3317 fn
= dst_r
->u
.must_not_throw
.failure_decl
;
3318 x
= gimple_build_call (fn
, 0);
3319 gimple_set_location (x
, dst_r
->u
.must_not_throw
.failure_loc
);
3320 gsi_insert_after (&gsi2
, x
, GSI_CONTINUE_LINKING
);
3322 mnt_map
->put (dst_r
, lab
);
3327 new_bb
= label_to_block (cfun
, lab
);
3330 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3331 e
= make_single_succ_edge (bb
, new_bb
, EDGE_FALLTHRU
);
3336 tree dst_nr
= build_int_cst (integer_type_node
, dst_r
->index
);
3338 fn
= builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES
);
3339 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3340 x
= gimple_build_call (fn
, 2, dst_nr
, src_nr
);
3341 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3343 /* Update the flags for the outgoing edge. */
3344 e
= single_succ_edge (bb
);
3345 gcc_assert (e
->flags
& EDGE_EH
);
3346 e
->flags
= (e
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
3347 e
->probability
= profile_probability::always ();
3349 /* If there are no more EH users of the landing pad, delete it. */
3350 FOR_EACH_EDGE (e
, ei
, e
->dest
->preds
)
3351 if (e
->flags
& EDGE_EH
)
3355 eh_landing_pad lp
= get_eh_landing_pad_from_number (lp_nr
);
3356 remove_eh_landing_pad (lp
);
3366 /* When we don't have a destination region, this exception escapes
3367 up the call chain. We resolve this by generating a call to the
3368 _Unwind_Resume library function. */
3370 /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3371 with no arguments for C++. Check for that. */
3372 if (src_r
->use_cxa_end_cleanup
)
3374 fn
= builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP
);
3375 x
= gimple_build_call (fn
, 0);
3376 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3380 fn
= builtin_decl_implicit (BUILT_IN_EH_POINTER
);
3381 src_nr
= build_int_cst (integer_type_node
, src_r
->index
);
3382 x
= gimple_build_call (fn
, 1, src_nr
);
3383 var
= create_tmp_var (ptr_type_node
);
3384 var
= make_ssa_name (var
, x
);
3385 gimple_call_set_lhs (x
, var
);
3386 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3388 /* When exception handling is delegated to a caller function, we
3389 have to guarantee that shadow memory variables living on stack
3390 will be cleaner before control is given to a parent function. */
3391 if (sanitize_flags_p (SANITIZE_ADDRESS
))
3394 = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN
);
3395 gimple
*g
= gimple_build_call (decl
, 0);
3396 gimple_set_location (g
, gimple_location (stmt
));
3397 gsi_insert_before (&gsi
, g
, GSI_SAME_STMT
);
3400 fn
= builtin_decl_implicit (BUILT_IN_UNWIND_RESUME
);
3401 x
= gimple_build_call (fn
, 1, var
);
3402 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3405 gcc_assert (EDGE_COUNT (bb
->succs
) == 0);
3408 gsi_remove (&gsi
, true);
3415 const pass_data pass_data_lower_resx
=
3417 GIMPLE_PASS
, /* type */
3419 OPTGROUP_NONE
, /* optinfo_flags */
3420 TV_TREE_EH
, /* tv_id */
3421 PROP_gimple_lcf
, /* properties_required */
3422 0, /* properties_provided */
3423 0, /* properties_destroyed */
3424 0, /* todo_flags_start */
3425 0, /* todo_flags_finish */
3428 class pass_lower_resx
: public gimple_opt_pass
3431 pass_lower_resx (gcc::context
*ctxt
)
3432 : gimple_opt_pass (pass_data_lower_resx
, ctxt
)
3435 /* opt_pass methods: */
3436 virtual bool gate (function
*) { return flag_exceptions
!= 0; }
3437 virtual unsigned int execute (function
*);
3439 }; // class pass_lower_resx
3442 pass_lower_resx::execute (function
*fun
)
3445 bool dominance_invalidated
= false;
3446 bool any_rewritten
= false;
3448 hash_map
<eh_region
, tree
> mnt_map
;
3450 FOR_EACH_BB_FN (bb
, fun
)
3452 gimple
*last
= last_stmt (bb
);
3453 if (last
&& is_gimple_resx (last
))
3455 dominance_invalidated
|=
3456 lower_resx (bb
, as_a
<gresx
*> (last
), &mnt_map
);
3457 any_rewritten
= true;
3461 if (dominance_invalidated
)
3463 free_dominance_info (CDI_DOMINATORS
);
3464 free_dominance_info (CDI_POST_DOMINATORS
);
3467 return any_rewritten
? TODO_update_ssa_only_virtuals
: 0;
3473 make_pass_lower_resx (gcc::context
*ctxt
)
3475 return new pass_lower_resx (ctxt
);
3478 /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3482 optimize_clobbers (basic_block bb
)
3484 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
3485 bool any_clobbers
= false;
3486 bool seen_stack_restore
= false;
3490 /* Only optimize anything if the bb contains at least one clobber,
3491 ends with resx (checked by caller), optionally contains some
3492 debug stmts or labels, or at most one __builtin_stack_restore
3493 call, and has an incoming EH edge. */
3494 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3496 gimple
*stmt
= gsi_stmt (gsi
);
3497 if (is_gimple_debug (stmt
))
3499 if (gimple_clobber_p (stmt
))
3501 any_clobbers
= true;
3504 if (!seen_stack_restore
3505 && gimple_call_builtin_p (stmt
, BUILT_IN_STACK_RESTORE
))
3507 seen_stack_restore
= true;
3510 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3516 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3517 if (e
->flags
& EDGE_EH
)
3521 gsi
= gsi_last_bb (bb
);
3522 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3524 gimple
*stmt
= gsi_stmt (gsi
);
3525 if (!gimple_clobber_p (stmt
))
3527 unlink_stmt_vdef (stmt
);
3528 gsi_remove (&gsi
, true);
3529 release_defs (stmt
);
3533 /* Try to sink var = {v} {CLOBBER} stmts followed just by
3534 internal throw to successor BB. */
3537 sink_clobbers (basic_block bb
)
3541 gimple_stmt_iterator gsi
, dgsi
;
3543 bool any_clobbers
= false;
3546 /* Only optimize if BB has a single EH successor and
3547 all predecessor edges are EH too. */
3548 if (!single_succ_p (bb
)
3549 || (single_succ_edge (bb
)->flags
& EDGE_EH
) == 0)
3552 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3554 if ((e
->flags
& EDGE_EH
) == 0)
3558 /* And BB contains only CLOBBER stmts before the final
3560 gsi
= gsi_last_bb (bb
);
3561 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3563 gimple
*stmt
= gsi_stmt (gsi
);
3564 if (is_gimple_debug (stmt
))
3566 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3568 if (!gimple_clobber_p (stmt
))
3570 any_clobbers
= true;
3575 edge succe
= single_succ_edge (bb
);
3576 succbb
= succe
->dest
;
3578 /* See if there is a virtual PHI node to take an updated virtual
3581 tree vuse
= NULL_TREE
;
3582 for (gphi_iterator gpi
= gsi_start_phis (succbb
);
3583 !gsi_end_p (gpi
); gsi_next (&gpi
))
3585 tree res
= gimple_phi_result (gpi
.phi ());
3586 if (virtual_operand_p (res
))
3594 dgsi
= gsi_after_labels (succbb
);
3595 gsi
= gsi_last_bb (bb
);
3596 for (gsi_prev (&gsi
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
3598 gimple
*stmt
= gsi_stmt (gsi
);
3600 if (is_gimple_debug (stmt
))
3602 if (gimple_code (stmt
) == GIMPLE_LABEL
)
3604 lhs
= gimple_assign_lhs (stmt
);
3605 /* Unfortunately we don't have dominance info updated at this
3606 point, so checking if
3607 dominated_by_p (CDI_DOMINATORS, succbb,
3608 gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0)))
3609 would be too costly. Thus, avoid sinking any clobbers that
3610 refer to non-(D) SSA_NAMEs. */
3611 if (TREE_CODE (lhs
) == MEM_REF
3612 && TREE_CODE (TREE_OPERAND (lhs
, 0)) == SSA_NAME
3613 && !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs
, 0)))
3615 unlink_stmt_vdef (stmt
);
3616 gsi_remove (&gsi
, true);
3617 release_defs (stmt
);
3621 /* As we do not change stmt order when sinking across a
3622 forwarder edge we can keep virtual operands in place. */
3623 gsi_remove (&gsi
, false);
3624 gsi_insert_before (&dgsi
, stmt
, GSI_NEW_STMT
);
3626 /* But adjust virtual operands if we sunk across a PHI node. */
3630 imm_use_iterator iter
;
3631 use_operand_p use_p
;
3632 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, vuse
)
3633 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3634 SET_USE (use_p
, gimple_vdef (stmt
));
3635 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse
))
3637 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (stmt
)) = 1;
3638 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse
) = 0;
3640 /* Adjust the incoming virtual operand. */
3641 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (vphi
, succe
), gimple_vuse (stmt
));
3642 SET_USE (gimple_vuse_op (stmt
), vuse
);
3644 /* If there isn't a single predecessor but no virtual PHI node
3645 arrange for virtual operands to be renamed. */
3646 else if (gimple_vuse_op (stmt
) != NULL_USE_OPERAND_P
3647 && !single_pred_p (succbb
))
3649 /* In this case there will be no use of the VDEF of this stmt.
3650 ??? Unless this is a secondary opportunity and we have not
3651 removed unreachable blocks yet, so we cannot assert this.
3652 Which also means we will end up renaming too many times. */
3653 SET_USE (gimple_vuse_op (stmt
), gimple_vop (cfun
));
3654 mark_virtual_operands_for_renaming (cfun
);
3655 todo
|= TODO_update_ssa_only_virtuals
;
3662 /* At the end of inlining, we can lower EH_DISPATCH. Return true when
3663 we have found some duplicate labels and removed some edges. */
3666 lower_eh_dispatch (basic_block src
, geh_dispatch
*stmt
)
3668 gimple_stmt_iterator gsi
;
3673 bool redirected
= false;
3675 region_nr
= gimple_eh_dispatch_region (stmt
);
3676 r
= get_eh_region_from_number (region_nr
);
3678 gsi
= gsi_last_bb (src
);
3684 auto_vec
<tree
> labels
;
3685 tree default_label
= NULL
;
3689 hash_set
<tree
> seen_values
;
3691 /* Collect the labels for a switch. Zero the post_landing_pad
3692 field becase we'll no longer have anything keeping these labels
3693 in existence and the optimizer will be free to merge these
3695 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
3697 tree tp_node
, flt_node
, lab
= c
->label
;
3698 bool have_label
= false;
3701 tp_node
= c
->type_list
;
3702 flt_node
= c
->filter_list
;
3704 if (tp_node
== NULL
)
3706 default_label
= lab
;
3711 /* Filter out duplicate labels that arise when this handler
3712 is shadowed by an earlier one. When no labels are
3713 attached to the handler anymore, we remove
3714 the corresponding edge and then we delete unreachable
3715 blocks at the end of this pass. */
3716 if (! seen_values
.contains (TREE_VALUE (flt_node
)))
3718 tree t
= build_case_label (TREE_VALUE (flt_node
),
3720 labels
.safe_push (t
);
3721 seen_values
.add (TREE_VALUE (flt_node
));
3725 tp_node
= TREE_CHAIN (tp_node
);
3726 flt_node
= TREE_CHAIN (flt_node
);
3731 remove_edge (find_edge (src
, label_to_block (cfun
, lab
)));
3736 /* Clean up the edge flags. */
3737 FOR_EACH_EDGE (e
, ei
, src
->succs
)
3739 if (e
->flags
& EDGE_FALLTHRU
)
3741 /* If there was no catch-all, use the fallthru edge. */
3742 if (default_label
== NULL
)
3743 default_label
= gimple_block_label (e
->dest
);
3744 e
->flags
&= ~EDGE_FALLTHRU
;
3747 gcc_assert (default_label
!= NULL
);
3749 /* Don't generate a switch if there's only a default case.
3750 This is common in the form of try { A; } catch (...) { B; }. */
3751 if (!labels
.exists ())
3753 e
= single_succ_edge (src
);
3754 e
->flags
|= EDGE_FALLTHRU
;
3758 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3759 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3761 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)));
3762 filter
= make_ssa_name (filter
, x
);
3763 gimple_call_set_lhs (x
, filter
);
3764 gimple_set_location (x
, gimple_location (stmt
));
3765 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3767 /* Turn the default label into a default case. */
3768 default_label
= build_case_label (NULL
, NULL
, default_label
);
3769 sort_case_labels (labels
);
3771 x
= gimple_build_switch (filter
, default_label
, labels
);
3772 gimple_set_location (x
, gimple_location (stmt
));
3773 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3778 case ERT_ALLOWED_EXCEPTIONS
:
3780 edge b_e
= BRANCH_EDGE (src
);
3781 edge f_e
= FALLTHRU_EDGE (src
);
3783 fn
= builtin_decl_implicit (BUILT_IN_EH_FILTER
);
3784 x
= gimple_build_call (fn
, 1, build_int_cst (integer_type_node
,
3786 filter
= create_tmp_var (TREE_TYPE (TREE_TYPE (fn
)));
3787 filter
= make_ssa_name (filter
, x
);
3788 gimple_call_set_lhs (x
, filter
);
3789 gimple_set_location (x
, gimple_location (stmt
));
3790 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3792 r
->u
.allowed
.label
= NULL
;
3793 x
= gimple_build_cond (EQ_EXPR
, filter
,
3794 build_int_cst (TREE_TYPE (filter
),
3795 r
->u
.allowed
.filter
),
3796 NULL_TREE
, NULL_TREE
);
3797 gsi_insert_before (&gsi
, x
, GSI_SAME_STMT
);
3799 b_e
->flags
= b_e
->flags
| EDGE_TRUE_VALUE
;
3800 f_e
->flags
= (f_e
->flags
& ~EDGE_FALLTHRU
) | EDGE_FALSE_VALUE
;
3808 /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3809 gsi_remove (&gsi
, true);
3815 const pass_data pass_data_lower_eh_dispatch
=
3817 GIMPLE_PASS
, /* type */
3818 "ehdisp", /* name */
3819 OPTGROUP_NONE
, /* optinfo_flags */
3820 TV_TREE_EH
, /* tv_id */
3821 PROP_gimple_lcf
, /* properties_required */
3822 0, /* properties_provided */
3823 0, /* properties_destroyed */
3824 0, /* todo_flags_start */
3825 0, /* todo_flags_finish */
3828 class pass_lower_eh_dispatch
: public gimple_opt_pass
3831 pass_lower_eh_dispatch (gcc::context
*ctxt
)
3832 : gimple_opt_pass (pass_data_lower_eh_dispatch
, ctxt
)
3835 /* opt_pass methods: */
3836 virtual bool gate (function
*fun
) { return fun
->eh
->region_tree
!= NULL
; }
3837 virtual unsigned int execute (function
*);
3839 }; // class pass_lower_eh_dispatch
3842 pass_lower_eh_dispatch::execute (function
*fun
)
3846 bool redirected
= false;
3848 assign_filter_values ();
3850 FOR_EACH_BB_FN (bb
, fun
)
3852 gimple
*last
= last_stmt (bb
);
3855 if (gimple_code (last
) == GIMPLE_EH_DISPATCH
)
3857 redirected
|= lower_eh_dispatch (bb
,
3858 as_a
<geh_dispatch
*> (last
));
3859 flags
|= TODO_update_ssa_only_virtuals
;
3861 else if (gimple_code (last
) == GIMPLE_RESX
)
3863 if (stmt_can_throw_external (cfun
, last
))
3864 optimize_clobbers (bb
);
3866 flags
|= sink_clobbers (bb
);
3872 free_dominance_info (CDI_DOMINATORS
);
3873 delete_unreachable_blocks ();
3881 make_pass_lower_eh_dispatch (gcc::context
*ctxt
)
3883 return new pass_lower_eh_dispatch (ctxt
);
3886 /* Walk statements, see what regions and, optionally, landing pads
3887 are really referenced.
3889 Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
3890 and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
3892 Passing NULL for LP_REACHABLE is valid, in this case only reachable
3895 The caller is responsible for freeing the returned sbitmaps. */
3898 mark_reachable_handlers (sbitmap
*r_reachablep
, sbitmap
*lp_reachablep
)
3900 sbitmap r_reachable
, lp_reachable
;
3902 bool mark_landing_pads
= (lp_reachablep
!= NULL
);
3903 gcc_checking_assert (r_reachablep
!= NULL
);
3905 r_reachable
= sbitmap_alloc (cfun
->eh
->region_array
->length ());
3906 bitmap_clear (r_reachable
);
3907 *r_reachablep
= r_reachable
;
3909 if (mark_landing_pads
)
3911 lp_reachable
= sbitmap_alloc (cfun
->eh
->lp_array
->length ());
3912 bitmap_clear (lp_reachable
);
3913 *lp_reachablep
= lp_reachable
;
3916 lp_reachable
= NULL
;
3918 FOR_EACH_BB_FN (bb
, cfun
)
3920 gimple_stmt_iterator gsi
;
3922 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3924 gimple
*stmt
= gsi_stmt (gsi
);
3926 if (mark_landing_pads
)
3928 int lp_nr
= lookup_stmt_eh_lp (stmt
);
3930 /* Negative LP numbers are MUST_NOT_THROW regions which
3931 are not considered BB enders. */
3933 bitmap_set_bit (r_reachable
, -lp_nr
);
3935 /* Positive LP numbers are real landing pads, and BB enders. */
3938 gcc_assert (gsi_one_before_end_p (gsi
));
3939 eh_region region
= get_eh_region_from_lp_number (lp_nr
);
3940 bitmap_set_bit (r_reachable
, region
->index
);
3941 bitmap_set_bit (lp_reachable
, lp_nr
);
3945 /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
3946 switch (gimple_code (stmt
))
3949 bitmap_set_bit (r_reachable
,
3950 gimple_resx_region (as_a
<gresx
*> (stmt
)));
3952 case GIMPLE_EH_DISPATCH
:
3953 bitmap_set_bit (r_reachable
,
3954 gimple_eh_dispatch_region (
3955 as_a
<geh_dispatch
*> (stmt
)));
3958 if (gimple_call_builtin_p (stmt
, BUILT_IN_EH_COPY_VALUES
))
3959 for (int i
= 0; i
< 2; ++i
)
3961 tree rt
= gimple_call_arg (stmt
, i
);
3962 HOST_WIDE_INT ri
= tree_to_shwi (rt
);
3964 gcc_assert (ri
== (int)ri
);
3965 bitmap_set_bit (r_reachable
, ri
);
3975 /* Remove unreachable handlers and unreachable landing pads. */
3978 remove_unreachable_handlers (void)
3980 sbitmap r_reachable
, lp_reachable
;
3985 mark_reachable_handlers (&r_reachable
, &lp_reachable
);
3989 fprintf (dump_file
, "Before removal of unreachable regions:\n");
3990 dump_eh_tree (dump_file
, cfun
);
3991 fprintf (dump_file
, "Reachable regions: ");
3992 dump_bitmap_file (dump_file
, r_reachable
);
3993 fprintf (dump_file
, "Reachable landing pads: ");
3994 dump_bitmap_file (dump_file
, lp_reachable
);
3999 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
4000 if (region
&& !bitmap_bit_p (r_reachable
, region
->index
))
4002 "Removing unreachable region %d\n",
4006 remove_unreachable_eh_regions (r_reachable
);
4008 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
4009 if (lp
&& !bitmap_bit_p (lp_reachable
, lp
->index
))
4013 "Removing unreachable landing pad %d\n",
4015 remove_eh_landing_pad (lp
);
4020 fprintf (dump_file
, "\n\nAfter removal of unreachable regions:\n");
4021 dump_eh_tree (dump_file
, cfun
);
4022 fprintf (dump_file
, "\n\n");
4025 sbitmap_free (r_reachable
);
4026 sbitmap_free (lp_reachable
);
4029 verify_eh_tree (cfun
);
4032 /* Remove unreachable handlers if any landing pads have been removed after
4033 last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
4036 maybe_remove_unreachable_handlers (void)
4041 if (cfun
->eh
== NULL
)
4044 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->lp_array
, i
, lp
)
4045 if (lp
&& lp
->post_landing_pad
)
4047 if (label_to_block (cfun
, lp
->post_landing_pad
) == NULL
)
4049 remove_unreachable_handlers ();
4055 /* Remove regions that do not have landing pads. This assumes
4056 that remove_unreachable_handlers has already been run, and
4057 that we've just manipulated the landing pads since then.
4059 Preserve regions with landing pads and regions that prevent
4060 exceptions from propagating further, even if these regions
4061 are not reachable. */
4064 remove_unreachable_handlers_no_lp (void)
4067 sbitmap r_reachable
;
4070 mark_reachable_handlers (&r_reachable
, /*lp_reachablep=*/NULL
);
4072 FOR_EACH_VEC_SAFE_ELT (cfun
->eh
->region_array
, i
, region
)
4077 if (region
->landing_pads
!= NULL
4078 || region
->type
== ERT_MUST_NOT_THROW
)
4079 bitmap_set_bit (r_reachable
, region
->index
);
4082 && !bitmap_bit_p (r_reachable
, region
->index
))
4084 "Removing unreachable region %d\n",
4088 remove_unreachable_eh_regions (r_reachable
);
4090 sbitmap_free (r_reachable
);
4093 /* Undo critical edge splitting on an EH landing pad. Earlier, we
4094 optimisticaly split all sorts of edges, including EH edges. The
4095 optimization passes in between may not have needed them; if not,
4096 we should undo the split.
4098 Recognize this case by having one EH edge incoming to the BB and
4099 one normal edge outgoing; BB should be empty apart from the
4100 post_landing_pad label.
4102 Note that this is slightly different from the empty handler case
4103 handled by cleanup_empty_eh, in that the actual handler may yet
4104 have actual code but the landing pad has been separated from the
4105 handler. As such, cleanup_empty_eh relies on this transformation
4106 having been done first. */
4109 unsplit_eh (eh_landing_pad lp
)
4111 basic_block bb
= label_to_block (cfun
, lp
->post_landing_pad
);
4112 gimple_stmt_iterator gsi
;
4115 /* Quickly check the edge counts on BB for singularity. */
4116 if (!single_pred_p (bb
) || !single_succ_p (bb
))
4118 e_in
= single_pred_edge (bb
);
4119 e_out
= single_succ_edge (bb
);
4121 /* Input edge must be EH and output edge must be normal. */
4122 if ((e_in
->flags
& EDGE_EH
) == 0 || (e_out
->flags
& EDGE_EH
) != 0)
4125 /* The block must be empty except for the labels and debug insns. */
4126 gsi
= gsi_after_labels (bb
);
4127 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4128 gsi_next_nondebug (&gsi
);
4129 if (!gsi_end_p (gsi
))
4132 /* The destination block must not already have a landing pad
4133 for a different region. */
4134 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4136 glabel
*label_stmt
= dyn_cast
<glabel
*> (gsi_stmt (gsi
));
4142 lab
= gimple_label_label (label_stmt
);
4143 lp_nr
= EH_LANDING_PAD_NR (lab
);
4144 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4148 /* The new destination block must not already be a destination of
4149 the source block, lest we merge fallthru and eh edges and get
4150 all sorts of confused. */
4151 if (find_edge (e_in
->src
, e_out
->dest
))
4154 /* ??? We can get degenerate phis due to cfg cleanups. I would have
4155 thought this should have been cleaned up by a phicprop pass, but
4156 that doesn't appear to handle virtuals. Propagate by hand. */
4157 if (!gimple_seq_empty_p (phi_nodes (bb
)))
4159 for (gphi_iterator gpi
= gsi_start_phis (bb
); !gsi_end_p (gpi
); )
4162 gphi
*phi
= gpi
.phi ();
4163 tree lhs
= gimple_phi_result (phi
);
4164 tree rhs
= gimple_phi_arg_def (phi
, 0);
4165 use_operand_p use_p
;
4166 imm_use_iterator iter
;
4168 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
4170 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
4171 SET_USE (use_p
, rhs
);
4174 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
4175 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs
) = 1;
4177 remove_phi_node (&gpi
, true);
4181 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4182 fprintf (dump_file
, "Unsplit EH landing pad %d to block %i.\n",
4183 lp
->index
, e_out
->dest
->index
);
4185 /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
4186 a successor edge, humor it. But do the real CFG change with the
4187 predecessor of E_OUT in order to preserve the ordering of arguments
4188 to the PHI nodes in E_OUT->DEST. */
4189 redirect_eh_edge_1 (e_in
, e_out
->dest
, false);
4190 redirect_edge_pred (e_out
, e_in
->src
);
4191 e_out
->flags
= e_in
->flags
;
4192 e_out
->probability
= e_in
->probability
;
4198 /* Examine each landing pad block and see if it matches unsplit_eh. */
4201 unsplit_all_eh (void)
4203 bool changed
= false;
4207 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4209 changed
|= unsplit_eh (lp
);
4214 /* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
4215 to OLD_BB to NEW_BB; return true on success, false on failure.
4217 OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
4218 PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
4219 Virtual PHIs may be deleted and marked for renaming. */
4222 cleanup_empty_eh_merge_phis (basic_block new_bb
, basic_block old_bb
,
4223 edge old_bb_out
, bool change_region
)
4225 gphi_iterator ngsi
, ogsi
;
4228 bitmap ophi_handled
;
4230 /* The destination block must not be a regular successor for any
4231 of the preds of the landing pad. Thus, avoid turning
4241 which CFG verification would choke on. See PR45172 and PR51089. */
4242 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4243 if (find_edge (e
->src
, new_bb
))
4246 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4247 redirect_edge_var_map_clear (e
);
4249 ophi_handled
= BITMAP_ALLOC (NULL
);
4251 /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
4252 for the edges we're going to move. */
4253 for (ngsi
= gsi_start_phis (new_bb
); !gsi_end_p (ngsi
); gsi_next (&ngsi
))
4255 gphi
*ophi
, *nphi
= ngsi
.phi ();
4258 nresult
= gimple_phi_result (nphi
);
4259 nop
= gimple_phi_arg_def (nphi
, old_bb_out
->dest_idx
);
4261 /* Find the corresponding PHI in OLD_BB so we can forward-propagate
4262 the source ssa_name. */
4264 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4267 if (gimple_phi_result (ophi
) == nop
)
4272 /* If we did find the corresponding PHI, copy those inputs. */
4275 /* If NOP is used somewhere else beyond phis in new_bb, give up. */
4276 if (!has_single_use (nop
))
4278 imm_use_iterator imm_iter
;
4279 use_operand_p use_p
;
4281 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, nop
)
4283 if (!gimple_debug_bind_p (USE_STMT (use_p
))
4284 && (gimple_code (USE_STMT (use_p
)) != GIMPLE_PHI
4285 || gimple_bb (USE_STMT (use_p
)) != new_bb
))
4289 bitmap_set_bit (ophi_handled
, SSA_NAME_VERSION (nop
));
4290 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4295 if ((e
->flags
& EDGE_EH
) == 0)
4297 oop
= gimple_phi_arg_def (ophi
, e
->dest_idx
);
4298 oloc
= gimple_phi_arg_location (ophi
, e
->dest_idx
);
4299 redirect_edge_var_map_add (e
, nresult
, oop
, oloc
);
4302 /* If we didn't find the PHI, if it's a real variable or a VOP, we know
4303 from the fact that OLD_BB is tree_empty_eh_handler_p that the
4304 variable is unchanged from input to the block and we can simply
4305 re-use the input to NEW_BB from the OLD_BB_OUT edge. */
4309 = gimple_phi_arg_location (nphi
, old_bb_out
->dest_idx
);
4310 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4311 redirect_edge_var_map_add (e
, nresult
, nop
, nloc
);
4315 /* Second, verify that all PHIs from OLD_BB have been handled. If not,
4316 we don't know what values from the other edges into NEW_BB to use. */
4317 for (ogsi
= gsi_start_phis (old_bb
); !gsi_end_p (ogsi
); gsi_next (&ogsi
))
4319 gphi
*ophi
= ogsi
.phi ();
4320 tree oresult
= gimple_phi_result (ophi
);
4321 if (!bitmap_bit_p (ophi_handled
, SSA_NAME_VERSION (oresult
)))
4325 /* Finally, move the edges and update the PHIs. */
4326 for (ei
= ei_start (old_bb
->preds
); (e
= ei_safe_edge (ei
)); )
4327 if (e
->flags
& EDGE_EH
)
4329 /* ??? CFG manipluation routines do not try to update loop
4330 form on edge redirection. Do so manually here for now. */
4331 /* If we redirect a loop entry or latch edge that will either create
4332 a multiple entry loop or rotate the loop. If the loops merge
4333 we may have created a loop with multiple latches.
4334 All of this isn't easily fixed thus cancel the affected loop
4335 and mark the other loop as possibly having multiple latches. */
4336 if (e
->dest
== e
->dest
->loop_father
->header
)
4338 mark_loop_for_removal (e
->dest
->loop_father
);
4339 new_bb
->loop_father
->latch
= NULL
;
4340 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES
);
4342 redirect_eh_edge_1 (e
, new_bb
, change_region
);
4343 redirect_edge_succ (e
, new_bb
);
4344 flush_pending_stmts (e
);
4349 BITMAP_FREE (ophi_handled
);
4353 FOR_EACH_EDGE (e
, ei
, old_bb
->preds
)
4354 redirect_edge_var_map_clear (e
);
4355 BITMAP_FREE (ophi_handled
);
4359 /* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4360 old region to NEW_REGION at BB. */
4363 cleanup_empty_eh_move_lp (basic_block bb
, edge e_out
,
4364 eh_landing_pad lp
, eh_region new_region
)
4366 gimple_stmt_iterator gsi
;
4369 for (pp
= &lp
->region
->landing_pads
; *pp
!= lp
; pp
= &(*pp
)->next_lp
)
4373 lp
->region
= new_region
;
4374 lp
->next_lp
= new_region
->landing_pads
;
4375 new_region
->landing_pads
= lp
;
4377 /* Delete the RESX that was matched within the empty handler block. */
4378 gsi
= gsi_last_bb (bb
);
4379 unlink_stmt_vdef (gsi_stmt (gsi
));
4380 gsi_remove (&gsi
, true);
4382 /* Clean up E_OUT for the fallthru. */
4383 e_out
->flags
= (e_out
->flags
& ~EDGE_EH
) | EDGE_FALLTHRU
;
4384 e_out
->probability
= profile_probability::always ();
4387 /* A subroutine of cleanup_empty_eh. Handle more complex cases of
4388 unsplitting than unsplit_eh was prepared to handle, e.g. when
4389 multiple incoming edges and phis are involved. */
4392 cleanup_empty_eh_unsplit (basic_block bb
, edge e_out
, eh_landing_pad lp
)
4394 gimple_stmt_iterator gsi
;
4397 /* We really ought not have totally lost everything following
4398 a landing pad label. Given that BB is empty, there had better
4400 gcc_assert (e_out
!= NULL
);
4402 /* The destination block must not already have a landing pad
4403 for a different region. */
4405 for (gsi
= gsi_start_bb (e_out
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4407 glabel
*stmt
= dyn_cast
<glabel
*> (gsi_stmt (gsi
));
4412 lab
= gimple_label_label (stmt
);
4413 lp_nr
= EH_LANDING_PAD_NR (lab
);
4414 if (lp_nr
&& get_eh_region_from_lp_number (lp_nr
) != lp
->region
)
4418 /* Attempt to move the PHIs into the successor block. */
4419 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, false))
4421 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4423 "Unsplit EH landing pad %d to block %i "
4424 "(via cleanup_empty_eh).\n",
4425 lp
->index
, e_out
->dest
->index
);
4432 /* Return true if edge E_FIRST is part of an empty infinite loop
4433 or leads to such a loop through a series of single successor
4437 infinite_empty_loop_p (edge e_first
)
4439 bool inf_loop
= false;
4442 if (e_first
->dest
== e_first
->src
)
4445 e_first
->src
->aux
= (void *) 1;
4446 for (e
= e_first
; single_succ_p (e
->dest
); e
= single_succ_edge (e
->dest
))
4448 gimple_stmt_iterator gsi
;
4454 e
->dest
->aux
= (void *) 1;
4455 gsi
= gsi_after_labels (e
->dest
);
4456 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4457 gsi_next_nondebug (&gsi
);
4458 if (!gsi_end_p (gsi
))
4461 e_first
->src
->aux
= NULL
;
4462 for (e
= e_first
; e
->dest
->aux
; e
= single_succ_edge (e
->dest
))
4463 e
->dest
->aux
= NULL
;
4468 /* Examine the block associated with LP to determine if it's an empty
4469 handler for its EH region. If so, attempt to redirect EH edges to
4470 an outer region. Return true the CFG was updated in any way. This
4471 is similar to jump forwarding, just across EH edges. */
4474 cleanup_empty_eh (eh_landing_pad lp
)
4476 basic_block bb
= label_to_block (cfun
, lp
->post_landing_pad
);
4477 gimple_stmt_iterator gsi
;
4479 eh_region new_region
;
4482 bool has_non_eh_pred
;
4486 /* There can be zero or one edges out of BB. This is the quickest test. */
4487 switch (EDGE_COUNT (bb
->succs
))
4493 e_out
= single_succ_edge (bb
);
4499 gsi
= gsi_last_nondebug_bb (bb
);
4500 resx
= gsi_stmt (gsi
);
4501 if (resx
&& is_gimple_resx (resx
))
4503 if (stmt_can_throw_external (cfun
, resx
))
4504 optimize_clobbers (bb
);
4505 else if (sink_clobbers (bb
))
4509 gsi
= gsi_after_labels (bb
);
4511 /* Make sure to skip debug statements. */
4512 if (!gsi_end_p (gsi
) && is_gimple_debug (gsi_stmt (gsi
)))
4513 gsi_next_nondebug (&gsi
);
4515 /* If the block is totally empty, look for more unsplitting cases. */
4516 if (gsi_end_p (gsi
))
4518 /* For the degenerate case of an infinite loop bail out.
4519 If bb has no successors and is totally empty, which can happen e.g.
4520 because of incorrect noreturn attribute, bail out too. */
4522 || infinite_empty_loop_p (e_out
))
4525 return ret
| cleanup_empty_eh_unsplit (bb
, e_out
, lp
);
4528 /* The block should consist only of a single RESX statement, modulo a
4529 preceding call to __builtin_stack_restore if there is no outgoing
4530 edge, since the call can be eliminated in this case. */
4531 resx
= gsi_stmt (gsi
);
4532 if (!e_out
&& gimple_call_builtin_p (resx
, BUILT_IN_STACK_RESTORE
))
4534 gsi_next_nondebug (&gsi
);
4535 resx
= gsi_stmt (gsi
);
4537 if (!is_gimple_resx (resx
))
4539 gcc_assert (gsi_one_nondebug_before_end_p (gsi
));
4541 /* Determine if there are non-EH edges, or resx edges into the handler. */
4542 has_non_eh_pred
= false;
4543 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
4544 if (!(e
->flags
& EDGE_EH
))
4545 has_non_eh_pred
= true;
4547 /* Find the handler that's outer of the empty handler by looking at
4548 where the RESX instruction was vectored. */
4549 new_lp_nr
= lookup_stmt_eh_lp (resx
);
4550 new_region
= get_eh_region_from_lp_number (new_lp_nr
);
4552 /* If there's no destination region within the current function,
4553 redirection is trivial via removing the throwing statements from
4554 the EH region, removing the EH edges, and allowing the block
4555 to go unreachable. */
4556 if (new_region
== NULL
)
4558 gcc_assert (e_out
== NULL
);
4559 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4560 if (e
->flags
& EDGE_EH
)
4562 gimple
*stmt
= last_stmt (e
->src
);
4563 remove_stmt_from_eh_lp (stmt
);
4571 /* If the destination region is a MUST_NOT_THROW, allow the runtime
4572 to handle the abort and allow the blocks to go unreachable. */
4573 if (new_region
->type
== ERT_MUST_NOT_THROW
)
4575 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
4576 if (e
->flags
& EDGE_EH
)
4578 gimple
*stmt
= last_stmt (e
->src
);
4579 remove_stmt_from_eh_lp (stmt
);
4580 add_stmt_to_eh_lp (stmt
, new_lp_nr
);
4588 /* Try to redirect the EH edges and merge the PHIs into the destination
4589 landing pad block. If the merge succeeds, we'll already have redirected
4590 all the EH edges. The handler itself will go unreachable if there were
4592 if (cleanup_empty_eh_merge_phis (e_out
->dest
, bb
, e_out
, true))
4595 /* Finally, if all input edges are EH edges, then we can (potentially)
4596 reduce the number of transfers from the runtime by moving the landing
4597 pad from the original region to the new region. This is a win when
4598 we remove the last CLEANUP region along a particular exception
4599 propagation path. Since nothing changes except for the region with
4600 which the landing pad is associated, the PHI nodes do not need to be
4602 if (!has_non_eh_pred
)
4604 cleanup_empty_eh_move_lp (bb
, e_out
, lp
, new_region
);
4605 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4606 fprintf (dump_file
, "Empty EH handler %i moved to EH region %i.\n",
4607 lp
->index
, new_region
->index
);
4609 /* ??? The CFG didn't change, but we may have rendered the
4610 old EH region unreachable. Trigger a cleanup there. */
4617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4618 fprintf (dump_file
, "Empty EH handler %i removed.\n", lp
->index
);
4619 remove_eh_landing_pad (lp
);
4623 /* Do a post-order traversal of the EH region tree. Examine each
4624 post_landing_pad block and see if we can eliminate it as empty. */
4627 cleanup_all_empty_eh (void)
4629 bool changed
= false;
4633 for (i
= 1; vec_safe_iterate (cfun
->eh
->lp_array
, i
, &lp
); ++i
)
4635 changed
|= cleanup_empty_eh (lp
);
4640 /* Perform cleanups and lowering of exception handling
4641 1) cleanups regions with handlers doing nothing are optimized out
4642 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4643 3) Info about regions that are containing instructions, and regions
4644 reachable via local EH edges is collected
4645 4) Eh tree is pruned for regions no longer necessary.
4647 TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4648 Unify those that have the same failure decl and locus.
4652 execute_cleanup_eh_1 (void)
4654 /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4655 looking up unreachable landing pads. */
4656 remove_unreachable_handlers ();
4658 /* Watch out for the region tree vanishing due to all unreachable. */
4659 if (cfun
->eh
->region_tree
)
4661 bool changed
= false;
4664 changed
|= unsplit_all_eh ();
4665 changed
|= cleanup_all_empty_eh ();
4669 free_dominance_info (CDI_DOMINATORS
);
4670 free_dominance_info (CDI_POST_DOMINATORS
);
4672 /* We delayed all basic block deletion, as we may have performed
4673 cleanups on EH edges while non-EH edges were still present. */
4674 delete_unreachable_blocks ();
4676 /* We manipulated the landing pads. Remove any region that no
4677 longer has a landing pad. */
4678 remove_unreachable_handlers_no_lp ();
4680 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
4689 const pass_data pass_data_cleanup_eh
=
4691 GIMPLE_PASS
, /* type */
4692 "ehcleanup", /* name */
4693 OPTGROUP_NONE
, /* optinfo_flags */
4694 TV_TREE_EH
, /* tv_id */
4695 PROP_gimple_lcf
, /* properties_required */
4696 0, /* properties_provided */
4697 0, /* properties_destroyed */
4698 0, /* todo_flags_start */
4699 0, /* todo_flags_finish */
4702 class pass_cleanup_eh
: public gimple_opt_pass
4705 pass_cleanup_eh (gcc::context
*ctxt
)
4706 : gimple_opt_pass (pass_data_cleanup_eh
, ctxt
)
4709 /* opt_pass methods: */
4710 opt_pass
* clone () { return new pass_cleanup_eh (m_ctxt
); }
4711 virtual bool gate (function
*fun
)
4713 return fun
->eh
!= NULL
&& fun
->eh
->region_tree
!= NULL
;
4716 virtual unsigned int execute (function
*);
4718 }; // class pass_cleanup_eh
4721 pass_cleanup_eh::execute (function
*fun
)
4723 int ret
= execute_cleanup_eh_1 ();
4725 /* If the function no longer needs an EH personality routine
4726 clear it. This exposes cross-language inlining opportunities
4727 and avoids references to a never defined personality routine. */
4728 if (DECL_FUNCTION_PERSONALITY (current_function_decl
)
4729 && function_needs_eh_personality (fun
) != eh_personality_lang
)
4730 DECL_FUNCTION_PERSONALITY (current_function_decl
) = NULL_TREE
;
4738 make_pass_cleanup_eh (gcc::context
*ctxt
)
4740 return new pass_cleanup_eh (ctxt
);
4743 /* Disable warnings about missing quoting in GCC diagnostics for
4744 the verification errors. Their format strings don't follow GCC
4745 diagnostic conventions but are only used for debugging. */
4747 # pragma GCC diagnostic push
4748 # pragma GCC diagnostic ignored "-Wformat-diag"
4751 /* Verify that BB containing STMT as the last statement, has precisely the
4752 edge that make_eh_edges would create. */
4755 verify_eh_edges (gimple
*stmt
)
4757 basic_block bb
= gimple_bb (stmt
);
4758 eh_landing_pad lp
= NULL
;
4763 lp_nr
= lookup_stmt_eh_lp (stmt
);
4765 lp
= get_eh_landing_pad_from_number (lp_nr
);
4768 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
4770 if (e
->flags
& EDGE_EH
)
4774 error ("BB %i has multiple EH edges", bb
->index
);
4786 error ("BB %i cannot throw but has an EH edge", bb
->index
);
4792 if (!stmt_could_throw_p (cfun
, stmt
))
4794 error ("BB %i last statement has incorrectly set lp", bb
->index
);
4798 if (eh_edge
== NULL
)
4800 error ("BB %i is missing an EH edge", bb
->index
);
4804 if (eh_edge
->dest
!= label_to_block (cfun
, lp
->post_landing_pad
))
4806 error ("Incorrect EH edge %i->%i", bb
->index
, eh_edge
->dest
->index
);
4813 /* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
4816 verify_eh_dispatch_edge (geh_dispatch
*stmt
)
4820 basic_block src
, dst
;
4821 bool want_fallthru
= true;
4825 r
= get_eh_region_from_number (gimple_eh_dispatch_region (stmt
));
4826 src
= gimple_bb (stmt
);
4828 FOR_EACH_EDGE (e
, ei
, src
->succs
)
4829 gcc_assert (e
->aux
== NULL
);
4834 for (c
= r
->u
.eh_try
.first_catch
; c
; c
= c
->next_catch
)
4836 dst
= label_to_block (cfun
, c
->label
);
4837 e
= find_edge (src
, dst
);
4840 error ("BB %i is missing an edge", src
->index
);
4845 /* A catch-all handler doesn't have a fallthru. */
4846 if (c
->type_list
== NULL
)
4848 want_fallthru
= false;
4854 case ERT_ALLOWED_EXCEPTIONS
:
4855 dst
= label_to_block (cfun
, r
->u
.allowed
.label
);
4856 e
= find_edge (src
, dst
);
4859 error ("BB %i is missing an edge", src
->index
);
4870 FOR_EACH_EDGE (e
, ei
, src
->succs
)
4872 if (e
->flags
& EDGE_FALLTHRU
)
4874 if (fall_edge
!= NULL
)
4876 error ("BB %i too many fallthru edges", src
->index
);
4885 error ("BB %i has incorrect edge", src
->index
);
4889 if ((fall_edge
!= NULL
) ^ want_fallthru
)
4891 error ("BB %i has incorrect fallthru edge", src
->index
);
4899 # pragma GCC diagnostic pop