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