1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
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.
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").
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.
34 Then we move the selected invariants out of the loop, creating a new
35 temporaries for them if necessary. */
39 #include "coretypes.h"
47 #include "insn-config.h"
58 /* The data stored for the loop. */
62 struct loop
*outermost_exit
; /* The outermost exit of the loop. */
63 bool has_call
; /* True if the loop contains a call. */
64 /* Maximal register pressure inside loop for given register class
65 (defined only for the pressure classes). */
66 int max_reg_pressure
[N_REG_CLASSES
];
67 /* Loop regs referenced and live pseudo-registers. */
69 bitmap_head regs_live
;
72 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
74 /* The description of an use. */
78 rtx
*pos
; /* Position of the use. */
79 rtx_insn
*insn
; /* The insn in that the use occurs. */
80 unsigned addr_use_p
; /* Whether the use occurs in an address. */
81 struct use
*next
; /* Next use in the list. */
84 /* The description of a def. */
88 struct use
*uses
; /* The list of uses that are uniquely reached
90 unsigned n_uses
; /* Number of such uses. */
91 unsigned n_addr_uses
; /* Number of uses in addresses. */
92 unsigned invno
; /* The corresponding invariant. */
93 bool can_prop_to_addr_uses
; /* True if the corresponding inv can be
94 propagated into its address uses. */
97 /* The data stored for each invariant. */
101 /* The number of the invariant. */
104 /* The number of the invariant with the same value. */
107 /* The number of invariants which eqto this. */
110 /* If we moved the invariant out of the loop, the register that contains its
114 /* If we moved the invariant out of the loop, the original regno
115 that contained its value. */
118 /* The definition of the invariant. */
121 /* The insn in that it is defined. */
124 /* Whether it is always executed. */
125 bool always_executed
;
127 /* Whether to move the invariant. */
130 /* Whether the invariant is cheap when used as an address. */
133 /* Cost of the invariant. */
136 /* The invariants it depends on. */
139 /* Used for detecting already visited invariants during determining
140 costs of movements. */
144 /* Currently processed loop. */
145 static struct loop
*curr_loop
;
147 /* Table of invariants indexed by the df_ref uid field. */
149 static unsigned int invariant_table_size
= 0;
150 static struct invariant
** invariant_table
;
152 /* Entry for hash table of invariant expressions. */
154 struct invariant_expr_entry
157 struct invariant
*inv
;
169 /* The actual stamp for marking already visited invariants during determining
170 costs of movements. */
172 static unsigned actual_stamp
;
174 typedef struct invariant
*invariant_p
;
177 /* The invariants. */
179 static vec
<invariant_p
> invariants
;
181 /* Check the size of the invariant table and realloc if necessary. */
184 check_invariant_table_size (void)
186 if (invariant_table_size
< DF_DEFS_TABLE_SIZE ())
188 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
189 invariant_table
= XRESIZEVEC (struct invariant
*, invariant_table
, new_size
);
190 memset (&invariant_table
[invariant_table_size
], 0,
191 (new_size
- invariant_table_size
) * sizeof (struct invariant
*));
192 invariant_table_size
= new_size
;
196 /* Test for possibility of invariantness of X. */
199 check_maybe_invariant (rtx x
)
201 enum rtx_code code
= GET_CODE (x
);
215 case UNSPEC_VOLATILE
:
223 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
224 It should not be hard, and might be faster than "elsewhere". */
226 /* Just handle the most trivial case where we load from an unchanging
227 location (most importantly, pic tables). */
228 if (MEM_READONLY_P (x
) && !MEM_VOLATILE_P (x
))
234 /* Don't mess with insns declared volatile. */
235 if (MEM_VOLATILE_P (x
))
243 fmt
= GET_RTX_FORMAT (code
);
244 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
248 if (!check_maybe_invariant (XEXP (x
, i
)))
251 else if (fmt
[i
] == 'E')
253 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
254 if (!check_maybe_invariant (XVECEXP (x
, i
, j
)))
262 /* Returns the invariant definition for USE, or NULL if USE is not
265 static struct invariant
*
266 invariant_for_use (df_ref use
)
268 struct df_link
*defs
;
270 basic_block bb
= DF_REF_BB (use
), def_bb
;
272 if (DF_REF_FLAGS (use
) & DF_REF_READ_WRITE
)
275 defs
= DF_REF_CHAIN (use
);
276 if (!defs
|| defs
->next
)
279 check_invariant_table_size ();
280 if (!invariant_table
[DF_REF_ID (def
)])
283 def_bb
= DF_REF_BB (def
);
284 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
286 return invariant_table
[DF_REF_ID (def
)];
289 /* Computes hash value for invariant expression X in INSN. */
292 hash_invariant_expr_1 (rtx_insn
*insn
, rtx x
)
294 enum rtx_code code
= GET_CODE (x
);
297 hashval_t val
= code
;
300 struct invariant
*inv
;
308 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
311 use
= df_find_use (insn
, x
);
313 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
314 inv
= invariant_for_use (use
);
316 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
318 gcc_assert (inv
->eqto
!= ~0u);
325 fmt
= GET_RTX_FORMAT (code
);
326 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
329 val
^= hash_invariant_expr_1 (insn
, XEXP (x
, i
));
330 else if (fmt
[i
] == 'E')
332 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
333 val
^= hash_invariant_expr_1 (insn
, XVECEXP (x
, i
, j
));
335 else if (fmt
[i
] == 'i' || fmt
[i
] == 'n')
342 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
343 and INSN2 have always the same value. */
346 invariant_expr_equal_p (rtx_insn
*insn1
, rtx e1
, rtx_insn
*insn2
, rtx e2
)
348 enum rtx_code code
= GET_CODE (e1
);
352 struct invariant
*inv1
= NULL
, *inv2
= NULL
;
355 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
356 the other one. If both are VOIDmode, we rely on the caller of this
357 function to verify that their modes are the same. */
358 if (code
!= GET_CODE (e2
) || GET_MODE (e1
) != GET_MODE (e2
))
367 return rtx_equal_p (e1
, e2
);
370 use1
= df_find_use (insn1
, e1
);
371 use2
= df_find_use (insn2
, e2
);
373 inv1
= invariant_for_use (use1
);
375 inv2
= invariant_for_use (use2
);
378 return rtx_equal_p (e1
, e2
);
383 gcc_assert (inv1
->eqto
!= ~0u);
384 gcc_assert (inv2
->eqto
!= ~0u);
385 return inv1
->eqto
== inv2
->eqto
;
391 fmt
= GET_RTX_FORMAT (code
);
392 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
399 if (!invariant_expr_equal_p (insn1
, sub1
, insn2
, sub2
))
403 else if (fmt
[i
] == 'E')
405 if (XVECLEN (e1
, i
) != XVECLEN (e2
, i
))
408 for (j
= 0; j
< XVECLEN (e1
, i
); j
++)
410 sub1
= XVECEXP (e1
, i
, j
);
411 sub2
= XVECEXP (e2
, i
, j
);
413 if (!invariant_expr_equal_p (insn1
, sub1
, insn2
, sub2
))
417 else if (fmt
[i
] == 'i' || fmt
[i
] == 'n')
419 if (XINT (e1
, i
) != XINT (e2
, i
))
422 /* Unhandled type of subexpression, we fail conservatively. */
430 struct invariant_expr_hasher
: free_ptr_hash
<invariant_expr_entry
>
432 static inline hashval_t
hash (const invariant_expr_entry
*);
433 static inline bool equal (const invariant_expr_entry
*,
434 const invariant_expr_entry
*);
437 /* Returns hash value for invariant expression entry ENTRY. */
440 invariant_expr_hasher::hash (const invariant_expr_entry
*entry
)
445 /* Compares invariant expression entries ENTRY1 and ENTRY2. */
448 invariant_expr_hasher::equal (const invariant_expr_entry
*entry1
,
449 const invariant_expr_entry
*entry2
)
451 if (entry1
->mode
!= entry2
->mode
)
454 return invariant_expr_equal_p (entry1
->inv
->insn
, entry1
->expr
,
455 entry2
->inv
->insn
, entry2
->expr
);
458 typedef hash_table
<invariant_expr_hasher
> invariant_htab_type
;
460 /* Checks whether invariant with value EXPR in machine mode MODE is
461 recorded in EQ. If this is the case, return the invariant. Otherwise
462 insert INV to the table for this expression and return INV. */
464 static struct invariant
*
465 find_or_insert_inv (invariant_htab_type
*eq
, rtx expr
, machine_mode mode
,
466 struct invariant
*inv
)
468 hashval_t hash
= hash_invariant_expr_1 (inv
->insn
, expr
);
469 struct invariant_expr_entry
*entry
;
470 struct invariant_expr_entry pentry
;
471 invariant_expr_entry
**slot
;
476 slot
= eq
->find_slot_with_hash (&pentry
, hash
, INSERT
);
482 entry
= XNEW (struct invariant_expr_entry
);
492 /* Finds invariants identical to INV and records the equivalence. EQ is the
493 hash table of the invariants. */
496 find_identical_invariants (invariant_htab_type
*eq
, struct invariant
*inv
)
500 struct invariant
*dep
;
503 struct invariant
*tmp
;
505 if (inv
->eqto
!= ~0u)
508 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
510 dep
= invariants
[depno
];
511 find_identical_invariants (eq
, dep
);
514 set
= single_set (inv
->insn
);
515 expr
= SET_SRC (set
);
516 mode
= GET_MODE (expr
);
517 if (mode
== VOIDmode
)
518 mode
= GET_MODE (SET_DEST (set
));
520 tmp
= find_or_insert_inv (eq
, expr
, mode
, inv
);
521 inv
->eqto
= tmp
->invno
;
523 if (tmp
->invno
!= inv
->invno
&& inv
->always_executed
)
526 if (dump_file
&& inv
->eqto
!= inv
->invno
)
528 "Invariant %d is equivalent to invariant %d.\n",
529 inv
->invno
, inv
->eqto
);
532 /* Find invariants with the same value and record the equivalences. */
535 merge_identical_invariants (void)
538 struct invariant
*inv
;
539 invariant_htab_type
eq (invariants
.length ());
541 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
542 find_identical_invariants (&eq
, inv
);
545 /* Determines the basic blocks inside LOOP that are always executed and
546 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
547 basic blocks that may either exit the loop, or contain the call that
548 does not have to return. BODY is body of the loop obtained by
549 get_loop_body_in_dom_order. */
552 compute_always_reached (struct loop
*loop
, basic_block
*body
,
553 bitmap may_exit
, bitmap always_reached
)
557 for (i
= 0; i
< loop
->num_nodes
; i
++)
559 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, body
[i
]))
560 bitmap_set_bit (always_reached
, i
);
562 if (bitmap_bit_p (may_exit
, i
))
567 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
568 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
569 additionally mark blocks that may exit due to a call. */
572 find_exits (struct loop
*loop
, basic_block
*body
,
573 bitmap may_exit
, bitmap has_exit
)
578 struct loop
*outermost_exit
= loop
, *aexit
;
579 bool has_call
= false;
582 for (i
= 0; i
< loop
->num_nodes
; i
++)
584 if (body
[i
]->loop_father
== loop
)
586 FOR_BB_INSNS (body
[i
], insn
)
589 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
590 || !RTL_CONST_OR_PURE_CALL_P (insn
)))
593 bitmap_set_bit (may_exit
, i
);
598 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
600 if (flow_bb_inside_loop_p (loop
, e
->dest
))
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
);
611 /* Use the data stored for the subloop to decide whether we may exit
612 through it. It is sufficient to do this for header of the loop,
613 as other basic blocks inside it must be dominated by it. */
614 if (body
[i
]->loop_father
->header
!= body
[i
])
617 if (LOOP_DATA (body
[i
]->loop_father
)->has_call
)
620 bitmap_set_bit (may_exit
, i
);
622 aexit
= LOOP_DATA (body
[i
]->loop_father
)->outermost_exit
;
625 bitmap_set_bit (may_exit
, i
);
626 bitmap_set_bit (has_exit
, i
);
628 if (flow_loop_nested_p (aexit
, outermost_exit
))
629 outermost_exit
= aexit
;
633 if (loop
->aux
== NULL
)
635 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
636 bitmap_initialize (&LOOP_DATA (loop
)->regs_ref
, ®_obstack
);
637 bitmap_initialize (&LOOP_DATA (loop
)->regs_live
, ®_obstack
);
639 LOOP_DATA (loop
)->outermost_exit
= outermost_exit
;
640 LOOP_DATA (loop
)->has_call
= has_call
;
643 /* Check whether we may assign a value to X from a register. */
646 may_assign_reg_p (rtx x
)
648 return (GET_MODE (x
) != VOIDmode
649 && GET_MODE (x
) != BLKmode
650 && can_copy_p (GET_MODE (x
))
652 || !HARD_REGISTER_P (x
)
653 || REGNO_REG_CLASS (REGNO (x
)) != NO_REGS
));
656 /* Finds definitions that may correspond to invariants in LOOP with body
660 find_defs (struct loop
*loop
)
665 "*****starting processing of loop %d ******\n",
669 df_remove_problem (df_chain
);
670 df_process_deferred_rescans ();
671 df_chain_add_problem (DF_UD_CHAIN
);
672 df_live_add_problem ();
673 df_live_set_all_dirty ();
674 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
675 df_analyze_loop (loop
);
676 check_invariant_table_size ();
680 df_dump_region (dump_file
);
682 "*****ending processing of loop %d ******\n",
687 /* Creates a new invariant for definition DEF in INSN, depending on invariants
688 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
689 unless the program ends due to a function call. The newly created invariant
692 static struct invariant
*
693 create_new_invariant (struct def
*def
, rtx_insn
*insn
, bitmap depends_on
,
694 bool always_executed
)
696 struct invariant
*inv
= XNEW (struct invariant
);
697 rtx set
= single_set (insn
);
698 bool speed
= optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn
));
701 inv
->always_executed
= always_executed
;
702 inv
->depends_on
= depends_on
;
704 /* If the set is simple, usually by moving it we move the whole store out of
705 the loop. Otherwise we save only cost of the computation. */
708 inv
->cost
= set_rtx_cost (set
, speed
);
709 /* ??? Try to determine cheapness of address computation. Unfortunately
710 the address cost is only a relative measure, we can't really compare
711 it with any absolute number, but only with other address costs.
712 But here we don't have any other addresses, so compare with a magic
713 number anyway. It has to be large enough to not regress PR33928
714 (by avoiding to move reg+8,reg+16,reg+24 invariants), but small
715 enough to not regress 410.bwaves either (by still moving reg+reg
717 See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html . */
718 if (SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set
))))
719 inv
->cheap_address
= address_cost (SET_SRC (set
), word_mode
,
720 ADDR_SPACE_GENERIC
, speed
) < 3;
722 inv
->cheap_address
= false;
726 inv
->cost
= set_src_cost (SET_SRC (set
), GET_MODE (SET_DEST (set
)),
728 inv
->cheap_address
= false;
733 inv
->orig_regno
= -1;
737 inv
->invno
= invariants
.length ();
744 def
->invno
= inv
->invno
;
745 invariants
.safe_push (inv
);
750 "Set in insn %d is invariant (%d), cost %d, depends on ",
751 INSN_UID (insn
), inv
->invno
, inv
->cost
);
752 dump_bitmap (dump_file
, inv
->depends_on
);
758 /* Return a canonical version of X for the address, from the point of view,
759 that all multiplications are represented as MULT instead of the multiply
760 by a power of 2 being represented as ASHIFT.
762 Callers should prepare a copy of X because this function may modify it
766 canonicalize_address_mult (rtx x
)
768 subrtx_var_iterator::array_type array
;
769 FOR_EACH_SUBRTX_VAR (iter
, array
, x
, NONCONST
)
773 if (GET_CODE (sub
) == ASHIFT
774 && CONST_INT_P (XEXP (sub
, 1))
775 && INTVAL (XEXP (sub
, 1)) < GET_MODE_BITSIZE (GET_MODE (sub
))
776 && INTVAL (XEXP (sub
, 1)) >= 0)
778 HOST_WIDE_INT shift
= INTVAL (XEXP (sub
, 1));
779 PUT_CODE (sub
, MULT
);
780 XEXP (sub
, 1) = gen_int_mode (HOST_WIDE_INT_1
<< shift
,
782 iter
.skip_subrtxes ();
787 /* Maximum number of sub expressions in address. We set it to
788 a small integer since it's unlikely to have a complicated
789 address expression. */
791 #define MAX_CANON_ADDR_PARTS (5)
793 /* Collect sub expressions in address X with PLUS as the seperator.
794 Sub expressions are stored in vector ADDR_PARTS. */
797 collect_address_parts (rtx x
, vec
<rtx
> *addr_parts
)
799 subrtx_var_iterator::array_type array
;
800 FOR_EACH_SUBRTX_VAR (iter
, array
, x
, NONCONST
)
804 if (GET_CODE (sub
) != PLUS
)
806 addr_parts
->safe_push (sub
);
807 iter
.skip_subrtxes ();
812 /* Compare function for sorting sub expressions X and Y based on
813 precedence defined for communitive operations. */
816 compare_address_parts (const void *x
, const void *y
)
818 const rtx
*rx
= (const rtx
*)x
;
819 const rtx
*ry
= (const rtx
*)y
;
820 int px
= commutative_operand_precedence (*rx
);
821 int py
= commutative_operand_precedence (*ry
);
826 /* Return a canonical version address for X by following steps:
827 1) Rewrite ASHIFT into MULT recursively.
828 2) Divide address into sub expressions with PLUS as the
830 3) Sort sub expressions according to precedence defined
831 for communative operations.
832 4) Simplify CONST_INT_P sub expressions.
833 5) Create new canonicalized address and return.
834 Callers should prepare a copy of X because this function may
835 modify it in place. */
838 canonicalize_address (rtx x
)
842 machine_mode mode
= GET_MODE (x
);
843 auto_vec
<rtx
, MAX_CANON_ADDR_PARTS
> addr_parts
;
845 /* Rewrite ASHIFT into MULT. */
846 canonicalize_address_mult (x
);
847 /* Divide address into sub expressions. */
848 collect_address_parts (x
, &addr_parts
);
849 /* Unlikely to have very complicated address. */
850 if (addr_parts
.length () < 2
851 || addr_parts
.length () > MAX_CANON_ADDR_PARTS
)
854 /* Sort sub expressions according to canonicalization precedence. */
855 addr_parts
.qsort (compare_address_parts
);
857 /* Simplify all constant int summary if possible. */
858 for (i
= 0; i
< addr_parts
.length (); i
++)
859 if (CONST_INT_P (addr_parts
[i
]))
862 for (j
= i
+ 1; j
< addr_parts
.length (); j
++)
864 gcc_assert (CONST_INT_P (addr_parts
[j
]));
865 addr_parts
[i
] = simplify_gen_binary (PLUS
, mode
,
870 /* Chain PLUS operators to the left for !CONST_INT_P sub expressions. */
872 for (j
= 1; j
< i
; j
++)
873 res
= simplify_gen_binary (PLUS
, mode
, res
, addr_parts
[j
]);
875 /* Pickup the last CONST_INT_P sub expression. */
876 if (i
< addr_parts
.length ())
877 res
= simplify_gen_binary (PLUS
, mode
, res
, addr_parts
[i
]);
882 /* Given invariant DEF and its address USE, check if the corresponding
883 invariant expr can be propagated into the use or not. */
886 inv_can_prop_to_addr_use (struct def
*def
, df_ref use
)
888 struct invariant
*inv
;
889 rtx
*pos
= DF_REF_REAL_LOC (use
), def_set
, use_set
;
890 rtx_insn
*use_insn
= DF_REF_INSN (use
);
894 inv
= invariants
[def
->invno
];
895 /* No need to check if address expression is expensive. */
896 if (!inv
->cheap_address
)
899 def_insn
= inv
->insn
;
900 def_set
= single_set (def_insn
);
904 validate_unshare_change (use_insn
, pos
, SET_SRC (def_set
), true);
905 ok
= verify_changes (0);
906 /* Try harder with canonicalization in address expression. */
907 if (!ok
&& (use_set
= single_set (use_insn
)) != NULL_RTX
)
909 rtx src
, dest
, mem
= NULL_RTX
;
911 src
= SET_SRC (use_set
);
912 dest
= SET_DEST (use_set
);
915 else if (MEM_P (dest
))
919 && !memory_address_addr_space_p (GET_MODE (mem
),
921 MEM_ADDR_SPACE (mem
)))
923 rtx addr
= canonicalize_address (copy_rtx (XEXP (mem
, 0)));
924 if (memory_address_addr_space_p (GET_MODE (mem
),
925 addr
, MEM_ADDR_SPACE (mem
)))
933 /* Record USE at DEF. */
936 record_use (struct def
*def
, df_ref use
)
938 struct use
*u
= XNEW (struct use
);
940 u
->pos
= DF_REF_REAL_LOC (use
);
941 u
->insn
= DF_REF_INSN (use
);
942 u
->addr_use_p
= (DF_REF_TYPE (use
) == DF_REF_REG_MEM_LOAD
943 || DF_REF_TYPE (use
) == DF_REF_REG_MEM_STORE
);
949 /* Initialize propagation information if this is the first addr
950 use of the inv def. */
951 if (def
->n_addr_uses
== 0)
952 def
->can_prop_to_addr_uses
= true;
955 if (def
->can_prop_to_addr_uses
&& !inv_can_prop_to_addr_use (def
, use
))
956 def
->can_prop_to_addr_uses
= false;
960 /* Finds the invariants USE depends on and store them to the DEPENDS_ON
961 bitmap. Returns true if all dependencies of USE are known to be
962 loop invariants, false otherwise. */
965 check_dependency (basic_block bb
, df_ref use
, bitmap depends_on
)
969 struct df_link
*defs
;
970 struct def
*def_data
;
971 struct invariant
*inv
;
973 if (DF_REF_FLAGS (use
) & DF_REF_READ_WRITE
)
976 defs
= DF_REF_CHAIN (use
);
979 unsigned int regno
= DF_REF_REGNO (use
);
981 /* If this is the use of an uninitialized argument register that is
982 likely to be spilled, do not move it lest this might extend its
983 lifetime and cause reload to die. This can occur for a call to
984 a function taking complex number arguments and moving the insns
985 preparing the arguments without moving the call itself wouldn't
986 gain much in practice. */
987 if ((DF_REF_FLAGS (use
) & DF_HARD_REG_LIVE
)
988 && FUNCTION_ARG_REGNO_P (regno
)
989 && targetm
.class_likely_spilled_p (REGNO_REG_CLASS (regno
)))
999 check_invariant_table_size ();
1000 inv
= invariant_table
[DF_REF_ID (def
)];
1004 def_data
= inv
->def
;
1005 gcc_assert (def_data
!= NULL
);
1007 def_bb
= DF_REF_BB (def
);
1008 /* Note that in case bb == def_bb, we know that the definition
1009 dominates insn, because def has invariant_table[DF_REF_ID(def)]
1010 defined and we process the insns in the basic block bb
1012 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
1015 bitmap_set_bit (depends_on
, def_data
->invno
);
1020 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
1021 bitmap. Returns true if all dependencies of INSN are known to be
1022 loop invariants, false otherwise. */
1025 check_dependencies (rtx_insn
*insn
, bitmap depends_on
)
1027 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
1029 basic_block bb
= BLOCK_FOR_INSN (insn
);
1031 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
1032 if (!check_dependency (bb
, use
, depends_on
))
1034 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
1035 if (!check_dependency (bb
, use
, depends_on
))
1041 /* Pre-check candidate DEST to skip the one which can not make a valid insn
1042 during move_invariant_reg. SIMPLE is to skip HARD_REGISTER. */
1044 pre_check_invariant_p (bool simple
, rtx dest
)
1046 if (simple
&& REG_P (dest
) && DF_REG_DEF_COUNT (REGNO (dest
)) > 1)
1049 unsigned int i
= REGNO (dest
);
1050 struct df_insn_info
*insn_info
;
1053 for (use
= DF_REG_USE_CHAIN (i
); use
; use
= DF_REF_NEXT_REG (use
))
1055 rtx_insn
*ref
= DF_REF_INSN (use
);
1056 insn_info
= DF_INSN_INFO_GET (ref
);
1058 FOR_EACH_INSN_INFO_DEF (def_rec
, insn_info
)
1059 if (DF_REF_REGNO (def_rec
) == i
)
1061 /* Multi definitions at this stage, most likely are due to
1062 instruction constraints, which requires both read and write
1063 on the same register. Since move_invariant_reg is not
1064 powerful enough to handle such cases, just ignore the INV
1065 and leave the chance to others. */
1073 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
1074 executed. ALWAYS_EXECUTED is true if the insn is always executed,
1075 unless the program ends due to a function call. */
1078 find_invariant_insn (rtx_insn
*insn
, bool always_reached
, bool always_executed
)
1085 struct invariant
*inv
;
1087 /* We can't move a CC0 setter without the user. */
1088 if (HAVE_cc0
&& sets_cc0_p (insn
))
1091 set
= single_set (insn
);
1094 dest
= SET_DEST (set
);
1097 || HARD_REGISTER_P (dest
))
1100 if (!may_assign_reg_p (dest
)
1101 || !pre_check_invariant_p (simple
, dest
)
1102 || !check_maybe_invariant (SET_SRC (set
)))
1105 /* If the insn can throw exception, we cannot move it at all without changing
1107 if (can_throw_internal (insn
))
1110 /* We cannot make trapping insn executed, unless it was executed before. */
1111 if (may_trap_or_fault_p (PATTERN (insn
)) && !always_reached
)
1114 depends_on
= BITMAP_ALLOC (NULL
);
1115 if (!check_dependencies (insn
, depends_on
))
1117 BITMAP_FREE (depends_on
);
1122 def
= XCNEW (struct def
);
1126 inv
= create_new_invariant (def
, insn
, depends_on
, always_executed
);
1130 ref
= df_find_def (insn
, dest
);
1131 check_invariant_table_size ();
1132 invariant_table
[DF_REF_ID (ref
)] = inv
;
1136 /* Record registers used in INSN that have a unique invariant definition. */
1139 record_uses (rtx_insn
*insn
)
1141 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
1143 struct invariant
*inv
;
1145 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
1147 inv
= invariant_for_use (use
);
1149 record_use (inv
->def
, use
);
1151 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
1153 inv
= invariant_for_use (use
);
1155 record_use (inv
->def
, use
);
1159 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
1160 executed. ALWAYS_EXECUTED is true if the insn is always executed,
1161 unless the program ends due to a function call. */
1164 find_invariants_insn (rtx_insn
*insn
, bool always_reached
, bool always_executed
)
1166 find_invariant_insn (insn
, always_reached
, always_executed
);
1170 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
1171 basic block is always executed. ALWAYS_EXECUTED is true if the basic
1172 block is always executed, unless the program ends due to a function
1176 find_invariants_bb (basic_block bb
, bool always_reached
, bool always_executed
)
1180 FOR_BB_INSNS (bb
, insn
)
1182 if (!NONDEBUG_INSN_P (insn
))
1185 find_invariants_insn (insn
, always_reached
, always_executed
);
1189 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
1190 || ! RTL_CONST_OR_PURE_CALL_P (insn
)))
1191 always_reached
= false;
1195 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
1196 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
1197 bitmap of basic blocks in BODY that are always executed unless the program
1198 ends due to a function call. */
1201 find_invariants_body (struct loop
*loop
, basic_block
*body
,
1202 bitmap always_reached
, bitmap always_executed
)
1206 for (i
= 0; i
< loop
->num_nodes
; i
++)
1207 find_invariants_bb (body
[i
],
1208 bitmap_bit_p (always_reached
, i
),
1209 bitmap_bit_p (always_executed
, i
));
1212 /* Finds invariants in LOOP. */
1215 find_invariants (struct loop
*loop
)
1217 bitmap may_exit
= BITMAP_ALLOC (NULL
);
1218 bitmap always_reached
= BITMAP_ALLOC (NULL
);
1219 bitmap has_exit
= BITMAP_ALLOC (NULL
);
1220 bitmap always_executed
= BITMAP_ALLOC (NULL
);
1221 basic_block
*body
= get_loop_body_in_dom_order (loop
);
1223 find_exits (loop
, body
, may_exit
, has_exit
);
1224 compute_always_reached (loop
, body
, may_exit
, always_reached
);
1225 compute_always_reached (loop
, body
, has_exit
, always_executed
);
1228 find_invariants_body (loop
, body
, always_reached
, always_executed
);
1229 merge_identical_invariants ();
1231 BITMAP_FREE (always_reached
);
1232 BITMAP_FREE (always_executed
);
1233 BITMAP_FREE (may_exit
);
1234 BITMAP_FREE (has_exit
);
1238 /* Frees a list of uses USE. */
1241 free_use_list (struct use
*use
)
1245 for (; use
; use
= next
)
1252 /* Return pressure class and number of hard registers (through *NREGS)
1253 for destination of INSN. */
1254 static enum reg_class
1255 get_pressure_class_and_nregs (rtx_insn
*insn
, int *nregs
)
1258 enum reg_class pressure_class
;
1259 rtx set
= single_set (insn
);
1261 /* Considered invariant insns have only one set. */
1262 gcc_assert (set
!= NULL_RTX
);
1263 reg
= SET_DEST (set
);
1264 if (GET_CODE (reg
) == SUBREG
)
1265 reg
= SUBREG_REG (reg
);
1269 pressure_class
= NO_REGS
;
1275 if (reg
== NULL_RTX
)
1276 pressure_class
= GENERAL_REGS
;
1279 pressure_class
= reg_allocno_class (REGNO (reg
));
1280 pressure_class
= ira_pressure_class_translate
[pressure_class
];
1283 = ira_reg_class_max_nregs
[pressure_class
][GET_MODE (SET_SRC (set
))];
1285 return pressure_class
;
1288 /* Calculates cost and number of registers needed for moving invariant INV
1289 out of the loop and stores them to *COST and *REGS_NEEDED. *CL will be
1290 the REG_CLASS of INV. Return
1291 -1: if INV is invalid.
1292 0: if INV and its depends_on have same reg_class
1293 1: if INV and its depends_on have different reg_classes. */
1296 get_inv_cost (struct invariant
*inv
, int *comp_cost
, unsigned *regs_needed
,
1300 unsigned aregs_needed
[N_REG_CLASSES
];
1302 struct invariant
*dep
;
1306 /* Find the representative of the class of the equivalent invariants. */
1307 inv
= invariants
[inv
->eqto
];
1310 if (! flag_ira_loop_pressure
)
1314 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1315 regs_needed
[ira_pressure_classes
[i
]] = 0;
1319 || inv
->stamp
== actual_stamp
)
1321 inv
->stamp
= actual_stamp
;
1323 if (! flag_ira_loop_pressure
)
1328 enum reg_class pressure_class
;
1330 pressure_class
= get_pressure_class_and_nregs (inv
->insn
, &nregs
);
1331 regs_needed
[pressure_class
] += nregs
;
1332 *cl
= pressure_class
;
1336 if (!inv
->cheap_address
1337 || inv
->def
->n_uses
== 0
1338 || inv
->def
->n_addr_uses
< inv
->def
->n_uses
1339 /* Count cost if the inv can't be propagated into address uses. */
1340 || !inv
->def
->can_prop_to_addr_uses
)
1341 (*comp_cost
) += inv
->cost
* inv
->eqno
;
1345 /* Hoisting constant pool constants into stack regs may cost more than
1346 just single register. On x87, the balance is affected both by the
1347 small number of FP registers, and by its register stack organization,
1348 that forces us to add compensation code in and around the loop to
1349 shuffle the operands to the top of stack before use, and pop them
1350 from the stack after the loop finishes.
1352 To model this effect, we increase the number of registers needed for
1353 stack registers by two: one register push, and one register pop.
1354 This usually has the effect that FP constant loads from the constant
1355 pool are not moved out of the loop.
1357 Note that this also means that dependent invariants can not be moved.
1358 However, the primary purpose of this pass is to move loop invariant
1359 address arithmetic out of loops, and address arithmetic that depends
1360 on floating point constants is unlikely to ever occur. */
1361 rtx set
= single_set (inv
->insn
);
1363 && IS_STACK_MODE (GET_MODE (SET_SRC (set
)))
1364 && constant_pool_constant_p (SET_SRC (set
)))
1366 if (flag_ira_loop_pressure
)
1367 regs_needed
[ira_stack_reg_pressure_class
] += 2;
1369 regs_needed
[0] += 2;
1374 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
1377 enum reg_class dep_cl
= ALL_REGS
;
1380 dep
= invariants
[depno
];
1382 /* If DEP is moved out of the loop, it is not a depends_on any more. */
1386 dep_ret
= get_inv_cost (dep
, &acomp_cost
, aregs_needed
, &dep_cl
);
1388 if (! flag_ira_loop_pressure
)
1389 check_p
= aregs_needed
[0] != 0;
1392 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1393 if (aregs_needed
[ira_pressure_classes
[i
]] != 0)
1395 check_p
= i
< ira_pressure_classes_num
;
1397 if ((dep_ret
== 1) || ((dep_ret
== 0) && (*cl
!= dep_cl
)))
1404 /* We need to check always_executed, since if the original value of
1405 the invariant may be preserved, we may need to keep it in a
1406 separate register. TODO check whether the register has an
1407 use outside of the loop. */
1408 && dep
->always_executed
1409 && !dep
->def
->uses
->next
)
1411 /* If this is a single use, after moving the dependency we will not
1412 need a new register. */
1413 if (! flag_ira_loop_pressure
)
1418 enum reg_class pressure_class
;
1420 pressure_class
= get_pressure_class_and_nregs (inv
->insn
, &nregs
);
1421 aregs_needed
[pressure_class
] -= nregs
;
1425 if (! flag_ira_loop_pressure
)
1426 regs_needed
[0] += aregs_needed
[0];
1429 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1430 regs_needed
[ira_pressure_classes
[i
]]
1431 += aregs_needed
[ira_pressure_classes
[i
]];
1433 (*comp_cost
) += acomp_cost
;
1438 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
1439 of registers used in the loop, NEW_REGS is the number of new variables
1440 already added due to the invariant motion. The number of registers needed
1441 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
1442 through to estimate_reg_pressure_cost. */
1445 gain_for_invariant (struct invariant
*inv
, unsigned *regs_needed
,
1446 unsigned *new_regs
, unsigned regs_used
,
1447 bool speed
, bool call_p
)
1449 int comp_cost
, size_cost
;
1450 /* Workaround -Wmaybe-uninitialized false positive during
1451 profiledbootstrap by initializing it. */
1452 enum reg_class cl
= NO_REGS
;
1457 ret
= get_inv_cost (inv
, &comp_cost
, regs_needed
, &cl
);
1459 if (! flag_ira_loop_pressure
)
1461 size_cost
= (estimate_reg_pressure_cost (new_regs
[0] + regs_needed
[0],
1462 regs_used
, speed
, call_p
)
1463 - estimate_reg_pressure_cost (new_regs
[0],
1464 regs_used
, speed
, call_p
));
1468 else if ((ret
== 0) && (cl
== NO_REGS
))
1469 /* Hoist it anyway since it does not impact register pressure. */
1474 enum reg_class pressure_class
;
1476 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1478 pressure_class
= ira_pressure_classes
[i
];
1480 if (!reg_classes_intersect_p (pressure_class
, cl
))
1483 if ((int) new_regs
[pressure_class
]
1484 + (int) regs_needed
[pressure_class
]
1485 + LOOP_DATA (curr_loop
)->max_reg_pressure
[pressure_class
]
1486 + IRA_LOOP_RESERVED_REGS
1487 > ira_class_hard_regs_num
[pressure_class
])
1490 if (i
< ira_pressure_classes_num
)
1491 /* There will be register pressure excess and we want not to
1492 make this loop invariant motion. All loop invariants with
1493 non-positive gains will be rejected in function
1494 find_invariants_to_move. Therefore we return the negative
1497 One could think that this rejects also expensive loop
1498 invariant motions and this will hurt code performance.
1499 However numerous experiments with different heuristics
1500 taking invariant cost into account did not confirm this
1501 assumption. There are possible explanations for this
1503 o probably all expensive invariants were already moved out
1504 of the loop by PRE and gimple invariant motion pass.
1505 o expensive invariant execution will be hidden by insn
1506 scheduling or OOO processor hardware because usually such
1507 invariants have a lot of freedom to be executed
1509 Another reason for ignoring invariant cost vs spilling cost
1510 heuristics is also in difficulties to evaluate accurately
1511 spill cost at this stage. */
1517 return comp_cost
- size_cost
;
1520 /* Finds invariant with best gain for moving. Returns the gain, stores
1521 the invariant in *BEST and number of registers needed for it to
1522 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1523 NEW_REGS is the number of new variables already added due to invariant
1527 best_gain_for_invariant (struct invariant
**best
, unsigned *regs_needed
,
1528 unsigned *new_regs
, unsigned regs_used
,
1529 bool speed
, bool call_p
)
1531 struct invariant
*inv
;
1532 int i
, gain
= 0, again
;
1533 unsigned aregs_needed
[N_REG_CLASSES
], invno
;
1535 FOR_EACH_VEC_ELT (invariants
, invno
, inv
)
1540 /* Only consider the "representatives" of equivalent invariants. */
1541 if (inv
->eqto
!= inv
->invno
)
1544 again
= gain_for_invariant (inv
, aregs_needed
, new_regs
, regs_used
,
1550 if (! flag_ira_loop_pressure
)
1551 regs_needed
[0] = aregs_needed
[0];
1554 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1555 regs_needed
[ira_pressure_classes
[i
]]
1556 = aregs_needed
[ira_pressure_classes
[i
]];
1564 /* Marks invariant INVNO and all its dependencies for moving. */
1567 set_move_mark (unsigned invno
, int gain
)
1569 struct invariant
*inv
= invariants
[invno
];
1572 /* Find the representative of the class of the equivalent invariants. */
1573 inv
= invariants
[inv
->eqto
];
1582 fprintf (dump_file
, "Decided to move invariant %d -- gain %d\n",
1585 fprintf (dump_file
, "Decided to move dependent invariant %d\n",
1589 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, invno
, bi
)
1591 set_move_mark (invno
, -1);
1595 /* Determines which invariants to move. */
1598 find_invariants_to_move (bool speed
, bool call_p
)
1601 unsigned i
, regs_used
, regs_needed
[N_REG_CLASSES
], new_regs
[N_REG_CLASSES
];
1602 struct invariant
*inv
= NULL
;
1604 if (!invariants
.length ())
1607 if (flag_ira_loop_pressure
)
1608 /* REGS_USED is actually never used when the flag is on. */
1611 /* We do not really do a good job in estimating number of
1612 registers used; we put some initial bound here to stand for
1613 induction variables etc. that we do not detect. */
1615 unsigned int n_regs
= DF_REG_SIZE (df
);
1619 for (i
= 0; i
< n_regs
; i
++)
1621 if (!DF_REGNO_FIRST_DEF (i
) && DF_REGNO_LAST_USE (i
))
1623 /* This is a value that is used but not changed inside loop. */
1629 if (! flag_ira_loop_pressure
)
1630 new_regs
[0] = regs_needed
[0] = 0;
1633 for (i
= 0; (int) i
< ira_pressure_classes_num
; i
++)
1634 new_regs
[ira_pressure_classes
[i
]] = 0;
1636 while ((gain
= best_gain_for_invariant (&inv
, regs_needed
,
1637 new_regs
, regs_used
,
1638 speed
, call_p
)) > 0)
1640 set_move_mark (inv
->invno
, gain
);
1641 if (! flag_ira_loop_pressure
)
1642 new_regs
[0] += regs_needed
[0];
1645 for (i
= 0; (int) i
< ira_pressure_classes_num
; i
++)
1646 new_regs
[ira_pressure_classes
[i
]]
1647 += regs_needed
[ira_pressure_classes
[i
]];
1652 /* Replace the uses, reached by the definition of invariant INV, by REG.
1654 IN_GROUP is nonzero if this is part of a group of changes that must be
1655 performed as a group. In that case, the changes will be stored. The
1656 function `apply_change_group' will validate and apply the changes. */
1659 replace_uses (struct invariant
*inv
, rtx reg
, bool in_group
)
1661 /* Replace the uses we know to be dominated. It saves work for copy
1662 propagation, and also it is necessary so that dependent invariants
1663 are computed right. */
1667 for (use
= inv
->def
->uses
; use
; use
= use
->next
)
1668 validate_change (use
->insn
, use
->pos
, reg
, true);
1670 /* If we aren't part of a larger group, apply the changes now. */
1672 return apply_change_group ();
1678 /* Whether invariant INV setting REG can be moved out of LOOP, at the end of
1679 the block preceding its header. */
1682 can_move_invariant_reg (struct loop
*loop
, struct invariant
*inv
, rtx reg
)
1685 unsigned int dest_regno
, defs_in_loop_count
= 0;
1686 rtx_insn
*insn
= inv
->insn
;
1687 basic_block bb
= BLOCK_FOR_INSN (inv
->insn
);
1689 /* We ignore hard register and memory access for cost and complexity reasons.
1690 Hard register are few at this stage and expensive to consider as they
1691 require building a separate data flow. Memory access would require using
1692 df_simulate_* and can_move_insns_across functions and is more complex. */
1693 if (!REG_P (reg
) || HARD_REGISTER_P (reg
))
1696 /* Check whether the set is always executed. We could omit this condition if
1697 we know that the register is unused outside of the loop, but it does not
1698 seem worth finding out. */
1699 if (!inv
->always_executed
)
1702 /* Check that all uses that would be dominated by def are already dominated
1704 dest_regno
= REGNO (reg
);
1705 for (use
= DF_REG_USE_CHAIN (dest_regno
); use
; use
= DF_REF_NEXT_REG (use
))
1710 use_insn
= DF_REF_INSN (use
);
1711 use_bb
= BLOCK_FOR_INSN (use_insn
);
1713 /* Ignore instruction considered for moving. */
1714 if (use_insn
== insn
)
1717 /* Don't consider uses outside loop. */
1718 if (!flow_bb_inside_loop_p (loop
, use_bb
))
1721 /* Don't move if a use is not dominated by def in insn. */
1722 if (use_bb
== bb
&& DF_INSN_LUID (insn
) >= DF_INSN_LUID (use_insn
))
1724 if (!dominated_by_p (CDI_DOMINATORS
, use_bb
, bb
))
1728 /* Check for other defs. Any other def in the loop might reach a use
1729 currently reached by the def in insn. */
1730 for (def
= DF_REG_DEF_CHAIN (dest_regno
); def
; def
= DF_REF_NEXT_REG (def
))
1732 basic_block def_bb
= DF_REF_BB (def
);
1734 /* Defs in exit block cannot reach a use they weren't already. */
1735 if (single_succ_p (def_bb
))
1737 basic_block def_bb_succ
;
1739 def_bb_succ
= single_succ (def_bb
);
1740 if (!flow_bb_inside_loop_p (loop
, def_bb_succ
))
1744 if (++defs_in_loop_count
> 1)
1751 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1755 move_invariant_reg (struct loop
*loop
, unsigned invno
)
1757 struct invariant
*inv
= invariants
[invno
];
1758 struct invariant
*repr
= invariants
[inv
->eqto
];
1760 basic_block preheader
= loop_preheader_edge (loop
)->src
;
1761 rtx reg
, set
, dest
, note
;
1770 /* If this is a representative of the class of equivalent invariants,
1771 really move the invariant. Otherwise just replace its use with
1772 the register used for the representative. */
1775 if (inv
->depends_on
)
1777 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, i
, bi
)
1779 if (!move_invariant_reg (loop
, i
))
1784 /* If possible, just move the set out of the loop. Otherwise, we
1785 need to create a temporary register. */
1786 set
= single_set (inv
->insn
);
1787 reg
= dest
= SET_DEST (set
);
1788 if (GET_CODE (reg
) == SUBREG
)
1789 reg
= SUBREG_REG (reg
);
1791 regno
= REGNO (reg
);
1793 if (!can_move_invariant_reg (loop
, inv
, dest
))
1795 reg
= gen_reg_rtx_and_attrs (dest
);
1797 /* Try replacing the destination by a new pseudoregister. */
1798 validate_change (inv
->insn
, &SET_DEST (set
), reg
, true);
1800 /* As well as all the dominated uses. */
1801 replace_uses (inv
, reg
, true);
1803 /* And validate all the changes. */
1804 if (!apply_change_group ())
1807 emit_insn_after (gen_move_insn (dest
, reg
), inv
->insn
);
1810 fprintf (dump_file
, "Invariant %d moved without introducing a new "
1811 "temporary register\n", invno
);
1812 reorder_insns (inv
->insn
, inv
->insn
, BB_END (preheader
));
1813 df_recompute_luids (preheader
);
1815 /* If there is a REG_EQUAL note on the insn we just moved, and the
1816 insn is in a basic block that is not always executed or the note
1817 contains something for which we don't know the invariant status,
1818 the note may no longer be valid after we move the insn. Note that
1819 uses in REG_EQUAL notes are taken into account in the computation
1820 of invariants, so it is safe to retain the note even if it contains
1821 register references for which we know the invariant status. */
1822 if ((note
= find_reg_note (inv
->insn
, REG_EQUAL
, NULL_RTX
))
1823 && (!inv
->always_executed
1824 || !check_maybe_invariant (XEXP (note
, 0))))
1825 remove_note (inv
->insn
, note
);
1829 if (!move_invariant_reg (loop
, repr
->invno
))
1832 regno
= repr
->orig_regno
;
1833 if (!replace_uses (inv
, reg
, false))
1835 set
= single_set (inv
->insn
);
1836 emit_insn_after (gen_move_insn (SET_DEST (set
), reg
), inv
->insn
);
1837 delete_insn (inv
->insn
);
1841 inv
->orig_regno
= regno
;
1846 /* If we failed, clear move flag, so that we do not try to move inv
1849 fprintf (dump_file
, "Failed to move invariant %d\n", invno
);
1851 inv
->reg
= NULL_RTX
;
1852 inv
->orig_regno
= -1;
1857 /* Move selected invariant out of the LOOP. Newly created regs are marked
1858 in TEMPORARY_REGS. */
1861 move_invariants (struct loop
*loop
)
1863 struct invariant
*inv
;
1866 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
1867 move_invariant_reg (loop
, i
);
1868 if (flag_ira_loop_pressure
&& resize_reg_info ())
1870 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
1871 if (inv
->reg
!= NULL_RTX
)
1873 if (inv
->orig_regno
>= 0)
1874 setup_reg_classes (REGNO (inv
->reg
),
1875 reg_preferred_class (inv
->orig_regno
),
1876 reg_alternate_class (inv
->orig_regno
),
1877 reg_allocno_class (inv
->orig_regno
));
1879 setup_reg_classes (REGNO (inv
->reg
),
1880 GENERAL_REGS
, NO_REGS
, GENERAL_REGS
);
1885 /* Initializes invariant motion data. */
1888 init_inv_motion_data (void)
1892 invariants
.create (100);
1895 /* Frees the data allocated by invariant motion. */
1898 free_inv_motion_data (void)
1902 struct invariant
*inv
;
1904 check_invariant_table_size ();
1905 for (i
= 0; i
< DF_DEFS_TABLE_SIZE (); i
++)
1907 inv
= invariant_table
[i
];
1911 gcc_assert (def
!= NULL
);
1913 free_use_list (def
->uses
);
1915 invariant_table
[i
] = NULL
;
1919 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
1921 BITMAP_FREE (inv
->depends_on
);
1924 invariants
.release ();
1927 /* Move the invariants out of the LOOP. */
1930 move_single_loop_invariants (struct loop
*loop
)
1932 init_inv_motion_data ();
1934 find_invariants (loop
);
1935 find_invariants_to_move (optimize_loop_for_speed_p (loop
),
1936 LOOP_DATA (loop
)->has_call
);
1937 move_invariants (loop
);
1939 free_inv_motion_data ();
1942 /* Releases the auxiliary data for LOOP. */
1945 free_loop_data (struct loop
*loop
)
1947 struct loop_data
*data
= LOOP_DATA (loop
);
1951 bitmap_clear (&LOOP_DATA (loop
)->regs_ref
);
1952 bitmap_clear (&LOOP_DATA (loop
)->regs_live
);
1959 /* Registers currently living. */
1960 static bitmap_head curr_regs_live
;
1962 /* Current reg pressure for each pressure class. */
1963 static int curr_reg_pressure
[N_REG_CLASSES
];
1965 /* Record all regs that are set in any one insn. Communication from
1966 mark_reg_{store,clobber} and global_conflicts. Asm can refer to
1967 all hard-registers. */
1968 static rtx regs_set
[(FIRST_PSEUDO_REGISTER
> MAX_RECOG_OPERANDS
1969 ? FIRST_PSEUDO_REGISTER
: MAX_RECOG_OPERANDS
) * 2];
1970 /* Number of regs stored in the previous array. */
1971 static int n_regs_set
;
1973 /* Return pressure class and number of needed hard registers (through
1974 *NREGS) of register REGNO. */
1975 static enum reg_class
1976 get_regno_pressure_class (int regno
, int *nregs
)
1978 if (regno
>= FIRST_PSEUDO_REGISTER
)
1980 enum reg_class pressure_class
;
1982 pressure_class
= reg_allocno_class (regno
);
1983 pressure_class
= ira_pressure_class_translate
[pressure_class
];
1985 = ira_reg_class_max_nregs
[pressure_class
][PSEUDO_REGNO_MODE (regno
)];
1986 return pressure_class
;
1988 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs
, regno
)
1989 && ! TEST_HARD_REG_BIT (eliminable_regset
, regno
))
1992 return ira_pressure_class_translate
[REGNO_REG_CLASS (regno
)];
2001 /* Increase (if INCR_P) or decrease current register pressure for
2004 change_pressure (int regno
, bool incr_p
)
2007 enum reg_class pressure_class
;
2009 pressure_class
= get_regno_pressure_class (regno
, &nregs
);
2011 curr_reg_pressure
[pressure_class
] -= nregs
;
2014 curr_reg_pressure
[pressure_class
] += nregs
;
2015 if (LOOP_DATA (curr_loop
)->max_reg_pressure
[pressure_class
]
2016 < curr_reg_pressure
[pressure_class
])
2017 LOOP_DATA (curr_loop
)->max_reg_pressure
[pressure_class
]
2018 = curr_reg_pressure
[pressure_class
];
2022 /* Mark REGNO birth. */
2024 mark_regno_live (int regno
)
2028 for (loop
= curr_loop
;
2029 loop
!= current_loops
->tree_root
;
2030 loop
= loop_outer (loop
))
2031 bitmap_set_bit (&LOOP_DATA (loop
)->regs_live
, regno
);
2032 if (!bitmap_set_bit (&curr_regs_live
, regno
))
2034 change_pressure (regno
, true);
2037 /* Mark REGNO death. */
2039 mark_regno_death (int regno
)
2041 if (! bitmap_clear_bit (&curr_regs_live
, regno
))
2043 change_pressure (regno
, false);
2046 /* Mark setting register REG. */
2048 mark_reg_store (rtx reg
, const_rtx setter ATTRIBUTE_UNUSED
,
2049 void *data ATTRIBUTE_UNUSED
)
2051 if (GET_CODE (reg
) == SUBREG
)
2052 reg
= SUBREG_REG (reg
);
2057 regs_set
[n_regs_set
++] = reg
;
2059 unsigned int end_regno
= END_REGNO (reg
);
2060 for (unsigned int regno
= REGNO (reg
); regno
< end_regno
; ++regno
)
2061 mark_regno_live (regno
);
2064 /* Mark clobbering register REG. */
2066 mark_reg_clobber (rtx reg
, const_rtx setter
, void *data
)
2068 if (GET_CODE (setter
) == CLOBBER
)
2069 mark_reg_store (reg
, setter
, data
);
2072 /* Mark register REG death. */
2074 mark_reg_death (rtx reg
)
2076 unsigned int end_regno
= END_REGNO (reg
);
2077 for (unsigned int regno
= REGNO (reg
); regno
< end_regno
; ++regno
)
2078 mark_regno_death (regno
);
2081 /* Mark occurrence of registers in X for the current loop. */
2083 mark_ref_regs (rtx x
)
2092 code
= GET_CODE (x
);
2097 for (loop
= curr_loop
;
2098 loop
!= current_loops
->tree_root
;
2099 loop
= loop_outer (loop
))
2100 bitmap_set_bit (&LOOP_DATA (loop
)->regs_ref
, REGNO (x
));
2104 fmt
= GET_RTX_FORMAT (code
);
2105 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2107 mark_ref_regs (XEXP (x
, i
));
2108 else if (fmt
[i
] == 'E')
2112 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2113 mark_ref_regs (XVECEXP (x
, i
, j
));
2117 /* Calculate register pressure in the loops. */
2119 calculate_loop_reg_pressure (void)
2127 struct loop
*loop
, *parent
;
2129 FOR_EACH_LOOP (loop
, 0)
2130 if (loop
->aux
== NULL
)
2132 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
2133 bitmap_initialize (&LOOP_DATA (loop
)->regs_ref
, ®_obstack
);
2134 bitmap_initialize (&LOOP_DATA (loop
)->regs_live
, ®_obstack
);
2136 ira_setup_eliminable_regset ();
2137 bitmap_initialize (&curr_regs_live
, ®_obstack
);
2138 FOR_EACH_BB_FN (bb
, cfun
)
2140 curr_loop
= bb
->loop_father
;
2141 if (curr_loop
== current_loops
->tree_root
)
2144 for (loop
= curr_loop
;
2145 loop
!= current_loops
->tree_root
;
2146 loop
= loop_outer (loop
))
2147 bitmap_ior_into (&LOOP_DATA (loop
)->regs_live
, DF_LR_IN (bb
));
2149 bitmap_copy (&curr_regs_live
, DF_LR_IN (bb
));
2150 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
2151 curr_reg_pressure
[ira_pressure_classes
[i
]] = 0;
2152 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live
, 0, j
, bi
)
2153 change_pressure (j
, true);
2155 FOR_BB_INSNS (bb
, insn
)
2157 if (! NONDEBUG_INSN_P (insn
))
2160 mark_ref_regs (PATTERN (insn
));
2162 note_stores (PATTERN (insn
), mark_reg_clobber
, NULL
);
2164 /* Mark any registers dead after INSN as dead now. */
2166 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
2167 if (REG_NOTE_KIND (link
) == REG_DEAD
)
2168 mark_reg_death (XEXP (link
, 0));
2170 /* Mark any registers set in INSN as live,
2171 and mark them as conflicting with all other live regs.
2172 Clobbers are processed again, so they conflict with
2173 the registers that are set. */
2175 note_stores (PATTERN (insn
), mark_reg_store
, NULL
);
2178 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
2179 if (REG_NOTE_KIND (link
) == REG_INC
)
2180 mark_reg_store (XEXP (link
, 0), NULL_RTX
, NULL
);
2182 while (n_regs_set
-- > 0)
2184 rtx note
= find_regno_note (insn
, REG_UNUSED
,
2185 REGNO (regs_set
[n_regs_set
]));
2189 mark_reg_death (XEXP (note
, 0));
2193 bitmap_clear (&curr_regs_live
);
2194 if (flag_ira_region
== IRA_REGION_MIXED
2195 || flag_ira_region
== IRA_REGION_ALL
)
2196 FOR_EACH_LOOP (loop
, 0)
2198 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop
)->regs_live
, 0, j
, bi
)
2199 if (! bitmap_bit_p (&LOOP_DATA (loop
)->regs_ref
, j
))
2201 enum reg_class pressure_class
;
2204 pressure_class
= get_regno_pressure_class (j
, &nregs
);
2205 LOOP_DATA (loop
)->max_reg_pressure
[pressure_class
] -= nregs
;
2208 if (dump_file
== NULL
)
2210 FOR_EACH_LOOP (loop
, 0)
2212 parent
= loop_outer (loop
);
2213 fprintf (dump_file
, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
2214 loop
->num
, (parent
== NULL
? -1 : parent
->num
),
2215 loop
->header
->index
, loop_depth (loop
));
2216 fprintf (dump_file
, "\n ref. regnos:");
2217 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop
)->regs_ref
, 0, j
, bi
)
2218 fprintf (dump_file
, " %d", j
);
2219 fprintf (dump_file
, "\n live regnos:");
2220 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop
)->regs_live
, 0, j
, bi
)
2221 fprintf (dump_file
, " %d", j
);
2222 fprintf (dump_file
, "\n Pressure:");
2223 for (i
= 0; (int) i
< ira_pressure_classes_num
; i
++)
2225 enum reg_class pressure_class
;
2227 pressure_class
= ira_pressure_classes
[i
];
2228 if (LOOP_DATA (loop
)->max_reg_pressure
[pressure_class
] == 0)
2230 fprintf (dump_file
, " %s=%d", reg_class_names
[pressure_class
],
2231 LOOP_DATA (loop
)->max_reg_pressure
[pressure_class
]);
2233 fprintf (dump_file
, "\n");
2239 /* Move the invariants out of the loops. */
2242 move_loop_invariants (void)
2246 if (flag_ira_loop_pressure
)
2249 regstat_init_n_sets_and_refs ();
2250 ira_set_pseudo_classes (true, dump_file
);
2251 calculate_loop_reg_pressure ();
2252 regstat_free_n_sets_and_refs ();
2254 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
2255 /* Process the loops, innermost first. */
2256 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
2259 /* move_single_loop_invariants for very large loops
2260 is time consuming and might need a lot of memory. */
2261 if (loop
->num_nodes
<= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP
)
2262 move_single_loop_invariants (loop
);
2265 FOR_EACH_LOOP (loop
, 0)
2267 free_loop_data (loop
);
2270 if (flag_ira_loop_pressure
)
2271 /* There is no sense to keep this info because it was most
2272 probably outdated by subsequent passes. */
2274 free (invariant_table
);
2275 invariant_table
= NULL
;
2276 invariant_table_size
= 0;
2278 checking_verify_flow_info ();