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