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