Use rtx_expr_list for expr_status.x_forced_labels
[official-gcc.git] / gcc / cfgbuild.c
blob05adac0cbc4d1b1df26d3467a7436093882e6ce2
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 "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_TABLE_DATA_P (NEXT_INSN (insn)));
56 case JUMP_INSN:
57 case CALL_INSN:
58 case INSN:
59 case DEBUG_INSN:
60 return true;
62 case JUMP_TABLE_DATA:
63 case BARRIER:
64 case NOTE:
65 return false;
67 default:
68 gcc_unreachable ();
72 /* Return true if INSN may cause control flow transfer, so it should be last in
73 the basic block. */
75 bool
76 control_flow_insn_p (const_rtx insn)
78 switch (GET_CODE (insn))
80 case NOTE:
81 case CODE_LABEL:
82 case DEBUG_INSN:
83 return false;
85 case JUMP_INSN:
86 return true;
88 case CALL_INSN:
89 /* Noreturn and sibling call instructions terminate the basic blocks
90 (but only if they happen unconditionally). */
91 if ((SIBLING_CALL_P (insn)
92 || find_reg_note (insn, REG_NORETURN, 0))
93 && GET_CODE (PATTERN (insn)) != COND_EXEC)
94 return true;
96 /* Call insn may return to the nonlocal goto handler. */
97 if (can_nonlocal_goto (insn))
98 return true;
99 break;
101 case INSN:
102 /* Treat trap instructions like noreturn calls (same provision). */
103 if (GET_CODE (PATTERN (insn)) == TRAP_IF
104 && XEXP (PATTERN (insn), 0) == const1_rtx)
105 return true;
106 if (!cfun->can_throw_non_call_exceptions)
107 return false;
108 break;
110 case JUMP_TABLE_DATA:
111 case BARRIER:
112 /* It is nonsense to reach this when looking for the
113 end of basic block, but before dead code is eliminated
114 this may happen. */
115 return false;
117 default:
118 gcc_unreachable ();
121 return can_throw_internal (insn);
125 /* Create an edge between two basic blocks. FLAGS are auxiliary information
126 about the edge that is accumulated between calls. */
128 /* Create an edge from a basic block to a label. */
130 static void
131 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
133 gcc_assert (LABEL_P (label));
135 /* If the label was never emitted, this insn is junk, but avoid a
136 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
137 as a result of a syntax error and a diagnostic has already been
138 printed. */
140 if (INSN_UID (label) == 0)
141 return;
143 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
146 /* Create the edges generated by INSN in REGION. */
148 void
149 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
151 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
153 if (lp)
155 rtx label = lp->landing_pad;
157 /* During initial rtl generation, use the post_landing_pad. */
158 if (label == NULL)
160 gcc_assert (lp->post_landing_pad);
161 label = label_rtx (lp->post_landing_pad);
164 make_label_edge (edge_cache, src, label,
165 EDGE_ABNORMAL | EDGE_EH
166 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
170 /* States of basic block as seen by find_many_sub_basic_blocks. */
171 enum state {
172 /* Basic blocks created via split_block belong to this state.
173 make_edges will examine these basic blocks to see if we need to
174 create edges going out of them. */
175 BLOCK_NEW = 0,
177 /* Basic blocks that do not need examining belong to this state.
178 These blocks will be left intact. In particular, make_edges will
179 not create edges going out of these basic blocks. */
180 BLOCK_ORIGINAL,
182 /* Basic blocks that may need splitting (due to a label appearing in
183 the middle, etc) belong to this state. After splitting them,
184 make_edges will create edges going out of them as needed. */
185 BLOCK_TO_SPLIT
188 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
189 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
191 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
192 #define BLOCK_USED_BY_TABLEJUMP 32
193 #define FULL_STATE(BB) ((size_t) (BB)->aux)
195 /* Identify the edges going out of basic blocks between MIN and MAX,
196 inclusive, that have their states set to BLOCK_NEW or
197 BLOCK_TO_SPLIT.
199 UPDATE_P should be nonzero if we are updating CFG and zero if we
200 are building CFG from scratch. */
202 static void
203 make_edges (basic_block min, basic_block max, int update_p)
205 basic_block bb;
206 sbitmap edge_cache = NULL;
208 /* Heavy use of computed goto in machine-generated code can lead to
209 nearly fully-connected CFGs. In that case we spend a significant
210 amount of time searching the edge lists for duplicates. */
211 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
212 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
214 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
215 is always the entry. */
216 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
217 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
219 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
221 rtx_insn *insn;
222 enum rtx_code code;
223 edge e;
224 edge_iterator ei;
226 if (STATE (bb) == BLOCK_ORIGINAL)
227 continue;
229 /* If we have an edge cache, cache edges going out of BB. */
230 if (edge_cache)
232 bitmap_clear (edge_cache);
233 if (update_p)
235 FOR_EACH_EDGE (e, ei, bb->succs)
236 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
237 bitmap_set_bit (edge_cache, e->dest->index);
241 if (LABEL_P (BB_HEAD (bb))
242 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
243 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
245 /* Examine the last instruction of the block, and discover the
246 ways we can leave the block. */
248 insn = BB_END (bb);
249 code = GET_CODE (insn);
251 /* A branch. */
252 if (code == JUMP_INSN)
254 rtx tmp;
255 rtx_jump_table_data *table;
257 /* Recognize a non-local goto as a branch outside the
258 current function. */
259 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
262 /* Recognize a tablejump and do the right thing. */
263 else if (tablejump_p (insn, NULL, &table))
265 rtvec vec = table->get_labels ();
266 int j;
268 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
269 make_label_edge (edge_cache, bb,
270 XEXP (RTVEC_ELT (vec, j), 0), 0);
272 /* Some targets (eg, ARM) emit a conditional jump that also
273 contains the out-of-range target. Scan for these and
274 add an edge if necessary. */
275 if ((tmp = single_set (insn)) != NULL
276 && SET_DEST (tmp) == pc_rtx
277 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
278 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
279 make_label_edge (edge_cache, bb,
280 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
283 /* If this is a computed jump, then mark it as reaching
284 everything on the forced_labels list. */
285 else if (computed_jump_p (insn))
287 for (rtx_expr_list *x = forced_labels; x; x = x->next ())
288 make_label_edge (edge_cache, bb, x->element (), EDGE_ABNORMAL);
291 /* Returns create an exit out. */
292 else if (returnjump_p (insn))
293 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
295 /* Recognize asm goto and do the right thing. */
296 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
298 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
299 for (i = 0; i < n; ++i)
300 make_label_edge (edge_cache, bb,
301 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
304 /* Otherwise, we have a plain conditional or unconditional jump. */
305 else
307 gcc_assert (JUMP_LABEL (insn));
308 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
312 /* If this is a sibling call insn, then this is in effect a combined call
313 and return, and so we need an edge to the exit block. No need to
314 worry about EH edges, since we wouldn't have created the sibling call
315 in the first place. */
316 if (code == CALL_INSN && SIBLING_CALL_P (insn))
317 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
318 EDGE_SIBCALL | EDGE_ABNORMAL);
320 /* If this is a CALL_INSN, then mark it as reaching the active EH
321 handler for this CALL_INSN. If we're handling non-call
322 exceptions then any insn can reach any of the active handlers.
323 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
324 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
326 /* Add any appropriate EH edges. */
327 rtl_make_eh_edge (edge_cache, bb, insn);
329 if (code == CALL_INSN)
331 if (can_nonlocal_goto (insn))
333 /* ??? This could be made smarter: in some cases it's
334 possible to tell that certain calls will not do a
335 nonlocal goto. For example, if the nested functions
336 that do the nonlocal gotos do not have their addresses
337 taken, then only calls to those functions or to other
338 nested functions that use them could possibly do
339 nonlocal gotos. */
340 for (rtx x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
341 make_label_edge (edge_cache, bb, XEXP (x, 0),
342 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
345 if (flag_tm)
347 rtx note;
348 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
349 if (REG_NOTE_KIND (note) == REG_TM)
350 make_label_edge (edge_cache, bb, XEXP (note, 0),
351 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
356 /* Find out if we can drop through to the next block. */
357 insn = NEXT_INSN (insn);
358 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
359 if (e && e->flags & EDGE_FALLTHRU)
360 insn = NULL;
362 while (insn
363 && NOTE_P (insn)
364 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
365 insn = NEXT_INSN (insn);
367 if (!insn)
368 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
369 EDGE_FALLTHRU);
370 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
372 if (insn == BB_HEAD (bb->next_bb))
373 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
377 if (edge_cache)
378 sbitmap_free (edge_cache);
381 static void
382 mark_tablejump_edge (rtx label)
384 basic_block bb;
386 gcc_assert (LABEL_P (label));
387 /* See comment in make_label_edge. */
388 if (INSN_UID (label) == 0)
389 return;
390 bb = BLOCK_FOR_INSN (label);
391 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
394 static void
395 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
397 rtx_insn *insn = BB_END (bb);
398 rtx tmp;
399 rtvec vec;
400 int j;
401 edge_iterator ei;
402 edge e;
404 vec = table->get_labels ();
406 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
407 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
409 /* Some targets (eg, ARM) emit a conditional jump that also
410 contains the out-of-range target. Scan for these and
411 add an edge if necessary. */
412 if ((tmp = single_set (insn)) != NULL
413 && SET_DEST (tmp) == pc_rtx
414 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
415 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
416 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
418 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
420 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
421 SET_STATE (e->dest, FULL_STATE (e->dest)
422 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
423 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
425 remove_edge (e);
426 continue;
428 ei_next (&ei);
432 /* Scan basic block BB for possible BB boundaries inside the block
433 and create new basic blocks in the progress. */
435 static void
436 find_bb_boundaries (basic_block bb)
438 basic_block orig_bb = bb;
439 rtx_insn *insn = BB_HEAD (bb);
440 rtx_insn *end = BB_END (bb), *x;
441 rtx_jump_table_data *table;
442 rtx_insn *flow_transfer_insn = NULL;
443 edge fallthru = NULL;
445 if (insn == BB_END (bb))
446 return;
448 if (LABEL_P (insn))
449 insn = NEXT_INSN (insn);
451 /* Scan insn chain and try to find new basic block boundaries. */
452 while (1)
454 enum rtx_code code = GET_CODE (insn);
456 /* In case we've previously seen an insn that effects a control
457 flow transfer, split the block. */
458 if ((flow_transfer_insn || code == CODE_LABEL)
459 && inside_basic_block_p (insn))
461 fallthru = split_block (bb, PREV_INSN (insn));
462 if (flow_transfer_insn)
464 BB_END (bb) = flow_transfer_insn;
466 /* Clean up the bb field for the insns between the blocks. */
467 for (x = NEXT_INSN (flow_transfer_insn);
468 x != BB_HEAD (fallthru->dest);
469 x = NEXT_INSN (x))
470 if (!BARRIER_P (x))
471 set_block_for_insn (x, NULL);
474 bb = fallthru->dest;
475 remove_edge (fallthru);
476 flow_transfer_insn = NULL;
477 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
478 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
480 else if (code == BARRIER)
482 /* __builtin_unreachable () may cause a barrier to be emitted in
483 the middle of a BB. We need to split it in the same manner as
484 if the barrier were preceded by a control_flow_insn_p insn. */
485 if (!flow_transfer_insn)
486 flow_transfer_insn = prev_nonnote_insn_bb (insn);
489 if (control_flow_insn_p (insn))
490 flow_transfer_insn = insn;
491 if (insn == end)
492 break;
493 insn = NEXT_INSN (insn);
496 /* In case expander replaced normal insn by sequence terminating by
497 return and barrier, or possibly other sequence not behaving like
498 ordinary jump, we need to take care and move basic block boundary. */
499 if (flow_transfer_insn)
501 BB_END (bb) = flow_transfer_insn;
503 /* Clean up the bb field for the insns that do not belong to BB. */
504 x = flow_transfer_insn;
505 while (x != end)
507 x = NEXT_INSN (x);
508 if (!BARRIER_P (x))
509 set_block_for_insn (x, NULL);
513 /* We've possibly replaced the conditional jump by conditional jump
514 followed by cleanup at fallthru edge, so the outgoing edges may
515 be dead. */
516 purge_dead_edges (bb);
518 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
519 basic block, we might need to kill some edges. */
520 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
521 purge_dead_tablejump_edges (bb, table);
524 /* Assume that frequency of basic block B is known. Compute frequencies
525 and probabilities of outgoing edges. */
527 static void
528 compute_outgoing_frequencies (basic_block b)
530 edge e, f;
531 edge_iterator ei;
533 if (EDGE_COUNT (b->succs) == 2)
535 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
536 int probability;
538 if (note)
540 probability = XINT (note, 0);
541 e = BRANCH_EDGE (b);
542 e->probability = probability;
543 e->count = apply_probability (b->count, probability);
544 f = FALLTHRU_EDGE (b);
545 f->probability = REG_BR_PROB_BASE - probability;
546 f->count = b->count - e->count;
547 return;
549 else
551 guess_outgoing_edge_probabilities (b);
554 else if (single_succ_p (b))
556 e = single_succ_edge (b);
557 e->probability = REG_BR_PROB_BASE;
558 e->count = b->count;
559 return;
561 else
563 /* We rely on BBs with more than two successors to have sane probabilities
564 and do not guess them here. For BBs terminated by switch statements
565 expanded to jump-table jump, we have done the right thing during
566 expansion. For EH edges, we still guess the probabilities here. */
567 bool complex_edge = false;
568 FOR_EACH_EDGE (e, ei, b->succs)
569 if (e->flags & EDGE_COMPLEX)
571 complex_edge = true;
572 break;
574 if (complex_edge)
575 guess_outgoing_edge_probabilities (b);
578 if (b->count)
579 FOR_EACH_EDGE (e, ei, b->succs)
580 e->count = apply_probability (b->count, e->probability);
583 /* Assume that some pass has inserted labels or control flow
584 instructions within a basic block. Split basic blocks as needed
585 and create edges. */
587 void
588 find_many_sub_basic_blocks (sbitmap blocks)
590 basic_block bb, min, max;
592 FOR_EACH_BB_FN (bb, cfun)
593 SET_STATE (bb,
594 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
596 FOR_EACH_BB_FN (bb, cfun)
597 if (STATE (bb) == BLOCK_TO_SPLIT)
598 find_bb_boundaries (bb);
600 FOR_EACH_BB_FN (bb, cfun)
601 if (STATE (bb) != BLOCK_ORIGINAL)
602 break;
604 min = max = bb;
605 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
606 if (STATE (bb) != BLOCK_ORIGINAL)
607 max = bb;
609 /* Now re-scan and wire in all edges. This expect simple (conditional)
610 jumps at the end of each new basic blocks. */
611 make_edges (min, max, 1);
613 /* Update branch probabilities. Expect only (un)conditional jumps
614 to be created with only the forward edges. */
615 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
616 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
618 edge e;
619 edge_iterator ei;
621 if (STATE (bb) == BLOCK_ORIGINAL)
622 continue;
623 if (STATE (bb) == BLOCK_NEW)
625 bb->count = 0;
626 bb->frequency = 0;
627 FOR_EACH_EDGE (e, ei, bb->preds)
629 bb->count += e->count;
630 bb->frequency += EDGE_FREQUENCY (e);
634 compute_outgoing_frequencies (bb);
637 FOR_EACH_BB_FN (bb, cfun)
638 SET_STATE (bb, 0);