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 : 6935865 : add_stmt_to_eh_lp_fn (struct function *ifun, gimple *t, int num)
73 : : {
74 : 6935865 : gcc_assert (num != 0);
75 : :
76 : 6935865 : if (!get_eh_throw_stmt_table (ifun))
77 : 401771 : set_eh_throw_stmt_table (ifun, hash_map<gimple *, int>::create_ggc (31));
78 : :
79 : 6935865 : bool existed = get_eh_throw_stmt_table (ifun)->put (t, num);
80 : 6935865 : gcc_assert (!existed);
81 : 6935865 : }
82 : :
83 : : /* Add statement T in the current function (cfun) to EH landing pad NUM. */
84 : :
85 : : void
86 : 2851771 : add_stmt_to_eh_lp (gimple *t, int num)
87 : : {
88 : 2851771 : add_stmt_to_eh_lp_fn (cfun, t, num);
89 : 2851771 : }
90 : :
91 : : /* Add statement T to the single EH landing pad in REGION. */
92 : :
93 : : static void
94 : 3266345 : record_stmt_eh_region (eh_region region, gimple *t)
95 : : {
96 : 3266345 : if (region == NULL)
97 : : return;
98 : 3266345 : if (region->type == ERT_MUST_NOT_THROW)
99 : 91563 : add_stmt_to_eh_lp_fn (cfun, t, -region->index);
100 : : else
101 : : {
102 : 3174782 : eh_landing_pad lp = region->landing_pads;
103 : 3174782 : if (lp == NULL)
104 : 905382 : lp = gen_eh_landing_pad (region);
105 : : else
106 : 2269400 : gcc_assert (lp->next_lp == NULL);
107 : 3174782 : 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 : 294889761 : remove_stmt_from_eh_lp_fn (struct function *ifun, gimple *t)
116 : : {
117 : 294889761 : if (!get_eh_throw_stmt_table (ifun))
118 : : return false;
119 : :
120 : 137203559 : if (!get_eh_throw_stmt_table (ifun)->get (t))
121 : : return false;
122 : :
123 : 6041120 : get_eh_throw_stmt_table (ifun)->remove (t);
124 : 6041120 : return true;
125 : : }
126 : :
127 : :
128 : : /* Remove statement T in the current function (cfun) from its
129 : : EH landing pad. */
130 : :
131 : : bool
132 : 119155638 : remove_stmt_from_eh_lp (gimple *t)
133 : : {
134 : 119155638 : 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 : 15676966390 : lookup_stmt_eh_lp_fn (struct function *ifun, const gimple *t)
144 : : {
145 : 15676966390 : if (ifun->eh->throw_stmt_table == NULL)
146 : : return 0;
147 : :
148 : 8674340002 : int *lp_nr = ifun->eh->throw_stmt_table->get (const_cast <gimple *> (t));
149 : 8674340002 : return lp_nr ? *lp_nr : 0;
150 : : }
151 : :
152 : : /* Likewise, but always use the current function. */
153 : :
154 : : int
155 : 14998584926 : 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 : 14998584926 : if (!cfun)
160 : : return 0;
161 : 14998584926 : 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 : 148464893 : finally_tree_hasher::hash (const finally_tree_node *v)
189 : : {
190 : 148464893 : return (intptr_t)v->child.t >> 4;
191 : : }
192 : :
193 : : inline bool
194 : 132189811 : finally_tree_hasher::equal (const finally_tree_node *v,
195 : : const finally_tree_node *c)
196 : : {
197 : 132189811 : 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 : 18126188 : record_in_finally_tree (treemple child, gtry *parent)
205 : : {
206 : 18126188 : struct finally_tree_node *n;
207 : 18126188 : finally_tree_node **slot;
208 : :
209 : 18126188 : n = XNEW (struct finally_tree_node);
210 : 18126188 : n->child = child;
211 : 18126188 : n->parent = parent;
212 : :
213 : 18126188 : slot = finally_tree->find_slot (n, INSERT);
214 : 18126188 : gcc_assert (!*slot);
215 : 18126188 : *slot = n;
216 : 18126188 : }
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 : 9253556 : collect_finally_tree_1 (gimple_seq seq, gtry *region)
226 : : {
227 : 9253556 : gimple_stmt_iterator gsi;
228 : :
229 : 99267774 : for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
230 : 90014218 : collect_finally_tree (gsi_stmt (gsi), region);
231 : 5208923 : }
232 : :
233 : : static void
234 : 90014218 : collect_finally_tree (gimple *stmt, gtry *region)
235 : : {
236 : 90014218 : treemple temp;
237 : :
238 : 90014218 : switch (gimple_code (stmt))
239 : : {
240 : 16381784 : case GIMPLE_LABEL:
241 : 16381784 : temp.t = gimple_label_label (as_a <glabel *> (stmt));
242 : 16381784 : record_in_finally_tree (temp, region);
243 : 16381784 : break;
244 : :
245 : 2572392 : case GIMPLE_TRY:
246 : 2572392 : if (gimple_try_kind (stmt) == GIMPLE_TRY_FINALLY)
247 : : {
248 : 1688892 : temp.g = stmt;
249 : 1688892 : record_in_finally_tree (temp, region);
250 : 1688892 : collect_finally_tree_1 (gimple_try_eval (stmt),
251 : : as_a <gtry *> (stmt));
252 : 1688892 : collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
253 : : }
254 : 883500 : else if (gimple_try_kind (stmt) == GIMPLE_TRY_CATCH)
255 : : {
256 : 883500 : collect_finally_tree_1 (gimple_try_eval (stmt), region);
257 : 883500 : collect_finally_tree_1 (gimple_try_cleanup (stmt), region);
258 : : }
259 : : break;
260 : :
261 : 57098 : case GIMPLE_CATCH:
262 : 114196 : collect_finally_tree_1 (gimple_catch_handler (
263 : 57098 : as_a <gcatch *> (stmt)),
264 : : region);
265 : 57098 : break;
266 : :
267 : 5769 : case GIMPLE_EH_FILTER:
268 : 5769 : collect_finally_tree_1 (gimple_eh_filter_failure (stmt), region);
269 : 5769 : break;
270 : :
271 : 636 : case GIMPLE_EH_ELSE:
272 : 636 : {
273 : 636 : geh_else *eh_else_stmt = as_a <geh_else *> (stmt);
274 : 636 : collect_finally_tree_1 (gimple_eh_else_n_body (eh_else_stmt), region);
275 : 636 : collect_finally_tree_1 (gimple_eh_else_e_body (eh_else_stmt), region);
276 : : }
277 : 636 : 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 : 90014218 : }
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 : 8904972 : outside_finally_tree (treemple start, gimple *target)
292 : : {
293 : 9629504 : struct finally_tree_node n, *p;
294 : :
295 : 9629504 : do
296 : : {
297 : 9629504 : n.child = start;
298 : 9629504 : p = finally_tree->find (&n);
299 : 9629504 : if (!p)
300 : : return true;
301 : 8939477 : start.g = p->parent;
302 : : }
303 : 8939477 : 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 : 7189079 : find_goto_replacement (struct leh_tf_state *tf, treemple stmt)
423 : : {
424 : 7189079 : unsigned int i;
425 : :
426 : 7189079 : if (tf->goto_queue_active < LARGE_GOTO_QUEUE)
427 : : {
428 : 21377189 : for (i = 0; i < tf->goto_queue_active; i++)
429 : 14949532 : if ( tf->goto_queue[i].stmt.g == stmt.g)
430 : 681012 : 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 : 80410 : if (!tf->goto_queue_map)
438 : : {
439 : 329 : tf->goto_queue_map = new hash_map<gimple *, goto_queue_node *>;
440 : 9344 : for (i = 0; i < tf->goto_queue_active; i++)
441 : : {
442 : 18030 : bool existed = tf->goto_queue_map->put (tf->goto_queue[i].stmt.g,
443 : 9015 : &tf->goto_queue[i]);
444 : 9015 : gcc_assert (!existed);
445 : : }
446 : : }
447 : :
448 : 80410 : goto_queue_node **slot = tf->goto_queue_map->get (stmt.g);
449 : 80410 : if (slot != NULL)
450 : 9015 : 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 : 4557446 : replace_goto_queue_cond_clause (tree *tp, struct leh_tf_state *tf,
462 : : gimple_stmt_iterator *gsi)
463 : : {
464 : 4557446 : tree label;
465 : 4557446 : gimple_seq new_seq;
466 : 4557446 : treemple temp;
467 : 4557446 : location_t loc = gimple_location (gsi_stmt (*gsi));
468 : :
469 : 4557446 : temp.tp = tp;
470 : 4557446 : new_seq = find_goto_replacement (tf, temp);
471 : 4557446 : if (!new_seq)
472 : 4557371 : return;
473 : :
474 : 1826 : if (gimple_seq_singleton_p (new_seq)
475 : 1751 : && gimple_code (gimple_seq_first_stmt (new_seq)) == GIMPLE_GOTO)
476 : : {
477 : 1751 : *tp = gimple_goto_dest (gimple_seq_first_stmt (new_seq));
478 : 1751 : 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 : 35986398 : replace_goto_queue_1 (gimple *stmt, struct leh_tf_state *tf,
496 : : gimple_stmt_iterator *gsi)
497 : : {
498 : 35986398 : gimple_seq seq;
499 : 35986398 : treemple temp;
500 : 35986398 : temp.g = NULL;
501 : :
502 : 35986398 : switch (gimple_code (stmt))
503 : : {
504 : 2631633 : case GIMPLE_GOTO:
505 : 2631633 : case GIMPLE_RETURN:
506 : 2631633 : temp.g = stmt;
507 : 2631633 : seq = find_goto_replacement (tf, temp);
508 : 2631633 : if (seq)
509 : : {
510 : 688201 : gimple_stmt_iterator i;
511 : 688201 : seq = gimple_seq_copy (seq);
512 : 1377634 : for (i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
513 : 1221604 : gimple_set_location (gsi_stmt (i), gimple_location (stmt));
514 : 688201 : gsi_insert_seq_before (gsi, seq, GSI_SAME_STMT);
515 : 688201 : gsi_remove (gsi, false);
516 : 688201 : return;
517 : : }
518 : : break;
519 : :
520 : 2278723 : case GIMPLE_COND:
521 : 2278723 : replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 2), tf, gsi);
522 : 2278723 : replace_goto_queue_cond_clause (gimple_op_ptr (stmt, 3), tf, gsi);
523 : 2278723 : 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 : 35298197 : gsi_next (gsi);
553 : : }
554 : :
555 : : /* A subroutine of replace_goto_queue. Handles GIMPLE_SEQ. */
556 : :
557 : : static void
558 : 1040240 : replace_goto_queue_stmt_list (gimple_seq *seq, struct leh_tf_state *tf)
559 : : {
560 : 1040240 : gimple_stmt_iterator gsi = gsi_start (*seq);
561 : :
562 : 37026638 : while (!gsi_end_p (gsi))
563 : 35986398 : replace_goto_queue_1 (gsi_stmt (gsi), tf, &gsi);
564 : 1040240 : }
565 : :
566 : : /* Replace all goto queue members. */
567 : :
568 : : static void
569 : 522822 : replace_goto_queue (struct leh_tf_state *tf)
570 : : {
571 : 522822 : if (tf->goto_queue_active == 0)
572 : : return;
573 : 520120 : replace_goto_queue_stmt_list (&tf->top_p_seq, tf);
574 : 520120 : 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 : 690027 : 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 : 690027 : size_t active, size;
589 : 690027 : struct goto_queue_node *q;
590 : :
591 : 690027 : gcc_assert (!tf->goto_queue_map);
592 : :
593 : 690027 : active = tf->goto_queue_active;
594 : 690027 : size = tf->goto_queue_size;
595 : 690027 : if (active >= size)
596 : : {
597 : 520155 : size = (size ? size * 2 : 32);
598 : 520155 : tf->goto_queue_size = size;
599 : 520155 : tf->goto_queue
600 : 520155 : = XRESIZEVEC (struct goto_queue_node, tf->goto_queue, size);
601 : : }
602 : :
603 : 690027 : q = &tf->goto_queue[active];
604 : 690027 : tf->goto_queue_active = active + 1;
605 : :
606 : 690027 : memset (q, 0, sizeof (*q));
607 : 690027 : q->stmt = new_stmt;
608 : 690027 : q->index = index;
609 : 690027 : q->location = location;
610 : 690027 : q->is_label = is_label;
611 : 690027 : }
612 : :
613 : : /* Record the LABEL label in the goto queue contained in TF.
614 : : TF is not null. */
615 : :
616 : : static void
617 : 8676861 : record_in_goto_queue_label (struct leh_tf_state *tf, treemple stmt, tree label,
618 : : location_t location)
619 : : {
620 : 8676861 : int index;
621 : 8676861 : treemple temp, new_stmt;
622 : :
623 : 8676861 : if (!label)
624 : 7986834 : 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 : 8676861 : if (TREE_CODE (label) != LABEL_DECL)
630 : : return;
631 : :
632 : : /* No need to record gotos that don't leave the try block. */
633 : 8676629 : temp.t = label;
634 : 8676629 : if (!outside_finally_tree (temp, tf->try_finally_expr))
635 : : return;
636 : :
637 : 690027 : if (! tf->dest_array.exists ())
638 : : {
639 : 520120 : tf->dest_array.create (10);
640 : 520120 : tf->dest_array.quick_push (label);
641 : 520120 : index = 0;
642 : : }
643 : : else
644 : : {
645 : 169907 : int n = tf->dest_array.length ();
646 : 186977 : for (index = 0; index < n; ++index)
647 : 182827 : if (tf->dest_array[index] == label)
648 : : break;
649 : 169907 : if (index == n)
650 : 4150 : 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 : 690027 : new_stmt = stmt;
657 : 690027 : 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 : 14449458 : maybe_record_in_goto_queue (struct leh_state *state, gimple *stmt)
666 : : {
667 : 14449458 : struct leh_tf_state *tf = state->tf;
668 : 14449458 : treemple new_stmt;
669 : :
670 : 14449458 : if (!tf)
671 : 14449458 : return;
672 : :
673 : 5685920 : switch (gimple_code (stmt))
674 : : {
675 : 2990941 : case GIMPLE_COND:
676 : 2990941 : {
677 : 2990941 : gcond *cond_stmt = as_a <gcond *> (stmt);
678 : 2990941 : new_stmt.tp = gimple_op_ptr (cond_stmt, 2);
679 : 2990941 : record_in_goto_queue_label (tf, new_stmt,
680 : : gimple_cond_true_label (cond_stmt),
681 : 2990941 : EXPR_LOCATION (*new_stmt.tp));
682 : 2990941 : 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 : 2990941 : EXPR_LOCATION (*new_stmt.tp));
686 : : }
687 : 2990941 : break;
688 : 2694979 : case GIMPLE_GOTO:
689 : 2694979 : new_stmt.g = stmt;
690 : 2694979 : record_in_goto_queue_label (tf, new_stmt, gimple_goto_dest (stmt),
691 : : gimple_location (stmt));
692 : 2694979 : 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 : 51964 : verify_norecord_switch_expr (struct leh_state *state,
713 : : gswitch *switch_expr)
714 : : {
715 : 51964 : struct leh_tf_state *tf = state->tf;
716 : 51964 : size_t i, n;
717 : :
718 : 51964 : if (!tf)
719 : : return;
720 : :
721 : 30579 : n = gimple_switch_num_labels (switch_expr);
722 : :
723 : 258922 : for (i = 0; i < n; ++i)
724 : : {
725 : 228343 : treemple temp;
726 : 228343 : tree lab = CASE_LABEL (gimple_switch_label (switch_expr, i));
727 : 228343 : temp.t = lab;
728 : 228343 : 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 : 690027 : do_goto_redirection (struct goto_queue_node *q, tree finlab, gimple_seq mod,
777 : : struct leh_tf_state *tf)
778 : : {
779 : 690027 : ggoto *x;
780 : :
781 : 690027 : gcc_assert (q->is_label);
782 : :
783 : 690027 : q->cont_stmt = gimple_build_goto (tf->dest_array[q->index]);
784 : :
785 : 690027 : if (mod)
786 : 1307 : gimple_seq_add_seq (&q->repl_stmt, mod);
787 : :
788 : 690027 : x = gimple_build_goto (finlab);
789 : 690027 : gimple_set_location (x, q->location);
790 : 690027 : gimple_seq_add_stmt (&q->repl_stmt, x);
791 : 690027 : }
792 : :
793 : : /* Emit a standard landing pad sequence into SEQ for REGION. */
794 : :
795 : : static void
796 : 905382 : emit_post_landing_pad (gimple_seq *seq, eh_region region)
797 : : {
798 : 905382 : eh_landing_pad lp = region->landing_pads;
799 : 905382 : glabel *x;
800 : :
801 : 905382 : if (lp == NULL)
802 : 0 : lp = gen_eh_landing_pad (region);
803 : :
804 : 905382 : lp->post_landing_pad = create_artificial_label (UNKNOWN_LOCATION);
805 : 905382 : EH_LANDING_PAD_NR (lp->post_landing_pad) = lp->index;
806 : :
807 : 905382 : x = gimple_build_label (lp->post_landing_pad);
808 : 905382 : gimple_seq_add_stmt (seq, x);
809 : 905382 : }
810 : :
811 : : /* Emit a RESX statement into SEQ for REGION. */
812 : :
813 : : static void
814 : 905380 : emit_resx (gimple_seq *seq, eh_region region)
815 : : {
816 : 905380 : gresx *x = gimple_build_resx (region->index);
817 : 905380 : gimple_seq_add_stmt (seq, x);
818 : 905380 : if (region->outer)
819 : 539007 : record_stmt_eh_region (region->outer, x);
820 : 905380 : }
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 : 2727338 : note_eh_region_may_contain_throw (eh_region region)
827 : : {
828 : 3266345 : while (bitmap_set_bit (eh_region_may_contain_throw_map, region->index))
829 : : {
830 : 979711 : if (region->type == ERT_MUST_NOT_THROW)
831 : : break;
832 : 905382 : region = region->outer;
833 : 905382 : if (region == NULL)
834 : : break;
835 : : }
836 : 2727338 : }
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 : 1315907 : eh_region_may_contain_throw (eh_region r)
843 : : {
844 : 1315907 : 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 : 41783 : frob_into_branch_around (gtry *tp, eh_region region, tree over)
864 : : {
865 : 41783 : gimple *x;
866 : 41783 : gimple_seq cleanup, result;
867 : 41783 : location_t loc = gimple_location (tp);
868 : :
869 : 41783 : cleanup = gimple_try_cleanup (tp);
870 : 41783 : result = gimple_try_eval (tp);
871 : :
872 : 41783 : if (region)
873 : 41783 : emit_post_landing_pad (&eh_seq, region);
874 : :
875 : 41783 : 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 : 41783 : gimple_seq_add_seq (&eh_seq, cleanup);
884 : :
885 : 41783 : if (over)
886 : : {
887 : 13216 : x = gimple_build_label (over);
888 : 13216 : gimple_seq_add_stmt (&result, x);
889 : : }
890 : 41783 : 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 : 1064851 : lower_try_finally_dup_block (gimple_seq seq, struct leh_state *outer_state,
898 : : location_t loc)
899 : : {
900 : 1064851 : gtry *region = NULL;
901 : 1064851 : gimple_seq new_seq;
902 : 1064851 : gimple_stmt_iterator gsi;
903 : :
904 : 1064851 : new_seq = copy_gimple_seq_and_replace_locals (seq);
905 : :
906 : 2572544 : for (gsi = gsi_start (new_seq); !gsi_end_p (gsi); gsi_next (&gsi))
907 : : {
908 : 1507693 : gimple *stmt = gsi_stmt (gsi);
909 : 1507693 : if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
910 : : {
911 : 981282 : tree block = gimple_block (stmt);
912 : 981282 : gimple_set_location (stmt, loc);
913 : 981282 : gimple_set_block (stmt, block);
914 : : }
915 : : }
916 : :
917 : 1064851 : if (outer_state->tf)
918 : 595110 : region = outer_state->tf->try_finally_expr;
919 : 1064851 : collect_finally_tree_1 (new_seq, region);
920 : :
921 : 1064851 : 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 : 115441 : lower_try_finally_fallthru_label (struct leh_tf_state *tf)
930 : : {
931 : 115441 : tree label = tf->fallthru_label;
932 : 115441 : treemple temp;
933 : :
934 : 115441 : if (!label)
935 : : {
936 : 115441 : label = create_artificial_label (gimple_location (tf->try_finally_expr));
937 : 115441 : tf->fallthru_label = label;
938 : 115441 : if (tf->outer->tf)
939 : : {
940 : 55512 : temp.t = label;
941 : 55512 : record_in_finally_tree (temp, tf->outer->tf->try_finally_expr);
942 : : }
943 : : }
944 : 115441 : 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 : 2740075 : get_eh_else (gimple_seq finally)
952 : : {
953 : 2740075 : gimple *x = gimple_seq_first_stmt (finally);
954 : 2740075 : if (x && gimple_code (x) == GIMPLE_EH_ELSE)
955 : : {
956 : 645 : gcc_assert (gimple_seq_singleton_p (finally));
957 : 645 : 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 : 863599 : honor_protect_cleanup_actions (struct leh_state *outer_state,
984 : : struct leh_state *this_state,
985 : : struct leh_tf_state *tf)
986 : : {
987 : 863599 : 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 : 863599 : if (geh_else *eh_else = get_eh_else (finally))
994 : : {
995 : 267 : gimple_try_set_cleanup (tf->top_p, gimple_eh_else_n_body (eh_else));
996 : 267 : 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 : 267 : eh_region save_ehp = outer_state->ehp_region;
1003 : 267 : outer_state->ehp_region = this_state->cur_region;
1004 : 267 : lower_eh_constructs_1 (outer_state, &finally);
1005 : 267 : outer_state->ehp_region = save_ehp;
1006 : : }
1007 : : else
1008 : : {
1009 : : /* First check for nothing to do. */
1010 : 863332 : if (lang_hooks.eh_protect_cleanup_actions == NULL)
1011 : 182264 : return;
1012 : 681068 : tree actions = lang_hooks.eh_protect_cleanup_actions ();
1013 : 681068 : if (actions == NULL)
1014 : : return;
1015 : :
1016 : 681068 : if (this_state)
1017 : 620650 : finally = lower_try_finally_dup_block (finally, outer_state,
1018 : 620650 : 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 : 681068 : gimple_stmt_iterator gsi = gsi_start (finally);
1028 : 681068 : gimple *x = !gsi_end_p (gsi) ? gsi_stmt (gsi) : NULL;
1029 : 681052 : if (x
1030 : 681052 : && gimple_code (x) == GIMPLE_TRY
1031 : 5894 : && gimple_try_kind (x) == GIMPLE_TRY_CATCH
1032 : 5894 : && 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 : 681068 : geh_mnt *eh_mnt = gimple_build_eh_must_not_throw (actions);
1040 : 681068 : gtry *try_stmt = gimple_build_try (finally,
1041 : : gimple_seq_alloc_with_stmt (eh_mnt),
1042 : : GIMPLE_TRY_CATCH);
1043 : 681068 : finally = lower_eh_must_not_throw (outer_state, try_stmt);
1044 : : }
1045 : :
1046 : : /* Drop all of this into the exception sequence. */
1047 : 681335 : emit_post_landing_pad (&eh_seq, tf->region);
1048 : 681335 : gimple_seq_add_seq (&eh_seq, finally);
1049 : 681335 : if (gimple_seq_may_fallthru (finally))
1050 : 681335 : 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 : 681335 : 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 : 2 : lower_try_finally_nofallthru (struct leh_state *state,
1064 : : struct leh_tf_state *tf)
1065 : : {
1066 : 2 : tree lab;
1067 : 2 : gimple *x;
1068 : 2 : geh_else *eh_else;
1069 : 2 : gimple_seq finally;
1070 : 2 : struct goto_queue_node *q, *qe;
1071 : :
1072 : 2 : lab = create_artificial_label (gimple_location (tf->try_finally_expr));
1073 : :
1074 : : /* We expect that tf->top_p is a GIMPLE_TRY. */
1075 : 2 : finally = gimple_try_cleanup (tf->top_p);
1076 : 2 : tf->top_p_seq = gimple_try_eval (tf->top_p);
1077 : :
1078 : 2 : x = gimple_build_label (lab);
1079 : 2 : gimple_seq_add_stmt (&tf->top_p_seq, x);
1080 : :
1081 : 2 : q = tf->goto_queue;
1082 : 2 : qe = q + tf->goto_queue_active;
1083 : 2 : 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 : 2 : replace_goto_queue (tf);
1090 : :
1091 : : /* Emit the finally block into the stream. Lower EH_ELSE at this time. */
1092 : 2 : eh_else = get_eh_else (finally);
1093 : 2 : 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 : 2 : lower_eh_constructs_1 (state, &finally);
1111 : 2 : gimple_seq_add_seq (&tf->top_p_seq, finally);
1112 : :
1113 : 2 : if (tf->may_throw)
1114 : : {
1115 : 2 : emit_post_landing_pad (&eh_seq, tf->region);
1116 : :
1117 : 2 : x = gimple_build_goto (lab);
1118 : 2 : gimple_set_location (x, gimple_location (tf->try_finally_expr));
1119 : 2 : gimple_seq_add_stmt (&eh_seq, x);
1120 : : }
1121 : : }
1122 : 2 : }
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 : 1455546 : lower_try_finally_onedest (struct leh_state *state, struct leh_tf_state *tf)
1130 : : {
1131 : 1455546 : struct goto_queue_node *q, *qe;
1132 : 1455546 : geh_else *eh_else;
1133 : 1455546 : glabel *label_stmt;
1134 : 1455546 : gimple *x;
1135 : 1455546 : gimple_seq finally;
1136 : 1455546 : gimple_stmt_iterator gsi;
1137 : 1455546 : tree finally_label;
1138 : 1455546 : location_t loc = gimple_location (tf->try_finally_expr);
1139 : :
1140 : 1455546 : finally = gimple_try_cleanup (tf->top_p);
1141 : 1455546 : 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 : 1455546 : eh_else = get_eh_else (finally);
1147 : 1455546 : 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 : 1455546 : lower_eh_constructs_1 (state, &finally);
1156 : :
1157 : 3653305 : for (gsi = gsi_start (finally); !gsi_end_p (gsi); gsi_next (&gsi))
1158 : : {
1159 : 2197759 : gimple *stmt = gsi_stmt (gsi);
1160 : 2197759 : if (LOCATION_LOCUS (gimple_location (stmt)) == UNKNOWN_LOCATION)
1161 : : {
1162 : 1103640 : tree block = gimple_block (stmt);
1163 : 1103640 : gimple_set_location (stmt, gimple_location (tf->try_finally_expr));
1164 : 1103640 : gimple_set_block (stmt, block);
1165 : : }
1166 : : }
1167 : :
1168 : 1455546 : 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 : 1148 : emit_post_landing_pad (&eh_seq, tf->region);
1173 : 1148 : gimple_seq_add_seq (&eh_seq, finally);
1174 : 1148 : emit_resx (&eh_seq, tf->region);
1175 : 1087284 : return;
1176 : : }
1177 : :
1178 : 1454398 : 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 : 1084988 : gimple_seq_add_seq (&tf->top_p_seq, finally);
1183 : 1084988 : return;
1184 : : }
1185 : :
1186 : 369410 : finally_label = create_artificial_label (loc);
1187 : 369410 : label_stmt = gimple_build_label (finally_label);
1188 : 369410 : gimple_seq_add_stmt (&tf->top_p_seq, label_stmt);
1189 : :
1190 : 369410 : gimple_seq_add_seq (&tf->top_p_seq, finally);
1191 : :
1192 : 369410 : q = tf->goto_queue;
1193 : 369410 : qe = q + tf->goto_queue_active;
1194 : :
1195 : 369410 : 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 : 803498 : for (; q < qe; ++q)
1206 : 434088 : do_goto_redirection (q, finally_label, NULL, tf);
1207 : 369410 : replace_goto_queue (tf);
1208 : :
1209 : 369410 : 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 : 369410 : x = tf->goto_queue[0].cont_stmt;
1222 : 369410 : gimple_seq_add_stmt (&tf->top_p_seq, x);
1223 : 369410 : 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 : 206934 : lower_try_finally_copy (struct leh_state *state, struct leh_tf_state *tf)
1232 : : {
1233 : 206934 : gimple_seq finally;
1234 : 206934 : gimple_seq new_stmt;
1235 : 206934 : gimple_seq seq;
1236 : 206934 : gimple *x;
1237 : 206934 : geh_else *eh_else;
1238 : 206934 : tree tmp;
1239 : 206934 : location_t tf_loc = gimple_location (tf->try_finally_expr);
1240 : :
1241 : 206934 : 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 : 206934 : eh_else = get_eh_else (finally);
1246 : 206934 : if (eh_else)
1247 : 6 : finally = gimple_eh_else_n_body (eh_else);
1248 : :
1249 : 206934 : tf->top_p_seq = gimple_try_eval (tf->top_p);
1250 : 206934 : new_stmt = NULL;
1251 : :
1252 : 206934 : if (tf->may_fallthru)
1253 : : {
1254 : 112110 : seq = lower_try_finally_dup_block (finally, state, tf_loc);
1255 : 112110 : lower_eh_constructs_1 (state, &seq);
1256 : 112110 : gimple_seq_add_seq (&new_stmt, seq);
1257 : :
1258 : 112110 : tmp = lower_try_finally_fallthru_label (tf);
1259 : 112110 : x = gimple_build_goto (tmp);
1260 : 112110 : gimple_set_location (x, tf_loc);
1261 : 112110 : gimple_seq_add_stmt (&new_stmt, x);
1262 : : }
1263 : :
1264 : 206934 : 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 : 178333 : if (eh_else)
1269 : 0 : seq = gimple_eh_else_e_body (eh_else);
1270 : : else
1271 : 178333 : seq = lower_try_finally_dup_block (finally, state, tf_loc);
1272 : 178333 : lower_eh_constructs_1 (state, &seq);
1273 : :
1274 : 178333 : emit_post_landing_pad (&eh_seq, tf->region);
1275 : 178333 : gimple_seq_add_seq (&eh_seq, seq);
1276 : 178333 : emit_resx (&eh_seq, tf->region);
1277 : : }
1278 : :
1279 : 206934 : if (tf->goto_queue)
1280 : : {
1281 : 149880 : struct goto_queue_node *q, *qe;
1282 : 149880 : int return_index, index;
1283 : 149880 : struct labels_s
1284 : : {
1285 : : struct goto_queue_node *q;
1286 : : tree label;
1287 : : } *labels;
1288 : :
1289 : 149880 : return_index = tf->dest_array.length ();
1290 : 149880 : labels = XCNEWVEC (struct labels_s, return_index + 1);
1291 : :
1292 : 149880 : q = tf->goto_queue;
1293 : 149880 : qe = q + tf->goto_queue_active;
1294 : 404512 : for (; q < qe; q++)
1295 : : {
1296 : 254632 : index = q->index < 0 ? return_index : q->index;
1297 : :
1298 : 254632 : if (!labels[index].q)
1299 : 153758 : labels[index].q = q;
1300 : : }
1301 : :
1302 : 453518 : for (index = 0; index < return_index + 1; index++)
1303 : : {
1304 : 303638 : tree lab;
1305 : :
1306 : 303638 : q = labels[index].q;
1307 : 303638 : if (! q)
1308 : 149880 : continue;
1309 : :
1310 : 307516 : lab = labels[index].label
1311 : 153758 : = create_artificial_label (tf_loc);
1312 : :
1313 : 153758 : if (index == return_index)
1314 : 0 : do_return_redirection (q, lab, NULL);
1315 : : else
1316 : 153758 : do_goto_redirection (q, lab, NULL, tf);
1317 : :
1318 : 153758 : x = gimple_build_label (lab);
1319 : 153758 : gimple_seq_add_stmt (&new_stmt, x);
1320 : :
1321 : 153758 : seq = lower_try_finally_dup_block (finally, state, q->location);
1322 : 153758 : lower_eh_constructs_1 (state, &seq);
1323 : 153758 : gimple_seq_add_seq (&new_stmt, seq);
1324 : :
1325 : 153758 : gimple_seq_add_stmt (&new_stmt, q->cont_stmt);
1326 : 153758 : maybe_record_in_goto_queue (state, q->cont_stmt);
1327 : : }
1328 : :
1329 : 404512 : for (q = tf->goto_queue; q < qe; q++)
1330 : : {
1331 : 254632 : tree lab;
1332 : :
1333 : 254632 : index = q->index < 0 ? return_index : q->index;
1334 : :
1335 : 254632 : if (labels[index].q == q)
1336 : 153758 : continue;
1337 : :
1338 : 100874 : lab = labels[index].label;
1339 : :
1340 : 100874 : if (index == return_index)
1341 : 0 : do_return_redirection (q, lab, NULL);
1342 : : else
1343 : 100874 : do_goto_redirection (q, lab, NULL, tf);
1344 : : }
1345 : :
1346 : 149880 : replace_goto_queue (tf);
1347 : 149880 : 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 : 206934 : gimple_seq_add_seq (&tf->top_p_seq, new_stmt);
1353 : 206934 : }
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 : 3530 : lower_try_finally_switch (struct leh_state *state, struct leh_tf_state *tf)
1362 : : {
1363 : 3530 : struct goto_queue_node *q, *qe;
1364 : 3530 : tree finally_tmp, finally_label;
1365 : 3530 : int return_index, eh_index, fallthru_index;
1366 : 3530 : int nlabels, ndests, j, last_case_index;
1367 : 3530 : tree last_case;
1368 : 3530 : auto_vec<tree> case_label_vec;
1369 : 3530 : gimple_seq switch_body = NULL;
1370 : 3530 : gimple *x;
1371 : 3530 : geh_else *eh_else;
1372 : 3530 : tree tmp;
1373 : 3530 : gimple *switch_stmt;
1374 : 3530 : gimple_seq finally;
1375 : 3530 : hash_map<tree, gimple *> *cont_map = NULL;
1376 : : /* The location of the TRY_FINALLY stmt. */
1377 : 3530 : location_t tf_loc = gimple_location (tf->try_finally_expr);
1378 : : /* The location of the finally block. */
1379 : 3530 : location_t finally_loc;
1380 : :
1381 : 3530 : finally = gimple_try_cleanup (tf->top_p);
1382 : 3530 : eh_else = get_eh_else (finally);
1383 : :
1384 : : /* Mash the TRY block to the head of the chain. */
1385 : 3530 : 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 : 3530 : x = gimple_seq_last_stmt (finally);
1390 : 3530 : finally_loc = x ? gimple_location (x) : tf_loc;
1391 : :
1392 : : /* Prepare for switch statement generation. */
1393 : 3530 : nlabels = tf->dest_array.length ();
1394 : 3530 : return_index = nlabels;
1395 : 3530 : eh_index = return_index + tf->may_return;
1396 : 3530 : fallthru_index = eh_index + (tf->may_throw && !eh_else);
1397 : 3530 : ndests = fallthru_index + tf->may_fallthru;
1398 : :
1399 : 3530 : finally_tmp = create_tmp_var (integer_type_node, "finally_tmp");
1400 : 3530 : 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 : 3530 : case_label_vec.create (ndests);
1406 : 3530 : last_case = NULL;
1407 : 3530 : 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 : 3530 : if (tf->may_fallthru)
1414 : : {
1415 : 3331 : x = gimple_build_assign (finally_tmp,
1416 : : build_int_cst (integer_type_node,
1417 : 3331 : fallthru_index));
1418 : 3331 : gimple_set_location (x, finally_loc);
1419 : 3331 : gimple_seq_add_stmt (&tf->top_p_seq, x);
1420 : :
1421 : 3331 : tmp = build_int_cst (integer_type_node, fallthru_index);
1422 : 3331 : last_case = build_case_label (tmp, NULL,
1423 : : create_artificial_label (finally_loc));
1424 : 3331 : case_label_vec.quick_push (last_case);
1425 : 3331 : last_case_index++;
1426 : :
1427 : 3331 : x = gimple_build_label (CASE_LABEL (last_case));
1428 : 3331 : gimple_seq_add_stmt (&switch_body, x);
1429 : :
1430 : 3331 : tmp = lower_try_finally_fallthru_label (tf);
1431 : 3331 : x = gimple_build_goto (tmp);
1432 : 3331 : gimple_set_location (x, finally_loc);
1433 : 3331 : 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 : 3530 : 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 : 3524 : else if (tf->may_throw)
1453 : : {
1454 : 2781 : emit_post_landing_pad (&eh_seq, tf->region);
1455 : :
1456 : 2781 : x = gimple_build_assign (finally_tmp,
1457 : 2781 : build_int_cst (integer_type_node, eh_index));
1458 : 2781 : gimple_seq_add_stmt (&eh_seq, x);
1459 : :
1460 : 2781 : x = gimple_build_goto (finally_label);
1461 : 2781 : gimple_set_location (x, tf_loc);
1462 : 2781 : gimple_seq_add_stmt (&eh_seq, x);
1463 : :
1464 : 2781 : tmp = build_int_cst (integer_type_node, eh_index);
1465 : 2781 : last_case = build_case_label (tmp, NULL,
1466 : : create_artificial_label (tf_loc));
1467 : 2781 : case_label_vec.quick_push (last_case);
1468 : 2781 : last_case_index++;
1469 : :
1470 : 2781 : x = gimple_build_label (CASE_LABEL (last_case));
1471 : 2781 : gimple_seq_add_stmt (&eh_seq, x);
1472 : 2781 : emit_resx (&eh_seq, tf->region);
1473 : : }
1474 : :
1475 : 3530 : x = gimple_build_label (finally_label);
1476 : 3530 : gimple_seq_add_stmt (&tf->top_p_seq, x);
1477 : :
1478 : 3530 : lower_eh_constructs_1 (state, &finally);
1479 : 3530 : gimple_seq_add_seq (&tf->top_p_seq, finally);
1480 : :
1481 : : /* Redirect each incoming goto edge. */
1482 : 3530 : q = tf->goto_queue;
1483 : 3530 : qe = q + tf->goto_queue_active;
1484 : 3530 : 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 : 4837 : for (; q < qe; ++q)
1488 : : {
1489 : 1307 : gimple_seq mod = NULL;
1490 : 1307 : int switch_id;
1491 : 1307 : unsigned int case_index;
1492 : :
1493 : 1307 : if (q->index < 0)
1494 : : {
1495 : 0 : x = gimple_build_assign (finally_tmp,
1496 : : build_int_cst (integer_type_node,
1497 : 0 : 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 : 1307 : x = gimple_build_assign (finally_tmp,
1505 : 1307 : build_int_cst (integer_type_node, q->index));
1506 : 1307 : gimple_seq_add_stmt (&mod, x);
1507 : 1307 : do_goto_redirection (q, finally_label, mod, tf);
1508 : 1307 : switch_id = q->index;
1509 : : }
1510 : :
1511 : 1307 : case_index = j + q->index;
1512 : 1512 : if (case_label_vec.length () <= case_index || !case_label_vec[case_index])
1513 : : {
1514 : 1102 : tree case_lab;
1515 : 1102 : tmp = build_int_cst (integer_type_node, switch_id);
1516 : 1102 : 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 : 1102 : if (!cont_map)
1521 : 830 : cont_map = new hash_map<tree, gimple *>;
1522 : 1102 : cont_map->put (case_lab, q->cont_stmt);
1523 : 1102 : case_label_vec.quick_push (case_lab);
1524 : : }
1525 : : }
1526 : 4632 : for (j = last_case_index; j < last_case_index + nlabels; j++)
1527 : : {
1528 : 1102 : gimple *cont_stmt;
1529 : :
1530 : 1102 : last_case = case_label_vec[j];
1531 : :
1532 : 1102 : gcc_assert (last_case);
1533 : 1102 : gcc_assert (cont_map);
1534 : :
1535 : 1102 : cont_stmt = *cont_map->get (last_case);
1536 : :
1537 : 1102 : x = gimple_build_label (CASE_LABEL (last_case));
1538 : 1102 : gimple_seq_add_stmt (&switch_body, x);
1539 : 1102 : gimple_seq_add_stmt (&switch_body, cont_stmt);
1540 : 1102 : maybe_record_in_goto_queue (state, cont_stmt);
1541 : : }
1542 : 3530 : if (cont_map)
1543 : 830 : delete cont_map;
1544 : :
1545 : 3530 : 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 : 3530 : CASE_LOW (last_case) = NULL;
1550 : 3530 : tree tem = case_label_vec.pop ();
1551 : 3530 : gcc_assert (tem == last_case);
1552 : 3530 : sort_case_labels (case_label_vec);
1553 : :
1554 : : /* Build the switch statement, setting last_case to be the default
1555 : : label. */
1556 : 3530 : switch_stmt = gimple_build_switch (finally_tmp, last_case,
1557 : : case_label_vec);
1558 : 3530 : 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 : 3530 : gimple_seq_add_stmt (&tf->top_p_seq, switch_stmt);
1563 : 3530 : gimple_seq_add_seq (&tf->top_p_seq, switch_body);
1564 : 3530 : }
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 : 210464 : decide_copy_try_finally (int ndests, bool may_throw, gimple_seq finally)
1576 : : {
1577 : 210464 : int f_estimate, sw_estimate;
1578 : 210464 : 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 : 210464 : eh_else = get_eh_else (finally);
1583 : 210464 : if (eh_else)
1584 : : {
1585 : 12 : ndests -= may_throw;
1586 : 12 : finally = gimple_eh_else_n_body (eh_else);
1587 : : }
1588 : :
1589 : 210464 : if (!optimize)
1590 : : {
1591 : 17701 : gimple_stmt_iterator gsi;
1592 : :
1593 : 17701 : if (ndests == 1)
1594 : : return true;
1595 : :
1596 : 47839 : 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 : 33648 : gimple *stmt = gsi_stmt (gsi);
1601 : 33648 : if (!is_gimple_debug (stmt)
1602 : 33648 : && !gimple_clobber_p (stmt)
1603 : 37275 : && !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 : 192763 : f_estimate = estimate_num_insns_seq (finally, &eni_size_weights);
1611 : 192763 : f_estimate = (f_estimate + 1) * ndests;
1612 : :
1613 : : /* Switch statement (cost 10), N variable assignments, N gotos. */
1614 : 192763 : sw_estimate = 10 + 2 * ndests;
1615 : :
1616 : : /* Optimize for size clearly wants our best guess. */
1617 : 192763 : if (optimize_function_for_size_p (cfun))
1618 : 2357 : return f_estimate < sw_estimate;
1619 : :
1620 : : /* ??? These numbers are completely made up so far. */
1621 : 190406 : if (optimize > 1)
1622 : 373734 : return f_estimate < 100 || f_estimate < sw_estimate * 2;
1623 : : else
1624 : 7071 : 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 : 1484613 : cleanup_is_dead_in (leh_state *state)
1637 : : {
1638 : 1484613 : if (flag_checking)
1639 : : {
1640 : 1484601 : eh_region reg = state->cur_region;
1641 : 27201672 : while (reg && reg->type == ERT_CLEANUP)
1642 : 25717071 : reg = reg->outer;
1643 : :
1644 : 1484601 : gcc_assert (reg == state->outer_non_cleanup);
1645 : : }
1646 : :
1647 : 1484613 : eh_region reg = state->outer_non_cleanup;
1648 : 1484613 : 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 : 1683985 : lower_try_finally (struct leh_state *state, gtry *tp)
1658 : : {
1659 : 1683985 : struct leh_tf_state this_tf;
1660 : 1683985 : struct leh_state this_state;
1661 : 1683985 : int ndests;
1662 : 1683985 : gimple_seq old_eh_seq;
1663 : :
1664 : : /* Process the try block. */
1665 : :
1666 : 1683985 : memset (&this_tf, 0, sizeof (this_tf));
1667 : 1683985 : this_tf.try_finally_expr = tp;
1668 : 1683985 : this_tf.top_p = tp;
1669 : 1683985 : this_tf.outer = state;
1670 : 1683985 : if (using_eh_for_cleanups_p () && !cleanup_is_dead_in (state))
1671 : : {
1672 : 1141267 : this_tf.region = gen_eh_region_cleanup (state->cur_region);
1673 : 1141267 : this_state.cur_region = this_tf.region;
1674 : : }
1675 : : else
1676 : : {
1677 : 542718 : this_tf.region = NULL;
1678 : 542718 : this_state.cur_region = state->cur_region;
1679 : : }
1680 : :
1681 : 1683985 : this_state.outer_non_cleanup = state->outer_non_cleanup;
1682 : 1683985 : this_state.ehp_region = state->ehp_region;
1683 : 1683985 : this_state.tf = &this_tf;
1684 : :
1685 : 1683985 : old_eh_seq = eh_seq;
1686 : 1683985 : eh_seq = NULL;
1687 : :
1688 : 1683985 : lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1689 : :
1690 : : /* Determine if the try block is escaped through the bottom. */
1691 : 1683985 : 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 : 1683985 : if (this_tf.region)
1695 : 1141267 : this_tf.may_throw = eh_region_may_contain_throw (this_tf.region);
1696 : 1683985 : if (this_tf.may_throw)
1697 : 803181 : 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 : 1683985 : ndests = this_tf.dest_array.length ();
1704 : 1683985 : ndests += this_tf.may_fallthru;
1705 : 1683985 : ndests += this_tf.may_return;
1706 : 1683985 : ndests += this_tf.may_throw;
1707 : :
1708 : : /* If the FINALLY block is not reachable, dike it out. */
1709 : 1683985 : if (ndests == 0)
1710 : : {
1711 : 17973 : gimple_seq_add_seq (&this_tf.top_p_seq, gimple_try_eval (tp));
1712 : 17973 : 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 : 1666012 : else if (!gimple_seq_may_fallthru (gimple_try_cleanup (tp)))
1718 : 2 : lower_try_finally_nofallthru (state, &this_tf);
1719 : :
1720 : : /* We can easily special-case redirection to a single destination. */
1721 : 1666010 : else if (ndests == 1)
1722 : 1455546 : lower_try_finally_onedest (state, &this_tf);
1723 : 210464 : else if (decide_copy_try_finally (ndests, this_tf.may_throw,
1724 : : gimple_try_cleanup (tp)))
1725 : 206934 : lower_try_finally_copy (state, &this_tf);
1726 : : else
1727 : 3530 : 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 : 1683985 : if (this_tf.fallthru_label)
1732 : : {
1733 : : /* This must be reached only if ndests == 0. */
1734 : 115441 : gimple *x = gimple_build_label (this_tf.fallthru_label);
1735 : 115441 : gimple_seq_add_stmt (&this_tf.top_p_seq, x);
1736 : : }
1737 : :
1738 : 1683985 : this_tf.dest_array.release ();
1739 : 1683985 : free (this_tf.goto_queue);
1740 : 1683985 : if (this_tf.goto_queue_map)
1741 : 329 : 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 : 1683985 : if (old_eh_seq)
1746 : : {
1747 : 146085 : if (eh_seq == NULL)
1748 : 13903 : eh_seq = old_eh_seq;
1749 : : else
1750 : : {
1751 : 132182 : gimple_seq new_eh_seq = eh_seq;
1752 : 132182 : eh_seq = old_eh_seq;
1753 : 132182 : gimple_seq_add_seq (&eh_seq, new_eh_seq);
1754 : : }
1755 : : }
1756 : :
1757 : 1683985 : 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 : 45752 : lower_catch (struct leh_state *state, gtry *tp)
1766 : : {
1767 : 45752 : eh_region try_region = NULL;
1768 : 45752 : struct leh_state this_state = *state;
1769 : 45752 : gimple_stmt_iterator gsi;
1770 : 45752 : tree out_label;
1771 : 45752 : gimple_seq new_seq, cleanup;
1772 : 45752 : gimple *x;
1773 : 45752 : geh_dispatch *eh_dispatch;
1774 : 45752 : location_t try_catch_loc = gimple_location (tp);
1775 : 45752 : location_t catch_loc = UNKNOWN_LOCATION;
1776 : :
1777 : 45752 : if (flag_exceptions)
1778 : : {
1779 : 45746 : try_region = gen_eh_region_try (state->cur_region);
1780 : 45746 : this_state.cur_region = try_region;
1781 : 45746 : this_state.outer_non_cleanup = this_state.cur_region;
1782 : : }
1783 : :
1784 : 45752 : lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1785 : :
1786 : 45752 : if (!eh_region_may_contain_throw (try_region))
1787 : 4528 : return gimple_try_eval (tp);
1788 : :
1789 : 41224 : new_seq = NULL;
1790 : 41224 : eh_dispatch = gimple_build_eh_dispatch (try_region->index);
1791 : 41224 : gimple_seq_add_stmt (&new_seq, eh_dispatch);
1792 : 41224 : emit_resx (&new_seq, try_region);
1793 : :
1794 : 41224 : this_state.cur_region = state->cur_region;
1795 : 41224 : this_state.outer_non_cleanup = state->outer_non_cleanup;
1796 : 41224 : 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 : 41224 : gimple_seq old_eh_seq = eh_seq;
1802 : 41224 : eh_seq = NULL;
1803 : :
1804 : 41224 : out_label = NULL;
1805 : 41224 : cleanup = gimple_try_cleanup (tp);
1806 : 41224 : for (gsi = gsi_start (cleanup);
1807 : 47034 : !gsi_end_p (gsi);
1808 : 5810 : gsi_next (&gsi))
1809 : : {
1810 : 44091 : eh_catch c;
1811 : 44091 : gcatch *catch_stmt;
1812 : 44091 : gimple_seq handler;
1813 : :
1814 : 44091 : catch_stmt = as_a <gcatch *> (gsi_stmt (gsi));
1815 : 44091 : if (catch_loc == UNKNOWN_LOCATION)
1816 : 41224 : catch_loc = gimple_location (catch_stmt);
1817 : 44091 : c = gen_eh_region_catch (try_region, gimple_catch_types (catch_stmt));
1818 : :
1819 : 44091 : handler = gimple_catch_handler (catch_stmt);
1820 : 44091 : lower_eh_constructs_1 (&this_state, &handler);
1821 : :
1822 : 44091 : c->label = create_artificial_label (UNKNOWN_LOCATION);
1823 : 44091 : x = gimple_build_label (c->label);
1824 : 44091 : gimple_seq_add_stmt (&new_seq, x);
1825 : :
1826 : 44091 : gimple_seq_add_seq (&new_seq, handler);
1827 : :
1828 : 44091 : if (gimple_seq_may_fallthru (new_seq))
1829 : : {
1830 : 13763 : if (!out_label)
1831 : 13216 : out_label = create_artificial_label (try_catch_loc);
1832 : :
1833 : 13763 : x = gimple_build_goto (out_label);
1834 : 13763 : gimple_seq_add_stmt (&new_seq, x);
1835 : : }
1836 : 44091 : 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 : 41224 : gimple_set_location (eh_dispatch, catch_loc);
1843 : :
1844 : 41224 : gimple_try_set_cleanup (tp, new_seq);
1845 : :
1846 : 41224 : gimple_seq new_eh_seq = eh_seq;
1847 : 41224 : eh_seq = old_eh_seq;
1848 : 41224 : gimple_seq ret_seq = frob_into_branch_around (tp, try_region, out_label);
1849 : 41224 : gimple_seq_add_seq (&eh_seq, new_eh_seq);
1850 : 41224 : 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 : 5769 : lower_eh_filter (struct leh_state *state, gtry *tp)
1859 : : {
1860 : 5769 : struct leh_state this_state = *state;
1861 : 5769 : eh_region this_region = NULL;
1862 : 5769 : gimple *inner, *x;
1863 : 5769 : gimple_seq new_seq;
1864 : :
1865 : 5769 : inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1866 : :
1867 : 5769 : if (flag_exceptions)
1868 : : {
1869 : 5769 : this_region = gen_eh_region_allowed (state->cur_region,
1870 : : gimple_eh_filter_types (inner));
1871 : 5769 : this_state.cur_region = this_region;
1872 : 5769 : this_state.outer_non_cleanup = this_state.cur_region;
1873 : : }
1874 : :
1875 : 5769 : lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1876 : :
1877 : 5769 : if (!eh_region_may_contain_throw (this_region))
1878 : 5210 : return gimple_try_eval (tp);
1879 : :
1880 : 559 : this_state.cur_region = state->cur_region;
1881 : 559 : this_state.ehp_region = this_region;
1882 : :
1883 : 559 : new_seq = NULL;
1884 : 559 : x = gimple_build_eh_dispatch (this_region->index);
1885 : 559 : gimple_set_location (x, gimple_location (tp));
1886 : 559 : gimple_seq_add_stmt (&new_seq, x);
1887 : 559 : emit_resx (&new_seq, this_region);
1888 : :
1889 : 559 : this_region->u.allowed.label = create_artificial_label (UNKNOWN_LOCATION);
1890 : 559 : x = gimple_build_label (this_region->u.allowed.label);
1891 : 559 : gimple_seq_add_stmt (&new_seq, x);
1892 : :
1893 : 559 : lower_eh_constructs_1 (&this_state, gimple_eh_filter_failure_ptr (inner));
1894 : 559 : gimple_seq_add_seq (&new_seq, gimple_eh_filter_failure (inner));
1895 : :
1896 : 559 : gimple_try_set_cleanup (tp, new_seq);
1897 : :
1898 : 559 : 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 : 1343403 : lower_eh_must_not_throw (struct leh_state *state, gtry *tp)
1907 : : {
1908 : 1343403 : struct leh_state this_state = *state;
1909 : :
1910 : 1343403 : if (flag_exceptions)
1911 : : {
1912 : 1343403 : gimple *inner = gimple_seq_first_stmt (gimple_try_cleanup (tp));
1913 : 1343403 : eh_region this_region;
1914 : :
1915 : 1343403 : this_region = gen_eh_region_must_not_throw (state->cur_region);
1916 : 1343403 : this_region->u.must_not_throw.failure_decl
1917 : 1343403 : = gimple_eh_must_not_throw_fndecl (
1918 : 1343403 : as_a <geh_mnt *> (inner));
1919 : 1343403 : this_region->u.must_not_throw.failure_loc
1920 : 1343403 : = 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 : 1343403 : TREE_USED (this_region->u.must_not_throw.failure_decl) = 1;
1926 : :
1927 : 1343403 : this_state.cur_region = this_region;
1928 : 1343403 : this_state.outer_non_cleanup = this_state.cur_region;
1929 : : }
1930 : :
1931 : 1343403 : lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1932 : :
1933 : 1343403 : 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 : 161207 : lower_cleanup (struct leh_state *state, gtry *tp)
1941 : : {
1942 : 161207 : struct leh_state this_state = *state;
1943 : 161207 : eh_region this_region = NULL;
1944 : 161207 : struct leh_tf_state fake_tf;
1945 : 161207 : gimple_seq result;
1946 : 161207 : bool cleanup_dead = cleanup_is_dead_in (state);
1947 : :
1948 : 161207 : if (flag_exceptions && !cleanup_dead)
1949 : : {
1950 : 122594 : this_region = gen_eh_region_cleanup (state->cur_region);
1951 : 122594 : this_state.cur_region = this_region;
1952 : 122594 : this_state.outer_non_cleanup = state->outer_non_cleanup;
1953 : : }
1954 : :
1955 : 161207 : lower_eh_constructs_1 (&this_state, gimple_try_eval_ptr (tp));
1956 : :
1957 : 161207 : if (cleanup_dead || !eh_region_may_contain_throw (this_region))
1958 : 100789 : 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 : 60418 : memset (&fake_tf, 0, sizeof (fake_tf));
1963 : 60418 : fake_tf.top_p = fake_tf.try_finally_expr = tp;
1964 : 60418 : fake_tf.outer = state;
1965 : 60418 : fake_tf.region = this_region;
1966 : 60418 : fake_tf.may_fallthru = gimple_seq_may_fallthru (gimple_try_eval (tp));
1967 : 60418 : fake_tf.may_throw = true;
1968 : :
1969 : 60418 : honor_protect_cleanup_actions (state, NULL, &fake_tf);
1970 : :
1971 : 60418 : 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 : 60418 : result = gimple_try_eval (tp);
1985 : 60418 : 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 : 60418 : return result;
1992 : : }
1993 : :
1994 : : /* Main loop for lowering eh constructs. Also moves gsi to the next
1995 : : statement. */
1996 : :
1997 : : static void
1998 : 90420378 : lower_eh_constructs_2 (struct leh_state *state, gimple_stmt_iterator *gsi)
1999 : : {
2000 : 90420378 : gimple_seq replace;
2001 : 90420378 : gimple *x;
2002 : 90420378 : gimple *stmt = gsi_stmt (*gsi);
2003 : :
2004 : 90420378 : switch (gimple_code (stmt))
2005 : : {
2006 : 11067685 : case GIMPLE_CALL:
2007 : 11067685 : {
2008 : 11067685 : tree fndecl = gimple_call_fndecl (stmt);
2009 : 11067685 : tree rhs, lhs;
2010 : :
2011 : 11067685 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
2012 : 2188319 : switch (DECL_FUNCTION_CODE (fndecl))
2013 : : {
2014 : 19258 : 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 : 19258 : if (state->ehp_region)
2019 : : {
2020 : 38506 : tree nr = build_int_cst (integer_type_node,
2021 : 19253 : state->ehp_region->index);
2022 : 19253 : 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 : 19253 : 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 : 2559058 : return;
2047 : :
2048 : : default:
2049 : : break;
2050 : : }
2051 : : }
2052 : : /* FALLTHRU */
2053 : :
2054 : 54091633 : 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 : 54091633 : if (stmt_could_throw_p (cfun, stmt)
2061 : 3556205 : && gimple_has_lhs (stmt)
2062 : 2598208 : && gimple_stmt_may_fallthru (stmt)
2063 : 2598205 : && !tree_could_throw_p (gimple_get_lhs (stmt))
2064 : 56488587 : && is_gimple_reg_type (TREE_TYPE (gimple_get_lhs (stmt))))
2065 : : {
2066 : 1757266 : tree lhs = gimple_get_lhs (stmt);
2067 : 1757266 : tree tmp = create_tmp_var (TREE_TYPE (lhs));
2068 : 1757266 : gimple *s = gimple_build_assign (lhs, tmp);
2069 : 1757266 : gimple_set_location (s, gimple_location (stmt));
2070 : 1757266 : gimple_set_block (s, gimple_block (stmt));
2071 : 1757266 : gimple_set_lhs (stmt, tmp);
2072 : 1757266 : gsi_insert_after (gsi, s, GSI_SAME_STMT);
2073 : : }
2074 : : /* Look for things that can throw exceptions, and record them. */
2075 : 54091633 : if (state->cur_region && stmt_could_throw_p (cfun, stmt))
2076 : : {
2077 : 2727338 : record_stmt_eh_region (state->cur_region, stmt);
2078 : 2727338 : note_eh_region_may_contain_throw (state->cur_region);
2079 : : }
2080 : : break;
2081 : :
2082 : 13925188 : case GIMPLE_COND:
2083 : 13925188 : case GIMPLE_GOTO:
2084 : 13925188 : case GIMPLE_RETURN:
2085 : 13925188 : maybe_record_in_goto_queue (state, stmt);
2086 : 13925188 : break;
2087 : :
2088 : 51964 : case GIMPLE_SWITCH:
2089 : 51964 : verify_norecord_switch_expr (state, as_a <gswitch *> (stmt));
2090 : 51964 : break;
2091 : :
2092 : 2559048 : case GIMPLE_TRY:
2093 : 2559048 : {
2094 : 2559048 : gtry *try_stmt = as_a <gtry *> (stmt);
2095 : 2559048 : if (gimple_try_kind (try_stmt) == GIMPLE_TRY_FINALLY)
2096 : 1683985 : replace = lower_try_finally (state, try_stmt);
2097 : : else
2098 : : {
2099 : 875063 : x = gimple_seq_first_stmt (gimple_try_cleanup (try_stmt));
2100 : 875063 : if (!x)
2101 : : {
2102 : 0 : replace = gimple_try_eval (try_stmt);
2103 : 0 : lower_eh_constructs_1 (state, &replace);
2104 : : }
2105 : : else
2106 : 875063 : switch (gimple_code (x))
2107 : : {
2108 : 45752 : case GIMPLE_CATCH:
2109 : 45752 : replace = lower_catch (state, try_stmt);
2110 : 45752 : break;
2111 : 5769 : case GIMPLE_EH_FILTER:
2112 : 5769 : replace = lower_eh_filter (state, try_stmt);
2113 : 5769 : break;
2114 : 662335 : case GIMPLE_EH_MUST_NOT_THROW:
2115 : 662335 : replace = lower_eh_must_not_throw (state, try_stmt);
2116 : 662335 : break;
2117 : 0 : case GIMPLE_EH_ELSE:
2118 : : /* This code is only valid with GIMPLE_TRY_FINALLY. */
2119 : 0 : gcc_unreachable ();
2120 : 161207 : default:
2121 : 161207 : replace = lower_cleanup (state, try_stmt);
2122 : 161207 : break;
2123 : : }
2124 : : }
2125 : : }
2126 : :
2127 : : /* Remove the old stmt and insert the transformed sequence
2128 : : instead. */
2129 : 2559048 : gsi_insert_seq_before (gsi, replace, GSI_SAME_STMT);
2130 : 2559048 : gsi_remove (gsi, true);
2131 : :
2132 : : /* Return since we don't want gsi_next () */
2133 : 2559048 : 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 : 87861325 : 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 : 8168094 : lower_eh_constructs_1 (struct leh_state *state, gimple_seq *pseq)
2152 : : {
2153 : 8168094 : gimple_stmt_iterator gsi;
2154 : 106600816 : for (gsi = gsi_start (*pseq); !gsi_end_p (gsi);)
2155 : 90420378 : lower_eh_constructs_2 (state, &gsi);
2156 : 8168094 : }
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 : 285689 : pass_lower_eh (gcc::context *ctxt)
2177 : 571378 : : 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 : 2979782 : pass_lower_eh::execute (function *fun)
2187 : : {
2188 : 2979782 : struct leh_state null_state;
2189 : 2979782 : gimple_seq bodyp;
2190 : :
2191 : 2979782 : bodyp = gimple_body (current_function_decl);
2192 : 2979782 : if (bodyp == NULL)
2193 : : return 0;
2194 : :
2195 : 2979782 : finally_tree = new hash_table<finally_tree_hasher> (31);
2196 : 2979782 : eh_region_may_contain_throw_map = BITMAP_ALLOC (NULL);
2197 : 2979782 : memset (&null_state, 0, sizeof (null_state));
2198 : :
2199 : 2979782 : collect_finally_tree_1 (bodyp, NULL);
2200 : 2979782 : lower_eh_constructs_1 (&null_state, &bodyp);
2201 : 2979782 : 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 : 2979782 : gcc_assert (!gimple_seq_may_fallthru (bodyp));
2207 : 2979782 : 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 : 2979782 : gcc_assert (bodyp == gimple_body (current_function_decl));
2212 : :
2213 : 2979782 : delete finally_tree;
2214 : 2979782 : finally_tree = NULL;
2215 : 2979782 : BITMAP_FREE (eh_region_may_contain_throw_map);
2216 : 2979782 : 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 : 2979782 : if (function_needs_eh_personality (fun) == eh_personality_lang
2221 : 2979782 : && !DECL_FUNCTION_PERSONALITY (current_function_decl))
2222 : 842665 : DECL_FUNCTION_PERSONALITY (current_function_decl)
2223 : 1685330 : = lang_hooks.eh_personality ();
2224 : :
2225 : : return 0;
2226 : : }
2227 : :
2228 : : } // anon namespace
2229 : :
2230 : : gimple_opt_pass *
2231 : 285689 : make_pass_lower_eh (gcc::context *ctxt)
2232 : : {
2233 : 285689 : 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 : 50988 : make_eh_dispatch_edges (geh_dispatch *stmt)
2242 : : {
2243 : 50988 : eh_region r;
2244 : 50988 : eh_catch c;
2245 : 50988 : basic_block src, dst;
2246 : :
2247 : 50988 : r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
2248 : 50988 : src = gimple_bb (stmt);
2249 : :
2250 : 50988 : switch (r->type)
2251 : : {
2252 : 50277 : case ERT_TRY:
2253 : 56900 : for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
2254 : : {
2255 : 53407 : dst = label_to_block (cfun, c->label);
2256 : 53407 : make_edge (src, dst, 0);
2257 : :
2258 : : /* A catch-all handler doesn't have a fallthru. */
2259 : 53407 : if (c->type_list == NULL)
2260 : : return false;
2261 : : }
2262 : : break;
2263 : :
2264 : 711 : case ERT_ALLOWED_EXCEPTIONS:
2265 : 711 : dst = label_to_block (cfun, r->u.allowed.label);
2266 : 711 : make_edge (src, dst, 0);
2267 : 711 : 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 : 6126807 : make_eh_edge (gimple *stmt)
2281 : : {
2282 : 6126807 : basic_block src, dst;
2283 : 6126807 : eh_landing_pad lp;
2284 : 6126807 : int lp_nr;
2285 : :
2286 : 6126807 : lp_nr = lookup_stmt_eh_lp (stmt);
2287 : 6126807 : if (lp_nr <= 0)
2288 : : return NULL;
2289 : :
2290 : 3924579 : lp = get_eh_landing_pad_from_number (lp_nr);
2291 : 3924579 : gcc_assert (lp != NULL);
2292 : :
2293 : 3924579 : src = gimple_bb (stmt);
2294 : 3924579 : dst = label_to_block (cfun, lp->post_landing_pad);
2295 : 3924579 : 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 : 2967611 : redirect_eh_edge_1 (edge edge_in, basic_block new_bb, bool change_region)
2309 : : {
2310 : 2967611 : eh_landing_pad old_lp, new_lp;
2311 : 2967611 : basic_block old_bb;
2312 : 2967611 : gimple *throw_stmt;
2313 : 2967611 : int old_lp_nr, new_lp_nr;
2314 : 2967611 : tree old_label, new_label;
2315 : 2967611 : edge_iterator ei;
2316 : 2967611 : edge e;
2317 : :
2318 : 2967611 : old_bb = edge_in->dest;
2319 : 2967611 : old_label = gimple_block_label (old_bb);
2320 : 2967611 : old_lp_nr = EH_LANDING_PAD_NR (old_label);
2321 : 2967611 : gcc_assert (old_lp_nr > 0);
2322 : 2967611 : old_lp = get_eh_landing_pad_from_number (old_lp_nr);
2323 : :
2324 : 2967611 : throw_stmt = *gsi_last_bb (edge_in->src);
2325 : 2967611 : gcc_checking_assert (lookup_stmt_eh_lp (throw_stmt) == old_lp_nr);
2326 : :
2327 : 2967611 : new_label = gimple_block_label (new_bb);
2328 : :
2329 : : /* Look for an existing region that might be using NEW_BB already. */
2330 : 2967611 : new_lp_nr = EH_LANDING_PAD_NR (new_label);
2331 : 2967611 : if (new_lp_nr)
2332 : : {
2333 : 1725743 : new_lp = get_eh_landing_pad_from_number (new_lp_nr);
2334 : 1725743 : 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 : 1725743 : gcc_assert (change_region || new_lp->region == old_lp->region);
2339 : : }
2340 : : else
2341 : : {
2342 : 1241868 : new_lp = NULL;
2343 : 1241868 : gcc_assert (!change_region);
2344 : : }
2345 : :
2346 : : /* Notice when we redirect the last EH edge away from OLD_BB. */
2347 : 12248921 : FOR_EACH_EDGE (e, ei, old_bb->preds)
2348 : 10801258 : if (e != edge_in && (e->flags & EDGE_EH))
2349 : : break;
2350 : :
2351 : 2967611 : 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 : 1725743 : if (e == NULL && !change_region)
2359 : 944617 : 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 : 1241868 : if (e == NULL)
2367 : : {
2368 : 277063 : EH_LANDING_PAD_NR (old_lp->post_landing_pad) = 0;
2369 : 277063 : new_lp = old_lp;
2370 : : }
2371 : : else
2372 : 964805 : new_lp = gen_eh_landing_pad (old_lp->region);
2373 : 1241868 : new_lp->post_landing_pad = new_label;
2374 : 1241868 : EH_LANDING_PAD_NR (new_label) = new_lp->index;
2375 : : }
2376 : :
2377 : : /* Maybe move the throwing statement to the new region. */
2378 : 2967611 : if (old_lp != new_lp)
2379 : : {
2380 : 2690548 : remove_stmt_from_eh_lp (throw_stmt);
2381 : 2690548 : add_stmt_to_eh_lp (throw_stmt, new_lp->index);
2382 : : }
2383 : 2967611 : }
2384 : :
2385 : : /* Redirect EH edge E to NEW_BB. */
2386 : :
2387 : : edge
2388 : 1112489 : redirect_eh_edge (edge edge_in, basic_block new_bb)
2389 : : {
2390 : 1112489 : redirect_eh_edge_1 (edge_in, new_bb, false);
2391 : 1112489 : 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 : 1848457987 : 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 : 1848457987 : *handled = true;
2448 : 1848457987 : switch (op)
2449 : : {
2450 : 2112615 : case TRUNC_DIV_EXPR:
2451 : 2112615 : case CEIL_DIV_EXPR:
2452 : 2112615 : case FLOOR_DIV_EXPR:
2453 : 2112615 : case ROUND_DIV_EXPR:
2454 : 2112615 : case EXACT_DIV_EXPR:
2455 : 2112615 : case CEIL_MOD_EXPR:
2456 : 2112615 : case FLOOR_MOD_EXPR:
2457 : 2112615 : case ROUND_MOD_EXPR:
2458 : 2112615 : case TRUNC_MOD_EXPR:
2459 : 2112615 : if (!TREE_CONSTANT (divisor) || integer_zerop (divisor))
2460 : 677781 : return true;
2461 : 1434834 : 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 : 4706292 : 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 : 417693 : case RDIV_EXPR:
2479 : 417693 : if (fp_operation)
2480 : : {
2481 : 417693 : if (honor_snans)
2482 : : return true;
2483 : 409864 : 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 : 207278454 : case EQ_EXPR:
2502 : 207278454 : case NE_EXPR:
2503 : 207278454 : case UNORDERED_EXPR:
2504 : 207278454 : case ORDERED_EXPR:
2505 : 207278454 : case UNLT_EXPR:
2506 : 207278454 : case UNLE_EXPR:
2507 : 207278454 : case UNGT_EXPR:
2508 : 207278454 : case UNGE_EXPR:
2509 : 207278454 : case UNEQ_EXPR:
2510 : 207278454 : return honor_snans;
2511 : :
2512 : 1324013 : case NEGATE_EXPR:
2513 : 1324013 : case ABS_EXPR:
2514 : 1324013 : case CONJ_EXPR:
2515 : : /* These operations don't trap with floating point. */
2516 : 1324013 : if (honor_trapv)
2517 : : return true;
2518 : : return false;
2519 : :
2520 : : case ABSU_EXPR:
2521 : : /* ABSU_EXPR never traps. */
2522 : : return false;
2523 : :
2524 : 81902011 : case PLUS_EXPR:
2525 : 81902011 : case MINUS_EXPR:
2526 : 81902011 : case MULT_EXPR:
2527 : : /* Any floating arithmetic may trap. */
2528 : 81902011 : if (fp_operation && flag_trapping_math)
2529 : : return true;
2530 : 80196582 : 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 : 229313 : case FIX_TRUNC_EXPR:
2542 : 229313 : case VEC_PACK_FIX_TRUNC_EXPR:
2543 : 229313 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2544 : 229313 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2545 : : /* The FIX_TRUNC family are always potentially trapping. */
2546 : 229313 : return flag_trapping_math;
2547 : :
2548 : 76668 : case COND_EXPR:
2549 : 76668 : case VEC_COND_EXPR:
2550 : : /* Whether *COND_EXPR can trap depends on whether the
2551 : : first argument can trap, so signal it as not handled.
2552 : : Whether lhs is floating or not doesn't matter. */
2553 : 76668 : *handled = false;
2554 : 76668 : return false;
2555 : :
2556 : 1475172632 : default:
2557 : : /* Any floating arithmetic may trap. */
2558 : 1475172632 : if (fp_operation && flag_trapping_math)
2559 : : return true;
2560 : :
2561 : 1472877687 : *handled = false;
2562 : 1472877687 : return false;
2563 : : }
2564 : : }
2565 : :
2566 : : /* Return true if operation OP may trap. FP_OPERATION is true if OP is applied
2567 : : on floating-point values. HONOR_TRAPV is true if OP is applied on integer
2568 : : type operands that may trap. If OP is a division operator, DIVISOR contains
2569 : : the value of the divisor. */
2570 : :
2571 : : bool
2572 : 4081955125 : operation_could_trap_p (enum tree_code op, bool fp_operation, bool honor_trapv,
2573 : : tree divisor)
2574 : : {
2575 : 44420288 : bool honor_nans = (fp_operation && flag_trapping_math
2576 : 4115058010 : && !flag_finite_math_only);
2577 : 44420288 : bool honor_snans = fp_operation && flag_signaling_nans != 0;
2578 : 4081955125 : bool handled;
2579 : :
2580 : : /* This function cannot tell whether or not COND_EXPR could trap,
2581 : : because that depends on its condition op. */
2582 : 4081955125 : gcc_assert (op != COND_EXPR);
2583 : :
2584 : 4081955125 : if (TREE_CODE_CLASS (op) != tcc_comparison
2585 : : && TREE_CODE_CLASS (op) != tcc_unary
2586 : 4081955125 : && TREE_CODE_CLASS (op) != tcc_binary)
2587 : : return false;
2588 : :
2589 : 267869789 : return operation_could_trap_helper_p (op, fp_operation, honor_trapv,
2590 : : honor_nans, honor_snans, divisor,
2591 : 267869789 : &handled);
2592 : : }
2593 : :
2594 : :
2595 : : /* Returns true if it is possible to prove that the index of
2596 : : an array access REF (an ARRAY_REF expression) falls into the
2597 : : array bounds. */
2598 : :
2599 : : static bool
2600 : 250331674 : in_array_bounds_p (tree ref)
2601 : : {
2602 : 250331674 : tree idx = TREE_OPERAND (ref, 1);
2603 : 250331674 : tree min, max;
2604 : :
2605 : 250331674 : if (TREE_CODE (idx) != INTEGER_CST)
2606 : : return false;
2607 : :
2608 : 247283007 : min = array_ref_low_bound (ref);
2609 : 247283007 : max = array_ref_up_bound (ref);
2610 : 247283007 : if (!min
2611 : 247283007 : || !max
2612 : 247266469 : || TREE_CODE (min) != INTEGER_CST
2613 : 247266469 : || TREE_CODE (max) != INTEGER_CST)
2614 : : return false;
2615 : :
2616 : 247261614 : if (tree_int_cst_lt (idx, min)
2617 : 247261614 : || tree_int_cst_lt (max, idx))
2618 : 53394 : return false;
2619 : :
2620 : : return true;
2621 : : }
2622 : :
2623 : : /* Returns true if it is possible to prove that the range of
2624 : : an array access REF (an ARRAY_RANGE_REF expression) falls
2625 : : into the array bounds. */
2626 : :
2627 : : static bool
2628 : 1874 : range_in_array_bounds_p (tree ref)
2629 : : {
2630 : 1874 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ref));
2631 : 1874 : tree range_min, range_max, min, max;
2632 : :
2633 : 1874 : range_min = TYPE_MIN_VALUE (domain_type);
2634 : 1874 : range_max = TYPE_MAX_VALUE (domain_type);
2635 : 1874 : if (!range_min
2636 : 1874 : || !range_max
2637 : 1874 : || TREE_CODE (range_min) != INTEGER_CST
2638 : 1874 : || TREE_CODE (range_max) != INTEGER_CST)
2639 : : return false;
2640 : :
2641 : 1874 : min = array_ref_low_bound (ref);
2642 : 1874 : max = array_ref_up_bound (ref);
2643 : 1874 : if (!min
2644 : 1874 : || !max
2645 : 1874 : || TREE_CODE (min) != INTEGER_CST
2646 : 1874 : || TREE_CODE (max) != INTEGER_CST)
2647 : : return false;
2648 : :
2649 : 1874 : if (tree_int_cst_lt (range_min, min)
2650 : 1874 : || tree_int_cst_lt (max, range_max))
2651 : 209 : return false;
2652 : :
2653 : : return true;
2654 : : }
2655 : :
2656 : : /* Return true iff a BIT_FIELD_REF <(TYPE)???, SIZE, OFFSET> would access a bit
2657 : : range that is known to be in bounds for TYPE. */
2658 : :
2659 : : bool
2660 : 609485 : access_in_bounds_of_type_p (tree type, poly_uint64 size, poly_uint64 offset)
2661 : : {
2662 : 609485 : tree type_size_tree;
2663 : 609485 : poly_uint64 type_size_max, min = offset, wid = size, max;
2664 : :
2665 : 609485 : type_size_tree = TYPE_SIZE (type);
2666 : 609485 : if (!type_size_tree || !poly_int_tree_p (type_size_tree, &type_size_max))
2667 : 20 : return false;
2668 : :
2669 : 609465 : max = min + wid;
2670 : 609465 : if (maybe_lt (max, min)
2671 : 609465 : || maybe_lt (type_size_max, max))
2672 : : return false;
2673 : :
2674 : : return true;
2675 : : }
2676 : :
2677 : : /* Return whether an access at [off, refsz[ to an object spanning [0, size[
2678 : : accesses storage outside of the object. */
2679 : :
2680 : : static bool
2681 : 246038176 : ref_outside_object_p (tree size, poly_offset_int off, tree refsz)
2682 : : {
2683 : 246038176 : if (size == NULL_TREE
2684 : 246038176 : || refsz == NULL_TREE
2685 : 246025246 : || !poly_int_tree_p (size)
2686 : 246025203 : || !poly_int_tree_p (refsz)
2687 : 246009497 : || maybe_le (wi::to_poly_offset (size), off)
2688 : 491980496 : || maybe_gt (off + wi::to_poly_offset (refsz),
2689 : : wi::to_poly_offset (size)))
2690 : 240117 : return true;
2691 : : /* Now we are sure the whole base of the access is inside
2692 : : the object. */
2693 : : return false;
2694 : : }
2695 : :
2696 : : /* Return true if EXPR can trap, as in dereferencing an invalid pointer
2697 : : location or floating point arithmetic. C.f. the rtl version, may_trap_p.
2698 : : This routine expects only GIMPLE lhs or rhs input. */
2699 : :
2700 : : bool
2701 : 3813658236 : tree_could_trap_p (tree expr)
2702 : : {
2703 : 3813658236 : enum tree_code code;
2704 : 3813658236 : bool fp_operation = false;
2705 : 3813658236 : bool honor_trapv = false;
2706 : 3813658236 : tree t, base, div = NULL_TREE;
2707 : :
2708 : 3813658236 : if (!expr)
2709 : : return false;
2710 : :
2711 : : /* In COND_EXPR and VEC_COND_EXPR only the condition may trap, but
2712 : : they won't appear as operands in GIMPLE form, so this is just for the
2713 : : GENERIC uses where it needs to recurse on the operands and so
2714 : : *COND_EXPR itself doesn't trap. */
2715 : 3808826526 : if (TREE_CODE (expr) == COND_EXPR || TREE_CODE (expr) == VEC_COND_EXPR)
2716 : : return false;
2717 : :
2718 : 3808825227 : code = TREE_CODE (expr);
2719 : 3808825227 : t = TREE_TYPE (expr);
2720 : :
2721 : 3808825227 : if (t)
2722 : : {
2723 : 3808818060 : if (COMPARISON_CLASS_P (expr))
2724 : 5862379 : fp_operation = FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
2725 : : else
2726 : 3802955681 : fp_operation = FLOAT_TYPE_P (t);
2727 : 3808818060 : honor_trapv = INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t);
2728 : : }
2729 : :
2730 : 3808825227 : if (TREE_CODE_CLASS (code) == tcc_binary)
2731 : 365242 : div = TREE_OPERAND (expr, 1);
2732 : 3808825227 : if (operation_could_trap_p (code, fp_operation, honor_trapv, div))
2733 : : return true;
2734 : :
2735 : 3807476202 : restart:
2736 : 4745781748 : switch (code)
2737 : : {
2738 : 1238855 : case BIT_FIELD_REF:
2739 : 1238855 : if (DECL_P (TREE_OPERAND (expr, 0))
2740 : 1238855 : && !access_in_bounds_of_type_p (TREE_TYPE (TREE_OPERAND (expr, 0)),
2741 : : bit_field_size (expr),
2742 : : bit_field_offset (expr)))
2743 : : return true;
2744 : : /* Fall through. */
2745 : :
2746 : 938305546 : case COMPONENT_REF:
2747 : 938305546 : case REALPART_EXPR:
2748 : 938305546 : case IMAGPART_EXPR:
2749 : 938305546 : case VIEW_CONVERT_EXPR:
2750 : 938305546 : case WITH_SIZE_EXPR:
2751 : 938305546 : expr = TREE_OPERAND (expr, 0);
2752 : 938305546 : code = TREE_CODE (expr);
2753 : 938305546 : goto restart;
2754 : :
2755 : 2097 : case ARRAY_RANGE_REF:
2756 : 2097 : base = TREE_OPERAND (expr, 0);
2757 : 2097 : if (tree_could_trap_p (base))
2758 : : return true;
2759 : 1874 : if (TREE_THIS_NOTRAP (expr))
2760 : : return false;
2761 : 1874 : return !range_in_array_bounds_p (expr);
2762 : :
2763 : 261209991 : case ARRAY_REF:
2764 : 261209991 : base = TREE_OPERAND (expr, 0);
2765 : 261209991 : if (tree_could_trap_p (base))
2766 : : return true;
2767 : 250361523 : if (TREE_THIS_NOTRAP (expr))
2768 : : return false;
2769 : 250331674 : return !in_array_bounds_p (expr);
2770 : :
2771 : 641127082 : case TARGET_MEM_REF:
2772 : 641127082 : case MEM_REF:
2773 : 641127082 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
2774 : 641127082 : && tree_could_trap_p (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
2775 : : return true;
2776 : 641127082 : if (TREE_THIS_NOTRAP (expr))
2777 : : return false;
2778 : : /* We cannot prove that the access is in-bounds when we have
2779 : : variable-index TARGET_MEM_REFs. */
2780 : 550903453 : if (code == TARGET_MEM_REF
2781 : 559439435 : && (TMR_INDEX (expr) || TMR_INDEX2 (expr)))
2782 : : return true;
2783 : 543201110 : if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR)
2784 : : {
2785 : 243961274 : tree base = TREE_OPERAND (TREE_OPERAND (expr, 0), 0);
2786 : 243961274 : poly_offset_int off = mem_ref_offset (expr);
2787 : 243961274 : if (maybe_lt (off, 0))
2788 : : return true;
2789 : 243958206 : if (TREE_CODE (base) == STRING_CST)
2790 : 204043 : return maybe_le (TREE_STRING_LENGTH (base), off);
2791 : 243754163 : tree size = DECL_SIZE_UNIT (base);
2792 : 243754163 : tree refsz = TYPE_SIZE_UNIT (TREE_TYPE (expr));
2793 : 243754163 : return ref_outside_object_p (size, off, refsz);
2794 : : }
2795 : 299239836 : if (cfun
2796 : 299082555 : && TREE_CODE (TREE_TYPE (cfun->decl)) == METHOD_TYPE
2797 : 305027131 : && ((TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
2798 : 4933314 : && SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (expr, 0))
2799 : 2868789 : && (SSA_NAME_VAR (TREE_OPERAND (expr, 0))
2800 : 2868789 : == DECL_ARGUMENTS (cfun->decl)))
2801 : 3511609 : || TREE_OPERAND (expr, 0) == DECL_ARGUMENTS (cfun->decl)))
2802 : : {
2803 : 2285914 : poly_offset_int off = mem_ref_offset (expr);
2804 : 2285914 : if (maybe_lt (off, 0))
2805 : : return true;
2806 : 2284013 : tree size = TYPE_SIZE_UNIT
2807 : : (TYPE_METHOD_BASETYPE (TREE_TYPE (cfun->decl)));
2808 : 2284013 : tree refsz = TYPE_SIZE_UNIT (TREE_TYPE (expr));
2809 : 2284013 : return ref_outside_object_p (size, off, refsz);
2810 : : }
2811 : : return true;
2812 : :
2813 : 744530 : case INDIRECT_REF:
2814 : 744530 : return !TREE_THIS_NOTRAP (expr);
2815 : :
2816 : 0 : case ASM_EXPR:
2817 : 0 : return TREE_THIS_VOLATILE (expr);
2818 : :
2819 : 311948 : case CALL_EXPR:
2820 : : /* Internal function calls do not trap. */
2821 : 311948 : if (CALL_EXPR_FN (expr) == NULL_TREE)
2822 : : return false;
2823 : 311830 : t = get_callee_fndecl (expr);
2824 : : /* Assume that indirect and calls to weak functions may trap. */
2825 : 311830 : if (!t || !DECL_P (t))
2826 : : return true;
2827 : 311830 : if (DECL_WEAK (t))
2828 : : return tree_could_trap_p (t);
2829 : : return false;
2830 : :
2831 : 214 : case FUNCTION_DECL:
2832 : : /* Assume that accesses to weak functions may trap, unless we know
2833 : : they are certainly defined in current TU or in some other
2834 : : LTO partition. */
2835 : 214 : if (DECL_WEAK (expr) && !DECL_COMDAT (expr) && DECL_EXTERNAL (expr))
2836 : : {
2837 : 0 : cgraph_node *node = cgraph_node::get (expr);
2838 : 0 : if (node)
2839 : 0 : node = node->function_symbol ();
2840 : 0 : return !(node && node->in_other_partition);
2841 : : }
2842 : : return false;
2843 : :
2844 : 1051469339 : case VAR_DECL:
2845 : : /* Assume that accesses to weak vars may trap, unless we know
2846 : : they are certainly defined in current TU or in some other
2847 : : LTO partition. */
2848 : 1051469339 : if (DECL_WEAK (expr) && !DECL_COMDAT (expr) && DECL_EXTERNAL (expr))
2849 : : {
2850 : 13947 : varpool_node *node = varpool_node::get (expr);
2851 : 13947 : if (node)
2852 : 13902 : node = node->ultimate_alias_target ();
2853 : 27849 : return !(node && node->in_other_partition);
2854 : : }
2855 : : return false;
2856 : :
2857 : : default:
2858 : : return false;
2859 : : }
2860 : : }
2861 : :
2862 : : /* Return non-NULL if there is an integer operation with trapping overflow
2863 : : we can rewrite into non-trapping. Called via walk_tree from
2864 : : rewrite_to_non_trapping_overflow. */
2865 : :
2866 : : static tree
2867 : 270 : find_trapping_overflow (tree *tp, int *walk_subtrees, void *data)
2868 : : {
2869 : 270 : if (EXPR_P (*tp)
2870 : 159 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (*tp))
2871 : 407 : && !operation_no_trapping_overflow (TREE_TYPE (*tp), TREE_CODE (*tp)))
2872 : 9 : return *tp;
2873 : 261 : if (IS_TYPE_OR_DECL_P (*tp)
2874 : 258 : || (TREE_CODE (*tp) == SAVE_EXPR && data == NULL))
2875 : 3 : *walk_subtrees = 0;
2876 : : return NULL_TREE;
2877 : : }
2878 : :
2879 : : /* Rewrite selected operations into unsigned arithmetics, so that they
2880 : : don't trap on overflow. */
2881 : :
2882 : : static tree
2883 : 74 : replace_trapping_overflow (tree *tp, int *walk_subtrees, void *data)
2884 : : {
2885 : 74 : if (find_trapping_overflow (tp, walk_subtrees, data))
2886 : : {
2887 : 6 : tree type = TREE_TYPE (*tp);
2888 : 6 : tree utype = unsigned_type_for (type);
2889 : 6 : *walk_subtrees = 0;
2890 : 6 : int len = TREE_OPERAND_LENGTH (*tp);
2891 : 18 : for (int i = 0; i < len; ++i)
2892 : 12 : walk_tree (&TREE_OPERAND (*tp, i), replace_trapping_overflow,
2893 : : data, (hash_set<tree> *) data);
2894 : :
2895 : 6 : if (TREE_CODE (*tp) == ABS_EXPR)
2896 : : {
2897 : 0 : TREE_SET_CODE (*tp, ABSU_EXPR);
2898 : 0 : TREE_TYPE (*tp) = utype;
2899 : 0 : *tp = fold_convert (type, *tp);
2900 : : }
2901 : : else
2902 : : {
2903 : 6 : TREE_TYPE (*tp) = utype;
2904 : 6 : len = TREE_OPERAND_LENGTH (*tp);
2905 : 18 : for (int i = 0; i < len; ++i)
2906 : 12 : TREE_OPERAND (*tp, i)
2907 : 24 : = fold_convert (utype, TREE_OPERAND (*tp, i));
2908 : 6 : *tp = fold_convert (type, *tp);
2909 : : }
2910 : : }
2911 : 74 : return NULL_TREE;
2912 : : }
2913 : :
2914 : : /* If any subexpression of EXPR can trap due to -ftrapv, rewrite it
2915 : : using unsigned arithmetics to avoid traps in it. */
2916 : :
2917 : : tree
2918 : 50470 : rewrite_to_non_trapping_overflow (tree expr)
2919 : : {
2920 : 50470 : if (!flag_trapv)
2921 : 50433 : return expr;
2922 : 37 : hash_set<tree> pset;
2923 : 37 : if (!walk_tree (&expr, find_trapping_overflow, &pset, &pset))
2924 : 34 : return expr;
2925 : 3 : expr = unshare_expr (expr);
2926 : 3 : pset.empty ();
2927 : 3 : walk_tree (&expr, replace_trapping_overflow, &pset, &pset);
2928 : 3 : return expr;
2929 : 37 : }
2930 : :
2931 : : /* Helper for stmt_could_throw_p. Return true if STMT (assumed to be a
2932 : : an assignment or a conditional) may throw. */
2933 : :
2934 : : static bool
2935 : 1646333951 : stmt_could_throw_1_p (gassign *stmt)
2936 : : {
2937 : 1646333951 : enum tree_code code = gimple_assign_rhs_code (stmt);
2938 : 1646333951 : bool honor_nans = false;
2939 : 1646333951 : bool honor_snans = false;
2940 : 1646333951 : bool fp_operation = false;
2941 : 1646333951 : bool honor_trapv = false;
2942 : 1646333951 : tree t;
2943 : 1646333951 : size_t i;
2944 : 1646333951 : bool handled, ret;
2945 : :
2946 : 1646333951 : if (TREE_CODE_CLASS (code) == tcc_comparison
2947 : : || TREE_CODE_CLASS (code) == tcc_unary
2948 : 1646333951 : || TREE_CODE_CLASS (code) == tcc_binary)
2949 : : {
2950 : 236093151 : if (TREE_CODE_CLASS (code) == tcc_comparison)
2951 : 31236852 : t = TREE_TYPE (gimple_assign_rhs1 (stmt));
2952 : : else
2953 : 204856299 : t = TREE_TYPE (gimple_assign_lhs (stmt));
2954 : 236093151 : fp_operation = FLOAT_TYPE_P (t);
2955 : 236093151 : if (fp_operation)
2956 : : {
2957 : 7413346 : honor_nans = flag_trapping_math && !flag_finite_math_only;
2958 : 7413346 : honor_snans = flag_signaling_nans != 0;
2959 : : }
2960 : 228679805 : else if (INTEGRAL_TYPE_P (t) && TYPE_OVERFLOW_TRAPS (t))
2961 : : honor_trapv = true;
2962 : : }
2963 : :
2964 : : /* First check the LHS. */
2965 : 1646333951 : if (tree_could_trap_p (gimple_assign_lhs (stmt)))
2966 : : return true;
2967 : :
2968 : : /* Check if the main expression may trap. */
2969 : 1725549841 : ret = operation_could_trap_helper_p (code, fp_operation, honor_trapv,
2970 : : honor_nans, honor_snans,
2971 : : gimple_assign_rhs2 (stmt),
2972 : : &handled);
2973 : 1575216442 : if (handled)
2974 : : return ret;
2975 : :
2976 : : /* If the expression does not trap, see if any of the individual operands may
2977 : : trap. */
2978 : 2747411605 : for (i = 1; i < gimple_num_ops (stmt); i++)
2979 : 1509204723 : if (tree_could_trap_p (gimple_op (stmt, i)))
2980 : : return true;
2981 : :
2982 : : return false;
2983 : : }
2984 : :
2985 : :
2986 : : /* Return true if statement STMT within FUN could throw an exception. */
2987 : :
2988 : : bool
2989 : 17081483667 : stmt_could_throw_p (function *fun, gimple *stmt)
2990 : : {
2991 : 17081483667 : if (!flag_exceptions)
2992 : : return false;
2993 : :
2994 : : /* The only statements that can throw an exception are assignments,
2995 : : conditionals, calls, resx, and asms. */
2996 : 12920960729 : switch (gimple_code (stmt))
2997 : : {
2998 : : case GIMPLE_RESX:
2999 : : return true;
3000 : :
3001 : 1070523020 : case GIMPLE_CALL:
3002 : 1070523020 : return !gimple_call_nothrow_p (as_a <gcall *> (stmt));
3003 : :
3004 : 194053960 : case GIMPLE_COND:
3005 : 194053960 : {
3006 : 194053960 : if (fun && !fun->can_throw_non_call_exceptions)
3007 : : return false;
3008 : 66738597 : gcond *cond = as_a <gcond *> (stmt);
3009 : 66738597 : tree lhs = gimple_cond_lhs (cond);
3010 : 66738597 : return operation_could_trap_p (gimple_cond_code (cond),
3011 : 66738597 : FLOAT_TYPE_P (TREE_TYPE (lhs)),
3012 : 66738597 : false, NULL_TREE);
3013 : : }
3014 : :
3015 : 4066239043 : case GIMPLE_ASSIGN:
3016 : 4066239043 : if ((fun && !fun->can_throw_non_call_exceptions)
3017 : 5761153105 : || gimple_clobber_p (stmt))
3018 : : return false;
3019 : 1646333951 : return stmt_could_throw_1_p (as_a <gassign *> (stmt));
3020 : :
3021 : 759010 : case GIMPLE_ASM:
3022 : 759010 : if (fun && !fun->can_throw_non_call_exceptions)
3023 : : return false;
3024 : 59416 : return gimple_asm_volatile_p (as_a <gasm *> (stmt));
3025 : :
3026 : : default:
3027 : : return false;
3028 : : }
3029 : : }
3030 : :
3031 : : /* Return true if STMT in function FUN must be assumed necessary because of
3032 : : non-call exceptions. */
3033 : :
3034 : : bool
3035 : 25900734 : stmt_unremovable_because_of_non_call_eh_p (function *fun, gimple *stmt)
3036 : : {
3037 : 25900734 : return (fun->can_throw_non_call_exceptions
3038 : 5835965 : && !fun->can_delete_dead_exceptions
3039 : 31714974 : && stmt_could_throw_p (fun, stmt));
3040 : : }
3041 : :
3042 : : /* Return true if expression T could throw an exception. */
3043 : :
3044 : : bool
3045 : 42248889 : tree_could_throw_p (tree t)
3046 : : {
3047 : 42248889 : if (!flag_exceptions)
3048 : : return false;
3049 : 35839304 : if (TREE_CODE (t) == MODIFY_EXPR)
3050 : : {
3051 : 0 : if (cfun->can_throw_non_call_exceptions
3052 : 0 : && tree_could_trap_p (TREE_OPERAND (t, 0)))
3053 : : return true;
3054 : 0 : t = TREE_OPERAND (t, 1);
3055 : : }
3056 : :
3057 : 35839304 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
3058 : 0 : t = TREE_OPERAND (t, 0);
3059 : 35839304 : if (TREE_CODE (t) == CALL_EXPR)
3060 : 420678 : return (call_expr_flags (t) & ECF_NOTHROW) == 0;
3061 : 35418626 : if (cfun->can_throw_non_call_exceptions)
3062 : 22192462 : return tree_could_trap_p (t);
3063 : : return false;
3064 : : }
3065 : :
3066 : : /* Return true if STMT can throw an exception that is not caught within its
3067 : : function FUN. FUN can be NULL but the function is extra conservative
3068 : : then. */
3069 : :
3070 : : bool
3071 : 826191845 : stmt_can_throw_external (function *fun, gimple *stmt)
3072 : : {
3073 : 826191845 : int lp_nr;
3074 : :
3075 : 826191845 : if (!stmt_could_throw_p (fun, stmt))
3076 : : return false;
3077 : 44057386 : if (!fun)
3078 : : return true;
3079 : :
3080 : 44051270 : lp_nr = lookup_stmt_eh_lp_fn (fun, stmt);
3081 : 44051270 : return lp_nr == 0;
3082 : : }
3083 : :
3084 : : /* Return true if STMT can throw an exception that is caught within its
3085 : : function FUN. */
3086 : :
3087 : : bool
3088 : 14764176869 : stmt_can_throw_internal (function *fun, gimple *stmt)
3089 : : {
3090 : 14764176869 : int lp_nr;
3091 : :
3092 : 14764176869 : gcc_checking_assert (fun);
3093 : 14764176869 : if (!stmt_could_throw_p (fun, stmt))
3094 : : return false;
3095 : :
3096 : 626060466 : lp_nr = lookup_stmt_eh_lp_fn (fun, stmt);
3097 : 626060466 : return lp_nr > 0;
3098 : : }
3099 : :
3100 : : /* Given a statement STMT in IFUN, if STMT can no longer throw, then
3101 : : remove any entry it might have from the EH table. Return true if
3102 : : any change was made. */
3103 : :
3104 : : bool
3105 : 190878496 : maybe_clean_eh_stmt_fn (struct function *ifun, gimple *stmt)
3106 : : {
3107 : 190878496 : if (stmt_could_throw_p (ifun, stmt))
3108 : : return false;
3109 : 173709332 : return remove_stmt_from_eh_lp_fn (ifun, stmt);
3110 : : }
3111 : :
3112 : : /* Likewise, but always use the current function. */
3113 : :
3114 : : bool
3115 : 190878496 : maybe_clean_eh_stmt (gimple *stmt)
3116 : : {
3117 : 190878496 : return maybe_clean_eh_stmt_fn (cfun, stmt);
3118 : : }
3119 : :
3120 : : /* Given a statement OLD_STMT and a new statement NEW_STMT that has replaced
3121 : : OLD_STMT in the function, remove OLD_STMT from the EH table and put NEW_STMT
3122 : : in the table if it should be in there. Return TRUE if a replacement was
3123 : : done that my require an EH edge purge. */
3124 : :
3125 : : bool
3126 : 47678363 : maybe_clean_or_replace_eh_stmt (gimple *old_stmt, gimple *new_stmt)
3127 : : {
3128 : 47678363 : int lp_nr = lookup_stmt_eh_lp (old_stmt);
3129 : :
3130 : 47678363 : if (lp_nr != 0)
3131 : : {
3132 : 1564203 : bool new_stmt_could_throw = stmt_could_throw_p (cfun, new_stmt);
3133 : :
3134 : 1564203 : if (new_stmt == old_stmt && new_stmt_could_throw)
3135 : : return false;
3136 : :
3137 : 316443 : remove_stmt_from_eh_lp (old_stmt);
3138 : 316443 : if (new_stmt_could_throw)
3139 : : {
3140 : 317 : add_stmt_to_eh_lp (new_stmt, lp_nr);
3141 : 317 : return false;
3142 : : }
3143 : : else
3144 : : return true;
3145 : : }
3146 : :
3147 : : return false;
3148 : : }
3149 : :
3150 : : /* Given a statement OLD_STMT in OLD_FUN and a duplicate statement NEW_STMT
3151 : : in NEW_FUN, copy the EH table data from OLD_STMT to NEW_STMT. The MAP
3152 : : operand is the return value of duplicate_eh_regions. */
3153 : :
3154 : : bool
3155 : 87460682 : maybe_duplicate_eh_stmt_fn (struct function *new_fun, gimple *new_stmt,
3156 : : struct function *old_fun, gimple *old_stmt,
3157 : : hash_map<void *, void *> *map,
3158 : : int default_lp_nr)
3159 : : {
3160 : 87460682 : int old_lp_nr, new_lp_nr;
3161 : :
3162 : 87460682 : if (!stmt_could_throw_p (new_fun, new_stmt))
3163 : : return false;
3164 : :
3165 : 1719002 : old_lp_nr = lookup_stmt_eh_lp_fn (old_fun, old_stmt);
3166 : 1719002 : if (old_lp_nr == 0)
3167 : : {
3168 : 1515235 : if (default_lp_nr == 0)
3169 : : return false;
3170 : : new_lp_nr = default_lp_nr;
3171 : : }
3172 : 203767 : else if (old_lp_nr > 0)
3173 : : {
3174 : 153127 : eh_landing_pad old_lp, new_lp;
3175 : :
3176 : 153127 : old_lp = (*old_fun->eh->lp_array)[old_lp_nr];
3177 : 153127 : new_lp = static_cast<eh_landing_pad> (*map->get (old_lp));
3178 : 153127 : new_lp_nr = new_lp->index;
3179 : : }
3180 : : else
3181 : : {
3182 : 50640 : eh_region old_r, new_r;
3183 : :
3184 : 50640 : old_r = (*old_fun->eh->region_array)[-old_lp_nr];
3185 : 50640 : new_r = static_cast<eh_region> (*map->get (old_r));
3186 : 50640 : new_lp_nr = -new_r->index;
3187 : : }
3188 : :
3189 : 817749 : add_stmt_to_eh_lp_fn (new_fun, new_stmt, new_lp_nr);
3190 : 817749 : return true;
3191 : : }
3192 : :
3193 : : /* Similar, but both OLD_STMT and NEW_STMT are within the current function,
3194 : : and thus no remapping is required. */
3195 : :
3196 : : bool
3197 : 19540772 : maybe_duplicate_eh_stmt (gimple *new_stmt, gimple *old_stmt)
3198 : : {
3199 : 19540772 : int lp_nr;
3200 : :
3201 : 19540772 : if (!stmt_could_throw_p (cfun, new_stmt))
3202 : : return false;
3203 : :
3204 : 63946 : lp_nr = lookup_stmt_eh_lp (old_stmt);
3205 : 63946 : if (lp_nr == 0)
3206 : : return false;
3207 : :
3208 : 6709 : add_stmt_to_eh_lp (new_stmt, lp_nr);
3209 : 6709 : return true;
3210 : : }
3211 : :
3212 : : /* Returns TRUE if oneh and twoh are exception handlers (gimple_try_cleanup of
3213 : : GIMPLE_TRY) that are similar enough to be considered the same. Currently
3214 : : this only handles handlers consisting of a single call, as that's the
3215 : : important case for C++: a destructor call for a particular object showing
3216 : : up in multiple handlers. */
3217 : :
3218 : : static bool
3219 : 1 : same_handler_p (gimple_seq oneh, gimple_seq twoh)
3220 : : {
3221 : 1 : gimple_stmt_iterator gsi;
3222 : 1 : gimple *ones, *twos;
3223 : 1 : unsigned int ai;
3224 : :
3225 : 1 : gsi = gsi_start (oneh);
3226 : 1 : if (!gsi_one_before_end_p (gsi))
3227 : : return false;
3228 : 1 : ones = gsi_stmt (gsi);
3229 : :
3230 : 1 : gsi = gsi_start (twoh);
3231 : 1 : if (!gsi_one_before_end_p (gsi))
3232 : : return false;
3233 : 1 : twos = gsi_stmt (gsi);
3234 : :
3235 : 1 : if (!is_gimple_call (ones)
3236 : 1 : || !is_gimple_call (twos)
3237 : 1 : || gimple_call_lhs (ones)
3238 : 1 : || gimple_call_lhs (twos)
3239 : 1 : || gimple_call_chain (ones)
3240 : 1 : || gimple_call_chain (twos)
3241 : 1 : || !gimple_call_same_target_p (ones, twos)
3242 : 2 : || gimple_call_num_args (ones) != gimple_call_num_args (twos))
3243 : 0 : return false;
3244 : :
3245 : 2 : for (ai = 0; ai < gimple_call_num_args (ones); ++ai)
3246 : 1 : if (!operand_equal_p (gimple_call_arg (ones, ai),
3247 : 1 : gimple_call_arg (twos, ai), 0))
3248 : : return false;
3249 : :
3250 : : return true;
3251 : : }
3252 : :
3253 : : /* Optimize
3254 : : try { A() } finally { try { ~B() } catch { ~A() } }
3255 : : try { ... } finally { ~A() }
3256 : : into
3257 : : try { A() } catch { ~B() }
3258 : : try { ~B() ... } finally { ~A() }
3259 : :
3260 : : This occurs frequently in C++, where A is a local variable and B is a
3261 : : temporary used in the initializer for A. */
3262 : :
3263 : : static void
3264 : 55768 : optimize_double_finally (gtry *one, gtry *two)
3265 : : {
3266 : 55768 : gimple *oneh;
3267 : 55768 : gimple_stmt_iterator gsi;
3268 : 55768 : gimple_seq cleanup;
3269 : :
3270 : 55768 : cleanup = gimple_try_cleanup (one);
3271 : 55768 : gsi = gsi_start (cleanup);
3272 : 55768 : if (!gsi_one_before_end_p (gsi))
3273 : 55768 : return;
3274 : :
3275 : 54401 : oneh = gsi_stmt (gsi);
3276 : 54401 : if (gimple_code (oneh) != GIMPLE_TRY
3277 : 54401 : || gimple_try_kind (oneh) != GIMPLE_TRY_CATCH)
3278 : : return;
3279 : :
3280 : 1 : if (same_handler_p (gimple_try_cleanup (oneh), gimple_try_cleanup (two)))
3281 : : {
3282 : 1 : gimple_seq seq = gimple_try_eval (oneh);
3283 : :
3284 : 1 : gimple_try_set_cleanup (one, seq);
3285 : 1 : gimple_try_set_kind (one, GIMPLE_TRY_CATCH);
3286 : 1 : seq = copy_gimple_seq_and_replace_locals (seq);
3287 : 1 : gimple_seq_add_seq (&seq, gimple_try_eval (two));
3288 : 1 : gimple_try_set_eval (two, seq);
3289 : : }
3290 : : }
3291 : :
3292 : : /* Perform EH refactoring optimizations that are simpler to do when code
3293 : : flow has been lowered but EH structures haven't. */
3294 : :
3295 : : static void
3296 : 6675283 : refactor_eh_r (gimple_seq seq)
3297 : : {
3298 : 6675283 : gimple_stmt_iterator gsi;
3299 : 6675283 : gimple *one, *two;
3300 : :
3301 : 6675283 : one = NULL;
3302 : 6675283 : two = NULL;
3303 : 6675283 : gsi = gsi_start (seq);
3304 : 51332684 : while (1)
3305 : : {
3306 : 58007967 : one = two;
3307 : 58007967 : if (gsi_end_p (gsi))
3308 : : two = NULL;
3309 : : else
3310 : : two = gsi_stmt (gsi);
3311 : 58007967 : if (one && two)
3312 : 44811616 : if (gtry *try_one = dyn_cast <gtry *> (one))
3313 : 1472391 : if (gtry *try_two = dyn_cast <gtry *> (two))
3314 : 71062 : if (gimple_try_kind (try_one) == GIMPLE_TRY_FINALLY
3315 : 71062 : && gimple_try_kind (try_two) == GIMPLE_TRY_FINALLY)
3316 : 55768 : optimize_double_finally (try_one, try_two);
3317 : 58007967 : if (one)
3318 : 51332684 : switch (gimple_code (one))
3319 : : {
3320 : 2187884 : case GIMPLE_TRY:
3321 : 2187884 : refactor_eh_r (gimple_try_eval (one));
3322 : 2187884 : refactor_eh_r (gimple_try_cleanup (one));
3323 : 2187884 : break;
3324 : 39609 : case GIMPLE_CATCH:
3325 : 39609 : refactor_eh_r (gimple_catch_handler (as_a <gcatch *> (one)));
3326 : 39609 : break;
3327 : 5769 : case GIMPLE_EH_FILTER:
3328 : 5769 : refactor_eh_r (gimple_eh_filter_failure (one));
3329 : 5769 : break;
3330 : 635 : case GIMPLE_EH_ELSE:
3331 : 635 : {
3332 : 635 : geh_else *eh_else_stmt = as_a <geh_else *> (one);
3333 : 635 : refactor_eh_r (gimple_eh_else_n_body (eh_else_stmt));
3334 : 635 : refactor_eh_r (gimple_eh_else_e_body (eh_else_stmt));
3335 : : }
3336 : 635 : break;
3337 : : default:
3338 : : break;
3339 : : }
3340 : 58007967 : if (two)
3341 : 51332684 : gsi_next (&gsi);
3342 : : else
3343 : : break;
3344 : : }
3345 : 6675283 : }
3346 : :
3347 : : namespace {
3348 : :
3349 : : const pass_data pass_data_refactor_eh =
3350 : : {
3351 : : GIMPLE_PASS, /* type */
3352 : : "ehopt", /* name */
3353 : : OPTGROUP_NONE, /* optinfo_flags */
3354 : : TV_TREE_EH, /* tv_id */
3355 : : PROP_gimple_lcf, /* properties_required */
3356 : : 0, /* properties_provided */
3357 : : 0, /* properties_destroyed */
3358 : : 0, /* todo_flags_start */
3359 : : 0, /* todo_flags_finish */
3360 : : };
3361 : :
3362 : : class pass_refactor_eh : public gimple_opt_pass
3363 : : {
3364 : : public:
3365 : 285689 : pass_refactor_eh (gcc::context *ctxt)
3366 : 571378 : : gimple_opt_pass (pass_data_refactor_eh, ctxt)
3367 : : {}
3368 : :
3369 : : /* opt_pass methods: */
3370 : 2979787 : bool gate (function *) final override { return flag_exceptions != 0; }
3371 : 2252867 : unsigned int execute (function *) final override
3372 : : {
3373 : 2252867 : refactor_eh_r (gimple_body (current_function_decl));
3374 : 2252867 : return 0;
3375 : : }
3376 : :
3377 : : }; // class pass_refactor_eh
3378 : :
3379 : : } // anon namespace
3380 : :
3381 : : gimple_opt_pass *
3382 : 285689 : make_pass_refactor_eh (gcc::context *ctxt)
3383 : : {
3384 : 285689 : return new pass_refactor_eh (ctxt);
3385 : : }
3386 : :
3387 : : /* At the end of gimple optimization, we can lower RESX. */
3388 : :
3389 : : static bool
3390 : 193620 : lower_resx (basic_block bb, gresx *stmt,
3391 : : hash_map<eh_region, tree> *mnt_map)
3392 : : {
3393 : 193620 : int lp_nr;
3394 : 193620 : eh_region src_r, dst_r;
3395 : 193620 : gimple_stmt_iterator gsi;
3396 : 193620 : gcall *x;
3397 : 193620 : tree fn, src_nr;
3398 : 193620 : bool ret = false;
3399 : :
3400 : 193620 : lp_nr = lookup_stmt_eh_lp (stmt);
3401 : 193620 : if (lp_nr != 0)
3402 : 112603 : dst_r = get_eh_region_from_lp_number (lp_nr);
3403 : : else
3404 : 81017 : dst_r = NULL;
3405 : :
3406 : 193620 : src_r = get_eh_region_from_number (gimple_resx_region (stmt));
3407 : 193620 : gsi = gsi_last_bb (bb);
3408 : :
3409 : 193620 : if (src_r == NULL)
3410 : : {
3411 : : /* We can wind up with no source region when pass_cleanup_eh shows
3412 : : that there are no entries into an eh region and deletes it, but
3413 : : then the block that contains the resx isn't removed. This can
3414 : : happen without optimization when the switch statement created by
3415 : : lower_try_finally_switch isn't simplified to remove the eh case.
3416 : :
3417 : : Resolve this by expanding the resx node to an abort. */
3418 : :
3419 : 0 : fn = builtin_decl_implicit (BUILT_IN_TRAP);
3420 : 0 : x = gimple_build_call (fn, 0);
3421 : 0 : gimple_call_set_ctrl_altering (x, true);
3422 : 0 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3423 : :
3424 : 0 : while (EDGE_COUNT (bb->succs) > 0)
3425 : 0 : remove_edge (EDGE_SUCC (bb, 0));
3426 : : }
3427 : 193620 : else if (dst_r)
3428 : : {
3429 : : /* When we have a destination region, we resolve this by copying
3430 : : the excptr and filter values into place, and changing the edge
3431 : : to immediately after the landing pad. */
3432 : 112603 : edge e;
3433 : :
3434 : 112603 : if (lp_nr < 0)
3435 : : {
3436 : 411 : basic_block new_bb;
3437 : 411 : tree lab;
3438 : :
3439 : : /* We are resuming into a MUST_NOT_CALL region. Expand a call to
3440 : : the failure decl into a new block, if needed. */
3441 : 411 : gcc_assert (dst_r->type == ERT_MUST_NOT_THROW);
3442 : :
3443 : 411 : tree *slot = mnt_map->get (dst_r);
3444 : 411 : if (slot == NULL)
3445 : : {
3446 : 358 : gimple_stmt_iterator gsi2;
3447 : :
3448 : 358 : new_bb = create_empty_bb (bb);
3449 : 358 : new_bb->count = bb->count;
3450 : 358 : add_bb_to_loop (new_bb, bb->loop_father);
3451 : 358 : lab = gimple_block_label (new_bb);
3452 : 358 : gsi2 = gsi_start_bb (new_bb);
3453 : :
3454 : : /* Handle failure fns that expect either no arguments or the
3455 : : exception pointer. */
3456 : 358 : fn = dst_r->u.must_not_throw.failure_decl;
3457 : 358 : if (TYPE_ARG_TYPES (TREE_TYPE (fn)) != void_list_node)
3458 : : {
3459 : 358 : tree epfn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
3460 : 358 : src_nr = build_int_cst (integer_type_node, src_r->index);
3461 : 358 : x = gimple_build_call (epfn, 1, src_nr);
3462 : 358 : tree var = create_tmp_var (ptr_type_node);
3463 : 358 : var = make_ssa_name (var, x);
3464 : 358 : gimple_call_set_lhs (x, var);
3465 : 358 : gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
3466 : 358 : x = gimple_build_call (fn, 1, var);
3467 : : }
3468 : : else
3469 : 0 : x = gimple_build_call (fn, 0);
3470 : 358 : gimple_set_location (x, dst_r->u.must_not_throw.failure_loc);
3471 : 358 : gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
3472 : :
3473 : 358 : mnt_map->put (dst_r, lab);
3474 : : }
3475 : : else
3476 : : {
3477 : 53 : lab = *slot;
3478 : 53 : new_bb = label_to_block (cfun, lab);
3479 : : }
3480 : :
3481 : 411 : gcc_assert (EDGE_COUNT (bb->succs) == 0);
3482 : 411 : e = make_single_succ_edge (bb, new_bb, EDGE_FALLTHRU);
3483 : : }
3484 : : else
3485 : : {
3486 : 112192 : edge_iterator ei;
3487 : 112192 : tree dst_nr = build_int_cst (integer_type_node, dst_r->index);
3488 : :
3489 : 112192 : fn = builtin_decl_implicit (BUILT_IN_EH_COPY_VALUES);
3490 : 112192 : src_nr = build_int_cst (integer_type_node, src_r->index);
3491 : 112192 : x = gimple_build_call (fn, 2, dst_nr, src_nr);
3492 : 112192 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3493 : :
3494 : : /* Update the flags for the outgoing edge. */
3495 : 112192 : e = single_succ_edge (bb);
3496 : 112192 : gcc_assert (e->flags & EDGE_EH);
3497 : 112192 : e->flags = (e->flags & ~EDGE_EH) | EDGE_FALLTHRU;
3498 : 112192 : e->probability = profile_probability::always ();
3499 : :
3500 : : /* If there are no more EH users of the landing pad, delete it. */
3501 : 168267 : FOR_EACH_EDGE (e, ei, e->dest->preds)
3502 : 144294 : if (e->flags & EDGE_EH)
3503 : : break;
3504 : 112192 : if (e == NULL)
3505 : : {
3506 : 23973 : eh_landing_pad lp = get_eh_landing_pad_from_number (lp_nr);
3507 : 23973 : remove_eh_landing_pad (lp);
3508 : : }
3509 : : }
3510 : :
3511 : 112603 : ret = true;
3512 : : }
3513 : : else
3514 : : {
3515 : 81017 : tree var;
3516 : :
3517 : : /* When we don't have a destination region, this exception escapes
3518 : : up the call chain. We resolve this by generating a call to the
3519 : : _Unwind_Resume library function. */
3520 : :
3521 : : /* The ARM EABI redefines _Unwind_Resume as __cxa_end_cleanup
3522 : : with no arguments for C++. Check for that. */
3523 : 81017 : if (src_r->use_cxa_end_cleanup)
3524 : : {
3525 : 0 : fn = builtin_decl_implicit (BUILT_IN_CXA_END_CLEANUP);
3526 : 0 : x = gimple_build_call (fn, 0);
3527 : 0 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3528 : : }
3529 : : else
3530 : : {
3531 : 81017 : fn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
3532 : 81017 : src_nr = build_int_cst (integer_type_node, src_r->index);
3533 : 81017 : x = gimple_build_call (fn, 1, src_nr);
3534 : 81017 : var = create_tmp_var (ptr_type_node);
3535 : 81017 : var = make_ssa_name (var, x);
3536 : 81017 : gimple_call_set_lhs (x, var);
3537 : 81017 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3538 : :
3539 : : /* When exception handling is delegated to a caller function, we
3540 : : have to guarantee that shadow memory variables living on stack
3541 : : will be cleaner before control is given to a parent function. */
3542 : 81017 : if (sanitize_flags_p (SANITIZE_ADDRESS))
3543 : : {
3544 : 271 : tree decl
3545 : 271 : = builtin_decl_implicit (BUILT_IN_ASAN_HANDLE_NO_RETURN);
3546 : 271 : gimple *g = gimple_build_call (decl, 0);
3547 : 271 : gimple_set_location (g, gimple_location (stmt));
3548 : 271 : gsi_insert_before (&gsi, g, GSI_SAME_STMT);
3549 : : }
3550 : :
3551 : 81017 : fn = builtin_decl_implicit (BUILT_IN_UNWIND_RESUME);
3552 : 81017 : x = gimple_build_call (fn, 1, var);
3553 : 81017 : gimple_call_set_ctrl_altering (x, true);
3554 : 81017 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3555 : : }
3556 : :
3557 : 81017 : gcc_assert (EDGE_COUNT (bb->succs) == 0);
3558 : : }
3559 : :
3560 : 193620 : gsi_remove (&gsi, true);
3561 : :
3562 : 193620 : return ret;
3563 : : }
3564 : :
3565 : : namespace {
3566 : :
3567 : : const pass_data pass_data_lower_resx =
3568 : : {
3569 : : GIMPLE_PASS, /* type */
3570 : : "resx", /* name */
3571 : : OPTGROUP_NONE, /* optinfo_flags */
3572 : : TV_TREE_EH, /* tv_id */
3573 : : PROP_gimple_lcf, /* properties_required */
3574 : : 0, /* properties_provided */
3575 : : 0, /* properties_destroyed */
3576 : : 0, /* todo_flags_start */
3577 : : 0, /* todo_flags_finish */
3578 : : };
3579 : :
3580 : : class pass_lower_resx : public gimple_opt_pass
3581 : : {
3582 : : public:
3583 : 285689 : pass_lower_resx (gcc::context *ctxt)
3584 : 571378 : : gimple_opt_pass (pass_data_lower_resx, ctxt)
3585 : : {}
3586 : :
3587 : : /* opt_pass methods: */
3588 : 1482119 : bool gate (function *) final override { return flag_exceptions != 0; }
3589 : : unsigned int execute (function *) final override;
3590 : :
3591 : : }; // class pass_lower_resx
3592 : :
3593 : : unsigned
3594 : 819120 : pass_lower_resx::execute (function *fun)
3595 : : {
3596 : 819120 : basic_block bb;
3597 : 819120 : bool dominance_invalidated = false;
3598 : 819120 : bool any_rewritten = false;
3599 : :
3600 : 819120 : hash_map<eh_region, tree> mnt_map;
3601 : :
3602 : 10008461 : FOR_EACH_BB_FN (bb, fun)
3603 : : {
3604 : 26031700 : if (gresx *last = safe_dyn_cast <gresx *> (*gsi_last_bb (bb)))
3605 : : {
3606 : 193620 : dominance_invalidated |= lower_resx (bb, last, &mnt_map);
3607 : 193620 : any_rewritten = true;
3608 : : }
3609 : : }
3610 : :
3611 : 819120 : if (dominance_invalidated)
3612 : : {
3613 : 22457 : free_dominance_info (CDI_DOMINATORS);
3614 : 22457 : free_dominance_info (CDI_POST_DOMINATORS);
3615 : : }
3616 : :
3617 : 819120 : return any_rewritten ? TODO_update_ssa_only_virtuals : 0;
3618 : 819120 : }
3619 : :
3620 : : } // anon namespace
3621 : :
3622 : : gimple_opt_pass *
3623 : 285689 : make_pass_lower_resx (gcc::context *ctxt)
3624 : : {
3625 : 285689 : return new pass_lower_resx (ctxt);
3626 : : }
3627 : :
3628 : : /* Try to optimize var = {v} {CLOBBER} stmts followed just by
3629 : : external throw. */
3630 : :
3631 : : static void
3632 : 629442 : optimize_clobbers (basic_block bb)
3633 : : {
3634 : 629442 : gimple_stmt_iterator gsi = gsi_last_bb (bb);
3635 : 629442 : bool any_clobbers = false;
3636 : 629442 : bool seen_stack_restore = false;
3637 : 629442 : edge_iterator ei;
3638 : 629442 : edge e;
3639 : :
3640 : : /* Only optimize anything if the bb contains at least one clobber,
3641 : : ends with resx (checked by caller), optionally contains some
3642 : : debug stmts or labels, or at most one __builtin_stack_restore
3643 : : call, and has an incoming EH edge. */
3644 : 5153838 : for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3645 : : {
3646 : 2560749 : gimple *stmt = gsi_stmt (gsi);
3647 : 2560749 : if (is_gimple_debug (stmt))
3648 : 1448093 : continue;
3649 : 1112656 : if (gimple_clobber_p (stmt))
3650 : : {
3651 : 498962 : any_clobbers = true;
3652 : 498962 : continue;
3653 : : }
3654 : 614116 : if (!seen_stack_restore
3655 : 613694 : && gimple_call_builtin_p (stmt, BUILT_IN_STACK_RESTORE))
3656 : : {
3657 : 422 : seen_stack_restore = true;
3658 : 422 : continue;
3659 : : }
3660 : 613272 : if (gimple_code (stmt) == GIMPLE_LABEL)
3661 : : break;
3662 : 429123 : return;
3663 : : }
3664 : 365599 : if (!any_clobbers)
3665 : : return;
3666 : 221640 : FOR_EACH_EDGE (e, ei, bb->preds)
3667 : 214286 : if (e->flags & EDGE_EH)
3668 : : break;
3669 : 207673 : if (e == NULL)
3670 : : return;
3671 : 200319 : gsi = gsi_last_bb (bb);
3672 : 1935596 : for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3673 : : {
3674 : 767479 : gimple *stmt = gsi_stmt (gsi);
3675 : 767479 : if (!gimple_clobber_p (stmt))
3676 : 396338 : continue;
3677 : 371141 : unlink_stmt_vdef (stmt);
3678 : 371141 : gsi_remove (&gsi, true);
3679 : 371141 : release_defs (stmt);
3680 : : }
3681 : : }
3682 : :
3683 : : /* Try to sink var = {v} {CLOBBER} stmts followed just by
3684 : : internal throw to successor BB.
3685 : : SUNK, if not NULL, is an array of sequences indexed by basic-block
3686 : : index to sink to and to pick up sinking opportunities from.
3687 : : If FOUND_OPPORTUNITY is not NULL then do not perform the optimization
3688 : : but set *FOUND_OPPORTUNITY to true. */
3689 : :
3690 : : static int
3691 : 742050 : sink_clobbers (basic_block bb,
3692 : : gimple_seq *sunk = NULL, bool *found_opportunity = NULL)
3693 : : {
3694 : 742050 : edge e;
3695 : 742050 : edge_iterator ei;
3696 : 742050 : gimple_stmt_iterator gsi, dgsi;
3697 : 742050 : basic_block succbb;
3698 : 742050 : bool any_clobbers = false;
3699 : 742050 : unsigned todo = 0;
3700 : :
3701 : : /* Only optimize if BB has a single EH successor and
3702 : : all predecessor edges are EH too. */
3703 : 1311932 : if (!single_succ_p (bb)
3704 : 741644 : || (single_succ_edge (bb)->flags & EDGE_EH) == 0)
3705 : : return 0;
3706 : :
3707 : 2947278 : FOR_EACH_EDGE (e, ei, bb->preds)
3708 : : {
3709 : 2225688 : if ((e->flags & EDGE_EH) == 0)
3710 : : return 0;
3711 : : }
3712 : :
3713 : : /* And BB contains only CLOBBER stmts before the final
3714 : : RESX. */
3715 : 721590 : gsi = gsi_last_bb (bb);
3716 : 7726514 : for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3717 : : {
3718 : 3863257 : gimple *stmt = gsi_stmt (gsi);
3719 : 3863257 : if (is_gimple_debug (stmt))
3720 : 2678626 : continue;
3721 : 1184631 : if (gimple_code (stmt) == GIMPLE_LABEL)
3722 : : break;
3723 : 923830 : if (!gimple_clobber_p (stmt))
3724 : : return 0;
3725 : : any_clobbers = true;
3726 : : }
3727 : 260801 : if (!any_clobbers && (!sunk || gimple_seq_empty_p (sunk[bb->index])))
3728 : : return 0;
3729 : :
3730 : : /* If this was a dry run, tell it we found clobbers to sink. */
3731 : 174828 : if (found_opportunity)
3732 : : {
3733 : 3066 : *found_opportunity = true;
3734 : 3066 : return 0;
3735 : : }
3736 : :
3737 : 171762 : edge succe = single_succ_edge (bb);
3738 : 171762 : succbb = succe->dest;
3739 : :
3740 : : /* See if there is a virtual PHI node to take an updated virtual
3741 : : operand from. */
3742 : 171762 : gphi *vphi = NULL;
3743 : 171762 : for (gphi_iterator gpi = gsi_start_phis (succbb);
3744 : 172173 : !gsi_end_p (gpi); gsi_next (&gpi))
3745 : : {
3746 : 121750 : tree res = gimple_phi_result (gpi.phi ());
3747 : 243500 : if (virtual_operand_p (res))
3748 : : {
3749 : : vphi = gpi.phi ();
3750 : : break;
3751 : : }
3752 : : }
3753 : :
3754 : 171762 : gimple *first_sunk = NULL;
3755 : 171762 : gimple *last_sunk = NULL;
3756 : 171762 : if (sunk && !(succbb->flags & BB_VISITED))
3757 : 8654 : dgsi = gsi_start (sunk[succbb->index]);
3758 : : else
3759 : 164713 : dgsi = gsi_after_labels (succbb);
3760 : 171762 : gsi = gsi_last_bb (bb);
3761 : 879526 : for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
3762 : : {
3763 : 439763 : gimple *stmt = gsi_stmt (gsi);
3764 : 439763 : tree lhs;
3765 : 439763 : if (is_gimple_debug (stmt))
3766 : 67076 : continue;
3767 : 372687 : if (gimple_code (stmt) == GIMPLE_LABEL)
3768 : : break;
3769 : 200925 : lhs = gimple_assign_lhs (stmt);
3770 : : /* Unfortunately we don't have dominance info updated at this
3771 : : point, so checking if
3772 : : dominated_by_p (CDI_DOMINATORS, succbb,
3773 : : gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0)))
3774 : : would be too costly. Thus, avoid sinking any clobbers that
3775 : : refer to non-(D) SSA_NAMEs. */
3776 : 200931 : if (TREE_CODE (lhs) == MEM_REF
3777 : 78 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME
3778 : 200974 : && !SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (lhs, 0)))
3779 : : {
3780 : 6 : unlink_stmt_vdef (stmt);
3781 : 6 : gsi_remove (&gsi, true);
3782 : 6 : release_defs (stmt);
3783 : 6 : continue;
3784 : : }
3785 : :
3786 : : /* As we do not change stmt order when sinking across a
3787 : : forwarder edge we can keep virtual operands in place. */
3788 : 200919 : gsi_remove (&gsi, false);
3789 : 200919 : gsi_insert_before (&dgsi, stmt, GSI_NEW_STMT);
3790 : 200919 : if (!first_sunk)
3791 : 171750 : first_sunk = stmt;
3792 : : last_sunk = stmt;
3793 : : }
3794 : 171762 : if (sunk && !gimple_seq_empty_p (sunk[bb->index]))
3795 : : {
3796 : 1651 : if (!first_sunk)
3797 : 6 : first_sunk = gsi_stmt (gsi_last (sunk[bb->index]));
3798 : 1651 : last_sunk = gsi_stmt (gsi_start (sunk[bb->index]));
3799 : 1651 : gsi_insert_seq_before_without_update (&dgsi,
3800 : : sunk[bb->index], GSI_NEW_STMT);
3801 : 1651 : sunk[bb->index] = NULL;
3802 : : }
3803 : 171762 : if (first_sunk)
3804 : : {
3805 : : /* Adjust virtual operands if we sunk across a virtual PHI. */
3806 : 171756 : if (vphi)
3807 : : {
3808 : 121339 : imm_use_iterator iter;
3809 : 121339 : use_operand_p use_p;
3810 : 121339 : gimple *use_stmt;
3811 : 121339 : tree phi_def = gimple_phi_result (vphi);
3812 : 243408 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, phi_def)
3813 : 366249 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3814 : 365519 : SET_USE (use_p, gimple_vdef (first_sunk));
3815 : 121339 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phi_def))
3816 : : {
3817 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_vdef (first_sunk)) = 1;
3818 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (phi_def) = 0;
3819 : : }
3820 : 242678 : SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (vphi, succe),
3821 : : gimple_vuse (last_sunk));
3822 : 121339 : SET_USE (gimple_vuse_op (last_sunk), phi_def);
3823 : : }
3824 : : /* If there isn't a single predecessor but no virtual PHI node
3825 : : arrange for virtual operands to be renamed. */
3826 : 78914 : else if (!single_pred_p (succbb)
3827 : 78914 : && TREE_CODE (gimple_vuse (last_sunk)) == SSA_NAME)
3828 : : {
3829 : 28410 : mark_virtual_operand_for_renaming (gimple_vuse (last_sunk));
3830 : 28410 : todo |= TODO_update_ssa_only_virtuals;
3831 : : }
3832 : : }
3833 : :
3834 : 171762 : return todo;
3835 : : }
3836 : :
3837 : : /* At the end of inlining, we can lower EH_DISPATCH. Return true when
3838 : : we have found some duplicate labels and removed some edges. */
3839 : :
3840 : : static bool
3841 : 41905 : lower_eh_dispatch (basic_block src, geh_dispatch *stmt)
3842 : : {
3843 : 41905 : gimple_stmt_iterator gsi;
3844 : 41905 : int region_nr;
3845 : 41905 : eh_region r;
3846 : 41905 : tree filter, fn;
3847 : 41905 : gimple *x;
3848 : 41905 : bool redirected = false;
3849 : :
3850 : 41905 : region_nr = gimple_eh_dispatch_region (stmt);
3851 : 41905 : r = get_eh_region_from_number (region_nr);
3852 : :
3853 : 41905 : gsi = gsi_last_bb (src);
3854 : :
3855 : 41905 : switch (r->type)
3856 : : {
3857 : 41619 : case ERT_TRY:
3858 : 41619 : {
3859 : 41619 : auto_vec<tree> labels;
3860 : 41619 : tree default_label = NULL;
3861 : 41619 : eh_catch c;
3862 : 41619 : edge_iterator ei;
3863 : 41619 : edge e;
3864 : 41619 : hash_set<tree> seen_values;
3865 : :
3866 : : /* Collect the labels for a switch. Zero the post_landing_pad
3867 : : field becase we'll no longer have anything keeping these labels
3868 : : in existence and the optimizer will be free to merge these
3869 : : blocks at will. */
3870 : 47369 : for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
3871 : : {
3872 : 44412 : tree tp_node, flt_node, lab = c->label;
3873 : 44412 : bool have_label = false;
3874 : :
3875 : 44412 : c->label = NULL;
3876 : 44412 : tp_node = c->type_list;
3877 : 44412 : flt_node = c->filter_list;
3878 : :
3879 : 44412 : if (tp_node == NULL)
3880 : : {
3881 : : default_label = lab;
3882 : : break;
3883 : : }
3884 : 5750 : do
3885 : : {
3886 : : /* Filter out duplicate labels that arise when this handler
3887 : : is shadowed by an earlier one. When no labels are
3888 : : attached to the handler anymore, we remove
3889 : : the corresponding edge and then we delete unreachable
3890 : : blocks at the end of this pass. */
3891 : 5750 : if (! seen_values.contains (TREE_VALUE (flt_node)))
3892 : : {
3893 : 5729 : tree t = build_case_label (TREE_VALUE (flt_node),
3894 : 5729 : NULL, lab);
3895 : 5729 : labels.safe_push (t);
3896 : 5729 : seen_values.add (TREE_VALUE (flt_node));
3897 : 5729 : have_label = true;
3898 : : }
3899 : :
3900 : 5750 : tp_node = TREE_CHAIN (tp_node);
3901 : 5750 : flt_node = TREE_CHAIN (flt_node);
3902 : : }
3903 : 5750 : while (tp_node);
3904 : 5750 : if (! have_label)
3905 : : {
3906 : 21 : remove_edge (find_edge (src, label_to_block (cfun, lab)));
3907 : 21 : redirected = true;
3908 : : }
3909 : : }
3910 : :
3911 : : /* Clean up the edge flags. */
3912 : 88967 : FOR_EACH_EDGE (e, ei, src->succs)
3913 : : {
3914 : 47348 : if (e->flags & EDGE_FALLTHRU)
3915 : : {
3916 : : /* If there was no catch-all, use the fallthru edge. */
3917 : 2957 : if (default_label == NULL)
3918 : 2957 : default_label = gimple_block_label (e->dest);
3919 : 2957 : e->flags &= ~EDGE_FALLTHRU;
3920 : : }
3921 : : }
3922 : 41619 : gcc_assert (default_label != NULL);
3923 : :
3924 : : /* Don't generate a switch if there's only a default case.
3925 : : This is common in the form of try { A; } catch (...) { B; }. */
3926 : 41619 : if (!labels.exists ())
3927 : : {
3928 : 36886 : e = single_succ_edge (src);
3929 : 36886 : e->flags |= EDGE_FALLTHRU;
3930 : : }
3931 : : else
3932 : : {
3933 : 4733 : fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
3934 : 4733 : x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3935 : 4733 : region_nr));
3936 : 4733 : filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)));
3937 : 4733 : filter = make_ssa_name (filter, x);
3938 : 4733 : gimple_call_set_lhs (x, filter);
3939 : 4733 : gimple_set_location (x, gimple_location (stmt));
3940 : 4733 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3941 : :
3942 : : /* Turn the default label into a default case. */
3943 : 4733 : default_label = build_case_label (NULL, NULL, default_label);
3944 : 4733 : sort_case_labels (labels);
3945 : :
3946 : 4733 : x = gimple_build_switch (filter, default_label, labels);
3947 : 4733 : gimple_set_location (x, gimple_location (stmt));
3948 : 4733 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3949 : : }
3950 : 41619 : }
3951 : 41619 : break;
3952 : :
3953 : 286 : case ERT_ALLOWED_EXCEPTIONS:
3954 : 286 : {
3955 : 286 : edge b_e = BRANCH_EDGE (src);
3956 : 286 : edge f_e = FALLTHRU_EDGE (src);
3957 : :
3958 : 286 : fn = builtin_decl_implicit (BUILT_IN_EH_FILTER);
3959 : 286 : x = gimple_build_call (fn, 1, build_int_cst (integer_type_node,
3960 : 286 : region_nr));
3961 : 286 : filter = create_tmp_var (TREE_TYPE (TREE_TYPE (fn)));
3962 : 286 : filter = make_ssa_name (filter, x);
3963 : 286 : gimple_call_set_lhs (x, filter);
3964 : 286 : gimple_set_location (x, gimple_location (stmt));
3965 : 286 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3966 : :
3967 : 286 : r->u.allowed.label = NULL;
3968 : 286 : x = gimple_build_cond (EQ_EXPR, filter,
3969 : 286 : build_int_cst (TREE_TYPE (filter),
3970 : 286 : r->u.allowed.filter),
3971 : : NULL_TREE, NULL_TREE);
3972 : 286 : gsi_insert_before (&gsi, x, GSI_SAME_STMT);
3973 : :
3974 : 286 : b_e->flags = b_e->flags | EDGE_TRUE_VALUE;
3975 : 286 : f_e->flags = (f_e->flags & ~EDGE_FALLTHRU) | EDGE_FALSE_VALUE;
3976 : : }
3977 : 286 : break;
3978 : :
3979 : 0 : default:
3980 : 0 : gcc_unreachable ();
3981 : : }
3982 : :
3983 : : /* Replace the EH_DISPATCH with the SWITCH or COND generated above. */
3984 : 41905 : gsi_remove (&gsi, true);
3985 : 41905 : return redirected;
3986 : : }
3987 : :
3988 : : namespace {
3989 : :
3990 : : const pass_data pass_data_lower_eh_dispatch =
3991 : : {
3992 : : GIMPLE_PASS, /* type */
3993 : : "ehdisp", /* name */
3994 : : OPTGROUP_NONE, /* optinfo_flags */
3995 : : TV_TREE_EH, /* tv_id */
3996 : : PROP_gimple_lcf, /* properties_required */
3997 : : 0, /* properties_provided */
3998 : : 0, /* properties_destroyed */
3999 : : 0, /* todo_flags_start */
4000 : : 0, /* todo_flags_finish */
4001 : : };
4002 : :
4003 : : class pass_lower_eh_dispatch : public gimple_opt_pass
4004 : : {
4005 : : public:
4006 : 285689 : pass_lower_eh_dispatch (gcc::context *ctxt)
4007 : 571378 : : gimple_opt_pass (pass_data_lower_eh_dispatch, ctxt)
4008 : : {}
4009 : :
4010 : : /* opt_pass methods: */
4011 : 1482289 : bool gate (function *fun) final override
4012 : : {
4013 : 1482289 : return fun->eh->region_tree != NULL;
4014 : : }
4015 : : unsigned int execute (function *) final override;
4016 : :
4017 : : }; // class pass_lower_eh_dispatch
4018 : :
4019 : : unsigned
4020 : 148032 : pass_lower_eh_dispatch::execute (function *fun)
4021 : : {
4022 : 148032 : basic_block bb;
4023 : 148032 : int flags = 0;
4024 : 148032 : bool redirected = false;
4025 : 148032 : bool any_resx_to_process = false;
4026 : :
4027 : 148032 : assign_filter_values ();
4028 : :
4029 : 5502976 : FOR_EACH_BB_FN (bb, fun)
4030 : : {
4031 : 5354944 : gimple *last = *gsi_last_bb (bb);
4032 : 5354944 : if (last == NULL)
4033 : 97776 : continue;
4034 : 5257168 : if (gimple_code (last) == GIMPLE_EH_DISPATCH)
4035 : : {
4036 : 41905 : redirected |= lower_eh_dispatch (bb,
4037 : : as_a <geh_dispatch *> (last));
4038 : 41905 : flags |= TODO_update_ssa_only_virtuals;
4039 : : }
4040 : 5215263 : else if (gimple_code (last) == GIMPLE_RESX)
4041 : : {
4042 : 232343 : if (stmt_can_throw_external (fun, last))
4043 : 100335 : optimize_clobbers (bb);
4044 : 132008 : else if (!any_resx_to_process)
4045 : 125819 : sink_clobbers (bb, NULL, &any_resx_to_process);
4046 : : }
4047 : 5257168 : bb->flags &= ~BB_VISITED;
4048 : : }
4049 : 148032 : if (redirected)
4050 : : {
4051 : 12 : free_dominance_info (CDI_DOMINATORS);
4052 : 12 : delete_unreachable_blocks ();
4053 : : }
4054 : :
4055 : 148032 : if (any_resx_to_process)
4056 : : {
4057 : : /* Make sure to catch all secondary sinking opportunities by processing
4058 : : blocks in RPO order and after all CFG modifications from lowering
4059 : : and unreachable block removal. */
4060 : 3066 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
4061 : 3066 : int rpo_n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
4062 : 3066 : gimple_seq *sunk = XCNEWVEC (gimple_seq, last_basic_block_for_fn (fun));
4063 : 113014 : for (int i = 0; i < rpo_n; ++i)
4064 : : {
4065 : 109948 : bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
4066 : 109948 : gimple *last = *gsi_last_bb (bb);
4067 : 109948 : if (last
4068 : 108147 : && gimple_code (last) == GIMPLE_RESX
4069 : 123637 : && !stmt_can_throw_external (fun, last))
4070 : 10310 : flags |= sink_clobbers (bb, sunk);
4071 : : /* If there were any clobbers sunk into this BB, insert them now. */
4072 : 109948 : if (!gimple_seq_empty_p (sunk[bb->index]))
4073 : : {
4074 : 3793 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
4075 : 3793 : gsi_insert_seq_before (&gsi, sunk[bb->index], GSI_NEW_STMT);
4076 : 3793 : sunk[bb->index] = NULL;
4077 : : }
4078 : 109948 : bb->flags |= BB_VISITED;
4079 : : }
4080 : 3066 : free (rpo);
4081 : 3066 : free (sunk);
4082 : : }
4083 : :
4084 : 148032 : return flags;
4085 : : }
4086 : :
4087 : : } // anon namespace
4088 : :
4089 : : gimple_opt_pass *
4090 : 285689 : make_pass_lower_eh_dispatch (gcc::context *ctxt)
4091 : : {
4092 : 285689 : return new pass_lower_eh_dispatch (ctxt);
4093 : : }
4094 : :
4095 : : /* Walk statements, see what regions and, optionally, landing pads
4096 : : are really referenced.
4097 : :
4098 : : Returns in R_REACHABLEP an sbitmap with bits set for reachable regions,
4099 : : and in LP_REACHABLE an sbitmap with bits set for reachable landing pads.
4100 : :
4101 : : Passing NULL for LP_REACHABLE is valid, in this case only reachable
4102 : : regions are marked.
4103 : :
4104 : : The caller is responsible for freeing the returned sbitmaps. */
4105 : :
4106 : : static void
4107 : 1475163 : mark_reachable_handlers (sbitmap *r_reachablep, sbitmap *lp_reachablep)
4108 : : {
4109 : 1475163 : sbitmap r_reachable, lp_reachable;
4110 : 1475163 : basic_block bb;
4111 : 1475163 : bool mark_landing_pads = (lp_reachablep != NULL);
4112 : 1475163 : gcc_checking_assert (r_reachablep != NULL);
4113 : :
4114 : 1475163 : r_reachable = sbitmap_alloc (cfun->eh->region_array->length ());
4115 : 1475163 : bitmap_clear (r_reachable);
4116 : 1475163 : *r_reachablep = r_reachable;
4117 : :
4118 : 1475163 : if (mark_landing_pads)
4119 : : {
4120 : 1187599 : lp_reachable = sbitmap_alloc (cfun->eh->lp_array->length ());
4121 : 1187599 : bitmap_clear (lp_reachable);
4122 : 1187599 : *lp_reachablep = lp_reachable;
4123 : : }
4124 : : else
4125 : : lp_reachable = NULL;
4126 : :
4127 : 22110054 : FOR_EACH_BB_FN (bb, cfun)
4128 : : {
4129 : 20634891 : gimple_stmt_iterator gsi;
4130 : :
4131 : 189523529 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4132 : : {
4133 : 148253747 : gimple *stmt = gsi_stmt (gsi);
4134 : :
4135 : 148253747 : if (mark_landing_pads)
4136 : : {
4137 : 87776597 : int lp_nr = lookup_stmt_eh_lp (stmt);
4138 : :
4139 : : /* Negative LP numbers are MUST_NOT_THROW regions which
4140 : : are not considered BB enders. */
4141 : 87776597 : if (lp_nr < 0)
4142 : 87815 : bitmap_set_bit (r_reachable, -lp_nr);
4143 : :
4144 : : /* Positive LP numbers are real landing pads, and BB enders. */
4145 : 87688782 : else if (lp_nr > 0)
4146 : : {
4147 : 3506522 : gcc_assert (gsi_one_before_end_p (gsi));
4148 : 3506522 : eh_region region = get_eh_region_from_lp_number (lp_nr);
4149 : 3506522 : bitmap_set_bit (r_reachable, region->index);
4150 : 3506522 : bitmap_set_bit (lp_reachable, lp_nr);
4151 : : }
4152 : : }
4153 : :
4154 : : /* Avoid removing regions referenced from RESX/EH_DISPATCH. */
4155 : 148253747 : switch (gimple_code (stmt))
4156 : : {
4157 : 1182110 : case GIMPLE_RESX:
4158 : 2364220 : bitmap_set_bit (r_reachable,
4159 : 1182110 : gimple_resx_region (as_a <gresx *> (stmt)));
4160 : 1182110 : break;
4161 : 64906 : case GIMPLE_EH_DISPATCH:
4162 : 129812 : bitmap_set_bit (r_reachable,
4163 : : gimple_eh_dispatch_region (
4164 : 64906 : as_a <geh_dispatch *> (stmt)));
4165 : 64906 : break;
4166 : 8001264 : case GIMPLE_CALL:
4167 : 8001264 : if (gimple_call_builtin_p (stmt, BUILT_IN_EH_COPY_VALUES))
4168 : 1836 : for (int i = 0; i < 2; ++i)
4169 : : {
4170 : 1224 : tree rt = gimple_call_arg (stmt, i);
4171 : 1224 : HOST_WIDE_INT ri = tree_to_shwi (rt);
4172 : :
4173 : 1224 : gcc_assert (ri == (int)ri);
4174 : 1224 : bitmap_set_bit (r_reachable, ri);
4175 : : }
4176 : : break;
4177 : : default:
4178 : : break;
4179 : : }
4180 : : }
4181 : : }
4182 : 1475163 : }
4183 : :
4184 : : /* Remove unreachable handlers and unreachable landing pads. */
4185 : :
4186 : : static void
4187 : 1187599 : remove_unreachable_handlers (void)
4188 : : {
4189 : 1187599 : sbitmap r_reachable, lp_reachable;
4190 : 1187599 : eh_region region;
4191 : 1187599 : eh_landing_pad lp;
4192 : 1187599 : unsigned i;
4193 : :
4194 : 1187599 : mark_reachable_handlers (&r_reachable, &lp_reachable);
4195 : :
4196 : 1187599 : if (dump_file)
4197 : : {
4198 : 3 : fprintf (dump_file, "Before removal of unreachable regions:\n");
4199 : 3 : dump_eh_tree (dump_file, cfun);
4200 : 3 : fprintf (dump_file, "Reachable regions: ");
4201 : 3 : dump_bitmap_file (dump_file, r_reachable);
4202 : 3 : fprintf (dump_file, "Reachable landing pads: ");
4203 : 3 : dump_bitmap_file (dump_file, lp_reachable);
4204 : : }
4205 : :
4206 : 1187599 : if (dump_file)
4207 : : {
4208 : 18 : FOR_EACH_VEC_SAFE_ELT (cfun->eh->region_array, i, region)
4209 : 15 : if (region && !bitmap_bit_p (r_reachable, region->index))
4210 : 6 : fprintf (dump_file,
4211 : : "Removing unreachable region %d\n",
4212 : : region->index);
4213 : : }
4214 : :
4215 : 1187599 : remove_unreachable_eh_regions (r_reachable);
4216 : :
4217 : 6062157 : FOR_EACH_VEC_SAFE_ELT (cfun->eh->lp_array, i, lp)
4218 : 3686959 : if (lp && !bitmap_bit_p (lp_reachable, lp->index))
4219 : : {
4220 : 13314 : if (dump_file)
4221 : 0 : fprintf (dump_file,
4222 : : "Removing unreachable landing pad %d\n",
4223 : : lp->index);
4224 : 13314 : remove_eh_landing_pad (lp);
4225 : : }
4226 : :
4227 : 1187599 : if (dump_file)
4228 : : {
4229 : 3 : fprintf (dump_file, "\n\nAfter removal of unreachable regions:\n");
4230 : 3 : dump_eh_tree (dump_file, cfun);
4231 : 3 : fprintf (dump_file, "\n\n");
4232 : : }
4233 : :
4234 : 1187599 : sbitmap_free (r_reachable);
4235 : 1187599 : sbitmap_free (lp_reachable);
4236 : :
4237 : 1187599 : if (flag_checking)
4238 : 1187584 : verify_eh_tree (cfun);
4239 : 1187599 : }
4240 : :
4241 : : /* Remove unreachable handlers if any landing pads have been removed after
4242 : : last ehcleanup pass (due to gimple_purge_dead_eh_edges). */
4243 : :
4244 : : void
4245 : 1723069 : maybe_remove_unreachable_handlers (void)
4246 : : {
4247 : 1723069 : eh_landing_pad lp;
4248 : 1723069 : unsigned i;
4249 : :
4250 : 1723069 : if (cfun->eh == NULL)
4251 : : return;
4252 : :
4253 : 5449305 : FOR_EACH_VEC_SAFE_ELT (cfun->eh->lp_array, i, lp)
4254 : 3729954 : if (lp
4255 : 3729954 : && (lp->post_landing_pad == NULL_TREE
4256 : 440098 : || label_to_block (cfun, lp->post_landing_pad) == NULL))
4257 : : {
4258 : 3718 : remove_unreachable_handlers ();
4259 : 3718 : return;
4260 : : }
4261 : : }
4262 : :
4263 : : /* Remove regions that do not have landing pads. This assumes
4264 : : that remove_unreachable_handlers has already been run, and
4265 : : that we've just manipulated the landing pads since then.
4266 : :
4267 : : Preserve regions with landing pads and regions that prevent
4268 : : exceptions from propagating further, even if these regions
4269 : : are not reachable. */
4270 : :
4271 : : static void
4272 : 287564 : remove_unreachable_handlers_no_lp (void)
4273 : : {
4274 : 287564 : eh_region region;
4275 : 287564 : sbitmap r_reachable;
4276 : 287564 : unsigned i;
4277 : :
4278 : 287564 : mark_reachable_handlers (&r_reachable, /*lp_reachablep=*/NULL);
4279 : :
4280 : 3194679 : FOR_EACH_VEC_SAFE_ELT (cfun->eh->region_array, i, region)
4281 : : {
4282 : 2619551 : if (! region)
4283 : 1686421 : continue;
4284 : :
4285 : 933130 : if (region->landing_pads != NULL
4286 : 592244 : || region->type == ERT_MUST_NOT_THROW)
4287 : 372731 : bitmap_set_bit (r_reachable, region->index);
4288 : :
4289 : 933130 : if (dump_file
4290 : 933130 : && !bitmap_bit_p (r_reachable, region->index))
4291 : 6 : fprintf (dump_file,
4292 : : "Removing unreachable region %d\n",
4293 : : region->index);
4294 : : }
4295 : :
4296 : 287564 : remove_unreachable_eh_regions (r_reachable);
4297 : :
4298 : 287564 : sbitmap_free (r_reachable);
4299 : 287564 : }
4300 : :
4301 : : /* Undo critical edge splitting on an EH landing pad. Earlier, we
4302 : : optimisticaly split all sorts of edges, including EH edges. The
4303 : : optimization passes in between may not have needed them; if not,
4304 : : we should undo the split.
4305 : :
4306 : : Recognize this case by having one EH edge incoming to the BB and
4307 : : one normal edge outgoing; BB should be empty apart from the
4308 : : post_landing_pad label.
4309 : :
4310 : : Note that this is slightly different from the empty handler case
4311 : : handled by cleanup_empty_eh, in that the actual handler may yet
4312 : : have actual code but the landing pad has been separated from the
4313 : : handler. As such, cleanup_empty_eh relies on this transformation
4314 : : having been done first. */
4315 : :
4316 : : static bool
4317 : 1903195 : unsplit_eh (eh_landing_pad lp)
4318 : : {
4319 : 1903195 : basic_block bb = label_to_block (cfun, lp->post_landing_pad);
4320 : 1903195 : gimple_stmt_iterator gsi;
4321 : 1903195 : edge e_in, e_out;
4322 : :
4323 : : /* Quickly check the edge counts on BB for singularity. */
4324 : 3904866 : if (!single_pred_p (bb) || !single_succ_p (bb))
4325 : : return false;
4326 : 1457698 : e_in = single_pred_edge (bb);
4327 : 1457698 : e_out = single_succ_edge (bb);
4328 : :
4329 : : /* Input edge must be EH and output edge must be normal. */
4330 : 1457698 : if ((e_in->flags & EDGE_EH) == 0 || (e_out->flags & EDGE_EH) != 0)
4331 : : return false;
4332 : :
4333 : : /* The block must be empty except for the labels and debug insns. */
4334 : 1145738 : gsi = gsi_after_labels (bb);
4335 : 1145738 : if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4336 : 10356 : gsi_next_nondebug (&gsi);
4337 : 1145738 : if (!gsi_end_p (gsi))
4338 : : return false;
4339 : :
4340 : : /* The destination block must not already have a landing pad
4341 : : for a different region. */
4342 : 3207988 : for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
4343 : : {
4344 : 2066614 : glabel *label_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
4345 : 1062828 : tree lab;
4346 : 1062828 : int lp_nr;
4347 : :
4348 : 1062828 : if (!label_stmt)
4349 : : break;
4350 : 1062828 : lab = gimple_label_label (label_stmt);
4351 : 1062828 : lp_nr = EH_LANDING_PAD_NR (lab);
4352 : 1062828 : if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
4353 : : return false;
4354 : : }
4355 : :
4356 : : /* The new destination block must not already be a destination of
4357 : : the source block, lest we merge fallthru and eh edges and get
4358 : : all sorts of confused. */
4359 : 1072580 : if (find_edge (e_in->src, e_out->dest))
4360 : : return false;
4361 : :
4362 : : /* ??? We can get degenerate phis due to cfg cleanups. I would have
4363 : : thought this should have been cleaned up by a phicprop pass, but
4364 : : that doesn't appear to handle virtuals. Propagate by hand. */
4365 : 1072576 : if (!gimple_seq_empty_p (phi_nodes (bb)))
4366 : : {
4367 : 18 : for (gphi_iterator gpi = gsi_start_phis (bb); !gsi_end_p (gpi); )
4368 : : {
4369 : 9 : gimple *use_stmt;
4370 : 9 : gphi *phi = gpi.phi ();
4371 : 9 : tree lhs = gimple_phi_result (phi);
4372 : 9 : tree rhs = gimple_phi_arg_def (phi, 0);
4373 : 9 : use_operand_p use_p;
4374 : 9 : imm_use_iterator iter;
4375 : :
4376 : 26 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4377 : : {
4378 : 51 : FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
4379 : 17 : SET_USE (use_p, rhs);
4380 : 9 : }
4381 : :
4382 : 9 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
4383 : 0 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
4384 : :
4385 : 9 : remove_phi_node (&gpi, true);
4386 : : }
4387 : : }
4388 : :
4389 : 1072576 : if (dump_file && (dump_flags & TDF_DETAILS))
4390 : 0 : fprintf (dump_file, "Unsplit EH landing pad %d to block %i.\n",
4391 : 0 : lp->index, e_out->dest->index);
4392 : :
4393 : : /* Redirect the edge. Since redirect_eh_edge_1 expects to be moving
4394 : : a successor edge, humor it. But do the real CFG change with the
4395 : : predecessor of E_OUT in order to preserve the ordering of arguments
4396 : : to the PHI nodes in E_OUT->DEST. */
4397 : 1072576 : redirect_eh_edge_1 (e_in, e_out->dest, false);
4398 : 1072576 : redirect_edge_pred (e_out, e_in->src);
4399 : 1072576 : e_out->flags = e_in->flags;
4400 : 1072576 : e_out->probability = e_in->probability;
4401 : 1072576 : remove_edge (e_in);
4402 : :
4403 : 1072576 : return true;
4404 : : }
4405 : :
4406 : : /* Examine each landing pad block and see if it matches unsplit_eh. */
4407 : :
4408 : : static bool
4409 : 558297 : unsplit_all_eh (void)
4410 : : {
4411 : 558297 : bool changed = false;
4412 : 558297 : eh_landing_pad lp;
4413 : 558297 : int i;
4414 : :
4415 : 3231977 : for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
4416 : 2673680 : if (lp)
4417 : 1903195 : changed |= unsplit_eh (lp);
4418 : :
4419 : 558297 : return changed;
4420 : : }
4421 : :
4422 : : /* Wrapper around unsplit_all_eh that makes it usable everywhere. */
4423 : :
4424 : : void
4425 : 240963 : unsplit_eh_edges (void)
4426 : : {
4427 : 240963 : bool changed;
4428 : :
4429 : : /* unsplit_all_eh can die looking up unreachable landing pads. */
4430 : 240963 : maybe_remove_unreachable_handlers ();
4431 : :
4432 : 240963 : changed = unsplit_all_eh ();
4433 : :
4434 : : /* If EH edges have been unsplit, delete unreachable forwarder blocks. */
4435 : 240963 : if (changed)
4436 : : {
4437 : 12471 : free_dominance_info (CDI_DOMINATORS);
4438 : 12471 : free_dominance_info (CDI_POST_DOMINATORS);
4439 : 12471 : delete_unreachable_blocks ();
4440 : : }
4441 : 240963 : }
4442 : :
4443 : : /* A subroutine of cleanup_empty_eh. Redirect all EH edges incoming
4444 : : to OLD_BB to NEW_BB; return true on success, false on failure.
4445 : :
4446 : : OLD_BB_OUT is the edge into NEW_BB from OLD_BB, so if we miss any
4447 : : PHI variables from OLD_BB we can pick them up from OLD_BB_OUT.
4448 : : Virtual PHIs may be deleted and marked for renaming. */
4449 : :
4450 : : static bool
4451 : 288287 : cleanup_empty_eh_merge_phis (basic_block new_bb, basic_block old_bb,
4452 : : edge old_bb_out, bool change_region)
4453 : : {
4454 : 288287 : gphi_iterator ngsi, ogsi;
4455 : 288287 : edge_iterator ei;
4456 : 288287 : edge e;
4457 : 288287 : bitmap ophi_handled;
4458 : :
4459 : : /* The destination block must not be a regular successor for any
4460 : : of the preds of the landing pad. Thus, avoid turning
4461 : : <..>
4462 : : | \ EH
4463 : : | <..>
4464 : : | /
4465 : : <..>
4466 : : into
4467 : : <..>
4468 : : | | EH
4469 : : <..>
4470 : : which CFG verification would choke on. See PR45172 and PR51089. */
4471 : 288287 : if (!single_pred_p (new_bb))
4472 : 1172072 : FOR_EACH_EDGE (e, ei, old_bb->preds)
4473 : 920111 : if (find_edge (e->src, new_bb))
4474 : : return false;
4475 : :
4476 : 1593702 : FOR_EACH_EDGE (e, ei, old_bb->preds)
4477 : 1305425 : redirect_edge_var_map_clear (e);
4478 : :
4479 : 288277 : ophi_handled = BITMAP_ALLOC (NULL);
4480 : :
4481 : : /* First, iterate through the PHIs on NEW_BB and set up the edge_var_map
4482 : : for the edges we're going to move. */
4483 : 517645 : for (ngsi = gsi_start_phis (new_bb); !gsi_end_p (ngsi); gsi_next (&ngsi))
4484 : : {
4485 : 229372 : gphi *ophi, *nphi = ngsi.phi ();
4486 : 229372 : tree nresult, nop;
4487 : :
4488 : 229372 : nresult = gimple_phi_result (nphi);
4489 : 229372 : nop = gimple_phi_arg_def (nphi, old_bb_out->dest_idx);
4490 : :
4491 : : /* Find the corresponding PHI in OLD_BB so we can forward-propagate
4492 : : the source ssa_name. */
4493 : 229372 : ophi = NULL;
4494 : 277067 : for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
4495 : : {
4496 : 115990 : ophi = ogsi.phi ();
4497 : 115990 : if (gimple_phi_result (ophi) == nop)
4498 : : break;
4499 : 47695 : ophi = NULL;
4500 : : }
4501 : :
4502 : : /* If we did find the corresponding PHI, copy those inputs. */
4503 : 229372 : if (ophi)
4504 : : {
4505 : : /* If NOP is used somewhere else beyond phis in new_bb, give up. */
4506 : 68295 : if (!has_single_use (nop))
4507 : : {
4508 : 3699 : imm_use_iterator imm_iter;
4509 : 3699 : use_operand_p use_p;
4510 : :
4511 : 11176 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, nop)
4512 : : {
4513 : 7481 : if (!gimple_debug_bind_p (USE_STMT (use_p))
4514 : 7481 : && (gimple_code (USE_STMT (use_p)) != GIMPLE_PHI
4515 : 7475 : || gimple_bb (USE_STMT (use_p)) != new_bb))
4516 : 4 : goto fail;
4517 : : }
4518 : : }
4519 : 68291 : bitmap_set_bit (ophi_handled, SSA_NAME_VERSION (nop));
4520 : 743792 : FOR_EACH_EDGE (e, ei, old_bb->preds)
4521 : : {
4522 : 675501 : location_t oloc;
4523 : 675501 : tree oop;
4524 : :
4525 : 675501 : if ((e->flags & EDGE_EH) == 0)
4526 : 9169 : continue;
4527 : 666332 : oop = gimple_phi_arg_def (ophi, e->dest_idx);
4528 : 666332 : oloc = gimple_phi_arg_location (ophi, e->dest_idx);
4529 : 666332 : redirect_edge_var_map_add (e, nresult, oop, oloc);
4530 : : }
4531 : : }
4532 : : /* If we didn't find the PHI, if it's a real variable or a VOP, we know
4533 : : from the fact that OLD_BB is tree_empty_eh_handler_p that the
4534 : : variable is unchanged from input to the block and we can simply
4535 : : re-use the input to NEW_BB from the OLD_BB_OUT edge. */
4536 : : else
4537 : : {
4538 : 161077 : location_t nloc
4539 : 161077 : = gimple_phi_arg_location (nphi, old_bb_out->dest_idx);
4540 : 569376 : FOR_EACH_EDGE (e, ei, old_bb->preds)
4541 : 408299 : redirect_edge_var_map_add (e, nresult, nop, nloc);
4542 : : }
4543 : : }
4544 : :
4545 : : /* Second, verify that all PHIs from OLD_BB have been handled. If not,
4546 : : we don't know what values from the other edges into NEW_BB to use. */
4547 : 356542 : for (ogsi = gsi_start_phis (old_bb); !gsi_end_p (ogsi); gsi_next (&ogsi))
4548 : : {
4549 : 119177 : gphi *ophi = ogsi.phi ();
4550 : 119177 : tree oresult = gimple_phi_result (ophi);
4551 : 119177 : if (!bitmap_bit_p (ophi_handled, SSA_NAME_VERSION (oresult)))
4552 : 50908 : goto fail;
4553 : : }
4554 : :
4555 : : /* Finally, move the edges and update the PHIs. */
4556 : 1022385 : for (ei = ei_start (old_bb->preds); (e = ei_safe_edge (ei)); )
4557 : 785020 : if (e->flags & EDGE_EH)
4558 : : {
4559 : : /* ??? CFG manipluation routines do not try to update loop
4560 : : form on edge redirection. Do so manually here for now. */
4561 : : /* If we redirect a loop entry or latch edge that will either create
4562 : : a multiple entry loop or rotate the loop. If the loops merge
4563 : : we may have created a loop with multiple latches.
4564 : : All of this isn't easily fixed thus cancel the affected loop
4565 : : and mark the other loop as possibly having multiple latches. */
4566 : 782546 : if (e->dest == e->dest->loop_father->header)
4567 : : {
4568 : 0 : mark_loop_for_removal (e->dest->loop_father);
4569 : 0 : new_bb->loop_father->latch = NULL;
4570 : 0 : loops_state_set (LOOPS_MAY_HAVE_MULTIPLE_LATCHES);
4571 : : }
4572 : 782546 : redirect_eh_edge_1 (e, new_bb, change_region);
4573 : 782546 : redirect_edge_succ (e, new_bb);
4574 : 782546 : flush_pending_stmts (e);
4575 : : }
4576 : : else
4577 : 2474 : ei_next (&ei);
4578 : :
4579 : 237365 : BITMAP_FREE (ophi_handled);
4580 : 237365 : return true;
4581 : :
4582 : 50912 : fail:
4583 : 571317 : FOR_EACH_EDGE (e, ei, old_bb->preds)
4584 : 520405 : redirect_edge_var_map_clear (e);
4585 : 50912 : BITMAP_FREE (ophi_handled);
4586 : 50912 : return false;
4587 : : }
4588 : :
4589 : : /* A subroutine of cleanup_empty_eh. Move a landing pad LP from its
4590 : : old region to NEW_REGION at BB. */
4591 : :
4592 : : static void
4593 : 25268 : cleanup_empty_eh_move_lp (basic_block bb, edge e_out,
4594 : : eh_landing_pad lp, eh_region new_region)
4595 : : {
4596 : 25268 : gimple_stmt_iterator gsi;
4597 : 25268 : eh_landing_pad *pp;
4598 : :
4599 : 25268 : for (pp = &lp->region->landing_pads; *pp != lp; pp = &(*pp)->next_lp)
4600 : 0 : continue;
4601 : 25268 : *pp = lp->next_lp;
4602 : :
4603 : 25268 : lp->region = new_region;
4604 : 25268 : lp->next_lp = new_region->landing_pads;
4605 : 25268 : new_region->landing_pads = lp;
4606 : :
4607 : : /* Delete the RESX that was matched within the empty handler block. */
4608 : 25268 : gsi = gsi_last_bb (bb);
4609 : 25268 : unlink_stmt_vdef (gsi_stmt (gsi));
4610 : 25268 : gsi_remove (&gsi, true);
4611 : :
4612 : : /* Clean up E_OUT for the fallthru. */
4613 : 25268 : e_out->flags = (e_out->flags & ~EDGE_EH) | EDGE_FALLTHRU;
4614 : 25268 : e_out->probability = profile_probability::always ();
4615 : 0 : }
4616 : :
4617 : : /* A subroutine of cleanup_empty_eh. Handle more complex cases of
4618 : : unsplitting than unsplit_eh was prepared to handle, e.g. when
4619 : : multiple incoming edges and phis are involved. */
4620 : :
4621 : : static bool
4622 : 36117 : cleanup_empty_eh_unsplit (basic_block bb, edge e_out, eh_landing_pad lp)
4623 : : {
4624 : 36117 : gimple_stmt_iterator gsi;
4625 : 36117 : tree lab;
4626 : :
4627 : : /* We really ought not have totally lost everything following
4628 : : a landing pad label. Given that BB is empty, there had better
4629 : : be a successor. */
4630 : 36117 : gcc_assert (e_out != NULL);
4631 : :
4632 : : /* The destination block must not already have a landing pad
4633 : : for a different region. */
4634 : 36117 : lab = NULL;
4635 : 107068 : for (gsi = gsi_start_bb (e_out->dest); !gsi_end_p (gsi); gsi_next (&gsi))
4636 : : {
4637 : 69943 : glabel *stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
4638 : 34834 : int lp_nr;
4639 : :
4640 : 34834 : if (!stmt)
4641 : : break;
4642 : 34834 : lab = gimple_label_label (stmt);
4643 : 34834 : lp_nr = EH_LANDING_PAD_NR (lab);
4644 : 34834 : if (lp_nr && get_eh_region_from_lp_number (lp_nr) != lp->region)
4645 : : return false;
4646 : : }
4647 : :
4648 : : /* Attempt to move the PHIs into the successor block. */
4649 : 36117 : if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, false))
4650 : : {
4651 : 11367 : if (dump_file && (dump_flags & TDF_DETAILS))
4652 : 0 : fprintf (dump_file,
4653 : : "Unsplit EH landing pad %d to block %i "
4654 : : "(via cleanup_empty_eh).\n",
4655 : 0 : lp->index, e_out->dest->index);
4656 : 11367 : return true;
4657 : : }
4658 : :
4659 : : return false;
4660 : : }
4661 : :
4662 : : /* Return true if edge E_FIRST is part of an empty infinite loop
4663 : : or leads to such a loop through a series of single successor
4664 : : empty bbs. */
4665 : :
4666 : : static bool
4667 : 36167 : infinite_empty_loop_p (edge e_first)
4668 : : {
4669 : 36167 : bool inf_loop = false;
4670 : 36167 : edge e;
4671 : :
4672 : 36167 : if (e_first->dest == e_first->src)
4673 : : return true;
4674 : :
4675 : 36165 : e_first->src->aux = (void *) 1;
4676 : 37258 : for (e = e_first; single_succ_p (e->dest); e = single_succ_edge (e->dest))
4677 : : {
4678 : 12944 : gimple_stmt_iterator gsi;
4679 : 12944 : if (e->dest->aux)
4680 : : {
4681 : : inf_loop = true;
4682 : 11851 : break;
4683 : : }
4684 : 12896 : e->dest->aux = (void *) 1;
4685 : 12896 : gsi = gsi_after_labels (e->dest);
4686 : 12896 : if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4687 : 6779 : gsi_next_nondebug (&gsi);
4688 : 12896 : if (!gsi_end_p (gsi))
4689 : : break;
4690 : : }
4691 : 36165 : e_first->src->aux = NULL;
4692 : 49061 : for (e = e_first; e->dest->aux; e = single_succ_edge (e->dest))
4693 : 12896 : e->dest->aux = NULL;
4694 : :
4695 : : return inf_loop;
4696 : : }
4697 : :
4698 : : /* Examine the block associated with LP to determine if it's an empty
4699 : : handler for its EH region. If so, attempt to redirect EH edges to
4700 : : an outer region. Return true the CFG was updated in any way. This
4701 : : is similar to jump forwarding, just across EH edges. */
4702 : :
4703 : : static bool
4704 : 1429929 : cleanup_empty_eh (eh_landing_pad lp)
4705 : : {
4706 : 1429929 : basic_block bb = label_to_block (cfun, lp->post_landing_pad);
4707 : 1429929 : gimple_stmt_iterator gsi;
4708 : 1429929 : gimple *resx;
4709 : 1429929 : eh_region new_region;
4710 : 1429929 : edge_iterator ei;
4711 : 1429929 : edge e, e_out;
4712 : 1429929 : bool has_non_eh_pred;
4713 : 1429929 : bool ret = false;
4714 : 1429929 : int new_lp_nr;
4715 : :
4716 : : /* There can be zero or one edges out of BB. This is the quickest test. */
4717 : 1429929 : switch (EDGE_COUNT (bb->succs))
4718 : : {
4719 : : case 0:
4720 : : e_out = NULL;
4721 : : break;
4722 : 784309 : case 1:
4723 : 784309 : e_out = single_succ_edge (bb);
4724 : 784309 : break;
4725 : : default:
4726 : : return false;
4727 : : }
4728 : :
4729 : 1314362 : gsi = gsi_last_nondebug_bb (bb);
4730 : 1314362 : resx = gsi_stmt (gsi);
4731 : 1314362 : if (resx && is_gimple_resx (resx))
4732 : : {
4733 : 1135028 : if (stmt_can_throw_external (cfun, resx))
4734 : 529107 : optimize_clobbers (bb);
4735 : 605921 : else if (sink_clobbers (bb))
4736 : 1314362 : ret = true;
4737 : : }
4738 : :
4739 : 1314362 : gsi = gsi_after_labels (bb);
4740 : :
4741 : : /* Make sure to skip debug statements. */
4742 : 1314362 : if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
4743 : 499105 : gsi_next_nondebug (&gsi);
4744 : :
4745 : : /* If the block is totally empty, look for more unsplitting cases. */
4746 : 1314362 : if (gsi_end_p (gsi))
4747 : : {
4748 : : /* For the degenerate case of an infinite loop bail out.
4749 : : If bb has no successors and is totally empty, which can happen e.g.
4750 : : because of incorrect noreturn attribute, bail out too. */
4751 : 36167 : if (e_out == NULL
4752 : 36167 : || infinite_empty_loop_p (e_out))
4753 : 50 : return ret;
4754 : :
4755 : 36117 : return ret | cleanup_empty_eh_unsplit (bb, e_out, lp);
4756 : : }
4757 : :
4758 : : /* The block should consist only of a single RESX statement, modulo a
4759 : : preceding call to __builtin_stack_restore if there is no outgoing
4760 : : edge, since the call can be eliminated in this case. */
4761 : 1278195 : resx = gsi_stmt (gsi);
4762 : 1278195 : if (!e_out && gimple_call_builtin_p (resx, BUILT_IN_STACK_RESTORE))
4763 : : {
4764 : 321 : gsi_next_nondebug (&gsi);
4765 : 321 : resx = gsi_stmt (gsi);
4766 : : }
4767 : 1278195 : if (!is_gimple_resx (resx))
4768 : : return ret;
4769 : 567316 : gcc_assert (gsi_one_nondebug_before_end_p (gsi));
4770 : :
4771 : : /* Determine if there are non-EH edges, or resx edges into the handler. */
4772 : 567316 : has_non_eh_pred = false;
4773 : 3197020 : FOR_EACH_EDGE (e, ei, bb->preds)
4774 : 2629704 : if (!(e->flags & EDGE_EH))
4775 : 19568 : has_non_eh_pred = true;
4776 : :
4777 : : /* Find the handler that's outer of the empty handler by looking at
4778 : : where the RESX instruction was vectored. */
4779 : 567316 : new_lp_nr = lookup_stmt_eh_lp (resx);
4780 : 567316 : new_region = get_eh_region_from_lp_number (new_lp_nr);
4781 : :
4782 : : /* If there's no destination region within the current function,
4783 : : redirection is trivial via removing the throwing statements from
4784 : : the EH region, removing the EH edges, and allowing the block
4785 : : to go unreachable. */
4786 : 567316 : if (new_region == NULL)
4787 : : {
4788 : 315141 : gcc_assert (e_out == NULL);
4789 : 1973100 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4790 : 1657959 : if (e->flags & EDGE_EH)
4791 : : {
4792 : 1641821 : gimple *stmt = *gsi_last_bb (e->src);
4793 : 1641821 : remove_stmt_from_eh_lp (stmt);
4794 : 1641821 : remove_edge (e);
4795 : : }
4796 : : else
4797 : 16138 : ei_next (&ei);
4798 : 315141 : goto succeed;
4799 : : }
4800 : :
4801 : : /* If the destination region is a MUST_NOT_THROW, allow the runtime
4802 : : to handle the abort and allow the blocks to go unreachable. */
4803 : 252175 : if (new_region->type == ERT_MUST_NOT_THROW)
4804 : : {
4805 : 11 : for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
4806 : 6 : if (e->flags & EDGE_EH)
4807 : : {
4808 : 6 : gimple *stmt = *gsi_last_bb (e->src);
4809 : 6 : remove_stmt_from_eh_lp (stmt);
4810 : 6 : add_stmt_to_eh_lp (stmt, new_lp_nr);
4811 : 6 : remove_edge (e);
4812 : : }
4813 : : else
4814 : 0 : ei_next (&ei);
4815 : 5 : goto succeed;
4816 : : }
4817 : :
4818 : : /* Try to redirect the EH edges and merge the PHIs into the destination
4819 : : landing pad block. If the merge succeeds, we'll already have redirected
4820 : : all the EH edges. The handler itself will go unreachable if there were
4821 : : no normal edges. */
4822 : 252170 : if (cleanup_empty_eh_merge_phis (e_out->dest, bb, e_out, true))
4823 : 225998 : goto succeed;
4824 : :
4825 : : /* Finally, if all input edges are EH edges, then we can (potentially)
4826 : : reduce the number of transfers from the runtime by moving the landing
4827 : : pad from the original region to the new region. This is a win when
4828 : : we remove the last CLEANUP region along a particular exception
4829 : : propagation path. Since nothing changes except for the region with
4830 : : which the landing pad is associated, the PHI nodes do not need to be
4831 : : adjusted at all. */
4832 : 26172 : if (!has_non_eh_pred)
4833 : : {
4834 : 25268 : cleanup_empty_eh_move_lp (bb, e_out, lp, new_region);
4835 : 25268 : if (dump_file && (dump_flags & TDF_DETAILS))
4836 : 0 : fprintf (dump_file, "Empty EH handler %i moved to EH region %i.\n",
4837 : : lp->index, new_region->index);
4838 : :
4839 : : /* ??? The CFG didn't change, but we may have rendered the
4840 : : old EH region unreachable. Trigger a cleanup there. */
4841 : 25268 : return true;
4842 : : }
4843 : :
4844 : : return ret;
4845 : :
4846 : 541144 : succeed:
4847 : 541144 : if (dump_file && (dump_flags & TDF_DETAILS))
4848 : 6 : fprintf (dump_file, "Empty EH handler %i removed.\n", lp->index);
4849 : 541144 : remove_eh_landing_pad (lp);
4850 : 541144 : return true;
4851 : : }
4852 : :
4853 : : /* Do a post-order traversal of the EH region tree. Examine each
4854 : : post_landing_pad block and see if we can eliminate it as empty. */
4855 : :
4856 : : static bool
4857 : 345597 : cleanup_all_empty_eh (void)
4858 : : {
4859 : 345597 : bool changed = false;
4860 : 345597 : eh_landing_pad lp;
4861 : 345597 : int i;
4862 : :
4863 : : /* The post-order traversal may lead to quadraticness in the redirection
4864 : : of incoming EH edges from inner LPs, so first try to walk the region
4865 : : tree from inner to outer LPs in order to eliminate these edges. */
4866 : 2918205 : for (i = vec_safe_length (cfun->eh->lp_array) - 1; i >= 1; --i)
4867 : : {
4868 : 2227011 : lp = (*cfun->eh->lp_array)[i];
4869 : 2227011 : if (lp)
4870 : 988948 : changed |= cleanup_empty_eh (lp);
4871 : : }
4872 : :
4873 : : /* Now do the post-order traversal to eliminate outer empty LPs. */
4874 : 2575655 : for (i = 1; vec_safe_iterate (cfun->eh->lp_array, i, &lp); ++i)
4875 : 2230058 : if (lp)
4876 : 440981 : changed |= cleanup_empty_eh (lp);
4877 : :
4878 : 345597 : return changed;
4879 : : }
4880 : :
4881 : : /* Perform cleanups and lowering of exception handling
4882 : : 1) cleanups regions with handlers doing nothing are optimized out
4883 : : 2) MUST_NOT_THROW regions that became dead because of 1) are optimized out
4884 : : 3) Info about regions that are containing instructions, and regions
4885 : : reachable via local EH edges is collected
4886 : : 4) Eh tree is pruned for regions no longer necessary.
4887 : :
4888 : : TODO: Push MUST_NOT_THROW regions to the root of the EH tree.
4889 : : Unify those that have the same failure decl and locus.
4890 : : */
4891 : :
4892 : : static unsigned int
4893 : 1183881 : execute_cleanup_eh_1 (void)
4894 : : {
4895 : : /* Do this first: unsplit_all_eh and cleanup_all_empty_eh can die
4896 : : looking up unreachable landing pads. */
4897 : 1183881 : remove_unreachable_handlers ();
4898 : :
4899 : : /* Watch out for the region tree vanishing due to all unreachable. */
4900 : 1183881 : if (cfun->eh->region_tree)
4901 : : {
4902 : 345597 : bool changed = false;
4903 : :
4904 : 345597 : if (optimize)
4905 : 317334 : changed |= unsplit_all_eh ();
4906 : 345597 : changed |= cleanup_all_empty_eh ();
4907 : :
4908 : 345597 : if (changed)
4909 : : {
4910 : 287564 : free_dominance_info (CDI_DOMINATORS);
4911 : 287564 : free_dominance_info (CDI_POST_DOMINATORS);
4912 : :
4913 : : /* We delayed all basic block deletion, as we may have performed
4914 : : cleanups on EH edges while non-EH edges were still present. */
4915 : 287564 : delete_unreachable_blocks ();
4916 : :
4917 : : /* We manipulated the landing pads. Remove any region that no
4918 : : longer has a landing pad. */
4919 : 287564 : remove_unreachable_handlers_no_lp ();
4920 : :
4921 : 287564 : return TODO_cleanup_cfg | TODO_update_ssa_only_virtuals;
4922 : : }
4923 : : }
4924 : :
4925 : : return 0;
4926 : : }
4927 : :
4928 : : namespace {
4929 : :
4930 : : const pass_data pass_data_cleanup_eh =
4931 : : {
4932 : : GIMPLE_PASS, /* type */
4933 : : "ehcleanup", /* name */
4934 : : OPTGROUP_NONE, /* optinfo_flags */
4935 : : TV_TREE_EH, /* tv_id */
4936 : : PROP_gimple_lcf, /* properties_required */
4937 : : 0, /* properties_provided */
4938 : : 0, /* properties_destroyed */
4939 : : 0, /* todo_flags_start */
4940 : : 0, /* todo_flags_finish */
4941 : : };
4942 : :
4943 : : class pass_cleanup_eh : public gimple_opt_pass
4944 : : {
4945 : : public:
4946 : 571378 : pass_cleanup_eh (gcc::context *ctxt)
4947 : 1142756 : : gimple_opt_pass (pass_data_cleanup_eh, ctxt)
4948 : : {}
4949 : :
4950 : : /* opt_pass methods: */
4951 : 285689 : opt_pass * clone () final override { return new pass_cleanup_eh (m_ctxt); }
4952 : 3993894 : bool gate (function *fun) final override
4953 : : {
4954 : 3993894 : return fun->eh != NULL && fun->eh->region_tree != NULL;
4955 : : }
4956 : :
4957 : : unsigned int execute (function *) final override;
4958 : :
4959 : : }; // class pass_cleanup_eh
4960 : :
4961 : : unsigned int
4962 : 1183881 : pass_cleanup_eh::execute (function *fun)
4963 : : {
4964 : 1183881 : int ret = execute_cleanup_eh_1 ();
4965 : :
4966 : : /* If the function no longer needs an EH personality routine
4967 : : clear it. This exposes cross-language inlining opportunities
4968 : : and avoids references to a never defined personality routine. */
4969 : 1183881 : if (DECL_FUNCTION_PERSONALITY (current_function_decl)
4970 : 1183881 : && function_needs_eh_personality (fun) != eh_personality_lang)
4971 : 792006 : DECL_FUNCTION_PERSONALITY (current_function_decl) = NULL_TREE;
4972 : :
4973 : 1183881 : return ret;
4974 : : }
4975 : :
4976 : : } // anon namespace
4977 : :
4978 : : gimple_opt_pass *
4979 : 285689 : make_pass_cleanup_eh (gcc::context *ctxt)
4980 : : {
4981 : 285689 : return new pass_cleanup_eh (ctxt);
4982 : : }
4983 : :
4984 : : /* Disable warnings about missing quoting in GCC diagnostics for
4985 : : the verification errors. Their format strings don't follow GCC
4986 : : diagnostic conventions but are only used for debugging. */
4987 : : #if __GNUC__ >= 10
4988 : : # pragma GCC diagnostic push
4989 : : # pragma GCC diagnostic ignored "-Wformat-diag"
4990 : : #endif
4991 : :
4992 : : /* Verify that BB containing STMT as the last statement, has precisely the
4993 : : edge that make_eh_edge would create. */
4994 : :
4995 : : DEBUG_FUNCTION bool
4996 : 1840966970 : verify_eh_edges (gimple *stmt)
4997 : : {
4998 : 1840966970 : basic_block bb = gimple_bb (stmt);
4999 : 1840966970 : eh_landing_pad lp = NULL;
5000 : 1840966970 : int lp_nr;
5001 : 1840966970 : edge_iterator ei;
5002 : 1840966970 : edge e, eh_edge;
5003 : :
5004 : 1840966970 : lp_nr = lookup_stmt_eh_lp (stmt);
5005 : 1840966970 : if (lp_nr > 0)
5006 : 209871729 : lp = get_eh_landing_pad_from_number (lp_nr);
5007 : :
5008 : 1840966970 : eh_edge = NULL;
5009 : 4485456303 : FOR_EACH_EDGE (e, ei, bb->succs)
5010 : : {
5011 : 2644489333 : if (e->flags & EDGE_EH)
5012 : : {
5013 : 209871729 : if (eh_edge)
5014 : : {
5015 : 0 : error ("BB %i has multiple EH edges", bb->index);
5016 : 0 : return true;
5017 : : }
5018 : : else
5019 : : eh_edge = e;
5020 : : }
5021 : : }
5022 : :
5023 : 1840966970 : if (lp == NULL)
5024 : : {
5025 : 1631095241 : if (eh_edge)
5026 : : {
5027 : 0 : error ("BB %i cannot throw but has an EH edge", bb->index);
5028 : 0 : return true;
5029 : : }
5030 : : return false;
5031 : : }
5032 : :
5033 : 209871729 : if (!stmt_could_throw_p (cfun, stmt))
5034 : : {
5035 : 0 : error ("BB %i last statement has incorrectly set lp", bb->index);
5036 : 0 : return true;
5037 : : }
5038 : :
5039 : 209871729 : if (eh_edge == NULL)
5040 : : {
5041 : 0 : error ("BB %i is missing an EH edge", bb->index);
5042 : 0 : return true;
5043 : : }
5044 : :
5045 : 209871729 : if (eh_edge->dest != label_to_block (cfun, lp->post_landing_pad))
5046 : : {
5047 : 0 : error ("Incorrect EH edge %i->%i", bb->index, eh_edge->dest->index);
5048 : 0 : return true;
5049 : : }
5050 : :
5051 : : return false;
5052 : : }
5053 : :
5054 : : /* Similarly, but handle GIMPLE_EH_DISPATCH specifically. */
5055 : :
5056 : : DEBUG_FUNCTION bool
5057 : 1886435 : verify_eh_dispatch_edge (geh_dispatch *stmt)
5058 : : {
5059 : 1886435 : eh_region r;
5060 : 1886435 : eh_catch c;
5061 : 1886435 : basic_block src, dst;
5062 : 1886435 : bool want_fallthru = true;
5063 : 1886435 : edge_iterator ei;
5064 : 1886435 : edge e, fall_edge;
5065 : :
5066 : 1886435 : r = get_eh_region_from_number (gimple_eh_dispatch_region (stmt));
5067 : 1886435 : src = gimple_bb (stmt);
5068 : :
5069 : 4020187 : FOR_EACH_EDGE (e, ei, src->succs)
5070 : 2133752 : gcc_assert (e->aux == NULL);
5071 : :
5072 : 1886435 : switch (r->type)
5073 : : {
5074 : 1877390 : case ERT_TRY:
5075 : 2115662 : for (c = r->u.eh_try.first_catch; c ; c = c->next_catch)
5076 : : {
5077 : 1985747 : dst = label_to_block (cfun, c->label);
5078 : 1985747 : e = find_edge (src, dst);
5079 : 1985747 : if (e == NULL)
5080 : : {
5081 : 0 : error ("BB %i is missing an edge", src->index);
5082 : 0 : return true;
5083 : : }
5084 : 1985747 : e->aux = (void *)e;
5085 : :
5086 : : /* A catch-all handler doesn't have a fallthru. */
5087 : 1985747 : if (c->type_list == NULL)
5088 : : {
5089 : : want_fallthru = false;
5090 : : break;
5091 : : }
5092 : : }
5093 : : break;
5094 : :
5095 : 9045 : case ERT_ALLOWED_EXCEPTIONS:
5096 : 9045 : dst = label_to_block (cfun, r->u.allowed.label);
5097 : 9045 : e = find_edge (src, dst);
5098 : 9045 : if (e == NULL)
5099 : : {
5100 : 0 : error ("BB %i is missing an edge", src->index);
5101 : 0 : return true;
5102 : : }
5103 : 9045 : e->aux = (void *)e;
5104 : 9045 : break;
5105 : :
5106 : 0 : default:
5107 : 0 : gcc_unreachable ();
5108 : : }
5109 : :
5110 : 1886435 : fall_edge = NULL;
5111 : 4020187 : FOR_EACH_EDGE (e, ei, src->succs)
5112 : : {
5113 : 2133752 : if (e->flags & EDGE_FALLTHRU)
5114 : : {
5115 : 138960 : if (fall_edge != NULL)
5116 : : {
5117 : 0 : error ("BB %i too many fallthru edges", src->index);
5118 : 0 : return true;
5119 : : }
5120 : : fall_edge = e;
5121 : : }
5122 : 1994792 : else if (e->aux)
5123 : 1994792 : e->aux = NULL;
5124 : : else
5125 : : {
5126 : 0 : error ("BB %i has incorrect edge", src->index);
5127 : 0 : return true;
5128 : : }
5129 : : }
5130 : 1886435 : if ((fall_edge != NULL) ^ want_fallthru)
5131 : : {
5132 : 0 : error ("BB %i has incorrect fallthru edge", src->index);
5133 : 0 : return true;
5134 : : }
5135 : :
5136 : : return false;
5137 : : }
5138 : :
5139 : : #if __GNUC__ >= 10
5140 : : # pragma GCC diagnostic pop
5141 : : #endif
|