gcc/ada/
[official-gcc.git] / gcc / tracer.c
blob79cfd8d4dcf1a5ebe907d821578f3432b223d272
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-2015 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)
11 any later version.
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
33 performed. */
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
40 #include "alias.h"
41 #include "symtab.h"
42 #include "tree.h"
43 #include "fold-const.h"
44 #include "rtl.h"
45 #include "hard-reg-set.h"
46 #include "profile.h"
47 #include "predict.h"
48 #include "function.h"
49 #include "dominance.h"
50 #include "cfg.h"
51 #include "cfganal.h"
52 #include "basic-block.h"
53 #include "flags.h"
54 #include "params.h"
55 #include "coverage.h"
56 #include "tree-pass.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "gimple-expr.h"
60 #include "gimple.h"
61 #include "gimple-iterator.h"
62 #include "tree-cfg.h"
63 #include "tree-ssa.h"
64 #include "tree-inline.h"
65 #include "cfgloop.h"
66 #include "fibonacci_heap.h"
68 static int count_insns (basic_block);
69 static bool ignore_bb_p (const_basic_block);
70 static bool better_p (const_edge, const_edge);
71 static edge find_best_successor (basic_block);
72 static edge find_best_predecessor (basic_block);
73 static int find_trace (basic_block, basic_block *);
75 /* Minimal outgoing edge probability considered for superblock formation. */
76 static int probability_cutoff;
77 static int branch_ratio_cutoff;
79 /* A bit BB->index is set if BB has already been seen, i.e. it is
80 connected to some trace already. */
81 sbitmap bb_seen;
83 static inline void
84 mark_bb_seen (basic_block bb)
86 unsigned int size = SBITMAP_SIZE (bb_seen);
88 if ((unsigned int)bb->index >= size)
89 bb_seen = sbitmap_resize (bb_seen, size * 2, 0);
91 bitmap_set_bit (bb_seen, bb->index);
94 static inline bool
95 bb_seen_p (basic_block bb)
97 return bitmap_bit_p (bb_seen, bb->index);
100 /* Return true if we should ignore the basic block for purposes of tracing. */
101 static bool
102 ignore_bb_p (const_basic_block bb)
104 gimple g;
106 if (bb->index < NUM_FIXED_BLOCKS)
107 return true;
108 if (optimize_bb_for_size_p (bb))
109 return true;
111 /* A transaction is a single entry multiple exit region. It must be
112 duplicated in its entirety or not at all. */
113 g = last_stmt (CONST_CAST_BB (bb));
114 if (g && gimple_code (g) == GIMPLE_TRANSACTION)
115 return true;
117 return false;
120 /* Return number of instructions in the block. */
122 static int
123 count_insns (basic_block bb)
125 gimple_stmt_iterator gsi;
126 gimple stmt;
127 int n = 0;
129 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
131 stmt = gsi_stmt (gsi);
132 n += estimate_num_insns (stmt, &eni_size_weights);
134 return n;
137 /* Return true if E1 is more frequent than E2. */
138 static bool
139 better_p (const_edge e1, const_edge e2)
141 if (e1->count != e2->count)
142 return e1->count > e2->count;
143 if (e1->src->frequency * e1->probability !=
144 e2->src->frequency * e2->probability)
145 return (e1->src->frequency * e1->probability
146 > e2->src->frequency * e2->probability);
147 /* This is needed to avoid changes in the decision after
148 CFG is modified. */
149 if (e1->src != e2->src)
150 return e1->src->index > e2->src->index;
151 return e1->dest->index > e2->dest->index;
154 /* Return most frequent successor of basic block BB. */
156 static edge
157 find_best_successor (basic_block bb)
159 edge e;
160 edge best = NULL;
161 edge_iterator ei;
163 FOR_EACH_EDGE (e, ei, bb->succs)
164 if (!best || better_p (e, best))
165 best = e;
166 if (!best || ignore_bb_p (best->dest))
167 return NULL;
168 if (best->probability <= probability_cutoff)
169 return NULL;
170 return best;
173 /* Return most frequent predecessor of basic block BB. */
175 static edge
176 find_best_predecessor (basic_block bb)
178 edge e;
179 edge best = NULL;
180 edge_iterator ei;
182 FOR_EACH_EDGE (e, ei, bb->preds)
183 if (!best || better_p (e, best))
184 best = e;
185 if (!best || ignore_bb_p (best->src))
186 return NULL;
187 if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
188 < bb->frequency * branch_ratio_cutoff)
189 return NULL;
190 return best;
193 /* Find the trace using bb and record it in the TRACE array.
194 Return number of basic blocks recorded. */
196 static int
197 find_trace (basic_block bb, basic_block *trace)
199 int i = 0;
200 edge e;
202 if (dump_file)
203 fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
205 while ((e = find_best_predecessor (bb)) != NULL)
207 basic_block bb2 = e->src;
208 if (bb_seen_p (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
209 || find_best_successor (bb2) != e)
210 break;
211 if (dump_file)
212 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
213 bb = bb2;
215 if (dump_file)
216 fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
217 trace[i++] = bb;
219 /* Follow the trace in forward direction. */
220 while ((e = find_best_successor (bb)) != NULL)
222 bb = e->dest;
223 if (bb_seen_p (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
224 || find_best_predecessor (bb) != e)
225 break;
226 if (dump_file)
227 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
228 trace[i++] = bb;
230 if (dump_file)
231 fprintf (dump_file, "\n");
232 return i;
235 /* Look for basic blocks in frequency order, construct traces and tail duplicate
236 if profitable. */
238 static bool
239 tail_duplicate (void)
241 auto_vec<fibonacci_node<long, basic_block_def>*> blocks;
242 blocks.safe_grow_cleared (last_basic_block_for_fn (cfun));
244 basic_block *trace = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
245 int *counts = XNEWVEC (int, last_basic_block_for_fn (cfun));
246 int ninsns = 0, nduplicated = 0;
247 gcov_type weighted_insns = 0, traced_insns = 0;
248 fibonacci_heap<long, basic_block_def> heap (LONG_MIN);
249 gcov_type cover_insns;
250 int max_dup_insns;
251 basic_block bb;
252 bool changed = false;
254 /* Create an oversized sbitmap to reduce the chance that we need to
255 resize it. */
256 bb_seen = sbitmap_alloc (last_basic_block_for_fn (cfun) * 2);
257 bitmap_clear (bb_seen);
258 initialize_original_copy_tables ();
260 if (profile_info && flag_branch_probabilities)
261 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
262 else
263 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
264 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
266 branch_ratio_cutoff =
267 (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));
269 FOR_EACH_BB_FN (bb, cfun)
271 int n = count_insns (bb);
272 if (!ignore_bb_p (bb))
273 blocks[bb->index] = heap.insert (-bb->frequency, bb);
275 counts [bb->index] = n;
276 ninsns += n;
277 weighted_insns += n * bb->frequency;
280 if (profile_info && flag_branch_probabilities)
281 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
282 else
283 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
284 cover_insns = (weighted_insns * cover_insns + 50) / 100;
285 max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;
287 while (traced_insns < cover_insns && nduplicated < max_dup_insns
288 && !heap.empty ())
290 basic_block bb = heap.extract_min ();
291 int n, pos;
293 if (!bb)
294 break;
296 blocks[bb->index] = NULL;
298 if (ignore_bb_p (bb))
299 continue;
300 gcc_assert (!bb_seen_p (bb));
302 n = find_trace (bb, trace);
304 bb = trace[0];
305 traced_insns += bb->frequency * counts [bb->index];
306 if (blocks[bb->index])
308 heap.delete_node (blocks[bb->index]);
309 blocks[bb->index] = NULL;
312 for (pos = 1; pos < n; pos++)
314 basic_block bb2 = trace[pos];
316 if (blocks[bb2->index])
318 heap.delete_node (blocks[bb2->index]);
319 blocks[bb2->index] = NULL;
321 traced_insns += bb2->frequency * counts [bb2->index];
322 if (EDGE_COUNT (bb2->preds) > 1
323 && can_duplicate_block_p (bb2)
324 /* We have the tendency to duplicate the loop header
325 of all do { } while loops. Do not do that - it is
326 not profitable and it might create a loop with multiple
327 entries or at least rotate the loop. */
328 && bb2->loop_father->header != bb2)
330 edge e;
331 basic_block copy;
333 nduplicated += counts [bb2->index];
335 e = find_edge (bb, bb2);
337 copy = duplicate_block (bb2, e, bb);
338 flush_pending_stmts (e);
340 add_phi_args_after_copy (&copy, 1, NULL);
342 /* Reconsider the original copy of block we've duplicated.
343 Removing the most common predecessor may make it to be
344 head. */
345 blocks[bb2->index] = heap.insert (-bb2->frequency, bb2);
347 if (dump_file)
348 fprintf (dump_file, "Duplicated %i as %i [%i]\n",
349 bb2->index, copy->index, copy->frequency);
351 bb2 = copy;
352 changed = true;
354 mark_bb_seen (bb2);
355 bb = bb2;
356 /* In case the trace became infrequent, stop duplicating. */
357 if (ignore_bb_p (bb))
358 break;
360 if (dump_file)
361 fprintf (dump_file, " covered now %.1f\n\n",
362 traced_insns * 100.0 / weighted_insns);
364 if (dump_file)
365 fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
366 nduplicated * 100 / ninsns);
368 free_original_copy_tables ();
369 sbitmap_free (bb_seen);
370 free (trace);
371 free (counts);
373 return changed;
376 namespace {
378 const pass_data pass_data_tracer =
380 GIMPLE_PASS, /* type */
381 "tracer", /* name */
382 OPTGROUP_NONE, /* optinfo_flags */
383 TV_TRACER, /* tv_id */
384 0, /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
388 TODO_update_ssa, /* todo_flags_finish */
391 class pass_tracer : public gimple_opt_pass
393 public:
394 pass_tracer (gcc::context *ctxt)
395 : gimple_opt_pass (pass_data_tracer, ctxt)
398 /* opt_pass methods: */
399 virtual bool gate (function *)
401 return (optimize > 0 && flag_tracer && flag_reorder_blocks);
404 virtual unsigned int execute (function *);
406 }; // class pass_tracer
408 unsigned int
409 pass_tracer::execute (function *fun)
411 bool changed;
413 if (n_basic_blocks_for_fn (fun) <= NUM_FIXED_BLOCKS + 1)
414 return 0;
416 mark_dfs_back_edges ();
417 if (dump_file)
418 brief_dump_cfg (dump_file, dump_flags);
420 /* Trace formation is done on the fly inside tail_duplicate */
421 changed = tail_duplicate ();
422 if (changed)
424 free_dominance_info (CDI_DOMINATORS);
425 /* If we changed the CFG schedule loops for fixup by cleanup_cfg. */
426 loops_state_set (LOOPS_NEED_FIXUP);
429 if (dump_file)
430 brief_dump_cfg (dump_file, dump_flags);
432 return changed ? TODO_cleanup_cfg : 0;
434 } // anon namespace
436 gimple_opt_pass *
437 make_pass_tracer (gcc::context *ctxt)
439 return new pass_tracer (ctxt);