]>
Commit | Line | Data |
---|---|---|
402209ff | 1 | /* Control flow graph building code for GNU compiler. |
5624e564 | 2 | Copyright (C) 1987-2015 Free Software Foundation, Inc. |
402209ff 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 |
402209ff 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/>. */ | |
402209ff | 19 | |
402209ff JH |
20 | \f |
21 | #include "config.h" | |
22 | #include "system.h" | |
4977bab6 | 23 | #include "coretypes.h" |
c7131fb2 | 24 | #include "backend.h" |
40e23961 | 25 | #include "alias.h" |
402209ff JH |
26 | #include "tree.h" |
27 | #include "rtl.h" | |
60393bbc AM |
28 | #include "cfgrtl.h" |
29 | #include "cfganal.h" | |
30 | #include "cfgbuild.h" | |
60393bbc AM |
31 | #include "regs.h" |
32 | #include "flags.h" | |
402209ff | 33 | #include "except.h" |
36566b39 PK |
34 | #include "insn-config.h" |
35 | #include "expmed.h" | |
36 | #include "dojump.h" | |
37 | #include "explow.h" | |
38 | #include "calls.h" | |
39 | #include "emit-rtl.h" | |
40 | #include "varasm.h" | |
41 | #include "stmt.h" | |
1d65f45c | 42 | #include "expr.h" |
718f9c0f | 43 | #include "diagnostic-core.h" |
402209ff | 44 | #include "timevar.h" |
4891442b | 45 | |
2df6cea5 | 46 | static void make_edges (basic_block, basic_block, int); |
a6ee1a15 | 47 | static void make_label_edge (sbitmap, basic_block, rtx, int); |
d329e058 AJ |
48 | static void find_bb_boundaries (basic_block); |
49 | static void compute_outgoing_frequencies (basic_block); | |
4891442b | 50 | \f |
9cd56be1 JH |
51 | /* Return true if insn is something that should be contained inside basic |
52 | block. */ | |
53 | ||
8f62128d | 54 | bool |
eefc72ed | 55 | inside_basic_block_p (const rtx_insn *insn) |
9cd56be1 JH |
56 | { |
57 | switch (GET_CODE (insn)) | |
58 | { | |
59 | case CODE_LABEL: | |
60 | /* Avoid creating of basic block for jumptables. */ | |
4891442b | 61 | return (NEXT_INSN (insn) == 0 |
6388c738 | 62 | || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn))); |
9cd56be1 JH |
63 | |
64 | case JUMP_INSN: | |
9cd56be1 JH |
65 | case CALL_INSN: |
66 | case INSN: | |
b5b8b0ac | 67 | case DEBUG_INSN: |
9cd56be1 JH |
68 | return true; |
69 | ||
39718607 | 70 | case JUMP_TABLE_DATA: |
9cd56be1 JH |
71 | case BARRIER: |
72 | case NOTE: | |
73 | return false; | |
74 | ||
75 | default: | |
341c100f | 76 | gcc_unreachable (); |
9cd56be1 JH |
77 | } |
78 | } | |
79 | ||
4891442b RK |
80 | /* Return true if INSN may cause control flow transfer, so it should be last in |
81 | the basic block. */ | |
9cd56be1 | 82 | |
4a69cf79 | 83 | bool |
43f9bab0 | 84 | control_flow_insn_p (const rtx_insn *insn) |
9cd56be1 | 85 | { |
9cd56be1 JH |
86 | switch (GET_CODE (insn)) |
87 | { | |
f87c27b4 KH |
88 | case NOTE: |
89 | case CODE_LABEL: | |
b5b8b0ac | 90 | case DEBUG_INSN: |
f87c27b4 KH |
91 | return false; |
92 | ||
93 | case JUMP_INSN: | |
39718607 | 94 | return true; |
f87c27b4 KH |
95 | |
96 | case CALL_INSN: | |
baf8706c JH |
97 | /* Noreturn and sibling call instructions terminate the basic blocks |
98 | (but only if they happen unconditionally). */ | |
99 | if ((SIBLING_CALL_P (insn) | |
100 | || find_reg_note (insn, REG_NORETURN, 0)) | |
101 | && GET_CODE (PATTERN (insn)) != COND_EXEC) | |
102 | return true; | |
1d65f45c | 103 | |
f87c27b4 | 104 | /* Call insn may return to the nonlocal goto handler. */ |
1d65f45c RH |
105 | if (can_nonlocal_goto (insn)) |
106 | return true; | |
107 | break; | |
f87c27b4 KH |
108 | |
109 | case INSN: | |
d47a8b83 EB |
110 | /* Treat trap instructions like noreturn calls (same provision). */ |
111 | if (GET_CODE (PATTERN (insn)) == TRAP_IF | |
112 | && XEXP (PATTERN (insn), 0) == const1_rtx) | |
113 | return true; | |
8f4f502f | 114 | if (!cfun->can_throw_non_call_exceptions) |
1d65f45c RH |
115 | return false; |
116 | break; | |
f87c27b4 | 117 | |
39718607 | 118 | case JUMP_TABLE_DATA: |
f87c27b4 | 119 | case BARRIER: |
39718607 | 120 | /* It is nonsense to reach this when looking for the |
c22cacf3 MS |
121 | end of basic block, but before dead code is eliminated |
122 | this may happen. */ | |
f87c27b4 KH |
123 | return false; |
124 | ||
125 | default: | |
341c100f | 126 | gcc_unreachable (); |
9cd56be1 | 127 | } |
1d65f45c RH |
128 | |
129 | return can_throw_internal (insn); | |
9cd56be1 | 130 | } |
402209ff | 131 | |
402209ff JH |
132 | \f |
133 | /* Create an edge between two basic blocks. FLAGS are auxiliary information | |
134 | about the edge that is accumulated between calls. */ | |
135 | ||
136 | /* Create an edge from a basic block to a label. */ | |
137 | ||
138 | static void | |
a6ee1a15 | 139 | make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags) |
402209ff | 140 | { |
341c100f | 141 | gcc_assert (LABEL_P (label)); |
402209ff JH |
142 | |
143 | /* If the label was never emitted, this insn is junk, but avoid a | |
144 | crash trying to refer to BLOCK_FOR_INSN (label). This can happen | |
145 | as a result of a syntax error and a diagnostic has already been | |
146 | printed. */ | |
147 | ||
148 | if (INSN_UID (label) == 0) | |
149 | return; | |
150 | ||
7ded4467 | 151 | cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags); |
402209ff JH |
152 | } |
153 | ||
154 | /* Create the edges generated by INSN in REGION. */ | |
155 | ||
12c3874e | 156 | void |
a6ee1a15 | 157 | rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn) |
402209ff | 158 | { |
1d65f45c | 159 | eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn); |
402209ff | 160 | |
1d65f45c RH |
161 | if (lp) |
162 | { | |
e67d1102 | 163 | rtx_insn *label = lp->landing_pad; |
402209ff | 164 | |
1d65f45c RH |
165 | /* During initial rtl generation, use the post_landing_pad. */ |
166 | if (label == NULL) | |
167 | { | |
168 | gcc_assert (lp->post_landing_pad); | |
169 | label = label_rtx (lp->post_landing_pad); | |
170 | } | |
402209ff | 171 | |
1d65f45c RH |
172 | make_label_edge (edge_cache, src, label, |
173 | EDGE_ABNORMAL | EDGE_EH | |
174 | | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0)); | |
175 | } | |
402209ff | 176 | } |
4891442b | 177 | |
55b18c19 KH |
178 | /* States of basic block as seen by find_many_sub_basic_blocks. */ |
179 | enum state { | |
180 | /* Basic blocks created via split_block belong to this state. | |
181 | make_edges will examine these basic blocks to see if we need to | |
182 | create edges going out of them. */ | |
183 | BLOCK_NEW = 0, | |
184 | ||
185 | /* Basic blocks that do not need examining belong to this state. | |
186 | These blocks will be left intact. In particular, make_edges will | |
187 | not create edges going out of these basic blocks. */ | |
188 | BLOCK_ORIGINAL, | |
189 | ||
190 | /* Basic blocks that may need splitting (due to a label appearing in | |
191 | the middle, etc) belong to this state. After splitting them, | |
0fa2e4df | 192 | make_edges will create edges going out of them as needed. */ |
55b18c19 KH |
193 | BLOCK_TO_SPLIT |
194 | }; | |
5e91f7a3 KH |
195 | |
196 | #define STATE(BB) (enum state) ((size_t) (BB)->aux) | |
197 | #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE)) | |
198 | ||
199 | /* Used internally by purge_dead_tablejump_edges, ORed into state. */ | |
200 | #define BLOCK_USED_BY_TABLEJUMP 32 | |
201 | #define FULL_STATE(BB) ((size_t) (BB)->aux) | |
202 | ||
55b18c19 KH |
203 | /* Identify the edges going out of basic blocks between MIN and MAX, |
204 | inclusive, that have their states set to BLOCK_NEW or | |
205 | BLOCK_TO_SPLIT. | |
402209ff | 206 | |
55b18c19 KH |
207 | UPDATE_P should be nonzero if we are updating CFG and zero if we |
208 | are building CFG from scratch. */ | |
402209ff JH |
209 | |
210 | static void | |
2df6cea5 | 211 | make_edges (basic_block min, basic_block max, int update_p) |
402209ff | 212 | { |
e0082a72 | 213 | basic_block bb; |
a6ee1a15 | 214 | sbitmap edge_cache = NULL; |
402209ff | 215 | |
402209ff JH |
216 | /* Heavy use of computed goto in machine-generated code can lead to |
217 | nearly fully-connected CFGs. In that case we spend a significant | |
218 | amount of time searching the edge lists for duplicates. */ | |
cb91fab0 | 219 | if (forced_labels || cfun->cfg->max_jumptable_ents > 100) |
8b1c6fd7 | 220 | edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun)); |
402209ff | 221 | |
e0082a72 ZD |
222 | /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block |
223 | is always the entry. */ | |
fefa31b5 DM |
224 | if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) |
225 | make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU); | |
402209ff | 226 | |
e0082a72 | 227 | FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb) |
402209ff | 228 | { |
3bbd5815 | 229 | rtx_insn *insn; |
402209ff | 230 | enum rtx_code code; |
623a66fa | 231 | edge e; |
a6ee1a15 | 232 | edge_iterator ei; |
402209ff | 233 | |
5e91f7a3 KH |
234 | if (STATE (bb) == BLOCK_ORIGINAL) |
235 | continue; | |
236 | ||
a6ee1a15 KH |
237 | /* If we have an edge cache, cache edges going out of BB. */ |
238 | if (edge_cache) | |
239 | { | |
f61e445a | 240 | bitmap_clear (edge_cache); |
a6ee1a15 KH |
241 | if (update_p) |
242 | { | |
243 | FOR_EACH_EDGE (e, ei, bb->succs) | |
fefa31b5 | 244 | if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
d7c028c0 | 245 | bitmap_set_bit (edge_cache, e->dest->index); |
a6ee1a15 KH |
246 | } |
247 | } | |
248 | ||
4b4bf941 | 249 | if (LABEL_P (BB_HEAD (bb)) |
a813c111 | 250 | && LABEL_ALT_ENTRY_P (BB_HEAD (bb))) |
fefa31b5 | 251 | cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0); |
402209ff JH |
252 | |
253 | /* Examine the last instruction of the block, and discover the | |
254 | ways we can leave the block. */ | |
255 | ||
a813c111 | 256 | insn = BB_END (bb); |
402209ff JH |
257 | code = GET_CODE (insn); |
258 | ||
259 | /* A branch. */ | |
260 | if (code == JUMP_INSN) | |
261 | { | |
262 | rtx tmp; | |
8942ee0f | 263 | rtx_jump_table_data *table; |
402209ff | 264 | |
402209ff JH |
265 | /* Recognize a non-local goto as a branch outside the |
266 | current function. */ | |
1d65f45c | 267 | if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX)) |
402209ff JH |
268 | ; |
269 | ||
e1233a7d | 270 | /* Recognize a tablejump and do the right thing. */ |
8942ee0f | 271 | else if (tablejump_p (insn, NULL, &table)) |
402209ff | 272 | { |
95c43227 | 273 | rtvec vec = table->get_labels (); |
402209ff JH |
274 | int j; |
275 | ||
402209ff JH |
276 | for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j) |
277 | make_label_edge (edge_cache, bb, | |
278 | XEXP (RTVEC_ELT (vec, j), 0), 0); | |
279 | ||
280 | /* Some targets (eg, ARM) emit a conditional jump that also | |
281 | contains the out-of-range target. Scan for these and | |
282 | add an edge if necessary. */ | |
283 | if ((tmp = single_set (insn)) != NULL | |
284 | && SET_DEST (tmp) == pc_rtx | |
285 | && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE | |
286 | && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF) | |
287 | make_label_edge (edge_cache, bb, | |
a827d9b1 | 288 | LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0); |
402209ff JH |
289 | } |
290 | ||
291 | /* If this is a computed jump, then mark it as reaching | |
2df6cea5 | 292 | everything on the forced_labels list. */ |
402209ff JH |
293 | else if (computed_jump_p (insn)) |
294 | { | |
e8c038ca DM |
295 | for (rtx_insn_list *x = forced_labels; x; x = x->next ()) |
296 | make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL); | |
402209ff JH |
297 | } |
298 | ||
299 | /* Returns create an exit out. */ | |
300 | else if (returnjump_p (insn)) | |
fefa31b5 | 301 | cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0); |
402209ff | 302 | |
1c384bf1 RH |
303 | /* Recognize asm goto and do the right thing. */ |
304 | else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL) | |
305 | { | |
306 | int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp); | |
307 | for (i = 0; i < n; ++i) | |
308 | make_label_edge (edge_cache, bb, | |
309 | XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0); | |
310 | } | |
311 | ||
402209ff JH |
312 | /* Otherwise, we have a plain conditional or unconditional jump. */ |
313 | else | |
314 | { | |
341c100f | 315 | gcc_assert (JUMP_LABEL (insn)); |
402209ff JH |
316 | make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0); |
317 | } | |
318 | } | |
319 | ||
4891442b RK |
320 | /* If this is a sibling call insn, then this is in effect a combined call |
321 | and return, and so we need an edge to the exit block. No need to | |
322 | worry about EH edges, since we wouldn't have created the sibling call | |
323 | in the first place. */ | |
402209ff | 324 | if (code == CALL_INSN && SIBLING_CALL_P (insn)) |
fefa31b5 | 325 | cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), |
792bb204 | 326 | EDGE_SIBCALL | EDGE_ABNORMAL); |
402209ff JH |
327 | |
328 | /* If this is a CALL_INSN, then mark it as reaching the active EH | |
329 | handler for this CALL_INSN. If we're handling non-call | |
330 | exceptions then any insn can reach any of the active handlers. | |
402209ff | 331 | Also mark the CALL_INSN as reaching any nonlocal goto handler. */ |
8f4f502f | 332 | else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions) |
402209ff JH |
333 | { |
334 | /* Add any appropriate EH edges. */ | |
6de9cd9a | 335 | rtl_make_eh_edge (edge_cache, bb, insn); |
402209ff | 336 | |
0a35513e | 337 | if (code == CALL_INSN) |
402209ff | 338 | { |
1d65f45c | 339 | if (can_nonlocal_goto (insn)) |
0a35513e AH |
340 | { |
341 | /* ??? This could be made smarter: in some cases it's | |
342 | possible to tell that certain calls will not do a | |
343 | nonlocal goto. For example, if the nested functions | |
344 | that do the nonlocal gotos do not have their addresses | |
345 | taken, then only calls to those functions or to other | |
346 | nested functions that use them could possibly do | |
347 | nonlocal gotos. */ | |
b5241a5a | 348 | for (rtx_insn_list *x = nonlocal_goto_handler_labels; |
2382940b DM |
349 | x; |
350 | x = x->next ()) | |
b5241a5a | 351 | make_label_edge (edge_cache, bb, x->insn (), |
0a35513e AH |
352 | EDGE_ABNORMAL | EDGE_ABNORMAL_CALL); |
353 | } | |
354 | ||
355 | if (flag_tm) | |
356 | { | |
357 | rtx note; | |
358 | for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) | |
359 | if (REG_NOTE_KIND (note) == REG_TM) | |
360 | make_label_edge (edge_cache, bb, XEXP (note, 0), | |
361 | EDGE_ABNORMAL | EDGE_ABNORMAL_CALL); | |
362 | } | |
402209ff JH |
363 | } |
364 | } | |
365 | ||
366 | /* Find out if we can drop through to the next block. */ | |
26f74aa3 | 367 | insn = NEXT_INSN (insn); |
fefa31b5 | 368 | e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)); |
9ff3d2de JL |
369 | if (e && e->flags & EDGE_FALLTHRU) |
370 | insn = NULL; | |
371 | ||
26f74aa3 | 372 | while (insn |
4b4bf941 | 373 | && NOTE_P (insn) |
a38e7aa5 | 374 | && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK) |
26f74aa3 JH |
375 | insn = NEXT_INSN (insn); |
376 | ||
6be85b25 | 377 | if (!insn) |
fefa31b5 DM |
378 | cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), |
379 | EDGE_FALLTHRU); | |
380 | else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
402209ff | 381 | { |
6be85b25 | 382 | if (insn == BB_HEAD (bb->next_bb)) |
f6366fc7 | 383 | cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU); |
402209ff JH |
384 | } |
385 | } | |
386 | ||
387 | if (edge_cache) | |
f61e445a | 388 | sbitmap_free (edge_cache); |
402209ff JH |
389 | } |
390 | \f | |
0210ae14 JJ |
391 | static void |
392 | mark_tablejump_edge (rtx label) | |
393 | { | |
394 | basic_block bb; | |
395 | ||
396 | gcc_assert (LABEL_P (label)); | |
397 | /* See comment in make_label_edge. */ | |
398 | if (INSN_UID (label) == 0) | |
399 | return; | |
400 | bb = BLOCK_FOR_INSN (label); | |
401 | SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP); | |
402 | } | |
403 | ||
404 | static void | |
95c43227 | 405 | purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table) |
0210ae14 | 406 | { |
3bbd5815 DM |
407 | rtx_insn *insn = BB_END (bb); |
408 | rtx tmp; | |
0210ae14 JJ |
409 | rtvec vec; |
410 | int j; | |
411 | edge_iterator ei; | |
412 | edge e; | |
413 | ||
95c43227 | 414 | vec = table->get_labels (); |
0210ae14 JJ |
415 | |
416 | for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j) | |
417 | mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0)); | |
418 | ||
419 | /* Some targets (eg, ARM) emit a conditional jump that also | |
420 | contains the out-of-range target. Scan for these and | |
421 | add an edge if necessary. */ | |
422 | if ((tmp = single_set (insn)) != NULL | |
423 | && SET_DEST (tmp) == pc_rtx | |
424 | && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE | |
425 | && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF) | |
a827d9b1 | 426 | mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2))); |
0210ae14 JJ |
427 | |
428 | for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) | |
429 | { | |
430 | if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP) | |
c22cacf3 MS |
431 | SET_STATE (e->dest, FULL_STATE (e->dest) |
432 | & ~(size_t) BLOCK_USED_BY_TABLEJUMP); | |
0210ae14 | 433 | else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH))) |
c22cacf3 MS |
434 | { |
435 | remove_edge (e); | |
436 | continue; | |
437 | } | |
0210ae14 JJ |
438 | ei_next (&ei); |
439 | } | |
440 | } | |
441 | ||
b932f770 JH |
442 | /* Scan basic block BB for possible BB boundaries inside the block |
443 | and create new basic blocks in the progress. */ | |
444 | ||
445 | static void | |
d329e058 | 446 | find_bb_boundaries (basic_block bb) |
402209ff | 447 | { |
0210ae14 | 448 | basic_block orig_bb = bb; |
3bbd5815 DM |
449 | rtx_insn *insn = BB_HEAD (bb); |
450 | rtx_insn *end = BB_END (bb), *x; | |
8942ee0f | 451 | rtx_jump_table_data *table; |
3bbd5815 | 452 | rtx_insn *flow_transfer_insn = NULL; |
f068df3f | 453 | edge fallthru = NULL; |
402209ff | 454 | |
a813c111 | 455 | if (insn == BB_END (bb)) |
402209ff JH |
456 | return; |
457 | ||
4b4bf941 | 458 | if (LABEL_P (insn)) |
402209ff JH |
459 | insn = NEXT_INSN (insn); |
460 | ||
461 | /* Scan insn chain and try to find new basic block boundaries. */ | |
462 | while (1) | |
463 | { | |
464 | enum rtx_code code = GET_CODE (insn); | |
f068df3f | 465 | |
1d65f45c RH |
466 | /* In case we've previously seen an insn that effects a control |
467 | flow transfer, split the block. */ | |
468 | if ((flow_transfer_insn || code == CODE_LABEL) | |
469 | && inside_basic_block_p (insn)) | |
402209ff | 470 | { |
f068df3f RH |
471 | fallthru = split_block (bb, PREV_INSN (insn)); |
472 | if (flow_transfer_insn) | |
a7b87f73 | 473 | { |
1130d5e3 | 474 | BB_END (bb) = flow_transfer_insn; |
a7b87f73 ZD |
475 | |
476 | /* Clean up the bb field for the insns between the blocks. */ | |
477 | for (x = NEXT_INSN (flow_transfer_insn); | |
478 | x != BB_HEAD (fallthru->dest); | |
479 | x = NEXT_INSN (x)) | |
480 | if (!BARRIER_P (x)) | |
481 | set_block_for_insn (x, NULL); | |
482 | } | |
4891442b | 483 | |
f068df3f RH |
484 | bb = fallthru->dest; |
485 | remove_edge (fallthru); | |
3bbd5815 | 486 | flow_transfer_insn = NULL; |
1d65f45c | 487 | if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn)) |
fefa31b5 | 488 | make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0); |
402209ff | 489 | } |
79876307 RH |
490 | else if (code == BARRIER) |
491 | { | |
492 | /* __builtin_unreachable () may cause a barrier to be emitted in | |
493 | the middle of a BB. We need to split it in the same manner as | |
494 | if the barrier were preceded by a control_flow_insn_p insn. */ | |
495 | if (!flow_transfer_insn) | |
496 | flow_transfer_insn = prev_nonnote_insn_bb (insn); | |
497 | } | |
79876307 | 498 | |
27b4689f RH |
499 | if (control_flow_insn_p (insn)) |
500 | flow_transfer_insn = insn; | |
402209ff JH |
501 | if (insn == end) |
502 | break; | |
503 | insn = NEXT_INSN (insn); | |
504 | } | |
505 | ||
506 | /* In case expander replaced normal insn by sequence terminating by | |
507 | return and barrier, or possibly other sequence not behaving like | |
508 | ordinary jump, we need to take care and move basic block boundary. */ | |
f068df3f | 509 | if (flow_transfer_insn) |
a7b87f73 | 510 | { |
1130d5e3 | 511 | BB_END (bb) = flow_transfer_insn; |
a7b87f73 ZD |
512 | |
513 | /* Clean up the bb field for the insns that do not belong to BB. */ | |
514 | x = flow_transfer_insn; | |
515 | while (x != end) | |
516 | { | |
517 | x = NEXT_INSN (x); | |
518 | if (!BARRIER_P (x)) | |
519 | set_block_for_insn (x, NULL); | |
520 | } | |
521 | } | |
402209ff JH |
522 | |
523 | /* We've possibly replaced the conditional jump by conditional jump | |
524 | followed by cleanup at fallthru edge, so the outgoing edges may | |
525 | be dead. */ | |
526 | purge_dead_edges (bb); | |
0210ae14 JJ |
527 | |
528 | /* purge_dead_edges doesn't handle tablejump's, but if we have split the | |
529 | basic block, we might need to kill some edges. */ | |
530 | if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table)) | |
531 | purge_dead_tablejump_edges (bb, table); | |
b932f770 JH |
532 | } |
533 | ||
534 | /* Assume that frequency of basic block B is known. Compute frequencies | |
535 | and probabilities of outgoing edges. */ | |
536 | ||
537 | static void | |
d329e058 | 538 | compute_outgoing_frequencies (basic_block b) |
b932f770 JH |
539 | { |
540 | edge e, f; | |
628f6a4e | 541 | edge_iterator ei; |
4891442b | 542 | |
628f6a4e | 543 | if (EDGE_COUNT (b->succs) == 2) |
b932f770 | 544 | { |
a813c111 | 545 | rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL); |
b932f770 JH |
546 | int probability; |
547 | ||
87022a6b JH |
548 | if (note) |
549 | { | |
e5af9ddd | 550 | probability = XINT (note, 0); |
87022a6b JH |
551 | e = BRANCH_EDGE (b); |
552 | e->probability = probability; | |
8ddb5a29 | 553 | e->count = apply_probability (b->count, probability); |
87022a6b JH |
554 | f = FALLTHRU_EDGE (b); |
555 | f->probability = REG_BR_PROB_BASE - probability; | |
556 | f->count = b->count - e->count; | |
557 | return; | |
558 | } | |
a4da41e1 ER |
559 | else |
560 | { | |
561 | guess_outgoing_edge_probabilities (b); | |
562 | } | |
b932f770 | 563 | } |
a4da41e1 | 564 | else if (single_succ_p (b)) |
b932f770 | 565 | { |
c5cbcccf | 566 | e = single_succ_edge (b); |
b932f770 JH |
567 | e->probability = REG_BR_PROB_BASE; |
568 | e->count = b->count; | |
87022a6b | 569 | return; |
b932f770 | 570 | } |
a4da41e1 ER |
571 | else |
572 | { | |
573 | /* We rely on BBs with more than two successors to have sane probabilities | |
574 | and do not guess them here. For BBs terminated by switch statements | |
575 | expanded to jump-table jump, we have done the right thing during | |
576 | expansion. For EH edges, we still guess the probabilities here. */ | |
577 | bool complex_edge = false; | |
578 | FOR_EACH_EDGE (e, ei, b->succs) | |
579 | if (e->flags & EDGE_COMPLEX) | |
580 | { | |
581 | complex_edge = true; | |
582 | break; | |
583 | } | |
584 | if (complex_edge) | |
585 | guess_outgoing_edge_probabilities (b); | |
586 | } | |
587 | ||
87022a6b | 588 | if (b->count) |
628f6a4e | 589 | FOR_EACH_EDGE (e, ei, b->succs) |
8ddb5a29 | 590 | e->count = apply_probability (b->count, e->probability); |
b932f770 JH |
591 | } |
592 | ||
55b18c19 KH |
593 | /* Assume that some pass has inserted labels or control flow |
594 | instructions within a basic block. Split basic blocks as needed | |
595 | and create edges. */ | |
b932f770 JH |
596 | |
597 | void | |
d329e058 | 598 | find_many_sub_basic_blocks (sbitmap blocks) |
b932f770 | 599 | { |
e0082a72 | 600 | basic_block bb, min, max; |
b932f770 | 601 | |
11cd3bed | 602 | FOR_EACH_BB_FN (bb, cfun) |
e0082a72 | 603 | SET_STATE (bb, |
d7c028c0 | 604 | bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL); |
b932f770 | 605 | |
11cd3bed | 606 | FOR_EACH_BB_FN (bb, cfun) |
e0082a72 ZD |
607 | if (STATE (bb) == BLOCK_TO_SPLIT) |
608 | find_bb_boundaries (bb); | |
b932f770 | 609 | |
11cd3bed | 610 | FOR_EACH_BB_FN (bb, cfun) |
e0082a72 | 611 | if (STATE (bb) != BLOCK_ORIGINAL) |
b932f770 | 612 | break; |
4891442b | 613 | |
e0082a72 | 614 | min = max = bb; |
fefa31b5 | 615 | for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb) |
e0082a72 ZD |
616 | if (STATE (bb) != BLOCK_ORIGINAL) |
617 | max = bb; | |
402209ff JH |
618 | |
619 | /* Now re-scan and wire in all edges. This expect simple (conditional) | |
620 | jumps at the end of each new basic blocks. */ | |
2df6cea5 | 621 | make_edges (min, max, 1); |
402209ff JH |
622 | |
623 | /* Update branch probabilities. Expect only (un)conditional jumps | |
624 | to be created with only the forward edges. */ | |
0a6a6ac9 | 625 | if (profile_status_for_fn (cfun) != PROFILE_ABSENT) |
87022a6b JH |
626 | FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb) |
627 | { | |
628 | edge e; | |
628f6a4e | 629 | edge_iterator ei; |
87022a6b JH |
630 | |
631 | if (STATE (bb) == BLOCK_ORIGINAL) | |
632 | continue; | |
633 | if (STATE (bb) == BLOCK_NEW) | |
634 | { | |
635 | bb->count = 0; | |
636 | bb->frequency = 0; | |
628f6a4e | 637 | FOR_EACH_EDGE (e, ei, bb->preds) |
87022a6b JH |
638 | { |
639 | bb->count += e->count; | |
640 | bb->frequency += EDGE_FREQUENCY (e); | |
641 | } | |
642 | } | |
4891442b | 643 | |
87022a6b JH |
644 | compute_outgoing_frequencies (bb); |
645 | } | |
4891442b | 646 | |
11cd3bed | 647 | FOR_EACH_BB_FN (bb, cfun) |
e0082a72 | 648 | SET_STATE (bb, 0); |
b932f770 | 649 | } |