]>
Commit | Line | Data |
---|---|---|
ca6c03ca JH |
1 | /* Control flow graph manipulation code for GNU compiler. |
2 | Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, | |
7072a650 | 3 | 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
6fb5fa3c | 4 | Free Software Foundation, Inc. |
ca6c03ca JH |
5 | |
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 10 | Software Foundation; either version 3, or (at your option) any later |
ca6c03ca JH |
11 | version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
ca6c03ca | 21 | |
5f0d2358 RK |
22 | /* This file contains low level functions to manipulate the CFG and analyze it |
23 | that are aware of the RTL intermediate language. | |
ca6c03ca JH |
24 | |
25 | Available functionality: | |
bc35512f | 26 | - Basic CFG/RTL manipulation API documented in cfghooks.h |
5f0d2358 | 27 | - CFG-aware instruction chain manipulation |
ca6c03ca | 28 | delete_insn, delete_insn_chain |
bc35512f JH |
29 | - Edge splitting and committing to edges |
30 | insert_insn_on_edge, commit_edge_insertions | |
31 | - CFG updating after insn simplification | |
32 | purge_dead_edges, purge_all_dead_edges | |
33 | ||
34 | Functions not supposed for generic use: | |
5f0d2358 | 35 | - Infrastructure to determine quickly basic block for insn |
ca6c03ca | 36 | compute_bb_for_insn, update_bb_for_insn, set_block_for_insn, |
5f0d2358 | 37 | - Edge redirection with updating and optimizing of insn chain |
bc35512f | 38 | block_label, tidy_fallthru_edge, force_nonfallthru */ |
ca6c03ca JH |
39 | \f |
40 | #include "config.h" | |
41 | #include "system.h" | |
4977bab6 ZW |
42 | #include "coretypes.h" |
43 | #include "tm.h" | |
ca6c03ca JH |
44 | #include "tree.h" |
45 | #include "rtl.h" | |
46 | #include "hard-reg-set.h" | |
47 | #include "basic-block.h" | |
48 | #include "regs.h" | |
49 | #include "flags.h" | |
50 | #include "output.h" | |
51 | #include "function.h" | |
52 | #include "except.h" | |
53 | #include "toplev.h" | |
54 | #include "tm_p.h" | |
55 | #include "obstack.h" | |
2a34bece | 56 | #include "insn-attr.h" |
0a2ed1f1 | 57 | #include "insn-config.h" |
9ee634e3 | 58 | #include "cfglayout.h" |
ff25ef99 | 59 | #include "expr.h" |
9fb32434 | 60 | #include "target.h" |
1cb7dfc3 | 61 | #include "cfgloop.h" |
5e2d947c | 62 | #include "ggc.h" |
ef330312 | 63 | #include "tree-pass.h" |
6fb5fa3c | 64 | #include "df.h" |
ca6c03ca | 65 | |
9678086d KG |
66 | static int can_delete_note_p (const_rtx); |
67 | static int can_delete_label_p (const_rtx); | |
d329e058 | 68 | static basic_block rtl_split_edge (edge); |
f470c378 | 69 | static bool rtl_move_block_after (basic_block, basic_block); |
d329e058 | 70 | static int rtl_verify_flow_info (void); |
f470c378 | 71 | static basic_block cfg_layout_split_block (basic_block, void *); |
6de9cd9a | 72 | static edge cfg_layout_redirect_edge_and_branch (edge, basic_block); |
d329e058 AJ |
73 | static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block); |
74 | static void cfg_layout_delete_block (basic_block); | |
75 | static void rtl_delete_block (basic_block); | |
76 | static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block); | |
6de9cd9a | 77 | static edge rtl_redirect_edge_and_branch (edge, basic_block); |
f470c378 | 78 | static basic_block rtl_split_block (basic_block, void *); |
726a989a | 79 | static void rtl_dump_bb (basic_block, FILE *, int, int); |
d329e058 | 80 | static int rtl_verify_flow_info_1 (void); |
f470c378 | 81 | static void rtl_make_forwarder_block (edge); |
ca6c03ca JH |
82 | \f |
83 | /* Return true if NOTE is not one of the ones that must be kept paired, | |
5f0d2358 | 84 | so that we may simply delete it. */ |
ca6c03ca JH |
85 | |
86 | static int | |
9678086d | 87 | can_delete_note_p (const_rtx note) |
ca6c03ca | 88 | { |
cd9c1ca8 RH |
89 | switch (NOTE_KIND (note)) |
90 | { | |
91 | case NOTE_INSN_DELETED: | |
92 | case NOTE_INSN_BASIC_BLOCK: | |
93 | case NOTE_INSN_EPILOGUE_BEG: | |
94 | return true; | |
95 | ||
96 | default: | |
97 | return false; | |
98 | } | |
ca6c03ca JH |
99 | } |
100 | ||
101 | /* True if a given label can be deleted. */ | |
102 | ||
103 | static int | |
9678086d | 104 | can_delete_label_p (const_rtx label) |
ca6c03ca | 105 | { |
5f0d2358 RK |
106 | return (!LABEL_PRESERVE_P (label) |
107 | /* User declared labels must be preserved. */ | |
108 | && LABEL_NAME (label) == 0 | |
610d2478 | 109 | && !in_expr_list_p (forced_labels, label)); |
ca6c03ca JH |
110 | } |
111 | ||
112 | /* Delete INSN by patching it out. Return the next insn. */ | |
113 | ||
114 | rtx | |
d329e058 | 115 | delete_insn (rtx insn) |
ca6c03ca JH |
116 | { |
117 | rtx next = NEXT_INSN (insn); | |
118 | rtx note; | |
119 | bool really_delete = true; | |
120 | ||
4b4bf941 | 121 | if (LABEL_P (insn)) |
ca6c03ca JH |
122 | { |
123 | /* Some labels can't be directly removed from the INSN chain, as they | |
c22cacf3 MS |
124 | might be references via variables, constant pool etc. |
125 | Convert them to the special NOTE_INSN_DELETED_LABEL note. */ | |
ca6c03ca JH |
126 | if (! can_delete_label_p (insn)) |
127 | { | |
128 | const char *name = LABEL_NAME (insn); | |
129 | ||
130 | really_delete = false; | |
131 | PUT_CODE (insn, NOTE); | |
a38e7aa5 | 132 | NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL; |
6773e15f | 133 | NOTE_DELETED_LABEL_NAME (insn) = name; |
ca6c03ca | 134 | } |
5f0d2358 | 135 | |
ca6c03ca JH |
136 | remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels); |
137 | } | |
138 | ||
139 | if (really_delete) | |
140 | { | |
cda94cbb | 141 | /* If this insn has already been deleted, something is very wrong. */ |
341c100f | 142 | gcc_assert (!INSN_DELETED_P (insn)); |
ca6c03ca JH |
143 | remove_insn (insn); |
144 | INSN_DELETED_P (insn) = 1; | |
145 | } | |
146 | ||
147 | /* If deleting a jump, decrement the use count of the label. Deleting | |
148 | the label itself should happen in the normal course of block merging. */ | |
cf7c4aa6 | 149 | if (JUMP_P (insn)) |
9295a326 | 150 | { |
cf7c4aa6 HPN |
151 | if (JUMP_LABEL (insn) |
152 | && LABEL_P (JUMP_LABEL (insn))) | |
153 | LABEL_NUSES (JUMP_LABEL (insn))--; | |
154 | ||
155 | /* If there are more targets, remove them too. */ | |
156 | while ((note | |
157 | = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX | |
4b4bf941 | 158 | && LABEL_P (XEXP (note, 0))) |
9295a326 JZ |
159 | { |
160 | LABEL_NUSES (XEXP (note, 0))--; | |
161 | remove_note (insn, note); | |
162 | } | |
163 | } | |
ca6c03ca | 164 | |
cf7c4aa6 HPN |
165 | /* Also if deleting any insn that references a label as an operand. */ |
166 | while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX | |
167 | && LABEL_P (XEXP (note, 0))) | |
168 | { | |
169 | LABEL_NUSES (XEXP (note, 0))--; | |
170 | remove_note (insn, note); | |
171 | } | |
172 | ||
481683e1 | 173 | if (JUMP_TABLE_DATA_P (insn)) |
ca6c03ca JH |
174 | { |
175 | rtx pat = PATTERN (insn); | |
176 | int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC; | |
177 | int len = XVECLEN (pat, diff_vec_p); | |
178 | int i; | |
179 | ||
180 | for (i = 0; i < len; i++) | |
a124fcda RH |
181 | { |
182 | rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0); | |
183 | ||
184 | /* When deleting code in bulk (e.g. removing many unreachable | |
185 | blocks) we can delete a label that's a target of the vector | |
186 | before deleting the vector itself. */ | |
4b4bf941 | 187 | if (!NOTE_P (label)) |
a124fcda RH |
188 | LABEL_NUSES (label)--; |
189 | } | |
ca6c03ca JH |
190 | } |
191 | ||
192 | return next; | |
193 | } | |
194 | ||
3dec4024 | 195 | /* Like delete_insn but also purge dead edges from BB. */ |
9ed7b221 | 196 | |
3dec4024 | 197 | rtx |
d329e058 | 198 | delete_insn_and_edges (rtx insn) |
3dec4024 JH |
199 | { |
200 | rtx x; | |
201 | bool purge = false; | |
202 | ||
ba4f7968 | 203 | if (INSN_P (insn) |
3dec4024 | 204 | && BLOCK_FOR_INSN (insn) |
a813c111 | 205 | && BB_END (BLOCK_FOR_INSN (insn)) == insn) |
3dec4024 JH |
206 | purge = true; |
207 | x = delete_insn (insn); | |
208 | if (purge) | |
209 | purge_dead_edges (BLOCK_FOR_INSN (insn)); | |
210 | return x; | |
211 | } | |
212 | ||
ca6c03ca | 213 | /* Unlink a chain of insns between START and FINISH, leaving notes |
a7b87f73 ZD |
214 | that must be paired. If CLEAR_BB is true, we set bb field for |
215 | insns that cannot be removed to NULL. */ | |
ca6c03ca JH |
216 | |
217 | void | |
a7b87f73 | 218 | delete_insn_chain (rtx start, rtx finish, bool clear_bb) |
ca6c03ca | 219 | { |
ca6c03ca JH |
220 | rtx next; |
221 | ||
5f0d2358 RK |
222 | /* Unchain the insns one by one. It would be quicker to delete all of these |
223 | with a single unchaining, rather than one at a time, but we need to keep | |
224 | the NOTE's. */ | |
ca6c03ca JH |
225 | while (1) |
226 | { | |
227 | next = NEXT_INSN (start); | |
4b4bf941 | 228 | if (NOTE_P (start) && !can_delete_note_p (start)) |
ca6c03ca JH |
229 | ; |
230 | else | |
231 | next = delete_insn (start); | |
232 | ||
a7b87f73 ZD |
233 | if (clear_bb && !INSN_DELETED_P (start)) |
234 | set_block_for_insn (start, NULL); | |
235 | ||
ca6c03ca JH |
236 | if (start == finish) |
237 | break; | |
238 | start = next; | |
239 | } | |
240 | } | |
241 | \f | |
5f0d2358 RK |
242 | /* Create a new basic block consisting of the instructions between HEAD and END |
243 | inclusive. This function is designed to allow fast BB construction - reuses | |
244 | the note and basic block struct in BB_NOTE, if any and do not grow | |
245 | BASIC_BLOCK chain and should be used directly only by CFG construction code. | |
246 | END can be NULL in to create new empty basic block before HEAD. Both END | |
918ed612 ZD |
247 | and HEAD can be NULL to create basic block at the end of INSN chain. |
248 | AFTER is the basic block we should be put after. */ | |
ca6c03ca JH |
249 | |
250 | basic_block | |
d329e058 | 251 | create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after) |
ca6c03ca JH |
252 | { |
253 | basic_block bb; | |
254 | ||
255 | if (bb_note | |
ca6c03ca JH |
256 | && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL |
257 | && bb->aux == NULL) | |
258 | { | |
259 | /* If we found an existing note, thread it back onto the chain. */ | |
260 | ||
261 | rtx after; | |
262 | ||
4b4bf941 | 263 | if (LABEL_P (head)) |
ca6c03ca JH |
264 | after = head; |
265 | else | |
266 | { | |
267 | after = PREV_INSN (head); | |
268 | head = bb_note; | |
269 | } | |
270 | ||
271 | if (after != bb_note && NEXT_INSN (after) != bb_note) | |
ba4f7968 | 272 | reorder_insns_nobb (bb_note, bb_note, after); |
ca6c03ca JH |
273 | } |
274 | else | |
275 | { | |
276 | /* Otherwise we must create a note and a basic block structure. */ | |
277 | ||
278 | bb = alloc_block (); | |
279 | ||
5e2d947c | 280 | init_rtl_bb_info (bb); |
ca6c03ca | 281 | if (!head && !end) |
5f0d2358 RK |
282 | head = end = bb_note |
283 | = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ()); | |
4b4bf941 | 284 | else if (LABEL_P (head) && end) |
ca6c03ca JH |
285 | { |
286 | bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head); | |
287 | if (head == end) | |
288 | end = bb_note; | |
289 | } | |
290 | else | |
291 | { | |
292 | bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head); | |
293 | head = bb_note; | |
294 | if (!end) | |
295 | end = head; | |
296 | } | |
5f0d2358 | 297 | |
ca6c03ca JH |
298 | NOTE_BASIC_BLOCK (bb_note) = bb; |
299 | } | |
300 | ||
301 | /* Always include the bb note in the block. */ | |
302 | if (NEXT_INSN (end) == bb_note) | |
303 | end = bb_note; | |
304 | ||
a813c111 SB |
305 | BB_HEAD (bb) = head; |
306 | BB_END (bb) = end; | |
852c6ec7 | 307 | bb->index = last_basic_block++; |
5e2d947c | 308 | bb->flags = BB_NEW | BB_RTL; |
918ed612 | 309 | link_block (bb, after); |
68f9b844 | 310 | SET_BASIC_BLOCK (bb->index, bb); |
6fb5fa3c | 311 | df_bb_refs_record (bb->index, false); |
ba4f7968 | 312 | update_bb_for_insn (bb); |
076c7ab8 | 313 | BB_SET_PARTITION (bb, BB_UNPARTITIONED); |
ca6c03ca JH |
314 | |
315 | /* Tag the block so that we know it has been used when considering | |
316 | other basic block notes. */ | |
317 | bb->aux = bb; | |
318 | ||
319 | return bb; | |
320 | } | |
321 | ||
5f0d2358 | 322 | /* Create new basic block consisting of instructions in between HEAD and END |
918ed612 | 323 | and place it to the BB chain after block AFTER. END can be NULL in to |
5f0d2358 RK |
324 | create new empty basic block before HEAD. Both END and HEAD can be NULL to |
325 | create basic block at the end of INSN chain. */ | |
ca6c03ca | 326 | |
bc35512f JH |
327 | static basic_block |
328 | rtl_create_basic_block (void *headp, void *endp, basic_block after) | |
ca6c03ca | 329 | { |
ae50c0cb | 330 | rtx head = (rtx) headp, end = (rtx) endp; |
ca6c03ca | 331 | basic_block bb; |
0b17ab2f | 332 | |
7eca0767 | 333 | /* Grow the basic block array if needed. */ |
68f9b844 | 334 | if ((size_t) last_basic_block >= VEC_length (basic_block, basic_block_info)) |
7eca0767 JH |
335 | { |
336 | size_t new_size = last_basic_block + (last_basic_block + 3) / 4; | |
a590ac65 | 337 | VEC_safe_grow_cleared (basic_block, gc, basic_block_info, new_size); |
7eca0767 | 338 | } |
0b17ab2f | 339 | |
bf77398c | 340 | n_basic_blocks++; |
ca6c03ca | 341 | |
852c6ec7 | 342 | bb = create_basic_block_structure (head, end, NULL, after); |
ca6c03ca JH |
343 | bb->aux = NULL; |
344 | return bb; | |
345 | } | |
bc35512f JH |
346 | |
347 | static basic_block | |
348 | cfg_layout_create_basic_block (void *head, void *end, basic_block after) | |
349 | { | |
350 | basic_block newbb = rtl_create_basic_block (head, end, after); | |
351 | ||
bc35512f JH |
352 | return newbb; |
353 | } | |
ca6c03ca JH |
354 | \f |
355 | /* Delete the insns in a (non-live) block. We physically delete every | |
356 | non-deleted-note insn, and update the flow graph appropriately. | |
357 | ||
358 | Return nonzero if we deleted an exception handler. */ | |
359 | ||
360 | /* ??? Preserving all such notes strikes me as wrong. It would be nice | |
361 | to post-process the stream to remove empty blocks, loops, ranges, etc. */ | |
362 | ||
f0fda11c | 363 | static void |
d329e058 | 364 | rtl_delete_block (basic_block b) |
ca6c03ca | 365 | { |
96370780 | 366 | rtx insn, end; |
ca6c03ca JH |
367 | |
368 | /* If the head of this block is a CODE_LABEL, then it might be the | |
f39e46ba SB |
369 | label for an exception handler which can't be reached. We need |
370 | to remove the label from the exception_handler_label list. */ | |
a813c111 | 371 | insn = BB_HEAD (b); |
ca6c03ca | 372 | |
96370780 | 373 | end = get_last_bb_insn (b); |
ca6c03ca JH |
374 | |
375 | /* Selectively delete the entire chain. */ | |
a813c111 | 376 | BB_HEAD (b) = NULL; |
a7b87f73 ZD |
377 | delete_insn_chain (insn, end, true); |
378 | ||
6fb5fa3c DB |
379 | |
380 | if (dump_file) | |
381 | fprintf (dump_file, "deleting block %d\n", b->index); | |
382 | df_bb_delete (b->index); | |
ca6c03ca JH |
383 | } |
384 | \f | |
852c6ec7 | 385 | /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */ |
ca6c03ca JH |
386 | |
387 | void | |
d329e058 | 388 | compute_bb_for_insn (void) |
ca6c03ca | 389 | { |
e0082a72 | 390 | basic_block bb; |
ca6c03ca | 391 | |
e0082a72 | 392 | FOR_EACH_BB (bb) |
ca6c03ca | 393 | { |
a813c111 | 394 | rtx end = BB_END (bb); |
5f0d2358 | 395 | rtx insn; |
ca6c03ca | 396 | |
a813c111 | 397 | for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn)) |
ca6c03ca | 398 | { |
ba4f7968 | 399 | BLOCK_FOR_INSN (insn) = bb; |
ca6c03ca JH |
400 | if (insn == end) |
401 | break; | |
ca6c03ca JH |
402 | } |
403 | } | |
404 | } | |
405 | ||
406 | /* Release the basic_block_for_insn array. */ | |
407 | ||
c2924966 | 408 | unsigned int |
d329e058 | 409 | free_bb_for_insn (void) |
ca6c03ca | 410 | { |
ba4f7968 JH |
411 | rtx insn; |
412 | for (insn = get_insns (); insn; insn = NEXT_INSN (insn)) | |
4b4bf941 | 413 | if (!BARRIER_P (insn)) |
ba4f7968 | 414 | BLOCK_FOR_INSN (insn) = NULL; |
c2924966 | 415 | return 0; |
ca6c03ca JH |
416 | } |
417 | ||
2a34bece EB |
418 | static unsigned int |
419 | rest_of_pass_free_cfg (void) | |
420 | { | |
421 | #ifdef DELAY_SLOTS | |
422 | /* The resource.c machinery uses DF but the CFG isn't guaranteed to be | |
423 | valid at that point so it would be too late to call df_analyze. */ | |
424 | if (optimize > 0 && flag_delayed_branch) | |
425 | df_analyze (); | |
426 | #endif | |
427 | ||
428 | free_bb_for_insn (); | |
429 | return 0; | |
430 | } | |
431 | ||
8ddbbcae | 432 | struct rtl_opt_pass pass_free_cfg = |
ef330312 | 433 | { |
8ddbbcae JH |
434 | { |
435 | RTL_PASS, | |
ef330312 PB |
436 | NULL, /* name */ |
437 | NULL, /* gate */ | |
2a34bece | 438 | rest_of_pass_free_cfg, /* execute */ |
ef330312 PB |
439 | NULL, /* sub */ |
440 | NULL, /* next */ | |
441 | 0, /* static_pass_number */ | |
7072a650 | 442 | TV_NONE, /* tv_id */ |
ef330312 PB |
443 | 0, /* properties_required */ |
444 | 0, /* properties_provided */ | |
445 | PROP_cfg, /* properties_destroyed */ | |
446 | 0, /* todo_flags_start */ | |
447 | 0, /* todo_flags_finish */ | |
8ddbbcae | 448 | } |
ef330312 PB |
449 | }; |
450 | ||
91278841 AP |
451 | /* Return RTX to emit after when we want to emit code on the entry of function. */ |
452 | rtx | |
453 | entry_of_function (void) | |
454 | { | |
c22cacf3 | 455 | return (n_basic_blocks > NUM_FIXED_BLOCKS ? |
24bd1a0b | 456 | BB_HEAD (ENTRY_BLOCK_PTR->next_bb) : get_insns ()); |
91278841 AP |
457 | } |
458 | ||
11b904a1 BS |
459 | /* Emit INSN at the entry point of the function, ensuring that it is only |
460 | executed once per function. */ | |
461 | void | |
462 | emit_insn_at_entry (rtx insn) | |
463 | { | |
464 | edge_iterator ei = ei_start (ENTRY_BLOCK_PTR->succs); | |
465 | edge e = ei_safe_edge (ei); | |
5419bc7f | 466 | gcc_assert (e->flags & EDGE_FALLTHRU); |
11b904a1 BS |
467 | |
468 | insert_insn_on_edge (insn, e); | |
469 | commit_edge_insertions (); | |
470 | } | |
471 | ||
6c3d0e31 SP |
472 | /* Update BLOCK_FOR_INSN of insns between BEGIN and END |
473 | (or BARRIER if found) and notify df of the bb change. | |
474 | The insn chain range is inclusive | |
475 | (i.e. both BEGIN and END will be updated. */ | |
ca6c03ca | 476 | |
6c3d0e31 SP |
477 | static void |
478 | update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb) | |
ca6c03ca JH |
479 | { |
480 | rtx insn; | |
481 | ||
6c3d0e31 SP |
482 | end = NEXT_INSN (end); |
483 | for (insn = begin; insn != end; insn = NEXT_INSN (insn)) | |
63642d5a AO |
484 | if (!BARRIER_P (insn)) |
485 | df_insn_change_bb (insn, bb); | |
ca6c03ca | 486 | } |
6c3d0e31 SP |
487 | |
488 | /* Update BLOCK_FOR_INSN of insns in BB to BB, | |
489 | and notify df of the change. */ | |
490 | ||
491 | void | |
492 | update_bb_for_insn (basic_block bb) | |
493 | { | |
494 | update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb); | |
495 | } | |
496 | ||
ca6c03ca | 497 | \f |
6fb5fa3c DB |
498 | /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK |
499 | note associated with the BLOCK. */ | |
500 | ||
501 | static rtx | |
502 | first_insn_after_basic_block_note (basic_block block) | |
503 | { | |
504 | rtx insn; | |
505 | ||
506 | /* Get the first instruction in the block. */ | |
507 | insn = BB_HEAD (block); | |
508 | ||
509 | if (insn == NULL_RTX) | |
510 | return NULL_RTX; | |
511 | if (LABEL_P (insn)) | |
512 | insn = NEXT_INSN (insn); | |
513 | gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn)); | |
514 | ||
515 | return NEXT_INSN (insn); | |
516 | } | |
517 | ||
f470c378 ZD |
518 | /* Creates a new basic block just after basic block B by splitting |
519 | everything after specified instruction I. */ | |
ca6c03ca | 520 | |
f470c378 | 521 | static basic_block |
d329e058 | 522 | rtl_split_block (basic_block bb, void *insnp) |
ca6c03ca JH |
523 | { |
524 | basic_block new_bb; | |
ae50c0cb | 525 | rtx insn = (rtx) insnp; |
f470c378 | 526 | edge e; |
628f6a4e | 527 | edge_iterator ei; |
ca6c03ca | 528 | |
f470c378 ZD |
529 | if (!insn) |
530 | { | |
531 | insn = first_insn_after_basic_block_note (bb); | |
532 | ||
533 | if (insn) | |
b5b8b0ac AO |
534 | { |
535 | rtx next = insn; | |
536 | ||
537 | insn = PREV_INSN (insn); | |
538 | ||
539 | /* If the block contains only debug insns, insn would have | |
540 | been NULL in a non-debug compilation, and then we'd end | |
541 | up emitting a DELETED note. For -fcompare-debug | |
542 | stability, emit the note too. */ | |
543 | if (insn != BB_END (bb) | |
544 | && DEBUG_INSN_P (next) | |
545 | && DEBUG_INSN_P (BB_END (bb))) | |
546 | { | |
547 | while (next != BB_END (bb) && DEBUG_INSN_P (next)) | |
548 | next = NEXT_INSN (next); | |
549 | ||
550 | if (next == BB_END (bb)) | |
551 | emit_note_after (NOTE_INSN_DELETED, next); | |
552 | } | |
553 | } | |
f470c378 ZD |
554 | else |
555 | insn = get_last_insn (); | |
556 | } | |
557 | ||
558 | /* We probably should check type of the insn so that we do not create | |
559 | inconsistent cfg. It is checked in verify_flow_info anyway, so do not | |
560 | bother. */ | |
561 | if (insn == BB_END (bb)) | |
562 | emit_note_after (NOTE_INSN_DELETED, insn); | |
ca6c03ca JH |
563 | |
564 | /* Create the new basic block. */ | |
a813c111 | 565 | new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb); |
076c7ab8 | 566 | BB_COPY_PARTITION (new_bb, bb); |
a813c111 | 567 | BB_END (bb) = insn; |
ca6c03ca JH |
568 | |
569 | /* Redirect the outgoing edges. */ | |
628f6a4e BE |
570 | new_bb->succs = bb->succs; |
571 | bb->succs = NULL; | |
572 | FOR_EACH_EDGE (e, ei, new_bb->succs) | |
ca6c03ca JH |
573 | e->src = new_bb; |
574 | ||
6fb5fa3c DB |
575 | /* The new block starts off being dirty. */ |
576 | df_set_bb_dirty (bb); | |
f470c378 | 577 | return new_bb; |
bc35512f JH |
578 | } |
579 | ||
ca6c03ca | 580 | /* Blocks A and B are to be merged into a single block A. The insns |
bc35512f | 581 | are already contiguous. */ |
ca6c03ca | 582 | |
bc35512f JH |
583 | static void |
584 | rtl_merge_blocks (basic_block a, basic_block b) | |
ca6c03ca | 585 | { |
a813c111 | 586 | rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a); |
ca6c03ca | 587 | rtx del_first = NULL_RTX, del_last = NULL_RTX; |
b5b8b0ac | 588 | rtx b_debug_start = b_end, b_debug_end = b_end; |
ca6c03ca JH |
589 | int b_empty = 0; |
590 | ||
6fb5fa3c DB |
591 | if (dump_file) |
592 | fprintf (dump_file, "merging block %d into block %d\n", b->index, a->index); | |
593 | ||
b5b8b0ac AO |
594 | while (DEBUG_INSN_P (b_end)) |
595 | b_end = PREV_INSN (b_debug_start = b_end); | |
596 | ||
ca6c03ca | 597 | /* If there was a CODE_LABEL beginning B, delete it. */ |
4b4bf941 | 598 | if (LABEL_P (b_head)) |
ca6c03ca JH |
599 | { |
600 | /* Detect basic blocks with nothing but a label. This can happen | |
601 | in particular at the end of a function. */ | |
602 | if (b_head == b_end) | |
603 | b_empty = 1; | |
5f0d2358 | 604 | |
ca6c03ca JH |
605 | del_first = del_last = b_head; |
606 | b_head = NEXT_INSN (b_head); | |
607 | } | |
608 | ||
5f0d2358 RK |
609 | /* Delete the basic block note and handle blocks containing just that |
610 | note. */ | |
ca6c03ca JH |
611 | if (NOTE_INSN_BASIC_BLOCK_P (b_head)) |
612 | { | |
613 | if (b_head == b_end) | |
614 | b_empty = 1; | |
615 | if (! del_last) | |
616 | del_first = b_head; | |
5f0d2358 | 617 | |
ca6c03ca JH |
618 | del_last = b_head; |
619 | b_head = NEXT_INSN (b_head); | |
620 | } | |
621 | ||
622 | /* If there was a jump out of A, delete it. */ | |
4b4bf941 | 623 | if (JUMP_P (a_end)) |
ca6c03ca JH |
624 | { |
625 | rtx prev; | |
626 | ||
627 | for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev)) | |
4b4bf941 | 628 | if (!NOTE_P (prev) |
a38e7aa5 | 629 | || NOTE_INSN_BASIC_BLOCK_P (prev) |
a813c111 | 630 | || prev == BB_HEAD (a)) |
ca6c03ca JH |
631 | break; |
632 | ||
633 | del_first = a_end; | |
634 | ||
635 | #ifdef HAVE_cc0 | |
636 | /* If this was a conditional jump, we need to also delete | |
637 | the insn that set cc0. */ | |
638 | if (only_sets_cc0_p (prev)) | |
639 | { | |
640 | rtx tmp = prev; | |
5f0d2358 | 641 | |
ca6c03ca JH |
642 | prev = prev_nonnote_insn (prev); |
643 | if (!prev) | |
a813c111 | 644 | prev = BB_HEAD (a); |
ca6c03ca JH |
645 | del_first = tmp; |
646 | } | |
647 | #endif | |
648 | ||
649 | a_end = PREV_INSN (del_first); | |
650 | } | |
4b4bf941 | 651 | else if (BARRIER_P (NEXT_INSN (a_end))) |
ca6c03ca JH |
652 | del_first = NEXT_INSN (a_end); |
653 | ||
ca6c03ca JH |
654 | /* Delete everything marked above as well as crap that might be |
655 | hanging out between the two blocks. */ | |
f470c378 | 656 | BB_HEAD (b) = NULL; |
a7b87f73 | 657 | delete_insn_chain (del_first, del_last, true); |
ca6c03ca JH |
658 | |
659 | /* Reassociate the insns of B with A. */ | |
660 | if (!b_empty) | |
661 | { | |
b5b8b0ac | 662 | update_bb_for_insn_chain (a_end, b_debug_end, a); |
5f0d2358 | 663 | |
b5b8b0ac AO |
664 | a_end = b_debug_end; |
665 | } | |
666 | else if (b_end != b_debug_end) | |
667 | { | |
668 | /* Move any deleted labels and other notes between the end of A | |
669 | and the debug insns that make up B after the debug insns, | |
670 | bringing the debug insns into A while keeping the notes after | |
671 | the end of A. */ | |
672 | if (NEXT_INSN (a_end) != b_debug_start) | |
673 | reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start), | |
674 | b_debug_end); | |
675 | update_bb_for_insn_chain (b_debug_start, b_debug_end, a); | |
676 | a_end = b_debug_end; | |
ca6c03ca | 677 | } |
5f0d2358 | 678 | |
6fb5fa3c | 679 | df_bb_delete (b->index); |
a813c111 | 680 | BB_END (a) = a_end; |
ca6c03ca | 681 | } |
bc35512f | 682 | |
6fb5fa3c | 683 | |
bc35512f | 684 | /* Return true when block A and B can be merged. */ |
9678086d | 685 | |
b48d0358 DN |
686 | static bool |
687 | rtl_can_merge_blocks (basic_block a, basic_block b) | |
bc35512f | 688 | { |
750054a2 CT |
689 | /* If we are partitioning hot/cold basic blocks, we don't want to |
690 | mess up unconditional or indirect jumps that cross between hot | |
076c7ab8 ZW |
691 | and cold sections. |
692 | ||
8e8d5162 | 693 | Basic block partitioning may result in some jumps that appear to |
c22cacf3 MS |
694 | be optimizable (or blocks that appear to be mergeable), but which really |
695 | must be left untouched (they are required to make it safely across | |
696 | partition boundaries). See the comments at the top of | |
8e8d5162 | 697 | bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */ |
076c7ab8 | 698 | |
87c8b4be | 699 | if (BB_PARTITION (a) != BB_PARTITION (b)) |
076c7ab8 | 700 | return false; |
750054a2 | 701 | |
bc35512f | 702 | /* There must be exactly one edge in between the blocks. */ |
c5cbcccf ZD |
703 | return (single_succ_p (a) |
704 | && single_succ (a) == b | |
705 | && single_pred_p (b) | |
628f6a4e | 706 | && a != b |
bc35512f | 707 | /* Must be simple edge. */ |
c5cbcccf | 708 | && !(single_succ_edge (a)->flags & EDGE_COMPLEX) |
bc35512f JH |
709 | && a->next_bb == b |
710 | && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR | |
711 | /* If the jump insn has side effects, | |
712 | we can't kill the edge. */ | |
4b4bf941 | 713 | && (!JUMP_P (BB_END (a)) |
e24e7211 | 714 | || (reload_completed |
a813c111 | 715 | ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a))))); |
bc35512f | 716 | } |
ca6c03ca | 717 | \f |
5f0d2358 RK |
718 | /* Return the label in the head of basic block BLOCK. Create one if it doesn't |
719 | exist. */ | |
ca6c03ca JH |
720 | |
721 | rtx | |
d329e058 | 722 | block_label (basic_block block) |
ca6c03ca JH |
723 | { |
724 | if (block == EXIT_BLOCK_PTR) | |
725 | return NULL_RTX; | |
5f0d2358 | 726 | |
4b4bf941 | 727 | if (!LABEL_P (BB_HEAD (block))) |
ca6c03ca | 728 | { |
a813c111 | 729 | BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block)); |
ca6c03ca | 730 | } |
5f0d2358 | 731 | |
a813c111 | 732 | return BB_HEAD (block); |
ca6c03ca JH |
733 | } |
734 | ||
735 | /* Attempt to perform edge redirection by replacing possibly complex jump | |
5f0d2358 RK |
736 | instruction by unconditional jump or removing jump completely. This can |
737 | apply only if all edges now point to the same block. The parameters and | |
738 | return values are equivalent to redirect_edge_and_branch. */ | |
ca6c03ca | 739 | |
6de9cd9a | 740 | edge |
bc35512f | 741 | try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout) |
ca6c03ca JH |
742 | { |
743 | basic_block src = e->src; | |
a813c111 | 744 | rtx insn = BB_END (src), kill_from; |
e1233a7d | 745 | rtx set; |
ca6c03ca | 746 | int fallthru = 0; |
750054a2 CT |
747 | |
748 | /* If we are partitioning hot/cold basic blocks, we don't want to | |
749 | mess up unconditional or indirect jumps that cross between hot | |
8e8d5162 CT |
750 | and cold sections. |
751 | ||
752 | Basic block partitioning may result in some jumps that appear to | |
c22cacf3 MS |
753 | be optimizable (or blocks that appear to be mergeable), but which really |
754 | must be left untouched (they are required to make it safely across | |
755 | partition boundaries). See the comments at the top of | |
8e8d5162 | 756 | bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */ |
c22cacf3 | 757 | |
87c8b4be CT |
758 | if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX) |
759 | || BB_PARTITION (src) != BB_PARTITION (target)) | |
9cf84a3c | 760 | return NULL; |
750054a2 | 761 | |
6a66a8a7 KH |
762 | /* We can replace or remove a complex jump only when we have exactly |
763 | two edges. Also, if we have exactly one outgoing edge, we can | |
764 | redirect that. */ | |
765 | if (EDGE_COUNT (src->succs) >= 3 | |
766 | /* Verify that all targets will be TARGET. Specifically, the | |
767 | edge that is not E must also go to TARGET. */ | |
768 | || (EDGE_COUNT (src->succs) == 2 | |
769 | && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)) | |
770 | return NULL; | |
5f0d2358 | 771 | |
6a66a8a7 | 772 | if (!onlyjump_p (insn)) |
6de9cd9a | 773 | return NULL; |
3348b696 | 774 | if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL)) |
6de9cd9a | 775 | return NULL; |
ca6c03ca JH |
776 | |
777 | /* Avoid removing branch with side effects. */ | |
778 | set = single_set (insn); | |
779 | if (!set || side_effects_p (set)) | |
6de9cd9a | 780 | return NULL; |
ca6c03ca JH |
781 | |
782 | /* In case we zap a conditional jump, we'll need to kill | |
783 | the cc0 setter too. */ | |
784 | kill_from = insn; | |
785 | #ifdef HAVE_cc0 | |
9caea4a7 RS |
786 | if (reg_mentioned_p (cc0_rtx, PATTERN (insn)) |
787 | && only_sets_cc0_p (PREV_INSN (insn))) | |
ca6c03ca JH |
788 | kill_from = PREV_INSN (insn); |
789 | #endif | |
790 | ||
791 | /* See if we can create the fallthru edge. */ | |
bc35512f | 792 | if (in_cfglayout || can_fallthru (src, target)) |
ca6c03ca | 793 | { |
c263766c RH |
794 | if (dump_file) |
795 | fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn)); | |
ca6c03ca JH |
796 | fallthru = 1; |
797 | ||
eaec9b3d | 798 | /* Selectively unlink whole insn chain. */ |
bc35512f JH |
799 | if (in_cfglayout) |
800 | { | |
370369e1 | 801 | rtx insn = src->il.rtl->footer; |
bc35512f | 802 | |
a7b87f73 | 803 | delete_insn_chain (kill_from, BB_END (src), false); |
bc35512f JH |
804 | |
805 | /* Remove barriers but keep jumptables. */ | |
806 | while (insn) | |
807 | { | |
4b4bf941 | 808 | if (BARRIER_P (insn)) |
bc35512f JH |
809 | { |
810 | if (PREV_INSN (insn)) | |
811 | NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn); | |
812 | else | |
370369e1 | 813 | src->il.rtl->footer = NEXT_INSN (insn); |
bc35512f JH |
814 | if (NEXT_INSN (insn)) |
815 | PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn); | |
816 | } | |
4b4bf941 | 817 | if (LABEL_P (insn)) |
bc35512f JH |
818 | break; |
819 | insn = NEXT_INSN (insn); | |
820 | } | |
821 | } | |
822 | else | |
a7b87f73 ZD |
823 | delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)), |
824 | false); | |
ca6c03ca | 825 | } |
5f0d2358 | 826 | |
ca6c03ca JH |
827 | /* If this already is simplejump, redirect it. */ |
828 | else if (simplejump_p (insn)) | |
829 | { | |
830 | if (e->dest == target) | |
6de9cd9a | 831 | return NULL; |
c263766c RH |
832 | if (dump_file) |
833 | fprintf (dump_file, "Redirecting jump %i from %i to %i.\n", | |
0b17ab2f | 834 | INSN_UID (insn), e->dest->index, target->index); |
6ee3c8e4 JJ |
835 | if (!redirect_jump (insn, block_label (target), 0)) |
836 | { | |
341c100f NS |
837 | gcc_assert (target == EXIT_BLOCK_PTR); |
838 | return NULL; | |
6ee3c8e4 | 839 | } |
ca6c03ca | 840 | } |
5f0d2358 | 841 | |
6ee3c8e4 JJ |
842 | /* Cannot do anything for target exit block. */ |
843 | else if (target == EXIT_BLOCK_PTR) | |
6de9cd9a | 844 | return NULL; |
6ee3c8e4 | 845 | |
ca6c03ca JH |
846 | /* Or replace possibly complicated jump insn by simple jump insn. */ |
847 | else | |
848 | { | |
849 | rtx target_label = block_label (target); | |
eb5b8ad4 | 850 | rtx barrier, label, table; |
ca6c03ca | 851 | |
a7102479 | 852 | emit_jump_insn_after_noloc (gen_jump (target_label), insn); |
a813c111 | 853 | JUMP_LABEL (BB_END (src)) = target_label; |
ca6c03ca | 854 | LABEL_NUSES (target_label)++; |
c263766c RH |
855 | if (dump_file) |
856 | fprintf (dump_file, "Replacing insn %i by jump %i\n", | |
a813c111 | 857 | INSN_UID (insn), INSN_UID (BB_END (src))); |
ca6c03ca | 858 | |
4da2eb6b | 859 | |
ba4807a0 LB |
860 | delete_insn_chain (kill_from, insn, false); |
861 | ||
4da2eb6b RH |
862 | /* Recognize a tablejump that we are converting to a |
863 | simple jump and remove its associated CODE_LABEL | |
864 | and ADDR_VEC or ADDR_DIFF_VEC. */ | |
ba4807a0 LB |
865 | if (tablejump_p (insn, &label, &table)) |
866 | delete_insn_chain (label, table, false); | |
eb5b8ad4 | 867 | |
a813c111 | 868 | barrier = next_nonnote_insn (BB_END (src)); |
4b4bf941 | 869 | if (!barrier || !BARRIER_P (barrier)) |
a813c111 | 870 | emit_barrier_after (BB_END (src)); |
5d693491 JZ |
871 | else |
872 | { | |
a813c111 | 873 | if (barrier != NEXT_INSN (BB_END (src))) |
5d693491 JZ |
874 | { |
875 | /* Move the jump before barrier so that the notes | |
876 | which originally were or were created before jump table are | |
877 | inside the basic block. */ | |
a813c111 | 878 | rtx new_insn = BB_END (src); |
5d693491 | 879 | |
6c3d0e31 SP |
880 | update_bb_for_insn_chain (NEXT_INSN (BB_END (src)), |
881 | PREV_INSN (barrier), src); | |
5d693491 JZ |
882 | |
883 | NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn); | |
884 | PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn); | |
885 | ||
886 | NEXT_INSN (new_insn) = barrier; | |
887 | NEXT_INSN (PREV_INSN (barrier)) = new_insn; | |
888 | ||
889 | PREV_INSN (new_insn) = PREV_INSN (barrier); | |
890 | PREV_INSN (barrier) = new_insn; | |
891 | } | |
892 | } | |
ca6c03ca JH |
893 | } |
894 | ||
895 | /* Keep only one edge out and set proper flags. */ | |
c5cbcccf | 896 | if (!single_succ_p (src)) |
628f6a4e | 897 | remove_edge (e); |
c5cbcccf | 898 | gcc_assert (single_succ_p (src)); |
628f6a4e | 899 | |
c5cbcccf | 900 | e = single_succ_edge (src); |
ca6c03ca JH |
901 | if (fallthru) |
902 | e->flags = EDGE_FALLTHRU; | |
903 | else | |
904 | e->flags = 0; | |
5f0d2358 | 905 | |
ca6c03ca JH |
906 | e->probability = REG_BR_PROB_BASE; |
907 | e->count = src->count; | |
908 | ||
ca6c03ca JH |
909 | if (e->dest != target) |
910 | redirect_edge_succ (e, target); | |
6de9cd9a | 911 | return e; |
ca6c03ca JH |
912 | } |
913 | ||
4e3825db MM |
914 | /* Subroutine of redirect_branch_edge that tries to patch the jump |
915 | instruction INSN so that it reaches block NEW. Do this | |
916 | only when it originally reached block OLD. Return true if this | |
917 | worked or the original target wasn't OLD, return false if redirection | |
918 | doesn't work. */ | |
919 | ||
920 | static bool | |
921 | patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb) | |
ca6c03ca JH |
922 | { |
923 | rtx tmp; | |
ca6c03ca | 924 | /* Recognize a tablejump and adjust all matching cases. */ |
e1233a7d | 925 | if (tablejump_p (insn, NULL, &tmp)) |
ca6c03ca JH |
926 | { |
927 | rtvec vec; | |
928 | int j; | |
4e3825db | 929 | rtx new_label = block_label (new_bb); |
ca6c03ca | 930 | |
4e3825db MM |
931 | if (new_bb == EXIT_BLOCK_PTR) |
932 | return false; | |
ca6c03ca JH |
933 | if (GET_CODE (PATTERN (tmp)) == ADDR_VEC) |
934 | vec = XVEC (PATTERN (tmp), 0); | |
935 | else | |
936 | vec = XVEC (PATTERN (tmp), 1); | |
937 | ||
938 | for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j) | |
939 | if (XEXP (RTVEC_ELT (vec, j), 0) == old_label) | |
940 | { | |
941 | RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label); | |
942 | --LABEL_NUSES (old_label); | |
943 | ++LABEL_NUSES (new_label); | |
944 | } | |
945 | ||
f9da5064 | 946 | /* Handle casesi dispatch insns. */ |
ca6c03ca JH |
947 | if ((tmp = single_set (insn)) != NULL |
948 | && SET_DEST (tmp) == pc_rtx | |
949 | && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE | |
950 | && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF | |
951 | && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label) | |
952 | { | |
4c33cb26 | 953 | XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode, |
ca6c03ca JH |
954 | new_label); |
955 | --LABEL_NUSES (old_label); | |
956 | ++LABEL_NUSES (new_label); | |
957 | } | |
958 | } | |
959 | else | |
960 | { | |
961 | /* ?? We may play the games with moving the named labels from | |
962 | one basic block to the other in case only one computed_jump is | |
963 | available. */ | |
5f0d2358 RK |
964 | if (computed_jump_p (insn) |
965 | /* A return instruction can't be redirected. */ | |
966 | || returnjump_p (insn)) | |
4e3825db | 967 | return false; |
6ee3c8e4 | 968 | |
4e3825db | 969 | if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label) |
6ee3c8e4 | 970 | { |
4e3825db MM |
971 | /* If the insn doesn't go where we think, we're confused. */ |
972 | gcc_assert (JUMP_LABEL (insn) == old_label); | |
973 | ||
974 | /* If the substitution doesn't succeed, die. This can happen | |
975 | if the back end emitted unrecognizable instructions or if | |
976 | target is exit block on some arches. */ | |
977 | if (!redirect_jump (insn, block_label (new_bb), 0)) | |
978 | { | |
979 | gcc_assert (new_bb == EXIT_BLOCK_PTR); | |
980 | return false; | |
981 | } | |
6ee3c8e4 | 982 | } |
ca6c03ca | 983 | } |
4e3825db MM |
984 | return true; |
985 | } | |
986 | ||
987 | ||
988 | /* Redirect edge representing branch of (un)conditional jump or tablejump, | |
989 | NULL on failure */ | |
990 | static edge | |
991 | redirect_branch_edge (edge e, basic_block target) | |
992 | { | |
993 | rtx old_label = BB_HEAD (e->dest); | |
994 | basic_block src = e->src; | |
995 | rtx insn = BB_END (src); | |
996 | ||
997 | /* We can only redirect non-fallthru edges of jump insn. */ | |
998 | if (e->flags & EDGE_FALLTHRU) | |
999 | return NULL; | |
1000 | else if (!JUMP_P (insn) && !currently_expanding_to_rtl) | |
1001 | return NULL; | |
1002 | ||
1003 | if (!currently_expanding_to_rtl) | |
1004 | { | |
1005 | if (!patch_jump_insn (insn, old_label, target)) | |
1006 | return NULL; | |
1007 | } | |
1008 | else | |
1009 | /* When expanding this BB might actually contain multiple | |
1010 | jumps (i.e. not yet split by find_many_sub_basic_blocks). | |
1011 | Redirect all of those that match our label. */ | |
1012 | for (insn = BB_HEAD (src); insn != NEXT_INSN (BB_END (src)); | |
1013 | insn = NEXT_INSN (insn)) | |
1014 | if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target)) | |
1015 | return NULL; | |
ca6c03ca | 1016 | |
c263766c RH |
1017 | if (dump_file) |
1018 | fprintf (dump_file, "Edge %i->%i redirected to %i\n", | |
0b17ab2f | 1019 | e->src->index, e->dest->index, target->index); |
5f0d2358 | 1020 | |
ca6c03ca | 1021 | if (e->dest != target) |
6de9cd9a | 1022 | e = redirect_edge_succ_nodup (e, target); |
6fb5fa3c | 1023 | |
6de9cd9a | 1024 | return e; |
bc35512f JH |
1025 | } |
1026 | ||
1027 | /* Attempt to change code to redirect edge E to TARGET. Don't do that on | |
1028 | expense of adding new instructions or reordering basic blocks. | |
1029 | ||
1030 | Function can be also called with edge destination equivalent to the TARGET. | |
1031 | Then it should try the simplifications and do nothing if none is possible. | |
1032 | ||
6de9cd9a DN |
1033 | Return edge representing the branch if transformation succeeded. Return NULL |
1034 | on failure. | |
1035 | We still return NULL in case E already destinated TARGET and we didn't | |
1036 | managed to simplify instruction stream. */ | |
bc35512f | 1037 | |
6de9cd9a | 1038 | static edge |
5671bf27 | 1039 | rtl_redirect_edge_and_branch (edge e, basic_block target) |
bc35512f | 1040 | { |
6de9cd9a | 1041 | edge ret; |
f345f21a JH |
1042 | basic_block src = e->src; |
1043 | ||
bc35512f | 1044 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) |
6de9cd9a | 1045 | return NULL; |
bc35512f | 1046 | |
3348b696 | 1047 | if (e->dest == target) |
6de9cd9a | 1048 | return e; |
3348b696 | 1049 | |
6de9cd9a | 1050 | if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL) |
f345f21a | 1051 | { |
6fb5fa3c | 1052 | df_set_bb_dirty (src); |
6de9cd9a | 1053 | return ret; |
f345f21a | 1054 | } |
bc35512f | 1055 | |
6de9cd9a DN |
1056 | ret = redirect_branch_edge (e, target); |
1057 | if (!ret) | |
1058 | return NULL; | |
5f0d2358 | 1059 | |
6fb5fa3c | 1060 | df_set_bb_dirty (src); |
6de9cd9a | 1061 | return ret; |
ca6c03ca JH |
1062 | } |
1063 | ||
4fe9b91c | 1064 | /* Like force_nonfallthru below, but additionally performs redirection |
ca6c03ca JH |
1065 | Used by redirect_edge_and_branch_force. */ |
1066 | ||
9167e1c0 | 1067 | static basic_block |
d329e058 | 1068 | force_nonfallthru_and_redirect (edge e, basic_block target) |
ca6c03ca | 1069 | { |
a3716585 | 1070 | basic_block jump_block, new_bb = NULL, src = e->src; |
ca6c03ca JH |
1071 | rtx note; |
1072 | edge new_edge; | |
a3716585 | 1073 | int abnormal_edge_flags = 0; |
7241571e | 1074 | int loc; |
ca6c03ca | 1075 | |
cb9a1d9b JH |
1076 | /* In the case the last instruction is conditional jump to the next |
1077 | instruction, first redirect the jump itself and then continue | |
b20b352b | 1078 | by creating a basic block afterwards to redirect fallthru edge. */ |
cb9a1d9b | 1079 | if (e->src != ENTRY_BLOCK_PTR && e->dest != EXIT_BLOCK_PTR |
a813c111 | 1080 | && any_condjump_p (BB_END (e->src)) |
a813c111 | 1081 | && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest)) |
cb9a1d9b JH |
1082 | { |
1083 | rtx note; | |
58e6ae30 | 1084 | edge b = unchecked_make_edge (e->src, target, 0); |
341c100f | 1085 | bool redirected; |
cb9a1d9b | 1086 | |
341c100f NS |
1087 | redirected = redirect_jump (BB_END (e->src), block_label (target), 0); |
1088 | gcc_assert (redirected); | |
c22cacf3 | 1089 | |
a813c111 | 1090 | note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX); |
cb9a1d9b JH |
1091 | if (note) |
1092 | { | |
1093 | int prob = INTVAL (XEXP (note, 0)); | |
1094 | ||
1095 | b->probability = prob; | |
1096 | b->count = e->count * prob / REG_BR_PROB_BASE; | |
1097 | e->probability -= e->probability; | |
1098 | e->count -= b->count; | |
1099 | if (e->probability < 0) | |
1100 | e->probability = 0; | |
1101 | if (e->count < 0) | |
1102 | e->count = 0; | |
1103 | } | |
1104 | } | |
1105 | ||
ca6c03ca | 1106 | if (e->flags & EDGE_ABNORMAL) |
a3716585 JH |
1107 | { |
1108 | /* Irritating special case - fallthru edge to the same block as abnormal | |
1109 | edge. | |
1110 | We can't redirect abnormal edge, but we still can split the fallthru | |
d329e058 | 1111 | one and create separate abnormal edge to original destination. |
a3716585 | 1112 | This allows bb-reorder to make such edge non-fallthru. */ |
341c100f | 1113 | gcc_assert (e->dest == target); |
a3716585 JH |
1114 | abnormal_edge_flags = e->flags & ~(EDGE_FALLTHRU | EDGE_CAN_FALLTHRU); |
1115 | e->flags &= EDGE_FALLTHRU | EDGE_CAN_FALLTHRU; | |
1116 | } | |
341c100f | 1117 | else |
24c545ff | 1118 | { |
341c100f NS |
1119 | gcc_assert (e->flags & EDGE_FALLTHRU); |
1120 | if (e->src == ENTRY_BLOCK_PTR) | |
1121 | { | |
1122 | /* We can't redirect the entry block. Create an empty block | |
628f6a4e BE |
1123 | at the start of the function which we use to add the new |
1124 | jump. */ | |
1125 | edge tmp; | |
1126 | edge_iterator ei; | |
1127 | bool found = false; | |
c22cacf3 | 1128 | |
628f6a4e | 1129 | basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, ENTRY_BLOCK_PTR); |
c22cacf3 | 1130 | |
341c100f NS |
1131 | /* Change the existing edge's source to be the new block, and add |
1132 | a new edge from the entry block to the new block. */ | |
1133 | e->src = bb; | |
628f6a4e BE |
1134 | for (ei = ei_start (ENTRY_BLOCK_PTR->succs); (tmp = ei_safe_edge (ei)); ) |
1135 | { | |
1136 | if (tmp == e) | |
1137 | { | |
865851d0 | 1138 | VEC_unordered_remove (edge, ENTRY_BLOCK_PTR->succs, ei.index); |
628f6a4e BE |
1139 | found = true; |
1140 | break; | |
1141 | } | |
1142 | else | |
1143 | ei_next (&ei); | |
1144 | } | |
c22cacf3 | 1145 | |
628f6a4e | 1146 | gcc_assert (found); |
c22cacf3 | 1147 | |
d4e6fecb | 1148 | VEC_safe_push (edge, gc, bb->succs, e); |
341c100f NS |
1149 | make_single_succ_edge (ENTRY_BLOCK_PTR, bb, EDGE_FALLTHRU); |
1150 | } | |
24c545ff BS |
1151 | } |
1152 | ||
628f6a4e | 1153 | if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags) |
ca6c03ca JH |
1154 | { |
1155 | /* Create the new structures. */ | |
31a78298 | 1156 | |
79019985 RH |
1157 | /* If the old block ended with a tablejump, skip its table |
1158 | by searching forward from there. Otherwise start searching | |
1159 | forward from the last instruction of the old block. */ | |
a813c111 SB |
1160 | if (!tablejump_p (BB_END (e->src), NULL, ¬e)) |
1161 | note = BB_END (e->src); | |
31a78298 RH |
1162 | note = NEXT_INSN (note); |
1163 | ||
31a78298 | 1164 | jump_block = create_basic_block (note, NULL, e->src); |
ca6c03ca JH |
1165 | jump_block->count = e->count; |
1166 | jump_block->frequency = EDGE_FREQUENCY (e); | |
1167 | jump_block->loop_depth = target->loop_depth; | |
1168 | ||
750054a2 CT |
1169 | /* Make sure new block ends up in correct hot/cold section. */ |
1170 | ||
076c7ab8 | 1171 | BB_COPY_PARTITION (jump_block, e->src); |
9fb32434 | 1172 | if (flag_reorder_blocks_and_partition |
87c8b4be CT |
1173 | && targetm.have_named_sections |
1174 | && JUMP_P (BB_END (jump_block)) | |
1175 | && !any_condjump_p (BB_END (jump_block)) | |
1176 | && (EDGE_SUCC (jump_block, 0)->flags & EDGE_CROSSING)) | |
65c5f2a6 | 1177 | add_reg_note (BB_END (jump_block), REG_CROSSING_JUMP, NULL_RTX); |
c22cacf3 | 1178 | |
ca6c03ca JH |
1179 | /* Wire edge in. */ |
1180 | new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU); | |
1181 | new_edge->probability = e->probability; | |
1182 | new_edge->count = e->count; | |
1183 | ||
1184 | /* Redirect old edge. */ | |
1185 | redirect_edge_pred (e, jump_block); | |
1186 | e->probability = REG_BR_PROB_BASE; | |
1187 | ||
1188 | new_bb = jump_block; | |
1189 | } | |
1190 | else | |
1191 | jump_block = e->src; | |
5f0d2358 | 1192 | |
7241571e JJ |
1193 | if (e->goto_locus && e->goto_block == NULL) |
1194 | loc = e->goto_locus; | |
1195 | else | |
1196 | loc = 0; | |
ca6c03ca JH |
1197 | e->flags &= ~EDGE_FALLTHRU; |
1198 | if (target == EXIT_BLOCK_PTR) | |
1199 | { | |
cf22ce3c | 1200 | #ifdef HAVE_return |
7241571e | 1201 | emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc); |
cf22ce3c | 1202 | #else |
341c100f | 1203 | gcc_unreachable (); |
cf22ce3c | 1204 | #endif |
ca6c03ca JH |
1205 | } |
1206 | else | |
1207 | { | |
1208 | rtx label = block_label (target); | |
7241571e | 1209 | emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc); |
a813c111 | 1210 | JUMP_LABEL (BB_END (jump_block)) = label; |
ca6c03ca JH |
1211 | LABEL_NUSES (label)++; |
1212 | } | |
5f0d2358 | 1213 | |
a813c111 | 1214 | emit_barrier_after (BB_END (jump_block)); |
ca6c03ca JH |
1215 | redirect_edge_succ_nodup (e, target); |
1216 | ||
a3716585 JH |
1217 | if (abnormal_edge_flags) |
1218 | make_edge (src, target, abnormal_edge_flags); | |
1219 | ||
6fb5fa3c | 1220 | df_mark_solutions_dirty (); |
ca6c03ca JH |
1221 | return new_bb; |
1222 | } | |
1223 | ||
1224 | /* Edge E is assumed to be fallthru edge. Emit needed jump instruction | |
1225 | (and possibly create new basic block) to make edge non-fallthru. | |
1226 | Return newly created BB or NULL if none. */ | |
5f0d2358 | 1227 | |
ca6c03ca | 1228 | basic_block |
d329e058 | 1229 | force_nonfallthru (edge e) |
ca6c03ca JH |
1230 | { |
1231 | return force_nonfallthru_and_redirect (e, e->dest); | |
1232 | } | |
1233 | ||
1234 | /* Redirect edge even at the expense of creating new jump insn or | |
1235 | basic block. Return new basic block if created, NULL otherwise. | |
41806d92 | 1236 | Conversion must be possible. */ |
ca6c03ca | 1237 | |
9ee634e3 | 1238 | static basic_block |
d329e058 | 1239 | rtl_redirect_edge_and_branch_force (edge e, basic_block target) |
ca6c03ca | 1240 | { |
5f0d2358 RK |
1241 | if (redirect_edge_and_branch (e, target) |
1242 | || e->dest == target) | |
ca6c03ca JH |
1243 | return NULL; |
1244 | ||
1245 | /* In case the edge redirection failed, try to force it to be non-fallthru | |
1246 | and redirect newly created simplejump. */ | |
6fb5fa3c | 1247 | df_set_bb_dirty (e->src); |
5f0d2358 | 1248 | return force_nonfallthru_and_redirect (e, target); |
ca6c03ca JH |
1249 | } |
1250 | ||
1251 | /* The given edge should potentially be a fallthru edge. If that is in | |
1252 | fact true, delete the jump and barriers that are in the way. */ | |
1253 | ||
f470c378 ZD |
1254 | static void |
1255 | rtl_tidy_fallthru_edge (edge e) | |
ca6c03ca JH |
1256 | { |
1257 | rtx q; | |
f470c378 ZD |
1258 | basic_block b = e->src, c = b->next_bb; |
1259 | ||
ca6c03ca JH |
1260 | /* ??? In a late-running flow pass, other folks may have deleted basic |
1261 | blocks by nopping out blocks, leaving multiple BARRIERs between here | |
0fa2e4df | 1262 | and the target label. They ought to be chastised and fixed. |
ca6c03ca JH |
1263 | |
1264 | We can also wind up with a sequence of undeletable labels between | |
1265 | one block and the next. | |
1266 | ||
1267 | So search through a sequence of barriers, labels, and notes for | |
1268 | the head of block C and assert that we really do fall through. */ | |
1269 | ||
a813c111 | 1270 | for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q)) |
9c0a0632 RH |
1271 | if (INSN_P (q)) |
1272 | return; | |
ca6c03ca JH |
1273 | |
1274 | /* Remove what will soon cease being the jump insn from the source block. | |
1275 | If block B consisted only of this single jump, turn it into a deleted | |
1276 | note. */ | |
a813c111 | 1277 | q = BB_END (b); |
4b4bf941 | 1278 | if (JUMP_P (q) |
ca6c03ca JH |
1279 | && onlyjump_p (q) |
1280 | && (any_uncondjump_p (q) | |
c5cbcccf | 1281 | || single_succ_p (b))) |
ca6c03ca JH |
1282 | { |
1283 | #ifdef HAVE_cc0 | |
1284 | /* If this was a conditional jump, we need to also delete | |
1285 | the insn that set cc0. */ | |
1286 | if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q))) | |
1287 | q = PREV_INSN (q); | |
1288 | #endif | |
1289 | ||
1290 | q = PREV_INSN (q); | |
ca6c03ca JH |
1291 | } |
1292 | ||
1293 | /* Selectively unlink the sequence. */ | |
a813c111 | 1294 | if (q != PREV_INSN (BB_HEAD (c))) |
a7b87f73 | 1295 | delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false); |
ca6c03ca JH |
1296 | |
1297 | e->flags |= EDGE_FALLTHRU; | |
1298 | } | |
ca6c03ca | 1299 | \f |
f470c378 ZD |
1300 | /* Should move basic block BB after basic block AFTER. NIY. */ |
1301 | ||
1302 | static bool | |
1303 | rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED, | |
1304 | basic_block after ATTRIBUTE_UNUSED) | |
1305 | { | |
1306 | return false; | |
1307 | } | |
1308 | ||
ca6c03ca | 1309 | /* Split a (typically critical) edge. Return the new block. |
41806d92 | 1310 | The edge must not be abnormal. |
ca6c03ca JH |
1311 | |
1312 | ??? The code generally expects to be called on critical edges. | |
1313 | The case of a block ending in an unconditional jump to a | |
1314 | block with multiple predecessors is not handled optimally. */ | |
1315 | ||
8ce33230 | 1316 | static basic_block |
d329e058 | 1317 | rtl_split_edge (edge edge_in) |
ca6c03ca JH |
1318 | { |
1319 | basic_block bb; | |
ca6c03ca JH |
1320 | rtx before; |
1321 | ||
1322 | /* Abnormal edges cannot be split. */ | |
341c100f | 1323 | gcc_assert (!(edge_in->flags & EDGE_ABNORMAL)); |
ca6c03ca JH |
1324 | |
1325 | /* We are going to place the new block in front of edge destination. | |
eaec9b3d | 1326 | Avoid existence of fallthru predecessors. */ |
ca6c03ca JH |
1327 | if ((edge_in->flags & EDGE_FALLTHRU) == 0) |
1328 | { | |
1329 | edge e; | |
628f6a4e | 1330 | edge_iterator ei; |
5f0d2358 | 1331 | |
628f6a4e | 1332 | FOR_EACH_EDGE (e, ei, edge_in->dest->preds) |
ca6c03ca JH |
1333 | if (e->flags & EDGE_FALLTHRU) |
1334 | break; | |
1335 | ||
1336 | if (e) | |
1337 | force_nonfallthru (e); | |
1338 | } | |
1339 | ||
96e82e0a ZD |
1340 | /* Create the basic block note. */ |
1341 | if (edge_in->dest != EXIT_BLOCK_PTR) | |
a813c111 | 1342 | before = BB_HEAD (edge_in->dest); |
ca6c03ca JH |
1343 | else |
1344 | before = NULL_RTX; | |
1345 | ||
623a66fa R |
1346 | /* If this is a fall through edge to the exit block, the blocks might be |
1347 | not adjacent, and the right place is the after the source. */ | |
1348 | if (edge_in->flags & EDGE_FALLTHRU && edge_in->dest == EXIT_BLOCK_PTR) | |
1349 | { | |
1350 | before = NEXT_INSN (BB_END (edge_in->src)); | |
623a66fa | 1351 | bb = create_basic_block (before, NULL, edge_in->src); |
076c7ab8 | 1352 | BB_COPY_PARTITION (bb, edge_in->src); |
623a66fa R |
1353 | } |
1354 | else | |
9fb32434 CT |
1355 | { |
1356 | bb = create_basic_block (before, NULL, edge_in->dest->prev_bb); | |
076c7ab8 ZW |
1357 | /* ??? Why not edge_in->dest->prev_bb here? */ |
1358 | BB_COPY_PARTITION (bb, edge_in->dest); | |
9fb32434 | 1359 | } |
ca6c03ca | 1360 | |
4977bab6 | 1361 | make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU); |
ca6c03ca | 1362 | |
4d6922ee | 1363 | /* For non-fallthru edges, we must adjust the predecessor's |
ca6c03ca JH |
1364 | jump instruction to target our new block. */ |
1365 | if ((edge_in->flags & EDGE_FALLTHRU) == 0) | |
1366 | { | |
341c100f NS |
1367 | edge redirected = redirect_edge_and_branch (edge_in, bb); |
1368 | gcc_assert (redirected); | |
ca6c03ca JH |
1369 | } |
1370 | else | |
1371 | redirect_edge_succ (edge_in, bb); | |
1372 | ||
1373 | return bb; | |
1374 | } | |
1375 | ||
1376 | /* Queue instructions for insertion on an edge between two basic blocks. | |
1377 | The new instructions and basic blocks (if any) will not appear in the | |
1378 | CFG until commit_edge_insertions is called. */ | |
1379 | ||
1380 | void | |
d329e058 | 1381 | insert_insn_on_edge (rtx pattern, edge e) |
ca6c03ca JH |
1382 | { |
1383 | /* We cannot insert instructions on an abnormal critical edge. | |
1384 | It will be easier to find the culprit if we die now. */ | |
341c100f | 1385 | gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))); |
ca6c03ca | 1386 | |
6de9cd9a | 1387 | if (e->insns.r == NULL_RTX) |
ca6c03ca JH |
1388 | start_sequence (); |
1389 | else | |
6de9cd9a | 1390 | push_to_sequence (e->insns.r); |
ca6c03ca JH |
1391 | |
1392 | emit_insn (pattern); | |
1393 | ||
6de9cd9a | 1394 | e->insns.r = get_insns (); |
ca6c03ca JH |
1395 | end_sequence (); |
1396 | } | |
1397 | ||
1398 | /* Update the CFG for the instructions queued on edge E. */ | |
1399 | ||
4e3825db | 1400 | void |
2ac66157 | 1401 | commit_one_edge_insertion (edge e) |
ca6c03ca JH |
1402 | { |
1403 | rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last; | |
eae4bc56 | 1404 | basic_block bb = NULL; |
ca6c03ca JH |
1405 | |
1406 | /* Pull the insns off the edge now since the edge might go away. */ | |
6de9cd9a DN |
1407 | insns = e->insns.r; |
1408 | e->insns.r = NULL_RTX; | |
ca6c03ca | 1409 | |
3dec4024 | 1410 | if (!before && !after) |
ca6c03ca | 1411 | { |
3dec4024 | 1412 | /* Figure out where to put these things. If the destination has |
c22cacf3 | 1413 | one predecessor, insert there. Except for the exit block. */ |
c5cbcccf | 1414 | if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR) |
ca6c03ca | 1415 | { |
3dec4024 JH |
1416 | bb = e->dest; |
1417 | ||
1418 | /* Get the location correct wrt a code label, and "nice" wrt | |
1419 | a basic block note, and before everything else. */ | |
a813c111 | 1420 | tmp = BB_HEAD (bb); |
4b4bf941 | 1421 | if (LABEL_P (tmp)) |
3dec4024 JH |
1422 | tmp = NEXT_INSN (tmp); |
1423 | if (NOTE_INSN_BASIC_BLOCK_P (tmp)) | |
1424 | tmp = NEXT_INSN (tmp); | |
a813c111 | 1425 | if (tmp == BB_HEAD (bb)) |
3dec4024 JH |
1426 | before = tmp; |
1427 | else if (tmp) | |
1428 | after = PREV_INSN (tmp); | |
1429 | else | |
1430 | after = get_last_insn (); | |
1431 | } | |
1432 | ||
1433 | /* If the source has one successor and the edge is not abnormal, | |
c22cacf3 | 1434 | insert there. Except for the entry block. */ |
3dec4024 | 1435 | else if ((e->flags & EDGE_ABNORMAL) == 0 |
c5cbcccf | 1436 | && single_succ_p (e->src) |
3dec4024 JH |
1437 | && e->src != ENTRY_BLOCK_PTR) |
1438 | { | |
1439 | bb = e->src; | |
1440 | ||
1441 | /* It is possible to have a non-simple jump here. Consider a target | |
1442 | where some forms of unconditional jumps clobber a register. This | |
1443 | happens on the fr30 for example. | |
1444 | ||
1445 | We know this block has a single successor, so we can just emit | |
1446 | the queued insns before the jump. */ | |
4b4bf941 | 1447 | if (JUMP_P (BB_END (bb))) |
96e82e0a | 1448 | before = BB_END (bb); |
3dec4024 JH |
1449 | else |
1450 | { | |
341c100f NS |
1451 | /* We'd better be fallthru, or we've lost track of |
1452 | what's what. */ | |
1453 | gcc_assert (e->flags & EDGE_FALLTHRU); | |
ca6c03ca | 1454 | |
a813c111 | 1455 | after = BB_END (bb); |
3dec4024 JH |
1456 | } |
1457 | } | |
1458 | /* Otherwise we must split the edge. */ | |
1459 | else | |
1460 | { | |
1461 | bb = split_edge (e); | |
a813c111 | 1462 | after = BB_END (bb); |
750054a2 | 1463 | |
750054a2 | 1464 | if (flag_reorder_blocks_and_partition |
9fb32434 | 1465 | && targetm.have_named_sections |
750054a2 | 1466 | && e->src != ENTRY_BLOCK_PTR |
076c7ab8 | 1467 | && BB_PARTITION (e->src) == BB_COLD_PARTITION |
bd454efd | 1468 | && !(e->flags & EDGE_CROSSING)) |
750054a2 | 1469 | { |
87c8b4be | 1470 | rtx bb_note, cur_insn; |
750054a2 CT |
1471 | |
1472 | bb_note = NULL_RTX; | |
1473 | for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb)); | |
1474 | cur_insn = NEXT_INSN (cur_insn)) | |
a38e7aa5 | 1475 | if (NOTE_INSN_BASIC_BLOCK_P (cur_insn)) |
750054a2 CT |
1476 | { |
1477 | bb_note = cur_insn; | |
1478 | break; | |
1479 | } | |
1480 | ||
4b4bf941 | 1481 | if (JUMP_P (BB_END (bb)) |
750054a2 | 1482 | && !any_condjump_p (BB_END (bb)) |
c22cacf3 | 1483 | && (single_succ_edge (bb)->flags & EDGE_CROSSING)) |
65c5f2a6 | 1484 | add_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX); |
750054a2 | 1485 | } |
ca6c03ca JH |
1486 | } |
1487 | } | |
1488 | ||
ca6c03ca JH |
1489 | /* Now that we've found the spot, do the insertion. */ |
1490 | ||
1491 | if (before) | |
1492 | { | |
6fb5fa3c | 1493 | emit_insn_before_noloc (insns, before, bb); |
ca6c03ca JH |
1494 | last = prev_nonnote_insn (before); |
1495 | } | |
1496 | else | |
6fb5fa3c | 1497 | last = emit_insn_after_noloc (insns, after, bb); |
ca6c03ca JH |
1498 | |
1499 | if (returnjump_p (last)) | |
1500 | { | |
1501 | /* ??? Remove all outgoing edges from BB and add one for EXIT. | |
c22cacf3 MS |
1502 | This is not currently a problem because this only happens |
1503 | for the (single) epilogue, which already has a fallthru edge | |
1504 | to EXIT. */ | |
ca6c03ca | 1505 | |
c5cbcccf | 1506 | e = single_succ_edge (bb); |
341c100f | 1507 | gcc_assert (e->dest == EXIT_BLOCK_PTR |
c5cbcccf | 1508 | && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU)); |
ca6c03ca | 1509 | |
5f0d2358 | 1510 | e->flags &= ~EDGE_FALLTHRU; |
ca6c03ca | 1511 | emit_barrier_after (last); |
0b17ab2f | 1512 | |
ca6c03ca JH |
1513 | if (before) |
1514 | delete_insn (before); | |
1515 | } | |
341c100f NS |
1516 | else |
1517 | gcc_assert (!JUMP_P (last)); | |
5f0d2358 | 1518 | |
12eff7b7 | 1519 | /* Mark the basic block for find_many_sub_basic_blocks. */ |
05549c96 SB |
1520 | if (current_ir_type () != IR_RTL_CFGLAYOUT) |
1521 | bb->aux = &bb->aux; | |
ca6c03ca JH |
1522 | } |
1523 | ||
1524 | /* Update the CFG for all queued instructions. */ | |
1525 | ||
1526 | void | |
d329e058 | 1527 | commit_edge_insertions (void) |
ca6c03ca | 1528 | { |
ca6c03ca | 1529 | basic_block bb; |
9dca2ad5 | 1530 | sbitmap blocks; |
9809a362 | 1531 | bool changed = false; |
ca6c03ca JH |
1532 | |
1533 | #ifdef ENABLE_CHECKING | |
1534 | verify_flow_info (); | |
1535 | #endif | |
1536 | ||
e0082a72 | 1537 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb) |
ca6c03ca | 1538 | { |
628f6a4e BE |
1539 | edge e; |
1540 | edge_iterator ei; | |
ca6c03ca | 1541 | |
628f6a4e BE |
1542 | FOR_EACH_EDGE (e, ei, bb->succs) |
1543 | if (e->insns.r) | |
1544 | { | |
1545 | changed = true; | |
2ac66157 | 1546 | commit_one_edge_insertion (e); |
628f6a4e | 1547 | } |
3dec4024 | 1548 | } |
9dca2ad5 | 1549 | |
9809a362 JH |
1550 | if (!changed) |
1551 | return; | |
1552 | ||
05549c96 SB |
1553 | /* In the old rtl CFG API, it was OK to insert control flow on an |
1554 | edge, apparently? In cfglayout mode, this will *not* work, and | |
1555 | the caller is responsible for making sure that control flow is | |
1556 | valid at all times. */ | |
1557 | if (current_ir_type () == IR_RTL_CFGLAYOUT) | |
1558 | return; | |
1559 | ||
9809a362 JH |
1560 | blocks = sbitmap_alloc (last_basic_block); |
1561 | sbitmap_zero (blocks); | |
1562 | FOR_EACH_BB (bb) | |
1563 | if (bb->aux) | |
1564 | { | |
c22cacf3 | 1565 | SET_BIT (blocks, bb->index); |
9809a362 JH |
1566 | /* Check for forgotten bb->aux values before commit_edge_insertions |
1567 | call. */ | |
341c100f | 1568 | gcc_assert (bb->aux == &bb->aux); |
9809a362 JH |
1569 | bb->aux = NULL; |
1570 | } | |
1571 | find_many_sub_basic_blocks (blocks); | |
1572 | sbitmap_free (blocks); | |
ca6c03ca JH |
1573 | } |
1574 | \f | |
6fb5fa3c | 1575 | |
f470c378 ZD |
1576 | /* Print out RTL-specific basic block information (live information |
1577 | at start and end). */ | |
ca6c03ca | 1578 | |
10e9fecc | 1579 | static void |
726a989a | 1580 | rtl_dump_bb (basic_block bb, FILE *outf, int indent, int flags ATTRIBUTE_UNUSED) |
ca6c03ca JH |
1581 | { |
1582 | rtx insn; | |
1583 | rtx last; | |
f470c378 | 1584 | char *s_indent; |
ca6c03ca | 1585 | |
ae50c0cb | 1586 | s_indent = (char *) alloca ((size_t) indent + 1); |
400e39e3 | 1587 | memset (s_indent, ' ', (size_t) indent); |
f470c378 | 1588 | s_indent[indent] = '\0'; |
6fb5fa3c DB |
1589 | |
1590 | if (df) | |
1591 | { | |
1592 | df_dump_top (bb, outf); | |
1593 | putc ('\n', outf); | |
1594 | } | |
ca6c03ca | 1595 | |
a813c111 | 1596 | for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last; |
ca6c03ca JH |
1597 | insn = NEXT_INSN (insn)) |
1598 | print_rtl_single (outf, insn); | |
1599 | ||
6fb5fa3c DB |
1600 | if (df) |
1601 | { | |
1602 | df_dump_bottom (bb, outf); | |
1603 | putc ('\n', outf); | |
1604 | } | |
1605 | ||
ca6c03ca JH |
1606 | } |
1607 | \f | |
1608 | /* Like print_rtl, but also print out live information for the start of each | |
1609 | basic block. */ | |
1610 | ||
1611 | void | |
22ea9ec0 | 1612 | print_rtl_with_bb (FILE *outf, const_rtx rtx_first) |
ca6c03ca | 1613 | { |
22ea9ec0 | 1614 | const_rtx tmp_rtx; |
ca6c03ca JH |
1615 | if (rtx_first == 0) |
1616 | fprintf (outf, "(nil)\n"); | |
1617 | else | |
1618 | { | |
ca6c03ca JH |
1619 | enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB }; |
1620 | int max_uid = get_max_uid (); | |
5ed6ace5 MD |
1621 | basic_block *start = XCNEWVEC (basic_block, max_uid); |
1622 | basic_block *end = XCNEWVEC (basic_block, max_uid); | |
1623 | enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid); | |
ca6c03ca | 1624 | |
e0082a72 ZD |
1625 | basic_block bb; |
1626 | ||
6fb5fa3c DB |
1627 | if (df) |
1628 | df_dump_start (outf); | |
1629 | ||
e0082a72 | 1630 | FOR_EACH_BB_REVERSE (bb) |
ca6c03ca | 1631 | { |
ca6c03ca JH |
1632 | rtx x; |
1633 | ||
a813c111 SB |
1634 | start[INSN_UID (BB_HEAD (bb))] = bb; |
1635 | end[INSN_UID (BB_END (bb))] = bb; | |
1636 | for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x)) | |
ca6c03ca JH |
1637 | { |
1638 | enum bb_state state = IN_MULTIPLE_BB; | |
5f0d2358 | 1639 | |
ca6c03ca JH |
1640 | if (in_bb_p[INSN_UID (x)] == NOT_IN_BB) |
1641 | state = IN_ONE_BB; | |
1642 | in_bb_p[INSN_UID (x)] = state; | |
1643 | ||
a813c111 | 1644 | if (x == BB_END (bb)) |
ca6c03ca JH |
1645 | break; |
1646 | } | |
1647 | } | |
1648 | ||
1649 | for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx)) | |
1650 | { | |
1651 | int did_output; | |
ca6c03ca JH |
1652 | if ((bb = start[INSN_UID (tmp_rtx)]) != NULL) |
1653 | { | |
6fb5fa3c DB |
1654 | edge e; |
1655 | edge_iterator ei; | |
1656 | ||
1657 | fprintf (outf, ";; Start of basic block ("); | |
1658 | FOR_EACH_EDGE (e, ei, bb->preds) | |
1659 | fprintf (outf, " %d", e->src->index); | |
1660 | fprintf (outf, ") -> %d\n", bb->index); | |
1661 | ||
1662 | if (df) | |
1663 | { | |
1664 | df_dump_top (bb, outf); | |
1665 | putc ('\n', outf); | |
1666 | } | |
e9ec5c6b SB |
1667 | FOR_EACH_EDGE (e, ei, bb->preds) |
1668 | { | |
1669 | fputs (";; Pred edge ", outf); | |
1670 | dump_edge_info (outf, e, 0); | |
1671 | fputc ('\n', outf); | |
1672 | } | |
ca6c03ca JH |
1673 | } |
1674 | ||
1675 | if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB | |
4b4bf941 JQ |
1676 | && !NOTE_P (tmp_rtx) |
1677 | && !BARRIER_P (tmp_rtx)) | |
ca6c03ca JH |
1678 | fprintf (outf, ";; Insn is not within a basic block\n"); |
1679 | else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB) | |
1680 | fprintf (outf, ";; Insn is in multiple basic blocks\n"); | |
1681 | ||
1682 | did_output = print_rtl_single (outf, tmp_rtx); | |
1683 | ||
1684 | if ((bb = end[INSN_UID (tmp_rtx)]) != NULL) | |
1685 | { | |
6fb5fa3c DB |
1686 | edge e; |
1687 | edge_iterator ei; | |
1688 | ||
1689 | fprintf (outf, ";; End of basic block %d -> (", bb->index); | |
1690 | FOR_EACH_EDGE (e, ei, bb->succs) | |
1691 | fprintf (outf, " %d", e->dest->index); | |
1692 | fprintf (outf, ")\n"); | |
1693 | ||
1694 | if (df) | |
1695 | { | |
1696 | df_dump_bottom (bb, outf); | |
1697 | putc ('\n', outf); | |
1698 | } | |
ca6c03ca | 1699 | putc ('\n', outf); |
e9ec5c6b SB |
1700 | FOR_EACH_EDGE (e, ei, bb->succs) |
1701 | { | |
1702 | fputs (";; Succ edge ", outf); | |
1703 | dump_edge_info (outf, e, 1); | |
1704 | fputc ('\n', outf); | |
1705 | } | |
ca6c03ca | 1706 | } |
ca6c03ca JH |
1707 | if (did_output) |
1708 | putc ('\n', outf); | |
1709 | } | |
1710 | ||
1711 | free (start); | |
1712 | free (end); | |
1713 | free (in_bb_p); | |
1714 | } | |
1715 | ||
cb91fab0 | 1716 | if (crtl->epilogue_delay_list != 0) |
ca6c03ca JH |
1717 | { |
1718 | fprintf (outf, "\n;; Insns in epilogue delay list:\n\n"); | |
cb91fab0 | 1719 | for (tmp_rtx = crtl->epilogue_delay_list; tmp_rtx != 0; |
ca6c03ca JH |
1720 | tmp_rtx = XEXP (tmp_rtx, 1)) |
1721 | print_rtl_single (outf, XEXP (tmp_rtx, 0)); | |
1722 | } | |
1723 | } | |
1724 | \f | |
b446e5a2 | 1725 | void |
d329e058 | 1726 | update_br_prob_note (basic_block bb) |
b446e5a2 JH |
1727 | { |
1728 | rtx note; | |
4b4bf941 | 1729 | if (!JUMP_P (BB_END (bb))) |
b446e5a2 | 1730 | return; |
a813c111 | 1731 | note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX); |
b446e5a2 JH |
1732 | if (!note || INTVAL (XEXP (note, 0)) == BRANCH_EDGE (bb)->probability) |
1733 | return; | |
1734 | XEXP (note, 0) = GEN_INT (BRANCH_EDGE (bb)->probability); | |
1735 | } | |
96370780 MK |
1736 | |
1737 | /* Get the last insn associated with block BB (that includes barriers and | |
1738 | tablejumps after BB). */ | |
1739 | rtx | |
1740 | get_last_bb_insn (basic_block bb) | |
1741 | { | |
1742 | rtx tmp; | |
1743 | rtx end = BB_END (bb); | |
1744 | ||
1745 | /* Include any jump table following the basic block. */ | |
1746 | if (tablejump_p (end, NULL, &tmp)) | |
1747 | end = tmp; | |
1748 | ||
1749 | /* Include any barriers that may follow the basic block. */ | |
1e211590 | 1750 | tmp = next_nonnote_insn_bb (end); |
96370780 MK |
1751 | while (tmp && BARRIER_P (tmp)) |
1752 | { | |
1753 | end = tmp; | |
1e211590 | 1754 | tmp = next_nonnote_insn_bb (end); |
96370780 MK |
1755 | } |
1756 | ||
1757 | return end; | |
1758 | } | |
b446e5a2 | 1759 | \f |
10e9fecc JH |
1760 | /* Verify the CFG and RTL consistency common for both underlying RTL and |
1761 | cfglayout RTL. | |
ca6c03ca JH |
1762 | |
1763 | Currently it does following checks: | |
1764 | ||
ca6c03ca | 1765 | - overlapping of basic blocks |
9eab6785 | 1766 | - insns with wrong BLOCK_FOR_INSN pointers |
ca6c03ca | 1767 | - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note) |
f63d1bf7 | 1768 | - tails of basic blocks (ensure that boundary is necessary) |
ca6c03ca JH |
1769 | - scans body of the basic block for JUMP_INSN, CODE_LABEL |
1770 | and NOTE_INSN_BASIC_BLOCK | |
750054a2 | 1771 | - verify that no fall_thru edge crosses hot/cold partition boundaries |
9eab6785 | 1772 | - verify that there are no pending RTL branch predictions |
ca6c03ca JH |
1773 | |
1774 | In future it can be extended check a lot of other stuff as well | |
1775 | (reachability of basic blocks, life information, etc. etc.). */ | |
f470c378 | 1776 | |
10e9fecc | 1777 | static int |
d329e058 | 1778 | rtl_verify_flow_info_1 (void) |
ca6c03ca | 1779 | { |
ca6c03ca | 1780 | rtx x; |
10e9fecc | 1781 | int err = 0; |
94eb5ddb | 1782 | basic_block bb; |
ca6c03ca | 1783 | |
9eab6785 | 1784 | /* Check the general integrity of the basic blocks. */ |
e0082a72 | 1785 | FOR_EACH_BB_REVERSE (bb) |
ca6c03ca | 1786 | { |
9eab6785 | 1787 | rtx insn; |
5f0d2358 | 1788 | |
5e2d947c JH |
1789 | if (!(bb->flags & BB_RTL)) |
1790 | { | |
1791 | error ("BB_RTL flag not set for block %d", bb->index); | |
1792 | err = 1; | |
1793 | } | |
1794 | ||
9eab6785 | 1795 | FOR_BB_INSNS (bb, insn) |
8ce9fd5d | 1796 | if (BLOCK_FOR_INSN (insn) != bb) |
9eab6785 SB |
1797 | { |
1798 | error ("insn %d basic block pointer is %d, should be %d", | |
1799 | INSN_UID (insn), | |
1800 | BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0, | |
1801 | bb->index); | |
1802 | err = 1; | |
1803 | } | |
a7b87f73 ZD |
1804 | |
1805 | for (insn = bb->il.rtl->header; insn; insn = NEXT_INSN (insn)) | |
1806 | if (!BARRIER_P (insn) | |
1807 | && BLOCK_FOR_INSN (insn) != NULL) | |
1808 | { | |
1809 | error ("insn %d in header of bb %d has non-NULL basic block", | |
1810 | INSN_UID (insn), bb->index); | |
1811 | err = 1; | |
1812 | } | |
1813 | for (insn = bb->il.rtl->footer; insn; insn = NEXT_INSN (insn)) | |
1814 | if (!BARRIER_P (insn) | |
1815 | && BLOCK_FOR_INSN (insn) != NULL) | |
1816 | { | |
1817 | error ("insn %d in footer of bb %d has non-NULL basic block", | |
1818 | INSN_UID (insn), bb->index); | |
1819 | err = 1; | |
1820 | } | |
ca6c03ca JH |
1821 | } |
1822 | ||
1823 | /* Now check the basic blocks (boundaries etc.) */ | |
e0082a72 | 1824 | FOR_EACH_BB_REVERSE (bb) |
ca6c03ca | 1825 | { |
3dec4024 | 1826 | int n_fallthru = 0, n_eh = 0, n_call = 0, n_abnormal = 0, n_branch = 0; |
3cf54412 | 1827 | edge e, fallthru = NULL; |
5a1a3e5e | 1828 | rtx note; |
628f6a4e | 1829 | edge_iterator ei; |
ca6c03ca | 1830 | |
2085a21f | 1831 | if (JUMP_P (BB_END (bb)) |
a813c111 | 1832 | && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX)) |
628f6a4e | 1833 | && EDGE_COUNT (bb->succs) >= 2 |
a813c111 | 1834 | && any_condjump_p (BB_END (bb))) |
5a1a3e5e | 1835 | { |
e53de54d JH |
1836 | if (INTVAL (XEXP (note, 0)) != BRANCH_EDGE (bb)->probability |
1837 | && profile_status != PROFILE_ABSENT) | |
5a1a3e5e | 1838 | { |
0108ae51 | 1839 | error ("verify_flow_info: REG_BR_PROB does not match cfg %wi %i", |
5a1a3e5e JH |
1840 | INTVAL (XEXP (note, 0)), BRANCH_EDGE (bb)->probability); |
1841 | err = 1; | |
1842 | } | |
1843 | } | |
628f6a4e | 1844 | FOR_EACH_EDGE (e, ei, bb->succs) |
ca6c03ca | 1845 | { |
ca6c03ca | 1846 | if (e->flags & EDGE_FALLTHRU) |
750054a2 CT |
1847 | { |
1848 | n_fallthru++, fallthru = e; | |
bd454efd | 1849 | if ((e->flags & EDGE_CROSSING) |
076c7ab8 | 1850 | || (BB_PARTITION (e->src) != BB_PARTITION (e->dest) |
9fb32434 CT |
1851 | && e->src != ENTRY_BLOCK_PTR |
1852 | && e->dest != EXIT_BLOCK_PTR)) | |
c22cacf3 | 1853 | { |
ab532386 | 1854 | error ("fallthru edge crosses section boundary (bb %i)", |
750054a2 CT |
1855 | e->src->index); |
1856 | err = 1; | |
1857 | } | |
1858 | } | |
3dec4024 | 1859 | |
65f43cdf ZD |
1860 | if ((e->flags & ~(EDGE_DFS_BACK |
1861 | | EDGE_CAN_FALLTHRU | |
1862 | | EDGE_IRREDUCIBLE_LOOP | |
9beb1c84 CT |
1863 | | EDGE_LOOP_EXIT |
1864 | | EDGE_CROSSING)) == 0) | |
3dec4024 JH |
1865 | n_branch++; |
1866 | ||
1867 | if (e->flags & EDGE_ABNORMAL_CALL) | |
1868 | n_call++; | |
1869 | ||
1870 | if (e->flags & EDGE_EH) | |
1871 | n_eh++; | |
1872 | else if (e->flags & EDGE_ABNORMAL) | |
1873 | n_abnormal++; | |
ca6c03ca | 1874 | } |
5f0d2358 | 1875 | |
a813c111 SB |
1876 | if (n_eh && GET_CODE (PATTERN (BB_END (bb))) != RESX |
1877 | && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX)) | |
3dec4024 | 1878 | { |
ab532386 | 1879 | error ("missing REG_EH_REGION note in the end of bb %i", bb->index); |
3dec4024 JH |
1880 | err = 1; |
1881 | } | |
1882 | if (n_branch | |
4b4bf941 | 1883 | && (!JUMP_P (BB_END (bb)) |
a813c111 SB |
1884 | || (n_branch > 1 && (any_uncondjump_p (BB_END (bb)) |
1885 | || any_condjump_p (BB_END (bb)))))) | |
3dec4024 | 1886 | { |
ab532386 | 1887 | error ("too many outgoing branch edges from bb %i", bb->index); |
3dec4024 JH |
1888 | err = 1; |
1889 | } | |
a813c111 | 1890 | if (n_fallthru && any_uncondjump_p (BB_END (bb))) |
3dec4024 | 1891 | { |
ab532386 | 1892 | error ("fallthru edge after unconditional jump %i", bb->index); |
3dec4024 JH |
1893 | err = 1; |
1894 | } | |
a813c111 | 1895 | if (n_branch != 1 && any_uncondjump_p (BB_END (bb))) |
3dec4024 | 1896 | { |
ab532386 | 1897 | error ("wrong amount of branch edges after unconditional jump %i", bb->index); |
3dec4024 JH |
1898 | err = 1; |
1899 | } | |
a813c111 | 1900 | if (n_branch != 1 && any_condjump_p (BB_END (bb)) |
c11fd0b2 | 1901 | && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest)) |
3dec4024 | 1902 | { |
c11fd0b2 RG |
1903 | error ("wrong amount of branch edges after conditional jump %i", |
1904 | bb->index); | |
3dec4024 JH |
1905 | err = 1; |
1906 | } | |
4b4bf941 | 1907 | if (n_call && !CALL_P (BB_END (bb))) |
3dec4024 | 1908 | { |
ab532386 | 1909 | error ("call edges for non-call insn in bb %i", bb->index); |
3dec4024 JH |
1910 | err = 1; |
1911 | } | |
1912 | if (n_abnormal | |
4b4bf941 JQ |
1913 | && (!CALL_P (BB_END (bb)) && n_call != n_abnormal) |
1914 | && (!JUMP_P (BB_END (bb)) | |
a813c111 SB |
1915 | || any_condjump_p (BB_END (bb)) |
1916 | || any_uncondjump_p (BB_END (bb)))) | |
3dec4024 | 1917 | { |
ab532386 | 1918 | error ("abnormal edges for no purpose in bb %i", bb->index); |
3dec4024 JH |
1919 | err = 1; |
1920 | } | |
f87c27b4 | 1921 | |
a813c111 | 1922 | for (x = BB_HEAD (bb); x != NEXT_INSN (BB_END (bb)); x = NEXT_INSN (x)) |
0ca541aa | 1923 | /* We may have a barrier inside a basic block before dead code |
9524880c HPN |
1924 | elimination. There is no BLOCK_FOR_INSN field in a barrier. */ |
1925 | if (!BARRIER_P (x) && BLOCK_FOR_INSN (x) != bb) | |
5f0d2358 RK |
1926 | { |
1927 | debug_rtx (x); | |
1928 | if (! BLOCK_FOR_INSN (x)) | |
1929 | error | |
1930 | ("insn %d inside basic block %d but block_for_insn is NULL", | |
0b17ab2f | 1931 | INSN_UID (x), bb->index); |
5f0d2358 RK |
1932 | else |
1933 | error | |
1934 | ("insn %d inside basic block %d but block_for_insn is %i", | |
0b17ab2f | 1935 | INSN_UID (x), bb->index, BLOCK_FOR_INSN (x)->index); |
5f0d2358 RK |
1936 | |
1937 | err = 1; | |
1938 | } | |
ca6c03ca JH |
1939 | |
1940 | /* OK pointers are correct. Now check the header of basic | |
c22cacf3 | 1941 | block. It ought to contain optional CODE_LABEL followed |
ca6c03ca | 1942 | by NOTE_BASIC_BLOCK. */ |
a813c111 | 1943 | x = BB_HEAD (bb); |
4b4bf941 | 1944 | if (LABEL_P (x)) |
ca6c03ca | 1945 | { |
a813c111 | 1946 | if (BB_END (bb) == x) |
ca6c03ca JH |
1947 | { |
1948 | error ("NOTE_INSN_BASIC_BLOCK is missing for block %d", | |
0b17ab2f | 1949 | bb->index); |
ca6c03ca JH |
1950 | err = 1; |
1951 | } | |
5f0d2358 | 1952 | |
ca6c03ca JH |
1953 | x = NEXT_INSN (x); |
1954 | } | |
5f0d2358 | 1955 | |
ca6c03ca JH |
1956 | if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb) |
1957 | { | |
1958 | error ("NOTE_INSN_BASIC_BLOCK is missing for block %d", | |
0b17ab2f | 1959 | bb->index); |
ca6c03ca JH |
1960 | err = 1; |
1961 | } | |
1962 | ||
a813c111 | 1963 | if (BB_END (bb) == x) |
49243025 | 1964 | /* Do checks for empty blocks here. */ |
5f0d2358 | 1965 | ; |
ca6c03ca | 1966 | else |
5f0d2358 RK |
1967 | for (x = NEXT_INSN (x); x; x = NEXT_INSN (x)) |
1968 | { | |
1969 | if (NOTE_INSN_BASIC_BLOCK_P (x)) | |
1970 | { | |
1971 | error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d", | |
0b17ab2f | 1972 | INSN_UID (x), bb->index); |
5f0d2358 RK |
1973 | err = 1; |
1974 | } | |
1975 | ||
a813c111 | 1976 | if (x == BB_END (bb)) |
5f0d2358 | 1977 | break; |
ca6c03ca | 1978 | |
83fd323c | 1979 | if (control_flow_insn_p (x)) |
5f0d2358 | 1980 | { |
0b17ab2f | 1981 | error ("in basic block %d:", bb->index); |
5f0d2358 RK |
1982 | fatal_insn ("flow control insn inside a basic block", x); |
1983 | } | |
1984 | } | |
ca6c03ca JH |
1985 | } |
1986 | ||
10e9fecc | 1987 | /* Clean up. */ |
10e9fecc JH |
1988 | return err; |
1989 | } | |
5f0d2358 | 1990 | |
10e9fecc JH |
1991 | /* Verify the CFG and RTL consistency common for both underlying RTL and |
1992 | cfglayout RTL. | |
5f0d2358 | 1993 | |
10e9fecc JH |
1994 | Currently it does following checks: |
1995 | - all checks of rtl_verify_flow_info_1 | |
9eab6785 | 1996 | - test head/end pointers |
10e9fecc JH |
1997 | - check that all insns are in the basic blocks |
1998 | (except the switch handling code, barriers and notes) | |
1999 | - check that all returns are followed by barriers | |
2000 | - check that all fallthru edge points to the adjacent blocks. */ | |
9eab6785 | 2001 | |
10e9fecc | 2002 | static int |
d329e058 | 2003 | rtl_verify_flow_info (void) |
10e9fecc JH |
2004 | { |
2005 | basic_block bb; | |
2006 | int err = rtl_verify_flow_info_1 (); | |
2007 | rtx x; | |
9eab6785 SB |
2008 | rtx last_head = get_last_insn (); |
2009 | basic_block *bb_info; | |
10e9fecc JH |
2010 | int num_bb_notes; |
2011 | const rtx rtx_first = get_insns (); | |
2012 | basic_block last_bb_seen = ENTRY_BLOCK_PTR, curr_bb = NULL; | |
9eab6785 SB |
2013 | const int max_uid = get_max_uid (); |
2014 | ||
2015 | bb_info = XCNEWVEC (basic_block, max_uid); | |
ca6c03ca | 2016 | |
10e9fecc JH |
2017 | FOR_EACH_BB_REVERSE (bb) |
2018 | { | |
2019 | edge e; | |
628f6a4e | 2020 | edge_iterator ei; |
9eab6785 SB |
2021 | rtx head = BB_HEAD (bb); |
2022 | rtx end = BB_END (bb); | |
628f6a4e | 2023 | |
9eab6785 | 2024 | for (x = last_head; x != NULL_RTX; x = PREV_INSN (x)) |
a7b87f73 ZD |
2025 | { |
2026 | /* Verify the end of the basic block is in the INSN chain. */ | |
2027 | if (x == end) | |
2028 | break; | |
2029 | ||
2030 | /* And that the code outside of basic blocks has NULL bb field. */ | |
2031 | if (!BARRIER_P (x) | |
2032 | && BLOCK_FOR_INSN (x) != NULL) | |
2033 | { | |
2034 | error ("insn %d outside of basic blocks has non-NULL bb field", | |
2035 | INSN_UID (x)); | |
2036 | err = 1; | |
2037 | } | |
2038 | } | |
9eab6785 SB |
2039 | |
2040 | if (!x) | |
2041 | { | |
2042 | error ("end insn %d for block %d not found in the insn stream", | |
2043 | INSN_UID (end), bb->index); | |
2044 | err = 1; | |
2045 | } | |
2046 | ||
2047 | /* Work backwards from the end to the head of the basic block | |
2048 | to verify the head is in the RTL chain. */ | |
2049 | for (; x != NULL_RTX; x = PREV_INSN (x)) | |
a00d11f0 | 2050 | { |
9eab6785 SB |
2051 | /* While walking over the insn chain, verify insns appear |
2052 | in only one basic block. */ | |
2053 | if (bb_info[INSN_UID (x)] != NULL) | |
2054 | { | |
2055 | error ("insn %d is in multiple basic blocks (%d and %d)", | |
2056 | INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index); | |
2057 | err = 1; | |
2058 | } | |
2059 | ||
2060 | bb_info[INSN_UID (x)] = bb; | |
2061 | ||
2062 | if (x == head) | |
2063 | break; | |
2064 | } | |
2065 | if (!x) | |
2066 | { | |
2067 | error ("head insn %d for block %d not found in the insn stream", | |
2068 | INSN_UID (head), bb->index); | |
a00d11f0 JH |
2069 | err = 1; |
2070 | } | |
2071 | ||
a7b87f73 | 2072 | last_head = PREV_INSN (x); |
9eab6785 | 2073 | |
628f6a4e | 2074 | FOR_EACH_EDGE (e, ei, bb->succs) |
10e9fecc JH |
2075 | if (e->flags & EDGE_FALLTHRU) |
2076 | break; | |
2077 | if (!e) | |
2078 | { | |
2079 | rtx insn; | |
2080 | ||
2081 | /* Ensure existence of barrier in BB with no fallthru edges. */ | |
468059bc DD |
2082 | for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn)) |
2083 | { | |
2084 | if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn)) | |
10e9fecc JH |
2085 | { |
2086 | error ("missing barrier after block %i", bb->index); | |
2087 | err = 1; | |
2088 | break; | |
2089 | } | |
468059bc DD |
2090 | if (BARRIER_P (insn)) |
2091 | break; | |
2092 | } | |
10e9fecc JH |
2093 | } |
2094 | else if (e->src != ENTRY_BLOCK_PTR | |
2095 | && e->dest != EXIT_BLOCK_PTR) | |
c22cacf3 | 2096 | { |
10e9fecc JH |
2097 | rtx insn; |
2098 | ||
2099 | if (e->src->next_bb != e->dest) | |
2100 | { | |
2101 | error | |
2102 | ("verify_flow_info: Incorrect blocks for fallthru %i->%i", | |
2103 | e->src->index, e->dest->index); | |
2104 | err = 1; | |
2105 | } | |
2106 | else | |
a813c111 | 2107 | for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest); |
10e9fecc | 2108 | insn = NEXT_INSN (insn)) |
6be85b25 | 2109 | if (BARRIER_P (insn) || INSN_P (insn)) |
10e9fecc JH |
2110 | { |
2111 | error ("verify_flow_info: Incorrect fallthru %i->%i", | |
2112 | e->src->index, e->dest->index); | |
2113 | fatal_insn ("wrong insn in the fallthru edge", insn); | |
2114 | err = 1; | |
2115 | } | |
c22cacf3 | 2116 | } |
10e9fecc | 2117 | } |
ca6c03ca | 2118 | |
a7b87f73 ZD |
2119 | for (x = last_head; x != NULL_RTX; x = PREV_INSN (x)) |
2120 | { | |
2121 | /* Check that the code before the first basic block has NULL | |
2122 | bb field. */ | |
2123 | if (!BARRIER_P (x) | |
2124 | && BLOCK_FOR_INSN (x) != NULL) | |
2125 | { | |
2126 | error ("insn %d outside of basic blocks has non-NULL bb field", | |
2127 | INSN_UID (x)); | |
2128 | err = 1; | |
2129 | } | |
2130 | } | |
9eab6785 SB |
2131 | free (bb_info); |
2132 | ||
ca6c03ca | 2133 | num_bb_notes = 0; |
e0082a72 ZD |
2134 | last_bb_seen = ENTRY_BLOCK_PTR; |
2135 | ||
5f0d2358 | 2136 | for (x = rtx_first; x; x = NEXT_INSN (x)) |
ca6c03ca JH |
2137 | { |
2138 | if (NOTE_INSN_BASIC_BLOCK_P (x)) | |
2139 | { | |
bf77398c | 2140 | bb = NOTE_BASIC_BLOCK (x); |
5f0d2358 | 2141 | |
ca6c03ca | 2142 | num_bb_notes++; |
e0082a72 | 2143 | if (bb != last_bb_seen->next_bb) |
10e9fecc | 2144 | internal_error ("basic blocks not laid down consecutively"); |
ca6c03ca | 2145 | |
10e9fecc | 2146 | curr_bb = last_bb_seen = bb; |
ca6c03ca JH |
2147 | } |
2148 | ||
10e9fecc | 2149 | if (!curr_bb) |
ca6c03ca JH |
2150 | { |
2151 | switch (GET_CODE (x)) | |
2152 | { | |
2153 | case BARRIER: | |
2154 | case NOTE: | |
2155 | break; | |
2156 | ||
2157 | case CODE_LABEL: | |
666c27b9 | 2158 | /* An addr_vec is placed outside any basic block. */ |
ca6c03ca | 2159 | if (NEXT_INSN (x) |
481683e1 | 2160 | && JUMP_TABLE_DATA_P (NEXT_INSN (x))) |
5f0d2358 | 2161 | x = NEXT_INSN (x); |
ca6c03ca JH |
2162 | |
2163 | /* But in any case, non-deletable labels can appear anywhere. */ | |
2164 | break; | |
2165 | ||
2166 | default: | |
1f978f5f | 2167 | fatal_insn ("insn outside basic block", x); |
ca6c03ca JH |
2168 | } |
2169 | } | |
2170 | ||
26cae194 | 2171 | if (JUMP_P (x) |
ca6c03ca | 2172 | && returnjump_p (x) && ! condjump_p (x) |
15eb3a2e | 2173 | && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x)))) |
1f978f5f | 2174 | fatal_insn ("return not followed by barrier", x); |
a813c111 | 2175 | if (curr_bb && x == BB_END (curr_bb)) |
10e9fecc | 2176 | curr_bb = NULL; |
ca6c03ca JH |
2177 | } |
2178 | ||
24bd1a0b | 2179 | if (num_bb_notes != n_basic_blocks - NUM_FIXED_BLOCKS) |
ca6c03ca | 2180 | internal_error |
0b17ab2f RH |
2181 | ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)", |
2182 | num_bb_notes, n_basic_blocks); | |
ca6c03ca | 2183 | |
10e9fecc | 2184 | return err; |
ca6c03ca JH |
2185 | } |
2186 | \f | |
eaec9b3d | 2187 | /* Assume that the preceding pass has possibly eliminated jump instructions |
ca6c03ca JH |
2188 | or converted the unconditional jumps. Eliminate the edges from CFG. |
2189 | Return true if any edges are eliminated. */ | |
2190 | ||
2191 | bool | |
d329e058 | 2192 | purge_dead_edges (basic_block bb) |
ca6c03ca | 2193 | { |
628f6a4e | 2194 | edge e; |
a813c111 | 2195 | rtx insn = BB_END (bb), note; |
ca6c03ca | 2196 | bool purged = false; |
628f6a4e BE |
2197 | bool found; |
2198 | edge_iterator ei; | |
ca6c03ca | 2199 | |
b5b8b0ac AO |
2200 | if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb)) |
2201 | do | |
2202 | insn = PREV_INSN (insn); | |
2203 | while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb)); | |
2204 | ||
70da1d03 | 2205 | /* If this instruction cannot trap, remove REG_EH_REGION notes. */ |
4b4bf941 | 2206 | if (NONJUMP_INSN_P (insn) |
70da1d03 JH |
2207 | && (note = find_reg_note (insn, REG_EH_REGION, NULL))) |
2208 | { | |
2209 | rtx eqnote; | |
2210 | ||
2211 | if (! may_trap_p (PATTERN (insn)) | |
2212 | || ((eqnote = find_reg_equal_equiv_note (insn)) | |
2213 | && ! may_trap_p (XEXP (eqnote, 0)))) | |
2214 | remove_note (insn, note); | |
2215 | } | |
2216 | ||
546c093e | 2217 | /* Cleanup abnormal edges caused by exceptions or non-local gotos. */ |
628f6a4e | 2218 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
546c093e | 2219 | { |
e5f9a909 JW |
2220 | /* There are three types of edges we need to handle correctly here: EH |
2221 | edges, abnormal call EH edges, and abnormal call non-EH edges. The | |
2222 | latter can appear when nonlocal gotos are used. */ | |
2223 | if (e->flags & EDGE_EH) | |
546c093e | 2224 | { |
b5b8b0ac | 2225 | if (can_throw_internal (insn) |
e5f9a909 JW |
2226 | /* If this is a call edge, verify that this is a call insn. */ |
2227 | && (! (e->flags & EDGE_ABNORMAL_CALL) | |
b5b8b0ac | 2228 | || CALL_P (insn))) |
628f6a4e BE |
2229 | { |
2230 | ei_next (&ei); | |
2231 | continue; | |
2232 | } | |
546c093e | 2233 | } |
e5f9a909 | 2234 | else if (e->flags & EDGE_ABNORMAL_CALL) |
546c093e | 2235 | { |
b5b8b0ac | 2236 | if (CALL_P (insn) |
e5f9a909 JW |
2237 | && (! (note = find_reg_note (insn, REG_EH_REGION, NULL)) |
2238 | || INTVAL (XEXP (note, 0)) >= 0)) | |
628f6a4e BE |
2239 | { |
2240 | ei_next (&ei); | |
2241 | continue; | |
2242 | } | |
546c093e RH |
2243 | } |
2244 | else | |
628f6a4e BE |
2245 | { |
2246 | ei_next (&ei); | |
2247 | continue; | |
2248 | } | |
546c093e RH |
2249 | |
2250 | remove_edge (e); | |
6fb5fa3c | 2251 | df_set_bb_dirty (bb); |
546c093e RH |
2252 | purged = true; |
2253 | } | |
5f0d2358 | 2254 | |
4b4bf941 | 2255 | if (JUMP_P (insn)) |
ca6c03ca JH |
2256 | { |
2257 | rtx note; | |
2258 | edge b,f; | |
628f6a4e | 2259 | edge_iterator ei; |
5f0d2358 | 2260 | |
ca6c03ca JH |
2261 | /* We do care only about conditional jumps and simplejumps. */ |
2262 | if (!any_condjump_p (insn) | |
2263 | && !returnjump_p (insn) | |
2264 | && !simplejump_p (insn)) | |
c51d95ec | 2265 | return purged; |
5f0d2358 | 2266 | |
5a1a3e5e JH |
2267 | /* Branch probability/prediction notes are defined only for |
2268 | condjumps. We've possibly turned condjump into simplejump. */ | |
2269 | if (simplejump_p (insn)) | |
2270 | { | |
2271 | note = find_reg_note (insn, REG_BR_PROB, NULL); | |
2272 | if (note) | |
2273 | remove_note (insn, note); | |
2274 | while ((note = find_reg_note (insn, REG_BR_PRED, NULL))) | |
2275 | remove_note (insn, note); | |
2276 | } | |
2277 | ||
628f6a4e | 2278 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
ca6c03ca | 2279 | { |
7fcd7218 JH |
2280 | /* Avoid abnormal flags to leak from computed jumps turned |
2281 | into simplejumps. */ | |
f87c27b4 | 2282 | |
0e1638d4 | 2283 | e->flags &= ~EDGE_ABNORMAL; |
7fcd7218 | 2284 | |
5a566bed MM |
2285 | /* See if this edge is one we should keep. */ |
2286 | if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn)) | |
2287 | /* A conditional jump can fall through into the next | |
2288 | block, so we should keep the edge. */ | |
628f6a4e BE |
2289 | { |
2290 | ei_next (&ei); | |
2291 | continue; | |
2292 | } | |
5f0d2358 | 2293 | else if (e->dest != EXIT_BLOCK_PTR |
a813c111 | 2294 | && BB_HEAD (e->dest) == JUMP_LABEL (insn)) |
5a566bed MM |
2295 | /* If the destination block is the target of the jump, |
2296 | keep the edge. */ | |
628f6a4e BE |
2297 | { |
2298 | ei_next (&ei); | |
2299 | continue; | |
2300 | } | |
5a566bed MM |
2301 | else if (e->dest == EXIT_BLOCK_PTR && returnjump_p (insn)) |
2302 | /* If the destination block is the exit block, and this | |
2303 | instruction is a return, then keep the edge. */ | |
628f6a4e BE |
2304 | { |
2305 | ei_next (&ei); | |
2306 | continue; | |
2307 | } | |
5a566bed MM |
2308 | else if ((e->flags & EDGE_EH) && can_throw_internal (insn)) |
2309 | /* Keep the edges that correspond to exceptions thrown by | |
0b75beaa EB |
2310 | this instruction and rematerialize the EDGE_ABNORMAL |
2311 | flag we just cleared above. */ | |
2312 | { | |
2313 | e->flags |= EDGE_ABNORMAL; | |
628f6a4e | 2314 | ei_next (&ei); |
0b75beaa EB |
2315 | continue; |
2316 | } | |
5f0d2358 | 2317 | |
5a566bed | 2318 | /* We do not need this edge. */ |
6fb5fa3c | 2319 | df_set_bb_dirty (bb); |
ca6c03ca JH |
2320 | purged = true; |
2321 | remove_edge (e); | |
2322 | } | |
5f0d2358 | 2323 | |
628f6a4e | 2324 | if (EDGE_COUNT (bb->succs) == 0 || !purged) |
c51d95ec | 2325 | return purged; |
5f0d2358 | 2326 | |
c263766c RH |
2327 | if (dump_file) |
2328 | fprintf (dump_file, "Purged edges from bb %i\n", bb->index); | |
5f0d2358 | 2329 | |
ca6c03ca JH |
2330 | if (!optimize) |
2331 | return purged; | |
2332 | ||
2333 | /* Redistribute probabilities. */ | |
c5cbcccf | 2334 | if (single_succ_p (bb)) |
ca6c03ca | 2335 | { |
c5cbcccf ZD |
2336 | single_succ_edge (bb)->probability = REG_BR_PROB_BASE; |
2337 | single_succ_edge (bb)->count = bb->count; | |
f87c27b4 | 2338 | } |
ca6c03ca JH |
2339 | else |
2340 | { | |
2341 | note = find_reg_note (insn, REG_BR_PROB, NULL); | |
2342 | if (!note) | |
2343 | return purged; | |
5f0d2358 | 2344 | |
ca6c03ca JH |
2345 | b = BRANCH_EDGE (bb); |
2346 | f = FALLTHRU_EDGE (bb); | |
2347 | b->probability = INTVAL (XEXP (note, 0)); | |
2348 | f->probability = REG_BR_PROB_BASE - b->probability; | |
2349 | b->count = bb->count * b->probability / REG_BR_PROB_BASE; | |
2350 | f->count = bb->count * f->probability / REG_BR_PROB_BASE; | |
2351 | } | |
5f0d2358 | 2352 | |
ca6c03ca JH |
2353 | return purged; |
2354 | } | |
4b4bf941 | 2355 | else if (CALL_P (insn) && SIBLING_CALL_P (insn)) |
1722c2c8 RH |
2356 | { |
2357 | /* First, there should not be any EH or ABCALL edges resulting | |
2358 | from non-local gotos and the like. If there were, we shouldn't | |
2359 | have created the sibcall in the first place. Second, there | |
2360 | should of course never have been a fallthru edge. */ | |
c5cbcccf ZD |
2361 | gcc_assert (single_succ_p (bb)); |
2362 | gcc_assert (single_succ_edge (bb)->flags | |
2363 | == (EDGE_SIBCALL | EDGE_ABNORMAL)); | |
1722c2c8 RH |
2364 | |
2365 | return 0; | |
2366 | } | |
ca6c03ca | 2367 | |
ca6c03ca JH |
2368 | /* If we don't see a jump insn, we don't know exactly why the block would |
2369 | have been broken at this point. Look for a simple, non-fallthru edge, | |
2370 | as these are only created by conditional branches. If we find such an | |
2371 | edge we know that there used to be a jump here and can then safely | |
2372 | remove all non-fallthru edges. */ | |
628f6a4e BE |
2373 | found = false; |
2374 | FOR_EACH_EDGE (e, ei, bb->succs) | |
2375 | if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))) | |
2376 | { | |
2377 | found = true; | |
2378 | break; | |
2379 | } | |
5f0d2358 | 2380 | |
628f6a4e | 2381 | if (!found) |
ca6c03ca | 2382 | return purged; |
5f0d2358 | 2383 | |
2afa8dce DB |
2384 | /* Remove all but the fake and fallthru edges. The fake edge may be |
2385 | the only successor for this block in the case of noreturn | |
2386 | calls. */ | |
628f6a4e | 2387 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
ca6c03ca | 2388 | { |
2afa8dce | 2389 | if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE))) |
c51d95ec | 2390 | { |
6fb5fa3c | 2391 | df_set_bb_dirty (bb); |
c51d95ec JH |
2392 | remove_edge (e); |
2393 | purged = true; | |
2394 | } | |
628f6a4e BE |
2395 | else |
2396 | ei_next (&ei); | |
ca6c03ca | 2397 | } |
5f0d2358 | 2398 | |
c5cbcccf | 2399 | gcc_assert (single_succ_p (bb)); |
5f0d2358 | 2400 | |
c5cbcccf ZD |
2401 | single_succ_edge (bb)->probability = REG_BR_PROB_BASE; |
2402 | single_succ_edge (bb)->count = bb->count; | |
ca6c03ca | 2403 | |
c263766c RH |
2404 | if (dump_file) |
2405 | fprintf (dump_file, "Purged non-fallthru edges from bb %i\n", | |
0b17ab2f | 2406 | bb->index); |
ca6c03ca JH |
2407 | return purged; |
2408 | } | |
2409 | ||
5f0d2358 RK |
2410 | /* Search all basic blocks for potentially dead edges and purge them. Return |
2411 | true if some edge has been eliminated. */ | |
ca6c03ca JH |
2412 | |
2413 | bool | |
25cd19de | 2414 | purge_all_dead_edges (void) |
ca6c03ca | 2415 | { |
e0082a72 | 2416 | int purged = false; |
e0082a72 | 2417 | basic_block bb; |
473fb060 | 2418 | |
e0082a72 | 2419 | FOR_EACH_BB (bb) |
473fb060 | 2420 | { |
e0082a72 | 2421 | bool purged_here = purge_dead_edges (bb); |
5f0d2358 | 2422 | |
473fb060 | 2423 | purged |= purged_here; |
473fb060 | 2424 | } |
5f0d2358 | 2425 | |
ca6c03ca JH |
2426 | return purged; |
2427 | } | |
9ee634e3 JH |
2428 | |
2429 | /* Same as split_block but update cfg_layout structures. */ | |
f470c378 ZD |
2430 | |
2431 | static basic_block | |
d329e058 | 2432 | cfg_layout_split_block (basic_block bb, void *insnp) |
9ee634e3 | 2433 | { |
ae50c0cb | 2434 | rtx insn = (rtx) insnp; |
f470c378 | 2435 | basic_block new_bb = rtl_split_block (bb, insn); |
9ee634e3 | 2436 | |
370369e1 JH |
2437 | new_bb->il.rtl->footer = bb->il.rtl->footer; |
2438 | bb->il.rtl->footer = NULL; | |
9ee634e3 | 2439 | |
f470c378 | 2440 | return new_bb; |
9ee634e3 JH |
2441 | } |
2442 | ||
9ee634e3 | 2443 | /* Redirect Edge to DEST. */ |
6de9cd9a | 2444 | static edge |
d329e058 | 2445 | cfg_layout_redirect_edge_and_branch (edge e, basic_block dest) |
9ee634e3 JH |
2446 | { |
2447 | basic_block src = e->src; | |
6de9cd9a | 2448 | edge ret; |
9ee634e3 | 2449 | |
bc35512f | 2450 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) |
6de9cd9a | 2451 | return NULL; |
bc35512f | 2452 | |
3348b696 | 2453 | if (e->dest == dest) |
6de9cd9a | 2454 | return e; |
bc35512f | 2455 | |
3348b696 | 2456 | if (e->src != ENTRY_BLOCK_PTR |
6de9cd9a | 2457 | && (ret = try_redirect_by_replacing_jump (e, dest, true))) |
f345f21a | 2458 | { |
6fb5fa3c | 2459 | df_set_bb_dirty (src); |
6de9cd9a | 2460 | return ret; |
f345f21a | 2461 | } |
bc35512f JH |
2462 | |
2463 | if (e->src == ENTRY_BLOCK_PTR | |
2464 | && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX)) | |
2465 | { | |
c263766c RH |
2466 | if (dump_file) |
2467 | fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n", | |
bc35512f JH |
2468 | e->src->index, dest->index); |
2469 | ||
6fb5fa3c | 2470 | df_set_bb_dirty (e->src); |
bc35512f | 2471 | redirect_edge_succ (e, dest); |
6de9cd9a | 2472 | return e; |
bc35512f JH |
2473 | } |
2474 | ||
9ee634e3 JH |
2475 | /* Redirect_edge_and_branch may decide to turn branch into fallthru edge |
2476 | in the case the basic block appears to be in sequence. Avoid this | |
2477 | transformation. */ | |
2478 | ||
9ee634e3 JH |
2479 | if (e->flags & EDGE_FALLTHRU) |
2480 | { | |
2481 | /* Redirect any branch edges unified with the fallthru one. */ | |
4b4bf941 | 2482 | if (JUMP_P (BB_END (src)) |
432f982f JH |
2483 | && label_is_jump_target_p (BB_HEAD (e->dest), |
2484 | BB_END (src))) | |
9ee634e3 | 2485 | { |
341c100f | 2486 | edge redirected; |
c22cacf3 | 2487 | |
c263766c RH |
2488 | if (dump_file) |
2489 | fprintf (dump_file, "Fallthru edge unified with branch " | |
432f982f JH |
2490 | "%i->%i redirected to %i\n", |
2491 | e->src->index, e->dest->index, dest->index); | |
2492 | e->flags &= ~EDGE_FALLTHRU; | |
341c100f NS |
2493 | redirected = redirect_branch_edge (e, dest); |
2494 | gcc_assert (redirected); | |
432f982f | 2495 | e->flags |= EDGE_FALLTHRU; |
6fb5fa3c | 2496 | df_set_bb_dirty (e->src); |
6de9cd9a | 2497 | return e; |
9ee634e3 JH |
2498 | } |
2499 | /* In case we are redirecting fallthru edge to the branch edge | |
c22cacf3 | 2500 | of conditional jump, remove it. */ |
628f6a4e | 2501 | if (EDGE_COUNT (src->succs) == 2) |
9ee634e3 | 2502 | { |
03101c6f KH |
2503 | /* Find the edge that is different from E. */ |
2504 | edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e); | |
628f6a4e | 2505 | |
9ee634e3 | 2506 | if (s->dest == dest |
a813c111 SB |
2507 | && any_condjump_p (BB_END (src)) |
2508 | && onlyjump_p (BB_END (src))) | |
2509 | delete_insn (BB_END (src)); | |
9ee634e3 | 2510 | } |
6de9cd9a | 2511 | ret = redirect_edge_succ_nodup (e, dest); |
c263766c RH |
2512 | if (dump_file) |
2513 | fprintf (dump_file, "Fallthru edge %i->%i redirected to %i\n", | |
bc35512f | 2514 | e->src->index, e->dest->index, dest->index); |
9ee634e3 JH |
2515 | } |
2516 | else | |
bc35512f | 2517 | ret = redirect_branch_edge (e, dest); |
9ee634e3 JH |
2518 | |
2519 | /* We don't want simplejumps in the insn stream during cfglayout. */ | |
341c100f | 2520 | gcc_assert (!simplejump_p (BB_END (src))); |
9ee634e3 | 2521 | |
6fb5fa3c | 2522 | df_set_bb_dirty (src); |
9ee634e3 JH |
2523 | return ret; |
2524 | } | |
2525 | ||
2526 | /* Simple wrapper as we always can redirect fallthru edges. */ | |
2527 | static basic_block | |
d329e058 | 2528 | cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest) |
9ee634e3 | 2529 | { |
341c100f NS |
2530 | edge redirected = cfg_layout_redirect_edge_and_branch (e, dest); |
2531 | ||
2532 | gcc_assert (redirected); | |
9ee634e3 JH |
2533 | return NULL; |
2534 | } | |
2535 | ||
f470c378 ZD |
2536 | /* Same as delete_basic_block but update cfg_layout structures. */ |
2537 | ||
9ee634e3 | 2538 | static void |
d329e058 | 2539 | cfg_layout_delete_block (basic_block bb) |
9ee634e3 | 2540 | { |
a813c111 | 2541 | rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints; |
9ee634e3 | 2542 | |
370369e1 | 2543 | if (bb->il.rtl->header) |
9ee634e3 | 2544 | { |
a813c111 | 2545 | next = BB_HEAD (bb); |
9ee634e3 | 2546 | if (prev) |
370369e1 | 2547 | NEXT_INSN (prev) = bb->il.rtl->header; |
9ee634e3 | 2548 | else |
370369e1 JH |
2549 | set_first_insn (bb->il.rtl->header); |
2550 | PREV_INSN (bb->il.rtl->header) = prev; | |
2551 | insn = bb->il.rtl->header; | |
9ee634e3 JH |
2552 | while (NEXT_INSN (insn)) |
2553 | insn = NEXT_INSN (insn); | |
2554 | NEXT_INSN (insn) = next; | |
2555 | PREV_INSN (next) = insn; | |
2556 | } | |
a813c111 | 2557 | next = NEXT_INSN (BB_END (bb)); |
370369e1 | 2558 | if (bb->il.rtl->footer) |
9ee634e3 | 2559 | { |
370369e1 | 2560 | insn = bb->il.rtl->footer; |
bc35512f JH |
2561 | while (insn) |
2562 | { | |
4b4bf941 | 2563 | if (BARRIER_P (insn)) |
bc35512f JH |
2564 | { |
2565 | if (PREV_INSN (insn)) | |
2566 | NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn); | |
2567 | else | |
370369e1 | 2568 | bb->il.rtl->footer = NEXT_INSN (insn); |
bc35512f JH |
2569 | if (NEXT_INSN (insn)) |
2570 | PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn); | |
2571 | } | |
4b4bf941 | 2572 | if (LABEL_P (insn)) |
bc35512f JH |
2573 | break; |
2574 | insn = NEXT_INSN (insn); | |
2575 | } | |
370369e1 | 2576 | if (bb->il.rtl->footer) |
bc35512f | 2577 | { |
a813c111 | 2578 | insn = BB_END (bb); |
370369e1 JH |
2579 | NEXT_INSN (insn) = bb->il.rtl->footer; |
2580 | PREV_INSN (bb->il.rtl->footer) = insn; | |
bc35512f JH |
2581 | while (NEXT_INSN (insn)) |
2582 | insn = NEXT_INSN (insn); | |
2583 | NEXT_INSN (insn) = next; | |
2584 | if (next) | |
2585 | PREV_INSN (next) = insn; | |
2586 | else | |
2587 | set_last_insn (insn); | |
2588 | } | |
9ee634e3 JH |
2589 | } |
2590 | if (bb->next_bb != EXIT_BLOCK_PTR) | |
370369e1 | 2591 | to = &bb->next_bb->il.rtl->header; |
9ee634e3 JH |
2592 | else |
2593 | to = &cfg_layout_function_footer; | |
997de8ed | 2594 | |
9ee634e3 JH |
2595 | rtl_delete_block (bb); |
2596 | ||
2597 | if (prev) | |
2598 | prev = NEXT_INSN (prev); | |
d329e058 | 2599 | else |
9ee634e3 JH |
2600 | prev = get_insns (); |
2601 | if (next) | |
2602 | next = PREV_INSN (next); | |
d329e058 | 2603 | else |
9ee634e3 JH |
2604 | next = get_last_insn (); |
2605 | ||
2606 | if (next && NEXT_INSN (next) != prev) | |
2607 | { | |
2608 | remaints = unlink_insn_chain (prev, next); | |
2609 | insn = remaints; | |
2610 | while (NEXT_INSN (insn)) | |
2611 | insn = NEXT_INSN (insn); | |
2612 | NEXT_INSN (insn) = *to; | |
2613 | if (*to) | |
2614 | PREV_INSN (*to) = insn; | |
2615 | *to = remaints; | |
2616 | } | |
2617 | } | |
2618 | ||
beb235f8 | 2619 | /* Return true when blocks A and B can be safely merged. */ |
b48d0358 | 2620 | |
bc35512f | 2621 | static bool |
b48d0358 | 2622 | cfg_layout_can_merge_blocks_p (basic_block a, basic_block b) |
bc35512f | 2623 | { |
750054a2 CT |
2624 | /* If we are partitioning hot/cold basic blocks, we don't want to |
2625 | mess up unconditional or indirect jumps that cross between hot | |
076c7ab8 ZW |
2626 | and cold sections. |
2627 | ||
8e8d5162 | 2628 | Basic block partitioning may result in some jumps that appear to |
c22cacf3 MS |
2629 | be optimizable (or blocks that appear to be mergeable), but which really |
2630 | must be left untouched (they are required to make it safely across | |
2631 | partition boundaries). See the comments at the top of | |
8e8d5162 CT |
2632 | bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */ |
2633 | ||
87c8b4be | 2634 | if (BB_PARTITION (a) != BB_PARTITION (b)) |
076c7ab8 | 2635 | return false; |
750054a2 | 2636 | |
bc35512f | 2637 | /* There must be exactly one edge in between the blocks. */ |
c5cbcccf ZD |
2638 | return (single_succ_p (a) |
2639 | && single_succ (a) == b | |
2640 | && single_pred_p (b) == 1 | |
628f6a4e | 2641 | && a != b |
bc35512f | 2642 | /* Must be simple edge. */ |
c5cbcccf | 2643 | && !(single_succ_edge (a)->flags & EDGE_COMPLEX) |
bc35512f | 2644 | && a != ENTRY_BLOCK_PTR && b != EXIT_BLOCK_PTR |
49ea3702 SB |
2645 | /* If the jump insn has side effects, we can't kill the edge. |
2646 | When not optimizing, try_redirect_by_replacing_jump will | |
2647 | not allow us to redirect an edge by replacing a table jump. */ | |
4b4bf941 | 2648 | && (!JUMP_P (BB_END (a)) |
49ea3702 | 2649 | || ((!optimize || reload_completed) |
a813c111 | 2650 | ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a))))); |
bc35512f JH |
2651 | } |
2652 | ||
41806d92 NS |
2653 | /* Merge block A and B. The blocks must be mergeable. */ |
2654 | ||
bc35512f JH |
2655 | static void |
2656 | cfg_layout_merge_blocks (basic_block a, basic_block b) | |
2657 | { | |
2658 | #ifdef ENABLE_CHECKING | |
341c100f | 2659 | gcc_assert (cfg_layout_can_merge_blocks_p (a, b)); |
bc35512f JH |
2660 | #endif |
2661 | ||
6fb5fa3c DB |
2662 | if (dump_file) |
2663 | fprintf (dump_file, "merging block %d into block %d\n", b->index, a->index); | |
2664 | ||
bc35512f | 2665 | /* If there was a CODE_LABEL beginning B, delete it. */ |
4b4bf941 | 2666 | if (LABEL_P (BB_HEAD (b))) |
2c97f8e4 | 2667 | { |
2c97f8e4 RH |
2668 | delete_insn (BB_HEAD (b)); |
2669 | } | |
bc35512f JH |
2670 | |
2671 | /* We should have fallthru edge in a, or we can do dummy redirection to get | |
2672 | it cleaned up. */ | |
4b4bf941 | 2673 | if (JUMP_P (BB_END (a))) |
628f6a4e | 2674 | try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true); |
341c100f | 2675 | gcc_assert (!JUMP_P (BB_END (a))); |
bc35512f | 2676 | |
7241571e JJ |
2677 | /* When not optimizing and the edge is the only place in RTL which holds |
2678 | some unique locus, emit a nop with that locus in between. */ | |
2679 | if (!optimize && EDGE_SUCC (a, 0)->goto_locus) | |
2680 | { | |
1692ae7e | 2681 | rtx insn = BB_END (a), end = PREV_INSN (BB_HEAD (a)); |
7241571e JJ |
2682 | int goto_locus = EDGE_SUCC (a, 0)->goto_locus; |
2683 | ||
1692ae7e JJ |
2684 | while (insn != end && (!INSN_P (insn) || INSN_LOCATOR (insn) == 0)) |
2685 | insn = PREV_INSN (insn); | |
2686 | if (insn != end && locator_eq (INSN_LOCATOR (insn), goto_locus)) | |
7241571e JJ |
2687 | goto_locus = 0; |
2688 | else | |
2689 | { | |
2690 | insn = BB_HEAD (b); | |
1692ae7e JJ |
2691 | end = NEXT_INSN (BB_END (b)); |
2692 | while (insn != end && !INSN_P (insn)) | |
2693 | insn = NEXT_INSN (insn); | |
2694 | if (insn != end && INSN_LOCATOR (insn) != 0 | |
2695 | && locator_eq (INSN_LOCATOR (insn), goto_locus)) | |
7241571e JJ |
2696 | goto_locus = 0; |
2697 | } | |
2698 | if (goto_locus) | |
2699 | { | |
2700 | BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a); | |
2701 | INSN_LOCATOR (BB_END (a)) = goto_locus; | |
2702 | } | |
2703 | } | |
2704 | ||
bc35512f | 2705 | /* Possible line number notes should appear in between. */ |
370369e1 | 2706 | if (b->il.rtl->header) |
bc35512f | 2707 | { |
a813c111 | 2708 | rtx first = BB_END (a), last; |
bc35512f | 2709 | |
6fb5fa3c | 2710 | last = emit_insn_after_noloc (b->il.rtl->header, BB_END (a), a); |
a7b87f73 | 2711 | delete_insn_chain (NEXT_INSN (first), last, false); |
370369e1 | 2712 | b->il.rtl->header = NULL; |
bc35512f JH |
2713 | } |
2714 | ||
2715 | /* In the case basic blocks are not adjacent, move them around. */ | |
a813c111 | 2716 | if (NEXT_INSN (BB_END (a)) != BB_HEAD (b)) |
bc35512f | 2717 | { |
a813c111 | 2718 | rtx first = unlink_insn_chain (BB_HEAD (b), BB_END (b)); |
bc35512f | 2719 | |
6fb5fa3c | 2720 | emit_insn_after_noloc (first, BB_END (a), a); |
bc35512f JH |
2721 | /* Skip possible DELETED_LABEL insn. */ |
2722 | if (!NOTE_INSN_BASIC_BLOCK_P (first)) | |
2723 | first = NEXT_INSN (first); | |
341c100f | 2724 | gcc_assert (NOTE_INSN_BASIC_BLOCK_P (first)); |
a813c111 | 2725 | BB_HEAD (b) = NULL; |
6c3d0e31 SP |
2726 | |
2727 | /* emit_insn_after_noloc doesn't call df_insn_change_bb. | |
2728 | We need to explicitly call. */ | |
2729 | update_bb_for_insn_chain (NEXT_INSN (first), | |
2730 | BB_END (b), | |
2731 | a); | |
2732 | ||
bc35512f JH |
2733 | delete_insn (first); |
2734 | } | |
2735 | /* Otherwise just re-associate the instructions. */ | |
2736 | else | |
2737 | { | |
2738 | rtx insn; | |
2739 | ||
6c3d0e31 | 2740 | update_bb_for_insn_chain (BB_HEAD (b), BB_END (b), a); |
6fb5fa3c | 2741 | |
a813c111 | 2742 | insn = BB_HEAD (b); |
bc35512f JH |
2743 | /* Skip possible DELETED_LABEL insn. */ |
2744 | if (!NOTE_INSN_BASIC_BLOCK_P (insn)) | |
2745 | insn = NEXT_INSN (insn); | |
341c100f | 2746 | gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn)); |
a813c111 SB |
2747 | BB_HEAD (b) = NULL; |
2748 | BB_END (a) = BB_END (b); | |
bc35512f JH |
2749 | delete_insn (insn); |
2750 | } | |
2751 | ||
6fb5fa3c DB |
2752 | df_bb_delete (b->index); |
2753 | ||
bc35512f | 2754 | /* Possible tablejumps and barriers should appear after the block. */ |
370369e1 | 2755 | if (b->il.rtl->footer) |
bc35512f | 2756 | { |
370369e1 JH |
2757 | if (!a->il.rtl->footer) |
2758 | a->il.rtl->footer = b->il.rtl->footer; | |
bc35512f JH |
2759 | else |
2760 | { | |
370369e1 | 2761 | rtx last = a->il.rtl->footer; |
bc35512f JH |
2762 | |
2763 | while (NEXT_INSN (last)) | |
2764 | last = NEXT_INSN (last); | |
370369e1 JH |
2765 | NEXT_INSN (last) = b->il.rtl->footer; |
2766 | PREV_INSN (b->il.rtl->footer) = last; | |
bc35512f | 2767 | } |
370369e1 | 2768 | b->il.rtl->footer = NULL; |
bc35512f JH |
2769 | } |
2770 | ||
c263766c RH |
2771 | if (dump_file) |
2772 | fprintf (dump_file, "Merged blocks %d and %d.\n", | |
bc35512f | 2773 | a->index, b->index); |
bc35512f JH |
2774 | } |
2775 | ||
2776 | /* Split edge E. */ | |
f470c378 | 2777 | |
bc35512f JH |
2778 | static basic_block |
2779 | cfg_layout_split_edge (edge e) | |
2780 | { | |
bc35512f JH |
2781 | basic_block new_bb = |
2782 | create_basic_block (e->src != ENTRY_BLOCK_PTR | |
a813c111 | 2783 | ? NEXT_INSN (BB_END (e->src)) : get_insns (), |
bc35512f JH |
2784 | NULL_RTX, e->src); |
2785 | ||
3e7eb734 JJ |
2786 | if (e->dest == EXIT_BLOCK_PTR) |
2787 | BB_COPY_PARTITION (new_bb, e->src); | |
2788 | else | |
2789 | BB_COPY_PARTITION (new_bb, e->dest); | |
a9b2ee88 | 2790 | make_edge (new_bb, e->dest, EDGE_FALLTHRU); |
bc35512f JH |
2791 | redirect_edge_and_branch_force (e, new_bb); |
2792 | ||
2793 | return new_bb; | |
2794 | } | |
2795 | ||
f470c378 ZD |
2796 | /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */ |
2797 | ||
2798 | static void | |
2799 | rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED) | |
2800 | { | |
2801 | } | |
2802 | ||
6de9cd9a DN |
2803 | /* Return 1 if BB ends with a call, possibly followed by some |
2804 | instructions that must stay with the call, 0 otherwise. */ | |
2805 | ||
2806 | static bool | |
b48d0358 | 2807 | rtl_block_ends_with_call_p (basic_block bb) |
6de9cd9a DN |
2808 | { |
2809 | rtx insn = BB_END (bb); | |
2810 | ||
4b4bf941 | 2811 | while (!CALL_P (insn) |
6de9cd9a | 2812 | && insn != BB_HEAD (bb) |
92ddef69 | 2813 | && (keep_with_call_p (insn) |
b5b8b0ac AO |
2814 | || NOTE_P (insn) |
2815 | || DEBUG_INSN_P (insn))) | |
6de9cd9a | 2816 | insn = PREV_INSN (insn); |
4b4bf941 | 2817 | return (CALL_P (insn)); |
6de9cd9a DN |
2818 | } |
2819 | ||
2820 | /* Return 1 if BB ends with a conditional branch, 0 otherwise. */ | |
2821 | ||
2822 | static bool | |
9678086d | 2823 | rtl_block_ends_with_condjump_p (const_basic_block bb) |
6de9cd9a DN |
2824 | { |
2825 | return any_condjump_p (BB_END (bb)); | |
2826 | } | |
2827 | ||
2828 | /* Return true if we need to add fake edge to exit. | |
2829 | Helper function for rtl_flow_call_edges_add. */ | |
2830 | ||
2831 | static bool | |
9678086d | 2832 | need_fake_edge_p (const_rtx insn) |
6de9cd9a DN |
2833 | { |
2834 | if (!INSN_P (insn)) | |
2835 | return false; | |
2836 | ||
4b4bf941 | 2837 | if ((CALL_P (insn) |
6de9cd9a DN |
2838 | && !SIBLING_CALL_P (insn) |
2839 | && !find_reg_note (insn, REG_NORETURN, NULL) | |
becfd6e5 | 2840 | && !(RTL_CONST_OR_PURE_CALL_P (insn)))) |
6de9cd9a DN |
2841 | return true; |
2842 | ||
2843 | return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS | |
2844 | && MEM_VOLATILE_P (PATTERN (insn))) | |
2845 | || (GET_CODE (PATTERN (insn)) == PARALLEL | |
2846 | && asm_noperands (insn) != -1 | |
2847 | && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0))) | |
2848 | || GET_CODE (PATTERN (insn)) == ASM_INPUT); | |
2849 | } | |
2850 | ||
2851 | /* Add fake edges to the function exit for any non constant and non noreturn | |
2852 | calls, volatile inline assembly in the bitmap of blocks specified by | |
2853 | BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks | |
2854 | that were split. | |
2855 | ||
2856 | The goal is to expose cases in which entering a basic block does not imply | |
2857 | that all subsequent instructions must be executed. */ | |
2858 | ||
2859 | static int | |
2860 | rtl_flow_call_edges_add (sbitmap blocks) | |
2861 | { | |
2862 | int i; | |
2863 | int blocks_split = 0; | |
2864 | int last_bb = last_basic_block; | |
2865 | bool check_last_block = false; | |
2866 | ||
24bd1a0b | 2867 | if (n_basic_blocks == NUM_FIXED_BLOCKS) |
6de9cd9a DN |
2868 | return 0; |
2869 | ||
2870 | if (! blocks) | |
2871 | check_last_block = true; | |
2872 | else | |
2873 | check_last_block = TEST_BIT (blocks, EXIT_BLOCK_PTR->prev_bb->index); | |
2874 | ||
2875 | /* In the last basic block, before epilogue generation, there will be | |
2876 | a fallthru edge to EXIT. Special care is required if the last insn | |
2877 | of the last basic block is a call because make_edge folds duplicate | |
2878 | edges, which would result in the fallthru edge also being marked | |
2879 | fake, which would result in the fallthru edge being removed by | |
2880 | remove_fake_edges, which would result in an invalid CFG. | |
2881 | ||
2882 | Moreover, we can't elide the outgoing fake edge, since the block | |
2883 | profiler needs to take this into account in order to solve the minimal | |
2884 | spanning tree in the case that the call doesn't return. | |
2885 | ||
2886 | Handle this by adding a dummy instruction in a new last basic block. */ | |
2887 | if (check_last_block) | |
2888 | { | |
2889 | basic_block bb = EXIT_BLOCK_PTR->prev_bb; | |
2890 | rtx insn = BB_END (bb); | |
2891 | ||
2892 | /* Back up past insns that must be kept in the same block as a call. */ | |
2893 | while (insn != BB_HEAD (bb) | |
2894 | && keep_with_call_p (insn)) | |
2895 | insn = PREV_INSN (insn); | |
2896 | ||
2897 | if (need_fake_edge_p (insn)) | |
2898 | { | |
2899 | edge e; | |
2900 | ||
9ff3d2de JL |
2901 | e = find_edge (bb, EXIT_BLOCK_PTR); |
2902 | if (e) | |
2903 | { | |
c41c1387 | 2904 | insert_insn_on_edge (gen_use (const0_rtx), e); |
9ff3d2de JL |
2905 | commit_edge_insertions (); |
2906 | } | |
6de9cd9a DN |
2907 | } |
2908 | } | |
2909 | ||
2910 | /* Now add fake edges to the function exit for any non constant | |
2911 | calls since there is no way that we can determine if they will | |
2912 | return or not... */ | |
2913 | ||
24bd1a0b | 2914 | for (i = NUM_FIXED_BLOCKS; i < last_bb; i++) |
6de9cd9a DN |
2915 | { |
2916 | basic_block bb = BASIC_BLOCK (i); | |
2917 | rtx insn; | |
2918 | rtx prev_insn; | |
2919 | ||
2920 | if (!bb) | |
2921 | continue; | |
2922 | ||
2923 | if (blocks && !TEST_BIT (blocks, i)) | |
2924 | continue; | |
2925 | ||
2926 | for (insn = BB_END (bb); ; insn = prev_insn) | |
2927 | { | |
2928 | prev_insn = PREV_INSN (insn); | |
2929 | if (need_fake_edge_p (insn)) | |
2930 | { | |
2931 | edge e; | |
2932 | rtx split_at_insn = insn; | |
2933 | ||
2934 | /* Don't split the block between a call and an insn that should | |
c22cacf3 | 2935 | remain in the same block as the call. */ |
4b4bf941 | 2936 | if (CALL_P (insn)) |
6de9cd9a DN |
2937 | while (split_at_insn != BB_END (bb) |
2938 | && keep_with_call_p (NEXT_INSN (split_at_insn))) | |
2939 | split_at_insn = NEXT_INSN (split_at_insn); | |
2940 | ||
2941 | /* The handling above of the final block before the epilogue | |
c22cacf3 | 2942 | should be enough to verify that there is no edge to the exit |
6de9cd9a DN |
2943 | block in CFG already. Calling make_edge in such case would |
2944 | cause us to mark that edge as fake and remove it later. */ | |
2945 | ||
2946 | #ifdef ENABLE_CHECKING | |
2947 | if (split_at_insn == BB_END (bb)) | |
628f6a4e | 2948 | { |
9ff3d2de JL |
2949 | e = find_edge (bb, EXIT_BLOCK_PTR); |
2950 | gcc_assert (e == NULL); | |
628f6a4e | 2951 | } |
6de9cd9a DN |
2952 | #endif |
2953 | ||
2954 | /* Note that the following may create a new basic block | |
2955 | and renumber the existing basic blocks. */ | |
2956 | if (split_at_insn != BB_END (bb)) | |
2957 | { | |
2958 | e = split_block (bb, split_at_insn); | |
2959 | if (e) | |
2960 | blocks_split++; | |
2961 | } | |
2962 | ||
2963 | make_edge (bb, EXIT_BLOCK_PTR, EDGE_FAKE); | |
2964 | } | |
2965 | ||
2966 | if (insn == BB_HEAD (bb)) | |
2967 | break; | |
2968 | } | |
2969 | } | |
2970 | ||
2971 | if (blocks_split) | |
2972 | verify_flow_info (); | |
2973 | ||
2974 | return blocks_split; | |
2975 | } | |
2976 | ||
1cb7dfc3 | 2977 | /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is |
315682fb | 2978 | the conditional branch target, SECOND_HEAD should be the fall-thru |
1cb7dfc3 MH |
2979 | there is no need to handle this here the loop versioning code handles |
2980 | this. the reason for SECON_HEAD is that it is needed for condition | |
2981 | in trees, and this should be of the same type since it is a hook. */ | |
2982 | static void | |
2983 | rtl_lv_add_condition_to_bb (basic_block first_head , | |
c22cacf3 MS |
2984 | basic_block second_head ATTRIBUTE_UNUSED, |
2985 | basic_block cond_bb, void *comp_rtx) | |
1cb7dfc3 MH |
2986 | { |
2987 | rtx label, seq, jump; | |
2988 | rtx op0 = XEXP ((rtx)comp_rtx, 0); | |
2989 | rtx op1 = XEXP ((rtx)comp_rtx, 1); | |
2990 | enum rtx_code comp = GET_CODE ((rtx)comp_rtx); | |
2991 | enum machine_mode mode; | |
2992 | ||
2993 | ||
2994 | label = block_label (first_head); | |
2995 | mode = GET_MODE (op0); | |
2996 | if (mode == VOIDmode) | |
2997 | mode = GET_MODE (op1); | |
2998 | ||
2999 | start_sequence (); | |
3000 | op0 = force_operand (op0, NULL_RTX); | |
3001 | op1 = force_operand (op1, NULL_RTX); | |
3002 | do_compare_rtx_and_jump (op0, op1, comp, 0, | |
3003 | mode, NULL_RTX, NULL_RTX, label); | |
3004 | jump = get_last_insn (); | |
3005 | JUMP_LABEL (jump) = label; | |
3006 | LABEL_NUSES (label)++; | |
3007 | seq = get_insns (); | |
3008 | end_sequence (); | |
3009 | ||
3010 | /* Add the new cond , in the new head. */ | |
3011 | emit_insn_after(seq, BB_END(cond_bb)); | |
3012 | } | |
3013 | ||
3014 | ||
3015 | /* Given a block B with unconditional branch at its end, get the | |
3016 | store the return the branch edge and the fall-thru edge in | |
3017 | BRANCH_EDGE and FALLTHRU_EDGE respectively. */ | |
3018 | static void | |
3019 | rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge, | |
3020 | edge *fallthru_edge) | |
3021 | { | |
3022 | edge e = EDGE_SUCC (b, 0); | |
3023 | ||
3024 | if (e->flags & EDGE_FALLTHRU) | |
3025 | { | |
3026 | *fallthru_edge = e; | |
3027 | *branch_edge = EDGE_SUCC (b, 1); | |
3028 | } | |
3029 | else | |
3030 | { | |
3031 | *branch_edge = e; | |
3032 | *fallthru_edge = EDGE_SUCC (b, 1); | |
3033 | } | |
3034 | } | |
3035 | ||
5e2d947c JH |
3036 | void |
3037 | init_rtl_bb_info (basic_block bb) | |
3038 | { | |
3039 | gcc_assert (!bb->il.rtl); | |
ae50c0cb | 3040 | bb->il.rtl = GGC_CNEW (struct rtl_bb_info); |
5e2d947c JH |
3041 | } |
3042 | ||
1cb7dfc3 | 3043 | |
8cd37d0b RL |
3044 | /* Add EXPR to the end of basic block BB. */ |
3045 | ||
3046 | rtx | |
3047 | insert_insn_end_bb_new (rtx pat, basic_block bb) | |
3048 | { | |
3049 | rtx insn = BB_END (bb); | |
3050 | rtx new_insn; | |
3051 | rtx pat_end = pat; | |
3052 | ||
3053 | while (NEXT_INSN (pat_end) != NULL_RTX) | |
3054 | pat_end = NEXT_INSN (pat_end); | |
3055 | ||
3056 | /* If the last insn is a jump, insert EXPR in front [taking care to | |
3057 | handle cc0, etc. properly]. Similarly we need to care trapping | |
3058 | instructions in presence of non-call exceptions. */ | |
3059 | ||
3060 | if (JUMP_P (insn) | |
3061 | || (NONJUMP_INSN_P (insn) | |
3062 | && (!single_succ_p (bb) | |
3063 | || single_succ_edge (bb)->flags & EDGE_ABNORMAL))) | |
3064 | { | |
3065 | #ifdef HAVE_cc0 | |
3066 | rtx note; | |
3067 | #endif | |
3068 | /* If this is a jump table, then we can't insert stuff here. Since | |
3069 | we know the previous real insn must be the tablejump, we insert | |
3070 | the new instruction just before the tablejump. */ | |
3071 | if (GET_CODE (PATTERN (insn)) == ADDR_VEC | |
3072 | || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC) | |
3073 | insn = prev_real_insn (insn); | |
3074 | ||
3075 | #ifdef HAVE_cc0 | |
3076 | /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts | |
3077 | if cc0 isn't set. */ | |
3078 | note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX); | |
3079 | if (note) | |
3080 | insn = XEXP (note, 0); | |
3081 | else | |
3082 | { | |
3083 | rtx maybe_cc0_setter = prev_nonnote_insn (insn); | |
3084 | if (maybe_cc0_setter | |
3085 | && INSN_P (maybe_cc0_setter) | |
3086 | && sets_cc0_p (PATTERN (maybe_cc0_setter))) | |
3087 | insn = maybe_cc0_setter; | |
3088 | } | |
3089 | #endif | |
3090 | /* FIXME: What if something in cc0/jump uses value set in new | |
3091 | insn? */ | |
6fb5fa3c | 3092 | new_insn = emit_insn_before_noloc (pat, insn, bb); |
8cd37d0b RL |
3093 | } |
3094 | ||
3095 | /* Likewise if the last insn is a call, as will happen in the presence | |
3096 | of exception handling. */ | |
3097 | else if (CALL_P (insn) | |
3098 | && (!single_succ_p (bb) | |
3099 | || single_succ_edge (bb)->flags & EDGE_ABNORMAL)) | |
3100 | { | |
3101 | /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers, | |
3102 | we search backward and place the instructions before the first | |
3103 | parameter is loaded. Do this for everyone for consistency and a | |
3104 | presumption that we'll get better code elsewhere as well. */ | |
3105 | ||
3106 | /* Since different machines initialize their parameter registers | |
3107 | in different orders, assume nothing. Collect the set of all | |
3108 | parameter registers. */ | |
3109 | insn = find_first_parameter_load (insn, BB_HEAD (bb)); | |
3110 | ||
3111 | /* If we found all the parameter loads, then we want to insert | |
3112 | before the first parameter load. | |
3113 | ||
3114 | If we did not find all the parameter loads, then we might have | |
3115 | stopped on the head of the block, which could be a CODE_LABEL. | |
3116 | If we inserted before the CODE_LABEL, then we would be putting | |
3117 | the insn in the wrong basic block. In that case, put the insn | |
3118 | after the CODE_LABEL. Also, respect NOTE_INSN_BASIC_BLOCK. */ | |
3119 | while (LABEL_P (insn) | |
3120 | || NOTE_INSN_BASIC_BLOCK_P (insn)) | |
3121 | insn = NEXT_INSN (insn); | |
3122 | ||
6fb5fa3c | 3123 | new_insn = emit_insn_before_noloc (pat, insn, bb); |
8cd37d0b RL |
3124 | } |
3125 | else | |
6fb5fa3c | 3126 | new_insn = emit_insn_after_noloc (pat, insn, bb); |
8cd37d0b RL |
3127 | |
3128 | return new_insn; | |
3129 | } | |
3130 | ||
14fa2cc0 ZD |
3131 | /* Returns true if it is possible to remove edge E by redirecting |
3132 | it to the destination of the other edge from E->src. */ | |
3133 | ||
3134 | static bool | |
9678086d | 3135 | rtl_can_remove_branch_p (const_edge e) |
14fa2cc0 | 3136 | { |
9678086d KG |
3137 | const_basic_block src = e->src; |
3138 | const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest; | |
3139 | const_rtx insn = BB_END (src), set; | |
14fa2cc0 ZD |
3140 | |
3141 | /* The conditions are taken from try_redirect_by_replacing_jump. */ | |
3142 | if (target == EXIT_BLOCK_PTR) | |
3143 | return false; | |
3144 | ||
3145 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) | |
3146 | return false; | |
3147 | ||
3148 | if (find_reg_note (insn, REG_CROSSING_JUMP, NULL_RTX) | |
3149 | || BB_PARTITION (src) != BB_PARTITION (target)) | |
3150 | return false; | |
3151 | ||
3152 | if (!onlyjump_p (insn) | |
3153 | || tablejump_p (insn, NULL, NULL)) | |
3154 | return false; | |
3155 | ||
3156 | set = single_set (insn); | |
3157 | if (!set || side_effects_p (set)) | |
3158 | return false; | |
3159 | ||
3160 | return true; | |
3161 | } | |
3162 | ||
9ee634e3 JH |
3163 | /* Implementation of CFG manipulation for linearized RTL. */ |
3164 | struct cfg_hooks rtl_cfg_hooks = { | |
f470c378 | 3165 | "rtl", |
9ee634e3 | 3166 | rtl_verify_flow_info, |
10e9fecc | 3167 | rtl_dump_bb, |
bc35512f | 3168 | rtl_create_basic_block, |
9ee634e3 JH |
3169 | rtl_redirect_edge_and_branch, |
3170 | rtl_redirect_edge_and_branch_force, | |
14fa2cc0 | 3171 | rtl_can_remove_branch_p, |
9ee634e3 JH |
3172 | rtl_delete_block, |
3173 | rtl_split_block, | |
f470c378 | 3174 | rtl_move_block_after, |
bc35512f JH |
3175 | rtl_can_merge_blocks, /* can_merge_blocks_p */ |
3176 | rtl_merge_blocks, | |
6de9cd9a DN |
3177 | rtl_predict_edge, |
3178 | rtl_predicted_by_p, | |
3179 | NULL, /* can_duplicate_block_p */ | |
3180 | NULL, /* duplicate_block */ | |
f470c378 ZD |
3181 | rtl_split_edge, |
3182 | rtl_make_forwarder_block, | |
6de9cd9a DN |
3183 | rtl_tidy_fallthru_edge, |
3184 | rtl_block_ends_with_call_p, | |
3185 | rtl_block_ends_with_condjump_p, | |
d9d4706f KH |
3186 | rtl_flow_call_edges_add, |
3187 | NULL, /* execute_on_growing_pred */ | |
1cb7dfc3 MH |
3188 | NULL, /* execute_on_shrinking_pred */ |
3189 | NULL, /* duplicate loop for trees */ | |
3190 | NULL, /* lv_add_condition_to_bb */ | |
3191 | NULL, /* lv_adjust_loop_header_phi*/ | |
3192 | NULL, /* extract_cond_bb_edges */ | |
c22cacf3 | 3193 | NULL /* flush_pending_stmts */ |
9ee634e3 JH |
3194 | }; |
3195 | ||
3196 | /* Implementation of CFG manipulation for cfg layout RTL, where | |
3197 | basic block connected via fallthru edges does not have to be adjacent. | |
3198 | This representation will hopefully become the default one in future | |
3199 | version of the compiler. */ | |
6de9cd9a DN |
3200 | |
3201 | /* We do not want to declare these functions in a header file, since they | |
3202 | should only be used through the cfghooks interface, and we do not want to | |
3203 | move them here since it would require also moving quite a lot of related | |
6fb5fa3c | 3204 | code. They are in cfglayout.c. */ |
9678086d | 3205 | extern bool cfg_layout_can_duplicate_bb_p (const_basic_block); |
6de9cd9a DN |
3206 | extern basic_block cfg_layout_duplicate_bb (basic_block); |
3207 | ||
9ee634e3 | 3208 | struct cfg_hooks cfg_layout_rtl_cfg_hooks = { |
f470c378 | 3209 | "cfglayout mode", |
bc35512f | 3210 | rtl_verify_flow_info_1, |
10e9fecc | 3211 | rtl_dump_bb, |
bc35512f | 3212 | cfg_layout_create_basic_block, |
9ee634e3 JH |
3213 | cfg_layout_redirect_edge_and_branch, |
3214 | cfg_layout_redirect_edge_and_branch_force, | |
14fa2cc0 | 3215 | rtl_can_remove_branch_p, |
9ee634e3 JH |
3216 | cfg_layout_delete_block, |
3217 | cfg_layout_split_block, | |
f470c378 | 3218 | rtl_move_block_after, |
bc35512f JH |
3219 | cfg_layout_can_merge_blocks_p, |
3220 | cfg_layout_merge_blocks, | |
6de9cd9a DN |
3221 | rtl_predict_edge, |
3222 | rtl_predicted_by_p, | |
3223 | cfg_layout_can_duplicate_bb_p, | |
3224 | cfg_layout_duplicate_bb, | |
f470c378 ZD |
3225 | cfg_layout_split_edge, |
3226 | rtl_make_forwarder_block, | |
6de9cd9a DN |
3227 | NULL, |
3228 | rtl_block_ends_with_call_p, | |
3229 | rtl_block_ends_with_condjump_p, | |
d9d4706f KH |
3230 | rtl_flow_call_edges_add, |
3231 | NULL, /* execute_on_growing_pred */ | |
1cb7dfc3 MH |
3232 | NULL, /* execute_on_shrinking_pred */ |
3233 | duplicate_loop_to_header_edge, /* duplicate loop for trees */ | |
3234 | rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */ | |
3235 | NULL, /* lv_adjust_loop_header_phi*/ | |
3236 | rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */ | |
c22cacf3 | 3237 | NULL /* flush_pending_stmts */ |
9ee634e3 | 3238 | }; |