1 /* RTL-level loop invariant motion. 2 Copyright (C) 2004-2017 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 7 under the terms of the GNU General Public License as published by the 8 Free Software Foundation; either version 3, or (at your option) any 9 later version. 10 11 GCC is distributed in the hope that it will be useful, but WITHOUT 12 ANY 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 /* This implements the loop invariant motion pass. It is very simple 21 (no calls, no loads/stores, etc.). This should be sufficient to cleanup 22 things like address arithmetics -- other more complicated invariants should 23 be eliminated on GIMPLE either in tree-ssa-loop-im.c or in tree-ssa-pre.c. 24 25 We proceed loop by loop -- it is simpler than trying to handle things 26 globally and should not lose much. First we inspect all sets inside loop 27 and create a dependency graph on insns (saying "to move this insn, you must 28 also move the following insns"). 29 30 We then need to determine what to move. We estimate the number of registers 31 used and move as many invariants as possible while we still have enough free 32 registers. We prefer the expensive invariants. 33 34 Then we move the selected invariants out of the loop, creating a new 35 temporaries for them if necessary. */ 36 37 #include "config.h" 38 #include "system.h" 39 #include "coretypes.h" 40 #include "backend.h" 41 #include "target.h" 42 #include "rtl.h" 43 #include "tree.h" 44 #include "cfghooks.h" 45 #include "df.h" 46 #include "memmodel.h" 47 #include "tm_p.h" 48 #include "insn-config.h" 49 #include "regs.h" 50 #include "ira.h" 51 #include "recog.h" 52 #include "cfgrtl.h" 53 #include "cfgloop.h" 54 #include "expr.h" 55 #include "params.h" 56 #include "rtl-iter.h" 57 #include "dumpfile.h" 58 59 /* The data stored for the loop. */ 60 61 struct loop_data 62 { 63 struct loop *outermost_exit; /* The outermost exit of the loop. */ 64 bool has_call; /* True if the loop contains a call. */ 65 /* Maximal register pressure inside loop for given register class 66 (defined only for the pressure classes). */ 67 int max_reg_pressure[N_REG_CLASSES]; 68 /* Loop regs referenced and live pseudo-registers. */ 69 bitmap_head regs_ref; 70 bitmap_head regs_live; 71 }; 72 73 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux) 74 75 /* The description of an use. */ 76 77 struct use 78 { 79 rtx *pos; /* Position of the use. */ 80 rtx_insn *insn; /* The insn in that the use occurs. */ 81 unsigned addr_use_p; /* Whether the use occurs in an address. */ 82 struct use *next; /* Next use in the list. */ 83 }; 84 85 /* The description of a def. */ 86 87 struct def 88 { 89 struct use *uses; /* The list of uses that are uniquely reached 90 by it. */ 91 unsigned n_uses; /* Number of such uses. */ 92 unsigned n_addr_uses; /* Number of uses in addresses. */ 93 unsigned invno; /* The corresponding invariant. */ 94 bool can_prop_to_addr_uses; /* True if the corresponding inv can be 95 propagated into its address uses. */ 96 }; 97 98 /* The data stored for each invariant. */ 99 100 struct invariant 101 { 102 /* The number of the invariant. */ 103 unsigned invno; 104 105 /* The number of the invariant with the same value. */ 106 unsigned eqto; 107 108 /* The number of invariants which eqto this. */ 109 unsigned eqno; 110 111 /* If we moved the invariant out of the loop, the original regno 112 that contained its value. */ 113 int orig_regno; 114 115 /* If we moved the invariant out of the loop, the register that contains its 116 value. */ 117 rtx reg; 118 119 /* The definition of the invariant. */ 120 struct def *def; 121 122 /* The insn in that it is defined. */ 123 rtx_insn *insn; 124 125 /* Whether it is always executed. */ 126 bool always_executed; 127 128 /* Whether to move the invariant. */ 129 bool move; 130 131 /* Whether the invariant is cheap when used as an address. */ 132 bool cheap_address; 133 134 /* Cost of the invariant. */ 135 unsigned cost; 136 137 /* Used for detecting already visited invariants during determining 138 costs of movements. */ 139 unsigned stamp; 140 141 /* The invariants it depends on. */ 142 bitmap depends_on; 143 }; 144 145 /* Currently processed loop. */ 146 static struct loop *curr_loop; 147 148 /* Table of invariants indexed by the df_ref uid field. */ 149 150 static unsigned int invariant_table_size = 0; 151 static struct invariant ** invariant_table; 152 153 /* Entry for hash table of invariant expressions. */ 154 155 struct invariant_expr_entry 156 { 157 /* The invariant. */ 158 struct invariant *inv; 159 160 /* Its value. */ 161 rtx expr; 162 163 /* Its mode. */ 164 machine_mode mode; 165 166 /* Its hash. */ 167 hashval_t hash; 168 }; 169 170 /* The actual stamp for marking already visited invariants during determining 171 costs of movements. */ 172 173 static unsigned actual_stamp; 174 175 typedef struct invariant *invariant_p; 176 177 178 /* The invariants. */ 179 180 static vec<invariant_p> invariants; 181 182 /* Check the size of the invariant table and realloc if necessary. */ 183 184 static void 185 check_invariant_table_size (void) 186 { 187 if (invariant_table_size < DF_DEFS_TABLE_SIZE ()) 188 { 189 unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4); 190 invariant_table = XRESIZEVEC (struct invariant *, invariant_table, new_size); 191 memset (&invariant_table[invariant_table_size], 0, 192 (new_size - invariant_table_size) * sizeof (struct invariant *)); 193 invariant_table_size = new_size; 194 } 195 } 196 197 /* Test for possibility of invariantness of X. */ 198 199 static bool 200 check_maybe_invariant (rtx x) 201 { 202 enum rtx_code code = GET_CODE (x); 203 int i, j; 204 const char *fmt; 205 206 switch (code) 207 { 208 CASE_CONST_ANY: 209 case SYMBOL_REF: 210 case CONST: 211 case LABEL_REF: 212 return true; 213 214 case PC: 215 case CC0: 216 case UNSPEC_VOLATILE: 217 case CALL: 218 return false; 219 220 case REG: 221 return true; 222 223 case MEM: 224 /* Load/store motion is done elsewhere. ??? Perhaps also add it here? 225 It should not be hard, and might be faster than "elsewhere". */ 226 227 /* Just handle the most trivial case where we load from an unchanging 228 location (most importantly, pic tables). */ 229 if (MEM_READONLY_P (x) && !MEM_VOLATILE_P (x)) 230 break; 231 232 return false; 233 234 case ASM_OPERANDS: 235 /* Don't mess with insns declared volatile. */ 236 if (MEM_VOLATILE_P (x)) 237 return false; 238 break; 239 240 default: 241 break; 242 } 243 244 fmt = GET_RTX_FORMAT (code); 245 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) 246 { 247 if (fmt[i] == 'e') 248 { 249 if (!check_maybe_invariant (XEXP (x, i))) 250 return false; 251 } 252 else if (fmt[i] == 'E') 253 { 254 for (j = 0; j < XVECLEN (x, i); j++) 255 if (!check_maybe_invariant (XVECEXP (x, i, j))) 256 return false; 257 } 258 } 259 260 return true; 261 } 262 263 /* Returns the invariant definition for USE, or NULL if USE is not 264 invariant. */ 265 266 static struct invariant * 267 invariant_for_use (df_ref use) 268 { 269 struct df_link *defs; 270 df_ref def; 271 basic_block bb = DF_REF_BB (use), def_bb; 272 273 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE) 274 return NULL; 275 276 defs = DF_REF_CHAIN (use); 277 if (!defs || defs->next) 278 return NULL; 279 def = defs->ref; 280 check_invariant_table_size (); 281 if (!invariant_table[DF_REF_ID (def)]) 282 return NULL; 283 284 def_bb = DF_REF_BB (def); 285 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb)) 286 return NULL; 287 return invariant_table[DF_REF_ID (def)]; 288 } 289 290 /* Computes hash value for invariant expression X in INSN. */ 291 292 static hashval_t 293 hash_invariant_expr_1 (rtx_insn *insn, rtx x) 294 { 295 enum rtx_code code = GET_CODE (x); 296 int i, j; 297 const char *fmt; 298 hashval_t val = code; 299 int do_not_record_p; 300 df_ref use; 301 struct invariant *inv; 302 303 switch (code) 304 { 305 CASE_CONST_ANY: 306 case SYMBOL_REF: 307 case CONST: 308 case LABEL_REF: 309 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false); 310 311 case REG: 312 use = df_find_use (insn, x); 313 if (!use) 314 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false); 315 inv = invariant_for_use (use); 316 if (!inv) 317 return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false); 318 319 gcc_assert (inv->eqto != ~0u); 320 return inv->eqto; 321 322 default: 323 break; 324 } 325 326 fmt = GET_RTX_FORMAT (code); 327 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) 328 { 329 if (fmt[i] == 'e') 330 val ^= hash_invariant_expr_1 (insn, XEXP (x, i)); 331 else if (fmt[i] == 'E') 332 { 333 for (j = 0; j < XVECLEN (x, i); j++) 334 val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j)); 335 } 336 else if (fmt[i] == 'i' || fmt[i] == 'n') 337 val ^= XINT (x, i); 338 } 339 340 return val; 341 } 342 343 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1 344 and INSN2 have always the same value. */ 345 346 static bool 347 invariant_expr_equal_p (rtx_insn *insn1, rtx e1, rtx_insn *insn2, rtx e2) 348 { 349 enum rtx_code code = GET_CODE (e1); 350 int i, j; 351 const char *fmt; 352 df_ref use1, use2; 353 struct invariant *inv1 = NULL, *inv2 = NULL; 354 rtx sub1, sub2; 355 356 /* If mode of only one of the operands is VOIDmode, it is not equivalent to 357 the other one. If both are VOIDmode, we rely on the caller of this 358 function to verify that their modes are the same. */ 359 if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2)) 360 return false; 361 362 switch (code) 363 { 364 CASE_CONST_ANY: 365 case SYMBOL_REF: 366 case CONST: 367 case LABEL_REF: 368 return rtx_equal_p (e1, e2); 369 370 case REG: 371 use1 = df_find_use (insn1, e1); 372 use2 = df_find_use (insn2, e2); 373 if (use1) 374 inv1 = invariant_for_use (use1); 375 if (use2) 376 inv2 = invariant_for_use (use2); 377 378 if (!inv1 && !inv2) 379 return rtx_equal_p (e1, e2); 380 381 if (!inv1 || !inv2) 382 return false; 383 384 gcc_assert (inv1->eqto != ~0u); 385 gcc_assert (inv2->eqto != ~0u); 386 return inv1->eqto == inv2->eqto; 387 388 default: 389 break; 390 } 391 392 fmt = GET_RTX_FORMAT (code); 393 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) 394 { 395 if (fmt[i] == 'e') 396 { 397 sub1 = XEXP (e1, i); 398 sub2 = XEXP (e2, i); 399 400 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2)) 401 return false; 402 } 403 404 else if (fmt[i] == 'E') 405 { 406 if (XVECLEN (e1, i) != XVECLEN (e2, i)) 407 return false; 408 409 for (j = 0; j < XVECLEN (e1, i); j++) 410 { 411 sub1 = XVECEXP (e1, i, j); 412 sub2 = XVECEXP (e2, i, j); 413 414 if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2)) 415 return false; 416 } 417 } 418 else if (fmt[i] == 'i' || fmt[i] == 'n') 419 { 420 if (XINT (e1, i) != XINT (e2, i)) 421 return false; 422 } 423 /* Unhandled type of subexpression, we fail conservatively. */ 424 else 425 return false; 426 } 427 428 return true; 429 } 430 431 struct invariant_expr_hasher : free_ptr_hash <invariant_expr_entry> 432 { 433 static inline hashval_t hash (const invariant_expr_entry *); 434 static inline bool equal (const invariant_expr_entry *, 435 const invariant_expr_entry *); 436 }; 437 438 /* Returns hash value for invariant expression entry ENTRY. */ 439 440 inline hashval_t 441 invariant_expr_hasher::hash (const invariant_expr_entry *entry) 442 { 443 return entry->hash; 444 } 445 446 /* Compares invariant expression entries ENTRY1 and ENTRY2. */ 447 448 inline bool 449 invariant_expr_hasher::equal (const invariant_expr_entry *entry1, 450 const invariant_expr_entry *entry2) 451 { 452 if (entry1->mode != entry2->mode) 453 return 0; 454 455 return invariant_expr_equal_p (entry1->inv->insn, entry1->expr, 456 entry2->inv->insn, entry2->expr); 457 } 458 459 typedef hash_table<invariant_expr_hasher> invariant_htab_type; 460 461 /* Checks whether invariant with value EXPR in machine mode MODE is 462 recorded in EQ. If this is the case, return the invariant. Otherwise 463 insert INV to the table for this expression and return INV. */ 464 465 static struct invariant * 466 find_or_insert_inv (invariant_htab_type *eq, rtx expr, machine_mode mode, 467 struct invariant *inv) 468 { 469 hashval_t hash = hash_invariant_expr_1 (inv->insn, expr); 470 struct invariant_expr_entry *entry; 471 struct invariant_expr_entry pentry; 472 invariant_expr_entry **slot; 473 474 pentry.expr = expr; 475 pentry.inv = inv; 476 pentry.mode = mode; 477 slot = eq->find_slot_with_hash (&pentry, hash, INSERT); 478 entry = *slot; 479 480 if (entry) 481 return entry->inv; 482 483 entry = XNEW (struct invariant_expr_entry); 484 entry->inv = inv; 485 entry->expr = expr; 486 entry->mode = mode; 487 entry->hash = hash; 488 *slot = entry; 489 490 return inv; 491 } 492 493 /* Finds invariants identical to INV and records the equivalence. EQ is the 494 hash table of the invariants. */ 495 496 static void 497 find_identical_invariants (invariant_htab_type *eq, struct invariant *inv) 498 { 499 unsigned depno; 500 bitmap_iterator bi; 501 struct invariant *dep; 502 rtx expr, set; 503 machine_mode mode; 504 struct invariant *tmp; 505 506 if (inv->eqto != ~0u) 507 return; 508 509 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi) 510 { 511 dep = invariants[depno]; 512 find_identical_invariants (eq, dep); 513 } 514 515 set = single_set (inv->insn); 516 expr = SET_SRC (set); 517 mode = GET_MODE (expr); 518 if (mode == VOIDmode) 519 mode = GET_MODE (SET_DEST (set)); 520 521 tmp = find_or_insert_inv (eq, expr, mode, inv); 522 inv->eqto = tmp->invno; 523 524 if (tmp->invno != inv->invno && inv->always_executed) 525 tmp->eqno++; 526 527 if (dump_file && inv->eqto != inv->invno) 528 fprintf (dump_file, 529 "Invariant %d is equivalent to invariant %d.\n", 530 inv->invno, inv->eqto); 531 } 532 533 /* Find invariants with the same value and record the equivalences. */ 534 535 static void 536 merge_identical_invariants (void) 537 { 538 unsigned i; 539 struct invariant *inv; 540 invariant_htab_type eq (invariants.length ()); 541 542 FOR_EACH_VEC_ELT (invariants, i, inv) 543 find_identical_invariants (&eq, inv); 544 } 545 546 /* Determines the basic blocks inside LOOP that are always executed and 547 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of 548 basic blocks that may either exit the loop, or contain the call that 549 does not have to return. BODY is body of the loop obtained by 550 get_loop_body_in_dom_order. */ 551 552 static void 553 compute_always_reached (struct loop *loop, basic_block *body, 554 bitmap may_exit, bitmap always_reached) 555 { 556 unsigned i; 557 558 for (i = 0; i < loop->num_nodes; i++) 559 { 560 if (dominated_by_p (CDI_DOMINATORS, loop->latch, body[i])) 561 bitmap_set_bit (always_reached, i); 562 563 if (bitmap_bit_p (may_exit, i)) 564 return; 565 } 566 } 567 568 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may 569 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT 570 additionally mark blocks that may exit due to a call. */ 571 572 static void 573 find_exits (struct loop *loop, basic_block *body, 574 bitmap may_exit, bitmap has_exit) 575 { 576 unsigned i; 577 edge_iterator ei; 578 edge e; 579 struct loop *outermost_exit = loop, *aexit; 580 bool has_call = false; 581 rtx_insn *insn; 582 583 for (i = 0; i < loop->num_nodes; i++) 584 { 585 if (body[i]->loop_father == loop) 586 { 587 FOR_BB_INSNS (body[i], insn) 588 { 589 if (CALL_P (insn) 590 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn) 591 || !RTL_CONST_OR_PURE_CALL_P (insn))) 592 { 593 has_call = true; 594 bitmap_set_bit (may_exit, i); 595 break; 596 } 597 } 598 599 FOR_EACH_EDGE (e, ei, body[i]->succs) 600 { 601 if (! flow_bb_inside_loop_p (loop, e->dest)) 602 { 603 bitmap_set_bit (may_exit, i); 604 bitmap_set_bit (has_exit, i); 605 outermost_exit = find_common_loop (outermost_exit, 606 e->dest->loop_father); 607 } 608 /* If we enter a subloop that might never terminate treat 609 it like a possible exit. */ 610 if (flow_loop_nested_p (loop, e->dest->loop_father)) 611 bitmap_set_bit (may_exit, i); 612 } 613 continue; 614 } 615 616 /* Use the data stored for the subloop to decide whether we may exit 617 through it. It is sufficient to do this for header of the loop, 618 as other basic blocks inside it must be dominated by it. */ 619 if (body[i]->loop_father->header != body[i]) 620 continue; 621 622 if (LOOP_DATA (body[i]->loop_father)->has_call) 623 { 624 has_call = true; 625 bitmap_set_bit (may_exit, i); 626 } 627 aexit = LOOP_DATA (body[i]->loop_father)->outermost_exit; 628 if (aexit != loop) 629 { 630 bitmap_set_bit (may_exit, i); 631 bitmap_set_bit (has_exit, i); 632 633 if (flow_loop_nested_p (aexit, outermost_exit)) 634 outermost_exit = aexit; 635 } 636 } 637 638 if (loop->aux == NULL) 639 { 640 loop->aux = xcalloc (1, sizeof (struct loop_data)); 641 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, ®_obstack); 642 bitmap_initialize (&LOOP_DATA (loop)->regs_live, ®_obstack); 643 } 644 LOOP_DATA (loop)->outermost_exit = outermost_exit; 645 LOOP_DATA (loop)->has_call = has_call; 646 } 647 648 /* Check whether we may assign a value to X from a register. */ 649 650 static bool 651 may_assign_reg_p (rtx x) 652 { 653 return (GET_MODE (x) != VOIDmode 654 && GET_MODE (x) != BLKmode 655 && can_copy_p (GET_MODE (x)) 656 && (!REG_P (x) 657 || !HARD_REGISTER_P (x) 658 || REGNO_REG_CLASS (REGNO (x)) != NO_REGS)); 659 } 660 661 /* Finds definitions that may correspond to invariants in LOOP with body 662 BODY. */ 663 664 static void 665 find_defs (struct loop *loop) 666 { 667 if (dump_file) 668 { 669 fprintf (dump_file, 670 "*****starting processing of loop %d ******\n", 671 loop->num); 672 } 673 674 df_remove_problem (df_chain); 675 df_process_deferred_rescans (); 676 df_chain_add_problem (DF_UD_CHAIN); 677 df_live_add_problem (); 678 df_live_set_all_dirty (); 679 df_set_flags (DF_RD_PRUNE_DEAD_DEFS); 680 df_analyze_loop (loop); 681 check_invariant_table_size (); 682 683 if (dump_file) 684 { 685 df_dump_region (dump_file); 686 fprintf (dump_file, 687 "*****ending processing of loop %d ******\n", 688 loop->num); 689 } 690 } 691 692 /* Creates a new invariant for definition DEF in INSN, depending on invariants 693 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed, 694 unless the program ends due to a function call. The newly created invariant 695 is returned. */ 696 697 static struct invariant * 698 create_new_invariant (struct def *def, rtx_insn *insn, bitmap depends_on, 699 bool always_executed) 700 { 701 struct invariant *inv = XNEW (struct invariant); 702 rtx set = single_set (insn); 703 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn)); 704 705 inv->def = def; 706 inv->always_executed = always_executed; 707 inv->depends_on = depends_on; 708 709 /* If the set is simple, usually by moving it we move the whole store out of 710 the loop. Otherwise we save only cost of the computation. */ 711 if (def) 712 { 713 inv->cost = set_rtx_cost (set, speed); 714 /* ??? Try to determine cheapness of address computation. Unfortunately 715 the address cost is only a relative measure, we can't really compare 716 it with any absolute number, but only with other address costs. 717 But here we don't have any other addresses, so compare with a magic 718 number anyway. It has to be large enough to not regress PR33928 719 (by avoiding to move reg+8,reg+16,reg+24 invariants), but small 720 enough to not regress 410.bwaves either (by still moving reg+reg 721 invariants). 722 See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html . */ 723 if (SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))) 724 inv->cheap_address = address_cost (SET_SRC (set), word_mode, 725 ADDR_SPACE_GENERIC, speed) < 3; 726 else 727 inv->cheap_address = false; 728 } 729 else 730 { 731 inv->cost = set_src_cost (SET_SRC (set), GET_MODE (SET_DEST (set)), 732 speed); 733 inv->cheap_address = false; 734 } 735 736 inv->move = false; 737 inv->reg = NULL_RTX; 738 inv->orig_regno = -1; 739 inv->stamp = 0; 740 inv->insn = insn; 741 742 inv->invno = invariants.length (); 743 inv->eqto = ~0u; 744 745 /* Itself. */ 746 inv->eqno = 1; 747 748 if (def) 749 def->invno = inv->invno; 750 invariants.safe_push (inv); 751 752 if (dump_file) 753 { 754 fprintf (dump_file, 755 "Set in insn %d is invariant (%d), cost %d, depends on ", 756 INSN_UID (insn), inv->invno, inv->cost); 757 dump_bitmap (dump_file, inv->depends_on); 758 } 759 760 return inv; 761 } 762 763 /* Return a canonical version of X for the address, from the point of view, 764 that all multiplications are represented as MULT instead of the multiply 765 by a power of 2 being represented as ASHIFT. 766 767 Callers should prepare a copy of X because this function may modify it 768 in place. */ 769 770 static void 771 canonicalize_address_mult (rtx x) 772 { 773 subrtx_var_iterator::array_type array; 774 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST) 775 { 776 rtx sub = *iter; 777 778 if (GET_CODE (sub) == ASHIFT 779 && CONST_INT_P (XEXP (sub, 1)) 780 && INTVAL (XEXP (sub, 1)) < GET_MODE_BITSIZE (GET_MODE (sub)) 781 && INTVAL (XEXP (sub, 1)) >= 0) 782 { 783 HOST_WIDE_INT shift = INTVAL (XEXP (sub, 1)); 784 PUT_CODE (sub, MULT); 785 XEXP (sub, 1) = gen_int_mode (HOST_WIDE_INT_1 << shift, 786 GET_MODE (sub)); 787 iter.skip_subrtxes (); 788 } 789 } 790 } 791 792 /* Maximum number of sub expressions in address. We set it to 793 a small integer since it's unlikely to have a complicated 794 address expression. */ 795 796 #define MAX_CANON_ADDR_PARTS (5) 797 798 /* Collect sub expressions in address X with PLUS as the seperator. 799 Sub expressions are stored in vector ADDR_PARTS. */ 800 801 static void 802 collect_address_parts (rtx x, vec<rtx> *addr_parts) 803 { 804 subrtx_var_iterator::array_type array; 805 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST) 806 { 807 rtx sub = *iter; 808 809 if (GET_CODE (sub) != PLUS) 810 { 811 addr_parts->safe_push (sub); 812 iter.skip_subrtxes (); 813 } 814 } 815 } 816 817 /* Compare function for sorting sub expressions X and Y based on 818 precedence defined for communitive operations. */ 819 820 static int 821 compare_address_parts (const void *x, const void *y) 822 { 823 const rtx *rx = (const rtx *)x; 824 const rtx *ry = (const rtx *)y; 825 int px = commutative_operand_precedence (*rx); 826 int py = commutative_operand_precedence (*ry); 827 828 return (py - px); 829 } 830 831 /* Return a canonical version address for X by following steps: 832 1) Rewrite ASHIFT into MULT recursively. 833 2) Divide address into sub expressions with PLUS as the 834 separator. 835 3) Sort sub expressions according to precedence defined 836 for communative operations. 837 4) Simplify CONST_INT_P sub expressions. 838 5) Create new canonicalized address and return. 839 Callers should prepare a copy of X because this function may 840 modify it in place. */ 841 842 static rtx 843 canonicalize_address (rtx x) 844 { 845 rtx res; 846 unsigned int i, j; 847 machine_mode mode = GET_MODE (x); 848 auto_vec<rtx, MAX_CANON_ADDR_PARTS> addr_parts; 849 850 /* Rewrite ASHIFT into MULT. */ 851 canonicalize_address_mult (x); 852 /* Divide address into sub expressions. */ 853 collect_address_parts (x, &addr_parts); 854 /* Unlikely to have very complicated address. */ 855 if (addr_parts.length () < 2 856 || addr_parts.length () > MAX_CANON_ADDR_PARTS) 857 return x; 858 859 /* Sort sub expressions according to canonicalization precedence. */ 860 addr_parts.qsort (compare_address_parts); 861 862 /* Simplify all constant int summary if possible. */ 863 for (i = 0; i < addr_parts.length (); i++) 864 if (CONST_INT_P (addr_parts[i])) 865 break; 866 867 for (j = i + 1; j < addr_parts.length (); j++) 868 { 869 gcc_assert (CONST_INT_P (addr_parts[j])); 870 addr_parts[i] = simplify_gen_binary (PLUS, mode, 871 addr_parts[i], 872 addr_parts[j]); 873 } 874 875 /* Chain PLUS operators to the left for !CONST_INT_P sub expressions. */ 876 res = addr_parts[0]; 877 for (j = 1; j < i; j++) 878 res = simplify_gen_binary (PLUS, mode, res, addr_parts[j]); 879 880 /* Pickup the last CONST_INT_P sub expression. */ 881 if (i < addr_parts.length ()) 882 res = simplify_gen_binary (PLUS, mode, res, addr_parts[i]); 883 884 return res; 885 } 886 887 /* Given invariant DEF and its address USE, check if the corresponding 888 invariant expr can be propagated into the use or not. */ 889 890 static bool 891 inv_can_prop_to_addr_use (struct def *def, df_ref use) 892 { 893 struct invariant *inv; 894 rtx *pos = DF_REF_REAL_LOC (use), def_set, use_set; 895 rtx_insn *use_insn = DF_REF_INSN (use); 896 rtx_insn *def_insn; 897 bool ok; 898 899 inv = invariants[def->invno]; 900 /* No need to check if address expression is expensive. */ 901 if (!inv->cheap_address) 902 return false; 903 904 def_insn = inv->insn; 905 def_set = single_set (def_insn); 906 if (!def_set) 907 return false; 908 909 validate_unshare_change (use_insn, pos, SET_SRC (def_set), true); 910 ok = verify_changes (0); 911 /* Try harder with canonicalization in address expression. */ 912 if (!ok && (use_set = single_set (use_insn)) != NULL_RTX) 913 { 914 rtx src, dest, mem = NULL_RTX; 915 916 src = SET_SRC (use_set); 917 dest = SET_DEST (use_set); 918 if (MEM_P (src)) 919 mem = src; 920 else if (MEM_P (dest)) 921 mem = dest; 922 923 if (mem != NULL_RTX 924 && !memory_address_addr_space_p (GET_MODE (mem), 925 XEXP (mem, 0), 926 MEM_ADDR_SPACE (mem))) 927 { 928 rtx addr = canonicalize_address (copy_rtx (XEXP (mem, 0))); 929 if (memory_address_addr_space_p (GET_MODE (mem), 930 addr, MEM_ADDR_SPACE (mem))) 931 ok = true; 932 } 933 } 934 cancel_changes (0); 935 return ok; 936 } 937 938 /* Record USE at DEF. */ 939 940 static void 941 record_use (struct def *def, df_ref use) 942 { 943 struct use *u = XNEW (struct use); 944 945 u->pos = DF_REF_REAL_LOC (use); 946 u->insn = DF_REF_INSN (use); 947 u->addr_use_p = (DF_REF_TYPE (use) == DF_REF_REG_MEM_LOAD 948 || DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE); 949 u->next = def->uses; 950 def->uses = u; 951 def->n_uses++; 952 if (u->addr_use_p) 953 { 954 /* Initialize propagation information if this is the first addr 955 use of the inv def. */ 956 if (def->n_addr_uses == 0) 957 def->can_prop_to_addr_uses = true; 958 959 def->n_addr_uses++; 960 if (def->can_prop_to_addr_uses && !inv_can_prop_to_addr_use (def, use)) 961 def->can_prop_to_addr_uses = false; 962 } 963 } 964 965 /* Finds the invariants USE depends on and store them to the DEPENDS_ON 966 bitmap. Returns true if all dependencies of USE are known to be 967 loop invariants, false otherwise. */ 968 969 static bool 970 check_dependency (basic_block bb, df_ref use, bitmap depends_on) 971 { 972 df_ref def; 973 basic_block def_bb; 974 struct df_link *defs; 975 struct def *def_data; 976 struct invariant *inv; 977 978 if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE) 979 return false; 980 981 defs = DF_REF_CHAIN (use); 982 if (!defs) 983 { 984 unsigned int regno = DF_REF_REGNO (use); 985 986 /* If this is the use of an uninitialized argument register that is 987 likely to be spilled, do not move it lest this might extend its 988 lifetime and cause reload to die. This can occur for a call to 989 a function taking complex number arguments and moving the insns 990 preparing the arguments without moving the call itself wouldn't 991 gain much in practice. */ 992 if ((DF_REF_FLAGS (use) & DF_HARD_REG_LIVE) 993 && FUNCTION_ARG_REGNO_P (regno) 994 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno))) 995 return false; 996 997 return true; 998 } 999 1000 if (defs->next) 1001 return false; 1002 1003 def = defs->ref; 1004 check_invariant_table_size (); 1005 inv = invariant_table[DF_REF_ID (def)]; 1006 if (!inv) 1007 return false; 1008 1009 def_data = inv->def; 1010 gcc_assert (def_data != NULL); 1011 1012 def_bb = DF_REF_BB (def); 1013 /* Note that in case bb == def_bb, we know that the definition 1014 dominates insn, because def has invariant_table[DF_REF_ID(def)] 1015 defined and we process the insns in the basic block bb 1016 sequentially. */ 1017 if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb)) 1018 return false; 1019 1020 bitmap_set_bit (depends_on, def_data->invno); 1021 return true; 1022 } 1023 1024 1025 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON 1026 bitmap. Returns true if all dependencies of INSN are known to be 1027 loop invariants, false otherwise. */ 1028 1029 static bool 1030 check_dependencies (rtx_insn *insn, bitmap depends_on) 1031 { 1032 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn); 1033 df_ref use; 1034 basic_block bb = BLOCK_FOR_INSN (insn); 1035 1036 FOR_EACH_INSN_INFO_USE (use, insn_info) 1037 if (!check_dependency (bb, use, depends_on)) 1038 return false; 1039 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info) 1040 if (!check_dependency (bb, use, depends_on)) 1041 return false; 1042 1043 return true; 1044 } 1045 1046 /* Pre-check candidate DEST to skip the one which can not make a valid insn 1047 during move_invariant_reg. SIMPLE is to skip HARD_REGISTER. */ 1048 static bool 1049 pre_check_invariant_p (bool simple, rtx dest) 1050 { 1051 if (simple && REG_P (dest) && DF_REG_DEF_COUNT (REGNO (dest)) > 1) 1052 { 1053 df_ref use; 1054 unsigned int i = REGNO (dest); 1055 struct df_insn_info *insn_info; 1056 df_ref def_rec; 1057 1058 for (use = DF_REG_USE_CHAIN (i); use; use = DF_REF_NEXT_REG (use)) 1059 { 1060 rtx_insn *ref = DF_REF_INSN (use); 1061 insn_info = DF_INSN_INFO_GET (ref); 1062 1063 FOR_EACH_INSN_INFO_DEF (def_rec, insn_info) 1064 if (DF_REF_REGNO (def_rec) == i) 1065 { 1066 /* Multi definitions at this stage, most likely are due to 1067 instruction constraints, which requires both read and write 1068 on the same register. Since move_invariant_reg is not 1069 powerful enough to handle such cases, just ignore the INV 1070 and leave the chance to others. */ 1071 return false; 1072 } 1073 } 1074 } 1075 return true; 1076 } 1077 1078 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always 1079 executed. ALWAYS_EXECUTED is true if the insn is always executed, 1080 unless the program ends due to a function call. */ 1081 1082 static void 1083 find_invariant_insn (rtx_insn *insn, bool always_reached, bool always_executed) 1084 { 1085 df_ref ref; 1086 struct def *def; 1087 bitmap depends_on; 1088 rtx set, dest; 1089 bool simple = true; 1090 struct invariant *inv; 1091 1092 /* We can't move a CC0 setter without the user. */ 1093 if (HAVE_cc0 && sets_cc0_p (insn)) 1094 return; 1095 1096 set = single_set (insn); 1097 if (!set) 1098 return; 1099 dest = SET_DEST (set); 1100 1101 if (!REG_P (dest) 1102 || HARD_REGISTER_P (dest)) 1103 simple = false; 1104 1105 if (!may_assign_reg_p (dest) 1106 || !pre_check_invariant_p (simple, dest) 1107 || !check_maybe_invariant (SET_SRC (set))) 1108 return; 1109 1110 /* If the insn can throw exception, we cannot move it at all without changing 1111 cfg. */ 1112 if (can_throw_internal (insn)) 1113 return; 1114 1115 /* We cannot make trapping insn executed, unless it was executed before. */ 1116 if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached) 1117 return; 1118 1119 depends_on = BITMAP_ALLOC (NULL); 1120 if (!check_dependencies (insn, depends_on)) 1121 { 1122 BITMAP_FREE (depends_on); 1123 return; 1124 } 1125 1126 if (simple) 1127 def = XCNEW (struct def); 1128 else 1129 def = NULL; 1130 1131 inv = create_new_invariant (def, insn, depends_on, always_executed); 1132 1133 if (simple) 1134 { 1135 ref = df_find_def (insn, dest); 1136 check_invariant_table_size (); 1137 invariant_table[DF_REF_ID (ref)] = inv; 1138 } 1139 } 1140 1141 /* Record registers used in INSN that have a unique invariant definition. */ 1142 1143 static void 1144 record_uses (rtx_insn *insn) 1145 { 1146 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn); 1147 df_ref use; 1148 struct invariant *inv; 1149 1150 FOR_EACH_INSN_INFO_USE (use, insn_info) 1151 { 1152 inv = invariant_for_use (use); 1153 if (inv) 1154 record_use (inv->def, use); 1155 } 1156 FOR_EACH_INSN_INFO_EQ_USE (use, insn_info) 1157 { 1158 inv = invariant_for_use (use); 1159 if (inv) 1160 record_use (inv->def, use); 1161 } 1162 } 1163 1164 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always 1165 executed. ALWAYS_EXECUTED is true if the insn is always executed, 1166 unless the program ends due to a function call. */ 1167 1168 static void 1169 find_invariants_insn (rtx_insn *insn, bool always_reached, bool always_executed) 1170 { 1171 find_invariant_insn (insn, always_reached, always_executed); 1172 record_uses (insn); 1173 } 1174 1175 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the 1176 basic block is always executed. ALWAYS_EXECUTED is true if the basic 1177 block is always executed, unless the program ends due to a function 1178 call. */ 1179 1180 static void 1181 find_invariants_bb (basic_block bb, bool always_reached, bool always_executed) 1182 { 1183 rtx_insn *insn; 1184 1185 FOR_BB_INSNS (bb, insn) 1186 { 1187 if (!NONDEBUG_INSN_P (insn)) 1188 continue; 1189 1190 find_invariants_insn (insn, always_reached, always_executed); 1191 1192 if (always_reached 1193 && CALL_P (insn) 1194 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn) 1195 || ! RTL_CONST_OR_PURE_CALL_P (insn))) 1196 always_reached = false; 1197 } 1198 } 1199 1200 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of 1201 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the 1202 bitmap of basic blocks in BODY that are always executed unless the program 1203 ends due to a function call. */ 1204 1205 static void 1206 find_invariants_body (struct loop *loop, basic_block *body, 1207 bitmap always_reached, bitmap always_executed) 1208 { 1209 unsigned i; 1210 1211 for (i = 0; i < loop->num_nodes; i++) 1212 find_invariants_bb (body[i], 1213 bitmap_bit_p (always_reached, i), 1214 bitmap_bit_p (always_executed, i)); 1215 } 1216 1217 /* Finds invariants in LOOP. */ 1218 1219 static void 1220 find_invariants (struct loop *loop) 1221 { 1222 bitmap may_exit = BITMAP_ALLOC (NULL); 1223 bitmap always_reached = BITMAP_ALLOC (NULL); 1224 bitmap has_exit = BITMAP_ALLOC (NULL); 1225 bitmap always_executed = BITMAP_ALLOC (NULL); 1226 basic_block *body = get_loop_body_in_dom_order (loop); 1227 1228 find_exits (loop, body, may_exit, has_exit); 1229 compute_always_reached (loop, body, may_exit, always_reached); 1230 compute_always_reached (loop, body, has_exit, always_executed); 1231 1232 find_defs (loop); 1233 find_invariants_body (loop, body, always_reached, always_executed); 1234 merge_identical_invariants (); 1235 1236 BITMAP_FREE (always_reached); 1237 BITMAP_FREE (always_executed); 1238 BITMAP_FREE (may_exit); 1239 BITMAP_FREE (has_exit); 1240 free (body); 1241 } 1242 1243 /* Frees a list of uses USE. */ 1244 1245 static void 1246 free_use_list (struct use *use) 1247 { 1248 struct use *next; 1249 1250 for (; use; use = next) 1251 { 1252 next = use->next; 1253 free (use); 1254 } 1255 } 1256 1257 /* Return pressure class and number of hard registers (through *NREGS) 1258 for destination of INSN. */ 1259 static enum reg_class 1260 get_pressure_class_and_nregs (rtx_insn *insn, int *nregs) 1261 { 1262 rtx reg; 1263 enum reg_class pressure_class; 1264 rtx set = single_set (insn); 1265 1266 /* Considered invariant insns have only one set. */ 1267 gcc_assert (set != NULL_RTX); 1268 reg = SET_DEST (set); 1269 if (GET_CODE (reg) == SUBREG) 1270 reg = SUBREG_REG (reg); 1271 if (MEM_P (reg)) 1272 { 1273 *nregs = 0; 1274 pressure_class = NO_REGS; 1275 } 1276 else 1277 { 1278 if (! REG_P (reg)) 1279 reg = NULL_RTX; 1280 if (reg == NULL_RTX) 1281 pressure_class = GENERAL_REGS; 1282 else 1283 { 1284 pressure_class = reg_allocno_class (REGNO (reg)); 1285 pressure_class = ira_pressure_class_translate[pressure_class]; 1286 } 1287 *nregs 1288 = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))]; 1289 } 1290 return pressure_class; 1291 } 1292 1293 /* Calculates cost and number of registers needed for moving invariant INV 1294 out of the loop and stores them to *COST and *REGS_NEEDED. *CL will be 1295 the REG_CLASS of INV. Return 1296 -1: if INV is invalid. 1297 0: if INV and its depends_on have same reg_class 1298 1: if INV and its depends_on have different reg_classes. */ 1299 1300 static int 1301 get_inv_cost (struct invariant *inv, int *comp_cost, unsigned *regs_needed, 1302 enum reg_class *cl) 1303 { 1304 int i, acomp_cost; 1305 unsigned aregs_needed[N_REG_CLASSES]; 1306 unsigned depno; 1307 struct invariant *dep; 1308 bitmap_iterator bi; 1309 int ret = 1; 1310 1311 /* Find the representative of the class of the equivalent invariants. */ 1312 inv = invariants[inv->eqto]; 1313 1314 *comp_cost = 0; 1315 if (! flag_ira_loop_pressure) 1316 regs_needed[0] = 0; 1317 else 1318 { 1319 for (i = 0; i < ira_pressure_classes_num; i++) 1320 regs_needed[ira_pressure_classes[i]] = 0; 1321 } 1322 1323 if (inv->move 1324 || inv->stamp == actual_stamp) 1325 return -1; 1326 inv->stamp = actual_stamp; 1327 1328 if (! flag_ira_loop_pressure) 1329 regs_needed[0]++; 1330 else 1331 { 1332 int nregs; 1333 enum reg_class pressure_class; 1334 1335 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs); 1336 regs_needed[pressure_class] += nregs; 1337 *cl = pressure_class; 1338 ret = 0; 1339 } 1340 1341 if (!inv->cheap_address 1342 || inv->def->n_uses == 0 1343 || inv->def->n_addr_uses < inv->def->n_uses 1344 /* Count cost if the inv can't be propagated into address uses. */ 1345 || !inv->def->can_prop_to_addr_uses) 1346 (*comp_cost) += inv->cost * inv->eqno; 1347 1348 #ifdef STACK_REGS 1349 { 1350 /* Hoisting constant pool constants into stack regs may cost more than 1351 just single register. On x87, the balance is affected both by the 1352 small number of FP registers, and by its register stack organization, 1353 that forces us to add compensation code in and around the loop to 1354 shuffle the operands to the top of stack before use, and pop them 1355 from the stack after the loop finishes. 1356 1357 To model this effect, we increase the number of registers needed for 1358 stack registers by two: one register push, and one register pop. 1359 This usually has the effect that FP constant loads from the constant 1360 pool are not moved out of the loop. 1361 1362 Note that this also means that dependent invariants can not be moved. 1363 However, the primary purpose of this pass is to move loop invariant 1364 address arithmetic out of loops, and address arithmetic that depends 1365 on floating point constants is unlikely to ever occur. */ 1366 rtx set = single_set (inv->insn); 1367 if (set 1368 && IS_STACK_MODE (GET_MODE (SET_SRC (set))) 1369 && constant_pool_constant_p (SET_SRC (set))) 1370 { 1371 if (flag_ira_loop_pressure) 1372 regs_needed[ira_stack_reg_pressure_class] += 2; 1373 else 1374 regs_needed[0] += 2; 1375 } 1376 } 1377 #endif 1378 1379 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi) 1380 { 1381 bool check_p; 1382 enum reg_class dep_cl = ALL_REGS; 1383 int dep_ret; 1384 1385 dep = invariants[depno]; 1386 1387 /* If DEP is moved out of the loop, it is not a depends_on any more. */ 1388 if (dep->move) 1389 continue; 1390 1391 dep_ret = get_inv_cost (dep, &acomp_cost, aregs_needed, &dep_cl); 1392 1393 if (! flag_ira_loop_pressure) 1394 check_p = aregs_needed[0] != 0; 1395 else 1396 { 1397 for (i = 0; i < ira_pressure_classes_num; i++) 1398 if (aregs_needed[ira_pressure_classes[i]] != 0) 1399 break; 1400 check_p = i < ira_pressure_classes_num; 1401 1402 if ((dep_ret == 1) || ((dep_ret == 0) && (*cl != dep_cl))) 1403 { 1404 *cl = ALL_REGS; 1405 ret = 1; 1406 } 1407 } 1408 if (check_p 1409 /* We need to check always_executed, since if the original value of 1410 the invariant may be preserved, we may need to keep it in a 1411 separate register. TODO check whether the register has an 1412 use outside of the loop. */ 1413 && dep->always_executed 1414 && !dep->def->uses->next) 1415 { 1416 /* If this is a single use, after moving the dependency we will not 1417 need a new register. */ 1418 if (! flag_ira_loop_pressure) 1419 aregs_needed[0]--; 1420 else 1421 { 1422 int nregs; 1423 enum reg_class pressure_class; 1424 1425 pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs); 1426 aregs_needed[pressure_class] -= nregs; 1427 } 1428 } 1429 1430 if (! flag_ira_loop_pressure) 1431 regs_needed[0] += aregs_needed[0]; 1432 else 1433 { 1434 for (i = 0; i < ira_pressure_classes_num; i++) 1435 regs_needed[ira_pressure_classes[i]] 1436 += aregs_needed[ira_pressure_classes[i]]; 1437 } 1438 (*comp_cost) += acomp_cost; 1439 } 1440 return ret; 1441 } 1442 1443 /* Calculates gain for eliminating invariant INV. REGS_USED is the number 1444 of registers used in the loop, NEW_REGS is the number of new variables 1445 already added due to the invariant motion. The number of registers needed 1446 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed 1447 through to estimate_reg_pressure_cost. */ 1448 1449 static int 1450 gain_for_invariant (struct invariant *inv, unsigned *regs_needed, 1451 unsigned *new_regs, unsigned regs_used, 1452 bool speed, bool call_p) 1453 { 1454 int comp_cost, size_cost; 1455 /* Workaround -Wmaybe-uninitialized false positive during 1456 profiledbootstrap by initializing it. */ 1457 enum reg_class cl = NO_REGS; 1458 int ret; 1459 1460 actual_stamp++; 1461 1462 ret = get_inv_cost (inv, &comp_cost, regs_needed, &cl); 1463 1464 if (! flag_ira_loop_pressure) 1465 { 1466 size_cost = (estimate_reg_pressure_cost (new_regs[0] + regs_needed[0], 1467 regs_used, speed, call_p) 1468 - estimate_reg_pressure_cost (new_regs[0], 1469 regs_used, speed, call_p)); 1470 } 1471 else if (ret < 0) 1472 return -1; 1473 else if ((ret == 0) && (cl == NO_REGS)) 1474 /* Hoist it anyway since it does not impact register pressure. */ 1475 return 1; 1476 else 1477 { 1478 int i; 1479 enum reg_class pressure_class; 1480 1481 for (i = 0; i < ira_pressure_classes_num; i++) 1482 { 1483 pressure_class = ira_pressure_classes[i]; 1484 1485 if (!reg_classes_intersect_p (pressure_class, cl)) 1486 continue; 1487 1488 if ((int) new_regs[pressure_class] 1489 + (int) regs_needed[pressure_class] 1490 + LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class] 1491 + IRA_LOOP_RESERVED_REGS 1492 > ira_class_hard_regs_num[pressure_class]) 1493 break; 1494 } 1495 if (i < ira_pressure_classes_num) 1496 /* There will be register pressure excess and we want not to 1497 make this loop invariant motion. All loop invariants with 1498 non-positive gains will be rejected in function 1499 find_invariants_to_move. Therefore we return the negative 1500 number here. 1501 1502 One could think that this rejects also expensive loop 1503 invariant motions and this will hurt code performance. 1504 However numerous experiments with different heuristics 1505 taking invariant cost into account did not confirm this 1506 assumption. There are possible explanations for this 1507 result: 1508 o probably all expensive invariants were already moved out 1509 of the loop by PRE and gimple invariant motion pass. 1510 o expensive invariant execution will be hidden by insn 1511 scheduling or OOO processor hardware because usually such 1512 invariants have a lot of freedom to be executed 1513 out-of-order. 1514 Another reason for ignoring invariant cost vs spilling cost 1515 heuristics is also in difficulties to evaluate accurately 1516 spill cost at this stage. */ 1517 return -1; 1518 else 1519 size_cost = 0; 1520 } 1521 1522 return comp_cost - size_cost; 1523 } 1524 1525 /* Finds invariant with best gain for moving. Returns the gain, stores 1526 the invariant in *BEST and number of registers needed for it to 1527 *REGS_NEEDED. REGS_USED is the number of registers used in the loop. 1528 NEW_REGS is the number of new variables already added due to invariant 1529 motion. */ 1530 1531 static int 1532 best_gain_for_invariant (struct invariant **best, unsigned *regs_needed, 1533 unsigned *new_regs, unsigned regs_used, 1534 bool speed, bool call_p) 1535 { 1536 struct invariant *inv; 1537 int i, gain = 0, again; 1538 unsigned aregs_needed[N_REG_CLASSES], invno; 1539 1540 FOR_EACH_VEC_ELT (invariants, invno, inv) 1541 { 1542 if (inv->move) 1543 continue; 1544 1545 /* Only consider the "representatives" of equivalent invariants. */ 1546 if (inv->eqto != inv->invno) 1547 continue; 1548 1549 again = gain_for_invariant (inv, aregs_needed, new_regs, regs_used, 1550 speed, call_p); 1551 if (again > gain) 1552 { 1553 gain = again; 1554 *best = inv; 1555 if (! flag_ira_loop_pressure) 1556 regs_needed[0] = aregs_needed[0]; 1557 else 1558 { 1559 for (i = 0; i < ira_pressure_classes_num; i++) 1560 regs_needed[ira_pressure_classes[i]] 1561 = aregs_needed[ira_pressure_classes[i]]; 1562 } 1563 } 1564 } 1565 1566 return gain; 1567 } 1568 1569 /* Marks invariant INVNO and all its dependencies for moving. */ 1570 1571 static void 1572 set_move_mark (unsigned invno, int gain) 1573 { 1574 struct invariant *inv = invariants[invno]; 1575 bitmap_iterator bi; 1576 1577 /* Find the representative of the class of the equivalent invariants. */ 1578 inv = invariants[inv->eqto]; 1579 1580 if (inv->move) 1581 return; 1582 inv->move = true; 1583 1584 if (dump_file) 1585 { 1586 if (gain >= 0) 1587 fprintf (dump_file, "Decided to move invariant %d -- gain %d\n", 1588 invno, gain); 1589 else 1590 fprintf (dump_file, "Decided to move dependent invariant %d\n", 1591 invno); 1592 }; 1593 1594 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, invno, bi) 1595 { 1596 set_move_mark (invno, -1); 1597 } 1598 } 1599 1600 /* Determines which invariants to move. */ 1601 1602 static void 1603 find_invariants_to_move (bool speed, bool call_p) 1604 { 1605 int gain; 1606 unsigned i, regs_used, regs_needed[N_REG_CLASSES], new_regs[N_REG_CLASSES]; 1607 struct invariant *inv = NULL; 1608 1609 if (!invariants.length ()) 1610 return; 1611 1612 if (flag_ira_loop_pressure) 1613 /* REGS_USED is actually never used when the flag is on. */ 1614 regs_used = 0; 1615 else 1616 /* We do not really do a good job in estimating number of 1617 registers used; we put some initial bound here to stand for 1618 induction variables etc. that we do not detect. */ 1619 { 1620 unsigned int n_regs = DF_REG_SIZE (df); 1621 1622 regs_used = 2; 1623 1624 for (i = 0; i < n_regs; i++) 1625 { 1626 if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i)) 1627 { 1628 /* This is a value that is used but not changed inside loop. */ 1629 regs_used++; 1630 } 1631 } 1632 } 1633 1634 if (! flag_ira_loop_pressure) 1635 new_regs[0] = regs_needed[0] = 0; 1636 else 1637 { 1638 for (i = 0; (int) i < ira_pressure_classes_num; i++) 1639 new_regs[ira_pressure_classes[i]] = 0; 1640 } 1641 while ((gain = best_gain_for_invariant (&inv, regs_needed, 1642 new_regs, regs_used, 1643 speed, call_p)) > 0) 1644 { 1645 set_move_mark (inv->invno, gain); 1646 if (! flag_ira_loop_pressure) 1647 new_regs[0] += regs_needed[0]; 1648 else 1649 { 1650 for (i = 0; (int) i < ira_pressure_classes_num; i++) 1651 new_regs[ira_pressure_classes[i]] 1652 += regs_needed[ira_pressure_classes[i]]; 1653 } 1654 } 1655 } 1656 1657 /* Replace the uses, reached by the definition of invariant INV, by REG. 1658 1659 IN_GROUP is nonzero if this is part of a group of changes that must be 1660 performed as a group. In that case, the changes will be stored. The 1661 function `apply_change_group' will validate and apply the changes. */ 1662 1663 static int 1664 replace_uses (struct invariant *inv, rtx reg, bool in_group) 1665 { 1666 /* Replace the uses we know to be dominated. It saves work for copy 1667 propagation, and also it is necessary so that dependent invariants 1668 are computed right. */ 1669 if (inv->def) 1670 { 1671 struct use *use; 1672 for (use = inv->def->uses; use; use = use->next) 1673 validate_change (use->insn, use->pos, reg, true); 1674 1675 /* If we aren't part of a larger group, apply the changes now. */ 1676 if (!in_group) 1677 return apply_change_group (); 1678 } 1679 1680 return 1; 1681 } 1682 1683 /* Whether invariant INV setting REG can be moved out of LOOP, at the end of 1684 the block preceding its header. */ 1685 1686 static bool 1687 can_move_invariant_reg (struct loop *loop, struct invariant *inv, rtx reg) 1688 { 1689 df_ref def, use; 1690 unsigned int dest_regno, defs_in_loop_count = 0; 1691 rtx_insn *insn = inv->insn; 1692 basic_block bb = BLOCK_FOR_INSN (inv->insn); 1693 1694 /* We ignore hard register and memory access for cost and complexity reasons. 1695 Hard register are few at this stage and expensive to consider as they 1696 require building a separate data flow. Memory access would require using 1697 df_simulate_* and can_move_insns_across functions and is more complex. */ 1698 if (!REG_P (reg) || HARD_REGISTER_P (reg)) 1699 return false; 1700 1701 /* Check whether the set is always executed. We could omit this condition if 1702 we know that the register is unused outside of the loop, but it does not 1703 seem worth finding out. */ 1704 if (!inv->always_executed) 1705 return false; 1706 1707 /* Check that all uses that would be dominated by def are already dominated 1708 by it. */ 1709 dest_regno = REGNO (reg); 1710 for (use = DF_REG_USE_CHAIN (dest_regno); use; use = DF_REF_NEXT_REG (use)) 1711 { 1712 rtx_insn *use_insn; 1713 basic_block use_bb; 1714 1715 use_insn = DF_REF_INSN (use); 1716 use_bb = BLOCK_FOR_INSN (use_insn); 1717 1718 /* Ignore instruction considered for moving. */ 1719 if (use_insn == insn) 1720 continue; 1721 1722 /* Don't consider uses outside loop. */ 1723 if (!flow_bb_inside_loop_p (loop, use_bb)) 1724 continue; 1725 1726 /* Don't move if a use is not dominated by def in insn. */ 1727 if (use_bb == bb && DF_INSN_LUID (insn) >= DF_INSN_LUID (use_insn)) 1728 return false; 1729 if (!dominated_by_p (CDI_DOMINATORS, use_bb, bb)) 1730 return false; 1731 } 1732 1733 /* Check for other defs. Any other def in the loop might reach a use 1734 currently reached by the def in insn. */ 1735 for (def = DF_REG_DEF_CHAIN (dest_regno); def; def = DF_REF_NEXT_REG (def)) 1736 { 1737 basic_block def_bb = DF_REF_BB (def); 1738 1739 /* Defs in exit block cannot reach a use they weren't already. */ 1740 if (single_succ_p (def_bb)) 1741 { 1742 basic_block def_bb_succ; 1743 1744 def_bb_succ = single_succ (def_bb); 1745 if (!flow_bb_inside_loop_p (loop, def_bb_succ)) 1746 continue; 1747 } 1748 1749 if (++defs_in_loop_count > 1) 1750 return false; 1751 } 1752 1753 return true; 1754 } 1755 1756 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false 1757 otherwise. */ 1758 1759 static bool 1760 move_invariant_reg (struct loop *loop, unsigned invno) 1761 { 1762 struct invariant *inv = invariants[invno]; 1763 struct invariant *repr = invariants[inv->eqto]; 1764 unsigned i; 1765 basic_block preheader = loop_preheader_edge (loop)->src; 1766 rtx reg, set, dest, note; 1767 bitmap_iterator bi; 1768 int regno = -1; 1769 1770 if (inv->reg) 1771 return true; 1772 if (!repr->move) 1773 return false; 1774 1775 /* If this is a representative of the class of equivalent invariants, 1776 really move the invariant. Otherwise just replace its use with 1777 the register used for the representative. */ 1778 if (inv == repr) 1779 { 1780 if (inv->depends_on) 1781 { 1782 EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, i, bi) 1783 { 1784 if (!move_invariant_reg (loop, i)) 1785 goto fail; 1786 } 1787 } 1788 1789 /* If possible, just move the set out of the loop. Otherwise, we 1790 need to create a temporary register. */ 1791 set = single_set (inv->insn); 1792 reg = dest = SET_DEST (set); 1793 if (GET_CODE (reg) == SUBREG) 1794 reg = SUBREG_REG (reg); 1795 if (REG_P (reg)) 1796 regno = REGNO (reg); 1797 1798 if (!can_move_invariant_reg (loop, inv, dest)) 1799 { 1800 reg = gen_reg_rtx_and_attrs (dest); 1801 1802 /* Try replacing the destination by a new pseudoregister. */ 1803 validate_change (inv->insn, &SET_DEST (set), reg, true); 1804 1805 /* As well as all the dominated uses. */ 1806 replace_uses (inv, reg, true); 1807 1808 /* And validate all the changes. */ 1809 if (!apply_change_group ()) 1810 goto fail; 1811 1812 emit_insn_after (gen_move_insn (dest, reg), inv->insn); 1813 } 1814 else if (dump_file) 1815 fprintf (dump_file, "Invariant %d moved without introducing a new " 1816 "temporary register\n", invno); 1817 reorder_insns (inv->insn, inv->insn, BB_END (preheader)); 1818 df_recompute_luids (preheader); 1819 1820 /* If there is a REG_EQUAL note on the insn we just moved, and the 1821 insn is in a basic block that is not always executed or the note 1822 contains something for which we don't know the invariant status, 1823 the note may no longer be valid after we move the insn. Note that 1824 uses in REG_EQUAL notes are taken into account in the computation 1825 of invariants, so it is safe to retain the note even if it contains 1826 register references for which we know the invariant status. */ 1827 if ((note = find_reg_note (inv->insn, REG_EQUAL, NULL_RTX)) 1828 && (!inv->always_executed 1829 || !check_maybe_invariant (XEXP (note, 0)))) 1830 remove_note (inv->insn, note); 1831 } 1832 else 1833 { 1834 if (!move_invariant_reg (loop, repr->invno)) 1835 goto fail; 1836 reg = repr->reg; 1837 regno = repr->orig_regno; 1838 if (!replace_uses (inv, reg, false)) 1839 goto fail; 1840 set = single_set (inv->insn); 1841 emit_insn_after (gen_move_insn (SET_DEST (set), reg), inv->insn); 1842 delete_insn (inv->insn); 1843 } 1844 1845 inv->reg = reg; 1846 inv->orig_regno = regno; 1847 1848 return true; 1849 1850 fail: 1851 /* If we failed, clear move flag, so that we do not try to move inv 1852 again. */ 1853 if (dump_file) 1854 fprintf (dump_file, "Failed to move invariant %d\n", invno); 1855 inv->move = false; 1856 inv->reg = NULL_RTX; 1857 inv->orig_regno = -1; 1858 1859 return false; 1860 } 1861 1862 /* Move selected invariant out of the LOOP. Newly created regs are marked 1863 in TEMPORARY_REGS. */ 1864 1865 static void 1866 move_invariants (struct loop *loop) 1867 { 1868 struct invariant *inv; 1869 unsigned i; 1870 1871 FOR_EACH_VEC_ELT (invariants, i, inv) 1872 move_invariant_reg (loop, i); 1873 if (flag_ira_loop_pressure && resize_reg_info ()) 1874 { 1875 FOR_EACH_VEC_ELT (invariants, i, inv) 1876 if (inv->reg != NULL_RTX) 1877 { 1878 if (inv->orig_regno >= 0) 1879 setup_reg_classes (REGNO (inv->reg), 1880 reg_preferred_class (inv->orig_regno), 1881 reg_alternate_class (inv->orig_regno), 1882 reg_allocno_class (inv->orig_regno)); 1883 else 1884 setup_reg_classes (REGNO (inv->reg), 1885 GENERAL_REGS, NO_REGS, GENERAL_REGS); 1886 } 1887 } 1888 } 1889 1890 /* Initializes invariant motion data. */ 1891 1892 static void 1893 init_inv_motion_data (void) 1894 { 1895 actual_stamp = 1; 1896 1897 invariants.create (100); 1898 } 1899 1900 /* Frees the data allocated by invariant motion. */ 1901 1902 static void 1903 free_inv_motion_data (void) 1904 { 1905 unsigned i; 1906 struct def *def; 1907 struct invariant *inv; 1908 1909 check_invariant_table_size (); 1910 for (i = 0; i < DF_DEFS_TABLE_SIZE (); i++) 1911 { 1912 inv = invariant_table[i]; 1913 if (inv) 1914 { 1915 def = inv->def; 1916 gcc_assert (def != NULL); 1917 1918 free_use_list (def->uses); 1919 free (def); 1920 invariant_table[i] = NULL; 1921 } 1922 } 1923 1924 FOR_EACH_VEC_ELT (invariants, i, inv) 1925 { 1926 BITMAP_FREE (inv->depends_on); 1927 free (inv); 1928 } 1929 invariants.release (); 1930 } 1931 1932 /* Move the invariants out of the LOOP. */ 1933 1934 static void 1935 move_single_loop_invariants (struct loop *loop) 1936 { 1937 init_inv_motion_data (); 1938 1939 find_invariants (loop); 1940 find_invariants_to_move (optimize_loop_for_speed_p (loop), 1941 LOOP_DATA (loop)->has_call); 1942 move_invariants (loop); 1943 1944 free_inv_motion_data (); 1945 } 1946 1947 /* Releases the auxiliary data for LOOP. */ 1948 1949 static void 1950 free_loop_data (struct loop *loop) 1951 { 1952 struct loop_data *data = LOOP_DATA (loop); 1953 if (!data) 1954 return; 1955 1956 bitmap_clear (&LOOP_DATA (loop)->regs_ref); 1957 bitmap_clear (&LOOP_DATA (loop)->regs_live); 1958 free (data); 1959 loop->aux = NULL; 1960 } 1961 1962 1963 1964 /* Registers currently living. */ 1965 static bitmap_head curr_regs_live; 1966 1967 /* Current reg pressure for each pressure class. */ 1968 static int curr_reg_pressure[N_REG_CLASSES]; 1969 1970 /* Record all regs that are set in any one insn. Communication from 1971 mark_reg_{store,clobber} and global_conflicts. Asm can refer to 1972 all hard-registers. */ 1973 static rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS 1974 ? FIRST_PSEUDO_REGISTER : MAX_RECOG_OPERANDS) * 2]; 1975 /* Number of regs stored in the previous array. */ 1976 static int n_regs_set; 1977 1978 /* Return pressure class and number of needed hard registers (through 1979 *NREGS) of register REGNO. */ 1980 static enum reg_class 1981 get_regno_pressure_class (int regno, int *nregs) 1982 { 1983 if (regno >= FIRST_PSEUDO_REGISTER) 1984 { 1985 enum reg_class pressure_class; 1986 1987 pressure_class = reg_allocno_class (regno); 1988 pressure_class = ira_pressure_class_translate[pressure_class]; 1989 *nregs 1990 = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)]; 1991 return pressure_class; 1992 } 1993 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno) 1994 && ! TEST_HARD_REG_BIT (eliminable_regset, regno)) 1995 { 1996 *nregs = 1; 1997 return ira_pressure_class_translate[REGNO_REG_CLASS (regno)]; 1998 } 1999 else 2000 { 2001 *nregs = 0; 2002 return NO_REGS; 2003 } 2004 } 2005 2006 /* Increase (if INCR_P) or decrease current register pressure for 2007 register REGNO. */ 2008 static void 2009 change_pressure (int regno, bool incr_p) 2010 { 2011 int nregs; 2012 enum reg_class pressure_class; 2013 2014 pressure_class = get_regno_pressure_class (regno, &nregs); 2015 if (! incr_p) 2016 curr_reg_pressure[pressure_class] -= nregs; 2017 else 2018 { 2019 curr_reg_pressure[pressure_class] += nregs; 2020 if (LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class] 2021 < curr_reg_pressure[pressure_class]) 2022 LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class] 2023 = curr_reg_pressure[pressure_class]; 2024 } 2025 } 2026 2027 /* Mark REGNO birth. */ 2028 static void 2029 mark_regno_live (int regno) 2030 { 2031 struct loop *loop; 2032 2033 for (loop = curr_loop; 2034 loop != current_loops->tree_root; 2035 loop = loop_outer (loop)) 2036 bitmap_set_bit (&LOOP_DATA (loop)->regs_live, regno); 2037 if (!bitmap_set_bit (&curr_regs_live, regno)) 2038 return; 2039 change_pressure (regno, true); 2040 } 2041 2042 /* Mark REGNO death. */ 2043 static void 2044 mark_regno_death (int regno) 2045 { 2046 if (! bitmap_clear_bit (&curr_regs_live, regno)) 2047 return; 2048 change_pressure (regno, false); 2049 } 2050 2051 /* Mark setting register REG. */ 2052 static void 2053 mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED, 2054 void *data ATTRIBUTE_UNUSED) 2055 { 2056 if (GET_CODE (reg) == SUBREG) 2057 reg = SUBREG_REG (reg); 2058 2059 if (! REG_P (reg)) 2060 return; 2061 2062 regs_set[n_regs_set++] = reg; 2063 2064 unsigned int end_regno = END_REGNO (reg); 2065 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno) 2066 mark_regno_live (regno); 2067 } 2068 2069 /* Mark clobbering register REG. */ 2070 static void 2071 mark_reg_clobber (rtx reg, const_rtx setter, void *data) 2072 { 2073 if (GET_CODE (setter) == CLOBBER) 2074 mark_reg_store (reg, setter, data); 2075 } 2076 2077 /* Mark register REG death. */ 2078 static void 2079 mark_reg_death (rtx reg) 2080 { 2081 unsigned int end_regno = END_REGNO (reg); 2082 for (unsigned int regno = REGNO (reg); regno < end_regno; ++regno) 2083 mark_regno_death (regno); 2084 } 2085 2086 /* Mark occurrence of registers in X for the current loop. */ 2087 static void 2088 mark_ref_regs (rtx x) 2089 { 2090 RTX_CODE code; 2091 int i; 2092 const char *fmt; 2093 2094 if (!x) 2095 return; 2096 2097 code = GET_CODE (x); 2098 if (code == REG) 2099 { 2100 struct loop *loop; 2101 2102 for (loop = curr_loop; 2103 loop != current_loops->tree_root; 2104 loop = loop_outer (loop)) 2105 bitmap_set_bit (&LOOP_DATA (loop)->regs_ref, REGNO (x)); 2106 return; 2107 } 2108 2109 fmt = GET_RTX_FORMAT (code); 2110 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) 2111 if (fmt[i] == 'e') 2112 mark_ref_regs (XEXP (x, i)); 2113 else if (fmt[i] == 'E') 2114 { 2115 int j; 2116 2117 for (j = 0; j < XVECLEN (x, i); j++) 2118 mark_ref_regs (XVECEXP (x, i, j)); 2119 } 2120 } 2121 2122 /* Calculate register pressure in the loops. */ 2123 static void 2124 calculate_loop_reg_pressure (void) 2125 { 2126 int i; 2127 unsigned int j; 2128 bitmap_iterator bi; 2129 basic_block bb; 2130 rtx_insn *insn; 2131 rtx link; 2132 struct loop *loop, *parent; 2133 2134 FOR_EACH_LOOP (loop, 0) 2135 if (loop->aux == NULL) 2136 { 2137 loop->aux = xcalloc (1, sizeof (struct loop_data)); 2138 bitmap_initialize (&LOOP_DATA (loop)->regs_ref, ®_obstack); 2139 bitmap_initialize (&LOOP_DATA (loop)->regs_live, ®_obstack); 2140 } 2141 ira_setup_eliminable_regset (); 2142 bitmap_initialize (&curr_regs_live, ®_obstack); 2143 FOR_EACH_BB_FN (bb, cfun) 2144 { 2145 curr_loop = bb->loop_father; 2146 if (curr_loop == current_loops->tree_root) 2147 continue; 2148 2149 for (loop = curr_loop; 2150 loop != current_loops->tree_root; 2151 loop = loop_outer (loop)) 2152 bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN (bb)); 2153 2154 bitmap_copy (&curr_regs_live, DF_LR_IN (bb)); 2155 for (i = 0; i < ira_pressure_classes_num; i++) 2156 curr_reg_pressure[ira_pressure_classes[i]] = 0; 2157 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live, 0, j, bi) 2158 change_pressure (j, true); 2159 2160 FOR_BB_INSNS (bb, insn) 2161 { 2162 if (! NONDEBUG_INSN_P (insn)) 2163 continue; 2164 2165 mark_ref_regs (PATTERN (insn)); 2166 n_regs_set = 0; 2167 note_stores (PATTERN (insn), mark_reg_clobber, NULL); 2168 2169 /* Mark any registers dead after INSN as dead now. */ 2170 2171 for (link = REG_NOTES (insn); link; link = XEXP (link, 1)) 2172 if (REG_NOTE_KIND (link) == REG_DEAD) 2173 mark_reg_death (XEXP (link, 0)); 2174 2175 /* Mark any registers set in INSN as live, 2176 and mark them as conflicting with all other live regs. 2177 Clobbers are processed again, so they conflict with 2178 the registers that are set. */ 2179 2180 note_stores (PATTERN (insn), mark_reg_store, NULL); 2181 2182 if (AUTO_INC_DEC) 2183 for (link = REG_NOTES (insn); link; link = XEXP (link, 1)) 2184 if (REG_NOTE_KIND (link) == REG_INC) 2185 mark_reg_store (XEXP (link, 0), NULL_RTX, NULL); 2186 2187 while (n_regs_set-- > 0) 2188 { 2189 rtx note = find_regno_note (insn, REG_UNUSED, 2190 REGNO (regs_set[n_regs_set])); 2191 if (! note) 2192 continue; 2193 2194 mark_reg_death (XEXP (note, 0)); 2195 } 2196 } 2197 } 2198 bitmap_clear (&curr_regs_live); 2199 if (flag_ira_region == IRA_REGION_MIXED 2200 || flag_ira_region == IRA_REGION_ALL) 2201 FOR_EACH_LOOP (loop, 0) 2202 { 2203 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi) 2204 if (! bitmap_bit_p (&LOOP_DATA (loop)->regs_ref, j)) 2205 { 2206 enum reg_class pressure_class; 2207 int nregs; 2208 2209 pressure_class = get_regno_pressure_class (j, &nregs); 2210 LOOP_DATA (loop)->max_reg_pressure[pressure_class] -= nregs; 2211 } 2212 } 2213 if (dump_file == NULL) 2214 return; 2215 FOR_EACH_LOOP (loop, 0) 2216 { 2217 parent = loop_outer (loop); 2218 fprintf (dump_file, "\n Loop %d (parent %d, header bb%d, depth %d)\n", 2219 loop->num, (parent == NULL ? -1 : parent->num), 2220 loop->header->index, loop_depth (loop)); 2221 fprintf (dump_file, "\n ref. regnos:"); 2222 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_ref, 0, j, bi) 2223 fprintf (dump_file, " %d", j); 2224 fprintf (dump_file, "\n live regnos:"); 2225 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi) 2226 fprintf (dump_file, " %d", j); 2227 fprintf (dump_file, "\n Pressure:"); 2228 for (i = 0; (int) i < ira_pressure_classes_num; i++) 2229 { 2230 enum reg_class pressure_class; 2231 2232 pressure_class = ira_pressure_classes[i]; 2233 if (LOOP_DATA (loop)->max_reg_pressure[pressure_class] == 0) 2234 continue; 2235 fprintf (dump_file, " %s=%d", reg_class_names[pressure_class], 2236 LOOP_DATA (loop)->max_reg_pressure[pressure_class]); 2237 } 2238 fprintf (dump_file, "\n"); 2239 } 2240 } 2241 2242 2243 2244 /* Move the invariants out of the loops. */ 2245 2246 void 2247 move_loop_invariants (void) 2248 { 2249 struct loop *loop; 2250 2251 if (flag_ira_loop_pressure) 2252 { 2253 df_analyze (); 2254 regstat_init_n_sets_and_refs (); 2255 ira_set_pseudo_classes (true, dump_file); 2256 calculate_loop_reg_pressure (); 2257 regstat_free_n_sets_and_refs (); 2258 } 2259 df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN); 2260 /* Process the loops, innermost first. */ 2261 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST) 2262 { 2263 curr_loop = loop; 2264 /* move_single_loop_invariants for very large loops 2265 is time consuming and might need a lot of memory. */ 2266 if (loop->num_nodes <= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP) 2267 move_single_loop_invariants (loop); 2268 } 2269 2270 FOR_EACH_LOOP (loop, 0) 2271 { 2272 free_loop_data (loop); 2273 } 2274 2275 if (flag_ira_loop_pressure) 2276 /* There is no sense to keep this info because it was most 2277 probably outdated by subsequent passes. */ 2278 free_reg_info (); 2279 free (invariant_table); 2280 invariant_table = NULL; 2281 invariant_table_size = 0; 2282 2283 checking_verify_flow_info (); 2284 } 2285