]>
Commit | Line | Data |
---|---|---|
c9784e6d | 1 | /* CFG cleanup for trees. |
d1e082c2 | 2 | Copyright (C) 2001-2013 Free Software Foundation, Inc. |
c9784e6d KH |
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) |
c9784e6d KH |
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/>. */ | |
c9784e6d KH |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tm.h" | |
24 | #include "tree.h" | |
c9784e6d | 25 | #include "tm_p.h" |
c9784e6d | 26 | #include "basic-block.h" |
718f9c0f | 27 | #include "diagnostic-core.h" |
c9784e6d KH |
28 | #include "flags.h" |
29 | #include "function.h" | |
c9784e6d KH |
30 | #include "ggc.h" |
31 | #include "langhooks.h" | |
c9784e6d | 32 | #include "tree-flow.h" |
c9784e6d | 33 | #include "tree-pass.h" |
c9784e6d KH |
34 | #include "except.h" |
35 | #include "cfgloop.h" | |
c9784e6d KH |
36 | #include "hashtab.h" |
37 | #include "tree-ssa-propagate.h" | |
17684618 | 38 | #include "tree-scalar-evolution.h" |
c9784e6d | 39 | |
672987e8 ZD |
40 | /* The set of blocks in that at least one of the following changes happened: |
41 | -- the statement at the end of the block was changed | |
42 | -- the block was newly created | |
43 | -- the set of the predecessors of the block changed | |
44 | -- the set of the successors of the block changed | |
45 | ??? Maybe we could track these changes separately, since they determine | |
46 | what cleanups it makes sense to try on the block. */ | |
47 | bitmap cfgcleanup_altered_bbs; | |
48 | ||
c9784e6d KH |
49 | /* Remove any fallthru edge from EV. Return true if an edge was removed. */ |
50 | ||
51 | static bool | |
9771b263 | 52 | remove_fallthru_edge (vec<edge, va_gc> *ev) |
c9784e6d KH |
53 | { |
54 | edge_iterator ei; | |
55 | edge e; | |
56 | ||
57 | FOR_EACH_EDGE (e, ei, ev) | |
58 | if ((e->flags & EDGE_FALLTHRU) != 0) | |
59 | { | |
672987e8 | 60 | remove_edge_and_dominated_blocks (e); |
c9784e6d KH |
61 | return true; |
62 | } | |
63 | return false; | |
64 | } | |
65 | ||
726a989a | 66 | |
c9784e6d KH |
67 | /* Disconnect an unreachable block in the control expression starting |
68 | at block BB. */ | |
69 | ||
70 | static bool | |
726a989a | 71 | cleanup_control_expr_graph (basic_block bb, gimple_stmt_iterator gsi) |
c9784e6d KH |
72 | { |
73 | edge taken_edge; | |
74 | bool retval = false; | |
726a989a RB |
75 | gimple stmt = gsi_stmt (gsi); |
76 | tree val; | |
c9784e6d KH |
77 | |
78 | if (!single_succ_p (bb)) | |
79 | { | |
80 | edge e; | |
81 | edge_iterator ei; | |
6ac01510 | 82 | bool warned; |
37530014 | 83 | location_t loc; |
6ac01510 ILT |
84 | |
85 | fold_defer_overflow_warnings (); | |
37530014 RG |
86 | loc = gimple_location (stmt); |
87 | switch (gimple_code (stmt)) | |
88 | { | |
89 | case GIMPLE_COND: | |
fea4ea73 EB |
90 | val = fold_binary_loc (loc, gimple_cond_code (stmt), |
91 | boolean_type_node, | |
92 | gimple_cond_lhs (stmt), | |
93 | gimple_cond_rhs (stmt)); | |
94 | break; | |
37530014 RG |
95 | |
96 | case GIMPLE_SWITCH: | |
97 | val = gimple_switch_index (stmt); | |
98 | break; | |
99 | ||
100 | default: | |
101 | val = NULL_TREE; | |
102 | } | |
c9784e6d KH |
103 | taken_edge = find_taken_edge (bb, val); |
104 | if (!taken_edge) | |
6ac01510 ILT |
105 | { |
106 | fold_undefer_and_ignore_overflow_warnings (); | |
107 | return false; | |
108 | } | |
c9784e6d KH |
109 | |
110 | /* Remove all the edges except the one that is always executed. */ | |
6ac01510 | 111 | warned = false; |
c9784e6d KH |
112 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
113 | { | |
114 | if (e != taken_edge) | |
115 | { | |
6ac01510 ILT |
116 | if (!warned) |
117 | { | |
118 | fold_undefer_overflow_warnings | |
726a989a | 119 | (true, stmt, WARN_STRICT_OVERFLOW_CONDITIONAL); |
6ac01510 ILT |
120 | warned = true; |
121 | } | |
122 | ||
c9784e6d KH |
123 | taken_edge->probability += e->probability; |
124 | taken_edge->count += e->count; | |
672987e8 | 125 | remove_edge_and_dominated_blocks (e); |
c9784e6d KH |
126 | retval = true; |
127 | } | |
128 | else | |
129 | ei_next (&ei); | |
130 | } | |
6ac01510 ILT |
131 | if (!warned) |
132 | fold_undefer_and_ignore_overflow_warnings (); | |
c9784e6d KH |
133 | if (taken_edge->probability > REG_BR_PROB_BASE) |
134 | taken_edge->probability = REG_BR_PROB_BASE; | |
135 | } | |
136 | else | |
137 | taken_edge = single_succ_edge (bb); | |
138 | ||
672987e8 | 139 | bitmap_set_bit (cfgcleanup_altered_bbs, bb->index); |
726a989a | 140 | gsi_remove (&gsi, true); |
c9784e6d KH |
141 | taken_edge->flags = EDGE_FALLTHRU; |
142 | ||
c9784e6d KH |
143 | return retval; |
144 | } | |
145 | ||
672987e8 ZD |
146 | /* Try to remove superfluous control structures in basic block BB. Returns |
147 | true if anything changes. */ | |
c9784e6d KH |
148 | |
149 | static bool | |
672987e8 | 150 | cleanup_control_flow_bb (basic_block bb) |
c9784e6d | 151 | { |
726a989a | 152 | gimple_stmt_iterator gsi; |
c9784e6d | 153 | bool retval = false; |
726a989a | 154 | gimple stmt; |
c9784e6d | 155 | |
672987e8 ZD |
156 | /* If the last statement of the block could throw and now cannot, |
157 | we need to prune cfg. */ | |
726a989a | 158 | retval |= gimple_purge_dead_eh_edges (bb); |
672987e8 | 159 | |
726a989a RB |
160 | gsi = gsi_last_bb (bb); |
161 | if (gsi_end_p (gsi)) | |
672987e8 ZD |
162 | return retval; |
163 | ||
726a989a | 164 | stmt = gsi_stmt (gsi); |
672987e8 | 165 | |
726a989a RB |
166 | if (gimple_code (stmt) == GIMPLE_COND |
167 | || gimple_code (stmt) == GIMPLE_SWITCH) | |
168 | retval |= cleanup_control_expr_graph (bb, gsi); | |
169 | else if (gimple_code (stmt) == GIMPLE_GOTO | |
170 | && TREE_CODE (gimple_goto_dest (stmt)) == ADDR_EXPR | |
171 | && (TREE_CODE (TREE_OPERAND (gimple_goto_dest (stmt), 0)) | |
672987e8 | 172 | == LABEL_DECL)) |
c9784e6d | 173 | { |
726a989a RB |
174 | /* If we had a computed goto which has a compile-time determinable |
175 | destination, then we can eliminate the goto. */ | |
672987e8 ZD |
176 | edge e; |
177 | tree label; | |
178 | edge_iterator ei; | |
179 | basic_block target_block; | |
c9784e6d | 180 | |
672987e8 ZD |
181 | /* First look at all the outgoing edges. Delete any outgoing |
182 | edges which do not go to the right block. For the one | |
183 | edge which goes to the right block, fix up its flags. */ | |
726a989a | 184 | label = TREE_OPERAND (gimple_goto_dest (stmt), 0); |
672987e8 ZD |
185 | target_block = label_to_block (label); |
186 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
c9784e6d | 187 | { |
672987e8 ZD |
188 | if (e->dest != target_block) |
189 | remove_edge_and_dominated_blocks (e); | |
190 | else | |
c9784e6d | 191 | { |
672987e8 ZD |
192 | /* Turn off the EDGE_ABNORMAL flag. */ |
193 | e->flags &= ~EDGE_ABNORMAL; | |
c9784e6d | 194 | |
672987e8 ZD |
195 | /* And set EDGE_FALLTHRU. */ |
196 | e->flags |= EDGE_FALLTHRU; | |
197 | ei_next (&ei); | |
198 | } | |
c9784e6d KH |
199 | } |
200 | ||
672987e8 ZD |
201 | bitmap_set_bit (cfgcleanup_altered_bbs, bb->index); |
202 | bitmap_set_bit (cfgcleanup_altered_bbs, target_block->index); | |
203 | ||
204 | /* Remove the GOTO_EXPR as it is not needed. The CFG has all the | |
205 | relevant information we need. */ | |
726a989a | 206 | gsi_remove (&gsi, true); |
672987e8 | 207 | retval = true; |
c9784e6d | 208 | } |
672987e8 ZD |
209 | |
210 | /* Check for indirect calls that have been turned into | |
211 | noreturn calls. */ | |
726a989a RB |
212 | else if (is_gimple_call (stmt) |
213 | && gimple_call_noreturn_p (stmt) | |
214 | && remove_fallthru_edge (bb->succs)) | |
672987e8 ZD |
215 | retval = true; |
216 | ||
c9784e6d KH |
217 | return retval; |
218 | } | |
219 | ||
220 | /* Return true if basic block BB does nothing except pass control | |
221 | flow to another block and that we can safely insert a label at | |
222 | the start of the successor block. | |
223 | ||
224 | As a precondition, we require that BB be not equal to | |
225 | ENTRY_BLOCK_PTR. */ | |
226 | ||
227 | static bool | |
228 | tree_forwarder_block_p (basic_block bb, bool phi_wanted) | |
229 | { | |
726a989a | 230 | gimple_stmt_iterator gsi; |
f99fcb3b | 231 | location_t locus; |
c9784e6d KH |
232 | |
233 | /* BB must have a single outgoing edge. */ | |
234 | if (single_succ_p (bb) != 1 | |
235 | /* If PHI_WANTED is false, BB must not have any PHI nodes. | |
236 | Otherwise, BB must have PHI nodes. */ | |
726a989a | 237 | || gimple_seq_empty_p (phi_nodes (bb)) == phi_wanted |
c9784e6d KH |
238 | /* BB may not be a predecessor of EXIT_BLOCK_PTR. */ |
239 | || single_succ (bb) == EXIT_BLOCK_PTR | |
240 | /* Nor should this be an infinite loop. */ | |
241 | || single_succ (bb) == bb | |
242 | /* BB may not have an abnormal outgoing edge. */ | |
243 | || (single_succ_edge (bb)->flags & EDGE_ABNORMAL)) | |
244 | return false; | |
245 | ||
77a74ed7 | 246 | gcc_checking_assert (bb != ENTRY_BLOCK_PTR); |
c9784e6d | 247 | |
f99fcb3b JJ |
248 | locus = single_succ_edge (bb)->goto_locus; |
249 | ||
1d65f45c RH |
250 | /* There should not be an edge coming from entry, or an EH edge. */ |
251 | { | |
252 | edge_iterator ei; | |
253 | edge e; | |
254 | ||
255 | FOR_EACH_EDGE (e, ei, bb->preds) | |
256 | if (e->src == ENTRY_BLOCK_PTR || (e->flags & EDGE_EH)) | |
257 | return false; | |
f99fcb3b JJ |
258 | /* If goto_locus of any of the edges differs, prevent removing |
259 | the forwarder block for -O0. */ | |
260 | else if (optimize == 0 && e->goto_locus != locus) | |
261 | return false; | |
1d65f45c RH |
262 | } |
263 | ||
c9784e6d KH |
264 | /* Now walk through the statements backward. We can ignore labels, |
265 | anything else means this is not a forwarder block. */ | |
726a989a | 266 | for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi)) |
c9784e6d | 267 | { |
726a989a | 268 | gimple stmt = gsi_stmt (gsi); |
c9784e6d | 269 | |
726a989a | 270 | switch (gimple_code (stmt)) |
c9784e6d | 271 | { |
726a989a RB |
272 | case GIMPLE_LABEL: |
273 | if (DECL_NONLOCAL (gimple_label_label (stmt))) | |
c9784e6d | 274 | return false; |
f99fcb3b JJ |
275 | if (optimize == 0 && gimple_location (stmt) != locus) |
276 | return false; | |
c9784e6d KH |
277 | break; |
278 | ||
b5b8b0ac AO |
279 | /* ??? For now, hope there's a corresponding debug |
280 | assignment at the destination. */ | |
281 | case GIMPLE_DEBUG: | |
282 | break; | |
283 | ||
c9784e6d KH |
284 | default: |
285 | return false; | |
286 | } | |
287 | } | |
288 | ||
c9784e6d KH |
289 | if (current_loops) |
290 | { | |
291 | basic_block dest; | |
292 | /* Protect loop latches, headers and preheaders. */ | |
293 | if (bb->loop_father->header == bb) | |
294 | return false; | |
295 | dest = EDGE_SUCC (bb, 0)->dest; | |
296 | ||
297 | if (dest->loop_father->header == dest) | |
298 | return false; | |
299 | } | |
c9784e6d KH |
300 | return true; |
301 | } | |
302 | ||
c9784e6d KH |
303 | /* If all the PHI nodes in DEST have alternatives for E1 and E2 and |
304 | those alternatives are equal in each of the PHI nodes, then return | |
305 | true, else return false. */ | |
306 | ||
307 | static bool | |
308 | phi_alternatives_equal (basic_block dest, edge e1, edge e2) | |
309 | { | |
310 | int n1 = e1->dest_idx; | |
311 | int n2 = e2->dest_idx; | |
726a989a | 312 | gimple_stmt_iterator gsi; |
c9784e6d | 313 | |
726a989a | 314 | for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi)) |
c9784e6d | 315 | { |
726a989a RB |
316 | gimple phi = gsi_stmt (gsi); |
317 | tree val1 = gimple_phi_arg_def (phi, n1); | |
318 | tree val2 = gimple_phi_arg_def (phi, n2); | |
c9784e6d KH |
319 | |
320 | gcc_assert (val1 != NULL_TREE); | |
321 | gcc_assert (val2 != NULL_TREE); | |
322 | ||
323 | if (!operand_equal_for_phi_arg_p (val1, val2)) | |
324 | return false; | |
325 | } | |
326 | ||
327 | return true; | |
328 | } | |
329 | ||
672987e8 | 330 | /* Removes forwarder block BB. Returns false if this failed. */ |
c9784e6d KH |
331 | |
332 | static bool | |
672987e8 | 333 | remove_forwarder_block (basic_block bb) |
c9784e6d KH |
334 | { |
335 | edge succ = single_succ_edge (bb), e, s; | |
336 | basic_block dest = succ->dest; | |
726a989a | 337 | gimple label; |
c9784e6d | 338 | edge_iterator ei; |
726a989a | 339 | gimple_stmt_iterator gsi, gsi_to; |
70235ab9 | 340 | bool can_move_debug_stmts; |
c9784e6d KH |
341 | |
342 | /* We check for infinite loops already in tree_forwarder_block_p. | |
343 | However it may happen that the infinite loop is created | |
344 | afterwards due to removal of forwarders. */ | |
345 | if (dest == bb) | |
346 | return false; | |
347 | ||
28e5ca15 RB |
348 | /* If the destination block consists of a nonlocal label or is a |
349 | EH landing pad, do not merge it. */ | |
c9784e6d KH |
350 | label = first_stmt (dest); |
351 | if (label | |
726a989a | 352 | && gimple_code (label) == GIMPLE_LABEL |
28e5ca15 RB |
353 | && (DECL_NONLOCAL (gimple_label_label (label)) |
354 | || EH_LANDING_PAD_NR (gimple_label_label (label)) != 0)) | |
c9784e6d KH |
355 | return false; |
356 | ||
357 | /* If there is an abnormal edge to basic block BB, but not into | |
358 | dest, problems might occur during removal of the phi node at out | |
359 | of ssa due to overlapping live ranges of registers. | |
360 | ||
361 | If there is an abnormal edge in DEST, the problems would occur | |
362 | anyway since cleanup_dead_labels would then merge the labels for | |
363 | two different eh regions, and rest of exception handling code | |
364 | does not like it. | |
365 | ||
366 | So if there is an abnormal edge to BB, proceed only if there is | |
367 | no abnormal edge to DEST and there are no phi nodes in DEST. */ | |
31ff2426 NF |
368 | if (bb_has_abnormal_pred (bb) |
369 | && (bb_has_abnormal_pred (dest) | |
1197e789 RG |
370 | || !gimple_seq_empty_p (phi_nodes (dest)))) |
371 | return false; | |
c9784e6d KH |
372 | |
373 | /* If there are phi nodes in DEST, and some of the blocks that are | |
374 | predecessors of BB are also predecessors of DEST, check that the | |
375 | phi node arguments match. */ | |
726a989a | 376 | if (!gimple_seq_empty_p (phi_nodes (dest))) |
c9784e6d KH |
377 | { |
378 | FOR_EACH_EDGE (e, ei, bb->preds) | |
379 | { | |
380 | s = find_edge (e->src, dest); | |
381 | if (!s) | |
382 | continue; | |
383 | ||
384 | if (!phi_alternatives_equal (dest, succ, s)) | |
385 | return false; | |
386 | } | |
387 | } | |
388 | ||
dc764d10 | 389 | can_move_debug_stmts = MAY_HAVE_DEBUG_STMTS && single_pred_p (dest); |
70235ab9 | 390 | |
c9784e6d KH |
391 | /* Redirect the edges. */ |
392 | for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); ) | |
393 | { | |
672987e8 ZD |
394 | bitmap_set_bit (cfgcleanup_altered_bbs, e->src->index); |
395 | ||
c9784e6d KH |
396 | if (e->flags & EDGE_ABNORMAL) |
397 | { | |
398 | /* If there is an abnormal edge, redirect it anyway, and | |
399 | move the labels to the new block to make it legal. */ | |
400 | s = redirect_edge_succ_nodup (e, dest); | |
401 | } | |
402 | else | |
403 | s = redirect_edge_and_branch (e, dest); | |
404 | ||
405 | if (s == e) | |
406 | { | |
407 | /* Create arguments for the phi nodes, since the edge was not | |
408 | here before. */ | |
726a989a RB |
409 | for (gsi = gsi_start_phis (dest); |
410 | !gsi_end_p (gsi); | |
411 | gsi_next (&gsi)) | |
412 | { | |
413 | gimple phi = gsi_stmt (gsi); | |
f5045c96 | 414 | source_location l = gimple_phi_arg_location_from_edge (phi, succ); |
9e227d60 | 415 | add_phi_arg (phi, gimple_phi_arg_def (phi, succ->dest_idx), s, l); |
726a989a | 416 | } |
c9784e6d | 417 | } |
c9784e6d KH |
418 | } |
419 | ||
1197e789 RG |
420 | /* Move nonlocal labels and computed goto targets as well as user |
421 | defined labels and labels with an EH landing pad number to the | |
422 | new block, so that the redirection of the abnormal edges works, | |
423 | jump targets end up in a sane place and debug information for | |
424 | labels is retained. */ | |
425 | gsi_to = gsi_start_bb (dest); | |
426 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); ) | |
427 | { | |
428 | tree decl; | |
429 | label = gsi_stmt (gsi); | |
430 | if (is_gimple_debug (label)) | |
431 | break; | |
432 | decl = gimple_label_label (label); | |
433 | if (EH_LANDING_PAD_NR (decl) != 0 | |
434 | || DECL_NONLOCAL (decl) | |
435 | || FORCED_LABEL (decl) | |
436 | || !DECL_ARTIFICIAL (decl)) | |
437 | { | |
438 | gsi_remove (&gsi, false); | |
439 | gsi_insert_before (&gsi_to, label, GSI_SAME_STMT); | |
440 | } | |
441 | else | |
442 | gsi_next (&gsi); | |
443 | } | |
444 | ||
dc764d10 | 445 | /* Move debug statements if the destination has a single predecessor. */ |
70235ab9 | 446 | if (can_move_debug_stmts) |
c9784e6d | 447 | { |
1197e789 RG |
448 | gsi_to = gsi_after_labels (dest); |
449 | for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); ) | |
c9784e6d | 450 | { |
70235ab9 JJ |
451 | gimple debug = gsi_stmt (gsi); |
452 | if (!is_gimple_debug (debug)) | |
1197e789 | 453 | break; |
726a989a | 454 | gsi_remove (&gsi, false); |
70235ab9 | 455 | gsi_insert_before (&gsi_to, debug, GSI_SAME_STMT); |
c9784e6d KH |
456 | } |
457 | } | |
458 | ||
672987e8 ZD |
459 | bitmap_set_bit (cfgcleanup_altered_bbs, dest->index); |
460 | ||
c9784e6d KH |
461 | /* Update the dominators. */ |
462 | if (dom_info_available_p (CDI_DOMINATORS)) | |
463 | { | |
464 | basic_block dom, dombb, domdest; | |
465 | ||
466 | dombb = get_immediate_dominator (CDI_DOMINATORS, bb); | |
467 | domdest = get_immediate_dominator (CDI_DOMINATORS, dest); | |
468 | if (domdest == bb) | |
469 | { | |
470 | /* Shortcut to avoid calling (relatively expensive) | |
471 | nearest_common_dominator unless necessary. */ | |
472 | dom = dombb; | |
473 | } | |
474 | else | |
475 | dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb); | |
476 | ||
477 | set_immediate_dominator (CDI_DOMINATORS, dest, dom); | |
478 | } | |
479 | ||
480 | /* And kill the forwarder block. */ | |
481 | delete_basic_block (bb); | |
482 | ||
483 | return true; | |
484 | } | |
485 | ||
566d09ef JH |
486 | /* STMT is a call that has been discovered noreturn. Fixup the CFG |
487 | and remove LHS. Return true if something changed. */ | |
488 | ||
489 | bool | |
490 | fixup_noreturn_call (gimple stmt) | |
491 | { | |
492 | basic_block bb = gimple_bb (stmt); | |
493 | bool changed = false; | |
494 | ||
495 | if (gimple_call_builtin_p (stmt, BUILT_IN_RETURN)) | |
496 | return false; | |
497 | ||
498 | /* First split basic block if stmt is not last. */ | |
499 | if (stmt != gsi_stmt (gsi_last_bb (bb))) | |
500 | split_block (bb, stmt); | |
501 | ||
502 | changed |= remove_fallthru_edge (bb->succs); | |
503 | ||
504 | /* If there is LHS, remove it. */ | |
505 | if (gimple_call_lhs (stmt)) | |
506 | { | |
507 | tree op = gimple_call_lhs (stmt); | |
508 | gimple_call_set_lhs (stmt, NULL_TREE); | |
02d635a2 | 509 | |
acb6411a | 510 | /* We need to remove SSA name to avoid checking errors. |
566d09ef | 511 | All uses are dominated by the noreturn and thus will |
02d635a2 JH |
512 | be removed afterwards. |
513 | We proactively remove affected non-PHI statements to avoid | |
514 | fixup_cfg from trying to update them and crashing. */ | |
566d09ef JH |
515 | if (TREE_CODE (op) == SSA_NAME) |
516 | { | |
517 | use_operand_p use_p; | |
518 | imm_use_iterator iter; | |
519 | gimple use_stmt; | |
02d635a2 JH |
520 | bitmap_iterator bi; |
521 | unsigned int bb_index; | |
522 | ||
523 | bitmap blocks = BITMAP_ALLOC (NULL); | |
566d09ef JH |
524 | |
525 | FOR_EACH_IMM_USE_STMT (use_stmt, iter, op) | |
02d635a2 JH |
526 | { |
527 | if (gimple_code (use_stmt) != GIMPLE_PHI) | |
528 | bitmap_set_bit (blocks, gimple_bb (use_stmt)->index); | |
529 | else | |
530 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
531 | SET_USE (use_p, error_mark_node); | |
532 | } | |
533 | EXECUTE_IF_SET_IN_BITMAP (blocks, 0, bb_index, bi) | |
534 | delete_basic_block (BASIC_BLOCK (bb_index)); | |
535 | BITMAP_FREE (blocks); | |
536 | release_ssa_name (op); | |
566d09ef JH |
537 | } |
538 | update_stmt (stmt); | |
539 | changed = true; | |
540 | } | |
8ba0479e JJ |
541 | /* Similarly remove VDEF if there is any. */ |
542 | else if (gimple_vdef (stmt)) | |
543 | update_stmt (stmt); | |
566d09ef JH |
544 | return changed; |
545 | } | |
546 | ||
547 | ||
672987e8 ZD |
548 | /* Split basic blocks on calls in the middle of a basic block that are now |
549 | known not to return, and remove the unreachable code. */ | |
c9784e6d KH |
550 | |
551 | static bool | |
672987e8 | 552 | split_bbs_on_noreturn_calls (void) |
c9784e6d | 553 | { |
c9784e6d | 554 | bool changed = false; |
726a989a | 555 | gimple stmt; |
672987e8 | 556 | basic_block bb; |
c9784e6d | 557 | |
672987e8 ZD |
558 | /* Detect cases where a mid-block call is now known not to return. */ |
559 | if (cfun->gimple_df) | |
9771b263 | 560 | while (vec_safe_length (MODIFIED_NORETURN_CALLS (cfun))) |
672987e8 | 561 | { |
9771b263 | 562 | stmt = MODIFIED_NORETURN_CALLS (cfun)->pop (); |
726a989a | 563 | bb = gimple_bb (stmt); |
4d731f17 JJ |
564 | /* BB might be deleted at this point, so verify first |
565 | BB is present in the cfg. */ | |
672987e8 | 566 | if (bb == NULL |
4d731f17 | 567 | || bb->index < NUM_FIXED_BLOCKS |
30f1e6de | 568 | || bb->index >= last_basic_block |
4d731f17 | 569 | || BASIC_BLOCK (bb->index) != bb |
726a989a | 570 | || !gimple_call_noreturn_p (stmt)) |
672987e8 ZD |
571 | continue; |
572 | ||
566d09ef | 573 | changed |= fixup_noreturn_call (stmt); |
672987e8 | 574 | } |
c9784e6d | 575 | |
c9784e6d KH |
576 | return changed; |
577 | } | |
578 | ||
672987e8 ZD |
579 | /* Tries to cleanup cfg in basic block BB. Returns true if anything |
580 | changes. */ | |
c9784e6d | 581 | |
89e80dd4 | 582 | static bool |
672987e8 | 583 | cleanup_tree_cfg_bb (basic_block bb) |
c9784e6d | 584 | { |
65e7bfe3 | 585 | bool retval = cleanup_control_flow_bb (bb); |
b8698a0f | 586 | |
f99fcb3b | 587 | if (tree_forwarder_block_p (bb, false) |
672987e8 ZD |
588 | && remove_forwarder_block (bb)) |
589 | return true; | |
c9784e6d | 590 | |
89e80dd4 DN |
591 | /* Merging the blocks may create new opportunities for folding |
592 | conditional branches (due to the elimination of single-valued PHI | |
593 | nodes). */ | |
672987e8 ZD |
594 | if (single_succ_p (bb) |
595 | && can_merge_blocks_p (bb, single_succ (bb))) | |
596 | { | |
597 | merge_blocks (bb, single_succ (bb)); | |
598 | return true; | |
599 | } | |
600 | ||
601 | return retval; | |
602 | } | |
603 | ||
604 | /* Iterate the cfg cleanups, while anything changes. */ | |
605 | ||
606 | static bool | |
607 | cleanup_tree_cfg_1 (void) | |
608 | { | |
609 | bool retval = false; | |
610 | basic_block bb; | |
611 | unsigned i, n; | |
612 | ||
613 | retval |= split_bbs_on_noreturn_calls (); | |
614 | ||
615 | /* Prepare the worklists of altered blocks. */ | |
616 | cfgcleanup_altered_bbs = BITMAP_ALLOC (NULL); | |
617 | ||
618 | /* During forwarder block cleanup, we may redirect edges out of | |
619 | SWITCH_EXPRs, which can get expensive. So we want to enable | |
620 | recording of edge to CASE_LABEL_EXPR. */ | |
621 | start_recording_case_labels (); | |
89e80dd4 | 622 | |
672987e8 ZD |
623 | /* Start by iterating over all basic blocks. We cannot use FOR_EACH_BB, |
624 | since the basic blocks may get removed. */ | |
625 | n = last_basic_block; | |
626 | for (i = NUM_FIXED_BLOCKS; i < n; i++) | |
627 | { | |
628 | bb = BASIC_BLOCK (i); | |
629 | if (bb) | |
630 | retval |= cleanup_tree_cfg_bb (bb); | |
631 | } | |
632 | ||
633 | /* Now process the altered blocks, as long as any are available. */ | |
634 | while (!bitmap_empty_p (cfgcleanup_altered_bbs)) | |
635 | { | |
636 | i = bitmap_first_set_bit (cfgcleanup_altered_bbs); | |
637 | bitmap_clear_bit (cfgcleanup_altered_bbs, i); | |
638 | if (i < NUM_FIXED_BLOCKS) | |
639 | continue; | |
640 | ||
641 | bb = BASIC_BLOCK (i); | |
642 | if (!bb) | |
643 | continue; | |
644 | ||
645 | retval |= cleanup_tree_cfg_bb (bb); | |
646 | ||
647 | /* Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn | |
648 | calls. */ | |
649 | retval |= split_bbs_on_noreturn_calls (); | |
650 | } | |
b8698a0f | 651 | |
672987e8 ZD |
652 | end_recording_case_labels (); |
653 | BITMAP_FREE (cfgcleanup_altered_bbs); | |
89e80dd4 DN |
654 | return retval; |
655 | } | |
656 | ||
657 | ||
e3594cb3 DN |
658 | /* Remove unreachable blocks and other miscellaneous clean up work. |
659 | Return true if the flowgraph was modified, false otherwise. */ | |
89e80dd4 | 660 | |
592c303d ZD |
661 | static bool |
662 | cleanup_tree_cfg_noloop (void) | |
89e80dd4 | 663 | { |
672987e8 | 664 | bool changed; |
89e80dd4 DN |
665 | |
666 | timevar_push (TV_TREE_CLEANUP_CFG); | |
667 | ||
e3594cb3 | 668 | /* Iterate until there are no more cleanups left to do. If any |
672987e8 ZD |
669 | iteration changed the flowgraph, set CHANGED to true. |
670 | ||
671 | If dominance information is available, there cannot be any unreachable | |
672 | blocks. */ | |
2b28c07a | 673 | if (!dom_info_available_p (CDI_DOMINATORS)) |
e3594cb3 | 674 | { |
672987e8 ZD |
675 | changed = delete_unreachable_blocks (); |
676 | calculate_dominance_info (CDI_DOMINATORS); | |
e3594cb3 | 677 | } |
672987e8 | 678 | else |
30251f7a ZD |
679 | { |
680 | #ifdef ENABLE_CHECKING | |
681 | verify_dominators (CDI_DOMINATORS); | |
682 | #endif | |
683 | changed = false; | |
684 | } | |
c9784e6d | 685 | |
672987e8 ZD |
686 | changed |= cleanup_tree_cfg_1 (); |
687 | ||
2b28c07a | 688 | gcc_assert (dom_info_available_p (CDI_DOMINATORS)); |
c9784e6d KH |
689 | compact_blocks (); |
690 | ||
691 | #ifdef ENABLE_CHECKING | |
692 | verify_flow_info (); | |
693 | #endif | |
89e80dd4 | 694 | |
c9784e6d | 695 | timevar_pop (TV_TREE_CLEANUP_CFG); |
89e80dd4 | 696 | |
592c303d | 697 | if (changed && current_loops) |
f87000d0 | 698 | loops_state_set (LOOPS_NEED_FIXUP); |
592c303d | 699 | |
e3594cb3 | 700 | return changed; |
c9784e6d KH |
701 | } |
702 | ||
592c303d | 703 | /* Repairs loop structures. */ |
c9784e6d | 704 | |
592c303d ZD |
705 | static void |
706 | repair_loop_structures (void) | |
c9784e6d | 707 | { |
a222c01a MM |
708 | bitmap changed_bbs; |
709 | ||
cc360b36 SB |
710 | calculate_dominance_info (CDI_DOMINATORS); |
711 | ||
a222c01a MM |
712 | timevar_push (TV_REPAIR_LOOPS); |
713 | changed_bbs = BITMAP_ALLOC (NULL); | |
592c303d | 714 | fix_loop_structure (changed_bbs); |
c9784e6d | 715 | |
592c303d ZD |
716 | /* This usually does nothing. But sometimes parts of cfg that originally |
717 | were inside a loop get out of it due to edge removal (since they | |
718 | become unreachable by back edges from latch). */ | |
f87000d0 | 719 | if (loops_state_satisfies_p (LOOP_CLOSED_SSA)) |
592c303d | 720 | rewrite_into_loop_closed_ssa (changed_bbs, TODO_update_ssa); |
c9784e6d | 721 | |
592c303d | 722 | BITMAP_FREE (changed_bbs); |
c9784e6d KH |
723 | |
724 | #ifdef ENABLE_CHECKING | |
592c303d | 725 | verify_loop_structure (); |
c9784e6d | 726 | #endif |
592c303d ZD |
727 | scev_reset (); |
728 | ||
a222c01a | 729 | timevar_pop (TV_REPAIR_LOOPS); |
592c303d ZD |
730 | } |
731 | ||
732 | /* Cleanup cfg and repair loop structures. */ | |
733 | ||
734 | bool | |
735 | cleanup_tree_cfg (void) | |
736 | { | |
737 | bool changed = cleanup_tree_cfg_noloop (); | |
738 | ||
739 | if (current_loops != NULL | |
f87000d0 | 740 | && loops_state_satisfies_p (LOOPS_NEED_FIXUP)) |
592c303d ZD |
741 | repair_loop_structures (); |
742 | ||
1994bfea | 743 | return changed; |
c9784e6d KH |
744 | } |
745 | ||
746 | /* Merge the PHI nodes at BB into those at BB's sole successor. */ | |
747 | ||
748 | static void | |
749 | remove_forwarder_block_with_phi (basic_block bb) | |
750 | { | |
751 | edge succ = single_succ_edge (bb); | |
752 | basic_block dest = succ->dest; | |
726a989a | 753 | gimple label; |
c9784e6d KH |
754 | basic_block dombb, domdest, dom; |
755 | ||
756 | /* We check for infinite loops already in tree_forwarder_block_p. | |
757 | However it may happen that the infinite loop is created | |
758 | afterwards due to removal of forwarders. */ | |
759 | if (dest == bb) | |
760 | return; | |
761 | ||
762 | /* If the destination block consists of a nonlocal label, do not | |
763 | merge it. */ | |
764 | label = first_stmt (dest); | |
765 | if (label | |
726a989a RB |
766 | && gimple_code (label) == GIMPLE_LABEL |
767 | && DECL_NONLOCAL (gimple_label_label (label))) | |
c9784e6d KH |
768 | return; |
769 | ||
770 | /* Redirect each incoming edge to BB to DEST. */ | |
771 | while (EDGE_COUNT (bb->preds) > 0) | |
772 | { | |
773 | edge e = EDGE_PRED (bb, 0), s; | |
726a989a | 774 | gimple_stmt_iterator gsi; |
c9784e6d KH |
775 | |
776 | s = find_edge (e->src, dest); | |
777 | if (s) | |
778 | { | |
779 | /* We already have an edge S from E->src to DEST. If S and | |
780 | E->dest's sole successor edge have the same PHI arguments | |
781 | at DEST, redirect S to DEST. */ | |
782 | if (phi_alternatives_equal (dest, s, succ)) | |
783 | { | |
784 | e = redirect_edge_and_branch (e, dest); | |
ea7e6d5a | 785 | redirect_edge_var_map_clear (e); |
c9784e6d KH |
786 | continue; |
787 | } | |
788 | ||
789 | /* PHI arguments are different. Create a forwarder block by | |
790 | splitting E so that we can merge PHI arguments on E to | |
791 | DEST. */ | |
792 | e = single_succ_edge (split_edge (e)); | |
793 | } | |
794 | ||
795 | s = redirect_edge_and_branch (e, dest); | |
796 | ||
797 | /* redirect_edge_and_branch must not create a new edge. */ | |
798 | gcc_assert (s == e); | |
799 | ||
800 | /* Add to the PHI nodes at DEST each PHI argument removed at the | |
801 | destination of E. */ | |
726a989a RB |
802 | for (gsi = gsi_start_phis (dest); |
803 | !gsi_end_p (gsi); | |
804 | gsi_next (&gsi)) | |
c9784e6d | 805 | { |
726a989a RB |
806 | gimple phi = gsi_stmt (gsi); |
807 | tree def = gimple_phi_arg_def (phi, succ->dest_idx); | |
f5045c96 | 808 | source_location locus = gimple_phi_arg_location_from_edge (phi, succ); |
c9784e6d KH |
809 | |
810 | if (TREE_CODE (def) == SSA_NAME) | |
811 | { | |
9771b263 | 812 | edge_var_map_vector *head; |
ea7e6d5a AH |
813 | edge_var_map *vm; |
814 | size_t i; | |
c9784e6d KH |
815 | |
816 | /* If DEF is one of the results of PHI nodes removed during | |
817 | redirection, replace it with the PHI argument that used | |
818 | to be on E. */ | |
ea7e6d5a | 819 | head = redirect_edge_var_map_vector (e); |
9771b263 | 820 | FOR_EACH_VEC_ELT (*head, i, vm) |
c9784e6d | 821 | { |
ea7e6d5a AH |
822 | tree old_arg = redirect_edge_var_map_result (vm); |
823 | tree new_arg = redirect_edge_var_map_def (vm); | |
c9784e6d KH |
824 | |
825 | if (def == old_arg) | |
826 | { | |
827 | def = new_arg; | |
f5045c96 | 828 | locus = redirect_edge_var_map_location (vm); |
c9784e6d KH |
829 | break; |
830 | } | |
831 | } | |
832 | } | |
833 | ||
9e227d60 | 834 | add_phi_arg (phi, def, s, locus); |
c9784e6d KH |
835 | } |
836 | ||
ea7e6d5a | 837 | redirect_edge_var_map_clear (e); |
c9784e6d KH |
838 | } |
839 | ||
840 | /* Update the dominators. */ | |
841 | dombb = get_immediate_dominator (CDI_DOMINATORS, bb); | |
842 | domdest = get_immediate_dominator (CDI_DOMINATORS, dest); | |
843 | if (domdest == bb) | |
844 | { | |
845 | /* Shortcut to avoid calling (relatively expensive) | |
846 | nearest_common_dominator unless necessary. */ | |
847 | dom = dombb; | |
848 | } | |
849 | else | |
850 | dom = nearest_common_dominator (CDI_DOMINATORS, domdest, dombb); | |
851 | ||
852 | set_immediate_dominator (CDI_DOMINATORS, dest, dom); | |
853 | ||
854 | /* Remove BB since all of BB's incoming edges have been redirected | |
855 | to DEST. */ | |
856 | delete_basic_block (bb); | |
857 | } | |
858 | ||
859 | /* This pass merges PHI nodes if one feeds into another. For example, | |
860 | suppose we have the following: | |
861 | ||
862 | goto <bb 9> (<L9>); | |
863 | ||
864 | <L8>:; | |
865 | tem_17 = foo (); | |
866 | ||
867 | # tem_6 = PHI <tem_17(8), tem_23(7)>; | |
868 | <L9>:; | |
869 | ||
870 | # tem_3 = PHI <tem_6(9), tem_2(5)>; | |
871 | <L10>:; | |
872 | ||
873 | Then we merge the first PHI node into the second one like so: | |
874 | ||
875 | goto <bb 9> (<L10>); | |
876 | ||
877 | <L8>:; | |
878 | tem_17 = foo (); | |
879 | ||
880 | # tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>; | |
881 | <L10>:; | |
882 | */ | |
883 | ||
c2924966 | 884 | static unsigned int |
c9784e6d KH |
885 | merge_phi_nodes (void) |
886 | { | |
5ed6ace5 | 887 | basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks); |
c9784e6d KH |
888 | basic_block *current = worklist; |
889 | basic_block bb; | |
890 | ||
891 | calculate_dominance_info (CDI_DOMINATORS); | |
892 | ||
893 | /* Find all PHI nodes that we may be able to merge. */ | |
894 | FOR_EACH_BB (bb) | |
895 | { | |
896 | basic_block dest; | |
897 | ||
898 | /* Look for a forwarder block with PHI nodes. */ | |
899 | if (!tree_forwarder_block_p (bb, true)) | |
900 | continue; | |
901 | ||
902 | dest = single_succ (bb); | |
903 | ||
904 | /* We have to feed into another basic block with PHI | |
905 | nodes. */ | |
8eacd016 | 906 | if (gimple_seq_empty_p (phi_nodes (dest)) |
c9784e6d KH |
907 | /* We don't want to deal with a basic block with |
908 | abnormal edges. */ | |
31ff2426 | 909 | || bb_has_abnormal_pred (bb)) |
c9784e6d KH |
910 | continue; |
911 | ||
912 | if (!dominated_by_p (CDI_DOMINATORS, dest, bb)) | |
913 | { | |
914 | /* If BB does not dominate DEST, then the PHI nodes at | |
915 | DEST must be the only users of the results of the PHI | |
916 | nodes at BB. */ | |
917 | *current++ = bb; | |
918 | } | |
ea65cd37 JL |
919 | else |
920 | { | |
726a989a | 921 | gimple_stmt_iterator gsi; |
338b5886 | 922 | unsigned int dest_idx = single_succ_edge (bb)->dest_idx; |
ea65cd37 JL |
923 | |
924 | /* BB dominates DEST. There may be many users of the PHI | |
925 | nodes in BB. However, there is still a trivial case we | |
926 | can handle. If the result of every PHI in BB is used | |
927 | only by a PHI in DEST, then we can trivially merge the | |
928 | PHI nodes from BB into DEST. */ | |
726a989a RB |
929 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
930 | gsi_next (&gsi)) | |
ea65cd37 | 931 | { |
726a989a RB |
932 | gimple phi = gsi_stmt (gsi); |
933 | tree result = gimple_phi_result (phi); | |
ea65cd37 | 934 | use_operand_p imm_use; |
726a989a | 935 | gimple use_stmt; |
ea65cd37 JL |
936 | |
937 | /* If the PHI's result is never used, then we can just | |
938 | ignore it. */ | |
bfc646bf | 939 | if (has_zero_uses (result)) |
ea65cd37 JL |
940 | continue; |
941 | ||
942 | /* Get the single use of the result of this PHI node. */ | |
943 | if (!single_imm_use (result, &imm_use, &use_stmt) | |
726a989a RB |
944 | || gimple_code (use_stmt) != GIMPLE_PHI |
945 | || gimple_bb (use_stmt) != dest | |
946 | || gimple_phi_arg_def (use_stmt, dest_idx) != result) | |
ea65cd37 JL |
947 | break; |
948 | } | |
949 | ||
c0220ea4 | 950 | /* If the loop above iterated through all the PHI nodes |
ea65cd37 | 951 | in BB, then we can merge the PHIs from BB into DEST. */ |
726a989a | 952 | if (gsi_end_p (gsi)) |
ea65cd37 JL |
953 | *current++ = bb; |
954 | } | |
c9784e6d KH |
955 | } |
956 | ||
957 | /* Now let's drain WORKLIST. */ | |
958 | while (current != worklist) | |
959 | { | |
960 | bb = *--current; | |
961 | remove_forwarder_block_with_phi (bb); | |
962 | } | |
963 | ||
964 | free (worklist); | |
c2924966 | 965 | return 0; |
c9784e6d KH |
966 | } |
967 | ||
968 | static bool | |
969 | gate_merge_phi (void) | |
970 | { | |
971 | return 1; | |
972 | } | |
973 | ||
b8698a0f | 974 | struct gimple_opt_pass pass_merge_phi = |
8ddbbcae JH |
975 | { |
976 | { | |
977 | GIMPLE_PASS, | |
c9784e6d | 978 | "mergephi", /* name */ |
2b4e6bf1 | 979 | OPTGROUP_NONE, /* optinfo_flags */ |
c9784e6d KH |
980 | gate_merge_phi, /* gate */ |
981 | merge_phi_nodes, /* execute */ | |
982 | NULL, /* sub */ | |
983 | NULL, /* next */ | |
984 | 0, /* static_pass_number */ | |
985 | TV_TREE_MERGE_PHI, /* tv_id */ | |
986 | PROP_cfg | PROP_ssa, /* properties_required */ | |
987 | 0, /* properties_provided */ | |
988 | 0, /* properties_destroyed */ | |
989 | 0, /* todo_flags_start */ | |
22c5fa5f | 990 | TODO_ggc_collect /* todo_flags_finish */ |
8ddbbcae JH |
991 | | TODO_verify_ssa |
992 | } | |
c9784e6d | 993 | }; |