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