Daily bump.
[official-gcc.git] / gcc / cfg.c
blob044fcaef2427f78d1345d32acb72a7651fc1d6dd
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file contains low level functions to manipulate the CFG and
24 analyze it. All other modules should not transform the data structure
25 directly and use abstraction instead. The file is supposed to be
26 ordered bottom-up and should not contain any code dependent on a
27 particular intermediate language (RTL or trees).
29 Available functionality:
30 - Initialization/deallocation
31 init_flow, clear_edges
32 - Low level basic block manipulation
33 alloc_block, expunge_block
34 - Edge manipulation
35 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
36 - Low level edge redirection (without updating instruction chain)
37 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
38 - Dumping and debugging
39 dump_flow_info, debug_flow_info, dump_edge_info
40 - Allocation of AUX fields for basic blocks
41 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
42 - clear_bb_flags
43 - Consistency checking
44 verify_flow_info
45 - Dumping and debugging
46 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "tm.h"
53 #include "tree.h"
54 #include "rtl.h"
55 #include "hard-reg-set.h"
56 #include "regs.h"
57 #include "flags.h"
58 #include "output.h"
59 #include "function.h"
60 #include "except.h"
61 #include "toplev.h"
62 #include "tm_p.h"
63 #include "obstack.h"
64 #include "timevar.h"
65 #include "ggc.h"
66 #include "hashtab.h"
67 #include "alloc-pool.h"
69 /* The obstack on which the flow graph components are allocated. */
71 struct bitmap_obstack reg_obstack;
73 void debug_flow_info (void);
74 static void free_edge (edge);
76 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
78 /* Called once at initialization time. */
80 void
81 init_flow (void)
83 if (!cfun->cfg)
84 cfun->cfg = ggc_alloc_cleared (sizeof (struct control_flow_graph));
85 n_edges = 0;
86 ENTRY_BLOCK_PTR = ggc_alloc_cleared (sizeof (struct basic_block_def));
87 ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
88 EXIT_BLOCK_PTR = ggc_alloc_cleared (sizeof (struct basic_block_def));
89 EXIT_BLOCK_PTR->index = EXIT_BLOCK;
90 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
91 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
94 /* Helper function for remove_edge and clear_edges. Frees edge structure
95 without actually unlinking it from the pred/succ lists. */
97 static void
98 free_edge (edge e ATTRIBUTE_UNUSED)
100 n_edges--;
101 ggc_free (e);
104 /* Free the memory associated with the edge structures. */
106 void
107 clear_edges (void)
109 basic_block bb;
110 edge e;
111 edge_iterator ei;
113 FOR_EACH_BB (bb)
115 FOR_EACH_EDGE (e, ei, bb->succs)
116 free_edge (e);
117 VEC_truncate (edge, bb->succs, 0);
118 VEC_truncate (edge, bb->preds, 0);
121 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
122 free_edge (e);
123 VEC_truncate (edge, EXIT_BLOCK_PTR->preds, 0);
124 VEC_truncate (edge, ENTRY_BLOCK_PTR->succs, 0);
126 gcc_assert (!n_edges);
129 /* Allocate memory for basic_block. */
131 basic_block
132 alloc_block (void)
134 basic_block bb;
135 bb = ggc_alloc_cleared (sizeof (*bb));
136 return bb;
139 /* Initialize rbi (the structure containing data used by basic block
140 duplication and reordering) for the given basic block. */
142 void
143 initialize_bb_rbi (basic_block bb)
145 gcc_assert (!bb->rbi);
146 bb->rbi = ggc_alloc_cleared (sizeof (struct reorder_block_def));
149 /* Link block B to chain after AFTER. */
150 void
151 link_block (basic_block b, basic_block after)
153 b->next_bb = after->next_bb;
154 b->prev_bb = after;
155 after->next_bb = b;
156 b->next_bb->prev_bb = b;
159 /* Unlink block B from chain. */
160 void
161 unlink_block (basic_block b)
163 b->next_bb->prev_bb = b->prev_bb;
164 b->prev_bb->next_bb = b->next_bb;
165 b->prev_bb = NULL;
166 b->next_bb = NULL;
169 /* Sequentially order blocks and compact the arrays. */
170 void
171 compact_blocks (void)
173 int i;
174 basic_block bb;
176 i = 0;
177 FOR_EACH_BB (bb)
179 BASIC_BLOCK (i) = bb;
180 bb->index = i;
181 i++;
184 gcc_assert (i == n_basic_blocks);
186 for (; i < last_basic_block; i++)
187 BASIC_BLOCK (i) = NULL;
189 last_basic_block = n_basic_blocks;
192 /* Remove block B from the basic block array. */
194 void
195 expunge_block (basic_block b)
197 unlink_block (b);
198 BASIC_BLOCK (b->index) = NULL;
199 n_basic_blocks--;
200 /* We should be able to ggc_free here, but we are not.
201 The dead SSA_NAMES are left pointing to dead statements that are pointing
202 to dead basic blocks making garbage collector to die.
203 We should be able to release all dead SSA_NAMES and at the same time we should
204 clear out BB pointer of dead statements consistently. */
207 /* Connect E to E->src. */
209 static inline void
210 connect_src (edge e)
212 VEC_safe_push (edge, gc, e->src->succs, e);
215 /* Connect E to E->dest. */
217 static inline void
218 connect_dest (edge e)
220 basic_block dest = e->dest;
221 VEC_safe_push (edge, gc, dest->preds, e);
222 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
225 /* Disconnect edge E from E->src. */
227 static inline void
228 disconnect_src (edge e)
230 basic_block src = e->src;
231 edge_iterator ei;
232 edge tmp;
234 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
236 if (tmp == e)
238 VEC_unordered_remove (edge, src->succs, ei.index);
239 return;
241 else
242 ei_next (&ei);
245 gcc_unreachable ();
248 /* Disconnect edge E from E->dest. */
250 static inline void
251 disconnect_dest (edge e)
253 basic_block dest = e->dest;
254 unsigned int dest_idx = e->dest_idx;
256 VEC_unordered_remove (edge, dest->preds, dest_idx);
258 /* If we removed an edge in the middle of the edge vector, we need
259 to update dest_idx of the edge that moved into the "hole". */
260 if (dest_idx < EDGE_COUNT (dest->preds))
261 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
264 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
265 created edge. Use this only if you are sure that this edge can't
266 possibly already exist. */
268 edge
269 unchecked_make_edge (basic_block src, basic_block dst, int flags)
271 edge e;
272 e = ggc_alloc_cleared (sizeof (*e));
273 n_edges++;
275 e->src = src;
276 e->dest = dst;
277 e->flags = flags;
279 connect_src (e);
280 connect_dest (e);
282 execute_on_growing_pred (e);
284 return e;
287 /* Create an edge connecting SRC and DST with FLAGS optionally using
288 edge cache CACHE. Return the new edge, NULL if already exist. */
290 edge
291 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
293 if (edge_cache == NULL
294 || src == ENTRY_BLOCK_PTR
295 || dst == EXIT_BLOCK_PTR)
296 return make_edge (src, dst, flags);
298 /* Does the requested edge already exist? */
299 if (! TEST_BIT (edge_cache, dst->index))
301 /* The edge does not exist. Create one and update the
302 cache. */
303 SET_BIT (edge_cache, dst->index);
304 return unchecked_make_edge (src, dst, flags);
307 /* At this point, we know that the requested edge exists. Adjust
308 flags if necessary. */
309 if (flags)
311 edge e = find_edge (src, dst);
312 e->flags |= flags;
315 return NULL;
318 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
319 created edge or NULL if already exist. */
321 edge
322 make_edge (basic_block src, basic_block dest, int flags)
324 edge e = find_edge (src, dest);
326 /* Make sure we don't add duplicate edges. */
327 if (e)
329 e->flags |= flags;
330 return NULL;
333 return unchecked_make_edge (src, dest, flags);
336 /* Create an edge connecting SRC to DEST and set probability by knowing
337 that it is the single edge leaving SRC. */
339 edge
340 make_single_succ_edge (basic_block src, basic_block dest, int flags)
342 edge e = make_edge (src, dest, flags);
344 e->probability = REG_BR_PROB_BASE;
345 e->count = src->count;
346 return e;
349 /* This function will remove an edge from the flow graph. */
351 void
352 remove_edge (edge e)
354 remove_predictions_associated_with_edge (e);
355 execute_on_shrinking_pred (e);
357 disconnect_src (e);
358 disconnect_dest (e);
360 free_edge (e);
363 /* Redirect an edge's successor from one block to another. */
365 void
366 redirect_edge_succ (edge e, basic_block new_succ)
368 execute_on_shrinking_pred (e);
370 disconnect_dest (e);
372 e->dest = new_succ;
374 /* Reconnect the edge to the new successor block. */
375 connect_dest (e);
377 execute_on_growing_pred (e);
380 /* Like previous but avoid possible duplicate edge. */
382 edge
383 redirect_edge_succ_nodup (edge e, basic_block new_succ)
385 edge s;
387 s = find_edge (e->src, new_succ);
388 if (s && s != e)
390 s->flags |= e->flags;
391 s->probability += e->probability;
392 if (s->probability > REG_BR_PROB_BASE)
393 s->probability = REG_BR_PROB_BASE;
394 s->count += e->count;
395 remove_edge (e);
396 e = s;
398 else
399 redirect_edge_succ (e, new_succ);
401 return e;
404 /* Redirect an edge's predecessor from one block to another. */
406 void
407 redirect_edge_pred (edge e, basic_block new_pred)
409 disconnect_src (e);
411 e->src = new_pred;
413 /* Reconnect the edge to the new predecessor block. */
414 connect_src (e);
417 /* Clear all basic block flags, with the exception of partitioning. */
418 void
419 clear_bb_flags (void)
421 basic_block bb;
423 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
424 bb->flags = (BB_PARTITION (bb) | (bb->flags & BB_DISABLE_SCHEDULE)
425 | (bb->flags & BB_RTL));
428 /* Check the consistency of profile information. We can't do that
429 in verify_flow_info, as the counts may get invalid for incompletely
430 solved graphs, later eliminating of conditionals or roundoff errors.
431 It is still practical to have them reported for debugging of simple
432 testcases. */
433 void
434 check_bb_profile (basic_block bb, FILE * file)
436 edge e;
437 int sum = 0;
438 gcov_type lsum;
439 edge_iterator ei;
441 if (profile_status == PROFILE_ABSENT)
442 return;
444 if (bb != EXIT_BLOCK_PTR)
446 FOR_EACH_EDGE (e, ei, bb->succs)
447 sum += e->probability;
448 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
449 fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
450 sum * 100.0 / REG_BR_PROB_BASE);
451 lsum = 0;
452 FOR_EACH_EDGE (e, ei, bb->succs)
453 lsum += e->count;
454 if (EDGE_COUNT (bb->succs)
455 && (lsum - bb->count > 100 || lsum - bb->count < -100))
456 fprintf (file, "Invalid sum of outgoing counts %i, should be %i\n",
457 (int) lsum, (int) bb->count);
459 if (bb != ENTRY_BLOCK_PTR)
461 sum = 0;
462 FOR_EACH_EDGE (e, ei, bb->preds)
463 sum += EDGE_FREQUENCY (e);
464 if (abs (sum - bb->frequency) > 100)
465 fprintf (file,
466 "Invalid sum of incoming frequencies %i, should be %i\n",
467 sum, bb->frequency);
468 lsum = 0;
469 FOR_EACH_EDGE (e, ei, bb->preds)
470 lsum += e->count;
471 if (lsum - bb->count > 100 || lsum - bb->count < -100)
472 fprintf (file, "Invalid sum of incoming counts %i, should be %i\n",
473 (int) lsum, (int) bb->count);
477 void
478 dump_flow_info (FILE *file)
480 basic_block bb;
482 /* There are no pseudo registers after reload. Don't dump them. */
483 if (reg_n_info && !reload_completed)
485 unsigned int i, max = max_reg_num ();
486 fprintf (file, "%d registers.\n", max);
487 for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
488 if (REG_N_REFS (i))
490 enum reg_class class, altclass;
492 fprintf (file, "\nRegister %d used %d times across %d insns",
493 i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
494 if (REG_BASIC_BLOCK (i) >= 0)
495 fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
496 if (REG_N_SETS (i))
497 fprintf (file, "; set %d time%s", REG_N_SETS (i),
498 (REG_N_SETS (i) == 1) ? "" : "s");
499 if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
500 fprintf (file, "; user var");
501 if (REG_N_DEATHS (i) != 1)
502 fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
503 if (REG_N_CALLS_CROSSED (i) == 1)
504 fprintf (file, "; crosses 1 call");
505 else if (REG_N_CALLS_CROSSED (i))
506 fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
507 if (regno_reg_rtx[i] != NULL
508 && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
509 fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
511 class = reg_preferred_class (i);
512 altclass = reg_alternate_class (i);
513 if (class != GENERAL_REGS || altclass != ALL_REGS)
515 if (altclass == ALL_REGS || class == ALL_REGS)
516 fprintf (file, "; pref %s", reg_class_names[(int) class]);
517 else if (altclass == NO_REGS)
518 fprintf (file, "; %s or none", reg_class_names[(int) class]);
519 else
520 fprintf (file, "; pref %s, else %s",
521 reg_class_names[(int) class],
522 reg_class_names[(int) altclass]);
525 if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
526 fprintf (file, "; pointer");
527 fprintf (file, ".\n");
531 fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
532 FOR_EACH_BB (bb)
534 edge e;
535 edge_iterator ei;
537 fprintf (file, "\nBasic block %d ", bb->index);
538 fprintf (file, "prev %d, next %d, ",
539 bb->prev_bb->index, bb->next_bb->index);
540 fprintf (file, "loop_depth %d, count ", bb->loop_depth);
541 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
542 fprintf (file, ", freq %i", bb->frequency);
543 if (maybe_hot_bb_p (bb))
544 fprintf (file, ", maybe hot");
545 if (probably_never_executed_bb_p (bb))
546 fprintf (file, ", probably never executed");
547 fprintf (file, ".\n");
549 fprintf (file, "Predecessors: ");
550 FOR_EACH_EDGE (e, ei, bb->preds)
551 dump_edge_info (file, e, 0);
553 fprintf (file, "\nSuccessors: ");
554 FOR_EACH_EDGE (e, ei, bb->succs)
555 dump_edge_info (file, e, 1);
557 if (bb->flags & BB_RTL)
559 if (bb->il.rtl->global_live_at_start)
561 fprintf (file, "\nRegisters live at start:");
562 dump_regset (bb->il.rtl->global_live_at_start, file);
565 if (bb->il.rtl->global_live_at_end)
567 fprintf (file, "\nRegisters live at end:");
568 dump_regset (bb->il.rtl->global_live_at_end, file);
572 putc ('\n', file);
573 check_bb_profile (bb, file);
576 putc ('\n', file);
579 void
580 debug_flow_info (void)
582 dump_flow_info (stderr);
585 void
586 dump_edge_info (FILE *file, edge e, int do_succ)
588 basic_block side = (do_succ ? e->dest : e->src);
590 if (side == ENTRY_BLOCK_PTR)
591 fputs (" ENTRY", file);
592 else if (side == EXIT_BLOCK_PTR)
593 fputs (" EXIT", file);
594 else
595 fprintf (file, " %d", side->index);
597 if (e->probability)
598 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
600 if (e->count)
602 fprintf (file, " count:");
603 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
606 if (e->flags)
608 static const char * const bitnames[] = {
609 "fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
610 "can_fallthru", "irreducible", "sibcall", "loop_exit",
611 "true", "false", "exec"
613 int comma = 0;
614 int i, flags = e->flags;
616 fputs (" (", file);
617 for (i = 0; flags; i++)
618 if (flags & (1 << i))
620 flags &= ~(1 << i);
622 if (comma)
623 fputc (',', file);
624 if (i < (int) ARRAY_SIZE (bitnames))
625 fputs (bitnames[i], file);
626 else
627 fprintf (file, "%d", i);
628 comma = 1;
631 fputc (')', file);
635 /* Simple routines to easily allocate AUX fields of basic blocks. */
637 static struct obstack block_aux_obstack;
638 static void *first_block_aux_obj = 0;
639 static struct obstack edge_aux_obstack;
640 static void *first_edge_aux_obj = 0;
642 /* Allocate a memory block of SIZE as BB->aux. The obstack must
643 be first initialized by alloc_aux_for_blocks. */
645 inline void
646 alloc_aux_for_block (basic_block bb, int size)
648 /* Verify that aux field is clear. */
649 gcc_assert (!bb->aux && first_block_aux_obj);
650 bb->aux = obstack_alloc (&block_aux_obstack, size);
651 memset (bb->aux, 0, size);
654 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
655 alloc_aux_for_block for each basic block. */
657 void
658 alloc_aux_for_blocks (int size)
660 static int initialized;
662 if (!initialized)
664 gcc_obstack_init (&block_aux_obstack);
665 initialized = 1;
667 else
668 /* Check whether AUX data are still allocated. */
669 gcc_assert (!first_block_aux_obj);
671 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
672 if (size)
674 basic_block bb;
676 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
677 alloc_aux_for_block (bb, size);
681 /* Clear AUX pointers of all blocks. */
683 void
684 clear_aux_for_blocks (void)
686 basic_block bb;
688 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
689 bb->aux = NULL;
692 /* Free data allocated in block_aux_obstack and clear AUX pointers
693 of all blocks. */
695 void
696 free_aux_for_blocks (void)
698 gcc_assert (first_block_aux_obj);
699 obstack_free (&block_aux_obstack, first_block_aux_obj);
700 first_block_aux_obj = NULL;
702 clear_aux_for_blocks ();
705 /* Allocate a memory edge of SIZE as BB->aux. The obstack must
706 be first initialized by alloc_aux_for_edges. */
708 inline void
709 alloc_aux_for_edge (edge e, int size)
711 /* Verify that aux field is clear. */
712 gcc_assert (!e->aux && first_edge_aux_obj);
713 e->aux = obstack_alloc (&edge_aux_obstack, size);
714 memset (e->aux, 0, size);
717 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
718 alloc_aux_for_edge for each basic edge. */
720 void
721 alloc_aux_for_edges (int size)
723 static int initialized;
725 if (!initialized)
727 gcc_obstack_init (&edge_aux_obstack);
728 initialized = 1;
730 else
731 /* Check whether AUX data are still allocated. */
732 gcc_assert (!first_edge_aux_obj);
734 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
735 if (size)
737 basic_block bb;
739 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
741 edge e;
742 edge_iterator ei;
744 FOR_EACH_EDGE (e, ei, bb->succs)
745 alloc_aux_for_edge (e, size);
750 /* Clear AUX pointers of all edges. */
752 void
753 clear_aux_for_edges (void)
755 basic_block bb;
756 edge e;
758 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
760 edge_iterator ei;
761 FOR_EACH_EDGE (e, ei, bb->succs)
762 e->aux = NULL;
766 /* Free data allocated in edge_aux_obstack and clear AUX pointers
767 of all edges. */
769 void
770 free_aux_for_edges (void)
772 gcc_assert (first_edge_aux_obj);
773 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
774 first_edge_aux_obj = NULL;
776 clear_aux_for_edges ();
779 void
780 debug_bb (basic_block bb)
782 dump_bb (bb, stderr, 0);
785 basic_block
786 debug_bb_n (int n)
788 basic_block bb = BASIC_BLOCK (n);
789 dump_bb (bb, stderr, 0);
790 return bb;
793 /* Dumps cfg related information about basic block BB to FILE. */
795 static void
796 dump_cfg_bb_info (FILE *file, basic_block bb)
798 unsigned i;
799 edge_iterator ei;
800 bool first = true;
801 static const char * const bb_bitnames[] =
803 "dirty", "new", "reachable", "visited", "irreducible_loop", "superblock"
805 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
806 edge e;
808 fprintf (file, "Basic block %d", bb->index);
809 for (i = 0; i < n_bitnames; i++)
810 if (bb->flags & (1 << i))
812 if (first)
813 fprintf (file, " (");
814 else
815 fprintf (file, ", ");
816 first = false;
817 fprintf (file, bb_bitnames[i]);
819 if (!first)
820 fprintf (file, ")");
821 fprintf (file, "\n");
823 fprintf (file, "Predecessors: ");
824 FOR_EACH_EDGE (e, ei, bb->preds)
825 dump_edge_info (file, e, 0);
827 fprintf (file, "\nSuccessors: ");
828 FOR_EACH_EDGE (e, ei, bb->succs)
829 dump_edge_info (file, e, 1);
830 fprintf (file, "\n\n");
833 /* Dumps a brief description of cfg to FILE. */
835 void
836 brief_dump_cfg (FILE *file)
838 basic_block bb;
840 FOR_EACH_BB (bb)
842 dump_cfg_bb_info (file, bb);
846 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
847 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
848 redirected to destination of TAKEN_EDGE.
850 This function may leave the profile inconsistent in the case TAKEN_EDGE
851 frequency or count is believed to be lower than FREQUENCY or COUNT
852 respectively. */
853 void
854 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
855 gcov_type count, edge taken_edge)
857 edge c;
858 int prob;
859 edge_iterator ei;
861 bb->count -= count;
862 if (bb->count < 0)
863 bb->count = 0;
865 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
866 Watch for overflows. */
867 if (bb->frequency)
868 prob = edge_frequency * REG_BR_PROB_BASE / bb->frequency;
869 else
870 prob = 0;
871 if (prob > taken_edge->probability)
873 if (dump_file)
874 fprintf (dump_file, "Jump threading proved probability of edge "
875 "%i->%i too small (it is %i, should be %i).\n",
876 taken_edge->src->index, taken_edge->dest->index,
877 taken_edge->probability, prob);
878 prob = taken_edge->probability;
881 /* Now rescale the probabilities. */
882 taken_edge->probability -= prob;
883 prob = REG_BR_PROB_BASE - prob;
884 bb->frequency -= edge_frequency;
885 if (bb->frequency < 0)
886 bb->frequency = 0;
887 if (prob <= 0)
889 if (dump_file)
890 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
891 "frequency of block should end up being 0, it is %i\n",
892 bb->index, bb->frequency);
893 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
894 ei = ei_start (bb->succs);
895 ei_next (&ei);
896 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
897 c->probability = 0;
899 else if (prob != REG_BR_PROB_BASE)
901 int scale = 65536 * REG_BR_PROB_BASE / prob;
903 FOR_EACH_EDGE (c, ei, bb->succs)
904 c->probability = (c->probability * scale) / 65536;
907 gcc_assert (bb == taken_edge->src);
908 taken_edge->count -= count;
909 if (taken_edge->count < 0)
910 taken_edge->count = 0;
913 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
914 by NUM/DEN, in int arithmetic. May lose some accuracy. */
915 void
916 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
918 int i;
919 edge e;
920 for (i = 0; i < nbbs; i++)
922 edge_iterator ei;
923 bbs[i]->frequency = (bbs[i]->frequency * num) / den;
924 bbs[i]->count = RDIV (bbs[i]->count * num, den);
925 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
926 e->count = (e->count * num) /den;
930 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
931 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
932 function but considerably slower. */
933 void
934 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
935 gcov_type den)
937 int i;
938 edge e;
940 for (i = 0; i < nbbs; i++)
942 edge_iterator ei;
943 bbs[i]->frequency = (bbs[i]->frequency * num) / den;
944 bbs[i]->count = RDIV (bbs[i]->count * num, den);
945 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
946 e->count = (e->count * num) /den;
950 /* Datastructures used to maintain mapping between basic blocks and copies. */
951 static htab_t bb_original;
952 static htab_t bb_copy;
953 static alloc_pool original_copy_bb_pool;
955 struct htab_bb_copy_original_entry
957 /* Block we are attaching info to. */
958 int index1;
959 /* Index of original or copy (depending on the hashtable) */
960 int index2;
963 static hashval_t
964 bb_copy_original_hash (const void *p)
966 struct htab_bb_copy_original_entry *data
967 = ((struct htab_bb_copy_original_entry *)p);
969 return data->index1;
971 static int
972 bb_copy_original_eq (const void *p, const void *q)
974 struct htab_bb_copy_original_entry *data
975 = ((struct htab_bb_copy_original_entry *)p);
976 struct htab_bb_copy_original_entry *data2
977 = ((struct htab_bb_copy_original_entry *)q);
979 return data->index1 == data2->index1;
982 /* Initialize the datstructures to maintain mapping between blocks and it's copies. */
983 void
984 initialize_original_copy_tables (void)
986 gcc_assert (!original_copy_bb_pool);
987 original_copy_bb_pool
988 = create_alloc_pool ("original_copy",
989 sizeof (struct htab_bb_copy_original_entry), 10);
990 bb_original = htab_create (10, bb_copy_original_hash,
991 bb_copy_original_eq, NULL);
992 bb_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
995 /* Free the datstructures to maintain mapping between blocks and it's copies. */
996 void
997 free_original_copy_tables (void)
999 gcc_assert (original_copy_bb_pool);
1000 htab_delete (bb_copy);
1001 htab_delete (bb_original);
1002 free_alloc_pool (original_copy_bb_pool);
1003 bb_copy = NULL;
1004 bb_original = NULL;
1005 original_copy_bb_pool = NULL;
1008 /* Set original for basic block. Do nothing when datstructures are not
1009 intialized so passes not needing this don't need to care. */
1010 void
1011 set_bb_original (basic_block bb, basic_block original)
1013 if (original_copy_bb_pool)
1015 struct htab_bb_copy_original_entry **slot;
1016 struct htab_bb_copy_original_entry key;
1018 key.index1 = bb->index;
1019 slot =
1020 (struct htab_bb_copy_original_entry **) htab_find_slot (bb_original,
1021 &key, INSERT);
1022 if (*slot)
1023 (*slot)->index2 = original->index;
1024 else
1026 *slot = pool_alloc (original_copy_bb_pool);
1027 (*slot)->index1 = bb->index;
1028 (*slot)->index2 = original->index;
1033 /* Get the original basic block. */
1034 basic_block
1035 get_bb_original (basic_block bb)
1037 struct htab_bb_copy_original_entry *entry;
1038 struct htab_bb_copy_original_entry key;
1040 gcc_assert (original_copy_bb_pool);
1042 key.index1 = bb->index;
1043 entry = (struct htab_bb_copy_original_entry *) htab_find (bb_original, &key);
1044 if (entry)
1045 return BASIC_BLOCK (entry->index2);
1046 else
1047 return NULL;
1050 /* Set copy for basic block. Do nothing when datstructures are not
1051 intialized so passes not needing this don't need to care. */
1052 void
1053 set_bb_copy (basic_block bb, basic_block copy)
1055 if (original_copy_bb_pool)
1057 struct htab_bb_copy_original_entry **slot;
1058 struct htab_bb_copy_original_entry key;
1060 key.index1 = bb->index;
1061 slot =
1062 (struct htab_bb_copy_original_entry **) htab_find_slot (bb_copy,
1063 &key, INSERT);
1064 if (*slot)
1065 (*slot)->index2 = copy->index;
1066 else
1068 *slot = pool_alloc (original_copy_bb_pool);
1069 (*slot)->index1 = bb->index;
1070 (*slot)->index2 = copy->index;
1075 /* Get the copy of basic block. */
1076 basic_block
1077 get_bb_copy (basic_block bb)
1079 struct htab_bb_copy_original_entry *entry;
1080 struct htab_bb_copy_original_entry key;
1082 gcc_assert (original_copy_bb_pool);
1084 key.index1 = bb->index;
1085 entry = (struct htab_bb_copy_original_entry *) htab_find (bb_copy, &key);
1086 if (entry)
1087 return BASIC_BLOCK (entry->index2);
1088 else
1089 return NULL;