Fix gnu11 fallout on SPARC
[official-gcc.git] / gcc / tracer.c
blobb72bcb50321aab96fb64c5bb26f4a4bd758afede
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-2014 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 "profile.h"
44 #include "basic-block.h"
45 #include "fibheap.h"
46 #include "flags.h"
47 #include "params.h"
48 #include "coverage.h"
49 #include "tree-pass.h"
50 #include "tree-ssa-alias.h"
51 #include "internal-fn.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimple-iterator.h"
56 #include "tree-cfg.h"
57 #include "tree-ssa.h"
58 #include "tree-inline.h"
59 #include "cfgloop.h"
61 static int count_insns (basic_block);
62 static bool ignore_bb_p (const_basic_block);
63 static bool better_p (const_edge, const_edge);
64 static edge find_best_successor (basic_block);
65 static edge find_best_predecessor (basic_block);
66 static int find_trace (basic_block, basic_block *);
68 /* Minimal outgoing edge probability considered for superblock formation. */
69 static int probability_cutoff;
70 static int branch_ratio_cutoff;
72 /* A bit BB->index is set if BB has already been seen, i.e. it is
73 connected to some trace already. */
74 sbitmap bb_seen;
76 static inline void
77 mark_bb_seen (basic_block bb)
79 unsigned int size = SBITMAP_SIZE (bb_seen);
81 if ((unsigned int)bb->index >= size)
82 bb_seen = sbitmap_resize (bb_seen, size * 2, 0);
84 bitmap_set_bit (bb_seen, bb->index);
87 static inline bool
88 bb_seen_p (basic_block bb)
90 return bitmap_bit_p (bb_seen, bb->index);
93 /* Return true if we should ignore the basic block for purposes of tracing. */
94 static bool
95 ignore_bb_p (const_basic_block bb)
97 gimple g;
99 if (bb->index < NUM_FIXED_BLOCKS)
100 return true;
101 if (optimize_bb_for_size_p (bb))
102 return true;
104 /* A transaction is a single entry multiple exit region. It must be
105 duplicated in its entirety or not at all. */
106 g = last_stmt (CONST_CAST_BB (bb));
107 if (g && gimple_code (g) == GIMPLE_TRANSACTION)
108 return true;
110 return false;
113 /* Return number of instructions in the block. */
115 static int
116 count_insns (basic_block bb)
118 gimple_stmt_iterator gsi;
119 gimple stmt;
120 int n = 0;
122 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
124 stmt = gsi_stmt (gsi);
125 n += estimate_num_insns (stmt, &eni_size_weights);
127 return n;
130 /* Return true if E1 is more frequent than E2. */
131 static bool
132 better_p (const_edge e1, const_edge e2)
134 if (e1->count != e2->count)
135 return e1->count > e2->count;
136 if (e1->src->frequency * e1->probability !=
137 e2->src->frequency * e2->probability)
138 return (e1->src->frequency * e1->probability
139 > e2->src->frequency * e2->probability);
140 /* This is needed to avoid changes in the decision after
141 CFG is modified. */
142 if (e1->src != e2->src)
143 return e1->src->index > e2->src->index;
144 return e1->dest->index > e2->dest->index;
147 /* Return most frequent successor of basic block BB. */
149 static edge
150 find_best_successor (basic_block bb)
152 edge e;
153 edge best = NULL;
154 edge_iterator ei;
156 FOR_EACH_EDGE (e, ei, bb->succs)
157 if (!best || better_p (e, best))
158 best = e;
159 if (!best || ignore_bb_p (best->dest))
160 return NULL;
161 if (best->probability <= probability_cutoff)
162 return NULL;
163 return best;
166 /* Return most frequent predecessor of basic block BB. */
168 static edge
169 find_best_predecessor (basic_block bb)
171 edge e;
172 edge best = NULL;
173 edge_iterator ei;
175 FOR_EACH_EDGE (e, ei, bb->preds)
176 if (!best || better_p (e, best))
177 best = e;
178 if (!best || ignore_bb_p (best->src))
179 return NULL;
180 if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
181 < bb->frequency * branch_ratio_cutoff)
182 return NULL;
183 return best;
186 /* Find the trace using bb and record it in the TRACE array.
187 Return number of basic blocks recorded. */
189 static int
190 find_trace (basic_block bb, basic_block *trace)
192 int i = 0;
193 edge e;
195 if (dump_file)
196 fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
198 while ((e = find_best_predecessor (bb)) != NULL)
200 basic_block bb2 = e->src;
201 if (bb_seen_p (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
202 || find_best_successor (bb2) != e)
203 break;
204 if (dump_file)
205 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
206 bb = bb2;
208 if (dump_file)
209 fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
210 trace[i++] = bb;
212 /* Follow the trace in forward direction. */
213 while ((e = find_best_successor (bb)) != NULL)
215 bb = e->dest;
216 if (bb_seen_p (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
217 || find_best_predecessor (bb) != e)
218 break;
219 if (dump_file)
220 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
221 trace[i++] = bb;
223 if (dump_file)
224 fprintf (dump_file, "\n");
225 return i;
228 /* Look for basic blocks in frequency order, construct traces and tail duplicate
229 if profitable. */
231 static bool
232 tail_duplicate (void)
234 fibnode_t *blocks = XCNEWVEC (fibnode_t, last_basic_block_for_fn (cfun));
235 basic_block *trace = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
236 int *counts = XNEWVEC (int, last_basic_block_for_fn (cfun));
237 int ninsns = 0, nduplicated = 0;
238 gcov_type weighted_insns = 0, traced_insns = 0;
239 fibheap_t heap = fibheap_new ();
240 gcov_type cover_insns;
241 int max_dup_insns;
242 basic_block bb;
243 bool changed = false;
245 /* Create an oversized sbitmap to reduce the chance that we need to
246 resize it. */
247 bb_seen = sbitmap_alloc (last_basic_block_for_fn (cfun) * 2);
248 bitmap_clear (bb_seen);
249 initialize_original_copy_tables ();
251 if (profile_info && flag_branch_probabilities)
252 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
253 else
254 probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
255 probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
257 branch_ratio_cutoff =
258 (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));
260 FOR_EACH_BB_FN (bb, cfun)
262 int n = count_insns (bb);
263 if (!ignore_bb_p (bb))
264 blocks[bb->index] = fibheap_insert (heap, -bb->frequency,
265 bb);
267 counts [bb->index] = n;
268 ninsns += n;
269 weighted_insns += n * bb->frequency;
272 if (profile_info && flag_branch_probabilities)
273 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
274 else
275 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
276 cover_insns = (weighted_insns * cover_insns + 50) / 100;
277 max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;
279 while (traced_insns < cover_insns && nduplicated < max_dup_insns
280 && !fibheap_empty (heap))
282 basic_block bb = (basic_block) fibheap_extract_min (heap);
283 int n, pos;
285 if (!bb)
286 break;
288 blocks[bb->index] = NULL;
290 if (ignore_bb_p (bb))
291 continue;
292 gcc_assert (!bb_seen_p (bb));
294 n = find_trace (bb, trace);
296 bb = trace[0];
297 traced_insns += bb->frequency * counts [bb->index];
298 if (blocks[bb->index])
300 fibheap_delete_node (heap, blocks[bb->index]);
301 blocks[bb->index] = NULL;
304 for (pos = 1; pos < n; pos++)
306 basic_block bb2 = trace[pos];
308 if (blocks[bb2->index])
310 fibheap_delete_node (heap, blocks[bb2->index]);
311 blocks[bb2->index] = NULL;
313 traced_insns += bb2->frequency * counts [bb2->index];
314 if (EDGE_COUNT (bb2->preds) > 1
315 && can_duplicate_block_p (bb2)
316 /* We have the tendency to duplicate the loop header
317 of all do { } while loops. Do not do that - it is
318 not profitable and it might create a loop with multiple
319 entries or at least rotate the loop. */
320 && bb2->loop_father->header != bb2)
322 edge e;
323 basic_block copy;
325 nduplicated += counts [bb2->index];
327 e = find_edge (bb, bb2);
329 copy = duplicate_block (bb2, e, bb);
330 flush_pending_stmts (e);
332 add_phi_args_after_copy (&copy, 1, NULL);
334 /* Reconsider the original copy of block we've duplicated.
335 Removing the most common predecessor may make it to be
336 head. */
337 blocks[bb2->index] =
338 fibheap_insert (heap, -bb2->frequency, bb2);
340 if (dump_file)
341 fprintf (dump_file, "Duplicated %i as %i [%i]\n",
342 bb2->index, copy->index, copy->frequency);
344 bb2 = copy;
345 changed = true;
347 mark_bb_seen (bb2);
348 bb = bb2;
349 /* In case the trace became infrequent, stop duplicating. */
350 if (ignore_bb_p (bb))
351 break;
353 if (dump_file)
354 fprintf (dump_file, " covered now %.1f\n\n",
355 traced_insns * 100.0 / weighted_insns);
357 if (dump_file)
358 fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
359 nduplicated * 100 / ninsns);
361 free_original_copy_tables ();
362 sbitmap_free (bb_seen);
363 free (blocks);
364 free (trace);
365 free (counts);
366 fibheap_delete (heap);
368 return changed;
371 namespace {
373 const pass_data pass_data_tracer =
375 GIMPLE_PASS, /* type */
376 "tracer", /* name */
377 OPTGROUP_NONE, /* optinfo_flags */
378 TV_TRACER, /* tv_id */
379 0, /* properties_required */
380 0, /* properties_provided */
381 0, /* properties_destroyed */
382 0, /* todo_flags_start */
383 TODO_update_ssa, /* todo_flags_finish */
386 class pass_tracer : public gimple_opt_pass
388 public:
389 pass_tracer (gcc::context *ctxt)
390 : gimple_opt_pass (pass_data_tracer, ctxt)
393 /* opt_pass methods: */
394 virtual bool gate (function *)
396 return (optimize > 0 && flag_tracer && flag_reorder_blocks);
399 virtual unsigned int execute (function *);
401 }; // class pass_tracer
403 unsigned int
404 pass_tracer::execute (function *fun)
406 bool changed;
408 if (n_basic_blocks_for_fn (fun) <= NUM_FIXED_BLOCKS + 1)
409 return 0;
411 mark_dfs_back_edges ();
412 if (dump_file)
413 brief_dump_cfg (dump_file, dump_flags);
415 /* Trace formation is done on the fly inside tail_duplicate */
416 changed = tail_duplicate ();
417 if (changed)
419 free_dominance_info (CDI_DOMINATORS);
420 /* If we changed the CFG schedule loops for fixup by cleanup_cfg. */
421 loops_state_set (LOOPS_NEED_FIXUP);
424 if (dump_file)
425 brief_dump_cfg (dump_file, dump_flags);
427 return changed ? TODO_cleanup_cfg : 0;
429 } // anon namespace
431 gimple_opt_pass *
432 make_pass_tracer (gcc::context *ctxt)
434 return new pass_tracer (ctxt);