PR bootstrap/66085
[official-gcc.git] / gcc / cfgbuild.c
blob7cbed5006322e4a569cdc6ba86952d47c7cd413f
1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2015 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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "rtl.h"
36 #include "hard-reg-set.h"
37 #include "predict.h"
38 #include "hashtab.h"
39 #include "function.h"
40 #include "dominance.h"
41 #include "cfg.h"
42 #include "cfgrtl.h"
43 #include "cfganal.h"
44 #include "cfgbuild.h"
45 #include "basic-block.h"
46 #include "regs.h"
47 #include "flags.h"
48 #include "except.h"
49 #include "statistics.h"
50 #include "real.h"
51 #include "fixed-value.h"
52 #include "insn-config.h"
53 #include "expmed.h"
54 #include "dojump.h"
55 #include "explow.h"
56 #include "calls.h"
57 #include "emit-rtl.h"
58 #include "varasm.h"
59 #include "stmt.h"
60 #include "expr.h"
61 #include "diagnostic-core.h"
62 #include "timevar.h"
63 #include "sbitmap.h"
65 static void make_edges (basic_block, basic_block, int);
66 static void make_label_edge (sbitmap, basic_block, rtx, int);
67 static void find_bb_boundaries (basic_block);
68 static void compute_outgoing_frequencies (basic_block);
70 /* Return true if insn is something that should be contained inside basic
71 block. */
73 bool
74 inside_basic_block_p (const rtx_insn *insn)
76 switch (GET_CODE (insn))
78 case CODE_LABEL:
79 /* Avoid creating of basic block for jumptables. */
80 return (NEXT_INSN (insn) == 0
81 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
83 case JUMP_INSN:
84 case CALL_INSN:
85 case INSN:
86 case DEBUG_INSN:
87 return true;
89 case JUMP_TABLE_DATA:
90 case BARRIER:
91 case NOTE:
92 return false;
94 default:
95 gcc_unreachable ();
99 /* Return true if INSN may cause control flow transfer, so it should be last in
100 the basic block. */
102 bool
103 control_flow_insn_p (const rtx_insn *insn)
105 switch (GET_CODE (insn))
107 case NOTE:
108 case CODE_LABEL:
109 case DEBUG_INSN:
110 return false;
112 case JUMP_INSN:
113 return true;
115 case CALL_INSN:
116 /* Noreturn and sibling call instructions terminate the basic blocks
117 (but only if they happen unconditionally). */
118 if ((SIBLING_CALL_P (insn)
119 || find_reg_note (insn, REG_NORETURN, 0))
120 && GET_CODE (PATTERN (insn)) != COND_EXEC)
121 return true;
123 /* Call insn may return to the nonlocal goto handler. */
124 if (can_nonlocal_goto (insn))
125 return true;
126 break;
128 case INSN:
129 /* Treat trap instructions like noreturn calls (same provision). */
130 if (GET_CODE (PATTERN (insn)) == TRAP_IF
131 && XEXP (PATTERN (insn), 0) == const1_rtx)
132 return true;
133 if (!cfun->can_throw_non_call_exceptions)
134 return false;
135 break;
137 case JUMP_TABLE_DATA:
138 case BARRIER:
139 /* It is nonsense to reach this when looking for the
140 end of basic block, but before dead code is eliminated
141 this may happen. */
142 return false;
144 default:
145 gcc_unreachable ();
148 return can_throw_internal (insn);
152 /* Create an edge between two basic blocks. FLAGS are auxiliary information
153 about the edge that is accumulated between calls. */
155 /* Create an edge from a basic block to a label. */
157 static void
158 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
160 gcc_assert (LABEL_P (label));
162 /* If the label was never emitted, this insn is junk, but avoid a
163 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
164 as a result of a syntax error and a diagnostic has already been
165 printed. */
167 if (INSN_UID (label) == 0)
168 return;
170 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
173 /* Create the edges generated by INSN in REGION. */
175 void
176 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
178 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
180 if (lp)
182 rtx label = lp->landing_pad;
184 /* During initial rtl generation, use the post_landing_pad. */
185 if (label == NULL)
187 gcc_assert (lp->post_landing_pad);
188 label = label_rtx (lp->post_landing_pad);
191 make_label_edge (edge_cache, src, label,
192 EDGE_ABNORMAL | EDGE_EH
193 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
197 /* States of basic block as seen by find_many_sub_basic_blocks. */
198 enum state {
199 /* Basic blocks created via split_block belong to this state.
200 make_edges will examine these basic blocks to see if we need to
201 create edges going out of them. */
202 BLOCK_NEW = 0,
204 /* Basic blocks that do not need examining belong to this state.
205 These blocks will be left intact. In particular, make_edges will
206 not create edges going out of these basic blocks. */
207 BLOCK_ORIGINAL,
209 /* Basic blocks that may need splitting (due to a label appearing in
210 the middle, etc) belong to this state. After splitting them,
211 make_edges will create edges going out of them as needed. */
212 BLOCK_TO_SPLIT
215 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
216 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
218 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
219 #define BLOCK_USED_BY_TABLEJUMP 32
220 #define FULL_STATE(BB) ((size_t) (BB)->aux)
222 /* Identify the edges going out of basic blocks between MIN and MAX,
223 inclusive, that have their states set to BLOCK_NEW or
224 BLOCK_TO_SPLIT.
226 UPDATE_P should be nonzero if we are updating CFG and zero if we
227 are building CFG from scratch. */
229 static void
230 make_edges (basic_block min, basic_block max, int update_p)
232 basic_block bb;
233 sbitmap edge_cache = NULL;
235 /* Heavy use of computed goto in machine-generated code can lead to
236 nearly fully-connected CFGs. In that case we spend a significant
237 amount of time searching the edge lists for duplicates. */
238 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
239 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
241 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
242 is always the entry. */
243 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
244 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
246 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
248 rtx_insn *insn;
249 enum rtx_code code;
250 edge e;
251 edge_iterator ei;
253 if (STATE (bb) == BLOCK_ORIGINAL)
254 continue;
256 /* If we have an edge cache, cache edges going out of BB. */
257 if (edge_cache)
259 bitmap_clear (edge_cache);
260 if (update_p)
262 FOR_EACH_EDGE (e, ei, bb->succs)
263 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
264 bitmap_set_bit (edge_cache, e->dest->index);
268 if (LABEL_P (BB_HEAD (bb))
269 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
270 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
272 /* Examine the last instruction of the block, and discover the
273 ways we can leave the block. */
275 insn = BB_END (bb);
276 code = GET_CODE (insn);
278 /* A branch. */
279 if (code == JUMP_INSN)
281 rtx tmp;
282 rtx_jump_table_data *table;
284 /* Recognize a non-local goto as a branch outside the
285 current function. */
286 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
289 /* Recognize a tablejump and do the right thing. */
290 else if (tablejump_p (insn, NULL, &table))
292 rtvec vec = table->get_labels ();
293 int j;
295 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
296 make_label_edge (edge_cache, bb,
297 XEXP (RTVEC_ELT (vec, j), 0), 0);
299 /* Some targets (eg, ARM) emit a conditional jump that also
300 contains the out-of-range target. Scan for these and
301 add an edge if necessary. */
302 if ((tmp = single_set (insn)) != NULL
303 && SET_DEST (tmp) == pc_rtx
304 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
305 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
306 make_label_edge (edge_cache, bb,
307 LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0);
310 /* If this is a computed jump, then mark it as reaching
311 everything on the forced_labels list. */
312 else if (computed_jump_p (insn))
314 for (rtx_insn_list *x = forced_labels; x; x = x->next ())
315 make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL);
318 /* Returns create an exit out. */
319 else if (returnjump_p (insn))
320 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
322 /* Recognize asm goto and do the right thing. */
323 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
325 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
326 for (i = 0; i < n; ++i)
327 make_label_edge (edge_cache, bb,
328 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
331 /* Otherwise, we have a plain conditional or unconditional jump. */
332 else
334 gcc_assert (JUMP_LABEL (insn));
335 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
339 /* If this is a sibling call insn, then this is in effect a combined call
340 and return, and so we need an edge to the exit block. No need to
341 worry about EH edges, since we wouldn't have created the sibling call
342 in the first place. */
343 if (code == CALL_INSN && SIBLING_CALL_P (insn))
344 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
345 EDGE_SIBCALL | EDGE_ABNORMAL);
347 /* If this is a CALL_INSN, then mark it as reaching the active EH
348 handler for this CALL_INSN. If we're handling non-call
349 exceptions then any insn can reach any of the active handlers.
350 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
351 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
353 /* Add any appropriate EH edges. */
354 rtl_make_eh_edge (edge_cache, bb, insn);
356 if (code == CALL_INSN)
358 if (can_nonlocal_goto (insn))
360 /* ??? This could be made smarter: in some cases it's
361 possible to tell that certain calls will not do a
362 nonlocal goto. For example, if the nested functions
363 that do the nonlocal gotos do not have their addresses
364 taken, then only calls to those functions or to other
365 nested functions that use them could possibly do
366 nonlocal gotos. */
367 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
369 x = x->next ())
370 make_label_edge (edge_cache, bb, x->insn (),
371 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
374 if (flag_tm)
376 rtx note;
377 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
378 if (REG_NOTE_KIND (note) == REG_TM)
379 make_label_edge (edge_cache, bb, XEXP (note, 0),
380 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
385 /* Find out if we can drop through to the next block. */
386 insn = NEXT_INSN (insn);
387 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
388 if (e && e->flags & EDGE_FALLTHRU)
389 insn = NULL;
391 while (insn
392 && NOTE_P (insn)
393 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
394 insn = NEXT_INSN (insn);
396 if (!insn)
397 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
398 EDGE_FALLTHRU);
399 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
401 if (insn == BB_HEAD (bb->next_bb))
402 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
406 if (edge_cache)
407 sbitmap_free (edge_cache);
410 static void
411 mark_tablejump_edge (rtx label)
413 basic_block bb;
415 gcc_assert (LABEL_P (label));
416 /* See comment in make_label_edge. */
417 if (INSN_UID (label) == 0)
418 return;
419 bb = BLOCK_FOR_INSN (label);
420 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
423 static void
424 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
426 rtx_insn *insn = BB_END (bb);
427 rtx tmp;
428 rtvec vec;
429 int j;
430 edge_iterator ei;
431 edge e;
433 vec = table->get_labels ();
435 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
436 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
438 /* Some targets (eg, ARM) emit a conditional jump that also
439 contains the out-of-range target. Scan for these and
440 add an edge if necessary. */
441 if ((tmp = single_set (insn)) != NULL
442 && SET_DEST (tmp) == pc_rtx
443 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
444 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
445 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)));
447 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
449 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
450 SET_STATE (e->dest, FULL_STATE (e->dest)
451 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
452 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
454 remove_edge (e);
455 continue;
457 ei_next (&ei);
461 /* Scan basic block BB for possible BB boundaries inside the block
462 and create new basic blocks in the progress. */
464 static void
465 find_bb_boundaries (basic_block bb)
467 basic_block orig_bb = bb;
468 rtx_insn *insn = BB_HEAD (bb);
469 rtx_insn *end = BB_END (bb), *x;
470 rtx_jump_table_data *table;
471 rtx_insn *flow_transfer_insn = NULL;
472 edge fallthru = NULL;
474 if (insn == BB_END (bb))
475 return;
477 if (LABEL_P (insn))
478 insn = NEXT_INSN (insn);
480 /* Scan insn chain and try to find new basic block boundaries. */
481 while (1)
483 enum rtx_code code = GET_CODE (insn);
485 /* In case we've previously seen an insn that effects a control
486 flow transfer, split the block. */
487 if ((flow_transfer_insn || code == CODE_LABEL)
488 && inside_basic_block_p (insn))
490 fallthru = split_block (bb, PREV_INSN (insn));
491 if (flow_transfer_insn)
493 BB_END (bb) = flow_transfer_insn;
495 /* Clean up the bb field for the insns between the blocks. */
496 for (x = NEXT_INSN (flow_transfer_insn);
497 x != BB_HEAD (fallthru->dest);
498 x = NEXT_INSN (x))
499 if (!BARRIER_P (x))
500 set_block_for_insn (x, NULL);
503 bb = fallthru->dest;
504 remove_edge (fallthru);
505 flow_transfer_insn = NULL;
506 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
507 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
509 else if (code == BARRIER)
511 /* __builtin_unreachable () may cause a barrier to be emitted in
512 the middle of a BB. We need to split it in the same manner as
513 if the barrier were preceded by a control_flow_insn_p insn. */
514 if (!flow_transfer_insn)
515 flow_transfer_insn = prev_nonnote_insn_bb (insn);
518 if (control_flow_insn_p (insn))
519 flow_transfer_insn = insn;
520 if (insn == end)
521 break;
522 insn = NEXT_INSN (insn);
525 /* In case expander replaced normal insn by sequence terminating by
526 return and barrier, or possibly other sequence not behaving like
527 ordinary jump, we need to take care and move basic block boundary. */
528 if (flow_transfer_insn)
530 BB_END (bb) = flow_transfer_insn;
532 /* Clean up the bb field for the insns that do not belong to BB. */
533 x = flow_transfer_insn;
534 while (x != end)
536 x = NEXT_INSN (x);
537 if (!BARRIER_P (x))
538 set_block_for_insn (x, NULL);
542 /* We've possibly replaced the conditional jump by conditional jump
543 followed by cleanup at fallthru edge, so the outgoing edges may
544 be dead. */
545 purge_dead_edges (bb);
547 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
548 basic block, we might need to kill some edges. */
549 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
550 purge_dead_tablejump_edges (bb, table);
553 /* Assume that frequency of basic block B is known. Compute frequencies
554 and probabilities of outgoing edges. */
556 static void
557 compute_outgoing_frequencies (basic_block b)
559 edge e, f;
560 edge_iterator ei;
562 if (EDGE_COUNT (b->succs) == 2)
564 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
565 int probability;
567 if (note)
569 probability = XINT (note, 0);
570 e = BRANCH_EDGE (b);
571 e->probability = probability;
572 e->count = apply_probability (b->count, probability);
573 f = FALLTHRU_EDGE (b);
574 f->probability = REG_BR_PROB_BASE - probability;
575 f->count = b->count - e->count;
576 return;
578 else
580 guess_outgoing_edge_probabilities (b);
583 else if (single_succ_p (b))
585 e = single_succ_edge (b);
586 e->probability = REG_BR_PROB_BASE;
587 e->count = b->count;
588 return;
590 else
592 /* We rely on BBs with more than two successors to have sane probabilities
593 and do not guess them here. For BBs terminated by switch statements
594 expanded to jump-table jump, we have done the right thing during
595 expansion. For EH edges, we still guess the probabilities here. */
596 bool complex_edge = false;
597 FOR_EACH_EDGE (e, ei, b->succs)
598 if (e->flags & EDGE_COMPLEX)
600 complex_edge = true;
601 break;
603 if (complex_edge)
604 guess_outgoing_edge_probabilities (b);
607 if (b->count)
608 FOR_EACH_EDGE (e, ei, b->succs)
609 e->count = apply_probability (b->count, e->probability);
612 /* Assume that some pass has inserted labels or control flow
613 instructions within a basic block. Split basic blocks as needed
614 and create edges. */
616 void
617 find_many_sub_basic_blocks (sbitmap blocks)
619 basic_block bb, min, max;
621 FOR_EACH_BB_FN (bb, cfun)
622 SET_STATE (bb,
623 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
625 FOR_EACH_BB_FN (bb, cfun)
626 if (STATE (bb) == BLOCK_TO_SPLIT)
627 find_bb_boundaries (bb);
629 FOR_EACH_BB_FN (bb, cfun)
630 if (STATE (bb) != BLOCK_ORIGINAL)
631 break;
633 min = max = bb;
634 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
635 if (STATE (bb) != BLOCK_ORIGINAL)
636 max = bb;
638 /* Now re-scan and wire in all edges. This expect simple (conditional)
639 jumps at the end of each new basic blocks. */
640 make_edges (min, max, 1);
642 /* Update branch probabilities. Expect only (un)conditional jumps
643 to be created with only the forward edges. */
644 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
645 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
647 edge e;
648 edge_iterator ei;
650 if (STATE (bb) == BLOCK_ORIGINAL)
651 continue;
652 if (STATE (bb) == BLOCK_NEW)
654 bb->count = 0;
655 bb->frequency = 0;
656 FOR_EACH_EDGE (e, ei, bb->preds)
658 bb->count += e->count;
659 bb->frequency += EDGE_FREQUENCY (e);
663 compute_outgoing_frequencies (bb);
666 FOR_EACH_BB_FN (bb, cfun)
667 SET_STATE (bb, 0);