1 /* Control flow graph manipulation code for GNU compiler.
2    Copyright (C) 1987-2014 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 
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "obstack.h"
53 #include "ggc.h"
54 #include "hash-table.h"
55 #include "alloc-pool.h"
56 #include "tree.h"
57 #include "basic-block.h"
58 #include "df.h"
59 #include "cfgloop.h" /* FIXME: For struct loop.  */
60 #include "dumpfile.h"
61 
62 
63 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
64 
65 /* Called once at initialization time.  */
66 
67 void
init_flow(struct function * the_fun)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_fn (the_fun) = 0;
73   ENTRY_BLOCK_PTR_FOR_FN (the_fun)
74     = ggc_alloc_cleared_basic_block_def ();
75   ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
76   EXIT_BLOCK_PTR_FOR_FN (the_fun)
77     = ggc_alloc_cleared_basic_block_def ();
78   EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
79   ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
80     = EXIT_BLOCK_PTR_FOR_FN (the_fun);
81   EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
82     = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
83 }
84 
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
free_edge(edge e)89 free_edge (edge e)
90 {
91   n_edges_for_fn (cfun)--;
92   ggc_free (e);
93 }
94 
95 /* Free the memory associated with the edge structures.  */
96 
97 void
clear_edges(void)98 clear_edges (void)
99 {
100   basic_block bb;
101   edge e;
102   edge_iterator ei;
103 
104   FOR_EACH_BB_FN (bb, cfun)
105     {
106       FOR_EACH_EDGE (e, ei, bb->succs)
107 	free_edge (e);
108       vec_safe_truncate (bb->succs, 0);
109       vec_safe_truncate (bb->preds, 0);
110     }
111 
112   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
113     free_edge (e);
114   vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, 0);
115   vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs, 0);
116 
117   gcc_assert (!n_edges_for_fn (cfun));
118 }
119 
120 /* Allocate memory for basic_block.  */
121 
122 basic_block
alloc_block(void)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
link_block(basic_block b,basic_block after)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
unlink_block(basic_block b)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
compact_blocks(void)152 compact_blocks (void)
153 {
154   int i;
155 
156   SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
157   SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
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_FN (bb, cfun)
167 	{
168 	  SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
169 	  bb->index = i;
170 	  i++;
171 	}
172       gcc_assert (i == n_basic_blocks_for_fn (cfun));
173 
174       for (; i < last_basic_block_for_fn (cfun); i++)
175 	SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
176     }
177   last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
178 }
179 
180 /* Remove block B from the basic block array.  */
181 
182 void
expunge_block(basic_block b)183 expunge_block (basic_block b)
184 {
185   unlink_block (b);
186   SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
187   n_basic_blocks_for_fn (cfun)--;
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 
195 /* Connect E to E->src.  */
196 
197 static inline void
connect_src(edge e)198 connect_src (edge e)
199 {
200   vec_safe_push (e->src->succs, e);
201   df_mark_solutions_dirty ();
202 }
203 
204 /* Connect E to E->dest.  */
205 
206 static inline void
connect_dest(edge e)207 connect_dest (edge e)
208 {
209   basic_block dest = e->dest;
210   vec_safe_push (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
disconnect_src(edge e)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 	  src->succs->unordered_remove (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
disconnect_dest(edge e)242 disconnect_dest (edge e)
243 {
244   basic_block dest = e->dest;
245   unsigned int dest_idx = e->dest_idx;
246 
247   dest->preds->unordered_remove (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
unchecked_make_edge(basic_block src,basic_block dst,int flags)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_for_fn (cfun)++;
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
cached_make_edge(sbitmap edge_cache,basic_block src,basic_block dst,int flags)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
make_edge(basic_block src,basic_block dest,int flags)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
make_single_succ_edge(basic_block src,basic_block dest,int flags)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
remove_edge_raw(edge e)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
redirect_edge_succ(edge e,basic_block new_succ)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
redirect_edge_pred(edge e,basic_block new_pred)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
clear_bb_flags(void)386 clear_bb_flags (void)
387 {
388   basic_block bb;
389 
390   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
391     bb->flags &= BB_FLAGS_TO_PRESERVE;
392 }
393 
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
check_bb_profile(basic_block bb,FILE * file,int indent,int flags)400 check_bb_profile (basic_block bb, FILE * file, int indent, int flags)
401 {
402   edge e;
403   int sum = 0;
404   gcov_type lsum;
405   edge_iterator ei;
406   struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
407   char *s_indent = (char *) alloca ((size_t) indent + 1);
408   memset ((void *) s_indent, ' ', (size_t) indent);
409   s_indent[indent] = '\0';
410 
411   if (profile_status_for_fn (fun) == PROFILE_ABSENT)
412     return;
413 
414   if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
415     {
416       FOR_EACH_EDGE (e, ei, bb->succs)
417 	sum += e->probability;
418       if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
419 	fprintf (file, "%s%sInvalid sum of outgoing probabilities %.1f%%\n",
420 		 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
421 		 sum * 100.0 / REG_BR_PROB_BASE);
422       lsum = 0;
423       FOR_EACH_EDGE (e, ei, bb->succs)
424 	lsum += e->count;
425       if (EDGE_COUNT (bb->succs)
426 	  && (lsum - bb->count > 100 || lsum - bb->count < -100))
427 	fprintf (file, "%s%sInvalid sum of outgoing counts %i, should be %i\n",
428 		 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
429 		 (int) lsum, (int) bb->count);
430     }
431     if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
432     {
433       sum = 0;
434       FOR_EACH_EDGE (e, ei, bb->preds)
435 	sum += EDGE_FREQUENCY (e);
436       if (abs (sum - bb->frequency) > 100)
437 	fprintf (file,
438 		 "%s%sInvalid sum of incoming frequencies %i, should be %i\n",
439 		 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
440 		 sum, bb->frequency);
441       lsum = 0;
442       FOR_EACH_EDGE (e, ei, bb->preds)
443 	lsum += e->count;
444       if (lsum - bb->count > 100 || lsum - bb->count < -100)
445 	fprintf (file, "%s%sInvalid sum of incoming counts %i, should be %i\n",
446 		 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
447 		 (int) lsum, (int) bb->count);
448     }
449   if (BB_PARTITION (bb) == BB_COLD_PARTITION)
450     {
451       /* Warn about inconsistencies in the partitioning that are
452          currently caused by profile insanities created via optimization.  */
453       if (!probably_never_executed_bb_p (fun, bb))
454         fprintf (file, "%s%sBlock in cold partition with hot count\n",
455                  (flags & TDF_COMMENT) ? ";; " : "", s_indent);
456       FOR_EACH_EDGE (e, ei, bb->preds)
457         {
458           if (!probably_never_executed_edge_p (fun, e))
459             fprintf (file,
460                      "%s%sBlock in cold partition with incoming hot edge\n",
461                      (flags & TDF_COMMENT) ? ";; " : "", s_indent);
462         }
463     }
464 }
465 
466 void
dump_edge_info(FILE * file,edge e,int flags,int do_succ)467 dump_edge_info (FILE *file, edge e, int flags, int do_succ)
468 {
469   basic_block side = (do_succ ? e->dest : e->src);
470   bool do_details = false;
471 
472   if ((flags & TDF_DETAILS) != 0
473       && (flags & TDF_SLIM) == 0)
474     do_details = true;
475 
476   if (side->index == ENTRY_BLOCK)
477     fputs (" ENTRY", file);
478   else if (side->index == EXIT_BLOCK)
479     fputs (" EXIT", file);
480   else
481     fprintf (file, " %d", side->index);
482 
483   if (e->probability && do_details)
484     fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
485 
486   if (e->count && do_details)
487     {
488       fputs (" count:", file);
489       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
490     }
491 
492   if (e->flags && do_details)
493     {
494       static const char * const bitnames[] =
495 	{
496 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
497 #include "cfg-flags.def"
498 	  NULL
499 #undef DEF_EDGE_FLAG
500 	};
501       bool comma = false;
502       int i, flags = e->flags;
503 
504       gcc_assert (e->flags <= EDGE_ALL_FLAGS);
505       fputs (" (", file);
506       for (i = 0; flags; i++)
507 	if (flags & (1 << i))
508 	  {
509 	    flags &= ~(1 << i);
510 
511 	    if (comma)
512 	      fputc (',', file);
513 	    fputs (bitnames[i], file);
514 	    comma = true;
515 	  }
516 
517       fputc (')', file);
518     }
519 }
520 
521 DEBUG_FUNCTION void
debug(edge_def & ref)522 debug (edge_def &ref)
523 {
524   /* FIXME (crowl): Is this desireable?  */
525   dump_edge_info (stderr, &ref, 0, false);
526   dump_edge_info (stderr, &ref, 0, true);
527 }
528 
529 DEBUG_FUNCTION void
debug(edge_def * ptr)530 debug (edge_def *ptr)
531 {
532   if (ptr)
533     debug (*ptr);
534   else
535     fprintf (stderr, "<nil>\n");
536 }
537 
538 /* Simple routines to easily allocate AUX fields of basic blocks.  */
539 
540 static struct obstack block_aux_obstack;
541 static void *first_block_aux_obj = 0;
542 static struct obstack edge_aux_obstack;
543 static void *first_edge_aux_obj = 0;
544 
545 /* Allocate a memory block of SIZE as BB->aux.  The obstack must
546    be first initialized by alloc_aux_for_blocks.  */
547 
548 static void
alloc_aux_for_block(basic_block bb,int size)549 alloc_aux_for_block (basic_block bb, int size)
550 {
551   /* Verify that aux field is clear.  */
552   gcc_assert (!bb->aux && first_block_aux_obj);
553   bb->aux = obstack_alloc (&block_aux_obstack, size);
554   memset (bb->aux, 0, size);
555 }
556 
557 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
558    alloc_aux_for_block for each basic block.  */
559 
560 void
alloc_aux_for_blocks(int size)561 alloc_aux_for_blocks (int size)
562 {
563   static int initialized;
564 
565   if (!initialized)
566     {
567       gcc_obstack_init (&block_aux_obstack);
568       initialized = 1;
569     }
570   else
571     /* Check whether AUX data are still allocated.  */
572     gcc_assert (!first_block_aux_obj);
573 
574   first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
575   if (size)
576     {
577       basic_block bb;
578 
579       FOR_ALL_BB_FN (bb, cfun)
580 	alloc_aux_for_block (bb, size);
581     }
582 }
583 
584 /* Clear AUX pointers of all blocks.  */
585 
586 void
clear_aux_for_blocks(void)587 clear_aux_for_blocks (void)
588 {
589   basic_block bb;
590 
591   FOR_ALL_BB_FN (bb, cfun)
592     bb->aux = NULL;
593 }
594 
595 /* Free data allocated in block_aux_obstack and clear AUX pointers
596    of all blocks.  */
597 
598 void
free_aux_for_blocks(void)599 free_aux_for_blocks (void)
600 {
601   gcc_assert (first_block_aux_obj);
602   obstack_free (&block_aux_obstack, first_block_aux_obj);
603   first_block_aux_obj = NULL;
604 
605   clear_aux_for_blocks ();
606 }
607 
608 /* Allocate a memory edge of SIZE as E->aux.  The obstack must
609    be first initialized by alloc_aux_for_edges.  */
610 
611 void
alloc_aux_for_edge(edge e,int size)612 alloc_aux_for_edge (edge e, int size)
613 {
614   /* Verify that aux field is clear.  */
615   gcc_assert (!e->aux && first_edge_aux_obj);
616   e->aux = obstack_alloc (&edge_aux_obstack, size);
617   memset (e->aux, 0, size);
618 }
619 
620 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
621    alloc_aux_for_edge for each basic edge.  */
622 
623 void
alloc_aux_for_edges(int size)624 alloc_aux_for_edges (int size)
625 {
626   static int initialized;
627 
628   if (!initialized)
629     {
630       gcc_obstack_init (&edge_aux_obstack);
631       initialized = 1;
632     }
633   else
634     /* Check whether AUX data are still allocated.  */
635     gcc_assert (!first_edge_aux_obj);
636 
637   first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
638   if (size)
639     {
640       basic_block bb;
641 
642       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
643 		      EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
644 	{
645 	  edge e;
646 	  edge_iterator ei;
647 
648 	  FOR_EACH_EDGE (e, ei, bb->succs)
649 	    alloc_aux_for_edge (e, size);
650 	}
651     }
652 }
653 
654 /* Clear AUX pointers of all edges.  */
655 
656 void
clear_aux_for_edges(void)657 clear_aux_for_edges (void)
658 {
659   basic_block bb;
660   edge e;
661 
662   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
663 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
664     {
665       edge_iterator ei;
666       FOR_EACH_EDGE (e, ei, bb->succs)
667 	e->aux = NULL;
668     }
669 }
670 
671 /* Free data allocated in edge_aux_obstack and clear AUX pointers
672    of all edges.  */
673 
674 void
free_aux_for_edges(void)675 free_aux_for_edges (void)
676 {
677   gcc_assert (first_edge_aux_obj);
678   obstack_free (&edge_aux_obstack, first_edge_aux_obj);
679   first_edge_aux_obj = NULL;
680 
681   clear_aux_for_edges ();
682 }
683 
684 DEBUG_FUNCTION void
debug_bb(basic_block bb)685 debug_bb (basic_block bb)
686 {
687   dump_bb (stderr, bb, 0, dump_flags);
688 }
689 
690 DEBUG_FUNCTION basic_block
debug_bb_n(int n)691 debug_bb_n (int n)
692 {
693   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
694   debug_bb (bb);
695   return bb;
696 }
697 
698 /* Dumps cfg related information about basic block BB to OUTF.
699    If HEADER is true, dump things that appear before the instructions
700    contained in BB.  If FOOTER is true, dump things that appear after.
701    Flags are the TDF_* masks as documented in dumpfile.h.
702    NB: With TDF_DETAILS, it is assumed that cfun is available, so
703    that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE.  */
704 
705 void
dump_bb_info(FILE * outf,basic_block bb,int indent,int flags,bool do_header,bool do_footer)706 dump_bb_info (FILE *outf, basic_block bb, int indent, int flags,
707 	      bool do_header, bool do_footer)
708 {
709   edge_iterator ei;
710   edge e;
711   static const char * const bb_bitnames[] =
712     {
713 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
714 #include "cfg-flags.def"
715       NULL
716 #undef DEF_BASIC_BLOCK_FLAG
717     };
718   const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
719   bool first;
720   char *s_indent = (char *) alloca ((size_t) indent + 1);
721   memset ((void *) s_indent, ' ', (size_t) indent);
722   s_indent[indent] = '\0';
723 
724   gcc_assert (bb->flags <= BB_ALL_FLAGS);
725 
726   if (do_header)
727     {
728       unsigned i;
729 
730       if (flags & TDF_COMMENT)
731 	fputs (";; ", outf);
732       fprintf (outf, "%sbasic block %d, loop depth %d",
733 	       s_indent, bb->index, bb_loop_depth (bb));
734       if (flags & TDF_DETAILS)
735 	{
736 	  struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
737 	  fprintf (outf, ", count " HOST_WIDEST_INT_PRINT_DEC,
738 		   (HOST_WIDEST_INT) bb->count);
739 	  fprintf (outf, ", freq %i", bb->frequency);
740 	  if (maybe_hot_bb_p (fun, bb))
741 	    fputs (", maybe hot", outf);
742 	  if (probably_never_executed_bb_p (fun, bb))
743 	    fputs (", probably never executed", outf);
744 	}
745       fputc ('\n', outf);
746       if (TDF_DETAILS)
747 	check_bb_profile (bb, outf, indent, flags);
748 
749       if (flags & TDF_DETAILS)
750 	{
751 	  if (flags & TDF_COMMENT)
752 	    fputs (";; ", outf);
753 	  fprintf (outf, "%s prev block ", s_indent);
754 	  if (bb->prev_bb)
755 	    fprintf (outf, "%d", bb->prev_bb->index);
756 	  else
757 	    fprintf (outf, "(nil)");
758 	  fprintf (outf, ", next block ");
759 	  if (bb->next_bb)
760 	    fprintf (outf, "%d", bb->next_bb->index);
761 	  else
762 	    fprintf (outf, "(nil)");
763 
764 	  fputs (", flags:", outf);
765 	  first = true;
766 	  for (i = 0; i < n_bitnames; i++)
767 	    if (bb->flags & (1 << i))
768 	      {
769 		if (first)
770 		  fputs (" (", outf);
771 		else
772 		  fputs (", ", outf);
773 		first = false;
774 		fputs (bb_bitnames[i], outf);
775 	      }
776 	  if (!first)
777 	    fputc (')', outf);
778 	  fputc ('\n', outf);
779 	}
780 
781       if (flags & TDF_COMMENT)
782 	fputs (";; ", outf);
783       fprintf (outf, "%s pred:      ", s_indent);
784       first = true;
785       FOR_EACH_EDGE (e, ei, bb->preds)
786 	{
787 	  if (! first)
788 	    {
789 	      if (flags & TDF_COMMENT)
790 		fputs (";; ", outf);
791 	      fprintf (outf, "%s            ", s_indent);
792 	    }
793 	  first = false;
794 	  dump_edge_info (outf, e, flags, 0);
795 	  fputc ('\n', outf);
796 	}
797       if (first)
798 	fputc ('\n', outf);
799     }
800 
801   if (do_footer)
802     {
803       if (flags & TDF_COMMENT)
804 	fputs (";; ", outf);
805       fprintf (outf, "%s succ:      ", s_indent);
806       first = true;
807       FOR_EACH_EDGE (e, ei, bb->succs)
808         {
809 	  if (! first)
810 	    {
811 	      if (flags & TDF_COMMENT)
812 		fputs (";; ", outf);
813 	      fprintf (outf, "%s            ", s_indent);
814 	    }
815 	  first = false;
816 	  dump_edge_info (outf, e, flags, 1);
817 	  fputc ('\n', outf);
818 	}
819       if (first)
820 	fputc ('\n', outf);
821     }
822 }
823 
824 /* Dumps a brief description of cfg to FILE.  */
825 
826 void
brief_dump_cfg(FILE * file,int flags)827 brief_dump_cfg (FILE *file, int flags)
828 {
829   basic_block bb;
830 
831   FOR_EACH_BB_FN (bb, cfun)
832     {
833       dump_bb_info (file, bb, 0,
834 		    flags & (TDF_COMMENT | TDF_DETAILS),
835 		    true, true);
836     }
837 }
838 
839 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
840    leave the block by TAKEN_EDGE.  Update profile of BB such that edge E can be
841    redirected to destination of TAKEN_EDGE.
842 
843    This function may leave the profile inconsistent in the case TAKEN_EDGE
844    frequency or count is believed to be lower than FREQUENCY or COUNT
845    respectively.  */
846 void
update_bb_profile_for_threading(basic_block bb,int edge_frequency,gcov_type count,edge taken_edge)847 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
848 				 gcov_type count, edge taken_edge)
849 {
850   edge c;
851   int prob;
852   edge_iterator ei;
853 
854   bb->count -= count;
855   if (bb->count < 0)
856     {
857       if (dump_file)
858 	fprintf (dump_file, "bb %i count became negative after threading",
859 		 bb->index);
860       bb->count = 0;
861     }
862 
863   /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
864      Watch for overflows.  */
865   if (bb->frequency)
866     prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
867   else
868     prob = 0;
869   if (prob > taken_edge->probability)
870     {
871       if (dump_file)
872 	fprintf (dump_file, "Jump threading proved probability of edge "
873 		 "%i->%i too small (it is %i, should be %i).\n",
874 		 taken_edge->src->index, taken_edge->dest->index,
875 		 taken_edge->probability, prob);
876       prob = taken_edge->probability;
877     }
878 
879   /* Now rescale the probabilities.  */
880   taken_edge->probability -= prob;
881   prob = REG_BR_PROB_BASE - prob;
882   bb->frequency -= edge_frequency;
883   if (bb->frequency < 0)
884     bb->frequency = 0;
885   if (prob <= 0)
886     {
887       if (dump_file)
888 	fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
889 		 "frequency of block should end up being 0, it is %i\n",
890 		 bb->index, bb->frequency);
891       EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
892       ei = ei_start (bb->succs);
893       ei_next (&ei);
894       for (; (c = ei_safe_edge (ei)); ei_next (&ei))
895 	c->probability = 0;
896     }
897   else if (prob != REG_BR_PROB_BASE)
898     {
899       int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
900 
901       FOR_EACH_EDGE (c, ei, bb->succs)
902 	{
903 	  /* Protect from overflow due to additional scaling.  */
904 	  if (c->probability > prob)
905 	    c->probability = REG_BR_PROB_BASE;
906 	  else
907 	    {
908 	      c->probability = RDIV (c->probability * scale, 65536);
909 	      if (c->probability > REG_BR_PROB_BASE)
910 		c->probability = REG_BR_PROB_BASE;
911 	    }
912 	}
913     }
914 
915   gcc_assert (bb == taken_edge->src);
916   taken_edge->count -= count;
917   if (taken_edge->count < 0)
918     {
919       if (dump_file)
920 	fprintf (dump_file, "edge %i->%i count became negative after threading",
921 		 taken_edge->src->index, taken_edge->dest->index);
922       taken_edge->count = 0;
923     }
924 }
925 
926 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
927    by NUM/DEN, in int arithmetic.  May lose some accuracy.  */
928 void
scale_bbs_frequencies_int(basic_block * bbs,int nbbs,int num,int den)929 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
930 {
931   int i;
932   edge e;
933   if (num < 0)
934     num = 0;
935 
936   /* Scale NUM and DEN to avoid overflows.  Frequencies are in order of
937      10^4, if we make DEN <= 10^3, we can afford to upscale by 100
938      and still safely fit in int during calculations.  */
939   if (den > 1000)
940     {
941       if (num > 1000000)
942 	return;
943 
944       num = RDIV (1000 * num, den);
945       den = 1000;
946     }
947   if (num > 100 * den)
948     return;
949 
950   for (i = 0; i < nbbs; i++)
951     {
952       edge_iterator ei;
953       bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
954       /* Make sure the frequencies do not grow over BB_FREQ_MAX.  */
955       if (bbs[i]->frequency > BB_FREQ_MAX)
956 	bbs[i]->frequency = BB_FREQ_MAX;
957       bbs[i]->count = RDIV (bbs[i]->count * num, den);
958       FOR_EACH_EDGE (e, ei, bbs[i]->succs)
959 	e->count = RDIV (e->count * num, den);
960     }
961 }
962 
963 /* numbers smaller than this value are safe to multiply without getting
964    64bit overflow.  */
965 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
966 
967 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
968    by NUM/DEN, in gcov_type arithmetic.  More accurate than previous
969    function but considerably slower.  */
970 void
scale_bbs_frequencies_gcov_type(basic_block * bbs,int nbbs,gcov_type num,gcov_type den)971 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
972 				 gcov_type den)
973 {
974   int i;
975   edge e;
976   gcov_type fraction = RDIV (num * 65536, den);
977 
978   gcc_assert (fraction >= 0);
979 
980   if (num < MAX_SAFE_MULTIPLIER)
981     for (i = 0; i < nbbs; i++)
982       {
983 	edge_iterator ei;
984 	bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
985 	if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
986 	  bbs[i]->count = RDIV (bbs[i]->count * num, den);
987 	else
988 	  bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
989 	FOR_EACH_EDGE (e, ei, bbs[i]->succs)
990 	  if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
991 	    e->count = RDIV (e->count * num, den);
992 	  else
993 	    e->count = RDIV (e->count * fraction, 65536);
994       }
995    else
996     for (i = 0; i < nbbs; i++)
997       {
998 	edge_iterator ei;
999 	if (sizeof (gcov_type) > sizeof (int))
1000 	  bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1001 	else
1002 	  bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
1003 	bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
1004 	FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1005 	  e->count = RDIV (e->count * fraction, 65536);
1006       }
1007 }
1008 
1009 /* Helper types for hash tables.  */
1010 
1011 struct htab_bb_copy_original_entry
1012 {
1013   /* Block we are attaching info to.  */
1014   int index1;
1015   /* Index of original or copy (depending on the hashtable) */
1016   int index2;
1017 };
1018 
1019 struct bb_copy_hasher : typed_noop_remove <htab_bb_copy_original_entry>
1020 {
1021   typedef htab_bb_copy_original_entry value_type;
1022   typedef htab_bb_copy_original_entry compare_type;
1023   static inline hashval_t hash (const value_type *);
1024   static inline bool equal (const value_type *existing,
1025 			    const compare_type * candidate);
1026 };
1027 
1028 inline hashval_t
hash(const value_type * data)1029 bb_copy_hasher::hash (const value_type *data)
1030 {
1031   return data->index1;
1032 }
1033 
1034 inline bool
equal(const value_type * data,const compare_type * data2)1035 bb_copy_hasher::equal (const value_type *data, const compare_type *data2)
1036 {
1037   return data->index1 == data2->index1;
1038 }
1039 
1040 /* Data structures used to maintain mapping between basic blocks and
1041    copies.  */
1042 static hash_table <bb_copy_hasher> bb_original;
1043 static hash_table <bb_copy_hasher> bb_copy;
1044 
1045 /* And between loops and copies.  */
1046 static hash_table <bb_copy_hasher> loop_copy;
1047 static alloc_pool original_copy_bb_pool;
1048 
1049 
1050 /* Initialize the data structures to maintain mapping between blocks
1051    and its copies.  */
1052 void
initialize_original_copy_tables(void)1053 initialize_original_copy_tables (void)
1054 {
1055   gcc_assert (!original_copy_bb_pool);
1056   original_copy_bb_pool
1057     = create_alloc_pool ("original_copy",
1058 			 sizeof (struct htab_bb_copy_original_entry), 10);
1059   bb_original.create (10);
1060   bb_copy.create (10);
1061   loop_copy.create (10);
1062 }
1063 
1064 /* Free the data structures to maintain mapping between blocks and
1065    its copies.  */
1066 void
free_original_copy_tables(void)1067 free_original_copy_tables (void)
1068 {
1069   gcc_assert (original_copy_bb_pool);
1070   bb_copy.dispose ();
1071   bb_original.dispose ();
1072   loop_copy.dispose ();
1073   free_alloc_pool (original_copy_bb_pool);
1074   original_copy_bb_pool = NULL;
1075 }
1076 
1077 /* Removes the value associated with OBJ from table TAB.  */
1078 
1079 static void
copy_original_table_clear(hash_table<bb_copy_hasher> tab,unsigned obj)1080 copy_original_table_clear (hash_table <bb_copy_hasher> tab, unsigned obj)
1081 {
1082   htab_bb_copy_original_entry **slot;
1083   struct htab_bb_copy_original_entry key, *elt;
1084 
1085   if (!original_copy_bb_pool)
1086     return;
1087 
1088   key.index1 = obj;
1089   slot = tab.find_slot (&key, NO_INSERT);
1090   if (!slot)
1091     return;
1092 
1093   elt = *slot;
1094   tab.clear_slot (slot);
1095   pool_free (original_copy_bb_pool, elt);
1096 }
1097 
1098 /* Sets the value associated with OBJ in table TAB to VAL.
1099    Do nothing when data structures are not initialized.  */
1100 
1101 static void
copy_original_table_set(hash_table<bb_copy_hasher> tab,unsigned obj,unsigned val)1102 copy_original_table_set (hash_table <bb_copy_hasher> tab,
1103 			 unsigned obj, unsigned val)
1104 {
1105   struct htab_bb_copy_original_entry **slot;
1106   struct htab_bb_copy_original_entry key;
1107 
1108   if (!original_copy_bb_pool)
1109     return;
1110 
1111   key.index1 = obj;
1112   slot = tab.find_slot (&key, INSERT);
1113   if (!*slot)
1114     {
1115       *slot = (struct htab_bb_copy_original_entry *)
1116 		pool_alloc (original_copy_bb_pool);
1117       (*slot)->index1 = obj;
1118     }
1119   (*slot)->index2 = val;
1120 }
1121 
1122 /* Set original for basic block.  Do nothing when data structures are not
1123    initialized so passes not needing this don't need to care.  */
1124 void
set_bb_original(basic_block bb,basic_block original)1125 set_bb_original (basic_block bb, basic_block original)
1126 {
1127   copy_original_table_set (bb_original, bb->index, original->index);
1128 }
1129 
1130 /* Get the original basic block.  */
1131 basic_block
get_bb_original(basic_block bb)1132 get_bb_original (basic_block bb)
1133 {
1134   struct htab_bb_copy_original_entry *entry;
1135   struct htab_bb_copy_original_entry key;
1136 
1137   gcc_assert (original_copy_bb_pool);
1138 
1139   key.index1 = bb->index;
1140   entry = bb_original.find (&key);
1141   if (entry)
1142     return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1143   else
1144     return NULL;
1145 }
1146 
1147 /* Set copy for basic block.  Do nothing when data structures are not
1148    initialized so passes not needing this don't need to care.  */
1149 void
set_bb_copy(basic_block bb,basic_block copy)1150 set_bb_copy (basic_block bb, basic_block copy)
1151 {
1152   copy_original_table_set (bb_copy, bb->index, copy->index);
1153 }
1154 
1155 /* Get the copy of basic block.  */
1156 basic_block
get_bb_copy(basic_block bb)1157 get_bb_copy (basic_block bb)
1158 {
1159   struct htab_bb_copy_original_entry *entry;
1160   struct htab_bb_copy_original_entry key;
1161 
1162   gcc_assert (original_copy_bb_pool);
1163 
1164   key.index1 = bb->index;
1165   entry = bb_copy.find (&key);
1166   if (entry)
1167     return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1168   else
1169     return NULL;
1170 }
1171 
1172 /* Set copy for LOOP to COPY.  Do nothing when data structures are not
1173    initialized so passes not needing this don't need to care.  */
1174 
1175 void
set_loop_copy(struct loop * loop,struct loop * copy)1176 set_loop_copy (struct loop *loop, struct loop *copy)
1177 {
1178   if (!copy)
1179     copy_original_table_clear (loop_copy, loop->num);
1180   else
1181     copy_original_table_set (loop_copy, loop->num, copy->num);
1182 }
1183 
1184 /* Get the copy of LOOP.  */
1185 
1186 struct loop *
get_loop_copy(struct loop * loop)1187 get_loop_copy (struct loop *loop)
1188 {
1189   struct htab_bb_copy_original_entry *entry;
1190   struct htab_bb_copy_original_entry key;
1191 
1192   gcc_assert (original_copy_bb_pool);
1193 
1194   key.index1 = loop->num;
1195   entry = loop_copy.find (&key);
1196   if (entry)
1197     return get_loop (cfun, entry->index2);
1198   else
1199     return NULL;
1200 }
1201