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