]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/value-prof.c
Update copyright years.
[thirdparty/gcc.git] / gcc / value-prof.c
CommitLineData
d81602c4 1/* Transformations based on profile information for values.
f1717362 2 Copyright (C) 2003-2016 Free Software Foundation, Inc.
d81602c4 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
d81602c4 9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
d81602c4 19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
9ef16211 23#include "backend.h"
7c29e30e 24#include "rtl.h"
41a8aa41 25#include "tree.h"
9ef16211 26#include "gimple.h"
7c29e30e 27#include "cfghooks.h"
9ef16211 28#include "ssa.h"
7c29e30e 29#include "cgraph.h"
30#include "coverage.h"
31#include "data-streamer.h"
32#include "diagnostic.h"
b20a8bb4 33#include "fold-const.h"
9ed99284 34#include "tree-nested.h"
35#include "calls.h"
d53441c8 36#include "expr.h"
d81602c4 37#include "value-prof.h"
bc61cadb 38#include "tree-eh.h"
a8783bee 39#include "gimplify.h"
dcf1a1ec 40#include "gimple-iterator.h"
073c1fd5 41#include "tree-cfg.h"
ce084dfc 42#include "gimple-pretty-print.h"
b9ed1410 43#include "dumpfile.h"
e797f49f 44#include "builtins.h"
b74245ec 45#include "params.h"
058a1b7a 46#include "tree-chkp.h"
d81602c4 47
8a5df2ce 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
597ff315 52 1) Division/modulo specialization. Provided that we can determine that the
8a5df2ce 53 operands of the division have some special properties, we may use it to
54 produce more effective code.
d81602c4 55
3e7f455b 56 2) Indirect/virtual call specialization. If we can determine most
167b550b 57 common function callee in indirect/virtual call. We can use this
0d424440 58 information to improve code effectiveness (especially info for
3e7f455b 59 the inliner).
167b550b 60
3e7f455b 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.
d2971487 66
d81602c4 67
3e7f455b 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()
f1f41a6c 75 collects the values to profile in a vec, and adds the number of counters
3e7f455b 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*/
ed4294da 112
1a91d914 113static tree gimple_divmod_fixed_value (gassign *, tree, int, gcov_type,
114 gcov_type);
115static tree gimple_mod_pow2 (gassign *, int, gcov_type, gcov_type);
116static tree gimple_mod_subtract (gassign *, int, int, int, gcov_type,
117 gcov_type, gcov_type);
75a70cf9 118static bool gimple_divmod_fixed_value_transform (gimple_stmt_iterator *);
119static bool gimple_mod_pow2_value_transform (gimple_stmt_iterator *);
120static bool gimple_mod_subtract_transform (gimple_stmt_iterator *);
121static bool gimple_stringops_transform (gimple_stmt_iterator *);
3e7f455b 122static bool gimple_ic_transform (gimple_stmt_iterator *);
d2971487 123
4992f399 124/* Allocate histogram value. */
125
94bed7c3 126histogram_value
4992f399 127gimple_alloc_histogram_value (struct function *fun ATTRIBUTE_UNUSED,
42acab1c 128 enum hist_type type, gimple *stmt, tree value)
4992f399 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
139static hashval_t
140histogram_hash (const void *x)
141{
c1fdef8e 142 return htab_hash_pointer (((const_histogram_value)x)->hvalue.stmt);
4992f399 143}
144
88c5a1d1 145/* Return nonzero if statement for histogram_value X is Y. */
4992f399 146
147static int
148histogram_eq (const void *x, const void *y)
149{
42acab1c 150 return ((const_histogram_value) x)->hvalue.stmt == (const gimple *) y;
4992f399 151}
152
153/* Set histogram for STMT. */
154
155static void
42acab1c 156set_histogram_value (struct function *fun, gimple *stmt, histogram_value hist)
4992f399 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
178histogram_value
42acab1c 179gimple_histogram_value (struct function *fun, gimple *stmt)
4992f399 180{
181 if (!VALUE_HISTOGRAMS (fun))
182 return NULL;
45ba1503 183 return (histogram_value) htab_find_with_hash (VALUE_HISTOGRAMS (fun), stmt,
184 htab_hash_pointer (stmt));
4992f399 185}
186
187/* Add histogram for STMT. */
188
189void
42acab1c 190gimple_add_histogram_value (struct function *fun, gimple *stmt,
75a70cf9 191 histogram_value hist)
4992f399 192{
193 hist->hvalue.next = gimple_histogram_value (fun, stmt);
194 set_histogram_value (fun, stmt, hist);
38fe12e3 195 hist->fun = fun;
4992f399 196}
197
198/* Remove histogram HIST from STMT's histogram list. */
199
200void
42acab1c 201gimple_remove_histogram_value (struct function *fun, gimple *stmt,
75a70cf9 202 histogram_value hist)
4992f399 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);
382ecba7 216 if (flag_checking)
217 memset (hist, 0xab, sizeof (*hist));
4992f399 218 free (hist);
219}
220
221/* Lookup histogram of type TYPE in the STMT. */
222
223histogram_value
42acab1c 224gimple_histogram_value_of_type (struct function *fun, gimple *stmt,
75a70cf9 225 enum hist_type type)
4992f399 226{
227 histogram_value hist;
75a70cf9 228 for (hist = gimple_histogram_value (fun, stmt); hist;
229 hist = hist->hvalue.next)
4992f399 230 if (hist->type == type)
231 return hist;
232 return NULL;
233}
234
235/* Dump information about HIST to DUMP_FILE. */
236
237static void
238dump_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;
9af5ce0c 250 fprintf (dump_file, " [");
4992f399 251 for (i = 0; i < hist->hdata.intvl.steps; i++)
f03df321 252 fprintf (dump_file, " %d:%" PRId64,
4992f399 253 hist->hdata.intvl.int_start + i,
3a4303e7 254 (int64_t) hist->hvalue.counters[i]);
f03df321 255 fprintf (dump_file, " ] outside range:%" PRId64,
3a4303e7 256 (int64_t) hist->hvalue.counters[i]);
4992f399 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 {
f03df321 265 fprintf (dump_file, "pow2:%" PRId64
266 " nonpow2:%" PRId64,
3a4303e7 267 (int64_t) hist->hvalue.counters[0],
268 (int64_t) hist->hvalue.counters[1]);
4992f399 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 {
f03df321 277 fprintf (dump_file, "value:%" PRId64
278 " match:%" PRId64
279 " wrong:%" PRId64,
3a4303e7 280 (int64_t) hist->hvalue.counters[0],
281 (int64_t) hist->hvalue.counters[1],
282 (int64_t) hist->hvalue.counters[2]);
4992f399 283 }
284 fprintf (dump_file, ".\n");
285 break;
286
162719b3 287 case HIST_TYPE_AVERAGE:
288 fprintf (dump_file, "Average value ");
289 if (hist->hvalue.counters)
290 {
f03df321 291 fprintf (dump_file, "sum:%" PRId64
292 " times:%" PRId64,
3a4303e7 293 (int64_t) hist->hvalue.counters[0],
294 (int64_t) hist->hvalue.counters[1]);
162719b3 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 {
f03df321 303 fprintf (dump_file, "ior:%" PRId64,
3a4303e7 304 (int64_t) hist->hvalue.counters[0]);
162719b3 305 }
306 fprintf (dump_file, ".\n");
307 break;
308
4992f399 309 case HIST_TYPE_CONST_DELTA:
310 fprintf (dump_file, "Constant delta ");
311 if (hist->hvalue.counters)
312 {
f03df321 313 fprintf (dump_file, "value:%" PRId64
314 " match:%" PRId64
315 " wrong:%" PRId64,
3a4303e7 316 (int64_t) hist->hvalue.counters[0],
317 (int64_t) hist->hvalue.counters[1],
318 (int64_t) hist->hvalue.counters[2]);
4992f399 319 }
320 fprintf (dump_file, ".\n");
321 break;
167b550b 322 case HIST_TYPE_INDIR_CALL:
323 fprintf (dump_file, "Indirect call ");
324 if (hist->hvalue.counters)
325 {
f03df321 326 fprintf (dump_file, "value:%" PRId64
327 " match:%" PRId64
328 " all:%" PRId64,
3a4303e7 329 (int64_t) hist->hvalue.counters[0],
330 (int64_t) hist->hvalue.counters[1],
331 (int64_t) hist->hvalue.counters[2]);
167b550b 332 }
333 fprintf (dump_file, ".\n");
334 break;
38fe12e3 335 case HIST_TYPE_TIME_PROFILE:
336 fprintf (dump_file, "Time profile ");
337 if (hist->hvalue.counters)
338 {
f03df321 339 fprintf (dump_file, "time:%" PRId64,
3a4303e7 340 (int64_t) hist->hvalue.counters[0]);
38fe12e3 341 }
342 fprintf (dump_file, ".\n");
343 break;
b74245ec 344 case HIST_TYPE_INDIR_CALL_TOPN:
345 fprintf (dump_file, "Indirect call topn ");
346 if (hist->hvalue.counters)
347 {
348 int i;
349
f03df321 350 fprintf (dump_file, "accu:%" PRId64, hist->hvalue.counters[0]);
b74245ec 351 for (i = 1; i < (GCOV_ICALL_TOPN_VAL << 2); i += 2)
352 {
f03df321 353 fprintf (dump_file, " target:%" PRId64 " value:%" PRId64,
b74245ec 354 (int64_t) hist->hvalue.counters[i],
355 (int64_t) hist->hvalue.counters[i+1]);
356 }
357 }
358 fprintf (dump_file, ".\n");
359 break;
fc44a215 360 case HIST_TYPE_MAX:
361 gcc_unreachable ();
4992f399 362 }
363}
364
fc44a215 365/* Dump information about HIST to DUMP_FILE. */
366
367void
368stream_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}
357e29f4 391
fc44a215 392/* Dump information about HIST to DUMP_FILE. */
393
394void
42acab1c 395stream_in_histogram_value (struct lto_input_block *ib, gimple *stmt)
fc44a215 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:
b74245ec 434 case HIST_TYPE_TIME_PROFILE:
fc44a215 435 ncounters = 1;
436 break;
b74245ec 437
438 case HIST_TYPE_INDIR_CALL_TOPN:
439 ncounters = (GCOV_ICALL_TOPN_VAL << 2) + 1;
440 break;
441
fc44a215 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);
fc44a215 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
4992f399 458/* Dump all histograms attached to STMT to DUMP_FILE. */
459
460void
42acab1c 461dump_histograms_for_stmt (struct function *fun, FILE *dump_file, gimple *stmt)
4992f399 462{
463 histogram_value hist;
464 for (hist = gimple_histogram_value (fun, stmt); hist; hist = hist->hvalue.next)
3e7f455b 465 dump_histogram_value (dump_file, hist);
4992f399 466}
467
468/* Remove all histograms associated with STMT. */
469
470void
42acab1c 471gimple_remove_stmt_histograms (struct function *fun, gimple *stmt)
4992f399 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
480void
42acab1c 481gimple_duplicate_stmt_histograms (struct function *fun, gimple *stmt,
482 struct function *ofun, gimple *ostmt)
4992f399 483{
484 histogram_value val;
485 for (val = gimple_histogram_value (ofun, ostmt); val != NULL; val = val->hvalue.next)
486 {
f4e36c33 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);
4992f399 493 }
494}
495
69eee21d 496/* Move all histograms associated with OSTMT to STMT. */
497
498void
42acab1c 499gimple_move_stmt_histograms (struct function *fun, gimple *stmt, gimple *ostmt)
69eee21d 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
4992f399 514static 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
519static int
520visit_hist (void **slot, void *data)
521{
431205b7 522 hash_set<histogram_value> *visited = (hash_set<histogram_value> *) data;
4992f399 523 histogram_value hist = *(histogram_value *) slot;
38fe12e3 524
431205b7 525 if (!visited->contains (hist)
38fe12e3 526 && hist->type != HIST_TYPE_TIME_PROFILE)
4992f399 527 {
bf776685 528 error ("dead histogram");
4992f399 529 dump_histogram_value (stderr, hist);
75a70cf9 530 debug_gimple_stmt (hist->hvalue.stmt);
4992f399 531 error_found = true;
532 }
a63f3f1d 533 return 1;
4992f399 534}
535
536/* Verify sanity of the histograms. */
537
4b987fac 538DEBUG_FUNCTION void
4992f399 539verify_histograms (void)
540{
541 basic_block bb;
75a70cf9 542 gimple_stmt_iterator gsi;
4992f399 543 histogram_value hist;
4992f399 544
545 error_found = false;
431205b7 546 hash_set<histogram_value> visited_hists;
fc00614f 547 FOR_EACH_BB_FN (bb, cfun)
75a70cf9 548 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4992f399 549 {
42acab1c 550 gimple *stmt = gsi_stmt (gsi);
4992f399 551
75a70cf9 552 for (hist = gimple_histogram_value (cfun, stmt); hist;
553 hist = hist->hvalue.next)
4992f399 554 {
555 if (hist->hvalue.stmt != stmt)
556 {
75a70cf9 557 error ("Histogram value statement does not correspond to "
558 "the statement it is associated with");
559 debug_gimple_stmt (stmt);
4992f399 560 dump_histogram_value (stderr, hist);
561 error_found = true;
562 }
431205b7 563 visited_hists.add (hist);
4992f399 564 }
565 }
566 if (VALUE_HISTOGRAMS (cfun))
431205b7 567 htab_traverse (VALUE_HISTOGRAMS (cfun), visit_hist, &visited_hists);
4992f399 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
575static int
576free_hist (void **slot, void *data ATTRIBUTE_UNUSED)
577{
578 histogram_value hist = *(histogram_value *) slot;
579 free (hist->hvalue.counters);
382ecba7 580 if (flag_checking)
581 memset (hist, 0xab, sizeof (*hist));
4992f399 582 free (hist);
a63f3f1d 583 return 1;
4992f399 584}
585
586void
d4f078b5 587free_histograms (struct function *fn)
4992f399 588{
d4f078b5 589 if (VALUE_HISTOGRAMS (fn))
4992f399 590 {
d4f078b5 591 htab_traverse (VALUE_HISTOGRAMS (fn), free_hist, NULL);
592 htab_delete (VALUE_HISTOGRAMS (fn));
593 VALUE_HISTOGRAMS (fn) = NULL;
4992f399 594 }
595}
596
75a70cf9 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
2ca9602a 602static bool
42acab1c 603check_counter (gimple *stmt, const char * name,
e0dc6f2b 604 gcov_type *count, gcov_type *all, gcov_type bb_count)
2ca9602a 605{
e0dc6f2b 606 if (*all != bb_count || *count > *all)
2ca9602a 607 {
75a70cf9 608 location_t locus;
609 locus = (stmt != NULL)
e0dc6f2b 610 ? gimple_location (stmt)
611 : DECL_SOURCE_LOCATION (current_function_decl);
612 if (flag_profile_correction)
613 {
202c2bd9 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 "
78bb46f5 618 "count (%d)\n", name, (int)*all, (int)bb_count);
e0dc6f2b 619 *all = bb_count;
620 if (*count > *all)
621 *count = *all;
622 return false;
623 }
624 else
625 {
bf776685 626 error_at (locus, "corrupted value profile: %s "
81c388fd 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);
e0dc6f2b 633 return true;
634 }
2ca9602a 635 }
75a70cf9 636
2ca9602a 637 return false;
638}
639
75a70cf9 640/* GIMPLE based transformations. */
641
fc49fbc1 642bool
75a70cf9 643gimple_value_profile_transformations (void)
d2971487 644{
645 basic_block bb;
75a70cf9 646 gimple_stmt_iterator gsi;
d2971487 647 bool changed = false;
fc00614f 648 FOR_EACH_BB_FN (bb, cfun)
d2971487 649 {
75a70cf9 650 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
d2971487 651 {
42acab1c 652 gimple *stmt = gsi_stmt (gsi);
4992f399 653 histogram_value th = gimple_histogram_value (cfun, stmt);
d2971487 654 if (!th)
655 continue;
656
657 if (dump_file)
658 {
4992f399 659 fprintf (dump_file, "Trying transformations on stmt ");
75a70cf9 660 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
4992f399 661 dump_histograms_for_stmt (cfun, dump_file, stmt);
d2971487 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. */
3e7f455b 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))
d2971487 676 {
75a70cf9 677 stmt = gsi_stmt (gsi);
d2971487 678 changed = true;
679 /* Original statement may no longer be in the same block. */
75a70cf9 680 if (bb != gimple_bb (stmt))
e3a1de9d 681 {
75a70cf9 682 bb = gimple_bb (stmt);
683 gsi = gsi_for_stmt (stmt);
e3a1de9d 684 }
d2971487 685 }
d2971487 686 }
687 }
688
689 if (changed)
690 {
691 counts_to_freqs ();
692 }
693
694 return changed;
695}
696
75a70cf9 697/* Generate code for transformation 1 (with parent gimple assignment
698 STMT and probability of taking the optimal path PROB, which is
699 equivalent to COUNT/ALL within roundoff error). This generates the
700 result into a temp and returns the temp; it does not replace or
701 alter the original STMT. */
702
d2971487 703static tree
1a91d914 704gimple_divmod_fixed_value (gassign *stmt, tree value, int prob,
705 gcov_type count, gcov_type all)
d2971487 706{
1a91d914 707 gassign *stmt1, *stmt2;
708 gcond *stmt3;
03d37e4e 709 tree tmp0, tmp1, tmp2;
42acab1c 710 gimple *bb1end, *bb2end, *bb3end;
d2971487 711 basic_block bb, bb2, bb3, bb4;
75a70cf9 712 tree optype, op1, op2;
d2971487 713 edge e12, e13, e23, e24, e34;
75a70cf9 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);
d2971487 723
75a70cf9 724 bb = gimple_bb (stmt);
725 gsi = gsi_for_stmt (stmt);
d2971487 726
03d37e4e 727 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
728 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
85344eeb 729 stmt1 = gimple_build_assign (tmp0, fold_convert (optype, value));
75a70cf9 730 stmt2 = gimple_build_assign (tmp1, op2);
85344eeb 731 stmt3 = gimple_build_cond (NE_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
75a70cf9 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);
d2971487 735 bb1end = stmt3;
736
072f7ab1 737 tmp2 = create_tmp_reg (optype, "PROF");
e9cf809e 738 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, tmp0);
75a70cf9 739 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
d2971487 740 bb2end = stmt1;
741
e9cf809e 742 stmt1 = gimple_build_assign (tmp2, gimple_assign_rhs_code (stmt), op1, op2);
75a70cf9 743 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
d2971487 744 bb3end = stmt1;
745
d2971487 746 /* Fix CFG. */
747 /* Edge e23 connects bb2 to bb3, etc. */
748 e12 = split_block (bb, bb1end);
749 bb2 = e12->dest;
750 bb2->count = count;
751 e23 = split_block (bb2, bb2end);
752 bb3 = e23->dest;
753 bb3->count = all - count;
754 e34 = split_block (bb3, bb3end);
755 bb4 = e34->dest;
756 bb4->count = all;
757
758 e12->flags &= ~EDGE_FALLTHRU;
759 e12->flags |= EDGE_FALSE_VALUE;
760 e12->probability = prob;
761 e12->count = count;
762
763 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
764 e13->probability = REG_BR_PROB_BASE - prob;
765 e13->count = all - count;
766
767 remove_edge (e23);
48e1416a 768
d2971487 769 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
770 e24->probability = REG_BR_PROB_BASE;
771 e24->count = count;
772
773 e34->probability = REG_BR_PROB_BASE;
774 e34->count = all - count;
775
776 return tmp2;
777}
778
779/* Do transform 1) on INSN if applicable. */
75a70cf9 780
d2971487 781static bool
75a70cf9 782gimple_divmod_fixed_value_transform (gimple_stmt_iterator *si)
d2971487 783{
d2971487 784 histogram_value histogram;
785 enum tree_code code;
786 gcov_type val, count, all;
75a70cf9 787 tree result, value, tree_val;
5b17b7ae 788 gcov_type prob;
1a91d914 789 gassign *stmt;
d2971487 790
1a91d914 791 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
792 if (!stmt)
d2971487 793 return false;
75a70cf9 794
795 if (!INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt))))
d2971487 796 return false;
75a70cf9 797
798 code = gimple_assign_rhs_code (stmt);
48e1416a 799
d2971487 800 if (code != TRUNC_DIV_EXPR && code != TRUNC_MOD_EXPR)
801 return false;
802
75a70cf9 803 histogram = gimple_histogram_value_of_type (cfun, stmt,
804 HIST_TYPE_SINGLE_VALUE);
d2971487 805 if (!histogram)
806 return false;
807
ed4294da 808 value = histogram->hvalue.value;
809 val = histogram->hvalue.counters[0];
810 count = histogram->hvalue.counters[1];
811 all = histogram->hvalue.counters[2];
4992f399 812 gimple_remove_histogram_value (cfun, stmt, histogram);
d2971487 813
814 /* We require that count is at least half of all; this means
815 that for the transformation to fire the value must be constant
816 at least 50% of time (and 75% gives the guarantee of usage). */
75a70cf9 817 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
818 || 2 * count < all
0bfd8d5c 819 || optimize_bb_for_size_p (gimple_bb (stmt)))
d2971487 820 return false;
821
e0dc6f2b 822 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
2ca9602a 823 return false;
824
d2971487 825 /* Compute probability of taking the optimal path. */
5b17b7ae 826 if (all > 0)
f9d4b7f4 827 prob = GCOV_COMPUTE_SCALE (count, all);
5b17b7ae 828 else
829 prob = 0;
d2971487 830
e913b5cd 831 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
832 tree_val = build_int_cst (get_gcov_type (), val);
833 else
834 {
835 HOST_WIDE_INT a[2];
836 a[0] = (unsigned HOST_WIDE_INT) val;
837 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
ddb1be65 838
e913b5cd 839 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
840 TYPE_PRECISION (get_gcov_type ()), false));
841 }
75a70cf9 842 result = gimple_divmod_fixed_value (stmt, tree_val, prob, count, all);
d2971487 843
421e19dd 844 if (dump_file)
845 {
846 fprintf (dump_file, "Div/mod by constant ");
847 print_generic_expr (dump_file, value, TDF_SLIM);
848 fprintf (dump_file, "=");
849 print_generic_expr (dump_file, tree_val, TDF_SLIM);
850 fprintf (dump_file, " transformation on insn ");
75a70cf9 851 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
421e19dd 852 }
853
75a70cf9 854 gimple_assign_set_rhs_from_tree (si, result);
4b7a4b97 855 update_stmt (gsi_stmt (*si));
d2971487 856
857 return true;
858}
859
75a70cf9 860/* Generate code for transformation 2 (with parent gimple assign STMT and
861 probability of taking the optimal path PROB, which is equivalent to COUNT/ALL
48e1416a 862 within roundoff error). This generates the result into a temp and returns
d2971487 863 the temp; it does not replace or alter the original STMT. */
357e29f4 864
d2971487 865static tree
1a91d914 866gimple_mod_pow2 (gassign *stmt, int prob, gcov_type count, gcov_type all)
d2971487 867{
1a91d914 868 gassign *stmt1, *stmt2, *stmt3;
869 gcond *stmt4;
03d37e4e 870 tree tmp2, tmp3;
42acab1c 871 gimple *bb1end, *bb2end, *bb3end;
d2971487 872 basic_block bb, bb2, bb3, bb4;
75a70cf9 873 tree optype, op1, op2;
d2971487 874 edge e12, e13, e23, e24, e34;
75a70cf9 875 gimple_stmt_iterator gsi;
876 tree result;
d2971487 877
75a70cf9 878 gcc_assert (is_gimple_assign (stmt)
879 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
d2971487 880
75a70cf9 881 optype = TREE_TYPE (gimple_assign_lhs (stmt));
882 op1 = gimple_assign_rhs1 (stmt);
883 op2 = gimple_assign_rhs2 (stmt);
884
885 bb = gimple_bb (stmt);
886 gsi = gsi_for_stmt (stmt);
887
072f7ab1 888 result = create_tmp_reg (optype, "PROF");
03d37e4e 889 tmp2 = make_temp_ssa_name (optype, NULL, "PROF");
890 tmp3 = make_temp_ssa_name (optype, NULL, "PROF");
e9cf809e 891 stmt2 = gimple_build_assign (tmp2, PLUS_EXPR, op2,
892 build_int_cst (optype, -1));
893 stmt3 = gimple_build_assign (tmp3, BIT_AND_EXPR, tmp2, op2);
75a70cf9 894 stmt4 = gimple_build_cond (NE_EXPR, tmp3, build_int_cst (optype, 0),
895 NULL_TREE, NULL_TREE);
896 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
897 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
898 gsi_insert_before (&gsi, stmt4, GSI_SAME_STMT);
d2971487 899 bb1end = stmt4;
900
75a70cf9 901 /* tmp2 == op2-1 inherited from previous block. */
e9cf809e 902 stmt1 = gimple_build_assign (result, BIT_AND_EXPR, op1, tmp2);
75a70cf9 903 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
d2971487 904 bb2end = stmt1;
905
e9cf809e 906 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
907 op1, op2);
75a70cf9 908 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
d2971487 909 bb3end = stmt1;
910
d2971487 911 /* Fix CFG. */
912 /* Edge e23 connects bb2 to bb3, etc. */
913 e12 = split_block (bb, bb1end);
914 bb2 = e12->dest;
915 bb2->count = count;
916 e23 = split_block (bb2, bb2end);
917 bb3 = e23->dest;
918 bb3->count = all - count;
919 e34 = split_block (bb3, bb3end);
920 bb4 = e34->dest;
921 bb4->count = all;
922
923 e12->flags &= ~EDGE_FALLTHRU;
924 e12->flags |= EDGE_FALSE_VALUE;
925 e12->probability = prob;
926 e12->count = count;
927
928 e13 = make_edge (bb, bb3, EDGE_TRUE_VALUE);
929 e13->probability = REG_BR_PROB_BASE - prob;
930 e13->count = all - count;
931
932 remove_edge (e23);
48e1416a 933
d2971487 934 e24 = make_edge (bb2, bb4, EDGE_FALLTHRU);
935 e24->probability = REG_BR_PROB_BASE;
936 e24->count = count;
937
938 e34->probability = REG_BR_PROB_BASE;
939 e34->count = all - count;
940
941 return result;
942}
943
944/* Do transform 2) on INSN if applicable. */
357e29f4 945
d2971487 946static bool
75a70cf9 947gimple_mod_pow2_value_transform (gimple_stmt_iterator *si)
d2971487 948{
d2971487 949 histogram_value histogram;
950 enum tree_code code;
951 gcov_type count, wrong_values, all;
75a70cf9 952 tree lhs_type, result, value;
5b17b7ae 953 gcov_type prob;
1a91d914 954 gassign *stmt;
d2971487 955
1a91d914 956 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
957 if (!stmt)
d2971487 958 return false;
75a70cf9 959
960 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
961 if (!INTEGRAL_TYPE_P (lhs_type))
d2971487 962 return false;
75a70cf9 963
964 code = gimple_assign_rhs_code (stmt);
48e1416a 965
75a70cf9 966 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
d2971487 967 return false;
968
4992f399 969 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_POW2);
d2971487 970 if (!histogram)
971 return false;
972
ed4294da 973 value = histogram->hvalue.value;
974 wrong_values = histogram->hvalue.counters[0];
975 count = histogram->hvalue.counters[1];
d2971487 976
4992f399 977 gimple_remove_histogram_value (cfun, stmt, histogram);
978
d2971487 979 /* We require that we hit a power of 2 at least half of all evaluations. */
75a70cf9 980 if (simple_cst_equal (gimple_assign_rhs2 (stmt), value) != 1
981 || count < wrong_values
0bfd8d5c 982 || optimize_bb_for_size_p (gimple_bb (stmt)))
d2971487 983 return false;
984
985 if (dump_file)
986 {
987 fprintf (dump_file, "Mod power of 2 transformation on insn ");
75a70cf9 988 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
d2971487 989 }
990
991 /* Compute probability of taking the optimal path. */
992 all = count + wrong_values;
4992f399 993
e0dc6f2b 994 if (check_counter (stmt, "pow2", &count, &all, gimple_bb (stmt)->count))
2ca9602a 995 return false;
996
5b17b7ae 997 if (all > 0)
f9d4b7f4 998 prob = GCOV_COMPUTE_SCALE (count, all);
5b17b7ae 999 else
1000 prob = 0;
d2971487 1001
75a70cf9 1002 result = gimple_mod_pow2 (stmt, prob, count, all);
d2971487 1003
75a70cf9 1004 gimple_assign_set_rhs_from_tree (si, result);
4b7a4b97 1005 update_stmt (gsi_stmt (*si));
d2971487 1006
1007 return true;
1008}
1009
75a70cf9 1010/* Generate code for transformations 3 and 4 (with parent gimple assign STMT, and
1011 NCOUNTS the number of cases to support. Currently only NCOUNTS==0 or 1 is
1012 supported and this is built into this interface. The probabilities of taking
1013 the optimal paths are PROB1 and PROB2, which are equivalent to COUNT1/ALL and
48e1416a 1014 COUNT2/ALL respectively within roundoff error). This generates the
1015 result into a temp and returns the temp; it does not replace or alter
d2971487 1016 the original STMT. */
1017/* FIXME: Generalize the interface to handle NCOUNTS > 1. */
1018
1019static tree
1a91d914 1020gimple_mod_subtract (gassign *stmt, int prob1, int prob2, int ncounts,
75a70cf9 1021 gcov_type count1, gcov_type count2, gcov_type all)
d2971487 1022{
1a91d914 1023 gassign *stmt1;
42acab1c 1024 gimple *stmt2;
1a91d914 1025 gcond *stmt3;
d2971487 1026 tree tmp1;
42acab1c 1027 gimple *bb1end, *bb2end = NULL, *bb3end;
d2971487 1028 basic_block bb, bb2, bb3, bb4;
75a70cf9 1029 tree optype, op1, op2;
d2971487 1030 edge e12, e23 = 0, e24, e34, e14;
75a70cf9 1031 gimple_stmt_iterator gsi;
1032 tree result;
1033
1034 gcc_assert (is_gimple_assign (stmt)
1035 && gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR);
d2971487 1036
75a70cf9 1037 optype = TREE_TYPE (gimple_assign_lhs (stmt));
1038 op1 = gimple_assign_rhs1 (stmt);
1039 op2 = gimple_assign_rhs2 (stmt);
d2971487 1040
75a70cf9 1041 bb = gimple_bb (stmt);
1042 gsi = gsi_for_stmt (stmt);
1043
072f7ab1 1044 result = create_tmp_reg (optype, "PROF");
03d37e4e 1045 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
75a70cf9 1046 stmt1 = gimple_build_assign (result, op1);
1047 stmt2 = gimple_build_assign (tmp1, op2);
1048 stmt3 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
1049 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1050 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
1051 gsi_insert_before (&gsi, stmt3, GSI_SAME_STMT);
d2971487 1052 bb1end = stmt3;
1053
1054 if (ncounts) /* Assumed to be 0 or 1 */
1055 {
e9cf809e 1056 stmt1 = gimple_build_assign (result, MINUS_EXPR, result, tmp1);
75a70cf9 1057 stmt2 = gimple_build_cond (LT_EXPR, result, tmp1, NULL_TREE, NULL_TREE);
75a70cf9 1058 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
1059 gsi_insert_before (&gsi, stmt2, GSI_SAME_STMT);
d2971487 1060 bb2end = stmt2;
1061 }
1062
1063 /* Fallback case. */
e9cf809e 1064 stmt1 = gimple_build_assign (result, gimple_assign_rhs_code (stmt),
1065 result, tmp1);
75a70cf9 1066 gsi_insert_before (&gsi, stmt1, GSI_SAME_STMT);
d2971487 1067 bb3end = stmt1;
1068
d2971487 1069 /* Fix CFG. */
1070 /* Edge e23 connects bb2 to bb3, etc. */
1071 /* However block 3 is optional; if it is not there, references
1072 to 3 really refer to block 2. */
1073 e12 = split_block (bb, bb1end);
1074 bb2 = e12->dest;
1075 bb2->count = all - count1;
48e1416a 1076
d2971487 1077 if (ncounts) /* Assumed to be 0 or 1. */
1078 {
1079 e23 = split_block (bb2, bb2end);
1080 bb3 = e23->dest;
1081 bb3->count = all - count1 - count2;
1082 }
1083
1084 e34 = split_block (ncounts ? bb3 : bb2, bb3end);
1085 bb4 = e34->dest;
1086 bb4->count = all;
1087
1088 e12->flags &= ~EDGE_FALLTHRU;
1089 e12->flags |= EDGE_FALSE_VALUE;
1090 e12->probability = REG_BR_PROB_BASE - prob1;
421e19dd 1091 e12->count = all - count1;
d2971487 1092
1093 e14 = make_edge (bb, bb4, EDGE_TRUE_VALUE);
1094 e14->probability = prob1;
421e19dd 1095 e14->count = count1;
d2971487 1096
1097 if (ncounts) /* Assumed to be 0 or 1. */
1098 {
1099 e23->flags &= ~EDGE_FALLTHRU;
1100 e23->flags |= EDGE_FALSE_VALUE;
1101 e23->count = all - count1 - count2;
1102 e23->probability = REG_BR_PROB_BASE - prob2;
1103
1104 e24 = make_edge (bb2, bb4, EDGE_TRUE_VALUE);
1105 e24->probability = prob2;
1106 e24->count = count2;
1107 }
1108
1109 e34->probability = REG_BR_PROB_BASE;
1110 e34->count = all - count1 - count2;
1111
1112 return result;
1113}
1114
75a70cf9 1115/* Do transforms 3) and 4) on the statement pointed-to by SI if applicable. */
1116
d2971487 1117static bool
75a70cf9 1118gimple_mod_subtract_transform (gimple_stmt_iterator *si)
d2971487 1119{
d2971487 1120 histogram_value histogram;
1121 enum tree_code code;
1122 gcov_type count, wrong_values, all;
f018d957 1123 tree lhs_type, result;
5b17b7ae 1124 gcov_type prob1, prob2;
4992f399 1125 unsigned int i, steps;
1126 gcov_type count1, count2;
1a91d914 1127 gassign *stmt;
1a91d914 1128 stmt = dyn_cast <gassign *> (gsi_stmt (*si));
1129 if (!stmt)
d2971487 1130 return false;
75a70cf9 1131
1132 lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
1133 if (!INTEGRAL_TYPE_P (lhs_type))
d2971487 1134 return false;
75a70cf9 1135
1136 code = gimple_assign_rhs_code (stmt);
48e1416a 1137
75a70cf9 1138 if (code != TRUNC_MOD_EXPR || !TYPE_UNSIGNED (lhs_type))
d2971487 1139 return false;
1140
4992f399 1141 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INTERVAL);
d2971487 1142 if (!histogram)
1143 return false;
1144
d2971487 1145 all = 0;
1146 wrong_values = 0;
1147 for (i = 0; i < histogram->hdata.intvl.steps; i++)
ed4294da 1148 all += histogram->hvalue.counters[i];
d2971487 1149
ed4294da 1150 wrong_values += histogram->hvalue.counters[i];
1151 wrong_values += histogram->hvalue.counters[i+1];
4992f399 1152 steps = histogram->hdata.intvl.steps;
d2971487 1153 all += wrong_values;
4992f399 1154 count1 = histogram->hvalue.counters[0];
1155 count2 = histogram->hvalue.counters[1];
d2971487 1156
2ca9602a 1157 /* Compute probability of taking the optimal path. */
e0dc6f2b 1158 if (check_counter (stmt, "interval", &count1, &all, gimple_bb (stmt)->count))
4992f399 1159 {
1160 gimple_remove_histogram_value (cfun, stmt, histogram);
1161 return false;
1162 }
2ca9602a 1163
e0dc6f2b 1164 if (flag_profile_correction && count1 + count2 > all)
1165 all = count1 + count2;
1166
1167 gcc_assert (count1 + count2 <= all);
1168
d2971487 1169 /* We require that we use just subtractions in at least 50% of all
1170 evaluations. */
1171 count = 0;
1172 for (i = 0; i < histogram->hdata.intvl.steps; i++)
1173 {
ed4294da 1174 count += histogram->hvalue.counters[i];
d2971487 1175 if (count * 2 >= all)
1176 break;
1177 }
4992f399 1178 if (i == steps
0bfd8d5c 1179 || optimize_bb_for_size_p (gimple_bb (stmt)))
d2971487 1180 return false;
1181
4992f399 1182 gimple_remove_histogram_value (cfun, stmt, histogram);
d2971487 1183 if (dump_file)
1184 {
1185 fprintf (dump_file, "Mod subtract transformation on insn ");
75a70cf9 1186 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
d2971487 1187 }
1188
1189 /* Compute probability of taking the optimal path(s). */
5b17b7ae 1190 if (all > 0)
1191 {
f9d4b7f4 1192 prob1 = GCOV_COMPUTE_SCALE (count1, all);
1193 prob2 = GCOV_COMPUTE_SCALE (count2, all);
5b17b7ae 1194 }
1195 else
1196 {
1197 prob1 = prob2 = 0;
1198 }
d2971487 1199
1200 /* In practice, "steps" is always 2. This interface reflects this,
1201 and will need to be changed if "steps" can change. */
75a70cf9 1202 result = gimple_mod_subtract (stmt, prob1, prob2, i, count1, count2, all);
d2971487 1203
75a70cf9 1204 gimple_assign_set_rhs_from_tree (si, result);
4b7a4b97 1205 update_stmt (gsi_stmt (*si));
d2971487 1206
1207 return true;
1208}
ed4294da 1209
613732c1 1210typedef int_hash <unsigned int, 0, UINT_MAX> profile_id_hash;
5f8841a5 1211
ee34b0e4 1212static hash_map<profile_id_hash, cgraph_node *> *cgraph_node_map = 0;
d9e50003 1213
1214/* Returns true if node graph is initialized. This
1215 is used to test if profile_id has been created
1216 for cgraph_nodes. */
1217
1218bool
1219coverage_node_map_initialized_p (void)
1220{
1221 return cgraph_node_map != 0;
1222}
167b550b 1223
fe37be54 1224/* Initialize map from PROFILE_ID to CGRAPH_NODE.
1225 When LOCAL is true, the PROFILE_IDs are computed. when it is false we assume
1226 that the PROFILE_IDs was already assigned. */
167b550b 1227
1ad3e14c 1228void
fe37be54 1229init_node_map (bool local)
167b550b 1230{
1231 struct cgraph_node *n;
ee34b0e4 1232 cgraph_node_map = new hash_map<profile_id_hash, cgraph_node *>;
167b550b 1233
fe37be54 1234 FOR_EACH_DEFINED_FUNCTION (n)
d9e50003 1235 if (n->has_gimple_body_p ())
fe37be54 1236 {
5f8841a5 1237 cgraph_node **val;
fe37be54 1238 if (local)
1239 {
1240 n->profile_id = coverage_compute_profile_id (n);
5f8841a5 1241 while ((val = cgraph_node_map->get (n->profile_id))
fe37be54 1242 || !n->profile_id)
1243 {
1244 if (dump_file)
1245 fprintf (dump_file, "Local profile-id %i conflict"
1246 " with nodes %s/%i %s/%i\n",
1247 n->profile_id,
f1c8b4d7 1248 n->name (),
02774f2d 1249 n->order,
5f8841a5 1250 (*val)->name (),
1251 (*val)->order);
fe37be54 1252 n->profile_id = (n->profile_id + 1) & 0x7fffffff;
1253 }
1254 }
1255 else if (!n->profile_id)
1256 {
1257 if (dump_file)
1258 fprintf (dump_file,
1259 "Node %s/%i has no profile-id"
1260 " (profile feedback missing?)\n",
f1c8b4d7 1261 n->name (),
02774f2d 1262 n->order);
fe37be54 1263 continue;
1264 }
5f8841a5 1265 else if ((val = cgraph_node_map->get (n->profile_id)))
fe37be54 1266 {
1267 if (dump_file)
1268 fprintf (dump_file,
1269 "Node %s/%i has IP profile-id %i conflict. "
1270 "Giving up.\n",
f1c8b4d7 1271 n->name (),
02774f2d 1272 n->order,
fe37be54 1273 n->profile_id);
1274 *val = NULL;
1275 continue;
1276 }
5f8841a5 1277 cgraph_node_map->put (n->profile_id, n);
fe37be54 1278 }
167b550b 1279}
1280
1ad3e14c 1281/* Delete the CGRAPH_NODE_MAP. */
1282
1283void
1284del_node_map (void)
1285{
5f8841a5 1286 delete cgraph_node_map;
1ad3e14c 1287}
1288
167b550b 1289/* Return cgraph node for function with pid */
1290
fe37be54 1291struct cgraph_node*
1292find_func_by_profile_id (int profile_id)
167b550b 1293{
5f8841a5 1294 cgraph_node **val = cgraph_node_map->get (profile_id);
fe37be54 1295 if (val)
5f8841a5 1296 return *val;
fe37be54 1297 else
1298 return NULL;
167b550b 1299}
1300
850ff64c 1301/* Perform sanity check on the indirect call target. Due to race conditions,
1302 false function target may be attributed to an indirect call site. If the
1303 call expression type mismatches with the target function's type, expand_call
1304 may ICE. Here we only do very minimal sanity check just to make compiler happy.
1305 Returns true if TARGET is considered ok for call CALL_STMT. */
1306
94bed7c3 1307bool
1a91d914 1308check_ic_target (gcall *call_stmt, struct cgraph_node *target)
850ff64c 1309{
1310 location_t locus;
02774f2d 1311 if (gimple_check_call_matching_types (call_stmt, target->decl, true))
850ff64c 1312 return true;
1313
1314 locus = gimple_location (call_stmt);
202c2bd9 1315 if (dump_enabled_p ())
1316 dump_printf_loc (MSG_MISSED_OPTIMIZATION, locus,
78bb46f5 1317 "Skipping target %s with mismatching types for icall\n",
f1c8b4d7 1318 target->name ());
850ff64c 1319 return false;
1320}
1321
167b550b 1322/* Do transformation
1323
f0b5f617 1324 if (actual_callee_address == address_of_most_common_function/method)
167b550b 1325 do direct call
1326 else
1327 old call
1328 */
1329
1a91d914 1330gcall *
1331gimple_ic (gcall *icall_stmt, struct cgraph_node *direct_call,
75a70cf9 1332 int prob, gcov_type count, gcov_type all)
167b550b 1333{
1a91d914 1334 gcall *dcall_stmt;
1335 gassign *load_stmt;
1336 gcond *cond_stmt;
1337 gcall *iretbnd_stmt = NULL;
03d37e4e 1338 tree tmp0, tmp1, tmp;
25ae05af 1339 basic_block cond_bb, dcall_bb, icall_bb, join_bb = NULL;
167b550b 1340 tree optype = build_pointer_type (void_type_node);
25ae05af 1341 edge e_cd, e_ci, e_di, e_dj = NULL, e_ij;
75a70cf9 1342 gimple_stmt_iterator gsi;
1cd188fc 1343 int lp_nr, dflags;
d27c9b1e 1344 edge e_eh, e;
1345 edge_iterator ei;
1346 gimple_stmt_iterator psi;
167b550b 1347
e38def9c 1348 cond_bb = gimple_bb (icall_stmt);
1349 gsi = gsi_for_stmt (icall_stmt);
167b550b 1350
058a1b7a 1351 if (gimple_call_with_bounds_p (icall_stmt) && gimple_call_lhs (icall_stmt))
1352 iretbnd_stmt = chkp_retbnd_call_by_val (gimple_call_lhs (icall_stmt));
1353
03d37e4e 1354 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1355 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
e38def9c 1356 tmp = unshare_expr (gimple_call_fn (icall_stmt));
85344eeb 1357 load_stmt = gimple_build_assign (tmp0, tmp);
e38def9c 1358 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
75a70cf9 1359
0e49e441 1360 tmp = fold_convert (optype, build_addr (direct_call->decl));
e38def9c 1361 load_stmt = gimple_build_assign (tmp1, tmp);
1362 gsi_insert_before (&gsi, load_stmt, GSI_SAME_STMT);
167b550b 1363
85344eeb 1364 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
e38def9c 1365 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1366
c05c1b41 1367 if (TREE_CODE (gimple_vdef (icall_stmt)) == SSA_NAME)
1368 {
1369 unlink_stmt_vdef (icall_stmt);
1370 release_ssa_name (gimple_vdef (icall_stmt));
1371 }
85344eeb 1372 gimple_set_vdef (icall_stmt, NULL_TREE);
1373 gimple_set_vuse (icall_stmt, NULL_TREE);
1374 update_stmt (icall_stmt);
1a91d914 1375 dcall_stmt = as_a <gcall *> (gimple_copy (icall_stmt));
02774f2d 1376 gimple_call_set_fndecl (dcall_stmt, direct_call->decl);
1377 dflags = flags_from_decl_or_type (direct_call->decl);
1cd188fc 1378 if ((dflags & ECF_NORETURN) != 0)
1379 gimple_call_set_lhs (dcall_stmt, NULL_TREE);
e38def9c 1380 gsi_insert_before (&gsi, dcall_stmt, GSI_SAME_STMT);
167b550b 1381
1382 /* Fix CFG. */
e38def9c 1383 /* Edge e_cd connects cond_bb to dcall_bb, etc; note the first letters. */
1384 e_cd = split_block (cond_bb, cond_stmt);
1385 dcall_bb = e_cd->dest;
1386 dcall_bb->count = count;
167b550b 1387
e38def9c 1388 e_di = split_block (dcall_bb, dcall_stmt);
1389 icall_bb = e_di->dest;
1390 icall_bb->count = all - count;
167b550b 1391
955700fa 1392 /* Do not disturb existing EH edges from the indirect call. */
33fb6a59 1393 if (!stmt_ends_bb_p (icall_stmt))
955700fa 1394 e_ij = split_block (icall_bb, icall_stmt);
1395 else
8881852c 1396 {
1397 e_ij = find_fallthru_edge (icall_bb->succs);
25ae05af 1398 /* The indirect call might be noreturn. */
1399 if (e_ij != NULL)
1400 {
1401 e_ij->probability = REG_BR_PROB_BASE;
1402 e_ij->count = all - count;
1403 e_ij = single_pred_edge (split_edge (e_ij));
1404 }
1405 }
1406 if (e_ij != NULL)
1407 {
1408 join_bb = e_ij->dest;
1409 join_bb->count = all;
8881852c 1410 }
167b550b 1411
e38def9c 1412 e_cd->flags = (e_cd->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1413 e_cd->probability = prob;
1414 e_cd->count = count;
1415
1416 e_ci = make_edge (cond_bb, icall_bb, EDGE_FALSE_VALUE);
1417 e_ci->probability = REG_BR_PROB_BASE - prob;
1418 e_ci->count = all - count;
1419
1420 remove_edge (e_di);
48e1416a 1421
25ae05af 1422 if (e_ij != NULL)
1423 {
1cd188fc 1424 if ((dflags & ECF_NORETURN) != 0)
1425 e_ij->count = all;
1426 else
1427 {
1428 e_dj = make_edge (dcall_bb, join_bb, EDGE_FALLTHRU);
1429 e_dj->probability = REG_BR_PROB_BASE;
1430 e_dj->count = count;
e38def9c 1431
1cd188fc 1432 e_ij->count = all - count;
1433 }
25ae05af 1434 e_ij->probability = REG_BR_PROB_BASE;
25ae05af 1435 }
167b550b 1436
85344eeb 1437 /* Insert PHI node for the call result if necessary. */
1438 if (gimple_call_lhs (icall_stmt)
1cd188fc 1439 && TREE_CODE (gimple_call_lhs (icall_stmt)) == SSA_NAME
1440 && (dflags & ECF_NORETURN) == 0)
85344eeb 1441 {
1442 tree result = gimple_call_lhs (icall_stmt);
1a91d914 1443 gphi *phi = create_phi_node (result, join_bb);
85344eeb 1444 gimple_call_set_lhs (icall_stmt,
7ecda5e8 1445 duplicate_ssa_name (result, icall_stmt));
60d535d2 1446 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
85344eeb 1447 gimple_call_set_lhs (dcall_stmt,
7ecda5e8 1448 duplicate_ssa_name (result, dcall_stmt));
60d535d2 1449 add_phi_arg (phi, gimple_call_lhs (dcall_stmt), e_dj, UNKNOWN_LOCATION);
058a1b7a 1450
1451 /* If indirect call has following BUILT_IN_CHKP_BNDRET
1452 call then we need to make it's copy for the direct
1453 call. */
1454 if (iretbnd_stmt)
1455 {
1456 if (gimple_call_lhs (iretbnd_stmt))
1457 {
42acab1c 1458 gimple *copy;
058a1b7a 1459
c05c1b41 1460 if (TREE_CODE (gimple_vdef (iretbnd_stmt)) == SSA_NAME)
1461 {
1462 unlink_stmt_vdef (iretbnd_stmt);
1463 release_ssa_name (gimple_vdef (iretbnd_stmt));
1464 }
058a1b7a 1465 gimple_set_vdef (iretbnd_stmt, NULL_TREE);
1466 gimple_set_vuse (iretbnd_stmt, NULL_TREE);
1467 update_stmt (iretbnd_stmt);
1468
1469 result = gimple_call_lhs (iretbnd_stmt);
1470 phi = create_phi_node (result, join_bb);
1471
1472 copy = gimple_copy (iretbnd_stmt);
1473 gimple_call_set_arg (copy, 0,
1474 gimple_call_lhs (dcall_stmt));
1475 gimple_call_set_lhs (copy, duplicate_ssa_name (result, copy));
1476 gsi_insert_on_edge (e_dj, copy);
1477 add_phi_arg (phi, gimple_call_lhs (copy),
1478 e_dj, UNKNOWN_LOCATION);
1479
1480 gimple_call_set_arg (iretbnd_stmt, 0,
1481 gimple_call_lhs (icall_stmt));
1482 gimple_call_set_lhs (iretbnd_stmt,
1483 duplicate_ssa_name (result, iretbnd_stmt));
1484 psi = gsi_for_stmt (iretbnd_stmt);
1485 gsi_remove (&psi, false);
1486 gsi_insert_on_edge (e_ij, iretbnd_stmt);
1487 add_phi_arg (phi, gimple_call_lhs (iretbnd_stmt),
1488 e_ij, UNKNOWN_LOCATION);
1489
1490 gsi_commit_one_edge_insert (e_dj, NULL);
1491 gsi_commit_one_edge_insert (e_ij, NULL);
1492 }
1493 else
1494 {
1495 psi = gsi_for_stmt (iretbnd_stmt);
1496 gsi_remove (&psi, true);
1497 }
1498 }
85344eeb 1499 }
1500
955700fa 1501 /* Build an EH edge for the direct call if necessary. */
e38def9c 1502 lp_nr = lookup_stmt_eh_lp (icall_stmt);
bce9fa89 1503 if (lp_nr > 0 && stmt_could_throw_p (dcall_stmt))
167b550b 1504 {
955700fa 1505 add_stmt_to_eh_lp (dcall_stmt, lp_nr);
167b550b 1506 }
1507
d27c9b1e 1508 FOR_EACH_EDGE (e_eh, ei, icall_bb->succs)
1509 if (e_eh->flags & (EDGE_EH | EDGE_ABNORMAL))
1510 {
1511 e = make_edge (dcall_bb, e_eh->dest, e_eh->flags);
1a91d914 1512 for (gphi_iterator psi = gsi_start_phis (e_eh->dest);
d27c9b1e 1513 !gsi_end_p (psi); gsi_next (&psi))
1514 {
1a91d914 1515 gphi *phi = psi.phi ();
d27c9b1e 1516 SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
1517 PHI_ARG_DEF_FROM_EDGE (phi, e_eh));
1518 }
1519 }
f91d0326 1520 if (!stmt_could_throw_p (dcall_stmt))
1521 gimple_purge_dead_eh_edges (dcall_bb);
e38def9c 1522 return dcall_stmt;
167b550b 1523}
1524
1525/*
1526 For every checked indirect/virtual call determine if most common pid of
1527 function/class method has probability more than 50%. If yes modify code of
1528 this call to:
1529 */
1530
1531static bool
3e7f455b 1532gimple_ic_transform (gimple_stmt_iterator *gsi)
167b550b 1533{
1a91d914 1534 gcall *stmt;
167b550b 1535 histogram_value histogram;
e0dc6f2b 1536 gcov_type val, count, all, bb_all;
167b550b 1537 struct cgraph_node *direct_call;
48e1416a 1538
1a91d914 1539 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1540 if (!stmt)
167b550b 1541 return false;
1542
2de00a2d 1543 if (gimple_call_fndecl (stmt) != NULL_TREE)
167b550b 1544 return false;
1545
fb049fba 1546 if (gimple_call_internal_p (stmt))
1547 return false;
1548
167b550b 1549 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_INDIR_CALL);
1550 if (!histogram)
1551 return false;
1552
1553 val = histogram->hvalue.counters [0];
1554 count = histogram->hvalue.counters [1];
1555 all = histogram->hvalue.counters [2];
167b550b 1556
e0dc6f2b 1557 bb_all = gimple_bb (stmt)->count;
48e1416a 1558 /* The order of CHECK_COUNTER calls is important -
e0dc6f2b 1559 since check_counter can correct the third parameter
1560 and we want to make count <= all <= bb_all. */
1561 if ( check_counter (stmt, "ic", &all, &bb_all, bb_all)
1562 || check_counter (stmt, "ic", &count, &all, all))
fe37be54 1563 {
1564 gimple_remove_histogram_value (cfun, stmt, histogram);
1565 return false;
1566 }
e0dc6f2b 1567
12d5ae9f 1568 if (4 * count <= 3 * all)
1569 return false;
1570
fe37be54 1571 direct_call = find_func_by_profile_id ((int)val);
167b550b 1572
1573 if (direct_call == NULL)
fe37be54 1574 {
1575 if (val)
1576 {
1577 if (dump_file)
1578 {
1579 fprintf (dump_file, "Indirect call -> direct call from other module");
1580 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1581 fprintf (dump_file, "=> %i (will resolve only with LTO)\n", (int)val);
1582 }
1583 }
1584 return false;
1585 }
167b550b 1586
850ff64c 1587 if (!check_ic_target (stmt, direct_call))
12d5ae9f 1588 {
1589 if (dump_file)
1590 {
1591 fprintf (dump_file, "Indirect call -> direct call ");
1592 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
1593 fprintf (dump_file, "=> ");
02774f2d 1594 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
12d5ae9f 1595 fprintf (dump_file, " transformation skipped because of type mismatch");
1596 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1597 }
1598 gimple_remove_histogram_value (cfun, stmt, histogram);
1599 return false;
1600 }
167b550b 1601
1602 if (dump_file)
1603 {
1604 fprintf (dump_file, "Indirect call -> direct call ");
75a70cf9 1605 print_generic_expr (dump_file, gimple_call_fn (stmt), TDF_SLIM);
167b550b 1606 fprintf (dump_file, "=> ");
02774f2d 1607 print_generic_expr (dump_file, direct_call->decl, TDF_SLIM);
12d5ae9f 1608 fprintf (dump_file, " transformation on insn postponned to ipa-profile");
75a70cf9 1609 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
f03df321 1610 fprintf (dump_file, "hist->count %" PRId64
1611 " hist->all %" PRId64"\n", count, all);
167b550b 1612 }
1613
1614 return true;
1615}
1616
357e29f4 1617/* Return true if the stringop CALL shall be profiled. SIZE_ARG be
1618 set to the argument index for the size of the string operation. */
1619
f656b751 1620static bool
357e29f4 1621interesting_stringop_to_profile_p (gcall *call, int *size_arg)
f656b751 1622{
357e29f4 1623 enum built_in_function fcode;
f656b751 1624
357e29f4 1625 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (call));
058d05d5 1626 if (fcode != BUILT_IN_MEMCPY && fcode != BUILT_IN_MEMPCPY
1627 && fcode != BUILT_IN_MEMSET && fcode != BUILT_IN_BZERO)
f656b751 1628 return false;
1629
1630 switch (fcode)
1631 {
1632 case BUILT_IN_MEMCPY:
1633 case BUILT_IN_MEMPCPY:
e0b41cd1 1634 *size_arg = 2;
75a70cf9 1635 return validate_gimple_arglist (call, POINTER_TYPE, POINTER_TYPE,
1636 INTEGER_TYPE, VOID_TYPE);
f656b751 1637 case BUILT_IN_MEMSET:
e0b41cd1 1638 *size_arg = 2;
75a70cf9 1639 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1640 INTEGER_TYPE, VOID_TYPE);
f656b751 1641 case BUILT_IN_BZERO:
e0b41cd1 1642 *size_arg = 1;
75a70cf9 1643 return validate_gimple_arglist (call, POINTER_TYPE, INTEGER_TYPE,
1644 VOID_TYPE);
f656b751 1645 default:
c2f47e15 1646 gcc_unreachable ();
f656b751 1647 }
1648}
1649
357e29f4 1650/* Convert stringop (..., vcall_size)
48e1416a 1651 into
e38def9c 1652 if (vcall_size == icall_size)
1653 stringop (..., icall_size);
f656b751 1654 else
e38def9c 1655 stringop (..., vcall_size);
1656 assuming we'll propagate a true constant into ICALL_SIZE later. */
1657
f656b751 1658static void
1a91d914 1659gimple_stringop_fixed_value (gcall *vcall_stmt, tree icall_size, int prob,
e38def9c 1660 gcov_type count, gcov_type all)
f656b751 1661{
1a91d914 1662 gassign *tmp_stmt;
1663 gcond *cond_stmt;
1664 gcall *icall_stmt;
03d37e4e 1665 tree tmp0, tmp1, vcall_size, optype;
e38def9c 1666 basic_block cond_bb, icall_bb, vcall_bb, join_bb;
1667 edge e_ci, e_cv, e_iv, e_ij, e_vj;
75a70cf9 1668 gimple_stmt_iterator gsi;
e0b41cd1 1669 int size_arg;
1670
357e29f4 1671 if (!interesting_stringop_to_profile_p (vcall_stmt, &size_arg))
9af5ce0c 1672 gcc_unreachable ();
f656b751 1673
e38def9c 1674 cond_bb = gimple_bb (vcall_stmt);
1675 gsi = gsi_for_stmt (vcall_stmt);
f656b751 1676
e0b41cd1 1677 vcall_size = gimple_call_arg (vcall_stmt, size_arg);
e38def9c 1678 optype = TREE_TYPE (vcall_size);
f656b751 1679
03d37e4e 1680 tmp0 = make_temp_ssa_name (optype, NULL, "PROF");
1681 tmp1 = make_temp_ssa_name (optype, NULL, "PROF");
85344eeb 1682 tmp_stmt = gimple_build_assign (tmp0, fold_convert (optype, icall_size));
e38def9c 1683 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
f656b751 1684
e38def9c 1685 tmp_stmt = gimple_build_assign (tmp1, vcall_size);
1686 gsi_insert_before (&gsi, tmp_stmt, GSI_SAME_STMT);
1687
85344eeb 1688 cond_stmt = gimple_build_cond (EQ_EXPR, tmp1, tmp0, NULL_TREE, NULL_TREE);
e38def9c 1689 gsi_insert_before (&gsi, cond_stmt, GSI_SAME_STMT);
1690
c05c1b41 1691 if (TREE_CODE (gimple_vdef (vcall_stmt)) == SSA_NAME)
1692 {
1693 unlink_stmt_vdef (vcall_stmt);
1694 release_ssa_name (gimple_vdef (vcall_stmt));
1695 }
85344eeb 1696 gimple_set_vdef (vcall_stmt, NULL);
1697 gimple_set_vuse (vcall_stmt, NULL);
1698 update_stmt (vcall_stmt);
1a91d914 1699 icall_stmt = as_a <gcall *> (gimple_copy (vcall_stmt));
e0b41cd1 1700 gimple_call_set_arg (icall_stmt, size_arg, icall_size);
e38def9c 1701 gsi_insert_before (&gsi, icall_stmt, GSI_SAME_STMT);
f656b751 1702
1703 /* Fix CFG. */
e38def9c 1704 /* Edge e_ci connects cond_bb to icall_bb, etc. */
1705 e_ci = split_block (cond_bb, cond_stmt);
1706 icall_bb = e_ci->dest;
1707 icall_bb->count = count;
f656b751 1708
e38def9c 1709 e_iv = split_block (icall_bb, icall_stmt);
1710 vcall_bb = e_iv->dest;
1711 vcall_bb->count = all - count;
f656b751 1712
e38def9c 1713 e_vj = split_block (vcall_bb, vcall_stmt);
1714 join_bb = e_vj->dest;
1715 join_bb->count = all;
f656b751 1716
e38def9c 1717 e_ci->flags = (e_ci->flags & ~EDGE_FALLTHRU) | EDGE_TRUE_VALUE;
1718 e_ci->probability = prob;
1719 e_ci->count = count;
1720
1721 e_cv = make_edge (cond_bb, vcall_bb, EDGE_FALSE_VALUE);
1722 e_cv->probability = REG_BR_PROB_BASE - prob;
1723 e_cv->count = all - count;
1724
1725 remove_edge (e_iv);
48e1416a 1726
e38def9c 1727 e_ij = make_edge (icall_bb, join_bb, EDGE_FALLTHRU);
1728 e_ij->probability = REG_BR_PROB_BASE;
1729 e_ij->count = count;
f656b751 1730
e38def9c 1731 e_vj->probability = REG_BR_PROB_BASE;
1732 e_vj->count = all - count;
1733
bb217be2 1734 /* Insert PHI node for the call result if necessary. */
1735 if (gimple_call_lhs (vcall_stmt)
1736 && TREE_CODE (gimple_call_lhs (vcall_stmt)) == SSA_NAME)
1737 {
1738 tree result = gimple_call_lhs (vcall_stmt);
1a91d914 1739 gphi *phi = create_phi_node (result, join_bb);
bb217be2 1740 gimple_call_set_lhs (vcall_stmt,
7ecda5e8 1741 duplicate_ssa_name (result, vcall_stmt));
60d535d2 1742 add_phi_arg (phi, gimple_call_lhs (vcall_stmt), e_vj, UNKNOWN_LOCATION);
bb217be2 1743 gimple_call_set_lhs (icall_stmt,
7ecda5e8 1744 duplicate_ssa_name (result, icall_stmt));
60d535d2 1745 add_phi_arg (phi, gimple_call_lhs (icall_stmt), e_ij, UNKNOWN_LOCATION);
bb217be2 1746 }
1747
e38def9c 1748 /* Because these are all string op builtins, they're all nothrow. */
1749 gcc_assert (!stmt_could_throw_p (vcall_stmt));
1750 gcc_assert (!stmt_could_throw_p (icall_stmt));
f656b751 1751}
1752
1753/* Find values inside STMT for that we want to measure histograms for
1754 division/modulo optimization. */
357e29f4 1755
f656b751 1756static bool
75a70cf9 1757gimple_stringops_transform (gimple_stmt_iterator *gsi)
f656b751 1758{
1a91d914 1759 gcall *stmt;
f656b751 1760 tree blck_size;
1761 enum built_in_function fcode;
f656b751 1762 histogram_value histogram;
1763 gcov_type count, all, val;
f656b751 1764 tree dest, src;
1765 unsigned int dest_align, src_align;
5b17b7ae 1766 gcov_type prob;
f656b751 1767 tree tree_val;
e0b41cd1 1768 int size_arg;
f656b751 1769
1a91d914 1770 stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1771 if (!stmt)
f656b751 1772 return false;
357e29f4 1773
1774 if (!gimple_call_builtin_p (gsi_stmt (*gsi), BUILT_IN_NORMAL))
f656b751 1775 return false;
357e29f4 1776
1777 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
f656b751 1778 return false;
1779
e0b41cd1 1780 blck_size = gimple_call_arg (stmt, size_arg);
f656b751 1781 if (TREE_CODE (blck_size) == INTEGER_CST)
1782 return false;
1783
4992f399 1784 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_SINGLE_VALUE);
f656b751 1785 if (!histogram)
1786 return false;
357e29f4 1787
f656b751 1788 val = histogram->hvalue.counters[0];
1789 count = histogram->hvalue.counters[1];
1790 all = histogram->hvalue.counters[2];
4992f399 1791 gimple_remove_histogram_value (cfun, stmt, histogram);
357e29f4 1792
f656b751 1793 /* We require that count is at least half of all; this means
1794 that for the transformation to fire the value must be constant
1795 at least 80% of time. */
0bfd8d5c 1796 if ((6 * count / 5) < all || optimize_bb_for_size_p (gimple_bb (stmt)))
f656b751 1797 return false;
e0dc6f2b 1798 if (check_counter (stmt, "value", &count, &all, gimple_bb (stmt)->count))
f656b751 1799 return false;
5b17b7ae 1800 if (all > 0)
f9d4b7f4 1801 prob = GCOV_COMPUTE_SCALE (count, all);
5b17b7ae 1802 else
1803 prob = 0;
357e29f4 1804
75a70cf9 1805 dest = gimple_call_arg (stmt, 0);
957d0361 1806 dest_align = get_pointer_alignment (dest);
357e29f4 1807 fcode = DECL_FUNCTION_CODE (gimple_call_fndecl (stmt));
f656b751 1808 switch (fcode)
1809 {
1810 case BUILT_IN_MEMCPY:
1811 case BUILT_IN_MEMPCPY:
75a70cf9 1812 src = gimple_call_arg (stmt, 1);
957d0361 1813 src_align = get_pointer_alignment (src);
f656b751 1814 if (!can_move_by_pieces (val, MIN (dest_align, src_align)))
1815 return false;
1816 break;
1817 case BUILT_IN_MEMSET:
1818 if (!can_store_by_pieces (val, builtin_memset_read_str,
75a70cf9 1819 gimple_call_arg (stmt, 1),
4b297e2e 1820 dest_align, true))
f656b751 1821 return false;
1822 break;
1823 case BUILT_IN_BZERO:
1824 if (!can_store_by_pieces (val, builtin_memset_read_str,
1825 integer_zero_node,
4b297e2e 1826 dest_align, true))
f656b751 1827 return false;
1828 break;
1829 default:
1830 gcc_unreachable ();
1831 }
357e29f4 1832
e913b5cd 1833 if (sizeof (gcov_type) == sizeof (HOST_WIDE_INT))
1834 tree_val = build_int_cst (get_gcov_type (), val);
1835 else
1836 {
1837 HOST_WIDE_INT a[2];
1838 a[0] = (unsigned HOST_WIDE_INT) val;
1839 a[1] = val >> (HOST_BITS_PER_WIDE_INT - 1) >> 1;
ddb1be65 1840
e913b5cd 1841 tree_val = wide_int_to_tree (get_gcov_type (), wide_int::from_array (a, 2,
1842 TYPE_PRECISION (get_gcov_type ()), false));
1843 }
1844
f656b751 1845 if (dump_file)
1846 {
1847 fprintf (dump_file, "Single value %i stringop transformation on ",
1848 (int)val);
75a70cf9 1849 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
f656b751 1850 }
357e29f4 1851
75a70cf9 1852 gimple_stringop_fixed_value (stmt, tree_val, prob, count, all);
48e1416a 1853
f656b751 1854 return true;
1855}
1856
162719b3 1857void
42acab1c 1858stringop_block_profile (gimple *stmt, unsigned int *expected_align,
162719b3 1859 HOST_WIDE_INT *expected_size)
1860{
1861 histogram_value histogram;
1862 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_AVERAGE);
357e29f4 1863
162719b3 1864 if (!histogram)
1865 *expected_size = -1;
553eb299 1866 else if (!histogram->hvalue.counters[1])
1867 {
1868 *expected_size = -1;
1869 gimple_remove_histogram_value (cfun, stmt, histogram);
1870 }
162719b3 1871 else
1872 {
1873 gcov_type size;
1874 size = ((histogram->hvalue.counters[0]
553eb299 1875 + histogram->hvalue.counters[1] / 2)
1876 / histogram->hvalue.counters[1]);
162719b3 1877 /* Even if we can hold bigger value in SIZE, INT_MAX
1878 is safe "infinity" for code generation strategies. */
1879 if (size > INT_MAX)
1880 size = INT_MAX;
1881 *expected_size = size;
1882 gimple_remove_histogram_value (cfun, stmt, histogram);
1883 }
357e29f4 1884
162719b3 1885 histogram = gimple_histogram_value_of_type (cfun, stmt, HIST_TYPE_IOR);
357e29f4 1886
162719b3 1887 if (!histogram)
f8eabf22 1888 *expected_align = 0;
553eb299 1889 else if (!histogram->hvalue.counters[0])
1890 {
1891 gimple_remove_histogram_value (cfun, stmt, histogram);
1892 *expected_align = 0;
1893 }
162719b3 1894 else
1895 {
1896 gcov_type count;
1897 int alignment;
1898
1899 count = histogram->hvalue.counters[0];
1900 alignment = 1;
1901 while (!(count & alignment)
1902 && (alignment * 2 * BITS_PER_UNIT))
1903 alignment <<= 1;
1904 *expected_align = alignment * BITS_PER_UNIT;
1905 gimple_remove_histogram_value (cfun, stmt, histogram);
1906 }
1907}
1908
4ee9c684 1909\f
d7683f13 1910/* Find values inside STMT for that we want to measure histograms for
d2971487 1911 division/modulo optimization. */
357e29f4 1912
d2971487 1913static void
42acab1c 1914gimple_divmod_values_to_profile (gimple *stmt, histogram_values *values)
d2971487 1915{
75a70cf9 1916 tree lhs, divisor, op0, type;
d2971487 1917 histogram_value hist;
1918
75a70cf9 1919 if (gimple_code (stmt) != GIMPLE_ASSIGN)
d2971487 1920 return;
75a70cf9 1921
1922 lhs = gimple_assign_lhs (stmt);
d7683f13 1923 type = TREE_TYPE (lhs);
1924 if (!INTEGRAL_TYPE_P (type))
d2971487 1925 return;
d7683f13 1926
75a70cf9 1927 switch (gimple_assign_rhs_code (stmt))
d2971487 1928 {
1929 case TRUNC_DIV_EXPR:
1930 case TRUNC_MOD_EXPR:
75a70cf9 1931 divisor = gimple_assign_rhs2 (stmt);
1932 op0 = gimple_assign_rhs1 (stmt);
d2971487 1933
f1f41a6c 1934 values->reserve (3);
d7683f13 1935
7c782c9b 1936 if (TREE_CODE (divisor) == SSA_NAME)
4992f399 1937 /* Check for the case where the divisor is the same value most
1938 of the time. */
f1f41a6c 1939 values->quick_push (gimple_alloc_histogram_value (cfun,
75a70cf9 1940 HIST_TYPE_SINGLE_VALUE,
4992f399 1941 stmt, divisor));
d2971487 1942
1943 /* For mod, check whether it is not often a noop (or replaceable by
1944 a few subtractions). */
75a70cf9 1945 if (gimple_assign_rhs_code (stmt) == TRUNC_MOD_EXPR
d7683f13 1946 && TYPE_UNSIGNED (type))
d2971487 1947 {
4992f399 1948 tree val;
421e19dd 1949 /* Check for a special case where the divisor is power of 2. */
f1f41a6c 1950 values->quick_push (gimple_alloc_histogram_value (cfun,
1951 HIST_TYPE_POW2,
1952 stmt, divisor));
421e19dd 1953
4992f399 1954 val = build2 (TRUNC_DIV_EXPR, type, op0, divisor);
1955 hist = gimple_alloc_histogram_value (cfun, HIST_TYPE_INTERVAL,
1956 stmt, val);
d2971487 1957 hist->hdata.intvl.int_start = 0;
1958 hist->hdata.intvl.steps = 2;
f1f41a6c 1959 values->quick_push (hist);
d2971487 1960 }
1961 return;
1962
1963 default:
1964 return;
1965 }
1966}
1967
48e1416a 1968/* Find calls inside STMT for that we want to measure histograms for
1969 indirect/virtual call optimization. */
167b550b 1970
1971static void
42acab1c 1972gimple_indirect_call_to_profile (gimple *stmt, histogram_values *values)
167b550b 1973{
75a70cf9 1974 tree callee;
167b550b 1975
0acacf9e 1976 if (gimple_code (stmt) != GIMPLE_CALL
fb049fba 1977 || gimple_call_internal_p (stmt)
0acacf9e 1978 || gimple_call_fndecl (stmt) != NULL_TREE)
167b550b 1979 return;
1980
75a70cf9 1981 callee = gimple_call_fn (stmt);
167b550b 1982
f1f41a6c 1983 values->reserve (3);
167b550b 1984
b74245ec 1985 values->quick_push (gimple_alloc_histogram_value (
1986 cfun,
1987 PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
1988 HIST_TYPE_INDIR_CALL_TOPN :
1989 HIST_TYPE_INDIR_CALL,
1990 stmt, callee));
167b550b 1991
1992 return;
1993}
1994
f656b751 1995/* Find values inside STMT for that we want to measure histograms for
4992f399 1996 string operations. */
357e29f4 1997
f656b751 1998static void
42acab1c 1999gimple_stringops_values_to_profile (gimple *gs, histogram_values *values)
f656b751 2000{
1a91d914 2001 gcall *stmt;
f656b751 2002 tree blck_size;
162719b3 2003 tree dest;
e0b41cd1 2004 int size_arg;
f656b751 2005
1a91d914 2006 stmt = dyn_cast <gcall *> (gs);
2007 if (!stmt)
f656b751 2008 return;
357e29f4 2009
2010 if (!gimple_call_builtin_p (gs, BUILT_IN_NORMAL))
f656b751 2011 return;
f656b751 2012
357e29f4 2013 if (!interesting_stringop_to_profile_p (stmt, &size_arg))
f656b751 2014 return;
2015
75a70cf9 2016 dest = gimple_call_arg (stmt, 0);
e0b41cd1 2017 blck_size = gimple_call_arg (stmt, size_arg);
f656b751 2018
162719b3 2019 if (TREE_CODE (blck_size) != INTEGER_CST)
2020 {
f1f41a6c 2021 values->safe_push (gimple_alloc_histogram_value (cfun,
2022 HIST_TYPE_SINGLE_VALUE,
2023 stmt, blck_size));
2024 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_AVERAGE,
2025 stmt, blck_size));
162719b3 2026 }
357e29f4 2027
f656b751 2028 if (TREE_CODE (blck_size) != INTEGER_CST)
f1f41a6c 2029 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_IOR,
2030 stmt, dest));
f656b751 2031}
2032
d7683f13 2033/* Find values inside STMT for that we want to measure histograms and adds
2034 them to list VALUES. */
2035
4ee9c684 2036static void
42acab1c 2037gimple_values_to_profile (gimple *stmt, histogram_values *values)
4ee9c684 2038{
3e7f455b 2039 gimple_divmod_values_to_profile (stmt, values);
2040 gimple_stringops_values_to_profile (stmt, values);
2041 gimple_indirect_call_to_profile (stmt, values);
4ee9c684 2042}
2043
fc49fbc1 2044void
75a70cf9 2045gimple_find_values_to_profile (histogram_values *values)
4ee9c684 2046{
d2971487 2047 basic_block bb;
75a70cf9 2048 gimple_stmt_iterator gsi;
d7683f13 2049 unsigned i;
4992f399 2050 histogram_value hist = NULL;
f1f41a6c 2051 values->create (0);
38fe12e3 2052
fc00614f 2053 FOR_EACH_BB_FN (bb, cfun)
75a70cf9 2054 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2055 gimple_values_to_profile (gsi_stmt (gsi), values);
48e1416a 2056
38fe12e3 2057 values->safe_push (gimple_alloc_histogram_value (cfun, HIST_TYPE_TIME_PROFILE, 0, 0));
2058
f1f41a6c 2059 FOR_EACH_VEC_ELT (*values, i, hist)
d2971487 2060 {
d2971487 2061 switch (hist->type)
2062 {
2063 case HIST_TYPE_INTERVAL:
d2971487 2064 hist->n_counters = hist->hdata.intvl.steps + 2;
2065 break;
2066
2067 case HIST_TYPE_POW2:
d7683f13 2068 hist->n_counters = 2;
d2971487 2069 break;
2070
2071 case HIST_TYPE_SINGLE_VALUE:
d2971487 2072 hist->n_counters = 3;
2073 break;
2074
2075 case HIST_TYPE_CONST_DELTA:
d2971487 2076 hist->n_counters = 4;
2077 break;
2078
167b550b 2079 case HIST_TYPE_INDIR_CALL:
2080 hist->n_counters = 3;
2081 break;
2082
b74245ec 2083 case HIST_TYPE_TIME_PROFILE:
2084 hist->n_counters = 1;
2085 break;
38fe12e3 2086
162719b3 2087 case HIST_TYPE_AVERAGE:
553eb299 2088 hist->n_counters = 2;
162719b3 2089 break;
2090
2091 case HIST_TYPE_IOR:
553eb299 2092 hist->n_counters = 1;
162719b3 2093 break;
2094
b74245ec 2095 case HIST_TYPE_INDIR_CALL_TOPN:
2096 hist->n_counters = GCOV_ICALL_TOPN_NCOUNTS;
2097 break;
2098
d2971487 2099 default:
a53ff4c1 2100 gcc_unreachable ();
d2971487 2101 }
4992f399 2102 if (dump_file)
2103 {
2104 fprintf (dump_file, "Stmt ");
75a70cf9 2105 print_gimple_stmt (dump_file, hist->hvalue.stmt, 0, TDF_SLIM);
4992f399 2106 dump_histogram_value (dump_file, hist);
2107 }
d2971487 2108 }
4ee9c684 2109}