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