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