1 /* Rtl-level loop invariant motion.
2 Copyright (C) 2004 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, 59 Temple Place - Suite 330, 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 like
23 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"
43 #include "hard-reg-set.h"
44 #include "basic-block.h"
52 /* The data stored for the loop. */
56 struct loop
*outermost_exit
; /* The outermost exit of the loop. */
57 bool has_call
; /* True if the loop contains a call. */
60 #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
62 /* The description of an use. */
66 rtx
*pos
; /* Position of the use. */
67 rtx insn
; /* The insn in that the use occurs. */
69 struct use
*next
; /* Next use in the list. */
72 /* The description of a def. */
76 struct use
*uses
; /* The list of uses that are uniquely reached
78 unsigned n_uses
; /* Number of such uses. */
79 unsigned invno
; /* The corresponding invariant. */
82 /* The data stored for each invariant. */
86 /* The number of the invariant. */
89 /* Whether we already processed the invariant. */
92 /* The definition of the invariant. */
95 /* The insn in that it is defined. */
98 /* Whether it is always executed. */
101 /* Whether to move the invariant. */
104 /* Cost if the invariant. */
107 /* The invariants it depends on. */
110 /* Used for detecting already visited invariants during determining
111 costs of movements. */
115 /* The actual stamp for marking already visited invariants during determining
116 costs of movements. */
118 static unsigned actual_stamp
;
120 /* The invariants. */
122 static varray_type invariants
;
124 /* Test for possibility of invariantness of X. */
127 check_maybe_invariant (rtx x
)
129 enum rtx_code code
= GET_CODE (x
);
144 case UNSPEC_VOLATILE
:
152 /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
153 It should not be hard, and might be faster than "elsewhere". */
155 /* Just handle the most trivial case where we load from an unchanging
156 location (most importantly, pic tables). */
157 if (RTX_UNCHANGING_P (x
))
163 /* Don't mess with insns declared volatile. */
164 if (MEM_VOLATILE_P (x
))
172 fmt
= GET_RTX_FORMAT (code
);
173 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
177 if (!check_maybe_invariant (XEXP (x
, i
)))
180 else if (fmt
[i
] == 'E')
182 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
183 if (!check_maybe_invariant (XVECEXP (x
, i
, j
)))
191 /* Determines the basic blocks inside LOOP that are always executed and
192 stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
193 basic blocks that may either exit the loop, or contain the call that
194 does not have to return. BODY is body of the loop obtained by
195 get_loop_body_in_dom_order. */
198 compute_always_reached (struct loop
*loop
, basic_block
*body
,
199 bitmap may_exit
, bitmap always_reached
)
203 for (i
= 0; i
< loop
->num_nodes
; i
++)
205 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, body
[i
]))
206 bitmap_set_bit (always_reached
, i
);
208 if (bitmap_bit_p (may_exit
, i
))
213 /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
214 exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
215 additionally mark blocks that may exit due to a call. */
218 find_exits (struct loop
*loop
, basic_block
*body
,
219 bitmap may_exit
, bitmap has_exit
)
223 struct loop
*outermost_exit
= loop
, *aexit
;
224 bool has_call
= false;
227 for (i
= 0; i
< loop
->num_nodes
; i
++)
229 if (body
[i
]->loop_father
== loop
)
231 FOR_BB_INSNS (body
[i
], insn
)
234 && !CONST_OR_PURE_CALL_P (insn
))
237 bitmap_set_bit (may_exit
, i
);
242 for (e
= body
[i
]->succ
; e
; e
= e
->succ_next
)
244 if (flow_bb_inside_loop_p (loop
, e
->dest
))
247 bitmap_set_bit (may_exit
, i
);
248 bitmap_set_bit (has_exit
, i
);
249 outermost_exit
= find_common_loop (outermost_exit
,
250 e
->dest
->loop_father
);
255 /* Use the data stored for the subloop to decide whether we may exit
256 through it. It is sufficient to do this for header of the loop,
257 as other basic blocks inside it must be dominated by it. */
258 if (body
[i
]->loop_father
->header
!= body
[i
])
261 if (LOOP_DATA (body
[i
]->loop_father
)->has_call
)
264 bitmap_set_bit (may_exit
, i
);
266 aexit
= LOOP_DATA (body
[i
]->loop_father
)->outermost_exit
;
269 bitmap_set_bit (may_exit
, i
);
270 bitmap_set_bit (has_exit
, i
);
272 if (flow_loop_nested_p (aexit
, outermost_exit
))
273 outermost_exit
= aexit
;
277 loop
->aux
= xcalloc (1, sizeof (struct loop_data
));
278 LOOP_DATA (loop
)->outermost_exit
= outermost_exit
;
279 LOOP_DATA (loop
)->has_call
= has_call
;
282 /* Check whether we may assign a value to X from a register. */
285 may_assign_reg_p (rtx x
)
287 return can_copy_p (GET_MODE (x
));
290 /* Finds definitions that may correspond to invariants in LOOP with body BODY.
291 DF is the dataflow object. */
294 find_defs (struct loop
*loop
, basic_block
*body
, struct df
*df
)
297 bitmap blocks
= BITMAP_XMALLOC ();
299 for (i
= 0; i
< loop
->num_nodes
; i
++)
300 bitmap_set_bit (blocks
, body
[i
]->index
);
302 df_analyze_subcfg (df
, blocks
, DF_UD_CHAIN
| DF_HARD_REGS
| DF_EQUIV_NOTES
);
303 BITMAP_XFREE (blocks
);
306 /* Creates a new invariant for definition DEF in INSN, depending on invariants
307 in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
308 unless the program ends due to a function call. */
311 create_new_invariant (struct def
*def
, rtx insn
, bitmap depends_on
,
312 bool always_executed
)
314 struct invariant
*inv
= xmalloc (sizeof (struct invariant
));
315 rtx set
= single_set (insn
);
318 inv
->always_executed
= always_executed
;
319 inv
->depends_on
= depends_on
;
321 /* If the set is simple, usually by moving it we move the whole store out of
322 the loop. Otherwise we save only cost of the computation. */
324 inv
->cost
= rtx_cost (set
, SET
);
326 inv
->cost
= rtx_cost (SET_SRC (set
), SET
);
329 inv
->processed
= false;
333 inv
->invno
= VARRAY_ACTIVE_SIZE (invariants
);
335 def
->invno
= inv
->invno
;
336 VARRAY_PUSH_GENERIC_PTR_NOGC (invariants
, inv
);
341 "Set in insn %d is invariant (%d), cost %d, depends on ",
342 INSN_UID (insn
), inv
->invno
, inv
->cost
);
343 dump_bitmap (dump_file
, inv
->depends_on
);
347 /* Record USE at DEF. */
350 record_use (struct def
*def
, rtx
*use
, rtx insn
)
352 struct use
*u
= xmalloc (sizeof (struct use
));
354 if (GET_CODE (*use
) == SUBREG
)
355 use
= &SUBREG_REG (*use
);
366 /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
367 bitmap. DF is the dataflow object. */
370 check_dependencies (rtx insn
, struct df
*df
, bitmap depends_on
)
372 struct df_link
*uses
, *defs
;
373 struct ref
*use
, *def
;
374 basic_block bb
= BLOCK_FOR_INSN (insn
), def_bb
;
375 struct def
*def_data
;
377 for (uses
= DF_INSN_USES (df
, insn
); uses
; uses
= uses
->next
)
381 defs
= DF_REF_CHAIN (use
);
389 def_data
= DF_REF_DATA (def
);
393 def_bb
= DF_REF_BB (def
);
394 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
397 bitmap_set_bit (depends_on
, def_data
->invno
);
403 /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
404 executed. ALWAYS_EXECUTED is true if the insn is always executed,
405 unless the program ends due to a function call. DF is the dataflow
409 find_invariant_insn (rtx insn
, bool always_reached
, bool always_executed
,
418 /* Until we get rid of LIBCALLS. */
419 if (find_reg_note (insn
, REG_RETVAL
, NULL_RTX
)
420 || find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)
421 || find_reg_note (insn
, REG_NO_CONFLICT
, NULL_RTX
))
424 set
= single_set (insn
);
427 dest
= SET_DEST (set
);
429 if (GET_CODE (dest
) != REG
430 || HARD_REGISTER_P (dest
))
433 if (!check_maybe_invariant (SET_SRC (set
))
434 || !may_assign_reg_p (SET_DEST (set
)))
437 if (may_trap_p (PATTERN (insn
)))
442 /* Unless the exceptions are handled, the behavior is undefined
443 if the trap occurs. */
444 if (flag_non_call_exceptions
)
448 depends_on
= BITMAP_XMALLOC ();
449 if (!check_dependencies (insn
, df
, depends_on
))
451 BITMAP_XFREE (depends_on
);
457 ref
= df_find_def (df
, insn
, dest
);
458 def
= xcalloc (1, sizeof (struct def
));
459 DF_REF_DATA (ref
) = def
;
464 create_new_invariant (def
, insn
, depends_on
, always_executed
);
467 /* Record registers used in INSN that have an unique invariant definition.
468 DF is the dataflow object. */
471 record_uses (rtx insn
, struct df
*df
)
473 struct df_link
*uses
, *defs
;
474 struct ref
*use
, *def
;
475 basic_block bb
= BLOCK_FOR_INSN (insn
), def_bb
;
477 for (uses
= DF_INSN_USES (df
, insn
); uses
; uses
= uses
->next
)
481 defs
= DF_REF_CHAIN (use
);
482 if (!defs
|| defs
->next
)
485 if (!DF_REF_DATA (def
))
488 def_bb
= DF_REF_BB (def
);
489 if (!dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
492 record_use (DF_REF_DATA (def
), DF_REF_LOC (use
), DF_REF_INSN (use
));
496 /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
497 executed. ALWAYS_EXECUTED is true if the insn is always executed,
498 unless the program ends due to a function call. DF is the dataflow
502 find_invariants_insn (rtx insn
, bool always_reached
, bool always_executed
,
505 find_invariant_insn (insn
, always_reached
, always_executed
, df
);
506 record_uses (insn
, df
);
509 /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
510 basic block is always executed. ALWAYS_EXECUTED is true if the basic
511 block is always executed, unless the program ends due to a function
512 call. DF is the dataflow object. */
515 find_invariants_bb (basic_block bb
, bool always_reached
, bool always_executed
,
520 FOR_BB_INSNS (bb
, insn
)
525 find_invariants_insn (insn
, always_reached
, always_executed
, df
);
529 && !CONST_OR_PURE_CALL_P (insn
))
530 always_reached
= false;
534 /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
535 basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
536 bitmap of basic blocks in BODY that are always executed unless the program
537 ends due to a function call. DF is the dataflow object. */
540 find_invariants_body (struct loop
*loop
, basic_block
*body
,
541 bitmap always_reached
, bitmap always_executed
,
546 for (i
= 0; i
< loop
->num_nodes
; i
++)
547 find_invariants_bb (body
[i
],
548 bitmap_bit_p (always_reached
, i
),
549 bitmap_bit_p (always_executed
, i
),
553 /* Finds invariants in LOOP. DF is the dataflow object. */
556 find_invariants (struct loop
*loop
, struct df
*df
)
558 bitmap may_exit
= BITMAP_XMALLOC ();
559 bitmap always_reached
= BITMAP_XMALLOC ();
560 bitmap has_exit
= BITMAP_XMALLOC ();
561 bitmap always_executed
= BITMAP_XMALLOC ();
562 basic_block
*body
= get_loop_body_in_dom_order (loop
);
564 find_exits (loop
, body
, may_exit
, has_exit
);
565 compute_always_reached (loop
, body
, may_exit
, always_reached
);
566 compute_always_reached (loop
, body
, has_exit
, always_executed
);
568 find_defs (loop
, body
, df
);
569 find_invariants_body (loop
, body
, always_reached
, always_executed
, df
);
571 BITMAP_XFREE (always_reached
);
572 BITMAP_XFREE (always_executed
);
573 BITMAP_XFREE (may_exit
);
574 BITMAP_XFREE (has_exit
);
578 /* Frees a list of uses USE. */
581 free_use_list (struct use
*use
)
585 for (; use
; use
= next
)
592 /* Calculates cost and number of registers needed for moving invariant INV
593 out of the loop and stores them to *COST and *REGS_NEEDED. */
596 get_inv_cost (struct invariant
*inv
, int *comp_cost
, unsigned *regs_needed
)
599 unsigned aregs_needed
;
601 struct invariant
*dep
;
606 || inv
->stamp
== actual_stamp
)
608 inv
->stamp
= actual_stamp
;
611 (*comp_cost
) += inv
->cost
;
613 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, depno
,
615 dep
= VARRAY_GENERIC_PTR_NOGC (invariants
, depno
);
617 get_inv_cost (dep
, &acomp_cost
, &aregs_needed
);
620 /* We need to check always_executed, since if the original value of
621 the invariant may be preserved, we may need to keep it in a
622 separate register. TODO check whether the register has an
623 use outside of the loop. */
624 && dep
->always_executed
625 && !dep
->def
->uses
->next
)
627 /* If this is a single use, after moving the dependency we will not
628 need a new register. */
632 (*regs_needed
) += aregs_needed
;
633 (*comp_cost
) += acomp_cost
;
637 /* Calculates gain for eliminating invariant INV. REGS_USED is the number
638 of registers used in the loop, N_INV_USES is the number of uses of
639 invariants, NEW_REGS is the number of new variables already added due to
640 the invariant motion. The number of registers needed for it is stored in
644 gain_for_invariant (struct invariant
*inv
, unsigned *regs_needed
,
645 unsigned new_regs
, unsigned regs_used
, unsigned n_inv_uses
)
647 int comp_cost
, size_cost
;
649 get_inv_cost (inv
, &comp_cost
, regs_needed
);
652 size_cost
= (global_cost_for_size (new_regs
+ *regs_needed
,
653 regs_used
, n_inv_uses
)
654 - global_cost_for_size (new_regs
, regs_used
, n_inv_uses
));
656 return comp_cost
- size_cost
;
659 /* Finds invariant with best gain for moving. Returns the gain, stores
660 the invariant in *BEST and number of registers needed for it to
661 *REGS_NEEDED. REGS_USED is the number of registers used in
662 the loop, N_INV_USES is the number of uses of invariants. NEW_REGS
663 is the number of new variables already added due to invariant motion. */
666 best_gain_for_invariant (struct invariant
**best
, unsigned *regs_needed
,
667 unsigned new_regs
, unsigned regs_used
,
670 struct invariant
*inv
;
672 unsigned aregs_needed
, invno
;
674 for (invno
= 0; invno
< VARRAY_ACTIVE_SIZE (invariants
); invno
++)
676 inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, invno
);
680 again
= gain_for_invariant (inv
, &aregs_needed
,
681 new_regs
, regs_used
, n_inv_uses
);
686 *regs_needed
= aregs_needed
;
693 /* Marks invariant INVNO and all its dependencies for moving. */
696 set_move_mark (unsigned invno
)
698 struct invariant
*inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, invno
);
705 fprintf (dump_file
, "Decided to move invariant %d\n", invno
);
707 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, invno
, set_move_mark (invno
));
710 /* Determines which invariants to move. DF is the dataflow object. */
713 find_invariants_to_move (struct df
*df
)
715 unsigned i
, regs_used
, n_inv_uses
, regs_needed
= 0, new_regs
;
716 struct invariant
*inv
= NULL
;
718 if (flag_move_all_movables
)
720 /* This is easy & stupid. */
721 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (invariants
); i
++)
723 inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, i
);
729 if (!VARRAY_ACTIVE_SIZE (invariants
))
732 /* Now something slightly more involved. First estimate the number of used
736 /* We do not really do a good job in this estimation; put some initial bound
737 here to stand for induction variables etc. that we do not detect. */
740 for (i
= 0; i
< df
->n_regs
; i
++)
742 if (!DF_REGNO_FIRST_DEF (df
, i
) && DF_REGNO_LAST_USE (df
, i
))
744 /* This is a value that is used but not changed inside loop. */
749 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (invariants
); i
++)
751 inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, i
);
753 n_inv_uses
+= inv
->def
->n_uses
;
757 while (best_gain_for_invariant (&inv
, ®s_needed
,
758 new_regs
, regs_used
, n_inv_uses
) > 0)
760 set_move_mark (inv
->invno
);
761 new_regs
+= regs_needed
;
765 /* Move invariant INVNO out of the LOOP. DF is the dataflow object. */
768 move_invariant_reg (struct loop
*loop
, unsigned invno
, struct df
*df
)
770 struct invariant
*inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, invno
);
772 basic_block preheader
= loop_preheader_edge (loop
)->src
;
778 inv
->processed
= true;
782 EXECUTE_IF_SET_IN_BITMAP (inv
->depends_on
, 0, i
,
784 move_invariant_reg (loop
, i
, df
);
788 /* Move the set out of the loop. If the set is always executed (we could
789 omit this condition if we know that the register is unused outside of the
790 loop, but it does not seem worth finding out) and it has no uses that
791 would not be dominated by it, we may just move it (TODO). Otherwise we
792 need to create a temporary register. */
793 set
= single_set (inv
->insn
);
794 reg
= gen_reg_rtx (GET_MODE (SET_DEST (set
)));
795 df_pattern_emit_after (df
, gen_move_insn (SET_DEST (set
), reg
),
796 BLOCK_FOR_INSN (inv
->insn
), inv
->insn
);
797 SET_DEST (set
) = reg
;
798 reorder_insns (inv
->insn
, inv
->insn
, BB_END (preheader
));
799 df_insn_modify (df
, preheader
, inv
->insn
);
801 /* Replace the uses we know to be dominated. It saves work for copy
802 propagation, and also it is necessary so that dependent invariants
803 are computed right. */
806 for (use
= inv
->def
->uses
; use
; use
= use
->next
)
809 df_insn_modify (df
, BLOCK_FOR_INSN (use
->insn
), use
->insn
);
814 /* Move selected invariant out of the LOOP. Newly created regs are marked
815 in TEMPORARY_REGS. DF is the dataflow object. */
818 move_invariants (struct loop
*loop
, struct df
*df
)
820 struct invariant
*inv
;
823 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (invariants
); i
++)
825 inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, i
);
827 move_invariant_reg (loop
, i
, df
);
831 /* Initializes invariant motion data. */
834 init_inv_motion_data (void)
839 VARRAY_GENERIC_PTR_NOGC_INIT (invariants
, 100, "invariants");
842 /* Frees the data allocated by invariant motion. DF is the dataflow
846 free_inv_motion_data (struct df
*df
)
850 struct invariant
*inv
;
852 for (i
= 0; i
< df
->n_defs
; i
++)
857 def
= DF_REF_DATA (df
->defs
[i
]);
861 free_use_list (def
->uses
);
863 DF_REF_DATA (df
->defs
[i
]) = NULL
;
866 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (invariants
); i
++)
868 inv
= VARRAY_GENERIC_PTR_NOGC (invariants
, i
);
869 BITMAP_XFREE (inv
->depends_on
);
872 VARRAY_POP_ALL (invariants
);
875 /* Move the invariants out of the LOOP. DF is the dataflow object. */
878 move_single_loop_invariants (struct loop
*loop
, struct df
*df
)
880 init_inv_motion_data ();
882 find_invariants (loop
, df
);
883 find_invariants_to_move (df
);
884 move_invariants (loop
, df
);
886 free_inv_motion_data (df
);
889 /* Releases the auxiliary data for LOOP. */
892 free_loop_data (struct loop
*loop
)
894 struct loop_data
*data
= LOOP_DATA (loop
);
900 /* Move the invariants out of the LOOPS. */
903 move_loop_invariants (struct loops
*loops
)
907 struct df
*df
= df_init ();
909 /* Process the loops, innermost first. */
910 loop
= loops
->tree_root
;
914 while (loop
!= loops
->tree_root
)
916 move_single_loop_invariants (loop
, df
);
928 for (i
= 1; i
< loops
->num
; i
++)
929 if (loops
->parray
[i
])
930 free_loop_data (loops
->parray
[i
]);