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