OSDN Git Service

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