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