libgomp: Use pthread mutexes in the nvptx plugin.
[official-gcc.git] / gcc / cfgbuild.c
blobc26abbf4e641581a11d8d9984615898f9405fa87
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "rtl.h"
36 #include "hard-reg-set.h"
37 #include "predict.h"
38 #include "vec.h"
39 #include "hashtab.h"
40 #include "hash-set.h"
41 #include "machmode.h"
42 #include "input.h"
43 #include "function.h"
44 #include "dominance.h"
45 #include "cfg.h"
46 #include "cfgrtl.h"
47 #include "cfganal.h"
48 #include "cfgbuild.h"
49 #include "basic-block.h"
50 #include "regs.h"
51 #include "flags.h"
52 #include "except.h"
53 #include "expr.h"
54 #include "diagnostic-core.h"
55 #include "timevar.h"
56 #include "sbitmap.h"
58 static void make_edges (basic_block, basic_block, int);
59 static void make_label_edge (sbitmap, basic_block, rtx, int);
60 static void find_bb_boundaries (basic_block);
61 static void compute_outgoing_frequencies (basic_block);
63 /* Return true if insn is something that should be contained inside basic
64 block. */
66 bool
67 inside_basic_block_p (const rtx_insn *insn)
69 switch (GET_CODE (insn))
71 case CODE_LABEL:
72 /* Avoid creating of basic block for jumptables. */
73 return (NEXT_INSN (insn) == 0
74 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
76 case JUMP_INSN:
77 case CALL_INSN:
78 case INSN:
79 case DEBUG_INSN:
80 return true;
82 case JUMP_TABLE_DATA:
83 case BARRIER:
84 case NOTE:
85 return false;
87 default:
88 gcc_unreachable ();
92 /* Return true if INSN may cause control flow transfer, so it should be last in
93 the basic block. */
95 bool
96 control_flow_insn_p (const rtx_insn *insn)
98 switch (GET_CODE (insn))
100 case NOTE:
101 case CODE_LABEL:
102 case DEBUG_INSN:
103 return false;
105 case JUMP_INSN:
106 return true;
108 case CALL_INSN:
109 /* Noreturn and sibling call instructions terminate the basic blocks
110 (but only if they happen unconditionally). */
111 if ((SIBLING_CALL_P (insn)
112 || find_reg_note (insn, REG_NORETURN, 0))
113 && GET_CODE (PATTERN (insn)) != COND_EXEC)
114 return true;
116 /* Call insn may return to the nonlocal goto handler. */
117 if (can_nonlocal_goto (insn))
118 return true;
119 break;
121 case INSN:
122 /* Treat trap instructions like noreturn calls (same provision). */
123 if (GET_CODE (PATTERN (insn)) == TRAP_IF
124 && XEXP (PATTERN (insn), 0) == const1_rtx)
125 return true;
126 if (!cfun->can_throw_non_call_exceptions)
127 return false;
128 break;
130 case JUMP_TABLE_DATA:
131 case BARRIER:
132 /* It is nonsense to reach this when looking for the
133 end of basic block, but before dead code is eliminated
134 this may happen. */
135 return false;
137 default:
138 gcc_unreachable ();
141 return can_throw_internal (insn);
145 /* Create an edge between two basic blocks. FLAGS are auxiliary information
146 about the edge that is accumulated between calls. */
148 /* Create an edge from a basic block to a label. */
150 static void
151 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
153 gcc_assert (LABEL_P (label));
155 /* If the label was never emitted, this insn is junk, but avoid a
156 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
157 as a result of a syntax error and a diagnostic has already been
158 printed. */
160 if (INSN_UID (label) == 0)
161 return;
163 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
166 /* Create the edges generated by INSN in REGION. */
168 void
169 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
171 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
173 if (lp)
175 rtx label = lp->landing_pad;
177 /* During initial rtl generation, use the post_landing_pad. */
178 if (label == NULL)
180 gcc_assert (lp->post_landing_pad);
181 label = label_rtx (lp->post_landing_pad);
184 make_label_edge (edge_cache, src, label,
185 EDGE_ABNORMAL | EDGE_EH
186 | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
190 /* States of basic block as seen by find_many_sub_basic_blocks. */
191 enum state {
192 /* Basic blocks created via split_block belong to this state.
193 make_edges will examine these basic blocks to see if we need to
194 create edges going out of them. */
195 BLOCK_NEW = 0,
197 /* Basic blocks that do not need examining belong to this state.
198 These blocks will be left intact. In particular, make_edges will
199 not create edges going out of these basic blocks. */
200 BLOCK_ORIGINAL,
202 /* Basic blocks that may need splitting (due to a label appearing in
203 the middle, etc) belong to this state. After splitting them,
204 make_edges will create edges going out of them as needed. */
205 BLOCK_TO_SPLIT
208 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
209 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
211 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
212 #define BLOCK_USED_BY_TABLEJUMP 32
213 #define FULL_STATE(BB) ((size_t) (BB)->aux)
215 /* Identify the edges going out of basic blocks between MIN and MAX,
216 inclusive, that have their states set to BLOCK_NEW or
217 BLOCK_TO_SPLIT.
219 UPDATE_P should be nonzero if we are updating CFG and zero if we
220 are building CFG from scratch. */
222 static void
223 make_edges (basic_block min, basic_block max, int update_p)
225 basic_block bb;
226 sbitmap edge_cache = NULL;
228 /* Heavy use of computed goto in machine-generated code can lead to
229 nearly fully-connected CFGs. In that case we spend a significant
230 amount of time searching the edge lists for duplicates. */
231 if (forced_labels || cfun->cfg->max_jumptable_ents > 100)
232 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
234 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
235 is always the entry. */
236 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
237 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
239 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
241 rtx_insn *insn;
242 enum rtx_code code;
243 edge e;
244 edge_iterator ei;
246 if (STATE (bb) == BLOCK_ORIGINAL)
247 continue;
249 /* If we have an edge cache, cache edges going out of BB. */
250 if (edge_cache)
252 bitmap_clear (edge_cache);
253 if (update_p)
255 FOR_EACH_EDGE (e, ei, bb->succs)
256 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
257 bitmap_set_bit (edge_cache, e->dest->index);
261 if (LABEL_P (BB_HEAD (bb))
262 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
263 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
265 /* Examine the last instruction of the block, and discover the
266 ways we can leave the block. */
268 insn = BB_END (bb);
269 code = GET_CODE (insn);
271 /* A branch. */
272 if (code == JUMP_INSN)
274 rtx tmp;
275 rtx_jump_table_data *table;
277 /* Recognize a non-local goto as a branch outside the
278 current function. */
279 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
282 /* Recognize a tablejump and do the right thing. */
283 else if (tablejump_p (insn, NULL, &table))
285 rtvec vec = table->get_labels ();
286 int j;
288 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
289 make_label_edge (edge_cache, bb,
290 XEXP (RTVEC_ELT (vec, j), 0), 0);
292 /* Some targets (eg, ARM) emit a conditional jump that also
293 contains the out-of-range target. Scan for these and
294 add an edge if necessary. */
295 if ((tmp = single_set (insn)) != NULL
296 && SET_DEST (tmp) == pc_rtx
297 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
298 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
299 make_label_edge (edge_cache, bb,
300 LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)), 0);
303 /* If this is a computed jump, then mark it as reaching
304 everything on the forced_labels list. */
305 else if (computed_jump_p (insn))
307 for (rtx_insn_list *x = forced_labels; x; x = x->next ())
308 make_label_edge (edge_cache, bb, x->insn (), EDGE_ABNORMAL);
311 /* Returns create an exit out. */
312 else if (returnjump_p (insn))
313 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
315 /* Recognize asm goto and do the right thing. */
316 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
318 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
319 for (i = 0; i < n; ++i)
320 make_label_edge (edge_cache, bb,
321 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
324 /* Otherwise, we have a plain conditional or unconditional jump. */
325 else
327 gcc_assert (JUMP_LABEL (insn));
328 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
332 /* If this is a sibling call insn, then this is in effect a combined call
333 and return, and so we need an edge to the exit block. No need to
334 worry about EH edges, since we wouldn't have created the sibling call
335 in the first place. */
336 if (code == CALL_INSN && SIBLING_CALL_P (insn))
337 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
338 EDGE_SIBCALL | EDGE_ABNORMAL);
340 /* If this is a CALL_INSN, then mark it as reaching the active EH
341 handler for this CALL_INSN. If we're handling non-call
342 exceptions then any insn can reach any of the active handlers.
343 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
344 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
346 /* Add any appropriate EH edges. */
347 rtl_make_eh_edge (edge_cache, bb, insn);
349 if (code == CALL_INSN)
351 if (can_nonlocal_goto (insn))
353 /* ??? This could be made smarter: in some cases it's
354 possible to tell that certain calls will not do a
355 nonlocal goto. For example, if the nested functions
356 that do the nonlocal gotos do not have their addresses
357 taken, then only calls to those functions or to other
358 nested functions that use them could possibly do
359 nonlocal gotos. */
360 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
362 x = x->next ())
363 make_label_edge (edge_cache, bb, x->insn (),
364 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
367 if (flag_tm)
369 rtx note;
370 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
371 if (REG_NOTE_KIND (note) == REG_TM)
372 make_label_edge (edge_cache, bb, XEXP (note, 0),
373 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
378 /* Find out if we can drop through to the next block. */
379 insn = NEXT_INSN (insn);
380 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
381 if (e && e->flags & EDGE_FALLTHRU)
382 insn = NULL;
384 while (insn
385 && NOTE_P (insn)
386 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
387 insn = NEXT_INSN (insn);
389 if (!insn)
390 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
391 EDGE_FALLTHRU);
392 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
394 if (insn == BB_HEAD (bb->next_bb))
395 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
399 if (edge_cache)
400 sbitmap_free (edge_cache);
403 static void
404 mark_tablejump_edge (rtx label)
406 basic_block bb;
408 gcc_assert (LABEL_P (label));
409 /* See comment in make_label_edge. */
410 if (INSN_UID (label) == 0)
411 return;
412 bb = BLOCK_FOR_INSN (label);
413 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
416 static void
417 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
419 rtx_insn *insn = BB_END (bb);
420 rtx tmp;
421 rtvec vec;
422 int j;
423 edge_iterator ei;
424 edge e;
426 vec = table->get_labels ();
428 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
429 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
431 /* Some targets (eg, ARM) emit a conditional jump that also
432 contains the out-of-range target. Scan for these and
433 add an edge if necessary. */
434 if ((tmp = single_set (insn)) != NULL
435 && SET_DEST (tmp) == pc_rtx
436 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
437 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
438 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp), 2)));
440 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
442 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
443 SET_STATE (e->dest, FULL_STATE (e->dest)
444 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
445 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
447 remove_edge (e);
448 continue;
450 ei_next (&ei);
454 /* Scan basic block BB for possible BB boundaries inside the block
455 and create new basic blocks in the progress. */
457 static void
458 find_bb_boundaries (basic_block bb)
460 basic_block orig_bb = bb;
461 rtx_insn *insn = BB_HEAD (bb);
462 rtx_insn *end = BB_END (bb), *x;
463 rtx_jump_table_data *table;
464 rtx_insn *flow_transfer_insn = NULL;
465 edge fallthru = NULL;
467 if (insn == BB_END (bb))
468 return;
470 if (LABEL_P (insn))
471 insn = NEXT_INSN (insn);
473 /* Scan insn chain and try to find new basic block boundaries. */
474 while (1)
476 enum rtx_code code = GET_CODE (insn);
478 /* In case we've previously seen an insn that effects a control
479 flow transfer, split the block. */
480 if ((flow_transfer_insn || code == CODE_LABEL)
481 && inside_basic_block_p (insn))
483 fallthru = split_block (bb, PREV_INSN (insn));
484 if (flow_transfer_insn)
486 BB_END (bb) = flow_transfer_insn;
488 /* Clean up the bb field for the insns between the blocks. */
489 for (x = NEXT_INSN (flow_transfer_insn);
490 x != BB_HEAD (fallthru->dest);
491 x = NEXT_INSN (x))
492 if (!BARRIER_P (x))
493 set_block_for_insn (x, NULL);
496 bb = fallthru->dest;
497 remove_edge (fallthru);
498 flow_transfer_insn = NULL;
499 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
500 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
502 else if (code == BARRIER)
504 /* __builtin_unreachable () may cause a barrier to be emitted in
505 the middle of a BB. We need to split it in the same manner as
506 if the barrier were preceded by a control_flow_insn_p insn. */
507 if (!flow_transfer_insn)
508 flow_transfer_insn = prev_nonnote_insn_bb (insn);
511 if (control_flow_insn_p (insn))
512 flow_transfer_insn = insn;
513 if (insn == end)
514 break;
515 insn = NEXT_INSN (insn);
518 /* In case expander replaced normal insn by sequence terminating by
519 return and barrier, or possibly other sequence not behaving like
520 ordinary jump, we need to take care and move basic block boundary. */
521 if (flow_transfer_insn)
523 BB_END (bb) = flow_transfer_insn;
525 /* Clean up the bb field for the insns that do not belong to BB. */
526 x = flow_transfer_insn;
527 while (x != end)
529 x = NEXT_INSN (x);
530 if (!BARRIER_P (x))
531 set_block_for_insn (x, NULL);
535 /* We've possibly replaced the conditional jump by conditional jump
536 followed by cleanup at fallthru edge, so the outgoing edges may
537 be dead. */
538 purge_dead_edges (bb);
540 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
541 basic block, we might need to kill some edges. */
542 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
543 purge_dead_tablejump_edges (bb, table);
546 /* Assume that frequency of basic block B is known. Compute frequencies
547 and probabilities of outgoing edges. */
549 static void
550 compute_outgoing_frequencies (basic_block b)
552 edge e, f;
553 edge_iterator ei;
555 if (EDGE_COUNT (b->succs) == 2)
557 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
558 int probability;
560 if (note)
562 probability = XINT (note, 0);
563 e = BRANCH_EDGE (b);
564 e->probability = probability;
565 e->count = apply_probability (b->count, probability);
566 f = FALLTHRU_EDGE (b);
567 f->probability = REG_BR_PROB_BASE - probability;
568 f->count = b->count - e->count;
569 return;
571 else
573 guess_outgoing_edge_probabilities (b);
576 else if (single_succ_p (b))
578 e = single_succ_edge (b);
579 e->probability = REG_BR_PROB_BASE;
580 e->count = b->count;
581 return;
583 else
585 /* We rely on BBs with more than two successors to have sane probabilities
586 and do not guess them here. For BBs terminated by switch statements
587 expanded to jump-table jump, we have done the right thing during
588 expansion. For EH edges, we still guess the probabilities here. */
589 bool complex_edge = false;
590 FOR_EACH_EDGE (e, ei, b->succs)
591 if (e->flags & EDGE_COMPLEX)
593 complex_edge = true;
594 break;
596 if (complex_edge)
597 guess_outgoing_edge_probabilities (b);
600 if (b->count)
601 FOR_EACH_EDGE (e, ei, b->succs)
602 e->count = apply_probability (b->count, e->probability);
605 /* Assume that some pass has inserted labels or control flow
606 instructions within a basic block. Split basic blocks as needed
607 and create edges. */
609 void
610 find_many_sub_basic_blocks (sbitmap blocks)
612 basic_block bb, min, max;
614 FOR_EACH_BB_FN (bb, cfun)
615 SET_STATE (bb,
616 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
618 FOR_EACH_BB_FN (bb, cfun)
619 if (STATE (bb) == BLOCK_TO_SPLIT)
620 find_bb_boundaries (bb);
622 FOR_EACH_BB_FN (bb, cfun)
623 if (STATE (bb) != BLOCK_ORIGINAL)
624 break;
626 min = max = bb;
627 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
628 if (STATE (bb) != BLOCK_ORIGINAL)
629 max = bb;
631 /* Now re-scan and wire in all edges. This expect simple (conditional)
632 jumps at the end of each new basic blocks. */
633 make_edges (min, max, 1);
635 /* Update branch probabilities. Expect only (un)conditional jumps
636 to be created with only the forward edges. */
637 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
638 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
640 edge e;
641 edge_iterator ei;
643 if (STATE (bb) == BLOCK_ORIGINAL)
644 continue;
645 if (STATE (bb) == BLOCK_NEW)
647 bb->count = 0;
648 bb->frequency = 0;
649 FOR_EACH_EDGE (e, ei, bb->preds)
651 bb->count += e->count;
652 bb->frequency += EDGE_FREQUENCY (e);
656 compute_outgoing_frequencies (bb);
659 FOR_EACH_BB_FN (bb, cfun)
660 SET_STATE (bb, 0);