OSDN Git Service

* integrate.c (copy_rtx_and_substitute): Fix error last change:
[pf3gnuchains/gcc-fork.git] / gcc / loop.h
1 /* Loop optimization definitions for GNU C-Compiler
2    Copyright (C) 1991, 1995, 1998, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "bitmap.h"
23 #include "sbitmap.h"
24 #include "hard-reg-set.h"
25 #include "basic-block.h"
26
27 /* Flags passed to loop_optimize.  */
28 #define LOOP_UNROLL 1
29 #define LOOP_BCT 2
30 #define LOOP_PREFETCH 4
31
32 /* Get the loop info pointer of a loop.  */
33 #define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux)
34
35 /* Get a pointer to the loop movables structure.  */
36 #define LOOP_MOVABLES(LOOP) (&LOOP_INFO (loop)->movables)
37
38 /* Get a pointer to the loop registers structure.  */
39 #define LOOP_REGS(LOOP) (&LOOP_INFO (loop)->regs)
40
41 /* Get a pointer to the loop induction variables structure.  */
42 #define LOOP_IVS(LOOP) (&LOOP_INFO (loop)->ivs)
43
44 /* Get the luid of an insn.  Catch the error of trying to reference the LUID
45    of an insn added during loop, since these don't have LUIDs.  */
46
47 #define INSN_LUID(INSN)                 \
48   (INSN_UID (INSN) < max_uid_for_loop ? uid_luid[INSN_UID (INSN)] \
49    : (abort (), -1))
50
51 #define REGNO_FIRST_LUID(REGNO) uid_luid[REGNO_FIRST_UID (REGNO)]
52 #define REGNO_LAST_LUID(REGNO) uid_luid[REGNO_LAST_UID (REGNO)]
53
54
55 /* A "basic induction variable" or biv is a pseudo reg that is set
56    (within this loop) only by incrementing or decrementing it.  */
57 /* A "general induction variable" or giv is a pseudo reg whose
58    value is a linear function of a biv.  */
59
60 /* Bivs are recognized by `basic_induction_var';
61    Givs by `general_induction_var'.  */
62
63 /* An enum for the two different types of givs, those that are used
64    as memory addresses and those that are calculated into registers.  */
65 enum g_types
66 {
67   DEST_ADDR,
68   DEST_REG
69 };
70
71
72 /* A `struct induction' is created for every instruction that sets
73    an induction variable (either a biv or a giv).  */
74
75 struct induction
76 {
77   rtx insn;                     /* The insn that sets a biv or giv */
78   rtx new_reg;                  /* New register, containing strength reduced
79                                    version of this giv.  */
80   rtx src_reg;                  /* Biv from which this giv is computed.
81                                    (If this is a biv, then this is the biv.) */
82   enum g_types giv_type;        /* Indicate whether DEST_ADDR or DEST_REG */
83   rtx dest_reg;                 /* Destination register for insn: this is the
84                                    register which was the biv or giv.
85                                    For a biv, this equals src_reg.
86                                    For a DEST_ADDR type giv, this is 0.  */
87   rtx *location;                /* Place in the insn where this giv occurs.
88                                    If GIV_TYPE is DEST_REG, this is 0.  */
89                                 /* For a biv, this is the place where add_val
90                                    was found.  */
91   enum machine_mode mode;       /* The mode of this biv or giv */
92   rtx mem;                      /* For DEST_ADDR, the memory object.  */
93   rtx mult_val;                 /* Multiplicative factor for src_reg.  */
94   rtx add_val;                  /* Additive constant for that product.  */
95   int benefit;                  /* Gain from eliminating this insn.  */
96   rtx final_value;              /* If the giv is used outside the loop, and its
97                                    final value could be calculated, it is put
98                                    here, and the giv is made replaceable.  Set
99                                    the giv to this value before the loop.  */
100   unsigned combined_with;       /* The number of givs this giv has been
101                                    combined with.  If nonzero, this giv
102                                    cannot combine with any other giv.  */
103   unsigned replaceable : 1;     /* 1 if we can substitute the strength-reduced
104                                    variable for the original variable.
105                                    0 means they must be kept separate and the
106                                    new one must be copied into the old pseudo
107                                    reg each time the old one is set.  */
108   unsigned not_replaceable : 1; /* Used to prevent duplicating work.  This is
109                                    1 if we know that the giv definitely can
110                                    not be made replaceable, in which case we
111                                    don't bother checking the variable again
112                                    even if further info is available.
113                                    Both this and the above can be zero.  */
114   unsigned ignore : 1;          /* 1 prohibits further processing of giv */
115   unsigned always_computable : 1;/* 1 if this value is computable every
116                                     iteration.  */
117   unsigned always_executed : 1; /* 1 if this set occurs each iteration.  */
118   unsigned maybe_multiple : 1;  /* Only used for a biv and  1 if this biv
119                                    update may be done multiple times per
120                                    iteration.  */
121   unsigned cant_derive : 1;     /* For giv's, 1 if this giv cannot derive
122                                    another giv.  This occurs in many cases
123                                    where a giv's lifetime spans an update to
124                                    a biv.  */
125   unsigned maybe_dead : 1;      /* 1 if this giv might be dead.  In that case,
126                                    we won't use it to eliminate a biv, it
127                                    would probably lose.  */
128   unsigned auto_inc_opt : 1;    /* 1 if this giv had its increment output next
129                                    to it to try to form an auto-inc address.  */
130   unsigned unrolled : 1;        /* 1 if new register has been allocated and
131                                    initialized in unrolled loop.  */
132   unsigned shared : 1;
133   unsigned no_const_addval : 1; /* 1 if add_val does not contain a const.  */
134   int lifetime;                 /* Length of life of this giv */
135   rtx derive_adjustment;        /* If nonzero, is an adjustment to be
136                                    subtracted from add_val when this giv
137                                    derives another.  This occurs when the
138                                    giv spans a biv update by incrementation.  */
139   rtx ext_dependent;            /* If nonzero, is a sign or zero extension
140                                    if a biv on which this giv is dependent.  */
141   struct induction *next_iv;    /* For givs, links together all givs that are
142                                    based on the same biv.  For bivs, links
143                                    together all biv entries that refer to the
144                                    same biv register.  */
145   struct induction *same;       /* If this giv has been combined with another
146                                    giv, this points to the base giv.  The base
147                                    giv will have COMBINED_WITH non-zero.  */
148   HOST_WIDE_INT const_adjust;   /* Used by loop unrolling, when an address giv
149                                    is split, and a constant is eliminated from
150                                    the address, the -constant is stored here
151                                    for later use.  */
152   struct induction *same_insn;  /* If there are multiple identical givs in
153                                    the same insn, then all but one have this
154                                    field set, and they all point to the giv
155                                    that doesn't have this field set.  */
156   rtx last_use;                 /* For a giv made from a biv increment, this is
157                                    a substitute for the lifetime information.  */
158 };
159
160
161 /* A `struct iv_class' is created for each biv.  */
162
163 struct iv_class
164 {
165   unsigned int regno;           /* Pseudo reg which is the biv.  */
166   int biv_count;                /* Number of insns setting this reg.  */
167   struct induction *biv;        /* List of all insns that set this reg.  */
168   int giv_count;                /* Number of DEST_REG givs computed from this
169                                    biv.  The resulting count is only used in
170                                    check_dbra_loop.  */
171   struct induction *giv;        /* List of all insns that compute a giv
172                                    from this reg.  */
173   int total_benefit;            /* Sum of BENEFITs of all those givs.  */
174   rtx initial_value;            /* Value of reg at loop start.  */
175   rtx initial_test;             /* Test performed on BIV before loop.  */
176   rtx final_value;              /* Value of reg at loop end, if known.  */
177   struct iv_class *next;        /* Links all class structures together.  */
178   rtx init_insn;                /* insn which initializes biv, 0 if none.  */
179   rtx init_set;                 /* SET of INIT_INSN, if any.  */
180   unsigned incremented : 1;     /* 1 if somewhere incremented/decremented */
181   unsigned eliminable : 1;      /* 1 if plausible candidate for
182                                    elimination.  */
183   unsigned nonneg : 1;          /* 1 if we added a REG_NONNEG note for
184                                    this.  */
185   unsigned reversed : 1;        /* 1 if we reversed the loop that this
186                                    biv controls.  */
187   unsigned all_reduced : 1;     /* 1 if all givs using this biv have
188                                    been reduced.  */
189 };
190
191
192 /* Definitions used by the basic induction variable discovery code.  */
193 enum iv_mode
194 {
195   UNKNOWN_INDUCT,
196   BASIC_INDUCT,
197   NOT_BASIC_INDUCT,
198   GENERAL_INDUCT
199 };
200
201
202 /* A `struct iv' is created for every register.  */
203
204 struct iv
205 {
206   enum iv_mode type;
207   union 
208   {
209     struct iv_class *class;
210     struct induction *info;
211   } iv;
212 };
213
214
215 #define REG_IV_TYPE(ivs, n) ivs->regs[n].type
216 #define REG_IV_INFO(ivs, n) ivs->regs[n].iv.info
217 #define REG_IV_CLASS(ivs, n) ivs->regs[n].iv.class
218
219
220 struct loop_ivs
221 {
222   /* Indexed by register number, contains pointer to `struct
223      iv' if register is an induction variable.  */
224   struct iv *regs;
225
226   /* Size of regs array.  */
227   unsigned int n_regs;
228
229   /* The head of a list which links together (via the next field)
230      every iv class for the current loop.  */
231   struct iv_class *list;
232 };
233
234
235 typedef struct loop_mem_info
236 {
237   rtx mem;      /* The MEM itself.  */
238   rtx reg;      /* Corresponding pseudo, if any.  */
239   int optimize; /* Nonzero if we can optimize access to this MEM.  */
240 } loop_mem_info;
241
242
243
244 struct loop_reg
245 {
246   /* Number of times the reg is set during the loop being scanned.
247      During code motion, a negative value indicates a reg that has
248      been made a candidate; in particular -2 means that it is an
249      candidate that we know is equal to a constant and -1 means that
250      it is an candidate not known equal to a constant.  After code
251      motion, regs moved have 0 (which is accurate now) while the
252      failed candidates have the original number of times set.
253
254      Therefore, at all times, == 0 indicates an invariant register;
255      < 0 a conditionally invariant one.  */
256   int set_in_loop;
257
258   /* Original value of set_in_loop; same except that this value
259      is not set negative for a reg whose sets have been made candidates
260      and not set to 0 for a reg that is moved.  */
261   int n_times_set;
262
263   /* Contains the insn in which a register was used if it was used
264      exactly once; contains const0_rtx if it was used more than once.  */
265   rtx single_usage;
266
267   /* Nonzero indicates that the register cannot be moved or strength
268      reduced.  */
269   char may_not_optimize;
270
271   /* Nonzero means reg N has already been moved out of one loop.
272      This reduces the desire to move it out of another.  */
273   char moved_once;
274 };
275
276
277 struct loop_regs
278 {
279   int num;                      /* Number of regs used in table.  */
280   int size;                     /* Size of table.  */
281   struct loop_reg *array;       /* Register usage info. array.  */
282   int multiple_uses;            /* Nonzero if a reg has multiple uses.  */
283 };
284
285
286
287 struct loop_movables
288 {
289   /* Head of movable chain.  */
290   struct movable *head;
291   /* Last movable in chain.  */
292   struct movable *last;
293 };
294
295
296 /* Information pertaining to a loop.  */
297
298 struct loop_info
299 {
300   /* Nonzero if there is a subroutine call in the current loop.  */
301   int has_call;
302   /* Nonzero if there is a libcall in the current loop.  */
303   int has_libcall;
304   /* Nonzero if there is a non constant call in the current loop.  */
305   int has_nonconst_call;
306   /* Nonzero if there is a volatile memory reference in the current
307      loop.  */
308   int has_volatile;
309   /* Nonzero if there is a tablejump in the current loop.  */
310   int has_tablejump;
311   /* Nonzero if there are ways to leave the loop other than falling
312      off the end.  */
313   int has_multiple_exit_targets;
314   /* Nonzero if there is an indirect jump in the current function.  */
315   int has_indirect_jump;
316   /* Register or constant initial loop value.  */
317   rtx initial_value;
318   /* Register or constant value used for comparison test.  */
319   rtx comparison_value;
320   /* Register or constant approximate final value.  */
321   rtx final_value;
322   /* Register or constant initial loop value with term common to
323      final_value removed.  */
324   rtx initial_equiv_value;
325   /* Register or constant final loop value with term common to
326      initial_value removed.  */
327   rtx final_equiv_value;
328   /* Register corresponding to iteration variable.  */
329   rtx iteration_var;
330   /* Constant loop increment.  */
331   rtx increment;
332   enum rtx_code comparison_code;
333   /* Holds the number of loop iterations.  It is zero if the number
334      could not be calculated.  Must be unsigned since the number of
335      iterations can be as high as 2^wordsize - 1.  For loops with a
336      wider iterator, this number will be zero if the number of loop
337      iterations is too large for an unsigned integer to hold.  */
338   unsigned HOST_WIDE_INT n_iterations;
339   /* The number of times the loop body was unrolled.  */
340   unsigned int unroll_number;
341   int used_count_register;
342   /* The loop iterator induction variable.  */
343   struct iv_class *iv;
344   /* List of MEMs that are stored in this loop.  */
345   rtx store_mems;
346   /* Array of MEMs that are used (read or written) in this loop, but
347      cannot be aliased by anything in this loop, except perhaps
348      themselves.  In other words, if mems[i] is altered during
349      the loop, it is altered by an expression that is rtx_equal_p to
350      it.  */
351   loop_mem_info *mems;
352   /* The index of the next available slot in MEMS.  */
353   int mems_idx;
354   /* The number of elements allocated in MEMS.  */
355   int mems_allocated;
356   /* Nonzero if we don't know what MEMs were changed in the current
357      loop.  This happens if the loop contains a call (in which case
358      `has_call' will also be set) or if we store into more than
359      NUM_STORES MEMs.  */
360   int unknown_address_altered;
361   /* The above doesn't count any readonly memory locations that are
362      stored.  This does.  */
363   int unknown_constant_address_altered;
364   /* Count of memory write instructions discovered in the loop.  */
365   int num_mem_sets;
366   /* The insn where the first of these was found.  */
367   rtx first_loop_store_insn;
368   /* The chain of movable insns in loop.  */
369   struct loop_movables movables;
370   /* The registers used the in loop.  */
371   struct loop_regs regs;
372   /* The induction variable information in loop.  */
373   struct loop_ivs ivs;
374   /* Non-zero if call is in pre_header extended basic block.  */
375   int pre_header_has_call;
376 };
377
378
379 /* Variables declared in loop.c, but also needed in unroll.c.  */
380
381 extern int *uid_luid;
382 extern int max_uid_for_loop;
383 extern unsigned int max_reg_before_loop;
384 extern struct loop **uid_loop;
385 extern FILE *loop_dump_stream;
386
387
388 /* Forward declarations for non-static functions declared in loop.c and
389    unroll.c.  */
390 int loop_invariant_p PARAMS ((const struct loop *, rtx));
391 rtx get_condition_for_loop PARAMS ((const struct loop *, rtx));
392 void loop_iv_add_mult_hoist PARAMS ((const struct loop *, rtx, rtx, rtx, rtx));
393 void loop_iv_add_mult_sink PARAMS ((const struct loop *, rtx, rtx, rtx, rtx));
394 void loop_iv_add_mult_emit_before PARAMS ((const struct loop *, rtx, 
395                                            rtx, rtx, rtx,
396                                            basic_block, rtx));
397 rtx express_from PARAMS ((struct induction *, struct induction *));
398 rtx extend_value_for_giv PARAMS ((struct induction *, rtx));
399
400 void unroll_loop PARAMS ((struct loop *, int, int));
401 rtx biv_total_increment PARAMS ((const struct iv_class *));
402 unsigned HOST_WIDE_INT loop_iterations PARAMS ((struct loop *));
403 int precondition_loop_p PARAMS ((const struct loop *,
404                                  rtx *, rtx *, rtx *,
405                                  enum machine_mode *mode));
406 rtx final_biv_value PARAMS ((const struct loop *, struct iv_class *));
407 rtx final_giv_value PARAMS ((const struct loop *, struct induction *));
408 void emit_unrolled_add PARAMS ((rtx, rtx, rtx));
409 int back_branch_in_range_p PARAMS ((const struct loop *, rtx));
410
411 int loop_insn_first_p PARAMS ((rtx, rtx));
412 typedef rtx (*loop_insn_callback) PARAMS ((struct loop *, rtx, int, int));
413 void for_each_insn_in_loop PARAMS ((struct loop *, loop_insn_callback));
414 rtx loop_insn_emit_before PARAMS((const struct loop *, basic_block, 
415                                   rtx, rtx));
416 rtx loop_insn_sink PARAMS((const struct loop *, rtx));
417 rtx loop_insn_hoist PARAMS((const struct loop *, rtx));
418
419 /* Forward declarations for non-static functions declared in doloop.c.  */
420 int doloop_optimize PARAMS ((const struct loop *));