OSDN Git Service

14735810fd52e4e1fff5a7defbcc00dc5a6893ae
[pf3gnuchains/gcc-fork.git] / gcc / loop.h
1 /* Loop optimization definitions for GNU C-Compiler
2    Copyright (C) 1991, 1995, 1998, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "varray.h"
22
23 /* Get the luid of an insn.  Catch the error of trying to reference the LUID
24    of an insn added during loop, since these don't have LUIDs.  */
25
26 #define INSN_LUID(INSN)                 \
27   (INSN_UID (INSN) < max_uid_for_loop ? uid_luid[INSN_UID (INSN)] \
28    : (abort (), -1))
29
30 /* A "basic induction variable" or biv is a pseudo reg that is set
31    (within this loop) only by incrementing or decrementing it.  */
32 /* A "general induction variable" or giv is a pseudo reg whose
33    value is a linear function of a biv.  */
34
35 /* Bivs are recognized by `basic_induction_var';
36    Givs by `general_induct_var'.  */
37
38 /* An enum for the two different types of givs, those that are used
39    as memory addresses and those that are calculated into registers.  */
40 enum g_types { DEST_ADDR, DEST_REG };
41
42 /* A `struct induction' is created for every instruction that sets
43    an induction variable (either a biv or a giv).  */
44
45 struct induction
46 {
47   rtx insn;                     /* The insn that sets a biv or giv */
48   rtx new_reg;                  /* New register, containing strength reduced
49                                    version of this giv.  */
50   rtx src_reg;                  /* Biv from which this giv is computed.
51                                    (If this is a biv, then this is the biv.) */
52   enum g_types giv_type;        /* Indicate whether DEST_ADDR or DEST_REG */
53   rtx dest_reg;                 /* Destination register for insn: this is the
54                                    register which was the biv or giv.
55                                    For a biv, this equals src_reg.
56                                    For a DEST_ADDR type giv, this is 0.  */
57   rtx *location;                /* Place in the insn where this giv occurs.
58                                    If GIV_TYPE is DEST_REG, this is 0.  */
59                                 /* For a biv, this is the place where add_val
60                                    was found.  */
61   enum machine_mode mode;       /* The mode of this biv or giv */
62   enum machine_mode mem_mode;   /* For DEST_ADDR, mode of the memory object. */
63   rtx mult_val;                 /* Multiplicative factor for src_reg.  */
64   rtx add_val;                  /* Additive constant for that product.  */
65   int benefit;                  /* Gain from eliminating this insn.  */
66   rtx final_value;              /* If the giv is used outside the loop, and its
67                                    final value could be calculated, it is put
68                                    here, and the giv is made replaceable.  Set
69                                    the giv to this value before the loop.  */
70   unsigned combined_with;       /* The number of givs this giv has been
71                                    combined with.  If nonzero, this giv
72                                    cannot combine with any other giv.  */
73   unsigned replaceable : 1;     /* 1 if we can substitute the strength-reduced
74                                    variable for the original variable.
75                                    0 means they must be kept separate and the
76                                    new one must be copied into the old pseudo
77                                    reg each time the old one is set.  */
78   unsigned not_replaceable : 1; /* Used to prevent duplicating work.  This is
79                                    1 if we know that the giv definitely can
80                                    not be made replaceable, in which case we
81                                    don't bother checking the variable again
82                                    even if further info is available.
83                                    Both this and the above can be zero.  */
84   unsigned ignore : 1;          /* 1 prohibits further processing of giv */
85   unsigned always_computable : 1;/* 1 if this value is computable every
86                                     iteration.  */
87   unsigned always_executed : 1; /* 1 if this set occurs each iteration.  */
88   unsigned maybe_multiple : 1;  /* Only used for a biv and  1 if this biv
89                                    update may be done multiple times per
90                                    iteration. */
91   unsigned cant_derive : 1;     /* For giv's, 1 if this giv cannot derive
92                                    another giv.  This occurs in many cases
93                                    where a giv's lifetime spans an update to
94                                    a biv. */
95   unsigned maybe_dead : 1;      /* 1 if this giv might be dead.  In that case,
96                                    we won't use it to eliminate a biv, it
97                                    would probably lose. */
98   unsigned auto_inc_opt : 1;    /* 1 if this giv had its increment output next
99                                    to it to try to form an auto-inc address. */
100   unsigned unrolled : 1;        /* 1 if new register has been allocated and
101                                    initialized in unrolled loop.  */
102   unsigned shared : 1;
103   unsigned no_const_addval : 1; /* 1 if add_val does not contain a const. */
104   unsigned multi_insn_incr : 1; /* 1 if multiple insns updated the biv.  */
105   int lifetime;                 /* Length of life of this giv */
106   rtx derive_adjustment;        /* If nonzero, is an adjustment to be
107                                    subtracted from add_val when this giv
108                                    derives another.  This occurs when the
109                                    giv spans a biv update by incrementation. */
110   struct induction *next_iv;    /* For givs, links together all givs that are
111                                    based on the same biv.  For bivs, links
112                                    together all biv entries that refer to the
113                                    same biv register.  */
114   struct induction *same;       /* If this giv has been combined with another
115                                    giv, this points to the base giv.  The base
116                                    giv will have COMBINED_WITH non-zero.  */
117   struct induction *derived_from;/* For a giv, if we decided to derive this
118                                    giv from another one.  */
119   HOST_WIDE_INT const_adjust;   /* Used by loop unrolling, when an address giv
120                                    is split, and a constant is eliminated from
121                                    the address, the -constant is stored here
122                                    for later use. */
123   int ix;                       /* Used by recombine_givs, as n index into
124                                    the stats array.  */
125   struct induction *same_insn;  /* If there are multiple identical givs in
126                                    the same insn, then all but one have this
127                                    field set, and they all point to the giv
128                                    that doesn't have this field set.  */
129   rtx last_use;                 /* For a giv made from a biv increment, this is
130                                    a substitute for the lifetime information. */
131 };
132
133 /* A `struct iv_class' is created for each biv.  */
134
135 struct iv_class {
136   int regno;                    /* Pseudo reg which is the biv.  */
137   int biv_count;                /* Number of insns setting this reg.  */
138   struct induction *biv;        /* List of all insns that set this reg.  */
139   int giv_count;                /* Number of DEST_REG givs computed from this
140                                    biv.  The resulting count is only used in
141                                    check_dbra_loop.  */
142   struct induction *giv;        /* List of all insns that compute a giv
143                                    from this reg.  */
144   int total_benefit;            /* Sum of BENEFITs of all those givs */
145   rtx initial_value;            /* Value of reg at loop start */
146   rtx initial_test;             /* Test performed on BIV before loop */
147   struct iv_class *next;        /* Links all class structures together */
148   rtx init_insn;                /* insn which initializes biv, 0 if none. */
149   rtx init_set;                 /* SET of INIT_INSN, if any. */
150   unsigned incremented : 1;     /* 1 if somewhere incremented/decremented */
151   unsigned eliminable : 1;      /* 1 if plausible candidate for elimination. */
152   unsigned nonneg : 1;          /* 1 if we added a REG_NONNEG note for this. */
153   unsigned reversed : 1;        /* 1 if we reversed the loop that this
154                                    biv controls. */
155 };
156
157 /* Information required to calculate the number of loop iterations. 
158    This is set by loop_iterations.  */
159
160 struct loop_info
161 {
162   /* Loop number.  */
163   int num;
164   /* Loops enclosed by this loop including itself.  */
165   int loops_enclosed;
166   /* Nonzero if there is a subroutine call in the current loop.  */
167   int has_call;
168   /* Nonzero if there is a volatile memory reference in the current
169      loop.  */
170   int has_volatile;
171   /* Nonzero if there is a tablejump in the current loop.  */
172   int has_tablejump;
173   /* Nonzero if there are ways to leave the loop other than falling
174      off the end.  */
175   int has_multiple_exit_targets;
176   /* Nonzero if there is an indirect jump in the current function.  */
177   int has_indirect_jump;
178   /* Register or constant initial loop value.  */
179   rtx initial_value;
180   /* Register or constant value used for comparison test.  */
181   rtx comparison_value;
182   /* Register or constant approximate final value.  */
183   rtx final_value;
184   /* Register or constant initial loop value with term common to
185      final_value removed.  */
186   rtx initial_equiv_value;
187   /* Register or constant final loop value with term common to
188      initial_value removed.  */
189   rtx final_equiv_value;
190   /* Register corresponding to iteration variable.  */
191   rtx iteration_var;
192   /* Constant loop increment.  */
193   rtx increment;
194   enum rtx_code comparison_code;
195   /* Holds the number of loop iterations.  It is zero if the number
196      could not be calculated.  Must be unsigned since the number of
197      iterations can be as high as 2^wordsize - 1.  For loops with a
198      wider iterator, this number will be zero if the number of loop
199      iterations is too large for an unsigned integer to hold.  */
200   unsigned HOST_WIDE_INT n_iterations;
201   /* The number of times the loop body was unrolled.  */
202   unsigned int unroll_number;
203   /* Non-zero if the loop has a NOTE_INSN_LOOP_VTOP.  */
204   rtx vtop;
205   /* Non-zero if the loop has a NOTE_INSN_LOOP_CONT.
206      A continue statement will generate a branch to NEXT_INSN (cont).  */
207   rtx cont;
208 };
209
210 /* Definitions used by the basic induction variable discovery code.  */
211 enum iv_mode { UNKNOWN_INDUCT, BASIC_INDUCT, NOT_BASIC_INDUCT,
212                  GENERAL_INDUCT };
213
214 /* Variables declared in loop.c, but also needed in unroll.c.  */
215
216 extern int *uid_luid;
217 extern int max_uid_for_loop;
218 extern int *uid_loop_num;
219 extern int *loop_outer_loop;
220 extern rtx *loop_number_exit_labels;
221 extern int *loop_number_exit_count;
222 extern int max_reg_before_loop;
223
224 extern FILE *loop_dump_stream;
225
226 extern varray_type reg_iv_type;
227 extern varray_type reg_iv_info;
228
229 #define REG_IV_TYPE(n) \
230   (*(enum iv_mode *) &VARRAY_INT(reg_iv_type, (n)))
231 #define REG_IV_INFO(n) \
232   (*(struct induction **) &VARRAY_GENERIC_PTR(reg_iv_info, (n)))
233
234 extern struct iv_class **reg_biv_class;
235 extern struct iv_class *loop_iv_list;
236
237 extern int first_increment_giv, last_increment_giv;
238
239 /* Forward declarations for non-static functions declared in loop.c and
240    unroll.c.  */
241 int invariant_p PROTO((rtx));
242 rtx get_condition_for_loop PROTO((rtx));
243 void emit_iv_add_mult PROTO((rtx, rtx, rtx, rtx, rtx));
244 rtx express_from PROTO((struct induction *, struct induction *));
245
246 void unroll_loop PROTO((rtx, int, rtx, rtx, struct loop_info *, int));
247 rtx biv_total_increment PROTO((struct iv_class *, rtx, rtx));
248 unsigned HOST_WIDE_INT loop_iterations PROTO((rtx, rtx, struct loop_info *));
249 int precondition_loop_p PROTO((rtx, struct loop_info *, 
250                                rtx *, rtx *, rtx *, 
251                                enum machine_mode *mode));
252 rtx final_biv_value PROTO((struct iv_class *, rtx, rtx,
253                            unsigned HOST_WIDE_INT));
254 rtx final_giv_value PROTO((struct induction *, rtx, rtx,
255                            unsigned HOST_WIDE_INT));
256 void emit_unrolled_add PROTO((rtx, rtx, rtx));
257 int back_branch_in_range_p PROTO((rtx, rtx, rtx));
258
259 int loop_insn_first_p PROTO((rtx, rtx));
260
261 /* Forward declarations for non-static functions declared in stmt.c.  */
262 void find_loop_tree_blocks PROTO((void));
263 void unroll_block_trees PROTO((void));
264