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