Update ChangeLog and version files for release
[official-gcc.git] / gcc / cfgbuild.c
blobc1ec46ad8d7f78f5c6cc6c5dc6c2f9a1e75ce3f6
1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2016 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 "backend.h"
25 #include "rtl.h"
26 #include "cfghooks.h"
27 #include "emit-rtl.h"
28 #include "cfgrtl.h"
29 #include "cfganal.h"
30 #include "cfgbuild.h"
31 #include "except.h"
32 #include "stmt.h"
34 static void make_edges (basic_block, basic_block, int);
35 static void make_label_edge (sbitmap, basic_block, rtx, int);
36 static void find_bb_boundaries (basic_block);
37 static void compute_outgoing_frequencies (basic_block);
39 /* Return true if insn is something that should be contained inside basic
40 block. */
42 bool
43 inside_basic_block_p (const rtx_insn *insn)
45 switch (GET_CODE (insn))
47 case CODE_LABEL:
48 /* Avoid creating of basic block for jumptables. */
49 return (NEXT_INSN (insn) == 0
50 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
52 case JUMP_INSN:
53 case CALL_INSN:
54 case INSN:
55 case DEBUG_INSN:
56 return true;
58 case JUMP_TABLE_DATA:
59 case BARRIER:
60 case NOTE:
61 return false;
63 default:
64 gcc_unreachable ();
68 /* Return true if INSN may cause control flow transfer, so it should be last in
69 the basic block. */
71 bool
72 control_flow_insn_p (const rtx_insn *insn)
74 switch (GET_CODE (insn))
76 case NOTE:
77 case CODE_LABEL:
78 case DEBUG_INSN:
79 return false;
81 case JUMP_INSN:
82 return true;
84 case CALL_INSN:
85 /* Noreturn and sibling call instructions terminate the basic blocks
86 (but only if they happen unconditionally). */
87 if ((SIBLING_CALL_P (insn)
88 || find_reg_note (insn, REG_NORETURN, 0))
89 && GET_CODE (PATTERN (insn)) != COND_EXEC)
90 return true;
92 /* Call insn may return to the nonlocal goto handler. */
93 if (can_nonlocal_goto (insn))
94 return true;
95 break;
97 case INSN:
98 /* Treat trap instructions like noreturn calls (same provision). */
99 if (GET_CODE (PATTERN (insn)) == TRAP_IF
100 && XEXP (PATTERN (insn), 0) == const1_rtx)
101 return true;
102 if (!cfun->can_throw_non_call_exceptions)
103 return false;
104 break;
106 case JUMP_TABLE_DATA:
107 case BARRIER:
108 /* It is nonsense to reach this when looking for the
109 end of basic block, but before dead code is eliminated
110 this may happen. */
111 return false;
113 default:
114 gcc_unreachable ();
117 return can_throw_internal (insn);
121 /* Create an edge between two basic blocks. FLAGS are auxiliary information
122 about the edge that is accumulated between calls. */
124 /* Create an edge from a basic block to a label. */
126 static void
127 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
129 gcc_assert (LABEL_P (label));
131 /* If the label was never emitted, this insn is junk, but avoid a
132 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
133 as a result of a syntax error and a diagnostic has already been
134 printed. */
136 if (INSN_UID (label) == 0)
137 return;
139 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
142 /* Create the edges generated by INSN in REGION. */
144 void
145 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
147 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
149 if (lp)
151 rtx_insn *label = lp->landing_pad;
153 /* During initial rtl generation, use the post_landing_pad. */
154 if (label == NULL)
156 gcc_assert (lp->post_landing_pad);
157 label = label_rtx (lp->post_landing_pad);
160 make_label_edge (edge_cache, src, label,
161 EDGE_ABNORMAL | EDGE_EH
162 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
166 /* States of basic block as seen by find_many_sub_basic_blocks. */
167 enum state {
168 /* Basic blocks created via split_block belong to this state.
169 make_edges will examine these basic blocks to see if we need to
170 create edges going out of them. */
171 BLOCK_NEW = 0,
173 /* Basic blocks that do not need examining belong to this state.
174 These blocks will be left intact. In particular, make_edges will
175 not create edges going out of these basic blocks. */
176 BLOCK_ORIGINAL,
178 /* Basic blocks that may need splitting (due to a label appearing in
179 the middle, etc) belong to this state. After splitting them,
180 make_edges will create edges going out of them as needed. */
181 BLOCK_TO_SPLIT
184 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
185 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
187 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
188 #define BLOCK_USED_BY_TABLEJUMP 32
189 #define FULL_STATE(BB) ((size_t) (BB)->aux)
191 /* Identify the edges going out of basic blocks between MIN and MAX,
192 inclusive, that have their states set to BLOCK_NEW or
193 BLOCK_TO_SPLIT.
195 UPDATE_P should be nonzero if we are updating CFG and zero if we
196 are building CFG from scratch. */
198 static void
199 make_edges (basic_block min, basic_block max, int update_p)
201 basic_block bb;
202 sbitmap edge_cache = NULL;
204 /* Heavy use of computed goto in machine-generated code can lead to
205 nearly fully-connected CFGs. In that case we spend a significant
206 amount of time searching the edge lists for duplicates. */
207 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
208 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
210 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
211 is always the entry. */
212 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
213 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
215 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
217 rtx_insn *insn;
218 enum rtx_code code;
219 edge e;
220 edge_iterator ei;
222 if (STATE (bb) == BLOCK_ORIGINAL)
223 continue;
225 /* If we have an edge cache, cache edges going out of BB. */
226 if (edge_cache)
228 bitmap_clear (edge_cache);
229 if (update_p)
231 FOR_EACH_EDGE (e, ei, bb->succs)
232 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
233 bitmap_set_bit (edge_cache, e->dest->index);
237 if (LABEL_P (BB_HEAD (bb))
238 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
239 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
241 /* Examine the last instruction of the block, and discover the
242 ways we can leave the block. */
244 insn = BB_END (bb);
245 code = GET_CODE (insn);
247 /* A branch. */
248 if (code == JUMP_INSN)
250 rtx tmp;
251 rtx_jump_table_data *table;
253 /* Recognize a non-local goto as a branch outside the
254 current function. */
255 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
258 /* Recognize a tablejump and do the right thing. */
259 else if (tablejump_p (insn, NULL, &table))
261 rtvec vec = table->get_labels ();
262 int j;
264 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
265 make_label_edge (edge_cache, bb,
266 XEXP (RTVEC_ELT (vec, j), 0), 0);
268 /* Some targets (eg, ARM) emit a conditional jump that also
269 contains the out-of-range target. Scan for these and
270 add an edge if necessary. */
271 if ((tmp = single_set (insn)) != NULL
272 && SET_DEST (tmp) == pc_rtx
273 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
274 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
275 make_label_edge (edge_cache, bb,
276 LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0);
279 /* If this is a computed jump, then mark it as reaching
280 everything on the forced_labels list. */
281 else if (computed_jump_p (insn))
283 for (rtx_insn_list *x = forced_labels; x; x = x->next ())
284 make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL);
287 /* Returns create an exit out. */
288 else if (returnjump_p (insn))
289 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
291 /* Recognize asm goto and do the right thing. */
292 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
294 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
295 for (i = 0; i < n; ++i)
296 make_label_edge (edge_cache, bb,
297 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
300 /* Otherwise, we have a plain conditional or unconditional jump. */
301 else
303 gcc_assert (JUMP_LABEL (insn));
304 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
308 /* If this is a sibling call insn, then this is in effect a combined call
309 and return, and so we need an edge to the exit block. No need to
310 worry about EH edges, since we wouldn't have created the sibling call
311 in the first place. */
312 if (code == CALL_INSN && SIBLING_CALL_P (insn))
313 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
314 EDGE_SIBCALL | EDGE_ABNORMAL);
316 /* If this is a CALL_INSN, then mark it as reaching the active EH
317 handler for this CALL_INSN. If we're handling non-call
318 exceptions then any insn can reach any of the active handlers.
319 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
320 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
322 /* Add any appropriate EH edges. */
323 rtl_make_eh_edge (edge_cache, bb, insn);
325 if (code == CALL_INSN)
327 if (can_nonlocal_goto (insn))
329 /* ??? This could be made smarter: in some cases it's
330 possible to tell that certain calls will not do a
331 nonlocal goto. For example, if the nested functions
332 that do the nonlocal gotos do not have their addresses
333 taken, then only calls to those functions or to other
334 nested functions that use them could possibly do
335 nonlocal gotos. */
336 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
338 x = x->next ())
339 make_label_edge (edge_cache, bb, x->insn (),
340 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
343 if (flag_tm)
345 rtx note;
346 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
347 if (REG_NOTE_KIND (note) == REG_TM)
348 make_label_edge (edge_cache, bb, XEXP (note, 0),
349 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
354 /* Find out if we can drop through to the next block. */
355 insn = NEXT_INSN (insn);
356 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
357 if (e && e->flags & EDGE_FALLTHRU)
358 insn = NULL;
360 while (insn
361 && NOTE_P (insn)
362 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
363 insn = NEXT_INSN (insn);
365 if (!insn)
366 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
367 EDGE_FALLTHRU);
368 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
370 if (insn == BB_HEAD (bb->next_bb))
371 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
375 if (edge_cache)
376 sbitmap_free (edge_cache);
379 static void
380 mark_tablejump_edge (rtx label)
382 basic_block bb;
384 gcc_assert (LABEL_P (label));
385 /* See comment in make_label_edge. */
386 if (INSN_UID (label) == 0)
387 return;
388 bb = BLOCK_FOR_INSN (label);
389 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
392 static void
393 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
395 rtx_insn *insn = BB_END (bb);
396 rtx tmp;
397 rtvec vec;
398 int j;
399 edge_iterator ei;
400 edge e;
402 vec = table->get_labels ();
404 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
405 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
407 /* Some targets (eg, ARM) emit a conditional jump that also
408 contains the out-of-range target. Scan for these and
409 add an edge if necessary. */
410 if ((tmp = single_set (insn)) != NULL
411 && SET_DEST (tmp) == pc_rtx
412 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
413 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
414 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)));
416 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
418 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
419 SET_STATE (e->dest, FULL_STATE (e->dest)
420 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
421 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
423 remove_edge (e);
424 continue;
426 ei_next (&ei);
430 /* Scan basic block BB for possible BB boundaries inside the block
431 and create new basic blocks in the progress. */
433 static void
434 find_bb_boundaries (basic_block bb)
436 basic_block orig_bb = bb;
437 rtx_insn *insn = BB_HEAD (bb);
438 rtx_insn *end = BB_END (bb), *x;
439 rtx_jump_table_data *table;
440 rtx_insn *flow_transfer_insn = NULL;
441 edge fallthru = NULL;
443 if (insn == BB_END (bb))
444 return;
446 if (LABEL_P (insn))
447 insn = NEXT_INSN (insn);
449 /* Scan insn chain and try to find new basic block boundaries. */
450 while (1)
452 enum rtx_code code = GET_CODE (insn);
454 /* In case we've previously seen an insn that effects a control
455 flow transfer, split the block. */
456 if ((flow_transfer_insn || code == CODE_LABEL)
457 && inside_basic_block_p (insn))
459 fallthru = split_block (bb, PREV_INSN (insn));
460 if (flow_transfer_insn)
462 BB_END (bb) = flow_transfer_insn;
464 /* Clean up the bb field for the insns between the blocks. */
465 for (x = NEXT_INSN (flow_transfer_insn);
466 x != BB_HEAD (fallthru->dest);
467 x = NEXT_INSN (x))
468 if (!BARRIER_P (x))
469 set_block_for_insn (x, NULL);
472 bb = fallthru->dest;
473 remove_edge (fallthru);
474 flow_transfer_insn = NULL;
475 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
476 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
478 else if (code == BARRIER)
480 /* __builtin_unreachable () may cause a barrier to be emitted in
481 the middle of a BB. We need to split it in the same manner as
482 if the barrier were preceded by a control_flow_insn_p insn. */
483 if (!flow_transfer_insn)
484 flow_transfer_insn = prev_nonnote_insn_bb (insn);
487 if (control_flow_insn_p (insn))
488 flow_transfer_insn = insn;
489 if (insn == end)
490 break;
491 insn = NEXT_INSN (insn);
494 /* In case expander replaced normal insn by sequence terminating by
495 return and barrier, or possibly other sequence not behaving like
496 ordinary jump, we need to take care and move basic block boundary. */
497 if (flow_transfer_insn)
499 BB_END (bb) = flow_transfer_insn;
501 /* Clean up the bb field for the insns that do not belong to BB. */
502 x = flow_transfer_insn;
503 while (x != end)
505 x = NEXT_INSN (x);
506 if (!BARRIER_P (x))
507 set_block_for_insn (x, NULL);
511 /* We've possibly replaced the conditional jump by conditional jump
512 followed by cleanup at fallthru edge, so the outgoing edges may
513 be dead. */
514 purge_dead_edges (bb);
516 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
517 basic block, we might need to kill some edges. */
518 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
519 purge_dead_tablejump_edges (bb, table);
522 /* Assume that frequency of basic block B is known. Compute frequencies
523 and probabilities of outgoing edges. */
525 static void
526 compute_outgoing_frequencies (basic_block b)
528 edge e, f;
529 edge_iterator ei;
531 if (EDGE_COUNT (b->succs) == 2)
533 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
534 int probability;
536 if (note)
538 probability = XINT (note, 0);
539 e = BRANCH_EDGE (b);
540 e->probability = probability;
541 e->count = apply_probability (b->count, probability);
542 f = FALLTHRU_EDGE (b);
543 f->probability = REG_BR_PROB_BASE - probability;
544 f->count = b->count - e->count;
545 return;
547 else
549 guess_outgoing_edge_probabilities (b);
552 else if (single_succ_p (b))
554 e = single_succ_edge (b);
555 e->probability = REG_BR_PROB_BASE;
556 e->count = b->count;
557 return;
559 else
561 /* We rely on BBs with more than two successors to have sane probabilities
562 and do not guess them here. For BBs terminated by switch statements
563 expanded to jump-table jump, we have done the right thing during
564 expansion. For EH edges, we still guess the probabilities here. */
565 bool complex_edge = false;
566 FOR_EACH_EDGE (e, ei, b->succs)
567 if (e->flags & EDGE_COMPLEX)
569 complex_edge = true;
570 break;
572 if (complex_edge)
573 guess_outgoing_edge_probabilities (b);
576 if (b->count)
577 FOR_EACH_EDGE (e, ei, b->succs)
578 e->count = apply_probability (b->count, e->probability);
581 /* Assume that some pass has inserted labels or control flow
582 instructions within a basic block. Split basic blocks as needed
583 and create edges. */
585 void
586 find_many_sub_basic_blocks (sbitmap blocks)
588 basic_block bb, min, max;
590 FOR_EACH_BB_FN (bb, cfun)
591 SET_STATE (bb,
592 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
594 FOR_EACH_BB_FN (bb, cfun)
595 if (STATE (bb) == BLOCK_TO_SPLIT)
596 find_bb_boundaries (bb);
598 FOR_EACH_BB_FN (bb, cfun)
599 if (STATE (bb) != BLOCK_ORIGINAL)
600 break;
602 min = max = bb;
603 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
604 if (STATE (bb) != BLOCK_ORIGINAL)
605 max = bb;
607 /* Now re-scan and wire in all edges. This expect simple (conditional)
608 jumps at the end of each new basic blocks. */
609 make_edges (min, max, 1);
611 /* Update branch probabilities. Expect only (un)conditional jumps
612 to be created with only the forward edges. */
613 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
614 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
616 edge e;
617 edge_iterator ei;
619 if (STATE (bb) == BLOCK_ORIGINAL)
620 continue;
621 if (STATE (bb) == BLOCK_NEW)
623 bb->count = 0;
624 bb->frequency = 0;
625 FOR_EACH_EDGE (e, ei, bb->preds)
627 bb->count += e->count;
628 bb->frequency += EDGE_FREQUENCY (e);
632 compute_outgoing_frequencies (bb);
635 FOR_EACH_BB_FN (bb, cfun)
636 SET_STATE (bb, 0);