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.
6 This file is part of GCC.
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)
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.
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/>. */
22 /* This pass performs the tail duplication needed for superblock formation.
23 For more information see:
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
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
38 #include "coretypes.h"
42 #include "hard-reg-set.h"
43 #include "basic-block.h"
48 #include "tree-pass.h"
49 #include "tree-flow.h"
50 #include "tree-inline.h"
53 static int count_insns (basic_block
);
54 static bool ignore_bb_p (const_basic_block
);
55 static bool better_p (const_edge
, const_edge
);
56 static edge
find_best_successor (basic_block
);
57 static edge
find_best_predecessor (basic_block
);
58 static int find_trace (basic_block
, basic_block
*);
60 /* Minimal outgoing edge probability considered for superblock formation. */
61 static int probability_cutoff
;
62 static int branch_ratio_cutoff
;
64 /* A bit BB->index is set if BB has already been seen, i.e. it is
65 connected to some trace already. */
69 mark_bb_seen (basic_block bb
)
71 unsigned int size
= SBITMAP_SIZE (bb_seen
);
73 if ((unsigned int)bb
->index
>= size
)
74 bb_seen
= sbitmap_resize (bb_seen
, size
* 2, 0);
76 bitmap_set_bit (bb_seen
, bb
->index
);
80 bb_seen_p (basic_block bb
)
82 return bitmap_bit_p (bb_seen
, bb
->index
);
85 /* Return true if we should ignore the basic block for purposes of tracing. */
87 ignore_bb_p (const_basic_block bb
)
91 if (bb
->index
< NUM_FIXED_BLOCKS
)
93 if (optimize_bb_for_size_p (bb
))
96 /* A transaction is a single entry multiple exit region. It must be
97 duplicated in its entirety or not at all. */
98 g
= last_stmt (CONST_CAST_BB (bb
));
99 if (g
&& gimple_code (g
) == GIMPLE_TRANSACTION
)
105 /* Return number of instructions in the block. */
108 count_insns (basic_block bb
)
110 gimple_stmt_iterator gsi
;
114 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
116 stmt
= gsi_stmt (gsi
);
117 n
+= estimate_num_insns (stmt
, &eni_size_weights
);
122 /* Return true if E1 is more frequent than E2. */
124 better_p (const_edge e1
, const_edge e2
)
126 if (e1
->count
!= e2
->count
)
127 return e1
->count
> e2
->count
;
128 if (e1
->src
->frequency
* e1
->probability
!=
129 e2
->src
->frequency
* e2
->probability
)
130 return (e1
->src
->frequency
* e1
->probability
131 > e2
->src
->frequency
* e2
->probability
);
132 /* This is needed to avoid changes in the decision after
134 if (e1
->src
!= e2
->src
)
135 return e1
->src
->index
> e2
->src
->index
;
136 return e1
->dest
->index
> e2
->dest
->index
;
139 /* Return most frequent successor of basic block BB. */
142 find_best_successor (basic_block bb
)
148 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
149 if (!best
|| better_p (e
, best
))
151 if (!best
|| ignore_bb_p (best
->dest
))
153 if (best
->probability
<= probability_cutoff
)
158 /* Return most frequent predecessor of basic block BB. */
161 find_best_predecessor (basic_block bb
)
167 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
168 if (!best
|| better_p (e
, best
))
170 if (!best
|| ignore_bb_p (best
->src
))
172 if (EDGE_FREQUENCY (best
) * REG_BR_PROB_BASE
173 < bb
->frequency
* branch_ratio_cutoff
)
178 /* Find the trace using bb and record it in the TRACE array.
179 Return number of basic blocks recorded. */
182 find_trace (basic_block bb
, basic_block
*trace
)
188 fprintf (dump_file
, "Trace seed %i [%i]", bb
->index
, bb
->frequency
);
190 while ((e
= find_best_predecessor (bb
)) != NULL
)
192 basic_block bb2
= e
->src
;
193 if (bb_seen_p (bb2
) || (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
194 || find_best_successor (bb2
) != e
)
197 fprintf (dump_file
, ",%i [%i]", bb
->index
, bb
->frequency
);
201 fprintf (dump_file
, " forward %i [%i]", bb
->index
, bb
->frequency
);
204 /* Follow the trace in forward direction. */
205 while ((e
= find_best_successor (bb
)) != NULL
)
208 if (bb_seen_p (bb
) || (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
209 || find_best_predecessor (bb
) != e
)
212 fprintf (dump_file
, ",%i [%i]", bb
->index
, bb
->frequency
);
216 fprintf (dump_file
, "\n");
220 /* Look for basic blocks in frequency order, construct traces and tail duplicate
224 tail_duplicate (void)
226 fibnode_t
*blocks
= XCNEWVEC (fibnode_t
, last_basic_block
);
227 basic_block
*trace
= XNEWVEC (basic_block
, n_basic_blocks
);
228 int *counts
= XNEWVEC (int, last_basic_block
);
229 int ninsns
= 0, nduplicated
= 0;
230 gcov_type weighted_insns
= 0, traced_insns
= 0;
231 fibheap_t heap
= fibheap_new ();
232 gcov_type cover_insns
;
235 bool changed
= false;
237 /* Create an oversized sbitmap to reduce the chance that we need to
239 bb_seen
= sbitmap_alloc (last_basic_block
* 2);
240 bitmap_clear (bb_seen
);
241 initialize_original_copy_tables ();
243 if (profile_info
&& flag_branch_probabilities
)
244 probability_cutoff
= PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY_FEEDBACK
);
246 probability_cutoff
= PARAM_VALUE (TRACER_MIN_BRANCH_PROBABILITY
);
247 probability_cutoff
= REG_BR_PROB_BASE
/ 100 * probability_cutoff
;
249 branch_ratio_cutoff
=
250 (REG_BR_PROB_BASE
/ 100 * PARAM_VALUE (TRACER_MIN_BRANCH_RATIO
));
254 int n
= count_insns (bb
);
255 if (!ignore_bb_p (bb
))
256 blocks
[bb
->index
] = fibheap_insert (heap
, -bb
->frequency
,
259 counts
[bb
->index
] = n
;
261 weighted_insns
+= n
* bb
->frequency
;
264 if (profile_info
&& flag_branch_probabilities
)
265 cover_insns
= PARAM_VALUE (TRACER_DYNAMIC_COVERAGE_FEEDBACK
);
267 cover_insns
= PARAM_VALUE (TRACER_DYNAMIC_COVERAGE
);
268 cover_insns
= (weighted_insns
* cover_insns
+ 50) / 100;
269 max_dup_insns
= (ninsns
* PARAM_VALUE (TRACER_MAX_CODE_GROWTH
) + 50) / 100;
271 while (traced_insns
< cover_insns
&& nduplicated
< max_dup_insns
272 && !fibheap_empty (heap
))
274 basic_block bb
= (basic_block
) fibheap_extract_min (heap
);
280 blocks
[bb
->index
] = NULL
;
282 if (ignore_bb_p (bb
))
284 gcc_assert (!bb_seen_p (bb
));
286 n
= find_trace (bb
, trace
);
289 traced_insns
+= bb
->frequency
* counts
[bb
->index
];
290 if (blocks
[bb
->index
])
292 fibheap_delete_node (heap
, blocks
[bb
->index
]);
293 blocks
[bb
->index
] = NULL
;
296 for (pos
= 1; pos
< n
; pos
++)
298 basic_block bb2
= trace
[pos
];
300 if (blocks
[bb2
->index
])
302 fibheap_delete_node (heap
, blocks
[bb2
->index
]);
303 blocks
[bb2
->index
] = NULL
;
305 traced_insns
+= bb2
->frequency
* counts
[bb2
->index
];
306 if (EDGE_COUNT (bb2
->preds
) > 1
307 && can_duplicate_block_p (bb2
)
308 /* We have the tendency to duplicate the loop header
309 of all do { } while loops. Do not do that - it is
310 not profitable and it might create a loop with multiple
311 entries or at least rotate the loop. */
313 || bb2
->loop_father
->header
!= bb2
))
318 nduplicated
+= counts
[bb2
->index
];
320 e
= find_edge (bb
, bb2
);
322 copy
= duplicate_block (bb2
, e
, bb
);
323 flush_pending_stmts (e
);
325 add_phi_args_after_copy (©
, 1, NULL
);
327 /* Reconsider the original copy of block we've duplicated.
328 Removing the most common predecessor may make it to be
331 fibheap_insert (heap
, -bb2
->frequency
, bb2
);
334 fprintf (dump_file
, "Duplicated %i as %i [%i]\n",
335 bb2
->index
, copy
->index
, copy
->frequency
);
342 /* In case the trace became infrequent, stop duplicating. */
343 if (ignore_bb_p (bb
))
347 fprintf (dump_file
, " covered now %.1f\n\n",
348 traced_insns
* 100.0 / weighted_insns
);
351 fprintf (dump_file
, "Duplicated %i insns (%i%%)\n", nduplicated
,
352 nduplicated
* 100 / ninsns
);
354 free_original_copy_tables ();
355 sbitmap_free (bb_seen
);
359 fibheap_delete (heap
);
364 /* Main entry point to this file. */
371 if (n_basic_blocks
<= NUM_FIXED_BLOCKS
+ 1)
374 mark_dfs_back_edges ();
376 brief_dump_cfg (dump_file
, dump_flags
);
378 /* Trace formation is done on the fly inside tail_duplicate */
379 changed
= tail_duplicate ();
382 free_dominance_info (CDI_DOMINATORS
);
383 calculate_dominance_info (CDI_DOMINATORS
);
385 fix_loop_structure (NULL
);
389 brief_dump_cfg (dump_file
, dump_flags
);
391 return changed
? TODO_cleanup_cfg
: 0;
397 return (optimize
> 0 && flag_tracer
&& flag_reorder_blocks
);
400 struct gimple_opt_pass pass_tracer
=
405 OPTGROUP_NONE
, /* optinfo_flags */
406 gate_tracer
, /* gate */
407 tracer
, /* execute */
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 */
417 | TODO_verify_ssa
/* todo_flags_finish */