2013-03-20 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tracer.c
blob975cadb9285a27e639538a54beb686b89aecf332
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 "tree-flow.h"
50 #include "tree-inline.h"
51 #include "cfgloop.h"
53 static int count_insns (basic_block);
54 static bool ignore_bb_p (const_basic_block);
55 static bool better_p (const_edge, const_edge);
56 static edge find_best_successor (basic_block);
57 static edge find_best_predecessor (basic_block);
58 static int find_trace (basic_block, basic_block *);
60 /* Minimal outgoing edge probability considered for superblock formation. */
61 static int probability_cutoff;
62 static int branch_ratio_cutoff;
64 /* A bit BB->index is set if BB has already been seen, i.e. it is
65 connected to some trace already. */
66 sbitmap bb_seen;
68 static inline void
69 mark_bb_seen (basic_block bb)
71 unsigned int size = SBITMAP_SIZE (bb_seen);
73 if ((unsigned int)bb->index >= size)
74 bb_seen = sbitmap_resize (bb_seen, size * 2, 0);
76 bitmap_set_bit (bb_seen, bb->index);
79 static inline bool
80 bb_seen_p (basic_block bb)
82 return bitmap_bit_p (bb_seen, bb->index);
85 /* Return true if we should ignore the basic block for purposes of tracing. */
86 static bool
87 ignore_bb_p (const_basic_block bb)
89 gimple g;
91 if (bb->index < NUM_FIXED_BLOCKS)
92 return true;
93 if (optimize_bb_for_size_p (bb))
94 return true;
96 /* A transaction is a single entry multiple exit region. It must be
97 duplicated in its entirety or not at all. */
98 g = last_stmt (CONST_CAST_BB (bb));
99 if (g && gimple_code (g) == GIMPLE_TRANSACTION)
100 return true;
102 return false;
105 /* Return number of instructions in the block. */
107 static int
108 count_insns (basic_block bb)
110 gimple_stmt_iterator gsi;
111 gimple stmt;
112 int n = 0;
114 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
116 stmt = gsi_stmt (gsi);
117 n += estimate_num_insns (stmt, &eni_size_weights);
119 return n;
122 /* Return true if E1 is more frequent than E2. */
123 static bool
124 better_p (const_edge e1, const_edge e2)
126 if (e1->count != e2->count)
127 return e1->count > e2->count;
128 if (e1->src->frequency * e1->probability !=
129 e2->src->frequency * e2->probability)
130 return (e1->src->frequency * e1->probability
131 > e2->src->frequency * e2->probability);
132 /* This is needed to avoid changes in the decision after
133 CFG is modified. */
134 if (e1->src != e2->src)
135 return e1->src->index > e2->src->index;
136 return e1->dest->index > e2->dest->index;
139 /* Return most frequent successor of basic block BB. */
141 static edge
142 find_best_successor (basic_block bb)
144 edge e;
145 edge best = NULL;
146 edge_iterator ei;
148 FOR_EACH_EDGE (e, ei, bb->succs)
149 if (!best || better_p (e, best))
150 best = e;
151 if (!best || ignore_bb_p (best->dest))
152 return NULL;
153 if (best->probability <= probability_cutoff)
154 return NULL;
155 return best;
158 /* Return most frequent predecessor of basic block BB. */
160 static edge
161 find_best_predecessor (basic_block bb)
163 edge e;
164 edge best = NULL;
165 edge_iterator ei;
167 FOR_EACH_EDGE (e, ei, bb->preds)
168 if (!best || better_p (e, best))
169 best = e;
170 if (!best || ignore_bb_p (best->src))
171 return NULL;
172 if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
173 < bb->frequency * branch_ratio_cutoff)
174 return NULL;
175 return best;
178 /* Find the trace using bb and record it in the TRACE array.
179 Return number of basic blocks recorded. */
181 static int
182 find_trace (basic_block bb, basic_block *trace)
184 int i = 0;
185 edge e;
187 if (dump_file)
188 fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
190 while ((e = find_best_predecessor (bb)) != NULL)
192 basic_block bb2 = e->src;
193 if (bb_seen_p (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
194 || find_best_successor (bb2) != e)
195 break;
196 if (dump_file)
197 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
198 bb = bb2;
200 if (dump_file)
201 fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
202 trace[i++] = bb;
204 /* Follow the trace in forward direction. */
205 while ((e = find_best_successor (bb)) != NULL)
207 bb = e->dest;
208 if (bb_seen_p (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
209 || find_best_predecessor (bb) != e)
210 break;
211 if (dump_file)
212 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
213 trace[i++] = bb;
215 if (dump_file)
216 fprintf (dump_file, "\n");
217 return i;
220 /* Look for basic blocks in frequency order, construct traces and tail duplicate
221 if profitable. */
223 static bool
224 tail_duplicate (void)
226 fibnode_t *blocks = XCNEWVEC (fibnode_t, last_basic_block);
227 basic_block *trace = XNEWVEC (basic_block, n_basic_blocks);
228 int *counts = XNEWVEC (int, last_basic_block);
229 int ninsns = 0, nduplicated = 0;
230 gcov_type weighted_insns = 0, traced_insns = 0;
231 fibheap_t heap = fibheap_new ();
232 gcov_type cover_insns;
233 int max_dup_insns;
234 basic_block bb;
235 bool changed = false;
237 /* Create an oversized sbitmap to reduce the chance that we need to
238 resize it. */
239 bb_seen = sbitmap_alloc (last_basic_block * 2);
240 bitmap_clear (bb_seen);
241 initialize_original_copy_tables ();
243 if (profile_info && flag_branch_probabilities)
244 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
245 else
246 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
247 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
249 branch_ratio_cutoff =
250 (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));
252 FOR_EACH_BB (bb)
254 int n = count_insns (bb);
255 if (!ignore_bb_p (bb))
256 blocks[bb->index] = fibheap_insert (heap, -bb->frequency,
257 bb);
259 counts [bb->index] = n;
260 ninsns += n;
261 weighted_insns += n * bb->frequency;
264 if (profile_info && flag_branch_probabilities)
265 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
266 else
267 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
268 cover_insns = (weighted_insns * cover_insns + 50) / 100;
269 max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;
271 while (traced_insns < cover_insns && nduplicated < max_dup_insns
272 && !fibheap_empty (heap))
274 basic_block bb = (basic_block) fibheap_extract_min (heap);
275 int n, pos;
277 if (!bb)
278 break;
280 blocks[bb->index] = NULL;
282 if (ignore_bb_p (bb))
283 continue;
284 gcc_assert (!bb_seen_p (bb));
286 n = find_trace (bb, trace);
288 bb = trace[0];
289 traced_insns += bb->frequency * counts [bb->index];
290 if (blocks[bb->index])
292 fibheap_delete_node (heap, blocks[bb->index]);
293 blocks[bb->index] = NULL;
296 for (pos = 1; pos < n; pos++)
298 basic_block bb2 = trace[pos];
300 if (blocks[bb2->index])
302 fibheap_delete_node (heap, blocks[bb2->index]);
303 blocks[bb2->index] = NULL;
305 traced_insns += bb2->frequency * counts [bb2->index];
306 if (EDGE_COUNT (bb2->preds) > 1
307 && can_duplicate_block_p (bb2)
308 /* We have the tendency to duplicate the loop header
309 of all do { } while loops. Do not do that - it is
310 not profitable and it might create a loop with multiple
311 entries or at least rotate the loop. */
312 && (!current_loops
313 || bb2->loop_father->header != bb2))
315 edge e;
316 basic_block copy;
318 nduplicated += counts [bb2->index];
320 e = find_edge (bb, bb2);
322 copy = duplicate_block (bb2, e, bb);
323 flush_pending_stmts (e);
325 add_phi_args_after_copy (&copy, 1, NULL);
327 /* Reconsider the original copy of block we've duplicated.
328 Removing the most common predecessor may make it to be
329 head. */
330 blocks[bb2->index] =
331 fibheap_insert (heap, -bb2->frequency, bb2);
333 if (dump_file)
334 fprintf (dump_file, "Duplicated %i as %i [%i]\n",
335 bb2->index, copy->index, copy->frequency);
337 bb2 = copy;
338 changed = true;
340 mark_bb_seen (bb2);
341 bb = bb2;
342 /* In case the trace became infrequent, stop duplicating. */
343 if (ignore_bb_p (bb))
344 break;
346 if (dump_file)
347 fprintf (dump_file, " covered now %.1f\n\n",
348 traced_insns * 100.0 / weighted_insns);
350 if (dump_file)
351 fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
352 nduplicated * 100 / ninsns);
354 free_original_copy_tables ();
355 sbitmap_free (bb_seen);
356 free (blocks);
357 free (trace);
358 free (counts);
359 fibheap_delete (heap);
361 return changed;
364 /* Main entry point to this file. */
366 static unsigned int
367 tracer (void)
369 bool changed;
371 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1)
372 return 0;
374 mark_dfs_back_edges ();
375 if (dump_file)
376 brief_dump_cfg (dump_file, dump_flags);
378 /* Trace formation is done on the fly inside tail_duplicate */
379 changed = tail_duplicate ();
380 if (changed)
382 free_dominance_info (CDI_DOMINATORS);
383 /* If we changed the CFG schedule loops for fixup by cleanup_cfg. */
384 if (current_loops)
385 loops_state_set (LOOPS_NEED_FIXUP);
388 if (dump_file)
389 brief_dump_cfg (dump_file, dump_flags);
391 return changed ? TODO_cleanup_cfg : 0;
394 static bool
395 gate_tracer (void)
397 return (optimize > 0 && flag_tracer && flag_reorder_blocks);
400 struct gimple_opt_pass pass_tracer =
403 GIMPLE_PASS,
404 "tracer", /* name */
405 OPTGROUP_NONE, /* optinfo_flags */
406 gate_tracer, /* gate */
407 tracer, /* execute */
408 NULL, /* sub */
409 NULL, /* next */
410 0, /* static_pass_number */
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
417 | TODO_verify_ssa /* todo_flags_finish */