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