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