1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004, 2005 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 2, 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 COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 /* This implements the loop invariant motion pass. It is very simple
22 (no calls, libcalls, etc.). This should be sufficient to cleanup things
23 like address arithmetics -- other more complicated invariants should be
24 eliminated on tree level either in tree-ssa-loop-im.c or in tree-ssa-pre.c.
26 We proceed loop by loop -- it is simpler than trying to handle things
27 globally and should not lose much. First we inspect all sets inside loop
28 and create a dependency graph on insns (saying "to move this insn, you must
29 also move the following insns").
31 We then need to determine what to move. We estimate the number of registers
32 used and move as many invariants as possible while we still have enough free
33 registers. We prefer the expensive invariants.
35 Then we move the selected invariants out of the loop, creating a new
36 temporaries for them if necessary. */
40 #include "coretypes.h"
44 #include "hard-reg-set.h"
46 #include "basic-block.h"
56 /* The data stored for the loop. */
60 struct loop
*outermost_exit
; /* The outermost exit of the loop. */
61 bool has_call
; /* True if the loop contains a call. */
64 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
66 /* The description of an use. */
70 rtx
*pos
; /* Position of the use. */
71 rtx insn
; /* The insn in that the use occurs. */
73 struct use
*next
; /* Next use in the list. */
76 /* The description of a def. */
80 struct use
*uses
; /* The list of uses that are uniquely reached
82 unsigned n_uses
; /* Number of such uses. */
83 unsigned invno
; /* The corresponding invariant. */
86 /* The data stored for each invariant. */
90 /* The number of the invariant. */
93 /* The number of the invariant with the same value. */
96 /* If we moved the invariant out of the loop, the register that contains its
100 /* The definition of the invariant. */
103 /* The insn in that it is defined. */
106 /* Whether it is always executed. */
107 bool always_executed
;
109 /* Whether to move the invariant. */
112 /* Cost of the invariant. */
115 /* The invariants it depends on. */
118 /* Used for detecting already visited invariants during determining
119 costs of movements. */
123 /* Entry for hash table of invariant expressions. */
125 struct invariant_expr_entry
128 struct invariant
*inv
;
134 enum machine_mode mode
;
140 /* The actual stamp for marking already visited invariants during determining
141 costs of movements. */
143 static unsigned actual_stamp
;
145 typedef struct invariant
*invariant_p
;
147 DEF_VEC_P(invariant_p
);
148 DEF_VEC_ALLOC_P(invariant_p
, heap
);
150 /* The invariants. */
152 static VEC(invariant_p
,heap
) *invariants
;
154 /* The dataflow object. */
156 static struct df
*df
;
158 /* Test for possibility of invariantness of X. */
161 check_maybe_invariant (rtx x
)
163 enum rtx_code code
= GET_CODE (x
);
178 case UNSPEC_VOLATILE
:
186 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
187 It should not be hard, and might be faster than "elsewhere". */
189 /* Just handle the most trivial case where we load from an unchanging
190 location (most importantly, pic tables). */
191 if (MEM_READONLY_P (x
))
197 /* Don't mess with insns declared volatile. */
198 if (MEM_VOLATILE_P (x
))
206 fmt
= GET_RTX_FORMAT (code
);
207 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
211 if (!check_maybe_invariant (XEXP (x
, i
)))
214 else if (fmt
[i
] == 'E')
216 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
217 if (!check_maybe_invariant (XVECEXP (x
, i
, j
)))
225 /* Returns the invariant definition for USE, or NULL if USE is not
228 static struct invariant
*
229 invariant_for_use (struct ref
*use
)
231 struct df_link
*defs
;
233 basic_block bb
= BLOCK_FOR_INSN (use
->insn
), def_bb
;
235 defs
= DF_REF_CHAIN (use
);
236 if (!defs
|| defs
->next
)
239 if (!DF_REF_DATA (def
))
242 def_bb
= DF_REF_BB (def
);
243 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
245 return DF_REF_DATA (def
);
248 /* Computes hash value for invariant expression X in INSN. */
251 hash_invariant_expr_1 (rtx insn
, rtx x
)
253 enum rtx_code code
= GET_CODE (x
);
256 hashval_t val
= code
;
259 struct invariant
*inv
;
268 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
271 use
= df_find_use (df
, insn
, x
);
273 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
274 inv
= invariant_for_use (use
);
276 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
278 gcc_assert (inv
->eqto
!= ~0u);
285 fmt
= GET_RTX_FORMAT (code
);
286 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
289 val
^= hash_invariant_expr_1 (insn
, XEXP (x
, i
));
290 else if (fmt
[i
] == 'E')
292 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
293 val
^= hash_invariant_expr_1 (insn
, XVECEXP (x
, i
, j
));
300 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
301 and INSN2 have always the same value. */
304 invariant_expr_equal_p (rtx insn1
, rtx e1
, rtx insn2
, rtx e2
)
306 enum rtx_code code
= GET_CODE (e1
);
309 struct ref
*use1
, *use2
;
310 struct invariant
*inv1
= NULL
, *inv2
= NULL
;
313 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
314 the other one. If both are VOIDmode, we rely on the caller of this
315 function to verify that their modes are the same. */
316 if (code
!= GET_CODE (e2
) || GET_MODE (e1
) != GET_MODE (e2
))
326 return rtx_equal_p (e1
, e2
);
329 use1
= df_find_use (df
, insn1
, e1
);
330 use2
= df_find_use (df
, insn2
, e2
);
332 inv1
= invariant_for_use (use1
);
334 inv2
= invariant_for_use (use2
);
337 return rtx_equal_p (e1
, e2
);
342 gcc_assert (inv1
->eqto
!= ~0u);
343 gcc_assert (inv2
->eqto
!= ~0u);
344 return inv1
->eqto
== inv2
->eqto
;
350 fmt
= GET_RTX_FORMAT (code
);
351 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
358 if (!invariant_expr_equal_p (insn1
, sub1
, insn2
, sub2
))
362 else if (fmt
[i
] == 'E')
364 if (XVECLEN (e1
, i
) != XVECLEN (e2
, i
))
367 for (j
= 0; j
< XVECLEN (e1
, i
); j
++)
369 sub1
= XVECEXP (e1
, i
, j
);
370 sub2
= XVECEXP (e2
, i
, j
);
372 if (!invariant_expr_equal_p (insn1
, sub1
, insn2
, sub2
))
381 /* Returns hash value for invariant expression entry E. */
384 hash_invariant_expr (const void *e
)
386 const struct invariant_expr_entry
*entry
= e
;
391 /* Compares invariant expression entries E1 and E2. */
394 eq_invariant_expr (const void *e1
, const void *e2
)
396 const struct invariant_expr_entry
*entry1
= e1
;
397 const struct invariant_expr_entry
*entry2
= e2
;
399 if (entry1
->mode
!= entry2
->mode
)
402 return invariant_expr_equal_p (entry1
->inv
->insn
, entry1
->expr
,
403 entry2
->inv
->insn
, entry2
->expr
);
406 /* Checks whether invariant with value EXPR in machine mode MODE is
407 recorded in EQ. If this is the case, return the invariant. Otherwise
408 insert INV to the table for this expression and return INV. */
410 static struct invariant
*
411 find_or_insert_inv (htab_t eq
, rtx expr
, enum machine_mode mode
,
412 struct invariant
*inv
)
414 hashval_t hash
= hash_invariant_expr_1 (inv
->insn
, expr
);
415 struct invariant_expr_entry
*entry
;
416 struct invariant_expr_entry pentry
;
422 slot
= htab_find_slot_with_hash (eq
, &pentry
, hash
, INSERT
);
428 entry
= xmalloc (sizeof (struct invariant_expr_entry
));
438 /* Finds invariants identical to INV and records the equivalence. EQ is the
439 hash table of the invariants. */
442 find_identical_invariants (htab_t eq
, struct invariant
*inv
)
446 struct invariant
*dep
;
448 enum machine_mode mode
;
450 if (inv
->eqto
!= ~0u)
453 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
455 dep
= VEC_index (invariant_p
, invariants
, depno
);
456 find_identical_invariants (eq
, dep
);
459 set
= single_set (inv
->insn
);
460 expr
= SET_SRC (set
);
461 mode
= GET_MODE (expr
);
462 if (mode
== VOIDmode
)
463 mode
= GET_MODE (SET_DEST (set
));
464 inv
->eqto
= find_or_insert_inv (eq
, expr
, mode
, inv
)->invno
;
466 if (dump_file
&& inv
->eqto
!= inv
->invno
)
468 "Invariant %d is equivalent to invariant %d.\n ",
469 inv
->invno
, inv
->eqto
);
472 /* Find invariants with the same value and record the equivalences. */
475 merge_identical_invariants (void)
478 struct invariant
*inv
;
479 htab_t eq
= htab_create (VEC_length (invariant_p
, invariants
),
480 hash_invariant_expr
, eq_invariant_expr
, free
);
482 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
483 find_identical_invariants (eq
, inv
);
488 /* Determines the basic blocks inside LOOP that are always executed and
489 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
490 basic blocks that may either exit the loop, or contain the call that
491 does not have to return. BODY is body of the loop obtained by
492 get_loop_body_in_dom_order. */
495 compute_always_reached (struct loop
*loop
, basic_block
*body
,
496 bitmap may_exit
, bitmap always_reached
)
500 for (i
= 0; i
< loop
->num_nodes
; i
++)
502 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, body
[i
]))
503 bitmap_set_bit (always_reached
, i
);
505 if (bitmap_bit_p (may_exit
, i
))
510 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
511 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
512 additionally mark blocks that may exit due to a call. */
515 find_exits (struct loop
*loop
, basic_block
*body
,
516 bitmap may_exit
, bitmap has_exit
)
521 struct loop
*outermost_exit
= loop
, *aexit
;
522 bool has_call
= false;
525 for (i
= 0; i
< loop
->num_nodes
; i
++)
527 if (body
[i
]->loop_father
== loop
)
529 FOR_BB_INSNS (body
[i
], insn
)
532 && !CONST_OR_PURE_CALL_P (insn
))
535 bitmap_set_bit (may_exit
, i
);
540 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
542 if (flow_bb_inside_loop_p (loop
, e
->dest
))
545 bitmap_set_bit (may_exit
, i
);
546 bitmap_set_bit (has_exit
, i
);
547 outermost_exit
= find_common_loop (outermost_exit
,
548 e
->dest
->loop_father
);
553 /* Use the data stored for the subloop to decide whether we may exit
554 through it. It is sufficient to do this for header of the loop,
555 as other basic blocks inside it must be dominated by it. */
556 if (body
[i
]->loop_father
->header
!= body
[i
])
559 if (LOOP_DATA (body
[i
]->loop_father
)->has_call
)
562 bitmap_set_bit (may_exit
, i
);
564 aexit
= LOOP_DATA (body
[i
]->loop_father
)->outermost_exit
;
567 bitmap_set_bit (may_exit
, i
);
568 bitmap_set_bit (has_exit
, i
);
570 if (flow_loop_nested_p (aexit
, outermost_exit
))
571 outermost_exit
= aexit
;
575 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
576 LOOP_DATA (loop
)->outermost_exit
= outermost_exit
;
577 LOOP_DATA (loop
)->has_call
= has_call
;
580 /* Check whether we may assign a value to X from a register. */
583 may_assign_reg_p (rtx x
)
585 return (can_copy_p (GET_MODE (x
))
587 || !HARD_REGISTER_P (x
)
588 || REGNO_REG_CLASS (REGNO (x
)) != NO_REGS
));
591 /* Finds definitions that may correspond to invariants in LOOP with body
595 find_defs (struct loop
*loop
, basic_block
*body
)
598 bitmap blocks
= BITMAP_ALLOC (NULL
);
600 for (i
= 0; i
< loop
->num_nodes
; i
++)
601 bitmap_set_bit (blocks
, body
[i
]->index
);
603 df_analyze_subcfg (df
, blocks
, DF_UD_CHAIN
| DF_HARD_REGS
| DF_EQUIV_NOTES
);
604 BITMAP_FREE (blocks
);
607 /* Creates a new invariant for definition DEF in INSN, depending on invariants
608 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
609 unless the program ends due to a function call. The newly created invariant
612 static struct invariant
*
613 create_new_invariant (struct def
*def
, rtx insn
, bitmap depends_on
,
614 bool always_executed
)
616 struct invariant
*inv
= xmalloc (sizeof (struct invariant
));
617 rtx set
= single_set (insn
);
620 inv
->always_executed
= always_executed
;
621 inv
->depends_on
= depends_on
;
623 /* If the set is simple, usually by moving it we move the whole store out of
624 the loop. Otherwise we save only cost of the computation. */
626 inv
->cost
= rtx_cost (set
, SET
);
628 inv
->cost
= rtx_cost (SET_SRC (set
), SET
);
635 inv
->invno
= VEC_length (invariant_p
, invariants
);
638 def
->invno
= inv
->invno
;
639 VEC_safe_push (invariant_p
, heap
, invariants
, inv
);
644 "Set in insn %d is invariant (%d), cost %d, depends on ",
645 INSN_UID (insn
), inv
->invno
, inv
->cost
);
646 dump_bitmap (dump_file
, inv
->depends_on
);
652 /* Record USE at DEF. */
655 record_use (struct def
*def
, rtx
*use
, rtx insn
)
657 struct use
*u
= xmalloc (sizeof (struct use
));
659 if (GET_CODE (*use
) == SUBREG
)
660 use
= &SUBREG_REG (*use
);
661 gcc_assert (REG_P (*use
));
670 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
674 check_dependencies (rtx insn
, bitmap depends_on
)
676 struct df_link
*uses
, *defs
;
677 struct ref
*use
, *def
;
678 basic_block bb
= BLOCK_FOR_INSN (insn
), def_bb
;
679 struct def
*def_data
;
680 struct invariant
*inv
;
682 for (uses
= DF_INSN_USES (df
, insn
); uses
; uses
= uses
->next
)
686 defs
= DF_REF_CHAIN (use
);
694 inv
= DF_REF_DATA (def
);
699 gcc_assert (def_data
!= NULL
);
701 def_bb
= DF_REF_BB (def
);
702 /* Note that in case bb == def_bb, we know that the definition dominates
703 insn, because def has DF_REF_DATA defined and we process the insns
704 in the basic block bb sequentially. */
705 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
708 bitmap_set_bit (depends_on
, def_data
->invno
);
714 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
715 executed. ALWAYS_EXECUTED is true if the insn is always executed,
716 unless the program ends due to a function call. */
719 find_invariant_insn (rtx insn
, bool always_reached
, bool always_executed
)
726 struct invariant
*inv
;
728 /* Until we get rid of LIBCALLS. */
729 if (find_reg_note (insn
, REG_RETVAL
, NULL_RTX
)
730 || find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)
731 || find_reg_note (insn
, REG_NO_CONFLICT
, NULL_RTX
))
734 set
= single_set (insn
);
737 dest
= SET_DEST (set
);
740 || HARD_REGISTER_P (dest
))
743 if (!may_assign_reg_p (SET_DEST (set
))
744 || !check_maybe_invariant (SET_SRC (set
)))
747 if (may_trap_p (PATTERN (insn
)))
752 /* Unless the exceptions are handled, the behavior is undefined
753 if the trap occurs. */
754 if (flag_non_call_exceptions
)
758 depends_on
= BITMAP_ALLOC (NULL
);
759 if (!check_dependencies (insn
, depends_on
))
761 BITMAP_FREE (depends_on
);
766 def
= xcalloc (1, sizeof (struct def
));
770 inv
= create_new_invariant (def
, insn
, depends_on
, always_executed
);
774 ref
= df_find_def (df
, insn
, dest
);
775 DF_REF_DATA (ref
) = inv
;
779 /* Record registers used in INSN that have a unique invariant definition. */
782 record_uses (rtx insn
)
784 struct df_link
*uses
;
786 struct invariant
*inv
;
788 for (uses
= DF_INSN_USES (df
, insn
); uses
; uses
= uses
->next
)
791 inv
= invariant_for_use (use
);
793 record_use (inv
->def
, DF_REF_LOC (use
), DF_REF_INSN (use
));
797 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
798 executed. ALWAYS_EXECUTED is true if the insn is always executed,
799 unless the program ends due to a function call. */
802 find_invariants_insn (rtx insn
, bool always_reached
, bool always_executed
)
804 find_invariant_insn (insn
, always_reached
, always_executed
);
808 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
809 basic block is always executed. ALWAYS_EXECUTED is true if the basic
810 block is always executed, unless the program ends due to a function
814 find_invariants_bb (basic_block bb
, bool always_reached
, bool always_executed
)
818 FOR_BB_INSNS (bb
, insn
)
823 find_invariants_insn (insn
, always_reached
, always_executed
);
827 && !CONST_OR_PURE_CALL_P (insn
))
828 always_reached
= false;
832 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
833 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
834 bitmap of basic blocks in BODY that are always executed unless the program
835 ends due to a function call. */
838 find_invariants_body (struct loop
*loop
, basic_block
*body
,
839 bitmap always_reached
, bitmap always_executed
)
843 for (i
= 0; i
< loop
->num_nodes
; i
++)
844 find_invariants_bb (body
[i
],
845 bitmap_bit_p (always_reached
, i
),
846 bitmap_bit_p (always_executed
, i
));
849 /* Finds invariants in LOOP. */
852 find_invariants (struct loop
*loop
)
854 bitmap may_exit
= BITMAP_ALLOC (NULL
);
855 bitmap always_reached
= BITMAP_ALLOC (NULL
);
856 bitmap has_exit
= BITMAP_ALLOC (NULL
);
857 bitmap always_executed
= BITMAP_ALLOC (NULL
);
858 basic_block
*body
= get_loop_body_in_dom_order (loop
);
860 find_exits (loop
, body
, may_exit
, has_exit
);
861 compute_always_reached (loop
, body
, may_exit
, always_reached
);
862 compute_always_reached (loop
, body
, has_exit
, always_executed
);
864 find_defs (loop
, body
);
865 find_invariants_body (loop
, body
, always_reached
, always_executed
);
866 merge_identical_invariants ();
868 BITMAP_FREE (always_reached
);
869 BITMAP_FREE (always_executed
);
870 BITMAP_FREE (may_exit
);
871 BITMAP_FREE (has_exit
);
875 /* Frees a list of uses USE. */
878 free_use_list (struct use
*use
)
882 for (; use
; use
= next
)
889 /* Calculates cost and number of registers needed for moving invariant INV
890 out of the loop and stores them to *COST and *REGS_NEEDED. */
893 get_inv_cost (struct invariant
*inv
, int *comp_cost
, unsigned *regs_needed
)
896 unsigned aregs_needed
;
898 struct invariant
*dep
;
901 /* Find the representative of the class of the equivalent invariants. */
902 inv
= VEC_index (invariant_p
, invariants
, inv
->eqto
);
907 || inv
->stamp
== actual_stamp
)
909 inv
->stamp
= actual_stamp
;
912 (*comp_cost
) += inv
->cost
;
914 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
916 dep
= VEC_index (invariant_p
, invariants
, depno
);
918 get_inv_cost (dep
, &acomp_cost
, &aregs_needed
);
921 /* We need to check always_executed, since if the original value of
922 the invariant may be preserved, we may need to keep it in a
923 separate register. TODO check whether the register has an
924 use outside of the loop. */
925 && dep
->always_executed
926 && !dep
->def
->uses
->next
)
928 /* If this is a single use, after moving the dependency we will not
929 need a new register. */
933 (*regs_needed
) += aregs_needed
;
934 (*comp_cost
) += acomp_cost
;
938 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
939 of registers used in the loop, N_INV_USES is the number of uses of
940 invariants, NEW_REGS is the number of new variables already added due to
941 the invariant motion. The number of registers needed for it is stored in
945 gain_for_invariant (struct invariant
*inv
, unsigned *regs_needed
,
946 unsigned new_regs
, unsigned regs_used
, unsigned n_inv_uses
)
948 int comp_cost
, size_cost
;
950 get_inv_cost (inv
, &comp_cost
, regs_needed
);
953 size_cost
= (global_cost_for_size (new_regs
+ *regs_needed
,
954 regs_used
, n_inv_uses
)
955 - global_cost_for_size (new_regs
, regs_used
, n_inv_uses
));
957 return comp_cost
- size_cost
;
960 /* Finds invariant with best gain for moving. Returns the gain, stores
961 the invariant in *BEST and number of registers needed for it to
962 *REGS_NEEDED. REGS_USED is the number of registers used in
963 the loop, N_INV_USES is the number of uses of invariants. NEW_REGS
964 is the number of new variables already added due to invariant motion. */
967 best_gain_for_invariant (struct invariant
**best
, unsigned *regs_needed
,
968 unsigned new_regs
, unsigned regs_used
,
971 struct invariant
*inv
;
973 unsigned aregs_needed
, invno
;
975 for (invno
= 0; VEC_iterate (invariant_p
, invariants
, invno
, inv
); invno
++)
980 /* Only consider the "representatives" of equivalent invariants. */
981 if (inv
->eqto
!= inv
->invno
)
984 again
= gain_for_invariant (inv
, &aregs_needed
,
985 new_regs
, regs_used
, n_inv_uses
);
990 *regs_needed
= aregs_needed
;
997 /* Marks invariant INVNO and all its dependencies for moving. */
1000 set_move_mark (unsigned invno
)
1002 struct invariant
*inv
= VEC_index (invariant_p
, invariants
, invno
);
1005 /* Find the representative of the class of the equivalent invariants. */
1006 inv
= VEC_index (invariant_p
, invariants
, inv
->eqto
);
1013 fprintf (dump_file
, "Decided to move invariant %d\n", invno
);
1015 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, invno
, bi
)
1017 set_move_mark (invno
);
1021 /* Determines which invariants to move. */
1024 find_invariants_to_move (void)
1026 unsigned i
, regs_used
, n_inv_uses
, regs_needed
= 0, new_regs
;
1027 struct invariant
*inv
= NULL
;
1029 if (!VEC_length (invariant_p
, invariants
))
1032 /* Now something slightly more involved. First estimate the number of used
1036 /* We do not really do a good job in this estimation; put some initial bound
1037 here to stand for induction variables etc. that we do not detect. */
1040 for (i
= 0; i
< df
->n_regs
; i
++)
1042 if (!DF_REGNO_FIRST_DEF (df
, i
) && DF_REGNO_LAST_USE (df
, i
))
1044 /* This is a value that is used but not changed inside loop. */
1049 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
1052 n_inv_uses
+= inv
->def
->n_uses
;
1056 while (best_gain_for_invariant (&inv
, ®s_needed
,
1057 new_regs
, regs_used
, n_inv_uses
) > 0)
1059 set_move_mark (inv
->invno
);
1060 new_regs
+= regs_needed
;
1064 /* Move invariant INVNO out of the LOOP. */
1067 move_invariant_reg (struct loop
*loop
, unsigned invno
)
1069 struct invariant
*inv
= VEC_index (invariant_p
, invariants
, invno
);
1070 struct invariant
*repr
= VEC_index (invariant_p
, invariants
, inv
->eqto
);
1072 basic_block preheader
= loop_preheader_edge (loop
)->src
;
1081 /* If this is a representative of the class of equivalent invariants,
1082 really move the invariant. Otherwise just replace its use with
1083 the register used for the representative. */
1086 if (inv
->depends_on
)
1088 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, i
, bi
)
1090 move_invariant_reg (loop
, i
);
1094 /* Move the set out of the loop. If the set is always executed (we could
1095 omit this condition if we know that the register is unused outside of the
1096 loop, but it does not seem worth finding out) and it has no uses that
1097 would not be dominated by it, we may just move it (TODO). Otherwise we
1098 need to create a temporary register. */
1099 set
= single_set (inv
->insn
);
1100 reg
= gen_reg_rtx (GET_MODE (SET_DEST (set
)));
1101 df_pattern_emit_after (df
, gen_move_insn (SET_DEST (set
), reg
),
1102 BLOCK_FOR_INSN (inv
->insn
), inv
->insn
);
1104 /* If the SET_DEST of the invariant insn is a reg, we can just move
1105 the insn out of the loop. Otherwise, we have to use gen_move_insn
1106 to let emit_move_insn produce a valid instruction stream. */
1107 if (REG_P (SET_DEST (set
)))
1109 SET_DEST (set
) = reg
;
1110 reorder_insns (inv
->insn
, inv
->insn
, BB_END (preheader
));
1111 df_insn_modify (df
, preheader
, inv
->insn
);
1115 df_pattern_emit_after (df
, gen_move_insn (reg
, SET_SRC (set
)),
1116 preheader
, BB_END (preheader
));
1117 df_insn_delete (df
, BLOCK_FOR_INSN (inv
->insn
), inv
->insn
);
1122 move_invariant_reg (loop
, repr
->invno
);
1124 set
= single_set (inv
->insn
);
1125 df_pattern_emit_after (df
, gen_move_insn (SET_DEST (set
), reg
),
1126 BLOCK_FOR_INSN (inv
->insn
), inv
->insn
);
1127 df_insn_delete (df
, BLOCK_FOR_INSN (inv
->insn
), inv
->insn
);
1132 /* Replace the uses we know to be dominated. It saves work for copy
1133 propagation, and also it is necessary so that dependent invariants
1134 are computed right. */
1137 for (use
= inv
->def
->uses
; use
; use
= use
->next
)
1140 df_insn_modify (df
, BLOCK_FOR_INSN (use
->insn
), use
->insn
);
1145 /* Move selected invariant out of the LOOP. Newly created regs are marked
1146 in TEMPORARY_REGS. */
1149 move_invariants (struct loop
*loop
)
1151 struct invariant
*inv
;
1154 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
1155 move_invariant_reg (loop
, i
);
1158 /* Initializes invariant motion data. */
1161 init_inv_motion_data (void)
1165 invariants
= VEC_alloc (invariant_p
, heap
, 100);
1168 /* Frees the data allocated by invariant motion. */
1171 free_inv_motion_data (void)
1175 struct invariant
*inv
;
1177 for (i
= 0; i
< df
->n_defs
; i
++)
1182 inv
= DF_REF_DATA (df
->defs
[i
]);
1186 gcc_assert (def
!= NULL
);
1188 free_use_list (def
->uses
);
1190 DF_REF_DATA (df
->defs
[i
]) = NULL
;
1193 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
1195 BITMAP_FREE (inv
->depends_on
);
1198 VEC_free (invariant_p
, heap
, invariants
);
1201 /* Move the invariants out of the LOOP. */
1204 move_single_loop_invariants (struct loop
*loop
)
1206 init_inv_motion_data ();
1208 find_invariants (loop
);
1209 find_invariants_to_move ();
1210 move_invariants (loop
);
1212 free_inv_motion_data ();
1215 /* Releases the auxiliary data for LOOP. */
1218 free_loop_data (struct loop
*loop
)
1220 struct loop_data
*data
= LOOP_DATA (loop
);
1226 /* Move the invariants out of the LOOPS. */
1229 move_loop_invariants (struct loops
*loops
)
1236 /* Process the loops, innermost first. */
1237 loop
= loops
->tree_root
;
1241 while (loop
!= loops
->tree_root
)
1243 move_single_loop_invariants (loop
);
1255 for (i
= 1; i
< loops
->num
; i
++)
1256 if (loops
->parray
[i
])
1257 free_loop_data (loops
->parray
[i
]);
1262 #ifdef ENABLE_CHECKING
1263 verify_flow_info ();