OSDN Git Service

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