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