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