]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cfg.c
2017-05-23 Jan Hubicka <hubicka@ucw.cz>
[thirdparty/gcc.git] / gcc / cfg.c
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This file contains low level functions to manipulate the CFG and
21 analyze it. All other modules should not transform the data structure
22 directly and use abstraction instead. The file is supposed to be
23 ordered bottom-up and should not contain any code dependent on a
24 particular intermediate language (RTL or trees).
25
26 Available functionality:
27 - Initialization/deallocation
28 init_flow, clear_edges
29 - Low level basic block manipulation
30 alloc_block, expunge_block
31 - Edge manipulation
32 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
33 - Low level edge redirection (without updating instruction chain)
34 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
35 - Dumping and debugging
36 dump_flow_info, debug_flow_info, dump_edge_info
37 - Allocation of AUX fields for basic blocks
38 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
39 - clear_bb_flags
40 - Consistency checking
41 verify_flow_info
42 - Dumping and debugging
43 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
44
45 TODO: Document these "Available functionality" functions in the files
46 that implement them.
47 */
48 \f
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "backend.h"
53 #include "hard-reg-set.h"
54 #include "tree.h"
55 #include "cfghooks.h"
56 #include "df.h"
57 #include "cfganal.h"
58 #include "cfgloop.h" /* FIXME: For struct loop. */
59 #include "dumpfile.h"
60
61 \f
62
63 /* Called once at initialization time. */
64
65 void
66 init_flow (struct function *the_fun)
67 {
68 if (!the_fun->cfg)
69 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
70 n_edges_for_fn (the_fun) = 0;
71 ENTRY_BLOCK_PTR_FOR_FN (the_fun)
72 = alloc_block ();
73 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
74 EXIT_BLOCK_PTR_FOR_FN (the_fun)
75 = alloc_block ();
76 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
77 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
78 = EXIT_BLOCK_PTR_FOR_FN (the_fun);
79 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
80 = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
81 }
82 \f
83 /* Helper function for remove_edge and clear_edges. Frees edge structure
84 without actually removing it from the pred/succ arrays. */
85
86 static void
87 free_edge (function *fn, edge e)
88 {
89 n_edges_for_fn (fn)--;
90 ggc_free (e);
91 }
92
93 /* Free the memory associated with the edge structures. */
94
95 void
96 clear_edges (struct function *fn)
97 {
98 basic_block bb;
99 edge e;
100 edge_iterator ei;
101
102 FOR_EACH_BB_FN (bb, fn)
103 {
104 FOR_EACH_EDGE (e, ei, bb->succs)
105 free_edge (fn, e);
106 vec_safe_truncate (bb->succs, 0);
107 vec_safe_truncate (bb->preds, 0);
108 }
109
110 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fn)->succs)
111 free_edge (fn, e);
112 vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (fn)->preds, 0);
113 vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs, 0);
114
115 gcc_assert (!n_edges_for_fn (fn));
116 }
117 \f
118 /* Allocate memory for basic_block. */
119
120 basic_block
121 alloc_block (void)
122 {
123 basic_block bb;
124 bb = ggc_cleared_alloc<basic_block_def> ();
125 bb->count = profile_count::uninitialized ();
126 return bb;
127 }
128
129 /* Link block B to chain after AFTER. */
130 void
131 link_block (basic_block b, basic_block after)
132 {
133 b->next_bb = after->next_bb;
134 b->prev_bb = after;
135 after->next_bb = b;
136 b->next_bb->prev_bb = b;
137 }
138
139 /* Unlink block B from chain. */
140 void
141 unlink_block (basic_block b)
142 {
143 b->next_bb->prev_bb = b->prev_bb;
144 b->prev_bb->next_bb = b->next_bb;
145 b->prev_bb = NULL;
146 b->next_bb = NULL;
147 }
148
149 /* Sequentially order blocks and compact the arrays. */
150 void
151 compact_blocks (void)
152 {
153 int i;
154
155 SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
156 SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
157
158 if (df)
159 df_compact_blocks ();
160 else
161 {
162 basic_block bb;
163
164 i = NUM_FIXED_BLOCKS;
165 FOR_EACH_BB_FN (bb, cfun)
166 {
167 SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
168 bb->index = i;
169 i++;
170 }
171 gcc_assert (i == n_basic_blocks_for_fn (cfun));
172
173 for (; i < last_basic_block_for_fn (cfun); i++)
174 SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
175 }
176 last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
177 }
178
179 /* Remove block B from the basic block array. */
180
181 void
182 expunge_block (basic_block b)
183 {
184 unlink_block (b);
185 SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
186 n_basic_blocks_for_fn (cfun)--;
187 /* We should be able to ggc_free here, but we are not.
188 The dead SSA_NAMES are left pointing to dead statements that are pointing
189 to dead basic blocks making garbage collector to die.
190 We should be able to release all dead SSA_NAMES and at the same time we should
191 clear out BB pointer of dead statements consistently. */
192 }
193 \f
194 /* Connect E to E->src. */
195
196 static inline void
197 connect_src (edge e)
198 {
199 vec_safe_push (e->src->succs, e);
200 df_mark_solutions_dirty ();
201 }
202
203 /* Connect E to E->dest. */
204
205 static inline void
206 connect_dest (edge e)
207 {
208 basic_block dest = e->dest;
209 vec_safe_push (dest->preds, e);
210 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
211 df_mark_solutions_dirty ();
212 }
213
214 /* Disconnect edge E from E->src. */
215
216 static inline void
217 disconnect_src (edge e)
218 {
219 basic_block src = e->src;
220 edge_iterator ei;
221 edge tmp;
222
223 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
224 {
225 if (tmp == e)
226 {
227 src->succs->unordered_remove (ei.index);
228 df_mark_solutions_dirty ();
229 return;
230 }
231 else
232 ei_next (&ei);
233 }
234
235 gcc_unreachable ();
236 }
237
238 /* Disconnect edge E from E->dest. */
239
240 static inline void
241 disconnect_dest (edge e)
242 {
243 basic_block dest = e->dest;
244 unsigned int dest_idx = e->dest_idx;
245
246 dest->preds->unordered_remove (dest_idx);
247
248 /* If we removed an edge in the middle of the edge vector, we need
249 to update dest_idx of the edge that moved into the "hole". */
250 if (dest_idx < EDGE_COUNT (dest->preds))
251 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
252 df_mark_solutions_dirty ();
253 }
254
255 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
256 created edge. Use this only if you are sure that this edge can't
257 possibly already exist. */
258
259 edge
260 unchecked_make_edge (basic_block src, basic_block dst, int flags)
261 {
262 edge e;
263 e = ggc_cleared_alloc<edge_def> ();
264 n_edges_for_fn (cfun)++;
265
266 e->count = profile_count::uninitialized ();
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_FOR_FN (cfun)
286 || dst == EXIT_BLOCK_PTR_FOR_FN (cfun))
287 return make_edge (src, dst, flags);
288
289 /* Does the requested edge already exist? */
290 if (! bitmap_bit_p (edge_cache, dst->index))
291 {
292 /* The edge does not exist. Create one and update the
293 cache. */
294 bitmap_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 (cfun, 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 that do not have to be preserved. */
385 void
386 clear_bb_flags (void)
387 {
388 basic_block bb;
389
390 FOR_ALL_BB_FN (bb, cfun)
391 bb->flags &= BB_FLAGS_TO_PRESERVE;
392 }
393 \f
394 /* Check the consistency of profile information. We can't do that
395 in verify_flow_info, as the counts may get invalid for incompletely
396 solved graphs, later eliminating of conditionals or roundoff errors.
397 It is still practical to have them reported for debugging of simple
398 testcases. */
399 static void
400 check_bb_profile (basic_block bb, FILE * file, int indent)
401 {
402 edge e;
403 int sum = 0;
404 edge_iterator ei;
405 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
406 char *s_indent = (char *) alloca ((size_t) indent + 1);
407 memset ((void *) s_indent, ' ', (size_t) indent);
408 s_indent[indent] = '\0';
409
410 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
411 return;
412
413 if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
414 {
415 bool found = false;
416 FOR_EACH_EDGE (e, ei, bb->succs)
417 {
418 if (!(e->flags & EDGE_EH))
419 found = true;
420 sum += e->probability;
421 }
422 /* Only report mismatches for non-EH control flow. If there are only EH
423 edges it means that the BB ends by noreturn call. Here the control
424 flow may just terminate. */
425 if (found)
426 {
427 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
428 fprintf (file,
429 ";; %sInvalid sum of outgoing probabilities %.1f%%\n",
430 s_indent, sum * 100.0 / REG_BR_PROB_BASE);
431 profile_count lsum = profile_count::zero ();
432 FOR_EACH_EDGE (e, ei, bb->succs)
433 lsum += e->count;
434 if (EDGE_COUNT (bb->succs) && lsum.differs_from_p (bb->count))
435 {
436 fprintf (file, ";; %sInvalid sum of outgoing counts ",
437 s_indent);
438 lsum.dump (file);
439 fprintf (file, ", should be ");
440 bb->count.dump (file);
441 fprintf (file, "\n");
442 }
443 }
444 }
445 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
446 {
447 sum = 0;
448 FOR_EACH_EDGE (e, ei, bb->preds)
449 sum += EDGE_FREQUENCY (e);
450 if (abs (sum - bb->frequency) > 100)
451 fprintf (file,
452 ";; %sInvalid sum of incoming frequencies %i, should be %i\n",
453 s_indent, sum, bb->frequency);
454 profile_count lsum = profile_count::zero ();
455 FOR_EACH_EDGE (e, ei, bb->preds)
456 lsum += e->count;
457 if (lsum.differs_from_p (bb->count))
458 {
459 fprintf (file, ";; %sInvalid sum of incoming counts ",
460 s_indent);
461 lsum.dump (file);
462 fprintf (file, ", should be ");
463 bb->count.dump (file);
464 fprintf (file, "\n");
465 }
466 }
467 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
468 {
469 /* Warn about inconsistencies in the partitioning that are
470 currently caused by profile insanities created via optimization. */
471 if (!probably_never_executed_bb_p (fun, bb))
472 fprintf (file, ";; %sBlock in cold partition with hot count\n",
473 s_indent);
474 FOR_EACH_EDGE (e, ei, bb->preds)
475 {
476 if (!probably_never_executed_edge_p (fun, e))
477 fprintf (file,
478 ";; %sBlock in cold partition with incoming hot edge\n",
479 s_indent);
480 }
481 }
482 }
483 \f
484 void
485 dump_edge_info (FILE *file, edge e, dump_flags_t flags, int do_succ)
486 {
487 basic_block side = (do_succ ? e->dest : e->src);
488 bool do_details = false;
489
490 if ((flags & TDF_DETAILS) != 0
491 && (flags & TDF_SLIM) == 0)
492 do_details = true;
493
494 if (side->index == ENTRY_BLOCK)
495 fputs (" ENTRY", file);
496 else if (side->index == EXIT_BLOCK)
497 fputs (" EXIT", file);
498 else
499 fprintf (file, " %d", side->index);
500
501 if (e->probability && do_details)
502 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
503
504 if (e->count.initialized_p () && do_details)
505 {
506 fputs (" count:", file);
507 e->count.dump (file);
508 }
509
510 if (e->flags && do_details)
511 {
512 static const char * const bitnames[] =
513 {
514 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
515 #include "cfg-flags.def"
516 NULL
517 #undef DEF_EDGE_FLAG
518 };
519 bool comma = false;
520 int i, flags = e->flags;
521
522 gcc_assert (e->flags <= EDGE_ALL_FLAGS);
523 fputs (" (", file);
524 for (i = 0; flags; i++)
525 if (flags & (1 << i))
526 {
527 flags &= ~(1 << i);
528
529 if (comma)
530 fputc (',', file);
531 fputs (bitnames[i], file);
532 comma = true;
533 }
534
535 fputc (')', file);
536 }
537 }
538
539 DEBUG_FUNCTION void
540 debug (edge_def &ref)
541 {
542 /* FIXME (crowl): Is this desireable? */
543 dump_edge_info (stderr, &ref, 0, false);
544 dump_edge_info (stderr, &ref, 0, true);
545 }
546
547 DEBUG_FUNCTION void
548 debug (edge_def *ptr)
549 {
550 if (ptr)
551 debug (*ptr);
552 else
553 fprintf (stderr, "<nil>\n");
554 }
555 \f
556 /* Simple routines to easily allocate AUX fields of basic blocks. */
557
558 static struct obstack block_aux_obstack;
559 static void *first_block_aux_obj = 0;
560 static struct obstack edge_aux_obstack;
561 static void *first_edge_aux_obj = 0;
562
563 /* Allocate a memory block of SIZE as BB->aux. The obstack must
564 be first initialized by alloc_aux_for_blocks. */
565
566 static void
567 alloc_aux_for_block (basic_block bb, int size)
568 {
569 /* Verify that aux field is clear. */
570 gcc_assert (!bb->aux && first_block_aux_obj);
571 bb->aux = obstack_alloc (&block_aux_obstack, size);
572 memset (bb->aux, 0, size);
573 }
574
575 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
576 alloc_aux_for_block for each basic block. */
577
578 void
579 alloc_aux_for_blocks (int size)
580 {
581 static int initialized;
582
583 if (!initialized)
584 {
585 gcc_obstack_init (&block_aux_obstack);
586 initialized = 1;
587 }
588 else
589 /* Check whether AUX data are still allocated. */
590 gcc_assert (!first_block_aux_obj);
591
592 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
593 if (size)
594 {
595 basic_block bb;
596
597 FOR_ALL_BB_FN (bb, cfun)
598 alloc_aux_for_block (bb, size);
599 }
600 }
601
602 /* Clear AUX pointers of all blocks. */
603
604 void
605 clear_aux_for_blocks (void)
606 {
607 basic_block bb;
608
609 FOR_ALL_BB_FN (bb, cfun)
610 bb->aux = NULL;
611 }
612
613 /* Free data allocated in block_aux_obstack and clear AUX pointers
614 of all blocks. */
615
616 void
617 free_aux_for_blocks (void)
618 {
619 gcc_assert (first_block_aux_obj);
620 obstack_free (&block_aux_obstack, first_block_aux_obj);
621 first_block_aux_obj = NULL;
622
623 clear_aux_for_blocks ();
624 }
625
626 /* Allocate a memory edge of SIZE as E->aux. The obstack must
627 be first initialized by alloc_aux_for_edges. */
628
629 void
630 alloc_aux_for_edge (edge e, int size)
631 {
632 /* Verify that aux field is clear. */
633 gcc_assert (!e->aux && first_edge_aux_obj);
634 e->aux = obstack_alloc (&edge_aux_obstack, size);
635 memset (e->aux, 0, size);
636 }
637
638 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
639 alloc_aux_for_edge for each basic edge. */
640
641 void
642 alloc_aux_for_edges (int size)
643 {
644 static int initialized;
645
646 if (!initialized)
647 {
648 gcc_obstack_init (&edge_aux_obstack);
649 initialized = 1;
650 }
651 else
652 /* Check whether AUX data are still allocated. */
653 gcc_assert (!first_edge_aux_obj);
654
655 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
656 if (size)
657 {
658 basic_block bb;
659
660 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
661 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
662 {
663 edge e;
664 edge_iterator ei;
665
666 FOR_EACH_EDGE (e, ei, bb->succs)
667 alloc_aux_for_edge (e, size);
668 }
669 }
670 }
671
672 /* Clear AUX pointers of all edges. */
673
674 void
675 clear_aux_for_edges (void)
676 {
677 basic_block bb;
678 edge e;
679
680 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
681 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
682 {
683 edge_iterator ei;
684 FOR_EACH_EDGE (e, ei, bb->succs)
685 e->aux = NULL;
686 }
687 }
688
689 /* Free data allocated in edge_aux_obstack and clear AUX pointers
690 of all edges. */
691
692 void
693 free_aux_for_edges (void)
694 {
695 gcc_assert (first_edge_aux_obj);
696 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
697 first_edge_aux_obj = NULL;
698
699 clear_aux_for_edges ();
700 }
701
702 DEBUG_FUNCTION void
703 debug_bb (basic_block bb)
704 {
705 dump_bb (stderr, bb, 0, dump_flags);
706 }
707
708 DEBUG_FUNCTION basic_block
709 debug_bb_n (int n)
710 {
711 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
712 debug_bb (bb);
713 return bb;
714 }
715
716 /* Dumps cfg related information about basic block BB to OUTF.
717 If HEADER is true, dump things that appear before the instructions
718 contained in BB. If FOOTER is true, dump things that appear after.
719 Flags are the TDF_* masks as documented in dumpfile.h.
720 NB: With TDF_DETAILS, it is assumed that cfun is available, so
721 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
722
723 void
724 dump_bb_info (FILE *outf, basic_block bb, int indent, dump_flags_t flags,
725 bool do_header, bool do_footer)
726 {
727 edge_iterator ei;
728 edge e;
729 static const char * const bb_bitnames[] =
730 {
731 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
732 #include "cfg-flags.def"
733 NULL
734 #undef DEF_BASIC_BLOCK_FLAG
735 };
736 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
737 bool first;
738 char *s_indent = (char *) alloca ((size_t) indent + 1);
739 memset ((void *) s_indent, ' ', (size_t) indent);
740 s_indent[indent] = '\0';
741
742 gcc_assert (bb->flags <= BB_ALL_FLAGS);
743
744 if (do_header)
745 {
746 unsigned i;
747
748 fputs (";; ", outf);
749 fprintf (outf, "%sbasic block %d, loop depth %d",
750 s_indent, bb->index, bb_loop_depth (bb));
751 if (flags & TDF_DETAILS)
752 {
753 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
754 if (bb->count.initialized_p ())
755 {
756 fputs (", count ", outf);
757 bb->count.dump (outf);
758 }
759 fprintf (outf, ", freq %i", bb->frequency);
760 if (maybe_hot_bb_p (fun, bb))
761 fputs (", maybe hot", outf);
762 if (probably_never_executed_bb_p (fun, bb))
763 fputs (", probably never executed", outf);
764 }
765 fputc ('\n', outf);
766
767 if (flags & TDF_DETAILS)
768 {
769 check_bb_profile (bb, outf, indent);
770 fputs (";; ", outf);
771 fprintf (outf, "%s prev block ", s_indent);
772 if (bb->prev_bb)
773 fprintf (outf, "%d", bb->prev_bb->index);
774 else
775 fprintf (outf, "(nil)");
776 fprintf (outf, ", next block ");
777 if (bb->next_bb)
778 fprintf (outf, "%d", bb->next_bb->index);
779 else
780 fprintf (outf, "(nil)");
781
782 fputs (", flags:", outf);
783 first = true;
784 for (i = 0; i < n_bitnames; i++)
785 if (bb->flags & (1 << i))
786 {
787 if (first)
788 fputs (" (", outf);
789 else
790 fputs (", ", outf);
791 first = false;
792 fputs (bb_bitnames[i], outf);
793 }
794 if (!first)
795 fputc (')', outf);
796 fputc ('\n', outf);
797 }
798
799 fputs (";; ", outf);
800 fprintf (outf, "%s pred: ", s_indent);
801 first = true;
802 FOR_EACH_EDGE (e, ei, bb->preds)
803 {
804 if (! first)
805 {
806 fputs (";; ", outf);
807 fprintf (outf, "%s ", s_indent);
808 }
809 first = false;
810 dump_edge_info (outf, e, flags, 0);
811 fputc ('\n', outf);
812 }
813 if (first)
814 fputc ('\n', outf);
815 }
816
817 if (do_footer)
818 {
819 fputs (";; ", outf);
820 fprintf (outf, "%s succ: ", s_indent);
821 first = true;
822 FOR_EACH_EDGE (e, ei, bb->succs)
823 {
824 if (! first)
825 {
826 fputs (";; ", outf);
827 fprintf (outf, "%s ", s_indent);
828 }
829 first = false;
830 dump_edge_info (outf, e, flags, 1);
831 fputc ('\n', outf);
832 }
833 if (first)
834 fputc ('\n', outf);
835 }
836 }
837
838 /* Dumps a brief description of cfg to FILE. */
839
840 void
841 brief_dump_cfg (FILE *file, dump_flags_t flags)
842 {
843 basic_block bb;
844
845 FOR_EACH_BB_FN (bb, cfun)
846 {
847 dump_bb_info (file, bb, 0, flags & TDF_DETAILS, true, true);
848 }
849 }
850
851 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
852 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
853 redirected to destination of TAKEN_EDGE.
854
855 This function may leave the profile inconsistent in the case TAKEN_EDGE
856 frequency or count is believed to be lower than FREQUENCY or COUNT
857 respectively. */
858 void
859 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
860 profile_count count, edge taken_edge)
861 {
862 edge c;
863 int prob;
864 edge_iterator ei;
865
866 if (bb->count < count)
867 {
868 if (dump_file)
869 fprintf (dump_file, "bb %i count became negative after threading",
870 bb->index);
871 }
872 bb->count -= count;
873
874 bb->frequency -= edge_frequency;
875 if (bb->frequency < 0)
876 bb->frequency = 0;
877
878 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
879 Watch for overflows. */
880 if (bb->frequency)
881 prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
882 else
883 prob = 0;
884 if (prob > taken_edge->probability)
885 {
886 if (dump_file)
887 fprintf (dump_file, "Jump threading proved probability of edge "
888 "%i->%i too small (it is %i, should be %i).\n",
889 taken_edge->src->index, taken_edge->dest->index,
890 taken_edge->probability, prob);
891 prob = taken_edge->probability * 6 / 8;
892 }
893
894 /* Now rescale the probabilities. */
895 taken_edge->probability -= prob;
896 prob = REG_BR_PROB_BASE - prob;
897 if (prob <= 0)
898 {
899 if (dump_file)
900 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
901 "frequency of block should end up being 0, it is %i\n",
902 bb->index, bb->frequency);
903 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
904 ei = ei_start (bb->succs);
905 ei_next (&ei);
906 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
907 c->probability = 0;
908 }
909 else if (prob != REG_BR_PROB_BASE)
910 {
911 int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
912
913 FOR_EACH_EDGE (c, ei, bb->succs)
914 {
915 /* Protect from overflow due to additional scaling. */
916 if (c->probability > prob)
917 c->probability = REG_BR_PROB_BASE;
918 else
919 {
920 c->probability = RDIV (c->probability * scale, 65536);
921 if (c->probability > REG_BR_PROB_BASE)
922 c->probability = REG_BR_PROB_BASE;
923 }
924 }
925 }
926
927 gcc_assert (bb == taken_edge->src);
928 if (taken_edge->count < count)
929 {
930 if (dump_file)
931 fprintf (dump_file, "edge %i->%i count became negative after threading",
932 taken_edge->src->index, taken_edge->dest->index);
933 }
934 taken_edge->count -= count;
935 }
936
937 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
938 by NUM/DEN, in int arithmetic. May lose some accuracy. */
939 void
940 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
941 {
942 int i;
943 edge e;
944 if (num < 0)
945 num = 0;
946
947 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
948 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
949 and still safely fit in int during calculations. */
950 if (den > 1000)
951 {
952 if (num > 1000000)
953 return;
954
955 num = RDIV (1000 * num, den);
956 den = 1000;
957 }
958 if (num > 100 * den)
959 return;
960
961 for (i = 0; i < nbbs; i++)
962 {
963 edge_iterator ei;
964 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
965 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
966 if (bbs[i]->frequency > BB_FREQ_MAX)
967 bbs[i]->frequency = BB_FREQ_MAX;
968 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
969 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
970 e->count = e->count.apply_scale (num, den);
971 }
972 }
973
974 /* numbers smaller than this value are safe to multiply without getting
975 64bit overflow. */
976 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1))
977
978 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
979 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
980 function but considerably slower. */
981 void
982 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
983 gcov_type den)
984 {
985 int i;
986 edge e;
987 gcov_type fraction = RDIV (num * 65536, den);
988
989 gcc_assert (fraction >= 0);
990
991 if (num < MAX_SAFE_MULTIPLIER)
992 for (i = 0; i < nbbs; i++)
993 {
994 edge_iterator ei;
995 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
996 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
997 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
998 else
999 bbs[i]->count = bbs[i]->count.apply_scale (fraction, 65536);
1000 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1001 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
1002 e->count = e->count.apply_scale (num, den);
1003 else
1004 e->count = e->count.apply_scale (fraction, 65536);
1005 }
1006 else
1007 for (i = 0; i < nbbs; i++)
1008 {
1009 edge_iterator ei;
1010 if (sizeof (gcov_type) > sizeof (int))
1011 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1012 else
1013 bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
1014 bbs[i]->count = bbs[i]->count.apply_scale (fraction, 65536);
1015 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1016 e->count = e->count.apply_scale (fraction, 65536);
1017 }
1018 }
1019
1020 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1021 by NUM/DEN, in profile_count arithmetic. More accurate than previous
1022 function but considerably slower. */
1023 void
1024 scale_bbs_frequencies_profile_count (basic_block *bbs, int nbbs,
1025 profile_count num, profile_count den)
1026 {
1027 int i;
1028 edge e;
1029
1030 for (i = 0; i < nbbs; i++)
1031 {
1032 edge_iterator ei;
1033 bbs[i]->frequency = RDIV (bbs[i]->frequency * num.to_gcov_type (),
1034 den.to_gcov_type ());
1035 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
1036 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1037 e->count = e->count.apply_scale (num, den);
1038 }
1039 }
1040
1041 /* Helper types for hash tables. */
1042
1043 struct htab_bb_copy_original_entry
1044 {
1045 /* Block we are attaching info to. */
1046 int index1;
1047 /* Index of original or copy (depending on the hashtable) */
1048 int index2;
1049 };
1050
1051 struct bb_copy_hasher : nofree_ptr_hash <htab_bb_copy_original_entry>
1052 {
1053 static inline hashval_t hash (const htab_bb_copy_original_entry *);
1054 static inline bool equal (const htab_bb_copy_original_entry *existing,
1055 const htab_bb_copy_original_entry * candidate);
1056 };
1057
1058 inline hashval_t
1059 bb_copy_hasher::hash (const htab_bb_copy_original_entry *data)
1060 {
1061 return data->index1;
1062 }
1063
1064 inline bool
1065 bb_copy_hasher::equal (const htab_bb_copy_original_entry *data,
1066 const htab_bb_copy_original_entry *data2)
1067 {
1068 return data->index1 == data2->index1;
1069 }
1070
1071 /* Data structures used to maintain mapping between basic blocks and
1072 copies. */
1073 static hash_table<bb_copy_hasher> *bb_original;
1074 static hash_table<bb_copy_hasher> *bb_copy;
1075
1076 /* And between loops and copies. */
1077 static hash_table<bb_copy_hasher> *loop_copy;
1078 static object_allocator<htab_bb_copy_original_entry> *original_copy_bb_pool;
1079
1080 /* Initialize the data structures to maintain mapping between blocks
1081 and its copies. */
1082 void
1083 initialize_original_copy_tables (void)
1084 {
1085 original_copy_bb_pool = new object_allocator<htab_bb_copy_original_entry>
1086 ("original_copy");
1087 bb_original = new hash_table<bb_copy_hasher> (10);
1088 bb_copy = new hash_table<bb_copy_hasher> (10);
1089 loop_copy = new hash_table<bb_copy_hasher> (10);
1090 }
1091
1092 /* Reset the data structures to maintain mapping between blocks and
1093 its copies. */
1094
1095 void
1096 reset_original_copy_tables (void)
1097 {
1098 gcc_assert (original_copy_bb_pool);
1099 bb_original->empty ();
1100 bb_copy->empty ();
1101 loop_copy->empty ();
1102 }
1103
1104 /* Free the data structures to maintain mapping between blocks and
1105 its copies. */
1106 void
1107 free_original_copy_tables (void)
1108 {
1109 gcc_assert (original_copy_bb_pool);
1110 delete bb_copy;
1111 bb_copy = NULL;
1112 delete bb_original;
1113 bb_original = NULL;
1114 delete loop_copy;
1115 loop_copy = NULL;
1116 delete original_copy_bb_pool;
1117 original_copy_bb_pool = NULL;
1118 }
1119
1120 /* Return true iff we have had a call to initialize_original_copy_tables
1121 without a corresponding call to free_original_copy_tables. */
1122
1123 bool
1124 original_copy_tables_initialized_p (void)
1125 {
1126 return original_copy_bb_pool != NULL;
1127 }
1128
1129 /* Removes the value associated with OBJ from table TAB. */
1130
1131 static void
1132 copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj)
1133 {
1134 htab_bb_copy_original_entry **slot;
1135 struct htab_bb_copy_original_entry key, *elt;
1136
1137 if (!original_copy_bb_pool)
1138 return;
1139
1140 key.index1 = obj;
1141 slot = tab->find_slot (&key, NO_INSERT);
1142 if (!slot)
1143 return;
1144
1145 elt = *slot;
1146 tab->clear_slot (slot);
1147 original_copy_bb_pool->remove (elt);
1148 }
1149
1150 /* Sets the value associated with OBJ in table TAB to VAL.
1151 Do nothing when data structures are not initialized. */
1152
1153 static void
1154 copy_original_table_set (hash_table<bb_copy_hasher> *tab,
1155 unsigned obj, unsigned val)
1156 {
1157 struct htab_bb_copy_original_entry **slot;
1158 struct htab_bb_copy_original_entry key;
1159
1160 if (!original_copy_bb_pool)
1161 return;
1162
1163 key.index1 = obj;
1164 slot = tab->find_slot (&key, INSERT);
1165 if (!*slot)
1166 {
1167 *slot = original_copy_bb_pool->allocate ();
1168 (*slot)->index1 = obj;
1169 }
1170 (*slot)->index2 = val;
1171 }
1172
1173 /* Set original for basic block. Do nothing when data structures are not
1174 initialized so passes not needing this don't need to care. */
1175 void
1176 set_bb_original (basic_block bb, basic_block original)
1177 {
1178 copy_original_table_set (bb_original, bb->index, original->index);
1179 }
1180
1181 /* Get the original basic block. */
1182 basic_block
1183 get_bb_original (basic_block bb)
1184 {
1185 struct htab_bb_copy_original_entry *entry;
1186 struct htab_bb_copy_original_entry key;
1187
1188 gcc_assert (original_copy_bb_pool);
1189
1190 key.index1 = bb->index;
1191 entry = bb_original->find (&key);
1192 if (entry)
1193 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1194 else
1195 return NULL;
1196 }
1197
1198 /* Set copy for basic block. Do nothing when data structures are not
1199 initialized so passes not needing this don't need to care. */
1200 void
1201 set_bb_copy (basic_block bb, basic_block copy)
1202 {
1203 copy_original_table_set (bb_copy, bb->index, copy->index);
1204 }
1205
1206 /* Get the copy of basic block. */
1207 basic_block
1208 get_bb_copy (basic_block bb)
1209 {
1210 struct htab_bb_copy_original_entry *entry;
1211 struct htab_bb_copy_original_entry key;
1212
1213 gcc_assert (original_copy_bb_pool);
1214
1215 key.index1 = bb->index;
1216 entry = bb_copy->find (&key);
1217 if (entry)
1218 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1219 else
1220 return NULL;
1221 }
1222
1223 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1224 initialized so passes not needing this don't need to care. */
1225
1226 void
1227 set_loop_copy (struct loop *loop, struct loop *copy)
1228 {
1229 if (!copy)
1230 copy_original_table_clear (loop_copy, loop->num);
1231 else
1232 copy_original_table_set (loop_copy, loop->num, copy->num);
1233 }
1234
1235 /* Get the copy of LOOP. */
1236
1237 struct loop *
1238 get_loop_copy (struct loop *loop)
1239 {
1240 struct htab_bb_copy_original_entry *entry;
1241 struct htab_bb_copy_original_entry key;
1242
1243 gcc_assert (original_copy_bb_pool);
1244
1245 key.index1 = loop->num;
1246 entry = loop_copy->find (&key);
1247 if (entry)
1248 return get_loop (cfun, entry->index2);
1249 else
1250 return NULL;
1251 }