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