Fix bootstrap/PR63632
[official-gcc.git] / gcc / cfgbuild.c
blobe1ce1191534da5692e97776e203841baba8d0daa
1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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
8 Software Foundation; either version 3, or (at your option) any later
9 version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "vec.h"
34 #include "machmode.h"
35 #include "input.h"
36 #include "function.h"
37 #include "except.h"
38 #include "expr.h"
39 #include "diagnostic-core.h"
40 #include "timevar.h"
41 #include "sbitmap.h"
43 static void make_edges (basic_block, basic_block, int);
44 static void make_label_edge (sbitmap, basic_block, rtx, int);
45 static void find_bb_boundaries (basic_block);
46 static void compute_outgoing_frequencies (basic_block);
48 /* Return true if insn is something that should be contained inside basic
49 block. */
51 bool
52 inside_basic_block_p (const rtx_insn *insn)
54 switch (GET_CODE (insn))
56 case CODE_LABEL:
57 /* Avoid creating of basic block for jumptables. */
58 return (NEXT_INSN (insn) == 0
59 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
61 case JUMP_INSN:
62 case CALL_INSN:
63 case INSN:
64 case DEBUG_INSN:
65 return true;
67 case JUMP_TABLE_DATA:
68 case BARRIER:
69 case NOTE:
70 return false;
72 default:
73 gcc_unreachable ();
77 /* Return true if INSN may cause control flow transfer, so it should be last in
78 the basic block. */
80 bool
81 control_flow_insn_p (const rtx_insn *insn)
83 switch (GET_CODE (insn))
85 case NOTE:
86 case CODE_LABEL:
87 case DEBUG_INSN:
88 return false;
90 case JUMP_INSN:
91 return true;
93 case CALL_INSN:
94 /* Noreturn and sibling call instructions terminate the basic blocks
95 (but only if they happen unconditionally). */
96 if ((SIBLING_CALL_P (insn)
97 || find_reg_note (insn, REG_NORETURN, 0))
98 && GET_CODE (PATTERN (insn)) != COND_EXEC)
99 return true;
101 /* Call insn may return to the nonlocal goto handler. */
102 if (can_nonlocal_goto (insn))
103 return true;
104 break;
106 case INSN:
107 /* Treat trap instructions like noreturn calls (same provision). */
108 if (GET_CODE (PATTERN (insn)) == TRAP_IF
109 && XEXP (PATTERN (insn), 0) == const1_rtx)
110 return true;
111 if (!cfun->can_throw_non_call_exceptions)
112 return false;
113 break;
115 case JUMP_TABLE_DATA:
116 case BARRIER:
117 /* It is nonsense to reach this when looking for the
118 end of basic block, but before dead code is eliminated
119 this may happen. */
120 return false;
122 default:
123 gcc_unreachable ();
126 return can_throw_internal (insn);
130 /* Create an edge between two basic blocks. FLAGS are auxiliary information
131 about the edge that is accumulated between calls. */
133 /* Create an edge from a basic block to a label. */
135 static void
136 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
138 gcc_assert (LABEL_P (label));
140 /* If the label was never emitted, this insn is junk, but avoid a
141 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
142 as a result of a syntax error and a diagnostic has already been
143 printed. */
145 if (INSN_UID (label) == 0)
146 return;
148 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
151 /* Create the edges generated by INSN in REGION. */
153 void
154 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
156 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
158 if (lp)
160 rtx label = lp->landing_pad;
162 /* During initial rtl generation, use the post_landing_pad. */
163 if (label == NULL)
165 gcc_assert (lp->post_landing_pad);
166 label = label_rtx (lp->post_landing_pad);
169 make_label_edge (edge_cache, src, label,
170 EDGE_ABNORMAL | EDGE_EH
171 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
175 /* States of basic block as seen by find_many_sub_basic_blocks. */
176 enum state {
177 /* Basic blocks created via split_block belong to this state.
178 make_edges will examine these basic blocks to see if we need to
179 create edges going out of them. */
180 BLOCK_NEW = 0,
182 /* Basic blocks that do not need examining belong to this state.
183 These blocks will be left intact. In particular, make_edges will
184 not create edges going out of these basic blocks. */
185 BLOCK_ORIGINAL,
187 /* Basic blocks that may need splitting (due to a label appearing in
188 the middle, etc) belong to this state. After splitting them,
189 make_edges will create edges going out of them as needed. */
190 BLOCK_TO_SPLIT
193 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
194 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
196 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
197 #define BLOCK_USED_BY_TABLEJUMP 32
198 #define FULL_STATE(BB) ((size_t) (BB)->aux)
200 /* Identify the edges going out of basic blocks between MIN and MAX,
201 inclusive, that have their states set to BLOCK_NEW or
202 BLOCK_TO_SPLIT.
204 UPDATE_P should be nonzero if we are updating CFG and zero if we
205 are building CFG from scratch. */
207 static void
208 make_edges (basic_block min, basic_block max, int update_p)
210 basic_block bb;
211 sbitmap edge_cache = NULL;
213 /* Heavy use of computed goto in machine-generated code can lead to
214 nearly fully-connected CFGs. In that case we spend a significant
215 amount of time searching the edge lists for duplicates. */
216 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
217 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
219 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
220 is always the entry. */
221 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
222 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
224 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
226 rtx_insn *insn;
227 enum rtx_code code;
228 edge e;
229 edge_iterator ei;
231 if (STATE (bb) == BLOCK_ORIGINAL)
232 continue;
234 /* If we have an edge cache, cache edges going out of BB. */
235 if (edge_cache)
237 bitmap_clear (edge_cache);
238 if (update_p)
240 FOR_EACH_EDGE (e, ei, bb->succs)
241 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
242 bitmap_set_bit (edge_cache, e->dest->index);
246 if (LABEL_P (BB_HEAD (bb))
247 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
248 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
250 /* Examine the last instruction of the block, and discover the
251 ways we can leave the block. */
253 insn = BB_END (bb);
254 code = GET_CODE (insn);
256 /* A branch. */
257 if (code == JUMP_INSN)
259 rtx tmp;
260 rtx_jump_table_data *table;
262 /* Recognize a non-local goto as a branch outside the
263 current function. */
264 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
267 /* Recognize a tablejump and do the right thing. */
268 else if (tablejump_p (insn, NULL, &table))
270 rtvec vec = table->get_labels ();
271 int j;
273 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
274 make_label_edge (edge_cache, bb,
275 XEXP (RTVEC_ELT (vec, j), 0), 0);
277 /* Some targets (eg, ARM) emit a conditional jump that also
278 contains the out-of-range target. Scan for these and
279 add an edge if necessary. */
280 if ((tmp = single_set (insn)) != NULL
281 && SET_DEST (tmp) == pc_rtx
282 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
283 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
284 make_label_edge (edge_cache, bb,
285 LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0);
288 /* If this is a computed jump, then mark it as reaching
289 everything on the forced_labels list. */
290 else if (computed_jump_p (insn))
292 for (rtx_insn_list *x = forced_labels; x; x = x->next ())
293 make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL);
296 /* Returns create an exit out. */
297 else if (returnjump_p (insn))
298 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
300 /* Recognize asm goto and do the right thing. */
301 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
303 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
304 for (i = 0; i < n; ++i)
305 make_label_edge (edge_cache, bb,
306 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
309 /* Otherwise, we have a plain conditional or unconditional jump. */
310 else
312 gcc_assert (JUMP_LABEL (insn));
313 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
317 /* If this is a sibling call insn, then this is in effect a combined call
318 and return, and so we need an edge to the exit block. No need to
319 worry about EH edges, since we wouldn't have created the sibling call
320 in the first place. */
321 if (code == CALL_INSN && SIBLING_CALL_P (insn))
322 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
323 EDGE_SIBCALL | EDGE_ABNORMAL);
325 /* If this is a CALL_INSN, then mark it as reaching the active EH
326 handler for this CALL_INSN. If we're handling non-call
327 exceptions then any insn can reach any of the active handlers.
328 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
329 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
331 /* Add any appropriate EH edges. */
332 rtl_make_eh_edge (edge_cache, bb, insn);
334 if (code == CALL_INSN)
336 if (can_nonlocal_goto (insn))
338 /* ??? This could be made smarter: in some cases it's
339 possible to tell that certain calls will not do a
340 nonlocal goto. For example, if the nested functions
341 that do the nonlocal gotos do not have their addresses
342 taken, then only calls to those functions or to other
343 nested functions that use them could possibly do
344 nonlocal gotos. */
345 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
347 x = x->next ())
348 make_label_edge (edge_cache, bb, x->insn (),
349 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
352 if (flag_tm)
354 rtx note;
355 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
356 if (REG_NOTE_KIND (note) == REG_TM)
357 make_label_edge (edge_cache, bb, XEXP (note, 0),
358 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
363 /* Find out if we can drop through to the next block. */
364 insn = NEXT_INSN (insn);
365 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
366 if (e && e->flags & EDGE_FALLTHRU)
367 insn = NULL;
369 while (insn
370 && NOTE_P (insn)
371 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
372 insn = NEXT_INSN (insn);
374 if (!insn)
375 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
376 EDGE_FALLTHRU);
377 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
379 if (insn == BB_HEAD (bb->next_bb))
380 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
384 if (edge_cache)
385 sbitmap_free (edge_cache);
388 static void
389 mark_tablejump_edge (rtx label)
391 basic_block bb;
393 gcc_assert (LABEL_P (label));
394 /* See comment in make_label_edge. */
395 if (INSN_UID (label) == 0)
396 return;
397 bb = BLOCK_FOR_INSN (label);
398 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
401 static void
402 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
404 rtx_insn *insn = BB_END (bb);
405 rtx tmp;
406 rtvec vec;
407 int j;
408 edge_iterator ei;
409 edge e;
411 vec = table->get_labels ();
413 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
414 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
416 /* Some targets (eg, ARM) emit a conditional jump that also
417 contains the out-of-range target. Scan for these and
418 add an edge if necessary. */
419 if ((tmp = single_set (insn)) != NULL
420 && SET_DEST (tmp) == pc_rtx
421 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
422 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
423 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)));
425 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
427 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
428 SET_STATE (e->dest, FULL_STATE (e->dest)
429 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
430 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
432 remove_edge (e);
433 continue;
435 ei_next (&ei);
439 /* Scan basic block BB for possible BB boundaries inside the block
440 and create new basic blocks in the progress. */
442 static void
443 find_bb_boundaries (basic_block bb)
445 basic_block orig_bb = bb;
446 rtx_insn *insn = BB_HEAD (bb);
447 rtx_insn *end = BB_END (bb), *x;
448 rtx_jump_table_data *table;
449 rtx_insn *flow_transfer_insn = NULL;
450 edge fallthru = NULL;
452 if (insn == BB_END (bb))
453 return;
455 if (LABEL_P (insn))
456 insn = NEXT_INSN (insn);
458 /* Scan insn chain and try to find new basic block boundaries. */
459 while (1)
461 enum rtx_code code = GET_CODE (insn);
463 /* In case we've previously seen an insn that effects a control
464 flow transfer, split the block. */
465 if ((flow_transfer_insn || code == CODE_LABEL)
466 && inside_basic_block_p (insn))
468 fallthru = split_block (bb, PREV_INSN (insn));
469 if (flow_transfer_insn)
471 BB_END (bb) = flow_transfer_insn;
473 /* Clean up the bb field for the insns between the blocks. */
474 for (x = NEXT_INSN (flow_transfer_insn);
475 x != BB_HEAD (fallthru->dest);
476 x = NEXT_INSN (x))
477 if (!BARRIER_P (x))
478 set_block_for_insn (x, NULL);
481 bb = fallthru->dest;
482 remove_edge (fallthru);
483 flow_transfer_insn = NULL;
484 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
485 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
487 else if (code == BARRIER)
489 /* __builtin_unreachable () may cause a barrier to be emitted in
490 the middle of a BB. We need to split it in the same manner as
491 if the barrier were preceded by a control_flow_insn_p insn. */
492 if (!flow_transfer_insn)
493 flow_transfer_insn = prev_nonnote_insn_bb (insn);
496 if (control_flow_insn_p (insn))
497 flow_transfer_insn = insn;
498 if (insn == end)
499 break;
500 insn = NEXT_INSN (insn);
503 /* In case expander replaced normal insn by sequence terminating by
504 return and barrier, or possibly other sequence not behaving like
505 ordinary jump, we need to take care and move basic block boundary. */
506 if (flow_transfer_insn)
508 BB_END (bb) = flow_transfer_insn;
510 /* Clean up the bb field for the insns that do not belong to BB. */
511 x = flow_transfer_insn;
512 while (x != end)
514 x = NEXT_INSN (x);
515 if (!BARRIER_P (x))
516 set_block_for_insn (x, NULL);
520 /* We've possibly replaced the conditional jump by conditional jump
521 followed by cleanup at fallthru edge, so the outgoing edges may
522 be dead. */
523 purge_dead_edges (bb);
525 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
526 basic block, we might need to kill some edges. */
527 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
528 purge_dead_tablejump_edges (bb, table);
531 /* Assume that frequency of basic block B is known. Compute frequencies
532 and probabilities of outgoing edges. */
534 static void
535 compute_outgoing_frequencies (basic_block b)
537 edge e, f;
538 edge_iterator ei;
540 if (EDGE_COUNT (b->succs) == 2)
542 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
543 int probability;
545 if (note)
547 probability = XINT (note, 0);
548 e = BRANCH_EDGE (b);
549 e->probability = probability;
550 e->count = apply_probability (b->count, probability);
551 f = FALLTHRU_EDGE (b);
552 f->probability = REG_BR_PROB_BASE - probability;
553 f->count = b->count - e->count;
554 return;
556 else
558 guess_outgoing_edge_probabilities (b);
561 else if (single_succ_p (b))
563 e = single_succ_edge (b);
564 e->probability = REG_BR_PROB_BASE;
565 e->count = b->count;
566 return;
568 else
570 /* We rely on BBs with more than two successors to have sane probabilities
571 and do not guess them here. For BBs terminated by switch statements
572 expanded to jump-table jump, we have done the right thing during
573 expansion. For EH edges, we still guess the probabilities here. */
574 bool complex_edge = false;
575 FOR_EACH_EDGE (e, ei, b->succs)
576 if (e->flags & EDGE_COMPLEX)
578 complex_edge = true;
579 break;
581 if (complex_edge)
582 guess_outgoing_edge_probabilities (b);
585 if (b->count)
586 FOR_EACH_EDGE (e, ei, b->succs)
587 e->count = apply_probability (b->count, e->probability);
590 /* Assume that some pass has inserted labels or control flow
591 instructions within a basic block. Split basic blocks as needed
592 and create edges. */
594 void
595 find_many_sub_basic_blocks (sbitmap blocks)
597 basic_block bb, min, max;
599 FOR_EACH_BB_FN (bb, cfun)
600 SET_STATE (bb,
601 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
603 FOR_EACH_BB_FN (bb, cfun)
604 if (STATE (bb) == BLOCK_TO_SPLIT)
605 find_bb_boundaries (bb);
607 FOR_EACH_BB_FN (bb, cfun)
608 if (STATE (bb) != BLOCK_ORIGINAL)
609 break;
611 min = max = bb;
612 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
613 if (STATE (bb) != BLOCK_ORIGINAL)
614 max = bb;
616 /* Now re-scan and wire in all edges. This expect simple (conditional)
617 jumps at the end of each new basic blocks. */
618 make_edges (min, max, 1);
620 /* Update branch probabilities. Expect only (un)conditional jumps
621 to be created with only the forward edges. */
622 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
623 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
625 edge e;
626 edge_iterator ei;
628 if (STATE (bb) == BLOCK_ORIGINAL)
629 continue;
630 if (STATE (bb) == BLOCK_NEW)
632 bb->count = 0;
633 bb->frequency = 0;
634 FOR_EACH_EDGE (e, ei, bb->preds)
636 bb->count += e->count;
637 bb->frequency += EDGE_FREQUENCY (e);
641 compute_outgoing_frequencies (bb);
644 FOR_EACH_BB_FN (bb, cfun)
645 SET_STATE (bb, 0);