1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004-2014 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"
41 #include "hard-reg-set.h"
45 #include "basic-block.h"
53 #include "hash-table.h"
60 /* The data stored for the loop. */
64 struct loop
*outermost_exit
; /* The outermost exit of the loop. */
65 bool has_call
; /* True if the loop contains a call. */
66 /* Maximal register pressure inside loop for given register class
67 (defined only for the pressure classes). */
68 int max_reg_pressure
[N_REG_CLASSES
];
69 /* Loop regs referenced and live pseudo-registers. */
71 bitmap_head regs_live
;
74 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
76 /* The description of an use. */
80 rtx
*pos
; /* Position of the use. */
81 rtx insn
; /* The insn in that the use occurs. */
82 unsigned addr_use_p
; /* Whether the use occurs in an address. */
83 struct use
*next
; /* Next use in the list. */
86 /* The description of a def. */
90 struct use
*uses
; /* The list of uses that are uniquely reached
92 unsigned n_uses
; /* Number of such uses. */
93 unsigned n_addr_uses
; /* Number of uses in addresses. */
94 unsigned invno
; /* The corresponding invariant. */
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
;
163 enum machine_mode mode
;
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
, 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 insn1
, rtx e1
, rtx 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
: typed_free_remove
<invariant_expr_entry
>
432 typedef invariant_expr_entry value_type
;
433 typedef invariant_expr_entry compare_type
;
434 static inline hashval_t
hash (const value_type
*);
435 static inline bool equal (const value_type
*, const compare_type
*);
438 /* Returns hash value for invariant expression entry ENTRY. */
441 invariant_expr_hasher::hash (const value_type
*entry
)
446 /* Compares invariant expression entries ENTRY1 and ENTRY2. */
449 invariant_expr_hasher::equal (const value_type
*entry1
,
450 const compare_type
*entry2
)
452 if (entry1
->mode
!= entry2
->mode
)
455 return invariant_expr_equal_p (entry1
->inv
->insn
, entry1
->expr
,
456 entry2
->inv
->insn
, entry2
->expr
);
459 typedef hash_table
<invariant_expr_hasher
> invariant_htab_type
;
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. */
465 static struct invariant
*
466 find_or_insert_inv (invariant_htab_type
*eq
, rtx expr
, enum machine_mode mode
,
467 struct invariant
*inv
)
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
;
477 slot
= eq
->find_slot_with_hash (&pentry
, hash
, INSERT
);
483 entry
= XNEW (struct invariant_expr_entry
);
493 /* Finds invariants identical to INV and records the equivalence. EQ is the
494 hash table of the invariants. */
497 find_identical_invariants (invariant_htab_type
*eq
, struct invariant
*inv
)
501 struct invariant
*dep
;
503 enum machine_mode mode
;
504 struct invariant
*tmp
;
506 if (inv
->eqto
!= ~0u)
509 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
511 dep
= invariants
[depno
];
512 find_identical_invariants (eq
, dep
);
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
));
521 tmp
= find_or_insert_inv (eq
, expr
, mode
, inv
);
522 inv
->eqto
= tmp
->invno
;
524 if (tmp
->invno
!= inv
->invno
&& inv
->always_executed
)
527 if (dump_file
&& inv
->eqto
!= inv
->invno
)
529 "Invariant %d is equivalent to invariant %d.\n",
530 inv
->invno
, inv
->eqto
);
533 /* Find invariants with the same value and record the equivalences. */
536 merge_identical_invariants (void)
539 struct invariant
*inv
;
540 invariant_htab_type
eq (invariants
.length ());
542 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
543 find_identical_invariants (&eq
, inv
);
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. */
553 compute_always_reached (struct loop
*loop
, basic_block
*body
,
554 bitmap may_exit
, bitmap always_reached
)
558 for (i
= 0; i
< loop
->num_nodes
; i
++)
560 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, body
[i
]))
561 bitmap_set_bit (always_reached
, i
);
563 if (bitmap_bit_p (may_exit
, i
))
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. */
573 find_exits (struct loop
*loop
, basic_block
*body
,
574 bitmap may_exit
, bitmap has_exit
)
579 struct loop
*outermost_exit
= loop
, *aexit
;
580 bool has_call
= false;
583 for (i
= 0; i
< loop
->num_nodes
; i
++)
585 if (body
[i
]->loop_father
== loop
)
587 FOR_BB_INSNS (body
[i
], insn
)
590 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
591 || !RTL_CONST_OR_PURE_CALL_P (insn
)))
594 bitmap_set_bit (may_exit
, i
);
599 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
601 if (flow_bb_inside_loop_p (loop
, e
->dest
))
604 bitmap_set_bit (may_exit
, i
);
605 bitmap_set_bit (has_exit
, i
);
606 outermost_exit
= find_common_loop (outermost_exit
,
607 e
->dest
->loop_father
);
612 /* Use the data stored for the subloop to decide whether we may exit
613 through it. It is sufficient to do this for header of the loop,
614 as other basic blocks inside it must be dominated by it. */
615 if (body
[i
]->loop_father
->header
!= body
[i
])
618 if (LOOP_DATA (body
[i
]->loop_father
)->has_call
)
621 bitmap_set_bit (may_exit
, i
);
623 aexit
= LOOP_DATA (body
[i
]->loop_father
)->outermost_exit
;
626 bitmap_set_bit (may_exit
, i
);
627 bitmap_set_bit (has_exit
, i
);
629 if (flow_loop_nested_p (aexit
, outermost_exit
))
630 outermost_exit
= aexit
;
634 if (loop
->aux
== NULL
)
636 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
637 bitmap_initialize (&LOOP_DATA (loop
)->regs_ref
, ®_obstack
);
638 bitmap_initialize (&LOOP_DATA (loop
)->regs_live
, ®_obstack
);
640 LOOP_DATA (loop
)->outermost_exit
= outermost_exit
;
641 LOOP_DATA (loop
)->has_call
= has_call
;
644 /* Check whether we may assign a value to X from a register. */
647 may_assign_reg_p (rtx x
)
649 return (GET_MODE (x
) != VOIDmode
650 && GET_MODE (x
) != BLKmode
651 && can_copy_p (GET_MODE (x
))
653 || !HARD_REGISTER_P (x
)
654 || REGNO_REG_CLASS (REGNO (x
)) != NO_REGS
));
657 /* Finds definitions that may correspond to invariants in LOOP with body
661 find_defs (struct loop
*loop
)
666 "*****starting processing of loop %d ******\n",
670 df_remove_problem (df_chain
);
671 df_process_deferred_rescans ();
672 df_chain_add_problem (DF_UD_CHAIN
);
673 df_set_flags (DF_RD_PRUNE_DEAD_DEFS
);
674 df_analyze_loop (loop
);
675 check_invariant_table_size ();
679 df_dump_region (dump_file
);
681 "*****ending processing of loop %d ******\n",
686 /* Creates a new invariant for definition DEF in INSN, depending on invariants
687 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
688 unless the program ends due to a function call. The newly created invariant
691 static struct invariant
*
692 create_new_invariant (struct def
*def
, rtx insn
, bitmap depends_on
,
693 bool always_executed
)
695 struct invariant
*inv
= XNEW (struct invariant
);
696 rtx set
= single_set (insn
);
697 bool speed
= optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn
));
700 inv
->always_executed
= always_executed
;
701 inv
->depends_on
= depends_on
;
703 /* If the set is simple, usually by moving it we move the whole store out of
704 the loop. Otherwise we save only cost of the computation. */
707 inv
->cost
= set_rtx_cost (set
, speed
);
708 /* ??? Try to determine cheapness of address computation. Unfortunately
709 the address cost is only a relative measure, we can't really compare
710 it with any absolute number, but only with other address costs.
711 But here we don't have any other addresses, so compare with a magic
712 number anyway. It has to be large enough to not regress PR33928
713 (by avoiding to move reg+8,reg+16,reg+24 invariants), but small
714 enough to not regress 410.bwaves either (by still moving reg+reg
716 See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html . */
717 inv
->cheap_address
= address_cost (SET_SRC (set
), word_mode
,
718 ADDR_SPACE_GENERIC
, speed
) < 3;
722 inv
->cost
= set_src_cost (SET_SRC (set
), speed
);
723 inv
->cheap_address
= false;
728 inv
->orig_regno
= -1;
732 inv
->invno
= invariants
.length ();
739 def
->invno
= inv
->invno
;
740 invariants
.safe_push (inv
);
745 "Set in insn %d is invariant (%d), cost %d, depends on ",
746 INSN_UID (insn
), inv
->invno
, inv
->cost
);
747 dump_bitmap (dump_file
, inv
->depends_on
);
753 /* Record USE at DEF. */
756 record_use (struct def
*def
, df_ref use
)
758 struct use
*u
= XNEW (struct use
);
760 u
->pos
= DF_REF_REAL_LOC (use
);
761 u
->insn
= DF_REF_INSN (use
);
762 u
->addr_use_p
= (DF_REF_TYPE (use
) == DF_REF_REG_MEM_LOAD
763 || DF_REF_TYPE (use
) == DF_REF_REG_MEM_STORE
);
771 /* Finds the invariants USE depends on and store them to the DEPENDS_ON
772 bitmap. Returns true if all dependencies of USE are known to be
773 loop invariants, false otherwise. */
776 check_dependency (basic_block bb
, df_ref use
, bitmap depends_on
)
780 struct df_link
*defs
;
781 struct def
*def_data
;
782 struct invariant
*inv
;
784 if (DF_REF_FLAGS (use
) & DF_REF_READ_WRITE
)
787 defs
= DF_REF_CHAIN (use
);
790 unsigned int regno
= DF_REF_REGNO (use
);
792 /* If this is the use of an uninitialized argument register that is
793 likely to be spilled, do not move it lest this might extend its
794 lifetime and cause reload to die. This can occur for a call to
795 a function taking complex number arguments and moving the insns
796 preparing the arguments without moving the call itself wouldn't
797 gain much in practice. */
798 if ((DF_REF_FLAGS (use
) & DF_HARD_REG_LIVE
)
799 && FUNCTION_ARG_REGNO_P (regno
)
800 && targetm
.class_likely_spilled_p (REGNO_REG_CLASS (regno
)))
810 check_invariant_table_size ();
811 inv
= invariant_table
[DF_REF_ID (def
)];
816 gcc_assert (def_data
!= NULL
);
818 def_bb
= DF_REF_BB (def
);
819 /* Note that in case bb == def_bb, we know that the definition
820 dominates insn, because def has invariant_table[DF_REF_ID(def)]
821 defined and we process the insns in the basic block bb
823 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
826 bitmap_set_bit (depends_on
, def_data
->invno
);
831 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
832 bitmap. Returns true if all dependencies of INSN are known to be
833 loop invariants, false otherwise. */
836 check_dependencies (rtx insn
, bitmap depends_on
)
838 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
840 basic_block bb
= BLOCK_FOR_INSN (insn
);
842 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
843 if (!check_dependency (bb
, use
, depends_on
))
845 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
846 if (!check_dependency (bb
, use
, depends_on
))
852 /* Pre-check candidate DEST to skip the one which can not make a valid insn
853 during move_invariant_reg. SIMPLE is to skip HARD_REGISTER. */
855 pre_check_invariant_p (bool simple
, rtx dest
)
857 if (simple
&& REG_P (dest
) && DF_REG_DEF_COUNT (REGNO (dest
)) > 1)
861 unsigned int i
= REGNO (dest
);
862 struct df_insn_info
*insn_info
;
865 for (use
= DF_REG_USE_CHAIN (i
); use
; use
= DF_REF_NEXT_REG (use
))
867 ref
= DF_REF_INSN (use
);
868 insn_info
= DF_INSN_INFO_GET (ref
);
870 FOR_EACH_INSN_INFO_DEF (def_rec
, insn_info
)
871 if (DF_REF_REGNO (def_rec
) == i
)
873 /* Multi definitions at this stage, most likely are due to
874 instruction constraints, which requires both read and write
875 on the same register. Since move_invariant_reg is not
876 powerful enough to handle such cases, just ignore the INV
877 and leave the chance to others. */
885 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
886 executed. ALWAYS_EXECUTED is true if the insn is always executed,
887 unless the program ends due to a function call. */
890 find_invariant_insn (rtx insn
, bool always_reached
, bool always_executed
)
897 struct invariant
*inv
;
900 /* We can't move a CC0 setter without the user. */
901 if (sets_cc0_p (insn
))
905 set
= single_set (insn
);
908 dest
= SET_DEST (set
);
911 || HARD_REGISTER_P (dest
))
914 if (!may_assign_reg_p (dest
)
915 || !pre_check_invariant_p (simple
, dest
)
916 || !check_maybe_invariant (SET_SRC (set
)))
919 /* If the insn can throw exception, we cannot move it at all without changing
921 if (can_throw_internal (insn
))
924 /* We cannot make trapping insn executed, unless it was executed before. */
925 if (may_trap_or_fault_p (PATTERN (insn
)) && !always_reached
)
928 depends_on
= BITMAP_ALLOC (NULL
);
929 if (!check_dependencies (insn
, depends_on
))
931 BITMAP_FREE (depends_on
);
936 def
= XCNEW (struct def
);
940 inv
= create_new_invariant (def
, insn
, depends_on
, always_executed
);
944 ref
= df_find_def (insn
, dest
);
945 check_invariant_table_size ();
946 invariant_table
[DF_REF_ID (ref
)] = inv
;
950 /* Record registers used in INSN that have a unique invariant definition. */
953 record_uses (rtx insn
)
955 struct df_insn_info
*insn_info
= DF_INSN_INFO_GET (insn
);
957 struct invariant
*inv
;
959 FOR_EACH_INSN_INFO_USE (use
, insn_info
)
961 inv
= invariant_for_use (use
);
963 record_use (inv
->def
, use
);
965 FOR_EACH_INSN_INFO_EQ_USE (use
, insn_info
)
967 inv
= invariant_for_use (use
);
969 record_use (inv
->def
, use
);
973 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
974 executed. ALWAYS_EXECUTED is true if the insn is always executed,
975 unless the program ends due to a function call. */
978 find_invariants_insn (rtx insn
, bool always_reached
, bool always_executed
)
980 find_invariant_insn (insn
, always_reached
, always_executed
);
984 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
985 basic block is always executed. ALWAYS_EXECUTED is true if the basic
986 block is always executed, unless the program ends due to a function
990 find_invariants_bb (basic_block bb
, bool always_reached
, bool always_executed
)
994 FOR_BB_INSNS (bb
, insn
)
996 if (!NONDEBUG_INSN_P (insn
))
999 find_invariants_insn (insn
, always_reached
, always_executed
);
1003 && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn
)
1004 || ! RTL_CONST_OR_PURE_CALL_P (insn
)))
1005 always_reached
= false;
1009 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
1010 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
1011 bitmap of basic blocks in BODY that are always executed unless the program
1012 ends due to a function call. */
1015 find_invariants_body (struct loop
*loop
, basic_block
*body
,
1016 bitmap always_reached
, bitmap always_executed
)
1020 for (i
= 0; i
< loop
->num_nodes
; i
++)
1021 find_invariants_bb (body
[i
],
1022 bitmap_bit_p (always_reached
, i
),
1023 bitmap_bit_p (always_executed
, i
));
1026 /* Finds invariants in LOOP. */
1029 find_invariants (struct loop
*loop
)
1031 bitmap may_exit
= BITMAP_ALLOC (NULL
);
1032 bitmap always_reached
= BITMAP_ALLOC (NULL
);
1033 bitmap has_exit
= BITMAP_ALLOC (NULL
);
1034 bitmap always_executed
= BITMAP_ALLOC (NULL
);
1035 basic_block
*body
= get_loop_body_in_dom_order (loop
);
1037 find_exits (loop
, body
, may_exit
, has_exit
);
1038 compute_always_reached (loop
, body
, may_exit
, always_reached
);
1039 compute_always_reached (loop
, body
, has_exit
, always_executed
);
1042 find_invariants_body (loop
, body
, always_reached
, always_executed
);
1043 merge_identical_invariants ();
1045 BITMAP_FREE (always_reached
);
1046 BITMAP_FREE (always_executed
);
1047 BITMAP_FREE (may_exit
);
1048 BITMAP_FREE (has_exit
);
1052 /* Frees a list of uses USE. */
1055 free_use_list (struct use
*use
)
1059 for (; use
; use
= next
)
1066 /* Return pressure class and number of hard registers (through *NREGS)
1067 for destination of INSN. */
1068 static enum reg_class
1069 get_pressure_class_and_nregs (rtx insn
, int *nregs
)
1072 enum reg_class pressure_class
;
1073 rtx set
= single_set (insn
);
1075 /* Considered invariant insns have only one set. */
1076 gcc_assert (set
!= NULL_RTX
);
1077 reg
= SET_DEST (set
);
1078 if (GET_CODE (reg
) == SUBREG
)
1079 reg
= SUBREG_REG (reg
);
1083 pressure_class
= NO_REGS
;
1089 if (reg
== NULL_RTX
)
1090 pressure_class
= GENERAL_REGS
;
1093 pressure_class
= reg_allocno_class (REGNO (reg
));
1094 pressure_class
= ira_pressure_class_translate
[pressure_class
];
1097 = ira_reg_class_max_nregs
[pressure_class
][GET_MODE (SET_SRC (set
))];
1099 return pressure_class
;
1102 /* Calculates cost and number of registers needed for moving invariant INV
1103 out of the loop and stores them to *COST and *REGS_NEEDED. *CL will be
1104 the REG_CLASS of INV. Return
1105 -1: if INV is invalid.
1106 0: if INV and its depends_on have same reg_class
1107 1: if INV and its depends_on have different reg_classes. */
1110 get_inv_cost (struct invariant
*inv
, int *comp_cost
, unsigned *regs_needed
,
1114 unsigned aregs_needed
[N_REG_CLASSES
];
1116 struct invariant
*dep
;
1120 /* Find the representative of the class of the equivalent invariants. */
1121 inv
= invariants
[inv
->eqto
];
1124 if (! flag_ira_loop_pressure
)
1128 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1129 regs_needed
[ira_pressure_classes
[i
]] = 0;
1133 || inv
->stamp
== actual_stamp
)
1135 inv
->stamp
= actual_stamp
;
1137 if (! flag_ira_loop_pressure
)
1142 enum reg_class pressure_class
;
1144 pressure_class
= get_pressure_class_and_nregs (inv
->insn
, &nregs
);
1145 regs_needed
[pressure_class
] += nregs
;
1146 *cl
= pressure_class
;
1150 if (!inv
->cheap_address
1151 || inv
->def
->n_addr_uses
< inv
->def
->n_uses
)
1152 (*comp_cost
) += inv
->cost
* inv
->eqno
;
1156 /* Hoisting constant pool constants into stack regs may cost more than
1157 just single register. On x87, the balance is affected both by the
1158 small number of FP registers, and by its register stack organization,
1159 that forces us to add compensation code in and around the loop to
1160 shuffle the operands to the top of stack before use, and pop them
1161 from the stack after the loop finishes.
1163 To model this effect, we increase the number of registers needed for
1164 stack registers by two: one register push, and one register pop.
1165 This usually has the effect that FP constant loads from the constant
1166 pool are not moved out of the loop.
1168 Note that this also means that dependent invariants can not be moved.
1169 However, the primary purpose of this pass is to move loop invariant
1170 address arithmetic out of loops, and address arithmetic that depends
1171 on floating point constants is unlikely to ever occur. */
1172 rtx set
= single_set (inv
->insn
);
1174 && IS_STACK_MODE (GET_MODE (SET_SRC (set
)))
1175 && constant_pool_constant_p (SET_SRC (set
)))
1177 if (flag_ira_loop_pressure
)
1178 regs_needed
[ira_stack_reg_pressure_class
] += 2;
1180 regs_needed
[0] += 2;
1185 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
1188 enum reg_class dep_cl
= ALL_REGS
;
1191 dep
= invariants
[depno
];
1193 /* If DEP is moved out of the loop, it is not a depends_on any more. */
1197 dep_ret
= get_inv_cost (dep
, &acomp_cost
, aregs_needed
, &dep_cl
);
1199 if (! flag_ira_loop_pressure
)
1200 check_p
= aregs_needed
[0] != 0;
1203 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1204 if (aregs_needed
[ira_pressure_classes
[i
]] != 0)
1206 check_p
= i
< ira_pressure_classes_num
;
1208 if ((dep_ret
== 1) || ((dep_ret
== 0) && (*cl
!= dep_cl
)))
1215 /* We need to check always_executed, since if the original value of
1216 the invariant may be preserved, we may need to keep it in a
1217 separate register. TODO check whether the register has an
1218 use outside of the loop. */
1219 && dep
->always_executed
1220 && !dep
->def
->uses
->next
)
1222 /* If this is a single use, after moving the dependency we will not
1223 need a new register. */
1224 if (! flag_ira_loop_pressure
)
1229 enum reg_class pressure_class
;
1231 pressure_class
= get_pressure_class_and_nregs (inv
->insn
, &nregs
);
1232 aregs_needed
[pressure_class
] -= nregs
;
1236 if (! flag_ira_loop_pressure
)
1237 regs_needed
[0] += aregs_needed
[0];
1240 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1241 regs_needed
[ira_pressure_classes
[i
]]
1242 += aregs_needed
[ira_pressure_classes
[i
]];
1244 (*comp_cost
) += acomp_cost
;
1249 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
1250 of registers used in the loop, NEW_REGS is the number of new variables
1251 already added due to the invariant motion. The number of registers needed
1252 for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
1253 through to estimate_reg_pressure_cost. */
1256 gain_for_invariant (struct invariant
*inv
, unsigned *regs_needed
,
1257 unsigned *new_regs
, unsigned regs_used
,
1258 bool speed
, bool call_p
)
1260 int comp_cost
, size_cost
;
1266 ret
= get_inv_cost (inv
, &comp_cost
, regs_needed
, &cl
);
1268 if (! flag_ira_loop_pressure
)
1270 size_cost
= (estimate_reg_pressure_cost (new_regs
[0] + regs_needed
[0],
1271 regs_used
, speed
, call_p
)
1272 - estimate_reg_pressure_cost (new_regs
[0],
1273 regs_used
, speed
, call_p
));
1277 else if ((ret
== 0) && (cl
== NO_REGS
))
1278 /* Hoist it anyway since it does not impact register pressure. */
1283 enum reg_class pressure_class
;
1285 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1287 pressure_class
= ira_pressure_classes
[i
];
1289 if (!reg_classes_intersect_p (pressure_class
, cl
))
1292 if ((int) new_regs
[pressure_class
]
1293 + (int) regs_needed
[pressure_class
]
1294 + LOOP_DATA (curr_loop
)->max_reg_pressure
[pressure_class
]
1295 + IRA_LOOP_RESERVED_REGS
1296 > ira_class_hard_regs_num
[pressure_class
])
1299 if (i
< ira_pressure_classes_num
)
1300 /* There will be register pressure excess and we want not to
1301 make this loop invariant motion. All loop invariants with
1302 non-positive gains will be rejected in function
1303 find_invariants_to_move. Therefore we return the negative
1306 One could think that this rejects also expensive loop
1307 invariant motions and this will hurt code performance.
1308 However numerous experiments with different heuristics
1309 taking invariant cost into account did not confirm this
1310 assumption. There are possible explanations for this
1312 o probably all expensive invariants were already moved out
1313 of the loop by PRE and gimple invariant motion pass.
1314 o expensive invariant execution will be hidden by insn
1315 scheduling or OOO processor hardware because usually such
1316 invariants have a lot of freedom to be executed
1318 Another reason for ignoring invariant cost vs spilling cost
1319 heuristics is also in difficulties to evaluate accurately
1320 spill cost at this stage. */
1326 return comp_cost
- size_cost
;
1329 /* Finds invariant with best gain for moving. Returns the gain, stores
1330 the invariant in *BEST and number of registers needed for it to
1331 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1332 NEW_REGS is the number of new variables already added due to invariant
1336 best_gain_for_invariant (struct invariant
**best
, unsigned *regs_needed
,
1337 unsigned *new_regs
, unsigned regs_used
,
1338 bool speed
, bool call_p
)
1340 struct invariant
*inv
;
1341 int i
, gain
= 0, again
;
1342 unsigned aregs_needed
[N_REG_CLASSES
], invno
;
1344 FOR_EACH_VEC_ELT (invariants
, invno
, inv
)
1349 /* Only consider the "representatives" of equivalent invariants. */
1350 if (inv
->eqto
!= inv
->invno
)
1353 again
= gain_for_invariant (inv
, aregs_needed
, new_regs
, regs_used
,
1359 if (! flag_ira_loop_pressure
)
1360 regs_needed
[0] = aregs_needed
[0];
1363 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1364 regs_needed
[ira_pressure_classes
[i
]]
1365 = aregs_needed
[ira_pressure_classes
[i
]];
1373 /* Marks invariant INVNO and all its dependencies for moving. */
1376 set_move_mark (unsigned invno
, int gain
)
1378 struct invariant
*inv
= invariants
[invno
];
1381 /* Find the representative of the class of the equivalent invariants. */
1382 inv
= invariants
[inv
->eqto
];
1391 fprintf (dump_file
, "Decided to move invariant %d -- gain %d\n",
1394 fprintf (dump_file
, "Decided to move dependent invariant %d\n",
1398 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, invno
, bi
)
1400 set_move_mark (invno
, -1);
1404 /* Determines which invariants to move. */
1407 find_invariants_to_move (bool speed
, bool call_p
)
1410 unsigned i
, regs_used
, regs_needed
[N_REG_CLASSES
], new_regs
[N_REG_CLASSES
];
1411 struct invariant
*inv
= NULL
;
1413 if (!invariants
.length ())
1416 if (flag_ira_loop_pressure
)
1417 /* REGS_USED is actually never used when the flag is on. */
1420 /* We do not really do a good job in estimating number of
1421 registers used; we put some initial bound here to stand for
1422 induction variables etc. that we do not detect. */
1424 unsigned int n_regs
= DF_REG_SIZE (df
);
1428 for (i
= 0; i
< n_regs
; i
++)
1430 if (!DF_REGNO_FIRST_DEF (i
) && DF_REGNO_LAST_USE (i
))
1432 /* This is a value that is used but not changed inside loop. */
1438 if (! flag_ira_loop_pressure
)
1439 new_regs
[0] = regs_needed
[0] = 0;
1442 for (i
= 0; (int) i
< ira_pressure_classes_num
; i
++)
1443 new_regs
[ira_pressure_classes
[i
]] = 0;
1445 while ((gain
= best_gain_for_invariant (&inv
, regs_needed
,
1446 new_regs
, regs_used
,
1447 speed
, call_p
)) > 0)
1449 set_move_mark (inv
->invno
, gain
);
1450 if (! flag_ira_loop_pressure
)
1451 new_regs
[0] += regs_needed
[0];
1454 for (i
= 0; (int) i
< ira_pressure_classes_num
; i
++)
1455 new_regs
[ira_pressure_classes
[i
]]
1456 += regs_needed
[ira_pressure_classes
[i
]];
1461 /* Replace the uses, reached by the definition of invariant INV, by REG.
1463 IN_GROUP is nonzero if this is part of a group of changes that must be
1464 performed as a group. In that case, the changes will be stored. The
1465 function `apply_change_group' will validate and apply the changes. */
1468 replace_uses (struct invariant
*inv
, rtx reg
, bool in_group
)
1470 /* Replace the uses we know to be dominated. It saves work for copy
1471 propagation, and also it is necessary so that dependent invariants
1472 are computed right. */
1476 for (use
= inv
->def
->uses
; use
; use
= use
->next
)
1477 validate_change (use
->insn
, use
->pos
, reg
, true);
1479 /* If we aren't part of a larger group, apply the changes now. */
1481 return apply_change_group ();
1487 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1491 move_invariant_reg (struct loop
*loop
, unsigned invno
)
1493 struct invariant
*inv
= invariants
[invno
];
1494 struct invariant
*repr
= invariants
[inv
->eqto
];
1496 basic_block preheader
= loop_preheader_edge (loop
)->src
;
1497 rtx reg
, set
, dest
, note
;
1506 /* If this is a representative of the class of equivalent invariants,
1507 really move the invariant. Otherwise just replace its use with
1508 the register used for the representative. */
1511 if (inv
->depends_on
)
1513 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, i
, bi
)
1515 if (!move_invariant_reg (loop
, i
))
1520 /* Move the set out of the loop. If the set is always executed (we could
1521 omit this condition if we know that the register is unused outside of
1522 the loop, but it does not seem worth finding out) and it has no uses
1523 that would not be dominated by it, we may just move it (TODO).
1524 Otherwise we need to create a temporary register. */
1525 set
= single_set (inv
->insn
);
1526 reg
= dest
= SET_DEST (set
);
1527 if (GET_CODE (reg
) == SUBREG
)
1528 reg
= SUBREG_REG (reg
);
1530 regno
= REGNO (reg
);
1532 reg
= gen_reg_rtx_and_attrs (dest
);
1534 /* Try replacing the destination by a new pseudoregister. */
1535 validate_change (inv
->insn
, &SET_DEST (set
), reg
, true);
1537 /* As well as all the dominated uses. */
1538 replace_uses (inv
, reg
, true);
1540 /* And validate all the changes. */
1541 if (!apply_change_group ())
1544 emit_insn_after (gen_move_insn (dest
, reg
), inv
->insn
);
1545 reorder_insns (inv
->insn
, inv
->insn
, BB_END (preheader
));
1547 /* If there is a REG_EQUAL note on the insn we just moved, and the
1548 insn is in a basic block that is not always executed or the note
1549 contains something for which we don't know the invariant status,
1550 the note may no longer be valid after we move the insn. Note that
1551 uses in REG_EQUAL notes are taken into account in the computation
1552 of invariants, so it is safe to retain the note even if it contains
1553 register references for which we know the invariant status. */
1554 if ((note
= find_reg_note (inv
->insn
, REG_EQUAL
, NULL_RTX
))
1555 && (!inv
->always_executed
1556 || !check_maybe_invariant (XEXP (note
, 0))))
1557 remove_note (inv
->insn
, note
);
1561 if (!move_invariant_reg (loop
, repr
->invno
))
1564 regno
= repr
->orig_regno
;
1565 if (!replace_uses (inv
, reg
, false))
1567 set
= single_set (inv
->insn
);
1568 emit_insn_after (gen_move_insn (SET_DEST (set
), reg
), inv
->insn
);
1569 delete_insn (inv
->insn
);
1573 inv
->orig_regno
= regno
;
1578 /* If we failed, clear move flag, so that we do not try to move inv
1581 fprintf (dump_file
, "Failed to move invariant %d\n", invno
);
1583 inv
->reg
= NULL_RTX
;
1584 inv
->orig_regno
= -1;
1589 /* Move selected invariant out of the LOOP. Newly created regs are marked
1590 in TEMPORARY_REGS. */
1593 move_invariants (struct loop
*loop
)
1595 struct invariant
*inv
;
1598 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
1599 move_invariant_reg (loop
, i
);
1600 if (flag_ira_loop_pressure
&& resize_reg_info ())
1602 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
1603 if (inv
->reg
!= NULL_RTX
)
1605 if (inv
->orig_regno
>= 0)
1606 setup_reg_classes (REGNO (inv
->reg
),
1607 reg_preferred_class (inv
->orig_regno
),
1608 reg_alternate_class (inv
->orig_regno
),
1609 reg_allocno_class (inv
->orig_regno
));
1611 setup_reg_classes (REGNO (inv
->reg
),
1612 GENERAL_REGS
, NO_REGS
, GENERAL_REGS
);
1617 /* Initializes invariant motion data. */
1620 init_inv_motion_data (void)
1624 invariants
.create (100);
1627 /* Frees the data allocated by invariant motion. */
1630 free_inv_motion_data (void)
1634 struct invariant
*inv
;
1636 check_invariant_table_size ();
1637 for (i
= 0; i
< DF_DEFS_TABLE_SIZE (); i
++)
1639 inv
= invariant_table
[i
];
1643 gcc_assert (def
!= NULL
);
1645 free_use_list (def
->uses
);
1647 invariant_table
[i
] = NULL
;
1651 FOR_EACH_VEC_ELT (invariants
, i
, inv
)
1653 BITMAP_FREE (inv
->depends_on
);
1656 invariants
.release ();
1659 /* Move the invariants out of the LOOP. */
1662 move_single_loop_invariants (struct loop
*loop
)
1664 init_inv_motion_data ();
1666 find_invariants (loop
);
1667 find_invariants_to_move (optimize_loop_for_speed_p (loop
),
1668 LOOP_DATA (loop
)->has_call
);
1669 move_invariants (loop
);
1671 free_inv_motion_data ();
1674 /* Releases the auxiliary data for LOOP. */
1677 free_loop_data (struct loop
*loop
)
1679 struct loop_data
*data
= LOOP_DATA (loop
);
1683 bitmap_clear (&LOOP_DATA (loop
)->regs_ref
);
1684 bitmap_clear (&LOOP_DATA (loop
)->regs_live
);
1691 /* Registers currently living. */
1692 static bitmap_head curr_regs_live
;
1694 /* Current reg pressure for each pressure class. */
1695 static int curr_reg_pressure
[N_REG_CLASSES
];
1697 /* Record all regs that are set in any one insn. Communication from
1698 mark_reg_{store,clobber} and global_conflicts. Asm can refer to
1699 all hard-registers. */
1700 static rtx regs_set
[(FIRST_PSEUDO_REGISTER
> MAX_RECOG_OPERANDS
1701 ? FIRST_PSEUDO_REGISTER
: MAX_RECOG_OPERANDS
) * 2];
1702 /* Number of regs stored in the previous array. */
1703 static int n_regs_set
;
1705 /* Return pressure class and number of needed hard registers (through
1706 *NREGS) of register REGNO. */
1707 static enum reg_class
1708 get_regno_pressure_class (int regno
, int *nregs
)
1710 if (regno
>= FIRST_PSEUDO_REGISTER
)
1712 enum reg_class pressure_class
;
1714 pressure_class
= reg_allocno_class (regno
);
1715 pressure_class
= ira_pressure_class_translate
[pressure_class
];
1717 = ira_reg_class_max_nregs
[pressure_class
][PSEUDO_REGNO_MODE (regno
)];
1718 return pressure_class
;
1720 else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs
, regno
)
1721 && ! TEST_HARD_REG_BIT (eliminable_regset
, regno
))
1724 return ira_pressure_class_translate
[REGNO_REG_CLASS (regno
)];
1733 /* Increase (if INCR_P) or decrease current register pressure for
1736 change_pressure (int regno
, bool incr_p
)
1739 enum reg_class pressure_class
;
1741 pressure_class
= get_regno_pressure_class (regno
, &nregs
);
1743 curr_reg_pressure
[pressure_class
] -= nregs
;
1746 curr_reg_pressure
[pressure_class
] += nregs
;
1747 if (LOOP_DATA (curr_loop
)->max_reg_pressure
[pressure_class
]
1748 < curr_reg_pressure
[pressure_class
])
1749 LOOP_DATA (curr_loop
)->max_reg_pressure
[pressure_class
]
1750 = curr_reg_pressure
[pressure_class
];
1754 /* Mark REGNO birth. */
1756 mark_regno_live (int regno
)
1760 for (loop
= curr_loop
;
1761 loop
!= current_loops
->tree_root
;
1762 loop
= loop_outer (loop
))
1763 bitmap_set_bit (&LOOP_DATA (loop
)->regs_live
, regno
);
1764 if (!bitmap_set_bit (&curr_regs_live
, regno
))
1766 change_pressure (regno
, true);
1769 /* Mark REGNO death. */
1771 mark_regno_death (int regno
)
1773 if (! bitmap_clear_bit (&curr_regs_live
, regno
))
1775 change_pressure (regno
, false);
1778 /* Mark setting register REG. */
1780 mark_reg_store (rtx reg
, const_rtx setter ATTRIBUTE_UNUSED
,
1781 void *data ATTRIBUTE_UNUSED
)
1785 if (GET_CODE (reg
) == SUBREG
)
1786 reg
= SUBREG_REG (reg
);
1791 regs_set
[n_regs_set
++] = reg
;
1793 regno
= REGNO (reg
);
1795 if (regno
>= FIRST_PSEUDO_REGISTER
)
1796 mark_regno_live (regno
);
1799 int last
= regno
+ hard_regno_nregs
[regno
][GET_MODE (reg
)];
1801 while (regno
< last
)
1803 mark_regno_live (regno
);
1809 /* Mark clobbering register REG. */
1811 mark_reg_clobber (rtx reg
, const_rtx setter
, void *data
)
1813 if (GET_CODE (setter
) == CLOBBER
)
1814 mark_reg_store (reg
, setter
, data
);
1817 /* Mark register REG death. */
1819 mark_reg_death (rtx reg
)
1821 int regno
= REGNO (reg
);
1823 if (regno
>= FIRST_PSEUDO_REGISTER
)
1824 mark_regno_death (regno
);
1827 int last
= regno
+ hard_regno_nregs
[regno
][GET_MODE (reg
)];
1829 while (regno
< last
)
1831 mark_regno_death (regno
);
1837 /* Mark occurrence of registers in X for the current loop. */
1839 mark_ref_regs (rtx x
)
1848 code
= GET_CODE (x
);
1853 for (loop
= curr_loop
;
1854 loop
!= current_loops
->tree_root
;
1855 loop
= loop_outer (loop
))
1856 bitmap_set_bit (&LOOP_DATA (loop
)->regs_ref
, REGNO (x
));
1860 fmt
= GET_RTX_FORMAT (code
);
1861 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1863 mark_ref_regs (XEXP (x
, i
));
1864 else if (fmt
[i
] == 'E')
1868 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1869 mark_ref_regs (XVECEXP (x
, i
, j
));
1873 /* Calculate register pressure in the loops. */
1875 calculate_loop_reg_pressure (void)
1882 struct loop
*loop
, *parent
;
1884 FOR_EACH_LOOP (loop
, 0)
1885 if (loop
->aux
== NULL
)
1887 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
1888 bitmap_initialize (&LOOP_DATA (loop
)->regs_ref
, ®_obstack
);
1889 bitmap_initialize (&LOOP_DATA (loop
)->regs_live
, ®_obstack
);
1891 ira_setup_eliminable_regset ();
1892 bitmap_initialize (&curr_regs_live
, ®_obstack
);
1893 FOR_EACH_BB_FN (bb
, cfun
)
1895 curr_loop
= bb
->loop_father
;
1896 if (curr_loop
== current_loops
->tree_root
)
1899 for (loop
= curr_loop
;
1900 loop
!= current_loops
->tree_root
;
1901 loop
= loop_outer (loop
))
1902 bitmap_ior_into (&LOOP_DATA (loop
)->regs_live
, DF_LR_IN (bb
));
1904 bitmap_copy (&curr_regs_live
, DF_LR_IN (bb
));
1905 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1906 curr_reg_pressure
[ira_pressure_classes
[i
]] = 0;
1907 EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live
, 0, j
, bi
)
1908 change_pressure (j
, true);
1910 FOR_BB_INSNS (bb
, insn
)
1912 if (! NONDEBUG_INSN_P (insn
))
1915 mark_ref_regs (PATTERN (insn
));
1917 note_stores (PATTERN (insn
), mark_reg_clobber
, NULL
);
1919 /* Mark any registers dead after INSN as dead now. */
1921 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
1922 if (REG_NOTE_KIND (link
) == REG_DEAD
)
1923 mark_reg_death (XEXP (link
, 0));
1925 /* Mark any registers set in INSN as live,
1926 and mark them as conflicting with all other live regs.
1927 Clobbers are processed again, so they conflict with
1928 the registers that are set. */
1930 note_stores (PATTERN (insn
), mark_reg_store
, NULL
);
1933 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
1934 if (REG_NOTE_KIND (link
) == REG_INC
)
1935 mark_reg_store (XEXP (link
, 0), NULL_RTX
, NULL
);
1937 while (n_regs_set
-- > 0)
1939 rtx note
= find_regno_note (insn
, REG_UNUSED
,
1940 REGNO (regs_set
[n_regs_set
]));
1944 mark_reg_death (XEXP (note
, 0));
1948 bitmap_clear (&curr_regs_live
);
1949 if (flag_ira_region
== IRA_REGION_MIXED
1950 || flag_ira_region
== IRA_REGION_ALL
)
1951 FOR_EACH_LOOP (loop
, 0)
1953 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop
)->regs_live
, 0, j
, bi
)
1954 if (! bitmap_bit_p (&LOOP_DATA (loop
)->regs_ref
, j
))
1956 enum reg_class pressure_class
;
1959 pressure_class
= get_regno_pressure_class (j
, &nregs
);
1960 LOOP_DATA (loop
)->max_reg_pressure
[pressure_class
] -= nregs
;
1963 if (dump_file
== NULL
)
1965 FOR_EACH_LOOP (loop
, 0)
1967 parent
= loop_outer (loop
);
1968 fprintf (dump_file
, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
1969 loop
->num
, (parent
== NULL
? -1 : parent
->num
),
1970 loop
->header
->index
, loop_depth (loop
));
1971 fprintf (dump_file
, "\n ref. regnos:");
1972 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop
)->regs_ref
, 0, j
, bi
)
1973 fprintf (dump_file
, " %d", j
);
1974 fprintf (dump_file
, "\n live regnos:");
1975 EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop
)->regs_live
, 0, j
, bi
)
1976 fprintf (dump_file
, " %d", j
);
1977 fprintf (dump_file
, "\n Pressure:");
1978 for (i
= 0; (int) i
< ira_pressure_classes_num
; i
++)
1980 enum reg_class pressure_class
;
1982 pressure_class
= ira_pressure_classes
[i
];
1983 if (LOOP_DATA (loop
)->max_reg_pressure
[pressure_class
] == 0)
1985 fprintf (dump_file
, " %s=%d", reg_class_names
[pressure_class
],
1986 LOOP_DATA (loop
)->max_reg_pressure
[pressure_class
]);
1988 fprintf (dump_file
, "\n");
1994 /* Move the invariants out of the loops. */
1997 move_loop_invariants (void)
2001 if (flag_ira_loop_pressure
)
2004 regstat_init_n_sets_and_refs ();
2005 ira_set_pseudo_classes (true, dump_file
);
2006 calculate_loop_reg_pressure ();
2007 regstat_free_n_sets_and_refs ();
2009 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
2010 /* Process the loops, innermost first. */
2011 FOR_EACH_LOOP (loop
, LI_FROM_INNERMOST
)
2014 /* move_single_loop_invariants for very large loops
2015 is time consuming and might need a lot of memory. */
2016 if (loop
->num_nodes
<= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP
)
2017 move_single_loop_invariants (loop
);
2020 FOR_EACH_LOOP (loop
, 0)
2022 free_loop_data (loop
);
2025 if (flag_ira_loop_pressure
)
2026 /* There is no sense to keep this info because it was most
2027 probably outdated by subsequent passes. */
2029 free (invariant_table
);
2030 invariant_table
= NULL
;
2031 invariant_table_size
= 0;
2033 #ifdef ENABLE_CHECKING
2034 verify_flow_info ();