re PR debug/66691 (ICE on valid code at -O3 with -g enabled in simplify_subreg, at...
[gcc.git] / gcc / cfg.c
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2015 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 "obstack.h"
53 #include "alloc-pool.h"
54 #include "alias.h"
55 #include "symtab.h"
56 #include "options.h"
57 #include "tree.h"
58 #include "predict.h"
59 #include "tm.h"
60 #include "hard-reg-set.h"
61 #include "function.h"
62 #include "dominance.h"
63 #include "cfg.h"
64 #include "cfganal.h"
65 #include "basic-block.h"
66 #include "df.h"
67 #include "cfgloop.h" /* FIXME: For struct loop. */
68 #include "dumpfile.h"
69
70 \f
71 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
72
73 /* Called once at initialization time. */
74
75 void
76 init_flow (struct function *the_fun)
77 {
78 if (!the_fun->cfg)
79 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
80 n_edges_for_fn (the_fun) = 0;
81 ENTRY_BLOCK_PTR_FOR_FN (the_fun)
82 = ggc_cleared_alloc<basic_block_def> ();
83 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
84 EXIT_BLOCK_PTR_FOR_FN (the_fun)
85 = ggc_cleared_alloc<basic_block_def> ();
86 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
87 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
88 = EXIT_BLOCK_PTR_FOR_FN (the_fun);
89 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
90 = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
91 }
92 \f
93 /* Helper function for remove_edge and clear_edges. Frees edge structure
94 without actually removing it from the pred/succ arrays. */
95
96 static void
97 free_edge (edge e)
98 {
99 n_edges_for_fn (cfun)--;
100 ggc_free (e);
101 }
102
103 /* Free the memory associated with the edge structures. */
104
105 void
106 clear_edges (void)
107 {
108 basic_block bb;
109 edge e;
110 edge_iterator ei;
111
112 FOR_EACH_BB_FN (bb, cfun)
113 {
114 FOR_EACH_EDGE (e, ei, bb->succs)
115 free_edge (e);
116 vec_safe_truncate (bb->succs, 0);
117 vec_safe_truncate (bb->preds, 0);
118 }
119
120 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
121 free_edge (e);
122 vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, 0);
123 vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs, 0);
124
125 gcc_assert (!n_edges_for_fn (cfun));
126 }
127 \f
128 /* Allocate memory for basic_block. */
129
130 basic_block
131 alloc_block (void)
132 {
133 basic_block bb;
134 bb = ggc_cleared_alloc<basic_block_def> ();
135 return bb;
136 }
137
138 /* Link block B to chain after AFTER. */
139 void
140 link_block (basic_block b, basic_block after)
141 {
142 b->next_bb = after->next_bb;
143 b->prev_bb = after;
144 after->next_bb = b;
145 b->next_bb->prev_bb = b;
146 }
147
148 /* Unlink block B from chain. */
149 void
150 unlink_block (basic_block b)
151 {
152 b->next_bb->prev_bb = b->prev_bb;
153 b->prev_bb->next_bb = b->next_bb;
154 b->prev_bb = NULL;
155 b->next_bb = NULL;
156 }
157
158 /* Sequentially order blocks and compact the arrays. */
159 void
160 compact_blocks (void)
161 {
162 int i;
163
164 SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
165 SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
166
167 if (df)
168 df_compact_blocks ();
169 else
170 {
171 basic_block bb;
172
173 i = NUM_FIXED_BLOCKS;
174 FOR_EACH_BB_FN (bb, cfun)
175 {
176 SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
177 bb->index = i;
178 i++;
179 }
180 gcc_assert (i == n_basic_blocks_for_fn (cfun));
181
182 for (; i < last_basic_block_for_fn (cfun); i++)
183 SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
184 }
185 last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
186 }
187
188 /* Remove block B from the basic block array. */
189
190 void
191 expunge_block (basic_block b)
192 {
193 unlink_block (b);
194 SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
195 n_basic_blocks_for_fn (cfun)--;
196 /* We should be able to ggc_free here, but we are not.
197 The dead SSA_NAMES are left pointing to dead statements that are pointing
198 to dead basic blocks making garbage collector to die.
199 We should be able to release all dead SSA_NAMES and at the same time we should
200 clear out BB pointer of dead statements consistently. */
201 }
202 \f
203 /* Connect E to E->src. */
204
205 static inline void
206 connect_src (edge e)
207 {
208 vec_safe_push (e->src->succs, e);
209 df_mark_solutions_dirty ();
210 }
211
212 /* Connect E to E->dest. */
213
214 static inline void
215 connect_dest (edge e)
216 {
217 basic_block dest = e->dest;
218 vec_safe_push (dest->preds, e);
219 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
220 df_mark_solutions_dirty ();
221 }
222
223 /* Disconnect edge E from E->src. */
224
225 static inline void
226 disconnect_src (edge e)
227 {
228 basic_block src = e->src;
229 edge_iterator ei;
230 edge tmp;
231
232 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
233 {
234 if (tmp == e)
235 {
236 src->succs->unordered_remove (ei.index);
237 df_mark_solutions_dirty ();
238 return;
239 }
240 else
241 ei_next (&ei);
242 }
243
244 gcc_unreachable ();
245 }
246
247 /* Disconnect edge E from E->dest. */
248
249 static inline void
250 disconnect_dest (edge e)
251 {
252 basic_block dest = e->dest;
253 unsigned int dest_idx = e->dest_idx;
254
255 dest->preds->unordered_remove (dest_idx);
256
257 /* If we removed an edge in the middle of the edge vector, we need
258 to update dest_idx of the edge that moved into the "hole". */
259 if (dest_idx < EDGE_COUNT (dest->preds))
260 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
261 df_mark_solutions_dirty ();
262 }
263
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. */
267
268 edge
269 unchecked_make_edge (basic_block src, basic_block dst, int flags)
270 {
271 edge e;
272 e = ggc_cleared_alloc<edge_def> ();
273 n_edges_for_fn (cfun)++;
274
275 e->src = src;
276 e->dest = dst;
277 e->flags = flags;
278
279 connect_src (e);
280 connect_dest (e);
281
282 execute_on_growing_pred (e);
283 return e;
284 }
285
286 /* Create an edge connecting SRC and DST with FLAGS optionally using
287 edge cache CACHE. Return the new edge, NULL if already exist. */
288
289 edge
290 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
291 {
292 if (edge_cache == NULL
293 || src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
294 || dst == EXIT_BLOCK_PTR_FOR_FN (cfun))
295 return make_edge (src, dst, flags);
296
297 /* Does the requested edge already exist? */
298 if (! bitmap_bit_p (edge_cache, dst->index))
299 {
300 /* The edge does not exist. Create one and update the
301 cache. */
302 bitmap_set_bit (edge_cache, dst->index);
303 return unchecked_make_edge (src, dst, flags);
304 }
305
306 /* At this point, we know that the requested edge exists. Adjust
307 flags if necessary. */
308 if (flags)
309 {
310 edge e = find_edge (src, dst);
311 e->flags |= flags;
312 }
313
314 return NULL;
315 }
316
317 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
318 created edge or NULL if already exist. */
319
320 edge
321 make_edge (basic_block src, basic_block dest, int flags)
322 {
323 edge e = find_edge (src, dest);
324
325 /* Make sure we don't add duplicate edges. */
326 if (e)
327 {
328 e->flags |= flags;
329 return NULL;
330 }
331
332 return unchecked_make_edge (src, dest, flags);
333 }
334
335 /* Create an edge connecting SRC to DEST and set probability by knowing
336 that it is the single edge leaving SRC. */
337
338 edge
339 make_single_succ_edge (basic_block src, basic_block dest, int flags)
340 {
341 edge e = make_edge (src, dest, flags);
342
343 e->probability = REG_BR_PROB_BASE;
344 e->count = src->count;
345 return e;
346 }
347
348 /* This function will remove an edge from the flow graph. */
349
350 void
351 remove_edge_raw (edge e)
352 {
353 remove_predictions_associated_with_edge (e);
354 execute_on_shrinking_pred (e);
355
356 disconnect_src (e);
357 disconnect_dest (e);
358
359 free_edge (e);
360 }
361
362 /* Redirect an edge's successor from one block to another. */
363
364 void
365 redirect_edge_succ (edge e, basic_block new_succ)
366 {
367 execute_on_shrinking_pred (e);
368
369 disconnect_dest (e);
370
371 e->dest = new_succ;
372
373 /* Reconnect the edge to the new successor block. */
374 connect_dest (e);
375
376 execute_on_growing_pred (e);
377 }
378
379 /* Redirect an edge's predecessor from one block to another. */
380
381 void
382 redirect_edge_pred (edge e, basic_block new_pred)
383 {
384 disconnect_src (e);
385
386 e->src = new_pred;
387
388 /* Reconnect the edge to the new predecessor block. */
389 connect_src (e);
390 }
391
392 /* Clear all basic block flags that do not have to be preserved. */
393 void
394 clear_bb_flags (void)
395 {
396 basic_block bb;
397
398 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
399 bb->flags &= BB_FLAGS_TO_PRESERVE;
400 }
401 \f
402 /* Check the consistency of profile information. We can't do that
403 in verify_flow_info, as the counts may get invalid for incompletely
404 solved graphs, later eliminating of conditionals or roundoff errors.
405 It is still practical to have them reported for debugging of simple
406 testcases. */
407 static void
408 check_bb_profile (basic_block bb, FILE * file, int indent, int flags)
409 {
410 edge e;
411 int sum = 0;
412 gcov_type lsum;
413 edge_iterator ei;
414 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
415 char *s_indent = (char *) alloca ((size_t) indent + 1);
416 memset ((void *) s_indent, ' ', (size_t) indent);
417 s_indent[indent] = '\0';
418
419 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
420 return;
421
422 if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
423 {
424 FOR_EACH_EDGE (e, ei, bb->succs)
425 sum += e->probability;
426 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
427 fprintf (file, "%s%sInvalid sum of outgoing probabilities %.1f%%\n",
428 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
429 sum * 100.0 / REG_BR_PROB_BASE);
430 lsum = 0;
431 FOR_EACH_EDGE (e, ei, bb->succs)
432 lsum += e->count;
433 if (EDGE_COUNT (bb->succs)
434 && (lsum - bb->count > 100 || lsum - bb->count < -100))
435 fprintf (file, "%s%sInvalid sum of outgoing counts %i, should be %i\n",
436 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
437 (int) lsum, (int) bb->count);
438 }
439 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
440 {
441 sum = 0;
442 FOR_EACH_EDGE (e, ei, bb->preds)
443 sum += EDGE_FREQUENCY (e);
444 if (abs (sum - bb->frequency) > 100)
445 fprintf (file,
446 "%s%sInvalid sum of incoming frequencies %i, should be %i\n",
447 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
448 sum, bb->frequency);
449 lsum = 0;
450 FOR_EACH_EDGE (e, ei, bb->preds)
451 lsum += e->count;
452 if (lsum - bb->count > 100 || lsum - bb->count < -100)
453 fprintf (file, "%s%sInvalid sum of incoming counts %i, should be %i\n",
454 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
455 (int) lsum, (int) bb->count);
456 }
457 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
458 {
459 /* Warn about inconsistencies in the partitioning that are
460 currently caused by profile insanities created via optimization. */
461 if (!probably_never_executed_bb_p (fun, bb))
462 fprintf (file, "%s%sBlock in cold partition with hot count\n",
463 (flags & TDF_COMMENT) ? ";; " : "", s_indent);
464 FOR_EACH_EDGE (e, ei, bb->preds)
465 {
466 if (!probably_never_executed_edge_p (fun, e))
467 fprintf (file,
468 "%s%sBlock in cold partition with incoming hot edge\n",
469 (flags & TDF_COMMENT) ? ";; " : "", s_indent);
470 }
471 }
472 }
473 \f
474 void
475 dump_edge_info (FILE *file, edge e, int flags, int do_succ)
476 {
477 basic_block side = (do_succ ? e->dest : e->src);
478 bool do_details = false;
479
480 if ((flags & TDF_DETAILS) != 0
481 && (flags & TDF_SLIM) == 0)
482 do_details = true;
483
484 if (side->index == ENTRY_BLOCK)
485 fputs (" ENTRY", file);
486 else if (side->index == EXIT_BLOCK)
487 fputs (" EXIT", file);
488 else
489 fprintf (file, " %d", side->index);
490
491 if (e->probability && do_details)
492 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
493
494 if (e->count && do_details)
495 {
496 fputs (" count:", file);
497 fprintf (file, "%" PRId64, e->count);
498 }
499
500 if (e->flags && do_details)
501 {
502 static const char * const bitnames[] =
503 {
504 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
505 #include "cfg-flags.def"
506 NULL
507 #undef DEF_EDGE_FLAG
508 };
509 bool comma = false;
510 int i, flags = e->flags;
511
512 gcc_assert (e->flags <= EDGE_ALL_FLAGS);
513 fputs (" (", file);
514 for (i = 0; flags; i++)
515 if (flags & (1 << i))
516 {
517 flags &= ~(1 << i);
518
519 if (comma)
520 fputc (',', file);
521 fputs (bitnames[i], file);
522 comma = true;
523 }
524
525 fputc (')', file);
526 }
527 }
528
529 DEBUG_FUNCTION void
530 debug (edge_def &ref)
531 {
532 /* FIXME (crowl): Is this desireable? */
533 dump_edge_info (stderr, &ref, 0, false);
534 dump_edge_info (stderr, &ref, 0, true);
535 }
536
537 DEBUG_FUNCTION void
538 debug (edge_def *ptr)
539 {
540 if (ptr)
541 debug (*ptr);
542 else
543 fprintf (stderr, "<nil>\n");
544 }
545 \f
546 /* Simple routines to easily allocate AUX fields of basic blocks. */
547
548 static struct obstack block_aux_obstack;
549 static void *first_block_aux_obj = 0;
550 static struct obstack edge_aux_obstack;
551 static void *first_edge_aux_obj = 0;
552
553 /* Allocate a memory block of SIZE as BB->aux. The obstack must
554 be first initialized by alloc_aux_for_blocks. */
555
556 static void
557 alloc_aux_for_block (basic_block bb, int size)
558 {
559 /* Verify that aux field is clear. */
560 gcc_assert (!bb->aux && first_block_aux_obj);
561 bb->aux = obstack_alloc (&block_aux_obstack, size);
562 memset (bb->aux, 0, size);
563 }
564
565 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
566 alloc_aux_for_block for each basic block. */
567
568 void
569 alloc_aux_for_blocks (int size)
570 {
571 static int initialized;
572
573 if (!initialized)
574 {
575 gcc_obstack_init (&block_aux_obstack);
576 initialized = 1;
577 }
578 else
579 /* Check whether AUX data are still allocated. */
580 gcc_assert (!first_block_aux_obj);
581
582 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
583 if (size)
584 {
585 basic_block bb;
586
587 FOR_ALL_BB_FN (bb, cfun)
588 alloc_aux_for_block (bb, size);
589 }
590 }
591
592 /* Clear AUX pointers of all blocks. */
593
594 void
595 clear_aux_for_blocks (void)
596 {
597 basic_block bb;
598
599 FOR_ALL_BB_FN (bb, cfun)
600 bb->aux = NULL;
601 }
602
603 /* Free data allocated in block_aux_obstack and clear AUX pointers
604 of all blocks. */
605
606 void
607 free_aux_for_blocks (void)
608 {
609 gcc_assert (first_block_aux_obj);
610 obstack_free (&block_aux_obstack, first_block_aux_obj);
611 first_block_aux_obj = NULL;
612
613 clear_aux_for_blocks ();
614 }
615
616 /* Allocate a memory edge of SIZE as E->aux. The obstack must
617 be first initialized by alloc_aux_for_edges. */
618
619 void
620 alloc_aux_for_edge (edge e, int size)
621 {
622 /* Verify that aux field is clear. */
623 gcc_assert (!e->aux && first_edge_aux_obj);
624 e->aux = obstack_alloc (&edge_aux_obstack, size);
625 memset (e->aux, 0, size);
626 }
627
628 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
629 alloc_aux_for_edge for each basic edge. */
630
631 void
632 alloc_aux_for_edges (int size)
633 {
634 static int initialized;
635
636 if (!initialized)
637 {
638 gcc_obstack_init (&edge_aux_obstack);
639 initialized = 1;
640 }
641 else
642 /* Check whether AUX data are still allocated. */
643 gcc_assert (!first_edge_aux_obj);
644
645 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
646 if (size)
647 {
648 basic_block bb;
649
650 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
651 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
652 {
653 edge e;
654 edge_iterator ei;
655
656 FOR_EACH_EDGE (e, ei, bb->succs)
657 alloc_aux_for_edge (e, size);
658 }
659 }
660 }
661
662 /* Clear AUX pointers of all edges. */
663
664 void
665 clear_aux_for_edges (void)
666 {
667 basic_block bb;
668 edge e;
669
670 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
671 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
672 {
673 edge_iterator ei;
674 FOR_EACH_EDGE (e, ei, bb->succs)
675 e->aux = NULL;
676 }
677 }
678
679 /* Free data allocated in edge_aux_obstack and clear AUX pointers
680 of all edges. */
681
682 void
683 free_aux_for_edges (void)
684 {
685 gcc_assert (first_edge_aux_obj);
686 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
687 first_edge_aux_obj = NULL;
688
689 clear_aux_for_edges ();
690 }
691
692 DEBUG_FUNCTION void
693 debug_bb (basic_block bb)
694 {
695 dump_bb (stderr, bb, 0, dump_flags);
696 }
697
698 DEBUG_FUNCTION basic_block
699 debug_bb_n (int n)
700 {
701 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
702 debug_bb (bb);
703 return bb;
704 }
705
706 /* Dumps cfg related information about basic block BB to OUTF.
707 If HEADER is true, dump things that appear before the instructions
708 contained in BB. If FOOTER is true, dump things that appear after.
709 Flags are the TDF_* masks as documented in dumpfile.h.
710 NB: With TDF_DETAILS, it is assumed that cfun is available, so
711 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
712
713 void
714 dump_bb_info (FILE *outf, basic_block bb, int indent, int flags,
715 bool do_header, bool do_footer)
716 {
717 edge_iterator ei;
718 edge e;
719 static const char * const bb_bitnames[] =
720 {
721 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
722 #include "cfg-flags.def"
723 NULL
724 #undef DEF_BASIC_BLOCK_FLAG
725 };
726 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
727 bool first;
728 char *s_indent = (char *) alloca ((size_t) indent + 1);
729 memset ((void *) s_indent, ' ', (size_t) indent);
730 s_indent[indent] = '\0';
731
732 gcc_assert (bb->flags <= BB_ALL_FLAGS);
733
734 if (do_header)
735 {
736 unsigned i;
737
738 if (flags & TDF_COMMENT)
739 fputs (";; ", outf);
740 fprintf (outf, "%sbasic block %d, loop depth %d",
741 s_indent, bb->index, bb_loop_depth (bb));
742 if (flags & TDF_DETAILS)
743 {
744 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
745 fprintf (outf, ", count " "%" PRId64,
746 (int64_t) bb->count);
747 fprintf (outf, ", freq %i", bb->frequency);
748 if (maybe_hot_bb_p (fun, bb))
749 fputs (", maybe hot", outf);
750 if (probably_never_executed_bb_p (fun, bb))
751 fputs (", probably never executed", outf);
752 }
753 fputc ('\n', outf);
754
755 if (flags & TDF_DETAILS)
756 {
757 check_bb_profile (bb, outf, indent, flags);
758 if (flags & TDF_COMMENT)
759 fputs (";; ", outf);
760 fprintf (outf, "%s prev block ", s_indent);
761 if (bb->prev_bb)
762 fprintf (outf, "%d", bb->prev_bb->index);
763 else
764 fprintf (outf, "(nil)");
765 fprintf (outf, ", next block ");
766 if (bb->next_bb)
767 fprintf (outf, "%d", bb->next_bb->index);
768 else
769 fprintf (outf, "(nil)");
770
771 fputs (", flags:", outf);
772 first = true;
773 for (i = 0; i < n_bitnames; i++)
774 if (bb->flags & (1 << i))
775 {
776 if (first)
777 fputs (" (", outf);
778 else
779 fputs (", ", outf);
780 first = false;
781 fputs (bb_bitnames[i], outf);
782 }
783 if (!first)
784 fputc (')', outf);
785 fputc ('\n', outf);
786 }
787
788 if (flags & TDF_COMMENT)
789 fputs (";; ", outf);
790 fprintf (outf, "%s pred: ", s_indent);
791 first = true;
792 FOR_EACH_EDGE (e, ei, bb->preds)
793 {
794 if (! first)
795 {
796 if (flags & TDF_COMMENT)
797 fputs (";; ", outf);
798 fprintf (outf, "%s ", s_indent);
799 }
800 first = false;
801 dump_edge_info (outf, e, flags, 0);
802 fputc ('\n', outf);
803 }
804 if (first)
805 fputc ('\n', outf);
806 }
807
808 if (do_footer)
809 {
810 if (flags & TDF_COMMENT)
811 fputs (";; ", outf);
812 fprintf (outf, "%s succ: ", s_indent);
813 first = true;
814 FOR_EACH_EDGE (e, ei, bb->succs)
815 {
816 if (! first)
817 {
818 if (flags & TDF_COMMENT)
819 fputs (";; ", outf);
820 fprintf (outf, "%s ", s_indent);
821 }
822 first = false;
823 dump_edge_info (outf, e, flags, 1);
824 fputc ('\n', outf);
825 }
826 if (first)
827 fputc ('\n', outf);
828 }
829 }
830
831 /* Dumps a brief description of cfg to FILE. */
832
833 void
834 brief_dump_cfg (FILE *file, int flags)
835 {
836 basic_block bb;
837
838 FOR_EACH_BB_FN (bb, cfun)
839 {
840 dump_bb_info (file, bb, 0,
841 flags & (TDF_COMMENT | TDF_DETAILS),
842 true, true);
843 }
844 }
845
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.
849
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)
856 {
857 edge c;
858 int prob;
859 edge_iterator ei;
860
861 bb->count -= count;
862 if (bb->count < 0)
863 {
864 if (dump_file)
865 fprintf (dump_file, "bb %i count became negative after threading",
866 bb->index);
867 bb->count = 0;
868 }
869
870 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
871 Watch for overflows. */
872 if (bb->frequency)
873 prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
874 else
875 prob = 0;
876 if (prob > taken_edge->probability)
877 {
878 if (dump_file)
879 fprintf (dump_file, "Jump threading proved probability of edge "
880 "%i->%i too small (it is %i, should be %i).\n",
881 taken_edge->src->index, taken_edge->dest->index,
882 taken_edge->probability, prob);
883 prob = taken_edge->probability;
884 }
885
886 /* Now rescale the probabilities. */
887 taken_edge->probability -= prob;
888 prob = REG_BR_PROB_BASE - prob;
889 bb->frequency -= edge_frequency;
890 if (bb->frequency < 0)
891 bb->frequency = 0;
892 if (prob <= 0)
893 {
894 if (dump_file)
895 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
896 "frequency of block should end up being 0, it is %i\n",
897 bb->index, bb->frequency);
898 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
899 ei = ei_start (bb->succs);
900 ei_next (&ei);
901 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
902 c->probability = 0;
903 }
904 else if (prob != REG_BR_PROB_BASE)
905 {
906 int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
907
908 FOR_EACH_EDGE (c, ei, bb->succs)
909 {
910 /* Protect from overflow due to additional scaling. */
911 if (c->probability > prob)
912 c->probability = REG_BR_PROB_BASE;
913 else
914 {
915 c->probability = RDIV (c->probability * scale, 65536);
916 if (c->probability > REG_BR_PROB_BASE)
917 c->probability = REG_BR_PROB_BASE;
918 }
919 }
920 }
921
922 gcc_assert (bb == taken_edge->src);
923 taken_edge->count -= count;
924 if (taken_edge->count < 0)
925 {
926 if (dump_file)
927 fprintf (dump_file, "edge %i->%i count became negative after threading",
928 taken_edge->src->index, taken_edge->dest->index);
929 taken_edge->count = 0;
930 }
931 }
932
933 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
934 by NUM/DEN, in int arithmetic. May lose some accuracy. */
935 void
936 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
937 {
938 int i;
939 edge e;
940 if (num < 0)
941 num = 0;
942
943 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
944 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
945 and still safely fit in int during calculations. */
946 if (den > 1000)
947 {
948 if (num > 1000000)
949 return;
950
951 num = RDIV (1000 * num, den);
952 den = 1000;
953 }
954 if (num > 100 * den)
955 return;
956
957 for (i = 0; i < nbbs; i++)
958 {
959 edge_iterator ei;
960 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
961 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
962 if (bbs[i]->frequency > BB_FREQ_MAX)
963 bbs[i]->frequency = BB_FREQ_MAX;
964 bbs[i]->count = RDIV (bbs[i]->count * num, den);
965 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
966 e->count = RDIV (e->count * num, den);
967 }
968 }
969
970 /* numbers smaller than this value are safe to multiply without getting
971 64bit overflow. */
972 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1))
973
974 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
975 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
976 function but considerably slower. */
977 void
978 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
979 gcov_type den)
980 {
981 int i;
982 edge e;
983 gcov_type fraction = RDIV (num * 65536, den);
984
985 gcc_assert (fraction >= 0);
986
987 if (num < MAX_SAFE_MULTIPLIER)
988 for (i = 0; i < nbbs; i++)
989 {
990 edge_iterator ei;
991 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
992 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
993 bbs[i]->count = RDIV (bbs[i]->count * num, den);
994 else
995 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
996 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
997 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
998 e->count = RDIV (e->count * num, den);
999 else
1000 e->count = RDIV (e->count * fraction, 65536);
1001 }
1002 else
1003 for (i = 0; i < nbbs; i++)
1004 {
1005 edge_iterator ei;
1006 if (sizeof (gcov_type) > sizeof (int))
1007 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1008 else
1009 bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
1010 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
1011 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1012 e->count = RDIV (e->count * fraction, 65536);
1013 }
1014 }
1015
1016 /* Helper types for hash tables. */
1017
1018 struct htab_bb_copy_original_entry
1019 {
1020 /* Block we are attaching info to. */
1021 int index1;
1022 /* Index of original or copy (depending on the hashtable) */
1023 int index2;
1024 };
1025
1026 struct bb_copy_hasher : nofree_ptr_hash <htab_bb_copy_original_entry>
1027 {
1028 static inline hashval_t hash (const htab_bb_copy_original_entry *);
1029 static inline bool equal (const htab_bb_copy_original_entry *existing,
1030 const htab_bb_copy_original_entry * candidate);
1031 };
1032
1033 inline hashval_t
1034 bb_copy_hasher::hash (const htab_bb_copy_original_entry *data)
1035 {
1036 return data->index1;
1037 }
1038
1039 inline bool
1040 bb_copy_hasher::equal (const htab_bb_copy_original_entry *data,
1041 const htab_bb_copy_original_entry *data2)
1042 {
1043 return data->index1 == data2->index1;
1044 }
1045
1046 /* Data structures used to maintain mapping between basic blocks and
1047 copies. */
1048 static hash_table<bb_copy_hasher> *bb_original;
1049 static hash_table<bb_copy_hasher> *bb_copy;
1050
1051 /* And between loops and copies. */
1052 static hash_table<bb_copy_hasher> *loop_copy;
1053 static pool_allocator<htab_bb_copy_original_entry> *original_copy_bb_pool;
1054
1055 /* Initialize the data structures to maintain mapping between blocks
1056 and its copies. */
1057 void
1058 initialize_original_copy_tables (void)
1059 {
1060
1061 original_copy_bb_pool = new pool_allocator<htab_bb_copy_original_entry>
1062 ("original_copy", 10);
1063 bb_original = new hash_table<bb_copy_hasher> (10);
1064 bb_copy = new hash_table<bb_copy_hasher> (10);
1065 loop_copy = new hash_table<bb_copy_hasher> (10);
1066 }
1067
1068 /* Free the data structures to maintain mapping between blocks and
1069 its copies. */
1070 void
1071 free_original_copy_tables (void)
1072 {
1073 gcc_assert (original_copy_bb_pool);
1074 delete bb_copy;
1075 bb_copy = NULL;
1076 delete bb_original;
1077 bb_copy = NULL;
1078 delete loop_copy;
1079 loop_copy = NULL;
1080 delete original_copy_bb_pool;
1081 original_copy_bb_pool = NULL;
1082 }
1083
1084 /* Removes the value associated with OBJ from table TAB. */
1085
1086 static void
1087 copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj)
1088 {
1089 htab_bb_copy_original_entry **slot;
1090 struct htab_bb_copy_original_entry key, *elt;
1091
1092 if (!original_copy_bb_pool)
1093 return;
1094
1095 key.index1 = obj;
1096 slot = tab->find_slot (&key, NO_INSERT);
1097 if (!slot)
1098 return;
1099
1100 elt = *slot;
1101 tab->clear_slot (slot);
1102 original_copy_bb_pool->remove (elt);
1103 }
1104
1105 /* Sets the value associated with OBJ in table TAB to VAL.
1106 Do nothing when data structures are not initialized. */
1107
1108 static void
1109 copy_original_table_set (hash_table<bb_copy_hasher> *tab,
1110 unsigned obj, unsigned val)
1111 {
1112 struct htab_bb_copy_original_entry **slot;
1113 struct htab_bb_copy_original_entry key;
1114
1115 if (!original_copy_bb_pool)
1116 return;
1117
1118 key.index1 = obj;
1119 slot = tab->find_slot (&key, INSERT);
1120 if (!*slot)
1121 {
1122 *slot = original_copy_bb_pool->allocate ();
1123 (*slot)->index1 = obj;
1124 }
1125 (*slot)->index2 = val;
1126 }
1127
1128 /* Set original for basic block. Do nothing when data structures are not
1129 initialized so passes not needing this don't need to care. */
1130 void
1131 set_bb_original (basic_block bb, basic_block original)
1132 {
1133 copy_original_table_set (bb_original, bb->index, original->index);
1134 }
1135
1136 /* Get the original basic block. */
1137 basic_block
1138 get_bb_original (basic_block bb)
1139 {
1140 struct htab_bb_copy_original_entry *entry;
1141 struct htab_bb_copy_original_entry key;
1142
1143 gcc_assert (original_copy_bb_pool);
1144
1145 key.index1 = bb->index;
1146 entry = bb_original->find (&key);
1147 if (entry)
1148 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1149 else
1150 return NULL;
1151 }
1152
1153 /* Set copy for basic block. Do nothing when data structures are not
1154 initialized so passes not needing this don't need to care. */
1155 void
1156 set_bb_copy (basic_block bb, basic_block copy)
1157 {
1158 copy_original_table_set (bb_copy, bb->index, copy->index);
1159 }
1160
1161 /* Get the copy of basic block. */
1162 basic_block
1163 get_bb_copy (basic_block bb)
1164 {
1165 struct htab_bb_copy_original_entry *entry;
1166 struct htab_bb_copy_original_entry key;
1167
1168 gcc_assert (original_copy_bb_pool);
1169
1170 key.index1 = bb->index;
1171 entry = bb_copy->find (&key);
1172 if (entry)
1173 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1174 else
1175 return NULL;
1176 }
1177
1178 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1179 initialized so passes not needing this don't need to care. */
1180
1181 void
1182 set_loop_copy (struct loop *loop, struct loop *copy)
1183 {
1184 if (!copy)
1185 copy_original_table_clear (loop_copy, loop->num);
1186 else
1187 copy_original_table_set (loop_copy, loop->num, copy->num);
1188 }
1189
1190 /* Get the copy of LOOP. */
1191
1192 struct loop *
1193 get_loop_copy (struct loop *loop)
1194 {
1195 struct htab_bb_copy_original_entry *entry;
1196 struct htab_bb_copy_original_entry key;
1197
1198 gcc_assert (original_copy_bb_pool);
1199
1200 key.index1 = loop->num;
1201 entry = loop_copy->find (&key);
1202 if (entry)
1203 return get_loop (cfun, entry->index2);
1204 else
1205 return NULL;
1206 }