]>
Commit | Line | Data |
---|---|---|
ca6c03ca | 1 | /* Control flow graph manipulation code for GNU compiler. |
d1e082c2 | 2 | Copyright (C) 1987-2013 Free Software Foundation, Inc. |
ca6c03ca JH |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
ca6c03ca JH |
9 | version. |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
ca6c03ca | 19 | |
5f0d2358 RK |
20 | /* This file contains low level functions to manipulate the CFG and analyze it |
21 | that are aware of the RTL intermediate language. | |
ca6c03ca JH |
22 | |
23 | Available functionality: | |
bc35512f | 24 | - Basic CFG/RTL manipulation API documented in cfghooks.h |
5f0d2358 | 25 | - CFG-aware instruction chain manipulation |
ca6c03ca | 26 | delete_insn, delete_insn_chain |
bc35512f JH |
27 | - Edge splitting and committing to edges |
28 | insert_insn_on_edge, commit_edge_insertions | |
29 | - CFG updating after insn simplification | |
30 | purge_dead_edges, purge_all_dead_edges | |
ba5e9aca EB |
31 | - CFG fixing after coarse manipulation |
32 | fixup_abnormal_edges | |
bc35512f JH |
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 | 44 | #include "tree.h" |
ca6c03ca JH |
45 | #include "hard-reg-set.h" |
46 | #include "basic-block.h" | |
3371a64f | 47 | #include "bb-reorder.h" |
ca6c03ca JH |
48 | #include "regs.h" |
49 | #include "flags.h" | |
ca6c03ca JH |
50 | #include "function.h" |
51 | #include "except.h" | |
0cbd9993 | 52 | #include "rtl-error.h" |
ca6c03ca JH |
53 | #include "tm_p.h" |
54 | #include "obstack.h" | |
2a34bece | 55 | #include "insn-attr.h" |
0a2ed1f1 | 56 | #include "insn-config.h" |
ff25ef99 | 57 | #include "expr.h" |
9fb32434 | 58 | #include "target.h" |
677f3fa8 | 59 | #include "common/common-target.h" |
1cb7dfc3 | 60 | #include "cfgloop.h" |
5e2d947c | 61 | #include "ggc.h" |
ef330312 | 62 | #include "tree-pass.h" |
6fb5fa3c | 63 | #include "df.h" |
ca6c03ca | 64 | |
78bde837 SB |
65 | /* Holds the interesting leading and trailing notes for the function. |
66 | Only applicable if the CFG is in cfglayout mode. */ | |
67 | static GTY(()) rtx cfg_layout_function_footer; | |
68 | static GTY(()) rtx cfg_layout_function_header; | |
69 | ||
70 | static rtx skip_insns_after_block (basic_block); | |
71 | static void record_effective_endpoints (void); | |
72 | static rtx label_for_bb (basic_block); | |
73 | static void fixup_reorder_chain (void); | |
74 | ||
75 | void verify_insn_chain (void); | |
76 | static void fixup_fallthru_exit_predecessor (void); | |
9678086d KG |
77 | static int can_delete_note_p (const_rtx); |
78 | static int can_delete_label_p (const_rtx); | |
d329e058 | 79 | static basic_block rtl_split_edge (edge); |
f470c378 | 80 | static bool rtl_move_block_after (basic_block, basic_block); |
d329e058 | 81 | static int rtl_verify_flow_info (void); |
f470c378 | 82 | static basic_block cfg_layout_split_block (basic_block, void *); |
6de9cd9a | 83 | static edge cfg_layout_redirect_edge_and_branch (edge, basic_block); |
d329e058 AJ |
84 | static basic_block cfg_layout_redirect_edge_and_branch_force (edge, basic_block); |
85 | static void cfg_layout_delete_block (basic_block); | |
86 | static void rtl_delete_block (basic_block); | |
87 | static basic_block rtl_redirect_edge_and_branch_force (edge, basic_block); | |
6de9cd9a | 88 | static edge rtl_redirect_edge_and_branch (edge, basic_block); |
f470c378 | 89 | static basic_block rtl_split_block (basic_block, void *); |
a315c44c | 90 | static void rtl_dump_bb (FILE *, basic_block, int, int); |
d329e058 | 91 | static int rtl_verify_flow_info_1 (void); |
f470c378 | 92 | static void rtl_make_forwarder_block (edge); |
ca6c03ca JH |
93 | \f |
94 | /* Return true if NOTE is not one of the ones that must be kept paired, | |
5f0d2358 | 95 | so that we may simply delete it. */ |
ca6c03ca JH |
96 | |
97 | static int | |
9678086d | 98 | can_delete_note_p (const_rtx note) |
ca6c03ca | 99 | { |
cd9c1ca8 RH |
100 | switch (NOTE_KIND (note)) |
101 | { | |
102 | case NOTE_INSN_DELETED: | |
103 | case NOTE_INSN_BASIC_BLOCK: | |
104 | case NOTE_INSN_EPILOGUE_BEG: | |
105 | return true; | |
106 | ||
107 | default: | |
108 | return false; | |
109 | } | |
ca6c03ca JH |
110 | } |
111 | ||
112 | /* True if a given label can be deleted. */ | |
113 | ||
114 | static int | |
9678086d | 115 | can_delete_label_p (const_rtx label) |
ca6c03ca | 116 | { |
5f0d2358 RK |
117 | return (!LABEL_PRESERVE_P (label) |
118 | /* User declared labels must be preserved. */ | |
119 | && LABEL_NAME (label) == 0 | |
610d2478 | 120 | && !in_expr_list_p (forced_labels, label)); |
ca6c03ca JH |
121 | } |
122 | ||
03fbe718 | 123 | /* Delete INSN by patching it out. */ |
ca6c03ca | 124 | |
03fbe718 | 125 | void |
d329e058 | 126 | delete_insn (rtx insn) |
ca6c03ca | 127 | { |
ca6c03ca JH |
128 | rtx note; |
129 | bool really_delete = true; | |
130 | ||
4b4bf941 | 131 | if (LABEL_P (insn)) |
ca6c03ca JH |
132 | { |
133 | /* Some labels can't be directly removed from the INSN chain, as they | |
c22cacf3 MS |
134 | might be references via variables, constant pool etc. |
135 | Convert them to the special NOTE_INSN_DELETED_LABEL note. */ | |
ca6c03ca JH |
136 | if (! can_delete_label_p (insn)) |
137 | { | |
138 | const char *name = LABEL_NAME (insn); | |
5e5df392 | 139 | basic_block bb = BLOCK_FOR_INSN (insn); |
03fbe718 | 140 | rtx bb_note = NEXT_INSN (insn); |
ca6c03ca JH |
141 | |
142 | really_delete = false; | |
143 | PUT_CODE (insn, NOTE); | |
a38e7aa5 | 144 | NOTE_KIND (insn) = NOTE_INSN_DELETED_LABEL; |
6773e15f | 145 | NOTE_DELETED_LABEL_NAME (insn) = name; |
03fbe718 | 146 | |
0f33baa9 | 147 | /* If the note following the label starts a basic block, and the |
5e5df392 | 148 | label is a member of the same basic block, interchange the two. */ |
0f33baa9 TV |
149 | if (bb_note != NULL_RTX |
150 | && NOTE_INSN_BASIC_BLOCK_P (bb_note) | |
5e5df392 TV |
151 | && bb != NULL |
152 | && bb == BLOCK_FOR_INSN (bb_note)) | |
03fbe718 TV |
153 | { |
154 | reorder_insns_nobb (insn, insn, bb_note); | |
155 | BB_HEAD (bb) = bb_note; | |
156 | if (BB_END (bb) == bb_note) | |
157 | BB_END (bb) = insn; | |
158 | } | |
ca6c03ca | 159 | } |
5f0d2358 | 160 | |
ca6c03ca JH |
161 | remove_node_from_expr_list (insn, &nonlocal_goto_handler_labels); |
162 | } | |
163 | ||
164 | if (really_delete) | |
165 | { | |
cda94cbb | 166 | /* If this insn has already been deleted, something is very wrong. */ |
341c100f | 167 | gcc_assert (!INSN_DELETED_P (insn)); |
1f397f45 SB |
168 | if (INSN_P (insn)) |
169 | df_insn_delete (insn); | |
ca6c03ca JH |
170 | remove_insn (insn); |
171 | INSN_DELETED_P (insn) = 1; | |
172 | } | |
173 | ||
174 | /* If deleting a jump, decrement the use count of the label. Deleting | |
175 | the label itself should happen in the normal course of block merging. */ | |
cf7c4aa6 | 176 | if (JUMP_P (insn)) |
9295a326 | 177 | { |
cf7c4aa6 HPN |
178 | if (JUMP_LABEL (insn) |
179 | && LABEL_P (JUMP_LABEL (insn))) | |
180 | LABEL_NUSES (JUMP_LABEL (insn))--; | |
181 | ||
182 | /* If there are more targets, remove them too. */ | |
183 | while ((note | |
184 | = find_reg_note (insn, REG_LABEL_TARGET, NULL_RTX)) != NULL_RTX | |
4b4bf941 | 185 | && LABEL_P (XEXP (note, 0))) |
9295a326 JZ |
186 | { |
187 | LABEL_NUSES (XEXP (note, 0))--; | |
188 | remove_note (insn, note); | |
189 | } | |
190 | } | |
ca6c03ca | 191 | |
cf7c4aa6 HPN |
192 | /* Also if deleting any insn that references a label as an operand. */ |
193 | while ((note = find_reg_note (insn, REG_LABEL_OPERAND, NULL_RTX)) != NULL_RTX | |
194 | && LABEL_P (XEXP (note, 0))) | |
195 | { | |
196 | LABEL_NUSES (XEXP (note, 0))--; | |
197 | remove_note (insn, note); | |
198 | } | |
199 | ||
481683e1 | 200 | if (JUMP_TABLE_DATA_P (insn)) |
ca6c03ca JH |
201 | { |
202 | rtx pat = PATTERN (insn); | |
203 | int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC; | |
204 | int len = XVECLEN (pat, diff_vec_p); | |
205 | int i; | |
206 | ||
207 | for (i = 0; i < len; i++) | |
a124fcda RH |
208 | { |
209 | rtx label = XEXP (XVECEXP (pat, diff_vec_p, i), 0); | |
210 | ||
211 | /* When deleting code in bulk (e.g. removing many unreachable | |
212 | blocks) we can delete a label that's a target of the vector | |
213 | before deleting the vector itself. */ | |
4b4bf941 | 214 | if (!NOTE_P (label)) |
a124fcda RH |
215 | LABEL_NUSES (label)--; |
216 | } | |
ca6c03ca | 217 | } |
ca6c03ca JH |
218 | } |
219 | ||
3dec4024 | 220 | /* Like delete_insn but also purge dead edges from BB. */ |
9ed7b221 | 221 | |
03fbe718 | 222 | void |
d329e058 | 223 | delete_insn_and_edges (rtx insn) |
3dec4024 | 224 | { |
3dec4024 JH |
225 | bool purge = false; |
226 | ||
ba4f7968 | 227 | if (INSN_P (insn) |
3dec4024 | 228 | && BLOCK_FOR_INSN (insn) |
a813c111 | 229 | && BB_END (BLOCK_FOR_INSN (insn)) == insn) |
3dec4024 | 230 | purge = true; |
03fbe718 | 231 | delete_insn (insn); |
3dec4024 JH |
232 | if (purge) |
233 | purge_dead_edges (BLOCK_FOR_INSN (insn)); | |
3dec4024 JH |
234 | } |
235 | ||
ca6c03ca | 236 | /* Unlink a chain of insns between START and FINISH, leaving notes |
a7b87f73 ZD |
237 | that must be paired. If CLEAR_BB is true, we set bb field for |
238 | insns that cannot be removed to NULL. */ | |
ca6c03ca JH |
239 | |
240 | void | |
a7b87f73 | 241 | delete_insn_chain (rtx start, rtx finish, bool clear_bb) |
ca6c03ca | 242 | { |
03fbe718 | 243 | rtx prev, current; |
ca6c03ca | 244 | |
5f0d2358 RK |
245 | /* Unchain the insns one by one. It would be quicker to delete all of these |
246 | with a single unchaining, rather than one at a time, but we need to keep | |
247 | the NOTE's. */ | |
03fbe718 | 248 | current = finish; |
ca6c03ca JH |
249 | while (1) |
250 | { | |
03fbe718 TV |
251 | prev = PREV_INSN (current); |
252 | if (NOTE_P (current) && !can_delete_note_p (current)) | |
ca6c03ca JH |
253 | ; |
254 | else | |
03fbe718 | 255 | delete_insn (current); |
ca6c03ca | 256 | |
03fbe718 TV |
257 | if (clear_bb && !INSN_DELETED_P (current)) |
258 | set_block_for_insn (current, NULL); | |
a7b87f73 | 259 | |
03fbe718 | 260 | if (current == start) |
ca6c03ca | 261 | break; |
03fbe718 | 262 | current = prev; |
ca6c03ca JH |
263 | } |
264 | } | |
265 | \f | |
5f0d2358 RK |
266 | /* Create a new basic block consisting of the instructions between HEAD and END |
267 | inclusive. This function is designed to allow fast BB construction - reuses | |
268 | the note and basic block struct in BB_NOTE, if any and do not grow | |
269 | BASIC_BLOCK chain and should be used directly only by CFG construction code. | |
270 | END can be NULL in to create new empty basic block before HEAD. Both END | |
918ed612 ZD |
271 | and HEAD can be NULL to create basic block at the end of INSN chain. |
272 | AFTER is the basic block we should be put after. */ | |
ca6c03ca JH |
273 | |
274 | basic_block | |
d329e058 | 275 | create_basic_block_structure (rtx head, rtx end, rtx bb_note, basic_block after) |
ca6c03ca JH |
276 | { |
277 | basic_block bb; | |
278 | ||
279 | if (bb_note | |
ca6c03ca JH |
280 | && (bb = NOTE_BASIC_BLOCK (bb_note)) != NULL |
281 | && bb->aux == NULL) | |
282 | { | |
283 | /* If we found an existing note, thread it back onto the chain. */ | |
284 | ||
285 | rtx after; | |
286 | ||
4b4bf941 | 287 | if (LABEL_P (head)) |
ca6c03ca JH |
288 | after = head; |
289 | else | |
290 | { | |
291 | after = PREV_INSN (head); | |
292 | head = bb_note; | |
293 | } | |
294 | ||
295 | if (after != bb_note && NEXT_INSN (after) != bb_note) | |
ba4f7968 | 296 | reorder_insns_nobb (bb_note, bb_note, after); |
ca6c03ca JH |
297 | } |
298 | else | |
299 | { | |
300 | /* Otherwise we must create a note and a basic block structure. */ | |
301 | ||
302 | bb = alloc_block (); | |
303 | ||
5e2d947c | 304 | init_rtl_bb_info (bb); |
ca6c03ca | 305 | if (!head && !end) |
5f0d2358 RK |
306 | head = end = bb_note |
307 | = emit_note_after (NOTE_INSN_BASIC_BLOCK, get_last_insn ()); | |
4b4bf941 | 308 | else if (LABEL_P (head) && end) |
ca6c03ca JH |
309 | { |
310 | bb_note = emit_note_after (NOTE_INSN_BASIC_BLOCK, head); | |
311 | if (head == end) | |
312 | end = bb_note; | |
313 | } | |
314 | else | |
315 | { | |
316 | bb_note = emit_note_before (NOTE_INSN_BASIC_BLOCK, head); | |
317 | head = bb_note; | |
318 | if (!end) | |
319 | end = head; | |
320 | } | |
5f0d2358 | 321 | |
ca6c03ca JH |
322 | NOTE_BASIC_BLOCK (bb_note) = bb; |
323 | } | |
324 | ||
325 | /* Always include the bb note in the block. */ | |
326 | if (NEXT_INSN (end) == bb_note) | |
327 | end = bb_note; | |
328 | ||
a813c111 SB |
329 | BB_HEAD (bb) = head; |
330 | BB_END (bb) = end; | |
852c6ec7 | 331 | bb->index = last_basic_block++; |
5e2d947c | 332 | bb->flags = BB_NEW | BB_RTL; |
918ed612 | 333 | link_block (bb, after); |
557c4b49 | 334 | SET_BASIC_BLOCK_FOR_FN (cfun, bb->index, bb); |
6fb5fa3c | 335 | df_bb_refs_record (bb->index, false); |
ba4f7968 | 336 | update_bb_for_insn (bb); |
076c7ab8 | 337 | BB_SET_PARTITION (bb, BB_UNPARTITIONED); |
ca6c03ca JH |
338 | |
339 | /* Tag the block so that we know it has been used when considering | |
340 | other basic block notes. */ | |
341 | bb->aux = bb; | |
342 | ||
343 | return bb; | |
344 | } | |
345 | ||
5f0d2358 | 346 | /* Create new basic block consisting of instructions in between HEAD and END |
ffe14686 AM |
347 | and place it to the BB chain after block AFTER. END can be NULL to |
348 | create a new empty basic block before HEAD. Both END and HEAD can be | |
349 | NULL to create basic block at the end of INSN chain. */ | |
ca6c03ca | 350 | |
bc35512f JH |
351 | static basic_block |
352 | rtl_create_basic_block (void *headp, void *endp, basic_block after) | |
ca6c03ca | 353 | { |
ae50c0cb | 354 | rtx head = (rtx) headp, end = (rtx) endp; |
ca6c03ca | 355 | basic_block bb; |
0b17ab2f | 356 | |
7eca0767 | 357 | /* Grow the basic block array if needed. */ |
c5f05f68 | 358 | if ((size_t) last_basic_block >= basic_block_info_for_fn (cfun)->length ()) |
7eca0767 JH |
359 | { |
360 | size_t new_size = last_basic_block + (last_basic_block + 3) / 4; | |
c5f05f68 | 361 | vec_safe_grow_cleared (basic_block_info_for_fn (cfun), new_size); |
7eca0767 | 362 | } |
0b17ab2f | 363 | |
0cae8d31 | 364 | n_basic_blocks_for_fn (cfun)++; |
ca6c03ca | 365 | |
852c6ec7 | 366 | bb = create_basic_block_structure (head, end, NULL, after); |
ca6c03ca JH |
367 | bb->aux = NULL; |
368 | return bb; | |
369 | } | |
bc35512f JH |
370 | |
371 | static basic_block | |
372 | cfg_layout_create_basic_block (void *head, void *end, basic_block after) | |
373 | { | |
374 | basic_block newbb = rtl_create_basic_block (head, end, after); | |
375 | ||
bc35512f JH |
376 | return newbb; |
377 | } | |
ca6c03ca JH |
378 | \f |
379 | /* Delete the insns in a (non-live) block. We physically delete every | |
380 | non-deleted-note insn, and update the flow graph appropriately. | |
381 | ||
382 | Return nonzero if we deleted an exception handler. */ | |
383 | ||
384 | /* ??? Preserving all such notes strikes me as wrong. It would be nice | |
385 | to post-process the stream to remove empty blocks, loops, ranges, etc. */ | |
386 | ||
f0fda11c | 387 | static void |
d329e058 | 388 | rtl_delete_block (basic_block b) |
ca6c03ca | 389 | { |
96370780 | 390 | rtx insn, end; |
ca6c03ca JH |
391 | |
392 | /* If the head of this block is a CODE_LABEL, then it might be the | |
f39e46ba SB |
393 | label for an exception handler which can't be reached. We need |
394 | to remove the label from the exception_handler_label list. */ | |
a813c111 | 395 | insn = BB_HEAD (b); |
ca6c03ca | 396 | |
96370780 | 397 | end = get_last_bb_insn (b); |
ca6c03ca JH |
398 | |
399 | /* Selectively delete the entire chain. */ | |
a813c111 | 400 | BB_HEAD (b) = NULL; |
a7b87f73 ZD |
401 | delete_insn_chain (insn, end, true); |
402 | ||
6fb5fa3c DB |
403 | |
404 | if (dump_file) | |
405 | fprintf (dump_file, "deleting block %d\n", b->index); | |
406 | df_bb_delete (b->index); | |
ca6c03ca JH |
407 | } |
408 | \f | |
852c6ec7 | 409 | /* Records the basic block struct in BLOCK_FOR_INSN for every insn. */ |
ca6c03ca JH |
410 | |
411 | void | |
d329e058 | 412 | compute_bb_for_insn (void) |
ca6c03ca | 413 | { |
e0082a72 | 414 | basic_block bb; |
ca6c03ca | 415 | |
e0082a72 | 416 | FOR_EACH_BB (bb) |
ca6c03ca | 417 | { |
a813c111 | 418 | rtx end = BB_END (bb); |
5f0d2358 | 419 | rtx insn; |
ca6c03ca | 420 | |
a813c111 | 421 | for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn)) |
ca6c03ca | 422 | { |
ba4f7968 | 423 | BLOCK_FOR_INSN (insn) = bb; |
ca6c03ca JH |
424 | if (insn == end) |
425 | break; | |
ca6c03ca JH |
426 | } |
427 | } | |
428 | } | |
429 | ||
430 | /* Release the basic_block_for_insn array. */ | |
431 | ||
c2924966 | 432 | unsigned int |
d329e058 | 433 | free_bb_for_insn (void) |
ca6c03ca | 434 | { |
ba4f7968 JH |
435 | rtx insn; |
436 | for (insn = get_insns (); insn; insn = NEXT_INSN (insn)) | |
4b4bf941 | 437 | if (!BARRIER_P (insn)) |
ba4f7968 | 438 | BLOCK_FOR_INSN (insn) = NULL; |
c2924966 | 439 | return 0; |
ca6c03ca JH |
440 | } |
441 | ||
2a34bece EB |
442 | static unsigned int |
443 | rest_of_pass_free_cfg (void) | |
444 | { | |
445 | #ifdef DELAY_SLOTS | |
446 | /* The resource.c machinery uses DF but the CFG isn't guaranteed to be | |
447 | valid at that point so it would be too late to call df_analyze. */ | |
448 | if (optimize > 0 && flag_delayed_branch) | |
ad78b8a6 EB |
449 | { |
450 | df_note_add_problem (); | |
451 | df_analyze (); | |
452 | } | |
2a34bece EB |
453 | #endif |
454 | ||
3371a64f TJ |
455 | if (crtl->has_bb_partition) |
456 | insert_section_boundary_note (); | |
457 | ||
2a34bece EB |
458 | free_bb_for_insn (); |
459 | return 0; | |
460 | } | |
461 | ||
27a4cd48 DM |
462 | namespace { |
463 | ||
464 | const pass_data pass_data_free_cfg = | |
465 | { | |
466 | RTL_PASS, /* type */ | |
467 | "*free_cfg", /* name */ | |
468 | OPTGROUP_NONE, /* optinfo_flags */ | |
469 | false, /* has_gate */ | |
470 | true, /* has_execute */ | |
471 | TV_NONE, /* tv_id */ | |
472 | 0, /* properties_required */ | |
473 | 0, /* properties_provided */ | |
474 | PROP_cfg, /* properties_destroyed */ | |
475 | 0, /* todo_flags_start */ | |
476 | 0, /* todo_flags_finish */ | |
ef330312 PB |
477 | }; |
478 | ||
27a4cd48 DM |
479 | class pass_free_cfg : public rtl_opt_pass |
480 | { | |
481 | public: | |
c3284718 RS |
482 | pass_free_cfg (gcc::context *ctxt) |
483 | : rtl_opt_pass (pass_data_free_cfg, ctxt) | |
27a4cd48 DM |
484 | {} |
485 | ||
486 | /* opt_pass methods: */ | |
487 | unsigned int execute () { return rest_of_pass_free_cfg (); } | |
488 | ||
489 | }; // class pass_free_cfg | |
490 | ||
491 | } // anon namespace | |
492 | ||
493 | rtl_opt_pass * | |
494 | make_pass_free_cfg (gcc::context *ctxt) | |
495 | { | |
496 | return new pass_free_cfg (ctxt); | |
497 | } | |
498 | ||
91278841 AP |
499 | /* Return RTX to emit after when we want to emit code on the entry of function. */ |
500 | rtx | |
501 | entry_of_function (void) | |
502 | { | |
0cae8d31 | 503 | return (n_basic_blocks_for_fn (cfun) > NUM_FIXED_BLOCKS ? |
fefa31b5 | 504 | BB_HEAD (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) : get_insns ()); |
91278841 AP |
505 | } |
506 | ||
11b904a1 BS |
507 | /* Emit INSN at the entry point of the function, ensuring that it is only |
508 | executed once per function. */ | |
509 | void | |
510 | emit_insn_at_entry (rtx insn) | |
511 | { | |
fefa31b5 | 512 | edge_iterator ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs); |
11b904a1 | 513 | edge e = ei_safe_edge (ei); |
5419bc7f | 514 | gcc_assert (e->flags & EDGE_FALLTHRU); |
11b904a1 BS |
515 | |
516 | insert_insn_on_edge (insn, e); | |
517 | commit_edge_insertions (); | |
518 | } | |
519 | ||
6c3d0e31 | 520 | /* Update BLOCK_FOR_INSN of insns between BEGIN and END |
b8698a0f | 521 | (or BARRIER if found) and notify df of the bb change. |
6c3d0e31 SP |
522 | The insn chain range is inclusive |
523 | (i.e. both BEGIN and END will be updated. */ | |
ca6c03ca | 524 | |
6c3d0e31 SP |
525 | static void |
526 | update_bb_for_insn_chain (rtx begin, rtx end, basic_block bb) | |
ca6c03ca JH |
527 | { |
528 | rtx insn; | |
529 | ||
6c3d0e31 SP |
530 | end = NEXT_INSN (end); |
531 | for (insn = begin; insn != end; insn = NEXT_INSN (insn)) | |
63642d5a AO |
532 | if (!BARRIER_P (insn)) |
533 | df_insn_change_bb (insn, bb); | |
ca6c03ca | 534 | } |
6c3d0e31 SP |
535 | |
536 | /* Update BLOCK_FOR_INSN of insns in BB to BB, | |
537 | and notify df of the change. */ | |
538 | ||
539 | void | |
540 | update_bb_for_insn (basic_block bb) | |
541 | { | |
542 | update_bb_for_insn_chain (BB_HEAD (bb), BB_END (bb), bb); | |
543 | } | |
544 | ||
532aafad SB |
545 | \f |
546 | /* Like active_insn_p, except keep the return value clobber around | |
547 | even after reload. */ | |
548 | ||
549 | static bool | |
550 | flow_active_insn_p (const_rtx insn) | |
551 | { | |
552 | if (active_insn_p (insn)) | |
553 | return true; | |
554 | ||
555 | /* A clobber of the function return value exists for buggy | |
556 | programs that fail to return a value. Its effect is to | |
557 | keep the return value from being live across the entire | |
558 | function. If we allow it to be skipped, we introduce the | |
559 | possibility for register lifetime confusion. */ | |
560 | if (GET_CODE (PATTERN (insn)) == CLOBBER | |
561 | && REG_P (XEXP (PATTERN (insn), 0)) | |
562 | && REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0))) | |
563 | return true; | |
564 | ||
565 | return false; | |
566 | } | |
567 | ||
568 | /* Return true if the block has no effect and only forwards control flow to | |
569 | its single destination. */ | |
532aafad SB |
570 | |
571 | bool | |
2407343c | 572 | contains_no_active_insn_p (const_basic_block bb) |
532aafad SB |
573 | { |
574 | rtx insn; | |
575 | ||
fefa31b5 | 576 | if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) |
532aafad SB |
577 | || !single_succ_p (bb)) |
578 | return false; | |
579 | ||
2407343c JR |
580 | for (insn = BB_HEAD (bb); insn != BB_END (bb); insn = NEXT_INSN (insn)) |
581 | if (INSN_P (insn) && flow_active_insn_p (insn)) | |
582 | return false; | |
583 | ||
584 | return (!INSN_P (insn) | |
585 | || (JUMP_P (insn) && simplejump_p (insn)) | |
586 | || !flow_active_insn_p (insn)); | |
587 | } | |
588 | ||
589 | /* Likewise, but protect loop latches, headers and preheaders. */ | |
590 | /* FIXME: Make this a cfg hook. */ | |
591 | ||
592 | bool | |
593 | forwarder_block_p (const_basic_block bb) | |
594 | { | |
595 | if (!contains_no_active_insn_p (bb)) | |
596 | return false; | |
597 | ||
532aafad SB |
598 | /* Protect loop latches, headers and preheaders. */ |
599 | if (current_loops) | |
600 | { | |
601 | basic_block dest; | |
602 | if (bb->loop_father->header == bb) | |
603 | return false; | |
604 | dest = EDGE_SUCC (bb, 0)->dest; | |
605 | if (dest->loop_father->header == dest) | |
606 | return false; | |
607 | } | |
608 | ||
2407343c | 609 | return true; |
532aafad SB |
610 | } |
611 | ||
612 | /* Return nonzero if we can reach target from src by falling through. */ | |
d7b6661b | 613 | /* FIXME: Make this a cfg hook, the result is only valid in cfgrtl mode. */ |
532aafad SB |
614 | |
615 | bool | |
616 | can_fallthru (basic_block src, basic_block target) | |
617 | { | |
618 | rtx insn = BB_END (src); | |
619 | rtx insn2; | |
620 | edge e; | |
621 | edge_iterator ei; | |
622 | ||
fefa31b5 | 623 | if (target == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
532aafad SB |
624 | return true; |
625 | if (src->next_bb != target) | |
d7b6661b SB |
626 | return false; |
627 | ||
628 | /* ??? Later we may add code to move jump tables offline. */ | |
629 | if (tablejump_p (insn, NULL, NULL)) | |
630 | return false; | |
631 | ||
532aafad | 632 | FOR_EACH_EDGE (e, ei, src->succs) |
fefa31b5 | 633 | if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun) |
532aafad | 634 | && e->flags & EDGE_FALLTHRU) |
d7b6661b | 635 | return false; |
532aafad SB |
636 | |
637 | insn2 = BB_HEAD (target); | |
d7b6661b | 638 | if (!active_insn_p (insn2)) |
532aafad SB |
639 | insn2 = next_active_insn (insn2); |
640 | ||
532aafad SB |
641 | return next_active_insn (insn) == insn2; |
642 | } | |
643 | ||
644 | /* Return nonzero if we could reach target from src by falling through, | |
645 | if the target was made adjacent. If we already have a fall-through | |
646 | edge to the exit block, we can't do that. */ | |
647 | static bool | |
648 | could_fall_through (basic_block src, basic_block target) | |
649 | { | |
650 | edge e; | |
651 | edge_iterator ei; | |
652 | ||
fefa31b5 | 653 | if (target == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
532aafad SB |
654 | return true; |
655 | FOR_EACH_EDGE (e, ei, src->succs) | |
fefa31b5 | 656 | if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun) |
532aafad SB |
657 | && e->flags & EDGE_FALLTHRU) |
658 | return 0; | |
659 | return true; | |
660 | } | |
ca6c03ca | 661 | \f |
ef2be249 RS |
662 | /* Return the NOTE_INSN_BASIC_BLOCK of BB. */ |
663 | rtx | |
664 | bb_note (basic_block bb) | |
665 | { | |
666 | rtx note; | |
667 | ||
668 | note = BB_HEAD (bb); | |
669 | if (LABEL_P (note)) | |
670 | note = NEXT_INSN (note); | |
671 | ||
672 | gcc_assert (NOTE_INSN_BASIC_BLOCK_P (note)); | |
673 | return note; | |
674 | } | |
675 | ||
6fb5fa3c DB |
676 | /* Return the INSN immediately following the NOTE_INSN_BASIC_BLOCK |
677 | note associated with the BLOCK. */ | |
678 | ||
679 | static rtx | |
680 | first_insn_after_basic_block_note (basic_block block) | |
681 | { | |
682 | rtx insn; | |
683 | ||
684 | /* Get the first instruction in the block. */ | |
685 | insn = BB_HEAD (block); | |
686 | ||
687 | if (insn == NULL_RTX) | |
688 | return NULL_RTX; | |
689 | if (LABEL_P (insn)) | |
690 | insn = NEXT_INSN (insn); | |
691 | gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn)); | |
692 | ||
693 | return NEXT_INSN (insn); | |
694 | } | |
695 | ||
f470c378 ZD |
696 | /* Creates a new basic block just after basic block B by splitting |
697 | everything after specified instruction I. */ | |
ca6c03ca | 698 | |
f470c378 | 699 | static basic_block |
d329e058 | 700 | rtl_split_block (basic_block bb, void *insnp) |
ca6c03ca JH |
701 | { |
702 | basic_block new_bb; | |
ae50c0cb | 703 | rtx insn = (rtx) insnp; |
f470c378 | 704 | edge e; |
628f6a4e | 705 | edge_iterator ei; |
ca6c03ca | 706 | |
f470c378 ZD |
707 | if (!insn) |
708 | { | |
709 | insn = first_insn_after_basic_block_note (bb); | |
710 | ||
711 | if (insn) | |
b5b8b0ac AO |
712 | { |
713 | rtx next = insn; | |
714 | ||
715 | insn = PREV_INSN (insn); | |
716 | ||
717 | /* If the block contains only debug insns, insn would have | |
718 | been NULL in a non-debug compilation, and then we'd end | |
719 | up emitting a DELETED note. For -fcompare-debug | |
720 | stability, emit the note too. */ | |
721 | if (insn != BB_END (bb) | |
722 | && DEBUG_INSN_P (next) | |
723 | && DEBUG_INSN_P (BB_END (bb))) | |
724 | { | |
725 | while (next != BB_END (bb) && DEBUG_INSN_P (next)) | |
726 | next = NEXT_INSN (next); | |
727 | ||
728 | if (next == BB_END (bb)) | |
729 | emit_note_after (NOTE_INSN_DELETED, next); | |
730 | } | |
731 | } | |
f470c378 ZD |
732 | else |
733 | insn = get_last_insn (); | |
734 | } | |
735 | ||
736 | /* We probably should check type of the insn so that we do not create | |
737 | inconsistent cfg. It is checked in verify_flow_info anyway, so do not | |
738 | bother. */ | |
739 | if (insn == BB_END (bb)) | |
740 | emit_note_after (NOTE_INSN_DELETED, insn); | |
ca6c03ca JH |
741 | |
742 | /* Create the new basic block. */ | |
a813c111 | 743 | new_bb = create_basic_block (NEXT_INSN (insn), BB_END (bb), bb); |
076c7ab8 | 744 | BB_COPY_PARTITION (new_bb, bb); |
a813c111 | 745 | BB_END (bb) = insn; |
ca6c03ca JH |
746 | |
747 | /* Redirect the outgoing edges. */ | |
628f6a4e BE |
748 | new_bb->succs = bb->succs; |
749 | bb->succs = NULL; | |
750 | FOR_EACH_EDGE (e, ei, new_bb->succs) | |
ca6c03ca JH |
751 | e->src = new_bb; |
752 | ||
6fb5fa3c DB |
753 | /* The new block starts off being dirty. */ |
754 | df_set_bb_dirty (bb); | |
f470c378 | 755 | return new_bb; |
bc35512f JH |
756 | } |
757 | ||
9be94227 EB |
758 | /* Return true if the single edge between blocks A and B is the only place |
759 | in RTL which holds some unique locus. */ | |
760 | ||
761 | static bool | |
762 | unique_locus_on_edge_between_p (basic_block a, basic_block b) | |
763 | { | |
5368224f | 764 | const location_t goto_locus = EDGE_SUCC (a, 0)->goto_locus; |
9be94227 EB |
765 | rtx insn, end; |
766 | ||
2f13f2de | 767 | if (LOCATION_LOCUS (goto_locus) == UNKNOWN_LOCATION) |
9be94227 EB |
768 | return false; |
769 | ||
770 | /* First scan block A backward. */ | |
771 | insn = BB_END (a); | |
772 | end = PREV_INSN (BB_HEAD (a)); | |
5368224f | 773 | while (insn != end && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn))) |
9be94227 EB |
774 | insn = PREV_INSN (insn); |
775 | ||
5368224f | 776 | if (insn != end && INSN_LOCATION (insn) == goto_locus) |
9be94227 EB |
777 | return false; |
778 | ||
779 | /* Then scan block B forward. */ | |
780 | insn = BB_HEAD (b); | |
781 | if (insn) | |
782 | { | |
783 | end = NEXT_INSN (BB_END (b)); | |
784 | while (insn != end && !NONDEBUG_INSN_P (insn)) | |
785 | insn = NEXT_INSN (insn); | |
786 | ||
5368224f DC |
787 | if (insn != end && INSN_HAS_LOCATION (insn) |
788 | && INSN_LOCATION (insn) == goto_locus) | |
9be94227 EB |
789 | return false; |
790 | } | |
791 | ||
792 | return true; | |
793 | } | |
794 | ||
795 | /* If the single edge between blocks A and B is the only place in RTL which | |
796 | holds some unique locus, emit a nop with that locus between the blocks. */ | |
797 | ||
798 | static void | |
799 | emit_nop_for_unique_locus_between (basic_block a, basic_block b) | |
800 | { | |
801 | if (!unique_locus_on_edge_between_p (a, b)) | |
802 | return; | |
803 | ||
804 | BB_END (a) = emit_insn_after_noloc (gen_nop (), BB_END (a), a); | |
5368224f | 805 | INSN_LOCATION (BB_END (a)) = EDGE_SUCC (a, 0)->goto_locus; |
9be94227 EB |
806 | } |
807 | ||
ca6c03ca | 808 | /* Blocks A and B are to be merged into a single block A. The insns |
bc35512f | 809 | are already contiguous. */ |
ca6c03ca | 810 | |
bc35512f JH |
811 | static void |
812 | rtl_merge_blocks (basic_block a, basic_block b) | |
ca6c03ca | 813 | { |
a813c111 | 814 | rtx b_head = BB_HEAD (b), b_end = BB_END (b), a_end = BB_END (a); |
ca6c03ca | 815 | rtx del_first = NULL_RTX, del_last = NULL_RTX; |
b5b8b0ac | 816 | rtx b_debug_start = b_end, b_debug_end = b_end; |
50a36e42 | 817 | bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0; |
ca6c03ca JH |
818 | int b_empty = 0; |
819 | ||
6fb5fa3c | 820 | if (dump_file) |
50a36e42 EB |
821 | fprintf (dump_file, "Merging block %d into block %d...\n", b->index, |
822 | a->index); | |
6fb5fa3c | 823 | |
b5b8b0ac AO |
824 | while (DEBUG_INSN_P (b_end)) |
825 | b_end = PREV_INSN (b_debug_start = b_end); | |
826 | ||
ca6c03ca | 827 | /* If there was a CODE_LABEL beginning B, delete it. */ |
4b4bf941 | 828 | if (LABEL_P (b_head)) |
ca6c03ca JH |
829 | { |
830 | /* Detect basic blocks with nothing but a label. This can happen | |
831 | in particular at the end of a function. */ | |
832 | if (b_head == b_end) | |
833 | b_empty = 1; | |
5f0d2358 | 834 | |
ca6c03ca JH |
835 | del_first = del_last = b_head; |
836 | b_head = NEXT_INSN (b_head); | |
837 | } | |
838 | ||
5f0d2358 RK |
839 | /* Delete the basic block note and handle blocks containing just that |
840 | note. */ | |
ca6c03ca JH |
841 | if (NOTE_INSN_BASIC_BLOCK_P (b_head)) |
842 | { | |
843 | if (b_head == b_end) | |
844 | b_empty = 1; | |
845 | if (! del_last) | |
846 | del_first = b_head; | |
5f0d2358 | 847 | |
ca6c03ca JH |
848 | del_last = b_head; |
849 | b_head = NEXT_INSN (b_head); | |
850 | } | |
851 | ||
852 | /* If there was a jump out of A, delete it. */ | |
4b4bf941 | 853 | if (JUMP_P (a_end)) |
ca6c03ca JH |
854 | { |
855 | rtx prev; | |
856 | ||
857 | for (prev = PREV_INSN (a_end); ; prev = PREV_INSN (prev)) | |
4b4bf941 | 858 | if (!NOTE_P (prev) |
a38e7aa5 | 859 | || NOTE_INSN_BASIC_BLOCK_P (prev) |
a813c111 | 860 | || prev == BB_HEAD (a)) |
ca6c03ca JH |
861 | break; |
862 | ||
863 | del_first = a_end; | |
864 | ||
865 | #ifdef HAVE_cc0 | |
866 | /* If this was a conditional jump, we need to also delete | |
867 | the insn that set cc0. */ | |
868 | if (only_sets_cc0_p (prev)) | |
869 | { | |
870 | rtx tmp = prev; | |
5f0d2358 | 871 | |
ca6c03ca JH |
872 | prev = prev_nonnote_insn (prev); |
873 | if (!prev) | |
a813c111 | 874 | prev = BB_HEAD (a); |
ca6c03ca JH |
875 | del_first = tmp; |
876 | } | |
877 | #endif | |
878 | ||
879 | a_end = PREV_INSN (del_first); | |
880 | } | |
4b4bf941 | 881 | else if (BARRIER_P (NEXT_INSN (a_end))) |
ca6c03ca JH |
882 | del_first = NEXT_INSN (a_end); |
883 | ||
ca6c03ca JH |
884 | /* Delete everything marked above as well as crap that might be |
885 | hanging out between the two blocks. */ | |
9be94227 EB |
886 | BB_END (a) = a_end; |
887 | BB_HEAD (b) = b_empty ? NULL_RTX : b_head; | |
a7b87f73 | 888 | delete_insn_chain (del_first, del_last, true); |
ca6c03ca | 889 | |
9be94227 EB |
890 | /* When not optimizing CFG and the edge is the only place in RTL which holds |
891 | some unique locus, emit a nop with that locus in between. */ | |
892 | if (!optimize) | |
893 | { | |
894 | emit_nop_for_unique_locus_between (a, b); | |
895 | a_end = BB_END (a); | |
896 | } | |
897 | ||
ca6c03ca JH |
898 | /* Reassociate the insns of B with A. */ |
899 | if (!b_empty) | |
900 | { | |
b5b8b0ac | 901 | update_bb_for_insn_chain (a_end, b_debug_end, a); |
5f0d2358 | 902 | |
9be94227 EB |
903 | BB_END (a) = b_debug_end; |
904 | BB_HEAD (b) = NULL_RTX; | |
b5b8b0ac AO |
905 | } |
906 | else if (b_end != b_debug_end) | |
907 | { | |
908 | /* Move any deleted labels and other notes between the end of A | |
909 | and the debug insns that make up B after the debug insns, | |
910 | bringing the debug insns into A while keeping the notes after | |
911 | the end of A. */ | |
912 | if (NEXT_INSN (a_end) != b_debug_start) | |
913 | reorder_insns_nobb (NEXT_INSN (a_end), PREV_INSN (b_debug_start), | |
914 | b_debug_end); | |
915 | update_bb_for_insn_chain (b_debug_start, b_debug_end, a); | |
9be94227 | 916 | BB_END (a) = b_debug_end; |
ca6c03ca | 917 | } |
5f0d2358 | 918 | |
6fb5fa3c | 919 | df_bb_delete (b->index); |
50a36e42 EB |
920 | |
921 | /* If B was a forwarder block, propagate the locus on the edge. */ | |
fbc68f2a DC |
922 | if (forwarder_p |
923 | && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION) | |
50a36e42 EB |
924 | EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus; |
925 | ||
926 | if (dump_file) | |
927 | fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index); | |
ca6c03ca | 928 | } |
bc35512f | 929 | |
6fb5fa3c | 930 | |
bc35512f | 931 | /* Return true when block A and B can be merged. */ |
9678086d | 932 | |
b48d0358 DN |
933 | static bool |
934 | rtl_can_merge_blocks (basic_block a, basic_block b) | |
bc35512f | 935 | { |
750054a2 CT |
936 | /* If we are partitioning hot/cold basic blocks, we don't want to |
937 | mess up unconditional or indirect jumps that cross between hot | |
076c7ab8 ZW |
938 | and cold sections. |
939 | ||
8e8d5162 | 940 | Basic block partitioning may result in some jumps that appear to |
c22cacf3 MS |
941 | be optimizable (or blocks that appear to be mergeable), but which really |
942 | must be left untouched (they are required to make it safely across | |
943 | partition boundaries). See the comments at the top of | |
8e8d5162 | 944 | bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */ |
076c7ab8 | 945 | |
87c8b4be | 946 | if (BB_PARTITION (a) != BB_PARTITION (b)) |
076c7ab8 | 947 | return false; |
750054a2 | 948 | |
7d776ee2 RG |
949 | /* Protect the loop latches. */ |
950 | if (current_loops && b->loop_father->latch == b) | |
951 | return false; | |
952 | ||
bc35512f | 953 | /* There must be exactly one edge in between the blocks. */ |
c5cbcccf ZD |
954 | return (single_succ_p (a) |
955 | && single_succ (a) == b | |
956 | && single_pred_p (b) | |
628f6a4e | 957 | && a != b |
bc35512f | 958 | /* Must be simple edge. */ |
c5cbcccf | 959 | && !(single_succ_edge (a)->flags & EDGE_COMPLEX) |
bc35512f | 960 | && a->next_bb == b |
fefa31b5 DM |
961 | && a != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
962 | && b != EXIT_BLOCK_PTR_FOR_FN (cfun) | |
bc35512f JH |
963 | /* If the jump insn has side effects, |
964 | we can't kill the edge. */ | |
4b4bf941 | 965 | && (!JUMP_P (BB_END (a)) |
e24e7211 | 966 | || (reload_completed |
a813c111 | 967 | ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a))))); |
bc35512f | 968 | } |
ca6c03ca | 969 | \f |
5f0d2358 RK |
970 | /* Return the label in the head of basic block BLOCK. Create one if it doesn't |
971 | exist. */ | |
ca6c03ca JH |
972 | |
973 | rtx | |
d329e058 | 974 | block_label (basic_block block) |
ca6c03ca | 975 | { |
fefa31b5 | 976 | if (block == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
ca6c03ca | 977 | return NULL_RTX; |
5f0d2358 | 978 | |
4b4bf941 | 979 | if (!LABEL_P (BB_HEAD (block))) |
ca6c03ca | 980 | { |
a813c111 | 981 | BB_HEAD (block) = emit_label_before (gen_label_rtx (), BB_HEAD (block)); |
ca6c03ca | 982 | } |
5f0d2358 | 983 | |
a813c111 | 984 | return BB_HEAD (block); |
ca6c03ca JH |
985 | } |
986 | ||
987 | /* Attempt to perform edge redirection by replacing possibly complex jump | |
5f0d2358 RK |
988 | instruction by unconditional jump or removing jump completely. This can |
989 | apply only if all edges now point to the same block. The parameters and | |
990 | return values are equivalent to redirect_edge_and_branch. */ | |
ca6c03ca | 991 | |
6de9cd9a | 992 | edge |
bc35512f | 993 | try_redirect_by_replacing_jump (edge e, basic_block target, bool in_cfglayout) |
ca6c03ca JH |
994 | { |
995 | basic_block src = e->src; | |
a813c111 | 996 | rtx insn = BB_END (src), kill_from; |
e1233a7d | 997 | rtx set; |
ca6c03ca | 998 | int fallthru = 0; |
750054a2 CT |
999 | |
1000 | /* If we are partitioning hot/cold basic blocks, we don't want to | |
1001 | mess up unconditional or indirect jumps that cross between hot | |
8e8d5162 CT |
1002 | and cold sections. |
1003 | ||
1004 | Basic block partitioning may result in some jumps that appear to | |
c22cacf3 MS |
1005 | be optimizable (or blocks that appear to be mergeable), but which really |
1006 | must be left untouched (they are required to make it safely across | |
1007 | partition boundaries). See the comments at the top of | |
8e8d5162 | 1008 | bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */ |
c22cacf3 | 1009 | |
3371a64f | 1010 | if (BB_PARTITION (src) != BB_PARTITION (target)) |
9cf84a3c | 1011 | return NULL; |
750054a2 | 1012 | |
6a66a8a7 KH |
1013 | /* We can replace or remove a complex jump only when we have exactly |
1014 | two edges. Also, if we have exactly one outgoing edge, we can | |
1015 | redirect that. */ | |
1016 | if (EDGE_COUNT (src->succs) >= 3 | |
1017 | /* Verify that all targets will be TARGET. Specifically, the | |
1018 | edge that is not E must also go to TARGET. */ | |
1019 | || (EDGE_COUNT (src->succs) == 2 | |
1020 | && EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest != target)) | |
1021 | return NULL; | |
5f0d2358 | 1022 | |
6a66a8a7 | 1023 | if (!onlyjump_p (insn)) |
6de9cd9a | 1024 | return NULL; |
3348b696 | 1025 | if ((!optimize || reload_completed) && tablejump_p (insn, NULL, NULL)) |
6de9cd9a | 1026 | return NULL; |
ca6c03ca JH |
1027 | |
1028 | /* Avoid removing branch with side effects. */ | |
1029 | set = single_set (insn); | |
1030 | if (!set || side_effects_p (set)) | |
6de9cd9a | 1031 | return NULL; |
ca6c03ca JH |
1032 | |
1033 | /* In case we zap a conditional jump, we'll need to kill | |
1034 | the cc0 setter too. */ | |
1035 | kill_from = insn; | |
1036 | #ifdef HAVE_cc0 | |
9caea4a7 RS |
1037 | if (reg_mentioned_p (cc0_rtx, PATTERN (insn)) |
1038 | && only_sets_cc0_p (PREV_INSN (insn))) | |
ca6c03ca JH |
1039 | kill_from = PREV_INSN (insn); |
1040 | #endif | |
1041 | ||
1042 | /* See if we can create the fallthru edge. */ | |
bc35512f | 1043 | if (in_cfglayout || can_fallthru (src, target)) |
ca6c03ca | 1044 | { |
c263766c RH |
1045 | if (dump_file) |
1046 | fprintf (dump_file, "Removing jump %i.\n", INSN_UID (insn)); | |
ca6c03ca JH |
1047 | fallthru = 1; |
1048 | ||
eaec9b3d | 1049 | /* Selectively unlink whole insn chain. */ |
bc35512f JH |
1050 | if (in_cfglayout) |
1051 | { | |
bcc708fc | 1052 | rtx insn = BB_FOOTER (src); |
bc35512f | 1053 | |
a7b87f73 | 1054 | delete_insn_chain (kill_from, BB_END (src), false); |
bc35512f JH |
1055 | |
1056 | /* Remove barriers but keep jumptables. */ | |
1057 | while (insn) | |
1058 | { | |
4b4bf941 | 1059 | if (BARRIER_P (insn)) |
bc35512f JH |
1060 | { |
1061 | if (PREV_INSN (insn)) | |
1062 | NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn); | |
1063 | else | |
bcc708fc | 1064 | BB_FOOTER (src) = NEXT_INSN (insn); |
bc35512f JH |
1065 | if (NEXT_INSN (insn)) |
1066 | PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn); | |
1067 | } | |
4b4bf941 | 1068 | if (LABEL_P (insn)) |
bc35512f JH |
1069 | break; |
1070 | insn = NEXT_INSN (insn); | |
1071 | } | |
1072 | } | |
1073 | else | |
a7b87f73 ZD |
1074 | delete_insn_chain (kill_from, PREV_INSN (BB_HEAD (target)), |
1075 | false); | |
ca6c03ca | 1076 | } |
5f0d2358 | 1077 | |
ca6c03ca JH |
1078 | /* If this already is simplejump, redirect it. */ |
1079 | else if (simplejump_p (insn)) | |
1080 | { | |
1081 | if (e->dest == target) | |
6de9cd9a | 1082 | return NULL; |
c263766c RH |
1083 | if (dump_file) |
1084 | fprintf (dump_file, "Redirecting jump %i from %i to %i.\n", | |
0b17ab2f | 1085 | INSN_UID (insn), e->dest->index, target->index); |
6ee3c8e4 JJ |
1086 | if (!redirect_jump (insn, block_label (target), 0)) |
1087 | { | |
fefa31b5 | 1088 | gcc_assert (target == EXIT_BLOCK_PTR_FOR_FN (cfun)); |
341c100f | 1089 | return NULL; |
6ee3c8e4 | 1090 | } |
ca6c03ca | 1091 | } |
5f0d2358 | 1092 | |
6ee3c8e4 | 1093 | /* Cannot do anything for target exit block. */ |
fefa31b5 | 1094 | else if (target == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
6de9cd9a | 1095 | return NULL; |
6ee3c8e4 | 1096 | |
ca6c03ca JH |
1097 | /* Or replace possibly complicated jump insn by simple jump insn. */ |
1098 | else | |
1099 | { | |
1100 | rtx target_label = block_label (target); | |
eb5b8ad4 | 1101 | rtx barrier, label, table; |
ca6c03ca | 1102 | |
a7102479 | 1103 | emit_jump_insn_after_noloc (gen_jump (target_label), insn); |
a813c111 | 1104 | JUMP_LABEL (BB_END (src)) = target_label; |
ca6c03ca | 1105 | LABEL_NUSES (target_label)++; |
c263766c RH |
1106 | if (dump_file) |
1107 | fprintf (dump_file, "Replacing insn %i by jump %i\n", | |
a813c111 | 1108 | INSN_UID (insn), INSN_UID (BB_END (src))); |
ca6c03ca | 1109 | |
4da2eb6b | 1110 | |
ba4807a0 LB |
1111 | delete_insn_chain (kill_from, insn, false); |
1112 | ||
4da2eb6b RH |
1113 | /* Recognize a tablejump that we are converting to a |
1114 | simple jump and remove its associated CODE_LABEL | |
1115 | and ADDR_VEC or ADDR_DIFF_VEC. */ | |
ba4807a0 LB |
1116 | if (tablejump_p (insn, &label, &table)) |
1117 | delete_insn_chain (label, table, false); | |
eb5b8ad4 | 1118 | |
a813c111 | 1119 | barrier = next_nonnote_insn (BB_END (src)); |
4b4bf941 | 1120 | if (!barrier || !BARRIER_P (barrier)) |
a813c111 | 1121 | emit_barrier_after (BB_END (src)); |
5d693491 JZ |
1122 | else |
1123 | { | |
a813c111 | 1124 | if (barrier != NEXT_INSN (BB_END (src))) |
5d693491 JZ |
1125 | { |
1126 | /* Move the jump before barrier so that the notes | |
1127 | which originally were or were created before jump table are | |
1128 | inside the basic block. */ | |
a813c111 | 1129 | rtx new_insn = BB_END (src); |
5d693491 | 1130 | |
6c3d0e31 SP |
1131 | update_bb_for_insn_chain (NEXT_INSN (BB_END (src)), |
1132 | PREV_INSN (barrier), src); | |
5d693491 JZ |
1133 | |
1134 | NEXT_INSN (PREV_INSN (new_insn)) = NEXT_INSN (new_insn); | |
1135 | PREV_INSN (NEXT_INSN (new_insn)) = PREV_INSN (new_insn); | |
1136 | ||
1137 | NEXT_INSN (new_insn) = barrier; | |
1138 | NEXT_INSN (PREV_INSN (barrier)) = new_insn; | |
1139 | ||
1140 | PREV_INSN (new_insn) = PREV_INSN (barrier); | |
1141 | PREV_INSN (barrier) = new_insn; | |
1142 | } | |
1143 | } | |
ca6c03ca JH |
1144 | } |
1145 | ||
1146 | /* Keep only one edge out and set proper flags. */ | |
c5cbcccf | 1147 | if (!single_succ_p (src)) |
628f6a4e | 1148 | remove_edge (e); |
c5cbcccf | 1149 | gcc_assert (single_succ_p (src)); |
628f6a4e | 1150 | |
c5cbcccf | 1151 | e = single_succ_edge (src); |
ca6c03ca JH |
1152 | if (fallthru) |
1153 | e->flags = EDGE_FALLTHRU; | |
1154 | else | |
1155 | e->flags = 0; | |
5f0d2358 | 1156 | |
ca6c03ca JH |
1157 | e->probability = REG_BR_PROB_BASE; |
1158 | e->count = src->count; | |
1159 | ||
ca6c03ca JH |
1160 | if (e->dest != target) |
1161 | redirect_edge_succ (e, target); | |
6de9cd9a | 1162 | return e; |
ca6c03ca JH |
1163 | } |
1164 | ||
4e3825db MM |
1165 | /* Subroutine of redirect_branch_edge that tries to patch the jump |
1166 | instruction INSN so that it reaches block NEW. Do this | |
1167 | only when it originally reached block OLD. Return true if this | |
1168 | worked or the original target wasn't OLD, return false if redirection | |
1169 | doesn't work. */ | |
1170 | ||
1171 | static bool | |
1172 | patch_jump_insn (rtx insn, rtx old_label, basic_block new_bb) | |
ca6c03ca JH |
1173 | { |
1174 | rtx tmp; | |
ca6c03ca | 1175 | /* Recognize a tablejump and adjust all matching cases. */ |
e1233a7d | 1176 | if (tablejump_p (insn, NULL, &tmp)) |
ca6c03ca JH |
1177 | { |
1178 | rtvec vec; | |
1179 | int j; | |
4e3825db | 1180 | rtx new_label = block_label (new_bb); |
ca6c03ca | 1181 | |
fefa31b5 | 1182 | if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
4e3825db | 1183 | return false; |
ca6c03ca JH |
1184 | if (GET_CODE (PATTERN (tmp)) == ADDR_VEC) |
1185 | vec = XVEC (PATTERN (tmp), 0); | |
1186 | else | |
1187 | vec = XVEC (PATTERN (tmp), 1); | |
1188 | ||
1189 | for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j) | |
1190 | if (XEXP (RTVEC_ELT (vec, j), 0) == old_label) | |
1191 | { | |
1192 | RTVEC_ELT (vec, j) = gen_rtx_LABEL_REF (Pmode, new_label); | |
1193 | --LABEL_NUSES (old_label); | |
1194 | ++LABEL_NUSES (new_label); | |
1195 | } | |
1196 | ||
f9da5064 | 1197 | /* Handle casesi dispatch insns. */ |
ca6c03ca JH |
1198 | if ((tmp = single_set (insn)) != NULL |
1199 | && SET_DEST (tmp) == pc_rtx | |
1200 | && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE | |
1201 | && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF | |
1202 | && XEXP (XEXP (SET_SRC (tmp), 2), 0) == old_label) | |
1203 | { | |
4c33cb26 | 1204 | XEXP (SET_SRC (tmp), 2) = gen_rtx_LABEL_REF (Pmode, |
ca6c03ca JH |
1205 | new_label); |
1206 | --LABEL_NUSES (old_label); | |
1207 | ++LABEL_NUSES (new_label); | |
1208 | } | |
1209 | } | |
1c384bf1 RH |
1210 | else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL) |
1211 | { | |
1212 | int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp); | |
1213 | rtx new_label, note; | |
1214 | ||
fefa31b5 | 1215 | if (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
1c384bf1 RH |
1216 | return false; |
1217 | new_label = block_label (new_bb); | |
1218 | ||
1219 | for (i = 0; i < n; ++i) | |
1220 | { | |
1221 | rtx old_ref = ASM_OPERANDS_LABEL (tmp, i); | |
1222 | gcc_assert (GET_CODE (old_ref) == LABEL_REF); | |
1223 | if (XEXP (old_ref, 0) == old_label) | |
1224 | { | |
1225 | ASM_OPERANDS_LABEL (tmp, i) | |
1226 | = gen_rtx_LABEL_REF (Pmode, new_label); | |
1227 | --LABEL_NUSES (old_label); | |
1228 | ++LABEL_NUSES (new_label); | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | if (JUMP_LABEL (insn) == old_label) | |
1233 | { | |
1234 | JUMP_LABEL (insn) = new_label; | |
1235 | note = find_reg_note (insn, REG_LABEL_TARGET, new_label); | |
1236 | if (note) | |
1237 | remove_note (insn, note); | |
1238 | } | |
1239 | else | |
1240 | { | |
1241 | note = find_reg_note (insn, REG_LABEL_TARGET, old_label); | |
1242 | if (note) | |
1243 | remove_note (insn, note); | |
1244 | if (JUMP_LABEL (insn) != new_label | |
1245 | && !find_reg_note (insn, REG_LABEL_TARGET, new_label)) | |
1246 | add_reg_note (insn, REG_LABEL_TARGET, new_label); | |
1247 | } | |
3b5fda81 JJ |
1248 | while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label)) |
1249 | != NULL_RTX) | |
1250 | XEXP (note, 0) = new_label; | |
1c384bf1 | 1251 | } |
ca6c03ca JH |
1252 | else |
1253 | { | |
1254 | /* ?? We may play the games with moving the named labels from | |
1255 | one basic block to the other in case only one computed_jump is | |
1256 | available. */ | |
5f0d2358 RK |
1257 | if (computed_jump_p (insn) |
1258 | /* A return instruction can't be redirected. */ | |
1259 | || returnjump_p (insn)) | |
4e3825db | 1260 | return false; |
6ee3c8e4 | 1261 | |
4e3825db | 1262 | if (!currently_expanding_to_rtl || JUMP_LABEL (insn) == old_label) |
6ee3c8e4 | 1263 | { |
4e3825db MM |
1264 | /* If the insn doesn't go where we think, we're confused. */ |
1265 | gcc_assert (JUMP_LABEL (insn) == old_label); | |
1266 | ||
1267 | /* If the substitution doesn't succeed, die. This can happen | |
1268 | if the back end emitted unrecognizable instructions or if | |
1269 | target is exit block on some arches. */ | |
1270 | if (!redirect_jump (insn, block_label (new_bb), 0)) | |
1271 | { | |
fefa31b5 | 1272 | gcc_assert (new_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)); |
4e3825db MM |
1273 | return false; |
1274 | } | |
6ee3c8e4 | 1275 | } |
ca6c03ca | 1276 | } |
4e3825db MM |
1277 | return true; |
1278 | } | |
1279 | ||
1280 | ||
1281 | /* Redirect edge representing branch of (un)conditional jump or tablejump, | |
1282 | NULL on failure */ | |
1283 | static edge | |
1284 | redirect_branch_edge (edge e, basic_block target) | |
1285 | { | |
1286 | rtx old_label = BB_HEAD (e->dest); | |
1287 | basic_block src = e->src; | |
1288 | rtx insn = BB_END (src); | |
1289 | ||
1290 | /* We can only redirect non-fallthru edges of jump insn. */ | |
1291 | if (e->flags & EDGE_FALLTHRU) | |
1292 | return NULL; | |
1293 | else if (!JUMP_P (insn) && !currently_expanding_to_rtl) | |
1294 | return NULL; | |
1295 | ||
1296 | if (!currently_expanding_to_rtl) | |
1297 | { | |
1298 | if (!patch_jump_insn (insn, old_label, target)) | |
1299 | return NULL; | |
1300 | } | |
1301 | else | |
1302 | /* When expanding this BB might actually contain multiple | |
1303 | jumps (i.e. not yet split by find_many_sub_basic_blocks). | |
1304 | Redirect all of those that match our label. */ | |
0f346928 | 1305 | FOR_BB_INSNS (src, insn) |
4e3825db MM |
1306 | if (JUMP_P (insn) && !patch_jump_insn (insn, old_label, target)) |
1307 | return NULL; | |
ca6c03ca | 1308 | |
c263766c RH |
1309 | if (dump_file) |
1310 | fprintf (dump_file, "Edge %i->%i redirected to %i\n", | |
0b17ab2f | 1311 | e->src->index, e->dest->index, target->index); |
5f0d2358 | 1312 | |
ca6c03ca | 1313 | if (e->dest != target) |
6de9cd9a | 1314 | e = redirect_edge_succ_nodup (e, target); |
6fb5fa3c | 1315 | |
6de9cd9a | 1316 | return e; |
bc35512f JH |
1317 | } |
1318 | ||
3371a64f TJ |
1319 | /* Called when edge E has been redirected to a new destination, |
1320 | in order to update the region crossing flag on the edge and | |
1321 | jump. */ | |
1322 | ||
1323 | static void | |
1324 | fixup_partition_crossing (edge e) | |
1325 | { | |
1326 | rtx note; | |
1327 | ||
fefa31b5 DM |
1328 | if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) || e->dest |
1329 | == EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
3371a64f TJ |
1330 | return; |
1331 | /* If we redirected an existing edge, it may already be marked | |
1332 | crossing, even though the new src is missing a reg crossing note. | |
1333 | But make sure reg crossing note doesn't already exist before | |
1334 | inserting. */ | |
1335 | if (BB_PARTITION (e->src) != BB_PARTITION (e->dest)) | |
1336 | { | |
1337 | e->flags |= EDGE_CROSSING; | |
1338 | note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX); | |
1339 | if (JUMP_P (BB_END (e->src)) | |
1340 | && !note) | |
1341 | add_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX); | |
1342 | } | |
1343 | else if (BB_PARTITION (e->src) == BB_PARTITION (e->dest)) | |
1344 | { | |
1345 | e->flags &= ~EDGE_CROSSING; | |
1346 | /* Remove the section crossing note from jump at end of | |
1347 | src if it exists, and if no other successors are | |
1348 | still crossing. */ | |
1349 | note = find_reg_note (BB_END (e->src), REG_CROSSING_JUMP, NULL_RTX); | |
1350 | if (note) | |
1351 | { | |
1352 | bool has_crossing_succ = false; | |
1353 | edge e2; | |
1354 | edge_iterator ei; | |
1355 | FOR_EACH_EDGE (e2, ei, e->src->succs) | |
1356 | { | |
1357 | has_crossing_succ |= (e2->flags & EDGE_CROSSING); | |
1358 | if (has_crossing_succ) | |
1359 | break; | |
1360 | } | |
1361 | if (!has_crossing_succ) | |
1362 | remove_note (BB_END (e->src), note); | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | ||
600b5b1d TJ |
1367 | /* Called when block BB has been reassigned to the cold partition, |
1368 | because it is now dominated by another cold block, | |
1369 | to ensure that the region crossing attributes are updated. */ | |
1370 | ||
1371 | static void | |
1372 | fixup_new_cold_bb (basic_block bb) | |
1373 | { | |
1374 | edge e; | |
1375 | edge_iterator ei; | |
1376 | ||
1377 | /* This is called when a hot bb is found to now be dominated | |
1378 | by a cold bb and therefore needs to become cold. Therefore, | |
1379 | its preds will no longer be region crossing. Any non-dominating | |
1380 | preds that were previously hot would also have become cold | |
1381 | in the caller for the same region. Any preds that were previously | |
1382 | region-crossing will be adjusted in fixup_partition_crossing. */ | |
1383 | FOR_EACH_EDGE (e, ei, bb->preds) | |
1384 | { | |
1385 | fixup_partition_crossing (e); | |
1386 | } | |
1387 | ||
1388 | /* Possibly need to make bb's successor edges region crossing, | |
1389 | or remove stale region crossing. */ | |
1390 | FOR_EACH_EDGE (e, ei, bb->succs) | |
1391 | { | |
1392 | /* We can't have fall-through edges across partition boundaries. | |
1393 | Note that force_nonfallthru will do any necessary partition | |
1394 | boundary fixup by calling fixup_partition_crossing itself. */ | |
1395 | if ((e->flags & EDGE_FALLTHRU) | |
1396 | && BB_PARTITION (bb) != BB_PARTITION (e->dest) | |
fefa31b5 | 1397 | && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
600b5b1d TJ |
1398 | force_nonfallthru (e); |
1399 | else | |
1400 | fixup_partition_crossing (e); | |
1401 | } | |
1402 | } | |
1403 | ||
bc35512f JH |
1404 | /* Attempt to change code to redirect edge E to TARGET. Don't do that on |
1405 | expense of adding new instructions or reordering basic blocks. | |
1406 | ||
1407 | Function can be also called with edge destination equivalent to the TARGET. | |
1408 | Then it should try the simplifications and do nothing if none is possible. | |
1409 | ||
6de9cd9a DN |
1410 | Return edge representing the branch if transformation succeeded. Return NULL |
1411 | on failure. | |
1412 | We still return NULL in case E already destinated TARGET and we didn't | |
1413 | managed to simplify instruction stream. */ | |
bc35512f | 1414 | |
6de9cd9a | 1415 | static edge |
5671bf27 | 1416 | rtl_redirect_edge_and_branch (edge e, basic_block target) |
bc35512f | 1417 | { |
6de9cd9a | 1418 | edge ret; |
f345f21a | 1419 | basic_block src = e->src; |
3371a64f | 1420 | basic_block dest = e->dest; |
f345f21a | 1421 | |
bc35512f | 1422 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) |
6de9cd9a | 1423 | return NULL; |
bc35512f | 1424 | |
3371a64f | 1425 | if (dest == target) |
6de9cd9a | 1426 | return e; |
3348b696 | 1427 | |
6de9cd9a | 1428 | if ((ret = try_redirect_by_replacing_jump (e, target, false)) != NULL) |
f345f21a | 1429 | { |
6fb5fa3c | 1430 | df_set_bb_dirty (src); |
3371a64f | 1431 | fixup_partition_crossing (ret); |
6de9cd9a | 1432 | return ret; |
f345f21a | 1433 | } |
bc35512f | 1434 | |
6de9cd9a DN |
1435 | ret = redirect_branch_edge (e, target); |
1436 | if (!ret) | |
1437 | return NULL; | |
5f0d2358 | 1438 | |
6fb5fa3c | 1439 | df_set_bb_dirty (src); |
3371a64f | 1440 | fixup_partition_crossing (ret); |
6de9cd9a | 1441 | return ret; |
ca6c03ca JH |
1442 | } |
1443 | ||
3371a64f TJ |
1444 | /* Emit a barrier after BB, into the footer if we are in CFGLAYOUT mode. */ |
1445 | ||
1446 | void | |
1447 | emit_barrier_after_bb (basic_block bb) | |
1448 | { | |
1449 | rtx barrier = emit_barrier_after (BB_END (bb)); | |
c3284718 | 1450 | gcc_assert (current_ir_type () == IR_RTL_CFGRTL |
3371a64f TJ |
1451 | || current_ir_type () == IR_RTL_CFGLAYOUT); |
1452 | if (current_ir_type () == IR_RTL_CFGLAYOUT) | |
1453 | BB_FOOTER (bb) = unlink_insn_chain (barrier, barrier); | |
1454 | } | |
1455 | ||
4fe9b91c | 1456 | /* Like force_nonfallthru below, but additionally performs redirection |
26898771 BS |
1457 | Used by redirect_edge_and_branch_force. JUMP_LABEL is used only |
1458 | when redirecting to the EXIT_BLOCK, it is either ret_rtx or | |
1459 | simple_return_rtx, indicating which kind of returnjump to create. | |
1460 | It should be NULL otherwise. */ | |
ca6c03ca | 1461 | |
dc0ff1c8 | 1462 | basic_block |
26898771 | 1463 | force_nonfallthru_and_redirect (edge e, basic_block target, rtx jump_label) |
ca6c03ca | 1464 | { |
a3716585 | 1465 | basic_block jump_block, new_bb = NULL, src = e->src; |
ca6c03ca JH |
1466 | rtx note; |
1467 | edge new_edge; | |
a3716585 | 1468 | int abnormal_edge_flags = 0; |
a3e6a37b | 1469 | bool asm_goto_edge = false; |
7241571e | 1470 | int loc; |
ca6c03ca | 1471 | |
cb9a1d9b JH |
1472 | /* In the case the last instruction is conditional jump to the next |
1473 | instruction, first redirect the jump itself and then continue | |
b20b352b | 1474 | by creating a basic block afterwards to redirect fallthru edge. */ |
fefa31b5 DM |
1475 | if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
1476 | && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) | |
a813c111 | 1477 | && any_condjump_p (BB_END (e->src)) |
a813c111 | 1478 | && JUMP_LABEL (BB_END (e->src)) == BB_HEAD (e->dest)) |
cb9a1d9b JH |
1479 | { |
1480 | rtx note; | |
58e6ae30 | 1481 | edge b = unchecked_make_edge (e->src, target, 0); |
341c100f | 1482 | bool redirected; |
cb9a1d9b | 1483 | |
341c100f NS |
1484 | redirected = redirect_jump (BB_END (e->src), block_label (target), 0); |
1485 | gcc_assert (redirected); | |
c22cacf3 | 1486 | |
a813c111 | 1487 | note = find_reg_note (BB_END (e->src), REG_BR_PROB, NULL_RTX); |
cb9a1d9b JH |
1488 | if (note) |
1489 | { | |
e5af9ddd | 1490 | int prob = XINT (note, 0); |
cb9a1d9b JH |
1491 | |
1492 | b->probability = prob; | |
8ddb5a29 | 1493 | /* Update this to use GCOV_COMPUTE_SCALE. */ |
cb9a1d9b JH |
1494 | b->count = e->count * prob / REG_BR_PROB_BASE; |
1495 | e->probability -= e->probability; | |
1496 | e->count -= b->count; | |
1497 | if (e->probability < 0) | |
1498 | e->probability = 0; | |
1499 | if (e->count < 0) | |
1500 | e->count = 0; | |
1501 | } | |
1502 | } | |
1503 | ||
ca6c03ca | 1504 | if (e->flags & EDGE_ABNORMAL) |
a3716585 JH |
1505 | { |
1506 | /* Irritating special case - fallthru edge to the same block as abnormal | |
1507 | edge. | |
1508 | We can't redirect abnormal edge, but we still can split the fallthru | |
d329e058 | 1509 | one and create separate abnormal edge to original destination. |
a3716585 | 1510 | This allows bb-reorder to make such edge non-fallthru. */ |
341c100f | 1511 | gcc_assert (e->dest == target); |
ee44c28d SB |
1512 | abnormal_edge_flags = e->flags & ~EDGE_FALLTHRU; |
1513 | e->flags &= EDGE_FALLTHRU; | |
a3716585 | 1514 | } |
341c100f | 1515 | else |
24c545ff | 1516 | { |
341c100f | 1517 | gcc_assert (e->flags & EDGE_FALLTHRU); |
fefa31b5 | 1518 | if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
341c100f NS |
1519 | { |
1520 | /* We can't redirect the entry block. Create an empty block | |
628f6a4e BE |
1521 | at the start of the function which we use to add the new |
1522 | jump. */ | |
1523 | edge tmp; | |
1524 | edge_iterator ei; | |
1525 | bool found = false; | |
c22cacf3 | 1526 | |
fefa31b5 DM |
1527 | basic_block bb = create_basic_block (BB_HEAD (e->dest), NULL, |
1528 | ENTRY_BLOCK_PTR_FOR_FN (cfun)); | |
c22cacf3 | 1529 | |
341c100f NS |
1530 | /* Change the existing edge's source to be the new block, and add |
1531 | a new edge from the entry block to the new block. */ | |
1532 | e->src = bb; | |
fefa31b5 DM |
1533 | for (ei = ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs); |
1534 | (tmp = ei_safe_edge (ei)); ) | |
628f6a4e BE |
1535 | { |
1536 | if (tmp == e) | |
1537 | { | |
fefa31b5 | 1538 | ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs->unordered_remove (ei.index); |
628f6a4e BE |
1539 | found = true; |
1540 | break; | |
1541 | } | |
1542 | else | |
1543 | ei_next (&ei); | |
1544 | } | |
c22cacf3 | 1545 | |
628f6a4e | 1546 | gcc_assert (found); |
c22cacf3 | 1547 | |
9771b263 | 1548 | vec_safe_push (bb->succs, e); |
fefa31b5 DM |
1549 | make_single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, |
1550 | EDGE_FALLTHRU); | |
341c100f | 1551 | } |
24c545ff BS |
1552 | } |
1553 | ||
a3e6a37b | 1554 | /* If e->src ends with asm goto, see if any of the ASM_OPERANDS_LABELs |
891ca07d | 1555 | don't point to the target or fallthru label. */ |
a3e6a37b | 1556 | if (JUMP_P (BB_END (e->src)) |
fefa31b5 | 1557 | && target != EXIT_BLOCK_PTR_FOR_FN (cfun) |
a3e6a37b JJ |
1558 | && (e->flags & EDGE_FALLTHRU) |
1559 | && (note = extract_asm_operands (PATTERN (BB_END (e->src))))) | |
ca6c03ca | 1560 | { |
a3e6a37b | 1561 | int i, n = ASM_OPERANDS_LABEL_LENGTH (note); |
5d68b7e6 | 1562 | bool adjust_jump_target = false; |
a3e6a37b JJ |
1563 | |
1564 | for (i = 0; i < n; ++i) | |
891ca07d JJ |
1565 | { |
1566 | if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (e->dest)) | |
5d68b7e6 JJ |
1567 | { |
1568 | LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))--; | |
1569 | XEXP (ASM_OPERANDS_LABEL (note, i), 0) = block_label (target); | |
1570 | LABEL_NUSES (XEXP (ASM_OPERANDS_LABEL (note, i), 0))++; | |
1571 | adjust_jump_target = true; | |
1572 | } | |
891ca07d | 1573 | if (XEXP (ASM_OPERANDS_LABEL (note, i), 0) == BB_HEAD (target)) |
a3e6a37b | 1574 | asm_goto_edge = true; |
891ca07d | 1575 | } |
5d68b7e6 JJ |
1576 | if (adjust_jump_target) |
1577 | { | |
1578 | rtx insn = BB_END (e->src), note; | |
1579 | rtx old_label = BB_HEAD (e->dest); | |
1580 | rtx new_label = BB_HEAD (target); | |
1581 | ||
1582 | if (JUMP_LABEL (insn) == old_label) | |
1583 | { | |
1584 | JUMP_LABEL (insn) = new_label; | |
1585 | note = find_reg_note (insn, REG_LABEL_TARGET, new_label); | |
1586 | if (note) | |
1587 | remove_note (insn, note); | |
1588 | } | |
1589 | else | |
1590 | { | |
1591 | note = find_reg_note (insn, REG_LABEL_TARGET, old_label); | |
1592 | if (note) | |
1593 | remove_note (insn, note); | |
1594 | if (JUMP_LABEL (insn) != new_label | |
1595 | && !find_reg_note (insn, REG_LABEL_TARGET, new_label)) | |
1596 | add_reg_note (insn, REG_LABEL_TARGET, new_label); | |
1597 | } | |
1598 | while ((note = find_reg_note (insn, REG_LABEL_OPERAND, old_label)) | |
1599 | != NULL_RTX) | |
1600 | XEXP (note, 0) = new_label; | |
1601 | } | |
a3e6a37b JJ |
1602 | } |
1603 | ||
1604 | if (EDGE_COUNT (e->src->succs) >= 2 || abnormal_edge_flags || asm_goto_edge) | |
1605 | { | |
1606 | gcov_type count = e->count; | |
1607 | int probability = e->probability; | |
ca6c03ca | 1608 | /* Create the new structures. */ |
31a78298 | 1609 | |
79019985 RH |
1610 | /* If the old block ended with a tablejump, skip its table |
1611 | by searching forward from there. Otherwise start searching | |
1612 | forward from the last instruction of the old block. */ | |
a813c111 SB |
1613 | if (!tablejump_p (BB_END (e->src), NULL, ¬e)) |
1614 | note = BB_END (e->src); | |
31a78298 RH |
1615 | note = NEXT_INSN (note); |
1616 | ||
31a78298 | 1617 | jump_block = create_basic_block (note, NULL, e->src); |
a3e6a37b | 1618 | jump_block->count = count; |
ca6c03ca | 1619 | jump_block->frequency = EDGE_FREQUENCY (e); |
ca6c03ca | 1620 | |
750054a2 CT |
1621 | /* Make sure new block ends up in correct hot/cold section. */ |
1622 | ||
076c7ab8 | 1623 | BB_COPY_PARTITION (jump_block, e->src); |
c22cacf3 | 1624 | |
ca6c03ca JH |
1625 | /* Wire edge in. */ |
1626 | new_edge = make_edge (e->src, jump_block, EDGE_FALLTHRU); | |
a3e6a37b JJ |
1627 | new_edge->probability = probability; |
1628 | new_edge->count = count; | |
ca6c03ca JH |
1629 | |
1630 | /* Redirect old edge. */ | |
1631 | redirect_edge_pred (e, jump_block); | |
1632 | e->probability = REG_BR_PROB_BASE; | |
1633 | ||
3371a64f TJ |
1634 | /* If e->src was previously region crossing, it no longer is |
1635 | and the reg crossing note should be removed. */ | |
1636 | fixup_partition_crossing (new_edge); | |
1637 | ||
a3e6a37b JJ |
1638 | /* If asm goto has any label refs to target's label, |
1639 | add also edge from asm goto bb to target. */ | |
1640 | if (asm_goto_edge) | |
1641 | { | |
1642 | new_edge->probability /= 2; | |
1643 | new_edge->count /= 2; | |
1644 | jump_block->count /= 2; | |
1645 | jump_block->frequency /= 2; | |
1646 | new_edge = make_edge (new_edge->src, target, | |
1647 | e->flags & ~EDGE_FALLTHRU); | |
1648 | new_edge->probability = probability - probability / 2; | |
1649 | new_edge->count = count - count / 2; | |
1650 | } | |
1651 | ||
ca6c03ca JH |
1652 | new_bb = jump_block; |
1653 | } | |
1654 | else | |
1655 | jump_block = e->src; | |
5f0d2358 | 1656 | |
2f13f2de | 1657 | loc = e->goto_locus; |
ca6c03ca | 1658 | e->flags &= ~EDGE_FALLTHRU; |
fefa31b5 | 1659 | if (target == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
ca6c03ca | 1660 | { |
26898771 BS |
1661 | if (jump_label == ret_rtx) |
1662 | { | |
cf22ce3c | 1663 | #ifdef HAVE_return |
26898771 | 1664 | emit_jump_insn_after_setloc (gen_return (), BB_END (jump_block), loc); |
cf22ce3c | 1665 | #else |
26898771 | 1666 | gcc_unreachable (); |
cf22ce3c | 1667 | #endif |
26898771 BS |
1668 | } |
1669 | else | |
1670 | { | |
1671 | gcc_assert (jump_label == simple_return_rtx); | |
1672 | #ifdef HAVE_simple_return | |
1673 | emit_jump_insn_after_setloc (gen_simple_return (), | |
1674 | BB_END (jump_block), loc); | |
1675 | #else | |
1676 | gcc_unreachable (); | |
1677 | #endif | |
1678 | } | |
387748de | 1679 | set_return_jump_label (BB_END (jump_block)); |
ca6c03ca JH |
1680 | } |
1681 | else | |
1682 | { | |
1683 | rtx label = block_label (target); | |
7241571e | 1684 | emit_jump_insn_after_setloc (gen_jump (label), BB_END (jump_block), loc); |
a813c111 | 1685 | JUMP_LABEL (BB_END (jump_block)) = label; |
ca6c03ca JH |
1686 | LABEL_NUSES (label)++; |
1687 | } | |
5f0d2358 | 1688 | |
3371a64f TJ |
1689 | /* We might be in cfg layout mode, and if so, the following routine will |
1690 | insert the barrier correctly. */ | |
1691 | emit_barrier_after_bb (jump_block); | |
ca6c03ca JH |
1692 | redirect_edge_succ_nodup (e, target); |
1693 | ||
a3716585 JH |
1694 | if (abnormal_edge_flags) |
1695 | make_edge (src, target, abnormal_edge_flags); | |
1696 | ||
b8698a0f | 1697 | df_mark_solutions_dirty (); |
3371a64f | 1698 | fixup_partition_crossing (e); |
ca6c03ca JH |
1699 | return new_bb; |
1700 | } | |
1701 | ||
1702 | /* Edge E is assumed to be fallthru edge. Emit needed jump instruction | |
1703 | (and possibly create new basic block) to make edge non-fallthru. | |
1704 | Return newly created BB or NULL if none. */ | |
5f0d2358 | 1705 | |
cf103ca4 EB |
1706 | static basic_block |
1707 | rtl_force_nonfallthru (edge e) | |
ca6c03ca | 1708 | { |
26898771 | 1709 | return force_nonfallthru_and_redirect (e, e->dest, NULL_RTX); |
ca6c03ca JH |
1710 | } |
1711 | ||
1712 | /* Redirect edge even at the expense of creating new jump insn or | |
1713 | basic block. Return new basic block if created, NULL otherwise. | |
41806d92 | 1714 | Conversion must be possible. */ |
ca6c03ca | 1715 | |
9ee634e3 | 1716 | static basic_block |
d329e058 | 1717 | rtl_redirect_edge_and_branch_force (edge e, basic_block target) |
ca6c03ca | 1718 | { |
5f0d2358 RK |
1719 | if (redirect_edge_and_branch (e, target) |
1720 | || e->dest == target) | |
ca6c03ca JH |
1721 | return NULL; |
1722 | ||
1723 | /* In case the edge redirection failed, try to force it to be non-fallthru | |
1724 | and redirect newly created simplejump. */ | |
6fb5fa3c | 1725 | df_set_bb_dirty (e->src); |
26898771 | 1726 | return force_nonfallthru_and_redirect (e, target, NULL_RTX); |
ca6c03ca JH |
1727 | } |
1728 | ||
1729 | /* The given edge should potentially be a fallthru edge. If that is in | |
1730 | fact true, delete the jump and barriers that are in the way. */ | |
1731 | ||
f470c378 ZD |
1732 | static void |
1733 | rtl_tidy_fallthru_edge (edge e) | |
ca6c03ca JH |
1734 | { |
1735 | rtx q; | |
f470c378 ZD |
1736 | basic_block b = e->src, c = b->next_bb; |
1737 | ||
ca6c03ca JH |
1738 | /* ??? In a late-running flow pass, other folks may have deleted basic |
1739 | blocks by nopping out blocks, leaving multiple BARRIERs between here | |
0fa2e4df | 1740 | and the target label. They ought to be chastised and fixed. |
ca6c03ca JH |
1741 | |
1742 | We can also wind up with a sequence of undeletable labels between | |
1743 | one block and the next. | |
1744 | ||
1745 | So search through a sequence of barriers, labels, and notes for | |
1746 | the head of block C and assert that we really do fall through. */ | |
1747 | ||
a813c111 | 1748 | for (q = NEXT_INSN (BB_END (b)); q != BB_HEAD (c); q = NEXT_INSN (q)) |
9c0a0632 RH |
1749 | if (INSN_P (q)) |
1750 | return; | |
ca6c03ca JH |
1751 | |
1752 | /* Remove what will soon cease being the jump insn from the source block. | |
1753 | If block B consisted only of this single jump, turn it into a deleted | |
1754 | note. */ | |
a813c111 | 1755 | q = BB_END (b); |
4b4bf941 | 1756 | if (JUMP_P (q) |
ca6c03ca JH |
1757 | && onlyjump_p (q) |
1758 | && (any_uncondjump_p (q) | |
c5cbcccf | 1759 | || single_succ_p (b))) |
ca6c03ca JH |
1760 | { |
1761 | #ifdef HAVE_cc0 | |
1762 | /* If this was a conditional jump, we need to also delete | |
1763 | the insn that set cc0. */ | |
1764 | if (any_condjump_p (q) && only_sets_cc0_p (PREV_INSN (q))) | |
1765 | q = PREV_INSN (q); | |
1766 | #endif | |
1767 | ||
1768 | q = PREV_INSN (q); | |
ca6c03ca JH |
1769 | } |
1770 | ||
1771 | /* Selectively unlink the sequence. */ | |
a813c111 | 1772 | if (q != PREV_INSN (BB_HEAD (c))) |
a7b87f73 | 1773 | delete_insn_chain (NEXT_INSN (q), PREV_INSN (BB_HEAD (c)), false); |
ca6c03ca JH |
1774 | |
1775 | e->flags |= EDGE_FALLTHRU; | |
1776 | } | |
ca6c03ca | 1777 | \f |
f470c378 ZD |
1778 | /* Should move basic block BB after basic block AFTER. NIY. */ |
1779 | ||
1780 | static bool | |
1781 | rtl_move_block_after (basic_block bb ATTRIBUTE_UNUSED, | |
1782 | basic_block after ATTRIBUTE_UNUSED) | |
1783 | { | |
1784 | return false; | |
1785 | } | |
1786 | ||
3371a64f TJ |
1787 | /* Locate the last bb in the same partition as START_BB. */ |
1788 | ||
1789 | static basic_block | |
1790 | last_bb_in_partition (basic_block start_bb) | |
1791 | { | |
1792 | basic_block bb; | |
fefa31b5 | 1793 | FOR_BB_BETWEEN (bb, start_bb, EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) |
3371a64f TJ |
1794 | { |
1795 | if (BB_PARTITION (start_bb) != BB_PARTITION (bb->next_bb)) | |
1796 | return bb; | |
1797 | } | |
6626665f | 1798 | /* Return bb before the exit block. */ |
3371a64f TJ |
1799 | return bb->prev_bb; |
1800 | } | |
1801 | ||
ca6c03ca | 1802 | /* Split a (typically critical) edge. Return the new block. |
41806d92 | 1803 | The edge must not be abnormal. |
ca6c03ca JH |
1804 | |
1805 | ??? The code generally expects to be called on critical edges. | |
1806 | The case of a block ending in an unconditional jump to a | |
1807 | block with multiple predecessors is not handled optimally. */ | |
1808 | ||
8ce33230 | 1809 | static basic_block |
d329e058 | 1810 | rtl_split_edge (edge edge_in) |
ca6c03ca | 1811 | { |
3371a64f | 1812 | basic_block bb, new_bb; |
ca6c03ca JH |
1813 | rtx before; |
1814 | ||
1815 | /* Abnormal edges cannot be split. */ | |
341c100f | 1816 | gcc_assert (!(edge_in->flags & EDGE_ABNORMAL)); |
ca6c03ca JH |
1817 | |
1818 | /* We are going to place the new block in front of edge destination. | |
eaec9b3d | 1819 | Avoid existence of fallthru predecessors. */ |
ca6c03ca JH |
1820 | if ((edge_in->flags & EDGE_FALLTHRU) == 0) |
1821 | { | |
0fd4b31d | 1822 | edge e = find_fallthru_edge (edge_in->dest->preds); |
ca6c03ca JH |
1823 | |
1824 | if (e) | |
1825 | force_nonfallthru (e); | |
1826 | } | |
1827 | ||
96e82e0a | 1828 | /* Create the basic block note. */ |
fefa31b5 | 1829 | if (edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
a813c111 | 1830 | before = BB_HEAD (edge_in->dest); |
ca6c03ca JH |
1831 | else |
1832 | before = NULL_RTX; | |
1833 | ||
623a66fa | 1834 | /* If this is a fall through edge to the exit block, the blocks might be |
ffe14686 | 1835 | not adjacent, and the right place is after the source. */ |
fefa31b5 DM |
1836 | if ((edge_in->flags & EDGE_FALLTHRU) |
1837 | && edge_in->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
623a66fa R |
1838 | { |
1839 | before = NEXT_INSN (BB_END (edge_in->src)); | |
623a66fa | 1840 | bb = create_basic_block (before, NULL, edge_in->src); |
076c7ab8 | 1841 | BB_COPY_PARTITION (bb, edge_in->src); |
623a66fa R |
1842 | } |
1843 | else | |
9fb32434 | 1844 | { |
fefa31b5 | 1845 | if (edge_in->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
3371a64f TJ |
1846 | { |
1847 | bb = create_basic_block (before, NULL, edge_in->dest->prev_bb); | |
1848 | BB_COPY_PARTITION (bb, edge_in->dest); | |
1849 | } | |
1850 | else | |
1851 | { | |
1852 | basic_block after = edge_in->dest->prev_bb; | |
1853 | /* If this is post-bb reordering, and the edge crosses a partition | |
1854 | boundary, the new block needs to be inserted in the bb chain | |
1855 | at the end of the src partition (since we put the new bb into | |
1856 | that partition, see below). Otherwise we may end up creating | |
1857 | an extra partition crossing in the chain, which is illegal. | |
1858 | It can't go after the src, because src may have a fall-through | |
1859 | to a different block. */ | |
1860 | if (crtl->bb_reorder_complete | |
1861 | && (edge_in->flags & EDGE_CROSSING)) | |
1862 | { | |
1863 | after = last_bb_in_partition (edge_in->src); | |
1864 | before = NEXT_INSN (BB_END (after)); | |
1865 | /* The instruction following the last bb in partition should | |
1866 | be a barrier, since it cannot end in a fall-through. */ | |
1867 | gcc_checking_assert (BARRIER_P (before)); | |
1868 | before = NEXT_INSN (before); | |
1869 | } | |
1870 | bb = create_basic_block (before, NULL, after); | |
1871 | /* Put the split bb into the src partition, to avoid creating | |
1872 | a situation where a cold bb dominates a hot bb, in the case | |
1873 | where src is cold and dest is hot. The src will dominate | |
1874 | the new bb (whereas it might not have dominated dest). */ | |
1875 | BB_COPY_PARTITION (bb, edge_in->src); | |
1876 | } | |
9fb32434 | 1877 | } |
ca6c03ca | 1878 | |
4977bab6 | 1879 | make_single_succ_edge (bb, edge_in->dest, EDGE_FALLTHRU); |
ca6c03ca | 1880 | |
3371a64f TJ |
1881 | /* Can't allow a region crossing edge to be fallthrough. */ |
1882 | if (BB_PARTITION (bb) != BB_PARTITION (edge_in->dest) | |
fefa31b5 | 1883 | && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
3371a64f TJ |
1884 | { |
1885 | new_bb = force_nonfallthru (single_succ_edge (bb)); | |
1886 | gcc_assert (!new_bb); | |
1887 | } | |
1888 | ||
4d6922ee | 1889 | /* For non-fallthru edges, we must adjust the predecessor's |
ca6c03ca JH |
1890 | jump instruction to target our new block. */ |
1891 | if ((edge_in->flags & EDGE_FALLTHRU) == 0) | |
1892 | { | |
341c100f NS |
1893 | edge redirected = redirect_edge_and_branch (edge_in, bb); |
1894 | gcc_assert (redirected); | |
ca6c03ca JH |
1895 | } |
1896 | else | |
3b5fda81 | 1897 | { |
fefa31b5 | 1898 | if (edge_in->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
3b5fda81 JJ |
1899 | { |
1900 | /* For asm goto even splitting of fallthru edge might | |
1901 | need insn patching, as other labels might point to the | |
1902 | old label. */ | |
1903 | rtx last = BB_END (edge_in->src); | |
1904 | if (last | |
1905 | && JUMP_P (last) | |
fefa31b5 | 1906 | && edge_in->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) |
3b5fda81 JJ |
1907 | && extract_asm_operands (PATTERN (last)) != NULL_RTX |
1908 | && patch_jump_insn (last, before, bb)) | |
1909 | df_set_bb_dirty (edge_in->src); | |
1910 | } | |
1911 | redirect_edge_succ (edge_in, bb); | |
1912 | } | |
ca6c03ca JH |
1913 | |
1914 | return bb; | |
1915 | } | |
1916 | ||
1917 | /* Queue instructions for insertion on an edge between two basic blocks. | |
1918 | The new instructions and basic blocks (if any) will not appear in the | |
1919 | CFG until commit_edge_insertions is called. */ | |
1920 | ||
1921 | void | |
d329e058 | 1922 | insert_insn_on_edge (rtx pattern, edge e) |
ca6c03ca JH |
1923 | { |
1924 | /* We cannot insert instructions on an abnormal critical edge. | |
1925 | It will be easier to find the culprit if we die now. */ | |
341c100f | 1926 | gcc_assert (!((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))); |
ca6c03ca | 1927 | |
6de9cd9a | 1928 | if (e->insns.r == NULL_RTX) |
ca6c03ca JH |
1929 | start_sequence (); |
1930 | else | |
6de9cd9a | 1931 | push_to_sequence (e->insns.r); |
ca6c03ca JH |
1932 | |
1933 | emit_insn (pattern); | |
1934 | ||
6de9cd9a | 1935 | e->insns.r = get_insns (); |
ca6c03ca JH |
1936 | end_sequence (); |
1937 | } | |
1938 | ||
1939 | /* Update the CFG for the instructions queued on edge E. */ | |
1940 | ||
4e3825db | 1941 | void |
2ac66157 | 1942 | commit_one_edge_insertion (edge e) |
ca6c03ca JH |
1943 | { |
1944 | rtx before = NULL_RTX, after = NULL_RTX, insns, tmp, last; | |
898c90c0 | 1945 | basic_block bb; |
ca6c03ca JH |
1946 | |
1947 | /* Pull the insns off the edge now since the edge might go away. */ | |
6de9cd9a DN |
1948 | insns = e->insns.r; |
1949 | e->insns.r = NULL_RTX; | |
ca6c03ca | 1950 | |
898c90c0 EB |
1951 | /* Figure out where to put these insns. If the destination has |
1952 | one predecessor, insert there. Except for the exit block. */ | |
fefa31b5 | 1953 | if (single_pred_p (e->dest) && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
ca6c03ca | 1954 | { |
898c90c0 EB |
1955 | bb = e->dest; |
1956 | ||
1957 | /* Get the location correct wrt a code label, and "nice" wrt | |
1958 | a basic block note, and before everything else. */ | |
1959 | tmp = BB_HEAD (bb); | |
1960 | if (LABEL_P (tmp)) | |
1961 | tmp = NEXT_INSN (tmp); | |
1962 | if (NOTE_INSN_BASIC_BLOCK_P (tmp)) | |
1963 | tmp = NEXT_INSN (tmp); | |
1964 | if (tmp == BB_HEAD (bb)) | |
1965 | before = tmp; | |
1966 | else if (tmp) | |
1967 | after = PREV_INSN (tmp); | |
1968 | else | |
1969 | after = get_last_insn (); | |
1970 | } | |
3dec4024 | 1971 | |
898c90c0 | 1972 | /* If the source has one successor and the edge is not abnormal, |
a1d50386 JJ |
1973 | insert there. Except for the entry block. |
1974 | Don't do this if the predecessor ends in a jump other than | |
1975 | unconditional simple jump. E.g. for asm goto that points all | |
1976 | its labels at the fallthru basic block, we can't insert instructions | |
1977 | before the asm goto, as the asm goto can have various of side effects, | |
1978 | and can't emit instructions after the asm goto, as it must end | |
1979 | the basic block. */ | |
898c90c0 EB |
1980 | else if ((e->flags & EDGE_ABNORMAL) == 0 |
1981 | && single_succ_p (e->src) | |
fefa31b5 | 1982 | && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
a1d50386 JJ |
1983 | && (!JUMP_P (BB_END (e->src)) |
1984 | || simplejump_p (BB_END (e->src)))) | |
898c90c0 EB |
1985 | { |
1986 | bb = e->src; | |
3dec4024 | 1987 | |
898c90c0 EB |
1988 | /* It is possible to have a non-simple jump here. Consider a target |
1989 | where some forms of unconditional jumps clobber a register. This | |
1990 | happens on the fr30 for example. | |
ca6c03ca | 1991 | |
898c90c0 EB |
1992 | We know this block has a single successor, so we can just emit |
1993 | the queued insns before the jump. */ | |
1994 | if (JUMP_P (BB_END (bb))) | |
1995 | before = BB_END (bb); | |
3dec4024 JH |
1996 | else |
1997 | { | |
898c90c0 EB |
1998 | /* We'd better be fallthru, or we've lost track of what's what. */ |
1999 | gcc_assert (e->flags & EDGE_FALLTHRU); | |
750054a2 | 2000 | |
898c90c0 | 2001 | after = BB_END (bb); |
ca6c03ca JH |
2002 | } |
2003 | } | |
2004 | ||
898c90c0 EB |
2005 | /* Otherwise we must split the edge. */ |
2006 | else | |
2007 | { | |
2008 | bb = split_edge (e); | |
3371a64f TJ |
2009 | |
2010 | /* If E crossed a partition boundary, we needed to make bb end in | |
2011 | a region-crossing jump, even though it was originally fallthru. */ | |
2012 | if (JUMP_P (BB_END (bb))) | |
2013 | before = BB_END (bb); | |
2014 | else | |
2015 | after = BB_END (bb); | |
898c90c0 EB |
2016 | } |
2017 | ||
2018 | /* Now that we've found the spot, do the insertion. */ | |
ca6c03ca JH |
2019 | if (before) |
2020 | { | |
6fb5fa3c | 2021 | emit_insn_before_noloc (insns, before, bb); |
ca6c03ca JH |
2022 | last = prev_nonnote_insn (before); |
2023 | } | |
2024 | else | |
6fb5fa3c | 2025 | last = emit_insn_after_noloc (insns, after, bb); |
ca6c03ca JH |
2026 | |
2027 | if (returnjump_p (last)) | |
2028 | { | |
2029 | /* ??? Remove all outgoing edges from BB and add one for EXIT. | |
c22cacf3 MS |
2030 | This is not currently a problem because this only happens |
2031 | for the (single) epilogue, which already has a fallthru edge | |
2032 | to EXIT. */ | |
ca6c03ca | 2033 | |
c5cbcccf | 2034 | e = single_succ_edge (bb); |
fefa31b5 | 2035 | gcc_assert (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun) |
c5cbcccf | 2036 | && single_succ_p (bb) && (e->flags & EDGE_FALLTHRU)); |
ca6c03ca | 2037 | |
5f0d2358 | 2038 | e->flags &= ~EDGE_FALLTHRU; |
ca6c03ca | 2039 | emit_barrier_after (last); |
0b17ab2f | 2040 | |
ca6c03ca JH |
2041 | if (before) |
2042 | delete_insn (before); | |
2043 | } | |
341c100f NS |
2044 | else |
2045 | gcc_assert (!JUMP_P (last)); | |
ca6c03ca JH |
2046 | } |
2047 | ||
2048 | /* Update the CFG for all queued instructions. */ | |
2049 | ||
2050 | void | |
d329e058 | 2051 | commit_edge_insertions (void) |
ca6c03ca | 2052 | { |
ca6c03ca JH |
2053 | basic_block bb; |
2054 | ||
600b5b1d TJ |
2055 | /* Optimization passes that invoke this routine can cause hot blocks |
2056 | previously reached by both hot and cold blocks to become dominated only | |
2057 | by cold blocks. This will cause the verification below to fail, | |
2058 | and lead to now cold code in the hot section. In some cases this | |
2059 | may only be visible after newly unreachable blocks are deleted, | |
2060 | which will be done by fixup_partitions. */ | |
2061 | fixup_partitions (); | |
2062 | ||
ca6c03ca JH |
2063 | #ifdef ENABLE_CHECKING |
2064 | verify_flow_info (); | |
2065 | #endif | |
2066 | ||
fefa31b5 DM |
2067 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), |
2068 | EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
ca6c03ca | 2069 | { |
628f6a4e BE |
2070 | edge e; |
2071 | edge_iterator ei; | |
ca6c03ca | 2072 | |
628f6a4e BE |
2073 | FOR_EACH_EDGE (e, ei, bb->succs) |
2074 | if (e->insns.r) | |
cf103ca4 | 2075 | commit_one_edge_insertion (e); |
3dec4024 | 2076 | } |
ca6c03ca JH |
2077 | } |
2078 | \f | |
6fb5fa3c | 2079 | |
f470c378 | 2080 | /* Print out RTL-specific basic block information (live information |
a315c44c SB |
2081 | at start and end with TDF_DETAILS). FLAGS are the TDF_* masks |
2082 | documented in dumpfile.h. */ | |
ca6c03ca | 2083 | |
10e9fecc | 2084 | static void |
a315c44c | 2085 | rtl_dump_bb (FILE *outf, basic_block bb, int indent, int flags) |
ca6c03ca JH |
2086 | { |
2087 | rtx insn; | |
2088 | rtx last; | |
f470c378 | 2089 | char *s_indent; |
ca6c03ca | 2090 | |
ae50c0cb | 2091 | s_indent = (char *) alloca ((size_t) indent + 1); |
400e39e3 | 2092 | memset (s_indent, ' ', (size_t) indent); |
f470c378 | 2093 | s_indent[indent] = '\0'; |
b8698a0f | 2094 | |
a315c44c | 2095 | if (df && (flags & TDF_DETAILS)) |
6fb5fa3c DB |
2096 | { |
2097 | df_dump_top (bb, outf); | |
2098 | putc ('\n', outf); | |
2099 | } | |
ca6c03ca | 2100 | |
68cc3174 EB |
2101 | if (bb->index != ENTRY_BLOCK && bb->index != EXIT_BLOCK) |
2102 | for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb)); insn != last; | |
2103 | insn = NEXT_INSN (insn)) | |
a315c44c | 2104 | { |
7b19209f SB |
2105 | if (flags & TDF_DETAILS) |
2106 | df_dump_insn_top (insn, outf); | |
a315c44c SB |
2107 | if (! (flags & TDF_SLIM)) |
2108 | print_rtl_single (outf, insn); | |
2109 | else | |
2110 | dump_insn_slim (outf, insn); | |
7b19209f SB |
2111 | if (flags & TDF_DETAILS) |
2112 | df_dump_insn_bottom (insn, outf); | |
a315c44c SB |
2113 | } |
2114 | ||
2115 | if (df && (flags & TDF_DETAILS)) | |
6fb5fa3c DB |
2116 | { |
2117 | df_dump_bottom (bb, outf); | |
2118 | putc ('\n', outf); | |
2119 | } | |
2120 | ||
ca6c03ca JH |
2121 | } |
2122 | \f | |
c4669594 SB |
2123 | /* Like dump_function_to_file, but for RTL. Print out dataflow information |
2124 | for the start of each basic block. FLAGS are the TDF_* masks documented | |
2125 | in dumpfile.h. */ | |
ca6c03ca JH |
2126 | |
2127 | void | |
a315c44c | 2128 | print_rtl_with_bb (FILE *outf, const_rtx rtx_first, int flags) |
ca6c03ca | 2129 | { |
22ea9ec0 | 2130 | const_rtx tmp_rtx; |
ca6c03ca JH |
2131 | if (rtx_first == 0) |
2132 | fprintf (outf, "(nil)\n"); | |
2133 | else | |
2134 | { | |
ca6c03ca JH |
2135 | enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB }; |
2136 | int max_uid = get_max_uid (); | |
5ed6ace5 MD |
2137 | basic_block *start = XCNEWVEC (basic_block, max_uid); |
2138 | basic_block *end = XCNEWVEC (basic_block, max_uid); | |
2139 | enum bb_state *in_bb_p = XCNEWVEC (enum bb_state, max_uid); | |
e0082a72 ZD |
2140 | basic_block bb; |
2141 | ||
c4669594 SB |
2142 | /* After freeing the CFG, we still have BLOCK_FOR_INSN set on most |
2143 | insns, but the CFG is not maintained so the basic block info | |
2144 | is not reliable. Therefore it's omitted from the dumps. */ | |
2145 | if (! (cfun->curr_properties & PROP_cfg)) | |
2146 | flags &= ~TDF_BLOCKS; | |
2147 | ||
6fb5fa3c DB |
2148 | if (df) |
2149 | df_dump_start (outf); | |
2150 | ||
c4669594 | 2151 | if (flags & TDF_BLOCKS) |
ca6c03ca | 2152 | { |
c4669594 | 2153 | FOR_EACH_BB_REVERSE (bb) |
ca6c03ca | 2154 | { |
c4669594 SB |
2155 | rtx x; |
2156 | ||
2157 | start[INSN_UID (BB_HEAD (bb))] = bb; | |
2158 | end[INSN_UID (BB_END (bb))] = bb; | |
2159 | for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x)) | |
2160 | { | |
2161 | enum bb_state state = IN_MULTIPLE_BB; | |
5f0d2358 | 2162 | |
c4669594 SB |
2163 | if (in_bb_p[INSN_UID (x)] == NOT_IN_BB) |
2164 | state = IN_ONE_BB; | |
2165 | in_bb_p[INSN_UID (x)] = state; | |
ca6c03ca | 2166 | |
c4669594 SB |
2167 | if (x == BB_END (bb)) |
2168 | break; | |
2169 | } | |
ca6c03ca JH |
2170 | } |
2171 | } | |
2172 | ||
2173 | for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx)) | |
2174 | { | |
c4669594 SB |
2175 | if (flags & TDF_BLOCKS) |
2176 | { | |
2177 | bb = start[INSN_UID (tmp_rtx)]; | |
2178 | if (bb != NULL) | |
2179 | { | |
2180 | dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, true, false); | |
2181 | if (df && (flags & TDF_DETAILS)) | |
2182 | df_dump_top (bb, outf); | |
2183 | } | |
ca6c03ca | 2184 | |
c4669594 SB |
2185 | if (in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB |
2186 | && !NOTE_P (tmp_rtx) | |
2187 | && !BARRIER_P (tmp_rtx)) | |
2188 | fprintf (outf, ";; Insn is not within a basic block\n"); | |
2189 | else if (in_bb_p[INSN_UID (tmp_rtx)] == IN_MULTIPLE_BB) | |
2190 | fprintf (outf, ";; Insn is in multiple basic blocks\n"); | |
2191 | } | |
ca6c03ca | 2192 | |
7b19209f SB |
2193 | if (flags & TDF_DETAILS) |
2194 | df_dump_insn_top (tmp_rtx, outf); | |
a315c44c SB |
2195 | if (! (flags & TDF_SLIM)) |
2196 | print_rtl_single (outf, tmp_rtx); | |
2197 | else | |
2198 | dump_insn_slim (outf, tmp_rtx); | |
7b19209f SB |
2199 | if (flags & TDF_DETAILS) |
2200 | df_dump_insn_bottom (tmp_rtx, outf); | |
ca6c03ca | 2201 | |
c4669594 SB |
2202 | if (flags & TDF_BLOCKS) |
2203 | { | |
2204 | bb = end[INSN_UID (tmp_rtx)]; | |
2205 | if (bb != NULL) | |
2206 | { | |
2207 | dump_bb_info (outf, bb, 0, dump_flags | TDF_COMMENT, false, true); | |
2208 | if (df && (flags & TDF_DETAILS)) | |
2209 | df_dump_bottom (bb, outf); | |
1b03a96d | 2210 | putc ('\n', outf); |
c4669594 SB |
2211 | } |
2212 | } | |
ca6c03ca JH |
2213 | } |
2214 | ||
2215 | free (start); | |
2216 | free (end); | |
2217 | free (in_bb_p); | |
2218 | } | |
ca6c03ca JH |
2219 | } |
2220 | \f | |
532aafad SB |
2221 | /* Update the branch probability of BB if a REG_BR_PROB is present. */ |
2222 | ||
b446e5a2 | 2223 | void |
d329e058 | 2224 | update_br_prob_note (basic_block bb) |
b446e5a2 JH |
2225 | { |
2226 | rtx note; | |
4b4bf941 | 2227 | if (!JUMP_P (BB_END (bb))) |
b446e5a2 | 2228 | return; |
a813c111 | 2229 | note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX); |
e5af9ddd | 2230 | if (!note || XINT (note, 0) == BRANCH_EDGE (bb)->probability) |
b446e5a2 | 2231 | return; |
e5af9ddd | 2232 | XINT (note, 0) = BRANCH_EDGE (bb)->probability; |
b446e5a2 | 2233 | } |
96370780 MK |
2234 | |
2235 | /* Get the last insn associated with block BB (that includes barriers and | |
2236 | tablejumps after BB). */ | |
2237 | rtx | |
2238 | get_last_bb_insn (basic_block bb) | |
2239 | { | |
2240 | rtx tmp; | |
2241 | rtx end = BB_END (bb); | |
2242 | ||
2243 | /* Include any jump table following the basic block. */ | |
2244 | if (tablejump_p (end, NULL, &tmp)) | |
2245 | end = tmp; | |
2246 | ||
2247 | /* Include any barriers that may follow the basic block. */ | |
1e211590 | 2248 | tmp = next_nonnote_insn_bb (end); |
96370780 MK |
2249 | while (tmp && BARRIER_P (tmp)) |
2250 | { | |
2251 | end = tmp; | |
1e211590 | 2252 | tmp = next_nonnote_insn_bb (end); |
96370780 MK |
2253 | } |
2254 | ||
2255 | return end; | |
2256 | } | |
af205f67 | 2257 | |
600b5b1d TJ |
2258 | /* Sanity check partition hotness to ensure that basic blocks in |
2259 | the cold partition don't dominate basic blocks in the hot partition. | |
2260 | If FLAG_ONLY is true, report violations as errors. Otherwise | |
2261 | re-mark the dominated blocks as cold, since this is run after | |
2262 | cfg optimizations that may make hot blocks previously reached | |
2263 | by both hot and cold blocks now only reachable along cold paths. */ | |
2264 | ||
2265 | static vec<basic_block> | |
2266 | find_partition_fixes (bool flag_only) | |
2267 | { | |
2268 | basic_block bb; | |
2269 | vec<basic_block> bbs_in_cold_partition = vNULL; | |
2270 | vec<basic_block> bbs_to_fix = vNULL; | |
2271 | ||
2272 | /* Callers check this. */ | |
2273 | gcc_checking_assert (crtl->has_bb_partition); | |
2274 | ||
2275 | FOR_EACH_BB (bb) | |
2276 | if ((BB_PARTITION (bb) == BB_COLD_PARTITION)) | |
2277 | bbs_in_cold_partition.safe_push (bb); | |
2278 | ||
2279 | if (bbs_in_cold_partition.is_empty ()) | |
2280 | return vNULL; | |
2281 | ||
2282 | bool dom_calculated_here = !dom_info_available_p (CDI_DOMINATORS); | |
2283 | ||
2284 | if (dom_calculated_here) | |
2285 | calculate_dominance_info (CDI_DOMINATORS); | |
2286 | ||
2287 | while (! bbs_in_cold_partition.is_empty ()) | |
2288 | { | |
2289 | bb = bbs_in_cold_partition.pop (); | |
2290 | /* Any blocks dominated by a block in the cold section | |
2291 | must also be cold. */ | |
2292 | basic_block son; | |
2293 | for (son = first_dom_son (CDI_DOMINATORS, bb); | |
2294 | son; | |
2295 | son = next_dom_son (CDI_DOMINATORS, son)) | |
2296 | { | |
2297 | /* If son is not yet cold, then mark it cold here and | |
2298 | enqueue it for further processing. */ | |
2299 | if ((BB_PARTITION (son) != BB_COLD_PARTITION)) | |
2300 | { | |
2301 | if (flag_only) | |
2302 | error ("non-cold basic block %d dominated " | |
2303 | "by a block in the cold partition (%d)", son->index, bb->index); | |
2304 | else | |
2305 | BB_SET_PARTITION (son, BB_COLD_PARTITION); | |
2306 | bbs_to_fix.safe_push (son); | |
2307 | bbs_in_cold_partition.safe_push (son); | |
2308 | } | |
2309 | } | |
2310 | } | |
2311 | ||
2312 | if (dom_calculated_here) | |
2313 | free_dominance_info (CDI_DOMINATORS); | |
2314 | ||
2315 | return bbs_to_fix; | |
2316 | } | |
2317 | ||
2318 | /* Perform cleanup on the hot/cold bb partitioning after optimization | |
2319 | passes that modify the cfg. */ | |
2320 | ||
2321 | void | |
2322 | fixup_partitions (void) | |
2323 | { | |
2324 | basic_block bb; | |
2325 | ||
2326 | if (!crtl->has_bb_partition) | |
2327 | return; | |
2328 | ||
2329 | /* Delete any blocks that became unreachable and weren't | |
2330 | already cleaned up, for example during edge forwarding | |
2331 | and convert_jumps_to_returns. This will expose more | |
2332 | opportunities for fixing the partition boundaries here. | |
2333 | Also, the calculation of the dominance graph during verification | |
2334 | will assert if there are unreachable nodes. */ | |
2335 | delete_unreachable_blocks (); | |
2336 | ||
2337 | /* If there are partitions, do a sanity check on them: A basic block in | |
2338 | a cold partition cannot dominate a basic block in a hot partition. | |
2339 | Fixup any that now violate this requirement, as a result of edge | |
2340 | forwarding and unreachable block deletion. */ | |
2341 | vec<basic_block> bbs_to_fix = find_partition_fixes (false); | |
2342 | ||
2343 | /* Do the partition fixup after all necessary blocks have been converted to | |
2344 | cold, so that we only update the region crossings the minimum number of | |
2345 | places, which can require forcing edges to be non fallthru. */ | |
2346 | while (! bbs_to_fix.is_empty ()) | |
2347 | { | |
2348 | bb = bbs_to_fix.pop (); | |
2349 | fixup_new_cold_bb (bb); | |
2350 | } | |
2351 | } | |
2352 | ||
af205f67 TJ |
2353 | /* Verify, in the basic block chain, that there is at most one switch |
2354 | between hot/cold partitions. This condition will not be true until | |
2355 | after reorder_basic_blocks is called. */ | |
2356 | ||
251a41b9 | 2357 | static int |
af205f67 TJ |
2358 | verify_hot_cold_block_grouping (void) |
2359 | { | |
2360 | basic_block bb; | |
2361 | int err = 0; | |
2362 | bool switched_sections = false; | |
2363 | int current_partition = BB_UNPARTITIONED; | |
2364 | ||
3371a64f TJ |
2365 | /* Even after bb reordering is complete, we go into cfglayout mode |
2366 | again (in compgoto). Ensure we don't call this before going back | |
2367 | into linearized RTL when any layout fixes would have been committed. */ | |
2368 | if (!crtl->bb_reorder_complete | |
c3284718 | 2369 | || current_ir_type () != IR_RTL_CFGRTL) |
251a41b9 | 2370 | return err; |
af205f67 TJ |
2371 | |
2372 | FOR_EACH_BB (bb) | |
2373 | { | |
2374 | if (current_partition != BB_UNPARTITIONED | |
2375 | && BB_PARTITION (bb) != current_partition) | |
2376 | { | |
2377 | if (switched_sections) | |
2378 | { | |
2379 | error ("multiple hot/cold transitions found (bb %i)", | |
2380 | bb->index); | |
2381 | err = 1; | |
2382 | } | |
2383 | else | |
2384 | switched_sections = true; | |
2385 | ||
2386 | if (!crtl->has_bb_partition) | |
2387 | error ("partition found but function partition flag not set"); | |
2388 | } | |
2389 | current_partition = BB_PARTITION (bb); | |
2390 | } | |
2391 | ||
251a41b9 | 2392 | return err; |
af205f67 | 2393 | } |
b446e5a2 | 2394 | \f |
ca6c03ca | 2395 | |
251a41b9 TJ |
2396 | /* Perform several checks on the edges out of each block, such as |
2397 | the consistency of the branch probabilities, the correctness | |
2398 | of hot/cold partition crossing edges, and the number of expected | |
600b5b1d TJ |
2399 | successor edges. Also verify that the dominance relationship |
2400 | between hot/cold blocks is sane. */ | |
f470c378 | 2401 | |
10e9fecc | 2402 | static int |
251a41b9 | 2403 | rtl_verify_edges (void) |
ca6c03ca | 2404 | { |
10e9fecc | 2405 | int err = 0; |
94eb5ddb | 2406 | basic_block bb; |
ca6c03ca | 2407 | |
e0082a72 | 2408 | FOR_EACH_BB_REVERSE (bb) |
ca6c03ca | 2409 | { |
f99ffaa3 EB |
2410 | int n_fallthru = 0, n_branch = 0, n_abnormal_call = 0, n_sibcall = 0; |
2411 | int n_eh = 0, n_abnormal = 0; | |
3cf54412 | 2412 | edge e, fallthru = NULL; |
628f6a4e | 2413 | edge_iterator ei; |
251a41b9 | 2414 | rtx note; |
3371a64f | 2415 | bool has_crossing_edge = false; |
ca6c03ca | 2416 | |
2085a21f | 2417 | if (JUMP_P (BB_END (bb)) |
a813c111 | 2418 | && (note = find_reg_note (BB_END (bb), REG_BR_PROB, NULL_RTX)) |
628f6a4e | 2419 | && EDGE_COUNT (bb->succs) >= 2 |
a813c111 | 2420 | && any_condjump_p (BB_END (bb))) |
5a1a3e5e | 2421 | { |
e5af9ddd | 2422 | if (XINT (note, 0) != BRANCH_EDGE (bb)->probability |
e53de54d | 2423 | && profile_status != PROFILE_ABSENT) |
5a1a3e5e | 2424 | { |
e5af9ddd RS |
2425 | error ("verify_flow_info: REG_BR_PROB does not match cfg %i %i", |
2426 | XINT (note, 0), BRANCH_EDGE (bb)->probability); | |
5a1a3e5e JH |
2427 | err = 1; |
2428 | } | |
2429 | } | |
251a41b9 | 2430 | |
628f6a4e | 2431 | FOR_EACH_EDGE (e, ei, bb->succs) |
ca6c03ca | 2432 | { |
0be7e7a6 RH |
2433 | bool is_crossing; |
2434 | ||
ca6c03ca | 2435 | if (e->flags & EDGE_FALLTHRU) |
0be7e7a6 RH |
2436 | n_fallthru++, fallthru = e; |
2437 | ||
2438 | is_crossing = (BB_PARTITION (e->src) != BB_PARTITION (e->dest) | |
fefa31b5 DM |
2439 | && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
2440 | && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)); | |
3371a64f | 2441 | has_crossing_edge |= is_crossing; |
0be7e7a6 | 2442 | if (e->flags & EDGE_CROSSING) |
750054a2 | 2443 | { |
0be7e7a6 RH |
2444 | if (!is_crossing) |
2445 | { | |
2446 | error ("EDGE_CROSSING incorrectly set across same section"); | |
2447 | err = 1; | |
2448 | } | |
2449 | if (e->flags & EDGE_FALLTHRU) | |
2450 | { | |
f99ffaa3 | 2451 | error ("fallthru edge crosses section boundary in bb %i", |
750054a2 CT |
2452 | e->src->index); |
2453 | err = 1; | |
2454 | } | |
0be7e7a6 RH |
2455 | if (e->flags & EDGE_EH) |
2456 | { | |
f99ffaa3 | 2457 | error ("EH edge crosses section boundary in bb %i", |
0be7e7a6 RH |
2458 | e->src->index); |
2459 | err = 1; | |
2460 | } | |
3371a64f TJ |
2461 | if (JUMP_P (BB_END (bb)) |
2462 | && !find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX)) | |
2463 | { | |
2464 | error ("No region crossing jump at section boundary in bb %i", | |
2465 | bb->index); | |
2466 | err = 1; | |
2467 | } | |
0be7e7a6 RH |
2468 | } |
2469 | else if (is_crossing) | |
2470 | { | |
2471 | error ("EDGE_CROSSING missing across section boundary"); | |
2472 | err = 1; | |
750054a2 | 2473 | } |
3dec4024 | 2474 | |
65f43cdf ZD |
2475 | if ((e->flags & ~(EDGE_DFS_BACK |
2476 | | EDGE_CAN_FALLTHRU | |
2477 | | EDGE_IRREDUCIBLE_LOOP | |
9beb1c84 | 2478 | | EDGE_LOOP_EXIT |
7e872b90 JJ |
2479 | | EDGE_CROSSING |
2480 | | EDGE_PRESERVE)) == 0) | |
3dec4024 JH |
2481 | n_branch++; |
2482 | ||
2483 | if (e->flags & EDGE_ABNORMAL_CALL) | |
f99ffaa3 EB |
2484 | n_abnormal_call++; |
2485 | ||
2486 | if (e->flags & EDGE_SIBCALL) | |
2487 | n_sibcall++; | |
3dec4024 JH |
2488 | |
2489 | if (e->flags & EDGE_EH) | |
2490 | n_eh++; | |
f99ffaa3 EB |
2491 | |
2492 | if (e->flags & EDGE_ABNORMAL) | |
3dec4024 | 2493 | n_abnormal++; |
ca6c03ca | 2494 | } |
5f0d2358 | 2495 | |
3371a64f TJ |
2496 | if (!has_crossing_edge |
2497 | && find_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX)) | |
2498 | { | |
2499 | print_rtl_with_bb (stderr, get_insns (), TDF_RTL | TDF_BLOCKS | TDF_DETAILS); | |
2500 | error ("Region crossing jump across same section in bb %i", | |
2501 | bb->index); | |
2502 | err = 1; | |
2503 | } | |
2504 | ||
1d65f45c | 2505 | if (n_eh && !find_reg_note (BB_END (bb), REG_EH_REGION, NULL_RTX)) |
3dec4024 | 2506 | { |
f99ffaa3 | 2507 | error ("missing REG_EH_REGION note at the end of bb %i", bb->index); |
3dec4024 JH |
2508 | err = 1; |
2509 | } | |
1d65f45c RH |
2510 | if (n_eh > 1) |
2511 | { | |
f99ffaa3 | 2512 | error ("too many exception handling edges in bb %i", bb->index); |
1d65f45c RH |
2513 | err = 1; |
2514 | } | |
3dec4024 | 2515 | if (n_branch |
4b4bf941 | 2516 | && (!JUMP_P (BB_END (bb)) |
a813c111 SB |
2517 | || (n_branch > 1 && (any_uncondjump_p (BB_END (bb)) |
2518 | || any_condjump_p (BB_END (bb)))))) | |
3dec4024 | 2519 | { |
ab532386 | 2520 | error ("too many outgoing branch edges from bb %i", bb->index); |
3dec4024 JH |
2521 | err = 1; |
2522 | } | |
a813c111 | 2523 | if (n_fallthru && any_uncondjump_p (BB_END (bb))) |
3dec4024 | 2524 | { |
f99ffaa3 | 2525 | error ("fallthru edge after unconditional jump in bb %i", bb->index); |
3dec4024 JH |
2526 | err = 1; |
2527 | } | |
a813c111 | 2528 | if (n_branch != 1 && any_uncondjump_p (BB_END (bb))) |
3dec4024 | 2529 | { |
f99ffaa3 EB |
2530 | error ("wrong number of branch edges after unconditional jump" |
2531 | " in bb %i", bb->index); | |
3dec4024 JH |
2532 | err = 1; |
2533 | } | |
a813c111 | 2534 | if (n_branch != 1 && any_condjump_p (BB_END (bb)) |
c11fd0b2 | 2535 | && JUMP_LABEL (BB_END (bb)) != BB_HEAD (fallthru->dest)) |
3dec4024 | 2536 | { |
f99ffaa3 EB |
2537 | error ("wrong amount of branch edges after conditional jump" |
2538 | " in bb %i", bb->index); | |
2539 | err = 1; | |
2540 | } | |
2541 | if (n_abnormal_call && !CALL_P (BB_END (bb))) | |
2542 | { | |
2543 | error ("abnormal call edges for non-call insn in bb %i", bb->index); | |
3dec4024 JH |
2544 | err = 1; |
2545 | } | |
f99ffaa3 | 2546 | if (n_sibcall && !CALL_P (BB_END (bb))) |
3dec4024 | 2547 | { |
f99ffaa3 | 2548 | error ("sibcall edges for non-call insn in bb %i", bb->index); |
3dec4024 JH |
2549 | err = 1; |
2550 | } | |
f99ffaa3 EB |
2551 | if (n_abnormal > n_eh |
2552 | && !(CALL_P (BB_END (bb)) | |
2553 | && n_abnormal == n_abnormal_call + n_sibcall) | |
4b4bf941 | 2554 | && (!JUMP_P (BB_END (bb)) |
a813c111 SB |
2555 | || any_condjump_p (BB_END (bb)) |
2556 | || any_uncondjump_p (BB_END (bb)))) | |
3dec4024 | 2557 | { |
ab532386 | 2558 | error ("abnormal edges for no purpose in bb %i", bb->index); |
3dec4024 JH |
2559 | err = 1; |
2560 | } | |
251a41b9 | 2561 | } |
f87c27b4 | 2562 | |
600b5b1d TJ |
2563 | /* If there are partitions, do a sanity check on them: A basic block in |
2564 | a cold partition cannot dominate a basic block in a hot partition. */ | |
2565 | if (crtl->has_bb_partition && !err) | |
2566 | { | |
2567 | vec<basic_block> bbs_to_fix = find_partition_fixes (true); | |
2568 | err = !bbs_to_fix.is_empty (); | |
2569 | } | |
2570 | ||
251a41b9 TJ |
2571 | /* Clean up. */ |
2572 | return err; | |
2573 | } | |
5f0d2358 | 2574 | |
251a41b9 TJ |
2575 | /* Checks on the instructions within blocks. Currently checks that each |
2576 | block starts with a basic block note, and that basic block notes and | |
2577 | control flow jumps are not found in the middle of the block. */ | |
ca6c03ca | 2578 | |
251a41b9 TJ |
2579 | static int |
2580 | rtl_verify_bb_insns (void) | |
2581 | { | |
2582 | rtx x; | |
2583 | int err = 0; | |
2584 | basic_block bb; | |
2585 | ||
2586 | FOR_EACH_BB_REVERSE (bb) | |
2587 | { | |
2588 | /* Now check the header of basic | |
c22cacf3 | 2589 | block. It ought to contain optional CODE_LABEL followed |
ca6c03ca | 2590 | by NOTE_BASIC_BLOCK. */ |
a813c111 | 2591 | x = BB_HEAD (bb); |
4b4bf941 | 2592 | if (LABEL_P (x)) |
ca6c03ca | 2593 | { |
a813c111 | 2594 | if (BB_END (bb) == x) |
ca6c03ca JH |
2595 | { |
2596 | error ("NOTE_INSN_BASIC_BLOCK is missing for block %d", | |
0b17ab2f | 2597 | bb->index); |
ca6c03ca JH |
2598 | err = 1; |
2599 | } | |
5f0d2358 | 2600 | |
ca6c03ca JH |
2601 | x = NEXT_INSN (x); |
2602 | } | |
5f0d2358 | 2603 | |
ca6c03ca JH |
2604 | if (!NOTE_INSN_BASIC_BLOCK_P (x) || NOTE_BASIC_BLOCK (x) != bb) |
2605 | { | |
2606 | error ("NOTE_INSN_BASIC_BLOCK is missing for block %d", | |
0b17ab2f | 2607 | bb->index); |
ca6c03ca JH |
2608 | err = 1; |
2609 | } | |
2610 | ||
a813c111 | 2611 | if (BB_END (bb) == x) |
49243025 | 2612 | /* Do checks for empty blocks here. */ |
5f0d2358 | 2613 | ; |
ca6c03ca | 2614 | else |
5f0d2358 RK |
2615 | for (x = NEXT_INSN (x); x; x = NEXT_INSN (x)) |
2616 | { | |
2617 | if (NOTE_INSN_BASIC_BLOCK_P (x)) | |
2618 | { | |
2619 | error ("NOTE_INSN_BASIC_BLOCK %d in middle of basic block %d", | |
0b17ab2f | 2620 | INSN_UID (x), bb->index); |
5f0d2358 RK |
2621 | err = 1; |
2622 | } | |
2623 | ||
a813c111 | 2624 | if (x == BB_END (bb)) |
5f0d2358 | 2625 | break; |
ca6c03ca | 2626 | |
83fd323c | 2627 | if (control_flow_insn_p (x)) |
5f0d2358 | 2628 | { |
0b17ab2f | 2629 | error ("in basic block %d:", bb->index); |
5f0d2358 RK |
2630 | fatal_insn ("flow control insn inside a basic block", x); |
2631 | } | |
2632 | } | |
ca6c03ca JH |
2633 | } |
2634 | ||
251a41b9 TJ |
2635 | /* Clean up. */ |
2636 | return err; | |
2637 | } | |
2638 | ||
2639 | /* Verify that block pointers for instructions in basic blocks, headers and | |
2640 | footers are set appropriately. */ | |
2641 | ||
2642 | static int | |
2643 | rtl_verify_bb_pointers (void) | |
2644 | { | |
2645 | int err = 0; | |
2646 | basic_block bb; | |
2647 | ||
2648 | /* Check the general integrity of the basic blocks. */ | |
2649 | FOR_EACH_BB_REVERSE (bb) | |
2650 | { | |
2651 | rtx insn; | |
2652 | ||
2653 | if (!(bb->flags & BB_RTL)) | |
2654 | { | |
2655 | error ("BB_RTL flag not set for block %d", bb->index); | |
2656 | err = 1; | |
2657 | } | |
2658 | ||
2659 | FOR_BB_INSNS (bb, insn) | |
2660 | if (BLOCK_FOR_INSN (insn) != bb) | |
2661 | { | |
2662 | error ("insn %d basic block pointer is %d, should be %d", | |
2663 | INSN_UID (insn), | |
2664 | BLOCK_FOR_INSN (insn) ? BLOCK_FOR_INSN (insn)->index : 0, | |
2665 | bb->index); | |
2666 | err = 1; | |
2667 | } | |
2668 | ||
2669 | for (insn = BB_HEADER (bb); insn; insn = NEXT_INSN (insn)) | |
2670 | if (!BARRIER_P (insn) | |
2671 | && BLOCK_FOR_INSN (insn) != NULL) | |
2672 | { | |
2673 | error ("insn %d in header of bb %d has non-NULL basic block", | |
2674 | INSN_UID (insn), bb->index); | |
2675 | err = 1; | |
2676 | } | |
2677 | for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn)) | |
2678 | if (!BARRIER_P (insn) | |
2679 | && BLOCK_FOR_INSN (insn) != NULL) | |
2680 | { | |
2681 | error ("insn %d in footer of bb %d has non-NULL basic block", | |
2682 | INSN_UID (insn), bb->index); | |
2683 | err = 1; | |
2684 | } | |
2685 | } | |
af205f67 | 2686 | |
10e9fecc | 2687 | /* Clean up. */ |
10e9fecc JH |
2688 | return err; |
2689 | } | |
5f0d2358 | 2690 | |
10e9fecc JH |
2691 | /* Verify the CFG and RTL consistency common for both underlying RTL and |
2692 | cfglayout RTL. | |
5f0d2358 | 2693 | |
10e9fecc | 2694 | Currently it does following checks: |
251a41b9 TJ |
2695 | |
2696 | - overlapping of basic blocks | |
2697 | - insns with wrong BLOCK_FOR_INSN pointers | |
2698 | - headers of basic blocks (the NOTE_INSN_BASIC_BLOCK note) | |
2699 | - tails of basic blocks (ensure that boundary is necessary) | |
2700 | - scans body of the basic block for JUMP_INSN, CODE_LABEL | |
2701 | and NOTE_INSN_BASIC_BLOCK | |
2702 | - verify that no fall_thru edge crosses hot/cold partition boundaries | |
2703 | - verify that there are no pending RTL branch predictions | |
600b5b1d | 2704 | - verify that hot blocks are not dominated by cold blocks |
251a41b9 TJ |
2705 | |
2706 | In future it can be extended check a lot of other stuff as well | |
2707 | (reachability of basic blocks, life information, etc. etc.). */ | |
9eab6785 | 2708 | |
10e9fecc | 2709 | static int |
251a41b9 TJ |
2710 | rtl_verify_flow_info_1 (void) |
2711 | { | |
2712 | int err = 0; | |
2713 | ||
2714 | err |= rtl_verify_bb_pointers (); | |
2715 | ||
2716 | err |= rtl_verify_bb_insns (); | |
2717 | ||
2718 | err |= rtl_verify_edges (); | |
2719 | ||
251a41b9 TJ |
2720 | return err; |
2721 | } | |
2722 | ||
2723 | /* Walk the instruction chain and verify that bb head/end pointers | |
2724 | are correct, and that instructions are in exactly one bb and have | |
2725 | correct block pointers. */ | |
2726 | ||
2727 | static int | |
2728 | rtl_verify_bb_insn_chain (void) | |
10e9fecc JH |
2729 | { |
2730 | basic_block bb; | |
251a41b9 | 2731 | int err = 0; |
10e9fecc | 2732 | rtx x; |
9eab6785 SB |
2733 | rtx last_head = get_last_insn (); |
2734 | basic_block *bb_info; | |
9eab6785 SB |
2735 | const int max_uid = get_max_uid (); |
2736 | ||
2737 | bb_info = XCNEWVEC (basic_block, max_uid); | |
ca6c03ca | 2738 | |
10e9fecc JH |
2739 | FOR_EACH_BB_REVERSE (bb) |
2740 | { | |
9eab6785 SB |
2741 | rtx head = BB_HEAD (bb); |
2742 | rtx end = BB_END (bb); | |
628f6a4e | 2743 | |
9eab6785 | 2744 | for (x = last_head; x != NULL_RTX; x = PREV_INSN (x)) |
a7b87f73 ZD |
2745 | { |
2746 | /* Verify the end of the basic block is in the INSN chain. */ | |
2747 | if (x == end) | |
2748 | break; | |
2749 | ||
251a41b9 TJ |
2750 | /* And that the code outside of basic blocks has NULL bb field. */ |
2751 | if (!BARRIER_P (x) | |
2752 | && BLOCK_FOR_INSN (x) != NULL) | |
2753 | { | |
2754 | error ("insn %d outside of basic blocks has non-NULL bb field", | |
2755 | INSN_UID (x)); | |
2756 | err = 1; | |
2757 | } | |
a7b87f73 | 2758 | } |
9eab6785 SB |
2759 | |
2760 | if (!x) | |
2761 | { | |
2762 | error ("end insn %d for block %d not found in the insn stream", | |
2763 | INSN_UID (end), bb->index); | |
2764 | err = 1; | |
2765 | } | |
2766 | ||
2767 | /* Work backwards from the end to the head of the basic block | |
2768 | to verify the head is in the RTL chain. */ | |
2769 | for (; x != NULL_RTX; x = PREV_INSN (x)) | |
a00d11f0 | 2770 | { |
9eab6785 SB |
2771 | /* While walking over the insn chain, verify insns appear |
2772 | in only one basic block. */ | |
2773 | if (bb_info[INSN_UID (x)] != NULL) | |
2774 | { | |
2775 | error ("insn %d is in multiple basic blocks (%d and %d)", | |
2776 | INSN_UID (x), bb->index, bb_info[INSN_UID (x)]->index); | |
2777 | err = 1; | |
2778 | } | |
2779 | ||
2780 | bb_info[INSN_UID (x)] = bb; | |
2781 | ||
2782 | if (x == head) | |
2783 | break; | |
2784 | } | |
2785 | if (!x) | |
2786 | { | |
2787 | error ("head insn %d for block %d not found in the insn stream", | |
2788 | INSN_UID (head), bb->index); | |
a00d11f0 JH |
2789 | err = 1; |
2790 | } | |
2791 | ||
a7b87f73 | 2792 | last_head = PREV_INSN (x); |
251a41b9 TJ |
2793 | } |
2794 | ||
2795 | for (x = last_head; x != NULL_RTX; x = PREV_INSN (x)) | |
2796 | { | |
2797 | /* Check that the code before the first basic block has NULL | |
2798 | bb field. */ | |
2799 | if (!BARRIER_P (x) | |
2800 | && BLOCK_FOR_INSN (x) != NULL) | |
2801 | { | |
2802 | error ("insn %d outside of basic blocks has non-NULL bb field", | |
2803 | INSN_UID (x)); | |
2804 | err = 1; | |
2805 | } | |
2806 | } | |
2807 | free (bb_info); | |
2808 | ||
2809 | return err; | |
2810 | } | |
2811 | ||
2812 | /* Verify that fallthru edges point to adjacent blocks in layout order and | |
2813 | that barriers exist after non-fallthru blocks. */ | |
2814 | ||
2815 | static int | |
2816 | rtl_verify_fallthru (void) | |
2817 | { | |
2818 | basic_block bb; | |
2819 | int err = 0; | |
2820 | ||
2821 | FOR_EACH_BB_REVERSE (bb) | |
2822 | { | |
2823 | edge e; | |
9eab6785 | 2824 | |
0fd4b31d | 2825 | e = find_fallthru_edge (bb->succs); |
10e9fecc JH |
2826 | if (!e) |
2827 | { | |
2828 | rtx insn; | |
2829 | ||
2830 | /* Ensure existence of barrier in BB with no fallthru edges. */ | |
468059bc DD |
2831 | for (insn = NEXT_INSN (BB_END (bb)); ; insn = NEXT_INSN (insn)) |
2832 | { | |
2833 | if (!insn || NOTE_INSN_BASIC_BLOCK_P (insn)) | |
10e9fecc JH |
2834 | { |
2835 | error ("missing barrier after block %i", bb->index); | |
2836 | err = 1; | |
2837 | break; | |
2838 | } | |
468059bc DD |
2839 | if (BARRIER_P (insn)) |
2840 | break; | |
2841 | } | |
10e9fecc | 2842 | } |
fefa31b5 DM |
2843 | else if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
2844 | && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
c22cacf3 | 2845 | { |
10e9fecc JH |
2846 | rtx insn; |
2847 | ||
2848 | if (e->src->next_bb != e->dest) | |
2849 | { | |
2850 | error | |
2851 | ("verify_flow_info: Incorrect blocks for fallthru %i->%i", | |
2852 | e->src->index, e->dest->index); | |
2853 | err = 1; | |
2854 | } | |
2855 | else | |
a813c111 | 2856 | for (insn = NEXT_INSN (BB_END (e->src)); insn != BB_HEAD (e->dest); |
10e9fecc | 2857 | insn = NEXT_INSN (insn)) |
6be85b25 | 2858 | if (BARRIER_P (insn) || INSN_P (insn)) |
10e9fecc JH |
2859 | { |
2860 | error ("verify_flow_info: Incorrect fallthru %i->%i", | |
2861 | e->src->index, e->dest->index); | |
2862 | fatal_insn ("wrong insn in the fallthru edge", insn); | |
2863 | err = 1; | |
2864 | } | |
c22cacf3 | 2865 | } |
10e9fecc | 2866 | } |
ca6c03ca | 2867 | |
251a41b9 TJ |
2868 | return err; |
2869 | } | |
2870 | ||
2871 | /* Verify that blocks are laid out in consecutive order. While walking the | |
2872 | instructions, verify that all expected instructions are inside the basic | |
2873 | blocks, and that all returns are followed by barriers. */ | |
2874 | ||
2875 | static int | |
2876 | rtl_verify_bb_layout (void) | |
2877 | { | |
2878 | basic_block bb; | |
2879 | int err = 0; | |
2880 | rtx x; | |
2881 | int num_bb_notes; | |
2882 | const rtx rtx_first = get_insns (); | |
fefa31b5 | 2883 | basic_block last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun), curr_bb = NULL; |
9eab6785 | 2884 | |
ca6c03ca | 2885 | num_bb_notes = 0; |
fefa31b5 | 2886 | last_bb_seen = ENTRY_BLOCK_PTR_FOR_FN (cfun); |
e0082a72 | 2887 | |
5f0d2358 | 2888 | for (x = rtx_first; x; x = NEXT_INSN (x)) |
ca6c03ca JH |
2889 | { |
2890 | if (NOTE_INSN_BASIC_BLOCK_P (x)) | |
2891 | { | |
bf77398c | 2892 | bb = NOTE_BASIC_BLOCK (x); |
5f0d2358 | 2893 | |
ca6c03ca | 2894 | num_bb_notes++; |
e0082a72 | 2895 | if (bb != last_bb_seen->next_bb) |
10e9fecc | 2896 | internal_error ("basic blocks not laid down consecutively"); |
ca6c03ca | 2897 | |
10e9fecc | 2898 | curr_bb = last_bb_seen = bb; |
ca6c03ca JH |
2899 | } |
2900 | ||
10e9fecc | 2901 | if (!curr_bb) |
ca6c03ca JH |
2902 | { |
2903 | switch (GET_CODE (x)) | |
2904 | { | |
2905 | case BARRIER: | |
2906 | case NOTE: | |
2907 | break; | |
2908 | ||
2909 | case CODE_LABEL: | |
39718607 | 2910 | /* An ADDR_VEC is placed outside any basic block. */ |
ca6c03ca | 2911 | if (NEXT_INSN (x) |
481683e1 | 2912 | && JUMP_TABLE_DATA_P (NEXT_INSN (x))) |
5f0d2358 | 2913 | x = NEXT_INSN (x); |
ca6c03ca JH |
2914 | |
2915 | /* But in any case, non-deletable labels can appear anywhere. */ | |
2916 | break; | |
2917 | ||
2918 | default: | |
1f978f5f | 2919 | fatal_insn ("insn outside basic block", x); |
ca6c03ca JH |
2920 | } |
2921 | } | |
2922 | ||
26cae194 | 2923 | if (JUMP_P (x) |
ca6c03ca | 2924 | && returnjump_p (x) && ! condjump_p (x) |
15eb3a2e | 2925 | && ! (next_nonnote_insn (x) && BARRIER_P (next_nonnote_insn (x)))) |
1f978f5f | 2926 | fatal_insn ("return not followed by barrier", x); |
251a41b9 | 2927 | |
a813c111 | 2928 | if (curr_bb && x == BB_END (curr_bb)) |
10e9fecc | 2929 | curr_bb = NULL; |
ca6c03ca JH |
2930 | } |
2931 | ||
0cae8d31 | 2932 | if (num_bb_notes != n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS) |
ca6c03ca | 2933 | internal_error |
0b17ab2f | 2934 | ("number of bb notes in insn chain (%d) != n_basic_blocks (%d)", |
0cae8d31 | 2935 | num_bb_notes, n_basic_blocks_for_fn (cfun)); |
ca6c03ca | 2936 | |
10e9fecc | 2937 | return err; |
ca6c03ca | 2938 | } |
251a41b9 TJ |
2939 | |
2940 | /* Verify the CFG and RTL consistency common for both underlying RTL and | |
2941 | cfglayout RTL, plus consistency checks specific to linearized RTL mode. | |
2942 | ||
2943 | Currently it does following checks: | |
2944 | - all checks of rtl_verify_flow_info_1 | |
2945 | - test head/end pointers | |
2946 | - check that blocks are laid out in consecutive order | |
2947 | - check that all insns are in the basic blocks | |
2948 | (except the switch handling code, barriers and notes) | |
2949 | - check that all returns are followed by barriers | |
600b5b1d TJ |
2950 | - check that all fallthru edge points to the adjacent blocks |
2951 | - verify that there is a single hot/cold partition boundary after bbro */ | |
251a41b9 TJ |
2952 | |
2953 | static int | |
2954 | rtl_verify_flow_info (void) | |
2955 | { | |
2956 | int err = 0; | |
2957 | ||
2958 | err |= rtl_verify_flow_info_1 (); | |
2959 | ||
2960 | err |= rtl_verify_bb_insn_chain (); | |
2961 | ||
2962 | err |= rtl_verify_fallthru (); | |
2963 | ||
2964 | err |= rtl_verify_bb_layout (); | |
2965 | ||
3371a64f TJ |
2966 | err |= verify_hot_cold_block_grouping (); |
2967 | ||
251a41b9 TJ |
2968 | return err; |
2969 | } | |
ca6c03ca | 2970 | \f |
eaec9b3d | 2971 | /* Assume that the preceding pass has possibly eliminated jump instructions |
ca6c03ca JH |
2972 | or converted the unconditional jumps. Eliminate the edges from CFG. |
2973 | Return true if any edges are eliminated. */ | |
2974 | ||
2975 | bool | |
d329e058 | 2976 | purge_dead_edges (basic_block bb) |
ca6c03ca | 2977 | { |
628f6a4e | 2978 | edge e; |
a813c111 | 2979 | rtx insn = BB_END (bb), note; |
ca6c03ca | 2980 | bool purged = false; |
628f6a4e BE |
2981 | bool found; |
2982 | edge_iterator ei; | |
ca6c03ca | 2983 | |
b5b8b0ac AO |
2984 | if (DEBUG_INSN_P (insn) && insn != BB_HEAD (bb)) |
2985 | do | |
2986 | insn = PREV_INSN (insn); | |
2987 | while ((DEBUG_INSN_P (insn) || NOTE_P (insn)) && insn != BB_HEAD (bb)); | |
2988 | ||
70da1d03 | 2989 | /* If this instruction cannot trap, remove REG_EH_REGION notes. */ |
4b4bf941 | 2990 | if (NONJUMP_INSN_P (insn) |
70da1d03 JH |
2991 | && (note = find_reg_note (insn, REG_EH_REGION, NULL))) |
2992 | { | |
2993 | rtx eqnote; | |
2994 | ||
2995 | if (! may_trap_p (PATTERN (insn)) | |
2996 | || ((eqnote = find_reg_equal_equiv_note (insn)) | |
2997 | && ! may_trap_p (XEXP (eqnote, 0)))) | |
2998 | remove_note (insn, note); | |
2999 | } | |
3000 | ||
546c093e | 3001 | /* Cleanup abnormal edges caused by exceptions or non-local gotos. */ |
628f6a4e | 3002 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
546c093e | 3003 | { |
1d65f45c RH |
3004 | bool remove = false; |
3005 | ||
e5f9a909 JW |
3006 | /* There are three types of edges we need to handle correctly here: EH |
3007 | edges, abnormal call EH edges, and abnormal call non-EH edges. The | |
3008 | latter can appear when nonlocal gotos are used. */ | |
1d65f45c | 3009 | if (e->flags & EDGE_ABNORMAL_CALL) |
546c093e | 3010 | { |
1d65f45c RH |
3011 | if (!CALL_P (insn)) |
3012 | remove = true; | |
3013 | else if (can_nonlocal_goto (insn)) | |
3014 | ; | |
3015 | else if ((e->flags & EDGE_EH) && can_throw_internal (insn)) | |
3016 | ; | |
0a35513e AH |
3017 | else if (flag_tm && find_reg_note (insn, REG_TM, NULL)) |
3018 | ; | |
1d65f45c RH |
3019 | else |
3020 | remove = true; | |
546c093e | 3021 | } |
1d65f45c RH |
3022 | else if (e->flags & EDGE_EH) |
3023 | remove = !can_throw_internal (insn); | |
3024 | ||
3025 | if (remove) | |
546c093e | 3026 | { |
1d65f45c RH |
3027 | remove_edge (e); |
3028 | df_set_bb_dirty (bb); | |
3029 | purged = true; | |
546c093e RH |
3030 | } |
3031 | else | |
1d65f45c | 3032 | ei_next (&ei); |
546c093e | 3033 | } |
5f0d2358 | 3034 | |
4b4bf941 | 3035 | if (JUMP_P (insn)) |
ca6c03ca JH |
3036 | { |
3037 | rtx note; | |
3038 | edge b,f; | |
628f6a4e | 3039 | edge_iterator ei; |
5f0d2358 | 3040 | |
ca6c03ca JH |
3041 | /* We do care only about conditional jumps and simplejumps. */ |
3042 | if (!any_condjump_p (insn) | |
3043 | && !returnjump_p (insn) | |
3044 | && !simplejump_p (insn)) | |
c51d95ec | 3045 | return purged; |
5f0d2358 | 3046 | |
5a1a3e5e JH |
3047 | /* Branch probability/prediction notes are defined only for |
3048 | condjumps. We've possibly turned condjump into simplejump. */ | |
3049 | if (simplejump_p (insn)) | |
3050 | { | |
3051 | note = find_reg_note (insn, REG_BR_PROB, NULL); | |
3052 | if (note) | |
3053 | remove_note (insn, note); | |
3054 | while ((note = find_reg_note (insn, REG_BR_PRED, NULL))) | |
3055 | remove_note (insn, note); | |
3056 | } | |
3057 | ||
628f6a4e | 3058 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
ca6c03ca | 3059 | { |
7fcd7218 JH |
3060 | /* Avoid abnormal flags to leak from computed jumps turned |
3061 | into simplejumps. */ | |
f87c27b4 | 3062 | |
0e1638d4 | 3063 | e->flags &= ~EDGE_ABNORMAL; |
7fcd7218 | 3064 | |
5a566bed MM |
3065 | /* See if this edge is one we should keep. */ |
3066 | if ((e->flags & EDGE_FALLTHRU) && any_condjump_p (insn)) | |
3067 | /* A conditional jump can fall through into the next | |
3068 | block, so we should keep the edge. */ | |
628f6a4e BE |
3069 | { |
3070 | ei_next (&ei); | |
3071 | continue; | |
3072 | } | |
fefa31b5 | 3073 | else if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) |
a813c111 | 3074 | && BB_HEAD (e->dest) == JUMP_LABEL (insn)) |
5a566bed MM |
3075 | /* If the destination block is the target of the jump, |
3076 | keep the edge. */ | |
628f6a4e BE |
3077 | { |
3078 | ei_next (&ei); | |
3079 | continue; | |
3080 | } | |
fefa31b5 DM |
3081 | else if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun) |
3082 | && returnjump_p (insn)) | |
5a566bed MM |
3083 | /* If the destination block is the exit block, and this |
3084 | instruction is a return, then keep the edge. */ | |
628f6a4e BE |
3085 | { |
3086 | ei_next (&ei); | |
3087 | continue; | |
3088 | } | |
5a566bed MM |
3089 | else if ((e->flags & EDGE_EH) && can_throw_internal (insn)) |
3090 | /* Keep the edges that correspond to exceptions thrown by | |
0b75beaa EB |
3091 | this instruction and rematerialize the EDGE_ABNORMAL |
3092 | flag we just cleared above. */ | |
3093 | { | |
3094 | e->flags |= EDGE_ABNORMAL; | |
628f6a4e | 3095 | ei_next (&ei); |
0b75beaa EB |
3096 | continue; |
3097 | } | |
5f0d2358 | 3098 | |
5a566bed | 3099 | /* We do not need this edge. */ |
6fb5fa3c | 3100 | df_set_bb_dirty (bb); |
ca6c03ca JH |
3101 | purged = true; |
3102 | remove_edge (e); | |
3103 | } | |
5f0d2358 | 3104 | |
628f6a4e | 3105 | if (EDGE_COUNT (bb->succs) == 0 || !purged) |
c51d95ec | 3106 | return purged; |
5f0d2358 | 3107 | |
c263766c RH |
3108 | if (dump_file) |
3109 | fprintf (dump_file, "Purged edges from bb %i\n", bb->index); | |
5f0d2358 | 3110 | |
ca6c03ca JH |
3111 | if (!optimize) |
3112 | return purged; | |
3113 | ||
3114 | /* Redistribute probabilities. */ | |
c5cbcccf | 3115 | if (single_succ_p (bb)) |
ca6c03ca | 3116 | { |
c5cbcccf ZD |
3117 | single_succ_edge (bb)->probability = REG_BR_PROB_BASE; |
3118 | single_succ_edge (bb)->count = bb->count; | |
f87c27b4 | 3119 | } |
ca6c03ca JH |
3120 | else |
3121 | { | |
3122 | note = find_reg_note (insn, REG_BR_PROB, NULL); | |
3123 | if (!note) | |
3124 | return purged; | |
5f0d2358 | 3125 | |
ca6c03ca JH |
3126 | b = BRANCH_EDGE (bb); |
3127 | f = FALLTHRU_EDGE (bb); | |
e5af9ddd | 3128 | b->probability = XINT (note, 0); |
ca6c03ca | 3129 | f->probability = REG_BR_PROB_BASE - b->probability; |
8ddb5a29 | 3130 | /* Update these to use GCOV_COMPUTE_SCALE. */ |
ca6c03ca JH |
3131 | b->count = bb->count * b->probability / REG_BR_PROB_BASE; |
3132 | f->count = bb->count * f->probability / REG_BR_PROB_BASE; | |
3133 | } | |
5f0d2358 | 3134 | |
ca6c03ca JH |
3135 | return purged; |
3136 | } | |
4b4bf941 | 3137 | else if (CALL_P (insn) && SIBLING_CALL_P (insn)) |
1722c2c8 RH |
3138 | { |
3139 | /* First, there should not be any EH or ABCALL edges resulting | |
3140 | from non-local gotos and the like. If there were, we shouldn't | |
3141 | have created the sibcall in the first place. Second, there | |
3142 | should of course never have been a fallthru edge. */ | |
c5cbcccf ZD |
3143 | gcc_assert (single_succ_p (bb)); |
3144 | gcc_assert (single_succ_edge (bb)->flags | |
3145 | == (EDGE_SIBCALL | EDGE_ABNORMAL)); | |
1722c2c8 RH |
3146 | |
3147 | return 0; | |
3148 | } | |
ca6c03ca | 3149 | |
ca6c03ca JH |
3150 | /* If we don't see a jump insn, we don't know exactly why the block would |
3151 | have been broken at this point. Look for a simple, non-fallthru edge, | |
3152 | as these are only created by conditional branches. If we find such an | |
3153 | edge we know that there used to be a jump here and can then safely | |
3154 | remove all non-fallthru edges. */ | |
628f6a4e BE |
3155 | found = false; |
3156 | FOR_EACH_EDGE (e, ei, bb->succs) | |
3157 | if (! (e->flags & (EDGE_COMPLEX | EDGE_FALLTHRU))) | |
3158 | { | |
3159 | found = true; | |
3160 | break; | |
3161 | } | |
5f0d2358 | 3162 | |
628f6a4e | 3163 | if (!found) |
ca6c03ca | 3164 | return purged; |
5f0d2358 | 3165 | |
2afa8dce DB |
3166 | /* Remove all but the fake and fallthru edges. The fake edge may be |
3167 | the only successor for this block in the case of noreturn | |
3168 | calls. */ | |
628f6a4e | 3169 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) |
ca6c03ca | 3170 | { |
2afa8dce | 3171 | if (!(e->flags & (EDGE_FALLTHRU | EDGE_FAKE))) |
c51d95ec | 3172 | { |
6fb5fa3c | 3173 | df_set_bb_dirty (bb); |
c51d95ec JH |
3174 | remove_edge (e); |
3175 | purged = true; | |
3176 | } | |
628f6a4e BE |
3177 | else |
3178 | ei_next (&ei); | |
ca6c03ca | 3179 | } |
5f0d2358 | 3180 | |
c5cbcccf | 3181 | gcc_assert (single_succ_p (bb)); |
5f0d2358 | 3182 | |
c5cbcccf ZD |
3183 | single_succ_edge (bb)->probability = REG_BR_PROB_BASE; |
3184 | single_succ_edge (bb)->count = bb->count; | |
ca6c03ca | 3185 | |
c263766c RH |
3186 | if (dump_file) |
3187 | fprintf (dump_file, "Purged non-fallthru edges from bb %i\n", | |
0b17ab2f | 3188 | bb->index); |
ca6c03ca JH |
3189 | return purged; |
3190 | } | |
3191 | ||
5f0d2358 RK |
3192 | /* Search all basic blocks for potentially dead edges and purge them. Return |
3193 | true if some edge has been eliminated. */ | |
ca6c03ca JH |
3194 | |
3195 | bool | |
25cd19de | 3196 | purge_all_dead_edges (void) |
ca6c03ca | 3197 | { |
e0082a72 | 3198 | int purged = false; |
e0082a72 | 3199 | basic_block bb; |
473fb060 | 3200 | |
e0082a72 | 3201 | FOR_EACH_BB (bb) |
473fb060 | 3202 | { |
e0082a72 | 3203 | bool purged_here = purge_dead_edges (bb); |
5f0d2358 | 3204 | |
473fb060 | 3205 | purged |= purged_here; |
473fb060 | 3206 | } |
5f0d2358 | 3207 | |
ca6c03ca JH |
3208 | return purged; |
3209 | } | |
9ee634e3 | 3210 | |
ba5e9aca EB |
3211 | /* This is used by a few passes that emit some instructions after abnormal |
3212 | calls, moving the basic block's end, while they in fact do want to emit | |
3213 | them on the fallthru edge. Look for abnormal call edges, find backward | |
3214 | the call in the block and insert the instructions on the edge instead. | |
3215 | ||
3216 | Similarly, handle instructions throwing exceptions internally. | |
3217 | ||
3218 | Return true when instructions have been found and inserted on edges. */ | |
3219 | ||
3220 | bool | |
3221 | fixup_abnormal_edges (void) | |
3222 | { | |
3223 | bool inserted = false; | |
3224 | basic_block bb; | |
3225 | ||
3226 | FOR_EACH_BB (bb) | |
3227 | { | |
3228 | edge e; | |
3229 | edge_iterator ei; | |
3230 | ||
3231 | /* Look for cases we are interested in - calls or instructions causing | |
3232 | exceptions. */ | |
3233 | FOR_EACH_EDGE (e, ei, bb->succs) | |
3234 | if ((e->flags & EDGE_ABNORMAL_CALL) | |
3235 | || ((e->flags & (EDGE_ABNORMAL | EDGE_EH)) | |
3236 | == (EDGE_ABNORMAL | EDGE_EH))) | |
3237 | break; | |
3238 | ||
3239 | if (e && !CALL_P (BB_END (bb)) && !can_throw_internal (BB_END (bb))) | |
3240 | { | |
3241 | rtx insn; | |
3242 | ||
3243 | /* Get past the new insns generated. Allow notes, as the insns | |
3244 | may be already deleted. */ | |
3245 | insn = BB_END (bb); | |
3246 | while ((NONJUMP_INSN_P (insn) || NOTE_P (insn)) | |
3247 | && !can_throw_internal (insn) | |
3248 | && insn != BB_HEAD (bb)) | |
3249 | insn = PREV_INSN (insn); | |
3250 | ||
3251 | if (CALL_P (insn) || can_throw_internal (insn)) | |
3252 | { | |
3253 | rtx stop, next; | |
3254 | ||
3255 | e = find_fallthru_edge (bb->succs); | |
3256 | ||
3257 | stop = NEXT_INSN (BB_END (bb)); | |
3258 | BB_END (bb) = insn; | |
3259 | ||
3260 | for (insn = NEXT_INSN (insn); insn != stop; insn = next) | |
3261 | { | |
3262 | next = NEXT_INSN (insn); | |
3263 | if (INSN_P (insn)) | |
3264 | { | |
3265 | delete_insn (insn); | |
3266 | ||
3267 | /* Sometimes there's still the return value USE. | |
3268 | If it's placed after a trapping call (i.e. that | |
3269 | call is the last insn anyway), we have no fallthru | |
3270 | edge. Simply delete this use and don't try to insert | |
3271 | on the non-existent edge. */ | |
3272 | if (GET_CODE (PATTERN (insn)) != USE) | |
3273 | { | |
3274 | /* We're not deleting it, we're moving it. */ | |
3275 | INSN_DELETED_P (insn) = 0; | |
3276 | PREV_INSN (insn) = NULL_RTX; | |
3277 | NEXT_INSN (insn) = NULL_RTX; | |
3278 | ||
3279 | insert_insn_on_edge (insn, e); | |
3280 | inserted = true; | |
3281 | } | |
3282 | } | |
3283 | else if (!BARRIER_P (insn)) | |
3284 | set_block_for_insn (insn, NULL); | |
3285 | } | |
3286 | } | |
3287 | ||
3288 | /* It may be that we don't find any trapping insn. In this | |
3289 | case we discovered quite late that the insn that had been | |
3290 | marked as can_throw_internal in fact couldn't trap at all. | |
3291 | So we should in fact delete the EH edges out of the block. */ | |
3292 | else | |
3293 | purge_dead_edges (bb); | |
3294 | } | |
3295 | } | |
3296 | ||
3297 | return inserted; | |
3298 | } | |
78bde837 SB |
3299 | \f |
3300 | /* Cut the insns from FIRST to LAST out of the insns stream. */ | |
3301 | ||
3302 | rtx | |
3303 | unlink_insn_chain (rtx first, rtx last) | |
3304 | { | |
3305 | rtx prevfirst = PREV_INSN (first); | |
3306 | rtx nextlast = NEXT_INSN (last); | |
3307 | ||
3308 | PREV_INSN (first) = NULL; | |
3309 | NEXT_INSN (last) = NULL; | |
3310 | if (prevfirst) | |
3311 | NEXT_INSN (prevfirst) = nextlast; | |
3312 | if (nextlast) | |
3313 | PREV_INSN (nextlast) = prevfirst; | |
3314 | else | |
3315 | set_last_insn (prevfirst); | |
3316 | if (!prevfirst) | |
3317 | set_first_insn (nextlast); | |
3318 | return first; | |
3319 | } | |
3320 | \f | |
3321 | /* Skip over inter-block insns occurring after BB which are typically | |
3322 | associated with BB (e.g., barriers). If there are any such insns, | |
3323 | we return the last one. Otherwise, we return the end of BB. */ | |
3324 | ||
3325 | static rtx | |
3326 | skip_insns_after_block (basic_block bb) | |
3327 | { | |
3328 | rtx insn, last_insn, next_head, prev; | |
3329 | ||
3330 | next_head = NULL_RTX; | |
fefa31b5 | 3331 | if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
78bde837 SB |
3332 | next_head = BB_HEAD (bb->next_bb); |
3333 | ||
3334 | for (last_insn = insn = BB_END (bb); (insn = NEXT_INSN (insn)) != 0; ) | |
3335 | { | |
3336 | if (insn == next_head) | |
3337 | break; | |
3338 | ||
3339 | switch (GET_CODE (insn)) | |
3340 | { | |
3341 | case BARRIER: | |
3342 | last_insn = insn; | |
3343 | continue; | |
3344 | ||
3345 | case NOTE: | |
3346 | switch (NOTE_KIND (insn)) | |
3347 | { | |
3348 | case NOTE_INSN_BLOCK_END: | |
3349 | gcc_unreachable (); | |
3350 | continue; | |
3351 | default: | |
3352 | continue; | |
3353 | break; | |
3354 | } | |
3355 | break; | |
3356 | ||
3357 | case CODE_LABEL: | |
3358 | if (NEXT_INSN (insn) | |
3359 | && JUMP_TABLE_DATA_P (NEXT_INSN (insn))) | |
3360 | { | |
3361 | insn = NEXT_INSN (insn); | |
3362 | last_insn = insn; | |
3363 | continue; | |
3364 | } | |
3365 | break; | |
3366 | ||
3367 | default: | |
3368 | break; | |
3369 | } | |
3370 | ||
3371 | break; | |
3372 | } | |
3373 | ||
3374 | /* It is possible to hit contradictory sequence. For instance: | |
3375 | ||
3376 | jump_insn | |
3377 | NOTE_INSN_BLOCK_BEG | |
3378 | barrier | |
3379 | ||
3380 | Where barrier belongs to jump_insn, but the note does not. This can be | |
3381 | created by removing the basic block originally following | |
3382 | NOTE_INSN_BLOCK_BEG. In such case reorder the notes. */ | |
3383 | ||
3384 | for (insn = last_insn; insn != BB_END (bb); insn = prev) | |
3385 | { | |
3386 | prev = PREV_INSN (insn); | |
3387 | if (NOTE_P (insn)) | |
3388 | switch (NOTE_KIND (insn)) | |
3389 | { | |
3390 | case NOTE_INSN_BLOCK_END: | |
3391 | gcc_unreachable (); | |
3392 | break; | |
3393 | case NOTE_INSN_DELETED: | |
3394 | case NOTE_INSN_DELETED_LABEL: | |
3395 | case NOTE_INSN_DELETED_DEBUG_LABEL: | |
3396 | continue; | |
3397 | default: | |
3398 | reorder_insns (insn, insn, last_insn); | |
3399 | } | |
3400 | } | |
3401 | ||
3402 | return last_insn; | |
3403 | } | |
3404 | ||
3405 | /* Locate or create a label for a given basic block. */ | |
3406 | ||
3407 | static rtx | |
3408 | label_for_bb (basic_block bb) | |
3409 | { | |
3410 | rtx label = BB_HEAD (bb); | |
3411 | ||
3412 | if (!LABEL_P (label)) | |
3413 | { | |
3414 | if (dump_file) | |
3415 | fprintf (dump_file, "Emitting label for block %d\n", bb->index); | |
3416 | ||
3417 | label = block_label (bb); | |
3418 | } | |
3419 | ||
3420 | return label; | |
3421 | } | |
3422 | ||
3423 | /* Locate the effective beginning and end of the insn chain for each | |
3424 | block, as defined by skip_insns_after_block above. */ | |
3425 | ||
3426 | static void | |
3427 | record_effective_endpoints (void) | |
3428 | { | |
3429 | rtx next_insn; | |
3430 | basic_block bb; | |
3431 | rtx insn; | |
3432 | ||
3433 | for (insn = get_insns (); | |
3434 | insn | |
3435 | && NOTE_P (insn) | |
3436 | && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK; | |
3437 | insn = NEXT_INSN (insn)) | |
3438 | continue; | |
3439 | /* No basic blocks at all? */ | |
3440 | gcc_assert (insn); | |
3441 | ||
3442 | if (PREV_INSN (insn)) | |
3443 | cfg_layout_function_header = | |
3444 | unlink_insn_chain (get_insns (), PREV_INSN (insn)); | |
3445 | else | |
3446 | cfg_layout_function_header = NULL_RTX; | |
3447 | ||
3448 | next_insn = get_insns (); | |
3449 | FOR_EACH_BB (bb) | |
3450 | { | |
3451 | rtx end; | |
3452 | ||
3453 | if (PREV_INSN (BB_HEAD (bb)) && next_insn != BB_HEAD (bb)) | |
3454 | BB_HEADER (bb) = unlink_insn_chain (next_insn, | |
3455 | PREV_INSN (BB_HEAD (bb))); | |
3456 | end = skip_insns_after_block (bb); | |
3457 | if (NEXT_INSN (BB_END (bb)) && BB_END (bb) != end) | |
3458 | BB_FOOTER (bb) = unlink_insn_chain (NEXT_INSN (BB_END (bb)), end); | |
3459 | next_insn = NEXT_INSN (BB_END (bb)); | |
3460 | } | |
3461 | ||
3462 | cfg_layout_function_footer = next_insn; | |
3463 | if (cfg_layout_function_footer) | |
3464 | cfg_layout_function_footer = unlink_insn_chain (cfg_layout_function_footer, get_last_insn ()); | |
3465 | } | |
3466 | \f | |
3467 | static unsigned int | |
3468 | into_cfg_layout_mode (void) | |
3469 | { | |
3470 | cfg_layout_initialize (0); | |
3471 | return 0; | |
3472 | } | |
3473 | ||
3474 | static unsigned int | |
3475 | outof_cfg_layout_mode (void) | |
3476 | { | |
3477 | basic_block bb; | |
3478 | ||
3479 | FOR_EACH_BB (bb) | |
fefa31b5 | 3480 | if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
78bde837 SB |
3481 | bb->aux = bb->next_bb; |
3482 | ||
3483 | cfg_layout_finalize (); | |
3484 | ||
3485 | return 0; | |
3486 | } | |
3487 | ||
27a4cd48 DM |
3488 | namespace { |
3489 | ||
3490 | const pass_data pass_data_into_cfg_layout_mode = | |
3491 | { | |
3492 | RTL_PASS, /* type */ | |
3493 | "into_cfglayout", /* name */ | |
3494 | OPTGROUP_NONE, /* optinfo_flags */ | |
3495 | false, /* has_gate */ | |
3496 | true, /* has_execute */ | |
3497 | TV_CFG, /* tv_id */ | |
3498 | 0, /* properties_required */ | |
3499 | PROP_cfglayout, /* properties_provided */ | |
3500 | 0, /* properties_destroyed */ | |
3501 | 0, /* todo_flags_start */ | |
3502 | 0, /* todo_flags_finish */ | |
78bde837 SB |
3503 | }; |
3504 | ||
27a4cd48 DM |
3505 | class pass_into_cfg_layout_mode : public rtl_opt_pass |
3506 | { | |
3507 | public: | |
c3284718 RS |
3508 | pass_into_cfg_layout_mode (gcc::context *ctxt) |
3509 | : rtl_opt_pass (pass_data_into_cfg_layout_mode, ctxt) | |
27a4cd48 DM |
3510 | {} |
3511 | ||
3512 | /* opt_pass methods: */ | |
3513 | unsigned int execute () { return into_cfg_layout_mode (); } | |
3514 | ||
3515 | }; // class pass_into_cfg_layout_mode | |
3516 | ||
3517 | } // anon namespace | |
3518 | ||
3519 | rtl_opt_pass * | |
3520 | make_pass_into_cfg_layout_mode (gcc::context *ctxt) | |
3521 | { | |
3522 | return new pass_into_cfg_layout_mode (ctxt); | |
3523 | } | |
3524 | ||
3525 | namespace { | |
3526 | ||
3527 | const pass_data pass_data_outof_cfg_layout_mode = | |
3528 | { | |
3529 | RTL_PASS, /* type */ | |
3530 | "outof_cfglayout", /* name */ | |
3531 | OPTGROUP_NONE, /* optinfo_flags */ | |
3532 | false, /* has_gate */ | |
3533 | true, /* has_execute */ | |
3534 | TV_CFG, /* tv_id */ | |
3535 | 0, /* properties_required */ | |
3536 | 0, /* properties_provided */ | |
3537 | PROP_cfglayout, /* properties_destroyed */ | |
3538 | 0, /* todo_flags_start */ | |
3539 | 0, /* todo_flags_finish */ | |
78bde837 | 3540 | }; |
27a4cd48 DM |
3541 | |
3542 | class pass_outof_cfg_layout_mode : public rtl_opt_pass | |
3543 | { | |
3544 | public: | |
c3284718 RS |
3545 | pass_outof_cfg_layout_mode (gcc::context *ctxt) |
3546 | : rtl_opt_pass (pass_data_outof_cfg_layout_mode, ctxt) | |
27a4cd48 DM |
3547 | {} |
3548 | ||
3549 | /* opt_pass methods: */ | |
3550 | unsigned int execute () { return outof_cfg_layout_mode (); } | |
3551 | ||
3552 | }; // class pass_outof_cfg_layout_mode | |
3553 | ||
3554 | } // anon namespace | |
3555 | ||
3556 | rtl_opt_pass * | |
3557 | make_pass_outof_cfg_layout_mode (gcc::context *ctxt) | |
3558 | { | |
3559 | return new pass_outof_cfg_layout_mode (ctxt); | |
3560 | } | |
78bde837 SB |
3561 | \f |
3562 | ||
3563 | /* Link the basic blocks in the correct order, compacting the basic | |
3564 | block queue while at it. If STAY_IN_CFGLAYOUT_MODE is false, this | |
3565 | function also clears the basic block header and footer fields. | |
3566 | ||
3567 | This function is usually called after a pass (e.g. tracer) finishes | |
3568 | some transformations while in cfglayout mode. The required sequence | |
3569 | of the basic blocks is in a linked list along the bb->aux field. | |
3570 | This functions re-links the basic block prev_bb and next_bb pointers | |
532aafad SB |
3571 | accordingly, and it compacts and renumbers the blocks. |
3572 | ||
3573 | FIXME: This currently works only for RTL, but the only RTL-specific | |
3574 | bits are the STAY_IN_CFGLAYOUT_MODE bits. The tracer pass was moved | |
3575 | to GIMPLE a long time ago, but it doesn't relink the basic block | |
3576 | chain. It could do that (to give better initial RTL) if this function | |
3577 | is made IR-agnostic (and moved to cfganal.c or cfg.c while at it). */ | |
78bde837 SB |
3578 | |
3579 | void | |
3580 | relink_block_chain (bool stay_in_cfglayout_mode) | |
3581 | { | |
3582 | basic_block bb, prev_bb; | |
3583 | int index; | |
3584 | ||
3585 | /* Maybe dump the re-ordered sequence. */ | |
3586 | if (dump_file) | |
3587 | { | |
3588 | fprintf (dump_file, "Reordered sequence:\n"); | |
fefa31b5 DM |
3589 | for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, index = |
3590 | NUM_FIXED_BLOCKS; | |
78bde837 SB |
3591 | bb; |
3592 | bb = (basic_block) bb->aux, index++) | |
3593 | { | |
3594 | fprintf (dump_file, " %i ", index); | |
3595 | if (get_bb_original (bb)) | |
3596 | fprintf (dump_file, "duplicate of %i ", | |
3597 | get_bb_original (bb)->index); | |
3598 | else if (forwarder_block_p (bb) | |
3599 | && !LABEL_P (BB_HEAD (bb))) | |
3600 | fprintf (dump_file, "compensation "); | |
3601 | else | |
3602 | fprintf (dump_file, "bb %i ", bb->index); | |
3603 | fprintf (dump_file, " [%i]\n", bb->frequency); | |
3604 | } | |
3605 | } | |
3606 | ||
3607 | /* Now reorder the blocks. */ | |
fefa31b5 DM |
3608 | prev_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun); |
3609 | bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; | |
78bde837 SB |
3610 | for (; bb; prev_bb = bb, bb = (basic_block) bb->aux) |
3611 | { | |
3612 | bb->prev_bb = prev_bb; | |
3613 | prev_bb->next_bb = bb; | |
3614 | } | |
fefa31b5 DM |
3615 | prev_bb->next_bb = EXIT_BLOCK_PTR_FOR_FN (cfun); |
3616 | EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb = prev_bb; | |
78bde837 SB |
3617 | |
3618 | /* Then, clean up the aux fields. */ | |
3619 | FOR_ALL_BB (bb) | |
3620 | { | |
3621 | bb->aux = NULL; | |
3622 | if (!stay_in_cfglayout_mode) | |
3623 | BB_HEADER (bb) = BB_FOOTER (bb) = NULL; | |
3624 | } | |
3625 | ||
3626 | /* Maybe reset the original copy tables, they are not valid anymore | |
3627 | when we renumber the basic blocks in compact_blocks. If we are | |
3628 | are going out of cfglayout mode, don't re-allocate the tables. */ | |
3629 | free_original_copy_tables (); | |
3630 | if (stay_in_cfglayout_mode) | |
3631 | initialize_original_copy_tables (); | |
3632 | ||
3633 | /* Finally, put basic_block_info in the new order. */ | |
3634 | compact_blocks (); | |
3635 | } | |
3636 | \f | |
3637 | ||
3638 | /* Given a reorder chain, rearrange the code to match. */ | |
3639 | ||
3640 | static void | |
3641 | fixup_reorder_chain (void) | |
3642 | { | |
3643 | basic_block bb; | |
3644 | rtx insn = NULL; | |
3645 | ||
3646 | if (cfg_layout_function_header) | |
3647 | { | |
3648 | set_first_insn (cfg_layout_function_header); | |
3649 | insn = cfg_layout_function_header; | |
3650 | while (NEXT_INSN (insn)) | |
3651 | insn = NEXT_INSN (insn); | |
3652 | } | |
3653 | ||
3654 | /* First do the bulk reordering -- rechain the blocks without regard to | |
3655 | the needed changes to jumps and labels. */ | |
3656 | ||
fefa31b5 DM |
3657 | for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb; bb = (basic_block) |
3658 | bb->aux) | |
78bde837 SB |
3659 | { |
3660 | if (BB_HEADER (bb)) | |
3661 | { | |
3662 | if (insn) | |
3663 | NEXT_INSN (insn) = BB_HEADER (bb); | |
3664 | else | |
3665 | set_first_insn (BB_HEADER (bb)); | |
3666 | PREV_INSN (BB_HEADER (bb)) = insn; | |
3667 | insn = BB_HEADER (bb); | |
3668 | while (NEXT_INSN (insn)) | |
3669 | insn = NEXT_INSN (insn); | |
3670 | } | |
3671 | if (insn) | |
3672 | NEXT_INSN (insn) = BB_HEAD (bb); | |
3673 | else | |
3674 | set_first_insn (BB_HEAD (bb)); | |
3675 | PREV_INSN (BB_HEAD (bb)) = insn; | |
3676 | insn = BB_END (bb); | |
3677 | if (BB_FOOTER (bb)) | |
3678 | { | |
3679 | NEXT_INSN (insn) = BB_FOOTER (bb); | |
3680 | PREV_INSN (BB_FOOTER (bb)) = insn; | |
3681 | while (NEXT_INSN (insn)) | |
3682 | insn = NEXT_INSN (insn); | |
3683 | } | |
3684 | } | |
3685 | ||
3686 | NEXT_INSN (insn) = cfg_layout_function_footer; | |
3687 | if (cfg_layout_function_footer) | |
3688 | PREV_INSN (cfg_layout_function_footer) = insn; | |
3689 | ||
3690 | while (NEXT_INSN (insn)) | |
3691 | insn = NEXT_INSN (insn); | |
3692 | ||
3693 | set_last_insn (insn); | |
3694 | #ifdef ENABLE_CHECKING | |
3695 | verify_insn_chain (); | |
3696 | #endif | |
3697 | ||
3698 | /* Now add jumps and labels as needed to match the blocks new | |
29f3fd5b | 3699 | outgoing edges. */ |
78bde837 | 3700 | |
29f3fd5b SB |
3701 | for (bb = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; bb ; bb = (basic_block) |
3702 | bb->aux) | |
78bde837 SB |
3703 | { |
3704 | edge e_fall, e_taken, e; | |
3705 | rtx bb_end_insn; | |
3706 | rtx ret_label = NULL_RTX; | |
3371a64f | 3707 | basic_block nb; |
78bde837 SB |
3708 | edge_iterator ei; |
3709 | ||
3710 | if (EDGE_COUNT (bb->succs) == 0) | |
3711 | continue; | |
3712 | ||
3713 | /* Find the old fallthru edge, and another non-EH edge for | |
3714 | a taken jump. */ | |
3715 | e_taken = e_fall = NULL; | |
3716 | ||
3717 | FOR_EACH_EDGE (e, ei, bb->succs) | |
3718 | if (e->flags & EDGE_FALLTHRU) | |
3719 | e_fall = e; | |
3720 | else if (! (e->flags & EDGE_EH)) | |
3721 | e_taken = e; | |
3722 | ||
3723 | bb_end_insn = BB_END (bb); | |
3724 | if (JUMP_P (bb_end_insn)) | |
3725 | { | |
3726 | ret_label = JUMP_LABEL (bb_end_insn); | |
3727 | if (any_condjump_p (bb_end_insn)) | |
3728 | { | |
3729 | /* This might happen if the conditional jump has side | |
3730 | effects and could therefore not be optimized away. | |
3731 | Make the basic block to end with a barrier in order | |
3732 | to prevent rtl_verify_flow_info from complaining. */ | |
3733 | if (!e_fall) | |
3734 | { | |
3735 | gcc_assert (!onlyjump_p (bb_end_insn) | |
3736 | || returnjump_p (bb_end_insn)); | |
39718607 | 3737 | emit_barrier_after (bb_end_insn); |
78bde837 SB |
3738 | continue; |
3739 | } | |
3740 | ||
3741 | /* If the old fallthru is still next, nothing to do. */ | |
3742 | if (bb->aux == e_fall->dest | |
fefa31b5 | 3743 | || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
78bde837 SB |
3744 | continue; |
3745 | ||
3746 | /* The degenerated case of conditional jump jumping to the next | |
3747 | instruction can happen for jumps with side effects. We need | |
3748 | to construct a forwarder block and this will be done just | |
3749 | fine by force_nonfallthru below. */ | |
3750 | if (!e_taken) | |
3751 | ; | |
3752 | ||
3753 | /* There is another special case: if *neither* block is next, | |
3754 | such as happens at the very end of a function, then we'll | |
3755 | need to add a new unconditional jump. Choose the taken | |
3756 | edge based on known or assumed probability. */ | |
3757 | else if (bb->aux != e_taken->dest) | |
3758 | { | |
3759 | rtx note = find_reg_note (bb_end_insn, REG_BR_PROB, 0); | |
3760 | ||
3761 | if (note | |
e5af9ddd | 3762 | && XINT (note, 0) < REG_BR_PROB_BASE / 2 |
78bde837 | 3763 | && invert_jump (bb_end_insn, |
fefa31b5 DM |
3764 | (e_fall->dest |
3765 | == EXIT_BLOCK_PTR_FOR_FN (cfun) | |
78bde837 SB |
3766 | ? NULL_RTX |
3767 | : label_for_bb (e_fall->dest)), 0)) | |
3768 | { | |
3769 | e_fall->flags &= ~EDGE_FALLTHRU; | |
3770 | gcc_checking_assert (could_fall_through | |
3771 | (e_taken->src, e_taken->dest)); | |
3772 | e_taken->flags |= EDGE_FALLTHRU; | |
3773 | update_br_prob_note (bb); | |
3774 | e = e_fall, e_fall = e_taken, e_taken = e; | |
3775 | } | |
3776 | } | |
3777 | ||
3778 | /* If the "jumping" edge is a crossing edge, and the fall | |
3779 | through edge is non-crossing, leave things as they are. */ | |
3780 | else if ((e_taken->flags & EDGE_CROSSING) | |
3781 | && !(e_fall->flags & EDGE_CROSSING)) | |
3782 | continue; | |
3783 | ||
3784 | /* Otherwise we can try to invert the jump. This will | |
3785 | basically never fail, however, keep up the pretense. */ | |
3786 | else if (invert_jump (bb_end_insn, | |
fefa31b5 DM |
3787 | (e_fall->dest |
3788 | == EXIT_BLOCK_PTR_FOR_FN (cfun) | |
78bde837 SB |
3789 | ? NULL_RTX |
3790 | : label_for_bb (e_fall->dest)), 0)) | |
3791 | { | |
3792 | e_fall->flags &= ~EDGE_FALLTHRU; | |
3793 | gcc_checking_assert (could_fall_through | |
3794 | (e_taken->src, e_taken->dest)); | |
3795 | e_taken->flags |= EDGE_FALLTHRU; | |
3796 | update_br_prob_note (bb); | |
3797 | if (LABEL_NUSES (ret_label) == 0 | |
3798 | && single_pred_p (e_taken->dest)) | |
3799 | delete_insn (ret_label); | |
3800 | continue; | |
3801 | } | |
3802 | } | |
3803 | else if (extract_asm_operands (PATTERN (bb_end_insn)) != NULL) | |
3804 | { | |
3805 | /* If the old fallthru is still next or if | |
3806 | asm goto doesn't have a fallthru (e.g. when followed by | |
3807 | __builtin_unreachable ()), nothing to do. */ | |
3808 | if (! e_fall | |
3809 | || bb->aux == e_fall->dest | |
fefa31b5 | 3810 | || e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
78bde837 SB |
3811 | continue; |
3812 | ||
3813 | /* Otherwise we'll have to use the fallthru fixup below. */ | |
3814 | } | |
3815 | else | |
3816 | { | |
3817 | /* Otherwise we have some return, switch or computed | |
3818 | jump. In the 99% case, there should not have been a | |
3819 | fallthru edge. */ | |
3820 | gcc_assert (returnjump_p (bb_end_insn) || !e_fall); | |
3821 | continue; | |
3822 | } | |
3823 | } | |
3824 | else | |
3825 | { | |
3826 | /* No fallthru implies a noreturn function with EH edges, or | |
3827 | something similarly bizarre. In any case, we don't need to | |
3828 | do anything. */ | |
3829 | if (! e_fall) | |
3830 | continue; | |
3831 | ||
3832 | /* If the fallthru block is still next, nothing to do. */ | |
3833 | if (bb->aux == e_fall->dest) | |
3834 | continue; | |
3835 | ||
3836 | /* A fallthru to exit block. */ | |
fefa31b5 | 3837 | if (e_fall->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
78bde837 SB |
3838 | continue; |
3839 | } | |
3840 | ||
3841 | /* We got here if we need to add a new jump insn. | |
3842 | Note force_nonfallthru can delete E_FALL and thus we have to | |
3843 | save E_FALL->src prior to the call to force_nonfallthru. */ | |
78bde837 SB |
3844 | nb = force_nonfallthru_and_redirect (e_fall, e_fall->dest, ret_label); |
3845 | if (nb) | |
3846 | { | |
3847 | nb->aux = bb->aux; | |
3848 | bb->aux = nb; | |
3849 | /* Don't process this new block. */ | |
3850 | bb = nb; | |
78bde837 SB |
3851 | } |
3852 | } | |
3853 | ||
3854 | relink_block_chain (/*stay_in_cfglayout_mode=*/false); | |
3855 | ||
29f3fd5b | 3856 | /* Annoying special case - jump around dead jumptables left in the code. */ |
78bde837 SB |
3857 | FOR_EACH_BB (bb) |
3858 | { | |
3859 | edge e = find_fallthru_edge (bb->succs); | |
3860 | ||
29f3fd5b SB |
3861 | if (e && !can_fallthru (e->src, e->dest)) |
3862 | force_nonfallthru (e); | |
78bde837 SB |
3863 | } |
3864 | ||
3865 | /* Ensure goto_locus from edges has some instructions with that locus | |
3866 | in RTL. */ | |
3867 | if (!optimize) | |
3868 | FOR_EACH_BB (bb) | |
3869 | { | |
3870 | edge e; | |
3871 | edge_iterator ei; | |
3872 | ||
3873 | FOR_EACH_EDGE (e, ei, bb->succs) | |
2f13f2de | 3874 | if (LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION |
5368224f | 3875 | && !(e->flags & EDGE_ABNORMAL)) |
78bde837 SB |
3876 | { |
3877 | edge e2; | |
3878 | edge_iterator ei2; | |
3879 | basic_block dest, nb; | |
3880 | rtx end; | |
3881 | ||
3882 | insn = BB_END (e->src); | |
3883 | end = PREV_INSN (BB_HEAD (e->src)); | |
3884 | while (insn != end | |
5368224f | 3885 | && (!NONDEBUG_INSN_P (insn) || !INSN_HAS_LOCATION (insn))) |
78bde837 SB |
3886 | insn = PREV_INSN (insn); |
3887 | if (insn != end | |
5368224f | 3888 | && INSN_LOCATION (insn) == e->goto_locus) |
78bde837 SB |
3889 | continue; |
3890 | if (simplejump_p (BB_END (e->src)) | |
5368224f | 3891 | && !INSN_HAS_LOCATION (BB_END (e->src))) |
78bde837 | 3892 | { |
5368224f | 3893 | INSN_LOCATION (BB_END (e->src)) = e->goto_locus; |
78bde837 SB |
3894 | continue; |
3895 | } | |
3896 | dest = e->dest; | |
fefa31b5 | 3897 | if (dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
78bde837 SB |
3898 | { |
3899 | /* Non-fallthru edges to the exit block cannot be split. */ | |
3900 | if (!(e->flags & EDGE_FALLTHRU)) | |
3901 | continue; | |
3902 | } | |
3903 | else | |
3904 | { | |
3905 | insn = BB_HEAD (dest); | |
3906 | end = NEXT_INSN (BB_END (dest)); | |
3907 | while (insn != end && !NONDEBUG_INSN_P (insn)) | |
3908 | insn = NEXT_INSN (insn); | |
5368224f DC |
3909 | if (insn != end && INSN_HAS_LOCATION (insn) |
3910 | && INSN_LOCATION (insn) == e->goto_locus) | |
78bde837 SB |
3911 | continue; |
3912 | } | |
3913 | nb = split_edge (e); | |
3914 | if (!INSN_P (BB_END (nb))) | |
3915 | BB_END (nb) = emit_insn_after_noloc (gen_nop (), BB_END (nb), | |
3916 | nb); | |
5368224f | 3917 | INSN_LOCATION (BB_END (nb)) = e->goto_locus; |
78bde837 SB |
3918 | |
3919 | /* If there are other incoming edges to the destination block | |
3920 | with the same goto locus, redirect them to the new block as | |
3921 | well, this can prevent other such blocks from being created | |
3922 | in subsequent iterations of the loop. */ | |
3923 | for (ei2 = ei_start (dest->preds); (e2 = ei_safe_edge (ei2)); ) | |
2f13f2de | 3924 | if (LOCATION_LOCUS (e2->goto_locus) != UNKNOWN_LOCATION |
78bde837 | 3925 | && !(e2->flags & (EDGE_ABNORMAL | EDGE_FALLTHRU)) |
5368224f | 3926 | && e->goto_locus == e2->goto_locus) |
78bde837 SB |
3927 | redirect_edge_and_branch (e2, nb); |
3928 | else | |
3929 | ei_next (&ei2); | |
3930 | } | |
3931 | } | |
3932 | } | |
3933 | \f | |
3934 | /* Perform sanity checks on the insn chain. | |
3935 | 1. Check that next/prev pointers are consistent in both the forward and | |
3936 | reverse direction. | |
3937 | 2. Count insns in chain, going both directions, and check if equal. | |
3938 | 3. Check that get_last_insn () returns the actual end of chain. */ | |
3939 | ||
3940 | DEBUG_FUNCTION void | |
3941 | verify_insn_chain (void) | |
3942 | { | |
3943 | rtx x, prevx, nextx; | |
3944 | int insn_cnt1, insn_cnt2; | |
3945 | ||
3946 | for (prevx = NULL, insn_cnt1 = 1, x = get_insns (); | |
3947 | x != 0; | |
3948 | prevx = x, insn_cnt1++, x = NEXT_INSN (x)) | |
3949 | gcc_assert (PREV_INSN (x) == prevx); | |
3950 | ||
3951 | gcc_assert (prevx == get_last_insn ()); | |
3952 | ||
3953 | for (nextx = NULL, insn_cnt2 = 1, x = get_last_insn (); | |
3954 | x != 0; | |
3955 | nextx = x, insn_cnt2++, x = PREV_INSN (x)) | |
3956 | gcc_assert (NEXT_INSN (x) == nextx); | |
3957 | ||
3958 | gcc_assert (insn_cnt1 == insn_cnt2); | |
3959 | } | |
3960 | \f | |
3961 | /* If we have assembler epilogues, the block falling through to exit must | |
3962 | be the last one in the reordered chain when we reach final. Ensure | |
3963 | that this condition is met. */ | |
3964 | static void | |
3965 | fixup_fallthru_exit_predecessor (void) | |
3966 | { | |
3967 | edge e; | |
3968 | basic_block bb = NULL; | |
3969 | ||
3970 | /* This transformation is not valid before reload, because we might | |
3971 | separate a call from the instruction that copies the return | |
3972 | value. */ | |
3973 | gcc_assert (reload_completed); | |
3974 | ||
fefa31b5 | 3975 | e = find_fallthru_edge (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); |
78bde837 SB |
3976 | if (e) |
3977 | bb = e->src; | |
3978 | ||
3979 | if (bb && bb->aux) | |
3980 | { | |
fefa31b5 | 3981 | basic_block c = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; |
78bde837 SB |
3982 | |
3983 | /* If the very first block is the one with the fall-through exit | |
3984 | edge, we have to split that block. */ | |
3985 | if (c == bb) | |
3986 | { | |
3987 | bb = split_block (bb, NULL)->dest; | |
3988 | bb->aux = c->aux; | |
3989 | c->aux = bb; | |
3990 | BB_FOOTER (bb) = BB_FOOTER (c); | |
3991 | BB_FOOTER (c) = NULL; | |
3992 | } | |
3993 | ||
3994 | while (c->aux != bb) | |
3995 | c = (basic_block) c->aux; | |
3996 | ||
3997 | c->aux = bb->aux; | |
3998 | while (c->aux) | |
3999 | c = (basic_block) c->aux; | |
4000 | ||
4001 | c->aux = bb; | |
4002 | bb->aux = NULL; | |
4003 | } | |
4004 | } | |
4005 | ||
4006 | /* In case there are more than one fallthru predecessors of exit, force that | |
4007 | there is only one. */ | |
4008 | ||
4009 | static void | |
4010 | force_one_exit_fallthru (void) | |
4011 | { | |
4012 | edge e, predecessor = NULL; | |
4013 | bool more = false; | |
4014 | edge_iterator ei; | |
4015 | basic_block forwarder, bb; | |
4016 | ||
fefa31b5 | 4017 | FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds) |
78bde837 SB |
4018 | if (e->flags & EDGE_FALLTHRU) |
4019 | { | |
4020 | if (predecessor == NULL) | |
4021 | predecessor = e; | |
4022 | else | |
4023 | { | |
4024 | more = true; | |
4025 | break; | |
4026 | } | |
4027 | } | |
4028 | ||
4029 | if (!more) | |
4030 | return; | |
4031 | ||
4032 | /* Exit has several fallthru predecessors. Create a forwarder block for | |
4033 | them. */ | |
4034 | forwarder = split_edge (predecessor); | |
fefa31b5 DM |
4035 | for (ei = ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds); |
4036 | (e = ei_safe_edge (ei)); ) | |
78bde837 SB |
4037 | { |
4038 | if (e->src == forwarder | |
4039 | || !(e->flags & EDGE_FALLTHRU)) | |
4040 | ei_next (&ei); | |
4041 | else | |
4042 | redirect_edge_and_branch_force (e, forwarder); | |
4043 | } | |
4044 | ||
4045 | /* Fix up the chain of blocks -- make FORWARDER immediately precede the | |
4046 | exit block. */ | |
4047 | FOR_EACH_BB (bb) | |
4048 | { | |
4049 | if (bb->aux == NULL && bb != forwarder) | |
4050 | { | |
4051 | bb->aux = forwarder; | |
4052 | break; | |
4053 | } | |
4054 | } | |
4055 | } | |
4056 | \f | |
4057 | /* Return true in case it is possible to duplicate the basic block BB. */ | |
4058 | ||
4059 | static bool | |
4060 | cfg_layout_can_duplicate_bb_p (const_basic_block bb) | |
4061 | { | |
4062 | /* Do not attempt to duplicate tablejumps, as we need to unshare | |
4063 | the dispatch table. This is difficult to do, as the instructions | |
4064 | computing jump destination may be hoisted outside the basic block. */ | |
4065 | if (tablejump_p (BB_END (bb), NULL, NULL)) | |
4066 | return false; | |
4067 | ||
4068 | /* Do not duplicate blocks containing insns that can't be copied. */ | |
4069 | if (targetm.cannot_copy_insn_p) | |
4070 | { | |
4071 | rtx insn = BB_HEAD (bb); | |
4072 | while (1) | |
4073 | { | |
4074 | if (INSN_P (insn) && targetm.cannot_copy_insn_p (insn)) | |
4075 | return false; | |
4076 | if (insn == BB_END (bb)) | |
4077 | break; | |
4078 | insn = NEXT_INSN (insn); | |
4079 | } | |
4080 | } | |
4081 | ||
4082 | return true; | |
4083 | } | |
4084 | ||
4085 | rtx | |
4086 | duplicate_insn_chain (rtx from, rtx to) | |
4087 | { | |
39718607 | 4088 | rtx insn, next, last, copy; |
78bde837 SB |
4089 | |
4090 | /* Avoid updating of boundaries of previous basic block. The | |
4091 | note will get removed from insn stream in fixup. */ | |
4092 | last = emit_note (NOTE_INSN_DELETED); | |
4093 | ||
4094 | /* Create copy at the end of INSN chain. The chain will | |
4095 | be reordered later. */ | |
4096 | for (insn = from; insn != NEXT_INSN (to); insn = NEXT_INSN (insn)) | |
4097 | { | |
4098 | switch (GET_CODE (insn)) | |
4099 | { | |
4100 | case DEBUG_INSN: | |
4101 | /* Don't duplicate label debug insns. */ | |
4102 | if (TREE_CODE (INSN_VAR_LOCATION_DECL (insn)) == LABEL_DECL) | |
4103 | break; | |
4104 | /* FALLTHRU */ | |
4105 | case INSN: | |
4106 | case CALL_INSN: | |
4107 | case JUMP_INSN: | |
78bde837 SB |
4108 | copy = emit_copy_of_insn_after (insn, get_last_insn ()); |
4109 | if (JUMP_P (insn) && JUMP_LABEL (insn) != NULL_RTX | |
4110 | && ANY_RETURN_P (JUMP_LABEL (insn))) | |
4111 | JUMP_LABEL (copy) = JUMP_LABEL (insn); | |
4112 | maybe_copy_prologue_epilogue_insn (insn, copy); | |
4113 | break; | |
4114 | ||
39718607 SB |
4115 | case JUMP_TABLE_DATA: |
4116 | /* Avoid copying of dispatch tables. We never duplicate | |
4117 | tablejumps, so this can hit only in case the table got | |
4118 | moved far from original jump. | |
4119 | Avoid copying following barrier as well if any | |
4120 | (and debug insns in between). */ | |
4121 | for (next = NEXT_INSN (insn); | |
4122 | next != NEXT_INSN (to); | |
4123 | next = NEXT_INSN (next)) | |
4124 | if (!DEBUG_INSN_P (next)) | |
4125 | break; | |
4126 | if (next != NEXT_INSN (to) && BARRIER_P (next)) | |
4127 | insn = next; | |
4128 | break; | |
4129 | ||
78bde837 SB |
4130 | case CODE_LABEL: |
4131 | break; | |
4132 | ||
4133 | case BARRIER: | |
4134 | emit_barrier (); | |
4135 | break; | |
4136 | ||
4137 | case NOTE: | |
4138 | switch (NOTE_KIND (insn)) | |
4139 | { | |
4140 | /* In case prologue is empty and function contain label | |
4141 | in first BB, we may want to copy the block. */ | |
4142 | case NOTE_INSN_PROLOGUE_END: | |
4143 | ||
4144 | case NOTE_INSN_DELETED: | |
4145 | case NOTE_INSN_DELETED_LABEL: | |
4146 | case NOTE_INSN_DELETED_DEBUG_LABEL: | |
4147 | /* No problem to strip these. */ | |
4148 | case NOTE_INSN_FUNCTION_BEG: | |
4149 | /* There is always just single entry to function. */ | |
4150 | case NOTE_INSN_BASIC_BLOCK: | |
3371a64f TJ |
4151 | /* We should only switch text sections once. */ |
4152 | case NOTE_INSN_SWITCH_TEXT_SECTIONS: | |
78bde837 SB |
4153 | break; |
4154 | ||
4155 | case NOTE_INSN_EPILOGUE_BEG: | |
78bde837 SB |
4156 | emit_note_copy (insn); |
4157 | break; | |
4158 | ||
4159 | default: | |
4160 | /* All other notes should have already been eliminated. */ | |
4161 | gcc_unreachable (); | |
4162 | } | |
4163 | break; | |
4164 | default: | |
4165 | gcc_unreachable (); | |
4166 | } | |
4167 | } | |
4168 | insn = NEXT_INSN (last); | |
4169 | delete_insn (last); | |
4170 | return insn; | |
4171 | } | |
4172 | ||
4173 | /* Create a duplicate of the basic block BB. */ | |
4174 | ||
4175 | static basic_block | |
4176 | cfg_layout_duplicate_bb (basic_block bb) | |
4177 | { | |
4178 | rtx insn; | |
4179 | basic_block new_bb; | |
4180 | ||
4181 | insn = duplicate_insn_chain (BB_HEAD (bb), BB_END (bb)); | |
4182 | new_bb = create_basic_block (insn, | |
4183 | insn ? get_last_insn () : NULL, | |
fefa31b5 | 4184 | EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb); |
78bde837 SB |
4185 | |
4186 | BB_COPY_PARTITION (new_bb, bb); | |
4187 | if (BB_HEADER (bb)) | |
4188 | { | |
4189 | insn = BB_HEADER (bb); | |
4190 | while (NEXT_INSN (insn)) | |
4191 | insn = NEXT_INSN (insn); | |
4192 | insn = duplicate_insn_chain (BB_HEADER (bb), insn); | |
4193 | if (insn) | |
4194 | BB_HEADER (new_bb) = unlink_insn_chain (insn, get_last_insn ()); | |
4195 | } | |
4196 | ||
4197 | if (BB_FOOTER (bb)) | |
4198 | { | |
4199 | insn = BB_FOOTER (bb); | |
4200 | while (NEXT_INSN (insn)) | |
4201 | insn = NEXT_INSN (insn); | |
4202 | insn = duplicate_insn_chain (BB_FOOTER (bb), insn); | |
4203 | if (insn) | |
4204 | BB_FOOTER (new_bb) = unlink_insn_chain (insn, get_last_insn ()); | |
4205 | } | |
4206 | ||
4207 | return new_bb; | |
4208 | } | |
4209 | ||
4210 | \f | |
4211 | /* Main entry point to this module - initialize the datastructures for | |
4212 | CFG layout changes. It keeps LOOPS up-to-date if not null. | |
4213 | ||
4214 | FLAGS is a set of additional flags to pass to cleanup_cfg(). */ | |
4215 | ||
4216 | void | |
4217 | cfg_layout_initialize (unsigned int flags) | |
4218 | { | |
4219 | rtx x; | |
4220 | basic_block bb; | |
4221 | ||
4ca19309 TJ |
4222 | /* Once bb reordering is complete, cfg layout mode should not be re-entered. |
4223 | Entering cfg layout mode will perform optimizations on the cfg that | |
4224 | could affect the bb layout negatively or even require fixups. An | |
4225 | example of the latter is if edge forwarding performed when optimizing | |
4226 | the cfg layout required moving a block from the hot to the cold section | |
4227 | under -freorder-blocks-and-partition. This would create an illegal | |
4228 | partitioning unless some manual fixup was performed. */ | |
4229 | gcc_assert (!crtl->bb_reorder_complete); | |
4230 | ||
78bde837 SB |
4231 | initialize_original_copy_tables (); |
4232 | ||
4233 | cfg_layout_rtl_register_cfg_hooks (); | |
4234 | ||
4235 | record_effective_endpoints (); | |
4236 | ||
4237 | /* Make sure that the targets of non local gotos are marked. */ | |
4238 | for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1)) | |
4239 | { | |
4240 | bb = BLOCK_FOR_INSN (XEXP (x, 0)); | |
4241 | bb->flags |= BB_NON_LOCAL_GOTO_TARGET; | |
4242 | } | |
4243 | ||
4244 | cleanup_cfg (CLEANUP_CFGLAYOUT | flags); | |
4245 | } | |
4246 | ||
4247 | /* Splits superblocks. */ | |
4248 | void | |
4249 | break_superblocks (void) | |
4250 | { | |
4251 | sbitmap superblocks; | |
4252 | bool need = false; | |
4253 | basic_block bb; | |
4254 | ||
4255 | superblocks = sbitmap_alloc (last_basic_block); | |
f61e445a | 4256 | bitmap_clear (superblocks); |
78bde837 SB |
4257 | |
4258 | FOR_EACH_BB (bb) | |
4259 | if (bb->flags & BB_SUPERBLOCK) | |
4260 | { | |
4261 | bb->flags &= ~BB_SUPERBLOCK; | |
d7c028c0 | 4262 | bitmap_set_bit (superblocks, bb->index); |
78bde837 SB |
4263 | need = true; |
4264 | } | |
4265 | ||
4266 | if (need) | |
4267 | { | |
4268 | rebuild_jump_labels (get_insns ()); | |
4269 | find_many_sub_basic_blocks (superblocks); | |
4270 | } | |
4271 | ||
4272 | free (superblocks); | |
4273 | } | |
4274 | ||
4275 | /* Finalize the changes: reorder insn list according to the sequence specified | |
29f3fd5b | 4276 | by aux pointers, enter compensation code, rebuild scope forest. */ |
78bde837 SB |
4277 | |
4278 | void | |
4279 | cfg_layout_finalize (void) | |
4280 | { | |
4281 | #ifdef ENABLE_CHECKING | |
4282 | verify_flow_info (); | |
4283 | #endif | |
4284 | force_one_exit_fallthru (); | |
4285 | rtl_register_cfg_hooks (); | |
4286 | if (reload_completed | |
4287 | #ifdef HAVE_epilogue | |
4288 | && !HAVE_epilogue | |
4289 | #endif | |
4290 | ) | |
4291 | fixup_fallthru_exit_predecessor (); | |
4292 | fixup_reorder_chain (); | |
4293 | ||
29f3fd5b SB |
4294 | rebuild_jump_labels (get_insns ()); |
4295 | delete_dead_jumptables (); | |
4296 | ||
78bde837 SB |
4297 | #ifdef ENABLE_CHECKING |
4298 | verify_insn_chain (); | |
4299 | verify_flow_info (); | |
4300 | #endif | |
4301 | } | |
4302 | ||
ba5e9aca | 4303 | |
9ee634e3 | 4304 | /* Same as split_block but update cfg_layout structures. */ |
f470c378 ZD |
4305 | |
4306 | static basic_block | |
d329e058 | 4307 | cfg_layout_split_block (basic_block bb, void *insnp) |
9ee634e3 | 4308 | { |
ae50c0cb | 4309 | rtx insn = (rtx) insnp; |
f470c378 | 4310 | basic_block new_bb = rtl_split_block (bb, insn); |
9ee634e3 | 4311 | |
bcc708fc MM |
4312 | BB_FOOTER (new_bb) = BB_FOOTER (bb); |
4313 | BB_FOOTER (bb) = NULL; | |
9ee634e3 | 4314 | |
f470c378 | 4315 | return new_bb; |
9ee634e3 JH |
4316 | } |
4317 | ||
9ee634e3 | 4318 | /* Redirect Edge to DEST. */ |
6de9cd9a | 4319 | static edge |
d329e058 | 4320 | cfg_layout_redirect_edge_and_branch (edge e, basic_block dest) |
9ee634e3 JH |
4321 | { |
4322 | basic_block src = e->src; | |
6de9cd9a | 4323 | edge ret; |
9ee634e3 | 4324 | |
bc35512f | 4325 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) |
6de9cd9a | 4326 | return NULL; |
bc35512f | 4327 | |
3348b696 | 4328 | if (e->dest == dest) |
6de9cd9a | 4329 | return e; |
bc35512f | 4330 | |
fefa31b5 | 4331 | if (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
6de9cd9a | 4332 | && (ret = try_redirect_by_replacing_jump (e, dest, true))) |
f345f21a | 4333 | { |
6fb5fa3c | 4334 | df_set_bb_dirty (src); |
6de9cd9a | 4335 | return ret; |
f345f21a | 4336 | } |
bc35512f | 4337 | |
fefa31b5 | 4338 | if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun) |
bc35512f JH |
4339 | && (e->flags & EDGE_FALLTHRU) && !(e->flags & EDGE_COMPLEX)) |
4340 | { | |
c263766c RH |
4341 | if (dump_file) |
4342 | fprintf (dump_file, "Redirecting entry edge from bb %i to %i\n", | |
bc35512f JH |
4343 | e->src->index, dest->index); |
4344 | ||
6fb5fa3c | 4345 | df_set_bb_dirty (e->src); |
bc35512f | 4346 | redirect_edge_succ (e, dest); |
6de9cd9a | 4347 | return e; |
bc35512f JH |
4348 | } |
4349 | ||
9ee634e3 JH |
4350 | /* Redirect_edge_and_branch may decide to turn branch into fallthru edge |
4351 | in the case the basic block appears to be in sequence. Avoid this | |
4352 | transformation. */ | |
4353 | ||
9ee634e3 JH |
4354 | if (e->flags & EDGE_FALLTHRU) |
4355 | { | |
4356 | /* Redirect any branch edges unified with the fallthru one. */ | |
4b4bf941 | 4357 | if (JUMP_P (BB_END (src)) |
432f982f JH |
4358 | && label_is_jump_target_p (BB_HEAD (e->dest), |
4359 | BB_END (src))) | |
9ee634e3 | 4360 | { |
341c100f | 4361 | edge redirected; |
c22cacf3 | 4362 | |
c263766c RH |
4363 | if (dump_file) |
4364 | fprintf (dump_file, "Fallthru edge unified with branch " | |
432f982f JH |
4365 | "%i->%i redirected to %i\n", |
4366 | e->src->index, e->dest->index, dest->index); | |
4367 | e->flags &= ~EDGE_FALLTHRU; | |
341c100f NS |
4368 | redirected = redirect_branch_edge (e, dest); |
4369 | gcc_assert (redirected); | |
0c617be4 JL |
4370 | redirected->flags |= EDGE_FALLTHRU; |
4371 | df_set_bb_dirty (redirected->src); | |
4372 | return redirected; | |
9ee634e3 JH |
4373 | } |
4374 | /* In case we are redirecting fallthru edge to the branch edge | |
c22cacf3 | 4375 | of conditional jump, remove it. */ |
628f6a4e | 4376 | if (EDGE_COUNT (src->succs) == 2) |
9ee634e3 | 4377 | { |
03101c6f KH |
4378 | /* Find the edge that is different from E. */ |
4379 | edge s = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e); | |
628f6a4e | 4380 | |
9ee634e3 | 4381 | if (s->dest == dest |
a813c111 SB |
4382 | && any_condjump_p (BB_END (src)) |
4383 | && onlyjump_p (BB_END (src))) | |
4384 | delete_insn (BB_END (src)); | |
9ee634e3 | 4385 | } |
c263766c | 4386 | if (dump_file) |
dc764d10 | 4387 | fprintf (dump_file, "Redirecting fallthru edge %i->%i to %i\n", |
bc35512f | 4388 | e->src->index, e->dest->index, dest->index); |
0c617be4 | 4389 | ret = redirect_edge_succ_nodup (e, dest); |
9ee634e3 JH |
4390 | } |
4391 | else | |
bc35512f | 4392 | ret = redirect_branch_edge (e, dest); |
9ee634e3 JH |
4393 | |
4394 | /* We don't want simplejumps in the insn stream during cfglayout. */ | |
341c100f | 4395 | gcc_assert (!simplejump_p (BB_END (src))); |
9ee634e3 | 4396 | |
6fb5fa3c | 4397 | df_set_bb_dirty (src); |
9ee634e3 JH |
4398 | return ret; |
4399 | } | |
4400 | ||
4401 | /* Simple wrapper as we always can redirect fallthru edges. */ | |
4402 | static basic_block | |
d329e058 | 4403 | cfg_layout_redirect_edge_and_branch_force (edge e, basic_block dest) |
9ee634e3 | 4404 | { |
341c100f NS |
4405 | edge redirected = cfg_layout_redirect_edge_and_branch (e, dest); |
4406 | ||
4407 | gcc_assert (redirected); | |
9ee634e3 JH |
4408 | return NULL; |
4409 | } | |
4410 | ||
f470c378 ZD |
4411 | /* Same as delete_basic_block but update cfg_layout structures. */ |
4412 | ||
9ee634e3 | 4413 | static void |
d329e058 | 4414 | cfg_layout_delete_block (basic_block bb) |
9ee634e3 | 4415 | { |
a813c111 | 4416 | rtx insn, next, prev = PREV_INSN (BB_HEAD (bb)), *to, remaints; |
9ee634e3 | 4417 | |
bcc708fc | 4418 | if (BB_HEADER (bb)) |
9ee634e3 | 4419 | { |
a813c111 | 4420 | next = BB_HEAD (bb); |
9ee634e3 | 4421 | if (prev) |
bcc708fc | 4422 | NEXT_INSN (prev) = BB_HEADER (bb); |
9ee634e3 | 4423 | else |
bcc708fc MM |
4424 | set_first_insn (BB_HEADER (bb)); |
4425 | PREV_INSN (BB_HEADER (bb)) = prev; | |
4426 | insn = BB_HEADER (bb); | |
9ee634e3 JH |
4427 | while (NEXT_INSN (insn)) |
4428 | insn = NEXT_INSN (insn); | |
4429 | NEXT_INSN (insn) = next; | |
4430 | PREV_INSN (next) = insn; | |
4431 | } | |
a813c111 | 4432 | next = NEXT_INSN (BB_END (bb)); |
bcc708fc | 4433 | if (BB_FOOTER (bb)) |
9ee634e3 | 4434 | { |
bcc708fc | 4435 | insn = BB_FOOTER (bb); |
bc35512f JH |
4436 | while (insn) |
4437 | { | |
4b4bf941 | 4438 | if (BARRIER_P (insn)) |
bc35512f JH |
4439 | { |
4440 | if (PREV_INSN (insn)) | |
4441 | NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn); | |
4442 | else | |
bcc708fc | 4443 | BB_FOOTER (bb) = NEXT_INSN (insn); |
bc35512f JH |
4444 | if (NEXT_INSN (insn)) |
4445 | PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn); | |
4446 | } | |
4b4bf941 | 4447 | if (LABEL_P (insn)) |
bc35512f JH |
4448 | break; |
4449 | insn = NEXT_INSN (insn); | |
4450 | } | |
bcc708fc | 4451 | if (BB_FOOTER (bb)) |
bc35512f | 4452 | { |
a813c111 | 4453 | insn = BB_END (bb); |
bcc708fc MM |
4454 | NEXT_INSN (insn) = BB_FOOTER (bb); |
4455 | PREV_INSN (BB_FOOTER (bb)) = insn; | |
bc35512f JH |
4456 | while (NEXT_INSN (insn)) |
4457 | insn = NEXT_INSN (insn); | |
4458 | NEXT_INSN (insn) = next; | |
4459 | if (next) | |
4460 | PREV_INSN (next) = insn; | |
4461 | else | |
4462 | set_last_insn (insn); | |
4463 | } | |
9ee634e3 | 4464 | } |
fefa31b5 | 4465 | if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
bcc708fc | 4466 | to = &BB_HEADER (bb->next_bb); |
9ee634e3 JH |
4467 | else |
4468 | to = &cfg_layout_function_footer; | |
997de8ed | 4469 | |
9ee634e3 JH |
4470 | rtl_delete_block (bb); |
4471 | ||
4472 | if (prev) | |
4473 | prev = NEXT_INSN (prev); | |
d329e058 | 4474 | else |
9ee634e3 JH |
4475 | prev = get_insns (); |
4476 | if (next) | |
4477 | next = PREV_INSN (next); | |
d329e058 | 4478 | else |
9ee634e3 JH |
4479 | next = get_last_insn (); |
4480 | ||
4481 | if (next && NEXT_INSN (next) != prev) | |
4482 | { | |
4483 | remaints = unlink_insn_chain (prev, next); | |
4484 | insn = remaints; | |
4485 | while (NEXT_INSN (insn)) | |
4486 | insn = NEXT_INSN (insn); | |
4487 | NEXT_INSN (insn) = *to; | |
4488 | if (*to) | |
4489 | PREV_INSN (*to) = insn; | |
4490 | *to = remaints; | |
4491 | } | |
4492 | } | |
4493 | ||
beb235f8 | 4494 | /* Return true when blocks A and B can be safely merged. */ |
b48d0358 | 4495 | |
bc35512f | 4496 | static bool |
b48d0358 | 4497 | cfg_layout_can_merge_blocks_p (basic_block a, basic_block b) |
bc35512f | 4498 | { |
750054a2 CT |
4499 | /* If we are partitioning hot/cold basic blocks, we don't want to |
4500 | mess up unconditional or indirect jumps that cross between hot | |
076c7ab8 ZW |
4501 | and cold sections. |
4502 | ||
8e8d5162 | 4503 | Basic block partitioning may result in some jumps that appear to |
c22cacf3 MS |
4504 | be optimizable (or blocks that appear to be mergeable), but which really |
4505 | must be left untouched (they are required to make it safely across | |
4506 | partition boundaries). See the comments at the top of | |
8e8d5162 CT |
4507 | bb-reorder.c:partition_hot_cold_basic_blocks for complete details. */ |
4508 | ||
87c8b4be | 4509 | if (BB_PARTITION (a) != BB_PARTITION (b)) |
076c7ab8 | 4510 | return false; |
750054a2 | 4511 | |
7d776ee2 RG |
4512 | /* Protect the loop latches. */ |
4513 | if (current_loops && b->loop_father->latch == b) | |
4514 | return false; | |
4515 | ||
894a84b5 BS |
4516 | /* If we would end up moving B's instructions, make sure it doesn't fall |
4517 | through into the exit block, since we cannot recover from a fallthrough | |
4518 | edge into the exit block occurring in the middle of a function. */ | |
4519 | if (NEXT_INSN (BB_END (a)) != BB_HEAD (b)) | |
4520 | { | |
4521 | edge e = find_fallthru_edge (b->succs); | |
fefa31b5 | 4522 | if (e && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
894a84b5 BS |
4523 | return false; |
4524 | } | |
4525 | ||
bc35512f | 4526 | /* There must be exactly one edge in between the blocks. */ |
c5cbcccf ZD |
4527 | return (single_succ_p (a) |
4528 | && single_succ (a) == b | |
4529 | && single_pred_p (b) == 1 | |
628f6a4e | 4530 | && a != b |
bc35512f | 4531 | /* Must be simple edge. */ |
c5cbcccf | 4532 | && !(single_succ_edge (a)->flags & EDGE_COMPLEX) |
fefa31b5 DM |
4533 | && a != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
4534 | && b != EXIT_BLOCK_PTR_FOR_FN (cfun) | |
49ea3702 SB |
4535 | /* If the jump insn has side effects, we can't kill the edge. |
4536 | When not optimizing, try_redirect_by_replacing_jump will | |
4537 | not allow us to redirect an edge by replacing a table jump. */ | |
4b4bf941 | 4538 | && (!JUMP_P (BB_END (a)) |
49ea3702 | 4539 | || ((!optimize || reload_completed) |
a813c111 | 4540 | ? simplejump_p (BB_END (a)) : onlyjump_p (BB_END (a))))); |
bc35512f JH |
4541 | } |
4542 | ||
41806d92 NS |
4543 | /* Merge block A and B. The blocks must be mergeable. */ |
4544 | ||
bc35512f JH |
4545 | static void |
4546 | cfg_layout_merge_blocks (basic_block a, basic_block b) | |
4547 | { | |
50a36e42 | 4548 | bool forwarder_p = (b->flags & BB_FORWARDER_BLOCK) != 0; |
f9df6f16 | 4549 | rtx insn; |
50a36e42 | 4550 | |
77a74ed7 | 4551 | gcc_checking_assert (cfg_layout_can_merge_blocks_p (a, b)); |
bc35512f | 4552 | |
6fb5fa3c | 4553 | if (dump_file) |
50a36e42 EB |
4554 | fprintf (dump_file, "Merging block %d into block %d...\n", b->index, |
4555 | a->index); | |
6fb5fa3c | 4556 | |
bc35512f | 4557 | /* If there was a CODE_LABEL beginning B, delete it. */ |
4b4bf941 | 4558 | if (LABEL_P (BB_HEAD (b))) |
2c97f8e4 | 4559 | { |
2c97f8e4 RH |
4560 | delete_insn (BB_HEAD (b)); |
4561 | } | |
bc35512f JH |
4562 | |
4563 | /* We should have fallthru edge in a, or we can do dummy redirection to get | |
4564 | it cleaned up. */ | |
4b4bf941 | 4565 | if (JUMP_P (BB_END (a))) |
628f6a4e | 4566 | try_redirect_by_replacing_jump (EDGE_SUCC (a, 0), b, true); |
341c100f | 4567 | gcc_assert (!JUMP_P (BB_END (a))); |
bc35512f | 4568 | |
9be94227 | 4569 | /* When not optimizing CFG and the edge is the only place in RTL which holds |
7241571e | 4570 | some unique locus, emit a nop with that locus in between. */ |
9be94227 EB |
4571 | if (!optimize) |
4572 | emit_nop_for_unique_locus_between (a, b); | |
7241571e | 4573 | |
4c8af858 SB |
4574 | /* Move things from b->footer after a->footer. */ |
4575 | if (BB_FOOTER (b)) | |
bc35512f | 4576 | { |
4c8af858 SB |
4577 | if (!BB_FOOTER (a)) |
4578 | BB_FOOTER (a) = BB_FOOTER (b); | |
4579 | else | |
4580 | { | |
4581 | rtx last = BB_FOOTER (a); | |
4582 | ||
4583 | while (NEXT_INSN (last)) | |
4584 | last = NEXT_INSN (last); | |
4585 | NEXT_INSN (last) = BB_FOOTER (b); | |
4586 | PREV_INSN (BB_FOOTER (b)) = last; | |
4587 | } | |
4588 | BB_FOOTER (b) = NULL; | |
4589 | } | |
4590 | ||
4591 | /* Move things from b->header before a->footer. | |
4592 | Note that this may include dead tablejump data, but we don't clean | |
4593 | those up until we go out of cfglayout mode. */ | |
4594 | if (BB_HEADER (b)) | |
4595 | { | |
4596 | if (! BB_FOOTER (a)) | |
4597 | BB_FOOTER (a) = BB_HEADER (b); | |
4598 | else | |
4599 | { | |
4600 | rtx last = BB_HEADER (b); | |
4601 | ||
4602 | while (NEXT_INSN (last)) | |
4603 | last = NEXT_INSN (last); | |
4604 | NEXT_INSN (last) = BB_FOOTER (a); | |
4605 | PREV_INSN (BB_FOOTER (a)) = last; | |
4606 | BB_FOOTER (a) = BB_HEADER (b); | |
4607 | } | |
bcc708fc | 4608 | BB_HEADER (b) = NULL; |
bc35512f JH |
4609 | } |
4610 | ||
4611 | /* In the case basic blocks are not adjacent, move them around. */ | |
a813c111 | 4612 | if (NEXT_INSN (BB_END (a)) != BB_HEAD (b)) |
bc35512f | 4613 | { |
f9df6f16 | 4614 | insn = unlink_insn_chain (BB_HEAD (b), BB_END (b)); |
6c3d0e31 | 4615 | |
f9df6f16 | 4616 | emit_insn_after_noloc (insn, BB_END (a), a); |
bc35512f JH |
4617 | } |
4618 | /* Otherwise just re-associate the instructions. */ | |
4619 | else | |
4620 | { | |
a813c111 | 4621 | insn = BB_HEAD (b); |
a813c111 | 4622 | BB_END (a) = BB_END (b); |
bc35512f JH |
4623 | } |
4624 | ||
f9df6f16 JJ |
4625 | /* emit_insn_after_noloc doesn't call df_insn_change_bb. |
4626 | We need to explicitly call. */ | |
4627 | update_bb_for_insn_chain (insn, BB_END (b), a); | |
4628 | ||
4629 | /* Skip possible DELETED_LABEL insn. */ | |
4630 | if (!NOTE_INSN_BASIC_BLOCK_P (insn)) | |
4631 | insn = NEXT_INSN (insn); | |
4632 | gcc_assert (NOTE_INSN_BASIC_BLOCK_P (insn)); | |
4c8af858 | 4633 | BB_HEAD (b) = BB_END (b) = NULL; |
f9df6f16 JJ |
4634 | delete_insn (insn); |
4635 | ||
6fb5fa3c DB |
4636 | df_bb_delete (b->index); |
4637 | ||
50a36e42 | 4638 | /* If B was a forwarder block, propagate the locus on the edge. */ |
2f13f2de | 4639 | if (forwarder_p |
fbc68f2a | 4640 | && LOCATION_LOCUS (EDGE_SUCC (b, 0)->goto_locus) == UNKNOWN_LOCATION) |
50a36e42 EB |
4641 | EDGE_SUCC (b, 0)->goto_locus = EDGE_SUCC (a, 0)->goto_locus; |
4642 | ||
c263766c | 4643 | if (dump_file) |
50a36e42 | 4644 | fprintf (dump_file, "Merged blocks %d and %d.\n", a->index, b->index); |
bc35512f JH |
4645 | } |
4646 | ||
4647 | /* Split edge E. */ | |
f470c378 | 4648 | |
bc35512f JH |
4649 | static basic_block |
4650 | cfg_layout_split_edge (edge e) | |
4651 | { | |
bc35512f | 4652 | basic_block new_bb = |
fefa31b5 | 4653 | create_basic_block (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
a813c111 | 4654 | ? NEXT_INSN (BB_END (e->src)) : get_insns (), |
bc35512f JH |
4655 | NULL_RTX, e->src); |
4656 | ||
fefa31b5 | 4657 | if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
3e7eb734 JJ |
4658 | BB_COPY_PARTITION (new_bb, e->src); |
4659 | else | |
4660 | BB_COPY_PARTITION (new_bb, e->dest); | |
a9b2ee88 | 4661 | make_edge (new_bb, e->dest, EDGE_FALLTHRU); |
bc35512f JH |
4662 | redirect_edge_and_branch_force (e, new_bb); |
4663 | ||
4664 | return new_bb; | |
4665 | } | |
4666 | ||
f470c378 ZD |
4667 | /* Do postprocessing after making a forwarder block joined by edge FALLTHRU. */ |
4668 | ||
4669 | static void | |
4670 | rtl_make_forwarder_block (edge fallthru ATTRIBUTE_UNUSED) | |
4671 | { | |
4672 | } | |
4673 | ||
df92c640 SB |
4674 | /* Return true if BB contains only labels or non-executable |
4675 | instructions. */ | |
4676 | ||
4677 | static bool | |
4678 | rtl_block_empty_p (basic_block bb) | |
4679 | { | |
4680 | rtx insn; | |
4681 | ||
fefa31b5 DM |
4682 | if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun) |
4683 | || bb == EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
df92c640 SB |
4684 | return true; |
4685 | ||
4686 | FOR_BB_INSNS (bb, insn) | |
4687 | if (NONDEBUG_INSN_P (insn) && !any_uncondjump_p (insn)) | |
4688 | return false; | |
4689 | ||
4690 | return true; | |
4691 | } | |
4692 | ||
4693 | /* Split a basic block if it ends with a conditional branch and if | |
4694 | the other part of the block is not empty. */ | |
4695 | ||
4696 | static basic_block | |
4697 | rtl_split_block_before_cond_jump (basic_block bb) | |
4698 | { | |
4699 | rtx insn; | |
4700 | rtx split_point = NULL; | |
4701 | rtx last = NULL; | |
4702 | bool found_code = false; | |
4703 | ||
4704 | FOR_BB_INSNS (bb, insn) | |
4705 | { | |
4706 | if (any_condjump_p (insn)) | |
4707 | split_point = last; | |
4708 | else if (NONDEBUG_INSN_P (insn)) | |
4709 | found_code = true; | |
4710 | last = insn; | |
4711 | } | |
4712 | ||
4713 | /* Did not find everything. */ | |
4714 | if (found_code && split_point) | |
4715 | return split_block (bb, split_point)->dest; | |
4716 | else | |
4717 | return NULL; | |
4718 | } | |
4719 | ||
6de9cd9a DN |
4720 | /* Return 1 if BB ends with a call, possibly followed by some |
4721 | instructions that must stay with the call, 0 otherwise. */ | |
4722 | ||
4723 | static bool | |
b48d0358 | 4724 | rtl_block_ends_with_call_p (basic_block bb) |
6de9cd9a DN |
4725 | { |
4726 | rtx insn = BB_END (bb); | |
4727 | ||
4b4bf941 | 4728 | while (!CALL_P (insn) |
6de9cd9a | 4729 | && insn != BB_HEAD (bb) |
92ddef69 | 4730 | && (keep_with_call_p (insn) |
b5b8b0ac AO |
4731 | || NOTE_P (insn) |
4732 | || DEBUG_INSN_P (insn))) | |
6de9cd9a | 4733 | insn = PREV_INSN (insn); |
4b4bf941 | 4734 | return (CALL_P (insn)); |
6de9cd9a DN |
4735 | } |
4736 | ||
4737 | /* Return 1 if BB ends with a conditional branch, 0 otherwise. */ | |
4738 | ||
4739 | static bool | |
9678086d | 4740 | rtl_block_ends_with_condjump_p (const_basic_block bb) |
6de9cd9a DN |
4741 | { |
4742 | return any_condjump_p (BB_END (bb)); | |
4743 | } | |
4744 | ||
4745 | /* Return true if we need to add fake edge to exit. | |
4746 | Helper function for rtl_flow_call_edges_add. */ | |
4747 | ||
4748 | static bool | |
9678086d | 4749 | need_fake_edge_p (const_rtx insn) |
6de9cd9a DN |
4750 | { |
4751 | if (!INSN_P (insn)) | |
4752 | return false; | |
4753 | ||
4b4bf941 | 4754 | if ((CALL_P (insn) |
6de9cd9a DN |
4755 | && !SIBLING_CALL_P (insn) |
4756 | && !find_reg_note (insn, REG_NORETURN, NULL) | |
becfd6e5 | 4757 | && !(RTL_CONST_OR_PURE_CALL_P (insn)))) |
6de9cd9a DN |
4758 | return true; |
4759 | ||
4760 | return ((GET_CODE (PATTERN (insn)) == ASM_OPERANDS | |
4761 | && MEM_VOLATILE_P (PATTERN (insn))) | |
4762 | || (GET_CODE (PATTERN (insn)) == PARALLEL | |
4763 | && asm_noperands (insn) != -1 | |
4764 | && MEM_VOLATILE_P (XVECEXP (PATTERN (insn), 0, 0))) | |
4765 | || GET_CODE (PATTERN (insn)) == ASM_INPUT); | |
4766 | } | |
4767 | ||
4768 | /* Add fake edges to the function exit for any non constant and non noreturn | |
4769 | calls, volatile inline assembly in the bitmap of blocks specified by | |
4770 | BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks | |
4771 | that were split. | |
4772 | ||
4773 | The goal is to expose cases in which entering a basic block does not imply | |
4774 | that all subsequent instructions must be executed. */ | |
4775 | ||
4776 | static int | |
4777 | rtl_flow_call_edges_add (sbitmap blocks) | |
4778 | { | |
4779 | int i; | |
4780 | int blocks_split = 0; | |
4781 | int last_bb = last_basic_block; | |
4782 | bool check_last_block = false; | |
4783 | ||
0cae8d31 | 4784 | if (n_basic_blocks_for_fn (cfun) == NUM_FIXED_BLOCKS) |
6de9cd9a DN |
4785 | return 0; |
4786 | ||
4787 | if (! blocks) | |
4788 | check_last_block = true; | |
4789 | else | |
fefa31b5 DM |
4790 | check_last_block = bitmap_bit_p (blocks, |
4791 | EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb->index); | |
6de9cd9a DN |
4792 | |
4793 | /* In the last basic block, before epilogue generation, there will be | |
4794 | a fallthru edge to EXIT. Special care is required if the last insn | |
4795 | of the last basic block is a call because make_edge folds duplicate | |
4796 | edges, which would result in the fallthru edge also being marked | |
4797 | fake, which would result in the fallthru edge being removed by | |
4798 | remove_fake_edges, which would result in an invalid CFG. | |
4799 | ||
4800 | Moreover, we can't elide the outgoing fake edge, since the block | |
4801 | profiler needs to take this into account in order to solve the minimal | |
4802 | spanning tree in the case that the call doesn't return. | |
4803 | ||
4804 | Handle this by adding a dummy instruction in a new last basic block. */ | |
4805 | if (check_last_block) | |
4806 | { | |
fefa31b5 | 4807 | basic_block bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb; |
6de9cd9a DN |
4808 | rtx insn = BB_END (bb); |
4809 | ||
4810 | /* Back up past insns that must be kept in the same block as a call. */ | |
4811 | while (insn != BB_HEAD (bb) | |
4812 | && keep_with_call_p (insn)) | |
4813 | insn = PREV_INSN (insn); | |
4814 | ||
4815 | if (need_fake_edge_p (insn)) | |
4816 | { | |
4817 | edge e; | |
4818 | ||
fefa31b5 | 4819 | e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)); |
9ff3d2de JL |
4820 | if (e) |
4821 | { | |
c41c1387 | 4822 | insert_insn_on_edge (gen_use (const0_rtx), e); |
9ff3d2de JL |
4823 | commit_edge_insertions (); |
4824 | } | |
6de9cd9a DN |
4825 | } |
4826 | } | |
4827 | ||
4828 | /* Now add fake edges to the function exit for any non constant | |
4829 | calls since there is no way that we can determine if they will | |
4830 | return or not... */ | |
4831 | ||
24bd1a0b | 4832 | for (i = NUM_FIXED_BLOCKS; i < last_bb; i++) |
6de9cd9a | 4833 | { |
06e28de2 | 4834 | basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i); |
6de9cd9a DN |
4835 | rtx insn; |
4836 | rtx prev_insn; | |
4837 | ||
4838 | if (!bb) | |
4839 | continue; | |
4840 | ||
d7c028c0 | 4841 | if (blocks && !bitmap_bit_p (blocks, i)) |
6de9cd9a DN |
4842 | continue; |
4843 | ||
4844 | for (insn = BB_END (bb); ; insn = prev_insn) | |
4845 | { | |
4846 | prev_insn = PREV_INSN (insn); | |
4847 | if (need_fake_edge_p (insn)) | |
4848 | { | |
4849 | edge e; | |
4850 | rtx split_at_insn = insn; | |
4851 | ||
4852 | /* Don't split the block between a call and an insn that should | |
c22cacf3 | 4853 | remain in the same block as the call. */ |
4b4bf941 | 4854 | if (CALL_P (insn)) |
6de9cd9a DN |
4855 | while (split_at_insn != BB_END (bb) |
4856 | && keep_with_call_p (NEXT_INSN (split_at_insn))) | |
4857 | split_at_insn = NEXT_INSN (split_at_insn); | |
4858 | ||
4859 | /* The handling above of the final block before the epilogue | |
c22cacf3 | 4860 | should be enough to verify that there is no edge to the exit |
6de9cd9a DN |
4861 | block in CFG already. Calling make_edge in such case would |
4862 | cause us to mark that edge as fake and remove it later. */ | |
4863 | ||
4864 | #ifdef ENABLE_CHECKING | |
4865 | if (split_at_insn == BB_END (bb)) | |
628f6a4e | 4866 | { |
fefa31b5 | 4867 | e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)); |
9ff3d2de | 4868 | gcc_assert (e == NULL); |
628f6a4e | 4869 | } |
6de9cd9a DN |
4870 | #endif |
4871 | ||
4872 | /* Note that the following may create a new basic block | |
4873 | and renumber the existing basic blocks. */ | |
4874 | if (split_at_insn != BB_END (bb)) | |
4875 | { | |
4876 | e = split_block (bb, split_at_insn); | |
4877 | if (e) | |
4878 | blocks_split++; | |
4879 | } | |
4880 | ||
fefa31b5 | 4881 | make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE); |
6de9cd9a DN |
4882 | } |
4883 | ||
4884 | if (insn == BB_HEAD (bb)) | |
4885 | break; | |
4886 | } | |
4887 | } | |
4888 | ||
4889 | if (blocks_split) | |
4890 | verify_flow_info (); | |
4891 | ||
4892 | return blocks_split; | |
4893 | } | |
4894 | ||
1cb7dfc3 | 4895 | /* Add COMP_RTX as a condition at end of COND_BB. FIRST_HEAD is |
315682fb | 4896 | the conditional branch target, SECOND_HEAD should be the fall-thru |
1cb7dfc3 MH |
4897 | there is no need to handle this here the loop versioning code handles |
4898 | this. the reason for SECON_HEAD is that it is needed for condition | |
4899 | in trees, and this should be of the same type since it is a hook. */ | |
4900 | static void | |
4901 | rtl_lv_add_condition_to_bb (basic_block first_head , | |
c22cacf3 MS |
4902 | basic_block second_head ATTRIBUTE_UNUSED, |
4903 | basic_block cond_bb, void *comp_rtx) | |
1cb7dfc3 MH |
4904 | { |
4905 | rtx label, seq, jump; | |
4906 | rtx op0 = XEXP ((rtx)comp_rtx, 0); | |
4907 | rtx op1 = XEXP ((rtx)comp_rtx, 1); | |
4908 | enum rtx_code comp = GET_CODE ((rtx)comp_rtx); | |
4909 | enum machine_mode mode; | |
4910 | ||
4911 | ||
4912 | label = block_label (first_head); | |
4913 | mode = GET_MODE (op0); | |
4914 | if (mode == VOIDmode) | |
4915 | mode = GET_MODE (op1); | |
4916 | ||
4917 | start_sequence (); | |
4918 | op0 = force_operand (op0, NULL_RTX); | |
4919 | op1 = force_operand (op1, NULL_RTX); | |
4920 | do_compare_rtx_and_jump (op0, op1, comp, 0, | |
40e90eac | 4921 | mode, NULL_RTX, NULL_RTX, label, -1); |
1cb7dfc3 MH |
4922 | jump = get_last_insn (); |
4923 | JUMP_LABEL (jump) = label; | |
4924 | LABEL_NUSES (label)++; | |
4925 | seq = get_insns (); | |
4926 | end_sequence (); | |
4927 | ||
4928 | /* Add the new cond , in the new head. */ | |
c3284718 | 4929 | emit_insn_after (seq, BB_END (cond_bb)); |
1cb7dfc3 MH |
4930 | } |
4931 | ||
4932 | ||
4933 | /* Given a block B with unconditional branch at its end, get the | |
4934 | store the return the branch edge and the fall-thru edge in | |
4935 | BRANCH_EDGE and FALLTHRU_EDGE respectively. */ | |
4936 | static void | |
4937 | rtl_extract_cond_bb_edges (basic_block b, edge *branch_edge, | |
4938 | edge *fallthru_edge) | |
4939 | { | |
4940 | edge e = EDGE_SUCC (b, 0); | |
4941 | ||
4942 | if (e->flags & EDGE_FALLTHRU) | |
4943 | { | |
4944 | *fallthru_edge = e; | |
4945 | *branch_edge = EDGE_SUCC (b, 1); | |
4946 | } | |
4947 | else | |
4948 | { | |
4949 | *branch_edge = e; | |
4950 | *fallthru_edge = EDGE_SUCC (b, 1); | |
4951 | } | |
4952 | } | |
4953 | ||
5e2d947c JH |
4954 | void |
4955 | init_rtl_bb_info (basic_block bb) | |
4956 | { | |
bcc708fc MM |
4957 | gcc_assert (!bb->il.x.rtl); |
4958 | bb->il.x.head_ = NULL; | |
4959 | bb->il.x.rtl = ggc_alloc_cleared_rtl_bb_info (); | |
5e2d947c JH |
4960 | } |
4961 | ||
14fa2cc0 ZD |
4962 | /* Returns true if it is possible to remove edge E by redirecting |
4963 | it to the destination of the other edge from E->src. */ | |
4964 | ||
4965 | static bool | |
9678086d | 4966 | rtl_can_remove_branch_p (const_edge e) |
14fa2cc0 | 4967 | { |
9678086d KG |
4968 | const_basic_block src = e->src; |
4969 | const_basic_block target = EDGE_SUCC (src, EDGE_SUCC (src, 0) == e)->dest; | |
4970 | const_rtx insn = BB_END (src), set; | |
14fa2cc0 ZD |
4971 | |
4972 | /* The conditions are taken from try_redirect_by_replacing_jump. */ | |
fefa31b5 | 4973 | if (target == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
14fa2cc0 ZD |
4974 | return false; |
4975 | ||
4976 | if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)) | |
4977 | return false; | |
4978 | ||
3371a64f | 4979 | if (BB_PARTITION (src) != BB_PARTITION (target)) |
14fa2cc0 ZD |
4980 | return false; |
4981 | ||
4982 | if (!onlyjump_p (insn) | |
4983 | || tablejump_p (insn, NULL, NULL)) | |
4984 | return false; | |
4985 | ||
4986 | set = single_set (insn); | |
4987 | if (!set || side_effects_p (set)) | |
4988 | return false; | |
4989 | ||
4990 | return true; | |
4991 | } | |
4992 | ||
ffe14686 AM |
4993 | static basic_block |
4994 | rtl_duplicate_bb (basic_block bb) | |
4995 | { | |
4996 | bb = cfg_layout_duplicate_bb (bb); | |
4997 | bb->aux = NULL; | |
4998 | return bb; | |
4999 | } | |
5000 | ||
aa4723d7 SB |
5001 | /* Do book-keeping of basic block BB for the profile consistency checker. |
5002 | If AFTER_PASS is 0, do pre-pass accounting, or if AFTER_PASS is 1 | |
5003 | then do post-pass accounting. Store the counting in RECORD. */ | |
5004 | static void | |
5005 | rtl_account_profile_record (basic_block bb, int after_pass, | |
5006 | struct profile_record *record) | |
5007 | { | |
5008 | rtx insn; | |
5009 | FOR_BB_INSNS (bb, insn) | |
5010 | if (INSN_P (insn)) | |
5011 | { | |
5012 | record->size[after_pass] | |
5013 | += insn_rtx_cost (PATTERN (insn), false); | |
5014 | if (profile_status == PROFILE_READ) | |
5015 | record->time[after_pass] | |
5016 | += insn_rtx_cost (PATTERN (insn), true) * bb->count; | |
5017 | else if (profile_status == PROFILE_GUESSED) | |
5018 | record->time[after_pass] | |
5019 | += insn_rtx_cost (PATTERN (insn), true) * bb->frequency; | |
5020 | } | |
5021 | } | |
5022 | ||
9ee634e3 JH |
5023 | /* Implementation of CFG manipulation for linearized RTL. */ |
5024 | struct cfg_hooks rtl_cfg_hooks = { | |
f470c378 | 5025 | "rtl", |
9ee634e3 | 5026 | rtl_verify_flow_info, |
10e9fecc | 5027 | rtl_dump_bb, |
2c895bd1 | 5028 | rtl_dump_bb_for_graph, |
bc35512f | 5029 | rtl_create_basic_block, |
9ee634e3 JH |
5030 | rtl_redirect_edge_and_branch, |
5031 | rtl_redirect_edge_and_branch_force, | |
14fa2cc0 | 5032 | rtl_can_remove_branch_p, |
9ee634e3 JH |
5033 | rtl_delete_block, |
5034 | rtl_split_block, | |
f470c378 | 5035 | rtl_move_block_after, |
bc35512f JH |
5036 | rtl_can_merge_blocks, /* can_merge_blocks_p */ |
5037 | rtl_merge_blocks, | |
6de9cd9a DN |
5038 | rtl_predict_edge, |
5039 | rtl_predicted_by_p, | |
ffe14686 AM |
5040 | cfg_layout_can_duplicate_bb_p, |
5041 | rtl_duplicate_bb, | |
f470c378 ZD |
5042 | rtl_split_edge, |
5043 | rtl_make_forwarder_block, | |
6de9cd9a | 5044 | rtl_tidy_fallthru_edge, |
cf103ca4 | 5045 | rtl_force_nonfallthru, |
6de9cd9a DN |
5046 | rtl_block_ends_with_call_p, |
5047 | rtl_block_ends_with_condjump_p, | |
d9d4706f KH |
5048 | rtl_flow_call_edges_add, |
5049 | NULL, /* execute_on_growing_pred */ | |
1cb7dfc3 MH |
5050 | NULL, /* execute_on_shrinking_pred */ |
5051 | NULL, /* duplicate loop for trees */ | |
5052 | NULL, /* lv_add_condition_to_bb */ | |
5053 | NULL, /* lv_adjust_loop_header_phi*/ | |
5054 | NULL, /* extract_cond_bb_edges */ | |
df92c640 SB |
5055 | NULL, /* flush_pending_stmts */ |
5056 | rtl_block_empty_p, /* block_empty_p */ | |
5057 | rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */ | |
aa4723d7 | 5058 | rtl_account_profile_record, |
9ee634e3 JH |
5059 | }; |
5060 | ||
5061 | /* Implementation of CFG manipulation for cfg layout RTL, where | |
5062 | basic block connected via fallthru edges does not have to be adjacent. | |
5063 | This representation will hopefully become the default one in future | |
5064 | version of the compiler. */ | |
6de9cd9a | 5065 | |
9ee634e3 | 5066 | struct cfg_hooks cfg_layout_rtl_cfg_hooks = { |
f470c378 | 5067 | "cfglayout mode", |
bc35512f | 5068 | rtl_verify_flow_info_1, |
10e9fecc | 5069 | rtl_dump_bb, |
2c895bd1 | 5070 | rtl_dump_bb_for_graph, |
bc35512f | 5071 | cfg_layout_create_basic_block, |
9ee634e3 JH |
5072 | cfg_layout_redirect_edge_and_branch, |
5073 | cfg_layout_redirect_edge_and_branch_force, | |
14fa2cc0 | 5074 | rtl_can_remove_branch_p, |
9ee634e3 JH |
5075 | cfg_layout_delete_block, |
5076 | cfg_layout_split_block, | |
f470c378 | 5077 | rtl_move_block_after, |
bc35512f JH |
5078 | cfg_layout_can_merge_blocks_p, |
5079 | cfg_layout_merge_blocks, | |
6de9cd9a DN |
5080 | rtl_predict_edge, |
5081 | rtl_predicted_by_p, | |
5082 | cfg_layout_can_duplicate_bb_p, | |
5083 | cfg_layout_duplicate_bb, | |
f470c378 ZD |
5084 | cfg_layout_split_edge, |
5085 | rtl_make_forwarder_block, | |
cf103ca4 EB |
5086 | NULL, /* tidy_fallthru_edge */ |
5087 | rtl_force_nonfallthru, | |
6de9cd9a DN |
5088 | rtl_block_ends_with_call_p, |
5089 | rtl_block_ends_with_condjump_p, | |
d9d4706f KH |
5090 | rtl_flow_call_edges_add, |
5091 | NULL, /* execute_on_growing_pred */ | |
1cb7dfc3 MH |
5092 | NULL, /* execute_on_shrinking_pred */ |
5093 | duplicate_loop_to_header_edge, /* duplicate loop for trees */ | |
5094 | rtl_lv_add_condition_to_bb, /* lv_add_condition_to_bb */ | |
5095 | NULL, /* lv_adjust_loop_header_phi*/ | |
5096 | rtl_extract_cond_bb_edges, /* extract_cond_bb_edges */ | |
df92c640 SB |
5097 | NULL, /* flush_pending_stmts */ |
5098 | rtl_block_empty_p, /* block_empty_p */ | |
5099 | rtl_split_block_before_cond_jump, /* split_block_before_cond_jump */ | |
aa4723d7 | 5100 | rtl_account_profile_record, |
9ee634e3 | 5101 | }; |
78bde837 SB |
5102 | |
5103 | #include "gt-cfgrtl.h" |