]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/tracer.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / tracer.c
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.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22 /* This pass performs the tail duplication needed for superblock formation.
23 For more information see:
24
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
28
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. */
34
35
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "backend.h"
40 #include "tree.h"
41 #include "gimple.h"
42 #include "rtl.h"
43 #include "alias.h"
44 #include "fold-const.h"
45 #include "profile.h"
46 #include "cfganal.h"
47 #include "flags.h"
48 #include "params.h"
49 #include "coverage.h"
50 #include "tree-pass.h"
51 #include "internal-fn.h"
52 #include "gimple-iterator.h"
53 #include "tree-cfg.h"
54 #include "tree-ssa.h"
55 #include "tree-inline.h"
56 #include "cfgloop.h"
57 #include "fibonacci_heap.h"
58
59 static int count_insns (basic_block);
60 static bool ignore_bb_p (const_basic_block);
61 static bool better_p (const_edge, const_edge);
62 static edge find_best_successor (basic_block);
63 static edge find_best_predecessor (basic_block);
64 static int find_trace (basic_block, basic_block *);
65
66 /* Minimal outgoing edge probability considered for superblock formation. */
67 static int probability_cutoff;
68 static int branch_ratio_cutoff;
69
70 /* A bit BB->index is set if BB has already been seen, i.e. it is
71 connected to some trace already. */
72 sbitmap bb_seen;
73
74 static inline void
75 mark_bb_seen (basic_block bb)
76 {
77 unsigned int size = SBITMAP_SIZE (bb_seen);
78
79 if ((unsigned int)bb->index >= size)
80 bb_seen = sbitmap_resize (bb_seen, size * 2, 0);
81
82 bitmap_set_bit (bb_seen, bb->index);
83 }
84
85 static inline bool
86 bb_seen_p (basic_block bb)
87 {
88 return bitmap_bit_p (bb_seen, bb->index);
89 }
90
91 /* Return true if we should ignore the basic block for purposes of tracing. */
92 static bool
93 ignore_bb_p (const_basic_block bb)
94 {
95 gimple g;
96
97 if (bb->index < NUM_FIXED_BLOCKS)
98 return true;
99 if (optimize_bb_for_size_p (bb))
100 return true;
101
102 /* A transaction is a single entry multiple exit region. It must be
103 duplicated in its entirety or not at all. */
104 g = last_stmt (CONST_CAST_BB (bb));
105 if (g && gimple_code (g) == GIMPLE_TRANSACTION)
106 return true;
107
108 return false;
109 }
110
111 /* Return number of instructions in the block. */
112
113 static int
114 count_insns (basic_block bb)
115 {
116 gimple_stmt_iterator gsi;
117 gimple stmt;
118 int n = 0;
119
120 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
121 {
122 stmt = gsi_stmt (gsi);
123 n += estimate_num_insns (stmt, &eni_size_weights);
124 }
125 return n;
126 }
127
128 /* Return true if E1 is more frequent than E2. */
129 static bool
130 better_p (const_edge e1, const_edge e2)
131 {
132 if (e1->count != e2->count)
133 return e1->count > e2->count;
134 if (e1->src->frequency * e1->probability !=
135 e2->src->frequency * e2->probability)
136 return (e1->src->frequency * e1->probability
137 > e2->src->frequency * e2->probability);
138 /* This is needed to avoid changes in the decision after
139 CFG is modified. */
140 if (e1->src != e2->src)
141 return e1->src->index > e2->src->index;
142 return e1->dest->index > e2->dest->index;
143 }
144
145 /* Return most frequent successor of basic block BB. */
146
147 static edge
148 find_best_successor (basic_block bb)
149 {
150 edge e;
151 edge best = NULL;
152 edge_iterator ei;
153
154 FOR_EACH_EDGE (e, ei, bb->succs)
155 if (!best || better_p (e, best))
156 best = e;
157 if (!best || ignore_bb_p (best->dest))
158 return NULL;
159 if (best->probability <= probability_cutoff)
160 return NULL;
161 return best;
162 }
163
164 /* Return most frequent predecessor of basic block BB. */
165
166 static edge
167 find_best_predecessor (basic_block bb)
168 {
169 edge e;
170 edge best = NULL;
171 edge_iterator ei;
172
173 FOR_EACH_EDGE (e, ei, bb->preds)
174 if (!best || better_p (e, best))
175 best = e;
176 if (!best || ignore_bb_p (best->src))
177 return NULL;
178 if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
179 < bb->frequency * branch_ratio_cutoff)
180 return NULL;
181 return best;
182 }
183
184 /* Find the trace using bb and record it in the TRACE array.
185 Return number of basic blocks recorded. */
186
187 static int
188 find_trace (basic_block bb, basic_block *trace)
189 {
190 int i = 0;
191 edge e;
192
193 if (dump_file)
194 fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
195
196 while ((e = find_best_predecessor (bb)) != NULL)
197 {
198 basic_block bb2 = e->src;
199 if (bb_seen_p (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
200 || find_best_successor (bb2) != e)
201 break;
202 if (dump_file)
203 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
204 bb = bb2;
205 }
206 if (dump_file)
207 fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
208 trace[i++] = bb;
209
210 /* Follow the trace in forward direction. */
211 while ((e = find_best_successor (bb)) != NULL)
212 {
213 bb = e->dest;
214 if (bb_seen_p (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
215 || find_best_predecessor (bb) != e)
216 break;
217 if (dump_file)
218 fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
219 trace[i++] = bb;
220 }
221 if (dump_file)
222 fprintf (dump_file, "\n");
223 return i;
224 }
225
226 /* Look for basic blocks in frequency order, construct traces and tail duplicate
227 if profitable. */
228
229 static bool
230 tail_duplicate (void)
231 {
232 auto_vec<fibonacci_node<long, basic_block_def>*> blocks;
233 blocks.safe_grow_cleared (last_basic_block_for_fn (cfun));
234
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 fibonacci_heap<long, basic_block_def> heap (LONG_MIN);
240 gcov_type cover_insns;
241 int max_dup_insns;
242 basic_block bb;
243 bool changed = false;
244
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 ();
250
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;
256
257 branch_ratio_cutoff =
258 (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));
259
260 FOR_EACH_BB_FN (bb, cfun)
261 {
262 int n = count_insns (bb);
263 if (!ignore_bb_p (bb))
264 blocks[bb->index] = heap.insert (-bb->frequency, bb);
265
266 counts [bb->index] = n;
267 ninsns += n;
268 weighted_insns += n * bb->frequency;
269 }
270
271 if (profile_info && flag_branch_probabilities)
272 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
273 else
274 cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
275 cover_insns = (weighted_insns * cover_insns + 50) / 100;
276 max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;
277
278 while (traced_insns < cover_insns && nduplicated < max_dup_insns
279 && !heap.empty ())
280 {
281 basic_block bb = heap.extract_min ();
282 int n, pos;
283
284 if (!bb)
285 break;
286
287 blocks[bb->index] = NULL;
288
289 if (ignore_bb_p (bb))
290 continue;
291 gcc_assert (!bb_seen_p (bb));
292
293 n = find_trace (bb, trace);
294
295 bb = trace[0];
296 traced_insns += bb->frequency * counts [bb->index];
297 if (blocks[bb->index])
298 {
299 heap.delete_node (blocks[bb->index]);
300 blocks[bb->index] = NULL;
301 }
302
303 for (pos = 1; pos < n; pos++)
304 {
305 basic_block bb2 = trace[pos];
306
307 if (blocks[bb2->index])
308 {
309 heap.delete_node (blocks[bb2->index]);
310 blocks[bb2->index] = NULL;
311 }
312 traced_insns += bb2->frequency * counts [bb2->index];
313 if (EDGE_COUNT (bb2->preds) > 1
314 && can_duplicate_block_p (bb2)
315 /* We have the tendency to duplicate the loop header
316 of all do { } while loops. Do not do that - it is
317 not profitable and it might create a loop with multiple
318 entries or at least rotate the loop. */
319 && bb2->loop_father->header != bb2)
320 {
321 edge e;
322 basic_block copy;
323
324 nduplicated += counts [bb2->index];
325
326 e = find_edge (bb, bb2);
327
328 copy = duplicate_block (bb2, e, bb);
329 flush_pending_stmts (e);
330
331 add_phi_args_after_copy (&copy, 1, NULL);
332
333 /* Reconsider the original copy of block we've duplicated.
334 Removing the most common predecessor may make it to be
335 head. */
336 blocks[bb2->index] = heap.insert (-bb2->frequency, bb2);
337
338 if (dump_file)
339 fprintf (dump_file, "Duplicated %i as %i [%i]\n",
340 bb2->index, copy->index, copy->frequency);
341
342 bb2 = copy;
343 changed = true;
344 }
345 mark_bb_seen (bb2);
346 bb = bb2;
347 /* In case the trace became infrequent, stop duplicating. */
348 if (ignore_bb_p (bb))
349 break;
350 }
351 if (dump_file)
352 fprintf (dump_file, " covered now %.1f\n\n",
353 traced_insns * 100.0 / weighted_insns);
354 }
355 if (dump_file)
356 fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
357 nduplicated * 100 / ninsns);
358
359 free_original_copy_tables ();
360 sbitmap_free (bb_seen);
361 free (trace);
362 free (counts);
363
364 return changed;
365 }
366 \f
367 namespace {
368
369 const pass_data pass_data_tracer =
370 {
371 GIMPLE_PASS, /* type */
372 "tracer", /* name */
373 OPTGROUP_NONE, /* optinfo_flags */
374 TV_TRACER, /* tv_id */
375 0, /* properties_required */
376 0, /* properties_provided */
377 0, /* properties_destroyed */
378 0, /* todo_flags_start */
379 TODO_update_ssa, /* todo_flags_finish */
380 };
381
382 class pass_tracer : public gimple_opt_pass
383 {
384 public:
385 pass_tracer (gcc::context *ctxt)
386 : gimple_opt_pass (pass_data_tracer, ctxt)
387 {}
388
389 /* opt_pass methods: */
390 virtual bool gate (function *)
391 {
392 return (optimize > 0 && flag_tracer && flag_reorder_blocks);
393 }
394
395 virtual unsigned int execute (function *);
396
397 }; // class pass_tracer
398
399 unsigned int
400 pass_tracer::execute (function *fun)
401 {
402 bool changed;
403
404 if (n_basic_blocks_for_fn (fun) <= NUM_FIXED_BLOCKS + 1)
405 return 0;
406
407 mark_dfs_back_edges ();
408 if (dump_file)
409 brief_dump_cfg (dump_file, dump_flags);
410
411 /* Trace formation is done on the fly inside tail_duplicate */
412 changed = tail_duplicate ();
413 if (changed)
414 {
415 free_dominance_info (CDI_DOMINATORS);
416 /* If we changed the CFG schedule loops for fixup by cleanup_cfg. */
417 loops_state_set (LOOPS_NEED_FIXUP);
418 }
419
420 if (dump_file)
421 brief_dump_cfg (dump_file, dump_flags);
422
423 return changed ? TODO_cleanup_cfg : 0;
424 }
425 } // anon namespace
426
427 gimple_opt_pass *
428 make_pass_tracer (gcc::context *ctxt)
429 {
430 return new pass_tracer (ctxt);
431 }