tracer.c 11.1 KB
Newer Older
1 2
/* The tracer pass for the GNU compiler.
   Contributed by Jan Hubicka, SuSE Labs.
3
   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING.  If not, write to the Free
Kelley Cook committed
19 20
   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.  */
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

/* This pass performs the tail duplication needed for superblock formation.
   For more information see:

     Design and Analysis of Profile-Based Optimization in Compaq's
     Compilation Tools for Alpha; Journal of Instruction-Level
     Parallelism 3 (2000) 1-25

   Unlike Compaq's implementation we don't do the loop peeling as most
   probably a better job can be done by a special pass and we don't
   need to worry too much about the code size implications as the tail
   duplicates are crossjumped again if optimizations are not
   performed.  */


#include "config.h"
#include "system.h"
38 39
#include "coretypes.h"
#include "tm.h"
40 41 42 43 44 45 46 47
#include "tree.h"
#include "rtl.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "output.h"
#include "cfglayout.h"
#include "fibheap.h"
#include "flags.h"
48
#include "timevar.h"
49
#include "params.h"
50
#include "coverage.h"
51
#include "tree-pass.h"
52

53 54 55 56 57 58 59 60
static int count_insns (basic_block);
static bool ignore_bb_p (basic_block);
static bool better_p (edge, edge);
static edge find_best_successor (basic_block);
static edge find_best_predecessor (basic_block);
static int find_trace (basic_block, basic_block *);
static void tail_duplicate (void);
static void layout_superblocks (void);
61 62 63 64 65 66 67 68

/* Minimal outgoing edge probability considered for superblock formation.  */
static int probability_cutoff;
static int branch_ratio_cutoff;

/* Return true if BB has been seen - it is connected to some trace
   already.  */

69
#define seen(bb) (bb->il.rtl->visited || bb->aux)
70

71
/* Return true if we should ignore the basic block for purposes of tracing.  */
72
static bool
73
ignore_bb_p (basic_block bb)
74 75 76 77 78 79 80 81 82 83 84
{
  if (bb->index < 0)
    return true;
  if (!maybe_hot_bb_p (bb))
    return true;
  return false;
}

/* Return number of instructions in the block.  */

static int
85
count_insns (basic_block bb)
86 87 88 89
{
  rtx insn;
  int n = 0;

90 91 92
  for (insn = BB_HEAD (bb);
       insn != NEXT_INSN (BB_END (bb));
       insn = NEXT_INSN (insn))
93 94 95 96 97 98 99
    if (active_insn_p (insn))
      n++;
  return n;
}

/* Return true if E1 is more frequent than E2.  */
static bool
100
better_p (edge e1, edge e2)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
{
  if (e1->count != e2->count)
    return e1->count > e2->count;
  if (e1->src->frequency * e1->probability !=
      e2->src->frequency * e2->probability)
    return (e1->src->frequency * e1->probability
	    > e2->src->frequency * e2->probability);
  /* This is needed to avoid changes in the decision after
     CFG is modified.  */
  if (e1->src != e2->src)
    return e1->src->index > e2->src->index;
  return e1->dest->index > e2->dest->index;
}

/* Return most frequent successor of basic block BB.  */

static edge
118
find_best_successor (basic_block bb)
119 120 121
{
  edge e;
  edge best = NULL;
122
  edge_iterator ei;
123

124
  FOR_EACH_EDGE (e, ei, bb->succs)
125 126 127 128 129 130 131 132 133 134 135 136
    if (!best || better_p (e, best))
      best = e;
  if (!best || ignore_bb_p (best->dest))
    return NULL;
  if (best->probability <= probability_cutoff)
    return NULL;
  return best;
}

/* Return most frequent predecessor of basic block BB.  */

static edge
137
find_best_predecessor (basic_block bb)
138 139 140
{
  edge e;
  edge best = NULL;
141
  edge_iterator ei;
142

143
  FOR_EACH_EDGE (e, ei, bb->preds)
144 145 146 147 148 149 150 151 152 153 154 155 156 157
    if (!best || better_p (e, best))
      best = e;
  if (!best || ignore_bb_p (best->src))
    return NULL;
  if (EDGE_FREQUENCY (best) * REG_BR_PROB_BASE
      < bb->frequency * branch_ratio_cutoff)
    return NULL;
  return best;
}

/* Find the trace using bb and record it in the TRACE array.
   Return number of basic blocks recorded.  */

static int
158
find_trace (basic_block bb, basic_block *trace)
159 160 161 162
{
  int i = 0;
  edge e;

163 164
  if (dump_file)
    fprintf (dump_file, "Trace seed %i [%i]", bb->index, bb->frequency);
165 166 167 168 169 170 171

  while ((e = find_best_predecessor (bb)) != NULL)
    {
      basic_block bb2 = e->src;
      if (seen (bb2) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
	  || find_best_successor (bb2) != e)
	break;
172 173
      if (dump_file)
	fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
174 175
      bb = bb2;
    }
176 177
  if (dump_file)
    fprintf (dump_file, " forward %i [%i]", bb->index, bb->frequency);
178 179 180 181 182 183 184 185 186
  trace[i++] = bb;

  /* Follow the trace in forward direction.  */
  while ((e = find_best_successor (bb)) != NULL)
    {
      bb = e->dest;
      if (seen (bb) || (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
	  || find_best_predecessor (bb) != e)
	break;
187 188
      if (dump_file)
	fprintf (dump_file, ",%i [%i]", bb->index, bb->frequency);
189 190
      trace[i++] = bb;
    }
191 192
  if (dump_file)
    fprintf (dump_file, "\n");
193 194 195 196 197 198 199
  return i;
}

/* Look for basic blocks in frequency order, construct traces and tail duplicate
   if profitable.  */

static void
200
tail_duplicate (void)
201 202 203 204 205 206 207 208 209 210 211
{
  fibnode_t *blocks = xcalloc (last_basic_block, sizeof (fibnode_t));
  basic_block *trace = xmalloc (sizeof (basic_block) * n_basic_blocks);
  int *counts = xmalloc (sizeof (int) * last_basic_block);
  int ninsns = 0, nduplicated = 0;
  gcov_type weighted_insns = 0, traced_insns = 0;
  fibheap_t heap = fibheap_new ();
  gcov_type cover_insns;
  int max_dup_insns;
  basic_block bb;

212
  if (profile_info && flag_branch_probabilities)
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK);
  else
    probability_cutoff = PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY);
  probability_cutoff = REG_BR_PROB_BASE / 100 * probability_cutoff;

  branch_ratio_cutoff =
    (REG_BR_PROB_BASE / 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO));

  FOR_EACH_BB (bb)
    {
      int n = count_insns (bb);
      if (!ignore_bb_p (bb))
	blocks[bb->index] = fibheap_insert (heap, -bb->frequency,
					    bb);

      counts [bb->index] = n;
      ninsns += n;
      weighted_insns += n * bb->frequency;
    }

233
  if (profile_info && flag_branch_probabilities)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK);
  else
    cover_insns = PARAM_VALUE (TRACER_DYNAMIC_COVERAGE);
  cover_insns = (weighted_insns * cover_insns + 50) / 100;
  max_dup_insns = (ninsns * PARAM_VALUE (TRACER_MAX_CODE_GROWTH) + 50) / 100;

  while (traced_insns < cover_insns && nduplicated < max_dup_insns
         && !fibheap_empty (heap))
    {
      basic_block bb = fibheap_extract_min (heap);
      int n, pos;

      if (!bb)
	break;

      blocks[bb->index] = NULL;

      if (ignore_bb_p (bb))
	continue;
253
      gcc_assert (!seen (bb));
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

      n = find_trace (bb, trace);

      bb = trace[0];
      traced_insns += bb->frequency * counts [bb->index];
      if (blocks[bb->index])
	{
	  fibheap_delete_node (heap, blocks[bb->index]);
	  blocks[bb->index] = NULL;
	}

      for (pos = 1; pos < n; pos++)
	{
	  basic_block bb2 = trace[pos];

	  if (blocks[bb2->index])
	    {
	      fibheap_delete_node (heap, blocks[bb2->index]);
	      blocks[bb2->index] = NULL;
	    }
	  traced_insns += bb2->frequency * counts [bb2->index];
275
	  if (EDGE_COUNT (bb2->preds) > 1
276
	      && can_duplicate_block_p (bb2))
277
	    {
278
	      edge e;
279 280
	      basic_block old = bb2;

281
	      e = find_edge (bb, bb2);
282

283
	      nduplicated += counts [bb2->index];
284
	      bb2 = duplicate_block (bb2, e, bb);
285 286

	      /* Reconsider the original copy of block we've duplicated.
287
	         Removing the most common predecessor may make it to be
288 289 290 291
	         head.  */
	      blocks[old->index] =
		fibheap_insert (heap, -old->frequency, old);

292 293
	      if (dump_file)
		fprintf (dump_file, "Duplicated %i as %i [%i]\n",
294 295
			 old->index, bb2->index, bb2->frequency);
	    }
296 297
	  bb->aux = bb2;
	  bb2->il.rtl->visited = 1;
298 299 300 301 302
	  bb = bb2;
	  /* In case the trace became infrequent, stop duplicating.  */
	  if (ignore_bb_p (bb))
	    break;
	}
303 304
      if (dump_file)
	fprintf (dump_file, " covered now %.1f\n\n",
305 306
		 traced_insns * 100.0 / weighted_insns);
    }
307 308
  if (dump_file)
    fprintf (dump_file, "Duplicated %i insns (%i%%)\n", nduplicated,
309 310 311 312 313 314 315 316
	     nduplicated * 100 / ninsns);

  free (blocks);
  free (trace);
  free (counts);
  fibheap_delete (heap);
}

317
/* Connect the superblocks into linear sequence.  At the moment we attempt to keep
318 319 320 321 322
   the original order as much as possible, but the algorithm may be made smarter
   later if needed.  BB reordering pass should void most of the benefits of such
   change though.  */

static void
323
layout_superblocks (void)
324
{
325 326
  basic_block end = single_succ (ENTRY_BLOCK_PTR);
  basic_block bb = end->next_bb;
327 328 329

  while (bb != EXIT_BLOCK_PTR)
    {
330
      edge_iterator ei;
331
      edge e, best = NULL;
332 333
      while (end->aux)
	end = end->aux;
334

335
      FOR_EACH_EDGE (e, ei, end->succs)
336
	if (e->dest != EXIT_BLOCK_PTR
337
	    && e->dest != single_succ (ENTRY_BLOCK_PTR)
338
	    && !e->dest->il.rtl->visited
339 340 341 342 343
	    && (!best || EDGE_FREQUENCY (e) > EDGE_FREQUENCY (best)))
	  best = e;

      if (best)
	{
344 345
	  end->aux = best->dest;
	  best->dest->il.rtl->visited = 1;
346 347
	}
      else
348
	for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
349
	  {
350
	    if (!bb->il.rtl->visited)
351
	      {
352 353
		end->aux = bb;
		bb->il.rtl->visited = 1;
354 355 356 357 358 359
		break;
	      }
	  }
    }
}

360 361
/* Main entry point to this file.  FLAGS is the set of flags to pass
   to cfg_layout_initialize().  */
362 363

void
364
tracer (unsigned int flags)
365 366 367
{
  if (n_basic_blocks <= 1)
    return;
368

369
  cfg_layout_initialize (flags);
370
  mark_dfs_back_edges ();
371 372
  if (dump_file)
    dump_flow_info (dump_file);
373 374
  tail_duplicate ();
  layout_superblocks ();
375 376
  if (dump_file)
    dump_flow_info (dump_file);
377
  cfg_layout_finalize ();
378

379 380
  /* Merge basic blocks in duplicated traces.  */
  cleanup_cfg (CLEANUP_EXPENSIVE);
381 382 383 384 385 386 387
}

static bool
gate_handle_tracer (void)
{
  return (optimize > 0 && flag_tracer);
}
388

389 390 391 392 393 394 395 396 397
/* Run tracer.  */
static void
rest_of_handle_tracer (void)
{
  if (dump_file)
    dump_flow_info (dump_file);
  tracer (0);
  cleanup_cfg (CLEANUP_EXPENSIVE);
  reg_scan (get_insns (), max_reg_num ());
398
}
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

struct tree_opt_pass pass_tracer =
{
  "tracer",                             /* name */
  gate_handle_tracer,                   /* gate */
  rest_of_handle_tracer,                /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_TRACER,                            /* tv_id */
  0,                                    /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
  TODO_dump_func,                       /* todo_flags_finish */
  'T'                                   /* letter */
};