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