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