]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cfg.c
basic-block.h: Re-group most prototypes per file.
[thirdparty/gcc.git] / gcc / cfg.c
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, 2006, 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 /* This file contains low level functions to manipulate the CFG and
23 analyze it. All other modules should not transform the data structure
24 directly and use abstraction instead. The file is supposed to be
25 ordered bottom-up and should not contain any code dependent on a
26 particular intermediate language (RTL or trees).
27
28 Available functionality:
29 - Initialization/deallocation
30 init_flow, clear_edges
31 - Low level basic block manipulation
32 alloc_block, expunge_block
33 - Edge manipulation
34 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
35 - Low level edge redirection (without updating instruction chain)
36 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
37 - Dumping and debugging
38 dump_flow_info, debug_flow_info, dump_edge_info
39 - Allocation of AUX fields for basic blocks
40 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
41 - clear_bb_flags
42 - Consistency checking
43 verify_flow_info
44 - Dumping and debugging
45 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
46
47 TODO: Document these "Available functionality" functions in the files
48 that implement them.
49 */
50 \f
51 #include "config.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "obstack.h"
55 #include "ggc.h"
56 #include "hashtab.h"
57 #include "alloc-pool.h"
58 #include "basic-block.h"
59 #include "df.h"
60 #include "cfgloop.h" /* FIXME: For struct loop. */
61
62 \f
63 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
64
65 /* Called once at initialization time. */
66
67 void
68 init_flow (struct function *the_fun)
69 {
70 if (!the_fun->cfg)
71 the_fun->cfg = ggc_alloc_cleared_control_flow_graph ();
72 n_edges_for_function (the_fun) = 0;
73 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)
74 = ggc_alloc_cleared_basic_block_def ();
75 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)->index = ENTRY_BLOCK;
76 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)
77 = ggc_alloc_cleared_basic_block_def ();
78 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)->index = EXIT_BLOCK;
79 ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)->next_bb
80 = EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun);
81 EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)->prev_bb
82 = ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun);
83 }
84 \f
85 /* Helper function for remove_edge and clear_edges. Frees edge structure
86 without actually removing it from the pred/succ arrays. */
87
88 static void
89 free_edge (edge e)
90 {
91 n_edges--;
92 ggc_free (e);
93 }
94
95 /* Free the memory associated with the edge structures. */
96
97 void
98 clear_edges (void)
99 {
100 basic_block bb;
101 edge e;
102 edge_iterator ei;
103
104 FOR_EACH_BB (bb)
105 {
106 FOR_EACH_EDGE (e, ei, bb->succs)
107 free_edge (e);
108 VEC_truncate (edge, bb->succs, 0);
109 VEC_truncate (edge, bb->preds, 0);
110 }
111
112 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
113 free_edge (e);
114 VEC_truncate (edge, EXIT_BLOCK_PTR->preds, 0);
115 VEC_truncate (edge, ENTRY_BLOCK_PTR->succs, 0);
116
117 gcc_assert (!n_edges);
118 }
119 \f
120 /* Allocate memory for basic_block. */
121
122 basic_block
123 alloc_block (void)
124 {
125 basic_block bb;
126 bb = ggc_alloc_cleared_basic_block_def ();
127 return bb;
128 }
129
130 /* Link block B to chain after AFTER. */
131 void
132 link_block (basic_block b, basic_block after)
133 {
134 b->next_bb = after->next_bb;
135 b->prev_bb = after;
136 after->next_bb = b;
137 b->next_bb->prev_bb = b;
138 }
139
140 /* Unlink block B from chain. */
141 void
142 unlink_block (basic_block b)
143 {
144 b->next_bb->prev_bb = b->prev_bb;
145 b->prev_bb->next_bb = b->next_bb;
146 b->prev_bb = NULL;
147 b->next_bb = NULL;
148 }
149
150 /* Sequentially order blocks and compact the arrays. */
151 void
152 compact_blocks (void)
153 {
154 int i;
155
156 SET_BASIC_BLOCK (ENTRY_BLOCK, ENTRY_BLOCK_PTR);
157 SET_BASIC_BLOCK (EXIT_BLOCK, EXIT_BLOCK_PTR);
158
159 if (df)
160 df_compact_blocks ();
161 else
162 {
163 basic_block bb;
164
165 i = NUM_FIXED_BLOCKS;
166 FOR_EACH_BB (bb)
167 {
168 SET_BASIC_BLOCK (i, bb);
169 bb->index = i;
170 i++;
171 }
172 gcc_assert (i == n_basic_blocks);
173
174 for (; i < last_basic_block; i++)
175 SET_BASIC_BLOCK (i, NULL);
176 }
177 last_basic_block = n_basic_blocks;
178 }
179
180 /* Remove block B from the basic block array. */
181
182 void
183 expunge_block (basic_block b)
184 {
185 unlink_block (b);
186 SET_BASIC_BLOCK (b->index, NULL);
187 n_basic_blocks--;
188 /* We should be able to ggc_free here, but we are not.
189 The dead SSA_NAMES are left pointing to dead statements that are pointing
190 to dead basic blocks making garbage collector to die.
191 We should be able to release all dead SSA_NAMES and at the same time we should
192 clear out BB pointer of dead statements consistently. */
193 }
194 \f
195 /* Connect E to E->src. */
196
197 static inline void
198 connect_src (edge e)
199 {
200 VEC_safe_push (edge, gc, e->src->succs, e);
201 df_mark_solutions_dirty ();
202 }
203
204 /* Connect E to E->dest. */
205
206 static inline void
207 connect_dest (edge e)
208 {
209 basic_block dest = e->dest;
210 VEC_safe_push (edge, gc, dest->preds, e);
211 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
212 df_mark_solutions_dirty ();
213 }
214
215 /* Disconnect edge E from E->src. */
216
217 static inline void
218 disconnect_src (edge e)
219 {
220 basic_block src = e->src;
221 edge_iterator ei;
222 edge tmp;
223
224 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
225 {
226 if (tmp == e)
227 {
228 VEC_unordered_remove (edge, src->succs, ei.index);
229 df_mark_solutions_dirty ();
230 return;
231 }
232 else
233 ei_next (&ei);
234 }
235
236 gcc_unreachable ();
237 }
238
239 /* Disconnect edge E from E->dest. */
240
241 static inline void
242 disconnect_dest (edge e)
243 {
244 basic_block dest = e->dest;
245 unsigned int dest_idx = e->dest_idx;
246
247 VEC_unordered_remove (edge, dest->preds, dest_idx);
248
249 /* If we removed an edge in the middle of the edge vector, we need
250 to update dest_idx of the edge that moved into the "hole". */
251 if (dest_idx < EDGE_COUNT (dest->preds))
252 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
253 df_mark_solutions_dirty ();
254 }
255
256 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
257 created edge. Use this only if you are sure that this edge can't
258 possibly already exist. */
259
260 edge
261 unchecked_make_edge (basic_block src, basic_block dst, int flags)
262 {
263 edge e;
264 e = ggc_alloc_cleared_edge_def ();
265 n_edges++;
266
267 e->src = src;
268 e->dest = dst;
269 e->flags = flags;
270
271 connect_src (e);
272 connect_dest (e);
273
274 execute_on_growing_pred (e);
275 return e;
276 }
277
278 /* Create an edge connecting SRC and DST with FLAGS optionally using
279 edge cache CACHE. Return the new edge, NULL if already exist. */
280
281 edge
282 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
283 {
284 if (edge_cache == NULL
285 || src == ENTRY_BLOCK_PTR
286 || dst == EXIT_BLOCK_PTR)
287 return make_edge (src, dst, flags);
288
289 /* Does the requested edge already exist? */
290 if (! TEST_BIT (edge_cache, dst->index))
291 {
292 /* The edge does not exist. Create one and update the
293 cache. */
294 SET_BIT (edge_cache, dst->index);
295 return unchecked_make_edge (src, dst, flags);
296 }
297
298 /* At this point, we know that the requested edge exists. Adjust
299 flags if necessary. */
300 if (flags)
301 {
302 edge e = find_edge (src, dst);
303 e->flags |= flags;
304 }
305
306 return NULL;
307 }
308
309 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
310 created edge or NULL if already exist. */
311
312 edge
313 make_edge (basic_block src, basic_block dest, int flags)
314 {
315 edge e = find_edge (src, dest);
316
317 /* Make sure we don't add duplicate edges. */
318 if (e)
319 {
320 e->flags |= flags;
321 return NULL;
322 }
323
324 return unchecked_make_edge (src, dest, flags);
325 }
326
327 /* Create an edge connecting SRC to DEST and set probability by knowing
328 that it is the single edge leaving SRC. */
329
330 edge
331 make_single_succ_edge (basic_block src, basic_block dest, int flags)
332 {
333 edge e = make_edge (src, dest, flags);
334
335 e->probability = REG_BR_PROB_BASE;
336 e->count = src->count;
337 return e;
338 }
339
340 /* This function will remove an edge from the flow graph. */
341
342 void
343 remove_edge_raw (edge e)
344 {
345 remove_predictions_associated_with_edge (e);
346 execute_on_shrinking_pred (e);
347
348 disconnect_src (e);
349 disconnect_dest (e);
350
351 free_edge (e);
352 }
353
354 /* Redirect an edge's successor from one block to another. */
355
356 void
357 redirect_edge_succ (edge e, basic_block new_succ)
358 {
359 execute_on_shrinking_pred (e);
360
361 disconnect_dest (e);
362
363 e->dest = new_succ;
364
365 /* Reconnect the edge to the new successor block. */
366 connect_dest (e);
367
368 execute_on_growing_pred (e);
369 }
370
371 /* Redirect an edge's predecessor from one block to another. */
372
373 void
374 redirect_edge_pred (edge e, basic_block new_pred)
375 {
376 disconnect_src (e);
377
378 e->src = new_pred;
379
380 /* Reconnect the edge to the new predecessor block. */
381 connect_src (e);
382 }
383
384 /* Clear all basic block flags, with the exception of partitioning and
385 setjmp_target. */
386 void
387 clear_bb_flags (void)
388 {
389 basic_block bb;
390
391 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
392 bb->flags = (BB_PARTITION (bb)
393 | (bb->flags & (BB_DISABLE_SCHEDULE + BB_RTL + BB_NON_LOCAL_GOTO_TARGET)));
394 }
395 \f
396 /* Check the consistency of profile information. We can't do that
397 in verify_flow_info, as the counts may get invalid for incompletely
398 solved graphs, later eliminating of conditionals or roundoff errors.
399 It is still practical to have them reported for debugging of simple
400 testcases. */
401 void
402 check_bb_profile (basic_block bb, FILE * file)
403 {
404 edge e;
405 int sum = 0;
406 gcov_type lsum;
407 edge_iterator ei;
408
409 if (profile_status == PROFILE_ABSENT)
410 return;
411
412 if (bb != EXIT_BLOCK_PTR)
413 {
414 FOR_EACH_EDGE (e, ei, bb->succs)
415 sum += e->probability;
416 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
417 fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
418 sum * 100.0 / REG_BR_PROB_BASE);
419 lsum = 0;
420 FOR_EACH_EDGE (e, ei, bb->succs)
421 lsum += e->count;
422 if (EDGE_COUNT (bb->succs)
423 && (lsum - bb->count > 100 || lsum - bb->count < -100))
424 fprintf (file, "Invalid sum of outgoing counts %i, should be %i\n",
425 (int) lsum, (int) bb->count);
426 }
427 if (bb != ENTRY_BLOCK_PTR)
428 {
429 sum = 0;
430 FOR_EACH_EDGE (e, ei, bb->preds)
431 sum += EDGE_FREQUENCY (e);
432 if (abs (sum - bb->frequency) > 100)
433 fprintf (file,
434 "Invalid sum of incoming frequencies %i, should be %i\n",
435 sum, bb->frequency);
436 lsum = 0;
437 FOR_EACH_EDGE (e, ei, bb->preds)
438 lsum += e->count;
439 if (lsum - bb->count > 100 || lsum - bb->count < -100)
440 fprintf (file, "Invalid sum of incoming counts %i, should be %i\n",
441 (int) lsum, (int) bb->count);
442 }
443 }
444 \f
445 void
446 dump_edge_info (FILE *file, edge e, int do_succ)
447 {
448 basic_block side = (do_succ ? e->dest : e->src);
449 /* ENTRY_BLOCK_PTR/EXIT_BLOCK_PTR depend on cfun.
450 Compare against ENTRY_BLOCK/EXIT_BLOCK to avoid that dependency. */
451 if (side->index == ENTRY_BLOCK)
452 fputs (" ENTRY", file);
453 else if (side->index == EXIT_BLOCK)
454 fputs (" EXIT", file);
455 else
456 fprintf (file, " %d", side->index);
457
458 if (e->probability)
459 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
460
461 if (e->count)
462 {
463 fputs (" count:", file);
464 fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
465 }
466
467 if (e->flags)
468 {
469 static const char * const bitnames[] = {
470 "fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
471 "can_fallthru", "irreducible", "sibcall", "loop_exit",
472 "true", "false", "exec", "crossing", "preserve"
473 };
474 int comma = 0;
475 int i, flags = e->flags;
476
477 fputs (" (", file);
478 for (i = 0; flags; i++)
479 if (flags & (1 << i))
480 {
481 flags &= ~(1 << i);
482
483 if (comma)
484 fputc (',', file);
485 if (i < (int) ARRAY_SIZE (bitnames))
486 fputs (bitnames[i], file);
487 else
488 fprintf (file, "%d", i);
489 comma = 1;
490 }
491
492 fputc (')', file);
493 }
494 }
495 \f
496 /* Simple routines to easily allocate AUX fields of basic blocks. */
497
498 static struct obstack block_aux_obstack;
499 static void *first_block_aux_obj = 0;
500 static struct obstack edge_aux_obstack;
501 static void *first_edge_aux_obj = 0;
502
503 /* Allocate a memory block of SIZE as BB->aux. The obstack must
504 be first initialized by alloc_aux_for_blocks. */
505
506 static void
507 alloc_aux_for_block (basic_block bb, int size)
508 {
509 /* Verify that aux field is clear. */
510 gcc_assert (!bb->aux && first_block_aux_obj);
511 bb->aux = obstack_alloc (&block_aux_obstack, size);
512 memset (bb->aux, 0, size);
513 }
514
515 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
516 alloc_aux_for_block for each basic block. */
517
518 void
519 alloc_aux_for_blocks (int size)
520 {
521 static int initialized;
522
523 if (!initialized)
524 {
525 gcc_obstack_init (&block_aux_obstack);
526 initialized = 1;
527 }
528 else
529 /* Check whether AUX data are still allocated. */
530 gcc_assert (!first_block_aux_obj);
531
532 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
533 if (size)
534 {
535 basic_block bb;
536
537 FOR_ALL_BB (bb)
538 alloc_aux_for_block (bb, size);
539 }
540 }
541
542 /* Clear AUX pointers of all blocks. */
543
544 void
545 clear_aux_for_blocks (void)
546 {
547 basic_block bb;
548
549 FOR_ALL_BB (bb)
550 bb->aux = NULL;
551 }
552
553 /* Free data allocated in block_aux_obstack and clear AUX pointers
554 of all blocks. */
555
556 void
557 free_aux_for_blocks (void)
558 {
559 gcc_assert (first_block_aux_obj);
560 obstack_free (&block_aux_obstack, first_block_aux_obj);
561 first_block_aux_obj = NULL;
562
563 clear_aux_for_blocks ();
564 }
565
566 /* Allocate a memory edge of SIZE as E->aux. The obstack must
567 be first initialized by alloc_aux_for_edges. */
568
569 void
570 alloc_aux_for_edge (edge e, int size)
571 {
572 /* Verify that aux field is clear. */
573 gcc_assert (!e->aux && first_edge_aux_obj);
574 e->aux = obstack_alloc (&edge_aux_obstack, size);
575 memset (e->aux, 0, size);
576 }
577
578 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
579 alloc_aux_for_edge for each basic edge. */
580
581 void
582 alloc_aux_for_edges (int size)
583 {
584 static int initialized;
585
586 if (!initialized)
587 {
588 gcc_obstack_init (&edge_aux_obstack);
589 initialized = 1;
590 }
591 else
592 /* Check whether AUX data are still allocated. */
593 gcc_assert (!first_edge_aux_obj);
594
595 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
596 if (size)
597 {
598 basic_block bb;
599
600 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
601 {
602 edge e;
603 edge_iterator ei;
604
605 FOR_EACH_EDGE (e, ei, bb->succs)
606 alloc_aux_for_edge (e, size);
607 }
608 }
609 }
610
611 /* Clear AUX pointers of all edges. */
612
613 void
614 clear_aux_for_edges (void)
615 {
616 basic_block bb;
617 edge e;
618
619 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
620 {
621 edge_iterator ei;
622 FOR_EACH_EDGE (e, ei, bb->succs)
623 e->aux = NULL;
624 }
625 }
626
627 /* Free data allocated in edge_aux_obstack and clear AUX pointers
628 of all edges. */
629
630 void
631 free_aux_for_edges (void)
632 {
633 gcc_assert (first_edge_aux_obj);
634 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
635 first_edge_aux_obj = NULL;
636
637 clear_aux_for_edges ();
638 }
639
640 DEBUG_FUNCTION void
641 debug_bb (basic_block bb)
642 {
643 dump_bb (bb, stderr, 0);
644 }
645
646 DEBUG_FUNCTION basic_block
647 debug_bb_n (int n)
648 {
649 basic_block bb = BASIC_BLOCK (n);
650 dump_bb (bb, stderr, 0);
651 return bb;
652 }
653
654 /* Dumps cfg related information about basic block BB to FILE. */
655
656 static void
657 dump_cfg_bb_info (FILE *file, basic_block bb)
658 {
659 unsigned i;
660 edge_iterator ei;
661 bool first = true;
662 static const char * const bb_bitnames[] =
663 {
664 "new", "reachable", "irreducible_loop", "superblock",
665 "nosched", "hot", "cold", "dup", "xlabel", "rtl",
666 "fwdr", "nothrd"
667 };
668 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
669 edge e;
670
671 fprintf (file, "Basic block %d", bb->index);
672 for (i = 0; i < n_bitnames; i++)
673 if (bb->flags & (1 << i))
674 {
675 if (first)
676 fputs (" (", file);
677 else
678 fputs (", ", file);
679 first = false;
680 fputs (bb_bitnames[i], file);
681 }
682 if (!first)
683 putc (')', file);
684 putc ('\n', file);
685
686 fputs ("Predecessors: ", file);
687 FOR_EACH_EDGE (e, ei, bb->preds)
688 dump_edge_info (file, e, 0);
689
690 fprintf (file, "\nSuccessors: ");
691 FOR_EACH_EDGE (e, ei, bb->succs)
692 dump_edge_info (file, e, 1);
693 fputs ("\n\n", file);
694 }
695
696 /* Dumps a brief description of cfg to FILE. */
697
698 void
699 brief_dump_cfg (FILE *file)
700 {
701 basic_block bb;
702
703 FOR_EACH_BB (bb)
704 {
705 dump_cfg_bb_info (file, bb);
706 }
707 }
708
709 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
710 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
711 redirected to destination of TAKEN_EDGE.
712
713 This function may leave the profile inconsistent in the case TAKEN_EDGE
714 frequency or count is believed to be lower than FREQUENCY or COUNT
715 respectively. */
716 void
717 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
718 gcov_type count, edge taken_edge)
719 {
720 edge c;
721 int prob;
722 edge_iterator ei;
723
724 bb->count -= count;
725 if (bb->count < 0)
726 {
727 if (dump_file)
728 fprintf (dump_file, "bb %i count became negative after threading",
729 bb->index);
730 bb->count = 0;
731 }
732
733 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
734 Watch for overflows. */
735 if (bb->frequency)
736 prob = edge_frequency * REG_BR_PROB_BASE / bb->frequency;
737 else
738 prob = 0;
739 if (prob > taken_edge->probability)
740 {
741 if (dump_file)
742 fprintf (dump_file, "Jump threading proved probability of edge "
743 "%i->%i too small (it is %i, should be %i).\n",
744 taken_edge->src->index, taken_edge->dest->index,
745 taken_edge->probability, prob);
746 prob = taken_edge->probability;
747 }
748
749 /* Now rescale the probabilities. */
750 taken_edge->probability -= prob;
751 prob = REG_BR_PROB_BASE - prob;
752 bb->frequency -= edge_frequency;
753 if (bb->frequency < 0)
754 bb->frequency = 0;
755 if (prob <= 0)
756 {
757 if (dump_file)
758 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
759 "frequency of block should end up being 0, it is %i\n",
760 bb->index, bb->frequency);
761 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
762 ei = ei_start (bb->succs);
763 ei_next (&ei);
764 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
765 c->probability = 0;
766 }
767 else if (prob != REG_BR_PROB_BASE)
768 {
769 int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
770
771 FOR_EACH_EDGE (c, ei, bb->succs)
772 {
773 /* Protect from overflow due to additional scaling. */
774 if (c->probability > prob)
775 c->probability = REG_BR_PROB_BASE;
776 else
777 {
778 c->probability = RDIV (c->probability * scale, 65536);
779 if (c->probability > REG_BR_PROB_BASE)
780 c->probability = REG_BR_PROB_BASE;
781 }
782 }
783 }
784
785 gcc_assert (bb == taken_edge->src);
786 taken_edge->count -= count;
787 if (taken_edge->count < 0)
788 {
789 if (dump_file)
790 fprintf (dump_file, "edge %i->%i count became negative after threading",
791 taken_edge->src->index, taken_edge->dest->index);
792 taken_edge->count = 0;
793 }
794 }
795
796 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
797 by NUM/DEN, in int arithmetic. May lose some accuracy. */
798 void
799 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
800 {
801 int i;
802 edge e;
803 if (num < 0)
804 num = 0;
805
806 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
807 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
808 and still safely fit in int during calculations. */
809 if (den > 1000)
810 {
811 if (num > 1000000)
812 return;
813
814 num = RDIV (1000 * num, den);
815 den = 1000;
816 }
817 if (num > 100 * den)
818 return;
819
820 for (i = 0; i < nbbs; i++)
821 {
822 edge_iterator ei;
823 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
824 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
825 if (bbs[i]->frequency > BB_FREQ_MAX)
826 bbs[i]->frequency = BB_FREQ_MAX;
827 bbs[i]->count = RDIV (bbs[i]->count * num, den);
828 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
829 e->count = RDIV (e->count * num, den);
830 }
831 }
832
833 /* numbers smaller than this value are safe to multiply without getting
834 64bit overflow. */
835 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
836
837 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
838 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
839 function but considerably slower. */
840 void
841 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
842 gcov_type den)
843 {
844 int i;
845 edge e;
846 gcov_type fraction = RDIV (num * 65536, den);
847
848 gcc_assert (fraction >= 0);
849
850 if (num < MAX_SAFE_MULTIPLIER)
851 for (i = 0; i < nbbs; i++)
852 {
853 edge_iterator ei;
854 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
855 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
856 bbs[i]->count = RDIV (bbs[i]->count * num, den);
857 else
858 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
859 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
860 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
861 e->count = RDIV (e->count * num, den);
862 else
863 e->count = RDIV (e->count * fraction, 65536);
864 }
865 else
866 for (i = 0; i < nbbs; i++)
867 {
868 edge_iterator ei;
869 if (sizeof (gcov_type) > sizeof (int))
870 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
871 else
872 bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
873 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
874 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
875 e->count = RDIV (e->count * fraction, 65536);
876 }
877 }
878
879 /* Data structures used to maintain mapping between basic blocks and
880 copies. */
881 static htab_t bb_original;
882 static htab_t bb_copy;
883
884 /* And between loops and copies. */
885 static htab_t loop_copy;
886 static alloc_pool original_copy_bb_pool;
887
888 struct htab_bb_copy_original_entry
889 {
890 /* Block we are attaching info to. */
891 int index1;
892 /* Index of original or copy (depending on the hashtable) */
893 int index2;
894 };
895
896 static hashval_t
897 bb_copy_original_hash (const void *p)
898 {
899 const struct htab_bb_copy_original_entry *data
900 = ((const struct htab_bb_copy_original_entry *)p);
901
902 return data->index1;
903 }
904 static int
905 bb_copy_original_eq (const void *p, const void *q)
906 {
907 const struct htab_bb_copy_original_entry *data
908 = ((const struct htab_bb_copy_original_entry *)p);
909 const struct htab_bb_copy_original_entry *data2
910 = ((const struct htab_bb_copy_original_entry *)q);
911
912 return data->index1 == data2->index1;
913 }
914
915 /* Initialize the data structures to maintain mapping between blocks
916 and its copies. */
917 void
918 initialize_original_copy_tables (void)
919 {
920 gcc_assert (!original_copy_bb_pool);
921 original_copy_bb_pool
922 = create_alloc_pool ("original_copy",
923 sizeof (struct htab_bb_copy_original_entry), 10);
924 bb_original = htab_create (10, bb_copy_original_hash,
925 bb_copy_original_eq, NULL);
926 bb_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
927 loop_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
928 }
929
930 /* Free the data structures to maintain mapping between blocks and
931 its copies. */
932 void
933 free_original_copy_tables (void)
934 {
935 gcc_assert (original_copy_bb_pool);
936 htab_delete (bb_copy);
937 htab_delete (bb_original);
938 htab_delete (loop_copy);
939 free_alloc_pool (original_copy_bb_pool);
940 bb_copy = NULL;
941 bb_original = NULL;
942 loop_copy = NULL;
943 original_copy_bb_pool = NULL;
944 }
945
946 /* Removes the value associated with OBJ from table TAB. */
947
948 static void
949 copy_original_table_clear (htab_t tab, unsigned obj)
950 {
951 void **slot;
952 struct htab_bb_copy_original_entry key, *elt;
953
954 if (!original_copy_bb_pool)
955 return;
956
957 key.index1 = obj;
958 slot = htab_find_slot (tab, &key, NO_INSERT);
959 if (!slot)
960 return;
961
962 elt = (struct htab_bb_copy_original_entry *) *slot;
963 htab_clear_slot (tab, slot);
964 pool_free (original_copy_bb_pool, elt);
965 }
966
967 /* Sets the value associated with OBJ in table TAB to VAL.
968 Do nothing when data structures are not initialized. */
969
970 static void
971 copy_original_table_set (htab_t tab, unsigned obj, unsigned val)
972 {
973 struct htab_bb_copy_original_entry **slot;
974 struct htab_bb_copy_original_entry key;
975
976 if (!original_copy_bb_pool)
977 return;
978
979 key.index1 = obj;
980 slot = (struct htab_bb_copy_original_entry **)
981 htab_find_slot (tab, &key, INSERT);
982 if (!*slot)
983 {
984 *slot = (struct htab_bb_copy_original_entry *)
985 pool_alloc (original_copy_bb_pool);
986 (*slot)->index1 = obj;
987 }
988 (*slot)->index2 = val;
989 }
990
991 /* Set original for basic block. Do nothing when data structures are not
992 initialized so passes not needing this don't need to care. */
993 void
994 set_bb_original (basic_block bb, basic_block original)
995 {
996 copy_original_table_set (bb_original, bb->index, original->index);
997 }
998
999 /* Get the original basic block. */
1000 basic_block
1001 get_bb_original (basic_block bb)
1002 {
1003 struct htab_bb_copy_original_entry *entry;
1004 struct htab_bb_copy_original_entry key;
1005
1006 gcc_assert (original_copy_bb_pool);
1007
1008 key.index1 = bb->index;
1009 entry = (struct htab_bb_copy_original_entry *) htab_find (bb_original, &key);
1010 if (entry)
1011 return BASIC_BLOCK (entry->index2);
1012 else
1013 return NULL;
1014 }
1015
1016 /* Set copy for basic block. Do nothing when data structures are not
1017 initialized so passes not needing this don't need to care. */
1018 void
1019 set_bb_copy (basic_block bb, basic_block copy)
1020 {
1021 copy_original_table_set (bb_copy, bb->index, copy->index);
1022 }
1023
1024 /* Get the copy of basic block. */
1025 basic_block
1026 get_bb_copy (basic_block bb)
1027 {
1028 struct htab_bb_copy_original_entry *entry;
1029 struct htab_bb_copy_original_entry key;
1030
1031 gcc_assert (original_copy_bb_pool);
1032
1033 key.index1 = bb->index;
1034 entry = (struct htab_bb_copy_original_entry *) htab_find (bb_copy, &key);
1035 if (entry)
1036 return BASIC_BLOCK (entry->index2);
1037 else
1038 return NULL;
1039 }
1040
1041 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1042 initialized so passes not needing this don't need to care. */
1043
1044 void
1045 set_loop_copy (struct loop *loop, struct loop *copy)
1046 {
1047 if (!copy)
1048 copy_original_table_clear (loop_copy, loop->num);
1049 else
1050 copy_original_table_set (loop_copy, loop->num, copy->num);
1051 }
1052
1053 /* Get the copy of LOOP. */
1054
1055 struct loop *
1056 get_loop_copy (struct loop *loop)
1057 {
1058 struct htab_bb_copy_original_entry *entry;
1059 struct htab_bb_copy_original_entry key;
1060
1061 gcc_assert (original_copy_bb_pool);
1062
1063 key.index1 = loop->num;
1064 entry = (struct htab_bb_copy_original_entry *) htab_find (loop_copy, &key);
1065 if (entry)
1066 return get_loop (entry->index2);
1067 else
1068 return NULL;
1069 }