OSDN Git Service

* unroll.c (iteration_info): Subsume into loop_iterations.
[pf3gnuchains/gcc-fork.git] / gcc / loop.h
1 /* Loop optimization definitions for GNU C-Compiler
2    Copyright (C) 1991, 1995, 1998, 1999, 2000 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 #include "bitmap.h"
23
24 /* Flags passed to loop_optimize.  */
25 #define LOOP_UNROLL 1
26 #define LOOP_BCT 2
27
28
29 /* Get the loop info pointer of a loop.  */
30 #define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux) 
31
32 /* Get the luid of an insn.  Catch the error of trying to reference the LUID
33    of an insn added during loop, since these don't have LUIDs.  */
34
35 #define INSN_LUID(INSN)                 \
36   (INSN_UID (INSN) < max_uid_for_loop ? uid_luid[INSN_UID (INSN)] \
37    : (abort (), -1))
38
39 /* A "basic induction variable" or biv is a pseudo reg that is set
40    (within this loop) only by incrementing or decrementing it.  */
41 /* A "general induction variable" or giv is a pseudo reg whose
42    value is a linear function of a biv.  */
43
44 /* Bivs are recognized by `basic_induction_var';
45    Givs by `general_induct_var'.  */
46
47 /* An enum for the two different types of givs, those that are used
48    as memory addresses and those that are calculated into registers.  */
49 enum g_types { DEST_ADDR, DEST_REG };
50
51 /* A `struct induction' is created for every instruction that sets
52    an induction variable (either a biv or a giv).  */
53
54 struct induction
55 {
56   rtx insn;                     /* The insn that sets a biv or giv */
57   rtx new_reg;                  /* New register, containing strength reduced
58                                    version of this giv.  */
59   rtx src_reg;                  /* Biv from which this giv is computed.
60                                    (If this is a biv, then this is the biv.) */
61   enum g_types giv_type;        /* Indicate whether DEST_ADDR or DEST_REG */
62   rtx dest_reg;                 /* Destination register for insn: this is the
63                                    register which was the biv or giv.
64                                    For a biv, this equals src_reg.
65                                    For a DEST_ADDR type giv, this is 0.  */
66   rtx *location;                /* Place in the insn where this giv occurs.
67                                    If GIV_TYPE is DEST_REG, this is 0.  */
68                                 /* For a biv, this is the place where add_val
69                                    was found.  */
70   enum machine_mode mode;       /* The mode of this biv or giv */
71   enum machine_mode mem_mode;   /* For DEST_ADDR, mode of the memory object. */
72   rtx mult_val;                 /* Multiplicative factor for src_reg.  */
73   rtx add_val;                  /* Additive constant for that product.  */
74   int benefit;                  /* Gain from eliminating this insn.  */
75   rtx final_value;              /* If the giv is used outside the loop, and its
76                                    final value could be calculated, it is put
77                                    here, and the giv is made replaceable.  Set
78                                    the giv to this value before the loop.  */
79   unsigned combined_with;       /* The number of givs this giv has been
80                                    combined with.  If nonzero, this giv
81                                    cannot combine with any other giv.  */
82   unsigned replaceable : 1;     /* 1 if we can substitute the strength-reduced
83                                    variable for the original variable.
84                                    0 means they must be kept separate and the
85                                    new one must be copied into the old pseudo
86                                    reg each time the old one is set.  */
87   unsigned not_replaceable : 1; /* Used to prevent duplicating work.  This is
88                                    1 if we know that the giv definitely can
89                                    not be made replaceable, in which case we
90                                    don't bother checking the variable again
91                                    even if further info is available.
92                                    Both this and the above can be zero.  */
93   unsigned ignore : 1;          /* 1 prohibits further processing of giv */
94   unsigned always_computable : 1;/* 1 if this value is computable every
95                                     iteration.  */
96   unsigned always_executed : 1; /* 1 if this set occurs each iteration.  */
97   unsigned maybe_multiple : 1;  /* Only used for a biv and  1 if this biv
98                                    update may be done multiple times per
99                                    iteration. */
100   unsigned cant_derive : 1;     /* For giv's, 1 if this giv cannot derive
101                                    another giv.  This occurs in many cases
102                                    where a giv's lifetime spans an update to
103                                    a biv. */
104   unsigned maybe_dead : 1;      /* 1 if this giv might be dead.  In that case,
105                                    we won't use it to eliminate a biv, it
106                                    would probably lose. */
107   unsigned auto_inc_opt : 1;    /* 1 if this giv had its increment output next
108                                    to it to try to form an auto-inc address. */
109   unsigned unrolled : 1;        /* 1 if new register has been allocated and
110                                    initialized in unrolled loop.  */
111   unsigned shared : 1;
112   unsigned no_const_addval : 1; /* 1 if add_val does not contain a const. */
113   int lifetime;                 /* Length of life of this giv */
114   rtx derive_adjustment;        /* If nonzero, is an adjustment to be
115                                    subtracted from add_val when this giv
116                                    derives another.  This occurs when the
117                                    giv spans a biv update by incrementation. */
118   rtx ext_dependant;            /* If nonzero, is a sign or zero extension
119                                    if a biv on which this giv is dependant.  */
120   struct induction *next_iv;    /* For givs, links together all givs that are
121                                    based on the same biv.  For bivs, links
122                                    together all biv entries that refer to the
123                                    same biv register.  */
124   struct induction *same;       /* If this giv has been combined with another
125                                    giv, this points to the base giv.  The base
126                                    giv will have COMBINED_WITH non-zero.  */
127   struct induction *derived_from;/* For a giv, if we decided to derive this
128                                    giv from another one.  */
129   HOST_WIDE_INT const_adjust;   /* Used by loop unrolling, when an address giv
130                                    is split, and a constant is eliminated from
131                                    the address, the -constant is stored here
132                                    for later use. */
133   int ix;                       /* Used by recombine_givs, as n index into
134                                    the stats array.  */
135   struct induction *same_insn;  /* If there are multiple identical givs in
136                                    the same insn, then all but one have this
137                                    field set, and they all point to the giv
138                                    that doesn't have this field set.  */
139   rtx last_use;                 /* For a giv made from a biv increment, this is
140                                    a substitute for the lifetime information. */
141 };
142
143 /* A `struct iv_class' is created for each biv.  */
144
145 struct iv_class {
146   unsigned int regno;           /* Pseudo reg which is the biv.  */
147   int biv_count;                /* Number of insns setting this reg.  */
148   struct induction *biv;        /* List of all insns that set this reg.  */
149   int giv_count;                /* Number of DEST_REG givs computed from this
150                                    biv.  The resulting count is only used in
151                                    check_dbra_loop.  */
152   struct induction *giv;        /* List of all insns that compute a giv
153                                    from this reg.  */
154   int total_benefit;            /* Sum of BENEFITs of all those givs */
155   rtx initial_value;            /* Value of reg at loop start */
156   rtx initial_test;             /* Test performed on BIV before loop */
157   struct iv_class *next;        /* Links all class structures together */
158   rtx init_insn;                /* insn which initializes biv, 0 if none. */
159   rtx init_set;                 /* SET of INIT_INSN, if any. */
160   unsigned incremented : 1;     /* 1 if somewhere incremented/decremented */
161   unsigned eliminable : 1;      /* 1 if plausible candidate for elimination. */
162   unsigned nonneg : 1;          /* 1 if we added a REG_NONNEG note for this. */
163   unsigned reversed : 1;        /* 1 if we reversed the loop that this
164                                    biv controls. */
165 };
166
167
168 typedef struct loop_mem_info
169 {
170   rtx mem;      /* The MEM itself.  */
171   rtx reg;      /* Corresponding pseudo, if any.  */
172   int optimize; /* Nonzero if we can optimize access to this MEM.  */
173 } loop_mem_info;
174
175
176 /* Information required to calculate the number of loop iterations. 
177    This is set by loop_iterations.  */
178
179 struct loop_info
180 {
181   /* Nonzero if there is a subroutine call in the current loop.  */
182   int has_call;
183   /* Nonzero if there is a volatile memory reference in the current
184      loop.  */
185   int has_volatile;
186   /* Nonzero if there is a tablejump in the current loop.  */
187   int has_tablejump;
188   /* Nonzero if there are ways to leave the loop other than falling
189      off the end.  */
190   int has_multiple_exit_targets;
191   /* Nonzero if there is an indirect jump in the current function.  */
192   int has_indirect_jump;
193   /* Register or constant initial loop value.  */
194   rtx initial_value;
195   /* Register or constant value used for comparison test.  */
196   rtx comparison_value;
197   /* Register or constant approximate final value.  */
198   rtx final_value;
199   /* Register or constant initial loop value with term common to
200      final_value removed.  */
201   rtx initial_equiv_value;
202   /* Register or constant final loop value with term common to
203      initial_value removed.  */
204   rtx final_equiv_value;
205   /* Register corresponding to iteration variable.  */
206   rtx iteration_var;
207   /* Constant loop increment.  */
208   rtx increment;
209   enum rtx_code comparison_code;
210   /* Holds the number of loop iterations.  It is zero if the number
211      could not be calculated.  Must be unsigned since the number of
212      iterations can be as high as 2^wordsize - 1.  For loops with a
213      wider iterator, this number will be zero if the number of loop
214      iterations is too large for an unsigned integer to hold.  */
215   unsigned HOST_WIDE_INT n_iterations;
216   /* The number of times the loop body was unrolled.  */
217   unsigned int unroll_number;
218   int used_count_register;
219   /* The loop iterator induction variable.  */
220   struct iv_class *iv;
221   /* List of MEMs that are stored in this loop.  */
222   rtx store_mems;
223   /* Array of MEMs that are used (read or written) in this loop, but
224      cannot be aliased by anything in this loop, except perhaps
225      themselves.  In other words, if mems[i] is altered during
226      the loop, it is altered by an expression that is rtx_equal_p to
227      it.  */
228   loop_mem_info *mems;
229   /* The index of the next available slot in MEMS.  */
230   int mems_idx;
231   /* The number of elements allocated in MEMS.  */
232   int mems_allocated;
233   /* Nonzero if we don't know what MEMs were changed in the current
234      loop.  This happens if the loop contains a call (in which case
235      `has_call' will also be set) or if we store into more than
236      NUM_STORES MEMs.  */
237   int unknown_address_altered;
238   /* The above doesn't count any readonly memory locations that are
239      stored.  This does.  */
240   int unknown_constant_address_altered;
241   /* Count of memory write instructions discovered in the loop.  */
242   int num_mem_sets;
243   /* The insn where the first of these was found.  */
244   rtx first_loop_store_insn;
245 };
246
247 /* Definitions used by the basic induction variable discovery code.  */
248 enum iv_mode { UNKNOWN_INDUCT, BASIC_INDUCT, NOT_BASIC_INDUCT,
249                  GENERAL_INDUCT };
250
251 /* Variables declared in loop.c, but also needed in unroll.c.  */
252
253 extern int *uid_luid;
254 extern int max_uid_for_loop;
255 extern unsigned int max_reg_before_loop;
256 extern struct loop **uid_loop;
257 extern FILE *loop_dump_stream;
258
259 extern varray_type reg_iv_type;
260 extern varray_type reg_iv_info;
261
262 #define REG_IV_TYPE(n) \
263   (*(enum iv_mode *) &VARRAY_INT(reg_iv_type, (n)))
264 #define REG_IV_INFO(n) \
265   (*(struct induction **) &VARRAY_GENERIC_PTR(reg_iv_info, (n)))
266
267 extern struct iv_class **reg_biv_class;
268 extern struct iv_class *loop_iv_list;
269
270 extern unsigned int first_increment_giv, last_increment_giv;
271
272 /* Forward declarations for non-static functions declared in loop.c and
273    unroll.c.  */
274 int loop_invariant_p PARAMS ((const struct loop *, rtx));
275 rtx get_condition_for_loop PARAMS ((const struct loop *, rtx));
276 void emit_iv_add_mult PARAMS ((rtx, rtx, rtx, rtx, rtx));
277 rtx express_from PARAMS ((struct induction *, struct induction *));
278 rtx extend_value_for_giv PARAMS ((struct induction *, rtx));
279
280 void unroll_loop PARAMS ((struct loop *, int, rtx, int));
281 rtx biv_total_increment PARAMS ((struct iv_class *));
282 unsigned HOST_WIDE_INT loop_iterations PARAMS ((struct loop *));
283 int precondition_loop_p PARAMS ((const struct loop *,
284                                rtx *, rtx *, rtx *, 
285                                enum machine_mode *mode));
286 rtx final_biv_value PARAMS ((const struct loop *, struct iv_class *));
287 rtx final_giv_value PARAMS ((const struct loop *, struct induction *));
288 void emit_unrolled_add PARAMS ((rtx, rtx, rtx));
289 int back_branch_in_range_p PARAMS ((const struct loop *, rtx));
290
291 int loop_insn_first_p PARAMS ((rtx, rtx));
292 typedef rtx (*loop_insn_callback) PARAMS ((struct loop *, rtx, int, int));
293 void for_each_insn_in_loop PARAMS ((struct loop *, loop_insn_callback));
294
295 /* Forward declarations for non-static functions declared in doloop.c.  */
296 int doloop_optimize PARAMS ((const struct loop *));