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