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)
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. */
24 /* Flags passed to loop_optimize. */
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)] \
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. */
60 /* A `struct induction' is created for every instruction that sets
61 an induction variable (either a biv or a giv). */
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
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
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
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
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. */
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 HOST_WIDE_INT const_adjust
; /* Used by loop unrolling, when an address giv
137 is split, and a constant is eliminated from
138 the address, the -constant is stored here
140 struct induction
*same_insn
; /* If there are multiple identical givs in
141 the same insn, then all but one have this
142 field set, and they all point to the giv
143 that doesn't have this field set. */
144 rtx last_use
; /* For a giv made from a biv increment, this is
145 a substitute for the lifetime information. */
148 /* A `struct iv_class' is created for each biv. */
152 unsigned int regno
; /* Pseudo reg which is the biv. */
153 int biv_count
; /* Number of insns setting this reg. */
154 struct induction
*biv
; /* List of all insns that set this reg. */
155 int giv_count
; /* Number of DEST_REG givs computed from this
156 biv. The resulting count is only used in
158 struct induction
*giv
; /* List of all insns that compute a giv
160 int total_benefit
; /* Sum of BENEFITs of all those givs */
161 rtx initial_value
; /* Value of reg at loop start */
162 rtx initial_test
; /* Test performed on BIV before loop */
163 struct iv_class
*next
; /* Links all class structures together */
164 rtx init_insn
; /* insn which initializes biv, 0 if none. */
165 rtx init_set
; /* SET of INIT_INSN, if any. */
166 unsigned incremented
: 1; /* 1 if somewhere incremented/decremented */
167 unsigned eliminable
: 1; /* 1 if plausible candidate for elimination. */
168 unsigned nonneg
: 1; /* 1 if we added a REG_NONNEG note for this. */
169 unsigned reversed
: 1; /* 1 if we reversed the loop that this
173 typedef struct loop_mem_info
175 rtx mem
; /* The MEM itself. */
176 rtx reg
; /* Corresponding pseudo, if any. */
177 int optimize
; /* Nonzero if we can optimize access to this MEM. */
182 /* Indexed by register number, indicates whether or not register is
183 an induction variable, and if so what type. */
184 varray_type reg_iv_type
;
186 /* Indexed by register number, contains pointer to `struct
187 induction' if register is an induction variable. This holds
188 general info for all induction variables. */
189 varray_type reg_iv_info
;
191 /* Indexed by register number, contains pointer to `struct iv_class'
192 if register is a basic induction variable. This holds info
193 describing the class (a related group) of induction variables
194 that the biv belongs to. */
195 struct iv_class
**reg_biv_class
;
197 /* The head of a list which links together (via the next field)
198 every iv class for the current loop. */
199 struct iv_class
*loop_iv_list
;
206 /* Indexed by register number, contains the number of times the reg
207 is set during the loop being scanned.
208 During code motion, a negative value indicates a reg that has been
209 made a candidate; in particular -2 means that it is an candidate that
210 we know is equal to a constant and -1 means that it is an candidate
211 not known equal to a constant.
212 After code motion, regs moved have 0 (which is accurate now)
213 while the failed candidates have the original number of times set.
215 Therefore, at all times, == 0 indicates an invariant register;
216 < 0 a conditionally invariant one. */
217 varray_type set_in_loop
;
219 /* Original value of set_in_loop; same except that this value
220 is not set negative for a reg whose sets have been made candidates
221 and not set to 0 for a reg that is moved. */
222 varray_type n_times_set
;
224 /* Index by register number, 1 indicates that the register
225 cannot be moved or strength reduced. */
226 varray_type may_not_optimize
;
228 /* Contains the insn in which a register was used if it was used
229 exactly once; contains const0_rtx if it was used more than once. */
230 varray_type single_usage
;
232 /* Nonzero means reg N has already been moved out of one loop.
233 This reduces the desire to move it out of another. */
239 /* Information pertaining to a loop. */
243 /* Nonzero if there is a subroutine call in the current loop. */
245 /* Nonzero if there is a volatile memory reference in the current
248 /* Nonzero if there is a tablejump in the current loop. */
250 /* Nonzero if there are ways to leave the loop other than falling
252 int has_multiple_exit_targets
;
253 /* Nonzero if there is an indirect jump in the current function. */
254 int has_indirect_jump
;
255 /* Register or constant initial loop value. */
257 /* Register or constant value used for comparison test. */
258 rtx comparison_value
;
259 /* Register or constant approximate final value. */
261 /* Register or constant initial loop value with term common to
262 final_value removed. */
263 rtx initial_equiv_value
;
264 /* Register or constant final loop value with term common to
265 initial_value removed. */
266 rtx final_equiv_value
;
267 /* Register corresponding to iteration variable. */
269 /* Constant loop increment. */
271 enum rtx_code comparison_code
;
272 /* Holds the number of loop iterations. It is zero if the number
273 could not be calculated. Must be unsigned since the number of
274 iterations can be as high as 2^wordsize - 1. For loops with a
275 wider iterator, this number will be zero if the number of loop
276 iterations is too large for an unsigned integer to hold. */
277 unsigned HOST_WIDE_INT n_iterations
;
278 /* The number of times the loop body was unrolled. */
279 unsigned int unroll_number
;
280 int used_count_register
;
281 /* The loop iterator induction variable. */
283 /* List of MEMs that are stored in this loop. */
285 /* Array of MEMs that are used (read or written) in this loop, but
286 cannot be aliased by anything in this loop, except perhaps
287 themselves. In other words, if mems[i] is altered during
288 the loop, it is altered by an expression that is rtx_equal_p to
291 /* The index of the next available slot in MEMS. */
293 /* The number of elements allocated in MEMS. */
295 /* Nonzero if we don't know what MEMs were changed in the current
296 loop. This happens if the loop contains a call (in which case
297 `has_call' will also be set) or if we store into more than
299 int unknown_address_altered
;
300 /* The above doesn't count any readonly memory locations that are
301 stored. This does. */
302 int unknown_constant_address_altered
;
303 /* Count of memory write instructions discovered in the loop. */
305 /* The insn where the first of these was found. */
306 rtx first_loop_store_insn
;
307 /* The registers used the in loop. */
308 struct loop_regs regs
;
309 /* The induction variable information in loop. */
313 /* Definitions used by the basic induction variable discovery code. */
322 /* Variables declared in loop.c, but also needed in unroll.c. */
324 extern int *uid_luid
;
325 extern int max_uid_for_loop
;
326 extern unsigned int max_reg_before_loop
;
327 extern struct loop
**uid_loop
;
328 extern FILE *loop_dump_stream
;
330 #define REG_IV_TYPE(ivs, n) \
331 (*(enum iv_mode *) &VARRAY_INT(ivs->reg_iv_type, (n)))
332 #define REG_IV_INFO(ivs, n) \
333 (*(struct induction **) &VARRAY_GENERIC_PTR(ivs->reg_iv_info, (n)))
335 /* Forward declarations for non-static functions declared in loop.c and
337 int loop_invariant_p
PARAMS ((const struct loop
*, rtx
));
338 rtx get_condition_for_loop
PARAMS ((const struct loop
*, rtx
));
339 void emit_iv_add_mult
PARAMS ((rtx
, rtx
, rtx
, rtx
, rtx
));
340 rtx express_from
PARAMS ((struct induction
*, struct induction
*));
341 rtx extend_value_for_giv
PARAMS ((struct induction
*, rtx
));
343 void unroll_loop
PARAMS ((struct loop
*, int, rtx
, int));
344 rtx biv_total_increment
PARAMS ((struct iv_class
*));
345 unsigned HOST_WIDE_INT loop_iterations
PARAMS ((struct loop
*));
346 int precondition_loop_p
PARAMS ((const struct loop
*,
348 enum machine_mode
*mode
));
349 rtx final_biv_value
PARAMS ((const struct loop
*, struct iv_class
*));
350 rtx final_giv_value
PARAMS ((const struct loop
*, struct induction
*));
351 void emit_unrolled_add
PARAMS ((rtx
, rtx
, rtx
));
352 int back_branch_in_range_p
PARAMS ((const struct loop
*, rtx
));
354 int loop_insn_first_p
PARAMS ((rtx
, rtx
));
355 typedef rtx (*loop_insn_callback
) PARAMS ((struct loop
*, rtx
, int, int));
356 void for_each_insn_in_loop
PARAMS ((struct loop
*, loop_insn_callback
));
358 /* Forward declarations for non-static functions declared in doloop.c. */
359 int doloop_optimize
PARAMS ((const struct loop
*));