Daily bump.
[official-gcc.git] / gcc / tracer.c
blob86557febab7d6c8104b6468883691c610db86ac5
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-2013 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 "tree.h"
41 #include "rtl.h"
42 #include "hard-reg-set.h"
43 #include "basic-block.h"
44 #include "fibheap.h"
45 #include "flags.h"
46 #include "params.h"
47 #include "coverage.h"
48 #include "tree-pass.h"
49 #include "gimple.h"
50 #include "tree-cfg.h"
51 #include "tree-ssa.h"
52 #include "tree-inline.h"
53 #include "cfgloop.h"
55 static int count_insns (basic_block);
56 static bool ignore_bb_p (const_basic_block);
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 sbitmap bb_seen;
70 static inline void
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);
81 static inline bool
82 bb_seen_p (basic_block bb)
84 return bitmap_bit_p (bb_seen, bb->index);
87 /* Return true if we should ignore the basic block for purposes of tracing. */
88 static bool
89 ignore_bb_p (const_basic_block bb)
91 gimple g;
93 if (bb->index < NUM_FIXED_BLOCKS)
94 return true;
95 if (optimize_bb_for_size_p (bb))
96 return true;
98 /* A transaction is a single entry multiple exit region. It must be
99 duplicated in its entirety or not at all. */
100 g = last_stmt (CONST_CAST_BB (bb));
101 if (g && gimple_code (g) == GIMPLE_TRANSACTION)
102 return true;
104 return false;
107 /* Return number of instructions in the block. */
109 static int
110 count_insns (basic_block bb)
112 gimple_stmt_iterator gsi;
113 gimple stmt;
114 int n = 0;
116 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
118 stmt = gsi_stmt (gsi);
119 n += estimate_num_insns (stmt, &eni_size_weights);
121 return n;
124 /* Return true if E1 is more frequent than E2. */
125 static bool
126 better_p (const_edge e1, const_edge e2)
128 if (e1->count != e2->count)
129 return e1->count > e2->count;
130 if (e1->src->frequency * e1->probability !=
131 e2->src->frequency * e2->probability)
132 return (e1->src->frequency * e1->probability
133 > e2->src->frequency * e2->probability);
134 /* This is needed to avoid changes in the decision after
135 CFG is modified. */
136 if (e1->src != e2->src)
137 return e1->src->index > e2->src->index;
138 return e1->dest->index > e2->dest->index;
141 /* Return most frequent successor of basic block BB. */
143 static edge
144 find_best_successor (basic_block bb)
146 edge e;
147 edge best = NULL;
148 edge_iterator ei;
150 FOR_EACH_EDGE (e, ei, bb->succs)
151 if (!best || better_p (e, best))
152 best = e;
153 if (!best || ignore_bb_p (best->dest))
154 return NULL;
155 if (best->probability <= probability_cutoff)
156 return NULL;
157 return best;
160 /* Return most frequent predecessor of basic block BB. */
162 static edge
163 find_best_predecessor (basic_block bb)
165 edge e;
166 edge best = NULL;
167 edge_iterator ei;
169 FOR_EACH_EDGE (e, ei, bb->preds)
170 if (!best || better_p (e, best))
171 best = e;
172 if (!best || ignore_bb_p (best->src))
173 return NULL;
174 if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
175 < bb->frequency * branch_ratio_cutoff)
176 return NULL;
177 return best;
180 /* Find the trace using bb and record it in the TRACE array.
181 Return number of basic blocks recorded. */
183 static int
184 find_trace (basic_block bb, basic_block *trace)
186 int i = 0;
187 edge e;
189 if (dump_file)
190 fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
192 while ((e = find_best_predecessor (bb)) != NULL)
194 basic_block bb2 = e->src;
195 if (bb_seen_p (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
196 || find_best_successor (bb2) != e)
197 break;
198 if (dump_file)
199 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
200 bb = bb2;
202 if (dump_file)
203 fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
204 trace[i++] = bb;
206 /* Follow the trace in forward direction. */
207 while ((e = find_best_successor (bb)) != NULL)
209 bb = e->dest;
210 if (bb_seen_p (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
211 || find_best_predecessor (bb) != e)
212 break;
213 if (dump_file)
214 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
215 trace[i++] = bb;
217 if (dump_file)
218 fprintf (dump_file, "\n");
219 return i;
222 /* Look for basic blocks in frequency order, construct traces and tail duplicate
223 if profitable. */
225 static bool
226 tail_duplicate (void)
228 fibnode_t *blocks = XCNEWVEC (fibnode_t, last_basic_block);
229 basic_block *trace = XNEWVEC (basic_block, n_basic_blocks);
230 int *counts = XNEWVEC (int, last_basic_block);
231 int ninsns = 0, nduplicated = 0;
232 gcov_type weighted_insns = 0, traced_insns = 0;
233 fibheap_t heap = fibheap_new ();
234 gcov_type cover_insns;
235 int max_dup_insns;
236 basic_block bb;
237 bool changed = false;
239 /* Create an oversized sbitmap to reduce the chance that we need to
240 resize it. */
241 bb_seen = sbitmap_alloc (last_basic_block * 2);
242 bitmap_clear (bb_seen);
243 initialize_original_copy_tables ();
245 if (profile_info && flag_branch_probabilities)
246 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
247 else
248 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
249 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
251 branch_ratio_cutoff =
252 (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));
254 FOR_EACH_BB (bb)
256 int n = count_insns (bb);
257 if (!ignore_bb_p (bb))
258 blocks[bb->index] = fibheap_insert (heap, -bb->frequency,
259 bb);
261 counts [bb->index] = n;
262 ninsns += n;
263 weighted_insns += n * bb->frequency;
266 if (profile_info && flag_branch_probabilities)
267 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
268 else
269 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
270 cover_insns = (weighted_insns * cover_insns + 50) / 100;
271 max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;
273 while (traced_insns < cover_insns && nduplicated < max_dup_insns
274 && !fibheap_empty (heap))
276 basic_block bb = (basic_block) fibheap_extract_min (heap);
277 int n, pos;
279 if (!bb)
280 break;
282 blocks[bb->index] = NULL;
284 if (ignore_bb_p (bb))
285 continue;
286 gcc_assert (!bb_seen_p (bb));
288 n = find_trace (bb, trace);
290 bb = trace[0];
291 traced_insns += bb->frequency * counts [bb->index];
292 if (blocks[bb->index])
294 fibheap_delete_node (heap, blocks[bb->index]);
295 blocks[bb->index] = NULL;
298 for (pos = 1; pos < n; pos++)
300 basic_block bb2 = trace[pos];
302 if (blocks[bb2->index])
304 fibheap_delete_node (heap, blocks[bb2->index]);
305 blocks[bb2->index] = NULL;
307 traced_insns += bb2->frequency * counts [bb2->index];
308 if (EDGE_COUNT (bb2->preds) > 1
309 && can_duplicate_block_p (bb2)
310 /* We have the tendency to duplicate the loop header
311 of all do { } while loops. Do not do that - it is
312 not profitable and it might create a loop with multiple
313 entries or at least rotate the loop. */
314 && (!current_loops
315 || bb2->loop_father->header != bb2))
317 edge e;
318 basic_block copy;
320 nduplicated += counts [bb2->index];
322 e = find_edge (bb, bb2);
324 copy = duplicate_block (bb2, e, bb);
325 flush_pending_stmts (e);
327 add_phi_args_after_copy (&copy, 1, NULL);
329 /* Reconsider the original copy of block we've duplicated.
330 Removing the most common predecessor may make it to be
331 head. */
332 blocks[bb2->index] =
333 fibheap_insert (heap, -bb2->frequency, bb2);
335 if (dump_file)
336 fprintf (dump_file, "Duplicated %i as %i [%i]\n",
337 bb2->index, copy->index, copy->frequency);
339 bb2 = copy;
340 changed = true;
342 mark_bb_seen (bb2);
343 bb = bb2;
344 /* In case the trace became infrequent, stop duplicating. */
345 if (ignore_bb_p (bb))
346 break;
348 if (dump_file)
349 fprintf (dump_file, " covered now %.1f\n\n",
350 traced_insns * 100.0 / weighted_insns);
352 if (dump_file)
353 fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
354 nduplicated * 100 / ninsns);
356 free_original_copy_tables ();
357 sbitmap_free (bb_seen);
358 free (blocks);
359 free (trace);
360 free (counts);
361 fibheap_delete (heap);
363 return changed;
366 /* Main entry point to this file. */
368 static unsigned int
369 tracer (void)
371 bool changed;
373 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1)
374 return 0;
376 mark_dfs_back_edges ();
377 if (dump_file)
378 brief_dump_cfg (dump_file, dump_flags);
380 /* Trace formation is done on the fly inside tail_duplicate */
381 changed = tail_duplicate ();
382 if (changed)
384 free_dominance_info (CDI_DOMINATORS);
385 /* If we changed the CFG schedule loops for fixup by cleanup_cfg. */
386 if (current_loops)
387 loops_state_set (LOOPS_NEED_FIXUP);
390 if (dump_file)
391 brief_dump_cfg (dump_file, dump_flags);
393 return changed ? TODO_cleanup_cfg : 0;
396 static bool
397 gate_tracer (void)
399 return (optimize > 0 && flag_tracer && flag_reorder_blocks);
402 namespace {
404 const pass_data pass_data_tracer =
406 GIMPLE_PASS, /* type */
407 "tracer", /* name */
408 OPTGROUP_NONE, /* optinfo_flags */
409 true, /* has_gate */
410 true, /* has_execute */
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_verify_ssa ), /* todo_flags_finish */
419 class pass_tracer : public gimple_opt_pass
421 public:
422 pass_tracer (gcc::context *ctxt)
423 : gimple_opt_pass (pass_data_tracer, ctxt)
426 /* opt_pass methods: */
427 bool gate () { return gate_tracer (); }
428 unsigned int execute () { return tracer (); }
430 }; // class pass_tracer
432 } // anon namespace
434 gimple_opt_pass *
435 make_pass_tracer (gcc::context *ctxt)
437 return new pass_tracer (ctxt);