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