2014-12-22 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cfgbuild.c
blob8732bf9e2dffa562d886286fcdb171a58171fbae
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 "predict.h"
29 #include "vec.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "input.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "cfgrtl.h"
38 #include "cfganal.h"
39 #include "cfgbuild.h"
40 #include "basic-block.h"
41 #include "regs.h"
42 #include "flags.h"
43 #include "except.h"
44 #include "expr.h"
45 #include "diagnostic-core.h"
46 #include "timevar.h"
47 #include "sbitmap.h"
49 static void make_edges (basic_block, basic_block, int);
50 static void make_label_edge (sbitmap, basic_block, rtx, int);
51 static void find_bb_boundaries (basic_block);
52 static void compute_outgoing_frequencies (basic_block);
54 /* Return true if insn is something that should be contained inside basic
55 block. */
57 bool
58 inside_basic_block_p (const rtx_insn *insn)
60 switch (GET_CODE (insn))
62 case CODE_LABEL:
63 /* Avoid creating of basic block for jumptables. */
64 return (NEXT_INSN (insn) == 0
65 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
67 case JUMP_INSN:
68 case CALL_INSN:
69 case INSN:
70 case DEBUG_INSN:
71 return true;
73 case JUMP_TABLE_DATA:
74 case BARRIER:
75 case NOTE:
76 return false;
78 default:
79 gcc_unreachable ();
83 /* Return true if INSN may cause control flow transfer, so it should be last in
84 the basic block. */
86 bool
87 control_flow_insn_p (const rtx_insn *insn)
89 switch (GET_CODE (insn))
91 case NOTE:
92 case CODE_LABEL:
93 case DEBUG_INSN:
94 return false;
96 case JUMP_INSN:
97 return true;
99 case CALL_INSN:
100 /* Noreturn and sibling call instructions terminate the basic blocks
101 (but only if they happen unconditionally). */
102 if ((SIBLING_CALL_P (insn)
103 || find_reg_note (insn, REG_NORETURN, 0))
104 && GET_CODE (PATTERN (insn)) != COND_EXEC)
105 return true;
107 /* Call insn may return to the nonlocal goto handler. */
108 if (can_nonlocal_goto (insn))
109 return true;
110 break;
112 case INSN:
113 /* Treat trap instructions like noreturn calls (same provision). */
114 if (GET_CODE (PATTERN (insn)) == TRAP_IF
115 && XEXP (PATTERN (insn), 0) == const1_rtx)
116 return true;
117 if (!cfun->can_throw_non_call_exceptions)
118 return false;
119 break;
121 case JUMP_TABLE_DATA:
122 case BARRIER:
123 /* It is nonsense to reach this when looking for the
124 end of basic block, but before dead code is eliminated
125 this may happen. */
126 return false;
128 default:
129 gcc_unreachable ();
132 return can_throw_internal (insn);
136 /* Create an edge between two basic blocks. FLAGS are auxiliary information
137 about the edge that is accumulated between calls. */
139 /* Create an edge from a basic block to a label. */
141 static void
142 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
144 gcc_assert (LABEL_P (label));
146 /* If the label was never emitted, this insn is junk, but avoid a
147 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
148 as a result of a syntax error and a diagnostic has already been
149 printed. */
151 if (INSN_UID (label) == 0)
152 return;
154 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
157 /* Create the edges generated by INSN in REGION. */
159 void
160 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
162 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
164 if (lp)
166 rtx label = lp->landing_pad;
168 /* During initial rtl generation, use the post_landing_pad. */
169 if (label == NULL)
171 gcc_assert (lp->post_landing_pad);
172 label = label_rtx (lp->post_landing_pad);
175 make_label_edge (edge_cache, src, label,
176 EDGE_ABNORMAL | EDGE_EH
177 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
181 /* States of basic block as seen by find_many_sub_basic_blocks. */
182 enum state {
183 /* Basic blocks created via split_block belong to this state.
184 make_edges will examine these basic blocks to see if we need to
185 create edges going out of them. */
186 BLOCK_NEW = 0,
188 /* Basic blocks that do not need examining belong to this state.
189 These blocks will be left intact. In particular, make_edges will
190 not create edges going out of these basic blocks. */
191 BLOCK_ORIGINAL,
193 /* Basic blocks that may need splitting (due to a label appearing in
194 the middle, etc) belong to this state. After splitting them,
195 make_edges will create edges going out of them as needed. */
196 BLOCK_TO_SPLIT
199 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
200 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
202 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
203 #define BLOCK_USED_BY_TABLEJUMP 32
204 #define FULL_STATE(BB) ((size_t) (BB)->aux)
206 /* Identify the edges going out of basic blocks between MIN and MAX,
207 inclusive, that have their states set to BLOCK_NEW or
208 BLOCK_TO_SPLIT.
210 UPDATE_P should be nonzero if we are updating CFG and zero if we
211 are building CFG from scratch. */
213 static void
214 make_edges (basic_block min, basic_block max, int update_p)
216 basic_block bb;
217 sbitmap edge_cache = NULL;
219 /* Heavy use of computed goto in machine-generated code can lead to
220 nearly fully-connected CFGs. In that case we spend a significant
221 amount of time searching the edge lists for duplicates. */
222 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
223 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
225 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
226 is always the entry. */
227 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
228 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
230 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
232 rtx_insn *insn;
233 enum rtx_code code;
234 edge e;
235 edge_iterator ei;
237 if (STATE (bb) == BLOCK_ORIGINAL)
238 continue;
240 /* If we have an edge cache, cache edges going out of BB. */
241 if (edge_cache)
243 bitmap_clear (edge_cache);
244 if (update_p)
246 FOR_EACH_EDGE (e, ei, bb->succs)
247 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
248 bitmap_set_bit (edge_cache, e->dest->index);
252 if (LABEL_P (BB_HEAD (bb))
253 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
254 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
256 /* Examine the last instruction of the block, and discover the
257 ways we can leave the block. */
259 insn = BB_END (bb);
260 code = GET_CODE (insn);
262 /* A branch. */
263 if (code == JUMP_INSN)
265 rtx tmp;
266 rtx_jump_table_data *table;
268 /* Recognize a non-local goto as a branch outside the
269 current function. */
270 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
273 /* Recognize a tablejump and do the right thing. */
274 else if (tablejump_p (insn, NULL, &table))
276 rtvec vec = table->get_labels ();
277 int j;
279 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
280 make_label_edge (edge_cache, bb,
281 XEXP (RTVEC_ELT (vec, j), 0), 0);
283 /* Some targets (eg, ARM) emit a conditional jump that also
284 contains the out-of-range target. Scan for these and
285 add an edge if necessary. */
286 if ((tmp = single_set (insn)) != NULL
287 && SET_DEST (tmp) == pc_rtx
288 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
289 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
290 make_label_edge (edge_cache, bb,
291 LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0);
294 /* If this is a computed jump, then mark it as reaching
295 everything on the forced_labels list. */
296 else if (computed_jump_p (insn))
298 for (rtx_insn_list *x = forced_labels; x; x = x->next ())
299 make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL);
302 /* Returns create an exit out. */
303 else if (returnjump_p (insn))
304 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
306 /* Recognize asm goto and do the right thing. */
307 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
309 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
310 for (i = 0; i < n; ++i)
311 make_label_edge (edge_cache, bb,
312 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
315 /* Otherwise, we have a plain conditional or unconditional jump. */
316 else
318 gcc_assert (JUMP_LABEL (insn));
319 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
323 /* If this is a sibling call insn, then this is in effect a combined call
324 and return, and so we need an edge to the exit block. No need to
325 worry about EH edges, since we wouldn't have created the sibling call
326 in the first place. */
327 if (code == CALL_INSN && SIBLING_CALL_P (insn))
328 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
329 EDGE_SIBCALL | EDGE_ABNORMAL);
331 /* If this is a CALL_INSN, then mark it as reaching the active EH
332 handler for this CALL_INSN. If we're handling non-call
333 exceptions then any insn can reach any of the active handlers.
334 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
335 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
337 /* Add any appropriate EH edges. */
338 rtl_make_eh_edge (edge_cache, bb, insn);
340 if (code == CALL_INSN)
342 if (can_nonlocal_goto (insn))
344 /* ??? This could be made smarter: in some cases it's
345 possible to tell that certain calls will not do a
346 nonlocal goto. For example, if the nested functions
347 that do the nonlocal gotos do not have their addresses
348 taken, then only calls to those functions or to other
349 nested functions that use them could possibly do
350 nonlocal gotos. */
351 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
353 x = x->next ())
354 make_label_edge (edge_cache, bb, x->insn (),
355 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
358 if (flag_tm)
360 rtx note;
361 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
362 if (REG_NOTE_KIND (note) == REG_TM)
363 make_label_edge (edge_cache, bb, XEXP (note, 0),
364 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
369 /* Find out if we can drop through to the next block. */
370 insn = NEXT_INSN (insn);
371 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
372 if (e && e->flags & EDGE_FALLTHRU)
373 insn = NULL;
375 while (insn
376 && NOTE_P (insn)
377 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
378 insn = NEXT_INSN (insn);
380 if (!insn)
381 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
382 EDGE_FALLTHRU);
383 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
385 if (insn == BB_HEAD (bb->next_bb))
386 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
390 if (edge_cache)
391 sbitmap_free (edge_cache);
394 static void
395 mark_tablejump_edge (rtx label)
397 basic_block bb;
399 gcc_assert (LABEL_P (label));
400 /* See comment in make_label_edge. */
401 if (INSN_UID (label) == 0)
402 return;
403 bb = BLOCK_FOR_INSN (label);
404 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
407 static void
408 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
410 rtx_insn *insn = BB_END (bb);
411 rtx tmp;
412 rtvec vec;
413 int j;
414 edge_iterator ei;
415 edge e;
417 vec = table->get_labels ();
419 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
420 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
422 /* Some targets (eg, ARM) emit a conditional jump that also
423 contains the out-of-range target. Scan for these and
424 add an edge if necessary. */
425 if ((tmp = single_set (insn)) != NULL
426 && SET_DEST (tmp) == pc_rtx
427 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
428 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
429 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)));
431 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
433 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
434 SET_STATE (e->dest, FULL_STATE (e->dest)
435 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
436 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
438 remove_edge (e);
439 continue;
441 ei_next (&ei);
445 /* Scan basic block BB for possible BB boundaries inside the block
446 and create new basic blocks in the progress. */
448 static void
449 find_bb_boundaries (basic_block bb)
451 basic_block orig_bb = bb;
452 rtx_insn *insn = BB_HEAD (bb);
453 rtx_insn *end = BB_END (bb), *x;
454 rtx_jump_table_data *table;
455 rtx_insn *flow_transfer_insn = NULL;
456 edge fallthru = NULL;
458 if (insn == BB_END (bb))
459 return;
461 if (LABEL_P (insn))
462 insn = NEXT_INSN (insn);
464 /* Scan insn chain and try to find new basic block boundaries. */
465 while (1)
467 enum rtx_code code = GET_CODE (insn);
469 /* In case we've previously seen an insn that effects a control
470 flow transfer, split the block. */
471 if ((flow_transfer_insn || code == CODE_LABEL)
472 && inside_basic_block_p (insn))
474 fallthru = split_block (bb, PREV_INSN (insn));
475 if (flow_transfer_insn)
477 BB_END (bb) = flow_transfer_insn;
479 /* Clean up the bb field for the insns between the blocks. */
480 for (x = NEXT_INSN (flow_transfer_insn);
481 x != BB_HEAD (fallthru->dest);
482 x = NEXT_INSN (x))
483 if (!BARRIER_P (x))
484 set_block_for_insn (x, NULL);
487 bb = fallthru->dest;
488 remove_edge (fallthru);
489 flow_transfer_insn = NULL;
490 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
491 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
493 else if (code == BARRIER)
495 /* __builtin_unreachable () may cause a barrier to be emitted in
496 the middle of a BB. We need to split it in the same manner as
497 if the barrier were preceded by a control_flow_insn_p insn. */
498 if (!flow_transfer_insn)
499 flow_transfer_insn = prev_nonnote_insn_bb (insn);
502 if (control_flow_insn_p (insn))
503 flow_transfer_insn = insn;
504 if (insn == end)
505 break;
506 insn = NEXT_INSN (insn);
509 /* In case expander replaced normal insn by sequence terminating by
510 return and barrier, or possibly other sequence not behaving like
511 ordinary jump, we need to take care and move basic block boundary. */
512 if (flow_transfer_insn)
514 BB_END (bb) = flow_transfer_insn;
516 /* Clean up the bb field for the insns that do not belong to BB. */
517 x = flow_transfer_insn;
518 while (x != end)
520 x = NEXT_INSN (x);
521 if (!BARRIER_P (x))
522 set_block_for_insn (x, NULL);
526 /* We've possibly replaced the conditional jump by conditional jump
527 followed by cleanup at fallthru edge, so the outgoing edges may
528 be dead. */
529 purge_dead_edges (bb);
531 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
532 basic block, we might need to kill some edges. */
533 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
534 purge_dead_tablejump_edges (bb, table);
537 /* Assume that frequency of basic block B is known. Compute frequencies
538 and probabilities of outgoing edges. */
540 static void
541 compute_outgoing_frequencies (basic_block b)
543 edge e, f;
544 edge_iterator ei;
546 if (EDGE_COUNT (b->succs) == 2)
548 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
549 int probability;
551 if (note)
553 probability = XINT (note, 0);
554 e = BRANCH_EDGE (b);
555 e->probability = probability;
556 e->count = apply_probability (b->count, probability);
557 f = FALLTHRU_EDGE (b);
558 f->probability = REG_BR_PROB_BASE - probability;
559 f->count = b->count - e->count;
560 return;
562 else
564 guess_outgoing_edge_probabilities (b);
567 else if (single_succ_p (b))
569 e = single_succ_edge (b);
570 e->probability = REG_BR_PROB_BASE;
571 e->count = b->count;
572 return;
574 else
576 /* We rely on BBs with more than two successors to have sane probabilities
577 and do not guess them here. For BBs terminated by switch statements
578 expanded to jump-table jump, we have done the right thing during
579 expansion. For EH edges, we still guess the probabilities here. */
580 bool complex_edge = false;
581 FOR_EACH_EDGE (e, ei, b->succs)
582 if (e->flags & EDGE_COMPLEX)
584 complex_edge = true;
585 break;
587 if (complex_edge)
588 guess_outgoing_edge_probabilities (b);
591 if (b->count)
592 FOR_EACH_EDGE (e, ei, b->succs)
593 e->count = apply_probability (b->count, e->probability);
596 /* Assume that some pass has inserted labels or control flow
597 instructions within a basic block. Split basic blocks as needed
598 and create edges. */
600 void
601 find_many_sub_basic_blocks (sbitmap blocks)
603 basic_block bb, min, max;
605 FOR_EACH_BB_FN (bb, cfun)
606 SET_STATE (bb,
607 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
609 FOR_EACH_BB_FN (bb, cfun)
610 if (STATE (bb) == BLOCK_TO_SPLIT)
611 find_bb_boundaries (bb);
613 FOR_EACH_BB_FN (bb, cfun)
614 if (STATE (bb) != BLOCK_ORIGINAL)
615 break;
617 min = max = bb;
618 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
619 if (STATE (bb) != BLOCK_ORIGINAL)
620 max = bb;
622 /* Now re-scan and wire in all edges. This expect simple (conditional)
623 jumps at the end of each new basic blocks. */
624 make_edges (min, max, 1);
626 /* Update branch probabilities. Expect only (un)conditional jumps
627 to be created with only the forward edges. */
628 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
629 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
631 edge e;
632 edge_iterator ei;
634 if (STATE (bb) == BLOCK_ORIGINAL)
635 continue;
636 if (STATE (bb) == BLOCK_NEW)
638 bb->count = 0;
639 bb->frequency = 0;
640 FOR_EACH_EDGE (e, ei, bb->preds)
642 bb->count += e->count;
643 bb->frequency += EDGE_FREQUENCY (e);
647 compute_outgoing_frequencies (bb);
650 FOR_EACH_BB_FN (bb, cfun)
651 SET_STATE (bb, 0);