params.def (PARAM_INDIR_CALL_TOPN_PROFILE): New param.
[gcc.git] / gcc / value-prof.c
1 /* Transformations based on profile information for values.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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 3, or (at your option) any later
9 version.
10
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
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "tree-nested.h"
26 #include "calls.h"
27 #include "rtl.h"
28 #include "expr.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "value-prof.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "optabs.h"
36 #include "regs.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "tree-eh.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimplify.h"
44 #include "gimple-iterator.h"
45 #include "gimple-ssa.h"
46 #include "tree-cfg.h"
47 #include "tree-phinodes.h"
48 #include "ssa-iterators.h"
49 #include "stringpool.h"
50 #include "tree-ssanames.h"
51 #include "diagnostic.h"
52 #include "gimple-pretty-print.h"
53 #include "coverage.h"
54 #include "tree.h"
55 #include "gcov-io.h"
56 #include "timevar.h"
57 #include "dumpfile.h"
58 #include "profile.h"
59 #include "data-streamer.h"
60 #include "builtins.h"
61 #include "tree-nested.h"
62 #include "hash-set.h"
63 #include "params.h"
64
65 /* In this file value profile based optimizations are placed. Currently the
66 following optimizations are implemented (for more detailed descriptions
67 see comments at value_profile_transformations):
68
69 1) Division/modulo specialization. Provided that we can determine that the
70 operands of the division have some special properties, we may use it to
71 produce more effective code.
72
73 2) Indirect/virtual call specialization. If we can determine most
74 common function callee in indirect/virtual call. We can use this
75 information to improve code effectiveness (especially info for
76 the inliner).
77
78 3) Speculative prefetching. If we are able to determine that the difference
79 between addresses accessed by a memory reference is usually constant, we
80 may add the prefetch instructions.
81 FIXME: This transformation was removed together with RTL based value
82 profiling.
83
84
85 Value profiling internals
86 ==========================
87
88 Every value profiling transformation starts with defining what values
89 to profile. There are different histogram types (see HIST_TYPE_* in
90 value-prof.h) and each transformation can request one or more histogram
91 types per GIMPLE statement. The function gimple_find_values_to_profile()
92 collects the values to profile in a vec, and adds the number of counters
93 required for the different histogram types.
94
95 For a -fprofile-generate run, the statements for which values should be
96 recorded, are instrumented in instrument_values(). The instrumentation
97 is done by helper functions that can be found in tree-profile.c, where
98 new types of histograms can be added if necessary.
99
100 After a -fprofile-use, the value profiling data is read back in by
101 compute_value_histograms() that translates the collected data to
102 histograms and attaches them to the profiled statements via
103 gimple_add_histogram_value(). Histograms are stored in a hash table
104 that is attached to every intrumented function, see VALUE_HISTOGRAMS
105 in function.h.
106
107 The value-profile transformations driver is the function
108 gimple_value_profile_transformations(). It traverses all statements in
109 the to-be-transformed function, and looks for statements with one or
110 more histograms attached to it. If a statement has histograms, the
111 transformation functions are called on the statement.
112
113 Limitations / FIXME / TODO:
114 * Only one histogram of each type can be associated with a statement.
115 * Currently, HIST_TYPE_CONST_DELTA is not implemented.
116 (This type of histogram was originally used to implement a form of
117 stride profiling based speculative prefetching to improve SPEC2000
118 scores for memory-bound benchmarks, mcf and equake. However, this
119 was an RTL value-profiling transformation, and those have all been
120 removed.)
121 * Some value profile transformations are done in builtins.c (?!)
122 * Updating of histograms needs some TLC.
123 * The value profiling code could be used to record analysis results
124 from non-profiling (e.g. VRP).
125 * Adding new profilers should be simplified, starting with a cleanup
126 of what-happens-where andwith making gimple_find_values_to_profile
127 and gimple_value_profile_transformations table-driven, perhaps...
128 */
129
130 static tree gimple_divmod_fixed_value (gimple, tree, int, gcov_type, gcov_type);
131 static tree gimple_mod_pow2 (gimple, int, gcov_type, gcov_type);
132 static tree gimple_mod_subtract (gimple, int, int, int, gcov_type, gcov_type,
133 gcov_type);
134 static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
135 static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
136 static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
137 static bool gimple_stringops_transform (gimple_stmt_iterator *);
138 static bool gimple_ic_transform (gimple_stmt_iterator *);
139
140 /* Allocate histogram value. */
141
142 static histogram_value
143 gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
144 enum hist_type type, gimple stmt, tree value)
145 {
146 histogram_value hist = (histogram_value) xcalloc (1, sizeof (*hist));
147 hist->hvalue.value = value;
148 hist->hvalue.stmt = stmt;
149 hist->type = type;
150 return hist;
151 }
152
153 /* Hash value for histogram. */
154
155 static hashval_t
156 histogram_hash (const void *x)
157 {
158 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
159 }
160
161 /* Return nonzero if statement for histogram_value X is Y. */
162
163 static int
164 histogram_eq (const void *x, const void *y)
165 {
166 return ((const_histogram_value) x)->hvalue.stmt == (const_gimple) y;
167 }
168
169 /* Set histogram for STMT. */
170
171 static void
172 set_histogram_value (struct function *fun, gimple stmt, histogram_value hist)
173 {
174 void **loc;
175 if (!hist && !VALUE_HISTOGRAMS (fun))
176 return;
177 if (!VALUE_HISTOGRAMS (fun))
178 VALUE_HISTOGRAMS (fun) = htab_create (1, histogram_hash,
179 histogram_eq, NULL);
180 loc = htab_find_slot_with_hash (VALUE_HISTOGRAMS (fun), stmt,
181 htab_hash_pointer (stmt),
182 hist ? INSERT : NO_INSERT);
183 if (!hist)
184 {
185 if (loc)
186 htab_clear_slot (VALUE_HISTOGRAMS (fun), loc);
187 return;
188 }
189 *loc = hist;
190 }
191
192 /* Get histogram list for STMT. */
193
194 histogram_value
195 gimple_histogram_value (struct function *fun, gimple stmt)
196 {
197 if (!VALUE_HISTOGRAMS (fun))
198 return NULL;
199 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
200 htab_hash_pointer (stmt));
201 }
202
203 /* Add histogram for STMT. */
204
205 void
206 gimple_add_histogram_value (struct function *fun, gimple stmt,
207 histogram_value hist)
208 {
209 hist->hvalue.next = gimple_histogram_value (fun, stmt);
210 set_histogram_value (fun, stmt, hist);
211 hist->fun = fun;
212 }
213
214
215 /* Remove histogram HIST from STMT's histogram list. */
216
217 void
218 gimple_remove_histogram_value (struct function *fun, gimple stmt,
219 histogram_value hist)
220 {
221 histogram_value hist2 = gimple_histogram_value (fun, stmt);
222 if (hist == hist2)
223 {
224 set_histogram_value (fun, stmt, hist->hvalue.next);
225 }
226 else
227 {
228 while (hist2->hvalue.next != hist)
229 hist2 = hist2->hvalue.next;
230 hist2->hvalue.next = hist->hvalue.next;
231 }
232 free (hist->hvalue.counters);
233 #ifdef ENABLE_CHECKING
234 memset (hist, 0xab, sizeof (*hist));
235 #endif
236 free (hist);
237 }
238
239
240 /* Lookup histogram of type TYPE in the STMT. */
241
242 histogram_value
243 gimple_histogram_value_of_type (struct function *fun, gimple stmt,
244 enum hist_type type)
245 {
246 histogram_value hist;
247 for (hist = gimple_histogram_value (fun, stmt); hist;
248 hist = hist->hvalue.next)
249 if (hist->type == type)
250 return hist;
251 return NULL;
252 }
253
254 /* Dump information about HIST to DUMP_FILE. */
255
256 static void
257 dump_histogram_value (FILE *dump_file, histogram_value hist)
258 {
259 switch (hist->type)
260 {
261 case HIST_TYPE_INTERVAL:
262 fprintf (dump_file, "Interval counter range %d -- %d",
263 hist->hdata.intvl.int_start,
264 (hist->hdata.intvl.int_start
265 + hist->hdata.intvl.steps - 1));
266 if (hist->hvalue.counters)
267 {
268 unsigned int i;
269 fprintf (dump_file, " [");
270 for (i = 0; i < hist->hdata.intvl.steps; i++)
271 fprintf (dump_file, " %d:%"PRId64,
272 hist->hdata.intvl.int_start + i,
273 (int64_t) hist->hvalue.counters[i]);
274 fprintf (dump_file, " ] outside range:%"PRId64,
275 (int64_t) hist->hvalue.counters[i]);
276 }
277 fprintf (dump_file, ".\n");
278 break;
279
280 case HIST_TYPE_POW2:
281 fprintf (dump_file, "Pow2 counter ");
282 if (hist->hvalue.counters)
283 {
284 fprintf (dump_file, "pow2:%"PRId64
285 " nonpow2:%"PRId64,
286 (int64_t) hist->hvalue.counters[0],
287 (int64_t) hist->hvalue.counters[1]);
288 }
289 fprintf (dump_file, ".\n");
290 break;
291
292 case HIST_TYPE_SINGLE_VALUE:
293 fprintf (dump_file, "Single value ");
294 if (hist->hvalue.counters)
295 {
296 fprintf (dump_file, "value:%"PRId64
297 " match:%"PRId64
298 " wrong:%"PRId64,
299 (int64_t) hist->hvalue.counters[0],
300 (int64_t) hist->hvalue.counters[1],
301 (int64_t) hist->hvalue.counters[2]);
302 }
303 fprintf (dump_file, ".\n");
304 break;
305
306 case HIST_TYPE_AVERAGE:
307 fprintf (dump_file, "Average value ");
308 if (hist->hvalue.counters)
309 {
310 fprintf (dump_file, "sum:%"PRId64
311 " times:%"PRId64,
312 (int64_t) hist->hvalue.counters[0],
313 (int64_t) hist->hvalue.counters[1]);
314 }
315 fprintf (dump_file, ".\n");
316 break;
317
318 case HIST_TYPE_IOR:
319 fprintf (dump_file, "IOR value ");
320 if (hist->hvalue.counters)
321 {
322 fprintf (dump_file, "ior:%"PRId64,
323 (int64_t) hist->hvalue.counters[0]);
324 }
325 fprintf (dump_file, ".\n");
326 break;
327
328 case HIST_TYPE_CONST_DELTA:
329 fprintf (dump_file, "Constant delta ");
330 if (hist->hvalue.counters)
331 {
332 fprintf (dump_file, "value:%"PRId64
333 " match:%"PRId64
334 " wrong:%"PRId64,
335 (int64_t) hist->hvalue.counters[0],
336 (int64_t) hist->hvalue.counters[1],
337 (int64_t) hist->hvalue.counters[2]);
338 }
339 fprintf (dump_file, ".\n");
340 break;
341 case HIST_TYPE_INDIR_CALL:
342 fprintf (dump_file, "Indirect call ");
343 if (hist->hvalue.counters)
344 {
345 fprintf (dump_file, "value:%"PRId64
346 " match:%"PRId64
347 " all:%"PRId64,
348 (int64_t) hist->hvalue.counters[0],
349 (int64_t) hist->hvalue.counters[1],
350 (int64_t) hist->hvalue.counters[2]);
351 }
352 fprintf (dump_file, ".\n");
353 break;
354 case HIST_TYPE_TIME_PROFILE:
355 fprintf (dump_file, "Time profile ");
356 if (hist->hvalue.counters)
357 {
358 fprintf (dump_file, "time:%"PRId64,
359 (int64_t) hist->hvalue.counters[0]);
360 }
361 fprintf (dump_file, ".\n");
362 break;
363 case HIST_TYPE_INDIR_CALL_TOPN:
364 fprintf (dump_file, "Indirect call topn ");
365 if (hist->hvalue.counters)
366 {
367 int i;
368
369 fprintf (dump_file, "accu:%"PRId64, hist->hvalue.counters[0]);
370 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
371 {
372 fprintf (dump_file, " target:%"PRId64 " value:%"PRId64,
373 (int64_t) hist->hvalue.counters[i],
374 (int64_t) hist->hvalue.counters[i+1]);
375 }
376 }
377 fprintf (dump_file, ".\n");
378 break;
379 case HIST_TYPE_MAX:
380 gcc_unreachable ();
381 }
382 }
383
384 /* Dump information about HIST to DUMP_FILE. */
385
386 void
387 stream_out_histogram_value (struct output_block *ob, histogram_value hist)
388 {
389 struct bitpack_d bp;
390 unsigned int i;
391
392 bp = bitpack_create (ob->main_stream);
393 bp_pack_enum (&bp, hist_type, HIST_TYPE_MAX, hist->type);
394 bp_pack_value (&bp, hist->hvalue.next != NULL, 1);
395 streamer_write_bitpack (&bp);
396 switch (hist->type)
397 {
398 case HIST_TYPE_INTERVAL:
399 streamer_write_hwi (ob, hist->hdata.intvl.int_start);
400 streamer_write_uhwi (ob, hist->hdata.intvl.steps);
401 break;
402 default:
403 break;
404 }
405 for (i = 0; i < hist->n_counters; i++)
406 streamer_write_gcov_count (ob, hist->hvalue.counters[i]);
407 if (hist->hvalue.next)
408 stream_out_histogram_value (ob, hist->hvalue.next);
409 }
410 /* Dump information about HIST to DUMP_FILE. */
411
412 void
413 stream_in_histogram_value (struct lto_input_block *ib, gimple stmt)
414 {
415 enum hist_type type;
416 unsigned int ncounters = 0;
417 struct bitpack_d bp;
418 unsigned int i;
419 histogram_value new_val;
420 bool next;
421 histogram_value *next_p = NULL;
422
423 do
424 {
425 bp = streamer_read_bitpack (ib);
426 type = bp_unpack_enum (&bp, hist_type, HIST_TYPE_MAX);
427 next = bp_unpack_value (&bp, 1);
428 new_val = gimple_alloc_histogram_value (cfun, type, stmt, NULL);
429 switch (type)
430 {
431 case HIST_TYPE_INTERVAL:
432 new_val->hdata.intvl.int_start = streamer_read_hwi (ib);
433 new_val->hdata.intvl.steps = streamer_read_uhwi (ib);
434 ncounters = new_val->hdata.intvl.steps + 2;
435 break;
436
437 case HIST_TYPE_POW2:
438 case HIST_TYPE_AVERAGE:
439 ncounters = 2;
440 break;
441
442 case HIST_TYPE_SINGLE_VALUE:
443 case HIST_TYPE_INDIR_CALL:
444 ncounters = 3;
445 break;
446
447 case HIST_TYPE_CONST_DELTA:
448 ncounters = 4;
449 break;
450
451 case HIST_TYPE_IOR:
452 case HIST_TYPE_TIME_PROFILE:
453 ncounters = 1;
454 break;
455
456 case HIST_TYPE_INDIR_CALL_TOPN:
457 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
458 break;
459
460 case HIST_TYPE_MAX:
461 gcc_unreachable ();
462 }
463 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * ncounters);
464 new_val->n_counters = ncounters;
465 for (i = 0; i < ncounters; i++)
466 new_val->hvalue.counters[i] = streamer_read_gcov_count (ib);
467 if (!next_p)
468 gimple_add_histogram_value (cfun, stmt, new_val);
469 else
470 *next_p = new_val;
471 next_p = &new_val->hvalue.next;
472 }
473 while (next);
474 }
475
476 /* Dump all histograms attached to STMT to DUMP_FILE. */
477
478 void
479 dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple stmt)
480 {
481 histogram_value hist;
482 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
483 dump_histogram_value (dump_file, hist);
484 }
485
486 /* Remove all histograms associated with STMT. */
487
488 void
489 gimple_remove_stmt_histograms (struct function *fun, gimple stmt)
490 {
491 histogram_value val;
492 while ((val = gimple_histogram_value (fun, stmt)) != NULL)
493 gimple_remove_histogram_value (fun, stmt, val);
494 }
495
496 /* Duplicate all histograms associates with OSTMT to STMT. */
497
498 void
499 gimple_duplicate_stmt_histograms (struct function *fun, gimple stmt,
500 struct function *ofun, gimple ostmt)
501 {
502 histogram_value val;
503 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
504 {
505 histogram_value new_val = gimple_alloc_histogram_value (fun, val->type, NULL, NULL);
506 memcpy (new_val, val, sizeof (*val));
507 new_val->hvalue.stmt = stmt;
508 new_val->hvalue.counters = XNEWVAR (gcov_type, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
509 memcpy (new_val->hvalue.counters, val->hvalue.counters, sizeof (*new_val->hvalue.counters) * new_val->n_counters);
510 gimple_add_histogram_value (fun, stmt, new_val);
511 }
512 }
513
514
515 /* Move all histograms associated with OSTMT to STMT. */
516
517 void
518 gimple_move_stmt_histograms (struct function *fun, gimple stmt, gimple ostmt)
519 {
520 histogram_value val = gimple_histogram_value (fun, ostmt);
521 if (val)
522 {
523 /* The following three statements can't be reordered,
524 because histogram hashtab relies on stmt field value
525 for finding the exact slot. */
526 set_histogram_value (fun, ostmt, NULL);
527 for (; val != NULL; val = val->hvalue.next)
528 val->hvalue.stmt = stmt;
529 set_histogram_value (fun, stmt, val);
530 }
531 }
532
533 static bool error_found = false;
534
535 /* Helper function for verify_histograms. For each histogram reachable via htab
536 walk verify that it was reached via statement walk. */
537
538 static int
539 visit_hist (void **slot, void *data)
540 {
541 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
542 histogram_value hist = *(histogram_value *) slot;
543
544 if (!visited->contains (hist)
545 && hist->type != HIST_TYPE_TIME_PROFILE)
546 {
547 error ("dead histogram");
548 dump_histogram_value (stderr, hist);
549 debug_gimple_stmt (hist->hvalue.stmt);
550 error_found = true;
551 }
552 return 1;
553 }
554
555
556 /* Verify sanity of the histograms. */
557
558 DEBUG_FUNCTION void
559 verify_histograms (void)
560 {
561 basic_block bb;
562 gimple_stmt_iterator gsi;
563 histogram_value hist;
564
565 error_found = false;
566 hash_set<histogram_value> visited_hists;
567 FOR_EACH_BB_FN (bb, cfun)
568 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
569 {
570 gimple stmt = gsi_stmt (gsi);
571
572 for (hist = gimple_histogram_value (cfun, stmt); hist;
573 hist = hist->hvalue.next)
574 {
575 if (hist->hvalue.stmt != stmt)
576 {
577 error ("Histogram value statement does not correspond to "
578 "the statement it is associated with");
579 debug_gimple_stmt (stmt);
580 dump_histogram_value (stderr, hist);
581 error_found = true;
582 }
583 visited_hists.add (hist);
584 }
585 }
586 if (VALUE_HISTOGRAMS (cfun))
587 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
588 if (error_found)
589 internal_error ("verify_histograms failed");
590 }
591
592 /* Helper function for verify_histograms. For each histogram reachable via htab
593 walk verify that it was reached via statement walk. */
594
595 static int
596 free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
597 {
598 histogram_value hist = *(histogram_value *) slot;
599 free (hist->hvalue.counters);
600 #ifdef ENABLE_CHECKING
601 memset (hist, 0xab, sizeof (*hist));
602 #endif
603 free (hist);
604 return 1;
605 }
606
607 void
608 free_histograms (void)
609 {
610 if (VALUE_HISTOGRAMS (cfun))
611 {
612 htab_traverse (VALUE_HISTOGRAMS (cfun), free_hist, NULL);
613 htab_delete (VALUE_HISTOGRAMS (cfun));
614 VALUE_HISTOGRAMS (cfun) = NULL;
615 }
616 }
617
618
619 /* The overall number of invocations of the counter should match
620 execution count of basic block. Report it as error rather than
621 internal error as it might mean that user has misused the profile
622 somehow. */
623
624 static bool
625 check_counter (gimple stmt, const char * name,
626 gcov_type *count, gcov_type *all, gcov_type bb_count)
627 {
628 if (*all != bb_count || *count > *all)
629 {
630 location_t locus;
631 locus = (stmt != NULL)
632 ? gimple_location (stmt)
633 : DECL_SOURCE_LOCATION (current_function_decl);
634 if (flag_profile_correction)
635 {
636 if (dump_enabled_p ())
637 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
638 "correcting inconsistent value profile: %s "
639 "profiler overall count (%d) does not match BB "
640 "count (%d)\n", name, (int)*all, (int)bb_count);
641 *all = bb_count;
642 if (*count > *all)
643 *count = *all;
644 return false;
645 }
646 else
647 {
648 error_at (locus, "corrupted value profile: %s "
649 "profile counter (%d out of %d) inconsistent with "
650 "basic-block count (%d)",
651 name,
652 (int) *count,
653 (int) *all,
654 (int) bb_count);
655 return true;
656 }
657 }
658
659 return false;
660 }
661
662
663 /* GIMPLE based transformations. */
664
665 bool
666 gimple_value_profile_transformations (void)
667 {
668 basic_block bb;
669 gimple_stmt_iterator gsi;
670 bool changed = false;
671
672 FOR_EACH_BB_FN (bb, cfun)
673 {
674 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
675 {
676 gimple stmt = gsi_stmt (gsi);
677 histogram_value th = gimple_histogram_value (cfun, stmt);
678 if (!th)
679 continue;
680
681 if (dump_file)
682 {
683 fprintf (dump_file, "Trying transformations on stmt ");
684 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
685 dump_histograms_for_stmt (cfun, dump_file, stmt);
686 }
687
688 /* Transformations: */
689 /* The order of things in this conditional controls which
690 transformation is used when more than one is applicable. */
691 /* It is expected that any code added by the transformations
692 will be added before the current statement, and that the
693 current statement remain valid (although possibly
694 modified) upon return. */
695 if (gimple_mod_subtract_transform (&gsi)
696 || gimple_divmod_fixed_value_transform (&gsi)
697 || gimple_mod_pow2_value_transform (&gsi)
698 || gimple_stringops_transform (&gsi)
699 || gimple_ic_transform (&gsi))
700 {
701 stmt = gsi_stmt (gsi);
702 changed = true;
703 /* Original statement may no longer be in the same block. */
704 if (bb != gimple_bb (stmt))
705 {
706 bb = gimple_bb (stmt);
707 gsi = gsi_for_stmt (stmt);
708 }
709 }
710 }
711 }
712
713 if (changed)
714 {
715 counts_to_freqs ();
716 }
717
718 return changed;
719 }
720
721
722 /* Generate code for transformation 1 (with parent gimple assignment
723 STMT and probability of taking the optimal path PROB, which is
724 equivalent to COUNT/ALL within roundoff error). This generates the
725 result into a temp and returns the temp; it does not replace or
726 alter the original STMT. */
727
728 static tree
729 gimple_divmod_fixed_value (gimple stmt, tree value, int prob, gcov_type count,
730 gcov_type all)
731 {
732 gimple stmt1, stmt2, stmt3;
733 tree tmp0, tmp1, tmp2;
734 gimple bb1end, bb2end, bb3end;
735 basic_block bb, bb2, bb3, bb4;
736 tree optype, op1, op2;
737 edge e12, e13, e23, e24, e34;
738 gimple_stmt_iterator gsi;
739
740 gcc_assert (is_gimple_assign (stmt)
741 && (gimple_assign_rhs_code (stmt) == TRUNC_DIV_EXPR
742 || gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR));
743
744 optype = TREE_TYPE (gimple_assign_lhs (stmt));
745 op1 = gimple_assign_rhs1 (stmt);
746 op2 = gimple_assign_rhs2 (stmt);
747
748 bb = gimple_bb (stmt);
749 gsi = gsi_for_stmt (stmt);
750
751 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
752 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
753 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
754 stmt2 = gimple_build_assign (tmp1, op2);
755 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
756 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
757 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
758 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
759 bb1end = stmt3;
760
761 tmp2 = create_tmp_reg (optype, "PROF");
762 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
763 op1, tmp0);
764 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
765 bb2end = stmt1;
766
767 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), tmp2,
768 op1, op2);
769 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
770 bb3end = stmt1;
771
772 /* Fix CFG. */
773 /* Edge e23 connects bb2 to bb3, etc. */
774 e12 = split_block (bb, bb1end);
775 bb2 = e12->dest;
776 bb2->count = count;
777 e23 = split_block (bb2, bb2end);
778 bb3 = e23->dest;
779 bb3->count = all - count;
780 e34 = split_block (bb3, bb3end);
781 bb4 = e34->dest;
782 bb4->count = all;
783
784 e12->flags &= ~EDGE_FALLTHRU;
785 e12->flags |= EDGE_FALSE_VALUE;
786 e12->probability = prob;
787 e12->count = count;
788
789 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
790 e13->probability = REG_BR_PROB_BASE - prob;
791 e13->count = all - count;
792
793 remove_edge (e23);
794
795 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
796 e24->probability = REG_BR_PROB_BASE;
797 e24->count = count;
798
799 e34->probability = REG_BR_PROB_BASE;
800 e34->count = all - count;
801
802 return tmp2;
803 }
804
805
806 /* Do transform 1) on INSN if applicable. */
807
808 static bool
809 gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
810 {
811 histogram_value histogram;
812 enum tree_code code;
813 gcov_type val, count, all;
814 tree result, value, tree_val;
815 gcov_type prob;
816 gimple stmt;
817
818 stmt = gsi_stmt (*si);
819 if (gimple_code (stmt) != GIMPLE_ASSIGN)
820 return false;
821
822 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
823 return false;
824
825 code = gimple_assign_rhs_code (stmt);
826
827 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
828 return false;
829
830 histogram = gimple_histogram_value_of_type (cfun, stmt,
831 HIST_TYPE_SINGLE_VALUE);
832 if (!histogram)
833 return false;
834
835 value = histogram->hvalue.value;
836 val = histogram->hvalue.counters[0];
837 count = histogram->hvalue.counters[1];
838 all = histogram->hvalue.counters[2];
839 gimple_remove_histogram_value (cfun, stmt, histogram);
840
841 /* We require that count is at least half of all; this means
842 that for the transformation to fire the value must be constant
843 at least 50% of time (and 75% gives the guarantee of usage). */
844 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
845 || 2 * count < all
846 || optimize_bb_for_size_p (gimple_bb (stmt)))
847 return false;
848
849 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
850 return false;
851
852 /* Compute probability of taking the optimal path. */
853 if (all > 0)
854 prob = GCOV_COMPUTE_SCALE (count, all);
855 else
856 prob = 0;
857
858 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
859 tree_val = build_int_cst (get_gcov_type (), val);
860 else
861 {
862 HOST_WIDE_INT a[2];
863 a[0] = (unsigned HOST_WIDE_INT) val;
864 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
865
866 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
867 TYPE_PRECISION (get_gcov_type ()), false));
868 }
869 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
870
871 if (dump_file)
872 {
873 fprintf (dump_file, "Div/mod by constant ");
874 print_generic_expr (dump_file, value, TDF_SLIM);
875 fprintf (dump_file, "=");
876 print_generic_expr (dump_file, tree_val, TDF_SLIM);
877 fprintf (dump_file, " transformation on insn ");
878 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
879 }
880
881 gimple_assign_set_rhs_from_tree (si, result);
882 update_stmt (gsi_stmt (*si));
883
884 return true;
885 }
886
887 /* Generate code for transformation 2 (with parent gimple assign STMT and
888 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
889 within roundoff error). This generates the result into a temp and returns
890 the temp; it does not replace or alter the original STMT. */
891 static tree
892 gimple_mod_pow2 (gimple stmt, int prob, gcov_type count, gcov_type all)
893 {
894 gimple stmt1, stmt2, stmt3, stmt4;
895 tree tmp2, tmp3;
896 gimple bb1end, bb2end, bb3end;
897 basic_block bb, bb2, bb3, bb4;
898 tree optype, op1, op2;
899 edge e12, e13, e23, e24, e34;
900 gimple_stmt_iterator gsi;
901 tree result;
902
903 gcc_assert (is_gimple_assign (stmt)
904 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
905
906 optype = TREE_TYPE (gimple_assign_lhs (stmt));
907 op1 = gimple_assign_rhs1 (stmt);
908 op2 = gimple_assign_rhs2 (stmt);
909
910 bb = gimple_bb (stmt);
911 gsi = gsi_for_stmt (stmt);
912
913 result = create_tmp_reg (optype, "PROF");
914 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
915 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
916 stmt2 = gimple_build_assign_with_ops (PLUS_EXPR, tmp2, op2,
917 build_int_cst (optype, -1));
918 stmt3 = gimple_build_assign_with_ops (BIT_AND_EXPR, tmp3, tmp2, op2);
919 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
920 NULL_TREE, NULL_TREE);
921 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
922 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
923 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
924 bb1end = stmt4;
925
926 /* tmp2 == op2-1 inherited from previous block. */
927 stmt1 = gimple_build_assign_with_ops (BIT_AND_EXPR, result, op1, tmp2);
928 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
929 bb2end = stmt1;
930
931 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
932 op1, op2);
933 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
934 bb3end = stmt1;
935
936 /* Fix CFG. */
937 /* Edge e23 connects bb2 to bb3, etc. */
938 e12 = split_block (bb, bb1end);
939 bb2 = e12->dest;
940 bb2->count = count;
941 e23 = split_block (bb2, bb2end);
942 bb3 = e23->dest;
943 bb3->count = all - count;
944 e34 = split_block (bb3, bb3end);
945 bb4 = e34->dest;
946 bb4->count = all;
947
948 e12->flags &= ~EDGE_FALLTHRU;
949 e12->flags |= EDGE_FALSE_VALUE;
950 e12->probability = prob;
951 e12->count = count;
952
953 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
954 e13->probability = REG_BR_PROB_BASE - prob;
955 e13->count = all - count;
956
957 remove_edge (e23);
958
959 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
960 e24->probability = REG_BR_PROB_BASE;
961 e24->count = count;
962
963 e34->probability = REG_BR_PROB_BASE;
964 e34->count = all - count;
965
966 return result;
967 }
968
969 /* Do transform 2) on INSN if applicable. */
970 static bool
971 gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
972 {
973 histogram_value histogram;
974 enum tree_code code;
975 gcov_type count, wrong_values, all;
976 tree lhs_type, result, value;
977 gcov_type prob;
978 gimple stmt;
979
980 stmt = gsi_stmt (*si);
981 if (gimple_code (stmt) != GIMPLE_ASSIGN)
982 return false;
983
984 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
985 if (!INTEGRAL_TYPE_P (lhs_type))
986 return false;
987
988 code = gimple_assign_rhs_code (stmt);
989
990 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
991 return false;
992
993 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
994 if (!histogram)
995 return false;
996
997 value = histogram->hvalue.value;
998 wrong_values = histogram->hvalue.counters[0];
999 count = histogram->hvalue.counters[1];
1000
1001 gimple_remove_histogram_value (cfun, stmt, histogram);
1002
1003 /* We require that we hit a power of 2 at least half of all evaluations. */
1004 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
1005 || count < wrong_values
1006 || optimize_bb_for_size_p (gimple_bb (stmt)))
1007 return false;
1008
1009 if (dump_file)
1010 {
1011 fprintf (dump_file, "Mod power of 2 transformation on insn ");
1012 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1013 }
1014
1015 /* Compute probability of taking the optimal path. */
1016 all = count + wrong_values;
1017
1018 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
1019 return false;
1020
1021 if (all > 0)
1022 prob = GCOV_COMPUTE_SCALE (count, all);
1023 else
1024 prob = 0;
1025
1026 result = gimple_mod_pow2 (stmt, prob, count, all);
1027
1028 gimple_assign_set_rhs_from_tree (si, result);
1029 update_stmt (gsi_stmt (*si));
1030
1031 return true;
1032 }
1033
1034 /* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1035 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1036 supported and this is built into this interface. The probabilities of taking
1037 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
1038 COUNT2/ALL respectively within roundoff error). This generates the
1039 result into a temp and returns the temp; it does not replace or alter
1040 the original STMT. */
1041 /* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1042
1043 static tree
1044 gimple_mod_subtract (gimple stmt, int prob1, int prob2, int ncounts,
1045 gcov_type count1, gcov_type count2, gcov_type all)
1046 {
1047 gimple stmt1, stmt2, stmt3;
1048 tree tmp1;
1049 gimple bb1end, bb2end = NULL, bb3end;
1050 basic_block bb, bb2, bb3, bb4;
1051 tree optype, op1, op2;
1052 edge e12, e23 = 0, e24, e34, e14;
1053 gimple_stmt_iterator gsi;
1054 tree result;
1055
1056 gcc_assert (is_gimple_assign (stmt)
1057 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
1058
1059 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1060 op1 = gimple_assign_rhs1 (stmt);
1061 op2 = gimple_assign_rhs2 (stmt);
1062
1063 bb = gimple_bb (stmt);
1064 gsi = gsi_for_stmt (stmt);
1065
1066 result = create_tmp_reg (optype, "PROF");
1067 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1068 stmt1 = gimple_build_assign (result, op1);
1069 stmt2 = gimple_build_assign (tmp1, op2);
1070 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1071 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1072 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1073 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
1074 bb1end = stmt3;
1075
1076 if (ncounts) /* Assumed to be 0 or 1 */
1077 {
1078 stmt1 = gimple_build_assign_with_ops (MINUS_EXPR, result, result, tmp1);
1079 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1080 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1081 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1082 bb2end = stmt2;
1083 }
1084
1085 /* Fallback case. */
1086 stmt1 = gimple_build_assign_with_ops (gimple_assign_rhs_code (stmt), result,
1087 result, tmp1);
1088 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1089 bb3end = stmt1;
1090
1091 /* Fix CFG. */
1092 /* Edge e23 connects bb2 to bb3, etc. */
1093 /* However block 3 is optional; if it is not there, references
1094 to 3 really refer to block 2. */
1095 e12 = split_block (bb, bb1end);
1096 bb2 = e12->dest;
1097 bb2->count = all - count1;
1098
1099 if (ncounts) /* Assumed to be 0 or 1. */
1100 {
1101 e23 = split_block (bb2, bb2end);
1102 bb3 = e23->dest;
1103 bb3->count = all - count1 - count2;
1104 }
1105
1106 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1107 bb4 = e34->dest;
1108 bb4->count = all;
1109
1110 e12->flags &= ~EDGE_FALLTHRU;
1111 e12->flags |= EDGE_FALSE_VALUE;
1112 e12->probability = REG_BR_PROB_BASE - prob1;
1113 e12->count = all - count1;
1114
1115 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1116 e14->probability = prob1;
1117 e14->count = count1;
1118
1119 if (ncounts) /* Assumed to be 0 or 1. */
1120 {
1121 e23->flags &= ~EDGE_FALLTHRU;
1122 e23->flags |= EDGE_FALSE_VALUE;
1123 e23->count = all - count1 - count2;
1124 e23->probability = REG_BR_PROB_BASE - prob2;
1125
1126 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1127 e24->probability = prob2;
1128 e24->count = count2;
1129 }
1130
1131 e34->probability = REG_BR_PROB_BASE;
1132 e34->count = all - count1 - count2;
1133
1134 return result;
1135 }
1136
1137
1138 /* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1139
1140 static bool
1141 gimple_mod_subtract_transform (gimple_stmt_iterator *si)
1142 {
1143 histogram_value histogram;
1144 enum tree_code code;
1145 gcov_type count, wrong_values, all;
1146 tree lhs_type, result;
1147 gcov_type prob1, prob2;
1148 unsigned int i, steps;
1149 gcov_type count1, count2;
1150 gimple stmt;
1151
1152 stmt = gsi_stmt (*si);
1153 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1154 return false;
1155
1156 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1157 if (!INTEGRAL_TYPE_P (lhs_type))
1158 return false;
1159
1160 code = gimple_assign_rhs_code (stmt);
1161
1162 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
1163 return false;
1164
1165 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
1166 if (!histogram)
1167 return false;
1168
1169 all = 0;
1170 wrong_values = 0;
1171 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1172 all += histogram->hvalue.counters[i];
1173
1174 wrong_values += histogram->hvalue.counters[i];
1175 wrong_values += histogram->hvalue.counters[i+1];
1176 steps = histogram->hdata.intvl.steps;
1177 all += wrong_values;
1178 count1 = histogram->hvalue.counters[0];
1179 count2 = histogram->hvalue.counters[1];
1180
1181 /* Compute probability of taking the optimal path. */
1182 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
1183 {
1184 gimple_remove_histogram_value (cfun, stmt, histogram);
1185 return false;
1186 }
1187
1188 if (flag_profile_correction && count1 + count2 > all)
1189 all = count1 + count2;
1190
1191 gcc_assert (count1 + count2 <= all);
1192
1193 /* We require that we use just subtractions in at least 50% of all
1194 evaluations. */
1195 count = 0;
1196 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1197 {
1198 count += histogram->hvalue.counters[i];
1199 if (count * 2 >= all)
1200 break;
1201 }
1202 if (i == steps
1203 || optimize_bb_for_size_p (gimple_bb (stmt)))
1204 return false;
1205
1206 gimple_remove_histogram_value (cfun, stmt, histogram);
1207 if (dump_file)
1208 {
1209 fprintf (dump_file, "Mod subtract transformation on insn ");
1210 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1211 }
1212
1213 /* Compute probability of taking the optimal path(s). */
1214 if (all > 0)
1215 {
1216 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1217 prob2 = GCOV_COMPUTE_SCALE (count2, all);
1218 }
1219 else
1220 {
1221 prob1 = prob2 = 0;
1222 }
1223
1224 /* In practice, "steps" is always 2. This interface reflects this,
1225 and will need to be changed if "steps" can change. */
1226 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
1227
1228 gimple_assign_set_rhs_from_tree (si, result);
1229 update_stmt (gsi_stmt (*si));
1230
1231 return true;
1232 }
1233
1234 struct profile_id_traits : default_hashmap_traits
1235 {
1236 template<typename T>
1237 static bool
1238 is_deleted (T &e)
1239 {
1240 return e.m_key == UINT_MAX;
1241 }
1242
1243 template<typename T> static bool is_empty (T &e) { return e.m_key == 0; }
1244 template<typename T> static void mark_deleted (T &e) { e.m_key = UINT_MAX; }
1245 template<typename T> static void mark_empty (T &e) { e.m_key = 0; }
1246 };
1247
1248 static hash_map<unsigned int, cgraph_node *, profile_id_traits> *
1249 cgraph_node_map = 0;
1250
1251 /* Returns true if node graph is initialized. This
1252 is used to test if profile_id has been created
1253 for cgraph_nodes. */
1254
1255 bool
1256 coverage_node_map_initialized_p (void)
1257 {
1258 return cgraph_node_map != 0;
1259 }
1260
1261 /* Initialize map from PROFILE_ID to CGRAPH_NODE.
1262 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1263 that the PROFILE_IDs was already assigned. */
1264
1265 void
1266 init_node_map (bool local)
1267 {
1268 struct cgraph_node *n;
1269 cgraph_node_map
1270 = new hash_map<unsigned int, cgraph_node *, profile_id_traits>;
1271
1272 FOR_EACH_DEFINED_FUNCTION (n)
1273 if (n->has_gimple_body_p ())
1274 {
1275 cgraph_node **val;
1276 if (local)
1277 {
1278 n->profile_id = coverage_compute_profile_id (n);
1279 while ((val = cgraph_node_map->get (n->profile_id))
1280 || !n->profile_id)
1281 {
1282 if (dump_file)
1283 fprintf (dump_file, "Local profile-id %i conflict"
1284 " with nodes %s/%i %s/%i\n",
1285 n->profile_id,
1286 n->name (),
1287 n->order,
1288 (*val)->name (),
1289 (*val)->order);
1290 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1291 }
1292 }
1293 else if (!n->profile_id)
1294 {
1295 if (dump_file)
1296 fprintf (dump_file,
1297 "Node %s/%i has no profile-id"
1298 " (profile feedback missing?)\n",
1299 n->name (),
1300 n->order);
1301 continue;
1302 }
1303 else if ((val = cgraph_node_map->get (n->profile_id)))
1304 {
1305 if (dump_file)
1306 fprintf (dump_file,
1307 "Node %s/%i has IP profile-id %i conflict. "
1308 "Giving up.\n",
1309 n->name (),
1310 n->order,
1311 n->profile_id);
1312 *val = NULL;
1313 continue;
1314 }
1315 cgraph_node_map->put (n->profile_id, n);
1316 }
1317 }
1318
1319 /* Delete the CGRAPH_NODE_MAP. */
1320
1321 void
1322 del_node_map (void)
1323 {
1324 delete cgraph_node_map;
1325 }
1326
1327 /* Return cgraph node for function with pid */
1328
1329 struct cgraph_node*
1330 find_func_by_profile_id (int profile_id)
1331 {
1332 cgraph_node **val = cgraph_node_map->get (profile_id);
1333 if (val)
1334 return *val;
1335 else
1336 return NULL;
1337 }
1338
1339 /* Perform sanity check on the indirect call target. Due to race conditions,
1340 false function target may be attributed to an indirect call site. If the
1341 call expression type mismatches with the target function's type, expand_call
1342 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1343 Returns true if TARGET is considered ok for call CALL_STMT. */
1344
1345 static bool
1346 check_ic_target (gimple call_stmt, struct cgraph_node *target)
1347 {
1348 location_t locus;
1349 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
1350 return true;
1351
1352 locus = gimple_location (call_stmt);
1353 if (dump_enabled_p ())
1354 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
1355 "Skipping target %s with mismatching types for icall\n",
1356 target->name ());
1357 return false;
1358 }
1359
1360 /* Do transformation
1361
1362 if (actual_callee_address == address_of_most_common_function/method)
1363 do direct call
1364 else
1365 old call
1366 */
1367
1368 gimple
1369 gimple_ic (gimple icall_stmt, struct cgraph_node *direct_call,
1370 int prob, gcov_type count, gcov_type all)
1371 {
1372 gimple dcall_stmt, load_stmt, cond_stmt;
1373 tree tmp0, tmp1, tmp;
1374 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
1375 tree optype = build_pointer_type (void_type_node);
1376 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
1377 gimple_stmt_iterator gsi;
1378 int lp_nr, dflags;
1379 edge e_eh, e;
1380 edge_iterator ei;
1381 gimple_stmt_iterator psi;
1382
1383 cond_bb = gimple_bb (icall_stmt);
1384 gsi = gsi_for_stmt (icall_stmt);
1385
1386 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1387 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1388 tmp = unshare_expr (gimple_call_fn (icall_stmt));
1389 load_stmt = gimple_build_assign (tmp0, tmp);
1390 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1391
1392 tmp = fold_convert (optype, build_addr (direct_call->decl,
1393 current_function_decl));
1394 load_stmt = gimple_build_assign (tmp1, tmp);
1395 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
1396
1397 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1398 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1399
1400 gimple_set_vdef (icall_stmt, NULL_TREE);
1401 gimple_set_vuse (icall_stmt, NULL_TREE);
1402 update_stmt (icall_stmt);
1403 dcall_stmt = gimple_copy (icall_stmt);
1404 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1405 dflags = flags_from_decl_or_type (direct_call->decl);
1406 if ((dflags & ECF_NORETURN) != 0)
1407 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
1408 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
1409
1410 /* Fix CFG. */
1411 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1412 e_cd = split_block (cond_bb, cond_stmt);
1413 dcall_bb = e_cd->dest;
1414 dcall_bb->count = count;
1415
1416 e_di = split_block (dcall_bb, dcall_stmt);
1417 icall_bb = e_di->dest;
1418 icall_bb->count = all - count;
1419
1420 /* Do not disturb existing EH edges from the indirect call. */
1421 if (!stmt_ends_bb_p (icall_stmt))
1422 e_ij = split_block (icall_bb, icall_stmt);
1423 else
1424 {
1425 e_ij = find_fallthru_edge (icall_bb->succs);
1426 /* The indirect call might be noreturn. */
1427 if (e_ij != NULL)
1428 {
1429 e_ij->probability = REG_BR_PROB_BASE;
1430 e_ij->count = all - count;
1431 e_ij = single_pred_edge (split_edge (e_ij));
1432 }
1433 }
1434 if (e_ij != NULL)
1435 {
1436 join_bb = e_ij->dest;
1437 join_bb->count = all;
1438 }
1439
1440 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1441 e_cd->probability = prob;
1442 e_cd->count = count;
1443
1444 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1445 e_ci->probability = REG_BR_PROB_BASE - prob;
1446 e_ci->count = all - count;
1447
1448 remove_edge (e_di);
1449
1450 if (e_ij != NULL)
1451 {
1452 if ((dflags & ECF_NORETURN) != 0)
1453 e_ij->count = all;
1454 else
1455 {
1456 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1457 e_dj->probability = REG_BR_PROB_BASE;
1458 e_dj->count = count;
1459
1460 e_ij->count = all - count;
1461 }
1462 e_ij->probability = REG_BR_PROB_BASE;
1463 }
1464
1465 /* Insert PHI node for the call result if necessary. */
1466 if (gimple_call_lhs (icall_stmt)
1467 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1468 && (dflags & ECF_NORETURN) == 0)
1469 {
1470 tree result = gimple_call_lhs (icall_stmt);
1471 gimple phi = create_phi_node (result, join_bb);
1472 gimple_call_set_lhs (icall_stmt,
1473 duplicate_ssa_name (result, icall_stmt));
1474 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1475 gimple_call_set_lhs (dcall_stmt,
1476 duplicate_ssa_name (result, dcall_stmt));
1477 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
1478 }
1479
1480 /* Build an EH edge for the direct call if necessary. */
1481 lp_nr = lookup_stmt_eh_lp (icall_stmt);
1482 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
1483 {
1484 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
1485 }
1486
1487 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1488 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1489 {
1490 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1491 for (psi = gsi_start_phis (e_eh->dest);
1492 !gsi_end_p (psi); gsi_next (&psi))
1493 {
1494 gimple phi = gsi_stmt (psi);
1495 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1496 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1497 }
1498 }
1499 return dcall_stmt;
1500 }
1501
1502 /*
1503 For every checked indirect/virtual call determine if most common pid of
1504 function/class method has probability more than 50%. If yes modify code of
1505 this call to:
1506 */
1507
1508 static bool
1509 gimple_ic_transform (gimple_stmt_iterator *gsi)
1510 {
1511 gimple stmt = gsi_stmt (*gsi);
1512 histogram_value histogram;
1513 gcov_type val, count, all, bb_all;
1514 struct cgraph_node *direct_call;
1515
1516 if (gimple_code (stmt) != GIMPLE_CALL)
1517 return false;
1518
1519 if (gimple_call_fndecl (stmt) != NULL_TREE)
1520 return false;
1521
1522 if (gimple_call_internal_p (stmt))
1523 return false;
1524
1525 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1526 if (!histogram)
1527 return false;
1528
1529 val = histogram->hvalue.counters [0];
1530 count = histogram->hvalue.counters [1];
1531 all = histogram->hvalue.counters [2];
1532
1533 bb_all = gimple_bb (stmt)->count;
1534 /* The order of CHECK_COUNTER calls is important -
1535 since check_counter can correct the third parameter
1536 and we want to make count <= all <= bb_all. */
1537 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1538 || check_counter (stmt, "ic", &count, &all, all))
1539 {
1540 gimple_remove_histogram_value (cfun, stmt, histogram);
1541 return false;
1542 }
1543
1544 if (4 * count <= 3 * all)
1545 return false;
1546
1547 direct_call = find_func_by_profile_id ((int)val);
1548
1549 if (direct_call == NULL)
1550 {
1551 if (val)
1552 {
1553 if (dump_file)
1554 {
1555 fprintf (dump_file, "Indirect call -> direct call from other module");
1556 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1557 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1558 }
1559 }
1560 return false;
1561 }
1562
1563 if (!check_ic_target (stmt, direct_call))
1564 {
1565 if (dump_file)
1566 {
1567 fprintf (dump_file, "Indirect call -> direct call ");
1568 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1569 fprintf (dump_file, "=> ");
1570 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1571 fprintf (dump_file, " transformation skipped because of type mismatch");
1572 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1573 }
1574 gimple_remove_histogram_value (cfun, stmt, histogram);
1575 return false;
1576 }
1577
1578 if (dump_file)
1579 {
1580 fprintf (dump_file, "Indirect call -> direct call ");
1581 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1582 fprintf (dump_file, "=> ");
1583 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
1584 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
1585 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1586 fprintf (dump_file, "hist->count %"PRId64
1587 " hist->all %"PRId64"\n", count, all);
1588 }
1589
1590 return true;
1591 }
1592
1593 /* Return true if the stringop CALL with FNDECL shall be profiled.
1594 SIZE_ARG be set to the argument index for the size of the string
1595 operation.
1596 */
1597 static bool
1598 interesting_stringop_to_profile_p (tree fndecl, gimple call, int *size_arg)
1599 {
1600 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
1601
1602 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1603 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
1604 return false;
1605
1606 switch (fcode)
1607 {
1608 case BUILT_IN_MEMCPY:
1609 case BUILT_IN_MEMPCPY:
1610 *size_arg = 2;
1611 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1612 INTEGER_TYPE, VOID_TYPE);
1613 case BUILT_IN_MEMSET:
1614 *size_arg = 2;
1615 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1616 INTEGER_TYPE, VOID_TYPE);
1617 case BUILT_IN_BZERO:
1618 *size_arg = 1;
1619 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1620 VOID_TYPE);
1621 default:
1622 gcc_unreachable ();
1623 }
1624 }
1625
1626 /* Convert stringop (..., vcall_size)
1627 into
1628 if (vcall_size == icall_size)
1629 stringop (..., icall_size);
1630 else
1631 stringop (..., vcall_size);
1632 assuming we'll propagate a true constant into ICALL_SIZE later. */
1633
1634 static void
1635 gimple_stringop_fixed_value (gimple vcall_stmt, tree icall_size, int prob,
1636 gcov_type count, gcov_type all)
1637 {
1638 gimple tmp_stmt, cond_stmt, icall_stmt;
1639 tree tmp0, tmp1, vcall_size, optype;
1640 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1641 edge e_ci, e_cv, e_iv, e_ij, e_vj;
1642 gimple_stmt_iterator gsi;
1643 tree fndecl;
1644 int size_arg;
1645
1646 fndecl = gimple_call_fndecl (vcall_stmt);
1647 if (!interesting_stringop_to_profile_p (fndecl, vcall_stmt, &size_arg))
1648 gcc_unreachable ();
1649
1650 cond_bb = gimple_bb (vcall_stmt);
1651 gsi = gsi_for_stmt (vcall_stmt);
1652
1653 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
1654 optype = TREE_TYPE (vcall_size);
1655
1656 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1657 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
1658 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
1659 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1660
1661 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1662 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1663
1664 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
1665 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1666
1667 gimple_set_vdef (vcall_stmt, NULL);
1668 gimple_set_vuse (vcall_stmt, NULL);
1669 update_stmt (vcall_stmt);
1670 icall_stmt = gimple_copy (vcall_stmt);
1671 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
1672 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
1673
1674 /* Fix CFG. */
1675 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1676 e_ci = split_block (cond_bb, cond_stmt);
1677 icall_bb = e_ci->dest;
1678 icall_bb->count = count;
1679
1680 e_iv = split_block (icall_bb, icall_stmt);
1681 vcall_bb = e_iv->dest;
1682 vcall_bb->count = all - count;
1683
1684 e_vj = split_block (vcall_bb, vcall_stmt);
1685 join_bb = e_vj->dest;
1686 join_bb->count = all;
1687
1688 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1689 e_ci->probability = prob;
1690 e_ci->count = count;
1691
1692 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1693 e_cv->probability = REG_BR_PROB_BASE - prob;
1694 e_cv->count = all - count;
1695
1696 remove_edge (e_iv);
1697
1698 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1699 e_ij->probability = REG_BR_PROB_BASE;
1700 e_ij->count = count;
1701
1702 e_vj->probability = REG_BR_PROB_BASE;
1703 e_vj->count = all - count;
1704
1705 /* Insert PHI node for the call result if necessary. */
1706 if (gimple_call_lhs (vcall_stmt)
1707 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1708 {
1709 tree result = gimple_call_lhs (vcall_stmt);
1710 gimple phi = create_phi_node (result, join_bb);
1711 gimple_call_set_lhs (vcall_stmt,
1712 duplicate_ssa_name (result, vcall_stmt));
1713 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
1714 gimple_call_set_lhs (icall_stmt,
1715 duplicate_ssa_name (result, icall_stmt));
1716 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
1717 }
1718
1719 /* Because these are all string op builtins, they're all nothrow. */
1720 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1721 gcc_assert (!stmt_could_throw_p (icall_stmt));
1722 }
1723
1724 /* Find values inside STMT for that we want to measure histograms for
1725 division/modulo optimization. */
1726 static bool
1727 gimple_stringops_transform (gimple_stmt_iterator *gsi)
1728 {
1729 gimple stmt = gsi_stmt (*gsi);
1730 tree fndecl;
1731 tree blck_size;
1732 enum built_in_function fcode;
1733 histogram_value histogram;
1734 gcov_type count, all, val;
1735 tree dest, src;
1736 unsigned int dest_align, src_align;
1737 gcov_type prob;
1738 tree tree_val;
1739 int size_arg;
1740
1741 if (gimple_code (stmt) != GIMPLE_CALL)
1742 return false;
1743 fndecl = gimple_call_fndecl (stmt);
1744 if (!fndecl)
1745 return false;
1746 fcode = DECL_FUNCTION_CODE (fndecl);
1747 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1748 return false;
1749
1750 blck_size = gimple_call_arg (stmt, size_arg);
1751 if (TREE_CODE (blck_size) == INTEGER_CST)
1752 return false;
1753
1754 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
1755 if (!histogram)
1756 return false;
1757 val = histogram->hvalue.counters[0];
1758 count = histogram->hvalue.counters[1];
1759 all = histogram->hvalue.counters[2];
1760 gimple_remove_histogram_value (cfun, stmt, histogram);
1761 /* We require that count is at least half of all; this means
1762 that for the transformation to fire the value must be constant
1763 at least 80% of time. */
1764 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
1765 return false;
1766 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
1767 return false;
1768 if (all > 0)
1769 prob = GCOV_COMPUTE_SCALE (count, all);
1770 else
1771 prob = 0;
1772 dest = gimple_call_arg (stmt, 0);
1773 dest_align = get_pointer_alignment (dest);
1774 switch (fcode)
1775 {
1776 case BUILT_IN_MEMCPY:
1777 case BUILT_IN_MEMPCPY:
1778 src = gimple_call_arg (stmt, 1);
1779 src_align = get_pointer_alignment (src);
1780 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1781 return false;
1782 break;
1783 case BUILT_IN_MEMSET:
1784 if (!can_store_by_pieces (val, builtin_memset_read_str,
1785 gimple_call_arg (stmt, 1),
1786 dest_align, true))
1787 return false;
1788 break;
1789 case BUILT_IN_BZERO:
1790 if (!can_store_by_pieces (val, builtin_memset_read_str,
1791 integer_zero_node,
1792 dest_align, true))
1793 return false;
1794 break;
1795 default:
1796 gcc_unreachable ();
1797 }
1798 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1799 tree_val = build_int_cst (get_gcov_type (), val);
1800 else
1801 {
1802 HOST_WIDE_INT a[2];
1803 a[0] = (unsigned HOST_WIDE_INT) val;
1804 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
1805
1806 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1807 TYPE_PRECISION (get_gcov_type ()), false));
1808 }
1809
1810 if (dump_file)
1811 {
1812 fprintf (dump_file, "Single value %i stringop transformation on ",
1813 (int)val);
1814 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1815 }
1816 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
1817
1818 return true;
1819 }
1820
1821 void
1822 stringop_block_profile (gimple stmt, unsigned int *expected_align,
1823 HOST_WIDE_INT *expected_size)
1824 {
1825 histogram_value histogram;
1826 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
1827 if (!histogram)
1828 *expected_size = -1;
1829 else if (!histogram->hvalue.counters[1])
1830 {
1831 *expected_size = -1;
1832 gimple_remove_histogram_value (cfun, stmt, histogram);
1833 }
1834 else
1835 {
1836 gcov_type size;
1837 size = ((histogram->hvalue.counters[0]
1838 + histogram->hvalue.counters[1] / 2)
1839 / histogram->hvalue.counters[1]);
1840 /* Even if we can hold bigger value in SIZE, INT_MAX
1841 is safe "infinity" for code generation strategies. */
1842 if (size > INT_MAX)
1843 size = INT_MAX;
1844 *expected_size = size;
1845 gimple_remove_histogram_value (cfun, stmt, histogram);
1846 }
1847 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
1848 if (!histogram)
1849 *expected_align = 0;
1850 else if (!histogram->hvalue.counters[0])
1851 {
1852 gimple_remove_histogram_value (cfun, stmt, histogram);
1853 *expected_align = 0;
1854 }
1855 else
1856 {
1857 gcov_type count;
1858 int alignment;
1859
1860 count = histogram->hvalue.counters[0];
1861 alignment = 1;
1862 while (!(count & alignment)
1863 && (alignment * 2 * BITS_PER_UNIT))
1864 alignment <<= 1;
1865 *expected_align = alignment * BITS_PER_UNIT;
1866 gimple_remove_histogram_value (cfun, stmt, histogram);
1867 }
1868 }
1869
1870 \f
1871 /* Find values inside STMT for that we want to measure histograms for
1872 division/modulo optimization. */
1873 static void
1874 gimple_divmod_values_to_profile (gimple stmt, histogram_values *values)
1875 {
1876 tree lhs, divisor, op0, type;
1877 histogram_value hist;
1878
1879 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1880 return;
1881
1882 lhs = gimple_assign_lhs (stmt);
1883 type = TREE_TYPE (lhs);
1884 if (!INTEGRAL_TYPE_P (type))
1885 return;
1886
1887 switch (gimple_assign_rhs_code (stmt))
1888 {
1889 case TRUNC_DIV_EXPR:
1890 case TRUNC_MOD_EXPR:
1891 divisor = gimple_assign_rhs2 (stmt);
1892 op0 = gimple_assign_rhs1 (stmt);
1893
1894 values->reserve (3);
1895
1896 if (TREE_CODE (divisor) == SSA_NAME)
1897 /* Check for the case where the divisor is the same value most
1898 of the time. */
1899 values->quick_push (gimple_alloc_histogram_value (cfun,
1900 HIST_TYPE_SINGLE_VALUE,
1901 stmt, divisor));
1902
1903 /* For mod, check whether it is not often a noop (or replaceable by
1904 a few subtractions). */
1905 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
1906 && TYPE_UNSIGNED (type))
1907 {
1908 tree val;
1909 /* Check for a special case where the divisor is power of 2. */
1910 values->quick_push (gimple_alloc_histogram_value (cfun,
1911 HIST_TYPE_POW2,
1912 stmt, divisor));
1913
1914 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1915 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1916 stmt, val);
1917 hist->hdata.intvl.int_start = 0;
1918 hist->hdata.intvl.steps = 2;
1919 values->quick_push (hist);
1920 }
1921 return;
1922
1923 default:
1924 return;
1925 }
1926 }
1927
1928 /* Find calls inside STMT for that we want to measure histograms for
1929 indirect/virtual call optimization. */
1930
1931 static void
1932 gimple_indirect_call_to_profile (gimple stmt, histogram_values *values)
1933 {
1934 tree callee;
1935
1936 if (gimple_code (stmt) != GIMPLE_CALL
1937 || gimple_call_internal_p (stmt)
1938 || gimple_call_fndecl (stmt) != NULL_TREE)
1939 return;
1940
1941 callee = gimple_call_fn (stmt);
1942
1943 values->reserve (3);
1944
1945 values->quick_push (gimple_alloc_histogram_value (
1946 cfun,
1947 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1948 HIST_TYPE_INDIR_CALL_TOPN :
1949 HIST_TYPE_INDIR_CALL,
1950 stmt, callee));
1951
1952 return;
1953 }
1954
1955 /* Find values inside STMT for that we want to measure histograms for
1956 string operations. */
1957 static void
1958 gimple_stringops_values_to_profile (gimple stmt, histogram_values *values)
1959 {
1960 tree fndecl;
1961 tree blck_size;
1962 tree dest;
1963 int size_arg;
1964
1965 if (gimple_code (stmt) != GIMPLE_CALL)
1966 return;
1967 fndecl = gimple_call_fndecl (stmt);
1968 if (!fndecl)
1969 return;
1970
1971 if (!interesting_stringop_to_profile_p (fndecl, stmt, &size_arg))
1972 return;
1973
1974 dest = gimple_call_arg (stmt, 0);
1975 blck_size = gimple_call_arg (stmt, size_arg);
1976
1977 if (TREE_CODE (blck_size) != INTEGER_CST)
1978 {
1979 values->safe_push (gimple_alloc_histogram_value (cfun,
1980 HIST_TYPE_SINGLE_VALUE,
1981 stmt, blck_size));
1982 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
1983 stmt, blck_size));
1984 }
1985 if (TREE_CODE (blck_size) != INTEGER_CST)
1986 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
1987 stmt, dest));
1988 }
1989
1990 /* Find values inside STMT for that we want to measure histograms and adds
1991 them to list VALUES. */
1992
1993 static void
1994 gimple_values_to_profile (gimple stmt, histogram_values *values)
1995 {
1996 gimple_divmod_values_to_profile (stmt, values);
1997 gimple_stringops_values_to_profile (stmt, values);
1998 gimple_indirect_call_to_profile (stmt, values);
1999 }
2000
2001 void
2002 gimple_find_values_to_profile (histogram_values *values)
2003 {
2004 basic_block bb;
2005 gimple_stmt_iterator gsi;
2006 unsigned i;
2007 histogram_value hist = NULL;
2008 values->create (0);
2009
2010 FOR_EACH_BB_FN (bb, cfun)
2011 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2012 gimple_values_to_profile (gsi_stmt (gsi), values);
2013
2014 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2015
2016 FOR_EACH_VEC_ELT (*values, i, hist)
2017 {
2018 switch (hist->type)
2019 {
2020 case HIST_TYPE_INTERVAL:
2021 hist->n_counters = hist->hdata.intvl.steps + 2;
2022 break;
2023
2024 case HIST_TYPE_POW2:
2025 hist->n_counters = 2;
2026 break;
2027
2028 case HIST_TYPE_SINGLE_VALUE:
2029 hist->n_counters = 3;
2030 break;
2031
2032 case HIST_TYPE_CONST_DELTA:
2033 hist->n_counters = 4;
2034 break;
2035
2036 case HIST_TYPE_INDIR_CALL:
2037 hist->n_counters = 3;
2038 break;
2039
2040 case HIST_TYPE_TIME_PROFILE:
2041 hist->n_counters = 1;
2042 break;
2043
2044 case HIST_TYPE_AVERAGE:
2045 hist->n_counters = 2;
2046 break;
2047
2048 case HIST_TYPE_IOR:
2049 hist->n_counters = 1;
2050 break;
2051
2052 case HIST_TYPE_INDIR_CALL_TOPN:
2053 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2054 break;
2055
2056 default:
2057 gcc_unreachable ();
2058 }
2059 if (dump_file)
2060 {
2061 fprintf (dump_file, "Stmt ");
2062 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
2063 dump_histogram_value (dump_file, hist);
2064 }
2065 }
2066 }