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