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