Enable dumping of alias graphs.
[official-gcc/Ramakrishna.git] / gcc / cfgbuild.c
blob6e941bf55e9fe5e592b9daae4cddadf52719f5db
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
4 Free Software Foundation, Inc.
6 This file is part of GCC.
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.
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.
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/>. */
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 "output.h"
34 #include "function.h"
35 #include "except.h"
36 #include "toplev.h"
37 #include "timevar.h"
39 static void make_edges (basic_block, basic_block, int);
40 static void make_label_edge (sbitmap, basic_block, rtx, int);
41 static void find_bb_boundaries (basic_block);
42 static void compute_outgoing_frequencies (basic_block);
44 /* Return true if insn is something that should be contained inside basic
45 block. */
47 bool
48 inside_basic_block_p (const_rtx insn)
50 switch (GET_CODE (insn))
52 case CODE_LABEL:
53 /* Avoid creating of basic block for jumptables. */
54 return (NEXT_INSN (insn) == 0
55 || !JUMP_P (NEXT_INSN (insn))
56 || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
57 && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
59 case JUMP_INSN:
60 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
61 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
63 case CALL_INSN:
64 case INSN:
65 case DEBUG_INSN:
66 return true;
68 case BARRIER:
69 case NOTE:
70 return false;
72 default:
73 gcc_unreachable ();
77 /* Return true if INSN may cause control flow transfer, so it should be last in
78 the basic block. */
80 bool
81 control_flow_insn_p (const_rtx insn)
83 rtx note;
85 switch (GET_CODE (insn))
87 case NOTE:
88 case CODE_LABEL:
89 case DEBUG_INSN:
90 return false;
92 case JUMP_INSN:
93 /* Jump insn always causes control transfer except for tablejumps. */
94 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
95 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
97 case CALL_INSN:
98 /* Noreturn and sibling call instructions terminate the basic blocks
99 (but only if they happen unconditionally). */
100 if ((SIBLING_CALL_P (insn)
101 || find_reg_note (insn, REG_NORETURN, 0))
102 && GET_CODE (PATTERN (insn)) != COND_EXEC)
103 return true;
104 /* Call insn may return to the nonlocal goto handler. */
105 return ((nonlocal_goto_handler_labels
106 && (0 == (note = find_reg_note (insn, REG_EH_REGION,
107 NULL_RTX))
108 || INTVAL (XEXP (note, 0)) >= 0))
109 /* Or may trap. */
110 || can_throw_internal (insn));
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;
118 return (flag_non_call_exceptions && can_throw_internal (insn));
120 case BARRIER:
121 /* It is nonsense to reach barrier when looking for the
122 end of basic block, but before dead code is eliminated
123 this may happen. */
124 return false;
126 default:
127 gcc_unreachable ();
132 /* Create an edge between two basic blocks. FLAGS are auxiliary information
133 about the edge that is accumulated between calls. */
135 /* Create an edge from a basic block to a label. */
137 static void
138 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
140 gcc_assert (LABEL_P (label));
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. */
147 if (INSN_UID (label) == 0)
148 return;
150 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
153 /* Create the edges generated by INSN in REGION. */
155 void
156 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
158 int is_call = CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0;
159 rtx handlers, i;
161 handlers = reachable_handlers (insn);
163 for (i = handlers; i; i = XEXP (i, 1))
164 make_label_edge (edge_cache, src, XEXP (i, 0),
165 EDGE_ABNORMAL | EDGE_EH | is_call);
167 free_INSN_LIST_list (&handlers);
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);
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->next_bb)
217 make_edge (ENTRY_BLOCK_PTR, min, EDGE_FALLTHRU);
219 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
221 rtx insn, x;
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 sbitmap_zero (edge_cache);
233 if (update_p)
235 FOR_EACH_EDGE (e, ei, bb->succs)
236 if (e->dest != EXIT_BLOCK_PTR)
237 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, 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;
256 /* Recognize exception handling placeholders. */
257 if (GET_CODE (PATTERN (insn)) == RESX)
258 rtl_make_eh_edge (edge_cache, bb, insn);
260 /* Recognize a non-local goto as a branch outside the
261 current function. */
262 else if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
265 /* Recognize a tablejump and do the right thing. */
266 else if (tablejump_p (insn, NULL, &tmp))
268 rtvec vec;
269 int j;
271 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
272 vec = XVEC (PATTERN (tmp), 0);
273 else
274 vec = XVEC (PATTERN (tmp), 1);
276 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
277 make_label_edge (edge_cache, bb,
278 XEXP (RTVEC_ELT (vec, j), 0), 0);
280 /* Some targets (eg, ARM) emit a conditional jump that also
281 contains the out-of-range target. Scan for these and
282 add an edge if necessary. */
283 if ((tmp = single_set (insn)) != NULL
284 && SET_DEST (tmp) == pc_rtx
285 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
286 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
287 make_label_edge (edge_cache, bb,
288 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
291 /* If this is a computed jump, then mark it as reaching
292 everything on the forced_labels list. */
293 else if (computed_jump_p (insn))
295 for (x = forced_labels; x; x = XEXP (x, 1))
296 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
299 /* Returns create an exit out. */
300 else if (returnjump_p (insn))
301 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
303 /* Otherwise, we have a plain conditional or unconditional jump. */
304 else
306 gcc_assert (JUMP_LABEL (insn));
307 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
311 /* If this is a sibling call insn, then this is in effect a combined call
312 and return, and so we need an edge to the exit block. No need to
313 worry about EH edges, since we wouldn't have created the sibling call
314 in the first place. */
315 if (code == CALL_INSN && SIBLING_CALL_P (insn))
316 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
317 EDGE_SIBCALL | EDGE_ABNORMAL);
319 /* If this is a CALL_INSN, then mark it as reaching the active EH
320 handler for this CALL_INSN. If we're handling non-call
321 exceptions then any insn can reach any of the active handlers.
322 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
323 else if (code == CALL_INSN || flag_non_call_exceptions)
325 /* Add any appropriate EH edges. */
326 rtl_make_eh_edge (edge_cache, bb, insn);
328 if (code == CALL_INSN && nonlocal_goto_handler_labels)
330 /* ??? This could be made smarter: in some cases it's possible
331 to tell that certain calls will not do a nonlocal goto.
332 For example, if the nested functions that do the nonlocal
333 gotos do not have their addresses taken, then only calls to
334 those functions or to other nested functions that use them
335 could possibly do nonlocal gotos. */
337 /* We do know that a REG_EH_REGION note with a value less
338 than 0 is guaranteed not to perform a non-local goto. */
339 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
341 if (!note || INTVAL (XEXP (note, 0)) >= 0)
342 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
343 make_label_edge (edge_cache, bb, XEXP (x, 0),
344 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
348 /* Find out if we can drop through to the next block. */
349 insn = NEXT_INSN (insn);
350 e = find_edge (bb, EXIT_BLOCK_PTR);
351 if (e && e->flags & EDGE_FALLTHRU)
352 insn = NULL;
354 while (insn
355 && NOTE_P (insn)
356 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
357 insn = NEXT_INSN (insn);
359 if (!insn)
360 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
361 else if (bb->next_bb != EXIT_BLOCK_PTR)
363 if (insn == BB_HEAD (bb->next_bb))
364 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
368 if (edge_cache)
369 sbitmap_vector_free (edge_cache);
372 static void
373 mark_tablejump_edge (rtx label)
375 basic_block bb;
377 gcc_assert (LABEL_P (label));
378 /* See comment in make_label_edge. */
379 if (INSN_UID (label) == 0)
380 return;
381 bb = BLOCK_FOR_INSN (label);
382 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
385 static void
386 purge_dead_tablejump_edges (basic_block bb, rtx table)
388 rtx insn = BB_END (bb), tmp;
389 rtvec vec;
390 int j;
391 edge_iterator ei;
392 edge e;
394 if (GET_CODE (PATTERN (table)) == ADDR_VEC)
395 vec = XVEC (PATTERN (table), 0);
396 else
397 vec = XVEC (PATTERN (table), 1);
399 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
400 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
402 /* Some targets (eg, ARM) emit a conditional jump that also
403 contains the out-of-range target. Scan for these and
404 add an edge if necessary. */
405 if ((tmp = single_set (insn)) != NULL
406 && SET_DEST (tmp) == pc_rtx
407 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
408 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
409 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
411 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
413 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
414 SET_STATE (e->dest, FULL_STATE (e->dest)
415 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
416 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
418 remove_edge (e);
419 continue;
421 ei_next (&ei);
425 /* Scan basic block BB for possible BB boundaries inside the block
426 and create new basic blocks in the progress. */
428 static void
429 find_bb_boundaries (basic_block bb)
431 basic_block orig_bb = bb;
432 rtx insn = BB_HEAD (bb);
433 rtx end = BB_END (bb), x;
434 rtx table;
435 rtx flow_transfer_insn = NULL_RTX;
436 edge fallthru = NULL;
438 if (insn == BB_END (bb))
439 return;
441 if (LABEL_P (insn))
442 insn = NEXT_INSN (insn);
444 /* Scan insn chain and try to find new basic block boundaries. */
445 while (1)
447 enum rtx_code code = GET_CODE (insn);
449 /* On code label, split current basic block. */
450 if (code == CODE_LABEL)
452 fallthru = split_block (bb, PREV_INSN (insn));
453 if (flow_transfer_insn)
455 BB_END (bb) = flow_transfer_insn;
457 /* Clean up the bb field for the insns between the blocks. */
458 for (x = NEXT_INSN (flow_transfer_insn);
459 x != BB_HEAD (fallthru->dest);
460 x = NEXT_INSN (x))
461 if (!BARRIER_P (x))
462 set_block_for_insn (x, NULL);
465 bb = fallthru->dest;
466 remove_edge (fallthru);
467 flow_transfer_insn = NULL_RTX;
468 if (LABEL_ALT_ENTRY_P (insn))
469 make_edge (ENTRY_BLOCK_PTR, bb, 0);
472 /* __builtin_unreachable () may cause a barrier to be emitted in
473 the middle of a BB. We need to split it in the same manner
474 as if the barrier were preceded by a control_flow_insn_p
475 insn. */
476 if (code == BARRIER && !flow_transfer_insn)
477 flow_transfer_insn = prev_nonnote_insn_bb (insn);
479 /* In case we've previously seen an insn that effects a control
480 flow transfer, split the block. */
481 if (flow_transfer_insn && inside_basic_block_p (insn))
483 fallthru = split_block (bb, PREV_INSN (insn));
484 BB_END (bb) = flow_transfer_insn;
486 /* Clean up the bb field for the insns between the blocks. */
487 for (x = NEXT_INSN (flow_transfer_insn);
488 x != BB_HEAD (fallthru->dest);
489 x = NEXT_INSN (x))
490 if (!BARRIER_P (x))
491 set_block_for_insn (x, NULL);
493 bb = fallthru->dest;
494 remove_edge (fallthru);
495 flow_transfer_insn = NULL_RTX;
498 if (control_flow_insn_p (insn))
499 flow_transfer_insn = insn;
500 if (insn == end)
501 break;
502 insn = NEXT_INSN (insn);
505 /* In case expander replaced normal insn by sequence terminating by
506 return and barrier, or possibly other sequence not behaving like
507 ordinary jump, we need to take care and move basic block boundary. */
508 if (flow_transfer_insn)
510 BB_END (bb) = flow_transfer_insn;
512 /* Clean up the bb field for the insns that do not belong to BB. */
513 x = flow_transfer_insn;
514 while (x != end)
516 x = NEXT_INSN (x);
517 if (!BARRIER_P (x))
518 set_block_for_insn (x, NULL);
522 /* We've possibly replaced the conditional jump by conditional jump
523 followed by cleanup at fallthru edge, so the outgoing edges may
524 be dead. */
525 purge_dead_edges (bb);
527 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
528 basic block, we might need to kill some edges. */
529 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
530 purge_dead_tablejump_edges (bb, table);
533 /* Assume that frequency of basic block B is known. Compute frequencies
534 and probabilities of outgoing edges. */
536 static void
537 compute_outgoing_frequencies (basic_block b)
539 edge e, f;
540 edge_iterator ei;
542 if (EDGE_COUNT (b->succs) == 2)
544 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
545 int probability;
547 if (note)
549 probability = INTVAL (XEXP (note, 0));
550 e = BRANCH_EDGE (b);
551 e->probability = probability;
552 e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
553 / REG_BR_PROB_BASE);
554 f = FALLTHRU_EDGE (b);
555 f->probability = REG_BR_PROB_BASE - probability;
556 f->count = b->count - e->count;
557 return;
561 if (single_succ_p (b))
563 e = single_succ_edge (b);
564 e->probability = REG_BR_PROB_BASE;
565 e->count = b->count;
566 return;
568 guess_outgoing_edge_probabilities (b);
569 if (b->count)
570 FOR_EACH_EDGE (e, ei, b->succs)
571 e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
572 / REG_BR_PROB_BASE);
575 /* Assume that some pass has inserted labels or control flow
576 instructions within a basic block. Split basic blocks as needed
577 and create edges. */
579 void
580 find_many_sub_basic_blocks (sbitmap blocks)
582 basic_block bb, min, max;
584 FOR_EACH_BB (bb)
585 SET_STATE (bb,
586 TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
588 FOR_EACH_BB (bb)
589 if (STATE (bb) == BLOCK_TO_SPLIT)
590 find_bb_boundaries (bb);
592 FOR_EACH_BB (bb)
593 if (STATE (bb) != BLOCK_ORIGINAL)
594 break;
596 min = max = bb;
597 for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
598 if (STATE (bb) != BLOCK_ORIGINAL)
599 max = bb;
601 /* Now re-scan and wire in all edges. This expect simple (conditional)
602 jumps at the end of each new basic blocks. */
603 make_edges (min, max, 1);
605 /* Update branch probabilities. Expect only (un)conditional jumps
606 to be created with only the forward edges. */
607 if (profile_status != PROFILE_ABSENT)
608 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
610 edge e;
611 edge_iterator ei;
613 if (STATE (bb) == BLOCK_ORIGINAL)
614 continue;
615 if (STATE (bb) == BLOCK_NEW)
617 bb->count = 0;
618 bb->frequency = 0;
619 FOR_EACH_EDGE (e, ei, bb->preds)
621 bb->count += e->count;
622 bb->frequency += EDGE_FREQUENCY (e);
626 compute_outgoing_frequencies (bb);
629 FOR_EACH_BB (bb)
630 SET_STATE (bb, 0);