2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cfgbuild.c
blob806dcec7b4867db4836abd47da508d1af4db8ad8
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "rtl.h"
30 #include "hard-reg-set.h"
31 #include "predict.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "cfgrtl.h"
36 #include "cfganal.h"
37 #include "cfgbuild.h"
38 #include "basic-block.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "except.h"
42 #include "insn-config.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "calls.h"
47 #include "emit-rtl.h"
48 #include "varasm.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "diagnostic-core.h"
52 #include "timevar.h"
53 #include "sbitmap.h"
55 static void make_edges (basic_block, basic_block, int);
56 static void make_label_edge (sbitmap, basic_block, rtx, int);
57 static void find_bb_boundaries (basic_block);
58 static void compute_outgoing_frequencies (basic_block);
60 /* Return true if insn is something that should be contained inside basic
61 block. */
63 bool
64 inside_basic_block_p (const rtx_insn *insn)
66 switch (GET_CODE (insn))
68 case CODE_LABEL:
69 /* Avoid creating of basic block for jumptables. */
70 return (NEXT_INSN (insn) == 0
71 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
73 case JUMP_INSN:
74 case CALL_INSN:
75 case INSN:
76 case DEBUG_INSN:
77 return true;
79 case JUMP_TABLE_DATA:
80 case BARRIER:
81 case NOTE:
82 return false;
84 default:
85 gcc_unreachable ();
89 /* Return true if INSN may cause control flow transfer, so it should be last in
90 the basic block. */
92 bool
93 control_flow_insn_p (const rtx_insn *insn)
95 switch (GET_CODE (insn))
97 case NOTE:
98 case CODE_LABEL:
99 case DEBUG_INSN:
100 return false;
102 case JUMP_INSN:
103 return true;
105 case CALL_INSN:
106 /* Noreturn and sibling call instructions terminate the basic blocks
107 (but only if they happen unconditionally). */
108 if ((SIBLING_CALL_P (insn)
109 || find_reg_note (insn, REG_NORETURN, 0))
110 && GET_CODE (PATTERN (insn)) != COND_EXEC)
111 return true;
113 /* Call insn may return to the nonlocal goto handler. */
114 if (can_nonlocal_goto (insn))
115 return true;
116 break;
118 case INSN:
119 /* Treat trap instructions like noreturn calls (same provision). */
120 if (GET_CODE (PATTERN (insn)) == TRAP_IF
121 && XEXP (PATTERN (insn), 0) == const1_rtx)
122 return true;
123 if (!cfun->can_throw_non_call_exceptions)
124 return false;
125 break;
127 case JUMP_TABLE_DATA:
128 case BARRIER:
129 /* It is nonsense to reach this when looking for the
130 end of basic block, but before dead code is eliminated
131 this may happen. */
132 return false;
134 default:
135 gcc_unreachable ();
138 return can_throw_internal (insn);
142 /* Create an edge between two basic blocks. FLAGS are auxiliary information
143 about the edge that is accumulated between calls. */
145 /* Create an edge from a basic block to a label. */
147 static void
148 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
150 gcc_assert (LABEL_P (label));
152 /* If the label was never emitted, this insn is junk, but avoid a
153 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
154 as a result of a syntax error and a diagnostic has already been
155 printed. */
157 if (INSN_UID (label) == 0)
158 return;
160 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
163 /* Create the edges generated by INSN in REGION. */
165 void
166 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
168 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
170 if (lp)
172 rtx_insn *label = lp->landing_pad;
174 /* During initial rtl generation, use the post_landing_pad. */
175 if (label == NULL)
177 gcc_assert (lp->post_landing_pad);
178 label = label_rtx (lp->post_landing_pad);
181 make_label_edge (edge_cache, src, label,
182 EDGE_ABNORMAL | EDGE_EH
183 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
187 /* States of basic block as seen by find_many_sub_basic_blocks. */
188 enum state {
189 /* Basic blocks created via split_block belong to this state.
190 make_edges will examine these basic blocks to see if we need to
191 create edges going out of them. */
192 BLOCK_NEW = 0,
194 /* Basic blocks that do not need examining belong to this state.
195 These blocks will be left intact. In particular, make_edges will
196 not create edges going out of these basic blocks. */
197 BLOCK_ORIGINAL,
199 /* Basic blocks that may need splitting (due to a label appearing in
200 the middle, etc) belong to this state. After splitting them,
201 make_edges will create edges going out of them as needed. */
202 BLOCK_TO_SPLIT
205 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
206 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
208 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
209 #define BLOCK_USED_BY_TABLEJUMP 32
210 #define FULL_STATE(BB) ((size_t) (BB)->aux)
212 /* Identify the edges going out of basic blocks between MIN and MAX,
213 inclusive, that have their states set to BLOCK_NEW or
214 BLOCK_TO_SPLIT.
216 UPDATE_P should be nonzero if we are updating CFG and zero if we
217 are building CFG from scratch. */
219 static void
220 make_edges (basic_block min, basic_block max, int update_p)
222 basic_block bb;
223 sbitmap edge_cache = NULL;
225 /* Heavy use of computed goto in machine-generated code can lead to
226 nearly fully-connected CFGs. In that case we spend a significant
227 amount of time searching the edge lists for duplicates. */
228 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
229 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
231 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
232 is always the entry. */
233 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
234 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
236 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
238 rtx_insn *insn;
239 enum rtx_code code;
240 edge e;
241 edge_iterator ei;
243 if (STATE (bb) == BLOCK_ORIGINAL)
244 continue;
246 /* If we have an edge cache, cache edges going out of BB. */
247 if (edge_cache)
249 bitmap_clear (edge_cache);
250 if (update_p)
252 FOR_EACH_EDGE (e, ei, bb->succs)
253 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
254 bitmap_set_bit (edge_cache, e->dest->index);
258 if (LABEL_P (BB_HEAD (bb))
259 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
260 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
262 /* Examine the last instruction of the block, and discover the
263 ways we can leave the block. */
265 insn = BB_END (bb);
266 code = GET_CODE (insn);
268 /* A branch. */
269 if (code == JUMP_INSN)
271 rtx tmp;
272 rtx_jump_table_data *table;
274 /* Recognize a non-local goto as a branch outside the
275 current function. */
276 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
279 /* Recognize a tablejump and do the right thing. */
280 else if (tablejump_p (insn, NULL, &table))
282 rtvec vec = table->get_labels ();
283 int j;
285 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
286 make_label_edge (edge_cache, bb,
287 XEXP (RTVEC_ELT (vec, j), 0), 0);
289 /* Some targets (eg, ARM) emit a conditional jump that also
290 contains the out-of-range target. Scan for these and
291 add an edge if necessary. */
292 if ((tmp = single_set (insn)) != NULL
293 && SET_DEST (tmp) == pc_rtx
294 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
295 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
296 make_label_edge (edge_cache, bb,
297 LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0);
300 /* If this is a computed jump, then mark it as reaching
301 everything on the forced_labels list. */
302 else if (computed_jump_p (insn))
304 for (rtx_insn_list *x = forced_labels; x; x = x->next ())
305 make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL);
308 /* Returns create an exit out. */
309 else if (returnjump_p (insn))
310 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
312 /* Recognize asm goto and do the right thing. */
313 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
315 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
316 for (i = 0; i < n; ++i)
317 make_label_edge (edge_cache, bb,
318 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
321 /* Otherwise, we have a plain conditional or unconditional jump. */
322 else
324 gcc_assert (JUMP_LABEL (insn));
325 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
329 /* If this is a sibling call insn, then this is in effect a combined call
330 and return, and so we need an edge to the exit block. No need to
331 worry about EH edges, since we wouldn't have created the sibling call
332 in the first place. */
333 if (code == CALL_INSN && SIBLING_CALL_P (insn))
334 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
335 EDGE_SIBCALL | EDGE_ABNORMAL);
337 /* If this is a CALL_INSN, then mark it as reaching the active EH
338 handler for this CALL_INSN. If we're handling non-call
339 exceptions then any insn can reach any of the active handlers.
340 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
341 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
343 /* Add any appropriate EH edges. */
344 rtl_make_eh_edge (edge_cache, bb, insn);
346 if (code == CALL_INSN)
348 if (can_nonlocal_goto (insn))
350 /* ??? This could be made smarter: in some cases it's
351 possible to tell that certain calls will not do a
352 nonlocal goto. For example, if the nested functions
353 that do the nonlocal gotos do not have their addresses
354 taken, then only calls to those functions or to other
355 nested functions that use them could possibly do
356 nonlocal gotos. */
357 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
359 x = x->next ())
360 make_label_edge (edge_cache, bb, x->insn (),
361 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
364 if (flag_tm)
366 rtx note;
367 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
368 if (REG_NOTE_KIND (note) == REG_TM)
369 make_label_edge (edge_cache, bb, XEXP (note, 0),
370 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
375 /* Find out if we can drop through to the next block. */
376 insn = NEXT_INSN (insn);
377 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
378 if (e && e->flags & EDGE_FALLTHRU)
379 insn = NULL;
381 while (insn
382 && NOTE_P (insn)
383 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
384 insn = NEXT_INSN (insn);
386 if (!insn)
387 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
388 EDGE_FALLTHRU);
389 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
391 if (insn == BB_HEAD (bb->next_bb))
392 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
396 if (edge_cache)
397 sbitmap_free (edge_cache);
400 static void
401 mark_tablejump_edge (rtx label)
403 basic_block bb;
405 gcc_assert (LABEL_P (label));
406 /* See comment in make_label_edge. */
407 if (INSN_UID (label) == 0)
408 return;
409 bb = BLOCK_FOR_INSN (label);
410 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
413 static void
414 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
416 rtx_insn *insn = BB_END (bb);
417 rtx tmp;
418 rtvec vec;
419 int j;
420 edge_iterator ei;
421 edge e;
423 vec = table->get_labels ();
425 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
426 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
428 /* Some targets (eg, ARM) emit a conditional jump that also
429 contains the out-of-range target. Scan for these and
430 add an edge if necessary. */
431 if ((tmp = single_set (insn)) != NULL
432 && SET_DEST (tmp) == pc_rtx
433 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
434 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
435 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)));
437 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
439 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
440 SET_STATE (e->dest, FULL_STATE (e->dest)
441 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
442 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
444 remove_edge (e);
445 continue;
447 ei_next (&ei);
451 /* Scan basic block BB for possible BB boundaries inside the block
452 and create new basic blocks in the progress. */
454 static void
455 find_bb_boundaries (basic_block bb)
457 basic_block orig_bb = bb;
458 rtx_insn *insn = BB_HEAD (bb);
459 rtx_insn *end = BB_END (bb), *x;
460 rtx_jump_table_data *table;
461 rtx_insn *flow_transfer_insn = NULL;
462 edge fallthru = NULL;
464 if (insn == BB_END (bb))
465 return;
467 if (LABEL_P (insn))
468 insn = NEXT_INSN (insn);
470 /* Scan insn chain and try to find new basic block boundaries. */
471 while (1)
473 enum rtx_code code = GET_CODE (insn);
475 /* In case we've previously seen an insn that effects a control
476 flow transfer, split the block. */
477 if ((flow_transfer_insn || code == CODE_LABEL)
478 && inside_basic_block_p (insn))
480 fallthru = split_block (bb, PREV_INSN (insn));
481 if (flow_transfer_insn)
483 BB_END (bb) = flow_transfer_insn;
485 /* Clean up the bb field for the insns between the blocks. */
486 for (x = NEXT_INSN (flow_transfer_insn);
487 x != BB_HEAD (fallthru->dest);
488 x = NEXT_INSN (x))
489 if (!BARRIER_P (x))
490 set_block_for_insn (x, NULL);
493 bb = fallthru->dest;
494 remove_edge (fallthru);
495 flow_transfer_insn = NULL;
496 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
497 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
499 else if (code == BARRIER)
501 /* __builtin_unreachable () may cause a barrier to be emitted in
502 the middle of a BB. We need to split it in the same manner as
503 if the barrier were preceded by a control_flow_insn_p insn. */
504 if (!flow_transfer_insn)
505 flow_transfer_insn = prev_nonnote_insn_bb (insn);
508 if (control_flow_insn_p (insn))
509 flow_transfer_insn = insn;
510 if (insn == end)
511 break;
512 insn = NEXT_INSN (insn);
515 /* In case expander replaced normal insn by sequence terminating by
516 return and barrier, or possibly other sequence not behaving like
517 ordinary jump, we need to take care and move basic block boundary. */
518 if (flow_transfer_insn)
520 BB_END (bb) = flow_transfer_insn;
522 /* Clean up the bb field for the insns that do not belong to BB. */
523 x = flow_transfer_insn;
524 while (x != end)
526 x = NEXT_INSN (x);
527 if (!BARRIER_P (x))
528 set_block_for_insn (x, NULL);
532 /* We've possibly replaced the conditional jump by conditional jump
533 followed by cleanup at fallthru edge, so the outgoing edges may
534 be dead. */
535 purge_dead_edges (bb);
537 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
538 basic block, we might need to kill some edges. */
539 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
540 purge_dead_tablejump_edges (bb, table);
543 /* Assume that frequency of basic block B is known. Compute frequencies
544 and probabilities of outgoing edges. */
546 static void
547 compute_outgoing_frequencies (basic_block b)
549 edge e, f;
550 edge_iterator ei;
552 if (EDGE_COUNT (b->succs) == 2)
554 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
555 int probability;
557 if (note)
559 probability = XINT (note, 0);
560 e = BRANCH_EDGE (b);
561 e->probability = probability;
562 e->count = apply_probability (b->count, probability);
563 f = FALLTHRU_EDGE (b);
564 f->probability = REG_BR_PROB_BASE - probability;
565 f->count = b->count - e->count;
566 return;
568 else
570 guess_outgoing_edge_probabilities (b);
573 else if (single_succ_p (b))
575 e = single_succ_edge (b);
576 e->probability = REG_BR_PROB_BASE;
577 e->count = b->count;
578 return;
580 else
582 /* We rely on BBs with more than two successors to have sane probabilities
583 and do not guess them here. For BBs terminated by switch statements
584 expanded to jump-table jump, we have done the right thing during
585 expansion. For EH edges, we still guess the probabilities here. */
586 bool complex_edge = false;
587 FOR_EACH_EDGE (e, ei, b->succs)
588 if (e->flags & EDGE_COMPLEX)
590 complex_edge = true;
591 break;
593 if (complex_edge)
594 guess_outgoing_edge_probabilities (b);
597 if (b->count)
598 FOR_EACH_EDGE (e, ei, b->succs)
599 e->count = apply_probability (b->count, e->probability);
602 /* Assume that some pass has inserted labels or control flow
603 instructions within a basic block. Split basic blocks as needed
604 and create edges. */
606 void
607 find_many_sub_basic_blocks (sbitmap blocks)
609 basic_block bb, min, max;
611 FOR_EACH_BB_FN (bb, cfun)
612 SET_STATE (bb,
613 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
615 FOR_EACH_BB_FN (bb, cfun)
616 if (STATE (bb) == BLOCK_TO_SPLIT)
617 find_bb_boundaries (bb);
619 FOR_EACH_BB_FN (bb, cfun)
620 if (STATE (bb) != BLOCK_ORIGINAL)
621 break;
623 min = max = bb;
624 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
625 if (STATE (bb) != BLOCK_ORIGINAL)
626 max = bb;
628 /* Now re-scan and wire in all edges. This expect simple (conditional)
629 jumps at the end of each new basic blocks. */
630 make_edges (min, max, 1);
632 /* Update branch probabilities. Expect only (un)conditional jumps
633 to be created with only the forward edges. */
634 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
635 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
637 edge e;
638 edge_iterator ei;
640 if (STATE (bb) == BLOCK_ORIGINAL)
641 continue;
642 if (STATE (bb) == BLOCK_NEW)
644 bb->count = 0;
645 bb->frequency = 0;
646 FOR_EACH_EDGE (e, ei, bb->preds)
648 bb->count += e->count;
649 bb->frequency += EDGE_FREQUENCY (e);
653 compute_outgoing_frequencies (bb);
656 FOR_EACH_BB_FN (bb, cfun)
657 SET_STATE (bb, 0);