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