alpha.c (alpha_gimplify_va_arg_1): Use build_va_arg_indirect_ref.
[official-gcc.git] / gcc / loop.c
blobae06307bca9c8becbe4be107893ec14acef759aa
1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This is the loop optimization pass of the compiler.
24 It finds invariant computations within loops and moves them
25 to the beginning of the loop. Then it identifies basic and
26 general induction variables.
28 Basic induction variables (BIVs) are a pseudo registers which are set within
29 a loop only by incrementing or decrementing its value. General induction
30 variables (GIVs) are pseudo registers with a value which is a linear function
31 of a basic induction variable. BIVs are recognized by `basic_induction_var';
32 GIVs by `general_induction_var'.
34 Once induction variables are identified, strength reduction is applied to the
35 general induction variables, and induction variable elimination is applied to
36 the basic induction variables.
38 It also finds cases where
39 a register is set within the loop by zero-extending a narrower value
40 and changes these to zero the entire register once before the loop
41 and merely copy the low part within the loop.
43 Most of the complexity is in heuristics to decide when it is worth
44 while to do these things. */
46 #include "config.h"
47 #include "system.h"
48 #include "coretypes.h"
49 #include "tm.h"
50 #include "rtl.h"
51 #include "tm_p.h"
52 #include "function.h"
53 #include "expr.h"
54 #include "hard-reg-set.h"
55 #include "basic-block.h"
56 #include "insn-config.h"
57 #include "regs.h"
58 #include "recog.h"
59 #include "flags.h"
60 #include "real.h"
61 #include "cselib.h"
62 #include "except.h"
63 #include "toplev.h"
64 #include "predict.h"
65 #include "insn-flags.h"
66 #include "optabs.h"
67 #include "cfgloop.h"
68 #include "ggc.h"
69 #include "timevar.h"
70 #include "tree-pass.h"
72 /* Get the loop info pointer of a loop. */
73 #define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux)
75 /* Get a pointer to the loop movables structure. */
76 #define LOOP_MOVABLES(LOOP) (&LOOP_INFO (LOOP)->movables)
78 /* Get a pointer to the loop registers structure. */
79 #define LOOP_REGS(LOOP) (&LOOP_INFO (LOOP)->regs)
81 /* Get a pointer to the loop induction variables structure. */
82 #define LOOP_IVS(LOOP) (&LOOP_INFO (LOOP)->ivs)
84 /* Get the luid of an insn. Catch the error of trying to reference the LUID
85 of an insn added during loop, since these don't have LUIDs. */
87 #define INSN_LUID(INSN) \
88 (gcc_assert (INSN_UID (INSN) < max_uid_for_loop), uid_luid[INSN_UID (INSN)])
90 #define REGNO_FIRST_LUID(REGNO) \
91 (REGNO_FIRST_UID (REGNO) < max_uid_for_loop \
92 ? uid_luid[REGNO_FIRST_UID (REGNO)] \
93 : 0)
94 #define REGNO_LAST_LUID(REGNO) \
95 (REGNO_LAST_UID (REGNO) < max_uid_for_loop \
96 ? uid_luid[REGNO_LAST_UID (REGNO)] \
97 : INT_MAX)
99 /* A "basic induction variable" or biv is a pseudo reg that is set
100 (within this loop) only by incrementing or decrementing it. */
101 /* A "general induction variable" or giv is a pseudo reg whose
102 value is a linear function of a biv. */
104 /* Bivs are recognized by `basic_induction_var';
105 Givs by `general_induction_var'. */
107 /* An enum for the two different types of givs, those that are used
108 as memory addresses and those that are calculated into registers. */
109 enum g_types
111 DEST_ADDR,
112 DEST_REG
116 /* A `struct induction' is created for every instruction that sets
117 an induction variable (either a biv or a giv). */
119 struct induction
121 rtx insn; /* The insn that sets a biv or giv */
122 rtx new_reg; /* New register, containing strength reduced
123 version of this giv. */
124 rtx src_reg; /* Biv from which this giv is computed.
125 (If this is a biv, then this is the biv.) */
126 enum g_types giv_type; /* Indicate whether DEST_ADDR or DEST_REG */
127 rtx dest_reg; /* Destination register for insn: this is the
128 register which was the biv or giv.
129 For a biv, this equals src_reg.
130 For a DEST_ADDR type giv, this is 0. */
131 rtx *location; /* Place in the insn where this giv occurs.
132 If GIV_TYPE is DEST_REG, this is 0. */
133 /* For a biv, this is the place where add_val
134 was found. */
135 enum machine_mode mode; /* The mode of this biv or giv */
136 rtx mem; /* For DEST_ADDR, the memory object. */
137 rtx mult_val; /* Multiplicative factor for src_reg. */
138 rtx add_val; /* Additive constant for that product. */
139 int benefit; /* Gain from eliminating this insn. */
140 rtx final_value; /* If the giv is used outside the loop, and its
141 final value could be calculated, it is put
142 here, and the giv is made replaceable. Set
143 the giv to this value before the loop. */
144 unsigned combined_with; /* The number of givs this giv has been
145 combined with. If nonzero, this giv
146 cannot combine with any other giv. */
147 unsigned replaceable : 1; /* 1 if we can substitute the strength-reduced
148 variable for the original variable.
149 0 means they must be kept separate and the
150 new one must be copied into the old pseudo
151 reg each time the old one is set. */
152 unsigned not_replaceable : 1; /* Used to prevent duplicating work. This is
153 1 if we know that the giv definitely can
154 not be made replaceable, in which case we
155 don't bother checking the variable again
156 even if further info is available.
157 Both this and the above can be zero. */
158 unsigned ignore : 1; /* 1 prohibits further processing of giv */
159 unsigned always_computable : 1;/* 1 if this value is computable every
160 iteration. */
161 unsigned always_executed : 1; /* 1 if this set occurs each iteration. */
162 unsigned maybe_multiple : 1; /* Only used for a biv and 1 if this biv
163 update may be done multiple times per
164 iteration. */
165 unsigned cant_derive : 1; /* For giv's, 1 if this giv cannot derive
166 another giv. This occurs in many cases
167 where a giv's lifetime spans an update to
168 a biv. */
169 unsigned maybe_dead : 1; /* 1 if this giv might be dead. In that case,
170 we won't use it to eliminate a biv, it
171 would probably lose. */
172 unsigned auto_inc_opt : 1; /* 1 if this giv had its increment output next
173 to it to try to form an auto-inc address. */
174 unsigned shared : 1;
175 unsigned no_const_addval : 1; /* 1 if add_val does not contain a const. */
176 int lifetime; /* Length of life of this giv */
177 rtx derive_adjustment; /* If nonzero, is an adjustment to be
178 subtracted from add_val when this giv
179 derives another. This occurs when the
180 giv spans a biv update by incrementation. */
181 rtx ext_dependent; /* If nonzero, is a sign or zero extension
182 if a biv on which this giv is dependent. */
183 struct induction *next_iv; /* For givs, links together all givs that are
184 based on the same biv. For bivs, links
185 together all biv entries that refer to the
186 same biv register. */
187 struct induction *same; /* For givs, if the giv has been combined with
188 another giv, this points to the base giv.
189 The base giv will have COMBINED_WITH nonzero.
190 For bivs, if the biv has the same LOCATION
191 than another biv, this points to the base
192 biv. */
193 struct induction *same_insn; /* If there are multiple identical givs in
194 the same insn, then all but one have this
195 field set, and they all point to the giv
196 that doesn't have this field set. */
197 rtx last_use; /* For a giv made from a biv increment, this is
198 a substitute for the lifetime information. */
202 /* A `struct iv_class' is created for each biv. */
204 struct iv_class
206 unsigned int regno; /* Pseudo reg which is the biv. */
207 int biv_count; /* Number of insns setting this reg. */
208 struct induction *biv; /* List of all insns that set this reg. */
209 int giv_count; /* Number of DEST_REG givs computed from this
210 biv. The resulting count is only used in
211 check_dbra_loop. */
212 struct induction *giv; /* List of all insns that compute a giv
213 from this reg. */
214 int total_benefit; /* Sum of BENEFITs of all those givs. */
215 rtx initial_value; /* Value of reg at loop start. */
216 rtx initial_test; /* Test performed on BIV before loop. */
217 rtx final_value; /* Value of reg at loop end, if known. */
218 struct iv_class *next; /* Links all class structures together. */
219 rtx init_insn; /* insn which initializes biv, 0 if none. */
220 rtx init_set; /* SET of INIT_INSN, if any. */
221 unsigned incremented : 1; /* 1 if somewhere incremented/decremented */
222 unsigned eliminable : 1; /* 1 if plausible candidate for
223 elimination. */
224 unsigned nonneg : 1; /* 1 if we added a REG_NONNEG note for
225 this. */
226 unsigned reversed : 1; /* 1 if we reversed the loop that this
227 biv controls. */
228 unsigned all_reduced : 1; /* 1 if all givs using this biv have
229 been reduced. */
233 /* Definitions used by the basic induction variable discovery code. */
234 enum iv_mode
236 UNKNOWN_INDUCT,
237 BASIC_INDUCT,
238 NOT_BASIC_INDUCT,
239 GENERAL_INDUCT
243 /* A `struct iv' is created for every register. */
245 struct iv
247 enum iv_mode type;
248 union
250 struct iv_class *class;
251 struct induction *info;
252 } iv;
256 #define REG_IV_TYPE(ivs, n) ivs->regs[n].type
257 #define REG_IV_INFO(ivs, n) ivs->regs[n].iv.info
258 #define REG_IV_CLASS(ivs, n) ivs->regs[n].iv.class
261 struct loop_ivs
263 /* Indexed by register number, contains pointer to `struct
264 iv' if register is an induction variable. */
265 struct iv *regs;
267 /* Size of regs array. */
268 unsigned int n_regs;
270 /* The head of a list which links together (via the next field)
271 every iv class for the current loop. */
272 struct iv_class *list;
276 typedef struct loop_mem_info
278 rtx mem; /* The MEM itself. */
279 rtx reg; /* Corresponding pseudo, if any. */
280 int optimize; /* Nonzero if we can optimize access to this MEM. */
281 } loop_mem_info;
285 struct loop_reg
287 /* Number of times the reg is set during the loop being scanned.
288 During code motion, a negative value indicates a reg that has
289 been made a candidate; in particular -2 means that it is an
290 candidate that we know is equal to a constant and -1 means that
291 it is a candidate not known equal to a constant. After code
292 motion, regs moved have 0 (which is accurate now) while the
293 failed candidates have the original number of times set.
295 Therefore, at all times, == 0 indicates an invariant register;
296 < 0 a conditionally invariant one. */
297 int set_in_loop;
299 /* Original value of set_in_loop; same except that this value
300 is not set negative for a reg whose sets have been made candidates
301 and not set to 0 for a reg that is moved. */
302 int n_times_set;
304 /* Contains the insn in which a register was used if it was used
305 exactly once; contains const0_rtx if it was used more than once. */
306 rtx single_usage;
308 /* Nonzero indicates that the register cannot be moved or strength
309 reduced. */
310 char may_not_optimize;
312 /* Nonzero means reg N has already been moved out of one loop.
313 This reduces the desire to move it out of another. */
314 char moved_once;
318 struct loop_regs
320 int num; /* Number of regs used in table. */
321 int size; /* Size of table. */
322 struct loop_reg *array; /* Register usage info. array. */
323 int multiple_uses; /* Nonzero if a reg has multiple uses. */
328 struct loop_movables
330 /* Head of movable chain. */
331 struct movable *head;
332 /* Last movable in chain. */
333 struct movable *last;
337 /* Information pertaining to a loop. */
339 struct loop_info
341 /* Nonzero if there is a subroutine call in the current loop. */
342 int has_call;
343 /* Nonzero if there is a libcall in the current loop. */
344 int has_libcall;
345 /* Nonzero if there is a non constant call in the current loop. */
346 int has_nonconst_call;
347 /* Nonzero if there is a prefetch instruction in the current loop. */
348 int has_prefetch;
349 /* Nonzero if there is a volatile memory reference in the current
350 loop. */
351 int has_volatile;
352 /* Nonzero if there is a tablejump in the current loop. */
353 int has_tablejump;
354 /* Nonzero if there are ways to leave the loop other than falling
355 off the end. */
356 int has_multiple_exit_targets;
357 /* Nonzero if there is an indirect jump in the current function. */
358 int has_indirect_jump;
359 /* Register or constant initial loop value. */
360 rtx initial_value;
361 /* Register or constant value used for comparison test. */
362 rtx comparison_value;
363 /* Register or constant approximate final value. */
364 rtx final_value;
365 /* Register or constant initial loop value with term common to
366 final_value removed. */
367 rtx initial_equiv_value;
368 /* Register or constant final loop value with term common to
369 initial_value removed. */
370 rtx final_equiv_value;
371 /* Register corresponding to iteration variable. */
372 rtx iteration_var;
373 /* Constant loop increment. */
374 rtx increment;
375 enum rtx_code comparison_code;
376 /* Holds the number of loop iterations. It is zero if the number
377 could not be calculated. Must be unsigned since the number of
378 iterations can be as high as 2^wordsize - 1. For loops with a
379 wider iterator, this number will be zero if the number of loop
380 iterations is too large for an unsigned integer to hold. */
381 unsigned HOST_WIDE_INT n_iterations;
382 int used_count_register;
383 /* The loop iterator induction variable. */
384 struct iv_class *iv;
385 /* List of MEMs that are stored in this loop. */
386 rtx store_mems;
387 /* Array of MEMs that are used (read or written) in this loop, but
388 cannot be aliased by anything in this loop, except perhaps
389 themselves. In other words, if mems[i] is altered during
390 the loop, it is altered by an expression that is rtx_equal_p to
391 it. */
392 loop_mem_info *mems;
393 /* The index of the next available slot in MEMS. */
394 int mems_idx;
395 /* The number of elements allocated in MEMS. */
396 int mems_allocated;
397 /* Nonzero if we don't know what MEMs were changed in the current
398 loop. This happens if the loop contains a call (in which case
399 `has_call' will also be set) or if we store into more than
400 NUM_STORES MEMs. */
401 int unknown_address_altered;
402 /* The above doesn't count any readonly memory locations that are
403 stored. This does. */
404 int unknown_constant_address_altered;
405 /* Count of memory write instructions discovered in the loop. */
406 int num_mem_sets;
407 /* The insn where the first of these was found. */
408 rtx first_loop_store_insn;
409 /* The chain of movable insns in loop. */
410 struct loop_movables movables;
411 /* The registers used the in loop. */
412 struct loop_regs regs;
413 /* The induction variable information in loop. */
414 struct loop_ivs ivs;
415 /* Nonzero if call is in pre_header extended basic block. */
416 int pre_header_has_call;
419 /* Not really meaningful values, but at least something. */
420 #ifndef SIMULTANEOUS_PREFETCHES
421 #define SIMULTANEOUS_PREFETCHES 3
422 #endif
423 #ifndef PREFETCH_BLOCK
424 #define PREFETCH_BLOCK 32
425 #endif
426 #ifndef HAVE_prefetch
427 #define HAVE_prefetch 0
428 #define CODE_FOR_prefetch 0
429 #define gen_prefetch(a,b,c) (gcc_unreachable (), NULL_RTX)
430 #endif
432 /* Give up the prefetch optimizations once we exceed a given threshold.
433 It is unlikely that we would be able to optimize something in a loop
434 with so many detected prefetches. */
435 #define MAX_PREFETCHES 100
436 /* The number of prefetch blocks that are beneficial to fetch at once before
437 a loop with a known (and low) iteration count. */
438 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
439 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
440 since it is likely that the data are already in the cache. */
441 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
443 /* Parameterize some prefetch heuristics so they can be turned on and off
444 easily for performance testing on new architectures. These can be
445 defined in target-dependent files. */
447 /* Prefetch is worthwhile only when loads/stores are dense. */
448 #ifndef PREFETCH_ONLY_DENSE_MEM
449 #define PREFETCH_ONLY_DENSE_MEM 1
450 #endif
452 /* Define what we mean by "dense" loads and stores; This value divided by 256
453 is the minimum percentage of memory references that worth prefetching. */
454 #ifndef PREFETCH_DENSE_MEM
455 #define PREFETCH_DENSE_MEM 220
456 #endif
458 /* Do not prefetch for a loop whose iteration count is known to be low. */
459 #ifndef PREFETCH_NO_LOW_LOOPCNT
460 #define PREFETCH_NO_LOW_LOOPCNT 1
461 #endif
463 /* Define what we mean by a "low" iteration count. */
464 #ifndef PREFETCH_LOW_LOOPCNT
465 #define PREFETCH_LOW_LOOPCNT 32
466 #endif
468 /* Do not prefetch for a loop that contains a function call; such a loop is
469 probably not an internal loop. */
470 #ifndef PREFETCH_NO_CALL
471 #define PREFETCH_NO_CALL 1
472 #endif
474 /* Do not prefetch accesses with an extreme stride. */
475 #ifndef PREFETCH_NO_EXTREME_STRIDE
476 #define PREFETCH_NO_EXTREME_STRIDE 1
477 #endif
479 /* Define what we mean by an "extreme" stride. */
480 #ifndef PREFETCH_EXTREME_STRIDE
481 #define PREFETCH_EXTREME_STRIDE 4096
482 #endif
484 /* Define a limit to how far apart indices can be and still be merged
485 into a single prefetch. */
486 #ifndef PREFETCH_EXTREME_DIFFERENCE
487 #define PREFETCH_EXTREME_DIFFERENCE 4096
488 #endif
490 /* Issue prefetch instructions before the loop to fetch data to be used
491 in the first few loop iterations. */
492 #ifndef PREFETCH_BEFORE_LOOP
493 #define PREFETCH_BEFORE_LOOP 1
494 #endif
496 /* Do not handle reversed order prefetches (negative stride). */
497 #ifndef PREFETCH_NO_REVERSE_ORDER
498 #define PREFETCH_NO_REVERSE_ORDER 1
499 #endif
501 /* Prefetch even if the GIV is in conditional code. */
502 #ifndef PREFETCH_CONDITIONAL
503 #define PREFETCH_CONDITIONAL 1
504 #endif
506 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
507 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
509 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
510 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
511 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
513 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
514 ((REGNO) < FIRST_PSEUDO_REGISTER \
515 ? (int) hard_regno_nregs[(REGNO)][GET_MODE (SET_DEST)] : 1)
518 /* Vector mapping INSN_UIDs to luids.
519 The luids are like uids but increase monotonically always.
520 We use them to see whether a jump comes from outside a given loop. */
522 static int *uid_luid;
524 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
525 number the insn is contained in. */
527 static struct loop **uid_loop;
529 /* 1 + largest uid of any insn. */
531 static int max_uid_for_loop;
533 /* Number of loops detected in current function. Used as index to the
534 next few tables. */
536 static int max_loop_num;
538 /* Bound on pseudo register number before loop optimization.
539 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
540 static unsigned int max_reg_before_loop;
542 /* The value to pass to the next call of reg_scan_update. */
543 static int loop_max_reg;
545 /* During the analysis of a loop, a chain of `struct movable's
546 is made to record all the movable insns found.
547 Then the entire chain can be scanned to decide which to move. */
549 struct movable
551 rtx insn; /* A movable insn */
552 rtx set_src; /* The expression this reg is set from. */
553 rtx set_dest; /* The destination of this SET. */
554 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
555 of any registers used within the LIBCALL. */
556 int consec; /* Number of consecutive following insns
557 that must be moved with this one. */
558 unsigned int regno; /* The register it sets */
559 short lifetime; /* lifetime of that register;
560 may be adjusted when matching movables
561 that load the same value are found. */
562 short savings; /* Number of insns we can move for this reg,
563 including other movables that force this
564 or match this one. */
565 ENUM_BITFIELD(machine_mode) savemode : 8; /* Nonzero means it is a mode for
566 a low part that we should avoid changing when
567 clearing the rest of the reg. */
568 unsigned int cond : 1; /* 1 if only conditionally movable */
569 unsigned int force : 1; /* 1 means MUST move this insn */
570 unsigned int global : 1; /* 1 means reg is live outside this loop */
571 /* If PARTIAL is 1, GLOBAL means something different:
572 that the reg is live outside the range from where it is set
573 to the following label. */
574 unsigned int done : 1; /* 1 inhibits further processing of this */
576 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
577 In particular, moving it does not make it
578 invariant. */
579 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
580 load SRC, rather than copying INSN. */
581 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
582 first insn of a consecutive sets group. */
583 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
584 unsigned int insert_temp : 1; /* 1 means we copy to a new pseudo and replace
585 the original insn with a copy from that
586 pseudo, rather than deleting it. */
587 struct movable *match; /* First entry for same value */
588 struct movable *forces; /* An insn that must be moved if this is */
589 struct movable *next;
593 static FILE *loop_dump_stream;
595 /* Forward declarations. */
597 static void invalidate_loops_containing_label (rtx);
598 static void find_and_verify_loops (rtx, struct loops *);
599 static void mark_loop_jump (rtx, struct loop *);
600 static void prescan_loop (struct loop *);
601 static int reg_in_basic_block_p (rtx, rtx);
602 static int consec_sets_invariant_p (const struct loop *, rtx, int, rtx);
603 static int labels_in_range_p (rtx, int);
604 static void count_one_set (struct loop_regs *, rtx, rtx, rtx *);
605 static void note_addr_stored (rtx, rtx, void *);
606 static void note_set_pseudo_multiple_uses (rtx, rtx, void *);
607 static int loop_reg_used_before_p (const struct loop *, rtx, rtx);
608 static rtx find_regs_nested (rtx, rtx);
609 static void scan_loop (struct loop*, int);
610 #if 0
611 static void replace_call_address (rtx, rtx, rtx);
612 #endif
613 static rtx skip_consec_insns (rtx, int);
614 static int libcall_benefit (rtx);
615 static rtx libcall_other_reg (rtx, rtx);
616 static void record_excess_regs (rtx, rtx, rtx *);
617 static void ignore_some_movables (struct loop_movables *);
618 static void force_movables (struct loop_movables *);
619 static void combine_movables (struct loop_movables *, struct loop_regs *);
620 static int num_unmoved_movables (const struct loop *);
621 static int regs_match_p (rtx, rtx, struct loop_movables *);
622 static int rtx_equal_for_loop_p (rtx, rtx, struct loop_movables *,
623 struct loop_regs *);
624 static void add_label_notes (rtx, rtx);
625 static void move_movables (struct loop *loop, struct loop_movables *, int,
626 int);
627 static void loop_movables_add (struct loop_movables *, struct movable *);
628 static void loop_movables_free (struct loop_movables *);
629 static int count_nonfixed_reads (const struct loop *, rtx);
630 static void loop_bivs_find (struct loop *);
631 static void loop_bivs_init_find (struct loop *);
632 static void loop_bivs_check (struct loop *);
633 static void loop_givs_find (struct loop *);
634 static void loop_givs_check (struct loop *);
635 static int loop_biv_eliminable_p (struct loop *, struct iv_class *, int, int);
636 static int loop_giv_reduce_benefit (struct loop *, struct iv_class *,
637 struct induction *, rtx);
638 static void loop_givs_dead_check (struct loop *, struct iv_class *);
639 static void loop_givs_reduce (struct loop *, struct iv_class *);
640 static void loop_givs_rescan (struct loop *, struct iv_class *, rtx *);
641 static void loop_ivs_free (struct loop *);
642 static void strength_reduce (struct loop *, int);
643 static void find_single_use_in_loop (struct loop_regs *, rtx, rtx);
644 static int valid_initial_value_p (rtx, rtx, int, rtx);
645 static void find_mem_givs (const struct loop *, rtx, rtx, int, int);
646 static void record_biv (struct loop *, struct induction *, rtx, rtx, rtx,
647 rtx, rtx *, int, int);
648 static void check_final_value (const struct loop *, struct induction *);
649 static void loop_ivs_dump (const struct loop *, FILE *, int);
650 static void loop_iv_class_dump (const struct iv_class *, FILE *, int);
651 static void loop_biv_dump (const struct induction *, FILE *, int);
652 static void loop_giv_dump (const struct induction *, FILE *, int);
653 static void record_giv (const struct loop *, struct induction *, rtx, rtx,
654 rtx, rtx, rtx, rtx, int, enum g_types, int, int,
655 rtx *);
656 static void update_giv_derive (const struct loop *, rtx);
657 static HOST_WIDE_INT get_monotonic_increment (struct iv_class *);
658 static bool biased_biv_fits_mode_p (const struct loop *, struct iv_class *,
659 HOST_WIDE_INT, enum machine_mode,
660 unsigned HOST_WIDE_INT);
661 static bool biv_fits_mode_p (const struct loop *, struct iv_class *,
662 HOST_WIDE_INT, enum machine_mode, bool);
663 static bool extension_within_bounds_p (const struct loop *, struct iv_class *,
664 HOST_WIDE_INT, rtx);
665 static void check_ext_dependent_givs (const struct loop *, struct iv_class *);
666 static int basic_induction_var (const struct loop *, rtx, enum machine_mode,
667 rtx, rtx, rtx *, rtx *, rtx **);
668 static rtx simplify_giv_expr (const struct loop *, rtx, rtx *, int *);
669 static int general_induction_var (const struct loop *loop, rtx, rtx *, rtx *,
670 rtx *, rtx *, int, int *, enum machine_mode);
671 static int consec_sets_giv (const struct loop *, int, rtx, rtx, rtx, rtx *,
672 rtx *, rtx *, rtx *);
673 static int check_dbra_loop (struct loop *, int);
674 static rtx express_from_1 (rtx, rtx, rtx);
675 static rtx combine_givs_p (struct induction *, struct induction *);
676 static int cmp_combine_givs_stats (const void *, const void *);
677 static void combine_givs (struct loop_regs *, struct iv_class *);
678 static int product_cheap_p (rtx, rtx);
679 static int maybe_eliminate_biv (const struct loop *, struct iv_class *, int,
680 int, int);
681 static int maybe_eliminate_biv_1 (const struct loop *, rtx, rtx,
682 struct iv_class *, int, basic_block, rtx);
683 static int last_use_this_basic_block (rtx, rtx);
684 static void record_initial (rtx, rtx, void *);
685 static void update_reg_last_use (rtx, rtx);
686 static rtx next_insn_in_loop (const struct loop *, rtx);
687 static void loop_regs_scan (const struct loop *, int);
688 static int count_insns_in_loop (const struct loop *);
689 static int find_mem_in_note_1 (rtx *, void *);
690 static rtx find_mem_in_note (rtx);
691 static void load_mems (const struct loop *);
692 static int insert_loop_mem (rtx *, void *);
693 static int replace_loop_mem (rtx *, void *);
694 static void replace_loop_mems (rtx, rtx, rtx, int);
695 static int replace_loop_reg (rtx *, void *);
696 static void replace_loop_regs (rtx insn, rtx, rtx);
697 static void note_reg_stored (rtx, rtx, void *);
698 static void try_copy_prop (const struct loop *, rtx, unsigned int);
699 static void try_swap_copy_prop (const struct loop *, rtx, unsigned int);
700 static rtx check_insn_for_givs (struct loop *, rtx, int, int);
701 static rtx check_insn_for_bivs (struct loop *, rtx, int, int);
702 static rtx gen_add_mult (rtx, rtx, rtx, rtx);
703 static void loop_regs_update (const struct loop *, rtx);
704 static int iv_add_mult_cost (rtx, rtx, rtx, rtx);
705 static int loop_invariant_p (const struct loop *, rtx);
706 static rtx loop_insn_hoist (const struct loop *, rtx);
707 static void loop_iv_add_mult_emit_before (const struct loop *, rtx, rtx, rtx,
708 rtx, basic_block, rtx);
709 static rtx loop_insn_emit_before (const struct loop *, basic_block,
710 rtx, rtx);
711 static int loop_insn_first_p (rtx, rtx);
712 static rtx get_condition_for_loop (const struct loop *, rtx);
713 static void loop_iv_add_mult_sink (const struct loop *, rtx, rtx, rtx, rtx);
714 static void loop_iv_add_mult_hoist (const struct loop *, rtx, rtx, rtx, rtx);
715 static rtx extend_value_for_giv (struct induction *, rtx);
716 static rtx loop_insn_sink (const struct loop *, rtx);
718 static rtx loop_insn_emit_after (const struct loop *, basic_block, rtx, rtx);
719 static rtx loop_call_insn_emit_before (const struct loop *, basic_block,
720 rtx, rtx);
721 static rtx loop_call_insn_hoist (const struct loop *, rtx);
722 static rtx loop_insn_sink_or_swim (const struct loop *, rtx);
724 static void loop_dump_aux (const struct loop *, FILE *, int);
725 static void loop_delete_insns (rtx, rtx);
726 static HOST_WIDE_INT remove_constant_addition (rtx *);
727 static rtx gen_load_of_final_value (rtx, rtx);
728 void debug_ivs (const struct loop *);
729 void debug_iv_class (const struct iv_class *);
730 void debug_biv (const struct induction *);
731 void debug_giv (const struct induction *);
732 void debug_loop (const struct loop *);
733 void debug_loops (const struct loops *);
735 typedef struct loop_replace_args
737 rtx match;
738 rtx replacement;
739 rtx insn;
740 } loop_replace_args;
742 /* Nonzero iff INSN is between START and END, inclusive. */
743 #define INSN_IN_RANGE_P(INSN, START, END) \
744 (INSN_UID (INSN) < max_uid_for_loop \
745 && INSN_LUID (INSN) >= INSN_LUID (START) \
746 && INSN_LUID (INSN) <= INSN_LUID (END))
748 /* Indirect_jump_in_function is computed once per function. */
749 static int indirect_jump_in_function;
750 static int indirect_jump_in_function_p (rtx);
752 static int compute_luids (rtx, rtx, int);
754 static int biv_elimination_giv_has_0_offset (struct induction *,
755 struct induction *, rtx);
757 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
758 copy the value of the strength reduced giv to its original register. */
759 static int copy_cost;
761 /* Cost of using a register, to normalize the benefits of a giv. */
762 static int reg_address_cost;
764 void
765 init_loop (void)
767 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
769 reg_address_cost = address_cost (reg, SImode);
771 copy_cost = COSTS_N_INSNS (1);
774 /* Compute the mapping from uids to luids.
775 LUIDs are numbers assigned to insns, like uids,
776 except that luids increase monotonically through the code.
777 Start at insn START and stop just before END. Assign LUIDs
778 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
779 static int
780 compute_luids (rtx start, rtx end, int prev_luid)
782 int i;
783 rtx insn;
785 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
787 if (INSN_UID (insn) >= max_uid_for_loop)
788 continue;
789 /* Don't assign luids to line-number NOTEs, so that the distance in
790 luids between two insns is not affected by -g. */
791 if (!NOTE_P (insn)
792 || NOTE_LINE_NUMBER (insn) <= 0)
793 uid_luid[INSN_UID (insn)] = ++i;
794 else
795 /* Give a line number note the same luid as preceding insn. */
796 uid_luid[INSN_UID (insn)] = i;
798 return i + 1;
801 /* Entry point of this file. Perform loop optimization
802 on the current function. F is the first insn of the function
803 and DUMPFILE is a stream for output of a trace of actions taken
804 (or 0 if none should be output). */
806 void
807 loop_optimize (rtx f, FILE *dumpfile, int flags)
809 rtx insn;
810 int i;
811 struct loops loops_data;
812 struct loops *loops = &loops_data;
813 struct loop_info *loops_info;
815 loop_dump_stream = dumpfile;
817 init_recog_no_volatile ();
819 max_reg_before_loop = max_reg_num ();
820 loop_max_reg = max_reg_before_loop;
822 regs_may_share = 0;
824 /* Count the number of loops. */
826 max_loop_num = 0;
827 for (insn = f; insn; insn = NEXT_INSN (insn))
829 if (NOTE_P (insn)
830 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
831 max_loop_num++;
834 /* Don't waste time if no loops. */
835 if (max_loop_num == 0)
836 return;
838 loops->num = max_loop_num;
840 /* Get size to use for tables indexed by uids.
841 Leave some space for labels allocated by find_and_verify_loops. */
842 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
844 uid_luid = xcalloc (max_uid_for_loop, sizeof (int));
845 uid_loop = xcalloc (max_uid_for_loop, sizeof (struct loop *));
847 /* Allocate storage for array of loops. */
848 loops->array = xcalloc (loops->num, sizeof (struct loop));
850 /* Find and process each loop.
851 First, find them, and record them in order of their beginnings. */
852 find_and_verify_loops (f, loops);
854 /* Allocate and initialize auxiliary loop information. */
855 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
856 for (i = 0; i < (int) loops->num; i++)
857 loops->array[i].aux = loops_info + i;
859 /* Now find all register lifetimes. This must be done after
860 find_and_verify_loops, because it might reorder the insns in the
861 function. */
862 reg_scan (f, max_reg_before_loop);
864 /* This must occur after reg_scan so that registers created by gcse
865 will have entries in the register tables.
867 We could have added a call to reg_scan after gcse_main in toplev.c,
868 but moving this call to init_alias_analysis is more efficient. */
869 init_alias_analysis ();
871 /* See if we went too far. Note that get_max_uid already returns
872 one more that the maximum uid of all insn. */
873 gcc_assert (get_max_uid () <= max_uid_for_loop);
874 /* Now reset it to the actual size we need. See above. */
875 max_uid_for_loop = get_max_uid ();
877 /* find_and_verify_loops has already called compute_luids, but it
878 might have rearranged code afterwards, so we need to recompute
879 the luids now. */
880 compute_luids (f, NULL_RTX, 0);
882 /* Don't leave gaps in uid_luid for insns that have been
883 deleted. It is possible that the first or last insn
884 using some register has been deleted by cross-jumping.
885 Make sure that uid_luid for that former insn's uid
886 points to the general area where that insn used to be. */
887 for (i = 0; i < max_uid_for_loop; i++)
889 uid_luid[0] = uid_luid[i];
890 if (uid_luid[0] != 0)
891 break;
893 for (i = 0; i < max_uid_for_loop; i++)
894 if (uid_luid[i] == 0)
895 uid_luid[i] = uid_luid[i - 1];
897 /* Determine if the function has indirect jump. On some systems
898 this prevents low overhead loop instructions from being used. */
899 indirect_jump_in_function = indirect_jump_in_function_p (f);
901 /* Now scan the loops, last ones first, since this means inner ones are done
902 before outer ones. */
903 for (i = max_loop_num - 1; i >= 0; i--)
905 struct loop *loop = &loops->array[i];
907 if (! loop->invalid && loop->end)
909 scan_loop (loop, flags);
910 ggc_collect ();
914 end_alias_analysis ();
916 /* Clean up. */
917 for (i = 0; i < (int) loops->num; i++)
918 free (loops_info[i].mems);
920 free (uid_luid);
921 free (uid_loop);
922 free (loops_info);
923 free (loops->array);
926 /* Returns the next insn, in execution order, after INSN. START and
927 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
928 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
929 insn-stream; it is used with loops that are entered near the
930 bottom. */
932 static rtx
933 next_insn_in_loop (const struct loop *loop, rtx insn)
935 insn = NEXT_INSN (insn);
937 if (insn == loop->end)
939 if (loop->top)
940 /* Go to the top of the loop, and continue there. */
941 insn = loop->top;
942 else
943 /* We're done. */
944 insn = NULL_RTX;
947 if (insn == loop->scan_start)
948 /* We're done. */
949 insn = NULL_RTX;
951 return insn;
954 /* Find any register references hidden inside X and add them to
955 the dependency list DEPS. This is used to look inside CLOBBER (MEM
956 when checking whether a PARALLEL can be pulled out of a loop. */
958 static rtx
959 find_regs_nested (rtx deps, rtx x)
961 enum rtx_code code = GET_CODE (x);
962 if (code == REG)
963 deps = gen_rtx_EXPR_LIST (VOIDmode, x, deps);
964 else
966 const char *fmt = GET_RTX_FORMAT (code);
967 int i, j;
968 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
970 if (fmt[i] == 'e')
971 deps = find_regs_nested (deps, XEXP (x, i));
972 else if (fmt[i] == 'E')
973 for (j = 0; j < XVECLEN (x, i); j++)
974 deps = find_regs_nested (deps, XVECEXP (x, i, j));
977 return deps;
980 /* Optimize one loop described by LOOP. */
982 /* ??? Could also move memory writes out of loops if the destination address
983 is invariant, the source is invariant, the memory write is not volatile,
984 and if we can prove that no read inside the loop can read this address
985 before the write occurs. If there is a read of this address after the
986 write, then we can also mark the memory read as invariant. */
988 static void
989 scan_loop (struct loop *loop, int flags)
991 struct loop_info *loop_info = LOOP_INFO (loop);
992 struct loop_regs *regs = LOOP_REGS (loop);
993 int i;
994 rtx loop_start = loop->start;
995 rtx loop_end = loop->end;
996 rtx p;
997 /* 1 if we are scanning insns that could be executed zero times. */
998 int maybe_never = 0;
999 /* 1 if we are scanning insns that might never be executed
1000 due to a subroutine call which might exit before they are reached. */
1001 int call_passed = 0;
1002 /* Number of insns in the loop. */
1003 int insn_count;
1004 int tem;
1005 rtx temp, update_start, update_end;
1006 /* The SET from an insn, if it is the only SET in the insn. */
1007 rtx set, set1;
1008 /* Chain describing insns movable in current loop. */
1009 struct loop_movables *movables = LOOP_MOVABLES (loop);
1010 /* Ratio of extra register life span we can justify
1011 for saving an instruction. More if loop doesn't call subroutines
1012 since in that case saving an insn makes more difference
1013 and more registers are available. */
1014 int threshold;
1015 int in_libcall;
1017 loop->top = 0;
1019 movables->head = 0;
1020 movables->last = 0;
1022 /* Determine whether this loop starts with a jump down to a test at
1023 the end. This will occur for a small number of loops with a test
1024 that is too complex to duplicate in front of the loop.
1026 We search for the first insn or label in the loop, skipping NOTEs.
1027 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
1028 (because we might have a loop executed only once that contains a
1029 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
1030 (in case we have a degenerate loop).
1032 Note that if we mistakenly think that a loop is entered at the top
1033 when, in fact, it is entered at the exit test, the only effect will be
1034 slightly poorer optimization. Making the opposite error can generate
1035 incorrect code. Since very few loops now start with a jump to the
1036 exit test, the code here to detect that case is very conservative. */
1038 for (p = NEXT_INSN (loop_start);
1039 p != loop_end
1040 && !LABEL_P (p) && ! INSN_P (p)
1041 && (!NOTE_P (p)
1042 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
1043 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
1044 p = NEXT_INSN (p))
1047 loop->scan_start = p;
1049 /* If loop end is the end of the current function, then emit a
1050 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
1051 note insn. This is the position we use when sinking insns out of
1052 the loop. */
1053 if (NEXT_INSN (loop->end) != 0)
1054 loop->sink = NEXT_INSN (loop->end);
1055 else
1056 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
1058 /* Set up variables describing this loop. */
1059 prescan_loop (loop);
1060 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
1062 /* If loop has a jump before the first label,
1063 the true entry is the target of that jump.
1064 Start scan from there.
1065 But record in LOOP->TOP the place where the end-test jumps
1066 back to so we can scan that after the end of the loop. */
1067 if (JUMP_P (p)
1068 /* Loop entry must be unconditional jump (and not a RETURN) */
1069 && any_uncondjump_p (p)
1070 && JUMP_LABEL (p) != 0
1071 /* Check to see whether the jump actually
1072 jumps out of the loop (meaning it's no loop).
1073 This case can happen for things like
1074 do {..} while (0). If this label was generated previously
1075 by loop, we can't tell anything about it and have to reject
1076 the loop. */
1077 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
1079 loop->top = next_label (loop->scan_start);
1080 loop->scan_start = JUMP_LABEL (p);
1083 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
1084 as required by loop_reg_used_before_p. So skip such loops. (This
1085 test may never be true, but it's best to play it safe.)
1087 Also, skip loops where we do not start scanning at a label. This
1088 test also rejects loops starting with a JUMP_INSN that failed the
1089 test above. */
1091 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
1092 || !LABEL_P (loop->scan_start))
1094 if (loop_dump_stream)
1095 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
1096 INSN_UID (loop_start), INSN_UID (loop_end));
1097 return;
1100 /* Allocate extra space for REGs that might be created by load_mems.
1101 We allocate a little extra slop as well, in the hopes that we
1102 won't have to reallocate the regs array. */
1103 loop_regs_scan (loop, loop_info->mems_idx + 16);
1104 insn_count = count_insns_in_loop (loop);
1106 if (loop_dump_stream)
1107 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
1108 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
1110 /* Scan through the loop finding insns that are safe to move.
1111 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
1112 this reg will be considered invariant for subsequent insns.
1113 We consider whether subsequent insns use the reg
1114 in deciding whether it is worth actually moving.
1116 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
1117 and therefore it is possible that the insns we are scanning
1118 would never be executed. At such times, we must make sure
1119 that it is safe to execute the insn once instead of zero times.
1120 When MAYBE_NEVER is 0, all insns will be executed at least once
1121 so that is not a problem. */
1123 for (in_libcall = 0, p = next_insn_in_loop (loop, loop->scan_start);
1124 p != NULL_RTX;
1125 p = next_insn_in_loop (loop, p))
1127 if (in_libcall && INSN_P (p) && find_reg_note (p, REG_RETVAL, NULL_RTX))
1128 in_libcall--;
1129 if (NONJUMP_INSN_P (p))
1131 /* Do not scan past an optimization barrier. */
1132 if (GET_CODE (PATTERN (p)) == ASM_INPUT)
1133 break;
1134 temp = find_reg_note (p, REG_LIBCALL, NULL_RTX);
1135 if (temp)
1136 in_libcall++;
1137 if (! in_libcall
1138 && (set = single_set (p))
1139 && REG_P (SET_DEST (set))
1140 && SET_DEST (set) != frame_pointer_rtx
1141 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
1142 && SET_DEST (set) != pic_offset_table_rtx
1143 #endif
1144 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
1146 int tem1 = 0;
1147 int tem2 = 0;
1148 int move_insn = 0;
1149 int insert_temp = 0;
1150 rtx src = SET_SRC (set);
1151 rtx dependencies = 0;
1153 /* Figure out what to use as a source of this insn. If a
1154 REG_EQUIV note is given or if a REG_EQUAL note with a
1155 constant operand is specified, use it as the source and
1156 mark that we should move this insn by calling
1157 emit_move_insn rather that duplicating the insn.
1159 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
1160 note is present. */
1161 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
1162 if (temp)
1163 src = XEXP (temp, 0), move_insn = 1;
1164 else
1166 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
1167 if (temp && CONSTANT_P (XEXP (temp, 0)))
1168 src = XEXP (temp, 0), move_insn = 1;
1169 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
1171 src = XEXP (temp, 0);
1172 /* A libcall block can use regs that don't appear in
1173 the equivalent expression. To move the libcall,
1174 we must move those regs too. */
1175 dependencies = libcall_other_reg (p, src);
1179 /* For parallels, add any possible uses to the dependencies, as
1180 we can't move the insn without resolving them first.
1181 MEMs inside CLOBBERs may also reference registers; these
1182 count as implicit uses. */
1183 if (GET_CODE (PATTERN (p)) == PARALLEL)
1185 for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
1187 rtx x = XVECEXP (PATTERN (p), 0, i);
1188 if (GET_CODE (x) == USE)
1189 dependencies
1190 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
1191 dependencies);
1192 else if (GET_CODE (x) == CLOBBER
1193 && MEM_P (XEXP (x, 0)))
1194 dependencies = find_regs_nested (dependencies,
1195 XEXP (XEXP (x, 0), 0));
1199 if (/* The register is used in basic blocks other
1200 than the one where it is set (meaning that
1201 something after this point in the loop might
1202 depend on its value before the set). */
1203 ! reg_in_basic_block_p (p, SET_DEST (set))
1204 /* And the set is not guaranteed to be executed once
1205 the loop starts, or the value before the set is
1206 needed before the set occurs...
1208 ??? Note we have quadratic behavior here, mitigated
1209 by the fact that the previous test will often fail for
1210 large loops. Rather than re-scanning the entire loop
1211 each time for register usage, we should build tables
1212 of the register usage and use them here instead. */
1213 && (maybe_never
1214 || loop_reg_used_before_p (loop, set, p)))
1215 /* It is unsafe to move the set. However, it may be OK to
1216 move the source into a new pseudo, and substitute a
1217 reg-to-reg copy for the original insn.
1219 This code used to consider it OK to move a set of a variable
1220 which was not created by the user and not used in an exit
1221 test.
1222 That behavior is incorrect and was removed. */
1223 insert_temp = 1;
1225 /* Don't try to optimize a MODE_CC set with a constant
1226 source. It probably will be combined with a conditional
1227 jump. */
1228 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set))) == MODE_CC
1229 && CONSTANT_P (src))
1231 /* Don't try to optimize a register that was made
1232 by loop-optimization for an inner loop.
1233 We don't know its life-span, so we can't compute
1234 the benefit. */
1235 else if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
1237 /* Don't move the source and add a reg-to-reg copy:
1238 - with -Os (this certainly increases size),
1239 - if the mode doesn't support copy operations (obviously),
1240 - if the source is already a reg (the motion will gain nothing),
1241 - if the source is a legitimate constant (likewise). */
1242 else if (insert_temp
1243 && (optimize_size
1244 || ! can_copy_p (GET_MODE (SET_SRC (set)))
1245 || REG_P (SET_SRC (set))
1246 || (CONSTANT_P (SET_SRC (set))
1247 && LEGITIMATE_CONSTANT_P (SET_SRC (set)))))
1249 else if ((tem = loop_invariant_p (loop, src))
1250 && (dependencies == 0
1251 || (tem2
1252 = loop_invariant_p (loop, dependencies)) != 0)
1253 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
1254 || (tem1
1255 = consec_sets_invariant_p
1256 (loop, SET_DEST (set),
1257 regs->array[REGNO (SET_DEST (set))].set_in_loop,
1258 p)))
1259 /* If the insn can cause a trap (such as divide by zero),
1260 can't move it unless it's guaranteed to be executed
1261 once loop is entered. Even a function call might
1262 prevent the trap insn from being reached
1263 (since it might exit!) */
1264 && ! ((maybe_never || call_passed)
1265 && may_trap_p (src)))
1267 struct movable *m;
1268 int regno = REGNO (SET_DEST (set));
1270 /* A potential lossage is where we have a case where two insns
1271 can be combined as long as they are both in the loop, but
1272 we move one of them outside the loop. For large loops,
1273 this can lose. The most common case of this is the address
1274 of a function being called.
1276 Therefore, if this register is marked as being used
1277 exactly once if we are in a loop with calls
1278 (a "large loop"), see if we can replace the usage of
1279 this register with the source of this SET. If we can,
1280 delete this insn.
1282 Don't do this if P has a REG_RETVAL note or if we have
1283 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
1285 if (loop_info->has_call
1286 && regs->array[regno].single_usage != 0
1287 && regs->array[regno].single_usage != const0_rtx
1288 && REGNO_FIRST_UID (regno) == INSN_UID (p)
1289 && (REGNO_LAST_UID (regno)
1290 == INSN_UID (regs->array[regno].single_usage))
1291 && regs->array[regno].set_in_loop == 1
1292 && GET_CODE (SET_SRC (set)) != ASM_OPERANDS
1293 && ! side_effects_p (SET_SRC (set))
1294 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
1295 && (! SMALL_REGISTER_CLASSES
1296 || (! (REG_P (SET_SRC (set))
1297 && (REGNO (SET_SRC (set))
1298 < FIRST_PSEUDO_REGISTER))))
1299 && regno >= FIRST_PSEUDO_REGISTER
1300 /* This test is not redundant; SET_SRC (set) might be
1301 a call-clobbered register and the life of REGNO
1302 might span a call. */
1303 && ! modified_between_p (SET_SRC (set), p,
1304 regs->array[regno].single_usage)
1305 && no_labels_between_p (p,
1306 regs->array[regno].single_usage)
1307 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
1308 regs->array[regno].single_usage))
1310 /* Replace any usage in a REG_EQUAL note. Must copy
1311 the new source, so that we don't get rtx sharing
1312 between the SET_SOURCE and REG_NOTES of insn p. */
1313 REG_NOTES (regs->array[regno].single_usage)
1314 = (replace_rtx
1315 (REG_NOTES (regs->array[regno].single_usage),
1316 SET_DEST (set), copy_rtx (SET_SRC (set))));
1318 delete_insn (p);
1319 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set));
1320 i++)
1321 regs->array[regno+i].set_in_loop = 0;
1322 continue;
1325 m = xmalloc (sizeof (struct movable));
1326 m->next = 0;
1327 m->insn = p;
1328 m->set_src = src;
1329 m->dependencies = dependencies;
1330 m->set_dest = SET_DEST (set);
1331 m->force = 0;
1332 m->consec
1333 = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
1334 m->done = 0;
1335 m->forces = 0;
1336 m->partial = 0;
1337 m->move_insn = move_insn;
1338 m->move_insn_first = 0;
1339 m->insert_temp = insert_temp;
1340 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
1341 m->savemode = VOIDmode;
1342 m->regno = regno;
1343 /* Set M->cond if either loop_invariant_p
1344 or consec_sets_invariant_p returned 2
1345 (only conditionally invariant). */
1346 m->cond = ((tem | tem1 | tem2) > 1);
1347 m->global = LOOP_REG_GLOBAL_P (loop, regno);
1348 m->match = 0;
1349 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1350 m->savings = regs->array[regno].n_times_set;
1351 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
1352 m->savings += libcall_benefit (p);
1353 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
1354 regs->array[regno+i].set_in_loop = move_insn ? -2 : -1;
1355 /* Add M to the end of the chain MOVABLES. */
1356 loop_movables_add (movables, m);
1358 if (m->consec > 0)
1360 /* It is possible for the first instruction to have a
1361 REG_EQUAL note but a non-invariant SET_SRC, so we must
1362 remember the status of the first instruction in case
1363 the last instruction doesn't have a REG_EQUAL note. */
1364 m->move_insn_first = m->move_insn;
1366 /* Skip this insn, not checking REG_LIBCALL notes. */
1367 p = next_nonnote_insn (p);
1368 /* Skip the consecutive insns, if there are any. */
1369 p = skip_consec_insns (p, m->consec);
1370 /* Back up to the last insn of the consecutive group. */
1371 p = prev_nonnote_insn (p);
1373 /* We must now reset m->move_insn, m->is_equiv, and
1374 possibly m->set_src to correspond to the effects of
1375 all the insns. */
1376 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
1377 if (temp)
1378 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1379 else
1381 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
1382 if (temp && CONSTANT_P (XEXP (temp, 0)))
1383 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1384 else
1385 m->move_insn = 0;
1388 m->is_equiv
1389 = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
1392 /* If this register is always set within a STRICT_LOW_PART
1393 or set to zero, then its high bytes are constant.
1394 So clear them outside the loop and within the loop
1395 just load the low bytes.
1396 We must check that the machine has an instruction to do so.
1397 Also, if the value loaded into the register
1398 depends on the same register, this cannot be done. */
1399 else if (SET_SRC (set) == const0_rtx
1400 && NONJUMP_INSN_P (NEXT_INSN (p))
1401 && (set1 = single_set (NEXT_INSN (p)))
1402 && GET_CODE (set1) == SET
1403 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
1404 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
1405 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
1406 == SET_DEST (set))
1407 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
1409 int regno = REGNO (SET_DEST (set));
1410 if (regs->array[regno].set_in_loop == 2)
1412 struct movable *m;
1413 m = xmalloc (sizeof (struct movable));
1414 m->next = 0;
1415 m->insn = p;
1416 m->set_dest = SET_DEST (set);
1417 m->dependencies = 0;
1418 m->force = 0;
1419 m->consec = 0;
1420 m->done = 0;
1421 m->forces = 0;
1422 m->move_insn = 0;
1423 m->move_insn_first = 0;
1424 m->insert_temp = insert_temp;
1425 m->partial = 1;
1426 /* If the insn may not be executed on some cycles,
1427 we can't clear the whole reg; clear just high part.
1428 Not even if the reg is used only within this loop.
1429 Consider this:
1430 while (1)
1431 while (s != t) {
1432 if (foo ()) x = *s;
1433 use (x);
1435 Clearing x before the inner loop could clobber a value
1436 being saved from the last time around the outer loop.
1437 However, if the reg is not used outside this loop
1438 and all uses of the register are in the same
1439 basic block as the store, there is no problem.
1441 If this insn was made by loop, we don't know its
1442 INSN_LUID and hence must make a conservative
1443 assumption. */
1444 m->global = (INSN_UID (p) >= max_uid_for_loop
1445 || LOOP_REG_GLOBAL_P (loop, regno)
1446 || (labels_in_range_p
1447 (p, REGNO_FIRST_LUID (regno))));
1448 if (maybe_never && m->global)
1449 m->savemode = GET_MODE (SET_SRC (set1));
1450 else
1451 m->savemode = VOIDmode;
1452 m->regno = regno;
1453 m->cond = 0;
1454 m->match = 0;
1455 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1456 m->savings = 1;
1457 for (i = 0;
1458 i < LOOP_REGNO_NREGS (regno, SET_DEST (set));
1459 i++)
1460 regs->array[regno+i].set_in_loop = -1;
1461 /* Add M to the end of the chain MOVABLES. */
1462 loop_movables_add (movables, m);
1467 /* Past a call insn, we get to insns which might not be executed
1468 because the call might exit. This matters for insns that trap.
1469 Constant and pure call insns always return, so they don't count. */
1470 else if (CALL_P (p) && ! CONST_OR_PURE_CALL_P (p))
1471 call_passed = 1;
1472 /* Past a label or a jump, we get to insns for which we
1473 can't count on whether or how many times they will be
1474 executed during each iteration. Therefore, we can
1475 only move out sets of trivial variables
1476 (those not used after the loop). */
1477 /* Similar code appears twice in strength_reduce. */
1478 else if ((LABEL_P (p) || JUMP_P (p))
1479 /* If we enter the loop in the middle, and scan around to the
1480 beginning, don't set maybe_never for that. This must be an
1481 unconditional jump, otherwise the code at the top of the
1482 loop might never be executed. Unconditional jumps are
1483 followed by a barrier then the loop_end. */
1484 && ! (JUMP_P (p) && JUMP_LABEL (p) == loop->top
1485 && NEXT_INSN (NEXT_INSN (p)) == loop_end
1486 && any_uncondjump_p (p)))
1487 maybe_never = 1;
1490 /* If one movable subsumes another, ignore that other. */
1492 ignore_some_movables (movables);
1494 /* For each movable insn, see if the reg that it loads
1495 leads when it dies right into another conditionally movable insn.
1496 If so, record that the second insn "forces" the first one,
1497 since the second can be moved only if the first is. */
1499 force_movables (movables);
1501 /* See if there are multiple movable insns that load the same value.
1502 If there are, make all but the first point at the first one
1503 through the `match' field, and add the priorities of them
1504 all together as the priority of the first. */
1506 combine_movables (movables, regs);
1508 /* Now consider each movable insn to decide whether it is worth moving.
1509 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1511 For machines with few registers this increases code size, so do not
1512 move moveables when optimizing for code size on such machines.
1513 (The 18 below is the value for i386.) */
1515 if (!optimize_size
1516 || (reg_class_size[GENERAL_REGS] > 18 && !loop_info->has_call))
1518 move_movables (loop, movables, threshold, insn_count);
1520 /* Recalculate regs->array if move_movables has created new
1521 registers. */
1522 if (max_reg_num () > regs->num)
1524 loop_regs_scan (loop, 0);
1525 for (update_start = loop_start;
1526 PREV_INSN (update_start)
1527 && !LABEL_P (PREV_INSN (update_start));
1528 update_start = PREV_INSN (update_start))
1530 update_end = NEXT_INSN (loop_end);
1532 reg_scan_update (update_start, update_end, loop_max_reg);
1533 loop_max_reg = max_reg_num ();
1537 /* Now candidates that still are negative are those not moved.
1538 Change regs->array[I].set_in_loop to indicate that those are not actually
1539 invariant. */
1540 for (i = 0; i < regs->num; i++)
1541 if (regs->array[i].set_in_loop < 0)
1542 regs->array[i].set_in_loop = regs->array[i].n_times_set;
1544 /* Now that we've moved some things out of the loop, we might be able to
1545 hoist even more memory references. */
1546 load_mems (loop);
1548 /* Recalculate regs->array if load_mems has created new registers. */
1549 if (max_reg_num () > regs->num)
1550 loop_regs_scan (loop, 0);
1552 for (update_start = loop_start;
1553 PREV_INSN (update_start)
1554 && !LABEL_P (PREV_INSN (update_start));
1555 update_start = PREV_INSN (update_start))
1557 update_end = NEXT_INSN (loop_end);
1559 reg_scan_update (update_start, update_end, loop_max_reg);
1560 loop_max_reg = max_reg_num ();
1562 if (flag_strength_reduce)
1564 if (update_end && LABEL_P (update_end))
1565 /* Ensure our label doesn't go away. */
1566 LABEL_NUSES (update_end)++;
1568 strength_reduce (loop, flags);
1570 reg_scan_update (update_start, update_end, loop_max_reg);
1571 loop_max_reg = max_reg_num ();
1573 if (update_end && LABEL_P (update_end)
1574 && --LABEL_NUSES (update_end) == 0)
1575 delete_related_insns (update_end);
1579 /* The movable information is required for strength reduction. */
1580 loop_movables_free (movables);
1582 free (regs->array);
1583 regs->array = 0;
1584 regs->num = 0;
1587 /* Add elements to *OUTPUT to record all the pseudo-regs
1588 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1590 static void
1591 record_excess_regs (rtx in_this, rtx not_in_this, rtx *output)
1593 enum rtx_code code;
1594 const char *fmt;
1595 int i;
1597 code = GET_CODE (in_this);
1599 switch (code)
1601 case PC:
1602 case CC0:
1603 case CONST_INT:
1604 case CONST_DOUBLE:
1605 case CONST:
1606 case SYMBOL_REF:
1607 case LABEL_REF:
1608 return;
1610 case REG:
1611 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1612 && ! reg_mentioned_p (in_this, not_in_this))
1613 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1614 return;
1616 default:
1617 break;
1620 fmt = GET_RTX_FORMAT (code);
1621 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1623 int j;
1625 switch (fmt[i])
1627 case 'E':
1628 for (j = 0; j < XVECLEN (in_this, i); j++)
1629 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1630 break;
1632 case 'e':
1633 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1634 break;
1639 /* Check what regs are referred to in the libcall block ending with INSN,
1640 aside from those mentioned in the equivalent value.
1641 If there are none, return 0.
1642 If there are one or more, return an EXPR_LIST containing all of them. */
1644 static rtx
1645 libcall_other_reg (rtx insn, rtx equiv)
1647 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1648 rtx p = XEXP (note, 0);
1649 rtx output = 0;
1651 /* First, find all the regs used in the libcall block
1652 that are not mentioned as inputs to the result. */
1654 while (p != insn)
1656 if (INSN_P (p))
1657 record_excess_regs (PATTERN (p), equiv, &output);
1658 p = NEXT_INSN (p);
1661 return output;
1664 /* Return 1 if all uses of REG
1665 are between INSN and the end of the basic block. */
1667 static int
1668 reg_in_basic_block_p (rtx insn, rtx reg)
1670 int regno = REGNO (reg);
1671 rtx p;
1673 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1674 return 0;
1676 /* Search this basic block for the already recorded last use of the reg. */
1677 for (p = insn; p; p = NEXT_INSN (p))
1679 switch (GET_CODE (p))
1681 case NOTE:
1682 break;
1684 case INSN:
1685 case CALL_INSN:
1686 /* Ordinary insn: if this is the last use, we win. */
1687 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1688 return 1;
1689 break;
1691 case JUMP_INSN:
1692 /* Jump insn: if this is the last use, we win. */
1693 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1694 return 1;
1695 /* Otherwise, it's the end of the basic block, so we lose. */
1696 return 0;
1698 case CODE_LABEL:
1699 case BARRIER:
1700 /* It's the end of the basic block, so we lose. */
1701 return 0;
1703 default:
1704 break;
1708 /* The "last use" that was recorded can't be found after the first
1709 use. This can happen when the last use was deleted while
1710 processing an inner loop, this inner loop was then completely
1711 unrolled, and the outer loop is always exited after the inner loop,
1712 so that everything after the first use becomes a single basic block. */
1713 return 1;
1716 /* Compute the benefit of eliminating the insns in the block whose
1717 last insn is LAST. This may be a group of insns used to compute a
1718 value directly or can contain a library call. */
1720 static int
1721 libcall_benefit (rtx last)
1723 rtx insn;
1724 int benefit = 0;
1726 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1727 insn != last; insn = NEXT_INSN (insn))
1729 if (CALL_P (insn))
1730 benefit += 10; /* Assume at least this many insns in a library
1731 routine. */
1732 else if (NONJUMP_INSN_P (insn)
1733 && GET_CODE (PATTERN (insn)) != USE
1734 && GET_CODE (PATTERN (insn)) != CLOBBER)
1735 benefit++;
1738 return benefit;
1741 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1743 static rtx
1744 skip_consec_insns (rtx insn, int count)
1746 for (; count > 0; count--)
1748 rtx temp;
1750 /* If first insn of libcall sequence, skip to end. */
1751 /* Do this at start of loop, since INSN is guaranteed to
1752 be an insn here. */
1753 if (!NOTE_P (insn)
1754 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1755 insn = XEXP (temp, 0);
1758 insn = NEXT_INSN (insn);
1759 while (NOTE_P (insn));
1762 return insn;
1765 /* Ignore any movable whose insn falls within a libcall
1766 which is part of another movable.
1767 We make use of the fact that the movable for the libcall value
1768 was made later and so appears later on the chain. */
1770 static void
1771 ignore_some_movables (struct loop_movables *movables)
1773 struct movable *m, *m1;
1775 for (m = movables->head; m; m = m->next)
1777 /* Is this a movable for the value of a libcall? */
1778 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1779 if (note)
1781 rtx insn;
1782 /* Check for earlier movables inside that range,
1783 and mark them invalid. We cannot use LUIDs here because
1784 insns created by loop.c for prior loops don't have LUIDs.
1785 Rather than reject all such insns from movables, we just
1786 explicitly check each insn in the libcall (since invariant
1787 libcalls aren't that common). */
1788 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1789 for (m1 = movables->head; m1 != m; m1 = m1->next)
1790 if (m1->insn == insn)
1791 m1->done = 1;
1796 /* For each movable insn, see if the reg that it loads
1797 leads when it dies right into another conditionally movable insn.
1798 If so, record that the second insn "forces" the first one,
1799 since the second can be moved only if the first is. */
1801 static void
1802 force_movables (struct loop_movables *movables)
1804 struct movable *m, *m1;
1806 for (m1 = movables->head; m1; m1 = m1->next)
1807 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1808 if (!m1->partial && !m1->done)
1810 int regno = m1->regno;
1811 for (m = m1->next; m; m = m->next)
1812 /* ??? Could this be a bug? What if CSE caused the
1813 register of M1 to be used after this insn?
1814 Since CSE does not update regno_last_uid,
1815 this insn M->insn might not be where it dies.
1816 But very likely this doesn't matter; what matters is
1817 that M's reg is computed from M1's reg. */
1818 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1819 && !m->done)
1820 break;
1821 if (m != 0 && m->set_src == m1->set_dest
1822 /* If m->consec, m->set_src isn't valid. */
1823 && m->consec == 0)
1824 m = 0;
1826 /* Increase the priority of the moving the first insn
1827 since it permits the second to be moved as well.
1828 Likewise for insns already forced by the first insn. */
1829 if (m != 0)
1831 struct movable *m2;
1833 m->forces = m1;
1834 for (m2 = m1; m2; m2 = m2->forces)
1836 m2->lifetime += m->lifetime;
1837 m2->savings += m->savings;
1843 /* Find invariant expressions that are equal and can be combined into
1844 one register. */
1846 static void
1847 combine_movables (struct loop_movables *movables, struct loop_regs *regs)
1849 struct movable *m;
1850 char *matched_regs = xmalloc (regs->num);
1851 enum machine_mode mode;
1853 /* Regs that are set more than once are not allowed to match
1854 or be matched. I'm no longer sure why not. */
1855 /* Only pseudo registers are allowed to match or be matched,
1856 since move_movables does not validate the change. */
1857 /* Perhaps testing m->consec_sets would be more appropriate here? */
1859 for (m = movables->head; m; m = m->next)
1860 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1861 && m->regno >= FIRST_PSEUDO_REGISTER
1862 && !m->insert_temp
1863 && !m->partial)
1865 struct movable *m1;
1866 int regno = m->regno;
1868 memset (matched_regs, 0, regs->num);
1869 matched_regs[regno] = 1;
1871 /* We want later insns to match the first one. Don't make the first
1872 one match any later ones. So start this loop at m->next. */
1873 for (m1 = m->next; m1; m1 = m1->next)
1874 if (m != m1 && m1->match == 0
1875 && !m1->insert_temp
1876 && regs->array[m1->regno].n_times_set == 1
1877 && m1->regno >= FIRST_PSEUDO_REGISTER
1878 /* A reg used outside the loop mustn't be eliminated. */
1879 && !m1->global
1880 /* A reg used for zero-extending mustn't be eliminated. */
1881 && !m1->partial
1882 && (matched_regs[m1->regno]
1885 /* Can combine regs with different modes loaded from the
1886 same constant only if the modes are the same or
1887 if both are integer modes with M wider or the same
1888 width as M1. The check for integer is redundant, but
1889 safe, since the only case of differing destination
1890 modes with equal sources is when both sources are
1891 VOIDmode, i.e., CONST_INT. */
1892 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1893 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1894 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1895 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1896 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1897 /* See if the source of M1 says it matches M. */
1898 && ((REG_P (m1->set_src)
1899 && matched_regs[REGNO (m1->set_src)])
1900 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1901 movables, regs))))
1902 && ((m->dependencies == m1->dependencies)
1903 || rtx_equal_p (m->dependencies, m1->dependencies)))
1905 m->lifetime += m1->lifetime;
1906 m->savings += m1->savings;
1907 m1->done = 1;
1908 m1->match = m;
1909 matched_regs[m1->regno] = 1;
1913 /* Now combine the regs used for zero-extension.
1914 This can be done for those not marked `global'
1915 provided their lives don't overlap. */
1917 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1918 mode = GET_MODE_WIDER_MODE (mode))
1920 struct movable *m0 = 0;
1922 /* Combine all the registers for extension from mode MODE.
1923 Don't combine any that are used outside this loop. */
1924 for (m = movables->head; m; m = m->next)
1925 if (m->partial && ! m->global
1926 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1928 struct movable *m1;
1930 int first = REGNO_FIRST_LUID (m->regno);
1931 int last = REGNO_LAST_LUID (m->regno);
1933 if (m0 == 0)
1935 /* First one: don't check for overlap, just record it. */
1936 m0 = m;
1937 continue;
1940 /* Make sure they extend to the same mode.
1941 (Almost always true.) */
1942 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1943 continue;
1945 /* We already have one: check for overlap with those
1946 already combined together. */
1947 for (m1 = movables->head; m1 != m; m1 = m1->next)
1948 if (m1 == m0 || (m1->partial && m1->match == m0))
1949 if (! (REGNO_FIRST_LUID (m1->regno) > last
1950 || REGNO_LAST_LUID (m1->regno) < first))
1951 goto overlap;
1953 /* No overlap: we can combine this with the others. */
1954 m0->lifetime += m->lifetime;
1955 m0->savings += m->savings;
1956 m->done = 1;
1957 m->match = m0;
1959 overlap:
1964 /* Clean up. */
1965 free (matched_regs);
1968 /* Returns the number of movable instructions in LOOP that were not
1969 moved outside the loop. */
1971 static int
1972 num_unmoved_movables (const struct loop *loop)
1974 int num = 0;
1975 struct movable *m;
1977 for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1978 if (!m->done)
1979 ++num;
1981 return num;
1985 /* Return 1 if regs X and Y will become the same if moved. */
1987 static int
1988 regs_match_p (rtx x, rtx y, struct loop_movables *movables)
1990 unsigned int xn = REGNO (x);
1991 unsigned int yn = REGNO (y);
1992 struct movable *mx, *my;
1994 for (mx = movables->head; mx; mx = mx->next)
1995 if (mx->regno == xn)
1996 break;
1998 for (my = movables->head; my; my = my->next)
1999 if (my->regno == yn)
2000 break;
2002 return (mx && my
2003 && ((mx->match == my->match && mx->match != 0)
2004 || mx->match == my
2005 || mx == my->match));
2008 /* Return 1 if X and Y are identical-looking rtx's.
2009 This is the Lisp function EQUAL for rtx arguments.
2011 If two registers are matching movables or a movable register and an
2012 equivalent constant, consider them equal. */
2014 static int
2015 rtx_equal_for_loop_p (rtx x, rtx y, struct loop_movables *movables,
2016 struct loop_regs *regs)
2018 int i;
2019 int j;
2020 struct movable *m;
2021 enum rtx_code code;
2022 const char *fmt;
2024 if (x == y)
2025 return 1;
2026 if (x == 0 || y == 0)
2027 return 0;
2029 code = GET_CODE (x);
2031 /* If we have a register and a constant, they may sometimes be
2032 equal. */
2033 if (REG_P (x) && regs->array[REGNO (x)].set_in_loop == -2
2034 && CONSTANT_P (y))
2036 for (m = movables->head; m; m = m->next)
2037 if (m->move_insn && m->regno == REGNO (x)
2038 && rtx_equal_p (m->set_src, y))
2039 return 1;
2041 else if (REG_P (y) && regs->array[REGNO (y)].set_in_loop == -2
2042 && CONSTANT_P (x))
2044 for (m = movables->head; m; m = m->next)
2045 if (m->move_insn && m->regno == REGNO (y)
2046 && rtx_equal_p (m->set_src, x))
2047 return 1;
2050 /* Otherwise, rtx's of different codes cannot be equal. */
2051 if (code != GET_CODE (y))
2052 return 0;
2054 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
2055 (REG:SI x) and (REG:HI x) are NOT equivalent. */
2057 if (GET_MODE (x) != GET_MODE (y))
2058 return 0;
2060 /* These three types of rtx's can be compared nonrecursively. */
2061 if (code == REG)
2062 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
2064 if (code == LABEL_REF)
2065 return XEXP (x, 0) == XEXP (y, 0);
2066 if (code == SYMBOL_REF)
2067 return XSTR (x, 0) == XSTR (y, 0);
2069 /* Compare the elements. If any pair of corresponding elements
2070 fail to match, return 0 for the whole things. */
2072 fmt = GET_RTX_FORMAT (code);
2073 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2075 switch (fmt[i])
2077 case 'w':
2078 if (XWINT (x, i) != XWINT (y, i))
2079 return 0;
2080 break;
2082 case 'i':
2083 if (XINT (x, i) != XINT (y, i))
2084 return 0;
2085 break;
2087 case 'E':
2088 /* Two vectors must have the same length. */
2089 if (XVECLEN (x, i) != XVECLEN (y, i))
2090 return 0;
2092 /* And the corresponding elements must match. */
2093 for (j = 0; j < XVECLEN (x, i); j++)
2094 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
2095 movables, regs) == 0)
2096 return 0;
2097 break;
2099 case 'e':
2100 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
2101 == 0)
2102 return 0;
2103 break;
2105 case 's':
2106 if (strcmp (XSTR (x, i), XSTR (y, i)))
2107 return 0;
2108 break;
2110 case 'u':
2111 /* These are just backpointers, so they don't matter. */
2112 break;
2114 case '0':
2115 break;
2117 /* It is believed that rtx's at this level will never
2118 contain anything but integers and other rtx's,
2119 except for within LABEL_REFs and SYMBOL_REFs. */
2120 default:
2121 gcc_unreachable ();
2124 return 1;
2127 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
2128 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
2129 references is incremented once for each added note. */
2131 static void
2132 add_label_notes (rtx x, rtx insns)
2134 enum rtx_code code = GET_CODE (x);
2135 int i, j;
2136 const char *fmt;
2137 rtx insn;
2139 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
2141 /* This code used to ignore labels that referred to dispatch tables to
2142 avoid flow generating (slightly) worse code.
2144 We no longer ignore such label references (see LABEL_REF handling in
2145 mark_jump_label for additional information). */
2146 for (insn = insns; insn; insn = NEXT_INSN (insn))
2147 if (reg_mentioned_p (XEXP (x, 0), insn))
2149 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
2150 REG_NOTES (insn));
2151 if (LABEL_P (XEXP (x, 0)))
2152 LABEL_NUSES (XEXP (x, 0))++;
2156 fmt = GET_RTX_FORMAT (code);
2157 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2159 if (fmt[i] == 'e')
2160 add_label_notes (XEXP (x, i), insns);
2161 else if (fmt[i] == 'E')
2162 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2163 add_label_notes (XVECEXP (x, i, j), insns);
2167 /* Scan MOVABLES, and move the insns that deserve to be moved.
2168 If two matching movables are combined, replace one reg with the
2169 other throughout. */
2171 static void
2172 move_movables (struct loop *loop, struct loop_movables *movables,
2173 int threshold, int insn_count)
2175 struct loop_regs *regs = LOOP_REGS (loop);
2176 int nregs = regs->num;
2177 rtx new_start = 0;
2178 struct movable *m;
2179 rtx p;
2180 rtx loop_start = loop->start;
2181 rtx loop_end = loop->end;
2182 /* Map of pseudo-register replacements to handle combining
2183 when we move several insns that load the same value
2184 into different pseudo-registers. */
2185 rtx *reg_map = xcalloc (nregs, sizeof (rtx));
2186 char *already_moved = xcalloc (nregs, sizeof (char));
2188 for (m = movables->head; m; m = m->next)
2190 /* Describe this movable insn. */
2192 if (loop_dump_stream)
2194 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
2195 INSN_UID (m->insn), m->regno, m->lifetime);
2196 if (m->consec > 0)
2197 fprintf (loop_dump_stream, "consec %d, ", m->consec);
2198 if (m->cond)
2199 fprintf (loop_dump_stream, "cond ");
2200 if (m->force)
2201 fprintf (loop_dump_stream, "force ");
2202 if (m->global)
2203 fprintf (loop_dump_stream, "global ");
2204 if (m->done)
2205 fprintf (loop_dump_stream, "done ");
2206 if (m->move_insn)
2207 fprintf (loop_dump_stream, "move-insn ");
2208 if (m->match)
2209 fprintf (loop_dump_stream, "matches %d ",
2210 INSN_UID (m->match->insn));
2211 if (m->forces)
2212 fprintf (loop_dump_stream, "forces %d ",
2213 INSN_UID (m->forces->insn));
2216 /* Ignore the insn if it's already done (it matched something else).
2217 Otherwise, see if it is now safe to move. */
2219 if (!m->done
2220 && (! m->cond
2221 || (1 == loop_invariant_p (loop, m->set_src)
2222 && (m->dependencies == 0
2223 || 1 == loop_invariant_p (loop, m->dependencies))
2224 && (m->consec == 0
2225 || 1 == consec_sets_invariant_p (loop, m->set_dest,
2226 m->consec + 1,
2227 m->insn))))
2228 && (! m->forces || m->forces->done))
2230 int regno;
2231 rtx p;
2232 int savings = m->savings;
2234 /* We have an insn that is safe to move.
2235 Compute its desirability. */
2237 p = m->insn;
2238 regno = m->regno;
2240 if (loop_dump_stream)
2241 fprintf (loop_dump_stream, "savings %d ", savings);
2243 if (regs->array[regno].moved_once && loop_dump_stream)
2244 fprintf (loop_dump_stream, "halved since already moved ");
2246 /* An insn MUST be moved if we already moved something else
2247 which is safe only if this one is moved too: that is,
2248 if already_moved[REGNO] is nonzero. */
2250 /* An insn is desirable to move if the new lifetime of the
2251 register is no more than THRESHOLD times the old lifetime.
2252 If it's not desirable, it means the loop is so big
2253 that moving won't speed things up much,
2254 and it is liable to make register usage worse. */
2256 /* It is also desirable to move if it can be moved at no
2257 extra cost because something else was already moved. */
2259 if (already_moved[regno]
2260 || (threshold * savings * m->lifetime) >=
2261 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
2262 || (m->forces && m->forces->done
2263 && regs->array[m->forces->regno].n_times_set == 1))
2265 int count;
2266 struct movable *m1;
2267 rtx first = NULL_RTX;
2268 rtx newreg = NULL_RTX;
2270 if (m->insert_temp)
2271 newreg = gen_reg_rtx (GET_MODE (m->set_dest));
2273 /* Now move the insns that set the reg. */
2275 if (m->partial && m->match)
2277 rtx newpat, i1;
2278 rtx r1, r2;
2279 /* Find the end of this chain of matching regs.
2280 Thus, we load each reg in the chain from that one reg.
2281 And that reg is loaded with 0 directly,
2282 since it has ->match == 0. */
2283 for (m1 = m; m1->match; m1 = m1->match);
2284 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
2285 SET_DEST (PATTERN (m1->insn)));
2286 i1 = loop_insn_hoist (loop, newpat);
2288 /* Mark the moved, invariant reg as being allowed to
2289 share a hard reg with the other matching invariant. */
2290 REG_NOTES (i1) = REG_NOTES (m->insn);
2291 r1 = SET_DEST (PATTERN (m->insn));
2292 r2 = SET_DEST (PATTERN (m1->insn));
2293 regs_may_share
2294 = gen_rtx_EXPR_LIST (VOIDmode, r1,
2295 gen_rtx_EXPR_LIST (VOIDmode, r2,
2296 regs_may_share));
2297 delete_insn (m->insn);
2299 if (new_start == 0)
2300 new_start = i1;
2302 if (loop_dump_stream)
2303 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
2305 /* If we are to re-generate the item being moved with a
2306 new move insn, first delete what we have and then emit
2307 the move insn before the loop. */
2308 else if (m->move_insn)
2310 rtx i1, temp, seq;
2312 for (count = m->consec; count >= 0; count--)
2314 if (!NOTE_P (p))
2316 /* If this is the first insn of a library
2317 call sequence, something is very
2318 wrong. */
2319 gcc_assert (!find_reg_note
2320 (p, REG_LIBCALL, NULL_RTX));
2322 /* If this is the last insn of a libcall
2323 sequence, then delete every insn in the
2324 sequence except the last. The last insn
2325 is handled in the normal manner. */
2326 temp = find_reg_note (p, REG_RETVAL, NULL_RTX);
2328 if (temp)
2330 temp = XEXP (temp, 0);
2331 while (temp != p)
2332 temp = delete_insn (temp);
2336 temp = p;
2337 p = delete_insn (p);
2339 /* simplify_giv_expr expects that it can walk the insns
2340 at m->insn forwards and see this old sequence we are
2341 tossing here. delete_insn does preserve the next
2342 pointers, but when we skip over a NOTE we must fix
2343 it up. Otherwise that code walks into the non-deleted
2344 insn stream. */
2345 while (p && NOTE_P (p))
2346 p = NEXT_INSN (temp) = NEXT_INSN (p);
2348 if (m->insert_temp)
2350 /* Replace the original insn with a move from
2351 our newly created temp. */
2352 start_sequence ();
2353 emit_move_insn (m->set_dest, newreg);
2354 seq = get_insns ();
2355 end_sequence ();
2356 emit_insn_before (seq, p);
2360 start_sequence ();
2361 emit_move_insn (m->insert_temp ? newreg : m->set_dest,
2362 m->set_src);
2363 seq = get_insns ();
2364 end_sequence ();
2366 add_label_notes (m->set_src, seq);
2368 i1 = loop_insn_hoist (loop, seq);
2369 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2370 set_unique_reg_note (i1,
2371 m->is_equiv ? REG_EQUIV : REG_EQUAL,
2372 m->set_src);
2374 if (loop_dump_stream)
2375 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
2377 /* The more regs we move, the less we like moving them. */
2378 threshold -= 3;
2380 else
2382 for (count = m->consec; count >= 0; count--)
2384 rtx i1, temp;
2386 /* If first insn of libcall sequence, skip to end. */
2387 /* Do this at start of loop, since p is guaranteed to
2388 be an insn here. */
2389 if (!NOTE_P (p)
2390 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
2391 p = XEXP (temp, 0);
2393 /* If last insn of libcall sequence, move all
2394 insns except the last before the loop. The last
2395 insn is handled in the normal manner. */
2396 if (!NOTE_P (p)
2397 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
2399 rtx fn_address = 0;
2400 rtx fn_reg = 0;
2401 rtx fn_address_insn = 0;
2403 first = 0;
2404 for (temp = XEXP (temp, 0); temp != p;
2405 temp = NEXT_INSN (temp))
2407 rtx body;
2408 rtx n;
2409 rtx next;
2411 if (NOTE_P (temp))
2412 continue;
2414 body = PATTERN (temp);
2416 /* Find the next insn after TEMP,
2417 not counting USE or NOTE insns. */
2418 for (next = NEXT_INSN (temp); next != p;
2419 next = NEXT_INSN (next))
2420 if (! (NONJUMP_INSN_P (next)
2421 && GET_CODE (PATTERN (next)) == USE)
2422 && !NOTE_P (next))
2423 break;
2425 /* If that is the call, this may be the insn
2426 that loads the function address.
2428 Extract the function address from the insn
2429 that loads it into a register.
2430 If this insn was cse'd, we get incorrect code.
2432 So emit a new move insn that copies the
2433 function address into the register that the
2434 call insn will use. flow.c will delete any
2435 redundant stores that we have created. */
2436 if (CALL_P (next)
2437 && GET_CODE (body) == SET
2438 && REG_P (SET_DEST (body))
2439 && (n = find_reg_note (temp, REG_EQUAL,
2440 NULL_RTX)))
2442 fn_reg = SET_SRC (body);
2443 if (!REG_P (fn_reg))
2444 fn_reg = SET_DEST (body);
2445 fn_address = XEXP (n, 0);
2446 fn_address_insn = temp;
2448 /* We have the call insn.
2449 If it uses the register we suspect it might,
2450 load it with the correct address directly. */
2451 if (CALL_P (temp)
2452 && fn_address != 0
2453 && reg_referenced_p (fn_reg, body))
2454 loop_insn_emit_after (loop, 0, fn_address_insn,
2455 gen_move_insn
2456 (fn_reg, fn_address));
2458 if (CALL_P (temp))
2460 i1 = loop_call_insn_hoist (loop, body);
2461 /* Because the USAGE information potentially
2462 contains objects other than hard registers
2463 we need to copy it. */
2464 if (CALL_INSN_FUNCTION_USAGE (temp))
2465 CALL_INSN_FUNCTION_USAGE (i1)
2466 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2468 else
2469 i1 = loop_insn_hoist (loop, body);
2470 if (first == 0)
2471 first = i1;
2472 if (temp == fn_address_insn)
2473 fn_address_insn = i1;
2474 REG_NOTES (i1) = REG_NOTES (temp);
2475 REG_NOTES (temp) = NULL;
2476 delete_insn (temp);
2478 if (new_start == 0)
2479 new_start = first;
2481 if (m->savemode != VOIDmode)
2483 /* P sets REG to zero; but we should clear only
2484 the bits that are not covered by the mode
2485 m->savemode. */
2486 rtx reg = m->set_dest;
2487 rtx sequence;
2488 rtx tem;
2490 start_sequence ();
2491 tem = expand_simple_binop
2492 (GET_MODE (reg), AND, reg,
2493 GEN_INT ((((HOST_WIDE_INT) 1
2494 << GET_MODE_BITSIZE (m->savemode)))
2495 - 1),
2496 reg, 1, OPTAB_LIB_WIDEN);
2497 gcc_assert (tem);
2498 if (tem != reg)
2499 emit_move_insn (reg, tem);
2500 sequence = get_insns ();
2501 end_sequence ();
2502 i1 = loop_insn_hoist (loop, sequence);
2504 else if (CALL_P (p))
2506 i1 = loop_call_insn_hoist (loop, PATTERN (p));
2507 /* Because the USAGE information potentially
2508 contains objects other than hard registers
2509 we need to copy it. */
2510 if (CALL_INSN_FUNCTION_USAGE (p))
2511 CALL_INSN_FUNCTION_USAGE (i1)
2512 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2514 else if (count == m->consec && m->move_insn_first)
2516 rtx seq;
2517 /* The SET_SRC might not be invariant, so we must
2518 use the REG_EQUAL note. */
2519 start_sequence ();
2520 emit_move_insn (m->insert_temp ? newreg : m->set_dest,
2521 m->set_src);
2522 seq = get_insns ();
2523 end_sequence ();
2525 add_label_notes (m->set_src, seq);
2527 i1 = loop_insn_hoist (loop, seq);
2528 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2529 set_unique_reg_note (i1, m->is_equiv ? REG_EQUIV
2530 : REG_EQUAL, m->set_src);
2532 else if (m->insert_temp)
2534 rtx *reg_map2 = xcalloc (REGNO (newreg),
2535 sizeof(rtx));
2536 reg_map2 [m->regno] = newreg;
2538 i1 = loop_insn_hoist (loop, copy_rtx (PATTERN (p)));
2539 replace_regs (i1, reg_map2, REGNO (newreg), 1);
2540 free (reg_map2);
2542 else
2543 i1 = loop_insn_hoist (loop, PATTERN (p));
2545 if (REG_NOTES (i1) == 0)
2547 REG_NOTES (i1) = REG_NOTES (p);
2548 REG_NOTES (p) = NULL;
2550 /* If there is a REG_EQUAL note present whose value
2551 is not loop invariant, then delete it, since it
2552 may cause problems with later optimization passes.
2553 It is possible for cse to create such notes
2554 like this as a result of record_jump_cond. */
2556 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2557 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2558 remove_note (i1, temp);
2561 if (new_start == 0)
2562 new_start = i1;
2564 if (loop_dump_stream)
2565 fprintf (loop_dump_stream, " moved to %d",
2566 INSN_UID (i1));
2568 /* If library call, now fix the REG_NOTES that contain
2569 insn pointers, namely REG_LIBCALL on FIRST
2570 and REG_RETVAL on I1. */
2571 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2573 XEXP (temp, 0) = first;
2574 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2575 XEXP (temp, 0) = i1;
2578 temp = p;
2579 delete_insn (p);
2580 p = NEXT_INSN (p);
2582 /* simplify_giv_expr expects that it can walk the insns
2583 at m->insn forwards and see this old sequence we are
2584 tossing here. delete_insn does preserve the next
2585 pointers, but when we skip over a NOTE we must fix
2586 it up. Otherwise that code walks into the non-deleted
2587 insn stream. */
2588 while (p && NOTE_P (p))
2589 p = NEXT_INSN (temp) = NEXT_INSN (p);
2591 if (m->insert_temp)
2593 rtx seq;
2594 /* Replace the original insn with a move from
2595 our newly created temp. */
2596 start_sequence ();
2597 emit_move_insn (m->set_dest, newreg);
2598 seq = get_insns ();
2599 end_sequence ();
2600 emit_insn_before (seq, p);
2604 /* The more regs we move, the less we like moving them. */
2605 threshold -= 3;
2608 m->done = 1;
2610 if (!m->insert_temp)
2612 /* Any other movable that loads the same register
2613 MUST be moved. */
2614 already_moved[regno] = 1;
2616 /* This reg has been moved out of one loop. */
2617 regs->array[regno].moved_once = 1;
2619 /* The reg set here is now invariant. */
2620 if (! m->partial)
2622 int i;
2623 for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2624 regs->array[regno+i].set_in_loop = 0;
2627 /* Change the length-of-life info for the register
2628 to say it lives at least the full length of this loop.
2629 This will help guide optimizations in outer loops. */
2631 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2632 /* This is the old insn before all the moved insns.
2633 We can't use the moved insn because it is out of range
2634 in uid_luid. Only the old insns have luids. */
2635 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2636 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2637 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2640 /* Combine with this moved insn any other matching movables. */
2642 if (! m->partial)
2643 for (m1 = movables->head; m1; m1 = m1->next)
2644 if (m1->match == m)
2646 rtx temp;
2648 /* Schedule the reg loaded by M1
2649 for replacement so that shares the reg of M.
2650 If the modes differ (only possible in restricted
2651 circumstances, make a SUBREG.
2653 Note this assumes that the target dependent files
2654 treat REG and SUBREG equally, including within
2655 GO_IF_LEGITIMATE_ADDRESS and in all the
2656 predicates since we never verify that replacing the
2657 original register with a SUBREG results in a
2658 recognizable insn. */
2659 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2660 reg_map[m1->regno] = m->set_dest;
2661 else
2662 reg_map[m1->regno]
2663 = gen_lowpart_common (GET_MODE (m1->set_dest),
2664 m->set_dest);
2666 /* Get rid of the matching insn
2667 and prevent further processing of it. */
2668 m1->done = 1;
2670 /* If library call, delete all insns. */
2671 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2672 NULL_RTX)))
2673 delete_insn_chain (XEXP (temp, 0), m1->insn);
2674 else
2675 delete_insn (m1->insn);
2677 /* Any other movable that loads the same register
2678 MUST be moved. */
2679 already_moved[m1->regno] = 1;
2681 /* The reg merged here is now invariant,
2682 if the reg it matches is invariant. */
2683 if (! m->partial)
2685 int i;
2686 for (i = 0;
2687 i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2688 i++)
2689 regs->array[m1->regno+i].set_in_loop = 0;
2693 else if (loop_dump_stream)
2694 fprintf (loop_dump_stream, "not desirable");
2696 else if (loop_dump_stream && !m->match)
2697 fprintf (loop_dump_stream, "not safe");
2699 if (loop_dump_stream)
2700 fprintf (loop_dump_stream, "\n");
2703 if (new_start == 0)
2704 new_start = loop_start;
2706 /* Go through all the instructions in the loop, making
2707 all the register substitutions scheduled in REG_MAP. */
2708 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2709 if (INSN_P (p))
2711 replace_regs (PATTERN (p), reg_map, nregs, 0);
2712 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2713 INSN_CODE (p) = -1;
2716 /* Clean up. */
2717 free (reg_map);
2718 free (already_moved);
2722 static void
2723 loop_movables_add (struct loop_movables *movables, struct movable *m)
2725 if (movables->head == 0)
2726 movables->head = m;
2727 else
2728 movables->last->next = m;
2729 movables->last = m;
2733 static void
2734 loop_movables_free (struct loop_movables *movables)
2736 struct movable *m;
2737 struct movable *m_next;
2739 for (m = movables->head; m; m = m_next)
2741 m_next = m->next;
2742 free (m);
2746 #if 0
2747 /* Scan X and replace the address of any MEM in it with ADDR.
2748 REG is the address that MEM should have before the replacement. */
2750 static void
2751 replace_call_address (rtx x, rtx reg, rtx addr)
2753 enum rtx_code code;
2754 int i;
2755 const char *fmt;
2757 if (x == 0)
2758 return;
2759 code = GET_CODE (x);
2760 switch (code)
2762 case PC:
2763 case CC0:
2764 case CONST_INT:
2765 case CONST_DOUBLE:
2766 case CONST:
2767 case SYMBOL_REF:
2768 case LABEL_REF:
2769 case REG:
2770 return;
2772 case SET:
2773 /* Short cut for very common case. */
2774 replace_call_address (XEXP (x, 1), reg, addr);
2775 return;
2777 case CALL:
2778 /* Short cut for very common case. */
2779 replace_call_address (XEXP (x, 0), reg, addr);
2780 return;
2782 case MEM:
2783 /* If this MEM uses a reg other than the one we expected,
2784 something is wrong. */
2785 gcc_assert (XEXP (x, 0) == reg);
2786 XEXP (x, 0) = addr;
2787 return;
2789 default:
2790 break;
2793 fmt = GET_RTX_FORMAT (code);
2794 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2796 if (fmt[i] == 'e')
2797 replace_call_address (XEXP (x, i), reg, addr);
2798 else if (fmt[i] == 'E')
2800 int j;
2801 for (j = 0; j < XVECLEN (x, i); j++)
2802 replace_call_address (XVECEXP (x, i, j), reg, addr);
2806 #endif
2808 /* Return the number of memory refs to addresses that vary
2809 in the rtx X. */
2811 static int
2812 count_nonfixed_reads (const struct loop *loop, rtx x)
2814 enum rtx_code code;
2815 int i;
2816 const char *fmt;
2817 int value;
2819 if (x == 0)
2820 return 0;
2822 code = GET_CODE (x);
2823 switch (code)
2825 case PC:
2826 case CC0:
2827 case CONST_INT:
2828 case CONST_DOUBLE:
2829 case CONST:
2830 case SYMBOL_REF:
2831 case LABEL_REF:
2832 case REG:
2833 return 0;
2835 case MEM:
2836 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2837 + count_nonfixed_reads (loop, XEXP (x, 0)));
2839 default:
2840 break;
2843 value = 0;
2844 fmt = GET_RTX_FORMAT (code);
2845 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2847 if (fmt[i] == 'e')
2848 value += count_nonfixed_reads (loop, XEXP (x, i));
2849 if (fmt[i] == 'E')
2851 int j;
2852 for (j = 0; j < XVECLEN (x, i); j++)
2853 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2856 return value;
2859 /* Scan a loop setting the elements `loops_enclosed',
2860 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2861 `unknown_address_altered', `unknown_constant_address_altered', and
2862 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2863 list `store_mems' in LOOP. */
2865 static void
2866 prescan_loop (struct loop *loop)
2868 int level = 1;
2869 rtx insn;
2870 struct loop_info *loop_info = LOOP_INFO (loop);
2871 rtx start = loop->start;
2872 rtx end = loop->end;
2873 /* The label after END. Jumping here is just like falling off the
2874 end of the loop. We use next_nonnote_insn instead of next_label
2875 as a hedge against the (pathological) case where some actual insn
2876 might end up between the two. */
2877 rtx exit_target = next_nonnote_insn (end);
2879 loop_info->has_indirect_jump = indirect_jump_in_function;
2880 loop_info->pre_header_has_call = 0;
2881 loop_info->has_call = 0;
2882 loop_info->has_nonconst_call = 0;
2883 loop_info->has_prefetch = 0;
2884 loop_info->has_volatile = 0;
2885 loop_info->has_tablejump = 0;
2886 loop_info->has_multiple_exit_targets = 0;
2887 loop->level = 1;
2889 loop_info->unknown_address_altered = 0;
2890 loop_info->unknown_constant_address_altered = 0;
2891 loop_info->store_mems = NULL_RTX;
2892 loop_info->first_loop_store_insn = NULL_RTX;
2893 loop_info->mems_idx = 0;
2894 loop_info->num_mem_sets = 0;
2896 for (insn = start; insn && !LABEL_P (insn);
2897 insn = PREV_INSN (insn))
2899 if (CALL_P (insn))
2901 loop_info->pre_header_has_call = 1;
2902 break;
2906 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2907 insn = NEXT_INSN (insn))
2909 switch (GET_CODE (insn))
2911 case NOTE:
2912 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2914 ++level;
2915 /* Count number of loops contained in this one. */
2916 loop->level++;
2918 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2919 --level;
2920 break;
2922 case CALL_INSN:
2923 if (! CONST_OR_PURE_CALL_P (insn))
2925 loop_info->unknown_address_altered = 1;
2926 loop_info->has_nonconst_call = 1;
2928 else if (pure_call_p (insn))
2929 loop_info->has_nonconst_call = 1;
2930 loop_info->has_call = 1;
2931 if (can_throw_internal (insn))
2932 loop_info->has_multiple_exit_targets = 1;
2933 break;
2935 case JUMP_INSN:
2936 if (! loop_info->has_multiple_exit_targets)
2938 rtx set = pc_set (insn);
2940 if (set)
2942 rtx src = SET_SRC (set);
2943 rtx label1, label2;
2945 if (GET_CODE (src) == IF_THEN_ELSE)
2947 label1 = XEXP (src, 1);
2948 label2 = XEXP (src, 2);
2950 else
2952 label1 = src;
2953 label2 = NULL_RTX;
2958 if (label1 && label1 != pc_rtx)
2960 if (GET_CODE (label1) != LABEL_REF)
2962 /* Something tricky. */
2963 loop_info->has_multiple_exit_targets = 1;
2964 break;
2966 else if (XEXP (label1, 0) != exit_target
2967 && LABEL_OUTSIDE_LOOP_P (label1))
2969 /* A jump outside the current loop. */
2970 loop_info->has_multiple_exit_targets = 1;
2971 break;
2975 label1 = label2;
2976 label2 = NULL_RTX;
2978 while (label1);
2980 else
2982 /* A return, or something tricky. */
2983 loop_info->has_multiple_exit_targets = 1;
2986 /* Fall through. */
2988 case INSN:
2989 if (volatile_refs_p (PATTERN (insn)))
2990 loop_info->has_volatile = 1;
2992 if (JUMP_P (insn)
2993 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2994 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2995 loop_info->has_tablejump = 1;
2997 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2998 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2999 loop_info->first_loop_store_insn = insn;
3001 if (flag_non_call_exceptions && can_throw_internal (insn))
3002 loop_info->has_multiple_exit_targets = 1;
3003 break;
3005 default:
3006 break;
3010 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
3011 if (/* An exception thrown by a called function might land us
3012 anywhere. */
3013 ! loop_info->has_nonconst_call
3014 /* We don't want loads for MEMs moved to a location before the
3015 one at which their stack memory becomes allocated. (Note
3016 that this is not a problem for malloc, etc., since those
3017 require actual function calls. */
3018 && ! current_function_calls_alloca
3019 /* There are ways to leave the loop other than falling off the
3020 end. */
3021 && ! loop_info->has_multiple_exit_targets)
3022 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
3023 insn = NEXT_INSN (insn))
3024 for_each_rtx (&insn, insert_loop_mem, loop_info);
3026 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
3027 that loop_invariant_p and load_mems can use true_dependence
3028 to determine what is really clobbered. */
3029 if (loop_info->unknown_address_altered)
3031 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
3033 loop_info->store_mems
3034 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
3036 if (loop_info->unknown_constant_address_altered)
3038 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
3039 MEM_READONLY_P (mem) = 1;
3040 loop_info->store_mems
3041 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
3045 /* Invalidate all loops containing LABEL. */
3047 static void
3048 invalidate_loops_containing_label (rtx label)
3050 struct loop *loop;
3051 for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
3052 loop->invalid = 1;
3055 /* Scan the function looking for loops. Record the start and end of each loop.
3056 Also mark as invalid loops any loops that contain a setjmp or are branched
3057 to from outside the loop. */
3059 static void
3060 find_and_verify_loops (rtx f, struct loops *loops)
3062 rtx insn;
3063 rtx label;
3064 int num_loops;
3065 struct loop *current_loop;
3066 struct loop *next_loop;
3067 struct loop *loop;
3069 num_loops = loops->num;
3071 compute_luids (f, NULL_RTX, 0);
3073 /* If there are jumps to undefined labels,
3074 treat them as jumps out of any/all loops.
3075 This also avoids writing past end of tables when there are no loops. */
3076 uid_loop[0] = NULL;
3078 /* Find boundaries of loops, mark which loops are contained within
3079 loops, and invalidate loops that have setjmp. */
3081 num_loops = 0;
3082 current_loop = NULL;
3083 for (insn = f; insn; insn = NEXT_INSN (insn))
3085 if (NOTE_P (insn))
3086 switch (NOTE_LINE_NUMBER (insn))
3088 case NOTE_INSN_LOOP_BEG:
3089 next_loop = loops->array + num_loops;
3090 next_loop->num = num_loops;
3091 num_loops++;
3092 next_loop->start = insn;
3093 next_loop->outer = current_loop;
3094 current_loop = next_loop;
3095 break;
3097 case NOTE_INSN_LOOP_END:
3098 gcc_assert (current_loop);
3100 current_loop->end = insn;
3101 current_loop = current_loop->outer;
3102 break;
3104 default:
3105 break;
3108 if (CALL_P (insn)
3109 && find_reg_note (insn, REG_SETJMP, NULL))
3111 /* In this case, we must invalidate our current loop and any
3112 enclosing loop. */
3113 for (loop = current_loop; loop; loop = loop->outer)
3115 loop->invalid = 1;
3116 if (loop_dump_stream)
3117 fprintf (loop_dump_stream,
3118 "\nLoop at %d ignored due to setjmp.\n",
3119 INSN_UID (loop->start));
3123 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
3124 enclosing loop, but this doesn't matter. */
3125 uid_loop[INSN_UID (insn)] = current_loop;
3128 /* Any loop containing a label used in an initializer must be invalidated,
3129 because it can be jumped into from anywhere. */
3130 for (label = forced_labels; label; label = XEXP (label, 1))
3131 invalidate_loops_containing_label (XEXP (label, 0));
3133 /* Any loop containing a label used for an exception handler must be
3134 invalidated, because it can be jumped into from anywhere. */
3135 for_each_eh_label (invalidate_loops_containing_label);
3137 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
3138 loop that it is not contained within, that loop is marked invalid.
3139 If any INSN or CALL_INSN uses a label's address, then the loop containing
3140 that label is marked invalid, because it could be jumped into from
3141 anywhere.
3143 Also look for blocks of code ending in an unconditional branch that
3144 exits the loop. If such a block is surrounded by a conditional
3145 branch around the block, move the block elsewhere (see below) and
3146 invert the jump to point to the code block. This may eliminate a
3147 label in our loop and will simplify processing by both us and a
3148 possible second cse pass. */
3150 for (insn = f; insn; insn = NEXT_INSN (insn))
3151 if (INSN_P (insn))
3153 struct loop *this_loop = uid_loop[INSN_UID (insn)];
3155 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
3157 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
3158 if (note)
3159 invalidate_loops_containing_label (XEXP (note, 0));
3162 if (!JUMP_P (insn))
3163 continue;
3165 mark_loop_jump (PATTERN (insn), this_loop);
3167 /* See if this is an unconditional branch outside the loop. */
3168 if (this_loop
3169 && (GET_CODE (PATTERN (insn)) == RETURN
3170 || (any_uncondjump_p (insn)
3171 && onlyjump_p (insn)
3172 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
3173 != this_loop)))
3174 && get_max_uid () < max_uid_for_loop)
3176 rtx p;
3177 rtx our_next = next_real_insn (insn);
3178 rtx last_insn_to_move = NEXT_INSN (insn);
3179 struct loop *dest_loop;
3180 struct loop *outer_loop = NULL;
3182 /* Go backwards until we reach the start of the loop, a label,
3183 or a JUMP_INSN. */
3184 for (p = PREV_INSN (insn);
3185 !LABEL_P (p)
3186 && ! (NOTE_P (p)
3187 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3188 && !JUMP_P (p);
3189 p = PREV_INSN (p))
3192 /* Check for the case where we have a jump to an inner nested
3193 loop, and do not perform the optimization in that case. */
3195 if (JUMP_LABEL (insn))
3197 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
3198 if (dest_loop)
3200 for (outer_loop = dest_loop; outer_loop;
3201 outer_loop = outer_loop->outer)
3202 if (outer_loop == this_loop)
3203 break;
3207 /* Make sure that the target of P is within the current loop. */
3209 if (JUMP_P (p) && JUMP_LABEL (p)
3210 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
3211 outer_loop = this_loop;
3213 /* If we stopped on a JUMP_INSN to the next insn after INSN,
3214 we have a block of code to try to move.
3216 We look backward and then forward from the target of INSN
3217 to find a BARRIER at the same loop depth as the target.
3218 If we find such a BARRIER, we make a new label for the start
3219 of the block, invert the jump in P and point it to that label,
3220 and move the block of code to the spot we found. */
3222 if (! outer_loop
3223 && JUMP_P (p)
3224 && JUMP_LABEL (p) != 0
3225 /* Just ignore jumps to labels that were never emitted.
3226 These always indicate compilation errors. */
3227 && INSN_UID (JUMP_LABEL (p)) != 0
3228 && any_condjump_p (p) && onlyjump_p (p)
3229 && next_real_insn (JUMP_LABEL (p)) == our_next
3230 /* If it's not safe to move the sequence, then we
3231 mustn't try. */
3232 && insns_safe_to_move_p (p, NEXT_INSN (insn),
3233 &last_insn_to_move))
3235 rtx target
3236 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
3237 struct loop *target_loop = uid_loop[INSN_UID (target)];
3238 rtx loc, loc2;
3239 rtx tmp;
3241 /* Search for possible garbage past the conditional jumps
3242 and look for the last barrier. */
3243 for (tmp = last_insn_to_move;
3244 tmp && !LABEL_P (tmp); tmp = NEXT_INSN (tmp))
3245 if (BARRIER_P (tmp))
3246 last_insn_to_move = tmp;
3248 for (loc = target; loc; loc = PREV_INSN (loc))
3249 if (BARRIER_P (loc)
3250 /* Don't move things inside a tablejump. */
3251 && ((loc2 = next_nonnote_insn (loc)) == 0
3252 || !LABEL_P (loc2)
3253 || (loc2 = next_nonnote_insn (loc2)) == 0
3254 || !JUMP_P (loc2)
3255 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
3256 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
3257 && uid_loop[INSN_UID (loc)] == target_loop)
3258 break;
3260 if (loc == 0)
3261 for (loc = target; loc; loc = NEXT_INSN (loc))
3262 if (BARRIER_P (loc)
3263 /* Don't move things inside a tablejump. */
3264 && ((loc2 = next_nonnote_insn (loc)) == 0
3265 || !LABEL_P (loc2)
3266 || (loc2 = next_nonnote_insn (loc2)) == 0
3267 || !JUMP_P (loc2)
3268 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
3269 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
3270 && uid_loop[INSN_UID (loc)] == target_loop)
3271 break;
3273 if (loc)
3275 rtx cond_label = JUMP_LABEL (p);
3276 rtx new_label = get_label_after (p);
3278 /* Ensure our label doesn't go away. */
3279 LABEL_NUSES (cond_label)++;
3281 /* Verify that uid_loop is large enough and that
3282 we can invert P. */
3283 if (invert_jump (p, new_label, 1))
3285 rtx q, r;
3286 bool only_notes;
3288 /* If no suitable BARRIER was found, create a suitable
3289 one before TARGET. Since TARGET is a fall through
3290 path, we'll need to insert a jump around our block
3291 and add a BARRIER before TARGET.
3293 This creates an extra unconditional jump outside
3294 the loop. However, the benefits of removing rarely
3295 executed instructions from inside the loop usually
3296 outweighs the cost of the extra unconditional jump
3297 outside the loop. */
3298 if (loc == 0)
3300 rtx temp;
3302 temp = gen_jump (JUMP_LABEL (insn));
3303 temp = emit_jump_insn_before (temp, target);
3304 JUMP_LABEL (temp) = JUMP_LABEL (insn);
3305 LABEL_NUSES (JUMP_LABEL (insn))++;
3306 loc = emit_barrier_before (target);
3309 /* Include the BARRIER after INSN and copy the
3310 block after LOC. */
3311 only_notes = squeeze_notes (&new_label,
3312 &last_insn_to_move);
3313 gcc_assert (!only_notes);
3315 reorder_insns (new_label, last_insn_to_move, loc);
3317 /* All those insns are now in TARGET_LOOP. */
3318 for (q = new_label;
3319 q != NEXT_INSN (last_insn_to_move);
3320 q = NEXT_INSN (q))
3321 uid_loop[INSN_UID (q)] = target_loop;
3323 /* The label jumped to by INSN is no longer a loop
3324 exit. Unless INSN does not have a label (e.g.,
3325 it is a RETURN insn), search loop->exit_labels
3326 to find its label_ref, and remove it. Also turn
3327 off LABEL_OUTSIDE_LOOP_P bit. */
3328 if (JUMP_LABEL (insn))
3330 for (q = 0, r = this_loop->exit_labels;
3332 q = r, r = LABEL_NEXTREF (r))
3333 if (XEXP (r, 0) == JUMP_LABEL (insn))
3335 LABEL_OUTSIDE_LOOP_P (r) = 0;
3336 if (q)
3337 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
3338 else
3339 this_loop->exit_labels = LABEL_NEXTREF (r);
3340 break;
3343 for (loop = this_loop; loop && loop != target_loop;
3344 loop = loop->outer)
3345 loop->exit_count--;
3347 /* If we didn't find it, then something is
3348 wrong. */
3349 gcc_assert (r);
3352 /* P is now a jump outside the loop, so it must be put
3353 in loop->exit_labels, and marked as such.
3354 The easiest way to do this is to just call
3355 mark_loop_jump again for P. */
3356 mark_loop_jump (PATTERN (p), this_loop);
3358 /* If INSN now jumps to the insn after it,
3359 delete INSN. */
3360 if (JUMP_LABEL (insn) != 0
3361 && (next_real_insn (JUMP_LABEL (insn))
3362 == next_real_insn (insn)))
3363 delete_related_insns (insn);
3366 /* Continue the loop after where the conditional
3367 branch used to jump, since the only branch insn
3368 in the block (if it still remains) is an inter-loop
3369 branch and hence needs no processing. */
3370 insn = NEXT_INSN (cond_label);
3372 if (--LABEL_NUSES (cond_label) == 0)
3373 delete_related_insns (cond_label);
3375 /* This loop will be continued with NEXT_INSN (insn). */
3376 insn = PREV_INSN (insn);
3383 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3384 loops it is contained in, mark the target loop invalid.
3386 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3388 static void
3389 mark_loop_jump (rtx x, struct loop *loop)
3391 struct loop *dest_loop;
3392 struct loop *outer_loop;
3393 int i;
3395 switch (GET_CODE (x))
3397 case PC:
3398 case USE:
3399 case CLOBBER:
3400 case REG:
3401 case MEM:
3402 case CONST_INT:
3403 case CONST_DOUBLE:
3404 case RETURN:
3405 return;
3407 case CONST:
3408 /* There could be a label reference in here. */
3409 mark_loop_jump (XEXP (x, 0), loop);
3410 return;
3412 case PLUS:
3413 case MINUS:
3414 case MULT:
3415 mark_loop_jump (XEXP (x, 0), loop);
3416 mark_loop_jump (XEXP (x, 1), loop);
3417 return;
3419 case LO_SUM:
3420 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3421 mark_loop_jump (XEXP (x, 1), loop);
3422 return;
3424 case SIGN_EXTEND:
3425 case ZERO_EXTEND:
3426 mark_loop_jump (XEXP (x, 0), loop);
3427 return;
3429 case LABEL_REF:
3430 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3432 /* Link together all labels that branch outside the loop. This
3433 is used by final_[bg]iv_value and the loop unrolling code. Also
3434 mark this LABEL_REF so we know that this branch should predict
3435 false. */
3437 /* A check to make sure the label is not in an inner nested loop,
3438 since this does not count as a loop exit. */
3439 if (dest_loop)
3441 for (outer_loop = dest_loop; outer_loop;
3442 outer_loop = outer_loop->outer)
3443 if (outer_loop == loop)
3444 break;
3446 else
3447 outer_loop = NULL;
3449 if (loop && ! outer_loop)
3451 LABEL_OUTSIDE_LOOP_P (x) = 1;
3452 LABEL_NEXTREF (x) = loop->exit_labels;
3453 loop->exit_labels = x;
3455 for (outer_loop = loop;
3456 outer_loop && outer_loop != dest_loop;
3457 outer_loop = outer_loop->outer)
3458 outer_loop->exit_count++;
3461 /* If this is inside a loop, but not in the current loop or one enclosed
3462 by it, it invalidates at least one loop. */
3464 if (! dest_loop)
3465 return;
3467 /* We must invalidate every nested loop containing the target of this
3468 label, except those that also contain the jump insn. */
3470 for (; dest_loop; dest_loop = dest_loop->outer)
3472 /* Stop when we reach a loop that also contains the jump insn. */
3473 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3474 if (dest_loop == outer_loop)
3475 return;
3477 /* If we get here, we know we need to invalidate a loop. */
3478 if (loop_dump_stream && ! dest_loop->invalid)
3479 fprintf (loop_dump_stream,
3480 "\nLoop at %d ignored due to multiple entry points.\n",
3481 INSN_UID (dest_loop->start));
3483 dest_loop->invalid = 1;
3485 return;
3487 case SET:
3488 /* If this is not setting pc, ignore. */
3489 if (SET_DEST (x) == pc_rtx)
3490 mark_loop_jump (SET_SRC (x), loop);
3491 return;
3493 case IF_THEN_ELSE:
3494 mark_loop_jump (XEXP (x, 1), loop);
3495 mark_loop_jump (XEXP (x, 2), loop);
3496 return;
3498 case PARALLEL:
3499 case ADDR_VEC:
3500 for (i = 0; i < XVECLEN (x, 0); i++)
3501 mark_loop_jump (XVECEXP (x, 0, i), loop);
3502 return;
3504 case ADDR_DIFF_VEC:
3505 for (i = 0; i < XVECLEN (x, 1); i++)
3506 mark_loop_jump (XVECEXP (x, 1, i), loop);
3507 return;
3509 default:
3510 /* Strictly speaking this is not a jump into the loop, only a possible
3511 jump out of the loop. However, we have no way to link the destination
3512 of this jump onto the list of exit labels. To be safe we mark this
3513 loop and any containing loops as invalid. */
3514 if (loop)
3516 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3518 if (loop_dump_stream && ! outer_loop->invalid)
3519 fprintf (loop_dump_stream,
3520 "\nLoop at %d ignored due to unknown exit jump.\n",
3521 INSN_UID (outer_loop->start));
3522 outer_loop->invalid = 1;
3525 return;
3529 /* Return nonzero if there is a label in the range from
3530 insn INSN to and including the insn whose luid is END
3531 INSN must have an assigned luid (i.e., it must not have
3532 been previously created by loop.c). */
3534 static int
3535 labels_in_range_p (rtx insn, int end)
3537 while (insn && INSN_LUID (insn) <= end)
3539 if (LABEL_P (insn))
3540 return 1;
3541 insn = NEXT_INSN (insn);
3544 return 0;
3547 /* Record that a memory reference X is being set. */
3549 static void
3550 note_addr_stored (rtx x, rtx y ATTRIBUTE_UNUSED,
3551 void *data ATTRIBUTE_UNUSED)
3553 struct loop_info *loop_info = data;
3555 if (x == 0 || !MEM_P (x))
3556 return;
3558 /* Count number of memory writes.
3559 This affects heuristics in strength_reduce. */
3560 loop_info->num_mem_sets++;
3562 /* BLKmode MEM means all memory is clobbered. */
3563 if (GET_MODE (x) == BLKmode)
3565 if (MEM_READONLY_P (x))
3566 loop_info->unknown_constant_address_altered = 1;
3567 else
3568 loop_info->unknown_address_altered = 1;
3570 return;
3573 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3574 loop_info->store_mems);
3577 /* X is a value modified by an INSN that references a biv inside a loop
3578 exit test (i.e., X is somehow related to the value of the biv). If X
3579 is a pseudo that is used more than once, then the biv is (effectively)
3580 used more than once. DATA is a pointer to a loop_regs structure. */
3582 static void
3583 note_set_pseudo_multiple_uses (rtx x, rtx y ATTRIBUTE_UNUSED, void *data)
3585 struct loop_regs *regs = (struct loop_regs *) data;
3587 if (x == 0)
3588 return;
3590 while (GET_CODE (x) == STRICT_LOW_PART
3591 || GET_CODE (x) == SIGN_EXTRACT
3592 || GET_CODE (x) == ZERO_EXTRACT
3593 || GET_CODE (x) == SUBREG)
3594 x = XEXP (x, 0);
3596 if (!REG_P (x) || REGNO (x) < FIRST_PSEUDO_REGISTER)
3597 return;
3599 /* If we do not have usage information, or if we know the register
3600 is used more than once, note that fact for check_dbra_loop. */
3601 if (REGNO (x) >= max_reg_before_loop
3602 || ! regs->array[REGNO (x)].single_usage
3603 || regs->array[REGNO (x)].single_usage == const0_rtx)
3604 regs->multiple_uses = 1;
3607 /* Return nonzero if the rtx X is invariant over the current loop.
3609 The value is 2 if we refer to something only conditionally invariant.
3611 A memory ref is invariant if it is not volatile and does not conflict
3612 with anything stored in `loop_info->store_mems'. */
3614 static int
3615 loop_invariant_p (const struct loop *loop, rtx x)
3617 struct loop_info *loop_info = LOOP_INFO (loop);
3618 struct loop_regs *regs = LOOP_REGS (loop);
3619 int i;
3620 enum rtx_code code;
3621 const char *fmt;
3622 int conditional = 0;
3623 rtx mem_list_entry;
3625 if (x == 0)
3626 return 1;
3627 code = GET_CODE (x);
3628 switch (code)
3630 case CONST_INT:
3631 case CONST_DOUBLE:
3632 case SYMBOL_REF:
3633 case CONST:
3634 return 1;
3636 case LABEL_REF:
3637 return 1;
3639 case PC:
3640 case CC0:
3641 case UNSPEC_VOLATILE:
3642 return 0;
3644 case REG:
3645 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3646 || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3647 && ! current_function_has_nonlocal_goto)
3648 return 1;
3650 if (LOOP_INFO (loop)->has_call
3651 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3652 return 0;
3654 /* Out-of-range regs can occur when we are called from unrolling.
3655 These registers created by the unroller are set in the loop,
3656 hence are never invariant.
3657 Other out-of-range regs can be generated by load_mems; those that
3658 are written to in the loop are not invariant, while those that are
3659 not written to are invariant. It would be easy for load_mems
3660 to set n_times_set correctly for these registers, however, there
3661 is no easy way to distinguish them from registers created by the
3662 unroller. */
3664 if (REGNO (x) >= (unsigned) regs->num)
3665 return 0;
3667 if (regs->array[REGNO (x)].set_in_loop < 0)
3668 return 2;
3670 return regs->array[REGNO (x)].set_in_loop == 0;
3672 case MEM:
3673 /* Volatile memory references must be rejected. Do this before
3674 checking for read-only items, so that volatile read-only items
3675 will be rejected also. */
3676 if (MEM_VOLATILE_P (x))
3677 return 0;
3679 /* See if there is any dependence between a store and this load. */
3680 mem_list_entry = loop_info->store_mems;
3681 while (mem_list_entry)
3683 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3684 x, rtx_varies_p))
3685 return 0;
3687 mem_list_entry = XEXP (mem_list_entry, 1);
3690 /* It's not invalidated by a store in memory
3691 but we must still verify the address is invariant. */
3692 break;
3694 case ASM_OPERANDS:
3695 /* Don't mess with insns declared volatile. */
3696 if (MEM_VOLATILE_P (x))
3697 return 0;
3698 break;
3700 default:
3701 break;
3704 fmt = GET_RTX_FORMAT (code);
3705 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3707 if (fmt[i] == 'e')
3709 int tem = loop_invariant_p (loop, XEXP (x, i));
3710 if (tem == 0)
3711 return 0;
3712 if (tem == 2)
3713 conditional = 1;
3715 else if (fmt[i] == 'E')
3717 int j;
3718 for (j = 0; j < XVECLEN (x, i); j++)
3720 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3721 if (tem == 0)
3722 return 0;
3723 if (tem == 2)
3724 conditional = 1;
3730 return 1 + conditional;
3733 /* Return nonzero if all the insns in the loop that set REG
3734 are INSN and the immediately following insns,
3735 and if each of those insns sets REG in an invariant way
3736 (not counting uses of REG in them).
3738 The value is 2 if some of these insns are only conditionally invariant.
3740 We assume that INSN itself is the first set of REG
3741 and that its source is invariant. */
3743 static int
3744 consec_sets_invariant_p (const struct loop *loop, rtx reg, int n_sets,
3745 rtx insn)
3747 struct loop_regs *regs = LOOP_REGS (loop);
3748 rtx p = insn;
3749 unsigned int regno = REGNO (reg);
3750 rtx temp;
3751 /* Number of sets we have to insist on finding after INSN. */
3752 int count = n_sets - 1;
3753 int old = regs->array[regno].set_in_loop;
3754 int value = 0;
3755 int this;
3757 /* If N_SETS hit the limit, we can't rely on its value. */
3758 if (n_sets == 127)
3759 return 0;
3761 regs->array[regno].set_in_loop = 0;
3763 while (count > 0)
3765 enum rtx_code code;
3766 rtx set;
3768 p = NEXT_INSN (p);
3769 code = GET_CODE (p);
3771 /* If library call, skip to end of it. */
3772 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3773 p = XEXP (temp, 0);
3775 this = 0;
3776 if (code == INSN
3777 && (set = single_set (p))
3778 && REG_P (SET_DEST (set))
3779 && REGNO (SET_DEST (set)) == regno)
3781 this = loop_invariant_p (loop, SET_SRC (set));
3782 if (this != 0)
3783 value |= this;
3784 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3786 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3787 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3788 notes are OK. */
3789 this = (CONSTANT_P (XEXP (temp, 0))
3790 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3791 && loop_invariant_p (loop, XEXP (temp, 0))));
3792 if (this != 0)
3793 value |= this;
3796 if (this != 0)
3797 count--;
3798 else if (code != NOTE)
3800 regs->array[regno].set_in_loop = old;
3801 return 0;
3805 regs->array[regno].set_in_loop = old;
3806 /* If loop_invariant_p ever returned 2, we return 2. */
3807 return 1 + (value & 2);
3810 /* Look at all uses (not sets) of registers in X. For each, if it is
3811 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3812 a different insn, set USAGE[REGNO] to const0_rtx. */
3814 static void
3815 find_single_use_in_loop (struct loop_regs *regs, rtx insn, rtx x)
3817 enum rtx_code code = GET_CODE (x);
3818 const char *fmt = GET_RTX_FORMAT (code);
3819 int i, j;
3821 if (code == REG)
3822 regs->array[REGNO (x)].single_usage
3823 = (regs->array[REGNO (x)].single_usage != 0
3824 && regs->array[REGNO (x)].single_usage != insn)
3825 ? const0_rtx : insn;
3827 else if (code == SET)
3829 /* Don't count SET_DEST if it is a REG; otherwise count things
3830 in SET_DEST because if a register is partially modified, it won't
3831 show up as a potential movable so we don't care how USAGE is set
3832 for it. */
3833 if (!REG_P (SET_DEST (x)))
3834 find_single_use_in_loop (regs, insn, SET_DEST (x));
3835 find_single_use_in_loop (regs, insn, SET_SRC (x));
3837 else
3838 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3840 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3841 find_single_use_in_loop (regs, insn, XEXP (x, i));
3842 else if (fmt[i] == 'E')
3843 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3844 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3848 /* Count and record any set in X which is contained in INSN. Update
3849 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3850 in X. */
3852 static void
3853 count_one_set (struct loop_regs *regs, rtx insn, rtx x, rtx *last_set)
3855 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
3856 /* Don't move a reg that has an explicit clobber.
3857 It's not worth the pain to try to do it correctly. */
3858 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3860 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3862 rtx dest = SET_DEST (x);
3863 while (GET_CODE (dest) == SUBREG
3864 || GET_CODE (dest) == ZERO_EXTRACT
3865 || GET_CODE (dest) == STRICT_LOW_PART)
3866 dest = XEXP (dest, 0);
3867 if (REG_P (dest))
3869 int i;
3870 int regno = REGNO (dest);
3871 for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3873 /* If this is the first setting of this reg
3874 in current basic block, and it was set before,
3875 it must be set in two basic blocks, so it cannot
3876 be moved out of the loop. */
3877 if (regs->array[regno].set_in_loop > 0
3878 && last_set[regno] == 0)
3879 regs->array[regno+i].may_not_optimize = 1;
3880 /* If this is not first setting in current basic block,
3881 see if reg was used in between previous one and this.
3882 If so, neither one can be moved. */
3883 if (last_set[regno] != 0
3884 && reg_used_between_p (dest, last_set[regno], insn))
3885 regs->array[regno+i].may_not_optimize = 1;
3886 if (regs->array[regno+i].set_in_loop < 127)
3887 ++regs->array[regno+i].set_in_loop;
3888 last_set[regno+i] = insn;
3894 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3895 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3896 contained in insn INSN is used by any insn that precedes INSN in
3897 cyclic order starting from the loop entry point.
3899 We don't want to use INSN_LUID here because if we restrict INSN to those
3900 that have a valid INSN_LUID, it means we cannot move an invariant out
3901 from an inner loop past two loops. */
3903 static int
3904 loop_reg_used_before_p (const struct loop *loop, rtx set, rtx insn)
3906 rtx reg = SET_DEST (set);
3907 rtx p;
3909 /* Scan forward checking for register usage. If we hit INSN, we
3910 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3911 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3913 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3914 return 1;
3916 if (p == loop->end)
3917 p = loop->start;
3920 return 0;
3924 /* Information we collect about arrays that we might want to prefetch. */
3925 struct prefetch_info
3927 struct iv_class *class; /* Class this prefetch is based on. */
3928 struct induction *giv; /* GIV this prefetch is based on. */
3929 rtx base_address; /* Start prefetching from this address plus
3930 index. */
3931 HOST_WIDE_INT index;
3932 HOST_WIDE_INT stride; /* Prefetch stride in bytes in each
3933 iteration. */
3934 unsigned int bytes_accessed; /* Sum of sizes of all accesses to this
3935 prefetch area in one iteration. */
3936 unsigned int total_bytes; /* Total bytes loop will access in this block.
3937 This is set only for loops with known
3938 iteration counts and is 0xffffffff
3939 otherwise. */
3940 int prefetch_in_loop; /* Number of prefetch insns in loop. */
3941 int prefetch_before_loop; /* Number of prefetch insns before loop. */
3942 unsigned int write : 1; /* 1 for read/write prefetches. */
3945 /* Data used by check_store function. */
3946 struct check_store_data
3948 rtx mem_address;
3949 int mem_write;
3952 static void check_store (rtx, rtx, void *);
3953 static void emit_prefetch_instructions (struct loop *);
3954 static int rtx_equal_for_prefetch_p (rtx, rtx);
3956 /* Set mem_write when mem_address is found. Used as callback to
3957 note_stores. */
3958 static void
3959 check_store (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3961 struct check_store_data *d = (struct check_store_data *) data;
3963 if ((MEM_P (x)) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3964 d->mem_write = 1;
3967 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3968 important to get some addresses combined. Later more sophisticated
3969 transformations can be added when necessary.
3971 ??? Same trick with swapping operand is done at several other places.
3972 It can be nice to develop some common way to handle this. */
3974 static int
3975 rtx_equal_for_prefetch_p (rtx x, rtx y)
3977 int i;
3978 int j;
3979 enum rtx_code code = GET_CODE (x);
3980 const char *fmt;
3982 if (x == y)
3983 return 1;
3984 if (code != GET_CODE (y))
3985 return 0;
3987 if (COMMUTATIVE_ARITH_P (x))
3989 return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3990 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3991 || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3992 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3995 /* Compare the elements. If any pair of corresponding elements fails to
3996 match, return 0 for the whole thing. */
3998 fmt = GET_RTX_FORMAT (code);
3999 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4001 switch (fmt[i])
4003 case 'w':
4004 if (XWINT (x, i) != XWINT (y, i))
4005 return 0;
4006 break;
4008 case 'i':
4009 if (XINT (x, i) != XINT (y, i))
4010 return 0;
4011 break;
4013 case 'E':
4014 /* Two vectors must have the same length. */
4015 if (XVECLEN (x, i) != XVECLEN (y, i))
4016 return 0;
4018 /* And the corresponding elements must match. */
4019 for (j = 0; j < XVECLEN (x, i); j++)
4020 if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
4021 XVECEXP (y, i, j)) == 0)
4022 return 0;
4023 break;
4025 case 'e':
4026 if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
4027 return 0;
4028 break;
4030 case 's':
4031 if (strcmp (XSTR (x, i), XSTR (y, i)))
4032 return 0;
4033 break;
4035 case 'u':
4036 /* These are just backpointers, so they don't matter. */
4037 break;
4039 case '0':
4040 break;
4042 /* It is believed that rtx's at this level will never
4043 contain anything but integers and other rtx's,
4044 except for within LABEL_REFs and SYMBOL_REFs. */
4045 default:
4046 gcc_unreachable ();
4049 return 1;
4052 /* Remove constant addition value from the expression X (when present)
4053 and return it. */
4055 static HOST_WIDE_INT
4056 remove_constant_addition (rtx *x)
4058 HOST_WIDE_INT addval = 0;
4059 rtx exp = *x;
4061 /* Avoid clobbering a shared CONST expression. */
4062 if (GET_CODE (exp) == CONST)
4064 if (GET_CODE (XEXP (exp, 0)) == PLUS
4065 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
4066 && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
4068 *x = XEXP (XEXP (exp, 0), 0);
4069 return INTVAL (XEXP (XEXP (exp, 0), 1));
4071 return 0;
4074 if (GET_CODE (exp) == CONST_INT)
4076 addval = INTVAL (exp);
4077 *x = const0_rtx;
4080 /* For plus expression recurse on ourself. */
4081 else if (GET_CODE (exp) == PLUS)
4083 addval += remove_constant_addition (&XEXP (exp, 0));
4084 addval += remove_constant_addition (&XEXP (exp, 1));
4086 /* In case our parameter was constant, remove extra zero from the
4087 expression. */
4088 if (XEXP (exp, 0) == const0_rtx)
4089 *x = XEXP (exp, 1);
4090 else if (XEXP (exp, 1) == const0_rtx)
4091 *x = XEXP (exp, 0);
4094 return addval;
4097 /* Attempt to identify accesses to arrays that are most likely to cause cache
4098 misses, and emit prefetch instructions a few prefetch blocks forward.
4100 To detect the arrays we use the GIV information that was collected by the
4101 strength reduction pass.
4103 The prefetch instructions are generated after the GIV information is done
4104 and before the strength reduction process. The new GIVs are injected into
4105 the strength reduction tables, so the prefetch addresses are optimized as
4106 well.
4108 GIVs are split into base address, stride, and constant addition values.
4109 GIVs with the same address, stride and close addition values are combined
4110 into a single prefetch. Also writes to GIVs are detected, so that prefetch
4111 for write instructions can be used for the block we write to, on machines
4112 that support write prefetches.
4114 Several heuristics are used to determine when to prefetch. They are
4115 controlled by defined symbols that can be overridden for each target. */
4117 static void
4118 emit_prefetch_instructions (struct loop *loop)
4120 int num_prefetches = 0;
4121 int num_real_prefetches = 0;
4122 int num_real_write_prefetches = 0;
4123 int num_prefetches_before = 0;
4124 int num_write_prefetches_before = 0;
4125 int ahead = 0;
4126 int i;
4127 struct iv_class *bl;
4128 struct induction *iv;
4129 struct prefetch_info info[MAX_PREFETCHES];
4130 struct loop_ivs *ivs = LOOP_IVS (loop);
4132 if (!HAVE_prefetch || PREFETCH_BLOCK == 0)
4133 return;
4135 /* Consider only loops w/o calls. When a call is done, the loop is probably
4136 slow enough to read the memory. */
4137 if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
4139 if (loop_dump_stream)
4140 fprintf (loop_dump_stream, "Prefetch: ignoring loop: has call.\n");
4142 return;
4145 /* Don't prefetch in loops known to have few iterations. */
4146 if (PREFETCH_NO_LOW_LOOPCNT
4147 && LOOP_INFO (loop)->n_iterations
4148 && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
4150 if (loop_dump_stream)
4151 fprintf (loop_dump_stream,
4152 "Prefetch: ignoring loop: not enough iterations.\n");
4153 return;
4156 /* Search all induction variables and pick those interesting for the prefetch
4157 machinery. */
4158 for (bl = ivs->list; bl; bl = bl->next)
4160 struct induction *biv = bl->biv, *biv1;
4161 int basestride = 0;
4163 biv1 = biv;
4165 /* Expect all BIVs to be executed in each iteration. This makes our
4166 analysis more conservative. */
4167 while (biv1)
4169 /* Discard non-constant additions that we can't handle well yet, and
4170 BIVs that are executed multiple times; such BIVs ought to be
4171 handled in the nested loop. We accept not_every_iteration BIVs,
4172 since these only result in larger strides and make our
4173 heuristics more conservative. */
4174 if (GET_CODE (biv->add_val) != CONST_INT)
4176 if (loop_dump_stream)
4178 fprintf (loop_dump_stream,
4179 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
4180 REGNO (biv->src_reg), INSN_UID (biv->insn));
4181 print_rtl (loop_dump_stream, biv->add_val);
4182 fprintf (loop_dump_stream, "\n");
4184 break;
4187 if (biv->maybe_multiple)
4189 if (loop_dump_stream)
4191 fprintf (loop_dump_stream,
4192 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
4193 REGNO (biv->src_reg), INSN_UID (biv->insn));
4194 print_rtl (loop_dump_stream, biv->add_val);
4195 fprintf (loop_dump_stream, "\n");
4197 break;
4200 basestride += INTVAL (biv1->add_val);
4201 biv1 = biv1->next_iv;
4204 if (biv1 || !basestride)
4205 continue;
4207 for (iv = bl->giv; iv; iv = iv->next_iv)
4209 rtx address;
4210 rtx temp;
4211 HOST_WIDE_INT index = 0;
4212 int add = 1;
4213 HOST_WIDE_INT stride = 0;
4214 int stride_sign = 1;
4215 struct check_store_data d;
4216 const char *ignore_reason = NULL;
4217 int size = GET_MODE_SIZE (GET_MODE (iv));
4219 /* See whether an induction variable is interesting to us and if
4220 not, report the reason. */
4221 if (iv->giv_type != DEST_ADDR)
4222 ignore_reason = "giv is not a destination address";
4224 /* We are interested only in constant stride memory references
4225 in order to be able to compute density easily. */
4226 else if (GET_CODE (iv->mult_val) != CONST_INT)
4227 ignore_reason = "stride is not constant";
4229 else
4231 stride = INTVAL (iv->mult_val) * basestride;
4232 if (stride < 0)
4234 stride = -stride;
4235 stride_sign = -1;
4238 /* On some targets, reversed order prefetches are not
4239 worthwhile. */
4240 if (PREFETCH_NO_REVERSE_ORDER && stride_sign < 0)
4241 ignore_reason = "reversed order stride";
4243 /* Prefetch of accesses with an extreme stride might not be
4244 worthwhile, either. */
4245 else if (PREFETCH_NO_EXTREME_STRIDE
4246 && stride > PREFETCH_EXTREME_STRIDE)
4247 ignore_reason = "extreme stride";
4249 /* Ignore GIVs with varying add values; we can't predict the
4250 value for the next iteration. */
4251 else if (!loop_invariant_p (loop, iv->add_val))
4252 ignore_reason = "giv has varying add value";
4254 /* Ignore GIVs in the nested loops; they ought to have been
4255 handled already. */
4256 else if (iv->maybe_multiple)
4257 ignore_reason = "giv is in nested loop";
4260 if (ignore_reason != NULL)
4262 if (loop_dump_stream)
4263 fprintf (loop_dump_stream,
4264 "Prefetch: ignoring giv at %d: %s.\n",
4265 INSN_UID (iv->insn), ignore_reason);
4266 continue;
4269 /* Determine the pointer to the basic array we are examining. It is
4270 the sum of the BIV's initial value and the GIV's add_val. */
4271 address = copy_rtx (iv->add_val);
4272 temp = copy_rtx (bl->initial_value);
4274 address = simplify_gen_binary (PLUS, Pmode, temp, address);
4275 index = remove_constant_addition (&address);
4277 d.mem_write = 0;
4278 d.mem_address = *iv->location;
4280 /* When the GIV is not always executed, we might be better off by
4281 not dirtying the cache pages. */
4282 if (PREFETCH_CONDITIONAL || iv->always_executed)
4283 note_stores (PATTERN (iv->insn), check_store, &d);
4284 else
4286 if (loop_dump_stream)
4287 fprintf (loop_dump_stream, "Prefetch: Ignoring giv at %d: %s\n",
4288 INSN_UID (iv->insn), "in conditional code.");
4289 continue;
4292 /* Attempt to find another prefetch to the same array and see if we
4293 can merge this one. */
4294 for (i = 0; i < num_prefetches; i++)
4295 if (rtx_equal_for_prefetch_p (address, info[i].base_address)
4296 && stride == info[i].stride)
4298 /* In case both access same array (same location
4299 just with small difference in constant indexes), merge
4300 the prefetches. Just do the later and the earlier will
4301 get prefetched from previous iteration.
4302 The artificial threshold should not be too small,
4303 but also not bigger than small portion of memory usually
4304 traversed by single loop. */
4305 if (index >= info[i].index
4306 && index - info[i].index < PREFETCH_EXTREME_DIFFERENCE)
4308 info[i].write |= d.mem_write;
4309 info[i].bytes_accessed += size;
4310 info[i].index = index;
4311 info[i].giv = iv;
4312 info[i].class = bl;
4313 info[num_prefetches].base_address = address;
4314 add = 0;
4315 break;
4318 if (index < info[i].index
4319 && info[i].index - index < PREFETCH_EXTREME_DIFFERENCE)
4321 info[i].write |= d.mem_write;
4322 info[i].bytes_accessed += size;
4323 add = 0;
4324 break;
4328 /* Merging failed. */
4329 if (add)
4331 info[num_prefetches].giv = iv;
4332 info[num_prefetches].class = bl;
4333 info[num_prefetches].index = index;
4334 info[num_prefetches].stride = stride;
4335 info[num_prefetches].base_address = address;
4336 info[num_prefetches].write = d.mem_write;
4337 info[num_prefetches].bytes_accessed = size;
4338 num_prefetches++;
4339 if (num_prefetches >= MAX_PREFETCHES)
4341 if (loop_dump_stream)
4342 fprintf (loop_dump_stream,
4343 "Maximal number of prefetches exceeded.\n");
4344 return;
4350 for (i = 0; i < num_prefetches; i++)
4352 int density;
4354 /* Attempt to calculate the total number of bytes fetched by all
4355 iterations of the loop. Avoid overflow. */
4356 if (LOOP_INFO (loop)->n_iterations
4357 && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
4358 >= LOOP_INFO (loop)->n_iterations))
4359 info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
4360 else
4361 info[i].total_bytes = 0xffffffff;
4363 density = info[i].bytes_accessed * 100 / info[i].stride;
4365 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4366 if (PREFETCH_ONLY_DENSE_MEM)
4367 if (density * 256 > PREFETCH_DENSE_MEM * 100
4368 && (info[i].total_bytes / PREFETCH_BLOCK
4369 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
4371 info[i].prefetch_before_loop = 1;
4372 info[i].prefetch_in_loop
4373 = (info[i].total_bytes / PREFETCH_BLOCK
4374 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
4376 else
4378 info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
4379 if (loop_dump_stream)
4380 fprintf (loop_dump_stream,
4381 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4382 INSN_UID (info[i].giv->insn), density);
4384 else
4385 info[i].prefetch_in_loop = 1, info[i].prefetch_before_loop = 1;
4387 /* Find how many prefetch instructions we'll use within the loop. */
4388 if (info[i].prefetch_in_loop != 0)
4390 info[i].prefetch_in_loop = ((info[i].stride + PREFETCH_BLOCK - 1)
4391 / PREFETCH_BLOCK);
4392 num_real_prefetches += info[i].prefetch_in_loop;
4393 if (info[i].write)
4394 num_real_write_prefetches += info[i].prefetch_in_loop;
4398 /* Determine how many iterations ahead to prefetch within the loop, based
4399 on how many prefetches we currently expect to do within the loop. */
4400 if (num_real_prefetches != 0)
4402 if ((ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches) == 0)
4404 if (loop_dump_stream)
4405 fprintf (loop_dump_stream,
4406 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4407 SIMULTANEOUS_PREFETCHES, num_real_prefetches);
4408 num_real_prefetches = 0, num_real_write_prefetches = 0;
4411 /* We'll also use AHEAD to determine how many prefetch instructions to
4412 emit before a loop, so don't leave it zero. */
4413 if (ahead == 0)
4414 ahead = PREFETCH_BLOCKS_BEFORE_LOOP_MAX;
4416 for (i = 0; i < num_prefetches; i++)
4418 /* Update if we've decided not to prefetch anything within the loop. */
4419 if (num_real_prefetches == 0)
4420 info[i].prefetch_in_loop = 0;
4422 /* Find how many prefetch instructions we'll use before the loop. */
4423 if (info[i].prefetch_before_loop != 0)
4425 int n = info[i].total_bytes / PREFETCH_BLOCK;
4426 if (n > ahead)
4427 n = ahead;
4428 info[i].prefetch_before_loop = n;
4429 num_prefetches_before += n;
4430 if (info[i].write)
4431 num_write_prefetches_before += n;
4434 if (loop_dump_stream)
4436 if (info[i].prefetch_in_loop == 0
4437 && info[i].prefetch_before_loop == 0)
4438 continue;
4439 fprintf (loop_dump_stream, "Prefetch insn: %d",
4440 INSN_UID (info[i].giv->insn));
4441 fprintf (loop_dump_stream,
4442 "; in loop: %d; before: %d; %s\n",
4443 info[i].prefetch_in_loop,
4444 info[i].prefetch_before_loop,
4445 info[i].write ? "read/write" : "read only");
4446 fprintf (loop_dump_stream,
4447 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4448 (int) (info[i].bytes_accessed * 100 / info[i].stride),
4449 info[i].bytes_accessed, info[i].total_bytes);
4450 fprintf (loop_dump_stream, " index: " HOST_WIDE_INT_PRINT_DEC
4451 "; stride: " HOST_WIDE_INT_PRINT_DEC "; address: ",
4452 info[i].index, info[i].stride);
4453 print_rtl (loop_dump_stream, info[i].base_address);
4454 fprintf (loop_dump_stream, "\n");
4458 if (num_real_prefetches + num_prefetches_before > 0)
4460 /* Record that this loop uses prefetch instructions. */
4461 LOOP_INFO (loop)->has_prefetch = 1;
4463 if (loop_dump_stream)
4465 fprintf (loop_dump_stream, "Real prefetches needed within loop: %d (write: %d)\n",
4466 num_real_prefetches, num_real_write_prefetches);
4467 fprintf (loop_dump_stream, "Real prefetches needed before loop: %d (write: %d)\n",
4468 num_prefetches_before, num_write_prefetches_before);
4472 for (i = 0; i < num_prefetches; i++)
4474 int y;
4476 for (y = 0; y < info[i].prefetch_in_loop; y++)
4478 rtx loc = copy_rtx (*info[i].giv->location);
4479 rtx insn;
4480 int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4481 rtx before_insn = info[i].giv->insn;
4482 rtx prev_insn = PREV_INSN (info[i].giv->insn);
4483 rtx seq;
4485 /* We can save some effort by offsetting the address on
4486 architectures with offsettable memory references. */
4487 if (offsettable_address_p (0, VOIDmode, loc))
4488 loc = plus_constant (loc, bytes_ahead);
4489 else
4491 rtx reg = gen_reg_rtx (Pmode);
4492 loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4493 GEN_INT (bytes_ahead), reg,
4494 0, before_insn);
4495 loc = reg;
4498 start_sequence ();
4499 /* Make sure the address operand is valid for prefetch. */
4500 if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4501 (loc, insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4502 loc = force_reg (Pmode, loc);
4503 emit_insn (gen_prefetch (loc, GEN_INT (info[i].write),
4504 GEN_INT (3)));
4505 seq = get_insns ();
4506 end_sequence ();
4507 emit_insn_before (seq, before_insn);
4509 /* Check all insns emitted and record the new GIV
4510 information. */
4511 insn = NEXT_INSN (prev_insn);
4512 while (insn != before_insn)
4514 insn = check_insn_for_givs (loop, insn,
4515 info[i].giv->always_executed,
4516 info[i].giv->maybe_multiple);
4517 insn = NEXT_INSN (insn);
4521 if (PREFETCH_BEFORE_LOOP)
4523 /* Emit insns before the loop to fetch the first cache lines or,
4524 if we're not prefetching within the loop, everything we expect
4525 to need. */
4526 for (y = 0; y < info[i].prefetch_before_loop; y++)
4528 rtx reg = gen_reg_rtx (Pmode);
4529 rtx loop_start = loop->start;
4530 rtx init_val = info[i].class->initial_value;
4531 rtx add_val = simplify_gen_binary (PLUS, Pmode,
4532 info[i].giv->add_val,
4533 GEN_INT (y * PREFETCH_BLOCK));
4535 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4536 non-constant INIT_VAL to have the same mode as REG, which
4537 in this case we know to be Pmode. */
4538 if (GET_MODE (init_val) != Pmode && !CONSTANT_P (init_val))
4540 rtx seq;
4542 start_sequence ();
4543 init_val = convert_to_mode (Pmode, init_val, 0);
4544 seq = get_insns ();
4545 end_sequence ();
4546 loop_insn_emit_before (loop, 0, loop_start, seq);
4548 loop_iv_add_mult_emit_before (loop, init_val,
4549 info[i].giv->mult_val,
4550 add_val, reg, 0, loop_start);
4551 emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4552 GEN_INT (3)),
4553 loop_start);
4558 return;
4561 /* Communication with routines called via `note_stores'. */
4563 static rtx note_insn;
4565 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4567 static rtx addr_placeholder;
4569 /* ??? Unfinished optimizations, and possible future optimizations,
4570 for the strength reduction code. */
4572 /* ??? The interaction of biv elimination, and recognition of 'constant'
4573 bivs, may cause problems. */
4575 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4576 performance problems.
4578 Perhaps don't eliminate things that can be combined with an addressing
4579 mode. Find all givs that have the same biv, mult_val, and add_val;
4580 then for each giv, check to see if its only use dies in a following
4581 memory address. If so, generate a new memory address and check to see
4582 if it is valid. If it is valid, then store the modified memory address,
4583 otherwise, mark the giv as not done so that it will get its own iv. */
4585 /* ??? Could try to optimize branches when it is known that a biv is always
4586 positive. */
4588 /* ??? When replace a biv in a compare insn, we should replace with closest
4589 giv so that an optimized branch can still be recognized by the combiner,
4590 e.g. the VAX acb insn. */
4592 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4593 was rerun in loop_optimize whenever a register was added or moved.
4594 Also, some of the optimizations could be a little less conservative. */
4596 /* Searches the insns between INSN and LOOP->END. Returns 1 if there
4597 is a backward branch in that range that branches to somewhere between
4598 LOOP->START and INSN. Returns 0 otherwise. */
4600 /* ??? This is quadratic algorithm. Could be rewritten to be linear.
4601 In practice, this is not a problem, because this function is seldom called,
4602 and uses a negligible amount of CPU time on average. */
4604 static int
4605 back_branch_in_range_p (const struct loop *loop, rtx insn)
4607 rtx p, q, target_insn;
4608 rtx loop_start = loop->start;
4609 rtx loop_end = loop->end;
4610 rtx orig_loop_end = loop->end;
4612 /* Stop before we get to the backward branch at the end of the loop. */
4613 loop_end = prev_nonnote_insn (loop_end);
4614 if (BARRIER_P (loop_end))
4615 loop_end = PREV_INSN (loop_end);
4617 /* Check in case insn has been deleted, search forward for first non
4618 deleted insn following it. */
4619 while (INSN_DELETED_P (insn))
4620 insn = NEXT_INSN (insn);
4622 /* Check for the case where insn is the last insn in the loop. Deal
4623 with the case where INSN was a deleted loop test insn, in which case
4624 it will now be the NOTE_LOOP_END. */
4625 if (insn == loop_end || insn == orig_loop_end)
4626 return 0;
4628 for (p = NEXT_INSN (insn); p != loop_end; p = NEXT_INSN (p))
4630 if (JUMP_P (p))
4632 target_insn = JUMP_LABEL (p);
4634 /* Search from loop_start to insn, to see if one of them is
4635 the target_insn. We can't use INSN_LUID comparisons here,
4636 since insn may not have an LUID entry. */
4637 for (q = loop_start; q != insn; q = NEXT_INSN (q))
4638 if (q == target_insn)
4639 return 1;
4643 return 0;
4646 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4647 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4648 callback.
4650 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4651 least once for every loop iteration except for the last one.
4653 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4654 loop iteration.
4656 typedef rtx (*loop_insn_callback) (struct loop *, rtx, int, int);
4657 static void
4658 for_each_insn_in_loop (struct loop *loop, loop_insn_callback fncall)
4660 int not_every_iteration = 0;
4661 int maybe_multiple = 0;
4662 int past_loop_latch = 0;
4663 bool exit_test_is_entry = false;
4664 rtx p;
4666 /* If loop_scan_start points to the loop exit test, the loop body
4667 cannot be counted on running on every iteration, and we have to
4668 be wary of subversive use of gotos inside expression
4669 statements. */
4670 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4672 exit_test_is_entry = true;
4673 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4676 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4677 for (p = next_insn_in_loop (loop, loop->scan_start);
4678 p != NULL_RTX;
4679 p = next_insn_in_loop (loop, p))
4681 p = fncall (loop, p, not_every_iteration, maybe_multiple);
4683 /* Past CODE_LABEL, we get to insns that may be executed multiple
4684 times. The only way we can be sure that they can't is if every
4685 jump insn between here and the end of the loop either
4686 returns, exits the loop, is a jump to a location that is still
4687 behind the label, or is a jump to the loop start. */
4689 if (LABEL_P (p))
4691 rtx insn = p;
4693 maybe_multiple = 0;
4695 while (1)
4697 insn = NEXT_INSN (insn);
4698 if (insn == loop->scan_start)
4699 break;
4700 if (insn == loop->end)
4702 if (loop->top != 0)
4703 insn = loop->top;
4704 else
4705 break;
4706 if (insn == loop->scan_start)
4707 break;
4710 if (JUMP_P (insn)
4711 && GET_CODE (PATTERN (insn)) != RETURN
4712 && (!any_condjump_p (insn)
4713 || (JUMP_LABEL (insn) != 0
4714 && JUMP_LABEL (insn) != loop->scan_start
4715 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4717 maybe_multiple = 1;
4718 break;
4723 /* Past a jump, we get to insns for which we can't count
4724 on whether they will be executed during each iteration. */
4725 /* This code appears twice in strength_reduce. There is also similar
4726 code in scan_loop. */
4727 if (JUMP_P (p)
4728 /* If we enter the loop in the middle, and scan around to the
4729 beginning, don't set not_every_iteration for that.
4730 This can be any kind of jump, since we want to know if insns
4731 will be executed if the loop is executed. */
4732 && (exit_test_is_entry
4733 || !(JUMP_LABEL (p) == loop->top
4734 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4735 && any_uncondjump_p (p))
4736 || (NEXT_INSN (p) == loop->end
4737 && any_condjump_p (p))))))
4739 rtx label = 0;
4741 /* If this is a jump outside the loop, then it also doesn't
4742 matter. Check to see if the target of this branch is on the
4743 loop->exits_labels list. */
4745 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4746 if (XEXP (label, 0) == JUMP_LABEL (p))
4747 break;
4749 if (!label)
4750 not_every_iteration = 1;
4753 /* Note if we pass a loop latch. If we do, then we can not clear
4754 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4755 a loop since a jump before the last CODE_LABEL may have started
4756 a new loop iteration.
4758 Note that LOOP_TOP is only set for rotated loops and we need
4759 this check for all loops, so compare against the CODE_LABEL
4760 which immediately follows LOOP_START. */
4761 if (JUMP_P (p)
4762 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4763 past_loop_latch = 1;
4765 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4766 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4767 or not an insn is known to be executed each iteration of the
4768 loop, whether or not any iterations are known to occur.
4770 Therefore, if we have just passed a label and have no more labels
4771 between here and the test insn of the loop, and we have not passed
4772 a jump to the top of the loop, then we know these insns will be
4773 executed each iteration. */
4775 if (not_every_iteration
4776 && !past_loop_latch
4777 && LABEL_P (p)
4778 && no_labels_between_p (p, loop->end))
4779 not_every_iteration = 0;
4783 static void
4784 loop_bivs_find (struct loop *loop)
4786 struct loop_regs *regs = LOOP_REGS (loop);
4787 struct loop_ivs *ivs = LOOP_IVS (loop);
4788 /* Temporary list pointers for traversing ivs->list. */
4789 struct iv_class *bl, **backbl;
4791 ivs->list = 0;
4793 for_each_insn_in_loop (loop, check_insn_for_bivs);
4795 /* Scan ivs->list to remove all regs that proved not to be bivs.
4796 Make a sanity check against regs->n_times_set. */
4797 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4799 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4800 /* Above happens if register modified by subreg, etc. */
4801 /* Make sure it is not recognized as a basic induction var: */
4802 || regs->array[bl->regno].n_times_set != bl->biv_count
4803 /* If never incremented, it is invariant that we decided not to
4804 move. So leave it alone. */
4805 || ! bl->incremented)
4807 if (loop_dump_stream)
4808 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
4809 bl->regno,
4810 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4811 ? "not induction variable"
4812 : (! bl->incremented ? "never incremented"
4813 : "count error")));
4815 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4816 *backbl = bl->next;
4818 else
4820 backbl = &bl->next;
4822 if (loop_dump_stream)
4823 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
4829 /* Determine how BIVS are initialized by looking through pre-header
4830 extended basic block. */
4831 static void
4832 loop_bivs_init_find (struct loop *loop)
4834 struct loop_ivs *ivs = LOOP_IVS (loop);
4835 /* Temporary list pointers for traversing ivs->list. */
4836 struct iv_class *bl;
4837 int call_seen;
4838 rtx p;
4840 /* Find initial value for each biv by searching backwards from loop_start,
4841 halting at first label. Also record any test condition. */
4843 call_seen = 0;
4844 for (p = loop->start; p && !LABEL_P (p); p = PREV_INSN (p))
4846 rtx test;
4848 note_insn = p;
4850 if (CALL_P (p))
4851 call_seen = 1;
4853 if (INSN_P (p))
4854 note_stores (PATTERN (p), record_initial, ivs);
4856 /* Record any test of a biv that branches around the loop if no store
4857 between it and the start of loop. We only care about tests with
4858 constants and registers and only certain of those. */
4859 if (JUMP_P (p)
4860 && JUMP_LABEL (p) != 0
4861 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4862 && (test = get_condition_for_loop (loop, p)) != 0
4863 && REG_P (XEXP (test, 0))
4864 && REGNO (XEXP (test, 0)) < max_reg_before_loop
4865 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4866 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4867 && bl->init_insn == 0)
4869 /* If an NE test, we have an initial value! */
4870 if (GET_CODE (test) == NE)
4872 bl->init_insn = p;
4873 bl->init_set = gen_rtx_SET (VOIDmode,
4874 XEXP (test, 0), XEXP (test, 1));
4876 else
4877 bl->initial_test = test;
4883 /* Look at the each biv and see if we can say anything better about its
4884 initial value from any initializing insns set up above. (This is done
4885 in two passes to avoid missing SETs in a PARALLEL.) */
4886 static void
4887 loop_bivs_check (struct loop *loop)
4889 struct loop_ivs *ivs = LOOP_IVS (loop);
4890 /* Temporary list pointers for traversing ivs->list. */
4891 struct iv_class *bl;
4892 struct iv_class **backbl;
4894 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4896 rtx src;
4897 rtx note;
4899 if (! bl->init_insn)
4900 continue;
4902 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4903 is a constant, use the value of that. */
4904 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4905 && CONSTANT_P (XEXP (note, 0)))
4906 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4907 && CONSTANT_P (XEXP (note, 0))))
4908 src = XEXP (note, 0);
4909 else
4910 src = SET_SRC (bl->init_set);
4912 if (loop_dump_stream)
4913 fprintf (loop_dump_stream,
4914 "Biv %d: initialized at insn %d: initial value ",
4915 bl->regno, INSN_UID (bl->init_insn));
4917 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4918 || GET_MODE (src) == VOIDmode)
4919 && valid_initial_value_p (src, bl->init_insn,
4920 LOOP_INFO (loop)->pre_header_has_call,
4921 loop->start))
4923 bl->initial_value = src;
4925 if (loop_dump_stream)
4927 print_simple_rtl (loop_dump_stream, src);
4928 fputc ('\n', loop_dump_stream);
4931 /* If we can't make it a giv,
4932 let biv keep initial value of "itself". */
4933 else if (loop_dump_stream)
4934 fprintf (loop_dump_stream, "is complex\n");
4939 /* Search the loop for general induction variables. */
4941 static void
4942 loop_givs_find (struct loop* loop)
4944 for_each_insn_in_loop (loop, check_insn_for_givs);
4948 /* For each giv for which we still don't know whether or not it is
4949 replaceable, check to see if it is replaceable because its final value
4950 can be calculated. */
4952 static void
4953 loop_givs_check (struct loop *loop)
4955 struct loop_ivs *ivs = LOOP_IVS (loop);
4956 struct iv_class *bl;
4958 for (bl = ivs->list; bl; bl = bl->next)
4960 struct induction *v;
4962 for (v = bl->giv; v; v = v->next_iv)
4963 if (! v->replaceable && ! v->not_replaceable)
4964 check_final_value (loop, v);
4968 /* Try to generate the simplest rtx for the expression
4969 (PLUS (MULT mult1 mult2) add1). This is used to calculate the initial
4970 value of giv's. */
4972 static rtx
4973 fold_rtx_mult_add (rtx mult1, rtx mult2, rtx add1, enum machine_mode mode)
4975 rtx temp, mult_res;
4976 rtx result;
4978 /* The modes must all be the same. This should always be true. For now,
4979 check to make sure. */
4980 gcc_assert (GET_MODE (mult1) == mode || GET_MODE (mult1) == VOIDmode);
4981 gcc_assert (GET_MODE (mult2) == mode || GET_MODE (mult2) == VOIDmode);
4982 gcc_assert (GET_MODE (add1) == mode || GET_MODE (add1) == VOIDmode);
4984 /* Ensure that if at least one of mult1/mult2 are constant, then mult2
4985 will be a constant. */
4986 if (GET_CODE (mult1) == CONST_INT)
4988 temp = mult2;
4989 mult2 = mult1;
4990 mult1 = temp;
4993 mult_res = simplify_binary_operation (MULT, mode, mult1, mult2);
4994 if (! mult_res)
4995 mult_res = gen_rtx_MULT (mode, mult1, mult2);
4997 /* Again, put the constant second. */
4998 if (GET_CODE (add1) == CONST_INT)
5000 temp = add1;
5001 add1 = mult_res;
5002 mult_res = temp;
5005 result = simplify_binary_operation (PLUS, mode, add1, mult_res);
5006 if (! result)
5007 result = gen_rtx_PLUS (mode, add1, mult_res);
5009 return result;
5012 /* Searches the list of induction struct's for the biv BL, to try to calculate
5013 the total increment value for one iteration of the loop as a constant.
5015 Returns the increment value as an rtx, simplified as much as possible,
5016 if it can be calculated. Otherwise, returns 0. */
5018 static rtx
5019 biv_total_increment (const struct iv_class *bl)
5021 struct induction *v;
5022 rtx result;
5024 /* For increment, must check every instruction that sets it. Each
5025 instruction must be executed only once each time through the loop.
5026 To verify this, we check that the insn is always executed, and that
5027 there are no backward branches after the insn that branch to before it.
5028 Also, the insn must have a mult_val of one (to make sure it really is
5029 an increment). */
5031 result = const0_rtx;
5032 for (v = bl->biv; v; v = v->next_iv)
5034 if (v->always_computable && v->mult_val == const1_rtx
5035 && ! v->maybe_multiple
5036 && SCALAR_INT_MODE_P (v->mode))
5038 /* If we have already counted it, skip it. */
5039 if (v->same)
5040 continue;
5042 result = fold_rtx_mult_add (result, const1_rtx, v->add_val, v->mode);
5044 else
5045 return 0;
5048 return result;
5051 /* Try to prove that the register is dead after the loop exits. Trace every
5052 loop exit looking for an insn that will always be executed, which sets
5053 the register to some value, and appears before the first use of the register
5054 is found. If successful, then return 1, otherwise return 0. */
5056 /* ?? Could be made more intelligent in the handling of jumps, so that
5057 it can search past if statements and other similar structures. */
5059 static int
5060 reg_dead_after_loop (const struct loop *loop, rtx reg)
5062 rtx insn, label;
5063 int jump_count = 0;
5064 int label_count = 0;
5066 /* In addition to checking all exits of this loop, we must also check
5067 all exits of inner nested loops that would exit this loop. We don't
5068 have any way to identify those, so we just give up if there are any
5069 such inner loop exits. */
5071 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
5072 label_count++;
5074 if (label_count != loop->exit_count)
5075 return 0;
5077 /* HACK: Must also search the loop fall through exit, create a label_ref
5078 here which points to the loop->end, and append the loop_number_exit_labels
5079 list to it. */
5080 label = gen_rtx_LABEL_REF (Pmode, loop->end);
5081 LABEL_NEXTREF (label) = loop->exit_labels;
5083 for (; label; label = LABEL_NEXTREF (label))
5085 /* Succeed if find an insn which sets the biv or if reach end of
5086 function. Fail if find an insn that uses the biv, or if come to
5087 a conditional jump. */
5089 insn = NEXT_INSN (XEXP (label, 0));
5090 while (insn)
5092 if (INSN_P (insn))
5094 rtx set, note;
5096 if (reg_referenced_p (reg, PATTERN (insn)))
5097 return 0;
5099 note = find_reg_equal_equiv_note (insn);
5100 if (note && reg_overlap_mentioned_p (reg, XEXP (note, 0)))
5101 return 0;
5103 set = single_set (insn);
5104 if (set && rtx_equal_p (SET_DEST (set), reg))
5105 break;
5107 if (JUMP_P (insn))
5109 if (GET_CODE (PATTERN (insn)) == RETURN)
5110 break;
5111 else if (!any_uncondjump_p (insn)
5112 /* Prevent infinite loop following infinite loops. */
5113 || jump_count++ > 20)
5114 return 0;
5115 else
5116 insn = JUMP_LABEL (insn);
5120 insn = NEXT_INSN (insn);
5124 /* Success, the register is dead on all loop exits. */
5125 return 1;
5128 /* Try to calculate the final value of the biv, the value it will have at
5129 the end of the loop. If we can do it, return that value. */
5131 static rtx
5132 final_biv_value (const struct loop *loop, struct iv_class *bl)
5134 unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
5135 rtx increment, tem;
5137 /* ??? This only works for MODE_INT biv's. Reject all others for now. */
5139 if (GET_MODE_CLASS (bl->biv->mode) != MODE_INT)
5140 return 0;
5142 /* The final value for reversed bivs must be calculated differently than
5143 for ordinary bivs. In this case, there is already an insn after the
5144 loop which sets this biv's final value (if necessary), and there are
5145 no other loop exits, so we can return any value. */
5146 if (bl->reversed)
5148 if (loop_dump_stream)
5149 fprintf (loop_dump_stream,
5150 "Final biv value for %d, reversed biv.\n", bl->regno);
5152 return const0_rtx;
5155 /* Try to calculate the final value as initial value + (number of iterations
5156 * increment). For this to work, increment must be invariant, the only
5157 exit from the loop must be the fall through at the bottom (otherwise
5158 it may not have its final value when the loop exits), and the initial
5159 value of the biv must be invariant. */
5161 if (n_iterations != 0
5162 && ! loop->exit_count
5163 && loop_invariant_p (loop, bl->initial_value))
5165 increment = biv_total_increment (bl);
5167 if (increment && loop_invariant_p (loop, increment))
5169 /* Can calculate the loop exit value, emit insns after loop
5170 end to calculate this value into a temporary register in
5171 case it is needed later. */
5173 tem = gen_reg_rtx (bl->biv->mode);
5174 record_base_value (REGNO (tem), bl->biv->add_val, 0);
5175 loop_iv_add_mult_sink (loop, increment, GEN_INT (n_iterations),
5176 bl->initial_value, tem);
5178 if (loop_dump_stream)
5179 fprintf (loop_dump_stream,
5180 "Final biv value for %d, calculated.\n", bl->regno);
5182 return tem;
5186 /* Check to see if the biv is dead at all loop exits. */
5187 if (reg_dead_after_loop (loop, bl->biv->src_reg))
5189 if (loop_dump_stream)
5190 fprintf (loop_dump_stream,
5191 "Final biv value for %d, biv dead after loop exit.\n",
5192 bl->regno);
5194 return const0_rtx;
5197 return 0;
5200 /* Return nonzero if it is possible to eliminate the biv BL provided
5201 all givs are reduced. This is possible if either the reg is not
5202 used outside the loop, or we can compute what its final value will
5203 be. */
5205 static int
5206 loop_biv_eliminable_p (struct loop *loop, struct iv_class *bl,
5207 int threshold, int insn_count)
5209 /* For architectures with a decrement_and_branch_until_zero insn,
5210 don't do this if we put a REG_NONNEG note on the endtest for this
5211 biv. */
5213 #ifdef HAVE_decrement_and_branch_until_zero
5214 if (bl->nonneg)
5216 if (loop_dump_stream)
5217 fprintf (loop_dump_stream,
5218 "Cannot eliminate nonneg biv %d.\n", bl->regno);
5219 return 0;
5221 #endif
5223 /* Check that biv is used outside loop or if it has a final value.
5224 Compare against bl->init_insn rather than loop->start. We aren't
5225 concerned with any uses of the biv between init_insn and
5226 loop->start since these won't be affected by the value of the biv
5227 elsewhere in the function, so long as init_insn doesn't use the
5228 biv itself. */
5230 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
5231 && bl->init_insn
5232 && INSN_UID (bl->init_insn) < max_uid_for_loop
5233 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
5234 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
5235 || (bl->final_value = final_biv_value (loop, bl)))
5236 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
5238 if (loop_dump_stream)
5240 fprintf (loop_dump_stream,
5241 "Cannot eliminate biv %d.\n",
5242 bl->regno);
5243 fprintf (loop_dump_stream,
5244 "First use: insn %d, last use: insn %d.\n",
5245 REGNO_FIRST_UID (bl->regno),
5246 REGNO_LAST_UID (bl->regno));
5248 return 0;
5252 /* Reduce each giv of BL that we have decided to reduce. */
5254 static void
5255 loop_givs_reduce (struct loop *loop, struct iv_class *bl)
5257 struct induction *v;
5259 for (v = bl->giv; v; v = v->next_iv)
5261 struct induction *tv;
5262 if (! v->ignore && v->same == 0)
5264 int auto_inc_opt = 0;
5266 /* If the code for derived givs immediately below has already
5267 allocated a new_reg, we must keep it. */
5268 if (! v->new_reg)
5269 v->new_reg = gen_reg_rtx (v->mode);
5271 #ifdef AUTO_INC_DEC
5272 /* If the target has auto-increment addressing modes, and
5273 this is an address giv, then try to put the increment
5274 immediately after its use, so that flow can create an
5275 auto-increment addressing mode. */
5276 /* Don't do this for loops entered at the bottom, to avoid
5277 this invalid transformation:
5278 jmp L; -> jmp L;
5279 TOP: TOP:
5280 use giv use giv
5281 L: inc giv
5282 inc biv L:
5283 test biv test giv
5284 cbr TOP cbr TOP
5286 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
5287 && bl->biv->always_executed && ! bl->biv->maybe_multiple
5288 /* We don't handle reversed biv's because bl->biv->insn
5289 does not have a valid INSN_LUID. */
5290 && ! bl->reversed
5291 && v->always_executed && ! v->maybe_multiple
5292 && INSN_UID (v->insn) < max_uid_for_loop
5293 && !loop->top)
5295 /* If other giv's have been combined with this one, then
5296 this will work only if all uses of the other giv's occur
5297 before this giv's insn. This is difficult to check.
5299 We simplify this by looking for the common case where
5300 there is one DEST_REG giv, and this giv's insn is the
5301 last use of the dest_reg of that DEST_REG giv. If the
5302 increment occurs after the address giv, then we can
5303 perform the optimization. (Otherwise, the increment
5304 would have to go before other_giv, and we would not be
5305 able to combine it with the address giv to get an
5306 auto-inc address.) */
5307 if (v->combined_with)
5309 struct induction *other_giv = 0;
5311 for (tv = bl->giv; tv; tv = tv->next_iv)
5312 if (tv->same == v)
5314 if (other_giv)
5315 break;
5316 else
5317 other_giv = tv;
5319 if (! tv && other_giv
5320 && REGNO (other_giv->dest_reg) < max_reg_before_loop
5321 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
5322 == INSN_UID (v->insn))
5323 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
5324 auto_inc_opt = 1;
5326 /* Check for case where increment is before the address
5327 giv. Do this test in "loop order". */
5328 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
5329 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
5330 || (INSN_LUID (bl->biv->insn)
5331 > INSN_LUID (loop->scan_start))))
5332 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
5333 && (INSN_LUID (loop->scan_start)
5334 < INSN_LUID (bl->biv->insn))))
5335 auto_inc_opt = -1;
5336 else
5337 auto_inc_opt = 1;
5339 #ifdef HAVE_cc0
5341 rtx prev;
5343 /* We can't put an insn immediately after one setting
5344 cc0, or immediately before one using cc0. */
5345 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
5346 || (auto_inc_opt == -1
5347 && (prev = prev_nonnote_insn (v->insn)) != 0
5348 && INSN_P (prev)
5349 && sets_cc0_p (PATTERN (prev))))
5350 auto_inc_opt = 0;
5352 #endif
5354 if (auto_inc_opt)
5355 v->auto_inc_opt = 1;
5357 #endif
5359 /* For each place where the biv is incremented, add an insn
5360 to increment the new, reduced reg for the giv. */
5361 for (tv = bl->biv; tv; tv = tv->next_iv)
5363 rtx insert_before;
5365 /* Skip if location is the same as a previous one. */
5366 if (tv->same)
5367 continue;
5368 if (! auto_inc_opt)
5369 insert_before = NEXT_INSN (tv->insn);
5370 else if (auto_inc_opt == 1)
5371 insert_before = NEXT_INSN (v->insn);
5372 else
5373 insert_before = v->insn;
5375 if (tv->mult_val == const1_rtx)
5376 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
5377 v->new_reg, v->new_reg,
5378 0, insert_before);
5379 else /* tv->mult_val == const0_rtx */
5380 /* A multiply is acceptable here
5381 since this is presumed to be seldom executed. */
5382 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
5383 v->add_val, v->new_reg,
5384 0, insert_before);
5387 /* Add code at loop start to initialize giv's reduced reg. */
5389 loop_iv_add_mult_hoist (loop,
5390 extend_value_for_giv (v, bl->initial_value),
5391 v->mult_val, v->add_val, v->new_reg);
5397 /* Check for givs whose first use is their definition and whose
5398 last use is the definition of another giv. If so, it is likely
5399 dead and should not be used to derive another giv nor to
5400 eliminate a biv. */
5402 static void
5403 loop_givs_dead_check (struct loop *loop ATTRIBUTE_UNUSED, struct iv_class *bl)
5405 struct induction *v;
5407 for (v = bl->giv; v; v = v->next_iv)
5409 if (v->ignore
5410 || (v->same && v->same->ignore))
5411 continue;
5413 if (v->giv_type == DEST_REG
5414 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
5416 struct induction *v1;
5418 for (v1 = bl->giv; v1; v1 = v1->next_iv)
5419 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
5420 v->maybe_dead = 1;
5426 static void
5427 loop_givs_rescan (struct loop *loop, struct iv_class *bl, rtx *reg_map)
5429 struct induction *v;
5431 for (v = bl->giv; v; v = v->next_iv)
5433 if (v->same && v->same->ignore)
5434 v->ignore = 1;
5436 if (v->ignore)
5437 continue;
5439 /* Update expression if this was combined, in case other giv was
5440 replaced. */
5441 if (v->same)
5442 v->new_reg = replace_rtx (v->new_reg,
5443 v->same->dest_reg, v->same->new_reg);
5445 /* See if this register is known to be a pointer to something. If
5446 so, see if we can find the alignment. First see if there is a
5447 destination register that is a pointer. If so, this shares the
5448 alignment too. Next see if we can deduce anything from the
5449 computational information. If not, and this is a DEST_ADDR
5450 giv, at least we know that it's a pointer, though we don't know
5451 the alignment. */
5452 if (REG_P (v->new_reg)
5453 && v->giv_type == DEST_REG
5454 && REG_POINTER (v->dest_reg))
5455 mark_reg_pointer (v->new_reg,
5456 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
5457 else if (REG_P (v->new_reg)
5458 && REG_POINTER (v->src_reg))
5460 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
5462 if (align == 0
5463 || GET_CODE (v->add_val) != CONST_INT
5464 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
5465 align = 0;
5467 mark_reg_pointer (v->new_reg, align);
5469 else if (REG_P (v->new_reg)
5470 && REG_P (v->add_val)
5471 && REG_POINTER (v->add_val))
5473 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
5475 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
5476 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
5477 align = 0;
5479 mark_reg_pointer (v->new_reg, align);
5481 else if (REG_P (v->new_reg) && v->giv_type == DEST_ADDR)
5482 mark_reg_pointer (v->new_reg, 0);
5484 if (v->giv_type == DEST_ADDR)
5486 /* Store reduced reg as the address in the memref where we found
5487 this giv. */
5488 if (validate_change_maybe_volatile (v->insn, v->location,
5489 v->new_reg))
5490 /* Yay, it worked! */;
5491 /* Not replaceable; emit an insn to set the original
5492 giv reg from the reduced giv. */
5493 else if (REG_P (*v->location))
5494 loop_insn_emit_before (loop, 0, v->insn,
5495 gen_move_insn (*v->location,
5496 v->new_reg));
5497 else if (GET_CODE (*v->location) == PLUS
5498 && REG_P (XEXP (*v->location, 0))
5499 && CONSTANT_P (XEXP (*v->location, 1)))
5501 rtx tem;
5502 start_sequence ();
5503 tem = expand_simple_binop (GET_MODE (*v->location), MINUS,
5504 v->new_reg, XEXP (*v->location, 1),
5505 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5506 emit_move_insn (XEXP (*v->location, 0), tem);
5507 tem = get_insns ();
5508 end_sequence ();
5509 loop_insn_emit_before (loop, 0, v->insn, tem);
5511 else
5513 /* If it wasn't a reg, create a pseudo and use that. */
5514 rtx reg, seq;
5515 start_sequence ();
5516 reg = force_reg (v->mode, *v->location);
5517 seq = get_insns ();
5518 end_sequence ();
5519 loop_insn_emit_before (loop, 0, v->insn, seq);
5520 if (!validate_change_maybe_volatile (v->insn, v->location, reg))
5521 gcc_unreachable ();
5524 else if (v->replaceable)
5526 reg_map[REGNO (v->dest_reg)] = v->new_reg;
5528 else
5530 rtx original_insn = v->insn;
5531 rtx note;
5533 /* Not replaceable; emit an insn to set the original giv reg from
5534 the reduced giv, same as above. */
5535 v->insn = loop_insn_emit_after (loop, 0, original_insn,
5536 gen_move_insn (v->dest_reg,
5537 v->new_reg));
5539 /* The original insn may have a REG_EQUAL note. This note is
5540 now incorrect and may result in invalid substitutions later.
5541 The original insn is dead, but may be part of a libcall
5542 sequence, which doesn't seem worth the bother of handling. */
5543 note = find_reg_note (original_insn, REG_EQUAL, NULL_RTX);
5544 if (note)
5545 remove_note (original_insn, note);
5548 /* When a loop is reversed, givs which depend on the reversed
5549 biv, and which are live outside the loop, must be set to their
5550 correct final value. This insn is only needed if the giv is
5551 not replaceable. The correct final value is the same as the
5552 value that the giv starts the reversed loop with. */
5553 if (bl->reversed && ! v->replaceable)
5554 loop_iv_add_mult_sink (loop,
5555 extend_value_for_giv (v, bl->initial_value),
5556 v->mult_val, v->add_val, v->dest_reg);
5557 else if (v->final_value)
5558 loop_insn_sink_or_swim (loop,
5559 gen_load_of_final_value (v->dest_reg,
5560 v->final_value));
5562 if (loop_dump_stream)
5564 fprintf (loop_dump_stream, "giv at %d reduced to ",
5565 INSN_UID (v->insn));
5566 print_simple_rtl (loop_dump_stream, v->new_reg);
5567 fprintf (loop_dump_stream, "\n");
5573 static int
5574 loop_giv_reduce_benefit (struct loop *loop ATTRIBUTE_UNUSED,
5575 struct iv_class *bl, struct induction *v,
5576 rtx test_reg)
5578 int add_cost;
5579 int benefit;
5581 benefit = v->benefit;
5582 PUT_MODE (test_reg, v->mode);
5583 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
5584 test_reg, test_reg);
5586 /* Reduce benefit if not replaceable, since we will insert a
5587 move-insn to replace the insn that calculates this giv. Don't do
5588 this unless the giv is a user variable, since it will often be
5589 marked non-replaceable because of the duplication of the exit
5590 code outside the loop. In such a case, the copies we insert are
5591 dead and will be deleted. So they don't have a cost. Similar
5592 situations exist. */
5593 /* ??? The new final_[bg]iv_value code does a much better job of
5594 finding replaceable giv's, and hence this code may no longer be
5595 necessary. */
5596 if (! v->replaceable && ! bl->eliminable
5597 && REG_USERVAR_P (v->dest_reg))
5598 benefit -= copy_cost;
5600 /* Decrease the benefit to count the add-insns that we will insert
5601 to increment the reduced reg for the giv. ??? This can
5602 overestimate the run-time cost of the additional insns, e.g. if
5603 there are multiple basic blocks that increment the biv, but only
5604 one of these blocks is executed during each iteration. There is
5605 no good way to detect cases like this with the current structure
5606 of the loop optimizer. This code is more accurate for
5607 determining code size than run-time benefits. */
5608 benefit -= add_cost * bl->biv_count;
5610 /* Decide whether to strength-reduce this giv or to leave the code
5611 unchanged (recompute it from the biv each time it is used). This
5612 decision can be made independently for each giv. */
5614 #ifdef AUTO_INC_DEC
5615 /* Attempt to guess whether autoincrement will handle some of the
5616 new add insns; if so, increase BENEFIT (undo the subtraction of
5617 add_cost that was done above). */
5618 if (v->giv_type == DEST_ADDR
5619 /* Increasing the benefit is risky, since this is only a guess.
5620 Avoid increasing register pressure in cases where there would
5621 be no other benefit from reducing this giv. */
5622 && benefit > 0
5623 && GET_CODE (v->mult_val) == CONST_INT)
5625 int size = GET_MODE_SIZE (GET_MODE (v->mem));
5627 if (HAVE_POST_INCREMENT
5628 && INTVAL (v->mult_val) == size)
5629 benefit += add_cost * bl->biv_count;
5630 else if (HAVE_PRE_INCREMENT
5631 && INTVAL (v->mult_val) == size)
5632 benefit += add_cost * bl->biv_count;
5633 else if (HAVE_POST_DECREMENT
5634 && -INTVAL (v->mult_val) == size)
5635 benefit += add_cost * bl->biv_count;
5636 else if (HAVE_PRE_DECREMENT
5637 && -INTVAL (v->mult_val) == size)
5638 benefit += add_cost * bl->biv_count;
5640 #endif
5642 return benefit;
5646 /* Free IV structures for LOOP. */
5648 static void
5649 loop_ivs_free (struct loop *loop)
5651 struct loop_ivs *ivs = LOOP_IVS (loop);
5652 struct iv_class *iv = ivs->list;
5654 free (ivs->regs);
5656 while (iv)
5658 struct iv_class *next = iv->next;
5659 struct induction *induction;
5660 struct induction *next_induction;
5662 for (induction = iv->biv; induction; induction = next_induction)
5664 next_induction = induction->next_iv;
5665 free (induction);
5667 for (induction = iv->giv; induction; induction = next_induction)
5669 next_induction = induction->next_iv;
5670 free (induction);
5673 free (iv);
5674 iv = next;
5678 /* Look back before LOOP->START for the insn that sets REG and return
5679 the equivalent constant if there is a REG_EQUAL note otherwise just
5680 the SET_SRC of REG. */
5682 static rtx
5683 loop_find_equiv_value (const struct loop *loop, rtx reg)
5685 rtx loop_start = loop->start;
5686 rtx insn, set;
5687 rtx ret;
5689 ret = reg;
5690 for (insn = PREV_INSN (loop_start); insn; insn = PREV_INSN (insn))
5692 if (LABEL_P (insn))
5693 break;
5695 else if (INSN_P (insn) && reg_set_p (reg, insn))
5697 /* We found the last insn before the loop that sets the register.
5698 If it sets the entire register, and has a REG_EQUAL note,
5699 then use the value of the REG_EQUAL note. */
5700 if ((set = single_set (insn))
5701 && (SET_DEST (set) == reg))
5703 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5705 /* Only use the REG_EQUAL note if it is a constant.
5706 Other things, divide in particular, will cause
5707 problems later if we use them. */
5708 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST
5709 && CONSTANT_P (XEXP (note, 0)))
5710 ret = XEXP (note, 0);
5711 else
5712 ret = SET_SRC (set);
5714 /* We cannot do this if it changes between the
5715 assignment and loop start though. */
5716 if (modified_between_p (ret, insn, loop_start))
5717 ret = reg;
5719 break;
5722 return ret;
5725 /* Find and return register term common to both expressions OP0 and
5726 OP1 or NULL_RTX if no such term exists. Each expression must be a
5727 REG or a PLUS of a REG. */
5729 static rtx
5730 find_common_reg_term (rtx op0, rtx op1)
5732 if ((REG_P (op0) || GET_CODE (op0) == PLUS)
5733 && (REG_P (op1) || GET_CODE (op1) == PLUS))
5735 rtx op00;
5736 rtx op01;
5737 rtx op10;
5738 rtx op11;
5740 if (GET_CODE (op0) == PLUS)
5741 op01 = XEXP (op0, 1), op00 = XEXP (op0, 0);
5742 else
5743 op01 = const0_rtx, op00 = op0;
5745 if (GET_CODE (op1) == PLUS)
5746 op11 = XEXP (op1, 1), op10 = XEXP (op1, 0);
5747 else
5748 op11 = const0_rtx, op10 = op1;
5750 /* Find and return common register term if present. */
5751 if (REG_P (op00) && (op00 == op10 || op00 == op11))
5752 return op00;
5753 else if (REG_P (op01) && (op01 == op10 || op01 == op11))
5754 return op01;
5757 /* No common register term found. */
5758 return NULL_RTX;
5761 /* Determine the loop iterator and calculate the number of loop
5762 iterations. Returns the exact number of loop iterations if it can
5763 be calculated, otherwise returns zero. */
5765 static unsigned HOST_WIDE_INT
5766 loop_iterations (struct loop *loop)
5768 struct loop_info *loop_info = LOOP_INFO (loop);
5769 struct loop_ivs *ivs = LOOP_IVS (loop);
5770 rtx comparison, comparison_value;
5771 rtx iteration_var, initial_value, increment, final_value;
5772 enum rtx_code comparison_code;
5773 HOST_WIDE_INT inc;
5774 unsigned HOST_WIDE_INT abs_inc;
5775 unsigned HOST_WIDE_INT abs_diff;
5776 int off_by_one;
5777 int increment_dir;
5778 int unsigned_p, compare_dir, final_larger;
5779 rtx last_loop_insn;
5780 struct iv_class *bl;
5782 loop_info->n_iterations = 0;
5783 loop_info->initial_value = 0;
5784 loop_info->initial_equiv_value = 0;
5785 loop_info->comparison_value = 0;
5786 loop_info->final_value = 0;
5787 loop_info->final_equiv_value = 0;
5788 loop_info->increment = 0;
5789 loop_info->iteration_var = 0;
5790 loop_info->iv = 0;
5792 /* We used to use prev_nonnote_insn here, but that fails because it might
5793 accidentally get the branch for a contained loop if the branch for this
5794 loop was deleted. We can only trust branches immediately before the
5795 loop_end. */
5796 last_loop_insn = PREV_INSN (loop->end);
5798 /* ??? We should probably try harder to find the jump insn
5799 at the end of the loop. The following code assumes that
5800 the last loop insn is a jump to the top of the loop. */
5801 if (!JUMP_P (last_loop_insn))
5803 if (loop_dump_stream)
5804 fprintf (loop_dump_stream,
5805 "Loop iterations: No final conditional branch found.\n");
5806 return 0;
5809 /* If there is a more than a single jump to the top of the loop
5810 we cannot (easily) determine the iteration count. */
5811 if (LABEL_NUSES (JUMP_LABEL (last_loop_insn)) > 1)
5813 if (loop_dump_stream)
5814 fprintf (loop_dump_stream,
5815 "Loop iterations: Loop has multiple back edges.\n");
5816 return 0;
5819 /* Find the iteration variable. If the last insn is a conditional
5820 branch, and the insn before tests a register value, make that the
5821 iteration variable. */
5823 comparison = get_condition_for_loop (loop, last_loop_insn);
5824 if (comparison == 0)
5826 if (loop_dump_stream)
5827 fprintf (loop_dump_stream,
5828 "Loop iterations: No final comparison found.\n");
5829 return 0;
5832 /* ??? Get_condition may switch position of induction variable and
5833 invariant register when it canonicalizes the comparison. */
5835 comparison_code = GET_CODE (comparison);
5836 iteration_var = XEXP (comparison, 0);
5837 comparison_value = XEXP (comparison, 1);
5839 if (!REG_P (iteration_var))
5841 if (loop_dump_stream)
5842 fprintf (loop_dump_stream,
5843 "Loop iterations: Comparison not against register.\n");
5844 return 0;
5847 /* The only new registers that are created before loop iterations
5848 are givs made from biv increments or registers created by
5849 load_mems. In the latter case, it is possible that try_copy_prop
5850 will propagate a new pseudo into the old iteration register but
5851 this will be marked by having the REG_USERVAR_P bit set. */
5853 gcc_assert ((unsigned) REGNO (iteration_var) < ivs->n_regs
5854 || REG_USERVAR_P (iteration_var));
5856 /* Determine the initial value of the iteration variable, and the amount
5857 that it is incremented each loop. Use the tables constructed by
5858 the strength reduction pass to calculate these values. */
5860 /* Clear the result values, in case no answer can be found. */
5861 initial_value = 0;
5862 increment = 0;
5864 /* The iteration variable can be either a giv or a biv. Check to see
5865 which it is, and compute the variable's initial value, and increment
5866 value if possible. */
5868 /* If this is a new register, can't handle it since we don't have any
5869 reg_iv_type entry for it. */
5870 if ((unsigned) REGNO (iteration_var) >= ivs->n_regs)
5872 if (loop_dump_stream)
5873 fprintf (loop_dump_stream,
5874 "Loop iterations: No reg_iv_type entry for iteration var.\n");
5875 return 0;
5878 /* Reject iteration variables larger than the host wide int size, since they
5879 could result in a number of iterations greater than the range of our
5880 `unsigned HOST_WIDE_INT' variable loop_info->n_iterations. */
5881 else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var))
5882 > HOST_BITS_PER_WIDE_INT))
5884 if (loop_dump_stream)
5885 fprintf (loop_dump_stream,
5886 "Loop iterations: Iteration var rejected because mode too large.\n");
5887 return 0;
5889 else if (GET_MODE_CLASS (GET_MODE (iteration_var)) != MODE_INT)
5891 if (loop_dump_stream)
5892 fprintf (loop_dump_stream,
5893 "Loop iterations: Iteration var not an integer.\n");
5894 return 0;
5897 /* Try swapping the comparison to identify a suitable iv. */
5898 if (REG_IV_TYPE (ivs, REGNO (iteration_var)) != BASIC_INDUCT
5899 && REG_IV_TYPE (ivs, REGNO (iteration_var)) != GENERAL_INDUCT
5900 && REG_P (comparison_value)
5901 && REGNO (comparison_value) < ivs->n_regs)
5903 rtx temp = comparison_value;
5904 comparison_code = swap_condition (comparison_code);
5905 comparison_value = iteration_var;
5906 iteration_var = temp;
5909 if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
5911 gcc_assert (REGNO (iteration_var) < ivs->n_regs);
5913 /* Grab initial value, only useful if it is a constant. */
5914 bl = REG_IV_CLASS (ivs, REGNO (iteration_var));
5915 initial_value = bl->initial_value;
5916 if (!bl->biv->always_executed || bl->biv->maybe_multiple)
5918 if (loop_dump_stream)
5919 fprintf (loop_dump_stream,
5920 "Loop iterations: Basic induction var not set once in each iteration.\n");
5921 return 0;
5924 increment = biv_total_increment (bl);
5926 else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
5928 HOST_WIDE_INT offset = 0;
5929 struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
5930 rtx biv_initial_value;
5932 gcc_assert (REGNO (v->src_reg) < ivs->n_regs);
5934 if (!v->always_executed || v->maybe_multiple)
5936 if (loop_dump_stream)
5937 fprintf (loop_dump_stream,
5938 "Loop iterations: General induction var not set once in each iteration.\n");
5939 return 0;
5942 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5944 /* Increment value is mult_val times the increment value of the biv. */
5946 increment = biv_total_increment (bl);
5947 if (increment)
5949 struct induction *biv_inc;
5951 increment = fold_rtx_mult_add (v->mult_val,
5952 extend_value_for_giv (v, increment),
5953 const0_rtx, v->mode);
5954 /* The caller assumes that one full increment has occurred at the
5955 first loop test. But that's not true when the biv is incremented
5956 after the giv is set (which is the usual case), e.g.:
5957 i = 6; do {;} while (i++ < 9) .
5958 Therefore, we bias the initial value by subtracting the amount of
5959 the increment that occurs between the giv set and the giv test. */
5960 for (biv_inc = bl->biv; biv_inc; biv_inc = biv_inc->next_iv)
5962 if (loop_insn_first_p (v->insn, biv_inc->insn))
5964 if (REG_P (biv_inc->add_val))
5966 if (loop_dump_stream)
5967 fprintf (loop_dump_stream,
5968 "Loop iterations: Basic induction var add_val is REG %d.\n",
5969 REGNO (biv_inc->add_val));
5970 return 0;
5973 /* If we have already counted it, skip it. */
5974 if (biv_inc->same)
5975 continue;
5977 offset -= INTVAL (biv_inc->add_val);
5981 if (loop_dump_stream)
5982 fprintf (loop_dump_stream,
5983 "Loop iterations: Giv iterator, initial value bias %ld.\n",
5984 (long) offset);
5986 /* Initial value is mult_val times the biv's initial value plus
5987 add_val. Only useful if it is a constant. */
5988 biv_initial_value = extend_value_for_giv (v, bl->initial_value);
5989 initial_value
5990 = fold_rtx_mult_add (v->mult_val,
5991 plus_constant (biv_initial_value, offset),
5992 v->add_val, v->mode);
5994 else
5996 if (loop_dump_stream)
5997 fprintf (loop_dump_stream,
5998 "Loop iterations: Not basic or general induction var.\n");
5999 return 0;
6002 if (initial_value == 0)
6003 return 0;
6005 unsigned_p = 0;
6006 off_by_one = 0;
6007 switch (comparison_code)
6009 case LEU:
6010 unsigned_p = 1;
6011 case LE:
6012 compare_dir = 1;
6013 off_by_one = 1;
6014 break;
6015 case GEU:
6016 unsigned_p = 1;
6017 case GE:
6018 compare_dir = -1;
6019 off_by_one = -1;
6020 break;
6021 case EQ:
6022 /* Cannot determine loop iterations with this case. */
6023 compare_dir = 0;
6024 break;
6025 case LTU:
6026 unsigned_p = 1;
6027 case LT:
6028 compare_dir = 1;
6029 break;
6030 case GTU:
6031 unsigned_p = 1;
6032 case GT:
6033 compare_dir = -1;
6034 break;
6035 case NE:
6036 compare_dir = 0;
6037 break;
6038 default:
6039 gcc_unreachable ();
6042 /* If the comparison value is an invariant register, then try to find
6043 its value from the insns before the start of the loop. */
6045 final_value = comparison_value;
6046 if (REG_P (comparison_value)
6047 && loop_invariant_p (loop, comparison_value))
6049 final_value = loop_find_equiv_value (loop, comparison_value);
6051 /* If we don't get an invariant final value, we are better
6052 off with the original register. */
6053 if (! loop_invariant_p (loop, final_value))
6054 final_value = comparison_value;
6057 /* Calculate the approximate final value of the induction variable
6058 (on the last successful iteration). The exact final value
6059 depends on the branch operator, and increment sign. It will be
6060 wrong if the iteration variable is not incremented by one each
6061 time through the loop and (comparison_value + off_by_one -
6062 initial_value) % increment != 0.
6063 ??? Note that the final_value may overflow and thus final_larger
6064 will be bogus. A potentially infinite loop will be classified
6065 as immediate, e.g. for (i = 0x7ffffff0; i <= 0x7fffffff; i++) */
6066 if (off_by_one)
6067 final_value = plus_constant (final_value, off_by_one);
6069 /* Save the calculated values describing this loop's bounds, in case
6070 precondition_loop_p will need them later. These values can not be
6071 recalculated inside precondition_loop_p because strength reduction
6072 optimizations may obscure the loop's structure.
6074 These values are only required by precondition_loop_p and insert_bct
6075 whenever the number of iterations cannot be computed at compile time.
6076 Only the difference between final_value and initial_value is
6077 important. Note that final_value is only approximate. */
6078 loop_info->initial_value = initial_value;
6079 loop_info->comparison_value = comparison_value;
6080 loop_info->final_value = plus_constant (comparison_value, off_by_one);
6081 loop_info->increment = increment;
6082 loop_info->iteration_var = iteration_var;
6083 loop_info->comparison_code = comparison_code;
6084 loop_info->iv = bl;
6086 /* Try to determine the iteration count for loops such
6087 as (for i = init; i < init + const; i++). When running the
6088 loop optimization twice, the first pass often converts simple
6089 loops into this form. */
6091 if (REG_P (initial_value))
6093 rtx reg1;
6094 rtx reg2;
6095 rtx const2;
6097 reg1 = initial_value;
6098 if (GET_CODE (final_value) == PLUS)
6099 reg2 = XEXP (final_value, 0), const2 = XEXP (final_value, 1);
6100 else
6101 reg2 = final_value, const2 = const0_rtx;
6103 /* Check for initial_value = reg1, final_value = reg2 + const2,
6104 where reg1 != reg2. */
6105 if (REG_P (reg2) && reg2 != reg1)
6107 rtx temp;
6109 /* Find what reg1 is equivalent to. Hopefully it will
6110 either be reg2 or reg2 plus a constant. */
6111 temp = loop_find_equiv_value (loop, reg1);
6113 if (find_common_reg_term (temp, reg2))
6114 initial_value = temp;
6115 else if (loop_invariant_p (loop, reg2))
6117 /* Find what reg2 is equivalent to. Hopefully it will
6118 either be reg1 or reg1 plus a constant. Let's ignore
6119 the latter case for now since it is not so common. */
6120 temp = loop_find_equiv_value (loop, reg2);
6122 if (temp == loop_info->iteration_var)
6123 temp = initial_value;
6124 if (temp == reg1)
6125 final_value = (const2 == const0_rtx)
6126 ? reg1 : gen_rtx_PLUS (GET_MODE (reg1), reg1, const2);
6131 loop_info->initial_equiv_value = initial_value;
6132 loop_info->final_equiv_value = final_value;
6134 /* For EQ comparison loops, we don't have a valid final value.
6135 Check this now so that we won't leave an invalid value if we
6136 return early for any other reason. */
6137 if (comparison_code == EQ)
6138 loop_info->final_equiv_value = loop_info->final_value = 0;
6140 if (increment == 0)
6142 if (loop_dump_stream)
6143 fprintf (loop_dump_stream,
6144 "Loop iterations: Increment value can't be calculated.\n");
6145 return 0;
6148 if (GET_CODE (increment) != CONST_INT)
6150 /* If we have a REG, check to see if REG holds a constant value. */
6151 /* ??? Other RTL, such as (neg (reg)) is possible here, but it isn't
6152 clear if it is worthwhile to try to handle such RTL. */
6153 if (REG_P (increment) || GET_CODE (increment) == SUBREG)
6154 increment = loop_find_equiv_value (loop, increment);
6156 if (GET_CODE (increment) != CONST_INT)
6158 if (loop_dump_stream)
6160 fprintf (loop_dump_stream,
6161 "Loop iterations: Increment value not constant ");
6162 print_simple_rtl (loop_dump_stream, increment);
6163 fprintf (loop_dump_stream, ".\n");
6165 return 0;
6167 loop_info->increment = increment;
6170 if (GET_CODE (initial_value) != CONST_INT)
6172 if (loop_dump_stream)
6174 fprintf (loop_dump_stream,
6175 "Loop iterations: Initial value not constant ");
6176 print_simple_rtl (loop_dump_stream, initial_value);
6177 fprintf (loop_dump_stream, ".\n");
6179 return 0;
6181 else if (GET_CODE (final_value) != CONST_INT)
6183 if (loop_dump_stream)
6185 fprintf (loop_dump_stream,
6186 "Loop iterations: Final value not constant ");
6187 print_simple_rtl (loop_dump_stream, final_value);
6188 fprintf (loop_dump_stream, ".\n");
6190 return 0;
6192 else if (comparison_code == EQ)
6194 rtx inc_once;
6196 if (loop_dump_stream)
6197 fprintf (loop_dump_stream, "Loop iterations: EQ comparison loop.\n");
6199 inc_once = gen_int_mode (INTVAL (initial_value) + INTVAL (increment),
6200 GET_MODE (iteration_var));
6202 if (inc_once == final_value)
6204 /* The iterator value once through the loop is equal to the
6205 comparison value. Either we have an infinite loop, or
6206 we'll loop twice. */
6207 if (increment == const0_rtx)
6208 return 0;
6209 loop_info->n_iterations = 2;
6211 else
6212 loop_info->n_iterations = 1;
6214 if (GET_CODE (loop_info->initial_value) == CONST_INT)
6215 loop_info->final_value
6216 = gen_int_mode ((INTVAL (loop_info->initial_value)
6217 + loop_info->n_iterations * INTVAL (increment)),
6218 GET_MODE (iteration_var));
6219 else
6220 loop_info->final_value
6221 = plus_constant (loop_info->initial_value,
6222 loop_info->n_iterations * INTVAL (increment));
6223 loop_info->final_equiv_value
6224 = gen_int_mode ((INTVAL (initial_value)
6225 + loop_info->n_iterations * INTVAL (increment)),
6226 GET_MODE (iteration_var));
6227 return loop_info->n_iterations;
6230 /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1. */
6231 if (unsigned_p)
6232 final_larger
6233 = ((unsigned HOST_WIDE_INT) INTVAL (final_value)
6234 > (unsigned HOST_WIDE_INT) INTVAL (initial_value))
6235 - ((unsigned HOST_WIDE_INT) INTVAL (final_value)
6236 < (unsigned HOST_WIDE_INT) INTVAL (initial_value));
6237 else
6238 final_larger = (INTVAL (final_value) > INTVAL (initial_value))
6239 - (INTVAL (final_value) < INTVAL (initial_value));
6241 if (INTVAL (increment) > 0)
6242 increment_dir = 1;
6243 else if (INTVAL (increment) == 0)
6244 increment_dir = 0;
6245 else
6246 increment_dir = -1;
6248 /* There are 27 different cases: compare_dir = -1, 0, 1;
6249 final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
6250 There are 4 normal cases, 4 reverse cases (where the iteration variable
6251 will overflow before the loop exits), 4 infinite loop cases, and 15
6252 immediate exit (0 or 1 iteration depending on loop type) cases.
6253 Only try to optimize the normal cases. */
6255 /* (compare_dir/final_larger/increment_dir)
6256 Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
6257 Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
6258 Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
6259 Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
6261 /* ?? If the meaning of reverse loops (where the iteration variable
6262 will overflow before the loop exits) is undefined, then could
6263 eliminate all of these special checks, and just always assume
6264 the loops are normal/immediate/infinite. Note that this means
6265 the sign of increment_dir does not have to be known. Also,
6266 since it does not really hurt if immediate exit loops or infinite loops
6267 are optimized, then that case could be ignored also, and hence all
6268 loops can be optimized.
6270 According to ANSI Spec, the reverse loop case result is undefined,
6271 because the action on overflow is undefined.
6273 See also the special test for NE loops below. */
6275 if (final_larger == increment_dir && final_larger != 0
6276 && (final_larger == compare_dir || compare_dir == 0))
6277 /* Normal case. */
6279 else
6281 if (loop_dump_stream)
6282 fprintf (loop_dump_stream, "Loop iterations: Not normal loop.\n");
6283 return 0;
6286 /* Calculate the number of iterations, final_value is only an approximation,
6287 so correct for that. Note that abs_diff and n_iterations are
6288 unsigned, because they can be as large as 2^n - 1. */
6290 inc = INTVAL (increment);
6291 gcc_assert (inc);
6292 if (inc > 0)
6294 abs_diff = INTVAL (final_value) - INTVAL (initial_value);
6295 abs_inc = inc;
6297 else
6299 abs_diff = INTVAL (initial_value) - INTVAL (final_value);
6300 abs_inc = -inc;
6303 /* Given that iteration_var is going to iterate over its own mode,
6304 not HOST_WIDE_INT, disregard higher bits that might have come
6305 into the picture due to sign extension of initial and final
6306 values. */
6307 abs_diff &= ((unsigned HOST_WIDE_INT) 1
6308 << (GET_MODE_BITSIZE (GET_MODE (iteration_var)) - 1)
6309 << 1) - 1;
6311 /* For NE tests, make sure that the iteration variable won't miss
6312 the final value. If abs_diff mod abs_incr is not zero, then the
6313 iteration variable will overflow before the loop exits, and we
6314 can not calculate the number of iterations. */
6315 if (compare_dir == 0 && (abs_diff % abs_inc) != 0)
6316 return 0;
6318 /* Note that the number of iterations could be calculated using
6319 (abs_diff + abs_inc - 1) / abs_inc, provided care was taken to
6320 handle potential overflow of the summation. */
6321 loop_info->n_iterations = abs_diff / abs_inc + ((abs_diff % abs_inc) != 0);
6322 return loop_info->n_iterations;
6325 /* Perform strength reduction and induction variable elimination.
6327 Pseudo registers created during this function will be beyond the
6328 last valid index in several tables including
6329 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
6330 problem here, because the added registers cannot be givs outside of
6331 their loop, and hence will never be reconsidered. But scan_loop
6332 must check regnos to make sure they are in bounds. */
6334 static void
6335 strength_reduce (struct loop *loop, int flags)
6337 struct loop_info *loop_info = LOOP_INFO (loop);
6338 struct loop_regs *regs = LOOP_REGS (loop);
6339 struct loop_ivs *ivs = LOOP_IVS (loop);
6340 rtx p;
6341 /* Temporary list pointer for traversing ivs->list. */
6342 struct iv_class *bl;
6343 /* Ratio of extra register life span we can justify
6344 for saving an instruction. More if loop doesn't call subroutines
6345 since in that case saving an insn makes more difference
6346 and more registers are available. */
6347 /* ??? could set this to last value of threshold in move_movables */
6348 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
6349 /* Map of pseudo-register replacements. */
6350 rtx *reg_map = NULL;
6351 int reg_map_size;
6352 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
6353 int insn_count = count_insns_in_loop (loop);
6355 addr_placeholder = gen_reg_rtx (Pmode);
6357 ivs->n_regs = max_reg_before_loop;
6358 ivs->regs = xcalloc (ivs->n_regs, sizeof (struct iv));
6360 /* Find all BIVs in loop. */
6361 loop_bivs_find (loop);
6363 /* Exit if there are no bivs. */
6364 if (! ivs->list)
6366 loop_ivs_free (loop);
6367 return;
6370 /* Determine how BIVS are initialized by looking through pre-header
6371 extended basic block. */
6372 loop_bivs_init_find (loop);
6374 /* Look at the each biv and see if we can say anything better about its
6375 initial value from any initializing insns set up above. */
6376 loop_bivs_check (loop);
6378 /* Search the loop for general induction variables. */
6379 loop_givs_find (loop);
6381 /* Try to calculate and save the number of loop iterations. This is
6382 set to zero if the actual number can not be calculated. This must
6383 be called after all giv's have been identified, since otherwise it may
6384 fail if the iteration variable is a giv. */
6385 loop_iterations (loop);
6387 #ifdef HAVE_prefetch
6388 if (flags & LOOP_PREFETCH)
6389 emit_prefetch_instructions (loop);
6390 #endif
6392 /* Now for each giv for which we still don't know whether or not it is
6393 replaceable, check to see if it is replaceable because its final value
6394 can be calculated. This must be done after loop_iterations is called,
6395 so that final_giv_value will work correctly. */
6396 loop_givs_check (loop);
6398 /* Try to prove that the loop counter variable (if any) is always
6399 nonnegative; if so, record that fact with a REG_NONNEG note
6400 so that "decrement and branch until zero" insn can be used. */
6401 check_dbra_loop (loop, insn_count);
6403 /* Create reg_map to hold substitutions for replaceable giv regs.
6404 Some givs might have been made from biv increments, so look at
6405 ivs->reg_iv_type for a suitable size. */
6406 reg_map_size = ivs->n_regs;
6407 reg_map = xcalloc (reg_map_size, sizeof (rtx));
6409 /* Examine each iv class for feasibility of strength reduction/induction
6410 variable elimination. */
6412 for (bl = ivs->list; bl; bl = bl->next)
6414 struct induction *v;
6415 int benefit;
6417 /* Test whether it will be possible to eliminate this biv
6418 provided all givs are reduced. */
6419 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
6421 /* This will be true at the end, if all givs which depend on this
6422 biv have been strength reduced.
6423 We can't (currently) eliminate the biv unless this is so. */
6424 bl->all_reduced = 1;
6426 /* Check each extension dependent giv in this class to see if its
6427 root biv is safe from wrapping in the interior mode. */
6428 check_ext_dependent_givs (loop, bl);
6430 /* Combine all giv's for this iv_class. */
6431 combine_givs (regs, bl);
6433 for (v = bl->giv; v; v = v->next_iv)
6435 struct induction *tv;
6437 if (v->ignore || v->same)
6438 continue;
6440 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
6442 /* If an insn is not to be strength reduced, then set its ignore
6443 flag, and clear bl->all_reduced. */
6445 /* A giv that depends on a reversed biv must be reduced if it is
6446 used after the loop exit, otherwise, it would have the wrong
6447 value after the loop exit. To make it simple, just reduce all
6448 of such giv's whether or not we know they are used after the loop
6449 exit. */
6451 if (v->lifetime * threshold * benefit < insn_count
6452 && ! bl->reversed)
6454 if (loop_dump_stream)
6455 fprintf (loop_dump_stream,
6456 "giv of insn %d not worth while, %d vs %d.\n",
6457 INSN_UID (v->insn),
6458 v->lifetime * threshold * benefit, insn_count);
6459 v->ignore = 1;
6460 bl->all_reduced = 0;
6462 else
6464 /* Check that we can increment the reduced giv without a
6465 multiply insn. If not, reject it. */
6467 for (tv = bl->biv; tv; tv = tv->next_iv)
6468 if (tv->mult_val == const1_rtx
6469 && ! product_cheap_p (tv->add_val, v->mult_val))
6471 if (loop_dump_stream)
6472 fprintf (loop_dump_stream,
6473 "giv of insn %d: would need a multiply.\n",
6474 INSN_UID (v->insn));
6475 v->ignore = 1;
6476 bl->all_reduced = 0;
6477 break;
6482 /* Check for givs whose first use is their definition and whose
6483 last use is the definition of another giv. If so, it is likely
6484 dead and should not be used to derive another giv nor to
6485 eliminate a biv. */
6486 loop_givs_dead_check (loop, bl);
6488 /* Reduce each giv that we decided to reduce. */
6489 loop_givs_reduce (loop, bl);
6491 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
6492 as not reduced.
6494 For each giv register that can be reduced now: if replaceable,
6495 substitute reduced reg wherever the old giv occurs;
6496 else add new move insn "giv_reg = reduced_reg". */
6497 loop_givs_rescan (loop, bl, reg_map);
6499 /* All the givs based on the biv bl have been reduced if they
6500 merit it. */
6502 /* For each giv not marked as maybe dead that has been combined with a
6503 second giv, clear any "maybe dead" mark on that second giv.
6504 v->new_reg will either be or refer to the register of the giv it
6505 combined with.
6507 Doing this clearing avoids problems in biv elimination where
6508 a giv's new_reg is a complex value that can't be put in the
6509 insn but the giv combined with (with a reg as new_reg) is
6510 marked maybe_dead. Since the register will be used in either
6511 case, we'd prefer it be used from the simpler giv. */
6513 for (v = bl->giv; v; v = v->next_iv)
6514 if (! v->maybe_dead && v->same)
6515 v->same->maybe_dead = 0;
6517 /* Try to eliminate the biv, if it is a candidate.
6518 This won't work if ! bl->all_reduced,
6519 since the givs we planned to use might not have been reduced.
6521 We have to be careful that we didn't initially think we could
6522 eliminate this biv because of a giv that we now think may be
6523 dead and shouldn't be used as a biv replacement.
6525 Also, there is the possibility that we may have a giv that looks
6526 like it can be used to eliminate a biv, but the resulting insn
6527 isn't valid. This can happen, for example, on the 88k, where a
6528 JUMP_INSN can compare a register only with zero. Attempts to
6529 replace it with a compare with a constant will fail.
6531 Note that in cases where this call fails, we may have replaced some
6532 of the occurrences of the biv with a giv, but no harm was done in
6533 doing so in the rare cases where it can occur. */
6535 if (bl->all_reduced == 1 && bl->eliminable
6536 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
6538 /* ?? If we created a new test to bypass the loop entirely,
6539 or otherwise drop straight in, based on this test, then
6540 we might want to rewrite it also. This way some later
6541 pass has more hope of removing the initialization of this
6542 biv entirely. */
6544 /* If final_value != 0, then the biv may be used after loop end
6545 and we must emit an insn to set it just in case.
6547 Reversed bivs already have an insn after the loop setting their
6548 value, so we don't need another one. We can't calculate the
6549 proper final value for such a biv here anyways. */
6550 if (bl->final_value && ! bl->reversed)
6551 loop_insn_sink_or_swim (loop,
6552 gen_load_of_final_value (bl->biv->dest_reg,
6553 bl->final_value));
6555 if (loop_dump_stream)
6556 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
6557 bl->regno);
6559 /* See above note wrt final_value. But since we couldn't eliminate
6560 the biv, we must set the value after the loop instead of before. */
6561 else if (bl->final_value && ! bl->reversed)
6562 loop_insn_sink (loop, gen_load_of_final_value (bl->biv->dest_reg,
6563 bl->final_value));
6566 /* Go through all the instructions in the loop, making all the
6567 register substitutions scheduled in REG_MAP. */
6569 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
6570 if (INSN_P (p))
6572 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
6573 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
6574 INSN_CODE (p) = -1;
6577 if (loop_dump_stream)
6578 fprintf (loop_dump_stream, "\n");
6580 loop_ivs_free (loop);
6581 if (reg_map)
6582 free (reg_map);
6585 /*Record all basic induction variables calculated in the insn. */
6586 static rtx
6587 check_insn_for_bivs (struct loop *loop, rtx p, int not_every_iteration,
6588 int maybe_multiple)
6590 struct loop_ivs *ivs = LOOP_IVS (loop);
6591 rtx set;
6592 rtx dest_reg;
6593 rtx inc_val;
6594 rtx mult_val;
6595 rtx *location;
6597 if (NONJUMP_INSN_P (p)
6598 && (set = single_set (p))
6599 && REG_P (SET_DEST (set)))
6601 dest_reg = SET_DEST (set);
6602 if (REGNO (dest_reg) < max_reg_before_loop
6603 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
6604 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
6606 if (basic_induction_var (loop, SET_SRC (set),
6607 GET_MODE (SET_SRC (set)),
6608 dest_reg, p, &inc_val, &mult_val,
6609 &location))
6611 /* It is a possible basic induction variable.
6612 Create and initialize an induction structure for it. */
6614 struct induction *v = xmalloc (sizeof (struct induction));
6616 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
6617 not_every_iteration, maybe_multiple);
6618 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
6620 else if (REGNO (dest_reg) < ivs->n_regs)
6621 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
6624 return p;
6627 /* Record all givs calculated in the insn.
6628 A register is a giv if: it is only set once, it is a function of a
6629 biv and a constant (or invariant), and it is not a biv. */
6630 static rtx
6631 check_insn_for_givs (struct loop *loop, rtx p, int not_every_iteration,
6632 int maybe_multiple)
6634 struct loop_regs *regs = LOOP_REGS (loop);
6636 rtx set;
6637 /* Look for a general induction variable in a register. */
6638 if (NONJUMP_INSN_P (p)
6639 && (set = single_set (p))
6640 && REG_P (SET_DEST (set))
6641 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
6643 rtx src_reg;
6644 rtx dest_reg;
6645 rtx add_val;
6646 rtx mult_val;
6647 rtx ext_val;
6648 int benefit;
6649 rtx regnote = 0;
6650 rtx last_consec_insn;
6652 dest_reg = SET_DEST (set);
6653 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
6654 return p;
6656 if (/* SET_SRC is a giv. */
6657 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
6658 &mult_val, &ext_val, 0, &benefit, VOIDmode)
6659 /* Equivalent expression is a giv. */
6660 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
6661 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
6662 &add_val, &mult_val, &ext_val, 0,
6663 &benefit, VOIDmode)))
6664 /* Don't try to handle any regs made by loop optimization.
6665 We have nothing on them in regno_first_uid, etc. */
6666 && REGNO (dest_reg) < max_reg_before_loop
6667 /* Don't recognize a BASIC_INDUCT_VAR here. */
6668 && dest_reg != src_reg
6669 /* This must be the only place where the register is set. */
6670 && (regs->array[REGNO (dest_reg)].n_times_set == 1
6671 /* or all sets must be consecutive and make a giv. */
6672 || (benefit = consec_sets_giv (loop, benefit, p,
6673 src_reg, dest_reg,
6674 &add_val, &mult_val, &ext_val,
6675 &last_consec_insn))))
6677 struct induction *v = xmalloc (sizeof (struct induction));
6679 /* If this is a library call, increase benefit. */
6680 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6681 benefit += libcall_benefit (p);
6683 /* Skip the consecutive insns, if there are any. */
6684 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
6685 p = last_consec_insn;
6687 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
6688 ext_val, benefit, DEST_REG, not_every_iteration,
6689 maybe_multiple, (rtx*) 0);
6694 /* Look for givs which are memory addresses. */
6695 if (NONJUMP_INSN_P (p))
6696 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
6697 maybe_multiple);
6699 /* Update the status of whether giv can derive other givs. This can
6700 change when we pass a label or an insn that updates a biv. */
6701 if (INSN_P (p) || LABEL_P (p))
6702 update_giv_derive (loop, p);
6703 return p;
6706 /* Return 1 if X is a valid source for an initial value (or as value being
6707 compared against in an initial test).
6709 X must be either a register or constant and must not be clobbered between
6710 the current insn and the start of the loop.
6712 INSN is the insn containing X. */
6714 static int
6715 valid_initial_value_p (rtx x, rtx insn, int call_seen, rtx loop_start)
6717 if (CONSTANT_P (x))
6718 return 1;
6720 /* Only consider pseudos we know about initialized in insns whose luids
6721 we know. */
6722 if (!REG_P (x)
6723 || REGNO (x) >= max_reg_before_loop)
6724 return 0;
6726 /* Don't use call-clobbered registers across a call which clobbers it. On
6727 some machines, don't use any hard registers at all. */
6728 if (REGNO (x) < FIRST_PSEUDO_REGISTER
6729 && (SMALL_REGISTER_CLASSES
6730 || (call_seen && call_used_regs[REGNO (x)])))
6731 return 0;
6733 /* Don't use registers that have been clobbered before the start of the
6734 loop. */
6735 if (reg_set_between_p (x, insn, loop_start))
6736 return 0;
6738 return 1;
6741 /* Scan X for memory refs and check each memory address
6742 as a possible giv. INSN is the insn whose pattern X comes from.
6743 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
6744 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
6745 more than once in each loop iteration. */
6747 static void
6748 find_mem_givs (const struct loop *loop, rtx x, rtx insn,
6749 int not_every_iteration, int maybe_multiple)
6751 int i, j;
6752 enum rtx_code code;
6753 const char *fmt;
6755 if (x == 0)
6756 return;
6758 code = GET_CODE (x);
6759 switch (code)
6761 case REG:
6762 case CONST_INT:
6763 case CONST:
6764 case CONST_DOUBLE:
6765 case SYMBOL_REF:
6766 case LABEL_REF:
6767 case PC:
6768 case CC0:
6769 case ADDR_VEC:
6770 case ADDR_DIFF_VEC:
6771 case USE:
6772 case CLOBBER:
6773 return;
6775 case MEM:
6777 rtx src_reg;
6778 rtx add_val;
6779 rtx mult_val;
6780 rtx ext_val;
6781 int benefit;
6783 /* This code used to disable creating GIVs with mult_val == 1 and
6784 add_val == 0. However, this leads to lost optimizations when
6785 it comes time to combine a set of related DEST_ADDR GIVs, since
6786 this one would not be seen. */
6788 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
6789 &mult_val, &ext_val, 1, &benefit,
6790 GET_MODE (x)))
6792 /* Found one; record it. */
6793 struct induction *v = xmalloc (sizeof (struct induction));
6795 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
6796 add_val, ext_val, benefit, DEST_ADDR,
6797 not_every_iteration, maybe_multiple, &XEXP (x, 0));
6799 v->mem = x;
6802 return;
6804 default:
6805 break;
6808 /* Recursively scan the subexpressions for other mem refs. */
6810 fmt = GET_RTX_FORMAT (code);
6811 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6812 if (fmt[i] == 'e')
6813 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
6814 maybe_multiple);
6815 else if (fmt[i] == 'E')
6816 for (j = 0; j < XVECLEN (x, i); j++)
6817 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
6818 maybe_multiple);
6821 /* Fill in the data about one biv update.
6822 V is the `struct induction' in which we record the biv. (It is
6823 allocated by the caller, with alloca.)
6824 INSN is the insn that sets it.
6825 DEST_REG is the biv's reg.
6827 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
6828 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
6829 being set to INC_VAL.
6831 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
6832 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
6833 can be executed more than once per iteration. If MAYBE_MULTIPLE
6834 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
6835 executed exactly once per iteration. */
6837 static void
6838 record_biv (struct loop *loop, struct induction *v, rtx insn, rtx dest_reg,
6839 rtx inc_val, rtx mult_val, rtx *location,
6840 int not_every_iteration, int maybe_multiple)
6842 struct loop_ivs *ivs = LOOP_IVS (loop);
6843 struct iv_class *bl;
6845 v->insn = insn;
6846 v->src_reg = dest_reg;
6847 v->dest_reg = dest_reg;
6848 v->mult_val = mult_val;
6849 v->add_val = inc_val;
6850 v->ext_dependent = NULL_RTX;
6851 v->location = location;
6852 v->mode = GET_MODE (dest_reg);
6853 v->always_computable = ! not_every_iteration;
6854 v->always_executed = ! not_every_iteration;
6855 v->maybe_multiple = maybe_multiple;
6856 v->same = 0;
6858 /* Add this to the reg's iv_class, creating a class
6859 if this is the first incrementation of the reg. */
6861 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
6862 if (bl == 0)
6864 /* Create and initialize new iv_class. */
6866 bl = xmalloc (sizeof (struct iv_class));
6868 bl->regno = REGNO (dest_reg);
6869 bl->biv = 0;
6870 bl->giv = 0;
6871 bl->biv_count = 0;
6872 bl->giv_count = 0;
6874 /* Set initial value to the reg itself. */
6875 bl->initial_value = dest_reg;
6876 bl->final_value = 0;
6877 /* We haven't seen the initializing insn yet. */
6878 bl->init_insn = 0;
6879 bl->init_set = 0;
6880 bl->initial_test = 0;
6881 bl->incremented = 0;
6882 bl->eliminable = 0;
6883 bl->nonneg = 0;
6884 bl->reversed = 0;
6885 bl->total_benefit = 0;
6887 /* Add this class to ivs->list. */
6888 bl->next = ivs->list;
6889 ivs->list = bl;
6891 /* Put it in the array of biv register classes. */
6892 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
6894 else
6896 /* Check if location is the same as a previous one. */
6897 struct induction *induction;
6898 for (induction = bl->biv; induction; induction = induction->next_iv)
6899 if (location == induction->location)
6901 v->same = induction;
6902 break;
6906 /* Update IV_CLASS entry for this biv. */
6907 v->next_iv = bl->biv;
6908 bl->biv = v;
6909 bl->biv_count++;
6910 if (mult_val == const1_rtx)
6911 bl->incremented = 1;
6913 if (loop_dump_stream)
6914 loop_biv_dump (v, loop_dump_stream, 0);
6917 /* Fill in the data about one giv.
6918 V is the `struct induction' in which we record the giv. (It is
6919 allocated by the caller, with alloca.)
6920 INSN is the insn that sets it.
6921 BENEFIT estimates the savings from deleting this insn.
6922 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
6923 into a register or is used as a memory address.
6925 SRC_REG is the biv reg which the giv is computed from.
6926 DEST_REG is the giv's reg (if the giv is stored in a reg).
6927 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
6928 LOCATION points to the place where this giv's value appears in INSN. */
6930 static void
6931 record_giv (const struct loop *loop, struct induction *v, rtx insn,
6932 rtx src_reg, rtx dest_reg, rtx mult_val, rtx add_val,
6933 rtx ext_val, int benefit, enum g_types type,
6934 int not_every_iteration, int maybe_multiple, rtx *location)
6936 struct loop_ivs *ivs = LOOP_IVS (loop);
6937 struct induction *b;
6938 struct iv_class *bl;
6939 rtx set = single_set (insn);
6940 rtx temp;
6942 /* Attempt to prove constantness of the values. Don't let simplify_rtx
6943 undo the MULT canonicalization that we performed earlier. */
6944 temp = simplify_rtx (add_val);
6945 if (temp
6946 && ! (GET_CODE (add_val) == MULT
6947 && GET_CODE (temp) == ASHIFT))
6948 add_val = temp;
6950 v->insn = insn;
6951 v->src_reg = src_reg;
6952 v->giv_type = type;
6953 v->dest_reg = dest_reg;
6954 v->mult_val = mult_val;
6955 v->add_val = add_val;
6956 v->ext_dependent = ext_val;
6957 v->benefit = benefit;
6958 v->location = location;
6959 v->cant_derive = 0;
6960 v->combined_with = 0;
6961 v->maybe_multiple = maybe_multiple;
6962 v->maybe_dead = 0;
6963 v->derive_adjustment = 0;
6964 v->same = 0;
6965 v->ignore = 0;
6966 v->new_reg = 0;
6967 v->final_value = 0;
6968 v->same_insn = 0;
6969 v->auto_inc_opt = 0;
6970 v->shared = 0;
6972 /* The v->always_computable field is used in update_giv_derive, to
6973 determine whether a giv can be used to derive another giv. For a
6974 DEST_REG giv, INSN computes a new value for the giv, so its value
6975 isn't computable if INSN insn't executed every iteration.
6976 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
6977 it does not compute a new value. Hence the value is always computable
6978 regardless of whether INSN is executed each iteration. */
6980 if (type == DEST_ADDR)
6981 v->always_computable = 1;
6982 else
6983 v->always_computable = ! not_every_iteration;
6985 v->always_executed = ! not_every_iteration;
6987 if (type == DEST_ADDR)
6989 v->mode = GET_MODE (*location);
6990 v->lifetime = 1;
6992 else /* type == DEST_REG */
6994 v->mode = GET_MODE (SET_DEST (set));
6996 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
6998 /* If the lifetime is zero, it means that this register is
6999 really a dead store. So mark this as a giv that can be
7000 ignored. This will not prevent the biv from being eliminated. */
7001 if (v->lifetime == 0)
7002 v->ignore = 1;
7004 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
7005 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
7008 /* Add the giv to the class of givs computed from one biv. */
7010 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
7011 gcc_assert (bl);
7012 v->next_iv = bl->giv;
7013 bl->giv = v;
7015 /* Don't count DEST_ADDR. This is supposed to count the number of
7016 insns that calculate givs. */
7017 if (type == DEST_REG)
7018 bl->giv_count++;
7019 bl->total_benefit += benefit;
7021 if (type == DEST_ADDR)
7023 v->replaceable = 1;
7024 v->not_replaceable = 0;
7026 else
7028 /* The giv can be replaced outright by the reduced register only if all
7029 of the following conditions are true:
7030 - the insn that sets the giv is always executed on any iteration
7031 on which the giv is used at all
7032 (there are two ways to deduce this:
7033 either the insn is executed on every iteration,
7034 or all uses follow that insn in the same basic block),
7035 - the giv is not used outside the loop
7036 - no assignments to the biv occur during the giv's lifetime. */
7038 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
7039 /* Previous line always fails if INSN was moved by loop opt. */
7040 && REGNO_LAST_LUID (REGNO (dest_reg))
7041 < INSN_LUID (loop->end)
7042 && (! not_every_iteration
7043 || last_use_this_basic_block (dest_reg, insn)))
7045 /* Now check that there are no assignments to the biv within the
7046 giv's lifetime. This requires two separate checks. */
7048 /* Check each biv update, and fail if any are between the first
7049 and last use of the giv.
7051 If this loop contains an inner loop that was unrolled, then
7052 the insn modifying the biv may have been emitted by the loop
7053 unrolling code, and hence does not have a valid luid. Just
7054 mark the biv as not replaceable in this case. It is not very
7055 useful as a biv, because it is used in two different loops.
7056 It is very unlikely that we would be able to optimize the giv
7057 using this biv anyways. */
7059 v->replaceable = 1;
7060 v->not_replaceable = 0;
7061 for (b = bl->biv; b; b = b->next_iv)
7063 if (INSN_UID (b->insn) >= max_uid_for_loop
7064 || ((INSN_LUID (b->insn)
7065 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
7066 && (INSN_LUID (b->insn)
7067 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
7069 v->replaceable = 0;
7070 v->not_replaceable = 1;
7071 break;
7075 /* If there are any backwards branches that go from after the
7076 biv update to before it, then this giv is not replaceable. */
7077 if (v->replaceable)
7078 for (b = bl->biv; b; b = b->next_iv)
7079 if (back_branch_in_range_p (loop, b->insn))
7081 v->replaceable = 0;
7082 v->not_replaceable = 1;
7083 break;
7086 else
7088 /* May still be replaceable, we don't have enough info here to
7089 decide. */
7090 v->replaceable = 0;
7091 v->not_replaceable = 0;
7095 /* Record whether the add_val contains a const_int, for later use by
7096 combine_givs. */
7098 rtx tem = add_val;
7100 v->no_const_addval = 1;
7101 if (tem == const0_rtx)
7103 else if (CONSTANT_P (add_val))
7104 v->no_const_addval = 0;
7105 if (GET_CODE (tem) == PLUS)
7107 while (1)
7109 if (GET_CODE (XEXP (tem, 0)) == PLUS)
7110 tem = XEXP (tem, 0);
7111 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
7112 tem = XEXP (tem, 1);
7113 else
7114 break;
7116 if (CONSTANT_P (XEXP (tem, 1)))
7117 v->no_const_addval = 0;
7121 if (loop_dump_stream)
7122 loop_giv_dump (v, loop_dump_stream, 0);
7125 /* Try to calculate the final value of the giv, the value it will have at
7126 the end of the loop. If we can do it, return that value. */
7128 static rtx
7129 final_giv_value (const struct loop *loop, struct induction *v)
7131 struct loop_ivs *ivs = LOOP_IVS (loop);
7132 struct iv_class *bl;
7133 rtx insn;
7134 rtx increment, tem;
7135 rtx seq;
7136 rtx loop_end = loop->end;
7137 unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
7139 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
7141 /* The final value for givs which depend on reversed bivs must be calculated
7142 differently than for ordinary givs. In this case, there is already an
7143 insn after the loop which sets this giv's final value (if necessary),
7144 and there are no other loop exits, so we can return any value. */
7145 if (bl->reversed)
7147 if (loop_dump_stream)
7148 fprintf (loop_dump_stream,
7149 "Final giv value for %d, depends on reversed biv\n",
7150 REGNO (v->dest_reg));
7151 return const0_rtx;
7154 /* Try to calculate the final value as a function of the biv it depends
7155 upon. The only exit from the loop must be the fall through at the bottom
7156 and the insn that sets the giv must be executed on every iteration
7157 (otherwise the giv may not have its final value when the loop exits). */
7159 /* ??? Can calculate the final giv value by subtracting off the
7160 extra biv increments times the giv's mult_val. The loop must have
7161 only one exit for this to work, but the loop iterations does not need
7162 to be known. */
7164 if (n_iterations != 0
7165 && ! loop->exit_count
7166 && v->always_executed)
7168 /* ?? It is tempting to use the biv's value here since these insns will
7169 be put after the loop, and hence the biv will have its final value
7170 then. However, this fails if the biv is subsequently eliminated.
7171 Perhaps determine whether biv's are eliminable before trying to
7172 determine whether giv's are replaceable so that we can use the
7173 biv value here if it is not eliminable. */
7175 /* We are emitting code after the end of the loop, so we must make
7176 sure that bl->initial_value is still valid then. It will still
7177 be valid if it is invariant. */
7179 increment = biv_total_increment (bl);
7181 if (increment && loop_invariant_p (loop, increment)
7182 && loop_invariant_p (loop, bl->initial_value))
7184 /* Can calculate the loop exit value of its biv as
7185 (n_iterations * increment) + initial_value */
7187 /* The loop exit value of the giv is then
7188 (final_biv_value - extra increments) * mult_val + add_val.
7189 The extra increments are any increments to the biv which
7190 occur in the loop after the giv's value is calculated.
7191 We must search from the insn that sets the giv to the end
7192 of the loop to calculate this value. */
7194 /* Put the final biv value in tem. */
7195 tem = gen_reg_rtx (v->mode);
7196 record_base_value (REGNO (tem), bl->biv->add_val, 0);
7197 loop_iv_add_mult_sink (loop, extend_value_for_giv (v, increment),
7198 GEN_INT (n_iterations),
7199 extend_value_for_giv (v, bl->initial_value),
7200 tem);
7202 /* Subtract off extra increments as we find them. */
7203 for (insn = NEXT_INSN (v->insn); insn != loop_end;
7204 insn = NEXT_INSN (insn))
7206 struct induction *biv;
7208 for (biv = bl->biv; biv; biv = biv->next_iv)
7209 if (biv->insn == insn)
7211 start_sequence ();
7212 tem = expand_simple_binop (GET_MODE (tem), MINUS, tem,
7213 biv->add_val, NULL_RTX, 0,
7214 OPTAB_LIB_WIDEN);
7215 seq = get_insns ();
7216 end_sequence ();
7217 loop_insn_sink (loop, seq);
7221 /* Now calculate the giv's final value. */
7222 loop_iv_add_mult_sink (loop, tem, v->mult_val, v->add_val, tem);
7224 if (loop_dump_stream)
7225 fprintf (loop_dump_stream,
7226 "Final giv value for %d, calc from biv's value.\n",
7227 REGNO (v->dest_reg));
7229 return tem;
7233 /* Replaceable giv's should never reach here. */
7234 gcc_assert (!v->replaceable);
7236 /* Check to see if the biv is dead at all loop exits. */
7237 if (reg_dead_after_loop (loop, v->dest_reg))
7239 if (loop_dump_stream)
7240 fprintf (loop_dump_stream,
7241 "Final giv value for %d, giv dead after loop exit.\n",
7242 REGNO (v->dest_reg));
7244 return const0_rtx;
7247 return 0;
7250 /* All this does is determine whether a giv can be made replaceable because
7251 its final value can be calculated. This code can not be part of record_giv
7252 above, because final_giv_value requires that the number of loop iterations
7253 be known, and that can not be accurately calculated until after all givs
7254 have been identified. */
7256 static void
7257 check_final_value (const struct loop *loop, struct induction *v)
7259 rtx final_value = 0;
7261 /* DEST_ADDR givs will never reach here, because they are always marked
7262 replaceable above in record_giv. */
7264 /* The giv can be replaced outright by the reduced register only if all
7265 of the following conditions are true:
7266 - the insn that sets the giv is always executed on any iteration
7267 on which the giv is used at all
7268 (there are two ways to deduce this:
7269 either the insn is executed on every iteration,
7270 or all uses follow that insn in the same basic block),
7271 - its final value can be calculated (this condition is different
7272 than the one above in record_giv)
7273 - it's not used before the it's set
7274 - no assignments to the biv occur during the giv's lifetime. */
7276 #if 0
7277 /* This is only called now when replaceable is known to be false. */
7278 /* Clear replaceable, so that it won't confuse final_giv_value. */
7279 v->replaceable = 0;
7280 #endif
7282 if ((final_value = final_giv_value (loop, v))
7283 && (v->always_executed
7284 || last_use_this_basic_block (v->dest_reg, v->insn)))
7286 int biv_increment_seen = 0, before_giv_insn = 0;
7287 rtx p = v->insn;
7288 rtx last_giv_use;
7290 v->replaceable = 1;
7291 v->not_replaceable = 0;
7293 /* When trying to determine whether or not a biv increment occurs
7294 during the lifetime of the giv, we can ignore uses of the variable
7295 outside the loop because final_value is true. Hence we can not
7296 use regno_last_uid and regno_first_uid as above in record_giv. */
7298 /* Search the loop to determine whether any assignments to the
7299 biv occur during the giv's lifetime. Start with the insn
7300 that sets the giv, and search around the loop until we come
7301 back to that insn again.
7303 Also fail if there is a jump within the giv's lifetime that jumps
7304 to somewhere outside the lifetime but still within the loop. This
7305 catches spaghetti code where the execution order is not linear, and
7306 hence the above test fails. Here we assume that the giv lifetime
7307 does not extend from one iteration of the loop to the next, so as
7308 to make the test easier. Since the lifetime isn't known yet,
7309 this requires two loops. See also record_giv above. */
7311 last_giv_use = v->insn;
7313 while (1)
7315 p = NEXT_INSN (p);
7316 if (p == loop->end)
7318 before_giv_insn = 1;
7319 p = NEXT_INSN (loop->start);
7321 if (p == v->insn)
7322 break;
7324 if (INSN_P (p))
7326 /* It is possible for the BIV increment to use the GIV if we
7327 have a cycle. Thus we must be sure to check each insn for
7328 both BIV and GIV uses, and we must check for BIV uses
7329 first. */
7331 if (! biv_increment_seen
7332 && reg_set_p (v->src_reg, PATTERN (p)))
7333 biv_increment_seen = 1;
7335 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
7337 if (biv_increment_seen || before_giv_insn)
7339 v->replaceable = 0;
7340 v->not_replaceable = 1;
7341 break;
7343 last_giv_use = p;
7348 /* Now that the lifetime of the giv is known, check for branches
7349 from within the lifetime to outside the lifetime if it is still
7350 replaceable. */
7352 if (v->replaceable)
7354 p = v->insn;
7355 while (1)
7357 p = NEXT_INSN (p);
7358 if (p == loop->end)
7359 p = NEXT_INSN (loop->start);
7360 if (p == last_giv_use)
7361 break;
7363 if (JUMP_P (p) && JUMP_LABEL (p)
7364 && LABEL_NAME (JUMP_LABEL (p))
7365 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
7366 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
7367 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
7368 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
7370 v->replaceable = 0;
7371 v->not_replaceable = 1;
7373 if (loop_dump_stream)
7374 fprintf (loop_dump_stream,
7375 "Found branch outside giv lifetime.\n");
7377 break;
7382 /* If it is replaceable, then save the final value. */
7383 if (v->replaceable)
7384 v->final_value = final_value;
7387 if (loop_dump_stream && v->replaceable)
7388 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
7389 INSN_UID (v->insn), REGNO (v->dest_reg));
7392 /* Update the status of whether a giv can derive other givs.
7394 We need to do something special if there is or may be an update to the biv
7395 between the time the giv is defined and the time it is used to derive
7396 another giv.
7398 In addition, a giv that is only conditionally set is not allowed to
7399 derive another giv once a label has been passed.
7401 The cases we look at are when a label or an update to a biv is passed. */
7403 static void
7404 update_giv_derive (const struct loop *loop, rtx p)
7406 struct loop_ivs *ivs = LOOP_IVS (loop);
7407 struct iv_class *bl;
7408 struct induction *biv, *giv;
7409 rtx tem;
7410 int dummy;
7412 /* Search all IV classes, then all bivs, and finally all givs.
7414 There are three cases we are concerned with. First we have the situation
7415 of a giv that is only updated conditionally. In that case, it may not
7416 derive any givs after a label is passed.
7418 The second case is when a biv update occurs, or may occur, after the
7419 definition of a giv. For certain biv updates (see below) that are
7420 known to occur between the giv definition and use, we can adjust the
7421 giv definition. For others, or when the biv update is conditional,
7422 we must prevent the giv from deriving any other givs. There are two
7423 sub-cases within this case.
7425 If this is a label, we are concerned with any biv update that is done
7426 conditionally, since it may be done after the giv is defined followed by
7427 a branch here (actually, we need to pass both a jump and a label, but
7428 this extra tracking doesn't seem worth it).
7430 If this is a jump, we are concerned about any biv update that may be
7431 executed multiple times. We are actually only concerned about
7432 backward jumps, but it is probably not worth performing the test
7433 on the jump again here.
7435 If this is a biv update, we must adjust the giv status to show that a
7436 subsequent biv update was performed. If this adjustment cannot be done,
7437 the giv cannot derive further givs. */
7439 for (bl = ivs->list; bl; bl = bl->next)
7440 for (biv = bl->biv; biv; biv = biv->next_iv)
7441 if (LABEL_P (p) || JUMP_P (p)
7442 || biv->insn == p)
7444 /* Skip if location is the same as a previous one. */
7445 if (biv->same)
7446 continue;
7448 for (giv = bl->giv; giv; giv = giv->next_iv)
7450 /* If cant_derive is already true, there is no point in
7451 checking all of these conditions again. */
7452 if (giv->cant_derive)
7453 continue;
7455 /* If this giv is conditionally set and we have passed a label,
7456 it cannot derive anything. */
7457 if (LABEL_P (p) && ! giv->always_computable)
7458 giv->cant_derive = 1;
7460 /* Skip givs that have mult_val == 0, since
7461 they are really invariants. Also skip those that are
7462 replaceable, since we know their lifetime doesn't contain
7463 any biv update. */
7464 else if (giv->mult_val == const0_rtx || giv->replaceable)
7465 continue;
7467 /* The only way we can allow this giv to derive another
7468 is if this is a biv increment and we can form the product
7469 of biv->add_val and giv->mult_val. In this case, we will
7470 be able to compute a compensation. */
7471 else if (biv->insn == p)
7473 rtx ext_val_dummy;
7475 tem = 0;
7476 if (biv->mult_val == const1_rtx)
7477 tem = simplify_giv_expr (loop,
7478 gen_rtx_MULT (giv->mode,
7479 biv->add_val,
7480 giv->mult_val),
7481 &ext_val_dummy, &dummy);
7483 if (tem && giv->derive_adjustment)
7484 tem = simplify_giv_expr
7485 (loop,
7486 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
7487 &ext_val_dummy, &dummy);
7489 if (tem)
7490 giv->derive_adjustment = tem;
7491 else
7492 giv->cant_derive = 1;
7494 else if ((LABEL_P (p) && ! biv->always_computable)
7495 || (JUMP_P (p) && biv->maybe_multiple))
7496 giv->cant_derive = 1;
7501 /* Check whether an insn is an increment legitimate for a basic induction var.
7502 X is the source of insn P, or a part of it.
7503 MODE is the mode in which X should be interpreted.
7505 DEST_REG is the putative biv, also the destination of the insn.
7506 We accept patterns of these forms:
7507 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
7508 REG = INVARIANT + REG
7510 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
7511 store the additive term into *INC_VAL, and store the place where
7512 we found the additive term into *LOCATION.
7514 If X is an assignment of an invariant into DEST_REG, we set
7515 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
7517 We also want to detect a BIV when it corresponds to a variable
7518 whose mode was promoted. In that case, an increment
7519 of the variable may be a PLUS that adds a SUBREG of that variable to
7520 an invariant and then sign- or zero-extends the result of the PLUS
7521 into the variable.
7523 Most GIVs in such cases will be in the promoted mode, since that is the
7524 probably the natural computation mode (and almost certainly the mode
7525 used for addresses) on the machine. So we view the pseudo-reg containing
7526 the variable as the BIV, as if it were simply incremented.
7528 Note that treating the entire pseudo as a BIV will result in making
7529 simple increments to any GIVs based on it. However, if the variable
7530 overflows in its declared mode but not its promoted mode, the result will
7531 be incorrect. This is acceptable if the variable is signed, since
7532 overflows in such cases are undefined, but not if it is unsigned, since
7533 those overflows are defined. So we only check for SIGN_EXTEND and
7534 not ZERO_EXTEND.
7536 If we cannot find a biv, we return 0. */
7538 static int
7539 basic_induction_var (const struct loop *loop, rtx x, enum machine_mode mode,
7540 rtx dest_reg, rtx p, rtx *inc_val, rtx *mult_val,
7541 rtx **location)
7543 enum rtx_code code;
7544 rtx *argp, arg;
7545 rtx insn, set = 0, last, inc;
7547 code = GET_CODE (x);
7548 *location = NULL;
7549 switch (code)
7551 case PLUS:
7552 if (rtx_equal_p (XEXP (x, 0), dest_reg)
7553 || (GET_CODE (XEXP (x, 0)) == SUBREG
7554 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
7555 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
7557 argp = &XEXP (x, 1);
7559 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
7560 || (GET_CODE (XEXP (x, 1)) == SUBREG
7561 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
7562 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
7564 argp = &XEXP (x, 0);
7566 else
7567 return 0;
7569 arg = *argp;
7570 if (loop_invariant_p (loop, arg) != 1)
7571 return 0;
7573 /* convert_modes can emit new instructions, e.g. when arg is a loop
7574 invariant MEM and dest_reg has a different mode.
7575 These instructions would be emitted after the end of the function
7576 and then *inc_val would be an uninitialized pseudo.
7577 Detect this and bail in this case.
7578 Other alternatives to solve this can be introducing a convert_modes
7579 variant which is allowed to fail but not allowed to emit new
7580 instructions, emit these instructions before loop start and let
7581 it be garbage collected if *inc_val is never used or saving the
7582 *inc_val initialization sequence generated here and when *inc_val
7583 is going to be actually used, emit it at some suitable place. */
7584 last = get_last_insn ();
7585 inc = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
7586 if (get_last_insn () != last)
7588 delete_insns_since (last);
7589 return 0;
7592 *inc_val = inc;
7593 *mult_val = const1_rtx;
7594 *location = argp;
7595 return 1;
7597 case SUBREG:
7598 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
7599 handle addition of promoted variables.
7600 ??? The comment at the start of this function is wrong: promoted
7601 variable increments don't look like it says they do. */
7602 return basic_induction_var (loop, SUBREG_REG (x),
7603 GET_MODE (SUBREG_REG (x)),
7604 dest_reg, p, inc_val, mult_val, location);
7606 case REG:
7607 /* If this register is assigned in a previous insn, look at its
7608 source, but don't go outside the loop or past a label. */
7610 /* If this sets a register to itself, we would repeat any previous
7611 biv increment if we applied this strategy blindly. */
7612 if (rtx_equal_p (dest_reg, x))
7613 return 0;
7615 insn = p;
7616 while (1)
7618 rtx dest;
7621 insn = PREV_INSN (insn);
7623 while (insn && NOTE_P (insn)
7624 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
7626 if (!insn)
7627 break;
7628 set = single_set (insn);
7629 if (set == 0)
7630 break;
7631 dest = SET_DEST (set);
7632 if (dest == x
7633 || (GET_CODE (dest) == SUBREG
7634 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
7635 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
7636 && SUBREG_REG (dest) == x))
7637 return basic_induction_var (loop, SET_SRC (set),
7638 (GET_MODE (SET_SRC (set)) == VOIDmode
7639 ? GET_MODE (x)
7640 : GET_MODE (SET_SRC (set))),
7641 dest_reg, insn,
7642 inc_val, mult_val, location);
7644 while (GET_CODE (dest) == SUBREG
7645 || GET_CODE (dest) == ZERO_EXTRACT
7646 || GET_CODE (dest) == STRICT_LOW_PART)
7647 dest = XEXP (dest, 0);
7648 if (dest == x)
7649 break;
7651 /* Fall through. */
7653 /* Can accept constant setting of biv only when inside inner most loop.
7654 Otherwise, a biv of an inner loop may be incorrectly recognized
7655 as a biv of the outer loop,
7656 causing code to be moved INTO the inner loop. */
7657 case MEM:
7658 if (loop_invariant_p (loop, x) != 1)
7659 return 0;
7660 case CONST_INT:
7661 case SYMBOL_REF:
7662 case CONST:
7663 /* convert_modes dies if we try to convert to or from CCmode, so just
7664 exclude that case. It is very unlikely that a condition code value
7665 would be a useful iterator anyways. convert_modes dies if we try to
7666 convert a float mode to non-float or vice versa too. */
7667 if (loop->level == 1
7668 && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
7669 && GET_MODE_CLASS (mode) != MODE_CC)
7671 /* Possible bug here? Perhaps we don't know the mode of X. */
7672 last = get_last_insn ();
7673 inc = convert_modes (GET_MODE (dest_reg), mode, x, 0);
7674 if (get_last_insn () != last)
7676 delete_insns_since (last);
7677 return 0;
7680 *inc_val = inc;
7681 *mult_val = const0_rtx;
7682 return 1;
7684 else
7685 return 0;
7687 case SIGN_EXTEND:
7688 /* Ignore this BIV if signed arithmetic overflow is defined. */
7689 if (flag_wrapv)
7690 return 0;
7691 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
7692 dest_reg, p, inc_val, mult_val, location);
7694 case ASHIFTRT:
7695 /* Similar, since this can be a sign extension. */
7696 for (insn = PREV_INSN (p);
7697 (insn && NOTE_P (insn)
7698 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
7699 insn = PREV_INSN (insn))
7702 if (insn)
7703 set = single_set (insn);
7705 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
7706 && set && SET_DEST (set) == XEXP (x, 0)
7707 && GET_CODE (XEXP (x, 1)) == CONST_INT
7708 && INTVAL (XEXP (x, 1)) >= 0
7709 && GET_CODE (SET_SRC (set)) == ASHIFT
7710 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
7711 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
7712 GET_MODE (XEXP (x, 0)),
7713 dest_reg, insn, inc_val, mult_val,
7714 location);
7715 return 0;
7717 default:
7718 return 0;
7722 /* A general induction variable (giv) is any quantity that is a linear
7723 function of a basic induction variable,
7724 i.e. giv = biv * mult_val + add_val.
7725 The coefficients can be any loop invariant quantity.
7726 A giv need not be computed directly from the biv;
7727 it can be computed by way of other givs. */
7729 /* Determine whether X computes a giv.
7730 If it does, return a nonzero value
7731 which is the benefit from eliminating the computation of X;
7732 set *SRC_REG to the register of the biv that it is computed from;
7733 set *ADD_VAL and *MULT_VAL to the coefficients,
7734 such that the value of X is biv * mult + add; */
7736 static int
7737 general_induction_var (const struct loop *loop, rtx x, rtx *src_reg,
7738 rtx *add_val, rtx *mult_val, rtx *ext_val,
7739 int is_addr, int *pbenefit,
7740 enum machine_mode addr_mode)
7742 struct loop_ivs *ivs = LOOP_IVS (loop);
7743 rtx orig_x = x;
7745 /* If this is an invariant, forget it, it isn't a giv. */
7746 if (loop_invariant_p (loop, x) == 1)
7747 return 0;
7749 *pbenefit = 0;
7750 *ext_val = NULL_RTX;
7751 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
7752 if (x == 0)
7753 return 0;
7755 switch (GET_CODE (x))
7757 case USE:
7758 case CONST_INT:
7759 /* Since this is now an invariant and wasn't before, it must be a giv
7760 with MULT_VAL == 0. It doesn't matter which BIV we associate this
7761 with. */
7762 *src_reg = ivs->list->biv->dest_reg;
7763 *mult_val = const0_rtx;
7764 *add_val = x;
7765 break;
7767 case REG:
7768 /* This is equivalent to a BIV. */
7769 *src_reg = x;
7770 *mult_val = const1_rtx;
7771 *add_val = const0_rtx;
7772 break;
7774 case PLUS:
7775 /* Either (plus (biv) (invar)) or
7776 (plus (mult (biv) (invar_1)) (invar_2)). */
7777 if (GET_CODE (XEXP (x, 0)) == MULT)
7779 *src_reg = XEXP (XEXP (x, 0), 0);
7780 *mult_val = XEXP (XEXP (x, 0), 1);
7782 else
7784 *src_reg = XEXP (x, 0);
7785 *mult_val = const1_rtx;
7787 *add_val = XEXP (x, 1);
7788 break;
7790 case MULT:
7791 /* ADD_VAL is zero. */
7792 *src_reg = XEXP (x, 0);
7793 *mult_val = XEXP (x, 1);
7794 *add_val = const0_rtx;
7795 break;
7797 default:
7798 gcc_unreachable ();
7801 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
7802 unless they are CONST_INT). */
7803 if (GET_CODE (*add_val) == USE)
7804 *add_val = XEXP (*add_val, 0);
7805 if (GET_CODE (*mult_val) == USE)
7806 *mult_val = XEXP (*mult_val, 0);
7808 if (is_addr)
7809 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
7810 else
7811 *pbenefit += rtx_cost (orig_x, SET);
7813 /* Always return true if this is a giv so it will be detected as such,
7814 even if the benefit is zero or negative. This allows elimination
7815 of bivs that might otherwise not be eliminated. */
7816 return 1;
7819 /* Given an expression, X, try to form it as a linear function of a biv.
7820 We will canonicalize it to be of the form
7821 (plus (mult (BIV) (invar_1))
7822 (invar_2))
7823 with possible degeneracies.
7825 The invariant expressions must each be of a form that can be used as a
7826 machine operand. We surround then with a USE rtx (a hack, but localized
7827 and certainly unambiguous!) if not a CONST_INT for simplicity in this
7828 routine; it is the caller's responsibility to strip them.
7830 If no such canonicalization is possible (i.e., two biv's are used or an
7831 expression that is neither invariant nor a biv or giv), this routine
7832 returns 0.
7834 For a nonzero return, the result will have a code of CONST_INT, USE,
7835 REG (for a BIV), PLUS, or MULT. No other codes will occur.
7837 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
7839 static rtx sge_plus (enum machine_mode, rtx, rtx);
7840 static rtx sge_plus_constant (rtx, rtx);
7842 static rtx
7843 simplify_giv_expr (const struct loop *loop, rtx x, rtx *ext_val, int *benefit)
7845 struct loop_ivs *ivs = LOOP_IVS (loop);
7846 struct loop_regs *regs = LOOP_REGS (loop);
7847 enum machine_mode mode = GET_MODE (x);
7848 rtx arg0, arg1;
7849 rtx tem;
7851 /* If this is not an integer mode, or if we cannot do arithmetic in this
7852 mode, this can't be a giv. */
7853 if (mode != VOIDmode
7854 && (GET_MODE_CLASS (mode) != MODE_INT
7855 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
7856 return NULL_RTX;
7858 switch (GET_CODE (x))
7860 case PLUS:
7861 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
7862 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
7863 if (arg0 == 0 || arg1 == 0)
7864 return NULL_RTX;
7866 /* Put constant last, CONST_INT last if both constant. */
7867 if ((GET_CODE (arg0) == USE
7868 || GET_CODE (arg0) == CONST_INT)
7869 && ! ((GET_CODE (arg0) == USE
7870 && GET_CODE (arg1) == USE)
7871 || GET_CODE (arg1) == CONST_INT))
7872 tem = arg0, arg0 = arg1, arg1 = tem;
7874 /* Handle addition of zero, then addition of an invariant. */
7875 if (arg1 == const0_rtx)
7876 return arg0;
7877 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
7878 switch (GET_CODE (arg0))
7880 case CONST_INT:
7881 case USE:
7882 /* Adding two invariants must result in an invariant, so enclose
7883 addition operation inside a USE and return it. */
7884 if (GET_CODE (arg0) == USE)
7885 arg0 = XEXP (arg0, 0);
7886 if (GET_CODE (arg1) == USE)
7887 arg1 = XEXP (arg1, 0);
7889 if (GET_CODE (arg0) == CONST_INT)
7890 tem = arg0, arg0 = arg1, arg1 = tem;
7891 if (GET_CODE (arg1) == CONST_INT)
7892 tem = sge_plus_constant (arg0, arg1);
7893 else
7894 tem = sge_plus (mode, arg0, arg1);
7896 if (GET_CODE (tem) != CONST_INT)
7897 tem = gen_rtx_USE (mode, tem);
7898 return tem;
7900 case REG:
7901 case MULT:
7902 /* biv + invar or mult + invar. Return sum. */
7903 return gen_rtx_PLUS (mode, arg0, arg1);
7905 case PLUS:
7906 /* (a + invar_1) + invar_2. Associate. */
7907 return
7908 simplify_giv_expr (loop,
7909 gen_rtx_PLUS (mode,
7910 XEXP (arg0, 0),
7911 gen_rtx_PLUS (mode,
7912 XEXP (arg0, 1),
7913 arg1)),
7914 ext_val, benefit);
7916 default:
7917 gcc_unreachable ();
7920 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
7921 MULT to reduce cases. */
7922 if (REG_P (arg0))
7923 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
7924 if (REG_P (arg1))
7925 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
7927 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
7928 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
7929 Recurse to associate the second PLUS. */
7930 if (GET_CODE (arg1) == MULT)
7931 tem = arg0, arg0 = arg1, arg1 = tem;
7933 if (GET_CODE (arg1) == PLUS)
7934 return
7935 simplify_giv_expr (loop,
7936 gen_rtx_PLUS (mode,
7937 gen_rtx_PLUS (mode, arg0,
7938 XEXP (arg1, 0)),
7939 XEXP (arg1, 1)),
7940 ext_val, benefit);
7942 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
7943 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
7944 return NULL_RTX;
7946 if (!rtx_equal_p (arg0, arg1))
7947 return NULL_RTX;
7949 return simplify_giv_expr (loop,
7950 gen_rtx_MULT (mode,
7951 XEXP (arg0, 0),
7952 gen_rtx_PLUS (mode,
7953 XEXP (arg0, 1),
7954 XEXP (arg1, 1))),
7955 ext_val, benefit);
7957 case MINUS:
7958 /* Handle "a - b" as "a + b * (-1)". */
7959 return simplify_giv_expr (loop,
7960 gen_rtx_PLUS (mode,
7961 XEXP (x, 0),
7962 gen_rtx_MULT (mode,
7963 XEXP (x, 1),
7964 constm1_rtx)),
7965 ext_val, benefit);
7967 case MULT:
7968 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
7969 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
7970 if (arg0 == 0 || arg1 == 0)
7971 return NULL_RTX;
7973 /* Put constant last, CONST_INT last if both constant. */
7974 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
7975 && GET_CODE (arg1) != CONST_INT)
7976 tem = arg0, arg0 = arg1, arg1 = tem;
7978 /* If second argument is not now constant, not giv. */
7979 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
7980 return NULL_RTX;
7982 /* Handle multiply by 0 or 1. */
7983 if (arg1 == const0_rtx)
7984 return const0_rtx;
7986 else if (arg1 == const1_rtx)
7987 return arg0;
7989 switch (GET_CODE (arg0))
7991 case REG:
7992 /* biv * invar. Done. */
7993 return gen_rtx_MULT (mode, arg0, arg1);
7995 case CONST_INT:
7996 /* Product of two constants. */
7997 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
7999 case USE:
8000 /* invar * invar is a giv, but attempt to simplify it somehow. */
8001 if (GET_CODE (arg1) != CONST_INT)
8002 return NULL_RTX;
8004 arg0 = XEXP (arg0, 0);
8005 if (GET_CODE (arg0) == MULT)
8007 /* (invar_0 * invar_1) * invar_2. Associate. */
8008 return simplify_giv_expr (loop,
8009 gen_rtx_MULT (mode,
8010 XEXP (arg0, 0),
8011 gen_rtx_MULT (mode,
8012 XEXP (arg0,
8014 arg1)),
8015 ext_val, benefit);
8017 /* Propagate the MULT expressions to the innermost nodes. */
8018 else if (GET_CODE (arg0) == PLUS)
8020 /* (invar_0 + invar_1) * invar_2. Distribute. */
8021 return simplify_giv_expr (loop,
8022 gen_rtx_PLUS (mode,
8023 gen_rtx_MULT (mode,
8024 XEXP (arg0,
8026 arg1),
8027 gen_rtx_MULT (mode,
8028 XEXP (arg0,
8030 arg1)),
8031 ext_val, benefit);
8033 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
8035 case MULT:
8036 /* (a * invar_1) * invar_2. Associate. */
8037 return simplify_giv_expr (loop,
8038 gen_rtx_MULT (mode,
8039 XEXP (arg0, 0),
8040 gen_rtx_MULT (mode,
8041 XEXP (arg0, 1),
8042 arg1)),
8043 ext_val, benefit);
8045 case PLUS:
8046 /* (a + invar_1) * invar_2. Distribute. */
8047 return simplify_giv_expr (loop,
8048 gen_rtx_PLUS (mode,
8049 gen_rtx_MULT (mode,
8050 XEXP (arg0, 0),
8051 arg1),
8052 gen_rtx_MULT (mode,
8053 XEXP (arg0, 1),
8054 arg1)),
8055 ext_val, benefit);
8057 default:
8058 gcc_unreachable ();
8061 case ASHIFT:
8062 /* Shift by constant is multiply by power of two. */
8063 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
8064 return 0;
8066 return
8067 simplify_giv_expr (loop,
8068 gen_rtx_MULT (mode,
8069 XEXP (x, 0),
8070 GEN_INT ((HOST_WIDE_INT) 1
8071 << INTVAL (XEXP (x, 1)))),
8072 ext_val, benefit);
8074 case NEG:
8075 /* "-a" is "a * (-1)" */
8076 return simplify_giv_expr (loop,
8077 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
8078 ext_val, benefit);
8080 case NOT:
8081 /* "~a" is "-a - 1". Silly, but easy. */
8082 return simplify_giv_expr (loop,
8083 gen_rtx_MINUS (mode,
8084 gen_rtx_NEG (mode, XEXP (x, 0)),
8085 const1_rtx),
8086 ext_val, benefit);
8088 case USE:
8089 /* Already in proper form for invariant. */
8090 return x;
8092 case SIGN_EXTEND:
8093 case ZERO_EXTEND:
8094 case TRUNCATE:
8095 /* Conditionally recognize extensions of simple IVs. After we've
8096 computed loop traversal counts and verified the range of the
8097 source IV, we'll reevaluate this as a GIV. */
8098 if (*ext_val == NULL_RTX)
8100 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
8101 if (arg0 && *ext_val == NULL_RTX && REG_P (arg0))
8103 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
8104 return arg0;
8107 goto do_default;
8109 case REG:
8110 /* If this is a new register, we can't deal with it. */
8111 if (REGNO (x) >= max_reg_before_loop)
8112 return 0;
8114 /* Check for biv or giv. */
8115 switch (REG_IV_TYPE (ivs, REGNO (x)))
8117 case BASIC_INDUCT:
8118 return x;
8119 case GENERAL_INDUCT:
8121 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
8123 /* Form expression from giv and add benefit. Ensure this giv
8124 can derive another and subtract any needed adjustment if so. */
8126 /* Increasing the benefit here is risky. The only case in which it
8127 is arguably correct is if this is the only use of V. In other
8128 cases, this will artificially inflate the benefit of the current
8129 giv, and lead to suboptimal code. Thus, it is disabled, since
8130 potentially not reducing an only marginally beneficial giv is
8131 less harmful than reducing many givs that are not really
8132 beneficial. */
8134 rtx single_use = regs->array[REGNO (x)].single_usage;
8135 if (single_use && single_use != const0_rtx)
8136 *benefit += v->benefit;
8139 if (v->cant_derive)
8140 return 0;
8142 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
8143 v->src_reg, v->mult_val),
8144 v->add_val);
8146 if (v->derive_adjustment)
8147 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
8148 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
8149 if (*ext_val)
8151 if (!v->ext_dependent)
8152 return arg0;
8154 else
8156 *ext_val = v->ext_dependent;
8157 return arg0;
8159 return 0;
8162 default:
8163 do_default:
8164 /* If it isn't an induction variable, and it is invariant, we
8165 may be able to simplify things further by looking through
8166 the bits we just moved outside the loop. */
8167 if (loop_invariant_p (loop, x) == 1)
8169 struct movable *m;
8170 struct loop_movables *movables = LOOP_MOVABLES (loop);
8172 for (m = movables->head; m; m = m->next)
8173 if (rtx_equal_p (x, m->set_dest))
8175 /* Ok, we found a match. Substitute and simplify. */
8177 /* If we match another movable, we must use that, as
8178 this one is going away. */
8179 if (m->match)
8180 return simplify_giv_expr (loop, m->match->set_dest,
8181 ext_val, benefit);
8183 /* If consec is nonzero, this is a member of a group of
8184 instructions that were moved together. We handle this
8185 case only to the point of seeking to the last insn and
8186 looking for a REG_EQUAL. Fail if we don't find one. */
8187 if (m->consec != 0)
8189 int i = m->consec;
8190 tem = m->insn;
8193 tem = NEXT_INSN (tem);
8195 while (--i > 0);
8197 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
8198 if (tem)
8199 tem = XEXP (tem, 0);
8201 else
8203 tem = single_set (m->insn);
8204 if (tem)
8205 tem = SET_SRC (tem);
8208 if (tem)
8210 /* What we are most interested in is pointer
8211 arithmetic on invariants -- only take
8212 patterns we may be able to do something with. */
8213 if (GET_CODE (tem) == PLUS
8214 || GET_CODE (tem) == MULT
8215 || GET_CODE (tem) == ASHIFT
8216 || GET_CODE (tem) == CONST_INT
8217 || GET_CODE (tem) == SYMBOL_REF)
8219 tem = simplify_giv_expr (loop, tem, ext_val,
8220 benefit);
8221 if (tem)
8222 return tem;
8224 else if (GET_CODE (tem) == CONST
8225 && GET_CODE (XEXP (tem, 0)) == PLUS
8226 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
8227 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
8229 tem = simplify_giv_expr (loop, XEXP (tem, 0),
8230 ext_val, benefit);
8231 if (tem)
8232 return tem;
8235 break;
8238 break;
8241 /* Fall through to general case. */
8242 default:
8243 /* If invariant, return as USE (unless CONST_INT).
8244 Otherwise, not giv. */
8245 if (GET_CODE (x) == USE)
8246 x = XEXP (x, 0);
8248 if (loop_invariant_p (loop, x) == 1)
8250 if (GET_CODE (x) == CONST_INT)
8251 return x;
8252 if (GET_CODE (x) == CONST
8253 && GET_CODE (XEXP (x, 0)) == PLUS
8254 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
8255 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
8256 x = XEXP (x, 0);
8257 return gen_rtx_USE (mode, x);
8259 else
8260 return 0;
8264 /* This routine folds invariants such that there is only ever one
8265 CONST_INT in the summation. It is only used by simplify_giv_expr. */
8267 static rtx
8268 sge_plus_constant (rtx x, rtx c)
8270 if (GET_CODE (x) == CONST_INT)
8271 return GEN_INT (INTVAL (x) + INTVAL (c));
8272 else if (GET_CODE (x) != PLUS)
8273 return gen_rtx_PLUS (GET_MODE (x), x, c);
8274 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8276 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
8277 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
8279 else if (GET_CODE (XEXP (x, 0)) == PLUS
8280 || GET_CODE (XEXP (x, 1)) != PLUS)
8282 return gen_rtx_PLUS (GET_MODE (x),
8283 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
8285 else
8287 return gen_rtx_PLUS (GET_MODE (x),
8288 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
8292 static rtx
8293 sge_plus (enum machine_mode mode, rtx x, rtx y)
8295 while (GET_CODE (y) == PLUS)
8297 rtx a = XEXP (y, 0);
8298 if (GET_CODE (a) == CONST_INT)
8299 x = sge_plus_constant (x, a);
8300 else
8301 x = gen_rtx_PLUS (mode, x, a);
8302 y = XEXP (y, 1);
8304 if (GET_CODE (y) == CONST_INT)
8305 x = sge_plus_constant (x, y);
8306 else
8307 x = gen_rtx_PLUS (mode, x, y);
8308 return x;
8311 /* Help detect a giv that is calculated by several consecutive insns;
8312 for example,
8313 giv = biv * M
8314 giv = giv + A
8315 The caller has already identified the first insn P as having a giv as dest;
8316 we check that all other insns that set the same register follow
8317 immediately after P, that they alter nothing else,
8318 and that the result of the last is still a giv.
8320 The value is 0 if the reg set in P is not really a giv.
8321 Otherwise, the value is the amount gained by eliminating
8322 all the consecutive insns that compute the value.
8324 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
8325 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
8327 The coefficients of the ultimate giv value are stored in
8328 *MULT_VAL and *ADD_VAL. */
8330 static int
8331 consec_sets_giv (const struct loop *loop, int first_benefit, rtx p,
8332 rtx src_reg, rtx dest_reg, rtx *add_val, rtx *mult_val,
8333 rtx *ext_val, rtx *last_consec_insn)
8335 struct loop_ivs *ivs = LOOP_IVS (loop);
8336 struct loop_regs *regs = LOOP_REGS (loop);
8337 int count;
8338 enum rtx_code code;
8339 int benefit;
8340 rtx temp;
8341 rtx set;
8343 /* Indicate that this is a giv so that we can update the value produced in
8344 each insn of the multi-insn sequence.
8346 This induction structure will be used only by the call to
8347 general_induction_var below, so we can allocate it on our stack.
8348 If this is a giv, our caller will replace the induct var entry with
8349 a new induction structure. */
8350 struct induction *v;
8352 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
8353 return 0;
8355 v = alloca (sizeof (struct induction));
8356 v->src_reg = src_reg;
8357 v->mult_val = *mult_val;
8358 v->add_val = *add_val;
8359 v->benefit = first_benefit;
8360 v->cant_derive = 0;
8361 v->derive_adjustment = 0;
8362 v->ext_dependent = NULL_RTX;
8364 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
8365 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
8367 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
8369 while (count > 0)
8371 p = NEXT_INSN (p);
8372 code = GET_CODE (p);
8374 /* If libcall, skip to end of call sequence. */
8375 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
8376 p = XEXP (temp, 0);
8378 if (code == INSN
8379 && (set = single_set (p))
8380 && REG_P (SET_DEST (set))
8381 && SET_DEST (set) == dest_reg
8382 && (general_induction_var (loop, SET_SRC (set), &src_reg,
8383 add_val, mult_val, ext_val, 0,
8384 &benefit, VOIDmode)
8385 /* Giv created by equivalent expression. */
8386 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
8387 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
8388 add_val, mult_val, ext_val, 0,
8389 &benefit, VOIDmode)))
8390 && src_reg == v->src_reg)
8392 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
8393 benefit += libcall_benefit (p);
8395 count--;
8396 v->mult_val = *mult_val;
8397 v->add_val = *add_val;
8398 v->benefit += benefit;
8400 else if (code != NOTE)
8402 /* Allow insns that set something other than this giv to a
8403 constant. Such insns are needed on machines which cannot
8404 include long constants and should not disqualify a giv. */
8405 if (code == INSN
8406 && (set = single_set (p))
8407 && SET_DEST (set) != dest_reg
8408 && CONSTANT_P (SET_SRC (set)))
8409 continue;
8411 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
8412 return 0;
8416 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
8417 *last_consec_insn = p;
8418 return v->benefit;
8421 /* Return an rtx, if any, that expresses giv G2 as a function of the register
8422 represented by G1. If no such expression can be found, or it is clear that
8423 it cannot possibly be a valid address, 0 is returned.
8425 To perform the computation, we note that
8426 G1 = x * v + a and
8427 G2 = y * v + b
8428 where `v' is the biv.
8430 So G2 = (y/b) * G1 + (b - a*y/x).
8432 Note that MULT = y/x.
8434 Update: A and B are now allowed to be additive expressions such that
8435 B contains all variables in A. That is, computing B-A will not require
8436 subtracting variables. */
8438 static rtx
8439 express_from_1 (rtx a, rtx b, rtx mult)
8441 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
8443 if (mult == const0_rtx)
8444 return b;
8446 /* If MULT is not 1, we cannot handle A with non-constants, since we
8447 would then be required to subtract multiples of the registers in A.
8448 This is theoretically possible, and may even apply to some Fortran
8449 constructs, but it is a lot of work and we do not attempt it here. */
8451 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
8452 return NULL_RTX;
8454 /* In general these structures are sorted top to bottom (down the PLUS
8455 chain), but not left to right across the PLUS. If B is a higher
8456 order giv than A, we can strip one level and recurse. If A is higher
8457 order, we'll eventually bail out, but won't know that until the end.
8458 If they are the same, we'll strip one level around this loop. */
8460 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
8462 rtx ra, rb, oa, ob, tmp;
8464 ra = XEXP (a, 0), oa = XEXP (a, 1);
8465 if (GET_CODE (ra) == PLUS)
8466 tmp = ra, ra = oa, oa = tmp;
8468 rb = XEXP (b, 0), ob = XEXP (b, 1);
8469 if (GET_CODE (rb) == PLUS)
8470 tmp = rb, rb = ob, ob = tmp;
8472 if (rtx_equal_p (ra, rb))
8473 /* We matched: remove one reg completely. */
8474 a = oa, b = ob;
8475 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
8476 /* An alternate match. */
8477 a = oa, b = rb;
8478 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
8479 /* An alternate match. */
8480 a = ra, b = ob;
8481 else
8483 /* Indicates an extra register in B. Strip one level from B and
8484 recurse, hoping B was the higher order expression. */
8485 ob = express_from_1 (a, ob, mult);
8486 if (ob == NULL_RTX)
8487 return NULL_RTX;
8488 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
8492 /* Here we are at the last level of A, go through the cases hoping to
8493 get rid of everything but a constant. */
8495 if (GET_CODE (a) == PLUS)
8497 rtx ra, oa;
8499 ra = XEXP (a, 0), oa = XEXP (a, 1);
8500 if (rtx_equal_p (oa, b))
8501 oa = ra;
8502 else if (!rtx_equal_p (ra, b))
8503 return NULL_RTX;
8505 if (GET_CODE (oa) != CONST_INT)
8506 return NULL_RTX;
8508 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
8510 else if (GET_CODE (a) == CONST_INT)
8512 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
8514 else if (CONSTANT_P (a))
8516 enum machine_mode mode_a = GET_MODE (a);
8517 enum machine_mode mode_b = GET_MODE (b);
8518 enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
8519 return simplify_gen_binary (MINUS, mode, b, a);
8521 else if (GET_CODE (b) == PLUS)
8523 if (rtx_equal_p (a, XEXP (b, 0)))
8524 return XEXP (b, 1);
8525 else if (rtx_equal_p (a, XEXP (b, 1)))
8526 return XEXP (b, 0);
8527 else
8528 return NULL_RTX;
8530 else if (rtx_equal_p (a, b))
8531 return const0_rtx;
8533 return NULL_RTX;
8536 static rtx
8537 express_from (struct induction *g1, struct induction *g2)
8539 rtx mult, add;
8541 /* The value that G1 will be multiplied by must be a constant integer. Also,
8542 the only chance we have of getting a valid address is if b*c/a (see above
8543 for notation) is also an integer. */
8544 if (GET_CODE (g1->mult_val) == CONST_INT
8545 && GET_CODE (g2->mult_val) == CONST_INT)
8547 if (g1->mult_val == const0_rtx
8548 || (g1->mult_val == constm1_rtx
8549 && INTVAL (g2->mult_val)
8550 == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
8551 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
8552 return NULL_RTX;
8553 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
8555 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
8556 mult = const1_rtx;
8557 else
8559 /* ??? Find out if the one is a multiple of the other? */
8560 return NULL_RTX;
8563 add = express_from_1 (g1->add_val, g2->add_val, mult);
8564 if (add == NULL_RTX)
8566 /* Failed. If we've got a multiplication factor between G1 and G2,
8567 scale G1's addend and try again. */
8568 if (INTVAL (mult) > 1)
8570 rtx g1_add_val = g1->add_val;
8571 if (GET_CODE (g1_add_val) == MULT
8572 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
8574 HOST_WIDE_INT m;
8575 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
8576 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
8577 XEXP (g1_add_val, 0), GEN_INT (m));
8579 else
8581 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
8582 mult);
8585 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
8588 if (add == NULL_RTX)
8589 return NULL_RTX;
8591 /* Form simplified final result. */
8592 if (mult == const0_rtx)
8593 return add;
8594 else if (mult == const1_rtx)
8595 mult = g1->dest_reg;
8596 else
8597 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
8599 if (add == const0_rtx)
8600 return mult;
8601 else
8603 if (GET_CODE (add) == PLUS
8604 && CONSTANT_P (XEXP (add, 1)))
8606 rtx tem = XEXP (add, 1);
8607 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
8608 add = tem;
8611 return gen_rtx_PLUS (g2->mode, mult, add);
8615 /* Return an rtx, if any, that expresses giv G2 as a function of the register
8616 represented by G1. This indicates that G2 should be combined with G1 and
8617 that G2 can use (either directly or via an address expression) a register
8618 used to represent G1. */
8620 static rtx
8621 combine_givs_p (struct induction *g1, struct induction *g2)
8623 rtx comb, ret;
8625 /* With the introduction of ext dependent givs, we must care for modes.
8626 G2 must not use a wider mode than G1. */
8627 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
8628 return NULL_RTX;
8630 ret = comb = express_from (g1, g2);
8631 if (comb == NULL_RTX)
8632 return NULL_RTX;
8633 if (g1->mode != g2->mode)
8634 ret = gen_lowpart (g2->mode, comb);
8636 /* If these givs are identical, they can be combined. We use the results
8637 of express_from because the addends are not in a canonical form, so
8638 rtx_equal_p is a weaker test. */
8639 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
8640 combination to be the other way round. */
8641 if (comb == g1->dest_reg
8642 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
8644 return ret;
8647 /* If G2 can be expressed as a function of G1 and that function is valid
8648 as an address and no more expensive than using a register for G2,
8649 the expression of G2 in terms of G1 can be used. */
8650 if (ret != NULL_RTX
8651 && g2->giv_type == DEST_ADDR
8652 && memory_address_p (GET_MODE (g2->mem), ret))
8653 return ret;
8655 return NULL_RTX;
8658 /* See if BL is monotonic and has a constant per-iteration increment.
8659 Return the increment if so, otherwise return 0. */
8661 static HOST_WIDE_INT
8662 get_monotonic_increment (struct iv_class *bl)
8664 struct induction *v;
8665 rtx incr;
8667 /* Get the total increment and check that it is constant. */
8668 incr = biv_total_increment (bl);
8669 if (incr == 0 || GET_CODE (incr) != CONST_INT)
8670 return 0;
8672 for (v = bl->biv; v != 0; v = v->next_iv)
8674 if (GET_CODE (v->add_val) != CONST_INT)
8675 return 0;
8677 if (INTVAL (v->add_val) < 0 && INTVAL (incr) >= 0)
8678 return 0;
8680 if (INTVAL (v->add_val) > 0 && INTVAL (incr) <= 0)
8681 return 0;
8683 return INTVAL (incr);
8687 /* Subroutine of biv_fits_mode_p. Return true if biv BL, when biased by
8688 BIAS, will never exceed the unsigned range of MODE. LOOP is the loop
8689 to which the biv belongs and INCR is its per-iteration increment. */
8691 static bool
8692 biased_biv_fits_mode_p (const struct loop *loop, struct iv_class *bl,
8693 HOST_WIDE_INT incr, enum machine_mode mode,
8694 unsigned HOST_WIDE_INT bias)
8696 unsigned HOST_WIDE_INT initial, maximum, span, delta;
8698 /* We need to be able to manipulate MODE-size constants. */
8699 if (HOST_BITS_PER_WIDE_INT < GET_MODE_BITSIZE (mode))
8700 return false;
8702 /* The number of loop iterations must be constant. */
8703 if (LOOP_INFO (loop)->n_iterations == 0)
8704 return false;
8706 /* So must the biv's initial value. */
8707 if (bl->initial_value == 0 || GET_CODE (bl->initial_value) != CONST_INT)
8708 return false;
8710 initial = bias + INTVAL (bl->initial_value);
8711 maximum = GET_MODE_MASK (mode);
8713 /* Make sure that the initial value is within range. */
8714 if (initial > maximum)
8715 return false;
8717 /* Set up DELTA and SPAN such that the number of iterations * DELTA
8718 (calculated to arbitrary precision) must be <= SPAN. */
8719 if (incr < 0)
8721 delta = -incr;
8722 span = initial;
8724 else
8726 delta = incr;
8727 /* Handle the special case in which MAXIMUM is the largest
8728 unsigned HOST_WIDE_INT and INITIAL is 0. */
8729 if (maximum + 1 == initial)
8730 span = LOOP_INFO (loop)->n_iterations * delta;
8731 else
8732 span = maximum + 1 - initial;
8734 return (span / LOOP_INFO (loop)->n_iterations >= delta);
8738 /* Return true if biv BL will never exceed the bounds of MODE. LOOP is
8739 the loop to which BL belongs and INCR is its per-iteration increment.
8740 UNSIGNEDP is true if the biv should be treated as unsigned. */
8742 static bool
8743 biv_fits_mode_p (const struct loop *loop, struct iv_class *bl,
8744 HOST_WIDE_INT incr, enum machine_mode mode, bool unsignedp)
8746 struct loop_info *loop_info;
8747 unsigned HOST_WIDE_INT bias;
8749 /* A biv's value will always be limited to its natural mode.
8750 Larger modes will observe the same wrap-around. */
8751 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (bl->biv->src_reg)))
8752 mode = GET_MODE (bl->biv->src_reg);
8754 loop_info = LOOP_INFO (loop);
8756 bias = (unsignedp ? 0 : (GET_MODE_MASK (mode) >> 1) + 1);
8757 if (biased_biv_fits_mode_p (loop, bl, incr, mode, bias))
8758 return true;
8760 if (mode == GET_MODE (bl->biv->src_reg)
8761 && bl->biv->src_reg == loop_info->iteration_var
8762 && loop_info->comparison_value
8763 && loop_invariant_p (loop, loop_info->comparison_value))
8765 /* If the increment is +1, and the exit test is a <, the BIV
8766 cannot overflow. (For <=, we have the problematic case that
8767 the comparison value might be the maximum value of the range.) */
8768 if (incr == 1)
8770 if (loop_info->comparison_code == LT)
8771 return true;
8772 if (loop_info->comparison_code == LTU && unsignedp)
8773 return true;
8776 /* Likewise for increment -1 and exit test >. */
8777 if (incr == -1)
8779 if (loop_info->comparison_code == GT)
8780 return true;
8781 if (loop_info->comparison_code == GTU && unsignedp)
8782 return true;
8785 return false;
8789 /* Given that X is an extension or truncation of BL, return true
8790 if it is unaffected by overflow. LOOP is the loop to which
8791 BL belongs and INCR is its per-iteration increment. */
8793 static bool
8794 extension_within_bounds_p (const struct loop *loop, struct iv_class *bl,
8795 HOST_WIDE_INT incr, rtx x)
8797 enum machine_mode mode;
8798 bool signedp, unsignedp;
8800 switch (GET_CODE (x))
8802 case SIGN_EXTEND:
8803 case ZERO_EXTEND:
8804 mode = GET_MODE (XEXP (x, 0));
8805 signedp = (GET_CODE (x) == SIGN_EXTEND);
8806 unsignedp = (GET_CODE (x) == ZERO_EXTEND);
8807 break;
8809 case TRUNCATE:
8810 /* We don't know whether this value is being used as signed
8811 or unsigned, so check the conditions for both. */
8812 mode = GET_MODE (x);
8813 signedp = unsignedp = true;
8814 break;
8816 default:
8817 gcc_unreachable ();
8820 return ((!signedp || biv_fits_mode_p (loop, bl, incr, mode, false))
8821 && (!unsignedp || biv_fits_mode_p (loop, bl, incr, mode, true)));
8825 /* Check each extension dependent giv in this class to see if its
8826 root biv is safe from wrapping in the interior mode, which would
8827 make the giv illegal. */
8829 static void
8830 check_ext_dependent_givs (const struct loop *loop, struct iv_class *bl)
8832 struct induction *v;
8833 HOST_WIDE_INT incr;
8835 incr = get_monotonic_increment (bl);
8837 /* Invalidate givs that fail the tests. */
8838 for (v = bl->giv; v; v = v->next_iv)
8839 if (v->ext_dependent)
8841 if (incr != 0
8842 && extension_within_bounds_p (loop, bl, incr, v->ext_dependent))
8844 if (loop_dump_stream)
8845 fprintf (loop_dump_stream,
8846 "Verified ext dependent giv at %d of reg %d\n",
8847 INSN_UID (v->insn), bl->regno);
8849 else
8851 if (loop_dump_stream)
8852 fprintf (loop_dump_stream,
8853 "Failed ext dependent giv at %d\n",
8854 INSN_UID (v->insn));
8856 v->ignore = 1;
8857 bl->all_reduced = 0;
8862 /* Generate a version of VALUE in a mode appropriate for initializing V. */
8864 static rtx
8865 extend_value_for_giv (struct induction *v, rtx value)
8867 rtx ext_dep = v->ext_dependent;
8869 if (! ext_dep)
8870 return value;
8872 /* Recall that check_ext_dependent_givs verified that the known bounds
8873 of a biv did not overflow or wrap with respect to the extension for
8874 the giv. Therefore, constants need no additional adjustment. */
8875 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
8876 return value;
8878 /* Otherwise, we must adjust the value to compensate for the
8879 differing modes of the biv and the giv. */
8880 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
8883 struct combine_givs_stats
8885 int giv_number;
8886 int total_benefit;
8889 static int
8890 cmp_combine_givs_stats (const void *xp, const void *yp)
8892 const struct combine_givs_stats * const x =
8893 (const struct combine_givs_stats *) xp;
8894 const struct combine_givs_stats * const y =
8895 (const struct combine_givs_stats *) yp;
8896 int d;
8897 d = y->total_benefit - x->total_benefit;
8898 /* Stabilize the sort. */
8899 if (!d)
8900 d = x->giv_number - y->giv_number;
8901 return d;
8904 /* Check all pairs of givs for iv_class BL and see if any can be combined with
8905 any other. If so, point SAME to the giv combined with and set NEW_REG to
8906 be an expression (in terms of the other giv's DEST_REG) equivalent to the
8907 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
8909 static void
8910 combine_givs (struct loop_regs *regs, struct iv_class *bl)
8912 /* Additional benefit to add for being combined multiple times. */
8913 const int extra_benefit = 3;
8915 struct induction *g1, *g2, **giv_array;
8916 int i, j, k, giv_count;
8917 struct combine_givs_stats *stats;
8918 rtx *can_combine;
8920 /* Count givs, because bl->giv_count is incorrect here. */
8921 giv_count = 0;
8922 for (g1 = bl->giv; g1; g1 = g1->next_iv)
8923 if (!g1->ignore)
8924 giv_count++;
8926 giv_array = alloca (giv_count * sizeof (struct induction *));
8927 i = 0;
8928 for (g1 = bl->giv; g1; g1 = g1->next_iv)
8929 if (!g1->ignore)
8930 giv_array[i++] = g1;
8932 stats = xcalloc (giv_count, sizeof (*stats));
8933 can_combine = xcalloc (giv_count, giv_count * sizeof (rtx));
8935 for (i = 0; i < giv_count; i++)
8937 int this_benefit;
8938 rtx single_use;
8940 g1 = giv_array[i];
8941 stats[i].giv_number = i;
8943 /* If a DEST_REG GIV is used only once, do not allow it to combine
8944 with anything, for in doing so we will gain nothing that cannot
8945 be had by simply letting the GIV with which we would have combined
8946 to be reduced on its own. The lossage shows up in particular with
8947 DEST_ADDR targets on hosts with reg+reg addressing, though it can
8948 be seen elsewhere as well. */
8949 if (g1->giv_type == DEST_REG
8950 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
8951 && single_use != const0_rtx)
8952 continue;
8954 this_benefit = g1->benefit;
8955 /* Add an additional weight for zero addends. */
8956 if (g1->no_const_addval)
8957 this_benefit += 1;
8959 for (j = 0; j < giv_count; j++)
8961 rtx this_combine;
8963 g2 = giv_array[j];
8964 if (g1 != g2
8965 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
8967 can_combine[i * giv_count + j] = this_combine;
8968 this_benefit += g2->benefit + extra_benefit;
8971 stats[i].total_benefit = this_benefit;
8974 /* Iterate, combining until we can't. */
8975 restart:
8976 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
8978 if (loop_dump_stream)
8980 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
8981 for (k = 0; k < giv_count; k++)
8983 g1 = giv_array[stats[k].giv_number];
8984 if (!g1->combined_with && !g1->same)
8985 fprintf (loop_dump_stream, " {%d, %d}",
8986 INSN_UID (giv_array[stats[k].giv_number]->insn),
8987 stats[k].total_benefit);
8989 putc ('\n', loop_dump_stream);
8992 for (k = 0; k < giv_count; k++)
8994 int g1_add_benefit = 0;
8996 i = stats[k].giv_number;
8997 g1 = giv_array[i];
8999 /* If it has already been combined, skip. */
9000 if (g1->combined_with || g1->same)
9001 continue;
9003 for (j = 0; j < giv_count; j++)
9005 g2 = giv_array[j];
9006 if (g1 != g2 && can_combine[i * giv_count + j]
9007 /* If it has already been combined, skip. */
9008 && ! g2->same && ! g2->combined_with)
9010 int l;
9012 g2->new_reg = can_combine[i * giv_count + j];
9013 g2->same = g1;
9014 /* For destination, we now may replace by mem expression instead
9015 of register. This changes the costs considerably, so add the
9016 compensation. */
9017 if (g2->giv_type == DEST_ADDR)
9018 g2->benefit = (g2->benefit + reg_address_cost
9019 - address_cost (g2->new_reg,
9020 GET_MODE (g2->mem)));
9021 g1->combined_with++;
9022 g1->lifetime += g2->lifetime;
9024 g1_add_benefit += g2->benefit;
9026 /* ??? The new final_[bg]iv_value code does a much better job
9027 of finding replaceable giv's, and hence this code may no
9028 longer be necessary. */
9029 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
9030 g1_add_benefit -= copy_cost;
9032 /* To help optimize the next set of combinations, remove
9033 this giv from the benefits of other potential mates. */
9034 for (l = 0; l < giv_count; ++l)
9036 int m = stats[l].giv_number;
9037 if (can_combine[m * giv_count + j])
9038 stats[l].total_benefit -= g2->benefit + extra_benefit;
9041 if (loop_dump_stream)
9042 fprintf (loop_dump_stream,
9043 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
9044 INSN_UID (g2->insn), INSN_UID (g1->insn),
9045 g1->benefit, g1_add_benefit, g1->lifetime);
9049 /* To help optimize the next set of combinations, remove
9050 this giv from the benefits of other potential mates. */
9051 if (g1->combined_with)
9053 for (j = 0; j < giv_count; ++j)
9055 int m = stats[j].giv_number;
9056 if (can_combine[m * giv_count + i])
9057 stats[j].total_benefit -= g1->benefit + extra_benefit;
9060 g1->benefit += g1_add_benefit;
9062 /* We've finished with this giv, and everything it touched.
9063 Restart the combination so that proper weights for the
9064 rest of the givs are properly taken into account. */
9065 /* ??? Ideally we would compact the arrays at this point, so
9066 as to not cover old ground. But sanely compacting
9067 can_combine is tricky. */
9068 goto restart;
9072 /* Clean up. */
9073 free (stats);
9074 free (can_combine);
9077 /* Generate sequence for REG = B * M + A. B is the initial value of
9078 the basic induction variable, M a multiplicative constant, A an
9079 additive constant and REG the destination register. */
9081 static rtx
9082 gen_add_mult (rtx b, rtx m, rtx a, rtx reg)
9084 rtx seq;
9085 rtx result;
9087 start_sequence ();
9088 /* Use unsigned arithmetic. */
9089 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
9090 if (reg != result)
9091 emit_move_insn (reg, result);
9092 seq = get_insns ();
9093 end_sequence ();
9095 return seq;
9099 /* Update registers created in insn sequence SEQ. */
9101 static void
9102 loop_regs_update (const struct loop *loop ATTRIBUTE_UNUSED, rtx seq)
9104 rtx insn;
9106 /* Update register info for alias analysis. */
9108 insn = seq;
9109 while (insn != NULL_RTX)
9111 rtx set = single_set (insn);
9113 if (set && REG_P (SET_DEST (set)))
9114 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
9116 insn = NEXT_INSN (insn);
9121 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. B
9122 is the initial value of the basic induction variable, M a
9123 multiplicative constant, A an additive constant and REG the
9124 destination register. */
9126 static void
9127 loop_iv_add_mult_emit_before (const struct loop *loop, rtx b, rtx m, rtx a,
9128 rtx reg, basic_block before_bb, rtx before_insn)
9130 rtx seq;
9132 if (! before_insn)
9134 loop_iv_add_mult_hoist (loop, b, m, a, reg);
9135 return;
9138 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9139 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
9141 /* Increase the lifetime of any invariants moved further in code. */
9142 update_reg_last_use (a, before_insn);
9143 update_reg_last_use (b, before_insn);
9144 update_reg_last_use (m, before_insn);
9146 /* It is possible that the expansion created lots of new registers.
9147 Iterate over the sequence we just created and record them all. We
9148 must do this before inserting the sequence. */
9149 loop_regs_update (loop, seq);
9151 loop_insn_emit_before (loop, before_bb, before_insn, seq);
9155 /* Emit insns in loop pre-header to set REG = B * M + A. B is the
9156 initial value of the basic induction variable, M a multiplicative
9157 constant, A an additive constant and REG the destination
9158 register. */
9160 static void
9161 loop_iv_add_mult_sink (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
9163 rtx seq;
9165 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9166 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
9168 /* Increase the lifetime of any invariants moved further in code.
9169 ???? Is this really necessary? */
9170 update_reg_last_use (a, loop->sink);
9171 update_reg_last_use (b, loop->sink);
9172 update_reg_last_use (m, loop->sink);
9174 /* It is possible that the expansion created lots of new registers.
9175 Iterate over the sequence we just created and record them all. We
9176 must do this before inserting the sequence. */
9177 loop_regs_update (loop, seq);
9179 loop_insn_sink (loop, seq);
9183 /* Emit insns after loop to set REG = B * M + A. B is the initial
9184 value of the basic induction variable, M a multiplicative constant,
9185 A an additive constant and REG the destination register. */
9187 static void
9188 loop_iv_add_mult_hoist (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
9190 rtx seq;
9192 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9193 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
9195 /* It is possible that the expansion created lots of new registers.
9196 Iterate over the sequence we just created and record them all. We
9197 must do this before inserting the sequence. */
9198 loop_regs_update (loop, seq);
9200 loop_insn_hoist (loop, seq);
9205 /* Similar to gen_add_mult, but compute cost rather than generating
9206 sequence. */
9208 static int
9209 iv_add_mult_cost (rtx b, rtx m, rtx a, rtx reg)
9211 int cost = 0;
9212 rtx last, result;
9214 start_sequence ();
9215 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
9216 if (reg != result)
9217 emit_move_insn (reg, result);
9218 last = get_last_insn ();
9219 while (last)
9221 rtx t = single_set (last);
9222 if (t)
9223 cost += rtx_cost (SET_SRC (t), SET);
9224 last = PREV_INSN (last);
9226 end_sequence ();
9227 return cost;
9230 /* Test whether A * B can be computed without
9231 an actual multiply insn. Value is 1 if so.
9233 ??? This function stinks because it generates a ton of wasted RTL
9234 ??? and as a result fragments GC memory to no end. There are other
9235 ??? places in the compiler which are invoked a lot and do the same
9236 ??? thing, generate wasted RTL just to see if something is possible. */
9238 static int
9239 product_cheap_p (rtx a, rtx b)
9241 rtx tmp;
9242 int win, n_insns;
9244 /* If only one is constant, make it B. */
9245 if (GET_CODE (a) == CONST_INT)
9246 tmp = a, a = b, b = tmp;
9248 /* If first constant, both constant, so don't need multiply. */
9249 if (GET_CODE (a) == CONST_INT)
9250 return 1;
9252 /* If second not constant, neither is constant, so would need multiply. */
9253 if (GET_CODE (b) != CONST_INT)
9254 return 0;
9256 /* One operand is constant, so might not need multiply insn. Generate the
9257 code for the multiply and see if a call or multiply, or long sequence
9258 of insns is generated. */
9260 start_sequence ();
9261 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
9262 tmp = get_insns ();
9263 end_sequence ();
9265 win = 1;
9266 if (tmp == NULL_RTX)
9268 else if (INSN_P (tmp))
9270 n_insns = 0;
9271 while (tmp != NULL_RTX)
9273 rtx next = NEXT_INSN (tmp);
9275 if (++n_insns > 3
9276 || !NONJUMP_INSN_P (tmp)
9277 || (GET_CODE (PATTERN (tmp)) == SET
9278 && GET_CODE (SET_SRC (PATTERN (tmp))) == MULT)
9279 || (GET_CODE (PATTERN (tmp)) == PARALLEL
9280 && GET_CODE (XVECEXP (PATTERN (tmp), 0, 0)) == SET
9281 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp), 0, 0))) == MULT))
9283 win = 0;
9284 break;
9287 tmp = next;
9290 else if (GET_CODE (tmp) == SET
9291 && GET_CODE (SET_SRC (tmp)) == MULT)
9292 win = 0;
9293 else if (GET_CODE (tmp) == PARALLEL
9294 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
9295 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
9296 win = 0;
9298 return win;
9301 /* Check to see if loop can be terminated by a "decrement and branch until
9302 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
9303 Also try reversing an increment loop to a decrement loop
9304 to see if the optimization can be performed.
9305 Value is nonzero if optimization was performed. */
9307 /* This is useful even if the architecture doesn't have such an insn,
9308 because it might change a loops which increments from 0 to n to a loop
9309 which decrements from n to 0. A loop that decrements to zero is usually
9310 faster than one that increments from zero. */
9312 /* ??? This could be rewritten to use some of the loop unrolling procedures,
9313 such as approx_final_value, biv_total_increment, loop_iterations, and
9314 final_[bg]iv_value. */
9316 static int
9317 check_dbra_loop (struct loop *loop, int insn_count)
9319 struct loop_info *loop_info = LOOP_INFO (loop);
9320 struct loop_regs *regs = LOOP_REGS (loop);
9321 struct loop_ivs *ivs = LOOP_IVS (loop);
9322 struct iv_class *bl;
9323 rtx reg;
9324 enum machine_mode mode;
9325 rtx jump_label;
9326 rtx final_value;
9327 rtx start_value;
9328 rtx new_add_val;
9329 rtx comparison;
9330 rtx before_comparison;
9331 rtx p;
9332 rtx jump;
9333 rtx first_compare;
9334 int compare_and_branch;
9335 rtx loop_start = loop->start;
9336 rtx loop_end = loop->end;
9338 /* If last insn is a conditional branch, and the insn before tests a
9339 register value, try to optimize it. Otherwise, we can't do anything. */
9341 jump = PREV_INSN (loop_end);
9342 comparison = get_condition_for_loop (loop, jump);
9343 if (comparison == 0)
9344 return 0;
9345 if (!onlyjump_p (jump))
9346 return 0;
9348 /* Try to compute whether the compare/branch at the loop end is one or
9349 two instructions. */
9350 get_condition (jump, &first_compare, false, true);
9351 if (first_compare == jump)
9352 compare_and_branch = 1;
9353 else if (first_compare == prev_nonnote_insn (jump))
9354 compare_and_branch = 2;
9355 else
9356 return 0;
9359 /* If more than one condition is present to control the loop, then
9360 do not proceed, as this function does not know how to rewrite
9361 loop tests with more than one condition.
9363 Look backwards from the first insn in the last comparison
9364 sequence and see if we've got another comparison sequence. */
9366 rtx jump1;
9367 if ((jump1 = prev_nonnote_insn (first_compare))
9368 && JUMP_P (jump1))
9369 return 0;
9372 /* Check all of the bivs to see if the compare uses one of them.
9373 Skip biv's set more than once because we can't guarantee that
9374 it will be zero on the last iteration. Also skip if the biv is
9375 used between its update and the test insn. */
9377 for (bl = ivs->list; bl; bl = bl->next)
9379 if (bl->biv_count == 1
9380 && ! bl->biv->maybe_multiple
9381 && bl->biv->dest_reg == XEXP (comparison, 0)
9382 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
9383 first_compare))
9384 break;
9387 /* Try swapping the comparison to identify a suitable biv. */
9388 if (!bl)
9389 for (bl = ivs->list; bl; bl = bl->next)
9390 if (bl->biv_count == 1
9391 && ! bl->biv->maybe_multiple
9392 && bl->biv->dest_reg == XEXP (comparison, 1)
9393 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
9394 first_compare))
9396 comparison = gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)),
9397 VOIDmode,
9398 XEXP (comparison, 1),
9399 XEXP (comparison, 0));
9400 break;
9403 if (! bl)
9404 return 0;
9406 /* Look for the case where the basic induction variable is always
9407 nonnegative, and equals zero on the last iteration.
9408 In this case, add a reg_note REG_NONNEG, which allows the
9409 m68k DBRA instruction to be used. */
9411 if (((GET_CODE (comparison) == GT && XEXP (comparison, 1) == constm1_rtx)
9412 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
9413 && GET_CODE (bl->biv->add_val) == CONST_INT
9414 && INTVAL (bl->biv->add_val) < 0)
9416 /* Initial value must be greater than 0,
9417 init_val % -dec_value == 0 to ensure that it equals zero on
9418 the last iteration */
9420 if (GET_CODE (bl->initial_value) == CONST_INT
9421 && INTVAL (bl->initial_value) > 0
9422 && (INTVAL (bl->initial_value)
9423 % (-INTVAL (bl->biv->add_val))) == 0)
9425 /* Register always nonnegative, add REG_NOTE to branch. */
9426 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
9427 REG_NOTES (jump)
9428 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
9429 REG_NOTES (jump));
9430 bl->nonneg = 1;
9432 return 1;
9435 /* If the decrement is 1 and the value was tested as >= 0 before
9436 the loop, then we can safely optimize. */
9437 for (p = loop_start; p; p = PREV_INSN (p))
9439 if (LABEL_P (p))
9440 break;
9441 if (!JUMP_P (p))
9442 continue;
9444 before_comparison = get_condition_for_loop (loop, p);
9445 if (before_comparison
9446 && XEXP (before_comparison, 0) == bl->biv->dest_reg
9447 && (GET_CODE (before_comparison) == LT
9448 || GET_CODE (before_comparison) == LTU)
9449 && XEXP (before_comparison, 1) == const0_rtx
9450 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
9451 && INTVAL (bl->biv->add_val) == -1)
9453 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
9454 REG_NOTES (jump)
9455 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
9456 REG_NOTES (jump));
9457 bl->nonneg = 1;
9459 return 1;
9463 else if (GET_CODE (bl->biv->add_val) == CONST_INT
9464 && INTVAL (bl->biv->add_val) > 0)
9466 /* Try to change inc to dec, so can apply above optimization. */
9467 /* Can do this if:
9468 all registers modified are induction variables or invariant,
9469 all memory references have non-overlapping addresses
9470 (obviously true if only one write)
9471 allow 2 insns for the compare/jump at the end of the loop. */
9472 /* Also, we must avoid any instructions which use both the reversed
9473 biv and another biv. Such instructions will fail if the loop is
9474 reversed. We meet this condition by requiring that either
9475 no_use_except_counting is true, or else that there is only
9476 one biv. */
9477 int num_nonfixed_reads = 0;
9478 /* 1 if the iteration var is used only to count iterations. */
9479 int no_use_except_counting = 0;
9480 /* 1 if the loop has no memory store, or it has a single memory store
9481 which is reversible. */
9482 int reversible_mem_store = 1;
9484 if (bl->giv_count == 0
9485 && !loop->exit_count
9486 && !loop_info->has_multiple_exit_targets)
9488 rtx bivreg = regno_reg_rtx[bl->regno];
9489 struct iv_class *blt;
9491 /* If there are no givs for this biv, and the only exit is the
9492 fall through at the end of the loop, then
9493 see if perhaps there are no uses except to count. */
9494 no_use_except_counting = 1;
9495 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
9496 if (INSN_P (p))
9498 rtx set = single_set (p);
9500 if (set && REG_P (SET_DEST (set))
9501 && REGNO (SET_DEST (set)) == bl->regno)
9502 /* An insn that sets the biv is okay. */
9504 else if (!reg_mentioned_p (bivreg, PATTERN (p)))
9505 /* An insn that doesn't mention the biv is okay. */
9507 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
9508 || p == prev_nonnote_insn (loop_end))
9510 /* If either of these insns uses the biv and sets a pseudo
9511 that has more than one usage, then the biv has uses
9512 other than counting since it's used to derive a value
9513 that is used more than one time. */
9514 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
9515 regs);
9516 if (regs->multiple_uses)
9518 no_use_except_counting = 0;
9519 break;
9522 else
9524 no_use_except_counting = 0;
9525 break;
9529 /* A biv has uses besides counting if it is used to set
9530 another biv. */
9531 for (blt = ivs->list; blt; blt = blt->next)
9532 if (blt->init_set
9533 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
9535 no_use_except_counting = 0;
9536 break;
9540 if (no_use_except_counting)
9541 /* No need to worry about MEMs. */
9543 else if (loop_info->num_mem_sets <= 1)
9545 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
9546 if (INSN_P (p))
9547 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
9549 /* If the loop has a single store, and the destination address is
9550 invariant, then we can't reverse the loop, because this address
9551 might then have the wrong value at loop exit.
9552 This would work if the source was invariant also, however, in that
9553 case, the insn should have been moved out of the loop. */
9555 if (loop_info->num_mem_sets == 1)
9557 struct induction *v;
9559 /* If we could prove that each of the memory locations
9560 written to was different, then we could reverse the
9561 store -- but we don't presently have any way of
9562 knowing that. */
9563 reversible_mem_store = 0;
9565 /* If the store depends on a register that is set after the
9566 store, it depends on the initial value, and is thus not
9567 reversible. */
9568 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
9570 if (v->giv_type == DEST_REG
9571 && reg_mentioned_p (v->dest_reg,
9572 PATTERN (loop_info->first_loop_store_insn))
9573 && loop_insn_first_p (loop_info->first_loop_store_insn,
9574 v->insn))
9575 reversible_mem_store = 0;
9579 else
9580 return 0;
9582 /* This code only acts for innermost loops. Also it simplifies
9583 the memory address check by only reversing loops with
9584 zero or one memory access.
9585 Two memory accesses could involve parts of the same array,
9586 and that can't be reversed.
9587 If the biv is used only for counting, than we don't need to worry
9588 about all these things. */
9590 if ((num_nonfixed_reads <= 1
9591 && ! loop_info->has_nonconst_call
9592 && ! loop_info->has_prefetch
9593 && ! loop_info->has_volatile
9594 && reversible_mem_store
9595 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
9596 + num_unmoved_movables (loop) + compare_and_branch == insn_count)
9597 && (bl == ivs->list && bl->next == 0))
9598 || (no_use_except_counting && ! loop_info->has_prefetch))
9600 rtx tem;
9602 /* Loop can be reversed. */
9603 if (loop_dump_stream)
9604 fprintf (loop_dump_stream, "Can reverse loop\n");
9606 /* Now check other conditions:
9608 The increment must be a constant, as must the initial value,
9609 and the comparison code must be LT.
9611 This test can probably be improved since +/- 1 in the constant
9612 can be obtained by changing LT to LE and vice versa; this is
9613 confusing. */
9615 if (comparison
9616 /* for constants, LE gets turned into LT */
9617 && (GET_CODE (comparison) == LT
9618 || (GET_CODE (comparison) == LE
9619 && no_use_except_counting)
9620 || GET_CODE (comparison) == LTU))
9622 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
9623 rtx initial_value, comparison_value;
9624 int nonneg = 0;
9625 enum rtx_code cmp_code;
9626 int comparison_const_width;
9627 unsigned HOST_WIDE_INT comparison_sign_mask;
9628 bool keep_first_compare;
9630 add_val = INTVAL (bl->biv->add_val);
9631 comparison_value = XEXP (comparison, 1);
9632 if (GET_MODE (comparison_value) == VOIDmode)
9633 comparison_const_width
9634 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
9635 else
9636 comparison_const_width
9637 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
9638 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
9639 comparison_const_width = HOST_BITS_PER_WIDE_INT;
9640 comparison_sign_mask
9641 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
9643 /* If the comparison value is not a loop invariant, then we
9644 can not reverse this loop.
9646 ??? If the insns which initialize the comparison value as
9647 a whole compute an invariant result, then we could move
9648 them out of the loop and proceed with loop reversal. */
9649 if (! loop_invariant_p (loop, comparison_value))
9650 return 0;
9652 if (GET_CODE (comparison_value) == CONST_INT)
9653 comparison_val = INTVAL (comparison_value);
9654 initial_value = bl->initial_value;
9656 /* Normalize the initial value if it is an integer and
9657 has no other use except as a counter. This will allow
9658 a few more loops to be reversed. */
9659 if (no_use_except_counting
9660 && GET_CODE (comparison_value) == CONST_INT
9661 && GET_CODE (initial_value) == CONST_INT)
9663 comparison_val = comparison_val - INTVAL (bl->initial_value);
9664 /* The code below requires comparison_val to be a multiple
9665 of add_val in order to do the loop reversal, so
9666 round up comparison_val to a multiple of add_val.
9667 Since comparison_value is constant, we know that the
9668 current comparison code is LT. */
9669 comparison_val = comparison_val + add_val - 1;
9670 comparison_val
9671 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
9672 /* We postpone overflow checks for COMPARISON_VAL here;
9673 even if there is an overflow, we might still be able to
9674 reverse the loop, if converting the loop exit test to
9675 NE is possible. */
9676 initial_value = const0_rtx;
9679 /* First check if we can do a vanilla loop reversal. */
9680 if (initial_value == const0_rtx
9681 && GET_CODE (comparison_value) == CONST_INT
9682 /* Now do postponed overflow checks on COMPARISON_VAL. */
9683 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
9684 & comparison_sign_mask))
9686 /* Register will always be nonnegative, with value
9687 0 on last iteration */
9688 add_adjust = add_val;
9689 nonneg = 1;
9690 cmp_code = GE;
9692 else
9693 return 0;
9695 if (GET_CODE (comparison) == LE)
9696 add_adjust -= add_val;
9698 /* If the initial value is not zero, or if the comparison
9699 value is not an exact multiple of the increment, then we
9700 can not reverse this loop. */
9701 if (initial_value == const0_rtx
9702 && GET_CODE (comparison_value) == CONST_INT)
9704 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
9705 return 0;
9707 else
9709 if (! no_use_except_counting || add_val != 1)
9710 return 0;
9713 final_value = comparison_value;
9715 /* Reset these in case we normalized the initial value
9716 and comparison value above. */
9717 if (GET_CODE (comparison_value) == CONST_INT
9718 && GET_CODE (initial_value) == CONST_INT)
9720 comparison_value = GEN_INT (comparison_val);
9721 final_value
9722 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
9724 bl->initial_value = initial_value;
9726 /* Save some info needed to produce the new insns. */
9727 reg = bl->biv->dest_reg;
9728 mode = GET_MODE (reg);
9729 jump_label = condjump_label (PREV_INSN (loop_end));
9730 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
9732 /* Set start_value; if this is not a CONST_INT, we need
9733 to generate a SUB.
9734 Initialize biv to start_value before loop start.
9735 The old initializing insn will be deleted as a
9736 dead store by flow.c. */
9737 if (initial_value == const0_rtx
9738 && GET_CODE (comparison_value) == CONST_INT)
9740 start_value
9741 = gen_int_mode (comparison_val - add_adjust, mode);
9742 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
9744 else if (GET_CODE (initial_value) == CONST_INT)
9746 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
9747 rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
9749 if (add_insn == 0)
9750 return 0;
9752 start_value
9753 = gen_rtx_PLUS (mode, comparison_value, offset);
9754 loop_insn_hoist (loop, add_insn);
9755 if (GET_CODE (comparison) == LE)
9756 final_value = gen_rtx_PLUS (mode, comparison_value,
9757 GEN_INT (add_val));
9759 else if (! add_adjust)
9761 rtx sub_insn = gen_sub3_insn (reg, comparison_value,
9762 initial_value);
9764 if (sub_insn == 0)
9765 return 0;
9766 start_value
9767 = gen_rtx_MINUS (mode, comparison_value, initial_value);
9768 loop_insn_hoist (loop, sub_insn);
9770 else
9771 /* We could handle the other cases too, but it'll be
9772 better to have a testcase first. */
9773 return 0;
9775 /* We may not have a single insn which can increment a reg, so
9776 create a sequence to hold all the insns from expand_inc. */
9777 start_sequence ();
9778 expand_inc (reg, new_add_val);
9779 tem = get_insns ();
9780 end_sequence ();
9782 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
9783 delete_insn (bl->biv->insn);
9785 /* Update biv info to reflect its new status. */
9786 bl->biv->insn = p;
9787 bl->initial_value = start_value;
9788 bl->biv->add_val = new_add_val;
9790 /* Update loop info. */
9791 loop_info->initial_value = reg;
9792 loop_info->initial_equiv_value = reg;
9793 loop_info->final_value = const0_rtx;
9794 loop_info->final_equiv_value = const0_rtx;
9795 loop_info->comparison_value = const0_rtx;
9796 loop_info->comparison_code = cmp_code;
9797 loop_info->increment = new_add_val;
9799 /* Inc LABEL_NUSES so that delete_insn will
9800 not delete the label. */
9801 LABEL_NUSES (XEXP (jump_label, 0))++;
9803 /* If we have a separate comparison insn that does more
9804 than just set cc0, the result of the comparison might
9805 be used outside the loop. */
9806 keep_first_compare = (compare_and_branch == 2
9807 #ifdef HAVE_CC0
9808 && sets_cc0_p (first_compare) <= 0
9809 #endif
9812 /* Emit an insn after the end of the loop to set the biv's
9813 proper exit value if it is used anywhere outside the loop. */
9814 if (keep_first_compare
9815 || (REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
9816 || ! bl->init_insn
9817 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
9818 loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
9820 if (keep_first_compare)
9821 loop_insn_sink (loop, PATTERN (first_compare));
9823 /* Delete compare/branch at end of loop. */
9824 delete_related_insns (PREV_INSN (loop_end));
9825 if (compare_and_branch == 2)
9826 delete_related_insns (first_compare);
9828 /* Add new compare/branch insn at end of loop. */
9829 start_sequence ();
9830 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
9831 mode, 0,
9832 XEXP (jump_label, 0));
9833 tem = get_insns ();
9834 end_sequence ();
9835 emit_jump_insn_before (tem, loop_end);
9837 for (tem = PREV_INSN (loop_end);
9838 tem && !JUMP_P (tem);
9839 tem = PREV_INSN (tem))
9842 if (tem)
9843 JUMP_LABEL (tem) = XEXP (jump_label, 0);
9845 if (nonneg)
9847 if (tem)
9849 /* Increment of LABEL_NUSES done above. */
9850 /* Register is now always nonnegative,
9851 so add REG_NONNEG note to the branch. */
9852 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
9853 REG_NOTES (tem));
9855 bl->nonneg = 1;
9858 /* No insn may reference both the reversed and another biv or it
9859 will fail (see comment near the top of the loop reversal
9860 code).
9861 Earlier on, we have verified that the biv has no use except
9862 counting, or it is the only biv in this function.
9863 However, the code that computes no_use_except_counting does
9864 not verify reg notes. It's possible to have an insn that
9865 references another biv, and has a REG_EQUAL note with an
9866 expression based on the reversed biv. To avoid this case,
9867 remove all REG_EQUAL notes based on the reversed biv
9868 here. */
9869 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
9870 if (INSN_P (p))
9872 rtx *pnote;
9873 rtx set = single_set (p);
9874 /* If this is a set of a GIV based on the reversed biv, any
9875 REG_EQUAL notes should still be correct. */
9876 if (! set
9877 || !REG_P (SET_DEST (set))
9878 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
9879 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
9880 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
9881 for (pnote = &REG_NOTES (p); *pnote;)
9883 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
9884 && reg_mentioned_p (regno_reg_rtx[bl->regno],
9885 XEXP (*pnote, 0)))
9886 *pnote = XEXP (*pnote, 1);
9887 else
9888 pnote = &XEXP (*pnote, 1);
9892 /* Mark that this biv has been reversed. Each giv which depends
9893 on this biv, and which is also live past the end of the loop
9894 will have to be fixed up. */
9896 bl->reversed = 1;
9898 if (loop_dump_stream)
9900 fprintf (loop_dump_stream, "Reversed loop");
9901 if (bl->nonneg)
9902 fprintf (loop_dump_stream, " and added reg_nonneg\n");
9903 else
9904 fprintf (loop_dump_stream, "\n");
9907 return 1;
9912 return 0;
9915 /* Verify whether the biv BL appears to be eliminable,
9916 based on the insns in the loop that refer to it.
9918 If ELIMINATE_P is nonzero, actually do the elimination.
9920 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
9921 determine whether invariant insns should be placed inside or at the
9922 start of the loop. */
9924 static int
9925 maybe_eliminate_biv (const struct loop *loop, struct iv_class *bl,
9926 int eliminate_p, int threshold, int insn_count)
9928 struct loop_ivs *ivs = LOOP_IVS (loop);
9929 rtx reg = bl->biv->dest_reg;
9930 rtx p;
9932 /* Scan all insns in the loop, stopping if we find one that uses the
9933 biv in a way that we cannot eliminate. */
9935 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
9937 enum rtx_code code = GET_CODE (p);
9938 basic_block where_bb = 0;
9939 rtx where_insn = threshold >= insn_count ? 0 : p;
9940 rtx note;
9942 /* If this is a libcall that sets a giv, skip ahead to its end. */
9943 if (INSN_P (p))
9945 note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
9947 if (note)
9949 rtx last = XEXP (note, 0);
9950 rtx set = single_set (last);
9952 if (set && REG_P (SET_DEST (set)))
9954 unsigned int regno = REGNO (SET_DEST (set));
9956 if (regno < ivs->n_regs
9957 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
9958 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
9959 p = last;
9964 /* Closely examine the insn if the biv is mentioned. */
9965 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
9966 && reg_mentioned_p (reg, PATTERN (p))
9967 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
9968 eliminate_p, where_bb, where_insn))
9970 if (loop_dump_stream)
9971 fprintf (loop_dump_stream,
9972 "Cannot eliminate biv %d: biv used in insn %d.\n",
9973 bl->regno, INSN_UID (p));
9974 break;
9977 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
9978 if (eliminate_p
9979 && (note = find_reg_note (p, REG_EQUAL, NULL_RTX)) != NULL_RTX
9980 && reg_mentioned_p (reg, XEXP (note, 0)))
9981 remove_note (p, note);
9984 if (p == loop->end)
9986 if (loop_dump_stream)
9987 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
9988 bl->regno, eliminate_p ? "was" : "can be");
9989 return 1;
9992 return 0;
9995 /* INSN and REFERENCE are instructions in the same insn chain.
9996 Return nonzero if INSN is first. */
9998 static int
9999 loop_insn_first_p (rtx insn, rtx reference)
10001 rtx p, q;
10003 for (p = insn, q = reference;;)
10005 /* Start with test for not first so that INSN == REFERENCE yields not
10006 first. */
10007 if (q == insn || ! p)
10008 return 0;
10009 if (p == reference || ! q)
10010 return 1;
10012 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
10013 previous insn, hence the <= comparison below does not work if
10014 P is a note. */
10015 if (INSN_UID (p) < max_uid_for_loop
10016 && INSN_UID (q) < max_uid_for_loop
10017 && !NOTE_P (p))
10018 return INSN_LUID (p) <= INSN_LUID (q);
10020 if (INSN_UID (p) >= max_uid_for_loop
10021 || NOTE_P (p))
10022 p = NEXT_INSN (p);
10023 if (INSN_UID (q) >= max_uid_for_loop)
10024 q = NEXT_INSN (q);
10028 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
10029 the offset that we have to take into account due to auto-increment /
10030 div derivation is zero. */
10031 static int
10032 biv_elimination_giv_has_0_offset (struct induction *biv,
10033 struct induction *giv, rtx insn)
10035 /* If the giv V had the auto-inc address optimization applied
10036 to it, and INSN occurs between the giv insn and the biv
10037 insn, then we'd have to adjust the value used here.
10038 This is rare, so we don't bother to make this possible. */
10039 if (giv->auto_inc_opt
10040 && ((loop_insn_first_p (giv->insn, insn)
10041 && loop_insn_first_p (insn, biv->insn))
10042 || (loop_insn_first_p (biv->insn, insn)
10043 && loop_insn_first_p (insn, giv->insn))))
10044 return 0;
10046 return 1;
10049 /* If BL appears in X (part of the pattern of INSN), see if we can
10050 eliminate its use. If so, return 1. If not, return 0.
10052 If BIV does not appear in X, return 1.
10054 If ELIMINATE_P is nonzero, actually do the elimination.
10055 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
10056 Depending on how many items have been moved out of the loop, it
10057 will either be before INSN (when WHERE_INSN is nonzero) or at the
10058 start of the loop (when WHERE_INSN is zero). */
10060 static int
10061 maybe_eliminate_biv_1 (const struct loop *loop, rtx x, rtx insn,
10062 struct iv_class *bl, int eliminate_p,
10063 basic_block where_bb, rtx where_insn)
10065 enum rtx_code code = GET_CODE (x);
10066 rtx reg = bl->biv->dest_reg;
10067 enum machine_mode mode = GET_MODE (reg);
10068 struct induction *v;
10069 rtx arg, tem;
10070 #ifdef HAVE_cc0
10071 rtx new;
10072 #endif
10073 int arg_operand;
10074 const char *fmt;
10075 int i, j;
10077 switch (code)
10079 case REG:
10080 /* If we haven't already been able to do something with this BIV,
10081 we can't eliminate it. */
10082 if (x == reg)
10083 return 0;
10084 return 1;
10086 case SET:
10087 /* If this sets the BIV, it is not a problem. */
10088 if (SET_DEST (x) == reg)
10089 return 1;
10091 /* If this is an insn that defines a giv, it is also ok because
10092 it will go away when the giv is reduced. */
10093 for (v = bl->giv; v; v = v->next_iv)
10094 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
10095 return 1;
10097 #ifdef HAVE_cc0
10098 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
10100 /* Can replace with any giv that was reduced and
10101 that has (MULT_VAL != 0) and (ADD_VAL == 0).
10102 Require a constant for MULT_VAL, so we know it's nonzero.
10103 ??? We disable this optimization to avoid potential
10104 overflows. */
10106 for (v = bl->giv; v; v = v->next_iv)
10107 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
10108 && v->add_val == const0_rtx
10109 && ! v->ignore && ! v->maybe_dead && v->always_computable
10110 && v->mode == mode
10111 && 0)
10113 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10114 continue;
10116 if (! eliminate_p)
10117 return 1;
10119 /* If the giv has the opposite direction of change,
10120 then reverse the comparison. */
10121 if (INTVAL (v->mult_val) < 0)
10122 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
10123 const0_rtx, v->new_reg);
10124 else
10125 new = v->new_reg;
10127 /* We can probably test that giv's reduced reg. */
10128 if (validate_change (insn, &SET_SRC (x), new, 0))
10129 return 1;
10132 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
10133 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
10134 Require a constant for MULT_VAL, so we know it's nonzero.
10135 ??? Do this only if ADD_VAL is a pointer to avoid a potential
10136 overflow problem. */
10138 for (v = bl->giv; v; v = v->next_iv)
10139 if (GET_CODE (v->mult_val) == CONST_INT
10140 && v->mult_val != const0_rtx
10141 && ! v->ignore && ! v->maybe_dead && v->always_computable
10142 && v->mode == mode
10143 && (GET_CODE (v->add_val) == SYMBOL_REF
10144 || GET_CODE (v->add_val) == LABEL_REF
10145 || GET_CODE (v->add_val) == CONST
10146 || (REG_P (v->add_val)
10147 && REG_POINTER (v->add_val))))
10149 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10150 continue;
10152 if (! eliminate_p)
10153 return 1;
10155 /* If the giv has the opposite direction of change,
10156 then reverse the comparison. */
10157 if (INTVAL (v->mult_val) < 0)
10158 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
10159 v->new_reg);
10160 else
10161 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
10162 copy_rtx (v->add_val));
10164 /* Replace biv with the giv's reduced register. */
10165 update_reg_last_use (v->add_val, insn);
10166 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
10167 return 1;
10169 /* Insn doesn't support that constant or invariant. Copy it
10170 into a register (it will be a loop invariant.) */
10171 tem = gen_reg_rtx (GET_MODE (v->new_reg));
10173 loop_insn_emit_before (loop, 0, where_insn,
10174 gen_move_insn (tem,
10175 copy_rtx (v->add_val)));
10177 /* Substitute the new register for its invariant value in
10178 the compare expression. */
10179 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
10180 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
10181 return 1;
10184 #endif
10185 break;
10187 case COMPARE:
10188 case EQ: case NE:
10189 case GT: case GE: case GTU: case GEU:
10190 case LT: case LE: case LTU: case LEU:
10191 /* See if either argument is the biv. */
10192 if (XEXP (x, 0) == reg)
10193 arg = XEXP (x, 1), arg_operand = 1;
10194 else if (XEXP (x, 1) == reg)
10195 arg = XEXP (x, 0), arg_operand = 0;
10196 else
10197 break;
10199 if (CONSTANT_P (arg))
10201 /* First try to replace with any giv that has constant positive
10202 mult_val and constant add_val. We might be able to support
10203 negative mult_val, but it seems complex to do it in general. */
10205 for (v = bl->giv; v; v = v->next_iv)
10206 if (GET_CODE (v->mult_val) == CONST_INT
10207 && INTVAL (v->mult_val) > 0
10208 && (GET_CODE (v->add_val) == SYMBOL_REF
10209 || GET_CODE (v->add_val) == LABEL_REF
10210 || GET_CODE (v->add_val) == CONST
10211 || (REG_P (v->add_val)
10212 && REG_POINTER (v->add_val)))
10213 && ! v->ignore && ! v->maybe_dead && v->always_computable
10214 && v->mode == mode)
10216 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10217 continue;
10219 /* Don't eliminate if the linear combination that makes up
10220 the giv overflows when it is applied to ARG. */
10221 if (GET_CODE (arg) == CONST_INT)
10223 rtx add_val;
10225 if (GET_CODE (v->add_val) == CONST_INT)
10226 add_val = v->add_val;
10227 else
10228 add_val = const0_rtx;
10230 if (const_mult_add_overflow_p (arg, v->mult_val,
10231 add_val, mode, 1))
10232 continue;
10235 if (! eliminate_p)
10236 return 1;
10238 /* Replace biv with the giv's reduced reg. */
10239 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
10241 /* If all constants are actually constant integers and
10242 the derived constant can be directly placed in the COMPARE,
10243 do so. */
10244 if (GET_CODE (arg) == CONST_INT
10245 && GET_CODE (v->add_val) == CONST_INT)
10247 tem = expand_mult_add (arg, NULL_RTX, v->mult_val,
10248 v->add_val, mode, 1);
10250 else
10252 /* Otherwise, load it into a register. */
10253 tem = gen_reg_rtx (mode);
10254 loop_iv_add_mult_emit_before (loop, arg,
10255 v->mult_val, v->add_val,
10256 tem, where_bb, where_insn);
10259 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
10261 if (apply_change_group ())
10262 return 1;
10265 /* Look for giv with positive constant mult_val and nonconst add_val.
10266 Insert insns to calculate new compare value.
10267 ??? Turn this off due to possible overflow. */
10269 for (v = bl->giv; v; v = v->next_iv)
10270 if (GET_CODE (v->mult_val) == CONST_INT
10271 && INTVAL (v->mult_val) > 0
10272 && ! v->ignore && ! v->maybe_dead && v->always_computable
10273 && v->mode == mode
10274 && 0)
10276 rtx tem;
10278 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10279 continue;
10281 if (! eliminate_p)
10282 return 1;
10284 tem = gen_reg_rtx (mode);
10286 /* Replace biv with giv's reduced register. */
10287 validate_change (insn, &XEXP (x, 1 - arg_operand),
10288 v->new_reg, 1);
10290 /* Compute value to compare against. */
10291 loop_iv_add_mult_emit_before (loop, arg,
10292 v->mult_val, v->add_val,
10293 tem, where_bb, where_insn);
10294 /* Use it in this insn. */
10295 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
10296 if (apply_change_group ())
10297 return 1;
10300 else if (REG_P (arg) || MEM_P (arg))
10302 if (loop_invariant_p (loop, arg) == 1)
10304 /* Look for giv with constant positive mult_val and nonconst
10305 add_val. Insert insns to compute new compare value.
10306 ??? Turn this off due to possible overflow. */
10308 for (v = bl->giv; v; v = v->next_iv)
10309 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
10310 && ! v->ignore && ! v->maybe_dead && v->always_computable
10311 && v->mode == mode
10312 && 0)
10314 rtx tem;
10316 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10317 continue;
10319 if (! eliminate_p)
10320 return 1;
10322 tem = gen_reg_rtx (mode);
10324 /* Replace biv with giv's reduced register. */
10325 validate_change (insn, &XEXP (x, 1 - arg_operand),
10326 v->new_reg, 1);
10328 /* Compute value to compare against. */
10329 loop_iv_add_mult_emit_before (loop, arg,
10330 v->mult_val, v->add_val,
10331 tem, where_bb, where_insn);
10332 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
10333 if (apply_change_group ())
10334 return 1;
10338 /* This code has problems. Basically, you can't know when
10339 seeing if we will eliminate BL, whether a particular giv
10340 of ARG will be reduced. If it isn't going to be reduced,
10341 we can't eliminate BL. We can try forcing it to be reduced,
10342 but that can generate poor code.
10344 The problem is that the benefit of reducing TV, below should
10345 be increased if BL can actually be eliminated, but this means
10346 we might have to do a topological sort of the order in which
10347 we try to process biv. It doesn't seem worthwhile to do
10348 this sort of thing now. */
10350 #if 0
10351 /* Otherwise the reg compared with had better be a biv. */
10352 if (!REG_P (arg)
10353 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
10354 return 0;
10356 /* Look for a pair of givs, one for each biv,
10357 with identical coefficients. */
10358 for (v = bl->giv; v; v = v->next_iv)
10360 struct induction *tv;
10362 if (v->ignore || v->maybe_dead || v->mode != mode)
10363 continue;
10365 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
10366 tv = tv->next_iv)
10367 if (! tv->ignore && ! tv->maybe_dead
10368 && rtx_equal_p (tv->mult_val, v->mult_val)
10369 && rtx_equal_p (tv->add_val, v->add_val)
10370 && tv->mode == mode)
10372 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10373 continue;
10375 if (! eliminate_p)
10376 return 1;
10378 /* Replace biv with its giv's reduced reg. */
10379 XEXP (x, 1 - arg_operand) = v->new_reg;
10380 /* Replace other operand with the other giv's
10381 reduced reg. */
10382 XEXP (x, arg_operand) = tv->new_reg;
10383 return 1;
10386 #endif
10389 /* If we get here, the biv can't be eliminated. */
10390 return 0;
10392 case MEM:
10393 /* If this address is a DEST_ADDR giv, it doesn't matter if the
10394 biv is used in it, since it will be replaced. */
10395 for (v = bl->giv; v; v = v->next_iv)
10396 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
10397 return 1;
10398 break;
10400 default:
10401 break;
10404 /* See if any subexpression fails elimination. */
10405 fmt = GET_RTX_FORMAT (code);
10406 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10408 switch (fmt[i])
10410 case 'e':
10411 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
10412 eliminate_p, where_bb, where_insn))
10413 return 0;
10414 break;
10416 case 'E':
10417 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10418 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
10419 eliminate_p, where_bb, where_insn))
10420 return 0;
10421 break;
10425 return 1;
10428 /* Return nonzero if the last use of REG
10429 is in an insn following INSN in the same basic block. */
10431 static int
10432 last_use_this_basic_block (rtx reg, rtx insn)
10434 rtx n;
10435 for (n = insn;
10436 n && !LABEL_P (n) && !JUMP_P (n);
10437 n = NEXT_INSN (n))
10439 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
10440 return 1;
10442 return 0;
10445 /* Called via `note_stores' to record the initial value of a biv. Here we
10446 just record the location of the set and process it later. */
10448 static void
10449 record_initial (rtx dest, rtx set, void *data ATTRIBUTE_UNUSED)
10451 struct loop_ivs *ivs = (struct loop_ivs *) data;
10452 struct iv_class *bl;
10454 if (!REG_P (dest)
10455 || REGNO (dest) >= ivs->n_regs
10456 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
10457 return;
10459 bl = REG_IV_CLASS (ivs, REGNO (dest));
10461 /* If this is the first set found, record it. */
10462 if (bl->init_insn == 0)
10464 bl->init_insn = note_insn;
10465 bl->init_set = set;
10469 /* If any of the registers in X are "old" and currently have a last use earlier
10470 than INSN, update them to have a last use of INSN. Their actual last use
10471 will be the previous insn but it will not have a valid uid_luid so we can't
10472 use it. X must be a source expression only. */
10474 static void
10475 update_reg_last_use (rtx x, rtx insn)
10477 /* Check for the case where INSN does not have a valid luid. In this case,
10478 there is no need to modify the regno_last_uid, as this can only happen
10479 when code is inserted after the loop_end to set a pseudo's final value,
10480 and hence this insn will never be the last use of x.
10481 ???? This comment is not correct. See for example loop_givs_reduce.
10482 This may insert an insn before another new insn. */
10483 if (REG_P (x) && REGNO (x) < max_reg_before_loop
10484 && INSN_UID (insn) < max_uid_for_loop
10485 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
10487 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
10489 else
10491 int i, j;
10492 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10493 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
10495 if (fmt[i] == 'e')
10496 update_reg_last_use (XEXP (x, i), insn);
10497 else if (fmt[i] == 'E')
10498 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10499 update_reg_last_use (XVECEXP (x, i, j), insn);
10504 /* Similar to rtlanal.c:get_condition, except that we also put an
10505 invariant last unless both operands are invariants. */
10507 static rtx
10508 get_condition_for_loop (const struct loop *loop, rtx x)
10510 rtx comparison = get_condition (x, (rtx*) 0, false, true);
10512 if (comparison == 0
10513 || ! loop_invariant_p (loop, XEXP (comparison, 0))
10514 || loop_invariant_p (loop, XEXP (comparison, 1)))
10515 return comparison;
10517 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
10518 XEXP (comparison, 1), XEXP (comparison, 0));
10521 /* Scan the function and determine whether it has indirect (computed) jumps.
10523 This is taken mostly from flow.c; similar code exists elsewhere
10524 in the compiler. It may be useful to put this into rtlanal.c. */
10525 static int
10526 indirect_jump_in_function_p (rtx start)
10528 rtx insn;
10530 for (insn = start; insn; insn = NEXT_INSN (insn))
10531 if (computed_jump_p (insn))
10532 return 1;
10534 return 0;
10537 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
10538 documentation for LOOP_MEMS for the definition of `appropriate'.
10539 This function is called from prescan_loop via for_each_rtx. */
10541 static int
10542 insert_loop_mem (rtx *mem, void *data ATTRIBUTE_UNUSED)
10544 struct loop_info *loop_info = data;
10545 int i;
10546 rtx m = *mem;
10548 if (m == NULL_RTX)
10549 return 0;
10551 switch (GET_CODE (m))
10553 case MEM:
10554 break;
10556 case CLOBBER:
10557 /* We're not interested in MEMs that are only clobbered. */
10558 return -1;
10560 case CONST_DOUBLE:
10561 /* We're not interested in the MEM associated with a
10562 CONST_DOUBLE, so there's no need to traverse into this. */
10563 return -1;
10565 case EXPR_LIST:
10566 /* We're not interested in any MEMs that only appear in notes. */
10567 return -1;
10569 default:
10570 /* This is not a MEM. */
10571 return 0;
10574 /* See if we've already seen this MEM. */
10575 for (i = 0; i < loop_info->mems_idx; ++i)
10576 if (rtx_equal_p (m, loop_info->mems[i].mem))
10578 if (MEM_VOLATILE_P (m) && !MEM_VOLATILE_P (loop_info->mems[i].mem))
10579 loop_info->mems[i].mem = m;
10580 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
10581 /* The modes of the two memory accesses are different. If
10582 this happens, something tricky is going on, and we just
10583 don't optimize accesses to this MEM. */
10584 loop_info->mems[i].optimize = 0;
10586 return 0;
10589 /* Resize the array, if necessary. */
10590 if (loop_info->mems_idx == loop_info->mems_allocated)
10592 if (loop_info->mems_allocated != 0)
10593 loop_info->mems_allocated *= 2;
10594 else
10595 loop_info->mems_allocated = 32;
10597 loop_info->mems = xrealloc (loop_info->mems,
10598 loop_info->mems_allocated * sizeof (loop_mem_info));
10601 /* Actually insert the MEM. */
10602 loop_info->mems[loop_info->mems_idx].mem = m;
10603 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
10604 because we can't put it in a register. We still store it in the
10605 table, though, so that if we see the same address later, but in a
10606 non-BLK mode, we'll not think we can optimize it at that point. */
10607 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
10608 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
10609 ++loop_info->mems_idx;
10611 return 0;
10615 /* Allocate REGS->ARRAY or reallocate it if it is too small.
10617 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
10618 register that is modified by an insn between FROM and TO. If the
10619 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
10620 more, stop incrementing it, to avoid overflow.
10622 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
10623 register I is used, if it is only used once. Otherwise, it is set
10624 to 0 (for no uses) or const0_rtx for more than one use. This
10625 parameter may be zero, in which case this processing is not done.
10627 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
10628 optimize register I. */
10630 static void
10631 loop_regs_scan (const struct loop *loop, int extra_size)
10633 struct loop_regs *regs = LOOP_REGS (loop);
10634 int old_nregs;
10635 /* last_set[n] is nonzero iff reg n has been set in the current
10636 basic block. In that case, it is the insn that last set reg n. */
10637 rtx *last_set;
10638 rtx insn;
10639 int i;
10641 old_nregs = regs->num;
10642 regs->num = max_reg_num ();
10644 /* Grow the regs array if not allocated or too small. */
10645 if (regs->num >= regs->size)
10647 regs->size = regs->num + extra_size;
10649 regs->array = xrealloc (regs->array, regs->size * sizeof (*regs->array));
10651 /* Zero the new elements. */
10652 memset (regs->array + old_nregs, 0,
10653 (regs->size - old_nregs) * sizeof (*regs->array));
10656 /* Clear previously scanned fields but do not clear n_times_set. */
10657 for (i = 0; i < old_nregs; i++)
10659 regs->array[i].set_in_loop = 0;
10660 regs->array[i].may_not_optimize = 0;
10661 regs->array[i].single_usage = NULL_RTX;
10664 last_set = xcalloc (regs->num, sizeof (rtx));
10666 /* Scan the loop, recording register usage. */
10667 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
10668 insn = NEXT_INSN (insn))
10670 if (INSN_P (insn))
10672 /* Record registers that have exactly one use. */
10673 find_single_use_in_loop (regs, insn, PATTERN (insn));
10675 /* Include uses in REG_EQUAL notes. */
10676 if (REG_NOTES (insn))
10677 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
10679 if (GET_CODE (PATTERN (insn)) == SET
10680 || GET_CODE (PATTERN (insn)) == CLOBBER)
10681 count_one_set (regs, insn, PATTERN (insn), last_set);
10682 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
10684 int i;
10685 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
10686 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
10687 last_set);
10691 if (LABEL_P (insn) || JUMP_P (insn))
10692 memset (last_set, 0, regs->num * sizeof (rtx));
10694 /* Invalidate all registers used for function argument passing.
10695 We check rtx_varies_p for the same reason as below, to allow
10696 optimizing PIC calculations. */
10697 if (CALL_P (insn))
10699 rtx link;
10700 for (link = CALL_INSN_FUNCTION_USAGE (insn);
10701 link;
10702 link = XEXP (link, 1))
10704 rtx op, reg;
10706 if (GET_CODE (op = XEXP (link, 0)) == USE
10707 && REG_P (reg = XEXP (op, 0))
10708 && rtx_varies_p (reg, 1))
10709 regs->array[REGNO (reg)].may_not_optimize = 1;
10714 /* Invalidate all hard registers clobbered by calls. With one exception:
10715 a call-clobbered PIC register is still function-invariant for our
10716 purposes, since we can hoist any PIC calculations out of the loop.
10717 Thus the call to rtx_varies_p. */
10718 if (LOOP_INFO (loop)->has_call)
10719 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10720 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
10721 && rtx_varies_p (regno_reg_rtx[i], 1))
10723 regs->array[i].may_not_optimize = 1;
10724 regs->array[i].set_in_loop = 1;
10727 #ifdef AVOID_CCMODE_COPIES
10728 /* Don't try to move insns which set CC registers if we should not
10729 create CCmode register copies. */
10730 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
10731 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
10732 regs->array[i].may_not_optimize = 1;
10733 #endif
10735 /* Set regs->array[I].n_times_set for the new registers. */
10736 for (i = old_nregs; i < regs->num; i++)
10737 regs->array[i].n_times_set = regs->array[i].set_in_loop;
10739 free (last_set);
10742 /* Returns the number of real INSNs in the LOOP. */
10744 static int
10745 count_insns_in_loop (const struct loop *loop)
10747 int count = 0;
10748 rtx insn;
10750 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
10751 insn = NEXT_INSN (insn))
10752 if (INSN_P (insn))
10753 ++count;
10755 return count;
10758 /* Move MEMs into registers for the duration of the loop. */
10760 static void
10761 load_mems (const struct loop *loop)
10763 struct loop_info *loop_info = LOOP_INFO (loop);
10764 struct loop_regs *regs = LOOP_REGS (loop);
10765 int maybe_never = 0;
10766 int i;
10767 rtx p, prev_ebb_head;
10768 rtx label = NULL_RTX;
10769 rtx end_label;
10770 /* Nonzero if the next instruction may never be executed. */
10771 int next_maybe_never = 0;
10772 unsigned int last_max_reg = max_reg_num ();
10774 if (loop_info->mems_idx == 0)
10775 return;
10777 /* We cannot use next_label here because it skips over normal insns. */
10778 end_label = next_nonnote_insn (loop->end);
10779 if (end_label && !LABEL_P (end_label))
10780 end_label = NULL_RTX;
10782 /* Check to see if it's possible that some instructions in the loop are
10783 never executed. Also check if there is a goto out of the loop other
10784 than right after the end of the loop. */
10785 for (p = next_insn_in_loop (loop, loop->scan_start);
10786 p != NULL_RTX;
10787 p = next_insn_in_loop (loop, p))
10789 if (LABEL_P (p))
10790 maybe_never = 1;
10791 else if (JUMP_P (p)
10792 /* If we enter the loop in the middle, and scan
10793 around to the beginning, don't set maybe_never
10794 for that. This must be an unconditional jump,
10795 otherwise the code at the top of the loop might
10796 never be executed. Unconditional jumps are
10797 followed a by barrier then loop end. */
10798 && ! (JUMP_P (p)
10799 && JUMP_LABEL (p) == loop->top
10800 && NEXT_INSN (NEXT_INSN (p)) == loop->end
10801 && any_uncondjump_p (p)))
10803 /* If this is a jump outside of the loop but not right
10804 after the end of the loop, we would have to emit new fixup
10805 sequences for each such label. */
10806 if (/* If we can't tell where control might go when this
10807 JUMP_INSN is executed, we must be conservative. */
10808 !JUMP_LABEL (p)
10809 || (JUMP_LABEL (p) != end_label
10810 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
10811 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
10812 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
10813 return;
10815 if (!any_condjump_p (p))
10816 /* Something complicated. */
10817 maybe_never = 1;
10818 else
10819 /* If there are any more instructions in the loop, they
10820 might not be reached. */
10821 next_maybe_never = 1;
10823 else if (next_maybe_never)
10824 maybe_never = 1;
10827 /* Find start of the extended basic block that enters the loop. */
10828 for (p = loop->start;
10829 PREV_INSN (p) && !LABEL_P (p);
10830 p = PREV_INSN (p))
10832 prev_ebb_head = p;
10834 cselib_init (true);
10836 /* Build table of mems that get set to constant values before the
10837 loop. */
10838 for (; p != loop->start; p = NEXT_INSN (p))
10839 cselib_process_insn (p);
10841 /* Actually move the MEMs. */
10842 for (i = 0; i < loop_info->mems_idx; ++i)
10844 regset_head load_copies;
10845 regset_head store_copies;
10846 int written = 0;
10847 rtx reg;
10848 rtx mem = loop_info->mems[i].mem;
10849 rtx mem_list_entry;
10851 if (MEM_VOLATILE_P (mem)
10852 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
10853 /* There's no telling whether or not MEM is modified. */
10854 loop_info->mems[i].optimize = 0;
10856 /* Go through the MEMs written to in the loop to see if this
10857 one is aliased by one of them. */
10858 mem_list_entry = loop_info->store_mems;
10859 while (mem_list_entry)
10861 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
10862 written = 1;
10863 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
10864 mem, rtx_varies_p))
10866 /* MEM is indeed aliased by this store. */
10867 loop_info->mems[i].optimize = 0;
10868 break;
10870 mem_list_entry = XEXP (mem_list_entry, 1);
10873 if (flag_float_store && written
10874 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
10875 loop_info->mems[i].optimize = 0;
10877 /* If this MEM is written to, we must be sure that there
10878 are no reads from another MEM that aliases this one. */
10879 if (loop_info->mems[i].optimize && written)
10881 int j;
10883 for (j = 0; j < loop_info->mems_idx; ++j)
10885 if (j == i)
10886 continue;
10887 else if (true_dependence (mem,
10888 VOIDmode,
10889 loop_info->mems[j].mem,
10890 rtx_varies_p))
10892 /* It's not safe to hoist loop_info->mems[i] out of
10893 the loop because writes to it might not be
10894 seen by reads from loop_info->mems[j]. */
10895 loop_info->mems[i].optimize = 0;
10896 break;
10901 if (maybe_never && may_trap_p (mem))
10902 /* We can't access the MEM outside the loop; it might
10903 cause a trap that wouldn't have happened otherwise. */
10904 loop_info->mems[i].optimize = 0;
10906 if (!loop_info->mems[i].optimize)
10907 /* We thought we were going to lift this MEM out of the
10908 loop, but later discovered that we could not. */
10909 continue;
10911 INIT_REG_SET (&load_copies);
10912 INIT_REG_SET (&store_copies);
10914 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
10915 order to keep scan_loop from moving stores to this MEM
10916 out of the loop just because this REG is neither a
10917 user-variable nor used in the loop test. */
10918 reg = gen_reg_rtx (GET_MODE (mem));
10919 REG_USERVAR_P (reg) = 1;
10920 loop_info->mems[i].reg = reg;
10922 /* Now, replace all references to the MEM with the
10923 corresponding pseudos. */
10924 maybe_never = 0;
10925 for (p = next_insn_in_loop (loop, loop->scan_start);
10926 p != NULL_RTX;
10927 p = next_insn_in_loop (loop, p))
10929 if (INSN_P (p))
10931 rtx set;
10933 set = single_set (p);
10935 /* See if this copies the mem into a register that isn't
10936 modified afterwards. We'll try to do copy propagation
10937 a little further on. */
10938 if (set
10939 /* @@@ This test is _way_ too conservative. */
10940 && ! maybe_never
10941 && REG_P (SET_DEST (set))
10942 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
10943 && REGNO (SET_DEST (set)) < last_max_reg
10944 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
10945 && rtx_equal_p (SET_SRC (set), mem))
10946 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
10948 /* See if this copies the mem from a register that isn't
10949 modified afterwards. We'll try to remove the
10950 redundant copy later on by doing a little register
10951 renaming and copy propagation. This will help
10952 to untangle things for the BIV detection code. */
10953 if (set
10954 && ! maybe_never
10955 && REG_P (SET_SRC (set))
10956 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
10957 && REGNO (SET_SRC (set)) < last_max_reg
10958 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
10959 && rtx_equal_p (SET_DEST (set), mem))
10960 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
10962 /* If this is a call which uses / clobbers this memory
10963 location, we must not change the interface here. */
10964 if (CALL_P (p)
10965 && reg_mentioned_p (loop_info->mems[i].mem,
10966 CALL_INSN_FUNCTION_USAGE (p)))
10968 cancel_changes (0);
10969 loop_info->mems[i].optimize = 0;
10970 break;
10972 else
10973 /* Replace the memory reference with the shadow register. */
10974 replace_loop_mems (p, loop_info->mems[i].mem,
10975 loop_info->mems[i].reg, written);
10978 if (LABEL_P (p)
10979 || JUMP_P (p))
10980 maybe_never = 1;
10983 if (! loop_info->mems[i].optimize)
10984 ; /* We found we couldn't do the replacement, so do nothing. */
10985 else if (! apply_change_group ())
10986 /* We couldn't replace all occurrences of the MEM. */
10987 loop_info->mems[i].optimize = 0;
10988 else
10990 /* Load the memory immediately before LOOP->START, which is
10991 the NOTE_LOOP_BEG. */
10992 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
10993 rtx set;
10994 rtx best = mem;
10995 unsigned j;
10996 struct elt_loc_list *const_equiv = 0;
10997 reg_set_iterator rsi;
10999 if (e)
11001 struct elt_loc_list *equiv;
11002 struct elt_loc_list *best_equiv = 0;
11003 for (equiv = e->locs; equiv; equiv = equiv->next)
11005 if (CONSTANT_P (equiv->loc))
11006 const_equiv = equiv;
11007 else if (REG_P (equiv->loc)
11008 /* Extending hard register lifetimes causes crash
11009 on SRC targets. Doing so on non-SRC is
11010 probably also not good idea, since we most
11011 probably have pseudoregister equivalence as
11012 well. */
11013 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
11014 best_equiv = equiv;
11016 /* Use the constant equivalence if that is cheap enough. */
11017 if (! best_equiv)
11018 best_equiv = const_equiv;
11019 else if (const_equiv
11020 && (rtx_cost (const_equiv->loc, SET)
11021 <= rtx_cost (best_equiv->loc, SET)))
11023 best_equiv = const_equiv;
11024 const_equiv = 0;
11027 /* If best_equiv is nonzero, we know that MEM is set to a
11028 constant or register before the loop. We will use this
11029 knowledge to initialize the shadow register with that
11030 constant or reg rather than by loading from MEM. */
11031 if (best_equiv)
11032 best = copy_rtx (best_equiv->loc);
11035 set = gen_move_insn (reg, best);
11036 set = loop_insn_hoist (loop, set);
11037 if (REG_P (best))
11039 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
11040 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
11042 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
11043 break;
11047 if (const_equiv)
11048 set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
11050 if (written)
11052 if (label == NULL_RTX)
11054 label = gen_label_rtx ();
11055 emit_label_after (label, loop->end);
11058 /* Store the memory immediately after END, which is
11059 the NOTE_LOOP_END. */
11060 set = gen_move_insn (copy_rtx (mem), reg);
11061 loop_insn_emit_after (loop, 0, label, set);
11064 if (loop_dump_stream)
11066 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
11067 REGNO (reg), (written ? "r/w" : "r/o"));
11068 print_rtl (loop_dump_stream, mem);
11069 fputc ('\n', loop_dump_stream);
11072 /* Attempt a bit of copy propagation. This helps untangle the
11073 data flow, and enables {basic,general}_induction_var to find
11074 more bivs/givs. */
11075 EXECUTE_IF_SET_IN_REG_SET
11076 (&load_copies, FIRST_PSEUDO_REGISTER, j, rsi)
11078 try_copy_prop (loop, reg, j);
11080 CLEAR_REG_SET (&load_copies);
11082 EXECUTE_IF_SET_IN_REG_SET
11083 (&store_copies, FIRST_PSEUDO_REGISTER, j, rsi)
11085 try_swap_copy_prop (loop, reg, j);
11087 CLEAR_REG_SET (&store_copies);
11091 /* Now, we need to replace all references to the previous exit
11092 label with the new one. */
11093 if (label != NULL_RTX && end_label != NULL_RTX)
11094 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
11095 if (JUMP_P (p) && JUMP_LABEL (p) == end_label)
11096 redirect_jump (p, label, false);
11098 cselib_finish ();
11101 /* For communication between note_reg_stored and its caller. */
11102 struct note_reg_stored_arg
11104 int set_seen;
11105 rtx reg;
11108 /* Called via note_stores, record in SET_SEEN whether X, which is written,
11109 is equal to ARG. */
11110 static void
11111 note_reg_stored (rtx x, rtx setter ATTRIBUTE_UNUSED, void *arg)
11113 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
11114 if (t->reg == x)
11115 t->set_seen = 1;
11118 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
11119 There must be exactly one insn that sets this pseudo; it will be
11120 deleted if all replacements succeed and we can prove that the register
11121 is not used after the loop. */
11123 static void
11124 try_copy_prop (const struct loop *loop, rtx replacement, unsigned int regno)
11126 /* This is the reg that we are copying from. */
11127 rtx reg_rtx = regno_reg_rtx[regno];
11128 rtx init_insn = 0;
11129 rtx insn;
11130 /* These help keep track of whether we replaced all uses of the reg. */
11131 int replaced_last = 0;
11132 int store_is_first = 0;
11134 for (insn = next_insn_in_loop (loop, loop->scan_start);
11135 insn != NULL_RTX;
11136 insn = next_insn_in_loop (loop, insn))
11138 rtx set;
11140 /* Only substitute within one extended basic block from the initializing
11141 insn. */
11142 if (LABEL_P (insn) && init_insn)
11143 break;
11145 if (! INSN_P (insn))
11146 continue;
11148 /* Is this the initializing insn? */
11149 set = single_set (insn);
11150 if (set
11151 && REG_P (SET_DEST (set))
11152 && REGNO (SET_DEST (set)) == regno)
11154 gcc_assert (!init_insn);
11156 init_insn = insn;
11157 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
11158 store_is_first = 1;
11161 /* Only substitute after seeing the initializing insn. */
11162 if (init_insn && insn != init_insn)
11164 struct note_reg_stored_arg arg;
11166 replace_loop_regs (insn, reg_rtx, replacement);
11167 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
11168 replaced_last = 1;
11170 /* Stop replacing when REPLACEMENT is modified. */
11171 arg.reg = replacement;
11172 arg.set_seen = 0;
11173 note_stores (PATTERN (insn), note_reg_stored, &arg);
11174 if (arg.set_seen)
11176 rtx note = find_reg_note (insn, REG_EQUAL, NULL);
11178 /* It is possible that we've turned previously valid REG_EQUAL to
11179 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
11180 REPLACEMENT is modified, we get different meaning. */
11181 if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
11182 remove_note (insn, note);
11183 break;
11187 gcc_assert (init_insn);
11188 if (apply_change_group ())
11190 if (loop_dump_stream)
11191 fprintf (loop_dump_stream, " Replaced reg %d", regno);
11192 if (store_is_first && replaced_last)
11194 rtx first;
11195 rtx retval_note;
11197 /* Assume we're just deleting INIT_INSN. */
11198 first = init_insn;
11199 /* Look for REG_RETVAL note. If we're deleting the end of
11200 the libcall sequence, the whole sequence can go. */
11201 retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
11202 /* If we found a REG_RETVAL note, find the first instruction
11203 in the sequence. */
11204 if (retval_note)
11205 first = XEXP (retval_note, 0);
11207 /* Delete the instructions. */
11208 loop_delete_insns (first, init_insn);
11210 if (loop_dump_stream)
11211 fprintf (loop_dump_stream, ".\n");
11215 /* Replace all the instructions from FIRST up to and including LAST
11216 with NOTE_INSN_DELETED notes. */
11218 static void
11219 loop_delete_insns (rtx first, rtx last)
11221 while (1)
11223 if (loop_dump_stream)
11224 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
11225 INSN_UID (first));
11226 delete_insn (first);
11228 /* If this was the LAST instructions we're supposed to delete,
11229 we're done. */
11230 if (first == last)
11231 break;
11233 first = NEXT_INSN (first);
11237 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
11238 loop LOOP if the order of the sets of these registers can be
11239 swapped. There must be exactly one insn within the loop that sets
11240 this pseudo followed immediately by a move insn that sets
11241 REPLACEMENT with REGNO. */
11242 static void
11243 try_swap_copy_prop (const struct loop *loop, rtx replacement,
11244 unsigned int regno)
11246 rtx insn;
11247 rtx set = NULL_RTX;
11248 unsigned int new_regno;
11250 new_regno = REGNO (replacement);
11252 for (insn = next_insn_in_loop (loop, loop->scan_start);
11253 insn != NULL_RTX;
11254 insn = next_insn_in_loop (loop, insn))
11256 /* Search for the insn that copies REGNO to NEW_REGNO? */
11257 if (INSN_P (insn)
11258 && (set = single_set (insn))
11259 && REG_P (SET_DEST (set))
11260 && REGNO (SET_DEST (set)) == new_regno
11261 && REG_P (SET_SRC (set))
11262 && REGNO (SET_SRC (set)) == regno)
11263 break;
11266 if (insn != NULL_RTX)
11268 rtx prev_insn;
11269 rtx prev_set;
11271 /* Some DEF-USE info would come in handy here to make this
11272 function more general. For now, just check the previous insn
11273 which is the most likely candidate for setting REGNO. */
11275 prev_insn = PREV_INSN (insn);
11277 if (INSN_P (insn)
11278 && (prev_set = single_set (prev_insn))
11279 && REG_P (SET_DEST (prev_set))
11280 && REGNO (SET_DEST (prev_set)) == regno)
11282 /* We have:
11283 (set (reg regno) (expr))
11284 (set (reg new_regno) (reg regno))
11286 so try converting this to:
11287 (set (reg new_regno) (expr))
11288 (set (reg regno) (reg new_regno))
11290 The former construct is often generated when a global
11291 variable used for an induction variable is shadowed by a
11292 register (NEW_REGNO). The latter construct improves the
11293 chances of GIV replacement and BIV elimination. */
11295 validate_change (prev_insn, &SET_DEST (prev_set),
11296 replacement, 1);
11297 validate_change (insn, &SET_DEST (set),
11298 SET_SRC (set), 1);
11299 validate_change (insn, &SET_SRC (set),
11300 replacement, 1);
11302 if (apply_change_group ())
11304 if (loop_dump_stream)
11305 fprintf (loop_dump_stream,
11306 " Swapped set of reg %d at %d with reg %d at %d.\n",
11307 regno, INSN_UID (insn),
11308 new_regno, INSN_UID (prev_insn));
11310 /* Update first use of REGNO. */
11311 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
11312 REGNO_FIRST_UID (regno) = INSN_UID (insn);
11314 /* Now perform copy propagation to hopefully
11315 remove all uses of REGNO within the loop. */
11316 try_copy_prop (loop, replacement, regno);
11322 /* Worker function for find_mem_in_note, called via for_each_rtx. */
11324 static int
11325 find_mem_in_note_1 (rtx *x, void *data)
11327 if (*x != NULL_RTX && MEM_P (*x))
11329 rtx *res = (rtx *) data;
11330 *res = *x;
11331 return 1;
11333 return 0;
11336 /* Returns the first MEM found in NOTE by depth-first search. */
11338 static rtx
11339 find_mem_in_note (rtx note)
11341 if (note && for_each_rtx (&note, find_mem_in_note_1, &note))
11342 return note;
11343 return NULL_RTX;
11346 /* Replace MEM with its associated pseudo register. This function is
11347 called from load_mems via for_each_rtx. DATA is actually a pointer
11348 to a structure describing the instruction currently being scanned
11349 and the MEM we are currently replacing. */
11351 static int
11352 replace_loop_mem (rtx *mem, void *data)
11354 loop_replace_args *args = (loop_replace_args *) data;
11355 rtx m = *mem;
11357 if (m == NULL_RTX)
11358 return 0;
11360 switch (GET_CODE (m))
11362 case MEM:
11363 break;
11365 case CONST_DOUBLE:
11366 /* We're not interested in the MEM associated with a
11367 CONST_DOUBLE, so there's no need to traverse into one. */
11368 return -1;
11370 default:
11371 /* This is not a MEM. */
11372 return 0;
11375 if (!rtx_equal_p (args->match, m))
11376 /* This is not the MEM we are currently replacing. */
11377 return 0;
11379 /* Actually replace the MEM. */
11380 validate_change (args->insn, mem, args->replacement, 1);
11382 return 0;
11385 static void
11386 replace_loop_mems (rtx insn, rtx mem, rtx reg, int written)
11388 loop_replace_args args;
11390 args.insn = insn;
11391 args.match = mem;
11392 args.replacement = reg;
11394 for_each_rtx (&insn, replace_loop_mem, &args);
11396 /* If we hoist a mem write out of the loop, then REG_EQUAL
11397 notes referring to the mem are no longer valid. */
11398 if (written)
11400 rtx note, sub;
11401 rtx *link;
11403 for (link = &REG_NOTES (insn); (note = *link); link = &XEXP (note, 1))
11405 if (REG_NOTE_KIND (note) == REG_EQUAL
11406 && (sub = find_mem_in_note (note))
11407 && true_dependence (mem, VOIDmode, sub, rtx_varies_p))
11409 /* Remove the note. */
11410 validate_change (NULL_RTX, link, XEXP (note, 1), 1);
11411 break;
11417 /* Replace one register with another. Called through for_each_rtx; PX points
11418 to the rtx being scanned. DATA is actually a pointer to
11419 a structure of arguments. */
11421 static int
11422 replace_loop_reg (rtx *px, void *data)
11424 rtx x = *px;
11425 loop_replace_args *args = (loop_replace_args *) data;
11427 if (x == NULL_RTX)
11428 return 0;
11430 if (x == args->match)
11431 validate_change (args->insn, px, args->replacement, 1);
11433 return 0;
11436 static void
11437 replace_loop_regs (rtx insn, rtx reg, rtx replacement)
11439 loop_replace_args args;
11441 args.insn = insn;
11442 args.match = reg;
11443 args.replacement = replacement;
11445 for_each_rtx (&insn, replace_loop_reg, &args);
11448 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
11449 (ignored in the interim). */
11451 static rtx
11452 loop_insn_emit_after (const struct loop *loop ATTRIBUTE_UNUSED,
11453 basic_block where_bb ATTRIBUTE_UNUSED, rtx where_insn,
11454 rtx pattern)
11456 return emit_insn_after (pattern, where_insn);
11460 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
11461 in basic block WHERE_BB (ignored in the interim) within the loop
11462 otherwise hoist PATTERN into the loop pre-header. */
11464 static rtx
11465 loop_insn_emit_before (const struct loop *loop,
11466 basic_block where_bb ATTRIBUTE_UNUSED,
11467 rtx where_insn, rtx pattern)
11469 if (! where_insn)
11470 return loop_insn_hoist (loop, pattern);
11471 return emit_insn_before (pattern, where_insn);
11475 /* Emit call insn for PATTERN before WHERE_INSN in basic block
11476 WHERE_BB (ignored in the interim) within the loop. */
11478 static rtx
11479 loop_call_insn_emit_before (const struct loop *loop ATTRIBUTE_UNUSED,
11480 basic_block where_bb ATTRIBUTE_UNUSED,
11481 rtx where_insn, rtx pattern)
11483 return emit_call_insn_before (pattern, where_insn);
11487 /* Hoist insn for PATTERN into the loop pre-header. */
11489 static rtx
11490 loop_insn_hoist (const struct loop *loop, rtx pattern)
11492 return loop_insn_emit_before (loop, 0, loop->start, pattern);
11496 /* Hoist call insn for PATTERN into the loop pre-header. */
11498 static rtx
11499 loop_call_insn_hoist (const struct loop *loop, rtx pattern)
11501 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
11505 /* Sink insn for PATTERN after the loop end. */
11507 static rtx
11508 loop_insn_sink (const struct loop *loop, rtx pattern)
11510 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
11513 /* bl->final_value can be either general_operand or PLUS of general_operand
11514 and constant. Emit sequence of instructions to load it into REG. */
11515 static rtx
11516 gen_load_of_final_value (rtx reg, rtx final_value)
11518 rtx seq;
11519 start_sequence ();
11520 final_value = force_operand (final_value, reg);
11521 if (final_value != reg)
11522 emit_move_insn (reg, final_value);
11523 seq = get_insns ();
11524 end_sequence ();
11525 return seq;
11528 /* If the loop has multiple exits, emit insn for PATTERN before the
11529 loop to ensure that it will always be executed no matter how the
11530 loop exits. Otherwise, emit the insn for PATTERN after the loop,
11531 since this is slightly more efficient. */
11533 static rtx
11534 loop_insn_sink_or_swim (const struct loop *loop, rtx pattern)
11536 if (loop->exit_count)
11537 return loop_insn_hoist (loop, pattern);
11538 else
11539 return loop_insn_sink (loop, pattern);
11542 static void
11543 loop_ivs_dump (const struct loop *loop, FILE *file, int verbose)
11545 struct iv_class *bl;
11546 int iv_num = 0;
11548 if (! loop || ! file)
11549 return;
11551 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
11552 iv_num++;
11554 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
11556 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
11558 loop_iv_class_dump (bl, file, verbose);
11559 fputc ('\n', file);
11564 static void
11565 loop_iv_class_dump (const struct iv_class *bl, FILE *file,
11566 int verbose ATTRIBUTE_UNUSED)
11568 struct induction *v;
11569 rtx incr;
11570 int i;
11572 if (! bl || ! file)
11573 return;
11575 fprintf (file, "IV class for reg %d, benefit %d\n",
11576 bl->regno, bl->total_benefit);
11578 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
11579 if (bl->initial_value)
11581 fprintf (file, ", init val: ");
11582 print_simple_rtl (file, bl->initial_value);
11584 if (bl->initial_test)
11586 fprintf (file, ", init test: ");
11587 print_simple_rtl (file, bl->initial_test);
11589 fputc ('\n', file);
11591 if (bl->final_value)
11593 fprintf (file, " Final val: ");
11594 print_simple_rtl (file, bl->final_value);
11595 fputc ('\n', file);
11598 if ((incr = biv_total_increment (bl)))
11600 fprintf (file, " Total increment: ");
11601 print_simple_rtl (file, incr);
11602 fputc ('\n', file);
11605 /* List the increments. */
11606 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
11608 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
11609 print_simple_rtl (file, v->add_val);
11610 fputc ('\n', file);
11613 /* List the givs. */
11614 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
11616 fprintf (file, " Giv%d: insn %d, benefit %d, ",
11617 i, INSN_UID (v->insn), v->benefit);
11618 if (v->giv_type == DEST_ADDR)
11619 print_simple_rtl (file, v->mem);
11620 else
11621 print_simple_rtl (file, single_set (v->insn));
11622 fputc ('\n', file);
11627 static void
11628 loop_biv_dump (const struct induction *v, FILE *file, int verbose)
11630 if (! v || ! file)
11631 return;
11633 fprintf (file,
11634 "Biv %d: insn %d",
11635 REGNO (v->dest_reg), INSN_UID (v->insn));
11636 fprintf (file, " const ");
11637 print_simple_rtl (file, v->add_val);
11639 if (verbose && v->final_value)
11641 fputc ('\n', file);
11642 fprintf (file, " final ");
11643 print_simple_rtl (file, v->final_value);
11646 fputc ('\n', file);
11650 static void
11651 loop_giv_dump (const struct induction *v, FILE *file, int verbose)
11653 if (! v || ! file)
11654 return;
11656 if (v->giv_type == DEST_REG)
11657 fprintf (file, "Giv %d: insn %d",
11658 REGNO (v->dest_reg), INSN_UID (v->insn));
11659 else
11660 fprintf (file, "Dest address: insn %d",
11661 INSN_UID (v->insn));
11663 fprintf (file, " src reg %d benefit %d",
11664 REGNO (v->src_reg), v->benefit);
11665 fprintf (file, " lifetime %d",
11666 v->lifetime);
11668 if (v->replaceable)
11669 fprintf (file, " replaceable");
11671 if (v->no_const_addval)
11672 fprintf (file, " ncav");
11674 if (v->ext_dependent)
11676 switch (GET_CODE (v->ext_dependent))
11678 case SIGN_EXTEND:
11679 fprintf (file, " ext se");
11680 break;
11681 case ZERO_EXTEND:
11682 fprintf (file, " ext ze");
11683 break;
11684 case TRUNCATE:
11685 fprintf (file, " ext tr");
11686 break;
11687 default:
11688 gcc_unreachable ();
11692 fputc ('\n', file);
11693 fprintf (file, " mult ");
11694 print_simple_rtl (file, v->mult_val);
11696 fputc ('\n', file);
11697 fprintf (file, " add ");
11698 print_simple_rtl (file, v->add_val);
11700 if (verbose && v->final_value)
11702 fputc ('\n', file);
11703 fprintf (file, " final ");
11704 print_simple_rtl (file, v->final_value);
11707 fputc ('\n', file);
11711 void
11712 debug_ivs (const struct loop *loop)
11714 loop_ivs_dump (loop, stderr, 1);
11718 void
11719 debug_iv_class (const struct iv_class *bl)
11721 loop_iv_class_dump (bl, stderr, 1);
11725 void
11726 debug_biv (const struct induction *v)
11728 loop_biv_dump (v, stderr, 1);
11732 void
11733 debug_giv (const struct induction *v)
11735 loop_giv_dump (v, stderr, 1);
11739 #define LOOP_BLOCK_NUM_1(INSN) \
11740 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
11742 /* The notes do not have an assigned block, so look at the next insn. */
11743 #define LOOP_BLOCK_NUM(INSN) \
11744 ((INSN) ? (NOTE_P (INSN) \
11745 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
11746 : LOOP_BLOCK_NUM_1 (INSN)) \
11747 : -1)
11749 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
11751 static void
11752 loop_dump_aux (const struct loop *loop, FILE *file,
11753 int verbose ATTRIBUTE_UNUSED)
11755 rtx label;
11757 if (! loop || ! file || !BB_HEAD (loop->first))
11758 return;
11760 /* Print diagnostics to compare our concept of a loop with
11761 what the loop notes say. */
11762 if (! PREV_INSN (BB_HEAD (loop->first))
11763 || !NOTE_P (PREV_INSN (BB_HEAD (loop->first)))
11764 || NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (loop->first)))
11765 != NOTE_INSN_LOOP_BEG)
11766 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
11767 INSN_UID (PREV_INSN (BB_HEAD (loop->first))));
11768 if (! NEXT_INSN (BB_END (loop->last))
11769 || !NOTE_P (NEXT_INSN (BB_END (loop->last)))
11770 || NOTE_LINE_NUMBER (NEXT_INSN (BB_END (loop->last)))
11771 != NOTE_INSN_LOOP_END)
11772 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
11773 INSN_UID (NEXT_INSN (BB_END (loop->last))));
11775 if (loop->start)
11777 fprintf (file,
11778 ";; start %d (%d), end %d (%d)\n",
11779 LOOP_BLOCK_NUM (loop->start),
11780 LOOP_INSN_UID (loop->start),
11781 LOOP_BLOCK_NUM (loop->end),
11782 LOOP_INSN_UID (loop->end));
11783 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
11784 LOOP_BLOCK_NUM (loop->top),
11785 LOOP_INSN_UID (loop->top),
11786 LOOP_BLOCK_NUM (loop->scan_start),
11787 LOOP_INSN_UID (loop->scan_start));
11788 fprintf (file, ";; exit_count %d", loop->exit_count);
11789 if (loop->exit_count)
11791 fputs (", labels:", file);
11792 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
11794 fprintf (file, " %d ",
11795 LOOP_INSN_UID (XEXP (label, 0)));
11798 fputs ("\n", file);
11802 /* Call this function from the debugger to dump LOOP. */
11804 void
11805 debug_loop (const struct loop *loop)
11807 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
11810 /* Call this function from the debugger to dump LOOPS. */
11812 void
11813 debug_loops (const struct loops *loops)
11815 flow_loops_dump (loops, stderr, loop_dump_aux, 1);
11818 static bool
11819 gate_handle_loop_optimize (void)
11821 return (optimize > 0 && flag_loop_optimize);
11824 /* Move constant computations out of loops. */
11825 static void
11826 rest_of_handle_loop_optimize (void)
11828 int do_prefetch;
11830 /* CFG is no longer maintained up-to-date. */
11831 free_bb_for_insn ();
11832 profile_status = PROFILE_ABSENT;
11834 do_prefetch = flag_prefetch_loop_arrays ? LOOP_PREFETCH : 0;
11836 if (flag_rerun_loop_opt)
11838 cleanup_barriers ();
11840 /* We only want to perform unrolling once. */
11841 loop_optimize (get_insns (), dump_file, 0);
11843 /* The first call to loop_optimize makes some instructions
11844 trivially dead. We delete those instructions now in the
11845 hope that doing so will make the heuristics in loop work
11846 better and possibly speed up compilation. */
11847 delete_trivially_dead_insns (get_insns (), max_reg_num ());
11849 /* The regscan pass is currently necessary as the alias
11850 analysis code depends on this information. */
11851 reg_scan (get_insns (), max_reg_num ());
11853 cleanup_barriers ();
11854 loop_optimize (get_insns (), dump_file, do_prefetch);
11856 /* Loop can create trivially dead instructions. */
11857 delete_trivially_dead_insns (get_insns (), max_reg_num ());
11858 find_basic_blocks (get_insns ());
11861 struct tree_opt_pass pass_loop_optimize =
11863 "old-loop", /* name */
11864 gate_handle_loop_optimize, /* gate */
11865 rest_of_handle_loop_optimize, /* execute */
11866 NULL, /* sub */
11867 NULL, /* next */
11868 0, /* static_pass_number */
11869 TV_LOOP, /* tv_id */
11870 0, /* properties_required */
11871 0, /* properties_provided */
11872 0, /* properties_destroyed */
11873 0, /* todo_flags_start */
11874 TODO_dump_func |
11875 TODO_ggc_collect, /* todo_flags_finish */
11876 'L' /* letter */