Mark ChangeLog
[official-gcc.git] / gcc / cfgbuild.c
blobc47aad0c12fff27de0fb54693606cd554b3b0abb
1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2013 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 "function.h"
32 #include "except.h"
33 #include "expr.h"
34 #include "diagnostic-core.h"
35 #include "timevar.h"
36 #include "sbitmap.h"
38 static void make_edges (basic_block, basic_block, int);
39 static void make_label_edge (sbitmap, basic_block, rtx, int);
40 static void find_bb_boundaries (basic_block);
41 static void compute_outgoing_frequencies (basic_block);
43 /* Return true if insn is something that should be contained inside basic
44 block. */
46 bool
47 inside_basic_block_p (const_rtx insn)
49 switch (GET_CODE (insn))
51 case CODE_LABEL:
52 /* Avoid creating of basic block for jumptables. */
53 return (NEXT_INSN (insn) == 0
54 || !JUMP_P (NEXT_INSN (insn))
55 || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
56 && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
58 case JUMP_INSN:
59 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
60 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
62 case CALL_INSN:
63 case INSN:
64 case DEBUG_INSN:
65 return true;
67 case BARRIER:
68 case NOTE:
69 return false;
71 default:
72 gcc_unreachable ();
76 /* Return true if INSN may cause control flow transfer, so it should be last in
77 the basic block. */
79 bool
80 control_flow_insn_p (const_rtx insn)
82 switch (GET_CODE (insn))
84 case NOTE:
85 case CODE_LABEL:
86 case DEBUG_INSN:
87 return false;
89 case JUMP_INSN:
90 /* Jump insn always causes control transfer except for tablejumps. */
91 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
92 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
94 case CALL_INSN:
95 /* Noreturn and sibling call instructions terminate the basic blocks
96 (but only if they happen unconditionally). */
97 if ((SIBLING_CALL_P (insn)
98 || find_reg_note (insn, REG_NORETURN, 0))
99 && GET_CODE (PATTERN (insn)) != COND_EXEC)
100 return true;
102 /* Call insn may return to the nonlocal goto handler. */
103 if (can_nonlocal_goto (insn))
104 return true;
105 break;
107 case INSN:
108 /* Treat trap instructions like noreturn calls (same provision). */
109 if (GET_CODE (PATTERN (insn)) == TRAP_IF
110 && XEXP (PATTERN (insn), 0) == const1_rtx)
111 return true;
112 if (!cfun->can_throw_non_call_exceptions)
113 return false;
114 break;
116 case BARRIER:
117 /* It is nonsense to reach barrier 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);
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->next_bb)
222 make_edge (ENTRY_BLOCK_PTR, min, EDGE_FALLTHRU);
224 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
226 rtx insn, x;
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)
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, 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;
261 /* Recognize a non-local goto as a branch outside the
262 current function. */
263 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
266 /* Recognize a tablejump and do the right thing. */
267 else if (tablejump_p (insn, NULL, &tmp))
269 rtvec vec;
270 int j;
272 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
273 vec = XVEC (PATTERN (tmp), 0);
274 else
275 vec = XVEC (PATTERN (tmp), 1);
277 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
278 make_label_edge (edge_cache, bb,
279 XEXP (RTVEC_ELT (vec, j), 0), 0);
281 /* Some targets (eg, ARM) emit a conditional jump that also
282 contains the out-of-range target. Scan for these and
283 add an edge if necessary. */
284 if ((tmp = single_set (insn)) != NULL
285 && SET_DEST (tmp) == pc_rtx
286 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
287 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
288 make_label_edge (edge_cache, bb,
289 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
292 /* If this is a computed jump, then mark it as reaching
293 everything on the forced_labels list. */
294 else if (computed_jump_p (insn))
296 for (x = forced_labels; x; x = XEXP (x, 1))
297 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
300 /* Returns create an exit out. */
301 else if (returnjump_p (insn))
302 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
304 /* Recognize asm goto and do the right thing. */
305 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
307 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
308 for (i = 0; i < n; ++i)
309 make_label_edge (edge_cache, bb,
310 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
313 /* Otherwise, we have a plain conditional or unconditional jump. */
314 else
316 gcc_assert (JUMP_LABEL (insn));
317 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
321 /* If this is a sibling call insn, then this is in effect a combined call
322 and return, and so we need an edge to the exit block. No need to
323 worry about EH edges, since we wouldn't have created the sibling call
324 in the first place. */
325 if (code == CALL_INSN && SIBLING_CALL_P (insn))
326 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
327 EDGE_SIBCALL | EDGE_ABNORMAL);
329 /* If this is a CALL_INSN, then mark it as reaching the active EH
330 handler for this CALL_INSN. If we're handling non-call
331 exceptions then any insn can reach any of the active handlers.
332 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
333 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
335 /* Add any appropriate EH edges. */
336 rtl_make_eh_edge (edge_cache, bb, insn);
338 if (code == CALL_INSN)
340 if (can_nonlocal_goto (insn))
342 /* ??? This could be made smarter: in some cases it's
343 possible to tell that certain calls will not do a
344 nonlocal goto. For example, if the nested functions
345 that do the nonlocal gotos do not have their addresses
346 taken, then only calls to those functions or to other
347 nested functions that use them could possibly do
348 nonlocal gotos. */
349 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
350 make_label_edge (edge_cache, bb, XEXP (x, 0),
351 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
354 if (flag_tm)
356 rtx note;
357 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
358 if (REG_NOTE_KIND (note) == REG_TM)
359 make_label_edge (edge_cache, bb, XEXP (note, 0),
360 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
365 /* Find out if we can drop through to the next block. */
366 insn = NEXT_INSN (insn);
367 e = find_edge (bb, EXIT_BLOCK_PTR);
368 if (e && e->flags & EDGE_FALLTHRU)
369 insn = NULL;
371 while (insn
372 && NOTE_P (insn)
373 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
374 insn = NEXT_INSN (insn);
376 if (!insn)
377 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
378 else if (bb->next_bb != EXIT_BLOCK_PTR)
380 if (insn == BB_HEAD (bb->next_bb))
381 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
385 if (edge_cache)
386 sbitmap_free (edge_cache);
389 static void
390 mark_tablejump_edge (rtx label)
392 basic_block bb;
394 gcc_assert (LABEL_P (label));
395 /* See comment in make_label_edge. */
396 if (INSN_UID (label) == 0)
397 return;
398 bb = BLOCK_FOR_INSN (label);
399 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
402 static void
403 purge_dead_tablejump_edges (basic_block bb, rtx table)
405 rtx insn = BB_END (bb), tmp;
406 rtvec vec;
407 int j;
408 edge_iterator ei;
409 edge e;
411 if (GET_CODE (PATTERN (table)) == ADDR_VEC)
412 vec = XVEC (PATTERN (table), 0);
413 else
414 vec = XVEC (PATTERN (table), 1);
416 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
417 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
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)
426 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
428 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
430 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
431 SET_STATE (e->dest, FULL_STATE (e->dest)
432 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
433 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
435 remove_edge (e);
436 continue;
438 ei_next (&ei);
442 /* Scan basic block BB for possible BB boundaries inside the block
443 and create new basic blocks in the progress. */
445 static void
446 find_bb_boundaries (basic_block bb)
448 basic_block orig_bb = bb;
449 rtx insn = BB_HEAD (bb);
450 rtx end = BB_END (bb), x;
451 rtx table;
452 rtx flow_transfer_insn = NULL_RTX;
453 edge fallthru = NULL;
455 if (insn == BB_END (bb))
456 return;
458 if (LABEL_P (insn))
459 insn = NEXT_INSN (insn);
461 /* Scan insn chain and try to find new basic block boundaries. */
462 while (1)
464 enum rtx_code code = GET_CODE (insn);
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))
471 fallthru = split_block (bb, PREV_INSN (insn));
472 if (flow_transfer_insn)
474 BB_END (bb) = flow_transfer_insn;
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);
484 bb = fallthru->dest;
485 remove_edge (fallthru);
486 flow_transfer_insn = NULL_RTX;
487 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
488 make_edge (ENTRY_BLOCK_PTR, bb, 0);
490 else if (code == BARRIER)
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);
499 if (control_flow_insn_p (insn))
500 flow_transfer_insn = insn;
501 if (insn == end)
502 break;
503 insn = NEXT_INSN (insn);
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. */
509 if (flow_transfer_insn)
511 BB_END (bb) = flow_transfer_insn;
513 /* Clean up the bb field for the insns that do not belong to BB. */
514 x = flow_transfer_insn;
515 while (x != end)
517 x = NEXT_INSN (x);
518 if (!BARRIER_P (x))
519 set_block_for_insn (x, NULL);
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);
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);
534 /* Assume that frequency of basic block B is known. Compute frequencies
535 and probabilities of outgoing edges. */
537 static void
538 compute_outgoing_frequencies (basic_block b)
540 edge e, f;
541 edge_iterator ei;
543 if (EDGE_COUNT (b->succs) == 2)
545 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
546 int probability;
548 if (note)
550 probability = INTVAL (XEXP (note, 0));
551 e = BRANCH_EDGE (b);
552 e->probability = probability;
553 e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
554 / REG_BR_PROB_BASE);
555 f = FALLTHRU_EDGE (b);
556 f->probability = REG_BR_PROB_BASE - probability;
557 f->count = b->count - e->count;
558 return;
560 else
562 guess_outgoing_edge_probabilities (b);
565 else if (single_succ_p (b))
567 e = single_succ_edge (b);
568 e->probability = REG_BR_PROB_BASE;
569 e->count = b->count;
570 return;
572 else
574 /* We rely on BBs with more than two successors to have sane probabilities
575 and do not guess them here. For BBs terminated by switch statements
576 expanded to jump-table jump, we have done the right thing during
577 expansion. For EH edges, we still guess the probabilities here. */
578 bool complex_edge = false;
579 FOR_EACH_EDGE (e, ei, b->succs)
580 if (e->flags & EDGE_COMPLEX)
582 complex_edge = true;
583 break;
585 if (complex_edge)
586 guess_outgoing_edge_probabilities (b);
589 if (b->count)
590 FOR_EACH_EDGE (e, ei, b->succs)
591 e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
592 / REG_BR_PROB_BASE);
595 /* Assume that some pass has inserted labels or control flow
596 instructions within a basic block. Split basic blocks as needed
597 and create edges. */
599 void
600 find_many_sub_basic_blocks (sbitmap blocks)
602 basic_block bb, min, max;
604 FOR_EACH_BB (bb)
605 SET_STATE (bb,
606 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
608 FOR_EACH_BB (bb)
609 if (STATE (bb) == BLOCK_TO_SPLIT)
610 find_bb_boundaries (bb);
612 FOR_EACH_BB (bb)
613 if (STATE (bb) != BLOCK_ORIGINAL)
614 break;
616 min = max = bb;
617 for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
618 if (STATE (bb) != BLOCK_ORIGINAL)
619 max = bb;
621 /* Now re-scan and wire in all edges. This expect simple (conditional)
622 jumps at the end of each new basic blocks. */
623 make_edges (min, max, 1);
625 /* Update branch probabilities. Expect only (un)conditional jumps
626 to be created with only the forward edges. */
627 if (profile_status != PROFILE_ABSENT)
628 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
630 edge e;
631 edge_iterator ei;
633 if (STATE (bb) == BLOCK_ORIGINAL)
634 continue;
635 if (STATE (bb) == BLOCK_NEW)
637 bb->count = 0;
638 bb->frequency = 0;
639 FOR_EACH_EDGE (e, ei, bb->preds)
641 bb->count += e->count;
642 bb->frequency += EDGE_FREQUENCY (e);
646 compute_outgoing_frequencies (bb);
649 FOR_EACH_BB (bb)
650 SET_STATE (bb, 0);