]>
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 JH |
62 | #define RDIV(X,Y) (((X) + (Y) / 2) / (Y)) |
63 | ||
eaec9b3d | 64 | /* Called once at initialization time. */ |
402209ff JH |
65 | |
66 | void | |
9defb1fe | 67 | init_flow (struct function *the_fun) |
402209ff | 68 | { |
9defb1fe | 69 | if (!the_fun->cfg) |
766090c2 | 70 | the_fun->cfg = ggc_cleared_alloc<control_flow_graph> (); |
dc936fb2 | 71 | n_edges_for_fn (the_fun) = 0; |
fefa31b5 | 72 | ENTRY_BLOCK_PTR_FOR_FN (the_fun) |
766090c2 | 73 | = ggc_cleared_alloc<basic_block_def> (); |
fefa31b5 DM |
74 | ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK; |
75 | EXIT_BLOCK_PTR_FOR_FN (the_fun) | |
766090c2 | 76 | = ggc_cleared_alloc<basic_block_def> (); |
fefa31b5 DM |
77 | EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK; |
78 | ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb | |
79 | = EXIT_BLOCK_PTR_FOR_FN (the_fun); | |
80 | EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb | |
81 | = ENTRY_BLOCK_PTR_FOR_FN (the_fun); | |
402209ff JH |
82 | } |
83 | \f | |
d39ac0fd | 84 | /* Helper function for remove_edge and clear_edges. Frees edge structure |
532aafad | 85 | without actually removing it from the pred/succ arrays. */ |
d39ac0fd JH |
86 | |
87 | static void | |
61183076 | 88 | free_edge (function *fn, edge e) |
d39ac0fd | 89 | { |
61183076 | 90 | n_edges_for_fn (fn)--; |
80d8221e | 91 | ggc_free (e); |
d39ac0fd JH |
92 | } |
93 | ||
402209ff JH |
94 | /* Free the memory associated with the edge structures. */ |
95 | ||
96 | void | |
61183076 | 97 | clear_edges (struct function *fn) |
402209ff | 98 | { |
e0082a72 | 99 | basic_block bb; |
d39ac0fd | 100 | edge e; |
628f6a4e | 101 | edge_iterator ei; |
402209ff | 102 | |
61183076 | 103 | FOR_EACH_BB_FN (bb, fn) |
402209ff | 104 | { |
628f6a4e | 105 | FOR_EACH_EDGE (e, ei, bb->succs) |
61183076 | 106 | free_edge (fn, e); |
9771b263 DN |
107 | vec_safe_truncate (bb->succs, 0); |
108 | vec_safe_truncate (bb->preds, 0); | |
d39ac0fd | 109 | } |
4891442b | 110 | |
61183076 RB |
111 | FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fn)->succs) |
112 | free_edge (fn, e); | |
113 | vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (fn)->preds, 0); | |
114 | vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs, 0); | |
402209ff | 115 | |
61183076 | 116 | gcc_assert (!n_edges_for_fn (fn)); |
402209ff JH |
117 | } |
118 | \f | |
ca6c03ca | 119 | /* Allocate memory for basic_block. */ |
402209ff | 120 | |
4262e623 | 121 | basic_block |
d329e058 | 122 | alloc_block (void) |
402209ff JH |
123 | { |
124 | basic_block bb; | |
766090c2 | 125 | bb = ggc_cleared_alloc<basic_block_def> (); |
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 | |
e0fd3e7a MM |
266 | e->src = src; |
267 | e->dest = dst; | |
268 | e->flags = flags; | |
adf4a335 KH |
269 | |
270 | connect_src (e); | |
271 | connect_dest (e); | |
e0fd3e7a | 272 | |
d9d4706f | 273 | execute_on_growing_pred (e); |
e0fd3e7a MM |
274 | return e; |
275 | } | |
276 | ||
7ded4467 | 277 | /* Create an edge connecting SRC and DST with FLAGS optionally using |
2ba84f36 | 278 | edge cache CACHE. Return the new edge, NULL if already exist. */ |
4262e623 | 279 | |
7ded4467 | 280 | edge |
a6ee1a15 | 281 | cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags) |
402209ff | 282 | { |
e2c879a1 | 283 | if (edge_cache == NULL |
fefa31b5 DM |
284 | || src == ENTRY_BLOCK_PTR_FOR_FN (cfun) |
285 | || dst == EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
e2c879a1 | 286 | return make_edge (src, dst, flags); |
402209ff | 287 | |
e2c879a1 | 288 | /* Does the requested edge already exist? */ |
d7c028c0 | 289 | if (! bitmap_bit_p (edge_cache, dst->index)) |
402209ff | 290 | { |
e2c879a1 KH |
291 | /* The edge does not exist. Create one and update the |
292 | cache. */ | |
d7c028c0 | 293 | bitmap_set_bit (edge_cache, dst->index); |
e2c879a1 | 294 | return unchecked_make_edge (src, dst, flags); |
402209ff | 295 | } |
d329e058 | 296 | |
e2c879a1 KH |
297 | /* At this point, we know that the requested edge exists. Adjust |
298 | flags if necessary. */ | |
299 | if (flags) | |
300 | { | |
301 | edge e = find_edge (src, dst); | |
302 | e->flags |= flags; | |
303 | } | |
7ded4467 | 304 | |
e2c879a1 | 305 | return NULL; |
7ded4467 JH |
306 | } |
307 | ||
308 | /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly | |
309 | created edge or NULL if already exist. */ | |
310 | ||
311 | edge | |
d329e058 | 312 | make_edge (basic_block src, basic_block dest, int flags) |
7ded4467 | 313 | { |
e2c879a1 KH |
314 | edge e = find_edge (src, dest); |
315 | ||
316 | /* Make sure we don't add duplicate edges. */ | |
317 | if (e) | |
318 | { | |
319 | e->flags |= flags; | |
320 | return NULL; | |
321 | } | |
322 | ||
323 | return unchecked_make_edge (src, dest, flags); | |
7ded4467 JH |
324 | } |
325 | ||
eaec9b3d | 326 | /* Create an edge connecting SRC to DEST and set probability by knowing |
7ded4467 JH |
327 | that it is the single edge leaving SRC. */ |
328 | ||
329 | edge | |
d329e058 | 330 | make_single_succ_edge (basic_block src, basic_block dest, int flags) |
7ded4467 JH |
331 | { |
332 | edge e = make_edge (src, dest, flags); | |
333 | ||
334 | e->probability = REG_BR_PROB_BASE; | |
335 | e->count = src->count; | |
336 | return e; | |
402209ff JH |
337 | } |
338 | ||
339 | /* This function will remove an edge from the flow graph. */ | |
340 | ||
341 | void | |
452ba14d | 342 | remove_edge_raw (edge e) |
402209ff | 343 | { |
3809e990 | 344 | remove_predictions_associated_with_edge (e); |
d9d4706f KH |
345 | execute_on_shrinking_pred (e); |
346 | ||
adf4a335 KH |
347 | disconnect_src (e); |
348 | disconnect_dest (e); | |
402209ff | 349 | |
61183076 | 350 | free_edge (cfun, e); |
402209ff JH |
351 | } |
352 | ||
353 | /* Redirect an edge's successor from one block to another. */ | |
354 | ||
355 | void | |
d329e058 | 356 | redirect_edge_succ (edge e, basic_block new_succ) |
402209ff | 357 | { |
d9d4706f KH |
358 | execute_on_shrinking_pred (e); |
359 | ||
adf4a335 | 360 | disconnect_dest (e); |
628f6a4e | 361 | |
adf4a335 | 362 | e->dest = new_succ; |
402209ff JH |
363 | |
364 | /* Reconnect the edge to the new successor block. */ | |
adf4a335 KH |
365 | connect_dest (e); |
366 | ||
d9d4706f | 367 | execute_on_growing_pred (e); |
402209ff JH |
368 | } |
369 | ||
402209ff JH |
370 | /* Redirect an edge's predecessor from one block to another. */ |
371 | ||
372 | void | |
d329e058 | 373 | redirect_edge_pred (edge e, basic_block new_pred) |
402209ff | 374 | { |
adf4a335 | 375 | disconnect_src (e); |
402209ff | 376 | |
adf4a335 | 377 | e->src = new_pred; |
402209ff JH |
378 | |
379 | /* Reconnect the edge to the new predecessor block. */ | |
adf4a335 | 380 | connect_src (e); |
402209ff | 381 | } |
38c1593d | 382 | |
c4669594 | 383 | /* Clear all basic block flags that do not have to be preserved. */ |
38c1593d | 384 | void |
d329e058 | 385 | clear_bb_flags (void) |
38c1593d | 386 | { |
e0082a72 ZD |
387 | basic_block bb; |
388 | ||
2300c332 | 389 | FOR_ALL_BB_FN (bb, cfun) |
c4669594 | 390 | bb->flags &= BB_FLAGS_TO_PRESERVE; |
38c1593d | 391 | } |
402209ff | 392 | \f |
878f99d2 JH |
393 | /* Check the consistency of profile information. We can't do that |
394 | in verify_flow_info, as the counts may get invalid for incompletely | |
395 | solved graphs, later eliminating of conditionals or roundoff errors. | |
396 | It is still practical to have them reported for debugging of simple | |
397 | testcases. */ | |
c4669594 SB |
398 | static void |
399 | check_bb_profile (basic_block bb, FILE * file, int indent, int flags) | |
878f99d2 JH |
400 | { |
401 | edge e; | |
402 | int sum = 0; | |
403 | gcov_type lsum; | |
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) | |
428 | fprintf (file, "%s%sInvalid sum of outgoing probabilities %.1f%%\n", | |
429 | (flags & TDF_COMMENT) ? ";; " : "", s_indent, | |
430 | sum * 100.0 / REG_BR_PROB_BASE); | |
431 | lsum = 0; | |
432 | FOR_EACH_EDGE (e, ei, bb->succs) | |
433 | lsum += e->count; | |
434 | if (EDGE_COUNT (bb->succs) | |
435 | && (lsum - bb->count > 100 || lsum - bb->count < -100)) | |
436 | fprintf (file, "%s%sInvalid sum of outgoing counts %i, should be %i\n", | |
437 | (flags & TDF_COMMENT) ? ";; " : "", s_indent, | |
438 | (int) lsum, (int) bb->count); | |
439 | } | |
878f99d2 | 440 | } |
fefa31b5 | 441 | if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun)) |
878f99d2 JH |
442 | { |
443 | sum = 0; | |
628f6a4e | 444 | FOR_EACH_EDGE (e, ei, bb->preds) |
878f99d2 JH |
445 | sum += EDGE_FREQUENCY (e); |
446 | if (abs (sum - bb->frequency) > 100) | |
447 | fprintf (file, | |
c4669594 SB |
448 | "%s%sInvalid sum of incoming frequencies %i, should be %i\n", |
449 | (flags & TDF_COMMENT) ? ";; " : "", s_indent, | |
878f99d2 JH |
450 | sum, bb->frequency); |
451 | lsum = 0; | |
628f6a4e | 452 | FOR_EACH_EDGE (e, ei, bb->preds) |
878f99d2 JH |
453 | lsum += e->count; |
454 | if (lsum - bb->count > 100 || lsum - bb->count < -100) | |
c4669594 SB |
455 | fprintf (file, "%s%sInvalid sum of incoming counts %i, should be %i\n", |
456 | (flags & TDF_COMMENT) ? ";; " : "", s_indent, | |
878f99d2 JH |
457 | (int) lsum, (int) bb->count); |
458 | } | |
600b5b1d TJ |
459 | if (BB_PARTITION (bb) == BB_COLD_PARTITION) |
460 | { | |
461 | /* Warn about inconsistencies in the partitioning that are | |
462 | currently caused by profile insanities created via optimization. */ | |
463 | if (!probably_never_executed_bb_p (fun, bb)) | |
464 | fprintf (file, "%s%sBlock in cold partition with hot count\n", | |
465 | (flags & TDF_COMMENT) ? ";; " : "", s_indent); | |
466 | FOR_EACH_EDGE (e, ei, bb->preds) | |
467 | { | |
468 | if (!probably_never_executed_edge_p (fun, e)) | |
469 | fprintf (file, | |
470 | "%s%sBlock in cold partition with incoming hot edge\n", | |
471 | (flags & TDF_COMMENT) ? ";; " : "", s_indent); | |
472 | } | |
473 | } | |
878f99d2 JH |
474 | } |
475 | \f | |
402209ff | 476 | void |
a315c44c | 477 | dump_edge_info (FILE *file, edge e, int flags, int do_succ) |
402209ff | 478 | { |
ca6c03ca | 479 | basic_block side = (do_succ ? e->dest : e->src); |
a315c44c SB |
480 | bool do_details = false; |
481 | ||
482 | if ((flags & TDF_DETAILS) != 0 | |
483 | && (flags & TDF_SLIM) == 0) | |
484 | do_details = true; | |
485 | ||
532aafad | 486 | if (side->index == ENTRY_BLOCK) |
ca6c03ca | 487 | fputs (" ENTRY", file); |
532aafad | 488 | else if (side->index == EXIT_BLOCK) |
ca6c03ca JH |
489 | fputs (" EXIT", file); |
490 | else | |
0b17ab2f | 491 | fprintf (file, " %d", side->index); |
ca6c03ca | 492 | |
a315c44c | 493 | if (e->probability && do_details) |
ca6c03ca | 494 | fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE); |
402209ff | 495 | |
a315c44c | 496 | if (e->count && do_details) |
402209ff | 497 | { |
edb30094 | 498 | fputs (" count:", file); |
16998094 | 499 | fprintf (file, "%" PRId64, e->count); |
402209ff JH |
500 | } |
501 | ||
a315c44c | 502 | if (e->flags && do_details) |
402209ff | 503 | { |
a315c44c SB |
504 | static const char * const bitnames[] = |
505 | { | |
506 | #define DEF_EDGE_FLAG(NAME,IDX) #NAME , | |
507 | #include "cfg-flags.def" | |
508 | NULL | |
509 | #undef DEF_EDGE_FLAG | |
510 | }; | |
511 | bool comma = false; | |
ca6c03ca | 512 | int i, flags = e->flags; |
402209ff | 513 | |
a315c44c | 514 | gcc_assert (e->flags <= EDGE_ALL_FLAGS); |
4891442b | 515 | fputs (" (", file); |
402209ff JH |
516 | for (i = 0; flags; i++) |
517 | if (flags & (1 << i)) | |
518 | { | |
519 | flags &= ~(1 << i); | |
520 | ||
521 | if (comma) | |
522 | fputc (',', file); | |
a315c44c SB |
523 | fputs (bitnames[i], file); |
524 | comma = true; | |
402209ff | 525 | } |
4891442b | 526 | |
402209ff JH |
527 | fputc (')', file); |
528 | } | |
529 | } | |
7b3b6ae4 LC |
530 | |
531 | DEBUG_FUNCTION void | |
532 | debug (edge_def &ref) | |
533 | { | |
534 | /* FIXME (crowl): Is this desireable? */ | |
535 | dump_edge_info (stderr, &ref, 0, false); | |
536 | dump_edge_info (stderr, &ref, 0, true); | |
537 | } | |
538 | ||
539 | DEBUG_FUNCTION void | |
540 | debug (edge_def *ptr) | |
541 | { | |
542 | if (ptr) | |
543 | debug (*ptr); | |
544 | else | |
545 | fprintf (stderr, "<nil>\n"); | |
546 | } | |
402209ff | 547 | \f |
ff7cc307 | 548 | /* Simple routines to easily allocate AUX fields of basic blocks. */ |
4891442b | 549 | |
ca6c03ca JH |
550 | static struct obstack block_aux_obstack; |
551 | static void *first_block_aux_obj = 0; | |
552 | static struct obstack edge_aux_obstack; | |
553 | static void *first_edge_aux_obj = 0; | |
402209ff | 554 | |
09da1532 | 555 | /* Allocate a memory block of SIZE as BB->aux. The obstack must |
ca6c03ca | 556 | be first initialized by alloc_aux_for_blocks. */ |
402209ff | 557 | |
a398224a | 558 | static void |
d329e058 | 559 | alloc_aux_for_block (basic_block bb, int size) |
402209ff | 560 | { |
ca6c03ca | 561 | /* Verify that aux field is clear. */ |
341c100f | 562 | gcc_assert (!bb->aux && first_block_aux_obj); |
ca6c03ca JH |
563 | bb->aux = obstack_alloc (&block_aux_obstack, size); |
564 | memset (bb->aux, 0, size); | |
402209ff JH |
565 | } |
566 | ||
ca6c03ca JH |
567 | /* Initialize the block_aux_obstack and if SIZE is nonzero, call |
568 | alloc_aux_for_block for each basic block. */ | |
402209ff JH |
569 | |
570 | void | |
d329e058 | 571 | alloc_aux_for_blocks (int size) |
402209ff | 572 | { |
ca6c03ca | 573 | static int initialized; |
402209ff | 574 | |
ca6c03ca | 575 | if (!initialized) |
402209ff | 576 | { |
ca6c03ca JH |
577 | gcc_obstack_init (&block_aux_obstack); |
578 | initialized = 1; | |
402209ff | 579 | } |
341c100f NS |
580 | else |
581 | /* Check whether AUX data are still allocated. */ | |
582 | gcc_assert (!first_block_aux_obj); | |
c22cacf3 | 583 | |
703ad42b | 584 | first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0); |
ca6c03ca | 585 | if (size) |
402209ff | 586 | { |
e0082a72 | 587 | basic_block bb; |
4891442b | 588 | |
04a90bec | 589 | FOR_ALL_BB_FN (bb, cfun) |
e0082a72 | 590 | alloc_aux_for_block (bb, size); |
402209ff JH |
591 | } |
592 | } | |
ca6c03ca | 593 | |
108c1afc | 594 | /* Clear AUX pointers of all blocks. */ |
402209ff JH |
595 | |
596 | void | |
d329e058 | 597 | clear_aux_for_blocks (void) |
402209ff | 598 | { |
e0082a72 | 599 | basic_block bb; |
4891442b | 600 | |
04a90bec | 601 | FOR_ALL_BB_FN (bb, cfun) |
e0082a72 | 602 | bb->aux = NULL; |
108c1afc RH |
603 | } |
604 | ||
605 | /* Free data allocated in block_aux_obstack and clear AUX pointers | |
606 | of all blocks. */ | |
607 | ||
608 | void | |
d329e058 | 609 | free_aux_for_blocks (void) |
108c1afc | 610 | { |
341c100f | 611 | gcc_assert (first_block_aux_obj); |
108c1afc | 612 | obstack_free (&block_aux_obstack, first_block_aux_obj); |
ca6c03ca | 613 | first_block_aux_obj = NULL; |
108c1afc RH |
614 | |
615 | clear_aux_for_blocks (); | |
ca6c03ca | 616 | } |
402209ff | 617 | |
039496da | 618 | /* Allocate a memory edge of SIZE as E->aux. The obstack must |
ca6c03ca | 619 | be first initialized by alloc_aux_for_edges. */ |
402209ff | 620 | |
039496da | 621 | void |
d329e058 | 622 | alloc_aux_for_edge (edge e, int size) |
ca6c03ca JH |
623 | { |
624 | /* Verify that aux field is clear. */ | |
341c100f | 625 | gcc_assert (!e->aux && first_edge_aux_obj); |
ca6c03ca JH |
626 | e->aux = obstack_alloc (&edge_aux_obstack, size); |
627 | memset (e->aux, 0, size); | |
628 | } | |
402209ff | 629 | |
ca6c03ca JH |
630 | /* Initialize the edge_aux_obstack and if SIZE is nonzero, call |
631 | alloc_aux_for_edge for each basic edge. */ | |
402209ff | 632 | |
ca6c03ca | 633 | void |
d329e058 | 634 | alloc_aux_for_edges (int size) |
ca6c03ca JH |
635 | { |
636 | static int initialized; | |
402209ff | 637 | |
ca6c03ca JH |
638 | if (!initialized) |
639 | { | |
640 | gcc_obstack_init (&edge_aux_obstack); | |
641 | initialized = 1; | |
402209ff | 642 | } |
341c100f NS |
643 | else |
644 | /* Check whether AUX data are still allocated. */ | |
645 | gcc_assert (!first_edge_aux_obj); | |
4891442b | 646 | |
703ad42b | 647 | first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0); |
ca6c03ca | 648 | if (size) |
402209ff | 649 | { |
e0082a72 ZD |
650 | basic_block bb; |
651 | ||
fefa31b5 DM |
652 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), |
653 | EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
402209ff | 654 | { |
ca6c03ca | 655 | edge e; |
628f6a4e | 656 | edge_iterator ei; |
ca6c03ca | 657 | |
628f6a4e | 658 | FOR_EACH_EDGE (e, ei, bb->succs) |
ca6c03ca | 659 | alloc_aux_for_edge (e, size); |
402209ff | 660 | } |
402209ff | 661 | } |
402209ff | 662 | } |
402209ff | 663 | |
108c1afc | 664 | /* Clear AUX pointers of all edges. */ |
ca6c03ca JH |
665 | |
666 | void | |
d329e058 | 667 | clear_aux_for_edges (void) |
402209ff | 668 | { |
e0082a72 ZD |
669 | basic_block bb; |
670 | edge e; | |
402209ff | 671 | |
fefa31b5 DM |
672 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), |
673 | EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
402209ff | 674 | { |
628f6a4e BE |
675 | edge_iterator ei; |
676 | FOR_EACH_EDGE (e, ei, bb->succs) | |
ca6c03ca | 677 | e->aux = NULL; |
402209ff | 678 | } |
108c1afc RH |
679 | } |
680 | ||
681 | /* Free data allocated in edge_aux_obstack and clear AUX pointers | |
682 | of all edges. */ | |
683 | ||
684 | void | |
d329e058 | 685 | free_aux_for_edges (void) |
108c1afc | 686 | { |
341c100f | 687 | gcc_assert (first_edge_aux_obj); |
108c1afc | 688 | obstack_free (&edge_aux_obstack, first_edge_aux_obj); |
ca6c03ca | 689 | first_edge_aux_obj = NULL; |
108c1afc RH |
690 | |
691 | clear_aux_for_edges (); | |
402209ff | 692 | } |
9ee634e3 | 693 | |
24e47c76 | 694 | DEBUG_FUNCTION void |
d329e058 | 695 | debug_bb (basic_block bb) |
10e9fecc | 696 | { |
f8923f7e | 697 | dump_bb (stderr, bb, 0, dump_flags); |
10e9fecc JH |
698 | } |
699 | ||
24e47c76 | 700 | DEBUG_FUNCTION basic_block |
d329e058 | 701 | debug_bb_n (int n) |
10e9fecc | 702 | { |
06e28de2 | 703 | basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n); |
a315c44c | 704 | debug_bb (bb); |
10e9fecc | 705 | return bb; |
9ee634e3 | 706 | } |
6de9cd9a | 707 | |
a315c44c SB |
708 | /* Dumps cfg related information about basic block BB to OUTF. |
709 | If HEADER is true, dump things that appear before the instructions | |
710 | contained in BB. If FOOTER is true, dump things that appear after. | |
711 | Flags are the TDF_* masks as documented in dumpfile.h. | |
712 | NB: With TDF_DETAILS, it is assumed that cfun is available, so | |
713 | that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */ | |
6de9cd9a | 714 | |
a315c44c SB |
715 | void |
716 | dump_bb_info (FILE *outf, basic_block bb, int indent, int flags, | |
717 | bool do_header, bool do_footer) | |
6de9cd9a | 718 | { |
628f6a4e | 719 | edge_iterator ei; |
a315c44c | 720 | edge e; |
6de9cd9a DN |
721 | static const char * const bb_bitnames[] = |
722 | { | |
a315c44c SB |
723 | #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME , |
724 | #include "cfg-flags.def" | |
725 | NULL | |
726 | #undef DEF_BASIC_BLOCK_FLAG | |
6de9cd9a DN |
727 | }; |
728 | const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *); | |
c4669594 | 729 | bool first; |
a315c44c SB |
730 | char *s_indent = (char *) alloca ((size_t) indent + 1); |
731 | memset ((void *) s_indent, ' ', (size_t) indent); | |
732 | s_indent[indent] = '\0'; | |
6de9cd9a | 733 | |
a315c44c SB |
734 | gcc_assert (bb->flags <= BB_ALL_FLAGS); |
735 | ||
736 | if (do_header) | |
737 | { | |
738 | unsigned i; | |
739 | ||
740 | if (flags & TDF_COMMENT) | |
741 | fputs (";; ", outf); | |
c4669594 | 742 | fprintf (outf, "%sbasic block %d, loop depth %d", |
391886c8 | 743 | s_indent, bb->index, bb_loop_depth (bb)); |
a315c44c SB |
744 | if (flags & TDF_DETAILS) |
745 | { | |
2eb712b4 | 746 | struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl); |
16998094 | 747 | fprintf (outf, ", count " "%" PRId64, |
a9243bfc | 748 | (int64_t) bb->count); |
a315c44c | 749 | fprintf (outf, ", freq %i", bb->frequency); |
2eb712b4 | 750 | if (maybe_hot_bb_p (fun, bb)) |
a315c44c | 751 | fputs (", maybe hot", outf); |
2eb712b4 | 752 | if (probably_never_executed_bb_p (fun, bb)) |
a315c44c SB |
753 | fputs (", probably never executed", outf); |
754 | } | |
755 | fputc ('\n', outf); | |
756 | ||
757 | if (flags & TDF_DETAILS) | |
758 | { | |
d00e4baa | 759 | check_bb_profile (bb, outf, indent, flags); |
a315c44c SB |
760 | if (flags & TDF_COMMENT) |
761 | fputs (";; ", outf); | |
762 | fprintf (outf, "%s prev block ", s_indent); | |
763 | if (bb->prev_bb) | |
764 | fprintf (outf, "%d", bb->prev_bb->index); | |
765 | else | |
766 | fprintf (outf, "(nil)"); | |
767 | fprintf (outf, ", next block "); | |
768 | if (bb->next_bb) | |
769 | fprintf (outf, "%d", bb->next_bb->index); | |
770 | else | |
771 | fprintf (outf, "(nil)"); | |
772 | ||
773 | fputs (", flags:", outf); | |
c4669594 | 774 | first = true; |
a315c44c SB |
775 | for (i = 0; i < n_bitnames; i++) |
776 | if (bb->flags & (1 << i)) | |
777 | { | |
778 | if (first) | |
779 | fputs (" (", outf); | |
780 | else | |
781 | fputs (", ", outf); | |
782 | first = false; | |
783 | fputs (bb_bitnames[i], outf); | |
784 | } | |
785 | if (!first) | |
786 | fputc (')', outf); | |
c4669594 | 787 | fputc ('\n', outf); |
a315c44c | 788 | } |
a315c44c SB |
789 | |
790 | if (flags & TDF_COMMENT) | |
791 | fputs (";; ", outf); | |
792 | fprintf (outf, "%s pred: ", s_indent); | |
c4669594 | 793 | first = true; |
a315c44c | 794 | FOR_EACH_EDGE (e, ei, bb->preds) |
c4669594 SB |
795 | { |
796 | if (! first) | |
797 | { | |
798 | if (flags & TDF_COMMENT) | |
799 | fputs (";; ", outf); | |
800 | fprintf (outf, "%s ", s_indent); | |
801 | } | |
802 | first = false; | |
803 | dump_edge_info (outf, e, flags, 0); | |
804 | fputc ('\n', outf); | |
805 | } | |
1dd5907e SB |
806 | if (first) |
807 | fputc ('\n', outf); | |
a315c44c SB |
808 | } |
809 | ||
810 | if (do_footer) | |
811 | { | |
a315c44c SB |
812 | if (flags & TDF_COMMENT) |
813 | fputs (";; ", outf); | |
814 | fprintf (outf, "%s succ: ", s_indent); | |
c4669594 | 815 | first = true; |
a315c44c | 816 | FOR_EACH_EDGE (e, ei, bb->succs) |
c4669594 SB |
817 | { |
818 | if (! first) | |
819 | { | |
820 | if (flags & TDF_COMMENT) | |
821 | fputs (";; ", outf); | |
822 | fprintf (outf, "%s ", s_indent); | |
823 | } | |
824 | first = false; | |
825 | dump_edge_info (outf, e, flags, 1); | |
826 | fputc ('\n', outf); | |
827 | } | |
1dd5907e SB |
828 | if (first) |
829 | fputc ('\n', outf); | |
a315c44c | 830 | } |
6de9cd9a DN |
831 | } |
832 | ||
833 | /* Dumps a brief description of cfg to FILE. */ | |
834 | ||
835 | void | |
c4669594 | 836 | brief_dump_cfg (FILE *file, int flags) |
6de9cd9a DN |
837 | { |
838 | basic_block bb; | |
839 | ||
11cd3bed | 840 | FOR_EACH_BB_FN (bb, cfun) |
6de9cd9a | 841 | { |
c4669594 SB |
842 | dump_bb_info (file, bb, 0, |
843 | flags & (TDF_COMMENT | TDF_DETAILS), | |
844 | true, true); | |
6de9cd9a DN |
845 | } |
846 | } | |
15db5571 JH |
847 | |
848 | /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to | |
849 | leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be | |
c22cacf3 | 850 | redirected to destination of TAKEN_EDGE. |
15db5571 JH |
851 | |
852 | This function may leave the profile inconsistent in the case TAKEN_EDGE | |
853 | frequency or count is believed to be lower than FREQUENCY or COUNT | |
d4a9b3a3 | 854 | respectively. */ |
15db5571 JH |
855 | void |
856 | update_bb_profile_for_threading (basic_block bb, int edge_frequency, | |
857 | gcov_type count, edge taken_edge) | |
858 | { | |
859 | edge c; | |
860 | int prob; | |
628f6a4e | 861 | edge_iterator ei; |
15db5571 JH |
862 | |
863 | bb->count -= count; | |
864 | if (bb->count < 0) | |
2b151cb2 JH |
865 | { |
866 | if (dump_file) | |
867 | fprintf (dump_file, "bb %i count became negative after threading", | |
868 | bb->index); | |
869 | bb->count = 0; | |
870 | } | |
15db5571 | 871 | |
a6a70dca JH |
872 | bb->frequency -= edge_frequency; |
873 | if (bb->frequency < 0) | |
874 | bb->frequency = 0; | |
875 | ||
15db5571 JH |
876 | /* Compute the probability of TAKEN_EDGE being reached via threaded edge. |
877 | Watch for overflows. */ | |
878 | if (bb->frequency) | |
8b47039c | 879 | prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency); |
15db5571 JH |
880 | else |
881 | prob = 0; | |
882 | if (prob > taken_edge->probability) | |
883 | { | |
884 | if (dump_file) | |
885 | fprintf (dump_file, "Jump threading proved probability of edge " | |
886 | "%i->%i too small (it is %i, should be %i).\n", | |
887 | taken_edge->src->index, taken_edge->dest->index, | |
888 | taken_edge->probability, prob); | |
a6a70dca | 889 | prob = taken_edge->probability * 6 / 8; |
15db5571 JH |
890 | } |
891 | ||
892 | /* Now rescale the probabilities. */ | |
893 | taken_edge->probability -= prob; | |
894 | prob = REG_BR_PROB_BASE - prob; | |
15db5571 JH |
895 | if (prob <= 0) |
896 | { | |
897 | if (dump_file) | |
898 | fprintf (dump_file, "Edge frequencies of bb %i has been reset, " | |
899 | "frequency of block should end up being 0, it is %i\n", | |
900 | bb->index, bb->frequency); | |
628f6a4e BE |
901 | EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE; |
902 | ei = ei_start (bb->succs); | |
903 | ei_next (&ei); | |
904 | for (; (c = ei_safe_edge (ei)); ei_next (&ei)) | |
15db5571 JH |
905 | c->probability = 0; |
906 | } | |
763ea904 JL |
907 | else if (prob != REG_BR_PROB_BASE) |
908 | { | |
09bac500 | 909 | int scale = RDIV (65536 * REG_BR_PROB_BASE, prob); |
763ea904 JL |
910 | |
911 | FOR_EACH_EDGE (c, ei, bb->succs) | |
84fc24e8 | 912 | { |
3bc8ba25 EB |
913 | /* Protect from overflow due to additional scaling. */ |
914 | if (c->probability > prob) | |
84fc24e8 | 915 | c->probability = REG_BR_PROB_BASE; |
3bc8ba25 EB |
916 | else |
917 | { | |
918 | c->probability = RDIV (c->probability * scale, 65536); | |
919 | if (c->probability > REG_BR_PROB_BASE) | |
920 | c->probability = REG_BR_PROB_BASE; | |
921 | } | |
84fc24e8 | 922 | } |
763ea904 | 923 | } |
15db5571 | 924 | |
41806d92 | 925 | gcc_assert (bb == taken_edge->src); |
15db5571 JH |
926 | taken_edge->count -= count; |
927 | if (taken_edge->count < 0) | |
2b151cb2 JH |
928 | { |
929 | if (dump_file) | |
930 | fprintf (dump_file, "edge %i->%i count became negative after threading", | |
931 | taken_edge->src->index, taken_edge->dest->index); | |
932 | taken_edge->count = 0; | |
933 | } | |
15db5571 | 934 | } |
33156717 JH |
935 | |
936 | /* Multiply all frequencies of basic blocks in array BBS of length NBBS | |
937 | by NUM/DEN, in int arithmetic. May lose some accuracy. */ | |
938 | void | |
939 | scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den) | |
940 | { | |
941 | int i; | |
942 | edge e; | |
84fc24e8 JH |
943 | if (num < 0) |
944 | num = 0; | |
03cb2019 ZD |
945 | |
946 | /* Scale NUM and DEN to avoid overflows. Frequencies are in order of | |
947 | 10^4, if we make DEN <= 10^3, we can afford to upscale by 100 | |
948 | and still safely fit in int during calculations. */ | |
949 | if (den > 1000) | |
950 | { | |
951 | if (num > 1000000) | |
952 | return; | |
953 | ||
954 | num = RDIV (1000 * num, den); | |
955 | den = 1000; | |
956 | } | |
957 | if (num > 100 * den) | |
84fc24e8 | 958 | return; |
03cb2019 | 959 | |
33156717 JH |
960 | for (i = 0; i < nbbs; i++) |
961 | { | |
962 | edge_iterator ei; | |
09bac500 | 963 | bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den); |
03cb2019 ZD |
964 | /* Make sure the frequencies do not grow over BB_FREQ_MAX. */ |
965 | if (bbs[i]->frequency > BB_FREQ_MAX) | |
966 | bbs[i]->frequency = BB_FREQ_MAX; | |
33156717 JH |
967 | bbs[i]->count = RDIV (bbs[i]->count * num, den); |
968 | FOR_EACH_EDGE (e, ei, bbs[i]->succs) | |
09bac500 | 969 | e->count = RDIV (e->count * num, den); |
33156717 JH |
970 | } |
971 | } | |
972 | ||
09bac500 JH |
973 | /* numbers smaller than this value are safe to multiply without getting |
974 | 64bit overflow. */ | |
a9243bfc | 975 | #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1)) |
09bac500 | 976 | |
33156717 JH |
977 | /* Multiply all frequencies of basic blocks in array BBS of length NBBS |
978 | by NUM/DEN, in gcov_type arithmetic. More accurate than previous | |
979 | function but considerably slower. */ | |
980 | void | |
c22cacf3 MS |
981 | scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num, |
982 | gcov_type den) | |
33156717 JH |
983 | { |
984 | int i; | |
985 | edge e; | |
09bac500 | 986 | gcov_type fraction = RDIV (num * 65536, den); |
33156717 | 987 | |
09bac500 JH |
988 | gcc_assert (fraction >= 0); |
989 | ||
990 | if (num < MAX_SAFE_MULTIPLIER) | |
991 | for (i = 0; i < nbbs; i++) | |
992 | { | |
993 | edge_iterator ei; | |
994 | bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den); | |
995 | if (bbs[i]->count <= MAX_SAFE_MULTIPLIER) | |
996 | bbs[i]->count = RDIV (bbs[i]->count * num, den); | |
997 | else | |
998 | bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536); | |
999 | FOR_EACH_EDGE (e, ei, bbs[i]->succs) | |
1000 | if (bbs[i]->count <= MAX_SAFE_MULTIPLIER) | |
1001 | e->count = RDIV (e->count * num, den); | |
1002 | else | |
1003 | e->count = RDIV (e->count * fraction, 65536); | |
1004 | } | |
1005 | else | |
1006 | for (i = 0; i < nbbs; i++) | |
1007 | { | |
1008 | edge_iterator ei; | |
1009 | if (sizeof (gcov_type) > sizeof (int)) | |
1010 | bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den); | |
1011 | else | |
1012 | bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536); | |
1013 | bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536); | |
1014 | FOR_EACH_EDGE (e, ei, bbs[i]->succs) | |
1015 | e->count = RDIV (e->count * fraction, 65536); | |
1016 | } | |
33156717 | 1017 | } |
6580ee77 | 1018 | |
703c8606 | 1019 | /* Helper types for hash tables. */ |
6580ee77 JH |
1020 | |
1021 | struct htab_bb_copy_original_entry | |
1022 | { | |
1023 | /* Block we are attaching info to. */ | |
1024 | int index1; | |
1025 | /* Index of original or copy (depending on the hashtable) */ | |
1026 | int index2; | |
1027 | }; | |
1028 | ||
8d67ee55 | 1029 | struct bb_copy_hasher : nofree_ptr_hash <htab_bb_copy_original_entry> |
6580ee77 | 1030 | { |
67f58944 TS |
1031 | static inline hashval_t hash (const htab_bb_copy_original_entry *); |
1032 | static inline bool equal (const htab_bb_copy_original_entry *existing, | |
1033 | const htab_bb_copy_original_entry * candidate); | |
703c8606 | 1034 | }; |
6580ee77 | 1035 | |
703c8606 | 1036 | inline hashval_t |
67f58944 | 1037 | bb_copy_hasher::hash (const htab_bb_copy_original_entry *data) |
703c8606 | 1038 | { |
6580ee77 JH |
1039 | return data->index1; |
1040 | } | |
6580ee77 | 1041 | |
703c8606 | 1042 | inline bool |
67f58944 TS |
1043 | bb_copy_hasher::equal (const htab_bb_copy_original_entry *data, |
1044 | const htab_bb_copy_original_entry *data2) | |
703c8606 | 1045 | { |
6580ee77 JH |
1046 | return data->index1 == data2->index1; |
1047 | } | |
1048 | ||
703c8606 LC |
1049 | /* Data structures used to maintain mapping between basic blocks and |
1050 | copies. */ | |
c203e8a7 TS |
1051 | static hash_table<bb_copy_hasher> *bb_original; |
1052 | static hash_table<bb_copy_hasher> *bb_copy; | |
703c8606 LC |
1053 | |
1054 | /* And between loops and copies. */ | |
c203e8a7 | 1055 | static hash_table<bb_copy_hasher> *loop_copy; |
fb0b2914 | 1056 | static object_allocator<htab_bb_copy_original_entry> *original_copy_bb_pool; |
703c8606 | 1057 | |
f341de7b KH |
1058 | /* Initialize the data structures to maintain mapping between blocks |
1059 | and its copies. */ | |
6580ee77 JH |
1060 | void |
1061 | initialize_original_copy_tables (void) | |
1062 | { | |
fb0b2914 | 1063 | original_copy_bb_pool = new object_allocator<htab_bb_copy_original_entry> |
fcb87c50 | 1064 | ("original_copy"); |
c203e8a7 TS |
1065 | bb_original = new hash_table<bb_copy_hasher> (10); |
1066 | bb_copy = new hash_table<bb_copy_hasher> (10); | |
1067 | loop_copy = new hash_table<bb_copy_hasher> (10); | |
6580ee77 JH |
1068 | } |
1069 | ||
d96004b8 BC |
1070 | /* Reset the data structures to maintain mapping between blocks and |
1071 | its copies. */ | |
1072 | ||
1073 | void | |
1074 | reset_original_copy_tables (void) | |
1075 | { | |
1076 | gcc_assert (original_copy_bb_pool); | |
1077 | bb_original->empty (); | |
1078 | bb_copy->empty (); | |
1079 | loop_copy->empty (); | |
1080 | } | |
1081 | ||
f341de7b KH |
1082 | /* Free the data structures to maintain mapping between blocks and |
1083 | its copies. */ | |
6580ee77 JH |
1084 | void |
1085 | free_original_copy_tables (void) | |
1086 | { | |
1087 | gcc_assert (original_copy_bb_pool); | |
c203e8a7 TS |
1088 | delete bb_copy; |
1089 | bb_copy = NULL; | |
1090 | delete bb_original; | |
af711c23 | 1091 | bb_original = NULL; |
c203e8a7 TS |
1092 | delete loop_copy; |
1093 | loop_copy = NULL; | |
ac0539d7 | 1094 | delete original_copy_bb_pool; |
6580ee77 JH |
1095 | original_copy_bb_pool = NULL; |
1096 | } | |
1097 | ||
c2e84327 DM |
1098 | /* Return true iff we have had a call to initialize_original_copy_tables |
1099 | without a corresponding call to free_original_copy_tables. */ | |
1100 | ||
1101 | bool | |
1102 | original_copy_tables_initialized_p (void) | |
1103 | { | |
1104 | return original_copy_bb_pool != NULL; | |
1105 | } | |
1106 | ||
561e8a90 ZD |
1107 | /* Removes the value associated with OBJ from table TAB. */ |
1108 | ||
1109 | static void | |
c203e8a7 | 1110 | copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj) |
561e8a90 | 1111 | { |
703c8606 | 1112 | htab_bb_copy_original_entry **slot; |
561e8a90 ZD |
1113 | struct htab_bb_copy_original_entry key, *elt; |
1114 | ||
1115 | if (!original_copy_bb_pool) | |
1116 | return; | |
1117 | ||
1118 | key.index1 = obj; | |
c203e8a7 | 1119 | slot = tab->find_slot (&key, NO_INSERT); |
561e8a90 ZD |
1120 | if (!slot) |
1121 | return; | |
1122 | ||
703c8606 | 1123 | elt = *slot; |
c203e8a7 | 1124 | tab->clear_slot (slot); |
ac0539d7 | 1125 | original_copy_bb_pool->remove (elt); |
561e8a90 ZD |
1126 | } |
1127 | ||
1128 | /* Sets the value associated with OBJ in table TAB to VAL. | |
1129 | Do nothing when data structures are not initialized. */ | |
1130 | ||
1131 | static void | |
c203e8a7 | 1132 | copy_original_table_set (hash_table<bb_copy_hasher> *tab, |
703c8606 | 1133 | unsigned obj, unsigned val) |
561e8a90 ZD |
1134 | { |
1135 | struct htab_bb_copy_original_entry **slot; | |
1136 | struct htab_bb_copy_original_entry key; | |
1137 | ||
1138 | if (!original_copy_bb_pool) | |
1139 | return; | |
1140 | ||
1141 | key.index1 = obj; | |
c203e8a7 | 1142 | slot = tab->find_slot (&key, INSERT); |
561e8a90 ZD |
1143 | if (!*slot) |
1144 | { | |
ac0539d7 | 1145 | *slot = original_copy_bb_pool->allocate (); |
561e8a90 ZD |
1146 | (*slot)->index1 = obj; |
1147 | } | |
1148 | (*slot)->index2 = val; | |
1149 | } | |
1150 | ||
f341de7b KH |
1151 | /* Set original for basic block. Do nothing when data structures are not |
1152 | initialized so passes not needing this don't need to care. */ | |
6580ee77 JH |
1153 | void |
1154 | set_bb_original (basic_block bb, basic_block original) | |
1155 | { | |
561e8a90 | 1156 | copy_original_table_set (bb_original, bb->index, original->index); |
6580ee77 JH |
1157 | } |
1158 | ||
1159 | /* Get the original basic block. */ | |
1160 | basic_block | |
1161 | get_bb_original (basic_block bb) | |
1162 | { | |
1163 | struct htab_bb_copy_original_entry *entry; | |
1164 | struct htab_bb_copy_original_entry key; | |
1165 | ||
1166 | gcc_assert (original_copy_bb_pool); | |
1167 | ||
1168 | key.index1 = bb->index; | |
c203e8a7 | 1169 | entry = bb_original->find (&key); |
6580ee77 | 1170 | if (entry) |
06e28de2 | 1171 | return BASIC_BLOCK_FOR_FN (cfun, entry->index2); |
6580ee77 JH |
1172 | else |
1173 | return NULL; | |
1174 | } | |
1175 | ||
f341de7b KH |
1176 | /* Set copy for basic block. Do nothing when data structures are not |
1177 | initialized so passes not needing this don't need to care. */ | |
6580ee77 JH |
1178 | void |
1179 | set_bb_copy (basic_block bb, basic_block copy) | |
1180 | { | |
561e8a90 | 1181 | copy_original_table_set (bb_copy, bb->index, copy->index); |
6580ee77 JH |
1182 | } |
1183 | ||
1184 | /* Get the copy of basic block. */ | |
1185 | basic_block | |
1186 | get_bb_copy (basic_block bb) | |
1187 | { | |
1188 | struct htab_bb_copy_original_entry *entry; | |
1189 | struct htab_bb_copy_original_entry key; | |
1190 | ||
1191 | gcc_assert (original_copy_bb_pool); | |
1192 | ||
1193 | key.index1 = bb->index; | |
c203e8a7 | 1194 | entry = bb_copy->find (&key); |
6580ee77 | 1195 | if (entry) |
06e28de2 | 1196 | return BASIC_BLOCK_FOR_FN (cfun, entry->index2); |
6580ee77 JH |
1197 | else |
1198 | return NULL; | |
1199 | } | |
561e8a90 ZD |
1200 | |
1201 | /* Set copy for LOOP to COPY. Do nothing when data structures are not | |
1202 | initialized so passes not needing this don't need to care. */ | |
1203 | ||
1204 | void | |
1205 | set_loop_copy (struct loop *loop, struct loop *copy) | |
1206 | { | |
1207 | if (!copy) | |
1208 | copy_original_table_clear (loop_copy, loop->num); | |
1209 | else | |
1210 | copy_original_table_set (loop_copy, loop->num, copy->num); | |
1211 | } | |
1212 | ||
1213 | /* Get the copy of LOOP. */ | |
1214 | ||
1215 | struct loop * | |
1216 | get_loop_copy (struct loop *loop) | |
1217 | { | |
1218 | struct htab_bb_copy_original_entry *entry; | |
1219 | struct htab_bb_copy_original_entry key; | |
1220 | ||
1221 | gcc_assert (original_copy_bb_pool); | |
1222 | ||
1223 | key.index1 = loop->num; | |
c203e8a7 | 1224 | entry = loop_copy->find (&key); |
561e8a90 | 1225 | if (entry) |
0fc822d0 | 1226 | return get_loop (cfun, entry->index2); |
561e8a90 ZD |
1227 | else |
1228 | return NULL; | |
1229 | } |