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