]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-eh.c
remove need for store_values_directly
[thirdparty/gcc.git] / gcc / tree-eh.c
CommitLineData
6de9cd9a 1/* Exception handling semantics and decomposition for trees.
5624e564 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
6de9cd9a
DN
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
9dcd6f09 8the Free Software Foundation; either version 3, or (at your option)
6de9cd9a
DN
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
4a8fb1a1 23#include "hash-table.h"
6de9cd9a 24#include "tm.h"
40e23961
MC
25#include "hash-set.h"
26#include "machmode.h"
27#include "vec.h"
28#include "double-int.h"
29#include "input.h"
30#include "alias.h"
31#include "symtab.h"
32#include "wide-int.h"
33#include "inchash.h"
6de9cd9a 34#include "tree.h"
40e23961 35#include "fold-const.h"
36566b39 36#include "hashtab.h"
83685514 37#include "hard-reg-set.h"
6de9cd9a 38#include "function.h"
36566b39
PK
39#include "rtl.h"
40#include "flags.h"
41#include "statistics.h"
42#include "real.h"
43#include "fixed-value.h"
44#include "insn-config.h"
45#include "expmed.h"
46#include "dojump.h"
47#include "explow.h"
48#include "calls.h"
49#include "emit-rtl.h"
50#include "varasm.h"
51#include "stmt.h"
52#include "expr.h"
6de9cd9a 53#include "except.h"
60393bbc
AM
54#include "predict.h"
55#include "dominance.h"
56#include "cfg.h"
57#include "cfganal.h"
58#include "cfgcleanup.h"
2fb9a547
AM
59#include "basic-block.h"
60#include "tree-ssa-alias.h"
61#include "internal-fn.h"
62#include "tree-eh.h"
63#include "gimple-expr.h"
64#include "is-a.h"
442b4905 65#include "gimple.h"
5be5c238 66#include "gimple-iterator.h"
442b4905 67#include "gimple-ssa.h"
c582198b
AM
68#include "hash-map.h"
69#include "plugin-api.h"
70#include "ipa-ref.h"
442b4905
AM
71#include "cgraph.h"
72#include "tree-cfg.h"
73#include "tree-phinodes.h"
74#include "ssa-iterators.h"
d8a2d370 75#include "stringpool.h"
442b4905
AM
76#include "tree-ssanames.h"
77#include "tree-into-ssa.h"
7a300452 78#include "tree-ssa.h"
6de9cd9a 79#include "tree-inline.h"
6de9cd9a 80#include "tree-pass.h"
6de9cd9a 81#include "langhooks.h"
718f9c0f 82#include "diagnostic-core.h"
1d65f45c 83#include "target.h"
7d776ee2 84#include "cfgloop.h"
4484a35a 85#include "gimple-low.h"
726a989a
RB
86
87/* In some instances a tree and a gimple need to be stored in a same table,
88 i.e. in hash tables. This is a structure to do this. */
89typedef union {tree *tp; tree t; gimple g;} treemple;
6de9cd9a 90
6de9cd9a
DN
91/* Misc functions used in this file. */
92
1d65f45c 93/* Remember and lookup EH landing pad data for arbitrary statements.
6de9cd9a
DN
94 Really this means any statement that could_throw_p. We could
95 stuff this information into the stmt_ann data structure, but:
96
97 (1) We absolutely rely on this information being kept until
98 we get to rtl. Once we're done with lowering here, if we lose
99 the information there's no way to recover it!
100
19114537 101 (2) There are many more statements that *cannot* throw as
6de9cd9a
DN
102 compared to those that can. We should be saving some amount
103 of space by only allocating memory for those that can throw. */
104
1d65f45c 105/* Add statement T in function IFUN to landing pad NUM. */
726a989a 106
481d1b81 107static void
1d65f45c 108add_stmt_to_eh_lp_fn (struct function *ifun, gimple t, int num)
6de9cd9a 109{
1d65f45c 110 gcc_assert (num != 0);
6de9cd9a 111
98f464e0 112 if (!get_eh_throw_stmt_table (ifun))
b086d530 113 set_eh_throw_stmt_table (ifun, hash_map<gimple, int>::create_ggc (31));
98f464e0 114
b086d530 115 gcc_assert (!get_eh_throw_stmt_table (ifun)->put (t, num));
6de9cd9a 116}
1eaba2f2 117
1d65f45c 118/* Add statement T in the current function (cfun) to EH landing pad NUM. */
726a989a 119
b4660e5a 120void
1d65f45c 121add_stmt_to_eh_lp (gimple t, int num)
b4660e5a 122{
1d65f45c
RH
123 add_stmt_to_eh_lp_fn (cfun, t, num);
124}
125
126/* Add statement T to the single EH landing pad in REGION. */
127
128static void
129record_stmt_eh_region (eh_region region, gimple t)
130{
131 if (region == NULL)
132 return;
133 if (region->type == ERT_MUST_NOT_THROW)
134 add_stmt_to_eh_lp_fn (cfun, t, -region->index);
135 else
136 {
137 eh_landing_pad lp = region->landing_pads;
138 if (lp == NULL)
139 lp = gen_eh_landing_pad (region);
140 else
141 gcc_assert (lp->next_lp == NULL);
142 add_stmt_to_eh_lp_fn (cfun, t, lp->index);
143 }
b4660e5a
JH
144}
145
726a989a 146
1d65f45c 147/* Remove statement T in function IFUN from its EH landing pad. */
726a989a 148
1eaba2f2 149bool
1d65f45c 150remove_stmt_from_eh_lp_fn (struct function *ifun, gimple t)
1eaba2f2 151{
b4660e5a 152 if (!get_eh_throw_stmt_table (ifun))
1eaba2f2
RH
153 return false;
154
b086d530 155 if (!get_eh_throw_stmt_table (ifun)->get (t))
1eaba2f2 156 return false;
b086d530
TS
157
158 get_eh_throw_stmt_table (ifun)->remove (t);
159 return true;
1eaba2f2
RH
160}
161
726a989a 162
1d65f45c
RH
163/* Remove statement T in the current function (cfun) from its
164 EH landing pad. */
726a989a 165
b4660e5a 166bool
1d65f45c 167remove_stmt_from_eh_lp (gimple t)
b4660e5a 168{
1d65f45c 169 return remove_stmt_from_eh_lp_fn (cfun, t);
b4660e5a
JH
170}
171
726a989a 172/* Determine if statement T is inside an EH region in function IFUN.
1d65f45c
RH
173 Positive numbers indicate a landing pad index; negative numbers
174 indicate a MUST_NOT_THROW region index; zero indicates that the
175 statement is not recorded in the region table. */
726a989a 176
6de9cd9a 177int
1d65f45c 178lookup_stmt_eh_lp_fn (struct function *ifun, gimple t)
6de9cd9a 179{
1d65f45c
RH
180 if (ifun->eh->throw_stmt_table == NULL)
181 return 0;
6de9cd9a 182
b086d530
TS
183 int *lp_nr = ifun->eh->throw_stmt_table->get (t);
184 return lp_nr ? *lp_nr : 0;
6de9cd9a
DN
185}
186
1d65f45c 187/* Likewise, but always use the current function. */
726a989a 188
b4660e5a 189int
1d65f45c 190lookup_stmt_eh_lp (gimple t)
b4660e5a
JH
191{
192 /* We can get called from initialized data when -fnon-call-exceptions
193 is on; prevent crash. */
194 if (!cfun)
1d65f45c
RH
195 return 0;
196 return lookup_stmt_eh_lp_fn (cfun, t);
b4660e5a 197}
6de9cd9a 198
726a989a 199/* First pass of EH node decomposition. Build up a tree of GIMPLE_TRY_FINALLY
6de9cd9a
DN
200 nodes and LABEL_DECL nodes. We will use this during the second phase to
201 determine if a goto leaves the body of a TRY_FINALLY_EXPR node. */
202
203struct finally_tree_node
204{
726a989a
RB
205 /* When storing a GIMPLE_TRY, we have to record a gimple. However
206 when deciding whether a GOTO to a certain LABEL_DECL (which is a
207 tree) leaves the TRY block, its necessary to record a tree in
208 this field. Thus a treemple is used. */
1d65f45c 209 treemple child;
538dd0b7 210 gtry *parent;
6de9cd9a
DN
211};
212
4a8fb1a1
LC
213/* Hashtable helpers. */
214
215struct finally_tree_hasher : typed_free_remove <finally_tree_node>
216{
67f58944
TS
217 typedef finally_tree_node *value_type;
218 typedef finally_tree_node *compare_type;
219 static inline hashval_t hash (const finally_tree_node *);
220 static inline bool equal (const finally_tree_node *,
221 const finally_tree_node *);
4a8fb1a1
LC
222};
223
224inline hashval_t
67f58944 225finally_tree_hasher::hash (const finally_tree_node *v)
4a8fb1a1
LC
226{
227 return (intptr_t)v->child.t >> 4;
228}
229
230inline bool
67f58944
TS
231finally_tree_hasher::equal (const finally_tree_node *v,
232 const finally_tree_node *c)
4a8fb1a1
LC
233{
234 return v->child.t == c->child.t;
235}
236
6de9cd9a 237/* Note that this table is *not* marked GTY. It is short-lived. */
c203e8a7 238static hash_table<finally_tree_hasher> *finally_tree;
6de9cd9a
DN
239
240static void
538dd0b7 241record_in_finally_tree (treemple child, gtry *parent)
6de9cd9a
DN
242{
243 struct finally_tree_node *n;
4a8fb1a1 244 finally_tree_node **slot;
6de9cd9a 245
858904db 246 n = XNEW (struct finally_tree_node);
6de9cd9a
DN
247 n->child = child;
248 n->parent = parent;
249
c203e8a7 250 slot = finally_tree->find_slot (n, INSERT);
1e128c5f 251 gcc_assert (!*slot);
6de9cd9a
DN
252 *slot = n;
253}
254
255static void
538dd0b7 256collect_finally_tree (gimple stmt, gtry *region);
726a989a 257
1d65f45c 258/* Go through the gimple sequence. Works with collect_finally_tree to
726a989a
RB
259 record all GIMPLE_LABEL and GIMPLE_TRY statements. */
260
261static void
538dd0b7 262collect_finally_tree_1 (gimple_seq seq, gtry *region)
6de9cd9a 263{
726a989a 264 gimple_stmt_iterator gsi;
6de9cd9a 265
726a989a
RB
266 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
267 collect_finally_tree (gsi_stmt (gsi), region);
268}
6de9cd9a 269
726a989a 270static void
538dd0b7 271collect_finally_tree (gimple stmt, gtry *region)
726a989a
RB
272{
273 treemple temp;
274
275 switch (gimple_code (stmt))
276 {
277 case GIMPLE_LABEL:
538dd0b7 278 temp.t = gimple_label_label (as_a <glabel *> (stmt));
726a989a
RB
279 record_in_finally_tree (temp, region);
280 break;
6de9cd9a 281
726a989a
RB
282 case GIMPLE_TRY:
283 if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
284 {
285 temp.g = stmt;
286 record_in_finally_tree (temp, region);
538dd0b7
DM
287 collect_finally_tree_1 (gimple_try_eval (stmt),
288 as_a <gtry *> (stmt));
726a989a
RB
289 collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
290 }
291 else if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
292 {
293 collect_finally_tree_1 (gimple_try_eval (stmt), region);
294 collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
295 }
296 break;
6de9cd9a 297
726a989a 298 case GIMPLE_CATCH:
538dd0b7
DM
299 collect_finally_tree_1 (gimple_catch_handler (
300 as_a <gcatch *> (stmt)),
301 region);
726a989a 302 break;
6de9cd9a 303
726a989a
RB
304 case GIMPLE_EH_FILTER:
305 collect_finally_tree_1 (gimple_eh_filter_failure (stmt), region);
6de9cd9a
DN
306 break;
307
0a35513e 308 case GIMPLE_EH_ELSE:
538dd0b7
DM
309 {
310 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
311 collect_finally_tree_1 (gimple_eh_else_n_body (eh_else_stmt), region);
312 collect_finally_tree_1 (gimple_eh_else_e_body (eh_else_stmt), region);
313 }
0a35513e
AH
314 break;
315
6de9cd9a
DN
316 default:
317 /* A type, a decl, or some kind of statement that we're not
318 interested in. Don't walk them. */
319 break;
320 }
321}
322
726a989a 323
6de9cd9a
DN
324/* Use the finally tree to determine if a jump from START to TARGET
325 would leave the try_finally node that START lives in. */
326
327static bool
726a989a 328outside_finally_tree (treemple start, gimple target)
6de9cd9a
DN
329{
330 struct finally_tree_node n, *p;
331
332 do
333 {
334 n.child = start;
c203e8a7 335 p = finally_tree->find (&n);
6de9cd9a
DN
336 if (!p)
337 return true;
726a989a 338 start.g = p->parent;
6de9cd9a 339 }
726a989a 340 while (start.g != target);
6de9cd9a
DN
341
342 return false;
343}
726a989a
RB
344
345/* Second pass of EH node decomposition. Actually transform the GIMPLE_TRY
346 nodes into a set of gotos, magic labels, and eh regions.
6de9cd9a
DN
347 The eh region creation is straight-forward, but frobbing all the gotos
348 and such into shape isn't. */
349
b8698a0f 350/* The sequence into which we record all EH stuff. This will be
1d65f45c
RH
351 placed at the end of the function when we're all done. */
352static gimple_seq eh_seq;
353
354/* Record whether an EH region contains something that can throw,
355 indexed by EH region number. */
b7da9fd4 356static bitmap eh_region_may_contain_throw_map;
1d65f45c 357
24b97832
ILT
358/* The GOTO_QUEUE is is an array of GIMPLE_GOTO and GIMPLE_RETURN
359 statements that are seen to escape this GIMPLE_TRY_FINALLY node.
360 The idea is to record a gimple statement for everything except for
361 the conditionals, which get their labels recorded. Since labels are
362 of type 'tree', we need this node to store both gimple and tree
363 objects. REPL_STMT is the sequence used to replace the goto/return
364 statement. CONT_STMT is used to store the statement that allows
365 the return/goto to jump to the original destination. */
366
367struct goto_queue_node
368{
369 treemple stmt;
820055a0 370 location_t location;
24b97832
ILT
371 gimple_seq repl_stmt;
372 gimple cont_stmt;
373 int index;
374 /* This is used when index >= 0 to indicate that stmt is a label (as
375 opposed to a goto stmt). */
376 int is_label;
377};
378
6de9cd9a
DN
379/* State of the world while lowering. */
380
381struct leh_state
382{
19114537 383 /* What's "current" while constructing the eh region tree. These
6de9cd9a
DN
384 correspond to variables of the same name in cfun->eh, which we
385 don't have easy access to. */
1d65f45c
RH
386 eh_region cur_region;
387
388 /* What's "current" for the purposes of __builtin_eh_pointer. For
389 a CATCH, this is the associated TRY. For an EH_FILTER, this is
390 the associated ALLOWED_EXCEPTIONS, etc. */
391 eh_region ehp_region;
6de9cd9a
DN
392
393 /* Processing of TRY_FINALLY requires a bit more state. This is
394 split out into a separate structure so that we don't have to
395 copy so much when processing other nodes. */
396 struct leh_tf_state *tf;
397};
398
399struct leh_tf_state
400{
726a989a
RB
401 /* Pointer to the GIMPLE_TRY_FINALLY node under discussion. The
402 try_finally_expr is the original GIMPLE_TRY_FINALLY. We need to retain
403 this so that outside_finally_tree can reliably reference the tree used
404 in the collect_finally_tree data structures. */
538dd0b7
DM
405 gtry *try_finally_expr;
406 gtry *top_p;
1d65f45c 407
726a989a
RB
408 /* While lowering a top_p usually it is expanded into multiple statements,
409 thus we need the following field to store them. */
410 gimple_seq top_p_seq;
6de9cd9a
DN
411
412 /* The state outside this try_finally node. */
413 struct leh_state *outer;
414
415 /* The exception region created for it. */
1d65f45c 416 eh_region region;
6de9cd9a 417
24b97832
ILT
418 /* The goto queue. */
419 struct goto_queue_node *goto_queue;
6de9cd9a
DN
420 size_t goto_queue_size;
421 size_t goto_queue_active;
422
fa10beec 423 /* Pointer map to help in searching goto_queue when it is large. */
b787e7a2 424 hash_map<gimple, goto_queue_node *> *goto_queue_map;
0f547d3d 425
6de9cd9a 426 /* The set of unique labels seen as entries in the goto queue. */
9771b263 427 vec<tree> dest_array;
6de9cd9a
DN
428
429 /* A label to be added at the end of the completed transformed
430 sequence. It will be set if may_fallthru was true *at one time*,
431 though subsequent transformations may have cleared that flag. */
432 tree fallthru_label;
433
6de9cd9a
DN
434 /* True if it is possible to fall out the bottom of the try block.
435 Cleared if the fallthru is converted to a goto. */
436 bool may_fallthru;
437
726a989a 438 /* True if any entry in goto_queue is a GIMPLE_RETURN. */
6de9cd9a
DN
439 bool may_return;
440
441 /* True if the finally block can receive an exception edge.
442 Cleared if the exception case is handled by code duplication. */
443 bool may_throw;
444};
445
538dd0b7 446static gimple_seq lower_eh_must_not_throw (struct leh_state *, gtry *);
6de9cd9a 447
6de9cd9a
DN
448/* Search for STMT in the goto queue. Return the replacement,
449 or null if the statement isn't in the queue. */
450
0f547d3d
SE
451#define LARGE_GOTO_QUEUE 20
452
355a7673 453static void lower_eh_constructs_1 (struct leh_state *state, gimple_seq *seq);
726a989a
RB
454
455static gimple_seq
456find_goto_replacement (struct leh_tf_state *tf, treemple stmt)
6de9cd9a 457{
0f547d3d 458 unsigned int i;
0f547d3d
SE
459
460 if (tf->goto_queue_active < LARGE_GOTO_QUEUE)
461 {
462 for (i = 0; i < tf->goto_queue_active; i++)
726a989a 463 if ( tf->goto_queue[i].stmt.g == stmt.g)
0f547d3d
SE
464 return tf->goto_queue[i].repl_stmt;
465 return NULL;
466 }
467
468 /* If we have a large number of entries in the goto_queue, create a
469 pointer map and use that for searching. */
470
471 if (!tf->goto_queue_map)
472 {
b787e7a2 473 tf->goto_queue_map = new hash_map<gimple, goto_queue_node *>;
0f547d3d
SE
474 for (i = 0; i < tf->goto_queue_active; i++)
475 {
b787e7a2
TS
476 bool existed = tf->goto_queue_map->put (tf->goto_queue[i].stmt.g,
477 &tf->goto_queue[i]);
478 gcc_assert (!existed);
0f547d3d
SE
479 }
480 }
481
b787e7a2 482 goto_queue_node **slot = tf->goto_queue_map->get (stmt.g);
0f547d3d 483 if (slot != NULL)
b787e7a2 484 return ((*slot)->repl_stmt);
0f547d3d
SE
485
486 return NULL;
6de9cd9a
DN
487}
488
489/* A subroutine of replace_goto_queue_1. Handles the sub-clauses of a
726a989a 490 lowered GIMPLE_COND. If, by chance, the replacement is a simple goto,
6de9cd9a 491 then we can just splat it in, otherwise we add the new stmts immediately
726a989a 492 after the GIMPLE_COND and redirect. */
6de9cd9a
DN
493
494static void
495replace_goto_queue_cond_clause (tree *tp, struct leh_tf_state *tf,
726a989a 496 gimple_stmt_iterator *gsi)
6de9cd9a 497{
726a989a 498 tree label;
82d6e6fc 499 gimple_seq new_seq;
726a989a 500 treemple temp;
c2255bc4 501 location_t loc = gimple_location (gsi_stmt (*gsi));
6de9cd9a 502
726a989a 503 temp.tp = tp;
82d6e6fc
KG
504 new_seq = find_goto_replacement (tf, temp);
505 if (!new_seq)
6de9cd9a
DN
506 return;
507
82d6e6fc
KG
508 if (gimple_seq_singleton_p (new_seq)
509 && gimple_code (gimple_seq_first_stmt (new_seq)) == GIMPLE_GOTO)
6de9cd9a 510 {
82d6e6fc 511 *tp = gimple_goto_dest (gimple_seq_first_stmt (new_seq));
6de9cd9a
DN
512 return;
513 }
514
c2255bc4 515 label = create_artificial_label (loc);
726a989a
RB
516 /* Set the new label for the GIMPLE_COND */
517 *tp = label;
6de9cd9a 518
726a989a 519 gsi_insert_after (gsi, gimple_build_label (label), GSI_CONTINUE_LINKING);
82d6e6fc 520 gsi_insert_seq_after (gsi, gimple_seq_copy (new_seq), GSI_CONTINUE_LINKING);
6de9cd9a
DN
521}
522
19114537 523/* The real work of replace_goto_queue. Returns with TSI updated to
6de9cd9a
DN
524 point to the next statement. */
525
355a7673 526static void replace_goto_queue_stmt_list (gimple_seq *, struct leh_tf_state *);
6de9cd9a
DN
527
528static void
726a989a
RB
529replace_goto_queue_1 (gimple stmt, struct leh_tf_state *tf,
530 gimple_stmt_iterator *gsi)
6de9cd9a 531{
726a989a
RB
532 gimple_seq seq;
533 treemple temp;
534 temp.g = NULL;
535
536 switch (gimple_code (stmt))
6de9cd9a 537 {
726a989a
RB
538 case GIMPLE_GOTO:
539 case GIMPLE_RETURN:
540 temp.g = stmt;
541 seq = find_goto_replacement (tf, temp);
542 if (seq)
6de9cd9a 543 {
726a989a
RB
544 gsi_insert_seq_before (gsi, gimple_seq_copy (seq), GSI_SAME_STMT);
545 gsi_remove (gsi, false);
6de9cd9a
DN
546 return;
547 }
548 break;
549
726a989a
RB
550 case GIMPLE_COND:
551 replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 2), tf, gsi);
552 replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 3), tf, gsi);
6de9cd9a
DN
553 break;
554
726a989a 555 case GIMPLE_TRY:
355a7673
MM
556 replace_goto_queue_stmt_list (gimple_try_eval_ptr (stmt), tf);
557 replace_goto_queue_stmt_list (gimple_try_cleanup_ptr (stmt), tf);
6de9cd9a 558 break;
726a989a 559 case GIMPLE_CATCH:
538dd0b7
DM
560 replace_goto_queue_stmt_list (gimple_catch_handler_ptr (
561 as_a <gcatch *> (stmt)),
562 tf);
6de9cd9a 563 break;
726a989a 564 case GIMPLE_EH_FILTER:
355a7673 565 replace_goto_queue_stmt_list (gimple_eh_filter_failure_ptr (stmt), tf);
6de9cd9a 566 break;
0a35513e 567 case GIMPLE_EH_ELSE:
538dd0b7
DM
568 {
569 geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
570 replace_goto_queue_stmt_list (gimple_eh_else_n_body_ptr (eh_else_stmt),
571 tf);
572 replace_goto_queue_stmt_list (gimple_eh_else_e_body_ptr (eh_else_stmt),
573 tf);
574 }
0a35513e 575 break;
6de9cd9a 576
6de9cd9a
DN
577 default:
578 /* These won't have gotos in them. */
579 break;
580 }
581
726a989a 582 gsi_next (gsi);
6de9cd9a
DN
583}
584
726a989a 585/* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
6de9cd9a
DN
586
587static void
355a7673 588replace_goto_queue_stmt_list (gimple_seq *seq, struct leh_tf_state *tf)
6de9cd9a 589{
355a7673 590 gimple_stmt_iterator gsi = gsi_start (*seq);
726a989a
RB
591
592 while (!gsi_end_p (gsi))
593 replace_goto_queue_1 (gsi_stmt (gsi), tf, &gsi);
6de9cd9a
DN
594}
595
596/* Replace all goto queue members. */
597
598static void
599replace_goto_queue (struct leh_tf_state *tf)
600{
8287d24a
EB
601 if (tf->goto_queue_active == 0)
602 return;
355a7673
MM
603 replace_goto_queue_stmt_list (&tf->top_p_seq, tf);
604 replace_goto_queue_stmt_list (&eh_seq, tf);
6de9cd9a
DN
605}
606
726a989a
RB
607/* Add a new record to the goto queue contained in TF. NEW_STMT is the
608 data to be added, IS_LABEL indicates whether NEW_STMT is a label or
609 a gimple return. */
6de9cd9a
DN
610
611static void
726a989a
RB
612record_in_goto_queue (struct leh_tf_state *tf,
613 treemple new_stmt,
614 int index,
820055a0
DC
615 bool is_label,
616 location_t location)
6de9cd9a 617{
6de9cd9a 618 size_t active, size;
726a989a 619 struct goto_queue_node *q;
6de9cd9a 620
0f547d3d
SE
621 gcc_assert (!tf->goto_queue_map);
622
6de9cd9a
DN
623 active = tf->goto_queue_active;
624 size = tf->goto_queue_size;
625 if (active >= size)
626 {
627 size = (size ? size * 2 : 32);
628 tf->goto_queue_size = size;
629 tf->goto_queue
858904db 630 = XRESIZEVEC (struct goto_queue_node, tf->goto_queue, size);
6de9cd9a
DN
631 }
632
633 q = &tf->goto_queue[active];
634 tf->goto_queue_active = active + 1;
19114537 635
6de9cd9a 636 memset (q, 0, sizeof (*q));
726a989a 637 q->stmt = new_stmt;
6de9cd9a 638 q->index = index;
820055a0 639 q->location = location;
726a989a
RB
640 q->is_label = is_label;
641}
642
643/* Record the LABEL label in the goto queue contained in TF.
644 TF is not null. */
645
646static void
820055a0
DC
647record_in_goto_queue_label (struct leh_tf_state *tf, treemple stmt, tree label,
648 location_t location)
726a989a
RB
649{
650 int index;
651 treemple temp, new_stmt;
652
653 if (!label)
654 return;
655
656 /* Computed and non-local gotos do not get processed. Given
657 their nature we can neither tell whether we've escaped the
658 finally block nor redirect them if we knew. */
659 if (TREE_CODE (label) != LABEL_DECL)
660 return;
661
662 /* No need to record gotos that don't leave the try block. */
663 temp.t = label;
664 if (!outside_finally_tree (temp, tf->try_finally_expr))
665 return;
666
9771b263 667 if (! tf->dest_array.exists ())
726a989a 668 {
9771b263
DN
669 tf->dest_array.create (10);
670 tf->dest_array.quick_push (label);
726a989a
RB
671 index = 0;
672 }
673 else
674 {
9771b263 675 int n = tf->dest_array.length ();
726a989a 676 for (index = 0; index < n; ++index)
9771b263 677 if (tf->dest_array[index] == label)
726a989a
RB
678 break;
679 if (index == n)
9771b263 680 tf->dest_array.safe_push (label);
726a989a
RB
681 }
682
683 /* In the case of a GOTO we want to record the destination label,
684 since with a GIMPLE_COND we have an easy access to the then/else
685 labels. */
686 new_stmt = stmt;
820055a0 687 record_in_goto_queue (tf, new_stmt, index, true, location);
726a989a
RB
688}
689
690/* For any GIMPLE_GOTO or GIMPLE_RETURN, decide whether it leaves a try_finally
691 node, and if so record that fact in the goto queue associated with that
692 try_finally node. */
693
694static void
695maybe_record_in_goto_queue (struct leh_state *state, gimple stmt)
696{
697 struct leh_tf_state *tf = state->tf;
698 treemple new_stmt;
699
700 if (!tf)
701 return;
702
703 switch (gimple_code (stmt))
704 {
705 case GIMPLE_COND:
538dd0b7
DM
706 {
707 gcond *cond_stmt = as_a <gcond *> (stmt);
708 new_stmt.tp = gimple_op_ptr (cond_stmt, 2);
709 record_in_goto_queue_label (tf, new_stmt,
710 gimple_cond_true_label (cond_stmt),
711 EXPR_LOCATION (*new_stmt.tp));
712 new_stmt.tp = gimple_op_ptr (cond_stmt, 3);
713 record_in_goto_queue_label (tf, new_stmt,
714 gimple_cond_false_label (cond_stmt),
715 EXPR_LOCATION (*new_stmt.tp));
716 }
726a989a
RB
717 break;
718 case GIMPLE_GOTO:
719 new_stmt.g = stmt;
820055a0
DC
720 record_in_goto_queue_label (tf, new_stmt, gimple_goto_dest (stmt),
721 gimple_location (stmt));
726a989a
RB
722 break;
723
724 case GIMPLE_RETURN:
725 tf->may_return = true;
726 new_stmt.g = stmt;
820055a0 727 record_in_goto_queue (tf, new_stmt, -1, false, gimple_location (stmt));
726a989a
RB
728 break;
729
730 default:
731 gcc_unreachable ();
732 }
6de9cd9a
DN
733}
734
726a989a 735
6de9cd9a 736#ifdef ENABLE_CHECKING
726a989a 737/* We do not process GIMPLE_SWITCHes for now. As long as the original source
6de9cd9a 738 was in fact structured, and we've not yet done jump threading, then none
726a989a 739 of the labels will leave outer GIMPLE_TRY_FINALLY nodes. Verify this. */
6de9cd9a
DN
740
741static void
538dd0b7
DM
742verify_norecord_switch_expr (struct leh_state *state,
743 gswitch *switch_expr)
6de9cd9a
DN
744{
745 struct leh_tf_state *tf = state->tf;
746 size_t i, n;
6de9cd9a
DN
747
748 if (!tf)
749 return;
750
726a989a 751 n = gimple_switch_num_labels (switch_expr);
6de9cd9a
DN
752
753 for (i = 0; i < n; ++i)
754 {
726a989a
RB
755 treemple temp;
756 tree lab = CASE_LABEL (gimple_switch_label (switch_expr, i));
757 temp.t = lab;
758 gcc_assert (!outside_finally_tree (temp, tf->try_finally_expr));
6de9cd9a
DN
759 }
760}
761#else
762#define verify_norecord_switch_expr(state, switch_expr)
763#endif
764
8d686507
ILT
765/* Redirect a RETURN_EXPR pointed to by Q to FINLAB. If MOD is
766 non-null, insert it before the new branch. */
6de9cd9a
DN
767
768static void
8d686507 769do_return_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod)
6de9cd9a 770{
726a989a
RB
771 gimple x;
772
8d686507 773 /* In the case of a return, the queue node must be a gimple statement. */
726a989a
RB
774 gcc_assert (!q->is_label);
775
8d686507 776 /* Note that the return value may have already been computed, e.g.,
6de9cd9a 777
8d686507
ILT
778 int x;
779 int foo (void)
6de9cd9a 780 {
8d686507
ILT
781 x = 0;
782 try {
783 return x;
784 } finally {
785 x++;
786 }
6de9cd9a 787 }
8d686507
ILT
788
789 should return 0, not 1. We don't have to do anything to make
790 this happens because the return value has been placed in the
791 RESULT_DECL already. */
792
793 q->cont_stmt = q->stmt.g;
726a989a 794
6de9cd9a 795 if (mod)
726a989a 796 gimple_seq_add_seq (&q->repl_stmt, mod);
6de9cd9a 797
726a989a 798 x = gimple_build_goto (finlab);
29f5bccb 799 gimple_set_location (x, q->location);
726a989a 800 gimple_seq_add_stmt (&q->repl_stmt, x);
6de9cd9a
DN
801}
802
726a989a 803/* Similar, but easier, for GIMPLE_GOTO. */
6de9cd9a
DN
804
805static void
726a989a
RB
806do_goto_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod,
807 struct leh_tf_state *tf)
6de9cd9a 808{
538dd0b7 809 ggoto *x;
726a989a
RB
810
811 gcc_assert (q->is_label);
726a989a 812
9771b263 813 q->cont_stmt = gimple_build_goto (tf->dest_array[q->index]);
6de9cd9a 814
6de9cd9a 815 if (mod)
726a989a 816 gimple_seq_add_seq (&q->repl_stmt, mod);
6de9cd9a 817
726a989a 818 x = gimple_build_goto (finlab);
29f5bccb 819 gimple_set_location (x, q->location);
726a989a 820 gimple_seq_add_stmt (&q->repl_stmt, x);
6de9cd9a
DN
821}
822
1d65f45c
RH
823/* Emit a standard landing pad sequence into SEQ for REGION. */
824
825static void
826emit_post_landing_pad (gimple_seq *seq, eh_region region)
827{
828 eh_landing_pad lp = region->landing_pads;
538dd0b7 829 glabel *x;
1d65f45c
RH
830
831 if (lp == NULL)
832 lp = gen_eh_landing_pad (region);
833
834 lp->post_landing_pad = create_artificial_label (UNKNOWN_LOCATION);
835 EH_LANDING_PAD_NR (lp->post_landing_pad) = lp->index;
836
837 x = gimple_build_label (lp->post_landing_pad);
838 gimple_seq_add_stmt (seq, x);
839}
840
841/* Emit a RESX statement into SEQ for REGION. */
842
843static void
844emit_resx (gimple_seq *seq, eh_region region)
845{
538dd0b7 846 gresx *x = gimple_build_resx (region->index);
1d65f45c
RH
847 gimple_seq_add_stmt (seq, x);
848 if (region->outer)
849 record_stmt_eh_region (region->outer, x);
850}
851
852/* Emit an EH_DISPATCH statement into SEQ for REGION. */
853
854static void
855emit_eh_dispatch (gimple_seq *seq, eh_region region)
856{
538dd0b7 857 geh_dispatch *x = gimple_build_eh_dispatch (region->index);
1d65f45c
RH
858 gimple_seq_add_stmt (seq, x);
859}
860
861/* Note that the current EH region may contain a throw, or a
862 call to a function which itself may contain a throw. */
863
864static void
865note_eh_region_may_contain_throw (eh_region region)
866{
fcaa4ca4 867 while (bitmap_set_bit (eh_region_may_contain_throw_map, region->index))
1d65f45c 868 {
6788475a
JJ
869 if (region->type == ERT_MUST_NOT_THROW)
870 break;
1d65f45c
RH
871 region = region->outer;
872 if (region == NULL)
873 break;
874 }
875}
876
b7da9fd4
RH
877/* Check if REGION has been marked as containing a throw. If REGION is
878 NULL, this predicate is false. */
879
880static inline bool
881eh_region_may_contain_throw (eh_region r)
882{
883 return r && bitmap_bit_p (eh_region_may_contain_throw_map, r->index);
884}
885
6de9cd9a
DN
886/* We want to transform
887 try { body; } catch { stuff; }
888 to
3b445b24 889 normal_sequence:
1d65f45c
RH
890 body;
891 over:
3b445b24 892 eh_sequence:
1d65f45c
RH
893 landing_pad:
894 stuff;
895 goto over;
896
897 TP is a GIMPLE_TRY node. REGION is the region whose post_landing_pad
6de9cd9a
DN
898 should be placed before the second operand, or NULL. OVER is
899 an existing label that should be put at the exit, or NULL. */
900
726a989a 901static gimple_seq
538dd0b7 902frob_into_branch_around (gtry *tp, eh_region region, tree over)
6de9cd9a 903{
726a989a
RB
904 gimple x;
905 gimple_seq cleanup, result;
c2255bc4 906 location_t loc = gimple_location (tp);
6de9cd9a 907
726a989a
RB
908 cleanup = gimple_try_cleanup (tp);
909 result = gimple_try_eval (tp);
6de9cd9a 910
1d65f45c
RH
911 if (region)
912 emit_post_landing_pad (&eh_seq, region);
913
914 if (gimple_seq_may_fallthru (cleanup))
6de9cd9a
DN
915 {
916 if (!over)
c2255bc4 917 over = create_artificial_label (loc);
726a989a 918 x = gimple_build_goto (over);
29f5bccb 919 gimple_set_location (x, loc);
1d65f45c 920 gimple_seq_add_stmt (&cleanup, x);
6de9cd9a 921 }
1d65f45c 922 gimple_seq_add_seq (&eh_seq, cleanup);
6de9cd9a
DN
923
924 if (over)
925 {
726a989a
RB
926 x = gimple_build_label (over);
927 gimple_seq_add_stmt (&result, x);
6de9cd9a 928 }
726a989a 929 return result;
6de9cd9a
DN
930}
931
932/* A subroutine of lower_try_finally. Duplicate the tree rooted at T.
933 Make sure to record all new labels found. */
934
726a989a 935static gimple_seq
820055a0
DC
936lower_try_finally_dup_block (gimple_seq seq, struct leh_state *outer_state,
937 location_t loc)
6de9cd9a 938{
538dd0b7 939 gtry *region = NULL;
726a989a 940 gimple_seq new_seq;
820055a0 941 gimple_stmt_iterator gsi;
6de9cd9a 942
726a989a 943 new_seq = copy_gimple_seq_and_replace_locals (seq);
6de9cd9a 944
820055a0 945 for (gsi = gsi_start (new_seq); !gsi_end_p (gsi); gsi_next (&gsi))
62d4d60c
DC
946 {
947 gimple stmt = gsi_stmt (gsi);
2f13f2de 948 if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
62d4d60c
DC
949 {
950 tree block = gimple_block (stmt);
951 gimple_set_location (stmt, loc);
952 gimple_set_block (stmt, block);
953 }
954 }
820055a0 955
6de9cd9a
DN
956 if (outer_state->tf)
957 region = outer_state->tf->try_finally_expr;
726a989a 958 collect_finally_tree_1 (new_seq, region);
6de9cd9a 959
726a989a 960 return new_seq;
6de9cd9a
DN
961}
962
963/* A subroutine of lower_try_finally. Create a fallthru label for
964 the given try_finally state. The only tricky bit here is that
965 we have to make sure to record the label in our outer context. */
966
967static tree
968lower_try_finally_fallthru_label (struct leh_tf_state *tf)
969{
970 tree label = tf->fallthru_label;
726a989a
RB
971 treemple temp;
972
6de9cd9a
DN
973 if (!label)
974 {
c2255bc4 975 label = create_artificial_label (gimple_location (tf->try_finally_expr));
6de9cd9a
DN
976 tf->fallthru_label = label;
977 if (tf->outer->tf)
726a989a
RB
978 {
979 temp.t = label;
980 record_in_finally_tree (temp, tf->outer->tf->try_finally_expr);
981 }
6de9cd9a
DN
982 }
983 return label;
984}
985
0a35513e
AH
986/* A subroutine of lower_try_finally. If FINALLY consits of a
987 GIMPLE_EH_ELSE node, return it. */
988
538dd0b7 989static inline geh_else *
0a35513e
AH
990get_eh_else (gimple_seq finally)
991{
992 gimple x = gimple_seq_first_stmt (finally);
993 if (gimple_code (x) == GIMPLE_EH_ELSE)
994 {
995 gcc_assert (gimple_seq_singleton_p (finally));
538dd0b7 996 return as_a <geh_else *> (x);
0a35513e
AH
997 }
998 return NULL;
999}
1000
3b06d379
SB
1001/* A subroutine of lower_try_finally. If the eh_protect_cleanup_actions
1002 langhook returns non-null, then the language requires that the exception
1003 path out of a try_finally be treated specially. To wit: the code within
1004 the finally block may not itself throw an exception. We have two choices
1005 here. First we can duplicate the finally block and wrap it in a
1006 must_not_throw region. Second, we can generate code like
6de9cd9a
DN
1007
1008 try {
1009 finally_block;
1010 } catch {
1011 if (fintmp == eh_edge)
1012 protect_cleanup_actions;
1013 }
1014
1015 where "fintmp" is the temporary used in the switch statement generation
1016 alternative considered below. For the nonce, we always choose the first
19114537 1017 option.
6de9cd9a 1018
3f117656 1019 THIS_STATE may be null if this is a try-cleanup, not a try-finally. */
6de9cd9a
DN
1020
1021static void
1022honor_protect_cleanup_actions (struct leh_state *outer_state,
1023 struct leh_state *this_state,
1024 struct leh_tf_state *tf)
1025{
1d65f45c 1026 tree protect_cleanup_actions;
726a989a 1027 gimple_stmt_iterator gsi;
6de9cd9a 1028 bool finally_may_fallthru;
726a989a 1029 gimple_seq finally;
538dd0b7
DM
1030 gimple x;
1031 geh_mnt *eh_mnt;
1032 gtry *try_stmt;
1033 geh_else *eh_else;
6de9cd9a
DN
1034
1035 /* First check for nothing to do. */
3b06d379 1036 if (lang_hooks.eh_protect_cleanup_actions == NULL)
1d65f45c 1037 return;
3b06d379 1038 protect_cleanup_actions = lang_hooks.eh_protect_cleanup_actions ();
1d65f45c
RH
1039 if (protect_cleanup_actions == NULL)
1040 return;
6de9cd9a 1041
726a989a 1042 finally = gimple_try_cleanup (tf->top_p);
0a35513e 1043 eh_else = get_eh_else (finally);
6de9cd9a
DN
1044
1045 /* Duplicate the FINALLY block. Only need to do this for try-finally,
0a35513e
AH
1046 and not for cleanups. If we've got an EH_ELSE, extract it now. */
1047 if (eh_else)
1048 {
1049 finally = gimple_eh_else_e_body (eh_else);
1050 gimple_try_set_cleanup (tf->top_p, gimple_eh_else_n_body (eh_else));
1051 }
1052 else if (this_state)
820055a0 1053 finally = lower_try_finally_dup_block (finally, outer_state,
5368224f 1054 gimple_location (tf->try_finally_expr));
0a35513e 1055 finally_may_fallthru = gimple_seq_may_fallthru (finally);
6de9cd9a 1056
33b45227
JM
1057 /* If this cleanup consists of a TRY_CATCH_EXPR with TRY_CATCH_IS_CLEANUP
1058 set, the handler of the TRY_CATCH_EXPR is another cleanup which ought
1059 to be in an enclosing scope, but needs to be implemented at this level
1060 to avoid a nesting violation (see wrap_temporary_cleanups in
1061 cp/decl.c). Since it's logically at an outer level, we should call
1062 terminate before we get to it, so strip it away before adding the
1063 MUST_NOT_THROW filter. */
726a989a
RB
1064 gsi = gsi_start (finally);
1065 x = gsi_stmt (gsi);
1d65f45c 1066 if (gimple_code (x) == GIMPLE_TRY
726a989a
RB
1067 && gimple_try_kind (x) == GIMPLE_TRY_CATCH
1068 && gimple_try_catch_is_cleanup (x))
33b45227 1069 {
726a989a
RB
1070 gsi_insert_seq_before (&gsi, gimple_try_eval (x), GSI_SAME_STMT);
1071 gsi_remove (&gsi, false);
33b45227
JM
1072 }
1073
6de9cd9a 1074 /* Wrap the block with protect_cleanup_actions as the action. */
538dd0b7
DM
1075 eh_mnt = gimple_build_eh_must_not_throw (protect_cleanup_actions);
1076 try_stmt = gimple_build_try (finally, gimple_seq_alloc_with_stmt (eh_mnt),
1077 GIMPLE_TRY_CATCH);
1078 finally = lower_eh_must_not_throw (outer_state, try_stmt);
1d65f45c
RH
1079
1080 /* Drop all of this into the exception sequence. */
1081 emit_post_landing_pad (&eh_seq, tf->region);
1082 gimple_seq_add_seq (&eh_seq, finally);
1083 if (finally_may_fallthru)
1084 emit_resx (&eh_seq, tf->region);
6de9cd9a
DN
1085
1086 /* Having now been handled, EH isn't to be considered with
1087 the rest of the outgoing edges. */
1088 tf->may_throw = false;
1089}
1090
1091/* A subroutine of lower_try_finally. We have determined that there is
1092 no fallthru edge out of the finally block. This means that there is
1093 no outgoing edge corresponding to any incoming edge. Restructure the
1094 try_finally node for this special case. */
1095
1096static void
726a989a
RB
1097lower_try_finally_nofallthru (struct leh_state *state,
1098 struct leh_tf_state *tf)
6de9cd9a 1099{
8d686507 1100 tree lab;
538dd0b7
DM
1101 gimple x;
1102 geh_else *eh_else;
726a989a 1103 gimple_seq finally;
6de9cd9a
DN
1104 struct goto_queue_node *q, *qe;
1105
1d65f45c 1106 lab = create_artificial_label (gimple_location (tf->try_finally_expr));
6de9cd9a 1107
726a989a
RB
1108 /* We expect that tf->top_p is a GIMPLE_TRY. */
1109 finally = gimple_try_cleanup (tf->top_p);
1110 tf->top_p_seq = gimple_try_eval (tf->top_p);
6de9cd9a 1111
726a989a
RB
1112 x = gimple_build_label (lab);
1113 gimple_seq_add_stmt (&tf->top_p_seq, x);
6de9cd9a 1114
6de9cd9a
DN
1115 q = tf->goto_queue;
1116 qe = q + tf->goto_queue_active;
1117 for (; q < qe; ++q)
1118 if (q->index < 0)
8d686507 1119 do_return_redirection (q, lab, NULL);
6de9cd9a 1120 else
726a989a 1121 do_goto_redirection (q, lab, NULL, tf);
6de9cd9a
DN
1122
1123 replace_goto_queue (tf);
1124
0a35513e
AH
1125 /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1126 eh_else = get_eh_else (finally);
1127 if (eh_else)
1128 {
1129 finally = gimple_eh_else_n_body (eh_else);
355a7673 1130 lower_eh_constructs_1 (state, &finally);
0a35513e 1131 gimple_seq_add_seq (&tf->top_p_seq, finally);
1d65f45c 1132
0a35513e
AH
1133 if (tf->may_throw)
1134 {
1135 finally = gimple_eh_else_e_body (eh_else);
355a7673 1136 lower_eh_constructs_1 (state, &finally);
0a35513e
AH
1137
1138 emit_post_landing_pad (&eh_seq, tf->region);
1139 gimple_seq_add_seq (&eh_seq, finally);
1140 }
1141 }
1142 else
1d65f45c 1143 {
355a7673 1144 lower_eh_constructs_1 (state, &finally);
0a35513e 1145 gimple_seq_add_seq (&tf->top_p_seq, finally);
1d65f45c 1146
0a35513e
AH
1147 if (tf->may_throw)
1148 {
1149 emit_post_landing_pad (&eh_seq, tf->region);
1150
1151 x = gimple_build_goto (lab);
29f5bccb 1152 gimple_set_location (x, gimple_location (tf->try_finally_expr));
0a35513e
AH
1153 gimple_seq_add_stmt (&eh_seq, x);
1154 }
1d65f45c 1155 }
6de9cd9a
DN
1156}
1157
1158/* A subroutine of lower_try_finally. We have determined that there is
1159 exactly one destination of the finally block. Restructure the
1160 try_finally node for this special case. */
1161
1162static void
1163lower_try_finally_onedest (struct leh_state *state, struct leh_tf_state *tf)
1164{
1165 struct goto_queue_node *q, *qe;
538dd0b7
DM
1166 geh_else *eh_else;
1167 glabel *label_stmt;
726a989a
RB
1168 gimple x;
1169 gimple_seq finally;
e368f44f 1170 gimple_stmt_iterator gsi;
726a989a 1171 tree finally_label;
c2255bc4 1172 location_t loc = gimple_location (tf->try_finally_expr);
6de9cd9a 1173
726a989a
RB
1174 finally = gimple_try_cleanup (tf->top_p);
1175 tf->top_p_seq = gimple_try_eval (tf->top_p);
6de9cd9a 1176
0a35513e
AH
1177 /* Since there's only one destination, and the destination edge can only
1178 either be EH or non-EH, that implies that all of our incoming edges
1179 are of the same type. Therefore we can lower EH_ELSE immediately. */
538dd0b7
DM
1180 eh_else = get_eh_else (finally);
1181 if (eh_else)
0a35513e
AH
1182 {
1183 if (tf->may_throw)
538dd0b7 1184 finally = gimple_eh_else_e_body (eh_else);
0a35513e 1185 else
538dd0b7 1186 finally = gimple_eh_else_n_body (eh_else);
0a35513e
AH
1187 }
1188
355a7673 1189 lower_eh_constructs_1 (state, &finally);
6de9cd9a 1190
e368f44f
DC
1191 for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
1192 {
1193 gimple stmt = gsi_stmt (gsi);
1194 if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
1195 {
1196 tree block = gimple_block (stmt);
1197 gimple_set_location (stmt, gimple_location (tf->try_finally_expr));
1198 gimple_set_block (stmt, block);
1199 }
1200 }
1201
6de9cd9a
DN
1202 if (tf->may_throw)
1203 {
1204 /* Only reachable via the exception edge. Add the given label to
1205 the head of the FINALLY block. Append a RESX at the end. */
1d65f45c
RH
1206 emit_post_landing_pad (&eh_seq, tf->region);
1207 gimple_seq_add_seq (&eh_seq, finally);
1208 emit_resx (&eh_seq, tf->region);
6de9cd9a
DN
1209 return;
1210 }
1211
1212 if (tf->may_fallthru)
1213 {
1214 /* Only reachable via the fallthru edge. Do nothing but let
1215 the two blocks run together; we'll fall out the bottom. */
726a989a 1216 gimple_seq_add_seq (&tf->top_p_seq, finally);
6de9cd9a
DN
1217 return;
1218 }
1219
c2255bc4 1220 finally_label = create_artificial_label (loc);
538dd0b7
DM
1221 label_stmt = gimple_build_label (finally_label);
1222 gimple_seq_add_stmt (&tf->top_p_seq, label_stmt);
6de9cd9a 1223
726a989a 1224 gimple_seq_add_seq (&tf->top_p_seq, finally);
6de9cd9a
DN
1225
1226 q = tf->goto_queue;
1227 qe = q + tf->goto_queue_active;
1228
1229 if (tf->may_return)
1230 {
1231 /* Reachable by return expressions only. Redirect them. */
6de9cd9a 1232 for (; q < qe; ++q)
8d686507 1233 do_return_redirection (q, finally_label, NULL);
6de9cd9a
DN
1234 replace_goto_queue (tf);
1235 }
1236 else
1237 {
1238 /* Reachable by goto expressions only. Redirect them. */
1239 for (; q < qe; ++q)
726a989a 1240 do_goto_redirection (q, finally_label, NULL, tf);
6de9cd9a 1241 replace_goto_queue (tf);
19114537 1242
9771b263 1243 if (tf->dest_array[0] == tf->fallthru_label)
6de9cd9a
DN
1244 {
1245 /* Reachable by goto to fallthru label only. Redirect it
1246 to the new label (already created, sadly), and do not
1247 emit the final branch out, or the fallthru label. */
1248 tf->fallthru_label = NULL;
1249 return;
1250 }
1251 }
1252
726a989a
RB
1253 /* Place the original return/goto to the original destination
1254 immediately after the finally block. */
1255 x = tf->goto_queue[0].cont_stmt;
1256 gimple_seq_add_stmt (&tf->top_p_seq, x);
1257 maybe_record_in_goto_queue (state, x);
6de9cd9a
DN
1258}
1259
1260/* A subroutine of lower_try_finally. There are multiple edges incoming
1261 and outgoing from the finally block. Implement this by duplicating the
1262 finally block for every destination. */
1263
1264static void
1265lower_try_finally_copy (struct leh_state *state, struct leh_tf_state *tf)
1266{
726a989a
RB
1267 gimple_seq finally;
1268 gimple_seq new_stmt;
1269 gimple_seq seq;
538dd0b7
DM
1270 gimple x;
1271 geh_else *eh_else;
726a989a 1272 tree tmp;
c2255bc4 1273 location_t tf_loc = gimple_location (tf->try_finally_expr);
6de9cd9a 1274
726a989a 1275 finally = gimple_try_cleanup (tf->top_p);
0a35513e
AH
1276
1277 /* Notice EH_ELSE, and simplify some of the remaining code
1278 by considering FINALLY to be the normal return path only. */
1279 eh_else = get_eh_else (finally);
1280 if (eh_else)
1281 finally = gimple_eh_else_n_body (eh_else);
1282
726a989a
RB
1283 tf->top_p_seq = gimple_try_eval (tf->top_p);
1284 new_stmt = NULL;
6de9cd9a
DN
1285
1286 if (tf->may_fallthru)
1287 {
820055a0 1288 seq = lower_try_finally_dup_block (finally, state, tf_loc);
355a7673 1289 lower_eh_constructs_1 (state, &seq);
726a989a 1290 gimple_seq_add_seq (&new_stmt, seq);
6de9cd9a 1291
726a989a
RB
1292 tmp = lower_try_finally_fallthru_label (tf);
1293 x = gimple_build_goto (tmp);
29f5bccb 1294 gimple_set_location (x, tf_loc);
726a989a 1295 gimple_seq_add_stmt (&new_stmt, x);
6de9cd9a
DN
1296 }
1297
1298 if (tf->may_throw)
1299 {
0a35513e
AH
1300 /* We don't need to copy the EH path of EH_ELSE,
1301 since it is only emitted once. */
1302 if (eh_else)
1303 seq = gimple_eh_else_e_body (eh_else);
1304 else
820055a0 1305 seq = lower_try_finally_dup_block (finally, state, tf_loc);
355a7673 1306 lower_eh_constructs_1 (state, &seq);
6de9cd9a 1307
288f5b2e
RH
1308 emit_post_landing_pad (&eh_seq, tf->region);
1309 gimple_seq_add_seq (&eh_seq, seq);
1d65f45c 1310 emit_resx (&eh_seq, tf->region);
6de9cd9a
DN
1311 }
1312
1313 if (tf->goto_queue)
1314 {
1315 struct goto_queue_node *q, *qe;
dd58eb5a 1316 int return_index, index;
858904db 1317 struct labels_s
dd58eb5a
AO
1318 {
1319 struct goto_queue_node *q;
1320 tree label;
1321 } *labels;
6de9cd9a 1322
9771b263 1323 return_index = tf->dest_array.length ();
858904db 1324 labels = XCNEWVEC (struct labels_s, return_index + 1);
6de9cd9a
DN
1325
1326 q = tf->goto_queue;
1327 qe = q + tf->goto_queue_active;
1328 for (; q < qe; q++)
1329 {
dd58eb5a
AO
1330 index = q->index < 0 ? return_index : q->index;
1331
1332 if (!labels[index].q)
1333 labels[index].q = q;
1334 }
1335
1336 for (index = 0; index < return_index + 1; index++)
1337 {
1338 tree lab;
1339
1340 q = labels[index].q;
1341 if (! q)
1342 continue;
1343
c2255bc4
AH
1344 lab = labels[index].label
1345 = create_artificial_label (tf_loc);
6de9cd9a
DN
1346
1347 if (index == return_index)
8d686507 1348 do_return_redirection (q, lab, NULL);
6de9cd9a 1349 else
726a989a 1350 do_goto_redirection (q, lab, NULL, tf);
6de9cd9a 1351
726a989a
RB
1352 x = gimple_build_label (lab);
1353 gimple_seq_add_stmt (&new_stmt, x);
6de9cd9a 1354
820055a0 1355 seq = lower_try_finally_dup_block (finally, state, q->location);
355a7673 1356 lower_eh_constructs_1 (state, &seq);
726a989a 1357 gimple_seq_add_seq (&new_stmt, seq);
6de9cd9a 1358
726a989a 1359 gimple_seq_add_stmt (&new_stmt, q->cont_stmt);
dd58eb5a 1360 maybe_record_in_goto_queue (state, q->cont_stmt);
6de9cd9a 1361 }
dd58eb5a
AO
1362
1363 for (q = tf->goto_queue; q < qe; q++)
1364 {
1365 tree lab;
1366
1367 index = q->index < 0 ? return_index : q->index;
1368
1369 if (labels[index].q == q)
1370 continue;
1371
1372 lab = labels[index].label;
1373
1374 if (index == return_index)
8d686507 1375 do_return_redirection (q, lab, NULL);
dd58eb5a 1376 else
726a989a 1377 do_goto_redirection (q, lab, NULL, tf);
dd58eb5a 1378 }
1d65f45c 1379
6de9cd9a
DN
1380 replace_goto_queue (tf);
1381 free (labels);
1382 }
1383
1384 /* Need to link new stmts after running replace_goto_queue due
1385 to not wanting to process the same goto stmts twice. */
726a989a 1386 gimple_seq_add_seq (&tf->top_p_seq, new_stmt);
6de9cd9a
DN
1387}
1388
1389/* A subroutine of lower_try_finally. There are multiple edges incoming
1390 and outgoing from the finally block. Implement this by instrumenting
1391 each incoming edge and creating a switch statement at the end of the
1392 finally block that branches to the appropriate destination. */
1393
1394static void
1395lower_try_finally_switch (struct leh_state *state, struct leh_tf_state *tf)
1396{
1397 struct goto_queue_node *q, *qe;
726a989a 1398 tree finally_tmp, finally_label;
6de9cd9a
DN
1399 int return_index, eh_index, fallthru_index;
1400 int nlabels, ndests, j, last_case_index;
726a989a 1401 tree last_case;
9771b263 1402 vec<tree> case_label_vec;
355a7673 1403 gimple_seq switch_body = NULL;
538dd0b7
DM
1404 gimple x;
1405 geh_else *eh_else;
726a989a
RB
1406 tree tmp;
1407 gimple switch_stmt;
1408 gimple_seq finally;
b787e7a2 1409 hash_map<tree, gimple> *cont_map = NULL;
c2255bc4 1410 /* The location of the TRY_FINALLY stmt. */
d40eb158 1411 location_t tf_loc = gimple_location (tf->try_finally_expr);
c2255bc4
AH
1412 /* The location of the finally block. */
1413 location_t finally_loc;
726a989a 1414
0a35513e
AH
1415 finally = gimple_try_cleanup (tf->top_p);
1416 eh_else = get_eh_else (finally);
6de9cd9a
DN
1417
1418 /* Mash the TRY block to the head of the chain. */
726a989a 1419 tf->top_p_seq = gimple_try_eval (tf->top_p);
6de9cd9a 1420
c2255bc4
AH
1421 /* The location of the finally is either the last stmt in the finally
1422 block or the location of the TRY_FINALLY itself. */
0118b919
EB
1423 x = gimple_seq_last_stmt (finally);
1424 finally_loc = x ? gimple_location (x) : tf_loc;
c2255bc4 1425
6de9cd9a 1426 /* Prepare for switch statement generation. */
9771b263 1427 nlabels = tf->dest_array.length ();
6de9cd9a
DN
1428 return_index = nlabels;
1429 eh_index = return_index + tf->may_return;
0a35513e 1430 fallthru_index = eh_index + (tf->may_throw && !eh_else);
6de9cd9a
DN
1431 ndests = fallthru_index + tf->may_fallthru;
1432
1433 finally_tmp = create_tmp_var (integer_type_node, "finally_tmp");
c2255bc4 1434 finally_label = create_artificial_label (finally_loc);
6de9cd9a 1435
9771b263 1436 /* We use vec::quick_push on case_label_vec throughout this function,
726a989a
RB
1437 since we know the size in advance and allocate precisely as muce
1438 space as needed. */
9771b263 1439 case_label_vec.create (ndests);
6de9cd9a
DN
1440 last_case = NULL;
1441 last_case_index = 0;
1442
1443 /* Begin inserting code for getting to the finally block. Things
1444 are done in this order to correspond to the sequence the code is
073a8998 1445 laid out. */
6de9cd9a
DN
1446
1447 if (tf->may_fallthru)
1448 {
1d65f45c 1449 x = gimple_build_assign (finally_tmp,
413581ba
RG
1450 build_int_cst (integer_type_node,
1451 fallthru_index));
726a989a 1452 gimple_seq_add_stmt (&tf->top_p_seq, x);
6de9cd9a 1453
3d528853
NF
1454 tmp = build_int_cst (integer_type_node, fallthru_index);
1455 last_case = build_case_label (tmp, NULL,
1456 create_artificial_label (tf_loc));
9771b263 1457 case_label_vec.quick_push (last_case);
6de9cd9a
DN
1458 last_case_index++;
1459
726a989a
RB
1460 x = gimple_build_label (CASE_LABEL (last_case));
1461 gimple_seq_add_stmt (&switch_body, x);
6de9cd9a 1462
726a989a
RB
1463 tmp = lower_try_finally_fallthru_label (tf);
1464 x = gimple_build_goto (tmp);
29f5bccb 1465 gimple_set_location (x, tf_loc);
726a989a 1466 gimple_seq_add_stmt (&switch_body, x);
6de9cd9a
DN
1467 }
1468
0a35513e
AH
1469 /* For EH_ELSE, emit the exception path (plus resx) now, then
1470 subsequently we only need consider the normal path. */
1471 if (eh_else)
1472 {
1473 if (tf->may_throw)
1474 {
1475 finally = gimple_eh_else_e_body (eh_else);
355a7673 1476 lower_eh_constructs_1 (state, &finally);
0a35513e
AH
1477
1478 emit_post_landing_pad (&eh_seq, tf->region);
1479 gimple_seq_add_seq (&eh_seq, finally);
1480 emit_resx (&eh_seq, tf->region);
1481 }
1482
1483 finally = gimple_eh_else_n_body (eh_else);
1484 }
1485 else if (tf->may_throw)
6de9cd9a 1486 {
1d65f45c 1487 emit_post_landing_pad (&eh_seq, tf->region);
6de9cd9a 1488
1d65f45c 1489 x = gimple_build_assign (finally_tmp,
413581ba 1490 build_int_cst (integer_type_node, eh_index));
1d65f45c
RH
1491 gimple_seq_add_stmt (&eh_seq, x);
1492
1493 x = gimple_build_goto (finally_label);
29f5bccb 1494 gimple_set_location (x, tf_loc);
1d65f45c 1495 gimple_seq_add_stmt (&eh_seq, x);
6de9cd9a 1496
3d528853
NF
1497 tmp = build_int_cst (integer_type_node, eh_index);
1498 last_case = build_case_label (tmp, NULL,
1499 create_artificial_label (tf_loc));
9771b263 1500 case_label_vec.quick_push (last_case);
6de9cd9a
DN
1501 last_case_index++;
1502
726a989a 1503 x = gimple_build_label (CASE_LABEL (last_case));
1d65f45c
RH
1504 gimple_seq_add_stmt (&eh_seq, x);
1505 emit_resx (&eh_seq, tf->region);
6de9cd9a
DN
1506 }
1507
726a989a
RB
1508 x = gimple_build_label (finally_label);
1509 gimple_seq_add_stmt (&tf->top_p_seq, x);
6de9cd9a 1510
efa7882f 1511 lower_eh_constructs_1 (state, &finally);
726a989a 1512 gimple_seq_add_seq (&tf->top_p_seq, finally);
6de9cd9a
DN
1513
1514 /* Redirect each incoming goto edge. */
1515 q = tf->goto_queue;
1516 qe = q + tf->goto_queue_active;
1517 j = last_case_index + tf->may_return;
726a989a
RB
1518 /* Prepare the assignments to finally_tmp that are executed upon the
1519 entrance through a particular edge. */
6de9cd9a
DN
1520 for (; q < qe; ++q)
1521 {
355a7673 1522 gimple_seq mod = NULL;
726a989a
RB
1523 int switch_id;
1524 unsigned int case_index;
1525
6de9cd9a
DN
1526 if (q->index < 0)
1527 {
726a989a 1528 x = gimple_build_assign (finally_tmp,
413581ba
RG
1529 build_int_cst (integer_type_node,
1530 return_index));
726a989a 1531 gimple_seq_add_stmt (&mod, x);
8d686507 1532 do_return_redirection (q, finally_label, mod);
6de9cd9a
DN
1533 switch_id = return_index;
1534 }
1535 else
1536 {
726a989a 1537 x = gimple_build_assign (finally_tmp,
413581ba 1538 build_int_cst (integer_type_node, q->index));
726a989a
RB
1539 gimple_seq_add_stmt (&mod, x);
1540 do_goto_redirection (q, finally_label, mod, tf);
6de9cd9a
DN
1541 switch_id = q->index;
1542 }
1543
1544 case_index = j + q->index;
9771b263 1545 if (case_label_vec.length () <= case_index || !case_label_vec[case_index])
726a989a
RB
1546 {
1547 tree case_lab;
3d528853
NF
1548 tmp = build_int_cst (integer_type_node, switch_id);
1549 case_lab = build_case_label (tmp, NULL,
1550 create_artificial_label (tf_loc));
726a989a 1551 /* We store the cont_stmt in the pointer map, so that we can recover
ffa03772 1552 it in the loop below. */
726a989a 1553 if (!cont_map)
b787e7a2
TS
1554 cont_map = new hash_map<tree, gimple>;
1555 cont_map->put (case_lab, q->cont_stmt);
9771b263 1556 case_label_vec.quick_push (case_lab);
726a989a 1557 }
dd58eb5a
AO
1558 }
1559 for (j = last_case_index; j < last_case_index + nlabels; j++)
1560 {
726a989a 1561 gimple cont_stmt;
dd58eb5a 1562
9771b263 1563 last_case = case_label_vec[j];
dd58eb5a
AO
1564
1565 gcc_assert (last_case);
726a989a 1566 gcc_assert (cont_map);
dd58eb5a 1567
b787e7a2 1568 cont_stmt = *cont_map->get (last_case);
dd58eb5a 1569
ffa03772 1570 x = gimple_build_label (CASE_LABEL (last_case));
726a989a
RB
1571 gimple_seq_add_stmt (&switch_body, x);
1572 gimple_seq_add_stmt (&switch_body, cont_stmt);
dd58eb5a 1573 maybe_record_in_goto_queue (state, cont_stmt);
6de9cd9a 1574 }
726a989a 1575 if (cont_map)
b787e7a2 1576 delete cont_map;
726a989a 1577
6de9cd9a 1578 replace_goto_queue (tf);
6de9cd9a 1579
0f1f6967
SB
1580 /* Make sure that the last case is the default label, as one is required.
1581 Then sort the labels, which is also required in GIMPLE. */
6de9cd9a 1582 CASE_LOW (last_case) = NULL;
e9ff9caf
RB
1583 tree tem = case_label_vec.pop ();
1584 gcc_assert (tem == last_case);
0f1f6967 1585 sort_case_labels (case_label_vec);
6de9cd9a 1586
726a989a
RB
1587 /* Build the switch statement, setting last_case to be the default
1588 label. */
fd8d363e
SB
1589 switch_stmt = gimple_build_switch (finally_tmp, last_case,
1590 case_label_vec);
c2255bc4 1591 gimple_set_location (switch_stmt, finally_loc);
726a989a
RB
1592
1593 /* Need to link SWITCH_STMT after running replace_goto_queue
1594 due to not wanting to process the same goto stmts twice. */
1595 gimple_seq_add_stmt (&tf->top_p_seq, switch_stmt);
1596 gimple_seq_add_seq (&tf->top_p_seq, switch_body);
6de9cd9a
DN
1597}
1598
1599/* Decide whether or not we are going to duplicate the finally block.
1600 There are several considerations.
1601
1602 First, if this is Java, then the finally block contains code
1603 written by the user. It has line numbers associated with it,
1604 so duplicating the block means it's difficult to set a breakpoint.
1605 Since controlling code generation via -g is verboten, we simply
1606 never duplicate code without optimization.
1607
1608 Second, we'd like to prevent egregious code growth. One way to
1609 do this is to estimate the size of the finally block, multiply
1610 that by the number of copies we'd need to make, and compare against
1611 the estimate of the size of the switch machinery we'd have to add. */
1612
1613static bool
0a35513e 1614decide_copy_try_finally (int ndests, bool may_throw, gimple_seq finally)
6de9cd9a
DN
1615{
1616 int f_estimate, sw_estimate;
538dd0b7 1617 geh_else *eh_else;
0a35513e
AH
1618
1619 /* If there's an EH_ELSE involved, the exception path is separate
1620 and really doesn't come into play for this computation. */
1621 eh_else = get_eh_else (finally);
1622 if (eh_else)
1623 {
1624 ndests -= may_throw;
1625 finally = gimple_eh_else_n_body (eh_else);
1626 }
6de9cd9a
DN
1627
1628 if (!optimize)
bccc50d4
JJ
1629 {
1630 gimple_stmt_iterator gsi;
1631
1632 if (ndests == 1)
1633 return true;
1634
1635 for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
1636 {
1637 gimple stmt = gsi_stmt (gsi);
1638 if (!is_gimple_debug (stmt) && !gimple_clobber_p (stmt))
1639 return false;
1640 }
1641 return true;
1642 }
6de9cd9a
DN
1643
1644 /* Finally estimate N times, plus N gotos. */
726a989a 1645 f_estimate = count_insns_seq (finally, &eni_size_weights);
6de9cd9a
DN
1646 f_estimate = (f_estimate + 1) * ndests;
1647
1648 /* Switch statement (cost 10), N variable assignments, N gotos. */
1649 sw_estimate = 10 + 2 * ndests;
1650
1651 /* Optimize for size clearly wants our best guess. */
efd8f750 1652 if (optimize_function_for_size_p (cfun))
6de9cd9a
DN
1653 return f_estimate < sw_estimate;
1654
1655 /* ??? These numbers are completely made up so far. */
1656 if (optimize > 1)
7465ed07 1657 return f_estimate < 100 || f_estimate < sw_estimate * 2;
6de9cd9a 1658 else
7465ed07 1659 return f_estimate < 40 || f_estimate * 2 < sw_estimate * 3;
6de9cd9a
DN
1660}
1661
d3f28910
JM
1662/* REG is the enclosing region for a possible cleanup region, or the region
1663 itself. Returns TRUE if such a region would be unreachable.
1664
1665 Cleanup regions within a must-not-throw region aren't actually reachable
1666 even if there are throwing stmts within them, because the personality
1667 routine will call terminate before unwinding. */
1668
1669static bool
1670cleanup_is_dead_in (eh_region reg)
1671{
1672 while (reg && reg->type == ERT_CLEANUP)
1673 reg = reg->outer;
1674 return (reg && reg->type == ERT_MUST_NOT_THROW);
1675}
726a989a
RB
1676
1677/* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_FINALLY nodes
6de9cd9a 1678 to a sequence of labels and blocks, plus the exception region trees
19114537 1679 that record all the magic. This is complicated by the need to
6de9cd9a
DN
1680 arrange for the FINALLY block to be executed on all exits. */
1681
726a989a 1682static gimple_seq
538dd0b7 1683lower_try_finally (struct leh_state *state, gtry *tp)
6de9cd9a
DN
1684{
1685 struct leh_tf_state this_tf;
1686 struct leh_state this_state;
1687 int ndests;
e19d1f06 1688 gimple_seq old_eh_seq;
6de9cd9a
DN
1689
1690 /* Process the try block. */
1691
1692 memset (&this_tf, 0, sizeof (this_tf));
726a989a 1693 this_tf.try_finally_expr = tp;
6de9cd9a
DN
1694 this_tf.top_p = tp;
1695 this_tf.outer = state;
481d1b81 1696 if (using_eh_for_cleanups_p () && !cleanup_is_dead_in (state->cur_region))
d3f28910
JM
1697 {
1698 this_tf.region = gen_eh_region_cleanup (state->cur_region);
1699 this_state.cur_region = this_tf.region;
1700 }
6de9cd9a 1701 else
d3f28910
JM
1702 {
1703 this_tf.region = NULL;
1704 this_state.cur_region = state->cur_region;
1705 }
6de9cd9a 1706
1d65f45c 1707 this_state.ehp_region = state->ehp_region;
6de9cd9a
DN
1708 this_state.tf = &this_tf;
1709
e19d1f06
RH
1710 old_eh_seq = eh_seq;
1711 eh_seq = NULL;
1712
355a7673 1713 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
6de9cd9a
DN
1714
1715 /* Determine if the try block is escaped through the bottom. */
726a989a 1716 this_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
6de9cd9a
DN
1717
1718 /* Determine if any exceptions are possible within the try block. */
d3f28910 1719 if (this_tf.region)
b7da9fd4 1720 this_tf.may_throw = eh_region_may_contain_throw (this_tf.region);
6de9cd9a 1721 if (this_tf.may_throw)
1d65f45c 1722 honor_protect_cleanup_actions (state, &this_state, &this_tf);
6de9cd9a 1723
6de9cd9a
DN
1724 /* Determine how many edges (still) reach the finally block. Or rather,
1725 how many destinations are reached by the finally block. Use this to
1726 determine how we process the finally block itself. */
1727
9771b263 1728 ndests = this_tf.dest_array.length ();
6de9cd9a
DN
1729 ndests += this_tf.may_fallthru;
1730 ndests += this_tf.may_return;
1731 ndests += this_tf.may_throw;
1732
1733 /* If the FINALLY block is not reachable, dike it out. */
1734 if (ndests == 0)
726a989a
RB
1735 {
1736 gimple_seq_add_seq (&this_tf.top_p_seq, gimple_try_eval (tp));
1737 gimple_try_set_cleanup (tp, NULL);
1738 }
6de9cd9a
DN
1739 /* If the finally block doesn't fall through, then any destination
1740 we might try to impose there isn't reached either. There may be
1741 some minor amount of cleanup and redirection still needed. */
726a989a 1742 else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp)))
6de9cd9a
DN
1743 lower_try_finally_nofallthru (state, &this_tf);
1744
1745 /* We can easily special-case redirection to a single destination. */
1746 else if (ndests == 1)
1747 lower_try_finally_onedest (state, &this_tf);
0a35513e
AH
1748 else if (decide_copy_try_finally (ndests, this_tf.may_throw,
1749 gimple_try_cleanup (tp)))
6de9cd9a
DN
1750 lower_try_finally_copy (state, &this_tf);
1751 else
1752 lower_try_finally_switch (state, &this_tf);
1753
1754 /* If someone requested we add a label at the end of the transformed
1755 block, do so. */
1756 if (this_tf.fallthru_label)
1757 {
726a989a
RB
1758 /* This must be reached only if ndests == 0. */
1759 gimple x = gimple_build_label (this_tf.fallthru_label);
1760 gimple_seq_add_stmt (&this_tf.top_p_seq, x);
6de9cd9a
DN
1761 }
1762
9771b263 1763 this_tf.dest_array.release ();
04695783 1764 free (this_tf.goto_queue);
0f547d3d 1765 if (this_tf.goto_queue_map)
b787e7a2 1766 delete this_tf.goto_queue_map;
726a989a 1767
e19d1f06
RH
1768 /* If there was an old (aka outer) eh_seq, append the current eh_seq.
1769 If there was no old eh_seq, then the append is trivially already done. */
1770 if (old_eh_seq)
1771 {
1772 if (eh_seq == NULL)
1773 eh_seq = old_eh_seq;
1774 else
1775 {
1776 gimple_seq new_eh_seq = eh_seq;
1777 eh_seq = old_eh_seq;
c3284718 1778 gimple_seq_add_seq (&eh_seq, new_eh_seq);
e19d1f06
RH
1779 }
1780 }
1781
726a989a 1782 return this_tf.top_p_seq;
6de9cd9a
DN
1783}
1784
726a989a
RB
1785/* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY_CATCH with a
1786 list of GIMPLE_CATCH to a sequence of labels and blocks, plus the
1787 exception region trees that records all the magic. */
6de9cd9a 1788
726a989a 1789static gimple_seq
538dd0b7 1790lower_catch (struct leh_state *state, gtry *tp)
6de9cd9a 1791{
b7da9fd4
RH
1792 eh_region try_region = NULL;
1793 struct leh_state this_state = *state;
726a989a 1794 gimple_stmt_iterator gsi;
6de9cd9a 1795 tree out_label;
355a7673 1796 gimple_seq new_seq, cleanup;
1d65f45c 1797 gimple x;
c2255bc4 1798 location_t try_catch_loc = gimple_location (tp);
6de9cd9a 1799
b7da9fd4
RH
1800 if (flag_exceptions)
1801 {
1802 try_region = gen_eh_region_try (state->cur_region);
1803 this_state.cur_region = try_region;
1804 }
6de9cd9a 1805
355a7673 1806 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
6de9cd9a 1807
b7da9fd4 1808 if (!eh_region_may_contain_throw (try_region))
1d65f45c
RH
1809 return gimple_try_eval (tp);
1810
1811 new_seq = NULL;
1812 emit_eh_dispatch (&new_seq, try_region);
1813 emit_resx (&new_seq, try_region);
1814
1815 this_state.cur_region = state->cur_region;
1816 this_state.ehp_region = try_region;
6de9cd9a 1817
3b445b24
JJ
1818 /* Add eh_seq from lowering EH in the cleanup sequence after the cleanup
1819 itself, so that e.g. for coverage purposes the nested cleanups don't
1820 appear before the cleanup body. See PR64634 for details. */
1821 gimple_seq old_eh_seq = eh_seq;
1822 eh_seq = NULL;
1823
6de9cd9a 1824 out_label = NULL;
355a7673
MM
1825 cleanup = gimple_try_cleanup (tp);
1826 for (gsi = gsi_start (cleanup);
1d65f45c
RH
1827 !gsi_end_p (gsi);
1828 gsi_next (&gsi))
6de9cd9a 1829 {
1d65f45c 1830 eh_catch c;
538dd0b7 1831 gcatch *catch_stmt;
1d65f45c 1832 gimple_seq handler;
6de9cd9a 1833
538dd0b7
DM
1834 catch_stmt = as_a <gcatch *> (gsi_stmt (gsi));
1835 c = gen_eh_region_catch (try_region, gimple_catch_types (catch_stmt));
6de9cd9a 1836
538dd0b7 1837 handler = gimple_catch_handler (catch_stmt);
355a7673 1838 lower_eh_constructs_1 (&this_state, &handler);
6de9cd9a 1839
1d65f45c
RH
1840 c->label = create_artificial_label (UNKNOWN_LOCATION);
1841 x = gimple_build_label (c->label);
1842 gimple_seq_add_stmt (&new_seq, x);
6de9cd9a 1843
1d65f45c 1844 gimple_seq_add_seq (&new_seq, handler);
6de9cd9a 1845
1d65f45c 1846 if (gimple_seq_may_fallthru (new_seq))
6de9cd9a
DN
1847 {
1848 if (!out_label)
c2255bc4 1849 out_label = create_artificial_label (try_catch_loc);
6de9cd9a 1850
726a989a 1851 x = gimple_build_goto (out_label);
1d65f45c 1852 gimple_seq_add_stmt (&new_seq, x);
6de9cd9a 1853 }
d815d34e
MM
1854 if (!c->type_list)
1855 break;
6de9cd9a
DN
1856 }
1857
1d65f45c
RH
1858 gimple_try_set_cleanup (tp, new_seq);
1859
3b445b24
JJ
1860 gimple_seq new_eh_seq = eh_seq;
1861 eh_seq = old_eh_seq;
1862 gimple_seq ret_seq = frob_into_branch_around (tp, try_region, out_label);
1863 gimple_seq_add_seq (&eh_seq, new_eh_seq);
1864 return ret_seq;
6de9cd9a
DN
1865}
1866
726a989a
RB
1867/* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with a
1868 GIMPLE_EH_FILTER to a sequence of labels and blocks, plus the exception
6de9cd9a
DN
1869 region trees that record all the magic. */
1870
726a989a 1871static gimple_seq
538dd0b7 1872lower_eh_filter (struct leh_state *state, gtry *tp)
6de9cd9a 1873{
b7da9fd4
RH
1874 struct leh_state this_state = *state;
1875 eh_region this_region = NULL;
1d65f45c
RH
1876 gimple inner, x;
1877 gimple_seq new_seq;
19114537 1878
726a989a
RB
1879 inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1880
b7da9fd4
RH
1881 if (flag_exceptions)
1882 {
1883 this_region = gen_eh_region_allowed (state->cur_region,
1884 gimple_eh_filter_types (inner));
1885 this_state.cur_region = this_region;
1886 }
19114537 1887
355a7673 1888 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
6de9cd9a 1889
b7da9fd4 1890 if (!eh_region_may_contain_throw (this_region))
1d65f45c
RH
1891 return gimple_try_eval (tp);
1892
1893 new_seq = NULL;
1894 this_state.cur_region = state->cur_region;
1895 this_state.ehp_region = this_region;
1896
1897 emit_eh_dispatch (&new_seq, this_region);
1898 emit_resx (&new_seq, this_region);
1899
1900 this_region->u.allowed.label = create_artificial_label (UNKNOWN_LOCATION);
1901 x = gimple_build_label (this_region->u.allowed.label);
1902 gimple_seq_add_stmt (&new_seq, x);
1903
355a7673 1904 lower_eh_constructs_1 (&this_state, gimple_eh_filter_failure_ptr (inner));
1d65f45c
RH
1905 gimple_seq_add_seq (&new_seq, gimple_eh_filter_failure (inner));
1906
1907 gimple_try_set_cleanup (tp, new_seq);
6de9cd9a 1908
1d65f45c
RH
1909 return frob_into_branch_around (tp, this_region, NULL);
1910}
1911
1912/* A subroutine of lower_eh_constructs_1. Lower a GIMPLE_TRY with
1913 an GIMPLE_EH_MUST_NOT_THROW to a sequence of labels and blocks,
1914 plus the exception region trees that record all the magic. */
1915
1916static gimple_seq
538dd0b7 1917lower_eh_must_not_throw (struct leh_state *state, gtry *tp)
1d65f45c 1918{
b7da9fd4 1919 struct leh_state this_state = *state;
1d65f45c 1920
b7da9fd4
RH
1921 if (flag_exceptions)
1922 {
1923 gimple inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1924 eh_region this_region;
1d65f45c 1925
b7da9fd4
RH
1926 this_region = gen_eh_region_must_not_throw (state->cur_region);
1927 this_region->u.must_not_throw.failure_decl
538dd0b7
DM
1928 = gimple_eh_must_not_throw_fndecl (
1929 as_a <geh_mnt *> (inner));
c16fd676
RB
1930 this_region->u.must_not_throw.failure_loc
1931 = LOCATION_LOCUS (gimple_location (tp));
1d65f45c 1932
b7da9fd4
RH
1933 /* In order to get mangling applied to this decl, we must mark it
1934 used now. Otherwise, pass_ipa_free_lang_data won't think it
1935 needs to happen. */
1936 TREE_USED (this_region->u.must_not_throw.failure_decl) = 1;
1d65f45c 1937
b7da9fd4
RH
1938 this_state.cur_region = this_region;
1939 }
6de9cd9a 1940
355a7673 1941 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
6de9cd9a 1942
1d65f45c 1943 return gimple_try_eval (tp);
6de9cd9a
DN
1944}
1945
1946/* Implement a cleanup expression. This is similar to try-finally,
1947 except that we only execute the cleanup block for exception edges. */
1948
726a989a 1949static gimple_seq
538dd0b7 1950lower_cleanup (struct leh_state *state, gtry *tp)
6de9cd9a 1951{
b7da9fd4
RH
1952 struct leh_state this_state = *state;
1953 eh_region this_region = NULL;
6de9cd9a 1954 struct leh_tf_state fake_tf;
726a989a 1955 gimple_seq result;
d3f28910 1956 bool cleanup_dead = cleanup_is_dead_in (state->cur_region);
6de9cd9a 1957
d3f28910 1958 if (flag_exceptions && !cleanup_dead)
6de9cd9a 1959 {
b7da9fd4
RH
1960 this_region = gen_eh_region_cleanup (state->cur_region);
1961 this_state.cur_region = this_region;
6de9cd9a
DN
1962 }
1963
355a7673 1964 lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
6de9cd9a 1965
d3f28910 1966 if (cleanup_dead || !eh_region_may_contain_throw (this_region))
1d65f45c 1967 return gimple_try_eval (tp);
6de9cd9a
DN
1968
1969 /* Build enough of a try-finally state so that we can reuse
1970 honor_protect_cleanup_actions. */
1971 memset (&fake_tf, 0, sizeof (fake_tf));
c2255bc4 1972 fake_tf.top_p = fake_tf.try_finally_expr = tp;
6de9cd9a
DN
1973 fake_tf.outer = state;
1974 fake_tf.region = this_region;
726a989a 1975 fake_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
6de9cd9a
DN
1976 fake_tf.may_throw = true;
1977
6de9cd9a
DN
1978 honor_protect_cleanup_actions (state, NULL, &fake_tf);
1979
1980 if (fake_tf.may_throw)
1981 {
1982 /* In this case honor_protect_cleanup_actions had nothing to do,
1983 and we should process this normally. */
355a7673 1984 lower_eh_constructs_1 (state, gimple_try_cleanup_ptr (tp));
1d65f45c
RH
1985 result = frob_into_branch_around (tp, this_region,
1986 fake_tf.fallthru_label);
6de9cd9a
DN
1987 }
1988 else
1989 {
1990 /* In this case honor_protect_cleanup_actions did nearly all of
1991 the work. All we have left is to append the fallthru_label. */
1992
726a989a 1993 result = gimple_try_eval (tp);
6de9cd9a
DN
1994 if (fake_tf.fallthru_label)
1995 {
726a989a
RB
1996 gimple x = gimple_build_label (fake_tf.fallthru_label);
1997 gimple_seq_add_stmt (&result, x);
6de9cd9a
DN
1998 }
1999 }
726a989a 2000 return result;
6de9cd9a
DN
2001}
2002
1d65f45c 2003/* Main loop for lowering eh constructs. Also moves gsi to the next
726a989a 2004 statement. */
6de9cd9a
DN
2005
2006static void
726a989a 2007lower_eh_constructs_2 (struct leh_state *state, gimple_stmt_iterator *gsi)
6de9cd9a 2008{
726a989a
RB
2009 gimple_seq replace;
2010 gimple x;
2011 gimple stmt = gsi_stmt (*gsi);
6de9cd9a 2012
726a989a 2013 switch (gimple_code (stmt))
6de9cd9a 2014 {
726a989a 2015 case GIMPLE_CALL:
1d65f45c
RH
2016 {
2017 tree fndecl = gimple_call_fndecl (stmt);
2018 tree rhs, lhs;
2019
2020 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
2021 switch (DECL_FUNCTION_CODE (fndecl))
2022 {
2023 case BUILT_IN_EH_POINTER:
2024 /* The front end may have generated a call to
2025 __builtin_eh_pointer (0) within a catch region. Replace
2026 this zero argument with the current catch region number. */
2027 if (state->ehp_region)
2028 {
413581ba
RG
2029 tree nr = build_int_cst (integer_type_node,
2030 state->ehp_region->index);
1d65f45c
RH
2031 gimple_call_set_arg (stmt, 0, nr);
2032 }
2033 else
2034 {
2035 /* The user has dome something silly. Remove it. */
9a9d280e 2036 rhs = null_pointer_node;
1d65f45c
RH
2037 goto do_replace;
2038 }
2039 break;
2040
2041 case BUILT_IN_EH_FILTER:
2042 /* ??? This should never appear, but since it's a builtin it
2043 is accessible to abuse by users. Just remove it and
2044 replace the use with the arbitrary value zero. */
2045 rhs = build_int_cst (TREE_TYPE (TREE_TYPE (fndecl)), 0);
2046 do_replace:
2047 lhs = gimple_call_lhs (stmt);
2048 x = gimple_build_assign (lhs, rhs);
2049 gsi_insert_before (gsi, x, GSI_SAME_STMT);
2050 /* FALLTHRU */
2051
2052 case BUILT_IN_EH_COPY_VALUES:
2053 /* Likewise this should not appear. Remove it. */
2054 gsi_remove (gsi, true);
2055 return;
2056
2057 default:
2058 break;
2059 }
2060 }
2061 /* FALLTHRU */
2062
726a989a 2063 case GIMPLE_ASSIGN:
ba4d8f9d
RG
2064 /* If the stmt can throw use a new temporary for the assignment
2065 to a LHS. This makes sure the old value of the LHS is
87cd4259 2066 available on the EH edge. Only do so for statements that
073a8998 2067 potentially fall through (no noreturn calls e.g.), otherwise
87cd4259 2068 this new assignment might create fake fallthru regions. */
ba4d8f9d
RG
2069 if (stmt_could_throw_p (stmt)
2070 && gimple_has_lhs (stmt)
87cd4259 2071 && gimple_stmt_may_fallthru (stmt)
ba4d8f9d
RG
2072 && !tree_could_throw_p (gimple_get_lhs (stmt))
2073 && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
2074 {
2075 tree lhs = gimple_get_lhs (stmt);
b731b390 2076 tree tmp = create_tmp_var (TREE_TYPE (lhs));
ba4d8f9d
RG
2077 gimple s = gimple_build_assign (lhs, tmp);
2078 gimple_set_location (s, gimple_location (stmt));
2079 gimple_set_block (s, gimple_block (stmt));
2080 gimple_set_lhs (stmt, tmp);
2081 if (TREE_CODE (TREE_TYPE (tmp)) == COMPLEX_TYPE
2082 || TREE_CODE (TREE_TYPE (tmp)) == VECTOR_TYPE)
2083 DECL_GIMPLE_REG_P (tmp) = 1;
2084 gsi_insert_after (gsi, s, GSI_SAME_STMT);
2085 }
6de9cd9a 2086 /* Look for things that can throw exceptions, and record them. */
726a989a 2087 if (state->cur_region && stmt_could_throw_p (stmt))
6de9cd9a 2088 {
726a989a 2089 record_stmt_eh_region (state->cur_region, stmt);
6de9cd9a 2090 note_eh_region_may_contain_throw (state->cur_region);
6de9cd9a
DN
2091 }
2092 break;
2093
726a989a
RB
2094 case GIMPLE_COND:
2095 case GIMPLE_GOTO:
2096 case GIMPLE_RETURN:
2097 maybe_record_in_goto_queue (state, stmt);
6de9cd9a
DN
2098 break;
2099
726a989a 2100 case GIMPLE_SWITCH:
538dd0b7 2101 verify_norecord_switch_expr (state, as_a <gswitch *> (stmt));
6de9cd9a
DN
2102 break;
2103
726a989a 2104 case GIMPLE_TRY:
538dd0b7
DM
2105 {
2106 gtry *try_stmt = as_a <gtry *> (stmt);
2107 if (gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
2108 replace = lower_try_finally (state, try_stmt);
2109 else
2110 {
2111 x = gimple_seq_first_stmt (gimple_try_cleanup (try_stmt));
2112 if (!x)
6728ee79 2113 {
538dd0b7
DM
2114 replace = gimple_try_eval (try_stmt);
2115 lower_eh_constructs_1 (state, &replace);
2116 }
2117 else
2118 switch (gimple_code (x))
2119 {
6728ee79 2120 case GIMPLE_CATCH:
538dd0b7
DM
2121 replace = lower_catch (state, try_stmt);
2122 break;
6728ee79 2123 case GIMPLE_EH_FILTER:
538dd0b7
DM
2124 replace = lower_eh_filter (state, try_stmt);
2125 break;
6728ee79 2126 case GIMPLE_EH_MUST_NOT_THROW:
538dd0b7
DM
2127 replace = lower_eh_must_not_throw (state, try_stmt);
2128 break;
0a35513e 2129 case GIMPLE_EH_ELSE:
538dd0b7
DM
2130 /* This code is only valid with GIMPLE_TRY_FINALLY. */
2131 gcc_unreachable ();
6728ee79 2132 default:
538dd0b7
DM
2133 replace = lower_cleanup (state, try_stmt);
2134 break;
2135 }
2136 }
2137 }
726a989a
RB
2138
2139 /* Remove the old stmt and insert the transformed sequence
2140 instead. */
2141 gsi_insert_seq_before (gsi, replace, GSI_SAME_STMT);
2142 gsi_remove (gsi, true);
2143
2144 /* Return since we don't want gsi_next () */
2145 return;
6de9cd9a 2146
0a35513e
AH
2147 case GIMPLE_EH_ELSE:
2148 /* We should be eliminating this in lower_try_finally et al. */
2149 gcc_unreachable ();
2150
6de9cd9a
DN
2151 default:
2152 /* A type, a decl, or some kind of statement that we're not
2153 interested in. Don't walk them. */
2154 break;
2155 }
726a989a
RB
2156
2157 gsi_next (gsi);
2158}
2159
2160/* A helper to unwrap a gimple_seq and feed stmts to lower_eh_constructs_2. */
2161
2162static void
355a7673 2163lower_eh_constructs_1 (struct leh_state *state, gimple_seq *pseq)
726a989a
RB
2164{
2165 gimple_stmt_iterator gsi;
355a7673 2166 for (gsi = gsi_start (*pseq); !gsi_end_p (gsi);)
726a989a 2167 lower_eh_constructs_2 (state, &gsi);
6de9cd9a
DN
2168}
2169
be55bfe6
TS
2170namespace {
2171
2172const pass_data pass_data_lower_eh =
2173{
2174 GIMPLE_PASS, /* type */
2175 "eh", /* name */
2176 OPTGROUP_NONE, /* optinfo_flags */
be55bfe6
TS
2177 TV_TREE_EH, /* tv_id */
2178 PROP_gimple_lcf, /* properties_required */
2179 PROP_gimple_leh, /* properties_provided */
2180 0, /* properties_destroyed */
2181 0, /* todo_flags_start */
2182 0, /* todo_flags_finish */
2183};
2184
2185class pass_lower_eh : public gimple_opt_pass
2186{
2187public:
2188 pass_lower_eh (gcc::context *ctxt)
2189 : gimple_opt_pass (pass_data_lower_eh, ctxt)
2190 {}
2191
2192 /* opt_pass methods: */
2193 virtual unsigned int execute (function *);
2194
2195}; // class pass_lower_eh
2196
2197unsigned int
2198pass_lower_eh::execute (function *fun)
6de9cd9a
DN
2199{
2200 struct leh_state null_state;
1d65f45c 2201 gimple_seq bodyp;
726a989a 2202
1d65f45c
RH
2203 bodyp = gimple_body (current_function_decl);
2204 if (bodyp == NULL)
2205 return 0;
6de9cd9a 2206
c203e8a7 2207 finally_tree = new hash_table<finally_tree_hasher> (31);
b7da9fd4 2208 eh_region_may_contain_throw_map = BITMAP_ALLOC (NULL);
1d65f45c 2209 memset (&null_state, 0, sizeof (null_state));
6de9cd9a 2210
726a989a 2211 collect_finally_tree_1 (bodyp, NULL);
355a7673
MM
2212 lower_eh_constructs_1 (&null_state, &bodyp);
2213 gimple_set_body (current_function_decl, bodyp);
6de9cd9a 2214
1d65f45c
RH
2215 /* We assume there's a return statement, or something, at the end of
2216 the function, and thus ploping the EH sequence afterward won't
2217 change anything. */
2218 gcc_assert (!gimple_seq_may_fallthru (bodyp));
2219 gimple_seq_add_seq (&bodyp, eh_seq);
2220
2221 /* We assume that since BODYP already existed, adding EH_SEQ to it
2222 didn't change its value, and we don't have to re-set the function. */
2223 gcc_assert (bodyp == gimple_body (current_function_decl));
6de9cd9a 2224
c203e8a7
TS
2225 delete finally_tree;
2226 finally_tree = NULL;
b7da9fd4 2227 BITMAP_FREE (eh_region_may_contain_throw_map);
1d65f45c 2228 eh_seq = NULL;
f9417da1
RG
2229
2230 /* If this function needs a language specific EH personality routine
2231 and the frontend didn't already set one do so now. */
be55bfe6 2232 if (function_needs_eh_personality (fun) == eh_personality_lang
f9417da1
RG
2233 && !DECL_FUNCTION_PERSONALITY (current_function_decl))
2234 DECL_FUNCTION_PERSONALITY (current_function_decl)
2235 = lang_hooks.eh_personality ();
2236
c2924966 2237 return 0;
6de9cd9a
DN
2238}
2239
27a4cd48
DM
2240} // anon namespace
2241
2242gimple_opt_pass *
2243make_pass_lower_eh (gcc::context *ctxt)
2244{
2245 return new pass_lower_eh (ctxt);
2246}
6de9cd9a 2247\f
1d65f45c
RH
2248/* Create the multiple edges from an EH_DISPATCH statement to all of
2249 the possible handlers for its EH region. Return true if there's
2250 no fallthru edge; false if there is. */
6de9cd9a 2251
1d65f45c 2252bool
538dd0b7 2253make_eh_dispatch_edges (geh_dispatch *stmt)
6de9cd9a 2254{
1d65f45c
RH
2255 eh_region r;
2256 eh_catch c;
6de9cd9a
DN
2257 basic_block src, dst;
2258
1d65f45c 2259 r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
726a989a 2260 src = gimple_bb (stmt);
6de9cd9a 2261
1d65f45c
RH
2262 switch (r->type)
2263 {
2264 case ERT_TRY:
2265 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
2266 {
2267 dst = label_to_block (c->label);
2268 make_edge (src, dst, 0);
19114537 2269
1d65f45c
RH
2270 /* A catch-all handler doesn't have a fallthru. */
2271 if (c->type_list == NULL)
2272 return false;
2273 }
2274 break;
a8ee227c 2275
1d65f45c
RH
2276 case ERT_ALLOWED_EXCEPTIONS:
2277 dst = label_to_block (r->u.allowed.label);
2278 make_edge (src, dst, 0);
2279 break;
2280
2281 default:
2282 gcc_unreachable ();
2283 }
2284
2285 return true;
a8ee227c
JH
2286}
2287
1d65f45c
RH
2288/* Create the single EH edge from STMT to its nearest landing pad,
2289 if there is such a landing pad within the current function. */
2290
6de9cd9a 2291void
726a989a 2292make_eh_edges (gimple stmt)
6de9cd9a 2293{
1d65f45c
RH
2294 basic_block src, dst;
2295 eh_landing_pad lp;
2296 int lp_nr;
6de9cd9a 2297
1d65f45c
RH
2298 lp_nr = lookup_stmt_eh_lp (stmt);
2299 if (lp_nr <= 0)
2300 return;
6de9cd9a 2301
1d65f45c
RH
2302 lp = get_eh_landing_pad_from_number (lp_nr);
2303 gcc_assert (lp != NULL);
a203a221 2304
1d65f45c
RH
2305 src = gimple_bb (stmt);
2306 dst = label_to_block (lp->post_landing_pad);
2307 make_edge (src, dst, EDGE_EH);
6de9cd9a
DN
2308}
2309
1d65f45c
RH
2310/* Do the work in redirecting EDGE_IN to NEW_BB within the EH region tree;
2311 do not actually perform the final edge redirection.
a3710436 2312
1d65f45c
RH
2313 CHANGE_REGION is true when we're being called from cleanup_empty_eh and
2314 we intend to change the destination EH region as well; this means
2315 EH_LANDING_PAD_NR must already be set on the destination block label.
2316 If false, we're being called from generic cfg manipulation code and we
2317 should preserve our place within the region tree. */
2318
2319static void
2320redirect_eh_edge_1 (edge edge_in, basic_block new_bb, bool change_region)
a3710436 2321{
1d65f45c
RH
2322 eh_landing_pad old_lp, new_lp;
2323 basic_block old_bb;
2324 gimple throw_stmt;
2325 int old_lp_nr, new_lp_nr;
2326 tree old_label, new_label;
2327 edge_iterator ei;
2328 edge e;
2329
2330 old_bb = edge_in->dest;
2331 old_label = gimple_block_label (old_bb);
2332 old_lp_nr = EH_LANDING_PAD_NR (old_label);
2333 gcc_assert (old_lp_nr > 0);
2334 old_lp = get_eh_landing_pad_from_number (old_lp_nr);
2335
2336 throw_stmt = last_stmt (edge_in->src);
2337 gcc_assert (lookup_stmt_eh_lp (throw_stmt) == old_lp_nr);
2338
2339 new_label = gimple_block_label (new_bb);
a3710436 2340
1d65f45c
RH
2341 /* Look for an existing region that might be using NEW_BB already. */
2342 new_lp_nr = EH_LANDING_PAD_NR (new_label);
2343 if (new_lp_nr)
a3710436 2344 {
1d65f45c
RH
2345 new_lp = get_eh_landing_pad_from_number (new_lp_nr);
2346 gcc_assert (new_lp);
b8698a0f 2347
1d65f45c
RH
2348 /* Unless CHANGE_REGION is true, the new and old landing pad
2349 had better be associated with the same EH region. */
2350 gcc_assert (change_region || new_lp->region == old_lp->region);
a3710436
JH
2351 }
2352 else
2353 {
1d65f45c
RH
2354 new_lp = NULL;
2355 gcc_assert (!change_region);
a3710436
JH
2356 }
2357
1d65f45c
RH
2358 /* Notice when we redirect the last EH edge away from OLD_BB. */
2359 FOR_EACH_EDGE (e, ei, old_bb->preds)
2360 if (e != edge_in && (e->flags & EDGE_EH))
2361 break;
cc7220fd 2362
1d65f45c 2363 if (new_lp)
cc7220fd 2364 {
1d65f45c
RH
2365 /* NEW_LP already exists. If there are still edges into OLD_LP,
2366 there's nothing to do with the EH tree. If there are no more
2367 edges into OLD_LP, then we want to remove OLD_LP as it is unused.
2368 If CHANGE_REGION is true, then our caller is expecting to remove
2369 the landing pad. */
2370 if (e == NULL && !change_region)
2371 remove_eh_landing_pad (old_lp);
cc7220fd 2372 }
1d65f45c 2373 else
cc7220fd 2374 {
1d65f45c
RH
2375 /* No correct landing pad exists. If there are no more edges
2376 into OLD_LP, then we can simply re-use the existing landing pad.
2377 Otherwise, we have to create a new landing pad. */
2378 if (e == NULL)
2379 {
2380 EH_LANDING_PAD_NR (old_lp->post_landing_pad) = 0;
2381 new_lp = old_lp;
2382 }
2383 else
2384 new_lp = gen_eh_landing_pad (old_lp->region);
2385 new_lp->post_landing_pad = new_label;
2386 EH_LANDING_PAD_NR (new_label) = new_lp->index;
cc7220fd 2387 }
1d65f45c
RH
2388
2389 /* Maybe move the throwing statement to the new region. */
2390 if (old_lp != new_lp)
cc7220fd 2391 {
1d65f45c
RH
2392 remove_stmt_from_eh_lp (throw_stmt);
2393 add_stmt_to_eh_lp (throw_stmt, new_lp->index);
cc7220fd 2394 }
cc7220fd
JH
2395}
2396
1d65f45c 2397/* Redirect EH edge E to NEW_BB. */
726a989a 2398
1d65f45c
RH
2399edge
2400redirect_eh_edge (edge edge_in, basic_block new_bb)
cc7220fd 2401{
1d65f45c
RH
2402 redirect_eh_edge_1 (edge_in, new_bb, false);
2403 return ssa_redirect_edge (edge_in, new_bb);
2404}
cc7220fd 2405
1d65f45c
RH
2406/* This is a subroutine of gimple_redirect_edge_and_branch. Update the
2407 labels for redirecting a non-fallthru EH_DISPATCH edge E to NEW_BB.
2408 The actual edge update will happen in the caller. */
cc7220fd 2409
1d65f45c 2410void
538dd0b7 2411redirect_eh_dispatch_edge (geh_dispatch *stmt, edge e, basic_block new_bb)
1d65f45c
RH
2412{
2413 tree new_lab = gimple_block_label (new_bb);
2414 bool any_changed = false;
2415 basic_block old_bb;
2416 eh_region r;
2417 eh_catch c;
2418
2419 r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
2420 switch (r->type)
cc7220fd 2421 {
1d65f45c
RH
2422 case ERT_TRY:
2423 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
cc7220fd 2424 {
1d65f45c
RH
2425 old_bb = label_to_block (c->label);
2426 if (old_bb == e->dest)
2427 {
2428 c->label = new_lab;
2429 any_changed = true;
2430 }
cc7220fd 2431 }
1d65f45c
RH
2432 break;
2433
2434 case ERT_ALLOWED_EXCEPTIONS:
2435 old_bb = label_to_block (r->u.allowed.label);
2436 gcc_assert (old_bb == e->dest);
2437 r->u.allowed.label = new_lab;
2438 any_changed = true;
2439 break;
2440
2441 default:
2442 gcc_unreachable ();
cc7220fd 2443 }
726a989a 2444
1d65f45c 2445 gcc_assert (any_changed);
cc7220fd 2446}
6de9cd9a 2447\f
726a989a
RB
2448/* Helper function for operation_could_trap_p and stmt_could_throw_p. */
2449
890065bf 2450bool
726a989a
RB
2451operation_could_trap_helper_p (enum tree_code op,
2452 bool fp_operation,
2453 bool honor_trapv,
2454 bool honor_nans,
2455 bool honor_snans,
2456 tree divisor,
2457 bool *handled)
2458{
2459 *handled = true;
2460 switch (op)
2461 {
2462 case TRUNC_DIV_EXPR:
2463 case CEIL_DIV_EXPR:
2464 case FLOOR_DIV_EXPR:
2465 case ROUND_DIV_EXPR:
2466 case EXACT_DIV_EXPR:
2467 case CEIL_MOD_EXPR:
2468 case FLOOR_MOD_EXPR:
2469 case ROUND_MOD_EXPR:
2470 case TRUNC_MOD_EXPR:
2471 case RDIV_EXPR:
2472 if (honor_snans || honor_trapv)
2473 return true;
2474 if (fp_operation)
2475 return flag_trapping_math;
2476 if (!TREE_CONSTANT (divisor) || integer_zerop (divisor))
2477 return true;
2478 return false;
2479
2480 case LT_EXPR:
2481 case LE_EXPR:
2482 case GT_EXPR:
2483 case GE_EXPR:
2484 case LTGT_EXPR:
2485 /* Some floating point comparisons may trap. */
2486 return honor_nans;
2487
2488 case EQ_EXPR:
2489 case NE_EXPR:
2490 case UNORDERED_EXPR:
2491 case ORDERED_EXPR:
2492 case UNLT_EXPR:
2493 case UNLE_EXPR:
2494 case UNGT_EXPR:
2495 case UNGE_EXPR:
2496 case UNEQ_EXPR:
2497 return honor_snans;
2498
726a989a
RB
2499 case NEGATE_EXPR:
2500 case ABS_EXPR:
2501 case CONJ_EXPR:
2502 /* These operations don't trap with floating point. */
2503 if (honor_trapv)
2504 return true;
2505 return false;
2506
2507 case PLUS_EXPR:
2508 case MINUS_EXPR:
2509 case MULT_EXPR:
2510 /* Any floating arithmetic may trap. */
2511 if (fp_operation && flag_trapping_math)
2512 return true;
2513 if (honor_trapv)
2514 return true;
2515 return false;
2516
f5e5b46c
RG
2517 case COMPLEX_EXPR:
2518 case CONSTRUCTOR:
2519 /* Constructing an object cannot trap. */
2520 return false;
2521
726a989a
RB
2522 default:
2523 /* Any floating arithmetic may trap. */
2524 if (fp_operation && flag_trapping_math)
2525 return true;
2526
2527 *handled = false;
2528 return false;
2529 }
2530}
2531
2532/* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2533 on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2534 type operands that may trap. If OP is a division operator, DIVISOR contains
2535 the value of the divisor. */
2536
2537bool
2538operation_could_trap_p (enum tree_code op, bool fp_operation, bool honor_trapv,
2539 tree divisor)
2540{
2541 bool honor_nans = (fp_operation && flag_trapping_math
2542 && !flag_finite_math_only);
2543 bool honor_snans = fp_operation && flag_signaling_nans != 0;
2544 bool handled;
2545
2546 if (TREE_CODE_CLASS (op) != tcc_comparison
2547 && TREE_CODE_CLASS (op) != tcc_unary
2548 && TREE_CODE_CLASS (op) != tcc_binary)
2549 return false;
2550
2551 return operation_could_trap_helper_p (op, fp_operation, honor_trapv,
2552 honor_nans, honor_snans, divisor,
2553 &handled);
2554}
2555
862d0b35
DN
2556
2557/* Returns true if it is possible to prove that the index of
2558 an array access REF (an ARRAY_REF expression) falls into the
2559 array bounds. */
2560
2561static bool
2562in_array_bounds_p (tree ref)
2563{
2564 tree idx = TREE_OPERAND (ref, 1);
2565 tree min, max;
2566
2567 if (TREE_CODE (idx) != INTEGER_CST)
2568 return false;
2569
2570 min = array_ref_low_bound (ref);
2571 max = array_ref_up_bound (ref);
2572 if (!min
2573 || !max
2574 || TREE_CODE (min) != INTEGER_CST
2575 || TREE_CODE (max) != INTEGER_CST)
2576 return false;
2577
2578 if (tree_int_cst_lt (idx, min)
2579 || tree_int_cst_lt (max, idx))
2580 return false;
2581
2582 return true;
2583}
2584
2585/* Returns true if it is possible to prove that the range of
2586 an array access REF (an ARRAY_RANGE_REF expression) falls
2587 into the array bounds. */
2588
2589static bool
2590range_in_array_bounds_p (tree ref)
2591{
2592 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
2593 tree range_min, range_max, min, max;
2594
2595 range_min = TYPE_MIN_VALUE (domain_type);
2596 range_max = TYPE_MAX_VALUE (domain_type);
2597 if (!range_min
2598 || !range_max
2599 || TREE_CODE (range_min) != INTEGER_CST
2600 || TREE_CODE (range_max) != INTEGER_CST)
2601 return false;
2602
2603 min = array_ref_low_bound (ref);
2604 max = array_ref_up_bound (ref);
2605 if (!min
2606 || !max
2607 || TREE_CODE (min) != INTEGER_CST
2608 || TREE_CODE (max) != INTEGER_CST)
2609 return false;
2610
2611 if (tree_int_cst_lt (range_min, min)
2612 || tree_int_cst_lt (max, range_max))
2613 return false;
2614
2615 return true;
2616}
2617
726a989a 2618/* Return true if EXPR can trap, as in dereferencing an invalid pointer
1eaba2f2
RH
2619 location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2620 This routine expects only GIMPLE lhs or rhs input. */
6de9cd9a
DN
2621
2622bool
2623tree_could_trap_p (tree expr)
2624{
726a989a 2625 enum tree_code code;
1eaba2f2 2626 bool fp_operation = false;
9675412f 2627 bool honor_trapv = false;
726a989a 2628 tree t, base, div = NULL_TREE;
6de9cd9a 2629
726a989a
RB
2630 if (!expr)
2631 return false;
1d65f45c 2632
726a989a
RB
2633 code = TREE_CODE (expr);
2634 t = TREE_TYPE (expr);
2635
2636 if (t)
1eaba2f2 2637 {
04b03edb
RAE
2638 if (COMPARISON_CLASS_P (expr))
2639 fp_operation = FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
2640 else
2641 fp_operation = FLOAT_TYPE_P (t);
726a989a 2642 honor_trapv = INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t);
1eaba2f2
RH
2643 }
2644
726a989a
RB
2645 if (TREE_CODE_CLASS (code) == tcc_binary)
2646 div = TREE_OPERAND (expr, 1);
2647 if (operation_could_trap_p (code, fp_operation, honor_trapv, div))
2648 return true;
2649
d25cee4d 2650 restart:
6de9cd9a
DN
2651 switch (code)
2652 {
6de9cd9a
DN
2653 case COMPONENT_REF:
2654 case REALPART_EXPR:
2655 case IMAGPART_EXPR:
2656 case BIT_FIELD_REF:
483edb92 2657 case VIEW_CONVERT_EXPR:
d25cee4d
RH
2658 case WITH_SIZE_EXPR:
2659 expr = TREE_OPERAND (expr, 0);
2660 code = TREE_CODE (expr);
2661 goto restart;
a7e5372d
ZD
2662
2663 case ARRAY_RANGE_REF:
11fc4275
EB
2664 base = TREE_OPERAND (expr, 0);
2665 if (tree_could_trap_p (base))
a7e5372d 2666 return true;
11fc4275
EB
2667 if (TREE_THIS_NOTRAP (expr))
2668 return false;
11fc4275 2669 return !range_in_array_bounds_p (expr);
a7e5372d
ZD
2670
2671 case ARRAY_REF:
2672 base = TREE_OPERAND (expr, 0);
a7e5372d
ZD
2673 if (tree_could_trap_p (base))
2674 return true;
a7e5372d
ZD
2675 if (TREE_THIS_NOTRAP (expr))
2676 return false;
a7e5372d 2677 return !in_array_bounds_p (expr);
6de9cd9a 2678
4e1f39e4 2679 case TARGET_MEM_REF:
70f34814 2680 case MEM_REF:
4e1f39e4
RB
2681 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
2682 && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
2683 return true;
2684 if (TREE_THIS_NOTRAP (expr))
70f34814 2685 return false;
4e1f39e4
RB
2686 /* We cannot prove that the access is in-bounds when we have
2687 variable-index TARGET_MEM_REFs. */
2688 if (code == TARGET_MEM_REF
2689 && (TMR_INDEX (expr) || TMR_INDEX2 (expr)))
2690 return true;
2691 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
2692 {
2693 tree base = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
807e902e
KZ
2694 offset_int off = mem_ref_offset (expr);
2695 if (wi::neg_p (off, SIGNED))
4e1f39e4
RB
2696 return true;
2697 if (TREE_CODE (base) == STRING_CST)
807e902e 2698 return wi::leu_p (TREE_STRING_LENGTH (base), off);
4e1f39e4
RB
2699 else if (DECL_SIZE_UNIT (base) == NULL_TREE
2700 || TREE_CODE (DECL_SIZE_UNIT (base)) != INTEGER_CST
807e902e 2701 || wi::leu_p (wi::to_offset (DECL_SIZE_UNIT (base)), off))
4e1f39e4
RB
2702 return true;
2703 /* Now we are sure the first byte of the access is inside
2704 the object. */
2705 return false;
2706 }
2707 return true;
2708
6de9cd9a 2709 case INDIRECT_REF:
1eaba2f2
RH
2710 return !TREE_THIS_NOTRAP (expr);
2711
2712 case ASM_EXPR:
2713 return TREE_THIS_VOLATILE (expr);
5cb2183e 2714
726a989a
RB
2715 case CALL_EXPR:
2716 t = get_callee_fndecl (expr);
2717 /* Assume that calls to weak functions may trap. */
f2c3a8ce 2718 if (!t || !DECL_P (t))
1eaba2f2 2719 return true;
f2c3a8ce
JJ
2720 if (DECL_WEAK (t))
2721 return tree_could_trap_p (t);
2722 return false;
2723
2724 case FUNCTION_DECL:
2725 /* Assume that accesses to weak functions may trap, unless we know
2726 they are certainly defined in current TU or in some other
2727 LTO partition. */
5940e204 2728 if (DECL_WEAK (expr) && !DECL_COMDAT (expr) && DECL_EXTERNAL (expr))
f2c3a8ce 2729 {
5940e204
EB
2730 cgraph_node *node = cgraph_node::get (expr);
2731 if (node)
2732 node = node->function_symbol ();
2733 return !(node && node->in_other_partition);
f2c3a8ce
JJ
2734 }
2735 return false;
2736
2737 case VAR_DECL:
2738 /* Assume that accesses to weak vars may trap, unless we know
2739 they are certainly defined in current TU or in some other
2740 LTO partition. */
5940e204 2741 if (DECL_WEAK (expr) && !DECL_COMDAT (expr) && DECL_EXTERNAL (expr))
f2c3a8ce 2742 {
5940e204
EB
2743 varpool_node *node = varpool_node::get (expr);
2744 if (node)
2745 node = node->ultimate_alias_target ();
2746 return !(node && node->in_other_partition);
f2c3a8ce 2747 }
1eaba2f2
RH
2748 return false;
2749
726a989a
RB
2750 default:
2751 return false;
2752 }
2753}
1eaba2f2 2754
1eaba2f2 2755
726a989a
RB
2756/* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2757 an assignment or a conditional) may throw. */
1eaba2f2 2758
726a989a
RB
2759static bool
2760stmt_could_throw_1_p (gimple stmt)
2761{
2762 enum tree_code code = gimple_expr_code (stmt);
2763 bool honor_nans = false;
2764 bool honor_snans = false;
2765 bool fp_operation = false;
2766 bool honor_trapv = false;
2767 tree t;
2768 size_t i;
2769 bool handled, ret;
9675412f 2770
726a989a
RB
2771 if (TREE_CODE_CLASS (code) == tcc_comparison
2772 || TREE_CODE_CLASS (code) == tcc_unary
2773 || TREE_CODE_CLASS (code) == tcc_binary)
2774 {
dd46054a
RG
2775 if (is_gimple_assign (stmt)
2776 && TREE_CODE_CLASS (code) == tcc_comparison)
2777 t = TREE_TYPE (gimple_assign_rhs1 (stmt));
2778 else if (gimple_code (stmt) == GIMPLE_COND)
2779 t = TREE_TYPE (gimple_cond_lhs (stmt));
2780 else
2781 t = gimple_expr_type (stmt);
726a989a
RB
2782 fp_operation = FLOAT_TYPE_P (t);
2783 if (fp_operation)
2784 {
2785 honor_nans = flag_trapping_math && !flag_finite_math_only;
2786 honor_snans = flag_signaling_nans != 0;
2787 }
2788 else if (INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t))
2789 honor_trapv = true;
2790 }
2791
2792 /* Check if the main expression may trap. */
2793 t = is_gimple_assign (stmt) ? gimple_assign_rhs2 (stmt) : NULL;
2794 ret = operation_could_trap_helper_p (code, fp_operation, honor_trapv,
2795 honor_nans, honor_snans, t,
2796 &handled);
2797 if (handled)
2798 return ret;
2799
2800 /* If the expression does not trap, see if any of the individual operands may
2801 trap. */
2802 for (i = 0; i < gimple_num_ops (stmt); i++)
2803 if (tree_could_trap_p (gimple_op (stmt, i)))
2804 return true;
2805
2806 return false;
2807}
2808
2809
2810/* Return true if statement STMT could throw an exception. */
2811
2812bool
2813stmt_could_throw_p (gimple stmt)
2814{
726a989a
RB
2815 if (!flag_exceptions)
2816 return false;
2817
2818 /* The only statements that can throw an exception are assignments,
1d65f45c
RH
2819 conditionals, calls, resx, and asms. */
2820 switch (gimple_code (stmt))
2821 {
2822 case GIMPLE_RESX:
2823 return true;
726a989a 2824
1d65f45c 2825 case GIMPLE_CALL:
538dd0b7 2826 return !gimple_call_nothrow_p (as_a <gcall *> (stmt));
726a989a 2827
1d65f45c
RH
2828 case GIMPLE_ASSIGN:
2829 case GIMPLE_COND:
8f4f502f 2830 if (!cfun->can_throw_non_call_exceptions)
1d65f45c
RH
2831 return false;
2832 return stmt_could_throw_1_p (stmt);
726a989a 2833
1d65f45c 2834 case GIMPLE_ASM:
8f4f502f 2835 if (!cfun->can_throw_non_call_exceptions)
1d65f45c 2836 return false;
538dd0b7 2837 return gimple_asm_volatile_p (as_a <gasm *> (stmt));
1d65f45c
RH
2838
2839 default:
2840 return false;
2841 }
6de9cd9a
DN
2842}
2843
726a989a
RB
2844
2845/* Return true if expression T could throw an exception. */
2846
6de9cd9a
DN
2847bool
2848tree_could_throw_p (tree t)
2849{
2850 if (!flag_exceptions)
2851 return false;
726a989a 2852 if (TREE_CODE (t) == MODIFY_EXPR)
6de9cd9a 2853 {
8f4f502f 2854 if (cfun->can_throw_non_call_exceptions
1d65f45c
RH
2855 && tree_could_trap_p (TREE_OPERAND (t, 0)))
2856 return true;
726a989a 2857 t = TREE_OPERAND (t, 1);
6de9cd9a
DN
2858 }
2859
d25cee4d
RH
2860 if (TREE_CODE (t) == WITH_SIZE_EXPR)
2861 t = TREE_OPERAND (t, 0);
6de9cd9a
DN
2862 if (TREE_CODE (t) == CALL_EXPR)
2863 return (call_expr_flags (t) & ECF_NOTHROW) == 0;
8f4f502f 2864 if (cfun->can_throw_non_call_exceptions)
67c605a5 2865 return tree_could_trap_p (t);
6de9cd9a
DN
2866 return false;
2867}
2868
33977f81
JH
2869/* Return true if STMT can throw an exception that is not caught within
2870 the current function (CFUN). */
2871
2872bool
2873stmt_can_throw_external (gimple stmt)
2874{
1d65f45c 2875 int lp_nr;
33977f81
JH
2876
2877 if (!stmt_could_throw_p (stmt))
2878 return false;
2879
1d65f45c
RH
2880 lp_nr = lookup_stmt_eh_lp (stmt);
2881 return lp_nr == 0;
33977f81 2882}
726a989a
RB
2883
2884/* Return true if STMT can throw an exception that is caught within
2885 the current function (CFUN). */
2886
6de9cd9a 2887bool
726a989a 2888stmt_can_throw_internal (gimple stmt)
6de9cd9a 2889{
1d65f45c 2890 int lp_nr;
726a989a 2891
1d65f45c 2892 if (!stmt_could_throw_p (stmt))
6de9cd9a 2893 return false;
726a989a 2894
1d65f45c
RH
2895 lp_nr = lookup_stmt_eh_lp (stmt);
2896 return lp_nr > 0;
2897}
2898
2899/* Given a statement STMT in IFUN, if STMT can no longer throw, then
2900 remove any entry it might have from the EH table. Return true if
2901 any change was made. */
2902
2903bool
2904maybe_clean_eh_stmt_fn (struct function *ifun, gimple stmt)
2905{
2906 if (stmt_could_throw_p (stmt))
2907 return false;
2908 return remove_stmt_from_eh_lp_fn (ifun, stmt);
6de9cd9a
DN
2909}
2910
1d65f45c
RH
2911/* Likewise, but always use the current function. */
2912
2913bool
2914maybe_clean_eh_stmt (gimple stmt)
2915{
2916 return maybe_clean_eh_stmt_fn (cfun, stmt);
2917}
6de9cd9a 2918
af47810a
RH
2919/* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
2920 OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
2921 in the table if it should be in there. Return TRUE if a replacement was
2922 done that my require an EH edge purge. */
2923
1d65f45c
RH
2924bool
2925maybe_clean_or_replace_eh_stmt (gimple old_stmt, gimple new_stmt)
1eaba2f2 2926{
1d65f45c 2927 int lp_nr = lookup_stmt_eh_lp (old_stmt);
af47810a 2928
1d65f45c 2929 if (lp_nr != 0)
af47810a 2930 {
726a989a 2931 bool new_stmt_could_throw = stmt_could_throw_p (new_stmt);
af47810a
RH
2932
2933 if (new_stmt == old_stmt && new_stmt_could_throw)
2934 return false;
2935
1d65f45c 2936 remove_stmt_from_eh_lp (old_stmt);
af47810a
RH
2937 if (new_stmt_could_throw)
2938 {
1d65f45c 2939 add_stmt_to_eh_lp (new_stmt, lp_nr);
af47810a
RH
2940 return false;
2941 }
2942 else
2943 return true;
2944 }
2945
1eaba2f2
RH
2946 return false;
2947}
1d65f45c 2948
073a8998 2949/* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
1d65f45c
RH
2950 in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
2951 operand is the return value of duplicate_eh_regions. */
2952
2953bool
2954maybe_duplicate_eh_stmt_fn (struct function *new_fun, gimple new_stmt,
2955 struct function *old_fun, gimple old_stmt,
b787e7a2
TS
2956 hash_map<void *, void *> *map,
2957 int default_lp_nr)
1d65f45c
RH
2958{
2959 int old_lp_nr, new_lp_nr;
1d65f45c
RH
2960
2961 if (!stmt_could_throw_p (new_stmt))
2962 return false;
2963
2964 old_lp_nr = lookup_stmt_eh_lp_fn (old_fun, old_stmt);
2965 if (old_lp_nr == 0)
2966 {
2967 if (default_lp_nr == 0)
2968 return false;
2969 new_lp_nr = default_lp_nr;
2970 }
2971 else if (old_lp_nr > 0)
2972 {
2973 eh_landing_pad old_lp, new_lp;
2974
9771b263 2975 old_lp = (*old_fun->eh->lp_array)[old_lp_nr];
b787e7a2 2976 new_lp = static_cast<eh_landing_pad> (*map->get (old_lp));
1d65f45c
RH
2977 new_lp_nr = new_lp->index;
2978 }
2979 else
2980 {
2981 eh_region old_r, new_r;
2982
9771b263 2983 old_r = (*old_fun->eh->region_array)[-old_lp_nr];
b787e7a2 2984 new_r = static_cast<eh_region> (*map->get (old_r));
1d65f45c
RH
2985 new_lp_nr = -new_r->index;
2986 }
2987
2988 add_stmt_to_eh_lp_fn (new_fun, new_stmt, new_lp_nr);
2989 return true;
2990}
2991
2992/* Similar, but both OLD_STMT and NEW_STMT are within the current function,
2993 and thus no remapping is required. */
2994
2995bool
2996maybe_duplicate_eh_stmt (gimple new_stmt, gimple old_stmt)
2997{
2998 int lp_nr;
2999
3000 if (!stmt_could_throw_p (new_stmt))
3001 return false;
3002
3003 lp_nr = lookup_stmt_eh_lp (old_stmt);
3004 if (lp_nr == 0)
3005 return false;
3006
3007 add_stmt_to_eh_lp (new_stmt, lp_nr);
3008 return true;
3009}
a24549d4 3010\f
726a989a
RB
3011/* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
3012 GIMPLE_TRY) that are similar enough to be considered the same. Currently
3013 this only handles handlers consisting of a single call, as that's the
3014 important case for C++: a destructor call for a particular object showing
3015 up in multiple handlers. */
a24549d4
JM
3016
3017static bool
726a989a 3018same_handler_p (gimple_seq oneh, gimple_seq twoh)
a24549d4 3019{
726a989a
RB
3020 gimple_stmt_iterator gsi;
3021 gimple ones, twos;
3022 unsigned int ai;
a24549d4 3023
726a989a
RB
3024 gsi = gsi_start (oneh);
3025 if (!gsi_one_before_end_p (gsi))
a24549d4 3026 return false;
726a989a 3027 ones = gsi_stmt (gsi);
a24549d4 3028
726a989a
RB
3029 gsi = gsi_start (twoh);
3030 if (!gsi_one_before_end_p (gsi))
a24549d4 3031 return false;
726a989a
RB
3032 twos = gsi_stmt (gsi);
3033
3034 if (!is_gimple_call (ones)
3035 || !is_gimple_call (twos)
3036 || gimple_call_lhs (ones)
3037 || gimple_call_lhs (twos)
3038 || gimple_call_chain (ones)
3039 || gimple_call_chain (twos)
25583c4f 3040 || !gimple_call_same_target_p (ones, twos)
726a989a 3041 || gimple_call_num_args (ones) != gimple_call_num_args (twos))
a24549d4
JM
3042 return false;
3043
726a989a
RB
3044 for (ai = 0; ai < gimple_call_num_args (ones); ++ai)
3045 if (!operand_equal_p (gimple_call_arg (ones, ai),
1d65f45c 3046 gimple_call_arg (twos, ai), 0))
a24549d4
JM
3047 return false;
3048
3049 return true;
3050}
3051
3052/* Optimize
3053 try { A() } finally { try { ~B() } catch { ~A() } }
3054 try { ... } finally { ~A() }
3055 into
3056 try { A() } catch { ~B() }
3057 try { ~B() ... } finally { ~A() }
3058
3059 This occurs frequently in C++, where A is a local variable and B is a
3060 temporary used in the initializer for A. */
3061
3062static void
538dd0b7 3063optimize_double_finally (gtry *one, gtry *two)
a24549d4 3064{
726a989a
RB
3065 gimple oneh;
3066 gimple_stmt_iterator gsi;
355a7673 3067 gimple_seq cleanup;
a24549d4 3068
355a7673
MM
3069 cleanup = gimple_try_cleanup (one);
3070 gsi = gsi_start (cleanup);
726a989a 3071 if (!gsi_one_before_end_p (gsi))
a24549d4
JM
3072 return;
3073
726a989a
RB
3074 oneh = gsi_stmt (gsi);
3075 if (gimple_code (oneh) != GIMPLE_TRY
3076 || gimple_try_kind (oneh) != GIMPLE_TRY_CATCH)
a24549d4
JM
3077 return;
3078
726a989a 3079 if (same_handler_p (gimple_try_cleanup (oneh), gimple_try_cleanup (two)))
a24549d4 3080 {
726a989a 3081 gimple_seq seq = gimple_try_eval (oneh);
a24549d4 3082
726a989a
RB
3083 gimple_try_set_cleanup (one, seq);
3084 gimple_try_set_kind (one, GIMPLE_TRY_CATCH);
3085 seq = copy_gimple_seq_and_replace_locals (seq);
3086 gimple_seq_add_seq (&seq, gimple_try_eval (two));
3087 gimple_try_set_eval (two, seq);
a24549d4
JM
3088 }
3089}
3090
3091/* Perform EH refactoring optimizations that are simpler to do when code
84fbffb2 3092 flow has been lowered but EH structures haven't. */
a24549d4
JM
3093
3094static void
726a989a 3095refactor_eh_r (gimple_seq seq)
a24549d4 3096{
726a989a
RB
3097 gimple_stmt_iterator gsi;
3098 gimple one, two;
a24549d4 3099
726a989a
RB
3100 one = NULL;
3101 two = NULL;
3102 gsi = gsi_start (seq);
3103 while (1)
3104 {
3105 one = two;
3106 if (gsi_end_p (gsi))
3107 two = NULL;
3108 else
3109 two = gsi_stmt (gsi);
538dd0b7
DM
3110 if (one && two)
3111 if (gtry *try_one = dyn_cast <gtry *> (one))
3112 if (gtry *try_two = dyn_cast <gtry *> (two))
3113 if (gimple_try_kind (try_one) == GIMPLE_TRY_FINALLY
3114 && gimple_try_kind (try_two) == GIMPLE_TRY_FINALLY)
3115 optimize_double_finally (try_one, try_two);
726a989a
RB
3116 if (one)
3117 switch (gimple_code (one))
a24549d4 3118 {
726a989a
RB
3119 case GIMPLE_TRY:
3120 refactor_eh_r (gimple_try_eval (one));
3121 refactor_eh_r (gimple_try_cleanup (one));
3122 break;
3123 case GIMPLE_CATCH:
538dd0b7 3124 refactor_eh_r (gimple_catch_handler (as_a <gcatch *> (one)));
726a989a
RB
3125 break;
3126 case GIMPLE_EH_FILTER:
3127 refactor_eh_r (gimple_eh_filter_failure (one));
3128 break;
0a35513e 3129 case GIMPLE_EH_ELSE:
538dd0b7
DM
3130 {
3131 geh_else *eh_else_stmt = as_a <geh_else *> (one);
3132 refactor_eh_r (gimple_eh_else_n_body (eh_else_stmt));
3133 refactor_eh_r (gimple_eh_else_e_body (eh_else_stmt));
3134 }
0a35513e 3135 break;
726a989a
RB
3136 default:
3137 break;
a24549d4 3138 }
726a989a
RB
3139 if (two)
3140 gsi_next (&gsi);
3141 else
3142 break;
a24549d4
JM
3143 }
3144}
3145
27a4cd48
DM
3146namespace {
3147
3148const pass_data pass_data_refactor_eh =
a24549d4 3149{
27a4cd48
DM
3150 GIMPLE_PASS, /* type */
3151 "ehopt", /* name */
3152 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
3153 TV_TREE_EH, /* tv_id */
3154 PROP_gimple_lcf, /* properties_required */
3155 0, /* properties_provided */
3156 0, /* properties_destroyed */
3157 0, /* todo_flags_start */
3158 0, /* todo_flags_finish */
a24549d4 3159};
27a4cd48
DM
3160
3161class pass_refactor_eh : public gimple_opt_pass
3162{
3163public:
c3284718
RS
3164 pass_refactor_eh (gcc::context *ctxt)
3165 : gimple_opt_pass (pass_data_refactor_eh, ctxt)
27a4cd48
DM
3166 {}
3167
3168 /* opt_pass methods: */
1a3d085c 3169 virtual bool gate (function *) { return flag_exceptions != 0; }
be55bfe6
TS
3170 virtual unsigned int execute (function *)
3171 {
3172 refactor_eh_r (gimple_body (current_function_decl));
3173 return 0;
3174 }
27a4cd48
DM
3175
3176}; // class pass_refactor_eh
3177
3178} // anon namespace
3179
3180gimple_opt_pass *
3181make_pass_refactor_eh (gcc::context *ctxt)
3182{
3183 return new pass_refactor_eh (ctxt);
3184}
1d65f45c
RH
3185\f
3186/* At the end of gimple optimization, we can lower RESX. */
a8da523f 3187
1d65f45c 3188static bool
538dd0b7
DM
3189lower_resx (basic_block bb, gresx *stmt,
3190 hash_map<eh_region, tree> *mnt_map)
a8da523f 3191{
1d65f45c
RH
3192 int lp_nr;
3193 eh_region src_r, dst_r;
3194 gimple_stmt_iterator gsi;
3195 gimple x;
3196 tree fn, src_nr;
3197 bool ret = false;
a8da523f 3198
1d65f45c
RH
3199 lp_nr = lookup_stmt_eh_lp (stmt);
3200 if (lp_nr != 0)
3201 dst_r = get_eh_region_from_lp_number (lp_nr);
3202 else
3203 dst_r = NULL;
a8da523f 3204
1d65f45c 3205 src_r = get_eh_region_from_number (gimple_resx_region (stmt));
1d65f45c 3206 gsi = gsi_last_bb (bb);
a8da523f 3207
072c87d1
RH
3208 if (src_r == NULL)
3209 {
3210 /* We can wind up with no source region when pass_cleanup_eh shows
3211 that there are no entries into an eh region and deletes it, but
3212 then the block that contains the resx isn't removed. This can
3213 happen without optimization when the switch statement created by
3214 lower_try_finally_switch isn't simplified to remove the eh case.
3215
3216 Resolve this by expanding the resx node to an abort. */
3217
e79983f4 3218 fn = builtin_decl_implicit (BUILT_IN_TRAP);
072c87d1
RH
3219 x = gimple_build_call (fn, 0);
3220 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3221
3222 while (EDGE_COUNT (bb->succs) > 0)
3223 remove_edge (EDGE_SUCC (bb, 0));
3224 }
3225 else if (dst_r)
1d65f45c
RH
3226 {
3227 /* When we have a destination region, we resolve this by copying
3228 the excptr and filter values into place, and changing the edge
3229 to immediately after the landing pad. */
3230 edge e;
a8da523f 3231
1d65f45c
RH
3232 if (lp_nr < 0)
3233 {
3234 basic_block new_bb;
1d65f45c 3235 tree lab;
496a4ef5 3236
1d65f45c
RH
3237 /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3238 the failure decl into a new block, if needed. */
3239 gcc_assert (dst_r->type == ERT_MUST_NOT_THROW);
a8da523f 3240
b787e7a2 3241 tree *slot = mnt_map->get (dst_r);
1d65f45c
RH
3242 if (slot == NULL)
3243 {
3244 gimple_stmt_iterator gsi2;
a8da523f 3245
1d65f45c 3246 new_bb = create_empty_bb (bb);
726338f4 3247 add_bb_to_loop (new_bb, bb->loop_father);
1d65f45c
RH
3248 lab = gimple_block_label (new_bb);
3249 gsi2 = gsi_start_bb (new_bb);
a8da523f 3250
1d65f45c
RH
3251 fn = dst_r->u.must_not_throw.failure_decl;
3252 x = gimple_build_call (fn, 0);
3253 gimple_set_location (x, dst_r->u.must_not_throw.failure_loc);
3254 gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
4e6d1743 3255
b787e7a2 3256 mnt_map->put (dst_r, lab);
1d65f45c
RH
3257 }
3258 else
3259 {
b787e7a2 3260 lab = *slot;
1d65f45c
RH
3261 new_bb = label_to_block (lab);
3262 }
a8da523f 3263
1d65f45c
RH
3264 gcc_assert (EDGE_COUNT (bb->succs) == 0);
3265 e = make_edge (bb, new_bb, EDGE_FALLTHRU);
3266 e->count = bb->count;
3267 e->probability = REG_BR_PROB_BASE;
3268 }
3269 else
3270 {
3271 edge_iterator ei;
413581ba 3272 tree dst_nr = build_int_cst (integer_type_node, dst_r->index);
a8da523f 3273
e79983f4 3274 fn = builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES);
413581ba 3275 src_nr = build_int_cst (integer_type_node, src_r->index);
1d65f45c
RH
3276 x = gimple_build_call (fn, 2, dst_nr, src_nr);
3277 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
a8da523f 3278
1d65f45c
RH
3279 /* Update the flags for the outgoing edge. */
3280 e = single_succ_edge (bb);
3281 gcc_assert (e->flags & EDGE_EH);
3282 e->flags = (e->flags & ~EDGE_EH) | EDGE_FALLTHRU;
a8da523f 3283
1d65f45c
RH
3284 /* If there are no more EH users of the landing pad, delete it. */
3285 FOR_EACH_EDGE (e, ei, e->dest->preds)
3286 if (e->flags & EDGE_EH)
3287 break;
3288 if (e == NULL)
3289 {
3290 eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
3291 remove_eh_landing_pad (lp);
3292 }
3293 }
a8da523f 3294
1d65f45c
RH
3295 ret = true;
3296 }
3297 else
3298 {
3299 tree var;
a8da523f 3300
1d65f45c
RH
3301 /* When we don't have a destination region, this exception escapes
3302 up the call chain. We resolve this by generating a call to the
3303 _Unwind_Resume library function. */
a8da523f 3304
384c400a 3305 /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
1d65f45c 3306 with no arguments for C++ and Java. Check for that. */
384c400a
RH
3307 if (src_r->use_cxa_end_cleanup)
3308 {
e79983f4 3309 fn = builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP);
384c400a
RH
3310 x = gimple_build_call (fn, 0);
3311 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3312 }
3313 else
4e6d1743 3314 {
e79983f4 3315 fn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
413581ba 3316 src_nr = build_int_cst (integer_type_node, src_r->index);
1d65f45c 3317 x = gimple_build_call (fn, 1, src_nr);
b731b390 3318 var = create_tmp_var (ptr_type_node);
1d65f45c
RH
3319 var = make_ssa_name (var, x);
3320 gimple_call_set_lhs (x, var);
3321 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3322
e79983f4 3323 fn = builtin_decl_implicit (BUILT_IN_UNWIND_RESUME);
1d65f45c
RH
3324 x = gimple_build_call (fn, 1, var);
3325 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
4e6d1743 3326 }
a8da523f 3327
1d65f45c 3328 gcc_assert (EDGE_COUNT (bb->succs) == 0);
4e6d1743 3329 }
496a4ef5 3330
1d65f45c
RH
3331 gsi_remove (&gsi, true);
3332
3333 return ret;
4e6d1743
JH
3334}
3335
27a4cd48
DM
3336namespace {
3337
3338const pass_data pass_data_lower_resx =
4e6d1743 3339{
27a4cd48
DM
3340 GIMPLE_PASS, /* type */
3341 "resx", /* name */
3342 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
3343 TV_TREE_EH, /* tv_id */
3344 PROP_gimple_lcf, /* properties_required */
3345 0, /* properties_provided */
3346 0, /* properties_destroyed */
3347 0, /* todo_flags_start */
3bea341f 3348 0, /* todo_flags_finish */
4e6d1743
JH
3349};
3350
27a4cd48
DM
3351class pass_lower_resx : public gimple_opt_pass
3352{
3353public:
c3284718
RS
3354 pass_lower_resx (gcc::context *ctxt)
3355 : gimple_opt_pass (pass_data_lower_resx, ctxt)
27a4cd48
DM
3356 {}
3357
3358 /* opt_pass methods: */
1a3d085c 3359 virtual bool gate (function *) { return flag_exceptions != 0; }
be55bfe6 3360 virtual unsigned int execute (function *);
27a4cd48
DM
3361
3362}; // class pass_lower_resx
3363
be55bfe6
TS
3364unsigned
3365pass_lower_resx::execute (function *fun)
3366{
3367 basic_block bb;
be55bfe6
TS
3368 bool dominance_invalidated = false;
3369 bool any_rewritten = false;
3370
b787e7a2 3371 hash_map<eh_region, tree> mnt_map;
be55bfe6
TS
3372
3373 FOR_EACH_BB_FN (bb, fun)
3374 {
3375 gimple last = last_stmt (bb);
3376 if (last && is_gimple_resx (last))
3377 {
538dd0b7
DM
3378 dominance_invalidated |=
3379 lower_resx (bb, as_a <gresx *> (last), &mnt_map);
be55bfe6
TS
3380 any_rewritten = true;
3381 }
3382 }
3383
be55bfe6
TS
3384 if (dominance_invalidated)
3385 {
3386 free_dominance_info (CDI_DOMINATORS);
3387 free_dominance_info (CDI_POST_DOMINATORS);
3388 }
3389
3390 return any_rewritten ? TODO_update_ssa_only_virtuals : 0;
3391}
3392
27a4cd48
DM
3393} // anon namespace
3394
3395gimple_opt_pass *
3396make_pass_lower_resx (gcc::context *ctxt)
3397{
3398 return new pass_lower_resx (ctxt);
3399}
3400
960f0c9d
JJ
3401/* Try to optimize var = {v} {CLOBBER} stmts followed just by
3402 external throw. */
3403
3404static void
3405optimize_clobbers (basic_block bb)
3406{
3407 gimple_stmt_iterator gsi = gsi_last_bb (bb);
f223bb13
JJ
3408 bool any_clobbers = false;
3409 bool seen_stack_restore = false;
3410 edge_iterator ei;
3411 edge e;
3412
3413 /* Only optimize anything if the bb contains at least one clobber,
3414 ends with resx (checked by caller), optionally contains some
3415 debug stmts or labels, or at most one __builtin_stack_restore
3416 call, and has an incoming EH edge. */
6d1c2bd3 3417 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
960f0c9d
JJ
3418 {
3419 gimple stmt = gsi_stmt (gsi);
3420 if (is_gimple_debug (stmt))
6d1c2bd3 3421 continue;
f223bb13
JJ
3422 if (gimple_clobber_p (stmt))
3423 {
3424 any_clobbers = true;
3425 continue;
3426 }
3427 if (!seen_stack_restore
3428 && gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
3429 {
3430 seen_stack_restore = true;
3431 continue;
3432 }
3433 if (gimple_code (stmt) == GIMPLE_LABEL)
3434 break;
3435 return;
3436 }
3437 if (!any_clobbers)
3438 return;
3439 FOR_EACH_EDGE (e, ei, bb->preds)
3440 if (e->flags & EDGE_EH)
3441 break;
3442 if (e == NULL)
3443 return;
3444 gsi = gsi_last_bb (bb);
3445 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3446 {
3447 gimple stmt = gsi_stmt (gsi);
3448 if (!gimple_clobber_p (stmt))
3449 continue;
960f0c9d
JJ
3450 unlink_stmt_vdef (stmt);
3451 gsi_remove (&gsi, true);
3452 release_defs (stmt);
3453 }
3454}
1d65f45c 3455
ea85edfe
JJ
3456/* Try to sink var = {v} {CLOBBER} stmts followed just by
3457 internal throw to successor BB. */
3458
3459static int
3460sink_clobbers (basic_block bb)
3461{
3462 edge e;
3463 edge_iterator ei;
3464 gimple_stmt_iterator gsi, dgsi;
3465 basic_block succbb;
3466 bool any_clobbers = false;
df35498a 3467 unsigned todo = 0;
ea85edfe
JJ
3468
3469 /* Only optimize if BB has a single EH successor and
3470 all predecessor edges are EH too. */
3471 if (!single_succ_p (bb)
3472 || (single_succ_edge (bb)->flags & EDGE_EH) == 0)
3473 return 0;
3474
3475 FOR_EACH_EDGE (e, ei, bb->preds)
3476 {
3477 if ((e->flags & EDGE_EH) == 0)
3478 return 0;
3479 }
3480
3481 /* And BB contains only CLOBBER stmts before the final
3482 RESX. */
3483 gsi = gsi_last_bb (bb);
3484 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3485 {
3486 gimple stmt = gsi_stmt (gsi);
3487 if (is_gimple_debug (stmt))
3488 continue;
3489 if (gimple_code (stmt) == GIMPLE_LABEL)
3490 break;
f223bb13 3491 if (!gimple_clobber_p (stmt))
ea85edfe
JJ
3492 return 0;
3493 any_clobbers = true;
3494 }
3495 if (!any_clobbers)
3496 return 0;
3497
4c1aff1c
RB
3498 edge succe = single_succ_edge (bb);
3499 succbb = succe->dest;
3500
3501 /* See if there is a virtual PHI node to take an updated virtual
3502 operand from. */
538dd0b7 3503 gphi *vphi = NULL;
4c1aff1c 3504 tree vuse = NULL_TREE;
538dd0b7
DM
3505 for (gphi_iterator gpi = gsi_start_phis (succbb);
3506 !gsi_end_p (gpi); gsi_next (&gpi))
4c1aff1c 3507 {
538dd0b7 3508 tree res = gimple_phi_result (gpi.phi ());
4c1aff1c
RB
3509 if (virtual_operand_p (res))
3510 {
538dd0b7 3511 vphi = gpi.phi ();
4c1aff1c
RB
3512 vuse = res;
3513 break;
3514 }
3515 }
3516
ea85edfe
JJ
3517 dgsi = gsi_after_labels (succbb);
3518 gsi = gsi_last_bb (bb);
3519 for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3520 {
3521 gimple stmt = gsi_stmt (gsi);
f223bb13 3522 tree lhs;
ea85edfe
JJ
3523 if (is_gimple_debug (stmt))
3524 continue;
3525 if (gimple_code (stmt) == GIMPLE_LABEL)
3526 break;
f223bb13
JJ
3527 lhs = gimple_assign_lhs (stmt);
3528 /* Unfortunately we don't have dominance info updated at this
3529 point, so checking if
3530 dominated_by_p (CDI_DOMINATORS, succbb,
3531 gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0)))
3532 would be too costly. Thus, avoid sinking any clobbers that
3533 refer to non-(D) SSA_NAMEs. */
3534 if (TREE_CODE (lhs) == MEM_REF
3535 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME
3536 && !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs, 0)))
3537 {
4c1aff1c 3538 unlink_stmt_vdef (stmt);
f223bb13
JJ
3539 gsi_remove (&gsi, true);
3540 release_defs (stmt);
3541 continue;
3542 }
4c1aff1c
RB
3543
3544 /* As we do not change stmt order when sinking across a
3545 forwarder edge we can keep virtual operands in place. */
ea85edfe 3546 gsi_remove (&gsi, false);
4c1aff1c
RB
3547 gsi_insert_before (&dgsi, stmt, GSI_NEW_STMT);
3548
3549 /* But adjust virtual operands if we sunk across a PHI node. */
3550 if (vuse)
3551 {
3552 gimple use_stmt;
3553 imm_use_iterator iter;
3554 use_operand_p use_p;
3555 FOR_EACH_IMM_USE_STMT (use_stmt, iter, vuse)
3556 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3557 SET_USE (use_p, gimple_vdef (stmt));
0a1a83cb
RB
3558 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse))
3559 {
3560 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (stmt)) = 1;
3561 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (vuse) = 0;
3562 }
4c1aff1c
RB
3563 /* Adjust the incoming virtual operand. */
3564 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (vphi, succe), gimple_vuse (stmt));
3565 SET_USE (gimple_vuse_op (stmt), vuse);
3566 }
df35498a
RB
3567 /* If there isn't a single predecessor but no virtual PHI node
3568 arrange for virtual operands to be renamed. */
3569 else if (gimple_vuse_op (stmt) != NULL_USE_OPERAND_P
3570 && !single_pred_p (succbb))
3571 {
3572 /* In this case there will be no use of the VDEF of this stmt.
3573 ??? Unless this is a secondary opportunity and we have not
3574 removed unreachable blocks yet, so we cannot assert this.
3575 Which also means we will end up renaming too many times. */
3576 SET_USE (gimple_vuse_op (stmt), gimple_vop (cfun));
3577 mark_virtual_operands_for_renaming (cfun);
3578 todo |= TODO_update_ssa_only_virtuals;
3579 }
ea85edfe
JJ
3580 }
3581
df35498a 3582 return todo;
ea85edfe
JJ
3583}
3584
9f698956
AB
3585/* At the end of inlining, we can lower EH_DISPATCH. Return true when
3586 we have found some duplicate labels and removed some edges. */
4e6d1743 3587
9f698956 3588static bool
538dd0b7 3589lower_eh_dispatch (basic_block src, geh_dispatch *stmt)
4e6d1743 3590{
1d65f45c
RH
3591 gimple_stmt_iterator gsi;
3592 int region_nr;
3593 eh_region r;
3594 tree filter, fn;
3595 gimple x;
9f698956 3596 bool redirected = false;
4e6d1743 3597
1d65f45c
RH
3598 region_nr = gimple_eh_dispatch_region (stmt);
3599 r = get_eh_region_from_number (region_nr);
4e6d1743 3600
1d65f45c 3601 gsi = gsi_last_bb (src);
4e6d1743 3602
1d65f45c 3603 switch (r->type)
4e6d1743 3604 {
1d65f45c
RH
3605 case ERT_TRY:
3606 {
ef062b13 3607 auto_vec<tree> labels;
1d65f45c
RH
3608 tree default_label = NULL;
3609 eh_catch c;
3610 edge_iterator ei;
3611 edge e;
6e2830c3 3612 hash_set<tree> seen_values;
1d65f45c
RH
3613
3614 /* Collect the labels for a switch. Zero the post_landing_pad
3615 field becase we'll no longer have anything keeping these labels
073a8998 3616 in existence and the optimizer will be free to merge these
1d65f45c
RH
3617 blocks at will. */
3618 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
3619 {
3620 tree tp_node, flt_node, lab = c->label;
9f698956 3621 bool have_label = false;
4e6d1743 3622
1d65f45c
RH
3623 c->label = NULL;
3624 tp_node = c->type_list;
3625 flt_node = c->filter_list;
3626
3627 if (tp_node == NULL)
3628 {
3629 default_label = lab;
3630 break;
3631 }
3632 do
3633 {
9f698956
AB
3634 /* Filter out duplicate labels that arise when this handler
3635 is shadowed by an earlier one. When no labels are
3636 attached to the handler anymore, we remove
3637 the corresponding edge and then we delete unreachable
3638 blocks at the end of this pass. */
6e2830c3 3639 if (! seen_values.contains (TREE_VALUE (flt_node)))
9f698956 3640 {
3d528853
NF
3641 tree t = build_case_label (TREE_VALUE (flt_node),
3642 NULL, lab);
9771b263 3643 labels.safe_push (t);
6e2830c3 3644 seen_values.add (TREE_VALUE (flt_node));
9f698956
AB
3645 have_label = true;
3646 }
1d65f45c
RH
3647
3648 tp_node = TREE_CHAIN (tp_node);
3649 flt_node = TREE_CHAIN (flt_node);
3650 }
3651 while (tp_node);
9f698956
AB
3652 if (! have_label)
3653 {
3654 remove_edge (find_edge (src, label_to_block (lab)));
3655 redirected = true;
3656 }
1d65f45c
RH
3657 }
3658
3659 /* Clean up the edge flags. */
3660 FOR_EACH_EDGE (e, ei, src->succs)
3661 {
3662 if (e->flags & EDGE_FALLTHRU)
3663 {
3664 /* If there was no catch-all, use the fallthru edge. */
3665 if (default_label == NULL)
3666 default_label = gimple_block_label (e->dest);
3667 e->flags &= ~EDGE_FALLTHRU;
3668 }
3669 }
3670 gcc_assert (default_label != NULL);
3671
3672 /* Don't generate a switch if there's only a default case.
3673 This is common in the form of try { A; } catch (...) { B; }. */
9771b263 3674 if (!labels.exists ())
1d65f45c
RH
3675 {
3676 e = single_succ_edge (src);
3677 e->flags |= EDGE_FALLTHRU;
3678 }
3679 else
3680 {
e79983f4 3681 fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
413581ba
RG
3682 x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3683 region_nr));
b731b390 3684 filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)));
1d65f45c
RH
3685 filter = make_ssa_name (filter, x);
3686 gimple_call_set_lhs (x, filter);
3687 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3688
3689 /* Turn the default label into a default case. */
3d528853 3690 default_label = build_case_label (NULL, NULL, default_label);
1d65f45c
RH
3691 sort_case_labels (labels);
3692
fd8d363e 3693 x = gimple_build_switch (filter, default_label, labels);
1d65f45c 3694 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
1d65f45c
RH
3695 }
3696 }
3697 break;
3698
3699 case ERT_ALLOWED_EXCEPTIONS:
3700 {
3701 edge b_e = BRANCH_EDGE (src);
3702 edge f_e = FALLTHRU_EDGE (src);
3703
e79983f4 3704 fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
413581ba
RG
3705 x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3706 region_nr));
b731b390 3707 filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)));
1d65f45c
RH
3708 filter = make_ssa_name (filter, x);
3709 gimple_call_set_lhs (x, filter);
3710 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3711
3712 r->u.allowed.label = NULL;
3713 x = gimple_build_cond (EQ_EXPR, filter,
3714 build_int_cst (TREE_TYPE (filter),
3715 r->u.allowed.filter),
3716 NULL_TREE, NULL_TREE);
3717 gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3718
3719 b_e->flags = b_e->flags | EDGE_TRUE_VALUE;
3720 f_e->flags = (f_e->flags & ~EDGE_FALLTHRU) | EDGE_FALSE_VALUE;
3721 }
3722 break;
3723
3724 default:
3725 gcc_unreachable ();
4e6d1743 3726 }
1d65f45c
RH
3727
3728 /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3729 gsi_remove (&gsi, true);
9f698956 3730 return redirected;
4e6d1743
JH
3731}
3732
be55bfe6
TS
3733namespace {
3734
3735const pass_data pass_data_lower_eh_dispatch =
3736{
3737 GIMPLE_PASS, /* type */
3738 "ehdisp", /* name */
3739 OPTGROUP_NONE, /* optinfo_flags */
be55bfe6
TS
3740 TV_TREE_EH, /* tv_id */
3741 PROP_gimple_lcf, /* properties_required */
3742 0, /* properties_provided */
3743 0, /* properties_destroyed */
3744 0, /* todo_flags_start */
3bea341f 3745 0, /* todo_flags_finish */
be55bfe6
TS
3746};
3747
3748class pass_lower_eh_dispatch : public gimple_opt_pass
3749{
3750public:
3751 pass_lower_eh_dispatch (gcc::context *ctxt)
3752 : gimple_opt_pass (pass_data_lower_eh_dispatch, ctxt)
3753 {}
3754
3755 /* opt_pass methods: */
3756 virtual bool gate (function *fun) { return fun->eh->region_tree != NULL; }
3757 virtual unsigned int execute (function *);
3758
3759}; // class pass_lower_eh_dispatch
3760
3761unsigned
3762pass_lower_eh_dispatch::execute (function *fun)
1d65f45c
RH
3763{
3764 basic_block bb;
ea85edfe 3765 int flags = 0;
9f698956 3766 bool redirected = false;
4e6d1743 3767
1d65f45c 3768 assign_filter_values ();
496a4ef5 3769
be55bfe6 3770 FOR_EACH_BB_FN (bb, fun)
1d65f45c
RH
3771 {
3772 gimple last = last_stmt (bb);
960f0c9d
JJ
3773 if (last == NULL)
3774 continue;
3775 if (gimple_code (last) == GIMPLE_EH_DISPATCH)
1d65f45c 3776 {
538dd0b7
DM
3777 redirected |= lower_eh_dispatch (bb,
3778 as_a <geh_dispatch *> (last));
ea85edfe
JJ
3779 flags |= TODO_update_ssa_only_virtuals;
3780 }
3781 else if (gimple_code (last) == GIMPLE_RESX)
3782 {
3783 if (stmt_can_throw_external (last))
3784 optimize_clobbers (bb);
3785 else
3786 flags |= sink_clobbers (bb);
1d65f45c
RH
3787 }
3788 }
3789
9f698956
AB
3790 if (redirected)
3791 delete_unreachable_blocks ();
ea85edfe 3792 return flags;
1d65f45c
RH
3793}
3794
27a4cd48
DM
3795} // anon namespace
3796
3797gimple_opt_pass *
3798make_pass_lower_eh_dispatch (gcc::context *ctxt)
3799{
3800 return new pass_lower_eh_dispatch (ctxt);
3801}
1d65f45c 3802\f
d273b176
SB
3803/* Walk statements, see what regions and, optionally, landing pads
3804 are really referenced.
3805
3806 Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
3807 and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
3808
3809 Passing NULL for LP_REACHABLE is valid, in this case only reachable
3810 regions are marked.
3811
3812 The caller is responsible for freeing the returned sbitmaps. */
1d65f45c
RH
3813
3814static void
d273b176 3815mark_reachable_handlers (sbitmap *r_reachablep, sbitmap *lp_reachablep)
1d65f45c
RH
3816{
3817 sbitmap r_reachable, lp_reachable;
1d65f45c 3818 basic_block bb;
d273b176
SB
3819 bool mark_landing_pads = (lp_reachablep != NULL);
3820 gcc_checking_assert (r_reachablep != NULL);
4e6d1743 3821
9771b263 3822 r_reachable = sbitmap_alloc (cfun->eh->region_array->length ());
f61e445a 3823 bitmap_clear (r_reachable);
d273b176
SB
3824 *r_reachablep = r_reachable;
3825
3826 if (mark_landing_pads)
3827 {
3828 lp_reachable = sbitmap_alloc (cfun->eh->lp_array->length ());
3829 bitmap_clear (lp_reachable);
3830 *lp_reachablep = lp_reachable;
3831 }
3832 else
3833 lp_reachable = NULL;
4e6d1743 3834
11cd3bed 3835 FOR_EACH_BB_FN (bb, cfun)
4e6d1743 3836 {
57f93411 3837 gimple_stmt_iterator gsi;
1d65f45c
RH
3838
3839 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3840 {
3841 gimple stmt = gsi_stmt (gsi);
1d65f45c 3842
d273b176 3843 if (mark_landing_pads)
1d65f45c 3844 {
d273b176
SB
3845 int lp_nr = lookup_stmt_eh_lp (stmt);
3846
3847 /* Negative LP numbers are MUST_NOT_THROW regions which
3848 are not considered BB enders. */
3849 if (lp_nr < 0)
3850 bitmap_set_bit (r_reachable, -lp_nr);
3851
3852 /* Positive LP numbers are real landing pads, and BB enders. */
3853 else if (lp_nr > 0)
3854 {
3855 gcc_assert (gsi_one_before_end_p (gsi));
3856 eh_region region = get_eh_region_from_lp_number (lp_nr);
3857 bitmap_set_bit (r_reachable, region->index);
3858 bitmap_set_bit (lp_reachable, lp_nr);
3859 }
1d65f45c 3860 }
6ae70ea2
JJ
3861
3862 /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
3863 switch (gimple_code (stmt))
3864 {
3865 case GIMPLE_RESX:
538dd0b7
DM
3866 bitmap_set_bit (r_reachable,
3867 gimple_resx_region (as_a <gresx *> (stmt)));
6ae70ea2
JJ
3868 break;
3869 case GIMPLE_EH_DISPATCH:
538dd0b7
DM
3870 bitmap_set_bit (r_reachable,
3871 gimple_eh_dispatch_region (
3872 as_a <geh_dispatch *> (stmt)));
6ae70ea2 3873 break;
5b1441be
RH
3874 case GIMPLE_CALL:
3875 if (gimple_call_builtin_p (stmt, BUILT_IN_EH_COPY_VALUES))
3876 for (int i = 0; i < 2; ++i)
3877 {
3878 tree rt = gimple_call_arg (stmt, i);
3879 HOST_WIDE_INT ri = tree_to_shwi (rt);
3880
3881 gcc_assert (ri = (int)ri);
3882 bitmap_set_bit (r_reachable, ri);
3883 }
3884 break;
6ae70ea2
JJ
3885 default:
3886 break;
3887 }
1d65f45c 3888 }
4e6d1743 3889 }
d273b176
SB
3890}
3891
3892/* Remove unreachable handlers and unreachable landing pads. */
3893
3894static void
3895remove_unreachable_handlers (void)
3896{
3897 sbitmap r_reachable, lp_reachable;
3898 eh_region region;
3899 eh_landing_pad lp;
3900 unsigned i;
3901
3902 mark_reachable_handlers (&r_reachable, &lp_reachable);
1d65f45c
RH
3903
3904 if (dump_file)
4e6d1743 3905 {
1d65f45c
RH
3906 fprintf (dump_file, "Before removal of unreachable regions:\n");
3907 dump_eh_tree (dump_file, cfun);
3908 fprintf (dump_file, "Reachable regions: ");
f61e445a 3909 dump_bitmap_file (dump_file, r_reachable);
1d65f45c 3910 fprintf (dump_file, "Reachable landing pads: ");
f61e445a 3911 dump_bitmap_file (dump_file, lp_reachable);
4e6d1743
JH
3912 }
3913
d273b176
SB
3914 if (dump_file)
3915 {
3916 FOR_EACH_VEC_SAFE_ELT (cfun->eh->region_array, i, region)
3917 if (region && !bitmap_bit_p (r_reachable, region->index))
3918 fprintf (dump_file,
3919 "Removing unreachable region %d\n",
3920 region->index);
3921 }
3922
3923 remove_unreachable_eh_regions (r_reachable);
4e6d1743 3924
d273b176
SB
3925 FOR_EACH_VEC_SAFE_ELT (cfun->eh->lp_array, i, lp)
3926 if (lp && !bitmap_bit_p (lp_reachable, lp->index))
1d65f45c
RH
3927 {
3928 if (dump_file)
d273b176
SB
3929 fprintf (dump_file,
3930 "Removing unreachable landing pad %d\n",
3931 lp->index);
1d65f45c
RH
3932 remove_eh_landing_pad (lp);
3933 }
b8698a0f 3934
1d65f45c 3935 if (dump_file)
4e6d1743 3936 {
1d65f45c
RH
3937 fprintf (dump_file, "\n\nAfter removal of unreachable regions:\n");
3938 dump_eh_tree (dump_file, cfun);
3939 fprintf (dump_file, "\n\n");
4e6d1743
JH
3940 }
3941
1d65f45c
RH
3942 sbitmap_free (r_reachable);
3943 sbitmap_free (lp_reachable);
3944
3945#ifdef ENABLE_CHECKING
3946 verify_eh_tree (cfun);
3947#endif
3948}
3949
99d8763e
JJ
3950/* Remove unreachable handlers if any landing pads have been removed after
3951 last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
3952
3953void
3954maybe_remove_unreachable_handlers (void)
3955{
3956 eh_landing_pad lp;
d273b176 3957 unsigned i;
99d8763e
JJ
3958
3959 if (cfun->eh == NULL)
3960 return;
d273b176
SB
3961
3962 FOR_EACH_VEC_SAFE_ELT (cfun->eh->lp_array, i, lp)
99d8763e
JJ
3963 if (lp && lp->post_landing_pad)
3964 {
3965 if (label_to_block (lp->post_landing_pad) == NULL)
3966 {
3967 remove_unreachable_handlers ();
3968 return;
3969 }
3970 }
3971}
3972
1d65f45c
RH
3973/* Remove regions that do not have landing pads. This assumes
3974 that remove_unreachable_handlers has already been run, and
d273b176
SB
3975 that we've just manipulated the landing pads since then.
3976
3977 Preserve regions with landing pads and regions that prevent
3978 exceptions from propagating further, even if these regions
3979 are not reachable. */
1d65f45c
RH
3980
3981static void
3982remove_unreachable_handlers_no_lp (void)
3983{
d273b176 3984 eh_region region;
1a47f99c 3985 sbitmap r_reachable;
d273b176 3986 unsigned i;
1a47f99c 3987
d273b176 3988 mark_reachable_handlers (&r_reachable, /*lp_reachablep=*/NULL);
1a47f99c 3989
d273b176 3990 FOR_EACH_VEC_SAFE_ELT (cfun->eh->region_array, i, region)
1a47f99c 3991 {
d273b176
SB
3992 if (! region)
3993 continue;
3994
3995 if (region->landing_pads != NULL
3996 || region->type == ERT_MUST_NOT_THROW)
3997 bitmap_set_bit (r_reachable, region->index);
3998
3999 if (dump_file
4000 && !bitmap_bit_p (r_reachable, region->index))
4001 fprintf (dump_file,
4002 "Removing unreachable region %d\n",
4003 region->index);
1a47f99c 4004 }
1d65f45c 4005
d273b176 4006 remove_unreachable_eh_regions (r_reachable);
1a47f99c
MM
4007
4008 sbitmap_free (r_reachable);
4e6d1743
JH
4009}
4010
1d65f45c
RH
4011/* Undo critical edge splitting on an EH landing pad. Earlier, we
4012 optimisticaly split all sorts of edges, including EH edges. The
4013 optimization passes in between may not have needed them; if not,
4014 we should undo the split.
4015
4016 Recognize this case by having one EH edge incoming to the BB and
4017 one normal edge outgoing; BB should be empty apart from the
4018 post_landing_pad label.
4019
4020 Note that this is slightly different from the empty handler case
4021 handled by cleanup_empty_eh, in that the actual handler may yet
4022 have actual code but the landing pad has been separated from the
4023 handler. As such, cleanup_empty_eh relies on this transformation
4024 having been done first. */
a8da523f
JH
4025
4026static bool
1d65f45c 4027unsplit_eh (eh_landing_pad lp)
a8da523f 4028{
1d65f45c
RH
4029 basic_block bb = label_to_block (lp->post_landing_pad);
4030 gimple_stmt_iterator gsi;
4031 edge e_in, e_out;
4032
4033 /* Quickly check the edge counts on BB for singularity. */
f223bb13 4034 if (!single_pred_p (bb) || !single_succ_p (bb))
1d65f45c 4035 return false;
f223bb13
JJ
4036 e_in = single_pred_edge (bb);
4037 e_out = single_succ_edge (bb);
a8da523f 4038
1d65f45c
RH
4039 /* Input edge must be EH and output edge must be normal. */
4040 if ((e_in->flags & EDGE_EH) == 0 || (e_out->flags & EDGE_EH) != 0)
4041 return false;
4042
3333cd50
RG
4043 /* The block must be empty except for the labels and debug insns. */
4044 gsi = gsi_after_labels (bb);
4045 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4046 gsi_next_nondebug (&gsi);
4047 if (!gsi_end_p (gsi))
1d65f45c
RH
4048 return false;
4049
4050 /* The destination block must not already have a landing pad
4051 for a different region. */
4052 for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
a8da523f 4053 {
538dd0b7 4054 glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
1d65f45c
RH
4055 tree lab;
4056 int lp_nr;
a8da523f 4057
538dd0b7 4058 if (!label_stmt)
1d65f45c 4059 break;
538dd0b7 4060 lab = gimple_label_label (label_stmt);
1d65f45c
RH
4061 lp_nr = EH_LANDING_PAD_NR (lab);
4062 if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
4063 return false;
4064 }
a8da523f 4065
f8fd49b5
RH
4066 /* The new destination block must not already be a destination of
4067 the source block, lest we merge fallthru and eh edges and get
4068 all sorts of confused. */
4069 if (find_edge (e_in->src, e_out->dest))
4070 return false;
4071
d6063d7f
RH
4072 /* ??? We can get degenerate phis due to cfg cleanups. I would have
4073 thought this should have been cleaned up by a phicprop pass, but
4074 that doesn't appear to handle virtuals. Propagate by hand. */
4075 if (!gimple_seq_empty_p (phi_nodes (bb)))
4076 {
538dd0b7 4077 for (gphi_iterator gpi = gsi_start_phis (bb); !gsi_end_p (gpi); )
d6063d7f 4078 {
538dd0b7
DM
4079 gimple use_stmt;
4080 gphi *phi = gpi.phi ();
d6063d7f
RH
4081 tree lhs = gimple_phi_result (phi);
4082 tree rhs = gimple_phi_arg_def (phi, 0);
4083 use_operand_p use_p;
4084 imm_use_iterator iter;
4085
4086 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4087 {
4088 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
4089 SET_USE (use_p, rhs);
4090 }
4091
4092 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
4093 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
4094
538dd0b7 4095 remove_phi_node (&gpi, true);
d6063d7f
RH
4096 }
4097 }
496a4ef5 4098
1d65f45c
RH
4099 if (dump_file && (dump_flags & TDF_DETAILS))
4100 fprintf (dump_file, "Unsplit EH landing pad %d to block %i.\n",
4101 lp->index, e_out->dest->index);
4102
4103 /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
4104 a successor edge, humor it. But do the real CFG change with the
4105 predecessor of E_OUT in order to preserve the ordering of arguments
4106 to the PHI nodes in E_OUT->DEST. */
4107 redirect_eh_edge_1 (e_in, e_out->dest, false);
4108 redirect_edge_pred (e_out, e_in->src);
4109 e_out->flags = e_in->flags;
4110 e_out->probability = e_in->probability;
4111 e_out->count = e_in->count;
4112 remove_edge (e_in);
496a4ef5 4113
1d65f45c
RH
4114 return true;
4115}
496a4ef5 4116
1d65f45c 4117/* Examine each landing pad block and see if it matches unsplit_eh. */
496a4ef5 4118
1d65f45c
RH
4119static bool
4120unsplit_all_eh (void)
4121{
4122 bool changed = false;
4123 eh_landing_pad lp;
4124 int i;
496a4ef5 4125
9771b263 4126 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1d65f45c
RH
4127 if (lp)
4128 changed |= unsplit_eh (lp);
4129
4130 return changed;
4131}
4132
4133/* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
4134 to OLD_BB to NEW_BB; return true on success, false on failure.
4135
4136 OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
4137 PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
4138 Virtual PHIs may be deleted and marked for renaming. */
4139
4140static bool
4141cleanup_empty_eh_merge_phis (basic_block new_bb, basic_block old_bb,
d6063d7f 4142 edge old_bb_out, bool change_region)
1d65f45c 4143{
538dd0b7 4144 gphi_iterator ngsi, ogsi;
1d65f45c
RH
4145 edge_iterator ei;
4146 edge e;
1d65f45c
RH
4147 bitmap ophi_handled;
4148
336ead04
JJ
4149 /* The destination block must not be a regular successor for any
4150 of the preds of the landing pad. Thus, avoid turning
4151 <..>
4152 | \ EH
4153 | <..>
4154 | /
4155 <..>
4156 into
4157 <..>
4158 | | EH
4159 <..>
4160 which CFG verification would choke on. See PR45172 and PR51089. */
4161 FOR_EACH_EDGE (e, ei, old_bb->preds)
4162 if (find_edge (e->src, new_bb))
4163 return false;
4164
1d65f45c
RH
4165 FOR_EACH_EDGE (e, ei, old_bb->preds)
4166 redirect_edge_var_map_clear (e);
4167
4168 ophi_handled = BITMAP_ALLOC (NULL);
1d65f45c
RH
4169
4170 /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
4171 for the edges we're going to move. */
4172 for (ngsi = gsi_start_phis (new_bb); !gsi_end_p (ngsi); gsi_next (&ngsi))
4173 {
538dd0b7 4174 gphi *ophi, *nphi = ngsi.phi ();
1d65f45c
RH
4175 tree nresult, nop;
4176
4177 nresult = gimple_phi_result (nphi);
4178 nop = gimple_phi_arg_def (nphi, old_bb_out->dest_idx);
4179
4180 /* Find the corresponding PHI in OLD_BB so we can forward-propagate
4181 the source ssa_name. */
4182 ophi = NULL;
4183 for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
4184 {
538dd0b7 4185 ophi = ogsi.phi ();
1d65f45c
RH
4186 if (gimple_phi_result (ophi) == nop)
4187 break;
4188 ophi = NULL;
a3710436 4189 }
496a4ef5 4190
1d65f45c
RH
4191 /* If we did find the corresponding PHI, copy those inputs. */
4192 if (ophi)
a8da523f 4193 {
3ffe07e1
JJ
4194 /* If NOP is used somewhere else beyond phis in new_bb, give up. */
4195 if (!has_single_use (nop))
4196 {
4197 imm_use_iterator imm_iter;
4198 use_operand_p use_p;
4199
4200 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, nop)
4201 {
4202 if (!gimple_debug_bind_p (USE_STMT (use_p))
4203 && (gimple_code (USE_STMT (use_p)) != GIMPLE_PHI
4204 || gimple_bb (USE_STMT (use_p)) != new_bb))
4205 goto fail;
4206 }
4207 }
1d65f45c
RH
4208 bitmap_set_bit (ophi_handled, SSA_NAME_VERSION (nop));
4209 FOR_EACH_EDGE (e, ei, old_bb->preds)
496a4ef5 4210 {
1d65f45c
RH
4211 location_t oloc;
4212 tree oop;
4213
4214 if ((e->flags & EDGE_EH) == 0)
4215 continue;
4216 oop = gimple_phi_arg_def (ophi, e->dest_idx);
4217 oloc = gimple_phi_arg_location (ophi, e->dest_idx);
9e227d60 4218 redirect_edge_var_map_add (e, nresult, oop, oloc);
496a4ef5 4219 }
1d65f45c 4220 }
d90e76d4 4221 /* If we didn't find the PHI, if it's a real variable or a VOP, we know
1d65f45c
RH
4222 from the fact that OLD_BB is tree_empty_eh_handler_p that the
4223 variable is unchanged from input to the block and we can simply
4224 re-use the input to NEW_BB from the OLD_BB_OUT edge. */
4225 else
4226 {
4227 location_t nloc
4228 = gimple_phi_arg_location (nphi, old_bb_out->dest_idx);
4229 FOR_EACH_EDGE (e, ei, old_bb->preds)
9e227d60 4230 redirect_edge_var_map_add (e, nresult, nop, nloc);
1d65f45c
RH
4231 }
4232 }
4233
4234 /* Second, verify that all PHIs from OLD_BB have been handled. If not,
4235 we don't know what values from the other edges into NEW_BB to use. */
4236 for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
4237 {
538dd0b7 4238 gphi *ophi = ogsi.phi ();
1d65f45c
RH
4239 tree oresult = gimple_phi_result (ophi);
4240 if (!bitmap_bit_p (ophi_handled, SSA_NAME_VERSION (oresult)))
4241 goto fail;
4242 }
4243
1d65f45c
RH
4244 /* Finally, move the edges and update the PHIs. */
4245 for (ei = ei_start (old_bb->preds); (e = ei_safe_edge (ei)); )
4246 if (e->flags & EDGE_EH)
4247 {
efa26eaa
RG
4248 /* ??? CFG manipluation routines do not try to update loop
4249 form on edge redirection. Do so manually here for now. */
4250 /* If we redirect a loop entry or latch edge that will either create
4251 a multiple entry loop or rotate the loop. If the loops merge
4252 we may have created a loop with multiple latches.
4253 All of this isn't easily fixed thus cancel the affected loop
4254 and mark the other loop as possibly having multiple latches. */
726338f4 4255 if (e->dest == e->dest->loop_father->header)
efa26eaa 4256 {
08c13199 4257 mark_loop_for_removal (e->dest->loop_father);
efa26eaa 4258 new_bb->loop_father->latch = NULL;
08c13199 4259 loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
efa26eaa 4260 }
d6063d7f 4261 redirect_eh_edge_1 (e, new_bb, change_region);
1d65f45c
RH
4262 redirect_edge_succ (e, new_bb);
4263 flush_pending_stmts (e);
4264 }
4265 else
4266 ei_next (&ei);
4e6d1743 4267
1d65f45c 4268 BITMAP_FREE (ophi_handled);
1d65f45c
RH
4269 return true;
4270
4271 fail:
4272 FOR_EACH_EDGE (e, ei, old_bb->preds)
4273 redirect_edge_var_map_clear (e);
4274 BITMAP_FREE (ophi_handled);
1d65f45c
RH
4275 return false;
4276}
4277
4278/* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4279 old region to NEW_REGION at BB. */
4280
4281static void
4282cleanup_empty_eh_move_lp (basic_block bb, edge e_out,
4283 eh_landing_pad lp, eh_region new_region)
4284{
4285 gimple_stmt_iterator gsi;
4286 eh_landing_pad *pp;
4287
4288 for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
4289 continue;
4290 *pp = lp->next_lp;
4291
4292 lp->region = new_region;
4293 lp->next_lp = new_region->landing_pads;
4294 new_region->landing_pads = lp;
4295
4296 /* Delete the RESX that was matched within the empty handler block. */
4297 gsi = gsi_last_bb (bb);
3d3f2249 4298 unlink_stmt_vdef (gsi_stmt (gsi));
1d65f45c
RH
4299 gsi_remove (&gsi, true);
4300
4301 /* Clean up E_OUT for the fallthru. */
4302 e_out->flags = (e_out->flags & ~EDGE_EH) | EDGE_FALLTHRU;
4303 e_out->probability = REG_BR_PROB_BASE;
4304}
4305
4306/* A subroutine of cleanup_empty_eh. Handle more complex cases of
b8698a0f 4307 unsplitting than unsplit_eh was prepared to handle, e.g. when
1d65f45c
RH
4308 multiple incoming edges and phis are involved. */
4309
4310static bool
d6063d7f 4311cleanup_empty_eh_unsplit (basic_block bb, edge e_out, eh_landing_pad lp)
1d65f45c
RH
4312{
4313 gimple_stmt_iterator gsi;
1d65f45c
RH
4314 tree lab;
4315
4316 /* We really ought not have totally lost everything following
4317 a landing pad label. Given that BB is empty, there had better
4318 be a successor. */
4319 gcc_assert (e_out != NULL);
4320
d6063d7f
RH
4321 /* The destination block must not already have a landing pad
4322 for a different region. */
1d65f45c
RH
4323 lab = NULL;
4324 for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
4325 {
538dd0b7 4326 glabel *stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
d6063d7f
RH
4327 int lp_nr;
4328
538dd0b7 4329 if (!stmt)
1d65f45c
RH
4330 break;
4331 lab = gimple_label_label (stmt);
d6063d7f
RH
4332 lp_nr = EH_LANDING_PAD_NR (lab);
4333 if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
4334 return false;
1d65f45c 4335 }
1d65f45c
RH
4336
4337 /* Attempt to move the PHIs into the successor block. */
d6063d7f 4338 if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, false))
1d65f45c
RH
4339 {
4340 if (dump_file && (dump_flags & TDF_DETAILS))
4341 fprintf (dump_file,
d6063d7f
RH
4342 "Unsplit EH landing pad %d to block %i "
4343 "(via cleanup_empty_eh).\n",
4344 lp->index, e_out->dest->index);
1d65f45c
RH
4345 return true;
4346 }
4347
4348 return false;
4349}
4350
afaaa67d
JJ
4351/* Return true if edge E_FIRST is part of an empty infinite loop
4352 or leads to such a loop through a series of single successor
4353 empty bbs. */
4354
4355static bool
4356infinite_empty_loop_p (edge e_first)
4357{
4358 bool inf_loop = false;
4359 edge e;
4360
4361 if (e_first->dest == e_first->src)
4362 return true;
4363
4364 e_first->src->aux = (void *) 1;
4365 for (e = e_first; single_succ_p (e->dest); e = single_succ_edge (e->dest))
4366 {
4367 gimple_stmt_iterator gsi;
4368 if (e->dest->aux)
4369 {
4370 inf_loop = true;
4371 break;
4372 }
4373 e->dest->aux = (void *) 1;
4374 gsi = gsi_after_labels (e->dest);
4375 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4376 gsi_next_nondebug (&gsi);
4377 if (!gsi_end_p (gsi))
4378 break;
4379 }
4380 e_first->src->aux = NULL;
4381 for (e = e_first; e->dest->aux; e = single_succ_edge (e->dest))
4382 e->dest->aux = NULL;
4383
4384 return inf_loop;
4385}
4386
1d65f45c
RH
4387/* Examine the block associated with LP to determine if it's an empty
4388 handler for its EH region. If so, attempt to redirect EH edges to
4389 an outer region. Return true the CFG was updated in any way. This
4390 is similar to jump forwarding, just across EH edges. */
4391
4392static bool
4393cleanup_empty_eh (eh_landing_pad lp)
4394{
4395 basic_block bb = label_to_block (lp->post_landing_pad);
4396 gimple_stmt_iterator gsi;
4397 gimple resx;
4398 eh_region new_region;
4399 edge_iterator ei;
4400 edge e, e_out;
4401 bool has_non_eh_pred;
81bfd197 4402 bool ret = false;
1d65f45c
RH
4403 int new_lp_nr;
4404
4405 /* There can be zero or one edges out of BB. This is the quickest test. */
4406 switch (EDGE_COUNT (bb->succs))
4407 {
4408 case 0:
4409 e_out = NULL;
4410 break;
4411 case 1:
f223bb13 4412 e_out = single_succ_edge (bb);
1d65f45c
RH
4413 break;
4414 default:
4415 return false;
4416 }
81bfd197
MM
4417
4418 resx = last_stmt (bb);
4419 if (resx && is_gimple_resx (resx))
4420 {
4421 if (stmt_can_throw_external (resx))
4422 optimize_clobbers (bb);
4423 else if (sink_clobbers (bb))
4424 ret = true;
4425 }
4426
1d65f45c
RH
4427 gsi = gsi_after_labels (bb);
4428
4429 /* Make sure to skip debug statements. */
4430 if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4431 gsi_next_nondebug (&gsi);
4432
4433 /* If the block is totally empty, look for more unsplitting cases. */
4434 if (gsi_end_p (gsi))
0d228a52 4435 {
609524d2
JJ
4436 /* For the degenerate case of an infinite loop bail out.
4437 If bb has no successors and is totally empty, which can happen e.g.
4438 because of incorrect noreturn attribute, bail out too. */
4439 if (e_out == NULL
4440 || infinite_empty_loop_p (e_out))
81bfd197 4441 return ret;
0d228a52 4442
81bfd197 4443 return ret | cleanup_empty_eh_unsplit (bb, e_out, lp);
0d228a52 4444 }
1d65f45c 4445
1ee0d660
EB
4446 /* The block should consist only of a single RESX statement, modulo a
4447 preceding call to __builtin_stack_restore if there is no outgoing
4448 edge, since the call can be eliminated in this case. */
1d65f45c 4449 resx = gsi_stmt (gsi);
1ee0d660
EB
4450 if (!e_out && gimple_call_builtin_p (resx, BUILT_IN_STACK_RESTORE))
4451 {
4452 gsi_next (&gsi);
4453 resx = gsi_stmt (gsi);
4454 }
1d65f45c 4455 if (!is_gimple_resx (resx))
81bfd197 4456 return ret;
1d65f45c
RH
4457 gcc_assert (gsi_one_before_end_p (gsi));
4458
4459 /* Determine if there are non-EH edges, or resx edges into the handler. */
4460 has_non_eh_pred = false;
4461 FOR_EACH_EDGE (e, ei, bb->preds)
4462 if (!(e->flags & EDGE_EH))
4463 has_non_eh_pred = true;
4464
4465 /* Find the handler that's outer of the empty handler by looking at
4466 where the RESX instruction was vectored. */
4467 new_lp_nr = lookup_stmt_eh_lp (resx);
4468 new_region = get_eh_region_from_lp_number (new_lp_nr);
4469
4470 /* If there's no destination region within the current function,
4471 redirection is trivial via removing the throwing statements from
4472 the EH region, removing the EH edges, and allowing the block
4473 to go unreachable. */
4474 if (new_region == NULL)
4475 {
4476 gcc_assert (e_out == NULL);
4477 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4478 if (e->flags & EDGE_EH)
4479 {
4480 gimple stmt = last_stmt (e->src);
4481 remove_stmt_from_eh_lp (stmt);
4482 remove_edge (e);
4483 }
4484 else
4485 ei_next (&ei);
4486 goto succeed;
4487 }
4488
4489 /* If the destination region is a MUST_NOT_THROW, allow the runtime
4490 to handle the abort and allow the blocks to go unreachable. */
4491 if (new_region->type == ERT_MUST_NOT_THROW)
4492 {
4493 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4494 if (e->flags & EDGE_EH)
4495 {
4496 gimple stmt = last_stmt (e->src);
4497 remove_stmt_from_eh_lp (stmt);
4498 add_stmt_to_eh_lp (stmt, new_lp_nr);
4499 remove_edge (e);
4500 }
4501 else
4502 ei_next (&ei);
4503 goto succeed;
4504 }
4505
4506 /* Try to redirect the EH edges and merge the PHIs into the destination
4507 landing pad block. If the merge succeeds, we'll already have redirected
4508 all the EH edges. The handler itself will go unreachable if there were
4509 no normal edges. */
d6063d7f 4510 if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, true))
1d65f45c
RH
4511 goto succeed;
4512
4513 /* Finally, if all input edges are EH edges, then we can (potentially)
4514 reduce the number of transfers from the runtime by moving the landing
4515 pad from the original region to the new region. This is a win when
4516 we remove the last CLEANUP region along a particular exception
4517 propagation path. Since nothing changes except for the region with
4518 which the landing pad is associated, the PHI nodes do not need to be
4519 adjusted at all. */
4520 if (!has_non_eh_pred)
4521 {
4522 cleanup_empty_eh_move_lp (bb, e_out, lp, new_region);
4523 if (dump_file && (dump_flags & TDF_DETAILS))
4524 fprintf (dump_file, "Empty EH handler %i moved to EH region %i.\n",
4525 lp->index, new_region->index);
4526
4527 /* ??? The CFG didn't change, but we may have rendered the
4528 old EH region unreachable. Trigger a cleanup there. */
a8da523f
JH
4529 return true;
4530 }
1d65f45c 4531
81bfd197 4532 return ret;
1d65f45c
RH
4533
4534 succeed:
4535 if (dump_file && (dump_flags & TDF_DETAILS))
4536 fprintf (dump_file, "Empty EH handler %i removed.\n", lp->index);
4537 remove_eh_landing_pad (lp);
4538 return true;
a8da523f
JH
4539}
4540
1d65f45c
RH
4541/* Do a post-order traversal of the EH region tree. Examine each
4542 post_landing_pad block and see if we can eliminate it as empty. */
4543
4544static bool
4545cleanup_all_empty_eh (void)
4546{
4547 bool changed = false;
4548 eh_landing_pad lp;
4549 int i;
4550
9771b263 4551 for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
1d65f45c
RH
4552 if (lp)
4553 changed |= cleanup_empty_eh (lp);
4554
4555 return changed;
4556}
a8da523f
JH
4557
4558/* Perform cleanups and lowering of exception handling
4559 1) cleanups regions with handlers doing nothing are optimized out
4560 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4561 3) Info about regions that are containing instructions, and regions
4562 reachable via local EH edges is collected
c0d18c6c 4563 4) Eh tree is pruned for regions no longer necessary.
1d65f45c
RH
4564
4565 TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4566 Unify those that have the same failure decl and locus.
4567*/
a8da523f
JH
4568
4569static unsigned int
66a3e339 4570execute_cleanup_eh_1 (void)
a8da523f 4571{
1d65f45c
RH
4572 /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4573 looking up unreachable landing pads. */
4574 remove_unreachable_handlers ();
a8da523f 4575
1d65f45c 4576 /* Watch out for the region tree vanishing due to all unreachable. */
25fe40b0 4577 if (cfun->eh->region_tree)
a8da523f 4578 {
1d65f45c 4579 bool changed = false;
a8da523f 4580
25fe40b0
RB
4581 if (optimize)
4582 changed |= unsplit_all_eh ();
1d65f45c
RH
4583 changed |= cleanup_all_empty_eh ();
4584
4585 if (changed)
6d07ad98
JH
4586 {
4587 free_dominance_info (CDI_DOMINATORS);
4588 free_dominance_info (CDI_POST_DOMINATORS);
a8da523f 4589
1d65f45c
RH
4590 /* We delayed all basic block deletion, as we may have performed
4591 cleanups on EH edges while non-EH edges were still present. */
4592 delete_unreachable_blocks ();
a8da523f 4593
1d65f45c
RH
4594 /* We manipulated the landing pads. Remove any region that no
4595 longer has a landing pad. */
4596 remove_unreachable_handlers_no_lp ();
4597
4598 return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
4599 }
a8da523f
JH
4600 }
4601
1d65f45c
RH
4602 return 0;
4603}
4604
27a4cd48
DM
4605namespace {
4606
4607const pass_data pass_data_cleanup_eh =
4608{
4609 GIMPLE_PASS, /* type */
4610 "ehcleanup", /* name */
4611 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
4612 TV_TREE_EH, /* tv_id */
4613 PROP_gimple_lcf, /* properties_required */
4614 0, /* properties_provided */
4615 0, /* properties_destroyed */
4616 0, /* todo_flags_start */
3bea341f 4617 0, /* todo_flags_finish */
a8da523f 4618};
27a4cd48
DM
4619
4620class pass_cleanup_eh : public gimple_opt_pass
4621{
4622public:
c3284718
RS
4623 pass_cleanup_eh (gcc::context *ctxt)
4624 : gimple_opt_pass (pass_data_cleanup_eh, ctxt)
27a4cd48
DM
4625 {}
4626
4627 /* opt_pass methods: */
65d3284b 4628 opt_pass * clone () { return new pass_cleanup_eh (m_ctxt); }
1a3d085c
TS
4629 virtual bool gate (function *fun)
4630 {
4631 return fun->eh != NULL && fun->eh->region_tree != NULL;
4632 }
4633
be55bfe6 4634 virtual unsigned int execute (function *);
27a4cd48
DM
4635
4636}; // class pass_cleanup_eh
4637
be55bfe6
TS
4638unsigned int
4639pass_cleanup_eh::execute (function *fun)
4640{
4641 int ret = execute_cleanup_eh_1 ();
4642
4643 /* If the function no longer needs an EH personality routine
4644 clear it. This exposes cross-language inlining opportunities
4645 and avoids references to a never defined personality routine. */
4646 if (DECL_FUNCTION_PERSONALITY (current_function_decl)
4647 && function_needs_eh_personality (fun) != eh_personality_lang)
4648 DECL_FUNCTION_PERSONALITY (current_function_decl) = NULL_TREE;
4649
4650 return ret;
4651}
4652
27a4cd48
DM
4653} // anon namespace
4654
4655gimple_opt_pass *
4656make_pass_cleanup_eh (gcc::context *ctxt)
4657{
4658 return new pass_cleanup_eh (ctxt);
4659}
1d65f45c
RH
4660\f
4661/* Verify that BB containing STMT as the last statement, has precisely the
4662 edge that make_eh_edges would create. */
4663
24e47c76 4664DEBUG_FUNCTION bool
1d65f45c
RH
4665verify_eh_edges (gimple stmt)
4666{
4667 basic_block bb = gimple_bb (stmt);
4668 eh_landing_pad lp = NULL;
4669 int lp_nr;
4670 edge_iterator ei;
4671 edge e, eh_edge;
4672
4673 lp_nr = lookup_stmt_eh_lp (stmt);
4674 if (lp_nr > 0)
4675 lp = get_eh_landing_pad_from_number (lp_nr);
4676
4677 eh_edge = NULL;
4678 FOR_EACH_EDGE (e, ei, bb->succs)
4679 {
4680 if (e->flags & EDGE_EH)
4681 {
4682 if (eh_edge)
4683 {
4684 error ("BB %i has multiple EH edges", bb->index);
4685 return true;
4686 }
4687 else
4688 eh_edge = e;
4689 }
4690 }
4691
4692 if (lp == NULL)
4693 {
4694 if (eh_edge)
4695 {
4696 error ("BB %i can not throw but has an EH edge", bb->index);
4697 return true;
4698 }
4699 return false;
4700 }
4701
4702 if (!stmt_could_throw_p (stmt))
4703 {
4704 error ("BB %i last statement has incorrectly set lp", bb->index);
4705 return true;
4706 }
4707
4708 if (eh_edge == NULL)
4709 {
4710 error ("BB %i is missing an EH edge", bb->index);
4711 return true;
4712 }
4713
4714 if (eh_edge->dest != label_to_block (lp->post_landing_pad))
4715 {
4716 error ("Incorrect EH edge %i->%i", bb->index, eh_edge->dest->index);
4717 return true;
4718 }
4719
4720 return false;
4721}
4722
4723/* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
4724
24e47c76 4725DEBUG_FUNCTION bool
538dd0b7 4726verify_eh_dispatch_edge (geh_dispatch *stmt)
1d65f45c
RH
4727{
4728 eh_region r;
4729 eh_catch c;
4730 basic_block src, dst;
4731 bool want_fallthru = true;
4732 edge_iterator ei;
4733 edge e, fall_edge;
4734
4735 r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
4736 src = gimple_bb (stmt);
4737
4738 FOR_EACH_EDGE (e, ei, src->succs)
4739 gcc_assert (e->aux == NULL);
4740
4741 switch (r->type)
4742 {
4743 case ERT_TRY:
4744 for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
4745 {
4746 dst = label_to_block (c->label);
4747 e = find_edge (src, dst);
4748 if (e == NULL)
4749 {
4750 error ("BB %i is missing an edge", src->index);
4751 return true;
4752 }
4753 e->aux = (void *)e;
4754
4755 /* A catch-all handler doesn't have a fallthru. */
4756 if (c->type_list == NULL)
4757 {
4758 want_fallthru = false;
4759 break;
4760 }
4761 }
4762 break;
4763
4764 case ERT_ALLOWED_EXCEPTIONS:
4765 dst = label_to_block (r->u.allowed.label);
4766 e = find_edge (src, dst);
4767 if (e == NULL)
4768 {
4769 error ("BB %i is missing an edge", src->index);
4770 return true;
4771 }
4772 e->aux = (void *)e;
4773 break;
4774
4775 default:
4776 gcc_unreachable ();
4777 }
4778
4779 fall_edge = NULL;
4780 FOR_EACH_EDGE (e, ei, src->succs)
4781 {
4782 if (e->flags & EDGE_FALLTHRU)
4783 {
4784 if (fall_edge != NULL)
4785 {
4786 error ("BB %i too many fallthru edges", src->index);
4787 return true;
4788 }
4789 fall_edge = e;
4790 }
4791 else if (e->aux)
4792 e->aux = NULL;
4793 else
4794 {
4795 error ("BB %i has incorrect edge", src->index);
4796 return true;
4797 }
4798 }
4799 if ((fall_edge != NULL) ^ want_fallthru)
4800 {
4801 error ("BB %i has incorrect fallthru edge", src->index);
4802 return true;
4803 }
4804
4805 return false;
4806}