1 /* The tracer pass for the GNU compiler.
2 Contributed by Jan Hubicka, SuSE Labs.
3 Adapted to work on GIMPLE instead of RTL by Robert Kidd, UIUC.
4 Copyright (C) 2001-2023 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This pass performs the tail duplication needed for superblock formation.
23 For more information see:
25 Design and Analysis of Profile-Based Optimization in Compaq's
26 Compilation Tools for Alpha; Journal of Instruction-Level
27 Parallelism 3 (2000) 1-25
29 Unlike Compaq's implementation we don't do the loop peeling as most
30 probably a better job can be done by a special pass and we don't
31 need to worry too much about the code size implications as the tail
32 duplicates are crossjumped again if optimizations are not
38 #include "coretypes.h"
44 #include "tree-pass.h"
47 #include "gimple-iterator.h"
50 #include "tree-inline.h"
52 #include "alloc-pool.h"
53 #include "fibonacci_heap.h"
56 static void analyze_bb (basic_block
, int *);
57 static bool better_p (const_edge
, const_edge
);
58 static edge
find_best_successor (basic_block
);
59 static edge
find_best_predecessor (basic_block
);
60 static int find_trace (basic_block
, basic_block
*);
62 /* Minimal outgoing edge probability considered for superblock formation. */
63 static int probability_cutoff
;
64 static int branch_ratio_cutoff
;
66 /* A bit BB->index is set if BB has already been seen, i.e. it is
67 connected to some trace already. */
68 static sbitmap bb_seen
;
71 mark_bb_seen (basic_block bb
)
73 unsigned int size
= SBITMAP_SIZE (bb_seen
);
75 if ((unsigned int)bb
->index
>= size
)
76 bb_seen
= sbitmap_resize (bb_seen
, size
* 2, 0);
78 bitmap_set_bit (bb_seen
, bb
->index
);
82 bb_seen_p (basic_block bb
)
84 return bitmap_bit_p (bb_seen
, bb
->index
);
87 static sbitmap can_duplicate_bb
;
89 /* Cache VAL as value of can_duplicate_bb_p for BB. */
91 cache_can_duplicate_bb_p (const_basic_block bb
, bool val
)
94 bitmap_set_bit (can_duplicate_bb
, bb
->index
);
97 /* Return cached value of can_duplicate_bb_p for BB. */
99 cached_can_duplicate_bb_p (const_basic_block bb
)
101 if (can_duplicate_bb
)
103 unsigned int size
= SBITMAP_SIZE (can_duplicate_bb
);
104 if ((unsigned int)bb
->index
< size
)
105 return bitmap_bit_p (can_duplicate_bb
, bb
->index
);
107 /* Assume added bb's should not be duplicated. */
111 return can_duplicate_block_p (bb
);
114 /* Return true if we should ignore the basic block for purposes of tracing. */
116 ignore_bb_p (const_basic_block bb
)
118 if (bb
->index
< NUM_FIXED_BLOCKS
)
120 if (optimize_bb_for_size_p (bb
))
123 return !cached_can_duplicate_bb_p (bb
);
126 /* Return number of instructions in the block. */
129 analyze_bb (basic_block bb
, int *count
)
131 gimple_stmt_iterator gsi
;
135 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
137 stmt
= gsi_stmt (gsi
);
138 n
+= estimate_num_insns (stmt
, &eni_size_weights
);
142 cache_can_duplicate_bb_p (bb
, can_duplicate_block_p (CONST_CAST_BB (bb
)));
145 /* Return true if E1 is more frequent than E2. */
147 better_p (const_edge e1
, const_edge e2
)
149 if ((e1
->count () > e2
->count ()) || (e1
->count () < e2
->count ()))
150 return e1
->count () > e2
->count ();
151 /* This is needed to avoid changes in the decision after
153 if (e1
->src
!= e2
->src
)
154 return e1
->src
->index
> e2
->src
->index
;
155 return e1
->dest
->index
> e2
->dest
->index
;
158 /* Return most frequent successor of basic block BB. */
161 find_best_successor (basic_block bb
)
167 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
169 if (!e
->count ().initialized_p ())
171 if (!best
|| better_p (e
, best
))
174 if (!best
|| ignore_bb_p (best
->dest
))
176 if (!best
->probability
.initialized_p ()
177 || best
->probability
.to_reg_br_prob_base () <= probability_cutoff
)
182 /* Return most frequent predecessor of basic block BB. */
185 find_best_predecessor (basic_block bb
)
191 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
193 if (!e
->count ().initialized_p ())
195 if (!best
|| better_p (e
, best
))
198 if (!best
|| ignore_bb_p (best
->src
))
200 if (bb
->count
.initialized_p ()
201 && (best
->count ().to_frequency (cfun
) * REG_BR_PROB_BASE
202 < bb
->count
.to_frequency (cfun
) * branch_ratio_cutoff
))
207 /* Find the trace using bb and record it in the TRACE array.
208 Return number of basic blocks recorded. */
211 find_trace (basic_block bb
, basic_block
*trace
)
217 fprintf (dump_file
, "Trace seed %i [%i]", bb
->index
, bb
->count
.to_frequency (cfun
));
219 while ((e
= find_best_predecessor (bb
)) != NULL
)
221 basic_block bb2
= e
->src
;
222 if (bb_seen_p (bb2
) || (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
223 || find_best_successor (bb2
) != e
)
226 fprintf (dump_file
, ",%i [%i]", bb
->index
, bb
->count
.to_frequency (cfun
));
230 fprintf (dump_file
, " forward %i [%i]", bb
->index
, bb
->count
.to_frequency (cfun
));
233 /* Follow the trace in forward direction. */
234 while ((e
= find_best_successor (bb
)) != NULL
)
237 if (bb_seen_p (bb
) || (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
238 || find_best_predecessor (bb
) != e
)
241 fprintf (dump_file
, ",%i [%i]", bb
->index
, bb
->count
.to_frequency (cfun
));
245 fprintf (dump_file
, "\n");
249 /* Duplicate block BB2, placing it after BB in the CFG. Return the
250 newly created block. */
252 transform_duplicate (basic_block bb
, basic_block bb2
)
257 e
= find_edge (bb
, bb2
);
259 copy
= duplicate_block (bb2
, e
, bb
);
260 flush_pending_stmts (e
);
262 add_phi_args_after_copy (©
, 1, NULL
);
267 /* Look for basic blocks in frequency order, construct traces and tail duplicate
271 tail_duplicate (void)
273 auto_vec
<fibonacci_node
<long, basic_block_def
>*> blocks
;
274 blocks
.safe_grow_cleared (last_basic_block_for_fn (cfun
), true);
276 basic_block
*trace
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
277 int *counts
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
278 int ninsns
= 0, nduplicated
= 0;
279 gcov_type weighted_insns
= 0, traced_insns
= 0;
280 fibonacci_heap
<long, basic_block_def
> heap (LONG_MIN
);
281 gcov_type cover_insns
;
284 bool changed
= false;
286 /* Create an oversized sbitmap to reduce the chance that we need to
288 bb_seen
= sbitmap_alloc (last_basic_block_for_fn (cfun
) * 2);
289 bitmap_clear (bb_seen
);
290 can_duplicate_bb
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
291 bitmap_clear (can_duplicate_bb
);
292 initialize_original_copy_tables ();
294 if (profile_info
&& profile_status_for_fn (cfun
) == PROFILE_READ
)
295 probability_cutoff
= param_tracer_min_branch_probability_feedback
;
297 probability_cutoff
= param_tracer_min_branch_probability
;
298 probability_cutoff
= REG_BR_PROB_BASE
/ 100 * probability_cutoff
;
300 branch_ratio_cutoff
=
301 (REG_BR_PROB_BASE
/ 100 * param_tracer_min_branch_ratio
);
303 FOR_EACH_BB_FN (bb
, cfun
)
307 if (!ignore_bb_p (bb
))
308 blocks
[bb
->index
] = heap
.insert (-bb
->count
.to_frequency (cfun
), bb
);
310 counts
[bb
->index
] = n
;
312 weighted_insns
+= n
* bb
->count
.to_frequency (cfun
);
315 if (profile_info
&& profile_status_for_fn (cfun
) == PROFILE_READ
)
316 cover_insns
= param_tracer_dynamic_coverage_feedback
;
318 cover_insns
= param_tracer_dynamic_coverage
;
319 cover_insns
= (weighted_insns
* cover_insns
+ 50) / 100;
320 max_dup_insns
= (ninsns
* param_tracer_max_code_growth
+ 50) / 100;
322 while (traced_insns
< cover_insns
&& nduplicated
< max_dup_insns
325 basic_block bb
= heap
.extract_min ();
331 blocks
[bb
->index
] = NULL
;
333 if (ignore_bb_p (bb
))
335 gcc_assert (!bb_seen_p (bb
));
337 n
= find_trace (bb
, trace
);
340 traced_insns
+= bb
->count
.to_frequency (cfun
) * counts
[bb
->index
];
341 if (blocks
[bb
->index
])
343 heap
.delete_node (blocks
[bb
->index
]);
344 blocks
[bb
->index
] = NULL
;
347 for (pos
= 1; pos
< n
; pos
++)
349 basic_block bb2
= trace
[pos
];
351 if (blocks
[bb2
->index
])
353 heap
.delete_node (blocks
[bb2
->index
]);
354 blocks
[bb2
->index
] = NULL
;
356 traced_insns
+= bb2
->count
.to_frequency (cfun
) * counts
[bb2
->index
];
357 if (EDGE_COUNT (bb2
->preds
) > 1
358 && can_duplicate_block_p (bb2
)
359 /* We have the tendency to duplicate the loop header
360 of all do { } while loops. Do not do that - it is
361 not profitable and it might create a loop with multiple
362 entries or at least rotate the loop. */
363 && bb2
->loop_father
->header
!= bb2
)
365 nduplicated
+= counts
[bb2
->index
];
366 basic_block copy
= transform_duplicate (bb
, bb2
);
368 /* Reconsider the original copy of block we've duplicated.
369 Removing the most common predecessor may make it to be
371 blocks
[bb2
->index
] = heap
.insert (-bb2
->count
.to_frequency (cfun
), bb2
);
374 fprintf (dump_file
, "Duplicated %i as %i [%i]\n",
375 bb2
->index
, copy
->index
, copy
->count
.to_frequency (cfun
));
382 /* In case the trace became infrequent, stop duplicating. */
383 if (ignore_bb_p (bb
))
387 fprintf (dump_file
, " covered now %.1f\n\n",
388 traced_insns
* 100.0 / weighted_insns
);
391 fprintf (dump_file
, "Duplicated %i insns (%i%%)\n", nduplicated
,
392 nduplicated
* 100 / ninsns
);
394 free_original_copy_tables ();
395 sbitmap_free (bb_seen
);
396 sbitmap_free (can_duplicate_bb
);
397 can_duplicate_bb
= NULL
;
406 const pass_data pass_data_tracer
=
408 GIMPLE_PASS
, /* type */
410 OPTGROUP_NONE
, /* optinfo_flags */
411 TV_TRACER
, /* tv_id */
412 0, /* properties_required */
413 0, /* properties_provided */
414 0, /* properties_destroyed */
415 0, /* todo_flags_start */
416 TODO_update_ssa
, /* todo_flags_finish */
419 class pass_tracer
: public gimple_opt_pass
422 pass_tracer (gcc::context
*ctxt
)
423 : gimple_opt_pass (pass_data_tracer
, ctxt
)
426 /* opt_pass methods: */
427 bool gate (function
*) final override
429 return (optimize
> 0 && flag_tracer
&& flag_reorder_blocks
);
432 unsigned int execute (function
*) final override
;
434 }; // class pass_tracer
437 pass_tracer::execute (function
*fun
)
441 if (n_basic_blocks_for_fn (fun
) <= NUM_FIXED_BLOCKS
+ 1)
444 mark_dfs_back_edges ();
446 brief_dump_cfg (dump_file
, dump_flags
);
448 /* Trace formation is done on the fly inside tail_duplicate */
449 changed
= tail_duplicate ();
452 free_dominance_info (CDI_DOMINATORS
);
453 /* If we changed the CFG schedule loops for fixup by cleanup_cfg. */
454 loops_state_set (LOOPS_NEED_FIXUP
);
458 brief_dump_cfg (dump_file
, dump_flags
);
460 return changed
? TODO_cleanup_cfg
: 0;
465 make_pass_tracer (gcc::context
*ctxt
)
467 return new pass_tracer (ctxt
);