1 /* Transformations based on profile information for values.
2 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "coretypes.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "value-prof.h"
32 #include "insn-config.h"
37 #include "tree-flow.h"
38 #include "tree-flow-inline.h"
39 #include "diagnostic.h"
44 #include "tree-pass.h"
47 static struct value_prof_hooks
*value_prof_hooks
;
49 /* In this file value profile based optimizations are placed. Currently the
50 following optimizations are implemented (for more detailed descriptions
51 see comments at value_profile_transformations):
53 1) Division/modulo specialization. Provided that we can determine that the
54 operands of the division have some special properties, we may use it to
55 produce more effective code.
56 2) Speculative prefetching. If we are able to determine that the difference
57 between addresses accessed by a memory reference is usually constant, we
58 may add the prefetch instructions.
59 FIXME: This transformation was removed together with RTL based value
62 Every such optimization should add its requirements for profiled values to
63 insn_values_to_profile function. This function is called from branch_prob
64 in profile.c and the requested values are instrumented by it in the first
65 compilation with -fprofile-arcs. The optimization may then read the
66 gathered data in the second compilation with -fbranch-probabilities.
68 The measured data is pointed to from the histograms
69 field of the statement annotation of the instrumented insns. It is
70 kept as a linked list of struct histogram_value_t's, which contain the
71 same information as above. */
74 static tree
tree_divmod_fixed_value (tree
, tree
, tree
, tree
,
75 tree
, int, gcov_type
, gcov_type
);
76 static tree
tree_mod_pow2 (tree
, tree
, tree
, tree
, int, gcov_type
, gcov_type
);
77 static tree
tree_mod_subtract (tree
, tree
, tree
, tree
, int, int, int,
78 gcov_type
, gcov_type
, gcov_type
);
79 static bool tree_divmod_fixed_value_transform (tree
);
80 static bool tree_mod_pow2_value_transform (tree
);
81 static bool tree_mod_subtract_transform (tree
);
83 /* The overall number of invocations of the counter should match execution count
84 of basic block. Report it as error rather than internal error as it might
85 mean that user has misused the profile somehow. */
87 check_counter (tree stmt
, const char * name
, gcov_type all
, gcov_type bb_count
)
92 locus
= (stmt
!= NULL
&& EXPR_HAS_LOCATION (stmt
)
94 : &DECL_SOURCE_LOCATION (current_function_decl
));
95 error ("%HCorrupted value profile: %s profiler overall count (%d) does not match BB count (%d)",
96 locus
, name
, (int)all
, (int)bb_count
);
102 /* Tree based transformations. */
104 tree_value_profile_transformations (void)
107 block_stmt_iterator bsi
;
108 bool changed
= false;
112 /* Ignore cold areas -- we are enlarging the code. */
113 if (!maybe_hot_bb_p (bb
))
116 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
118 tree stmt
= bsi_stmt (bsi
);
119 stmt_ann_t ann
= get_stmt_ann (stmt
);
120 histogram_value th
= ann
->histograms
;
126 fprintf (dump_file
, "Trying transformations on insn ");
127 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
130 /* Transformations: */
131 /* The order of things in this conditional controls which
132 transformation is used when more than one is applicable. */
133 /* It is expected that any code added by the transformations
134 will be added before the current statement, and that the
135 current statement remain valid (although possibly
136 modified) upon return. */
137 if (flag_value_profile_transformations
138 && (tree_mod_subtract_transform (stmt
)
139 || tree_divmod_fixed_value_transform (stmt
)
140 || tree_mod_pow2_value_transform (stmt
)))
143 /* Original statement may no longer be in the same block. */
144 if (bb
!= bb_for_stmt (stmt
))
146 bb
= bb_for_stmt (stmt
);
147 bsi
= bsi_for_stmt (stmt
);
151 /* Free extra storage from compute_value_histograms. */
154 free (th
->hvalue
.counters
);
155 th
= th
->hvalue
.next
;
169 /* Generate code for transformation 1 (with OPERATION, operands OP1
170 and OP2, whose value is expected to be VALUE, parent modify-expr STMT and
171 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
172 within roundoff error). This generates the result into a temp and returns
173 the temp; it does not replace or alter the original STMT. */
175 tree_divmod_fixed_value (tree stmt
, tree operation
,
176 tree op1
, tree op2
, tree value
, int prob
, gcov_type count
,
179 tree stmt1
, stmt2
, stmt3
;
180 tree tmp1
, tmp2
, tmpv
;
181 tree label_decl1
= create_artificial_label ();
182 tree label_decl2
= create_artificial_label ();
183 tree label_decl3
= create_artificial_label ();
184 tree label1
, label2
, label3
;
185 tree bb1end
, bb2end
, bb3end
;
186 basic_block bb
, bb2
, bb3
, bb4
;
187 tree optype
= TREE_TYPE (operation
);
188 edge e12
, e13
, e23
, e24
, e34
;
189 block_stmt_iterator bsi
;
191 bb
= bb_for_stmt (stmt
);
192 bsi
= bsi_for_stmt (stmt
);
194 tmpv
= create_tmp_var (optype
, "PROF");
195 tmp1
= create_tmp_var (optype
, "PROF");
196 stmt1
= build2 (MODIFY_EXPR
, optype
, tmpv
, fold_convert (optype
, value
));
197 stmt2
= build2 (MODIFY_EXPR
, optype
, tmp1
, op2
);
198 stmt3
= build3 (COND_EXPR
, void_type_node
,
199 build2 (NE_EXPR
, boolean_type_node
, tmp1
, tmpv
),
200 build1 (GOTO_EXPR
, void_type_node
, label_decl2
),
201 build1 (GOTO_EXPR
, void_type_node
, label_decl1
));
202 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
203 bsi_insert_before (&bsi
, stmt2
, BSI_SAME_STMT
);
204 bsi_insert_before (&bsi
, stmt3
, BSI_SAME_STMT
);
207 tmp2
= create_tmp_var (optype
, "PROF");
208 label1
= build1 (LABEL_EXPR
, void_type_node
, label_decl1
);
209 stmt1
= build2 (MODIFY_EXPR
, optype
, tmp2
,
210 build2 (TREE_CODE (operation
), optype
, op1
, tmpv
));
211 bsi_insert_before (&bsi
, label1
, BSI_SAME_STMT
);
212 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
215 label2
= build1 (LABEL_EXPR
, void_type_node
, label_decl2
);
216 stmt1
= build2 (MODIFY_EXPR
, optype
, tmp2
,
217 build2 (TREE_CODE (operation
), optype
, op1
, op2
));
218 bsi_insert_before (&bsi
, label2
, BSI_SAME_STMT
);
219 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
222 label3
= build1 (LABEL_EXPR
, void_type_node
, label_decl3
);
223 bsi_insert_before (&bsi
, label3
, BSI_SAME_STMT
);
226 /* Edge e23 connects bb2 to bb3, etc. */
227 e12
= split_block (bb
, bb1end
);
230 e23
= split_block (bb2
, bb2end
);
232 bb3
->count
= all
- count
;
233 e34
= split_block (bb3
, bb3end
);
237 e12
->flags
&= ~EDGE_FALLTHRU
;
238 e12
->flags
|= EDGE_FALSE_VALUE
;
239 e12
->probability
= prob
;
242 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
243 e13
->probability
= REG_BR_PROB_BASE
- prob
;
244 e13
->count
= all
- count
;
248 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
249 e24
->probability
= REG_BR_PROB_BASE
;
252 e34
->probability
= REG_BR_PROB_BASE
;
253 e34
->count
= all
- count
;
258 /* Do transform 1) on INSN if applicable. */
260 tree_divmod_fixed_value_transform (tree stmt
)
262 stmt_ann_t ann
= get_stmt_ann (stmt
);
263 histogram_value histogram
;
265 gcov_type val
, count
, all
;
266 tree modify
, op
, op1
, op2
, result
, value
, tree_val
;
270 if (TREE_CODE (stmt
) == RETURN_EXPR
271 && TREE_OPERAND (stmt
, 0)
272 && TREE_CODE (TREE_OPERAND (stmt
, 0)) == MODIFY_EXPR
)
273 modify
= TREE_OPERAND (stmt
, 0);
274 if (TREE_CODE (modify
) != MODIFY_EXPR
)
276 op
= TREE_OPERAND (modify
, 1);
277 if (!INTEGRAL_TYPE_P (TREE_TYPE (op
)))
279 code
= TREE_CODE (op
);
281 if (code
!= TRUNC_DIV_EXPR
&& code
!= TRUNC_MOD_EXPR
)
284 op1
= TREE_OPERAND (op
, 0);
285 op2
= TREE_OPERAND (op
, 1);
286 if (!ann
->histograms
)
289 for (histogram
= ann
->histograms
; histogram
; histogram
= histogram
->hvalue
.next
)
290 if (histogram
->type
== HIST_TYPE_SINGLE_VALUE
)
296 value
= histogram
->hvalue
.value
;
297 val
= histogram
->hvalue
.counters
[0];
298 count
= histogram
->hvalue
.counters
[1];
299 all
= histogram
->hvalue
.counters
[2];
301 /* We require that count is at least half of all; this means
302 that for the transformation to fire the value must be constant
303 at least 50% of time (and 75% gives the guarantee of usage). */
304 if (simple_cst_equal (op2
, value
) != 1 || 2 * count
< all
)
307 if (check_counter (stmt
, "value", all
, bb_for_stmt (stmt
)->count
))
310 /* Compute probability of taking the optimal path. */
311 prob
= (count
* REG_BR_PROB_BASE
+ all
/ 2) / all
;
313 tree_val
= build_int_cst_wide (get_gcov_type (),
314 (unsigned HOST_WIDE_INT
) val
,
315 val
>> (HOST_BITS_PER_WIDE_INT
- 1) >> 1);
316 result
= tree_divmod_fixed_value (stmt
, op
, op1
, op2
, tree_val
, prob
, count
, all
);
320 fprintf (dump_file
, "Div/mod by constant ");
321 print_generic_expr (dump_file
, value
, TDF_SLIM
);
322 fprintf (dump_file
, "=");
323 print_generic_expr (dump_file
, tree_val
, TDF_SLIM
);
324 fprintf (dump_file
, " transformation on insn ");
325 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
328 TREE_OPERAND (modify
, 1) = result
;
333 /* Generate code for transformation 2 (with OPERATION, operands OP1
334 and OP2, parent modify-expr STMT and probability of taking the optimal
335 path PROB, which is equivalent to COUNT/ALL within roundoff error).
336 This generates the result into a temp and returns
337 the temp; it does not replace or alter the original STMT. */
339 tree_mod_pow2 (tree stmt
, tree operation
, tree op1
, tree op2
, int prob
,
340 gcov_type count
, gcov_type all
)
342 tree stmt1
, stmt2
, stmt3
, stmt4
;
344 tree label_decl1
= create_artificial_label ();
345 tree label_decl2
= create_artificial_label ();
346 tree label_decl3
= create_artificial_label ();
347 tree label1
, label2
, label3
;
348 tree bb1end
, bb2end
, bb3end
;
349 basic_block bb
, bb2
, bb3
, bb4
;
350 tree optype
= TREE_TYPE (operation
);
351 edge e12
, e13
, e23
, e24
, e34
;
352 block_stmt_iterator bsi
;
353 tree result
= create_tmp_var (optype
, "PROF");
355 bb
= bb_for_stmt (stmt
);
356 bsi
= bsi_for_stmt (stmt
);
358 tmp2
= create_tmp_var (optype
, "PROF");
359 tmp3
= create_tmp_var (optype
, "PROF");
360 stmt2
= build2 (MODIFY_EXPR
, optype
, tmp2
,
361 build2 (PLUS_EXPR
, optype
, op2
, build_int_cst (optype
, -1)));
362 stmt3
= build2 (MODIFY_EXPR
, optype
, tmp3
,
363 build2 (BIT_AND_EXPR
, optype
, tmp2
, op2
));
364 stmt4
= build3 (COND_EXPR
, void_type_node
,
365 build2 (NE_EXPR
, boolean_type_node
,
366 tmp3
, build_int_cst (optype
, 0)),
367 build1 (GOTO_EXPR
, void_type_node
, label_decl2
),
368 build1 (GOTO_EXPR
, void_type_node
, label_decl1
));
369 bsi_insert_before (&bsi
, stmt2
, BSI_SAME_STMT
);
370 bsi_insert_before (&bsi
, stmt3
, BSI_SAME_STMT
);
371 bsi_insert_before (&bsi
, stmt4
, BSI_SAME_STMT
);
374 /* tmp2 == op2-1 inherited from previous block */
375 label1
= build1 (LABEL_EXPR
, void_type_node
, label_decl1
);
376 stmt1
= build2 (MODIFY_EXPR
, optype
, result
,
377 build2 (BIT_AND_EXPR
, optype
, op1
, tmp2
));
378 bsi_insert_before (&bsi
, label1
, BSI_SAME_STMT
);
379 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
382 label2
= build1 (LABEL_EXPR
, void_type_node
, label_decl2
);
383 stmt1
= build2 (MODIFY_EXPR
, optype
, result
,
384 build2 (TREE_CODE (operation
), optype
, op1
, op2
));
385 bsi_insert_before (&bsi
, label2
, BSI_SAME_STMT
);
386 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
389 label3
= build1 (LABEL_EXPR
, void_type_node
, label_decl3
);
390 bsi_insert_before (&bsi
, label3
, BSI_SAME_STMT
);
393 /* Edge e23 connects bb2 to bb3, etc. */
394 e12
= split_block (bb
, bb1end
);
397 e23
= split_block (bb2
, bb2end
);
399 bb3
->count
= all
- count
;
400 e34
= split_block (bb3
, bb3end
);
404 e12
->flags
&= ~EDGE_FALLTHRU
;
405 e12
->flags
|= EDGE_FALSE_VALUE
;
406 e12
->probability
= prob
;
409 e13
= make_edge (bb
, bb3
, EDGE_TRUE_VALUE
);
410 e13
->probability
= REG_BR_PROB_BASE
- prob
;
411 e13
->count
= all
- count
;
415 e24
= make_edge (bb2
, bb4
, EDGE_FALLTHRU
);
416 e24
->probability
= REG_BR_PROB_BASE
;
419 e34
->probability
= REG_BR_PROB_BASE
;
420 e34
->count
= all
- count
;
425 /* Do transform 2) on INSN if applicable. */
427 tree_mod_pow2_value_transform (tree stmt
)
429 stmt_ann_t ann
= get_stmt_ann (stmt
);
430 histogram_value histogram
;
432 gcov_type count
, wrong_values
, all
;
433 tree modify
, op
, op1
, op2
, result
, value
;
437 if (TREE_CODE (stmt
) == RETURN_EXPR
438 && TREE_OPERAND (stmt
, 0)
439 && TREE_CODE (TREE_OPERAND (stmt
, 0)) == MODIFY_EXPR
)
440 modify
= TREE_OPERAND (stmt
, 0);
441 if (TREE_CODE (modify
) != MODIFY_EXPR
)
443 op
= TREE_OPERAND (modify
, 1);
444 if (!INTEGRAL_TYPE_P (TREE_TYPE (op
)))
446 code
= TREE_CODE (op
);
448 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (TREE_TYPE (op
)))
451 op1
= TREE_OPERAND (op
, 0);
452 op2
= TREE_OPERAND (op
, 1);
453 if (!ann
->histograms
)
456 for (histogram
= ann
->histograms
; histogram
; histogram
= histogram
->hvalue
.next
)
457 if (histogram
->type
== HIST_TYPE_POW2
)
463 value
= histogram
->hvalue
.value
;
464 wrong_values
= histogram
->hvalue
.counters
[0];
465 count
= histogram
->hvalue
.counters
[1];
467 /* We require that we hit a power of 2 at least half of all evaluations. */
468 if (simple_cst_equal (op2
, value
) != 1 || count
< wrong_values
)
473 fprintf (dump_file
, "Mod power of 2 transformation on insn ");
474 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
477 /* Compute probability of taking the optimal path. */
478 all
= count
+ wrong_values
;
479 if (check_counter (stmt
, "pow2", all
, bb_for_stmt (stmt
)->count
))
482 prob
= (count
* REG_BR_PROB_BASE
+ all
/ 2) / all
;
484 result
= tree_mod_pow2 (stmt
, op
, op1
, op2
, prob
, count
, all
);
486 TREE_OPERAND (modify
, 1) = result
;
491 /* Generate code for transformations 3 and 4 (with OPERATION, operands OP1
492 and OP2, parent modify-expr STMT, and NCOUNTS the number of cases to
493 support. Currently only NCOUNTS==0 or 1 is supported and this is
494 built into this interface. The probabilities of taking the optimal
495 paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
496 COUNT2/ALL respectively within roundoff error). This generates the
497 result into a temp and returns the temp; it does not replace or alter
498 the original STMT. */
499 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
502 tree_mod_subtract (tree stmt
, tree operation
, tree op1
, tree op2
,
503 int prob1
, int prob2
, int ncounts
,
504 gcov_type count1
, gcov_type count2
, gcov_type all
)
506 tree stmt1
, stmt2
, stmt3
;
508 tree label_decl1
= create_artificial_label ();
509 tree label_decl2
= create_artificial_label ();
510 tree label_decl3
= create_artificial_label ();
511 tree label1
, label2
, label3
;
512 tree bb1end
, bb2end
= NULL_TREE
, bb3end
;
513 basic_block bb
, bb2
, bb3
, bb4
;
514 tree optype
= TREE_TYPE (operation
);
515 edge e12
, e23
= 0, e24
, e34
, e14
;
516 block_stmt_iterator bsi
;
517 tree result
= create_tmp_var (optype
, "PROF");
519 bb
= bb_for_stmt (stmt
);
520 bsi
= bsi_for_stmt (stmt
);
522 tmp1
= create_tmp_var (optype
, "PROF");
523 stmt1
= build2 (MODIFY_EXPR
, optype
, result
, op1
);
524 stmt2
= build2 (MODIFY_EXPR
, optype
, tmp1
, op2
);
525 stmt3
= build3 (COND_EXPR
, void_type_node
,
526 build2 (LT_EXPR
, boolean_type_node
, result
, tmp1
),
527 build1 (GOTO_EXPR
, void_type_node
, label_decl3
),
528 build1 (GOTO_EXPR
, void_type_node
,
529 ncounts
? label_decl1
: label_decl2
));
530 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
531 bsi_insert_before (&bsi
, stmt2
, BSI_SAME_STMT
);
532 bsi_insert_before (&bsi
, stmt3
, BSI_SAME_STMT
);
535 if (ncounts
) /* Assumed to be 0 or 1 */
537 label1
= build1 (LABEL_EXPR
, void_type_node
, label_decl1
);
538 stmt1
= build2 (MODIFY_EXPR
, optype
, result
,
539 build2 (MINUS_EXPR
, optype
, result
, tmp1
));
540 stmt2
= build3 (COND_EXPR
, void_type_node
,
541 build2 (LT_EXPR
, boolean_type_node
, result
, tmp1
),
542 build1 (GOTO_EXPR
, void_type_node
, label_decl3
),
543 build1 (GOTO_EXPR
, void_type_node
, label_decl2
));
544 bsi_insert_before (&bsi
, label1
, BSI_SAME_STMT
);
545 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
546 bsi_insert_before (&bsi
, stmt2
, BSI_SAME_STMT
);
551 label2
= build1 (LABEL_EXPR
, void_type_node
, label_decl2
);
552 stmt1
= build2 (MODIFY_EXPR
, optype
, result
,
553 build2 (TREE_CODE (operation
), optype
, result
, tmp1
));
554 bsi_insert_before (&bsi
, label2
, BSI_SAME_STMT
);
555 bsi_insert_before (&bsi
, stmt1
, BSI_SAME_STMT
);
558 label3
= build1 (LABEL_EXPR
, void_type_node
, label_decl3
);
559 bsi_insert_before (&bsi
, label3
, BSI_SAME_STMT
);
562 /* Edge e23 connects bb2 to bb3, etc. */
563 /* However block 3 is optional; if it is not there, references
564 to 3 really refer to block 2. */
565 e12
= split_block (bb
, bb1end
);
567 bb2
->count
= all
- count1
;
569 if (ncounts
) /* Assumed to be 0 or 1. */
571 e23
= split_block (bb2
, bb2end
);
573 bb3
->count
= all
- count1
- count2
;
576 e34
= split_block (ncounts
? bb3
: bb2
, bb3end
);
580 e12
->flags
&= ~EDGE_FALLTHRU
;
581 e12
->flags
|= EDGE_FALSE_VALUE
;
582 e12
->probability
= REG_BR_PROB_BASE
- prob1
;
583 e12
->count
= all
- count1
;
585 e14
= make_edge (bb
, bb4
, EDGE_TRUE_VALUE
);
586 e14
->probability
= prob1
;
589 if (ncounts
) /* Assumed to be 0 or 1. */
591 e23
->flags
&= ~EDGE_FALLTHRU
;
592 e23
->flags
|= EDGE_FALSE_VALUE
;
593 e23
->count
= all
- count1
- count2
;
594 e23
->probability
= REG_BR_PROB_BASE
- prob2
;
596 e24
= make_edge (bb2
, bb4
, EDGE_TRUE_VALUE
);
597 e24
->probability
= prob2
;
601 e34
->probability
= REG_BR_PROB_BASE
;
602 e34
->count
= all
- count1
- count2
;
607 /* Do transforms 3) and 4) on INSN if applicable. */
609 tree_mod_subtract_transform (tree stmt
)
611 stmt_ann_t ann
= get_stmt_ann (stmt
);
612 histogram_value histogram
;
614 gcov_type count
, wrong_values
, all
;
615 tree modify
, op
, op1
, op2
, result
, value
;
620 if (TREE_CODE (stmt
) == RETURN_EXPR
621 && TREE_OPERAND (stmt
, 0)
622 && TREE_CODE (TREE_OPERAND (stmt
, 0)) == MODIFY_EXPR
)
623 modify
= TREE_OPERAND (stmt
, 0);
624 if (TREE_CODE (modify
) != MODIFY_EXPR
)
626 op
= TREE_OPERAND (modify
, 1);
627 if (!INTEGRAL_TYPE_P (TREE_TYPE (op
)))
629 code
= TREE_CODE (op
);
631 if (code
!= TRUNC_MOD_EXPR
|| !TYPE_UNSIGNED (TREE_TYPE (op
)))
634 op1
= TREE_OPERAND (op
, 0);
635 op2
= TREE_OPERAND (op
, 1);
636 if (!ann
->histograms
)
639 for (histogram
= ann
->histograms
; histogram
; histogram
= histogram
->hvalue
.next
)
640 if (histogram
->type
== HIST_TYPE_INTERVAL
)
646 value
= histogram
->hvalue
.value
;
649 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
650 all
+= histogram
->hvalue
.counters
[i
];
652 wrong_values
+= histogram
->hvalue
.counters
[i
];
653 wrong_values
+= histogram
->hvalue
.counters
[i
+1];
656 /* Compute probability of taking the optimal path. */
657 if (check_counter (stmt
, "interval", all
, bb_for_stmt (stmt
)->count
))
660 /* We require that we use just subtractions in at least 50% of all
663 for (i
= 0; i
< histogram
->hdata
.intvl
.steps
; i
++)
665 count
+= histogram
->hvalue
.counters
[i
];
666 if (count
* 2 >= all
)
669 if (i
== histogram
->hdata
.intvl
.steps
)
674 fprintf (dump_file
, "Mod subtract transformation on insn ");
675 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
678 /* Compute probability of taking the optimal path(s). */
679 prob1
= (histogram
->hvalue
.counters
[0] * REG_BR_PROB_BASE
+ all
/ 2) / all
;
680 prob2
= (histogram
->hvalue
.counters
[1] * REG_BR_PROB_BASE
+ all
/ 2) / all
;
682 /* In practice, "steps" is always 2. This interface reflects this,
683 and will need to be changed if "steps" can change. */
684 result
= tree_mod_subtract (stmt
, op
, op1
, op2
, prob1
, prob2
, i
,
685 histogram
->hvalue
.counters
[0],
686 histogram
->hvalue
.counters
[1], all
);
688 TREE_OPERAND (modify
, 1) = result
;
693 struct value_prof_hooks
{
694 /* Find list of values for which we want to measure histograms. */
695 void (*find_values_to_profile
) (histogram_values
*);
697 /* Identify and exploit properties of values that are hard to analyze
698 statically. See value-prof.c for more detail. */
699 bool (*value_profile_transformations
) (void);
702 /* Find values inside STMT for that we want to measure histograms for
703 division/modulo optimization. */
705 tree_divmod_values_to_profile (tree stmt
, histogram_values
*values
)
707 tree assign
, lhs
, rhs
, divisor
, op0
, type
;
708 histogram_value hist
;
710 if (TREE_CODE (stmt
) == RETURN_EXPR
)
711 assign
= TREE_OPERAND (stmt
, 0);
716 || TREE_CODE (assign
) != MODIFY_EXPR
)
718 lhs
= TREE_OPERAND (assign
, 0);
719 type
= TREE_TYPE (lhs
);
720 if (!INTEGRAL_TYPE_P (type
))
723 rhs
= TREE_OPERAND (assign
, 1);
724 switch (TREE_CODE (rhs
))
728 divisor
= TREE_OPERAND (rhs
, 1);
729 op0
= TREE_OPERAND (rhs
, 0);
731 VEC_reserve (histogram_value
, heap
, *values
, 3);
733 if (is_gimple_reg (divisor
))
735 /* Check for the case where the divisor is the same value most
737 hist
= ggc_alloc (sizeof (*hist
));
738 hist
->hvalue
.value
= divisor
;
739 hist
->hvalue
.stmt
= stmt
;
740 hist
->type
= HIST_TYPE_SINGLE_VALUE
;
741 VEC_quick_push (histogram_value
, *values
, hist
);
744 /* For mod, check whether it is not often a noop (or replaceable by
745 a few subtractions). */
746 if (TREE_CODE (rhs
) == TRUNC_MOD_EXPR
747 && TYPE_UNSIGNED (type
))
749 /* Check for a special case where the divisor is power of 2. */
750 hist
= ggc_alloc (sizeof (*hist
));
751 hist
->hvalue
.value
= divisor
;
752 hist
->hvalue
.stmt
= stmt
;
753 hist
->type
= HIST_TYPE_POW2
;
754 VEC_quick_push (histogram_value
, *values
, hist
);
756 hist
= ggc_alloc (sizeof (*hist
));
757 hist
->hvalue
.stmt
= stmt
;
759 = build2 (TRUNC_DIV_EXPR
, type
, op0
, divisor
);
760 hist
->type
= HIST_TYPE_INTERVAL
;
761 hist
->hdata
.intvl
.int_start
= 0;
762 hist
->hdata
.intvl
.steps
= 2;
763 VEC_quick_push (histogram_value
, *values
, hist
);
772 /* Find values inside STMT for that we want to measure histograms and adds
773 them to list VALUES. */
776 tree_values_to_profile (tree stmt
, histogram_values
*values
)
778 if (flag_value_profile_transformations
)
779 tree_divmod_values_to_profile (stmt
, values
);
783 tree_find_values_to_profile (histogram_values
*values
)
786 block_stmt_iterator bsi
;
788 histogram_value hist
;
792 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
793 tree_values_to_profile (bsi_stmt (bsi
), values
);
795 for (i
= 0; VEC_iterate (histogram_value
, *values
, i
, hist
); i
++)
799 case HIST_TYPE_INTERVAL
:
802 fprintf (dump_file
, "Interval counter for tree ");
803 print_generic_expr (dump_file
, hist
->hvalue
.stmt
,
805 fprintf (dump_file
, ", range %d -- %d.\n",
806 hist
->hdata
.intvl
.int_start
,
807 (hist
->hdata
.intvl
.int_start
808 + hist
->hdata
.intvl
.steps
- 1));
810 hist
->n_counters
= hist
->hdata
.intvl
.steps
+ 2;
816 fprintf (dump_file
, "Pow2 counter for tree ");
817 print_generic_expr (dump_file
, hist
->hvalue
.stmt
, TDF_SLIM
);
818 fprintf (dump_file
, ".\n");
820 hist
->n_counters
= 2;
823 case HIST_TYPE_SINGLE_VALUE
:
826 fprintf (dump_file
, "Single value counter for tree ");
827 print_generic_expr (dump_file
, hist
->hvalue
.stmt
, TDF_SLIM
);
828 fprintf (dump_file
, ".\n");
830 hist
->n_counters
= 3;
833 case HIST_TYPE_CONST_DELTA
:
836 fprintf (dump_file
, "Constant delta counter for tree ");
837 print_generic_expr (dump_file
, hist
->hvalue
.stmt
, TDF_SLIM
);
838 fprintf (dump_file
, ".\n");
840 hist
->n_counters
= 4;
849 static struct value_prof_hooks tree_value_prof_hooks
= {
850 tree_find_values_to_profile
,
851 tree_value_profile_transformations
855 tree_register_value_prof_hooks (void)
857 value_prof_hooks
= &tree_value_prof_hooks
;
858 gcc_assert (ir_type ());
861 /* IR-independent entry points. */
863 find_values_to_profile (histogram_values
*values
)
865 (value_prof_hooks
->find_values_to_profile
) (values
);
869 value_profile_transformations (void)
871 return (value_prof_hooks
->value_profile_transformations
) ();