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