1 /* RTL-level loop invariant motion.
2 Copyright (C) 2004, 2005, 2006, 2007 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, libcalls, etc.). This should be sufficient to cleanup things
22 like address arithmetics -- other more complicated invariants should be
23 eliminated on tree level 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"
43 #include "hard-reg-set.h"
45 #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 /* Table of invariants indexed by the df_ref uid field. */
125 static unsigned int invariant_table_size
= 0;
126 static struct invariant
** invariant_table
;
128 /* Entry for hash table of invariant expressions. */
130 struct invariant_expr_entry
133 struct invariant
*inv
;
139 enum machine_mode mode
;
145 /* The actual stamp for marking already visited invariants during determining
146 costs of movements. */
148 static unsigned actual_stamp
;
150 typedef struct invariant
*invariant_p
;
152 DEF_VEC_P(invariant_p
);
153 DEF_VEC_ALLOC_P(invariant_p
, heap
);
155 /* The invariants. */
157 static VEC(invariant_p
,heap
) *invariants
;
159 /* Check the size of the invariant table and realloc if necessary. */
162 check_invariant_table_size (void)
164 if (invariant_table_size
< DF_DEFS_TABLE_SIZE())
166 unsigned int new_size
= DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
167 invariant_table
= xrealloc (invariant_table
,
168 sizeof (struct rtx_iv
*) * new_size
);
169 memset (&invariant_table
[invariant_table_size
], 0,
170 (new_size
- invariant_table_size
) * sizeof (struct rtx_iv
*));
171 invariant_table_size
= new_size
;
175 /* Test for possibility of invariantness of X. */
178 check_maybe_invariant (rtx x
)
180 enum rtx_code code
= GET_CODE (x
);
196 case UNSPEC_VOLATILE
:
204 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
205 It should not be hard, and might be faster than "elsewhere". */
207 /* Just handle the most trivial case where we load from an unchanging
208 location (most importantly, pic tables). */
209 if (MEM_READONLY_P (x
))
215 /* Don't mess with insns declared volatile. */
216 if (MEM_VOLATILE_P (x
))
224 fmt
= GET_RTX_FORMAT (code
);
225 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
229 if (!check_maybe_invariant (XEXP (x
, i
)))
232 else if (fmt
[i
] == 'E')
234 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
235 if (!check_maybe_invariant (XVECEXP (x
, i
, j
)))
243 /* Returns the invariant definition for USE, or NULL if USE is not
246 static struct invariant
*
247 invariant_for_use (struct df_ref
*use
)
249 struct df_link
*defs
;
251 basic_block bb
= BLOCK_FOR_INSN (use
->insn
), def_bb
;
253 if (use
->flags
& DF_REF_READ_WRITE
)
256 defs
= DF_REF_CHAIN (use
);
257 if (!defs
|| defs
->next
)
260 check_invariant_table_size ();
261 if (!invariant_table
[DF_REF_ID(def
)])
264 def_bb
= DF_REF_BB (def
);
265 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
267 return invariant_table
[DF_REF_ID(def
)];
270 /* Computes hash value for invariant expression X in INSN. */
273 hash_invariant_expr_1 (rtx insn
, rtx x
)
275 enum rtx_code code
= GET_CODE (x
);
278 hashval_t val
= code
;
281 struct invariant
*inv
;
291 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
294 use
= df_find_use (insn
, x
);
296 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
297 inv
= invariant_for_use (use
);
299 return hash_rtx (x
, GET_MODE (x
), &do_not_record_p
, NULL
, false);
301 gcc_assert (inv
->eqto
!= ~0u);
308 fmt
= GET_RTX_FORMAT (code
);
309 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
312 val
^= hash_invariant_expr_1 (insn
, XEXP (x
, i
));
313 else if (fmt
[i
] == 'E')
315 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
316 val
^= hash_invariant_expr_1 (insn
, XVECEXP (x
, i
, j
));
318 else if (fmt
[i
] == 'i' || fmt
[i
] == 'n')
325 /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
326 and INSN2 have always the same value. */
329 invariant_expr_equal_p (rtx insn1
, rtx e1
, rtx insn2
, rtx e2
)
331 enum rtx_code code
= GET_CODE (e1
);
334 struct df_ref
*use1
, *use2
;
335 struct invariant
*inv1
= NULL
, *inv2
= NULL
;
338 /* If mode of only one of the operands is VOIDmode, it is not equivalent to
339 the other one. If both are VOIDmode, we rely on the caller of this
340 function to verify that their modes are the same. */
341 if (code
!= GET_CODE (e2
) || GET_MODE (e1
) != GET_MODE (e2
))
352 return rtx_equal_p (e1
, e2
);
355 use1
= df_find_use (insn1
, e1
);
356 use2
= df_find_use (insn2
, e2
);
358 inv1
= invariant_for_use (use1
);
360 inv2
= invariant_for_use (use2
);
363 return rtx_equal_p (e1
, e2
);
368 gcc_assert (inv1
->eqto
!= ~0u);
369 gcc_assert (inv2
->eqto
!= ~0u);
370 return inv1
->eqto
== inv2
->eqto
;
376 fmt
= GET_RTX_FORMAT (code
);
377 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
384 if (!invariant_expr_equal_p (insn1
, sub1
, insn2
, sub2
))
388 else if (fmt
[i
] == 'E')
390 if (XVECLEN (e1
, i
) != XVECLEN (e2
, i
))
393 for (j
= 0; j
< XVECLEN (e1
, i
); j
++)
395 sub1
= XVECEXP (e1
, i
, j
);
396 sub2
= XVECEXP (e2
, i
, j
);
398 if (!invariant_expr_equal_p (insn1
, sub1
, insn2
, sub2
))
402 else if (fmt
[i
] == 'i' || fmt
[i
] == 'n')
404 if (XINT (e1
, i
) != XINT (e2
, i
))
407 /* Unhandled type of subexpression, we fail conservatively. */
415 /* Returns hash value for invariant expression entry E. */
418 hash_invariant_expr (const void *e
)
420 const struct invariant_expr_entry
*entry
= e
;
425 /* Compares invariant expression entries E1 and E2. */
428 eq_invariant_expr (const void *e1
, const void *e2
)
430 const struct invariant_expr_entry
*entry1
= e1
;
431 const struct invariant_expr_entry
*entry2
= e2
;
433 if (entry1
->mode
!= entry2
->mode
)
436 return invariant_expr_equal_p (entry1
->inv
->insn
, entry1
->expr
,
437 entry2
->inv
->insn
, entry2
->expr
);
440 /* Checks whether invariant with value EXPR in machine mode MODE is
441 recorded in EQ. If this is the case, return the invariant. Otherwise
442 insert INV to the table for this expression and return INV. */
444 static struct invariant
*
445 find_or_insert_inv (htab_t eq
, rtx expr
, enum machine_mode mode
,
446 struct invariant
*inv
)
448 hashval_t hash
= hash_invariant_expr_1 (inv
->insn
, expr
);
449 struct invariant_expr_entry
*entry
;
450 struct invariant_expr_entry pentry
;
456 slot
= htab_find_slot_with_hash (eq
, &pentry
, hash
, INSERT
);
462 entry
= XNEW (struct invariant_expr_entry
);
472 /* Finds invariants identical to INV and records the equivalence. EQ is the
473 hash table of the invariants. */
476 find_identical_invariants (htab_t eq
, struct invariant
*inv
)
480 struct invariant
*dep
;
482 enum machine_mode mode
;
484 if (inv
->eqto
!= ~0u)
487 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
489 dep
= VEC_index (invariant_p
, invariants
, depno
);
490 find_identical_invariants (eq
, dep
);
493 set
= single_set (inv
->insn
);
494 expr
= SET_SRC (set
);
495 mode
= GET_MODE (expr
);
496 if (mode
== VOIDmode
)
497 mode
= GET_MODE (SET_DEST (set
));
498 inv
->eqto
= find_or_insert_inv (eq
, expr
, mode
, inv
)->invno
;
500 if (dump_file
&& inv
->eqto
!= inv
->invno
)
502 "Invariant %d is equivalent to invariant %d.\n",
503 inv
->invno
, inv
->eqto
);
506 /* Find invariants with the same value and record the equivalences. */
509 merge_identical_invariants (void)
512 struct invariant
*inv
;
513 htab_t eq
= htab_create (VEC_length (invariant_p
, invariants
),
514 hash_invariant_expr
, eq_invariant_expr
, free
);
516 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
517 find_identical_invariants (eq
, inv
);
522 /* Determines the basic blocks inside LOOP that are always executed and
523 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
524 basic blocks that may either exit the loop, or contain the call that
525 does not have to return. BODY is body of the loop obtained by
526 get_loop_body_in_dom_order. */
529 compute_always_reached (struct loop
*loop
, basic_block
*body
,
530 bitmap may_exit
, bitmap always_reached
)
534 for (i
= 0; i
< loop
->num_nodes
; i
++)
536 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, body
[i
]))
537 bitmap_set_bit (always_reached
, i
);
539 if (bitmap_bit_p (may_exit
, i
))
544 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
545 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
546 additionally mark blocks that may exit due to a call. */
549 find_exits (struct loop
*loop
, basic_block
*body
,
550 bitmap may_exit
, bitmap has_exit
)
555 struct loop
*outermost_exit
= loop
, *aexit
;
556 bool has_call
= false;
559 for (i
= 0; i
< loop
->num_nodes
; i
++)
561 if (body
[i
]->loop_father
== loop
)
563 FOR_BB_INSNS (body
[i
], insn
)
566 && !CONST_OR_PURE_CALL_P (insn
))
569 bitmap_set_bit (may_exit
, i
);
574 FOR_EACH_EDGE (e
, ei
, body
[i
]->succs
)
576 if (flow_bb_inside_loop_p (loop
, e
->dest
))
579 bitmap_set_bit (may_exit
, i
);
580 bitmap_set_bit (has_exit
, i
);
581 outermost_exit
= find_common_loop (outermost_exit
,
582 e
->dest
->loop_father
);
587 /* Use the data stored for the subloop to decide whether we may exit
588 through it. It is sufficient to do this for header of the loop,
589 as other basic blocks inside it must be dominated by it. */
590 if (body
[i
]->loop_father
->header
!= body
[i
])
593 if (LOOP_DATA (body
[i
]->loop_father
)->has_call
)
596 bitmap_set_bit (may_exit
, i
);
598 aexit
= LOOP_DATA (body
[i
]->loop_father
)->outermost_exit
;
601 bitmap_set_bit (may_exit
, i
);
602 bitmap_set_bit (has_exit
, i
);
604 if (flow_loop_nested_p (aexit
, outermost_exit
))
605 outermost_exit
= aexit
;
609 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
610 LOOP_DATA (loop
)->outermost_exit
= outermost_exit
;
611 LOOP_DATA (loop
)->has_call
= has_call
;
614 /* Check whether we may assign a value to X from a register. */
617 may_assign_reg_p (rtx x
)
619 return (GET_MODE (x
) != VOIDmode
620 && GET_MODE (x
) != BLKmode
621 && can_copy_p (GET_MODE (x
))
623 || !HARD_REGISTER_P (x
)
624 || REGNO_REG_CLASS (REGNO (x
)) != NO_REGS
));
627 /* Finds definitions that may correspond to invariants in LOOP with body
631 find_defs (struct loop
*loop
, basic_block
*body
)
634 bitmap blocks
= BITMAP_ALLOC (NULL
);
636 for (i
= 0; i
< loop
->num_nodes
; i
++)
637 bitmap_set_bit (blocks
, body
[i
]->index
);
639 df_remove_problem (df_chain
);
640 df_process_deferred_rescans ();
641 df_chain_add_problem (DF_UD_CHAIN
);
642 df_set_blocks (blocks
);
647 df_dump_region (dump_file
);
648 fprintf (dump_file
, "*****starting processing of loop ******\n");
649 print_rtl_with_bb (dump_file
, get_insns ());
650 fprintf (dump_file
, "*****ending processing of loop ******\n");
652 check_invariant_table_size ();
654 BITMAP_FREE (blocks
);
657 /* Creates a new invariant for definition DEF in INSN, depending on invariants
658 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
659 unless the program ends due to a function call. The newly created invariant
662 static struct invariant
*
663 create_new_invariant (struct def
*def
, rtx insn
, bitmap depends_on
,
664 bool always_executed
)
666 struct invariant
*inv
= XNEW (struct invariant
);
667 rtx set
= single_set (insn
);
670 inv
->always_executed
= always_executed
;
671 inv
->depends_on
= depends_on
;
673 /* If the set is simple, usually by moving it we move the whole store out of
674 the loop. Otherwise we save only cost of the computation. */
676 inv
->cost
= rtx_cost (set
, SET
);
678 inv
->cost
= rtx_cost (SET_SRC (set
), SET
);
685 inv
->invno
= VEC_length (invariant_p
, invariants
);
688 def
->invno
= inv
->invno
;
689 VEC_safe_push (invariant_p
, heap
, invariants
, inv
);
694 "Set in insn %d is invariant (%d), cost %d, depends on ",
695 INSN_UID (insn
), inv
->invno
, inv
->cost
);
696 dump_bitmap (dump_file
, inv
->depends_on
);
702 /* Record USE at DEF. */
705 record_use (struct def
*def
, rtx
*use
, rtx insn
)
707 struct use
*u
= XNEW (struct use
);
709 gcc_assert (REG_P (*use
));
718 /* Finds the invariants USE depends on and store them to the DEPENDS_ON
719 bitmap. Returns true if all dependencies of USE are known to be
720 loop invariants, false otherwise. */
723 check_dependency (basic_block bb
, struct df_ref
*use
, bitmap depends_on
)
727 struct df_link
*defs
;
728 struct def
*def_data
;
729 struct invariant
*inv
;
731 if (use
->flags
& DF_REF_READ_WRITE
)
734 defs
= DF_REF_CHAIN (use
);
742 check_invariant_table_size ();
743 inv
= invariant_table
[DF_REF_ID(def
)];
748 gcc_assert (def_data
!= NULL
);
750 def_bb
= DF_REF_BB (def
);
751 /* Note that in case bb == def_bb, we know that the definition
752 dominates insn, because def has invariant_table[DF_REF_ID(def)]
753 defined and we process the insns in the basic block bb
755 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
758 bitmap_set_bit (depends_on
, def_data
->invno
);
763 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
764 bitmap. Returns true if all dependencies of INSN are known to be
765 loop invariants, false otherwise. */
768 check_dependencies (rtx insn
, bitmap depends_on
)
770 struct df_ref
**use_rec
;
771 basic_block bb
= BLOCK_FOR_INSN (insn
);
773 for (use_rec
= DF_INSN_USES (insn
); *use_rec
; use_rec
++)
774 if (!check_dependency (bb
, *use_rec
, depends_on
))
776 for (use_rec
= DF_INSN_EQ_USES (insn
); *use_rec
; use_rec
++)
777 if (!check_dependency (bb
, *use_rec
, depends_on
))
783 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
784 executed. ALWAYS_EXECUTED is true if the insn is always executed,
785 unless the program ends due to a function call. */
788 find_invariant_insn (rtx insn
, bool always_reached
, bool always_executed
)
795 struct invariant
*inv
;
797 /* Until we get rid of LIBCALLS. */
798 if (find_reg_note (insn
, REG_RETVAL
, NULL_RTX
)
799 || find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)
800 || find_reg_note (insn
, REG_NO_CONFLICT
, NULL_RTX
))
804 /* We can't move a CC0 setter without the user. */
805 if (sets_cc0_p (insn
))
809 set
= single_set (insn
);
812 dest
= SET_DEST (set
);
815 || HARD_REGISTER_P (dest
))
818 if (!may_assign_reg_p (SET_DEST (set
))
819 || !check_maybe_invariant (SET_SRC (set
)))
822 /* If the insn can throw exception, we cannot move it at all without changing
824 if (can_throw_internal (insn
))
827 /* We cannot make trapping insn executed, unless it was executed before. */
828 if (may_trap_after_code_motion_p (PATTERN (insn
)) && !always_reached
)
831 depends_on
= BITMAP_ALLOC (NULL
);
832 if (!check_dependencies (insn
, depends_on
))
834 BITMAP_FREE (depends_on
);
839 def
= XCNEW (struct def
);
843 inv
= create_new_invariant (def
, insn
, depends_on
, always_executed
);
847 ref
= df_find_def (insn
, dest
);
848 check_invariant_table_size ();
849 invariant_table
[DF_REF_ID(ref
)] = inv
;
853 /* Record registers used in INSN that have a unique invariant definition. */
856 record_uses (rtx insn
)
858 struct df_ref
**use_rec
;
859 struct invariant
*inv
;
861 for (use_rec
= DF_INSN_USES (insn
); *use_rec
; use_rec
++)
863 struct df_ref
*use
= *use_rec
;
864 inv
= invariant_for_use (use
);
866 record_use (inv
->def
, DF_REF_REAL_LOC (use
), DF_REF_INSN (use
));
868 for (use_rec
= DF_INSN_EQ_USES (insn
); *use_rec
; use_rec
++)
870 struct df_ref
*use
= *use_rec
;
871 inv
= invariant_for_use (use
);
873 record_use (inv
->def
, DF_REF_REAL_LOC (use
), DF_REF_INSN (use
));
877 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
878 executed. ALWAYS_EXECUTED is true if the insn is always executed,
879 unless the program ends due to a function call. */
882 find_invariants_insn (rtx insn
, bool always_reached
, bool always_executed
)
884 find_invariant_insn (insn
, always_reached
, always_executed
);
888 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
889 basic block is always executed. ALWAYS_EXECUTED is true if the basic
890 block is always executed, unless the program ends due to a function
894 find_invariants_bb (basic_block bb
, bool always_reached
, bool always_executed
)
898 FOR_BB_INSNS (bb
, insn
)
903 find_invariants_insn (insn
, always_reached
, always_executed
);
907 && !CONST_OR_PURE_CALL_P (insn
))
908 always_reached
= false;
912 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
913 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
914 bitmap of basic blocks in BODY that are always executed unless the program
915 ends due to a function call. */
918 find_invariants_body (struct loop
*loop
, basic_block
*body
,
919 bitmap always_reached
, bitmap always_executed
)
923 for (i
= 0; i
< loop
->num_nodes
; i
++)
924 find_invariants_bb (body
[i
],
925 bitmap_bit_p (always_reached
, i
),
926 bitmap_bit_p (always_executed
, i
));
929 /* Finds invariants in LOOP. */
932 find_invariants (struct loop
*loop
)
934 bitmap may_exit
= BITMAP_ALLOC (NULL
);
935 bitmap always_reached
= BITMAP_ALLOC (NULL
);
936 bitmap has_exit
= BITMAP_ALLOC (NULL
);
937 bitmap always_executed
= BITMAP_ALLOC (NULL
);
938 basic_block
*body
= get_loop_body_in_dom_order (loop
);
940 find_exits (loop
, body
, may_exit
, has_exit
);
941 compute_always_reached (loop
, body
, may_exit
, always_reached
);
942 compute_always_reached (loop
, body
, has_exit
, always_executed
);
944 find_defs (loop
, body
);
945 find_invariants_body (loop
, body
, always_reached
, always_executed
);
946 merge_identical_invariants ();
948 BITMAP_FREE (always_reached
);
949 BITMAP_FREE (always_executed
);
950 BITMAP_FREE (may_exit
);
951 BITMAP_FREE (has_exit
);
955 /* Frees a list of uses USE. */
958 free_use_list (struct use
*use
)
962 for (; use
; use
= next
)
969 /* Calculates cost and number of registers needed for moving invariant INV
970 out of the loop and stores them to *COST and *REGS_NEEDED. */
973 get_inv_cost (struct invariant
*inv
, int *comp_cost
, unsigned *regs_needed
)
976 unsigned aregs_needed
;
978 struct invariant
*dep
;
981 /* Find the representative of the class of the equivalent invariants. */
982 inv
= VEC_index (invariant_p
, invariants
, inv
->eqto
);
987 || inv
->stamp
== actual_stamp
)
989 inv
->stamp
= actual_stamp
;
992 (*comp_cost
) += inv
->cost
;
996 /* Hoisting constant pool constants into stack regs may cost more than
997 just single register. On x87, the balance is affected both by the
998 small number of FP registers, and by its register stack organization,
999 that forces us to add compensation code in and around the loop to
1000 shuffle the operands to the top of stack before use, and pop them
1001 from the stack after the loop finishes.
1003 To model this effect, we increase the number of registers needed for
1004 stack registers by two: one register push, and one register pop.
1005 This usually has the effect that FP constant loads from the constant
1006 pool are not moved out of the loop.
1008 Note that this also means that dependent invariants can not be moved.
1009 However, the primary purpose of this pass is to move loop invariant
1010 address arithmetic out of loops, and address arithmetic that depends
1011 on floating point constants is unlikely to ever occur. */
1012 rtx set
= single_set (inv
->insn
);
1014 && IS_STACK_MODE (GET_MODE (SET_SRC (set
)))
1015 && constant_pool_constant_p (SET_SRC (set
)))
1016 (*regs_needed
) += 2;
1020 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
, bi
)
1022 dep
= VEC_index (invariant_p
, invariants
, depno
);
1024 get_inv_cost (dep
, &acomp_cost
, &aregs_needed
);
1027 /* We need to check always_executed, since if the original value of
1028 the invariant may be preserved, we may need to keep it in a
1029 separate register. TODO check whether the register has an
1030 use outside of the loop. */
1031 && dep
->always_executed
1032 && !dep
->def
->uses
->next
)
1034 /* If this is a single use, after moving the dependency we will not
1035 need a new register. */
1039 (*regs_needed
) += aregs_needed
;
1040 (*comp_cost
) += acomp_cost
;
1044 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
1045 of registers used in the loop, NEW_REGS is the number of new variables
1046 already added due to the invariant motion. The number of registers needed
1047 for it is stored in *REGS_NEEDED. */
1050 gain_for_invariant (struct invariant
*inv
, unsigned *regs_needed
,
1051 unsigned new_regs
, unsigned regs_used
)
1053 int comp_cost
, size_cost
;
1055 get_inv_cost (inv
, &comp_cost
, regs_needed
);
1058 size_cost
= (estimate_reg_pressure_cost (new_regs
+ *regs_needed
, regs_used
)
1059 - estimate_reg_pressure_cost (new_regs
, regs_used
));
1061 return comp_cost
- size_cost
;
1064 /* Finds invariant with best gain for moving. Returns the gain, stores
1065 the invariant in *BEST and number of registers needed for it to
1066 *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
1067 NEW_REGS is the number of new variables already added due to invariant
1071 best_gain_for_invariant (struct invariant
**best
, unsigned *regs_needed
,
1072 unsigned new_regs
, unsigned regs_used
)
1074 struct invariant
*inv
;
1075 int gain
= 0, again
;
1076 unsigned aregs_needed
, invno
;
1078 for (invno
= 0; VEC_iterate (invariant_p
, invariants
, invno
, inv
); invno
++)
1083 /* Only consider the "representatives" of equivalent invariants. */
1084 if (inv
->eqto
!= inv
->invno
)
1087 again
= gain_for_invariant (inv
, &aregs_needed
, new_regs
, regs_used
);
1092 *regs_needed
= aregs_needed
;
1099 /* Marks invariant INVNO and all its dependencies for moving. */
1102 set_move_mark (unsigned invno
)
1104 struct invariant
*inv
= VEC_index (invariant_p
, invariants
, invno
);
1107 /* Find the representative of the class of the equivalent invariants. */
1108 inv
= VEC_index (invariant_p
, invariants
, inv
->eqto
);
1115 fprintf (dump_file
, "Decided to move invariant %d\n", invno
);
1117 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, invno
, bi
)
1119 set_move_mark (invno
);
1123 /* Determines which invariants to move. */
1126 find_invariants_to_move (void)
1128 unsigned i
, regs_used
, regs_needed
= 0, new_regs
;
1129 struct invariant
*inv
= NULL
;
1130 unsigned int n_regs
= DF_REG_SIZE (df
);
1132 if (!VEC_length (invariant_p
, invariants
))
1135 /* We do not really do a good job in estimating number of registers used;
1136 we put some initial bound here to stand for induction variables etc.
1137 that we do not detect. */
1140 for (i
= 0; i
< n_regs
; i
++)
1142 if (!DF_REGNO_FIRST_DEF (i
) && DF_REGNO_LAST_USE (i
))
1144 /* This is a value that is used but not changed inside loop. */
1150 while (best_gain_for_invariant (&inv
, ®s_needed
, new_regs
, regs_used
) > 0)
1152 set_move_mark (inv
->invno
);
1153 new_regs
+= regs_needed
;
1157 /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
1161 move_invariant_reg (struct loop
*loop
, unsigned invno
)
1163 struct invariant
*inv
= VEC_index (invariant_p
, invariants
, invno
);
1164 struct invariant
*repr
= VEC_index (invariant_p
, invariants
, inv
->eqto
);
1166 basic_block preheader
= loop_preheader_edge (loop
)->src
;
1167 rtx reg
, set
, dest
, note
;
1175 /* If this is a representative of the class of equivalent invariants,
1176 really move the invariant. Otherwise just replace its use with
1177 the register used for the representative. */
1180 if (inv
->depends_on
)
1182 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, i
, bi
)
1184 if (!move_invariant_reg (loop
, i
))
1189 /* Move the set out of the loop. If the set is always executed (we could
1190 omit this condition if we know that the register is unused outside of the
1191 loop, but it does not seem worth finding out) and it has no uses that
1192 would not be dominated by it, we may just move it (TODO). Otherwise we
1193 need to create a temporary register. */
1194 set
= single_set (inv
->insn
);
1195 dest
= SET_DEST (set
);
1196 reg
= gen_reg_rtx (GET_MODE (dest
));
1198 /* Try replacing the destination by a new pseudoregister. */
1199 if (!validate_change (inv
->insn
, &SET_DEST (set
), reg
, false))
1201 df_insn_rescan (inv
->insn
);
1203 emit_insn_after (gen_move_insn (dest
, reg
), inv
->insn
);
1204 reorder_insns (inv
->insn
, inv
->insn
, BB_END (preheader
));
1206 /* If there is a REG_EQUAL note on the insn we just moved, and
1207 insn is in a basic block that is not always executed, the note
1208 may no longer be valid after we move the insn.
1209 Note that uses in REG_EQUAL notes are taken into account in
1210 the computation of invariants. Hence it is safe to retain the
1211 note even if the note contains register references. */
1212 if (! inv
->always_executed
1213 && (note
= find_reg_note (inv
->insn
, REG_EQUAL
, NULL_RTX
)))
1214 remove_note (inv
->insn
, note
);
1218 if (!move_invariant_reg (loop
, repr
->invno
))
1221 set
= single_set (inv
->insn
);
1222 emit_insn_after (gen_move_insn (SET_DEST (set
), reg
), inv
->insn
);
1223 delete_insn (inv
->insn
);
1229 /* Replace the uses we know to be dominated. It saves work for copy
1230 propagation, and also it is necessary so that dependent invariants
1231 are computed right. */
1234 for (use
= inv
->def
->uses
; use
; use
= use
->next
)
1237 df_insn_rescan (use
->insn
);
1244 /* If we failed, clear move flag, so that we do not try to move inv
1247 fprintf (dump_file
, "Failed to move invariant %d\n", invno
);
1249 inv
->reg
= NULL_RTX
;
1254 /* Move selected invariant out of the LOOP. Newly created regs are marked
1255 in TEMPORARY_REGS. */
1258 move_invariants (struct loop
*loop
)
1260 struct invariant
*inv
;
1263 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
1264 move_invariant_reg (loop
, i
);
1267 /* Initializes invariant motion data. */
1270 init_inv_motion_data (void)
1274 invariants
= VEC_alloc (invariant_p
, heap
, 100);
1277 /* Frees the data allocated by invariant motion. */
1280 free_inv_motion_data (void)
1284 struct invariant
*inv
;
1286 check_invariant_table_size ();
1287 for (i
= 0; i
< DF_DEFS_TABLE_SIZE (); i
++)
1289 inv
= invariant_table
[i
];
1293 gcc_assert (def
!= NULL
);
1295 free_use_list (def
->uses
);
1297 invariant_table
[i
] = NULL
;
1301 for (i
= 0; VEC_iterate (invariant_p
, invariants
, i
, inv
); i
++)
1303 BITMAP_FREE (inv
->depends_on
);
1306 VEC_free (invariant_p
, heap
, invariants
);
1309 /* Move the invariants out of the LOOP. */
1312 move_single_loop_invariants (struct loop
*loop
)
1314 init_inv_motion_data ();
1316 find_invariants (loop
);
1317 find_invariants_to_move ();
1318 move_invariants (loop
);
1320 free_inv_motion_data ();
1323 /* Releases the auxiliary data for LOOP. */
1326 free_loop_data (struct loop
*loop
)
1328 struct loop_data
*data
= LOOP_DATA (loop
);
1334 /* Move the invariants out of the loops. */
1337 move_loop_invariants (void)
1342 df_set_flags (DF_EQ_NOTES
+ DF_DEFER_INSN_RESCAN
);
1343 /* Process the loops, innermost first. */
1344 FOR_EACH_LOOP (li
, loop
, LI_FROM_INNERMOST
)
1346 move_single_loop_invariants (loop
);
1349 FOR_EACH_LOOP (li
, loop
, 0)
1351 free_loop_data (loop
);
1354 free (invariant_table
);
1355 invariant_table
= NULL
;
1356 invariant_table_size
= 0;
1358 #ifdef ENABLE_CHECKING
1359 verify_flow_info ();