re PR bootstrap/52808 (LTO bootstrap failed with bootstrap-profiled)
[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, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5    Free Software Foundation, Inc.
6
7    This file is part of GCC.
8
9    GCC is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3, or (at your option)
12    any later version.
13
14    GCC is distributed in the hope that it will be useful, but WITHOUT
15    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
17    License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with GCC; see the file COPYING3.  If not see
21    <http://www.gnu.org/licenses/>.  */
22
23 /* This pass performs the tail duplication needed for superblock formation.
24    For more information see:
25
26      Design and Analysis of Profile-Based Optimization in Compaq's
27      Compilation Tools for Alpha; Journal of Instruction-Level
28      Parallelism 3 (2000) 1-25
29
30    Unlike Compaq's implementation we don't do the loop peeling as most
31    probably a better job can be done by a special pass and we don't
32    need to worry too much about the code size implications as the tail
33    duplicates are crossjumped again if optimizations are not
34    performed.  */
35
36
37 #include "config.h"
38 #include "system.h"
39 #include "coretypes.h"
40 #include "tm.h"
41 #include "tree.h"
42 #include "rtl.h"
43 #include "hard-reg-set.h"
44 #include "basic-block.h"
45 #include "output.h"
46 #include "cfglayout.h"
47 #include "fibheap.h"
48 #include "flags.h"
49 #include "timevar.h"
50 #include "params.h"
51 #include "coverage.h"
52 #include "tree-pass.h"
53 #include "tree-flow.h"
54 #include "tree-inline.h"
55 #include "cfgloop.h"
56
57 static int count_insns (basic_block);
58 static bool ignore_bb_p (const_basic_block);
59 static bool better_p (const_edge, const_edge);
60 static edge find_best_successor (basic_block);
61 static edge find_best_predecessor (basic_block);
62 static int find_trace (basic_block, basic_block *);
63
64 /* Minimal outgoing edge probability considered for superblock formation.  */
65 static int probability_cutoff;
66 static int branch_ratio_cutoff;
67
68 /* A bit BB->index is set if BB has already been seen, i.e. it is
69    connected to some trace already.  */
70 sbitmap bb_seen;
71
72 static inline void
73 mark_bb_seen (basic_block bb)
74 {
75   unsigned int size = SBITMAP_SIZE_BYTES (bb_seen) * 8;
76
77   if ((unsigned int)bb->index >= size)
78     bb_seen = sbitmap_resize (bb_seen, size * 2, 0);
79
80   SET_BIT (bb_seen, bb->index);
81 }
82
83 static inline bool
84 bb_seen_p (basic_block bb)
85 {
86   return TEST_BIT (bb_seen, bb->index);
87 }
88
89 /* Return true if we should ignore the basic block for purposes of tracing.  */
90 static bool
91 ignore_bb_p (const_basic_block bb)
92 {
93   gimple g;
94
95   if (bb->index < NUM_FIXED_BLOCKS)
96     return true;
97   if (optimize_bb_for_size_p (bb))
98     return true;
99
100   /* A transaction is a single entry multiple exit region.  It must be
101      duplicated in its entirety or not at all.  */
102   g = last_stmt (CONST_CAST_BB (bb));
103   if (g && gimple_code (g) == GIMPLE_TRANSACTION)
104     return true;
105
106   return false;
107 }
108
109 /* Return number of instructions in the block.  */
110
111 static int
112 count_insns (basic_block bb)
113 {
114   gimple_stmt_iterator gsi;
115   gimple stmt;
116   int n = 0;
117
118   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
119     {
120       stmt = gsi_stmt (gsi);
121       n += estimate_num_insns (stmt, &eni_size_weights);
122     }
123   return n;
124 }
125
126 /* Return true if E1 is more frequent than E2.  */
127 static bool
128 better_p (const_edge e1, const_edge e2)
129 {
130   if (e1->count != e2->count)
131     return e1->count > e2->count;
132   if (e1->src->frequency * e1->probability !=
133       e2->src->frequency * e2->probability)
134     return (e1->src->frequency * e1->probability
135             > e2->src->frequency * e2->probability);
136   /* This is needed to avoid changes in the decision after
137      CFG is modified.  */
138   if (e1->src != e2->src)
139     return e1->src->index > e2->src->index;
140   return e1->dest->index > e2->dest->index;
141 }
142
143 /* Return most frequent successor of basic block BB.  */
144
145 static edge
146 find_best_successor (basic_block bb)
147 {
148   edge e;
149   edge best = NULL;
150   edge_iterator ei;
151
152   FOR_EACH_EDGE (e, ei, bb->succs)
153     if (!best || better_p (e, best))
154       best = e;
155   if (!best || ignore_bb_p (best->dest))
156     return NULL;
157   if (best->probability <= probability_cutoff)
158     return NULL;
159   return best;
160 }
161
162 /* Return most frequent predecessor of basic block BB.  */
163
164 static edge
165 find_best_predecessor (basic_block bb)
166 {
167   edge e;
168   edge best = NULL;
169   edge_iterator ei;
170
171   FOR_EACH_EDGE (e, ei, bb->preds)
172     if (!best || better_p (e, best))
173       best = e;
174   if (!best || ignore_bb_p (best->src))
175     return NULL;
176   if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
177       < bb->frequency * branch_ratio_cutoff)
178     return NULL;
179   return best;
180 }
181
182 /* Find the trace using bb and record it in the TRACE array.
183    Return number of basic blocks recorded.  */
184
185 static int
186 find_trace (basic_block bb, basic_block *trace)
187 {
188   int i = 0;
189   edge e;
190
191   if (dump_file)
192     fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
193
194   while ((e = find_best_predecessor (bb)) != NULL)
195     {
196       basic_block bb2 = e->src;
197       if (bb_seen_p (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
198           || find_best_successor (bb2) != e)
199         break;
200       if (dump_file)
201         fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
202       bb = bb2;
203     }
204   if (dump_file)
205     fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
206   trace[i++] = bb;
207
208   /* Follow the trace in forward direction.  */
209   while ((e = find_best_successor (bb)) != NULL)
210     {
211       bb = e->dest;
212       if (bb_seen_p (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
213           || find_best_predecessor (bb) != e)
214         break;
215       if (dump_file)
216         fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
217       trace[i++] = bb;
218     }
219   if (dump_file)
220     fprintf (dump_file, "\n");
221   return i;
222 }
223
224 /* Look for basic blocks in frequency order, construct traces and tail duplicate
225    if profitable.  */
226
227 static bool
228 tail_duplicate (void)
229 {
230   fibnode_t *blocks = XCNEWVEC (fibnode_t, last_basic_block);
231   basic_block *trace = XNEWVEC (basic_block, n_basic_blocks);
232   int *counts = XNEWVEC (int, last_basic_block);
233   int ninsns = 0, nduplicated = 0;
234   gcov_type weighted_insns = 0, traced_insns = 0;
235   fibheap_t heap = fibheap_new ();
236   gcov_type cover_insns;
237   int max_dup_insns;
238   basic_block bb;
239   bool changed = false;
240
241   /* Create an oversized sbitmap to reduce the chance that we need to
242      resize it.  */
243   bb_seen = sbitmap_alloc (last_basic_block * 2);
244   sbitmap_zero (bb_seen);
245   initialize_original_copy_tables ();
246
247   if (profile_info && flag_branch_probabilities)
248     probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
249   else
250     probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
251   probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;
252
253   branch_ratio_cutoff =
254     (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));
255
256   FOR_EACH_BB (bb)
257     {
258       int n = count_insns (bb);
259       if (!ignore_bb_p (bb))
260         blocks[bb->index] = fibheap_insert (heap, -bb->frequency,
261                                             bb);
262
263       counts [bb->index] = n;
264       ninsns += n;
265       weighted_insns += n * bb->frequency;
266     }
267
268   if (profile_info && flag_branch_probabilities)
269     cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
270   else
271     cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
272   cover_insns = (weighted_insns * cover_insns + 50) / 100;
273   max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;
274
275   while (traced_insns < cover_insns && nduplicated < max_dup_insns
276          && !fibheap_empty (heap))
277     {
278       basic_block bb = (basic_block) fibheap_extract_min (heap);
279       int n, pos;
280
281       if (!bb)
282         break;
283
284       blocks[bb->index] = NULL;
285
286       if (ignore_bb_p (bb))
287         continue;
288       gcc_assert (!bb_seen_p (bb));
289
290       n = find_trace (bb, trace);
291
292       bb = trace[0];
293       traced_insns += bb->frequency * counts [bb->index];
294       if (blocks[bb->index])
295         {
296           fibheap_delete_node (heap, blocks[bb->index]);
297           blocks[bb->index] = NULL;
298         }
299
300       for (pos = 1; pos < n; pos++)
301         {
302           basic_block bb2 = trace[pos];
303
304           if (blocks[bb2->index])
305             {
306               fibheap_delete_node (heap, blocks[bb2->index]);
307               blocks[bb2->index] = NULL;
308             }
309           traced_insns += bb2->frequency * counts [bb2->index];
310           if (EDGE_COUNT (bb2->preds) > 1
311               && can_duplicate_block_p (bb2)
312               /* We have the tendency to duplicate the loop header
313                  of all do { } while loops.  Do not do that - it is
314                  not profitable and it might create a loop with multiple
315                  entries or at least rotate the loop.  */
316               && (!current_loops
317                   || bb2->loop_father->header != bb2))
318             {
319               edge e;
320               basic_block copy;
321
322               nduplicated += counts [bb2->index];
323
324               e = find_edge (bb, bb2);
325
326               copy = duplicate_block (bb2, e, bb);
327               flush_pending_stmts (e);
328
329               add_phi_args_after_copy (&copy, 1, NULL);
330
331               /* Reconsider the original copy of block we've duplicated.
332                  Removing the most common predecessor may make it to be
333                  head.  */
334               blocks[bb2->index] =
335                 fibheap_insert (heap, -bb2->frequency, bb2);
336
337               if (dump_file)
338                 fprintf (dump_file, "Duplicated %i as %i [%i]\n",
339                          bb2->index, copy->index, copy->frequency);
340
341               bb2 = copy;
342               changed = true;
343             }
344           mark_bb_seen (bb2);
345           bb = bb2;
346           /* In case the trace became infrequent, stop duplicating.  */
347           if (ignore_bb_p (bb))
348             break;
349         }
350       if (dump_file)
351         fprintf (dump_file, " covered now %.1f\n\n",
352                  traced_insns * 100.0 / weighted_insns);
353     }
354   if (dump_file)
355     fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
356              nduplicated * 100 / ninsns);
357
358   free_original_copy_tables ();
359   sbitmap_free (bb_seen);
360   free (blocks);
361   free (trace);
362   free (counts);
363   fibheap_delete (heap);
364
365   return changed;
366 }
367
368 /* Main entry point to this file.  */
369
370 static unsigned int
371 tracer (void)
372 {
373   bool changed;
374
375   gcc_assert (current_ir_type () == IR_GIMPLE);
376
377   if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1)
378     return 0;
379
380   mark_dfs_back_edges ();
381   if (dump_file)
382     dump_flow_info (dump_file, dump_flags);
383
384   /* Trace formation is done on the fly inside tail_duplicate */
385   changed = tail_duplicate ();
386   if (changed)
387     free_dominance_info (CDI_DOMINATORS);
388
389   if (dump_file)
390     dump_flow_info (dump_file, dump_flags);
391
392   return changed ? TODO_cleanup_cfg : 0;
393 }
394 \f
395 static bool
396 gate_tracer (void)
397 {
398   return (optimize > 0 && flag_tracer && flag_reorder_blocks);
399 }
400
401 struct gimple_opt_pass pass_tracer =
402 {
403  {
404   GIMPLE_PASS,
405   "tracer",                             /* name */
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 */
418  }
419 };