Merge from mainline
[official-gcc.git] / gcc / loop.c
blob1beb4dc16a6ae8ff37791a540426bcb03a1548b4
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 /* Forward declarations. */
595 static void invalidate_loops_containing_label (rtx);
596 static void find_and_verify_loops (rtx, struct loops *);
597 static void mark_loop_jump (rtx, struct loop *);
598 static void prescan_loop (struct loop *);
599 static int reg_in_basic_block_p (rtx, rtx);
600 static int consec_sets_invariant_p (const struct loop *, rtx, int, rtx);
601 static int labels_in_range_p (rtx, int);
602 static void count_one_set (struct loop_regs *, rtx, rtx, rtx *);
603 static void note_addr_stored (rtx, rtx, void *);
604 static void note_set_pseudo_multiple_uses (rtx, rtx, void *);
605 static int loop_reg_used_before_p (const struct loop *, rtx, rtx);
606 static rtx find_regs_nested (rtx, rtx);
607 static void scan_loop (struct loop*, int);
608 #if 0
609 static void replace_call_address (rtx, rtx, rtx);
610 #endif
611 static rtx skip_consec_insns (rtx, int);
612 static int libcall_benefit (rtx);
613 static rtx libcall_other_reg (rtx, rtx);
614 static void record_excess_regs (rtx, rtx, rtx *);
615 static void ignore_some_movables (struct loop_movables *);
616 static void force_movables (struct loop_movables *);
617 static void combine_movables (struct loop_movables *, struct loop_regs *);
618 static int num_unmoved_movables (const struct loop *);
619 static int regs_match_p (rtx, rtx, struct loop_movables *);
620 static int rtx_equal_for_loop_p (rtx, rtx, struct loop_movables *,
621 struct loop_regs *);
622 static void add_label_notes (rtx, rtx);
623 static void move_movables (struct loop *loop, struct loop_movables *, int,
624 int);
625 static void loop_movables_add (struct loop_movables *, struct movable *);
626 static void loop_movables_free (struct loop_movables *);
627 static int count_nonfixed_reads (const struct loop *, rtx);
628 static void loop_bivs_find (struct loop *);
629 static void loop_bivs_init_find (struct loop *);
630 static void loop_bivs_check (struct loop *);
631 static void loop_givs_find (struct loop *);
632 static void loop_givs_check (struct loop *);
633 static int loop_biv_eliminable_p (struct loop *, struct iv_class *, int, int);
634 static int loop_giv_reduce_benefit (struct loop *, struct iv_class *,
635 struct induction *, rtx);
636 static void loop_givs_dead_check (struct loop *, struct iv_class *);
637 static void loop_givs_reduce (struct loop *, struct iv_class *);
638 static void loop_givs_rescan (struct loop *, struct iv_class *, rtx *);
639 static void loop_ivs_free (struct loop *);
640 static void strength_reduce (struct loop *, int);
641 static void find_single_use_in_loop (struct loop_regs *, rtx, rtx);
642 static int valid_initial_value_p (rtx, rtx, int, rtx);
643 static void find_mem_givs (const struct loop *, rtx, rtx, int, int);
644 static void record_biv (struct loop *, struct induction *, rtx, rtx, rtx,
645 rtx, rtx *, int, int);
646 static void check_final_value (const struct loop *, struct induction *);
647 static void loop_ivs_dump (const struct loop *, FILE *, int);
648 static void loop_iv_class_dump (const struct iv_class *, FILE *, int);
649 static void loop_biv_dump (const struct induction *, FILE *, int);
650 static void loop_giv_dump (const struct induction *, FILE *, int);
651 static void record_giv (const struct loop *, struct induction *, rtx, rtx,
652 rtx, rtx, rtx, rtx, int, enum g_types, int, int,
653 rtx *);
654 static void update_giv_derive (const struct loop *, rtx);
655 static HOST_WIDE_INT get_monotonic_increment (struct iv_class *);
656 static bool biased_biv_fits_mode_p (const struct loop *, struct iv_class *,
657 HOST_WIDE_INT, enum machine_mode,
658 unsigned HOST_WIDE_INT);
659 static bool biv_fits_mode_p (const struct loop *, struct iv_class *,
660 HOST_WIDE_INT, enum machine_mode, bool);
661 static bool extension_within_bounds_p (const struct loop *, struct iv_class *,
662 HOST_WIDE_INT, rtx);
663 static void check_ext_dependent_givs (const struct loop *, struct iv_class *);
664 static int basic_induction_var (const struct loop *, rtx, enum machine_mode,
665 rtx, rtx, rtx *, rtx *, rtx **);
666 static rtx simplify_giv_expr (const struct loop *, rtx, rtx *, int *);
667 static int general_induction_var (const struct loop *loop, rtx, rtx *, rtx *,
668 rtx *, rtx *, int, int *, enum machine_mode);
669 static int consec_sets_giv (const struct loop *, int, rtx, rtx, rtx, rtx *,
670 rtx *, rtx *, rtx *);
671 static int check_dbra_loop (struct loop *, int);
672 static rtx express_from_1 (rtx, rtx, rtx);
673 static rtx combine_givs_p (struct induction *, struct induction *);
674 static int cmp_combine_givs_stats (const void *, const void *);
675 static void combine_givs (struct loop_regs *, struct iv_class *);
676 static int product_cheap_p (rtx, rtx);
677 static int maybe_eliminate_biv (const struct loop *, struct iv_class *, int,
678 int, int);
679 static int maybe_eliminate_biv_1 (const struct loop *, rtx, rtx,
680 struct iv_class *, int, basic_block, rtx);
681 static int last_use_this_basic_block (rtx, rtx);
682 static void record_initial (rtx, rtx, void *);
683 static void update_reg_last_use (rtx, rtx);
684 static rtx next_insn_in_loop (const struct loop *, rtx);
685 static void loop_regs_scan (const struct loop *, int);
686 static int count_insns_in_loop (const struct loop *);
687 static int find_mem_in_note_1 (rtx *, void *);
688 static rtx find_mem_in_note (rtx);
689 static void load_mems (const struct loop *);
690 static int insert_loop_mem (rtx *, void *);
691 static int replace_loop_mem (rtx *, void *);
692 static void replace_loop_mems (rtx, rtx, rtx, int);
693 static int replace_loop_reg (rtx *, void *);
694 static void replace_loop_regs (rtx insn, rtx, rtx);
695 static void note_reg_stored (rtx, rtx, void *);
696 static void try_copy_prop (const struct loop *, rtx, unsigned int);
697 static void try_swap_copy_prop (const struct loop *, rtx, unsigned int);
698 static rtx check_insn_for_givs (struct loop *, rtx, int, int);
699 static rtx check_insn_for_bivs (struct loop *, rtx, int, int);
700 static rtx gen_add_mult (rtx, rtx, rtx, rtx);
701 static void loop_regs_update (const struct loop *, rtx);
702 static int iv_add_mult_cost (rtx, rtx, rtx, rtx);
703 static int loop_invariant_p (const struct loop *, rtx);
704 static rtx loop_insn_hoist (const struct loop *, rtx);
705 static void loop_iv_add_mult_emit_before (const struct loop *, rtx, rtx, rtx,
706 rtx, basic_block, rtx);
707 static rtx loop_insn_emit_before (const struct loop *, basic_block,
708 rtx, rtx);
709 static int loop_insn_first_p (rtx, rtx);
710 static rtx get_condition_for_loop (const struct loop *, rtx);
711 static void loop_iv_add_mult_sink (const struct loop *, rtx, rtx, rtx, rtx);
712 static void loop_iv_add_mult_hoist (const struct loop *, rtx, rtx, rtx, rtx);
713 static rtx extend_value_for_giv (struct induction *, rtx);
714 static rtx loop_insn_sink (const struct loop *, rtx);
716 static rtx loop_insn_emit_after (const struct loop *, basic_block, rtx, rtx);
717 static rtx loop_call_insn_emit_before (const struct loop *, basic_block,
718 rtx, rtx);
719 static rtx loop_call_insn_hoist (const struct loop *, rtx);
720 static rtx loop_insn_sink_or_swim (const struct loop *, rtx);
722 static void loop_dump_aux (const struct loop *, FILE *, int);
723 static void loop_delete_insns (rtx, rtx);
724 static HOST_WIDE_INT remove_constant_addition (rtx *);
725 static rtx gen_load_of_final_value (rtx, rtx);
726 void debug_ivs (const struct loop *);
727 void debug_iv_class (const struct iv_class *);
728 void debug_biv (const struct induction *);
729 void debug_giv (const struct induction *);
730 void debug_loop (const struct loop *);
731 void debug_loops (const struct loops *);
733 typedef struct loop_replace_args
735 rtx match;
736 rtx replacement;
737 rtx insn;
738 } loop_replace_args;
740 /* Nonzero iff INSN is between START and END, inclusive. */
741 #define INSN_IN_RANGE_P(INSN, START, END) \
742 (INSN_UID (INSN) < max_uid_for_loop \
743 && INSN_LUID (INSN) >= INSN_LUID (START) \
744 && INSN_LUID (INSN) <= INSN_LUID (END))
746 /* Indirect_jump_in_function is computed once per function. */
747 static int indirect_jump_in_function;
748 static int indirect_jump_in_function_p (rtx);
750 static int compute_luids (rtx, rtx, int);
752 static int biv_elimination_giv_has_0_offset (struct induction *,
753 struct induction *, rtx);
755 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
756 copy the value of the strength reduced giv to its original register. */
757 static int copy_cost;
759 /* Cost of using a register, to normalize the benefits of a giv. */
760 static int reg_address_cost;
762 void
763 init_loop (void)
765 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
767 reg_address_cost = address_cost (reg, SImode);
769 copy_cost = COSTS_N_INSNS (1);
772 /* Compute the mapping from uids to luids.
773 LUIDs are numbers assigned to insns, like uids,
774 except that luids increase monotonically through the code.
775 Start at insn START and stop just before END. Assign LUIDs
776 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
777 static int
778 compute_luids (rtx start, rtx end, int prev_luid)
780 int i;
781 rtx insn;
783 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
785 if (INSN_UID (insn) >= max_uid_for_loop)
786 continue;
787 /* Don't assign luids to line-number NOTEs, so that the distance in
788 luids between two insns is not affected by -g. */
789 if (!NOTE_P (insn)
790 || NOTE_LINE_NUMBER (insn) <= 0)
791 uid_luid[INSN_UID (insn)] = ++i;
792 else
793 /* Give a line number note the same luid as preceding insn. */
794 uid_luid[INSN_UID (insn)] = i;
796 return i + 1;
799 /* Entry point of this file. Perform loop optimization
800 on the current function. F is the first insn of the function. */
802 static void
803 loop_optimize (rtx f, int flags)
805 rtx insn;
806 int i;
807 struct loops loops_data;
808 struct loops *loops = &loops_data;
809 struct loop_info *loops_info;
811 init_recog_no_volatile ();
813 max_reg_before_loop = max_reg_num ();
814 loop_max_reg = max_reg_before_loop;
816 regs_may_share = 0;
818 /* Count the number of loops. */
820 max_loop_num = 0;
821 for (insn = f; insn; insn = NEXT_INSN (insn))
823 if (NOTE_P (insn)
824 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
825 max_loop_num++;
828 /* Don't waste time if no loops. */
829 if (max_loop_num == 0)
830 return;
832 loops->num = max_loop_num;
834 /* Get size to use for tables indexed by uids.
835 Leave some space for labels allocated by find_and_verify_loops. */
836 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
838 uid_luid = XCNEWVEC (int, max_uid_for_loop);
839 uid_loop = XCNEWVEC (struct loop *, max_uid_for_loop);
841 /* Allocate storage for array of loops. */
842 loops->array = XCNEWVEC (struct loop, loops->num);
844 /* Find and process each loop.
845 First, find them, and record them in order of their beginnings. */
846 find_and_verify_loops (f, loops);
848 /* Allocate and initialize auxiliary loop information. */
849 loops_info = XCNEWVEC (struct loop_info, loops->num);
850 for (i = 0; i < (int) loops->num; i++)
851 loops->array[i].aux = loops_info + i;
853 /* Now find all register lifetimes. This must be done after
854 find_and_verify_loops, because it might reorder the insns in the
855 function. */
856 reg_scan (f, max_reg_before_loop);
858 /* This must occur after reg_scan so that registers created by gcse
859 will have entries in the register tables.
861 We could have added a call to reg_scan after gcse_main in toplev.c,
862 but moving this call to init_alias_analysis is more efficient. */
863 init_alias_analysis ();
865 /* See if we went too far. Note that get_max_uid already returns
866 one more that the maximum uid of all insn. */
867 gcc_assert (get_max_uid () <= max_uid_for_loop);
868 /* Now reset it to the actual size we need. See above. */
869 max_uid_for_loop = get_max_uid ();
871 /* find_and_verify_loops has already called compute_luids, but it
872 might have rearranged code afterwards, so we need to recompute
873 the luids now. */
874 compute_luids (f, NULL_RTX, 0);
876 /* Don't leave gaps in uid_luid for insns that have been
877 deleted. It is possible that the first or last insn
878 using some register has been deleted by cross-jumping.
879 Make sure that uid_luid for that former insn's uid
880 points to the general area where that insn used to be. */
881 for (i = 0; i < max_uid_for_loop; i++)
883 uid_luid[0] = uid_luid[i];
884 if (uid_luid[0] != 0)
885 break;
887 for (i = 0; i < max_uid_for_loop; i++)
888 if (uid_luid[i] == 0)
889 uid_luid[i] = uid_luid[i - 1];
891 /* Determine if the function has indirect jump. On some systems
892 this prevents low overhead loop instructions from being used. */
893 indirect_jump_in_function = indirect_jump_in_function_p (f);
895 /* Now scan the loops, last ones first, since this means inner ones are done
896 before outer ones. */
897 for (i = max_loop_num - 1; i >= 0; i--)
899 struct loop *loop = &loops->array[i];
901 if (! loop->invalid && loop->end)
903 scan_loop (loop, flags);
904 ggc_collect ();
908 end_alias_analysis ();
910 /* Clean up. */
911 for (i = 0; i < (int) loops->num; i++)
912 free (loops_info[i].mems);
914 free (uid_luid);
915 free (uid_loop);
916 free (loops_info);
917 free (loops->array);
920 /* Returns the next insn, in execution order, after INSN. START and
921 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
922 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
923 insn-stream; it is used with loops that are entered near the
924 bottom. */
926 static rtx
927 next_insn_in_loop (const struct loop *loop, rtx insn)
929 insn = NEXT_INSN (insn);
931 if (insn == loop->end)
933 if (loop->top)
934 /* Go to the top of the loop, and continue there. */
935 insn = loop->top;
936 else
937 /* We're done. */
938 insn = NULL_RTX;
941 if (insn == loop->scan_start)
942 /* We're done. */
943 insn = NULL_RTX;
945 return insn;
948 /* Find any register references hidden inside X and add them to
949 the dependency list DEPS. This is used to look inside CLOBBER (MEM
950 when checking whether a PARALLEL can be pulled out of a loop. */
952 static rtx
953 find_regs_nested (rtx deps, rtx x)
955 enum rtx_code code = GET_CODE (x);
956 if (code == REG)
957 deps = gen_rtx_EXPR_LIST (VOIDmode, x, deps);
958 else
960 const char *fmt = GET_RTX_FORMAT (code);
961 int i, j;
962 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
964 if (fmt[i] == 'e')
965 deps = find_regs_nested (deps, XEXP (x, i));
966 else if (fmt[i] == 'E')
967 for (j = 0; j < XVECLEN (x, i); j++)
968 deps = find_regs_nested (deps, XVECEXP (x, i, j));
971 return deps;
974 /* Optimize one loop described by LOOP. */
976 /* ??? Could also move memory writes out of loops if the destination address
977 is invariant, the source is invariant, the memory write is not volatile,
978 and if we can prove that no read inside the loop can read this address
979 before the write occurs. If there is a read of this address after the
980 write, then we can also mark the memory read as invariant. */
982 static void
983 scan_loop (struct loop *loop, int flags)
985 struct loop_info *loop_info = LOOP_INFO (loop);
986 struct loop_regs *regs = LOOP_REGS (loop);
987 int i;
988 rtx loop_start = loop->start;
989 rtx loop_end = loop->end;
990 rtx p;
991 /* 1 if we are scanning insns that could be executed zero times. */
992 int maybe_never = 0;
993 /* 1 if we are scanning insns that might never be executed
994 due to a subroutine call which might exit before they are reached. */
995 int call_passed = 0;
996 /* Number of insns in the loop. */
997 int insn_count;
998 int tem;
999 rtx temp, update_start, update_end;
1000 /* The SET from an insn, if it is the only SET in the insn. */
1001 rtx set, set1;
1002 /* Chain describing insns movable in current loop. */
1003 struct loop_movables *movables = LOOP_MOVABLES (loop);
1004 /* Ratio of extra register life span we can justify
1005 for saving an instruction. More if loop doesn't call subroutines
1006 since in that case saving an insn makes more difference
1007 and more registers are available. */
1008 int threshold;
1009 int in_libcall;
1011 loop->top = 0;
1013 movables->head = 0;
1014 movables->last = 0;
1016 /* Determine whether this loop starts with a jump down to a test at
1017 the end. This will occur for a small number of loops with a test
1018 that is too complex to duplicate in front of the loop.
1020 We search for the first insn or label in the loop, skipping NOTEs.
1021 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
1022 (because we might have a loop executed only once that contains a
1023 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
1024 (in case we have a degenerate loop).
1026 Note that if we mistakenly think that a loop is entered at the top
1027 when, in fact, it is entered at the exit test, the only effect will be
1028 slightly poorer optimization. Making the opposite error can generate
1029 incorrect code. Since very few loops now start with a jump to the
1030 exit test, the code here to detect that case is very conservative. */
1032 for (p = NEXT_INSN (loop_start);
1033 p != loop_end
1034 && !LABEL_P (p) && ! INSN_P (p)
1035 && (!NOTE_P (p)
1036 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
1037 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
1038 p = NEXT_INSN (p))
1041 loop->scan_start = p;
1043 /* If loop end is the end of the current function, then emit a
1044 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
1045 note insn. This is the position we use when sinking insns out of
1046 the loop. */
1047 if (NEXT_INSN (loop->end) != 0)
1048 loop->sink = NEXT_INSN (loop->end);
1049 else
1050 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
1052 /* Set up variables describing this loop. */
1053 prescan_loop (loop);
1054 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
1056 /* If loop has a jump before the first label,
1057 the true entry is the target of that jump.
1058 Start scan from there.
1059 But record in LOOP->TOP the place where the end-test jumps
1060 back to so we can scan that after the end of the loop. */
1061 if (JUMP_P (p)
1062 /* Loop entry must be unconditional jump (and not a RETURN) */
1063 && any_uncondjump_p (p)
1064 && JUMP_LABEL (p) != 0
1065 /* Check to see whether the jump actually
1066 jumps out of the loop (meaning it's no loop).
1067 This case can happen for things like
1068 do {..} while (0). If this label was generated previously
1069 by loop, we can't tell anything about it and have to reject
1070 the loop. */
1071 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
1073 loop->top = next_label (loop->scan_start);
1074 loop->scan_start = JUMP_LABEL (p);
1077 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
1078 as required by loop_reg_used_before_p. So skip such loops. (This
1079 test may never be true, but it's best to play it safe.)
1081 Also, skip loops where we do not start scanning at a label. This
1082 test also rejects loops starting with a JUMP_INSN that failed the
1083 test above. */
1085 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
1086 || !LABEL_P (loop->scan_start))
1088 if (dump_file)
1089 fprintf (dump_file, "\nLoop from %d to %d is phony.\n\n",
1090 INSN_UID (loop_start), INSN_UID (loop_end));
1091 return;
1094 /* Allocate extra space for REGs that might be created by load_mems.
1095 We allocate a little extra slop as well, in the hopes that we
1096 won't have to reallocate the regs array. */
1097 loop_regs_scan (loop, loop_info->mems_idx + 16);
1098 insn_count = count_insns_in_loop (loop);
1100 if (dump_file)
1101 fprintf (dump_file, "\nLoop from %d to %d: %d real insns.\n",
1102 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
1104 /* Scan through the loop finding insns that are safe to move.
1105 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
1106 this reg will be considered invariant for subsequent insns.
1107 We consider whether subsequent insns use the reg
1108 in deciding whether it is worth actually moving.
1110 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
1111 and therefore it is possible that the insns we are scanning
1112 would never be executed. At such times, we must make sure
1113 that it is safe to execute the insn once instead of zero times.
1114 When MAYBE_NEVER is 0, all insns will be executed at least once
1115 so that is not a problem. */
1117 for (in_libcall = 0, p = next_insn_in_loop (loop, loop->scan_start);
1118 p != NULL_RTX;
1119 p = next_insn_in_loop (loop, p))
1121 if (in_libcall && INSN_P (p) && find_reg_note (p, REG_RETVAL, NULL_RTX))
1122 in_libcall--;
1123 if (NONJUMP_INSN_P (p))
1125 /* Do not scan past an optimization barrier. */
1126 if (GET_CODE (PATTERN (p)) == ASM_INPUT)
1127 break;
1128 temp = find_reg_note (p, REG_LIBCALL, NULL_RTX);
1129 if (temp)
1130 in_libcall++;
1131 if (! in_libcall
1132 && (set = single_set (p))
1133 && REG_P (SET_DEST (set))
1134 && SET_DEST (set) != frame_pointer_rtx
1135 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
1136 && SET_DEST (set) != pic_offset_table_rtx
1137 #endif
1138 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
1140 int tem1 = 0;
1141 int tem2 = 0;
1142 int move_insn = 0;
1143 int insert_temp = 0;
1144 rtx src = SET_SRC (set);
1145 rtx dependencies = 0;
1147 /* Figure out what to use as a source of this insn. If a
1148 REG_EQUIV note is given or if a REG_EQUAL note with a
1149 constant operand is specified, use it as the source and
1150 mark that we should move this insn by calling
1151 emit_move_insn rather that duplicating the insn.
1153 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
1154 note is present. */
1155 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
1156 if (temp)
1157 src = XEXP (temp, 0), move_insn = 1;
1158 else
1160 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
1161 if (temp && CONSTANT_P (XEXP (temp, 0)))
1162 src = XEXP (temp, 0), move_insn = 1;
1163 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
1165 src = XEXP (temp, 0);
1166 /* A libcall block can use regs that don't appear in
1167 the equivalent expression. To move the libcall,
1168 we must move those regs too. */
1169 dependencies = libcall_other_reg (p, src);
1173 /* For parallels, add any possible uses to the dependencies, as
1174 we can't move the insn without resolving them first.
1175 MEMs inside CLOBBERs may also reference registers; these
1176 count as implicit uses. */
1177 if (GET_CODE (PATTERN (p)) == PARALLEL)
1179 for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
1181 rtx x = XVECEXP (PATTERN (p), 0, i);
1182 if (GET_CODE (x) == USE)
1183 dependencies
1184 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
1185 dependencies);
1186 else if (GET_CODE (x) == CLOBBER
1187 && MEM_P (XEXP (x, 0)))
1188 dependencies = find_regs_nested (dependencies,
1189 XEXP (XEXP (x, 0), 0));
1193 if (/* The register is used in basic blocks other
1194 than the one where it is set (meaning that
1195 something after this point in the loop might
1196 depend on its value before the set). */
1197 ! reg_in_basic_block_p (p, SET_DEST (set))
1198 /* And the set is not guaranteed to be executed once
1199 the loop starts, or the value before the set is
1200 needed before the set occurs...
1202 ??? Note we have quadratic behavior here, mitigated
1203 by the fact that the previous test will often fail for
1204 large loops. Rather than re-scanning the entire loop
1205 each time for register usage, we should build tables
1206 of the register usage and use them here instead. */
1207 && (maybe_never
1208 || loop_reg_used_before_p (loop, set, p)))
1209 /* It is unsafe to move the set. However, it may be OK to
1210 move the source into a new pseudo, and substitute a
1211 reg-to-reg copy for the original insn.
1213 This code used to consider it OK to move a set of a variable
1214 which was not created by the user and not used in an exit
1215 test.
1216 That behavior is incorrect and was removed. */
1217 insert_temp = 1;
1219 /* Don't try to optimize a MODE_CC set with a constant
1220 source. It probably will be combined with a conditional
1221 jump. */
1222 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set))) == MODE_CC
1223 && CONSTANT_P (src))
1225 /* Don't try to optimize a register that was made
1226 by loop-optimization for an inner loop.
1227 We don't know its life-span, so we can't compute
1228 the benefit. */
1229 else if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
1231 /* Don't move the source and add a reg-to-reg copy:
1232 - with -Os (this certainly increases size),
1233 - if the mode doesn't support copy operations (obviously),
1234 - if the source is already a reg (the motion will gain nothing),
1235 - if the source is a legitimate constant (likewise),
1236 - if the dest is a hard register (may be unrecognizable). */
1237 else if (insert_temp
1238 && (optimize_size
1239 || ! can_copy_p (GET_MODE (SET_SRC (set)))
1240 || REG_P (SET_SRC (set))
1241 || (CONSTANT_P (SET_SRC (set))
1242 && LEGITIMATE_CONSTANT_P (SET_SRC (set)))
1243 || REGNO (SET_DEST (set)) < FIRST_PSEUDO_REGISTER))
1245 else if ((tem = loop_invariant_p (loop, src))
1246 && (dependencies == 0
1247 || (tem2
1248 = loop_invariant_p (loop, dependencies)) != 0)
1249 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
1250 || (tem1
1251 = consec_sets_invariant_p
1252 (loop, SET_DEST (set),
1253 regs->array[REGNO (SET_DEST (set))].set_in_loop,
1254 p)))
1255 /* If the insn can cause a trap (such as divide by zero),
1256 can't move it unless it's guaranteed to be executed
1257 once loop is entered. Even a function call might
1258 prevent the trap insn from being reached
1259 (since it might exit!) */
1260 && ! ((maybe_never || call_passed)
1261 && may_trap_p (src)))
1263 struct movable *m;
1264 int regno = REGNO (SET_DEST (set));
1265 rtx user, user_set;
1267 /* A potential lossage is where we have a case where two
1268 insns can be combined as long as they are both in the
1269 loop, but we move one of them outside the loop. For
1270 large loops, this can lose. The most common case of
1271 this is the address of a function being called.
1273 Therefore, if this register is marked as being used
1274 exactly once if we are in a loop with calls
1275 (a "large loop"), see if we can replace the usage of
1276 this register with the source of this SET. If we can,
1277 delete this insn.
1279 Don't do this if:
1280 (1) P has a REG_RETVAL note or
1281 (2) if we have SMALL_REGISTER_CLASSES and
1282 (a) SET_SRC is a hard register or
1283 (b) the destination of the user is a hard register. */
1285 if (loop_info->has_call
1286 && regno >= FIRST_PSEUDO_REGISTER
1287 && (user = regs->array[regno].single_usage) != NULL
1288 && user != const0_rtx
1289 && REGNO_FIRST_UID (regno) == INSN_UID (p)
1290 && REGNO_LAST_UID (regno) == INSN_UID (user)
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 || !HARD_REGISTER_P (SET_SRC (set)))
1298 && (!SMALL_REGISTER_CLASSES
1299 || !NONJUMP_INSN_P (user)
1300 || !(user_set = single_set (user))
1301 || !REG_P (SET_DEST (user_set))
1302 || !HARD_REGISTER_P (SET_DEST (user_set)))
1303 /* This test is not redundant; SET_SRC (set) might be
1304 a call-clobbered register and the life of REGNO
1305 might span a call. */
1306 && ! modified_between_p (SET_SRC (set), p, user)
1307 && no_labels_between_p (p, user)
1308 && validate_replace_rtx (SET_DEST (set),
1309 SET_SRC (set), user))
1311 /* Replace any usage in a REG_EQUAL note. Must copy
1312 the new source, so that we don't get rtx sharing
1313 between the SET_SOURCE and REG_NOTES of insn p. */
1314 REG_NOTES (user)
1315 = replace_rtx (REG_NOTES (user), SET_DEST (set),
1316 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 = XNEW (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 = XNEW (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 = XNEWVEC (char, 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]
1884 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1885 /* See if the source of M1 says it matches M. */
1886 && ((REG_P (m1->set_src)
1887 && matched_regs[REGNO (m1->set_src)])
1888 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1889 movables, regs))))
1890 && ((m->dependencies == m1->dependencies)
1891 || rtx_equal_p (m->dependencies, m1->dependencies)))
1893 m->lifetime += m1->lifetime;
1894 m->savings += m1->savings;
1895 m1->done = 1;
1896 m1->match = m;
1897 matched_regs[m1->regno] = 1;
1901 /* Now combine the regs used for zero-extension.
1902 This can be done for those not marked `global'
1903 provided their lives don't overlap. */
1905 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1906 mode = GET_MODE_WIDER_MODE (mode))
1908 struct movable *m0 = 0;
1910 /* Combine all the registers for extension from mode MODE.
1911 Don't combine any that are used outside this loop. */
1912 for (m = movables->head; m; m = m->next)
1913 if (m->partial && ! m->global
1914 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1916 struct movable *m1;
1918 int first = REGNO_FIRST_LUID (m->regno);
1919 int last = REGNO_LAST_LUID (m->regno);
1921 if (m0 == 0)
1923 /* First one: don't check for overlap, just record it. */
1924 m0 = m;
1925 continue;
1928 /* Make sure they extend to the same mode.
1929 (Almost always true.) */
1930 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1931 continue;
1933 /* We already have one: check for overlap with those
1934 already combined together. */
1935 for (m1 = movables->head; m1 != m; m1 = m1->next)
1936 if (m1 == m0 || (m1->partial && m1->match == m0))
1937 if (! (REGNO_FIRST_LUID (m1->regno) > last
1938 || REGNO_LAST_LUID (m1->regno) < first))
1939 goto overlap;
1941 /* No overlap: we can combine this with the others. */
1942 m0->lifetime += m->lifetime;
1943 m0->savings += m->savings;
1944 m->done = 1;
1945 m->match = m0;
1947 overlap:
1952 /* Clean up. */
1953 free (matched_regs);
1956 /* Returns the number of movable instructions in LOOP that were not
1957 moved outside the loop. */
1959 static int
1960 num_unmoved_movables (const struct loop *loop)
1962 int num = 0;
1963 struct movable *m;
1965 for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1966 if (!m->done)
1967 ++num;
1969 return num;
1973 /* Return 1 if regs X and Y will become the same if moved. */
1975 static int
1976 regs_match_p (rtx x, rtx y, struct loop_movables *movables)
1978 unsigned int xn = REGNO (x);
1979 unsigned int yn = REGNO (y);
1980 struct movable *mx, *my;
1982 for (mx = movables->head; mx; mx = mx->next)
1983 if (mx->regno == xn)
1984 break;
1986 for (my = movables->head; my; my = my->next)
1987 if (my->regno == yn)
1988 break;
1990 return (mx && my
1991 && ((mx->match == my->match && mx->match != 0)
1992 || mx->match == my
1993 || mx == my->match));
1996 /* Return 1 if X and Y are identical-looking rtx's.
1997 This is the Lisp function EQUAL for rtx arguments.
1999 If two registers are matching movables or a movable register and an
2000 equivalent constant, consider them equal. */
2002 static int
2003 rtx_equal_for_loop_p (rtx x, rtx y, struct loop_movables *movables,
2004 struct loop_regs *regs)
2006 int i;
2007 int j;
2008 struct movable *m;
2009 enum rtx_code code;
2010 const char *fmt;
2012 if (x == y)
2013 return 1;
2014 if (x == 0 || y == 0)
2015 return 0;
2017 code = GET_CODE (x);
2019 /* If we have a register and a constant, they may sometimes be
2020 equal. */
2021 if (REG_P (x) && regs->array[REGNO (x)].set_in_loop == -2
2022 && CONSTANT_P (y))
2024 for (m = movables->head; m; m = m->next)
2025 if (m->move_insn && m->regno == REGNO (x)
2026 && rtx_equal_p (m->set_src, y))
2027 return 1;
2029 else if (REG_P (y) && regs->array[REGNO (y)].set_in_loop == -2
2030 && CONSTANT_P (x))
2032 for (m = movables->head; m; m = m->next)
2033 if (m->move_insn && m->regno == REGNO (y)
2034 && rtx_equal_p (m->set_src, x))
2035 return 1;
2038 /* Otherwise, rtx's of different codes cannot be equal. */
2039 if (code != GET_CODE (y))
2040 return 0;
2042 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
2043 (REG:SI x) and (REG:HI x) are NOT equivalent. */
2045 if (GET_MODE (x) != GET_MODE (y))
2046 return 0;
2048 /* These types of rtx's can be compared nonrecursively. */
2049 switch (code)
2051 case PC:
2052 case CC0:
2053 case CONST_INT:
2054 case CONST_DOUBLE:
2055 return 0;
2057 case REG:
2058 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
2060 case LABEL_REF:
2061 return XEXP (x, 0) == XEXP (y, 0);
2062 case SYMBOL_REF:
2063 return XSTR (x, 0) == XSTR (y, 0);
2065 default:
2066 break;
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 = XCNEWVEC (rtx, nregs);
2186 char *already_moved = XCNEWVEC (char, nregs);
2188 for (m = movables->head; m; m = m->next)
2190 /* Describe this movable insn. */
2192 if (dump_file)
2194 fprintf (dump_file, "Insn %d: regno %d (life %d), ",
2195 INSN_UID (m->insn), m->regno, m->lifetime);
2196 if (m->consec > 0)
2197 fprintf (dump_file, "consec %d, ", m->consec);
2198 if (m->cond)
2199 fprintf (dump_file, "cond ");
2200 if (m->force)
2201 fprintf (dump_file, "force ");
2202 if (m->global)
2203 fprintf (dump_file, "global ");
2204 if (m->done)
2205 fprintf (dump_file, "done ");
2206 if (m->move_insn)
2207 fprintf (dump_file, "move-insn ");
2208 if (m->match)
2209 fprintf (dump_file, "matches %d ",
2210 INSN_UID (m->match->insn));
2211 if (m->forces)
2212 fprintf (dump_file, "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 (dump_file)
2241 fprintf (dump_file, "savings %d ", savings);
2243 if (regs->array[regno].moved_once && dump_file)
2244 fprintf (dump_file, "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 (dump_file)
2303 fprintf (dump_file, " 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 (dump_file)
2375 fprintf (dump_file, " 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 = XCNEWVEC (rtx, REGNO(newreg));
2535 reg_map2 [m->regno] = newreg;
2537 i1 = loop_insn_hoist (loop, copy_rtx (PATTERN (p)));
2538 replace_regs (i1, reg_map2, REGNO (newreg), 1);
2539 free (reg_map2);
2541 else
2542 i1 = loop_insn_hoist (loop, PATTERN (p));
2544 if (REG_NOTES (i1) == 0)
2546 REG_NOTES (i1) = REG_NOTES (p);
2547 REG_NOTES (p) = NULL;
2549 /* If there is a REG_EQUAL note present whose value
2550 is not loop invariant, then delete it, since it
2551 may cause problems with later optimization passes.
2552 It is possible for cse to create such notes
2553 like this as a result of record_jump_cond. */
2555 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2556 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2557 remove_note (i1, temp);
2560 if (new_start == 0)
2561 new_start = i1;
2563 if (dump_file)
2564 fprintf (dump_file, " moved to %d",
2565 INSN_UID (i1));
2567 /* If library call, now fix the REG_NOTES that contain
2568 insn pointers, namely REG_LIBCALL on FIRST
2569 and REG_RETVAL on I1. */
2570 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2572 XEXP (temp, 0) = first;
2573 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2574 XEXP (temp, 0) = i1;
2577 temp = p;
2578 delete_insn (p);
2579 p = NEXT_INSN (p);
2581 /* simplify_giv_expr expects that it can walk the insns
2582 at m->insn forwards and see this old sequence we are
2583 tossing here. delete_insn does preserve the next
2584 pointers, but when we skip over a NOTE we must fix
2585 it up. Otherwise that code walks into the non-deleted
2586 insn stream. */
2587 while (p && NOTE_P (p))
2588 p = NEXT_INSN (temp) = NEXT_INSN (p);
2590 if (m->insert_temp)
2592 rtx seq;
2593 /* Replace the original insn with a move from
2594 our newly created temp. */
2595 start_sequence ();
2596 emit_move_insn (m->set_dest, newreg);
2597 seq = get_insns ();
2598 end_sequence ();
2599 emit_insn_before (seq, p);
2603 /* The more regs we move, the less we like moving them. */
2604 threshold -= 3;
2607 m->done = 1;
2609 if (!m->insert_temp)
2611 /* Any other movable that loads the same register
2612 MUST be moved. */
2613 already_moved[regno] = 1;
2615 /* This reg has been moved out of one loop. */
2616 regs->array[regno].moved_once = 1;
2618 /* The reg set here is now invariant. */
2619 if (! m->partial)
2621 int i;
2622 for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2623 regs->array[regno+i].set_in_loop = 0;
2626 /* Change the length-of-life info for the register
2627 to say it lives at least the full length of this loop.
2628 This will help guide optimizations in outer loops. */
2630 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2631 /* This is the old insn before all the moved insns.
2632 We can't use the moved insn because it is out of range
2633 in uid_luid. Only the old insns have luids. */
2634 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2635 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2636 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2639 /* Combine with this moved insn any other matching movables. */
2641 if (! m->partial)
2642 for (m1 = movables->head; m1; m1 = m1->next)
2643 if (m1->match == m)
2645 rtx temp;
2647 reg_map[m1->regno] = m->set_dest;
2649 /* Get rid of the matching insn
2650 and prevent further processing of it. */
2651 m1->done = 1;
2653 /* If library call, delete all insns. */
2654 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2655 NULL_RTX)))
2656 delete_insn_chain (XEXP (temp, 0), m1->insn);
2657 else
2658 delete_insn (m1->insn);
2660 /* Any other movable that loads the same register
2661 MUST be moved. */
2662 already_moved[m1->regno] = 1;
2664 /* The reg merged here is now invariant,
2665 if the reg it matches is invariant. */
2666 if (! m->partial)
2668 int i;
2669 for (i = 0;
2670 i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2671 i++)
2672 regs->array[m1->regno+i].set_in_loop = 0;
2676 else if (dump_file)
2677 fprintf (dump_file, "not desirable");
2679 else if (dump_file && !m->match)
2680 fprintf (dump_file, "not safe");
2682 if (dump_file)
2683 fprintf (dump_file, "\n");
2686 if (new_start == 0)
2687 new_start = loop_start;
2689 /* Go through all the instructions in the loop, making
2690 all the register substitutions scheduled in REG_MAP. */
2691 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2692 if (INSN_P (p))
2694 replace_regs (PATTERN (p), reg_map, nregs, 0);
2695 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2696 INSN_CODE (p) = -1;
2699 /* Clean up. */
2700 free (reg_map);
2701 free (already_moved);
2705 static void
2706 loop_movables_add (struct loop_movables *movables, struct movable *m)
2708 if (movables->head == 0)
2709 movables->head = m;
2710 else
2711 movables->last->next = m;
2712 movables->last = m;
2716 static void
2717 loop_movables_free (struct loop_movables *movables)
2719 struct movable *m;
2720 struct movable *m_next;
2722 for (m = movables->head; m; m = m_next)
2724 m_next = m->next;
2725 free (m);
2729 #if 0
2730 /* Scan X and replace the address of any MEM in it with ADDR.
2731 REG is the address that MEM should have before the replacement. */
2733 static void
2734 replace_call_address (rtx x, rtx reg, rtx addr)
2736 enum rtx_code code;
2737 int i;
2738 const char *fmt;
2740 if (x == 0)
2741 return;
2742 code = GET_CODE (x);
2743 switch (code)
2745 case PC:
2746 case CC0:
2747 case CONST_INT:
2748 case CONST_DOUBLE:
2749 case CONST:
2750 case SYMBOL_REF:
2751 case LABEL_REF:
2752 case REG:
2753 return;
2755 case SET:
2756 /* Short cut for very common case. */
2757 replace_call_address (XEXP (x, 1), reg, addr);
2758 return;
2760 case CALL:
2761 /* Short cut for very common case. */
2762 replace_call_address (XEXP (x, 0), reg, addr);
2763 return;
2765 case MEM:
2766 /* If this MEM uses a reg other than the one we expected,
2767 something is wrong. */
2768 gcc_assert (XEXP (x, 0) == reg);
2769 XEXP (x, 0) = addr;
2770 return;
2772 default:
2773 break;
2776 fmt = GET_RTX_FORMAT (code);
2777 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2779 if (fmt[i] == 'e')
2780 replace_call_address (XEXP (x, i), reg, addr);
2781 else if (fmt[i] == 'E')
2783 int j;
2784 for (j = 0; j < XVECLEN (x, i); j++)
2785 replace_call_address (XVECEXP (x, i, j), reg, addr);
2789 #endif
2791 /* Return the number of memory refs to addresses that vary
2792 in the rtx X. */
2794 static int
2795 count_nonfixed_reads (const struct loop *loop, rtx x)
2797 enum rtx_code code;
2798 int i;
2799 const char *fmt;
2800 int value;
2802 if (x == 0)
2803 return 0;
2805 code = GET_CODE (x);
2806 switch (code)
2808 case PC:
2809 case CC0:
2810 case CONST_INT:
2811 case CONST_DOUBLE:
2812 case CONST:
2813 case SYMBOL_REF:
2814 case LABEL_REF:
2815 case REG:
2816 return 0;
2818 case MEM:
2819 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2820 + count_nonfixed_reads (loop, XEXP (x, 0)));
2822 default:
2823 break;
2826 value = 0;
2827 fmt = GET_RTX_FORMAT (code);
2828 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2830 if (fmt[i] == 'e')
2831 value += count_nonfixed_reads (loop, XEXP (x, i));
2832 if (fmt[i] == 'E')
2834 int j;
2835 for (j = 0; j < XVECLEN (x, i); j++)
2836 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2839 return value;
2842 /* Scan a loop setting the elements `loops_enclosed',
2843 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2844 `unknown_address_altered', `unknown_constant_address_altered', and
2845 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2846 list `store_mems' in LOOP. */
2848 static void
2849 prescan_loop (struct loop *loop)
2851 int level = 1;
2852 rtx insn;
2853 struct loop_info *loop_info = LOOP_INFO (loop);
2854 rtx start = loop->start;
2855 rtx end = loop->end;
2856 /* The label after END. Jumping here is just like falling off the
2857 end of the loop. We use next_nonnote_insn instead of next_label
2858 as a hedge against the (pathological) case where some actual insn
2859 might end up between the two. */
2860 rtx exit_target = next_nonnote_insn (end);
2862 loop_info->has_indirect_jump = indirect_jump_in_function;
2863 loop_info->pre_header_has_call = 0;
2864 loop_info->has_call = 0;
2865 loop_info->has_nonconst_call = 0;
2866 loop_info->has_prefetch = 0;
2867 loop_info->has_volatile = 0;
2868 loop_info->has_tablejump = 0;
2869 loop_info->has_multiple_exit_targets = 0;
2870 loop->level = 1;
2872 loop_info->unknown_address_altered = 0;
2873 loop_info->unknown_constant_address_altered = 0;
2874 loop_info->store_mems = NULL_RTX;
2875 loop_info->first_loop_store_insn = NULL_RTX;
2876 loop_info->mems_idx = 0;
2877 loop_info->num_mem_sets = 0;
2879 for (insn = start; insn && !LABEL_P (insn);
2880 insn = PREV_INSN (insn))
2882 if (CALL_P (insn))
2884 loop_info->pre_header_has_call = 1;
2885 break;
2889 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2890 insn = NEXT_INSN (insn))
2892 switch (GET_CODE (insn))
2894 case NOTE:
2895 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2897 ++level;
2898 /* Count number of loops contained in this one. */
2899 loop->level++;
2901 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2902 --level;
2903 break;
2905 case CALL_INSN:
2906 if (! CONST_OR_PURE_CALL_P (insn))
2908 loop_info->unknown_address_altered = 1;
2909 loop_info->has_nonconst_call = 1;
2911 else if (pure_call_p (insn))
2912 loop_info->has_nonconst_call = 1;
2913 loop_info->has_call = 1;
2914 if (can_throw_internal (insn))
2915 loop_info->has_multiple_exit_targets = 1;
2916 break;
2918 case JUMP_INSN:
2919 if (! loop_info->has_multiple_exit_targets)
2921 rtx set = pc_set (insn);
2923 if (set)
2925 rtx src = SET_SRC (set);
2926 rtx label1, label2;
2928 if (GET_CODE (src) == IF_THEN_ELSE)
2930 label1 = XEXP (src, 1);
2931 label2 = XEXP (src, 2);
2933 else
2935 label1 = src;
2936 label2 = NULL_RTX;
2941 if (label1 && label1 != pc_rtx)
2943 if (GET_CODE (label1) != LABEL_REF)
2945 /* Something tricky. */
2946 loop_info->has_multiple_exit_targets = 1;
2947 break;
2949 else if (XEXP (label1, 0) != exit_target
2950 && LABEL_OUTSIDE_LOOP_P (label1))
2952 /* A jump outside the current loop. */
2953 loop_info->has_multiple_exit_targets = 1;
2954 break;
2958 label1 = label2;
2959 label2 = NULL_RTX;
2961 while (label1);
2963 else
2965 /* A return, or something tricky. */
2966 loop_info->has_multiple_exit_targets = 1;
2969 /* Fall through. */
2971 case INSN:
2972 if (volatile_refs_p (PATTERN (insn)))
2973 loop_info->has_volatile = 1;
2975 if (JUMP_P (insn)
2976 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2977 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2978 loop_info->has_tablejump = 1;
2980 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2981 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2982 loop_info->first_loop_store_insn = insn;
2984 if (flag_non_call_exceptions && can_throw_internal (insn))
2985 loop_info->has_multiple_exit_targets = 1;
2986 break;
2988 default:
2989 break;
2993 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2994 if (/* An exception thrown by a called function might land us
2995 anywhere. */
2996 ! loop_info->has_nonconst_call
2997 /* We don't want loads for MEMs moved to a location before the
2998 one at which their stack memory becomes allocated. (Note
2999 that this is not a problem for malloc, etc., since those
3000 require actual function calls. */
3001 && ! current_function_calls_alloca
3002 /* There are ways to leave the loop other than falling off the
3003 end. */
3004 && ! loop_info->has_multiple_exit_targets)
3005 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
3006 insn = NEXT_INSN (insn))
3007 for_each_rtx (&insn, insert_loop_mem, loop_info);
3009 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
3010 that loop_invariant_p and load_mems can use true_dependence
3011 to determine what is really clobbered. */
3012 if (loop_info->unknown_address_altered)
3014 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
3016 loop_info->store_mems
3017 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
3019 if (loop_info->unknown_constant_address_altered)
3021 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
3022 MEM_READONLY_P (mem) = 1;
3023 loop_info->store_mems
3024 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
3028 /* Invalidate all loops containing LABEL. */
3030 static void
3031 invalidate_loops_containing_label (rtx label)
3033 struct loop *loop;
3034 for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
3035 loop->invalid = 1;
3038 /* Scan the function looking for loops. Record the start and end of each loop.
3039 Also mark as invalid loops any loops that contain a setjmp or are branched
3040 to from outside the loop. */
3042 static void
3043 find_and_verify_loops (rtx f, struct loops *loops)
3045 rtx insn;
3046 rtx label;
3047 int num_loops;
3048 struct loop *current_loop;
3049 struct loop *next_loop;
3050 struct loop *loop;
3052 num_loops = loops->num;
3054 compute_luids (f, NULL_RTX, 0);
3056 /* If there are jumps to undefined labels,
3057 treat them as jumps out of any/all loops.
3058 This also avoids writing past end of tables when there are no loops. */
3059 uid_loop[0] = NULL;
3061 /* Find boundaries of loops, mark which loops are contained within
3062 loops, and invalidate loops that have setjmp. */
3064 num_loops = 0;
3065 current_loop = NULL;
3066 for (insn = f; insn; insn = NEXT_INSN (insn))
3068 if (NOTE_P (insn))
3069 switch (NOTE_LINE_NUMBER (insn))
3071 case NOTE_INSN_LOOP_BEG:
3072 next_loop = loops->array + num_loops;
3073 next_loop->num = num_loops;
3074 num_loops++;
3075 next_loop->start = insn;
3076 next_loop->outer = current_loop;
3077 current_loop = next_loop;
3078 break;
3080 case NOTE_INSN_LOOP_END:
3081 gcc_assert (current_loop);
3083 current_loop->end = insn;
3084 current_loop = current_loop->outer;
3085 break;
3087 default:
3088 break;
3091 if (CALL_P (insn)
3092 && find_reg_note (insn, REG_SETJMP, NULL))
3094 /* In this case, we must invalidate our current loop and any
3095 enclosing loop. */
3096 for (loop = current_loop; loop; loop = loop->outer)
3098 loop->invalid = 1;
3099 if (dump_file)
3100 fprintf (dump_file,
3101 "\nLoop at %d ignored due to setjmp.\n",
3102 INSN_UID (loop->start));
3106 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
3107 enclosing loop, but this doesn't matter. */
3108 uid_loop[INSN_UID (insn)] = current_loop;
3111 /* Any loop containing a label used in an initializer must be invalidated,
3112 because it can be jumped into from anywhere. */
3113 for (label = forced_labels; label; label = XEXP (label, 1))
3114 invalidate_loops_containing_label (XEXP (label, 0));
3116 /* Any loop containing a label used for an exception handler must be
3117 invalidated, because it can be jumped into from anywhere. */
3118 for_each_eh_label (invalidate_loops_containing_label);
3120 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
3121 loop that it is not contained within, that loop is marked invalid.
3122 If any INSN or CALL_INSN uses a label's address, then the loop containing
3123 that label is marked invalid, because it could be jumped into from
3124 anywhere.
3126 Also look for blocks of code ending in an unconditional branch that
3127 exits the loop. If such a block is surrounded by a conditional
3128 branch around the block, move the block elsewhere (see below) and
3129 invert the jump to point to the code block. This may eliminate a
3130 label in our loop and will simplify processing by both us and a
3131 possible second cse pass. */
3133 for (insn = f; insn; insn = NEXT_INSN (insn))
3134 if (INSN_P (insn))
3136 struct loop *this_loop = uid_loop[INSN_UID (insn)];
3138 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
3140 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
3141 if (note)
3142 invalidate_loops_containing_label (XEXP (note, 0));
3145 if (!JUMP_P (insn))
3146 continue;
3148 mark_loop_jump (PATTERN (insn), this_loop);
3150 /* See if this is an unconditional branch outside the loop. */
3151 if (this_loop
3152 && (GET_CODE (PATTERN (insn)) == RETURN
3153 || (any_uncondjump_p (insn)
3154 && onlyjump_p (insn)
3155 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
3156 != this_loop)))
3157 && get_max_uid () < max_uid_for_loop)
3159 rtx p;
3160 rtx our_next = next_real_insn (insn);
3161 rtx last_insn_to_move = NEXT_INSN (insn);
3162 struct loop *dest_loop;
3163 struct loop *outer_loop = NULL;
3165 /* Go backwards until we reach the start of the loop, a label,
3166 or a JUMP_INSN. */
3167 for (p = PREV_INSN (insn);
3168 !LABEL_P (p)
3169 && ! (NOTE_P (p)
3170 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3171 && !JUMP_P (p);
3172 p = PREV_INSN (p))
3175 /* Check for the case where we have a jump to an inner nested
3176 loop, and do not perform the optimization in that case. */
3178 if (JUMP_LABEL (insn))
3180 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
3181 if (dest_loop)
3183 for (outer_loop = dest_loop; outer_loop;
3184 outer_loop = outer_loop->outer)
3185 if (outer_loop == this_loop)
3186 break;
3190 /* Make sure that the target of P is within the current loop. */
3192 if (JUMP_P (p) && JUMP_LABEL (p)
3193 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
3194 outer_loop = this_loop;
3196 /* If we stopped on a JUMP_INSN to the next insn after INSN,
3197 we have a block of code to try to move.
3199 We look backward and then forward from the target of INSN
3200 to find a BARRIER at the same loop depth as the target.
3201 If we find such a BARRIER, we make a new label for the start
3202 of the block, invert the jump in P and point it to that label,
3203 and move the block of code to the spot we found. */
3205 if (! outer_loop
3206 && JUMP_P (p)
3207 && JUMP_LABEL (p) != 0
3208 /* Just ignore jumps to labels that were never emitted.
3209 These always indicate compilation errors. */
3210 && INSN_UID (JUMP_LABEL (p)) != 0
3211 && any_condjump_p (p) && onlyjump_p (p)
3212 && next_real_insn (JUMP_LABEL (p)) == our_next
3213 /* If it's not safe to move the sequence, then we
3214 mustn't try. */
3215 && insns_safe_to_move_p (p, NEXT_INSN (insn),
3216 &last_insn_to_move))
3218 rtx target
3219 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
3220 struct loop *target_loop = uid_loop[INSN_UID (target)];
3221 rtx loc, loc2;
3222 rtx tmp;
3224 /* Search for possible garbage past the conditional jumps
3225 and look for the last barrier. */
3226 for (tmp = last_insn_to_move;
3227 tmp && !LABEL_P (tmp); tmp = NEXT_INSN (tmp))
3228 if (BARRIER_P (tmp))
3229 last_insn_to_move = tmp;
3231 for (loc = target; loc; loc = PREV_INSN (loc))
3232 if (BARRIER_P (loc)
3233 /* Don't move things inside a tablejump. */
3234 && ((loc2 = next_nonnote_insn (loc)) == 0
3235 || !LABEL_P (loc2)
3236 || (loc2 = next_nonnote_insn (loc2)) == 0
3237 || !JUMP_P (loc2)
3238 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
3239 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
3240 && uid_loop[INSN_UID (loc)] == target_loop)
3241 break;
3243 if (loc == 0)
3244 for (loc = target; loc; loc = NEXT_INSN (loc))
3245 if (BARRIER_P (loc)
3246 /* Don't move things inside a tablejump. */
3247 && ((loc2 = next_nonnote_insn (loc)) == 0
3248 || !LABEL_P (loc2)
3249 || (loc2 = next_nonnote_insn (loc2)) == 0
3250 || !JUMP_P (loc2)
3251 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
3252 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
3253 && uid_loop[INSN_UID (loc)] == target_loop)
3254 break;
3256 if (loc)
3258 rtx cond_label = JUMP_LABEL (p);
3259 rtx new_label = get_label_after (p);
3261 /* Ensure our label doesn't go away. */
3262 LABEL_NUSES (cond_label)++;
3264 /* Verify that uid_loop is large enough and that
3265 we can invert P. */
3266 if (invert_jump (p, new_label, 1))
3268 rtx q, r;
3269 bool only_notes;
3271 /* If no suitable BARRIER was found, create a suitable
3272 one before TARGET. Since TARGET is a fall through
3273 path, we'll need to insert a jump around our block
3274 and add a BARRIER before TARGET.
3276 This creates an extra unconditional jump outside
3277 the loop. However, the benefits of removing rarely
3278 executed instructions from inside the loop usually
3279 outweighs the cost of the extra unconditional jump
3280 outside the loop. */
3281 if (loc == 0)
3283 rtx temp;
3285 temp = gen_jump (JUMP_LABEL (insn));
3286 temp = emit_jump_insn_before (temp, target);
3287 JUMP_LABEL (temp) = JUMP_LABEL (insn);
3288 LABEL_NUSES (JUMP_LABEL (insn))++;
3289 loc = emit_barrier_before (target);
3292 /* Include the BARRIER after INSN and copy the
3293 block after LOC. */
3294 only_notes = squeeze_notes (&new_label,
3295 &last_insn_to_move);
3296 gcc_assert (!only_notes);
3298 reorder_insns (new_label, last_insn_to_move, loc);
3300 /* All those insns are now in TARGET_LOOP. */
3301 for (q = new_label;
3302 q != NEXT_INSN (last_insn_to_move);
3303 q = NEXT_INSN (q))
3304 uid_loop[INSN_UID (q)] = target_loop;
3306 /* The label jumped to by INSN is no longer a loop
3307 exit. Unless INSN does not have a label (e.g.,
3308 it is a RETURN insn), search loop->exit_labels
3309 to find its label_ref, and remove it. Also turn
3310 off LABEL_OUTSIDE_LOOP_P bit. */
3311 if (JUMP_LABEL (insn))
3313 for (q = 0, r = this_loop->exit_labels;
3315 q = r, r = LABEL_NEXTREF (r))
3316 if (XEXP (r, 0) == JUMP_LABEL (insn))
3318 LABEL_OUTSIDE_LOOP_P (r) = 0;
3319 if (q)
3320 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
3321 else
3322 this_loop->exit_labels = LABEL_NEXTREF (r);
3323 break;
3326 for (loop = this_loop; loop && loop != target_loop;
3327 loop = loop->outer)
3328 loop->exit_count--;
3330 /* If we didn't find it, then something is
3331 wrong. */
3332 gcc_assert (r);
3335 /* P is now a jump outside the loop, so it must be put
3336 in loop->exit_labels, and marked as such.
3337 The easiest way to do this is to just call
3338 mark_loop_jump again for P. */
3339 mark_loop_jump (PATTERN (p), this_loop);
3341 /* If INSN now jumps to the insn after it,
3342 delete INSN. */
3343 if (JUMP_LABEL (insn) != 0
3344 && (next_real_insn (JUMP_LABEL (insn))
3345 == next_real_insn (insn)))
3346 delete_related_insns (insn);
3349 /* Continue the loop after where the conditional
3350 branch used to jump, since the only branch insn
3351 in the block (if it still remains) is an inter-loop
3352 branch and hence needs no processing. */
3353 insn = NEXT_INSN (cond_label);
3355 if (--LABEL_NUSES (cond_label) == 0)
3356 delete_related_insns (cond_label);
3358 /* This loop will be continued with NEXT_INSN (insn). */
3359 insn = PREV_INSN (insn);
3366 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3367 loops it is contained in, mark the target loop invalid.
3369 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3371 static void
3372 mark_loop_jump (rtx x, struct loop *loop)
3374 struct loop *dest_loop;
3375 struct loop *outer_loop;
3376 int i;
3378 switch (GET_CODE (x))
3380 case PC:
3381 case USE:
3382 case CLOBBER:
3383 case REG:
3384 case MEM:
3385 case CONST_INT:
3386 case CONST_DOUBLE:
3387 case RETURN:
3388 return;
3390 case CONST:
3391 /* There could be a label reference in here. */
3392 mark_loop_jump (XEXP (x, 0), loop);
3393 return;
3395 case PLUS:
3396 case MINUS:
3397 case MULT:
3398 mark_loop_jump (XEXP (x, 0), loop);
3399 mark_loop_jump (XEXP (x, 1), loop);
3400 return;
3402 case LO_SUM:
3403 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3404 mark_loop_jump (XEXP (x, 1), loop);
3405 return;
3407 case SIGN_EXTEND:
3408 case ZERO_EXTEND:
3409 mark_loop_jump (XEXP (x, 0), loop);
3410 return;
3412 case LABEL_REF:
3413 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3415 /* Link together all labels that branch outside the loop. This
3416 is used by final_[bg]iv_value and the loop unrolling code. Also
3417 mark this LABEL_REF so we know that this branch should predict
3418 false. */
3420 /* A check to make sure the label is not in an inner nested loop,
3421 since this does not count as a loop exit. */
3422 if (dest_loop)
3424 for (outer_loop = dest_loop; outer_loop;
3425 outer_loop = outer_loop->outer)
3426 if (outer_loop == loop)
3427 break;
3429 else
3430 outer_loop = NULL;
3432 if (loop && ! outer_loop)
3434 LABEL_OUTSIDE_LOOP_P (x) = 1;
3435 LABEL_NEXTREF (x) = loop->exit_labels;
3436 loop->exit_labels = x;
3438 for (outer_loop = loop;
3439 outer_loop && outer_loop != dest_loop;
3440 outer_loop = outer_loop->outer)
3441 outer_loop->exit_count++;
3444 /* If this is inside a loop, but not in the current loop or one enclosed
3445 by it, it invalidates at least one loop. */
3447 if (! dest_loop)
3448 return;
3450 /* We must invalidate every nested loop containing the target of this
3451 label, except those that also contain the jump insn. */
3453 for (; dest_loop; dest_loop = dest_loop->outer)
3455 /* Stop when we reach a loop that also contains the jump insn. */
3456 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3457 if (dest_loop == outer_loop)
3458 return;
3460 /* If we get here, we know we need to invalidate a loop. */
3461 if (dump_file && ! dest_loop->invalid)
3462 fprintf (dump_file,
3463 "\nLoop at %d ignored due to multiple entry points.\n",
3464 INSN_UID (dest_loop->start));
3466 dest_loop->invalid = 1;
3468 return;
3470 case SET:
3471 /* If this is not setting pc, ignore. */
3472 if (SET_DEST (x) == pc_rtx)
3473 mark_loop_jump (SET_SRC (x), loop);
3474 return;
3476 case IF_THEN_ELSE:
3477 mark_loop_jump (XEXP (x, 1), loop);
3478 mark_loop_jump (XEXP (x, 2), loop);
3479 return;
3481 case PARALLEL:
3482 case ADDR_VEC:
3483 for (i = 0; i < XVECLEN (x, 0); i++)
3484 mark_loop_jump (XVECEXP (x, 0, i), loop);
3485 return;
3487 case ADDR_DIFF_VEC:
3488 for (i = 0; i < XVECLEN (x, 1); i++)
3489 mark_loop_jump (XVECEXP (x, 1, i), loop);
3490 return;
3492 default:
3493 /* Strictly speaking this is not a jump into the loop, only a possible
3494 jump out of the loop. However, we have no way to link the destination
3495 of this jump onto the list of exit labels. To be safe we mark this
3496 loop and any containing loops as invalid. */
3497 if (loop)
3499 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3501 if (dump_file && ! outer_loop->invalid)
3502 fprintf (dump_file,
3503 "\nLoop at %d ignored due to unknown exit jump.\n",
3504 INSN_UID (outer_loop->start));
3505 outer_loop->invalid = 1;
3508 return;
3512 /* Return nonzero if there is a label in the range from
3513 insn INSN to and including the insn whose luid is END
3514 INSN must have an assigned luid (i.e., it must not have
3515 been previously created by loop.c). */
3517 static int
3518 labels_in_range_p (rtx insn, int end)
3520 while (insn && INSN_LUID (insn) <= end)
3522 if (LABEL_P (insn))
3523 return 1;
3524 insn = NEXT_INSN (insn);
3527 return 0;
3530 /* Record that a memory reference X is being set. */
3532 static void
3533 note_addr_stored (rtx x, rtx y ATTRIBUTE_UNUSED,
3534 void *data ATTRIBUTE_UNUSED)
3536 struct loop_info *loop_info = data;
3538 if (x == 0 || !MEM_P (x))
3539 return;
3541 /* Count number of memory writes.
3542 This affects heuristics in strength_reduce. */
3543 loop_info->num_mem_sets++;
3545 /* BLKmode MEM means all memory is clobbered. */
3546 if (GET_MODE (x) == BLKmode)
3548 if (MEM_READONLY_P (x))
3549 loop_info->unknown_constant_address_altered = 1;
3550 else
3551 loop_info->unknown_address_altered = 1;
3553 return;
3556 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3557 loop_info->store_mems);
3560 /* X is a value modified by an INSN that references a biv inside a loop
3561 exit test (i.e., X is somehow related to the value of the biv). If X
3562 is a pseudo that is used more than once, then the biv is (effectively)
3563 used more than once. DATA is a pointer to a loop_regs structure. */
3565 static void
3566 note_set_pseudo_multiple_uses (rtx x, rtx y ATTRIBUTE_UNUSED, void *data)
3568 struct loop_regs *regs = (struct loop_regs *) data;
3570 if (x == 0)
3571 return;
3573 while (GET_CODE (x) == STRICT_LOW_PART
3574 || GET_CODE (x) == SIGN_EXTRACT
3575 || GET_CODE (x) == ZERO_EXTRACT
3576 || GET_CODE (x) == SUBREG)
3577 x = XEXP (x, 0);
3579 if (!REG_P (x) || REGNO (x) < FIRST_PSEUDO_REGISTER)
3580 return;
3582 /* If we do not have usage information, or if we know the register
3583 is used more than once, note that fact for check_dbra_loop. */
3584 if (REGNO (x) >= max_reg_before_loop
3585 || ! regs->array[REGNO (x)].single_usage
3586 || regs->array[REGNO (x)].single_usage == const0_rtx)
3587 regs->multiple_uses = 1;
3590 /* Return nonzero if the rtx X is invariant over the current loop.
3592 The value is 2 if we refer to something only conditionally invariant.
3594 A memory ref is invariant if it is not volatile and does not conflict
3595 with anything stored in `loop_info->store_mems'. */
3597 static int
3598 loop_invariant_p (const struct loop *loop, rtx x)
3600 struct loop_info *loop_info = LOOP_INFO (loop);
3601 struct loop_regs *regs = LOOP_REGS (loop);
3602 int i;
3603 enum rtx_code code;
3604 const char *fmt;
3605 int conditional = 0;
3606 rtx mem_list_entry;
3608 if (x == 0)
3609 return 1;
3610 code = GET_CODE (x);
3611 switch (code)
3613 case CONST_INT:
3614 case CONST_DOUBLE:
3615 case SYMBOL_REF:
3616 case CONST:
3617 return 1;
3619 case LABEL_REF:
3620 return 1;
3622 case PC:
3623 case CC0:
3624 case UNSPEC_VOLATILE:
3625 return 0;
3627 case REG:
3628 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3629 || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3630 && ! current_function_has_nonlocal_goto)
3631 return 1;
3633 if (LOOP_INFO (loop)->has_call
3634 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3635 return 0;
3637 /* Out-of-range regs can occur when we are called from unrolling.
3638 These registers created by the unroller are set in the loop,
3639 hence are never invariant.
3640 Other out-of-range regs can be generated by load_mems; those that
3641 are written to in the loop are not invariant, while those that are
3642 not written to are invariant. It would be easy for load_mems
3643 to set n_times_set correctly for these registers, however, there
3644 is no easy way to distinguish them from registers created by the
3645 unroller. */
3647 if (REGNO (x) >= (unsigned) regs->num)
3648 return 0;
3650 if (regs->array[REGNO (x)].set_in_loop < 0)
3651 return 2;
3653 return regs->array[REGNO (x)].set_in_loop == 0;
3655 case MEM:
3656 /* Volatile memory references must be rejected. Do this before
3657 checking for read-only items, so that volatile read-only items
3658 will be rejected also. */
3659 if (MEM_VOLATILE_P (x))
3660 return 0;
3662 /* See if there is any dependence between a store and this load. */
3663 mem_list_entry = loop_info->store_mems;
3664 while (mem_list_entry)
3666 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3667 x, rtx_varies_p))
3668 return 0;
3670 mem_list_entry = XEXP (mem_list_entry, 1);
3673 /* It's not invalidated by a store in memory
3674 but we must still verify the address is invariant. */
3675 break;
3677 case ASM_OPERANDS:
3678 /* Don't mess with insns declared volatile. */
3679 if (MEM_VOLATILE_P (x))
3680 return 0;
3681 break;
3683 default:
3684 break;
3687 fmt = GET_RTX_FORMAT (code);
3688 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3690 if (fmt[i] == 'e')
3692 int tem = loop_invariant_p (loop, XEXP (x, i));
3693 if (tem == 0)
3694 return 0;
3695 if (tem == 2)
3696 conditional = 1;
3698 else if (fmt[i] == 'E')
3700 int j;
3701 for (j = 0; j < XVECLEN (x, i); j++)
3703 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3704 if (tem == 0)
3705 return 0;
3706 if (tem == 2)
3707 conditional = 1;
3713 return 1 + conditional;
3716 /* Return nonzero if all the insns in the loop that set REG
3717 are INSN and the immediately following insns,
3718 and if each of those insns sets REG in an invariant way
3719 (not counting uses of REG in them).
3721 The value is 2 if some of these insns are only conditionally invariant.
3723 We assume that INSN itself is the first set of REG
3724 and that its source is invariant. */
3726 static int
3727 consec_sets_invariant_p (const struct loop *loop, rtx reg, int n_sets,
3728 rtx insn)
3730 struct loop_regs *regs = LOOP_REGS (loop);
3731 rtx p = insn;
3732 unsigned int regno = REGNO (reg);
3733 rtx temp;
3734 /* Number of sets we have to insist on finding after INSN. */
3735 int count = n_sets - 1;
3736 int old = regs->array[regno].set_in_loop;
3737 int value = 0;
3738 int this;
3740 /* If N_SETS hit the limit, we can't rely on its value. */
3741 if (n_sets == 127)
3742 return 0;
3744 regs->array[regno].set_in_loop = 0;
3746 while (count > 0)
3748 enum rtx_code code;
3749 rtx set;
3751 p = NEXT_INSN (p);
3752 code = GET_CODE (p);
3754 /* If library call, skip to end of it. */
3755 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3756 p = XEXP (temp, 0);
3758 this = 0;
3759 if (code == INSN
3760 && (set = single_set (p))
3761 && REG_P (SET_DEST (set))
3762 && REGNO (SET_DEST (set)) == regno)
3764 this = loop_invariant_p (loop, SET_SRC (set));
3765 if (this != 0)
3766 value |= this;
3767 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3769 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3770 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3771 notes are OK. */
3772 this = (CONSTANT_P (XEXP (temp, 0))
3773 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3774 && loop_invariant_p (loop, XEXP (temp, 0))));
3775 if (this != 0)
3776 value |= this;
3779 if (this != 0)
3780 count--;
3781 else if (code != NOTE)
3783 regs->array[regno].set_in_loop = old;
3784 return 0;
3788 regs->array[regno].set_in_loop = old;
3789 /* If loop_invariant_p ever returned 2, we return 2. */
3790 return 1 + (value & 2);
3793 /* Look at all uses (not sets) of registers in X. For each, if it is
3794 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3795 a different insn, set USAGE[REGNO] to const0_rtx. */
3797 static void
3798 find_single_use_in_loop (struct loop_regs *regs, rtx insn, rtx x)
3800 enum rtx_code code = GET_CODE (x);
3801 const char *fmt = GET_RTX_FORMAT (code);
3802 int i, j;
3804 if (code == REG)
3805 regs->array[REGNO (x)].single_usage
3806 = (regs->array[REGNO (x)].single_usage != 0
3807 && regs->array[REGNO (x)].single_usage != insn)
3808 ? const0_rtx : insn;
3810 else if (code == SET)
3812 /* Don't count SET_DEST if it is a REG; otherwise count things
3813 in SET_DEST because if a register is partially modified, it won't
3814 show up as a potential movable so we don't care how USAGE is set
3815 for it. */
3816 if (!REG_P (SET_DEST (x)))
3817 find_single_use_in_loop (regs, insn, SET_DEST (x));
3818 find_single_use_in_loop (regs, insn, SET_SRC (x));
3820 else
3821 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3823 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3824 find_single_use_in_loop (regs, insn, XEXP (x, i));
3825 else if (fmt[i] == 'E')
3826 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3827 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3831 /* Count and record any set in X which is contained in INSN. Update
3832 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3833 in X. */
3835 static void
3836 count_one_set (struct loop_regs *regs, rtx insn, rtx x, rtx *last_set)
3838 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
3839 /* Don't move a reg that has an explicit clobber.
3840 It's not worth the pain to try to do it correctly. */
3841 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3843 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3845 rtx dest = SET_DEST (x);
3846 while (GET_CODE (dest) == SUBREG
3847 || GET_CODE (dest) == ZERO_EXTRACT
3848 || GET_CODE (dest) == STRICT_LOW_PART)
3849 dest = XEXP (dest, 0);
3850 if (REG_P (dest))
3852 int i;
3853 int regno = REGNO (dest);
3854 for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3856 /* If this is the first setting of this reg
3857 in current basic block, and it was set before,
3858 it must be set in two basic blocks, so it cannot
3859 be moved out of the loop. */
3860 if (regs->array[regno].set_in_loop > 0
3861 && last_set[regno] == 0)
3862 regs->array[regno+i].may_not_optimize = 1;
3863 /* If this is not first setting in current basic block,
3864 see if reg was used in between previous one and this.
3865 If so, neither one can be moved. */
3866 if (last_set[regno] != 0
3867 && reg_used_between_p (dest, last_set[regno], insn))
3868 regs->array[regno+i].may_not_optimize = 1;
3869 if (regs->array[regno+i].set_in_loop < 127)
3870 ++regs->array[regno+i].set_in_loop;
3871 last_set[regno+i] = insn;
3877 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3878 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3879 contained in insn INSN is used by any insn that precedes INSN in
3880 cyclic order starting from the loop entry point.
3882 We don't want to use INSN_LUID here because if we restrict INSN to those
3883 that have a valid INSN_LUID, it means we cannot move an invariant out
3884 from an inner loop past two loops. */
3886 static int
3887 loop_reg_used_before_p (const struct loop *loop, rtx set, rtx insn)
3889 rtx reg = SET_DEST (set);
3890 rtx p;
3892 /* Scan forward checking for register usage. If we hit INSN, we
3893 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3894 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3896 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3897 return 1;
3899 if (p == loop->end)
3900 p = loop->start;
3903 return 0;
3907 /* Information we collect about arrays that we might want to prefetch. */
3908 struct prefetch_info
3910 struct iv_class *class; /* Class this prefetch is based on. */
3911 struct induction *giv; /* GIV this prefetch is based on. */
3912 rtx base_address; /* Start prefetching from this address plus
3913 index. */
3914 HOST_WIDE_INT index;
3915 HOST_WIDE_INT stride; /* Prefetch stride in bytes in each
3916 iteration. */
3917 unsigned int bytes_accessed; /* Sum of sizes of all accesses to this
3918 prefetch area in one iteration. */
3919 unsigned int total_bytes; /* Total bytes loop will access in this block.
3920 This is set only for loops with known
3921 iteration counts and is 0xffffffff
3922 otherwise. */
3923 int prefetch_in_loop; /* Number of prefetch insns in loop. */
3924 int prefetch_before_loop; /* Number of prefetch insns before loop. */
3925 unsigned int write : 1; /* 1 for read/write prefetches. */
3928 /* Data used by check_store function. */
3929 struct check_store_data
3931 rtx mem_address;
3932 int mem_write;
3935 static void check_store (rtx, rtx, void *);
3936 static void emit_prefetch_instructions (struct loop *);
3937 static int rtx_equal_for_prefetch_p (rtx, rtx);
3939 /* Set mem_write when mem_address is found. Used as callback to
3940 note_stores. */
3941 static void
3942 check_store (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3944 struct check_store_data *d = (struct check_store_data *) data;
3946 if ((MEM_P (x)) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3947 d->mem_write = 1;
3950 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3951 important to get some addresses combined. Later more sophisticated
3952 transformations can be added when necessary.
3954 ??? Same trick with swapping operand is done at several other places.
3955 It can be nice to develop some common way to handle this. */
3957 static int
3958 rtx_equal_for_prefetch_p (rtx x, rtx y)
3960 int i;
3961 int j;
3962 enum rtx_code code = GET_CODE (x);
3963 const char *fmt;
3965 if (x == y)
3966 return 1;
3967 if (code != GET_CODE (y))
3968 return 0;
3970 if (GET_MODE (x) != GET_MODE (y))
3971 return 0;
3973 switch (code)
3975 case PC:
3976 case CC0:
3977 case CONST_INT:
3978 case CONST_DOUBLE:
3979 return 0;
3981 case LABEL_REF:
3982 return XEXP (x, 0) == XEXP (y, 0);
3984 default:
3985 break;
3988 if (COMMUTATIVE_ARITH_P (x))
3990 return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3991 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3992 || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3993 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3996 /* Compare the elements. If any pair of corresponding elements fails to
3997 match, return 0 for the whole thing. */
3999 fmt = GET_RTX_FORMAT (code);
4000 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4002 switch (fmt[i])
4004 case 'w':
4005 if (XWINT (x, i) != XWINT (y, i))
4006 return 0;
4007 break;
4009 case 'i':
4010 if (XINT (x, i) != XINT (y, i))
4011 return 0;
4012 break;
4014 case 'E':
4015 /* Two vectors must have the same length. */
4016 if (XVECLEN (x, i) != XVECLEN (y, i))
4017 return 0;
4019 /* And the corresponding elements must match. */
4020 for (j = 0; j < XVECLEN (x, i); j++)
4021 if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
4022 XVECEXP (y, i, j)) == 0)
4023 return 0;
4024 break;
4026 case 'e':
4027 if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
4028 return 0;
4029 break;
4031 case 's':
4032 if (strcmp (XSTR (x, i), XSTR (y, i)))
4033 return 0;
4034 break;
4036 case 'u':
4037 /* These are just backpointers, so they don't matter. */
4038 break;
4040 case '0':
4041 break;
4043 /* It is believed that rtx's at this level will never
4044 contain anything but integers and other rtx's,
4045 except for within LABEL_REFs and SYMBOL_REFs. */
4046 default:
4047 gcc_unreachable ();
4050 return 1;
4053 /* Remove constant addition value from the expression X (when present)
4054 and return it. */
4056 static HOST_WIDE_INT
4057 remove_constant_addition (rtx *x)
4059 HOST_WIDE_INT addval = 0;
4060 rtx exp = *x;
4062 /* Avoid clobbering a shared CONST expression. */
4063 if (GET_CODE (exp) == CONST)
4065 if (GET_CODE (XEXP (exp, 0)) == PLUS
4066 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
4067 && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
4069 *x = XEXP (XEXP (exp, 0), 0);
4070 return INTVAL (XEXP (XEXP (exp, 0), 1));
4072 return 0;
4075 if (GET_CODE (exp) == CONST_INT)
4077 addval = INTVAL (exp);
4078 *x = const0_rtx;
4081 /* For plus expression recurse on ourself. */
4082 else if (GET_CODE (exp) == PLUS)
4084 addval += remove_constant_addition (&XEXP (exp, 0));
4085 addval += remove_constant_addition (&XEXP (exp, 1));
4087 /* In case our parameter was constant, remove extra zero from the
4088 expression. */
4089 if (XEXP (exp, 0) == const0_rtx)
4090 *x = XEXP (exp, 1);
4091 else if (XEXP (exp, 1) == const0_rtx)
4092 *x = XEXP (exp, 0);
4095 return addval;
4098 /* Attempt to identify accesses to arrays that are most likely to cause cache
4099 misses, and emit prefetch instructions a few prefetch blocks forward.
4101 To detect the arrays we use the GIV information that was collected by the
4102 strength reduction pass.
4104 The prefetch instructions are generated after the GIV information is done
4105 and before the strength reduction process. The new GIVs are injected into
4106 the strength reduction tables, so the prefetch addresses are optimized as
4107 well.
4109 GIVs are split into base address, stride, and constant addition values.
4110 GIVs with the same address, stride and close addition values are combined
4111 into a single prefetch. Also writes to GIVs are detected, so that prefetch
4112 for write instructions can be used for the block we write to, on machines
4113 that support write prefetches.
4115 Several heuristics are used to determine when to prefetch. They are
4116 controlled by defined symbols that can be overridden for each target. */
4118 static void
4119 emit_prefetch_instructions (struct loop *loop)
4121 int num_prefetches = 0;
4122 int num_real_prefetches = 0;
4123 int num_real_write_prefetches = 0;
4124 int num_prefetches_before = 0;
4125 int num_write_prefetches_before = 0;
4126 int ahead = 0;
4127 int i;
4128 struct iv_class *bl;
4129 struct induction *iv;
4130 struct prefetch_info info[MAX_PREFETCHES];
4131 struct loop_ivs *ivs = LOOP_IVS (loop);
4133 if (!HAVE_prefetch || PREFETCH_BLOCK == 0)
4134 return;
4136 /* Consider only loops w/o calls. When a call is done, the loop is probably
4137 slow enough to read the memory. */
4138 if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
4140 if (dump_file)
4141 fprintf (dump_file, "Prefetch: ignoring loop: has call.\n");
4143 return;
4146 /* Don't prefetch in loops known to have few iterations. */
4147 if (PREFETCH_NO_LOW_LOOPCNT
4148 && LOOP_INFO (loop)->n_iterations
4149 && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
4151 if (dump_file)
4152 fprintf (dump_file,
4153 "Prefetch: ignoring loop: not enough iterations.\n");
4154 return;
4157 /* Search all induction variables and pick those interesting for the prefetch
4158 machinery. */
4159 for (bl = ivs->list; bl; bl = bl->next)
4161 struct induction *biv = bl->biv, *biv1;
4162 int basestride = 0;
4164 biv1 = biv;
4166 /* Expect all BIVs to be executed in each iteration. This makes our
4167 analysis more conservative. */
4168 while (biv1)
4170 /* Discard non-constant additions that we can't handle well yet, and
4171 BIVs that are executed multiple times; such BIVs ought to be
4172 handled in the nested loop. We accept not_every_iteration BIVs,
4173 since these only result in larger strides and make our
4174 heuristics more conservative. */
4175 if (GET_CODE (biv->add_val) != CONST_INT)
4177 if (dump_file)
4179 fprintf (dump_file,
4180 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
4181 REGNO (biv->src_reg), INSN_UID (biv->insn));
4182 print_rtl (dump_file, biv->add_val);
4183 fprintf (dump_file, "\n");
4185 break;
4188 if (biv->maybe_multiple)
4190 if (dump_file)
4192 fprintf (dump_file,
4193 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
4194 REGNO (biv->src_reg), INSN_UID (biv->insn));
4195 print_rtl (dump_file, biv->add_val);
4196 fprintf (dump_file, "\n");
4198 break;
4201 basestride += INTVAL (biv1->add_val);
4202 biv1 = biv1->next_iv;
4205 if (biv1 || !basestride)
4206 continue;
4208 for (iv = bl->giv; iv; iv = iv->next_iv)
4210 rtx address;
4211 rtx temp;
4212 HOST_WIDE_INT index = 0;
4213 int add = 1;
4214 HOST_WIDE_INT stride = 0;
4215 int stride_sign = 1;
4216 struct check_store_data d;
4217 const char *ignore_reason = NULL;
4218 int size = GET_MODE_SIZE (GET_MODE (iv));
4220 /* See whether an induction variable is interesting to us and if
4221 not, report the reason. */
4222 if (iv->giv_type != DEST_ADDR)
4223 ignore_reason = "giv is not a destination address";
4225 /* We are interested only in constant stride memory references
4226 in order to be able to compute density easily. */
4227 else if (GET_CODE (iv->mult_val) != CONST_INT)
4228 ignore_reason = "stride is not constant";
4230 else
4232 stride = INTVAL (iv->mult_val) * basestride;
4233 if (stride < 0)
4235 stride = -stride;
4236 stride_sign = -1;
4239 /* On some targets, reversed order prefetches are not
4240 worthwhile. */
4241 if (PREFETCH_NO_REVERSE_ORDER && stride_sign < 0)
4242 ignore_reason = "reversed order stride";
4244 /* Prefetch of accesses with an extreme stride might not be
4245 worthwhile, either. */
4246 else if (PREFETCH_NO_EXTREME_STRIDE
4247 && stride > PREFETCH_EXTREME_STRIDE)
4248 ignore_reason = "extreme stride";
4250 /* Ignore GIVs with varying add values; we can't predict the
4251 value for the next iteration. */
4252 else if (!loop_invariant_p (loop, iv->add_val))
4253 ignore_reason = "giv has varying add value";
4255 /* Ignore GIVs in the nested loops; they ought to have been
4256 handled already. */
4257 else if (iv->maybe_multiple)
4258 ignore_reason = "giv is in nested loop";
4261 if (ignore_reason != NULL)
4263 if (dump_file)
4264 fprintf (dump_file,
4265 "Prefetch: ignoring giv at %d: %s.\n",
4266 INSN_UID (iv->insn), ignore_reason);
4267 continue;
4270 /* Determine the pointer to the basic array we are examining. It is
4271 the sum of the BIV's initial value and the GIV's add_val. */
4272 address = copy_rtx (iv->add_val);
4273 temp = copy_rtx (bl->initial_value);
4275 address = simplify_gen_binary (PLUS, Pmode, temp, address);
4276 index = remove_constant_addition (&address);
4278 d.mem_write = 0;
4279 d.mem_address = *iv->location;
4281 /* When the GIV is not always executed, we might be better off by
4282 not dirtying the cache pages. */
4283 if (PREFETCH_CONDITIONAL || iv->always_executed)
4284 note_stores (PATTERN (iv->insn), check_store, &d);
4285 else
4287 if (dump_file)
4288 fprintf (dump_file, "Prefetch: Ignoring giv at %d: %s\n",
4289 INSN_UID (iv->insn), "in conditional code.");
4290 continue;
4293 /* Attempt to find another prefetch to the same array and see if we
4294 can merge this one. */
4295 for (i = 0; i < num_prefetches; i++)
4296 if (rtx_equal_for_prefetch_p (address, info[i].base_address)
4297 && stride == info[i].stride)
4299 /* In case both access same array (same location
4300 just with small difference in constant indexes), merge
4301 the prefetches. Just do the later and the earlier will
4302 get prefetched from previous iteration.
4303 The artificial threshold should not be too small,
4304 but also not bigger than small portion of memory usually
4305 traversed by single loop. */
4306 if (index >= info[i].index
4307 && index - info[i].index < PREFETCH_EXTREME_DIFFERENCE)
4309 info[i].write |= d.mem_write;
4310 info[i].bytes_accessed += size;
4311 info[i].index = index;
4312 info[i].giv = iv;
4313 info[i].class = bl;
4314 info[num_prefetches].base_address = address;
4315 add = 0;
4316 break;
4319 if (index < info[i].index
4320 && info[i].index - index < PREFETCH_EXTREME_DIFFERENCE)
4322 info[i].write |= d.mem_write;
4323 info[i].bytes_accessed += size;
4324 add = 0;
4325 break;
4329 /* Merging failed. */
4330 if (add)
4332 info[num_prefetches].giv = iv;
4333 info[num_prefetches].class = bl;
4334 info[num_prefetches].index = index;
4335 info[num_prefetches].stride = stride;
4336 info[num_prefetches].base_address = address;
4337 info[num_prefetches].write = d.mem_write;
4338 info[num_prefetches].bytes_accessed = size;
4339 num_prefetches++;
4340 if (num_prefetches >= MAX_PREFETCHES)
4342 if (dump_file)
4343 fprintf (dump_file,
4344 "Maximal number of prefetches exceeded.\n");
4345 return;
4351 for (i = 0; i < num_prefetches; i++)
4353 int density;
4355 /* Attempt to calculate the total number of bytes fetched by all
4356 iterations of the loop. Avoid overflow. */
4357 if (LOOP_INFO (loop)->n_iterations
4358 && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
4359 >= LOOP_INFO (loop)->n_iterations))
4360 info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
4361 else
4362 info[i].total_bytes = 0xffffffff;
4364 density = info[i].bytes_accessed * 100 / info[i].stride;
4366 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4367 if (PREFETCH_ONLY_DENSE_MEM)
4368 if (density * 256 > PREFETCH_DENSE_MEM * 100
4369 && (info[i].total_bytes / PREFETCH_BLOCK
4370 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
4372 info[i].prefetch_before_loop = 1;
4373 info[i].prefetch_in_loop
4374 = (info[i].total_bytes / PREFETCH_BLOCK
4375 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
4377 else
4379 info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
4380 if (dump_file)
4381 fprintf (dump_file,
4382 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4383 INSN_UID (info[i].giv->insn), density);
4385 else
4386 info[i].prefetch_in_loop = 1, info[i].prefetch_before_loop = 1;
4388 /* Find how many prefetch instructions we'll use within the loop. */
4389 if (info[i].prefetch_in_loop != 0)
4391 info[i].prefetch_in_loop = ((info[i].stride + PREFETCH_BLOCK - 1)
4392 / PREFETCH_BLOCK);
4393 num_real_prefetches += info[i].prefetch_in_loop;
4394 if (info[i].write)
4395 num_real_write_prefetches += info[i].prefetch_in_loop;
4399 /* Determine how many iterations ahead to prefetch within the loop, based
4400 on how many prefetches we currently expect to do within the loop. */
4401 if (num_real_prefetches != 0)
4403 if ((ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches) == 0)
4405 if (dump_file)
4406 fprintf (dump_file,
4407 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4408 SIMULTANEOUS_PREFETCHES, num_real_prefetches);
4409 num_real_prefetches = 0, num_real_write_prefetches = 0;
4412 /* We'll also use AHEAD to determine how many prefetch instructions to
4413 emit before a loop, so don't leave it zero. */
4414 if (ahead == 0)
4415 ahead = PREFETCH_BLOCKS_BEFORE_LOOP_MAX;
4417 for (i = 0; i < num_prefetches; i++)
4419 /* Update if we've decided not to prefetch anything within the loop. */
4420 if (num_real_prefetches == 0)
4421 info[i].prefetch_in_loop = 0;
4423 /* Find how many prefetch instructions we'll use before the loop. */
4424 if (info[i].prefetch_before_loop != 0)
4426 int n = info[i].total_bytes / PREFETCH_BLOCK;
4427 if (n > ahead)
4428 n = ahead;
4429 info[i].prefetch_before_loop = n;
4430 num_prefetches_before += n;
4431 if (info[i].write)
4432 num_write_prefetches_before += n;
4435 if (dump_file)
4437 if (info[i].prefetch_in_loop == 0
4438 && info[i].prefetch_before_loop == 0)
4439 continue;
4440 fprintf (dump_file, "Prefetch insn: %d",
4441 INSN_UID (info[i].giv->insn));
4442 fprintf (dump_file,
4443 "; in loop: %d; before: %d; %s\n",
4444 info[i].prefetch_in_loop,
4445 info[i].prefetch_before_loop,
4446 info[i].write ? "read/write" : "read only");
4447 fprintf (dump_file,
4448 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4449 (int) (info[i].bytes_accessed * 100 / info[i].stride),
4450 info[i].bytes_accessed, info[i].total_bytes);
4451 fprintf (dump_file, " index: " HOST_WIDE_INT_PRINT_DEC
4452 "; stride: " HOST_WIDE_INT_PRINT_DEC "; address: ",
4453 info[i].index, info[i].stride);
4454 print_rtl (dump_file, info[i].base_address);
4455 fprintf (dump_file, "\n");
4459 if (num_real_prefetches + num_prefetches_before > 0)
4461 /* Record that this loop uses prefetch instructions. */
4462 LOOP_INFO (loop)->has_prefetch = 1;
4464 if (dump_file)
4466 fprintf (dump_file, "Real prefetches needed within loop: %d (write: %d)\n",
4467 num_real_prefetches, num_real_write_prefetches);
4468 fprintf (dump_file, "Real prefetches needed before loop: %d (write: %d)\n",
4469 num_prefetches_before, num_write_prefetches_before);
4473 for (i = 0; i < num_prefetches; i++)
4475 int y;
4477 for (y = 0; y < info[i].prefetch_in_loop; y++)
4479 rtx loc = copy_rtx (*info[i].giv->location);
4480 rtx insn;
4481 int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4482 rtx before_insn = info[i].giv->insn;
4483 rtx prev_insn = PREV_INSN (info[i].giv->insn);
4484 rtx seq;
4486 /* We can save some effort by offsetting the address on
4487 architectures with offsettable memory references. */
4488 if (offsettable_address_p (0, VOIDmode, loc))
4489 loc = plus_constant (loc, bytes_ahead);
4490 else
4492 rtx reg = gen_reg_rtx (Pmode);
4493 loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4494 GEN_INT (bytes_ahead), reg,
4495 0, before_insn);
4496 loc = reg;
4499 start_sequence ();
4500 /* Make sure the address operand is valid for prefetch. */
4501 if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4502 (loc, insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4503 loc = force_reg (Pmode, loc);
4504 emit_insn (gen_prefetch (loc, GEN_INT (info[i].write),
4505 GEN_INT (3)));
4506 seq = get_insns ();
4507 end_sequence ();
4508 emit_insn_before (seq, before_insn);
4510 /* Check all insns emitted and record the new GIV
4511 information. */
4512 insn = NEXT_INSN (prev_insn);
4513 while (insn != before_insn)
4515 insn = check_insn_for_givs (loop, insn,
4516 info[i].giv->always_executed,
4517 info[i].giv->maybe_multiple);
4518 insn = NEXT_INSN (insn);
4522 if (PREFETCH_BEFORE_LOOP)
4524 /* Emit insns before the loop to fetch the first cache lines or,
4525 if we're not prefetching within the loop, everything we expect
4526 to need. */
4527 for (y = 0; y < info[i].prefetch_before_loop; y++)
4529 rtx reg = gen_reg_rtx (Pmode);
4530 rtx loop_start = loop->start;
4531 rtx init_val = info[i].class->initial_value;
4532 rtx add_val = simplify_gen_binary (PLUS, Pmode,
4533 info[i].giv->add_val,
4534 GEN_INT (y * PREFETCH_BLOCK));
4536 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4537 non-constant INIT_VAL to have the same mode as REG, which
4538 in this case we know to be Pmode. */
4539 if (GET_MODE (init_val) != Pmode && !CONSTANT_P (init_val))
4541 rtx seq;
4543 start_sequence ();
4544 init_val = convert_to_mode (Pmode, init_val, 0);
4545 seq = get_insns ();
4546 end_sequence ();
4547 loop_insn_emit_before (loop, 0, loop_start, seq);
4549 loop_iv_add_mult_emit_before (loop, init_val,
4550 info[i].giv->mult_val,
4551 add_val, reg, 0, loop_start);
4552 emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4553 GEN_INT (3)),
4554 loop_start);
4559 return;
4562 /* Communication with routines called via `note_stores'. */
4564 static rtx note_insn;
4566 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4568 static rtx addr_placeholder;
4570 /* ??? Unfinished optimizations, and possible future optimizations,
4571 for the strength reduction code. */
4573 /* ??? The interaction of biv elimination, and recognition of 'constant'
4574 bivs, may cause problems. */
4576 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4577 performance problems.
4579 Perhaps don't eliminate things that can be combined with an addressing
4580 mode. Find all givs that have the same biv, mult_val, and add_val;
4581 then for each giv, check to see if its only use dies in a following
4582 memory address. If so, generate a new memory address and check to see
4583 if it is valid. If it is valid, then store the modified memory address,
4584 otherwise, mark the giv as not done so that it will get its own iv. */
4586 /* ??? Could try to optimize branches when it is known that a biv is always
4587 positive. */
4589 /* ??? When replace a biv in a compare insn, we should replace with closest
4590 giv so that an optimized branch can still be recognized by the combiner,
4591 e.g. the VAX acb insn. */
4593 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4594 was rerun in loop_optimize whenever a register was added or moved.
4595 Also, some of the optimizations could be a little less conservative. */
4597 /* Searches the insns between INSN and LOOP->END. Returns 1 if there
4598 is a backward branch in that range that branches to somewhere between
4599 LOOP->START and INSN. Returns 0 otherwise. */
4601 /* ??? This is quadratic algorithm. Could be rewritten to be linear.
4602 In practice, this is not a problem, because this function is seldom called,
4603 and uses a negligible amount of CPU time on average. */
4605 static int
4606 back_branch_in_range_p (const struct loop *loop, rtx insn)
4608 rtx p, q, target_insn;
4609 rtx loop_start = loop->start;
4610 rtx loop_end = loop->end;
4611 rtx orig_loop_end = loop->end;
4613 /* Stop before we get to the backward branch at the end of the loop. */
4614 loop_end = prev_nonnote_insn (loop_end);
4615 if (BARRIER_P (loop_end))
4616 loop_end = PREV_INSN (loop_end);
4618 /* Check in case insn has been deleted, search forward for first non
4619 deleted insn following it. */
4620 while (INSN_DELETED_P (insn))
4621 insn = NEXT_INSN (insn);
4623 /* Check for the case where insn is the last insn in the loop. Deal
4624 with the case where INSN was a deleted loop test insn, in which case
4625 it will now be the NOTE_LOOP_END. */
4626 if (insn == loop_end || insn == orig_loop_end)
4627 return 0;
4629 for (p = NEXT_INSN (insn); p != loop_end; p = NEXT_INSN (p))
4631 if (JUMP_P (p))
4633 target_insn = JUMP_LABEL (p);
4635 /* Search from loop_start to insn, to see if one of them is
4636 the target_insn. We can't use INSN_LUID comparisons here,
4637 since insn may not have an LUID entry. */
4638 for (q = loop_start; q != insn; q = NEXT_INSN (q))
4639 if (q == target_insn)
4640 return 1;
4644 return 0;
4647 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4648 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4649 callback.
4651 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4652 least once for every loop iteration except for the last one.
4654 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4655 loop iteration.
4657 typedef rtx (*loop_insn_callback) (struct loop *, rtx, int, int);
4658 static void
4659 for_each_insn_in_loop (struct loop *loop, loop_insn_callback fncall)
4661 int not_every_iteration = 0;
4662 int maybe_multiple = 0;
4663 int past_loop_latch = 0;
4664 bool exit_test_is_entry = false;
4665 rtx p;
4667 /* If loop_scan_start points to the loop exit test, the loop body
4668 cannot be counted on running on every iteration, and we have to
4669 be wary of subversive use of gotos inside expression
4670 statements. */
4671 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4673 exit_test_is_entry = true;
4674 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4677 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4678 for (p = next_insn_in_loop (loop, loop->scan_start);
4679 p != NULL_RTX;
4680 p = next_insn_in_loop (loop, p))
4682 p = fncall (loop, p, not_every_iteration, maybe_multiple);
4684 /* Past CODE_LABEL, we get to insns that may be executed multiple
4685 times. The only way we can be sure that they can't is if every
4686 jump insn between here and the end of the loop either
4687 returns, exits the loop, is a jump to a location that is still
4688 behind the label, or is a jump to the loop start. */
4690 if (LABEL_P (p))
4692 rtx insn = p;
4694 maybe_multiple = 0;
4696 while (1)
4698 insn = NEXT_INSN (insn);
4699 if (insn == loop->scan_start)
4700 break;
4701 if (insn == loop->end)
4703 if (loop->top != 0)
4704 insn = loop->top;
4705 else
4706 break;
4707 if (insn == loop->scan_start)
4708 break;
4711 if (JUMP_P (insn)
4712 && GET_CODE (PATTERN (insn)) != RETURN
4713 && (!any_condjump_p (insn)
4714 || (JUMP_LABEL (insn) != 0
4715 && JUMP_LABEL (insn) != loop->scan_start
4716 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4718 maybe_multiple = 1;
4719 break;
4724 /* Past a jump, we get to insns for which we can't count
4725 on whether they will be executed during each iteration. */
4726 /* This code appears twice in strength_reduce. There is also similar
4727 code in scan_loop. */
4728 if (JUMP_P (p)
4729 /* If we enter the loop in the middle, and scan around to the
4730 beginning, don't set not_every_iteration for that.
4731 This can be any kind of jump, since we want to know if insns
4732 will be executed if the loop is executed. */
4733 && (exit_test_is_entry
4734 || !(JUMP_LABEL (p) == loop->top
4735 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4736 && any_uncondjump_p (p))
4737 || (NEXT_INSN (p) == loop->end
4738 && any_condjump_p (p))))))
4740 rtx label = 0;
4742 /* If this is a jump outside the loop, then it also doesn't
4743 matter. Check to see if the target of this branch is on the
4744 loop->exits_labels list. */
4746 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4747 if (XEXP (label, 0) == JUMP_LABEL (p))
4748 break;
4750 if (!label)
4751 not_every_iteration = 1;
4754 /* Note if we pass a loop latch. If we do, then we can not clear
4755 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4756 a loop since a jump before the last CODE_LABEL may have started
4757 a new loop iteration.
4759 Note that LOOP_TOP is only set for rotated loops and we need
4760 this check for all loops, so compare against the CODE_LABEL
4761 which immediately follows LOOP_START. */
4762 if (JUMP_P (p)
4763 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4764 past_loop_latch = 1;
4766 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4767 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4768 or not an insn is known to be executed each iteration of the
4769 loop, whether or not any iterations are known to occur.
4771 Therefore, if we have just passed a label and have no more labels
4772 between here and the test insn of the loop, and we have not passed
4773 a jump to the top of the loop, then we know these insns will be
4774 executed each iteration. */
4776 if (not_every_iteration
4777 && !past_loop_latch
4778 && LABEL_P (p)
4779 && no_labels_between_p (p, loop->end))
4780 not_every_iteration = 0;
4784 static void
4785 loop_bivs_find (struct loop *loop)
4787 struct loop_regs *regs = LOOP_REGS (loop);
4788 struct loop_ivs *ivs = LOOP_IVS (loop);
4789 /* Temporary list pointers for traversing ivs->list. */
4790 struct iv_class *bl, **backbl;
4792 ivs->list = 0;
4794 for_each_insn_in_loop (loop, check_insn_for_bivs);
4796 /* Scan ivs->list to remove all regs that proved not to be bivs.
4797 Make a sanity check against regs->n_times_set. */
4798 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4800 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4801 /* Above happens if register modified by subreg, etc. */
4802 /* Make sure it is not recognized as a basic induction var: */
4803 || regs->array[bl->regno].n_times_set != bl->biv_count
4804 /* If never incremented, it is invariant that we decided not to
4805 move. So leave it alone. */
4806 || ! bl->incremented)
4808 if (dump_file)
4809 fprintf (dump_file, "Biv %d: discarded, %s\n",
4810 bl->regno,
4811 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4812 ? "not induction variable"
4813 : (! bl->incremented ? "never incremented"
4814 : "count error")));
4816 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4817 *backbl = bl->next;
4819 else
4821 backbl = &bl->next;
4823 if (dump_file)
4824 fprintf (dump_file, "Biv %d: verified\n", bl->regno);
4830 /* Determine how BIVS are initialized by looking through pre-header
4831 extended basic block. */
4832 static void
4833 loop_bivs_init_find (struct loop *loop)
4835 struct loop_ivs *ivs = LOOP_IVS (loop);
4836 /* Temporary list pointers for traversing ivs->list. */
4837 struct iv_class *bl;
4838 int call_seen;
4839 rtx p;
4841 /* Find initial value for each biv by searching backwards from loop_start,
4842 halting at first label. Also record any test condition. */
4844 call_seen = 0;
4845 for (p = loop->start; p && !LABEL_P (p); p = PREV_INSN (p))
4847 rtx test;
4849 note_insn = p;
4851 if (CALL_P (p))
4852 call_seen = 1;
4854 if (INSN_P (p))
4855 note_stores (PATTERN (p), record_initial, ivs);
4857 /* Record any test of a biv that branches around the loop if no store
4858 between it and the start of loop. We only care about tests with
4859 constants and registers and only certain of those. */
4860 if (JUMP_P (p)
4861 && JUMP_LABEL (p) != 0
4862 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4863 && (test = get_condition_for_loop (loop, p)) != 0
4864 && REG_P (XEXP (test, 0))
4865 && REGNO (XEXP (test, 0)) < max_reg_before_loop
4866 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4867 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4868 && bl->init_insn == 0)
4870 /* If an NE test, we have an initial value! */
4871 if (GET_CODE (test) == NE)
4873 bl->init_insn = p;
4874 bl->init_set = gen_rtx_SET (VOIDmode,
4875 XEXP (test, 0), XEXP (test, 1));
4877 else
4878 bl->initial_test = test;
4884 /* Look at the each biv and see if we can say anything better about its
4885 initial value from any initializing insns set up above. (This is done
4886 in two passes to avoid missing SETs in a PARALLEL.) */
4887 static void
4888 loop_bivs_check (struct loop *loop)
4890 struct loop_ivs *ivs = LOOP_IVS (loop);
4891 /* Temporary list pointers for traversing ivs->list. */
4892 struct iv_class *bl;
4893 struct iv_class **backbl;
4895 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4897 rtx src;
4898 rtx note;
4900 if (! bl->init_insn)
4901 continue;
4903 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4904 is a constant, use the value of that. */
4905 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4906 && CONSTANT_P (XEXP (note, 0)))
4907 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4908 && CONSTANT_P (XEXP (note, 0))))
4909 src = XEXP (note, 0);
4910 else
4911 src = SET_SRC (bl->init_set);
4913 if (dump_file)
4914 fprintf (dump_file,
4915 "Biv %d: initialized at insn %d: initial value ",
4916 bl->regno, INSN_UID (bl->init_insn));
4918 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4919 || GET_MODE (src) == VOIDmode)
4920 && valid_initial_value_p (src, bl->init_insn,
4921 LOOP_INFO (loop)->pre_header_has_call,
4922 loop->start))
4924 bl->initial_value = src;
4926 if (dump_file)
4928 print_simple_rtl (dump_file, src);
4929 fputc ('\n', dump_file);
4932 /* If we can't make it a giv,
4933 let biv keep initial value of "itself". */
4934 else if (dump_file)
4935 fprintf (dump_file, "is complex\n");
4940 /* Search the loop for general induction variables. */
4942 static void
4943 loop_givs_find (struct loop* loop)
4945 for_each_insn_in_loop (loop, check_insn_for_givs);
4949 /* For each giv for which we still don't know whether or not it is
4950 replaceable, check to see if it is replaceable because its final value
4951 can be calculated. */
4953 static void
4954 loop_givs_check (struct loop *loop)
4956 struct loop_ivs *ivs = LOOP_IVS (loop);
4957 struct iv_class *bl;
4959 for (bl = ivs->list; bl; bl = bl->next)
4961 struct induction *v;
4963 for (v = bl->giv; v; v = v->next_iv)
4964 if (! v->replaceable && ! v->not_replaceable)
4965 check_final_value (loop, v);
4969 /* Try to generate the simplest rtx for the expression
4970 (PLUS (MULT mult1 mult2) add1). This is used to calculate the initial
4971 value of giv's. */
4973 static rtx
4974 fold_rtx_mult_add (rtx mult1, rtx mult2, rtx add1, enum machine_mode mode)
4976 rtx temp, mult_res;
4977 rtx result;
4979 /* The modes must all be the same. This should always be true. For now,
4980 check to make sure. */
4981 gcc_assert (GET_MODE (mult1) == mode || GET_MODE (mult1) == VOIDmode);
4982 gcc_assert (GET_MODE (mult2) == mode || GET_MODE (mult2) == VOIDmode);
4983 gcc_assert (GET_MODE (add1) == mode || GET_MODE (add1) == VOIDmode);
4985 /* Ensure that if at least one of mult1/mult2 are constant, then mult2
4986 will be a constant. */
4987 if (GET_CODE (mult1) == CONST_INT)
4989 temp = mult2;
4990 mult2 = mult1;
4991 mult1 = temp;
4994 mult_res = simplify_binary_operation (MULT, mode, mult1, mult2);
4995 if (! mult_res)
4996 mult_res = gen_rtx_MULT (mode, mult1, mult2);
4998 /* Again, put the constant second. */
4999 if (GET_CODE (add1) == CONST_INT)
5001 temp = add1;
5002 add1 = mult_res;
5003 mult_res = temp;
5006 result = simplify_binary_operation (PLUS, mode, add1, mult_res);
5007 if (! result)
5008 result = gen_rtx_PLUS (mode, add1, mult_res);
5010 return result;
5013 /* Searches the list of induction struct's for the biv BL, to try to calculate
5014 the total increment value for one iteration of the loop as a constant.
5016 Returns the increment value as an rtx, simplified as much as possible,
5017 if it can be calculated. Otherwise, returns 0. */
5019 static rtx
5020 biv_total_increment (const struct iv_class *bl)
5022 struct induction *v;
5023 rtx result;
5025 /* For increment, must check every instruction that sets it. Each
5026 instruction must be executed only once each time through the loop.
5027 To verify this, we check that the insn is always executed, and that
5028 there are no backward branches after the insn that branch to before it.
5029 Also, the insn must have a mult_val of one (to make sure it really is
5030 an increment). */
5032 result = const0_rtx;
5033 for (v = bl->biv; v; v = v->next_iv)
5035 if (v->always_computable && v->mult_val == const1_rtx
5036 && ! v->maybe_multiple
5037 && SCALAR_INT_MODE_P (v->mode))
5039 /* If we have already counted it, skip it. */
5040 if (v->same)
5041 continue;
5043 result = fold_rtx_mult_add (result, const1_rtx, v->add_val, v->mode);
5045 else
5046 return 0;
5049 return result;
5052 /* Try to prove that the register is dead after the loop exits. Trace every
5053 loop exit looking for an insn that will always be executed, which sets
5054 the register to some value, and appears before the first use of the register
5055 is found. If successful, then return 1, otherwise return 0. */
5057 /* ?? Could be made more intelligent in the handling of jumps, so that
5058 it can search past if statements and other similar structures. */
5060 static int
5061 reg_dead_after_loop (const struct loop *loop, rtx reg)
5063 rtx insn, label;
5064 int jump_count = 0;
5065 int label_count = 0;
5067 /* In addition to checking all exits of this loop, we must also check
5068 all exits of inner nested loops that would exit this loop. We don't
5069 have any way to identify those, so we just give up if there are any
5070 such inner loop exits. */
5072 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
5073 label_count++;
5075 if (label_count != loop->exit_count)
5076 return 0;
5078 /* HACK: Must also search the loop fall through exit, create a label_ref
5079 here which points to the loop->end, and append the loop_number_exit_labels
5080 list to it. */
5081 label = gen_rtx_LABEL_REF (Pmode, loop->end);
5082 LABEL_NEXTREF (label) = loop->exit_labels;
5084 for (; label; label = LABEL_NEXTREF (label))
5086 /* Succeed if find an insn which sets the biv or if reach end of
5087 function. Fail if find an insn that uses the biv, or if come to
5088 a conditional jump. */
5090 insn = NEXT_INSN (XEXP (label, 0));
5091 while (insn)
5093 if (INSN_P (insn))
5095 rtx set, note;
5097 if (reg_referenced_p (reg, PATTERN (insn)))
5098 return 0;
5100 note = find_reg_equal_equiv_note (insn);
5101 if (note && reg_overlap_mentioned_p (reg, XEXP (note, 0)))
5102 return 0;
5104 set = single_set (insn);
5105 if (set && rtx_equal_p (SET_DEST (set), reg))
5106 break;
5108 if (JUMP_P (insn))
5110 if (GET_CODE (PATTERN (insn)) == RETURN)
5111 break;
5112 else if (!any_uncondjump_p (insn)
5113 /* Prevent infinite loop following infinite loops. */
5114 || jump_count++ > 20)
5115 return 0;
5116 else
5117 insn = JUMP_LABEL (insn);
5121 insn = NEXT_INSN (insn);
5125 /* Success, the register is dead on all loop exits. */
5126 return 1;
5129 /* Try to calculate the final value of the biv, the value it will have at
5130 the end of the loop. If we can do it, return that value. */
5132 static rtx
5133 final_biv_value (const struct loop *loop, struct iv_class *bl)
5135 unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
5136 rtx increment, tem;
5138 /* ??? This only works for MODE_INT biv's. Reject all others for now. */
5140 if (GET_MODE_CLASS (bl->biv->mode) != MODE_INT)
5141 return 0;
5143 /* The final value for reversed bivs must be calculated differently than
5144 for ordinary bivs. In this case, there is already an insn after the
5145 loop which sets this biv's final value (if necessary), and there are
5146 no other loop exits, so we can return any value. */
5147 if (bl->reversed)
5149 if (dump_file)
5150 fprintf (dump_file,
5151 "Final biv value for %d, reversed biv.\n", bl->regno);
5153 return const0_rtx;
5156 /* Try to calculate the final value as initial value + (number of iterations
5157 * increment). For this to work, increment must be invariant, the only
5158 exit from the loop must be the fall through at the bottom (otherwise
5159 it may not have its final value when the loop exits), and the initial
5160 value of the biv must be invariant. */
5162 if (n_iterations != 0
5163 && ! loop->exit_count
5164 && loop_invariant_p (loop, bl->initial_value))
5166 increment = biv_total_increment (bl);
5168 if (increment && loop_invariant_p (loop, increment))
5170 /* Can calculate the loop exit value, emit insns after loop
5171 end to calculate this value into a temporary register in
5172 case it is needed later. */
5174 tem = gen_reg_rtx (bl->biv->mode);
5175 record_base_value (REGNO (tem), bl->biv->add_val, 0);
5176 loop_iv_add_mult_sink (loop, increment, GEN_INT (n_iterations),
5177 bl->initial_value, tem);
5179 if (dump_file)
5180 fprintf (dump_file,
5181 "Final biv value for %d, calculated.\n", bl->regno);
5183 return tem;
5187 /* Check to see if the biv is dead at all loop exits. */
5188 if (reg_dead_after_loop (loop, bl->biv->src_reg))
5190 if (dump_file)
5191 fprintf (dump_file,
5192 "Final biv value for %d, biv dead after loop exit.\n",
5193 bl->regno);
5195 return const0_rtx;
5198 return 0;
5201 /* Return nonzero if it is possible to eliminate the biv BL provided
5202 all givs are reduced. This is possible if either the reg is not
5203 used outside the loop, or we can compute what its final value will
5204 be. */
5206 static int
5207 loop_biv_eliminable_p (struct loop *loop, struct iv_class *bl,
5208 int threshold, int insn_count)
5210 /* For architectures with a decrement_and_branch_until_zero insn,
5211 don't do this if we put a REG_NONNEG note on the endtest for this
5212 biv. */
5214 #ifdef HAVE_decrement_and_branch_until_zero
5215 if (bl->nonneg)
5217 if (dump_file)
5218 fprintf (dump_file,
5219 "Cannot eliminate nonneg biv %d.\n", bl->regno);
5220 return 0;
5222 #endif
5224 /* Check that biv is used outside loop or if it has a final value.
5225 Compare against bl->init_insn rather than loop->start. We aren't
5226 concerned with any uses of the biv between init_insn and
5227 loop->start since these won't be affected by the value of the biv
5228 elsewhere in the function, so long as init_insn doesn't use the
5229 biv itself. */
5231 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
5232 && bl->init_insn
5233 && INSN_UID (bl->init_insn) < max_uid_for_loop
5234 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
5235 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
5236 || (bl->final_value = final_biv_value (loop, bl)))
5237 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
5239 if (dump_file)
5241 fprintf (dump_file,
5242 "Cannot eliminate biv %d.\n",
5243 bl->regno);
5244 fprintf (dump_file,
5245 "First use: insn %d, last use: insn %d.\n",
5246 REGNO_FIRST_UID (bl->regno),
5247 REGNO_LAST_UID (bl->regno));
5249 return 0;
5253 /* Reduce each giv of BL that we have decided to reduce. */
5255 static void
5256 loop_givs_reduce (struct loop *loop, struct iv_class *bl)
5258 struct induction *v;
5260 for (v = bl->giv; v; v = v->next_iv)
5262 struct induction *tv;
5263 if (! v->ignore && v->same == 0)
5265 int auto_inc_opt = 0;
5267 /* If the code for derived givs immediately below has already
5268 allocated a new_reg, we must keep it. */
5269 if (! v->new_reg)
5270 v->new_reg = gen_reg_rtx (v->mode);
5272 #ifdef AUTO_INC_DEC
5273 /* If the target has auto-increment addressing modes, and
5274 this is an address giv, then try to put the increment
5275 immediately after its use, so that flow can create an
5276 auto-increment addressing mode. */
5277 /* Don't do this for loops entered at the bottom, to avoid
5278 this invalid transformation:
5279 jmp L; -> jmp L;
5280 TOP: TOP:
5281 use giv use giv
5282 L: inc giv
5283 inc biv L:
5284 test biv test giv
5285 cbr TOP cbr TOP
5287 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
5288 && bl->biv->always_executed && ! bl->biv->maybe_multiple
5289 /* We don't handle reversed biv's because bl->biv->insn
5290 does not have a valid INSN_LUID. */
5291 && ! bl->reversed
5292 && v->always_executed && ! v->maybe_multiple
5293 && INSN_UID (v->insn) < max_uid_for_loop
5294 && !loop->top)
5296 /* If other giv's have been combined with this one, then
5297 this will work only if all uses of the other giv's occur
5298 before this giv's insn. This is difficult to check.
5300 We simplify this by looking for the common case where
5301 there is one DEST_REG giv, and this giv's insn is the
5302 last use of the dest_reg of that DEST_REG giv. If the
5303 increment occurs after the address giv, then we can
5304 perform the optimization. (Otherwise, the increment
5305 would have to go before other_giv, and we would not be
5306 able to combine it with the address giv to get an
5307 auto-inc address.) */
5308 if (v->combined_with)
5310 struct induction *other_giv = 0;
5312 for (tv = bl->giv; tv; tv = tv->next_iv)
5313 if (tv->same == v)
5315 if (other_giv)
5316 break;
5317 else
5318 other_giv = tv;
5320 if (! tv && other_giv
5321 && REGNO (other_giv->dest_reg) < max_reg_before_loop
5322 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
5323 == INSN_UID (v->insn))
5324 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
5325 auto_inc_opt = 1;
5327 /* Check for case where increment is before the address
5328 giv. Do this test in "loop order". */
5329 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
5330 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
5331 || (INSN_LUID (bl->biv->insn)
5332 > INSN_LUID (loop->scan_start))))
5333 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
5334 && (INSN_LUID (loop->scan_start)
5335 < INSN_LUID (bl->biv->insn))))
5336 auto_inc_opt = -1;
5337 else
5338 auto_inc_opt = 1;
5340 #ifdef HAVE_cc0
5342 rtx prev;
5344 /* We can't put an insn immediately after one setting
5345 cc0, or immediately before one using cc0. */
5346 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
5347 || (auto_inc_opt == -1
5348 && (prev = prev_nonnote_insn (v->insn)) != 0
5349 && INSN_P (prev)
5350 && sets_cc0_p (PATTERN (prev))))
5351 auto_inc_opt = 0;
5353 #endif
5355 if (auto_inc_opt)
5356 v->auto_inc_opt = 1;
5358 #endif
5360 /* For each place where the biv is incremented, add an insn
5361 to increment the new, reduced reg for the giv. */
5362 for (tv = bl->biv; tv; tv = tv->next_iv)
5364 rtx insert_before;
5366 /* Skip if location is the same as a previous one. */
5367 if (tv->same)
5368 continue;
5369 if (! auto_inc_opt)
5370 insert_before = NEXT_INSN (tv->insn);
5371 else if (auto_inc_opt == 1)
5372 insert_before = NEXT_INSN (v->insn);
5373 else
5374 insert_before = v->insn;
5376 if (tv->mult_val == const1_rtx)
5377 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
5378 v->new_reg, v->new_reg,
5379 0, insert_before);
5380 else /* tv->mult_val == const0_rtx */
5381 /* A multiply is acceptable here
5382 since this is presumed to be seldom executed. */
5383 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
5384 v->add_val, v->new_reg,
5385 0, insert_before);
5388 /* Add code at loop start to initialize giv's reduced reg. */
5390 loop_iv_add_mult_hoist (loop,
5391 extend_value_for_giv (v, bl->initial_value),
5392 v->mult_val, v->add_val, v->new_reg);
5398 /* Check for givs whose first use is their definition and whose
5399 last use is the definition of another giv. If so, it is likely
5400 dead and should not be used to derive another giv nor to
5401 eliminate a biv. */
5403 static void
5404 loop_givs_dead_check (struct loop *loop ATTRIBUTE_UNUSED, struct iv_class *bl)
5406 struct induction *v;
5408 for (v = bl->giv; v; v = v->next_iv)
5410 if (v->ignore
5411 || (v->same && v->same->ignore))
5412 continue;
5414 if (v->giv_type == DEST_REG
5415 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
5417 struct induction *v1;
5419 for (v1 = bl->giv; v1; v1 = v1->next_iv)
5420 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
5421 v->maybe_dead = 1;
5427 static void
5428 loop_givs_rescan (struct loop *loop, struct iv_class *bl, rtx *reg_map)
5430 struct induction *v;
5432 for (v = bl->giv; v; v = v->next_iv)
5434 if (v->same && v->same->ignore)
5435 v->ignore = 1;
5437 if (v->ignore)
5438 continue;
5440 /* Update expression if this was combined, in case other giv was
5441 replaced. */
5442 if (v->same)
5443 v->new_reg = replace_rtx (v->new_reg,
5444 v->same->dest_reg, v->same->new_reg);
5446 /* See if this register is known to be a pointer to something. If
5447 so, see if we can find the alignment. First see if there is a
5448 destination register that is a pointer. If so, this shares the
5449 alignment too. Next see if we can deduce anything from the
5450 computational information. If not, and this is a DEST_ADDR
5451 giv, at least we know that it's a pointer, though we don't know
5452 the alignment. */
5453 if (REG_P (v->new_reg)
5454 && v->giv_type == DEST_REG
5455 && REG_POINTER (v->dest_reg))
5456 mark_reg_pointer (v->new_reg,
5457 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
5458 else if (REG_P (v->new_reg)
5459 && REG_POINTER (v->src_reg))
5461 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
5463 if (align == 0
5464 || GET_CODE (v->add_val) != CONST_INT
5465 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
5466 align = 0;
5468 mark_reg_pointer (v->new_reg, align);
5470 else if (REG_P (v->new_reg)
5471 && REG_P (v->add_val)
5472 && REG_POINTER (v->add_val))
5474 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
5476 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
5477 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
5478 align = 0;
5480 mark_reg_pointer (v->new_reg, align);
5482 else if (REG_P (v->new_reg) && v->giv_type == DEST_ADDR)
5483 mark_reg_pointer (v->new_reg, 0);
5485 if (v->giv_type == DEST_ADDR)
5487 /* Store reduced reg as the address in the memref where we found
5488 this giv. */
5489 if (validate_change_maybe_volatile (v->insn, v->location,
5490 v->new_reg))
5491 /* Yay, it worked! */;
5492 /* Not replaceable; emit an insn to set the original
5493 giv reg from the reduced giv. */
5494 else if (REG_P (*v->location))
5496 rtx tem;
5497 start_sequence ();
5498 tem = force_operand (v->new_reg, *v->location);
5499 if (tem != *v->location)
5500 emit_move_insn (*v->location, tem);
5501 tem = get_insns ();
5502 end_sequence ();
5503 loop_insn_emit_before (loop, 0, v->insn, tem);
5505 else if (GET_CODE (*v->location) == PLUS
5506 && REG_P (XEXP (*v->location, 0))
5507 && CONSTANT_P (XEXP (*v->location, 1)))
5509 rtx tem;
5510 start_sequence ();
5511 tem = expand_simple_binop (GET_MODE (*v->location), MINUS,
5512 v->new_reg, XEXP (*v->location, 1),
5513 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5514 emit_move_insn (XEXP (*v->location, 0), tem);
5515 tem = get_insns ();
5516 end_sequence ();
5517 loop_insn_emit_before (loop, 0, v->insn, tem);
5519 else
5521 /* If it wasn't a reg, create a pseudo and use that. */
5522 rtx reg, seq;
5523 start_sequence ();
5524 reg = force_reg (v->mode, *v->location);
5525 if (validate_change_maybe_volatile (v->insn, v->location, reg))
5527 seq = get_insns ();
5528 end_sequence ();
5529 loop_insn_emit_before (loop, 0, v->insn, seq);
5531 else
5533 end_sequence ();
5534 if (dump_file)
5535 fprintf (dump_file,
5536 "unable to reduce iv in insn %d\n",
5537 INSN_UID (v->insn));
5538 bl->all_reduced = 0;
5539 v->ignore = 1;
5540 continue;
5544 else if (v->replaceable)
5546 reg_map[REGNO (v->dest_reg)] = v->new_reg;
5548 else
5550 rtx original_insn = v->insn;
5551 rtx note;
5553 /* Not replaceable; emit an insn to set the original giv reg from
5554 the reduced giv, same as above. */
5555 v->insn = loop_insn_emit_after (loop, 0, original_insn,
5556 gen_move_insn (v->dest_reg,
5557 v->new_reg));
5559 /* The original insn may have a REG_EQUAL note. This note is
5560 now incorrect and may result in invalid substitutions later.
5561 The original insn is dead, but may be part of a libcall
5562 sequence, which doesn't seem worth the bother of handling. */
5563 note = find_reg_note (original_insn, REG_EQUAL, NULL_RTX);
5564 if (note)
5565 remove_note (original_insn, note);
5568 /* When a loop is reversed, givs which depend on the reversed
5569 biv, and which are live outside the loop, must be set to their
5570 correct final value. This insn is only needed if the giv is
5571 not replaceable. The correct final value is the same as the
5572 value that the giv starts the reversed loop with. */
5573 if (bl->reversed && ! v->replaceable)
5574 loop_iv_add_mult_sink (loop,
5575 extend_value_for_giv (v, bl->initial_value),
5576 v->mult_val, v->add_val, v->dest_reg);
5577 else if (v->final_value)
5578 loop_insn_sink_or_swim (loop,
5579 gen_load_of_final_value (v->dest_reg,
5580 v->final_value));
5582 if (dump_file)
5584 fprintf (dump_file, "giv at %d reduced to ",
5585 INSN_UID (v->insn));
5586 print_simple_rtl (dump_file, v->new_reg);
5587 fprintf (dump_file, "\n");
5593 static int
5594 loop_giv_reduce_benefit (struct loop *loop ATTRIBUTE_UNUSED,
5595 struct iv_class *bl, struct induction *v,
5596 rtx test_reg)
5598 int add_cost;
5599 int benefit;
5601 benefit = v->benefit;
5602 PUT_MODE (test_reg, v->mode);
5603 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
5604 test_reg, test_reg);
5606 /* Reduce benefit if not replaceable, since we will insert a
5607 move-insn to replace the insn that calculates this giv. Don't do
5608 this unless the giv is a user variable, since it will often be
5609 marked non-replaceable because of the duplication of the exit
5610 code outside the loop. In such a case, the copies we insert are
5611 dead and will be deleted. So they don't have a cost. Similar
5612 situations exist. */
5613 /* ??? The new final_[bg]iv_value code does a much better job of
5614 finding replaceable giv's, and hence this code may no longer be
5615 necessary. */
5616 if (! v->replaceable && ! bl->eliminable
5617 && REG_USERVAR_P (v->dest_reg))
5618 benefit -= copy_cost;
5620 /* Decrease the benefit to count the add-insns that we will insert
5621 to increment the reduced reg for the giv. ??? This can
5622 overestimate the run-time cost of the additional insns, e.g. if
5623 there are multiple basic blocks that increment the biv, but only
5624 one of these blocks is executed during each iteration. There is
5625 no good way to detect cases like this with the current structure
5626 of the loop optimizer. This code is more accurate for
5627 determining code size than run-time benefits. */
5628 benefit -= add_cost * bl->biv_count;
5630 /* Decide whether to strength-reduce this giv or to leave the code
5631 unchanged (recompute it from the biv each time it is used). This
5632 decision can be made independently for each giv. */
5634 #ifdef AUTO_INC_DEC
5635 /* Attempt to guess whether autoincrement will handle some of the
5636 new add insns; if so, increase BENEFIT (undo the subtraction of
5637 add_cost that was done above). */
5638 if (v->giv_type == DEST_ADDR
5639 /* Increasing the benefit is risky, since this is only a guess.
5640 Avoid increasing register pressure in cases where there would
5641 be no other benefit from reducing this giv. */
5642 && benefit > 0
5643 && GET_CODE (v->mult_val) == CONST_INT)
5645 int size = GET_MODE_SIZE (GET_MODE (v->mem));
5647 if (HAVE_POST_INCREMENT
5648 && INTVAL (v->mult_val) == size)
5649 benefit += add_cost * bl->biv_count;
5650 else if (HAVE_PRE_INCREMENT
5651 && INTVAL (v->mult_val) == size)
5652 benefit += add_cost * bl->biv_count;
5653 else if (HAVE_POST_DECREMENT
5654 && -INTVAL (v->mult_val) == size)
5655 benefit += add_cost * bl->biv_count;
5656 else if (HAVE_PRE_DECREMENT
5657 && -INTVAL (v->mult_val) == size)
5658 benefit += add_cost * bl->biv_count;
5660 #endif
5662 return benefit;
5666 /* Free IV structures for LOOP. */
5668 static void
5669 loop_ivs_free (struct loop *loop)
5671 struct loop_ivs *ivs = LOOP_IVS (loop);
5672 struct iv_class *iv = ivs->list;
5674 free (ivs->regs);
5676 while (iv)
5678 struct iv_class *next = iv->next;
5679 struct induction *induction;
5680 struct induction *next_induction;
5682 for (induction = iv->biv; induction; induction = next_induction)
5684 next_induction = induction->next_iv;
5685 free (induction);
5687 for (induction = iv->giv; induction; induction = next_induction)
5689 next_induction = induction->next_iv;
5690 free (induction);
5693 free (iv);
5694 iv = next;
5698 /* Look back before LOOP->START for the insn that sets REG and return
5699 the equivalent constant if there is a REG_EQUAL note otherwise just
5700 the SET_SRC of REG. */
5702 static rtx
5703 loop_find_equiv_value (const struct loop *loop, rtx reg)
5705 rtx loop_start = loop->start;
5706 rtx insn, set;
5707 rtx ret;
5709 ret = reg;
5710 for (insn = PREV_INSN (loop_start); insn; insn = PREV_INSN (insn))
5712 if (LABEL_P (insn))
5713 break;
5715 else if (INSN_P (insn) && reg_set_p (reg, insn))
5717 /* We found the last insn before the loop that sets the register.
5718 If it sets the entire register, and has a REG_EQUAL note,
5719 then use the value of the REG_EQUAL note. */
5720 if ((set = single_set (insn))
5721 && (SET_DEST (set) == reg))
5723 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
5725 /* Only use the REG_EQUAL note if it is a constant.
5726 Other things, divide in particular, will cause
5727 problems later if we use them. */
5728 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST
5729 && CONSTANT_P (XEXP (note, 0)))
5730 ret = XEXP (note, 0);
5731 else
5732 ret = SET_SRC (set);
5734 /* We cannot do this if it changes between the
5735 assignment and loop start though. */
5736 if (modified_between_p (ret, insn, loop_start))
5737 ret = reg;
5739 break;
5742 return ret;
5745 /* Find and return register term common to both expressions OP0 and
5746 OP1 or NULL_RTX if no such term exists. Each expression must be a
5747 REG or a PLUS of a REG. */
5749 static rtx
5750 find_common_reg_term (rtx op0, rtx op1)
5752 if ((REG_P (op0) || GET_CODE (op0) == PLUS)
5753 && (REG_P (op1) || GET_CODE (op1) == PLUS))
5755 rtx op00;
5756 rtx op01;
5757 rtx op10;
5758 rtx op11;
5760 if (GET_CODE (op0) == PLUS)
5761 op01 = XEXP (op0, 1), op00 = XEXP (op0, 0);
5762 else
5763 op01 = const0_rtx, op00 = op0;
5765 if (GET_CODE (op1) == PLUS)
5766 op11 = XEXP (op1, 1), op10 = XEXP (op1, 0);
5767 else
5768 op11 = const0_rtx, op10 = op1;
5770 /* Find and return common register term if present. */
5771 if (REG_P (op00) && (op00 == op10 || op00 == op11))
5772 return op00;
5773 else if (REG_P (op01) && (op01 == op10 || op01 == op11))
5774 return op01;
5777 /* No common register term found. */
5778 return NULL_RTX;
5781 /* Determine the loop iterator and calculate the number of loop
5782 iterations. Returns the exact number of loop iterations if it can
5783 be calculated, otherwise returns zero. */
5785 static unsigned HOST_WIDE_INT
5786 loop_iterations (struct loop *loop)
5788 struct loop_info *loop_info = LOOP_INFO (loop);
5789 struct loop_ivs *ivs = LOOP_IVS (loop);
5790 rtx comparison, comparison_value;
5791 rtx iteration_var, initial_value, increment, final_value;
5792 enum rtx_code comparison_code;
5793 HOST_WIDE_INT inc;
5794 unsigned HOST_WIDE_INT abs_inc;
5795 unsigned HOST_WIDE_INT abs_diff;
5796 int off_by_one;
5797 int increment_dir;
5798 int unsigned_p, compare_dir, final_larger;
5799 rtx last_loop_insn;
5800 struct iv_class *bl;
5802 loop_info->n_iterations = 0;
5803 loop_info->initial_value = 0;
5804 loop_info->initial_equiv_value = 0;
5805 loop_info->comparison_value = 0;
5806 loop_info->final_value = 0;
5807 loop_info->final_equiv_value = 0;
5808 loop_info->increment = 0;
5809 loop_info->iteration_var = 0;
5810 loop_info->iv = 0;
5812 /* We used to use prev_nonnote_insn here, but that fails because it might
5813 accidentally get the branch for a contained loop if the branch for this
5814 loop was deleted. We can only trust branches immediately before the
5815 loop_end. */
5816 last_loop_insn = PREV_INSN (loop->end);
5818 /* ??? We should probably try harder to find the jump insn
5819 at the end of the loop. The following code assumes that
5820 the last loop insn is a jump to the top of the loop. */
5821 if (!JUMP_P (last_loop_insn))
5823 if (dump_file)
5824 fprintf (dump_file,
5825 "Loop iterations: No final conditional branch found.\n");
5826 return 0;
5829 /* If there is a more than a single jump to the top of the loop
5830 we cannot (easily) determine the iteration count. */
5831 if (LABEL_NUSES (JUMP_LABEL (last_loop_insn)) > 1)
5833 if (dump_file)
5834 fprintf (dump_file,
5835 "Loop iterations: Loop has multiple back edges.\n");
5836 return 0;
5839 /* Find the iteration variable. If the last insn is a conditional
5840 branch, and the insn before tests a register value, make that the
5841 iteration variable. */
5843 comparison = get_condition_for_loop (loop, last_loop_insn);
5844 if (comparison == 0)
5846 if (dump_file)
5847 fprintf (dump_file,
5848 "Loop iterations: No final comparison found.\n");
5849 return 0;
5852 /* ??? Get_condition may switch position of induction variable and
5853 invariant register when it canonicalizes the comparison. */
5855 comparison_code = GET_CODE (comparison);
5856 iteration_var = XEXP (comparison, 0);
5857 comparison_value = XEXP (comparison, 1);
5859 if (!REG_P (iteration_var))
5861 if (dump_file)
5862 fprintf (dump_file,
5863 "Loop iterations: Comparison not against register.\n");
5864 return 0;
5867 /* The only new registers that are created before loop iterations
5868 are givs made from biv increments or registers created by
5869 load_mems. In the latter case, it is possible that try_copy_prop
5870 will propagate a new pseudo into the old iteration register but
5871 this will be marked by having the REG_USERVAR_P bit set. */
5873 gcc_assert ((unsigned) REGNO (iteration_var) < ivs->n_regs
5874 || REG_USERVAR_P (iteration_var));
5876 /* Determine the initial value of the iteration variable, and the amount
5877 that it is incremented each loop. Use the tables constructed by
5878 the strength reduction pass to calculate these values. */
5880 /* Clear the result values, in case no answer can be found. */
5881 initial_value = 0;
5882 increment = 0;
5884 /* The iteration variable can be either a giv or a biv. Check to see
5885 which it is, and compute the variable's initial value, and increment
5886 value if possible. */
5888 /* If this is a new register, can't handle it since we don't have any
5889 reg_iv_type entry for it. */
5890 if ((unsigned) REGNO (iteration_var) >= ivs->n_regs)
5892 if (dump_file)
5893 fprintf (dump_file,
5894 "Loop iterations: No reg_iv_type entry for iteration var.\n");
5895 return 0;
5898 /* Reject iteration variables larger than the host wide int size, since they
5899 could result in a number of iterations greater than the range of our
5900 `unsigned HOST_WIDE_INT' variable loop_info->n_iterations. */
5901 else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var))
5902 > HOST_BITS_PER_WIDE_INT))
5904 if (dump_file)
5905 fprintf (dump_file,
5906 "Loop iterations: Iteration var rejected because mode too large.\n");
5907 return 0;
5909 else if (GET_MODE_CLASS (GET_MODE (iteration_var)) != MODE_INT)
5911 if (dump_file)
5912 fprintf (dump_file,
5913 "Loop iterations: Iteration var not an integer.\n");
5914 return 0;
5917 /* Try swapping the comparison to identify a suitable iv. */
5918 if (REG_IV_TYPE (ivs, REGNO (iteration_var)) != BASIC_INDUCT
5919 && REG_IV_TYPE (ivs, REGNO (iteration_var)) != GENERAL_INDUCT
5920 && REG_P (comparison_value)
5921 && REGNO (comparison_value) < ivs->n_regs)
5923 rtx temp = comparison_value;
5924 comparison_code = swap_condition (comparison_code);
5925 comparison_value = iteration_var;
5926 iteration_var = temp;
5929 if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
5931 gcc_assert (REGNO (iteration_var) < ivs->n_regs);
5933 /* Grab initial value, only useful if it is a constant. */
5934 bl = REG_IV_CLASS (ivs, REGNO (iteration_var));
5935 initial_value = bl->initial_value;
5936 if (!bl->biv->always_executed || bl->biv->maybe_multiple)
5938 if (dump_file)
5939 fprintf (dump_file,
5940 "Loop iterations: Basic induction var not set once in each iteration.\n");
5941 return 0;
5944 increment = biv_total_increment (bl);
5946 else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
5948 HOST_WIDE_INT offset = 0;
5949 struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
5950 rtx biv_initial_value;
5952 gcc_assert (REGNO (v->src_reg) < ivs->n_regs);
5954 if (!v->always_executed || v->maybe_multiple)
5956 if (dump_file)
5957 fprintf (dump_file,
5958 "Loop iterations: General induction var not set once in each iteration.\n");
5959 return 0;
5962 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5964 /* Increment value is mult_val times the increment value of the biv. */
5966 increment = biv_total_increment (bl);
5967 if (increment)
5969 struct induction *biv_inc;
5971 increment = fold_rtx_mult_add (v->mult_val,
5972 extend_value_for_giv (v, increment),
5973 const0_rtx, v->mode);
5974 /* The caller assumes that one full increment has occurred at the
5975 first loop test. But that's not true when the biv is incremented
5976 after the giv is set (which is the usual case), e.g.:
5977 i = 6; do {;} while (i++ < 9) .
5978 Therefore, we bias the initial value by subtracting the amount of
5979 the increment that occurs between the giv set and the giv test. */
5980 for (biv_inc = bl->biv; biv_inc; biv_inc = biv_inc->next_iv)
5982 if (loop_insn_first_p (v->insn, biv_inc->insn))
5984 if (REG_P (biv_inc->add_val))
5986 if (dump_file)
5987 fprintf (dump_file,
5988 "Loop iterations: Basic induction var add_val is REG %d.\n",
5989 REGNO (biv_inc->add_val));
5990 return 0;
5993 /* If we have already counted it, skip it. */
5994 if (biv_inc->same)
5995 continue;
5997 offset -= INTVAL (biv_inc->add_val);
6001 if (dump_file)
6002 fprintf (dump_file,
6003 "Loop iterations: Giv iterator, initial value bias %ld.\n",
6004 (long) offset);
6006 /* Initial value is mult_val times the biv's initial value plus
6007 add_val. Only useful if it is a constant. */
6008 biv_initial_value = extend_value_for_giv (v, bl->initial_value);
6009 initial_value
6010 = fold_rtx_mult_add (v->mult_val,
6011 plus_constant (biv_initial_value, offset),
6012 v->add_val, v->mode);
6014 else
6016 if (dump_file)
6017 fprintf (dump_file,
6018 "Loop iterations: Not basic or general induction var.\n");
6019 return 0;
6022 if (initial_value == 0)
6023 return 0;
6025 unsigned_p = 0;
6026 off_by_one = 0;
6027 switch (comparison_code)
6029 case LEU:
6030 unsigned_p = 1;
6031 case LE:
6032 compare_dir = 1;
6033 off_by_one = 1;
6034 break;
6035 case GEU:
6036 unsigned_p = 1;
6037 case GE:
6038 compare_dir = -1;
6039 off_by_one = -1;
6040 break;
6041 case EQ:
6042 /* Cannot determine loop iterations with this case. */
6043 compare_dir = 0;
6044 break;
6045 case LTU:
6046 unsigned_p = 1;
6047 case LT:
6048 compare_dir = 1;
6049 break;
6050 case GTU:
6051 unsigned_p = 1;
6052 case GT:
6053 compare_dir = -1;
6054 break;
6055 case NE:
6056 compare_dir = 0;
6057 break;
6058 default:
6059 gcc_unreachable ();
6062 /* If the comparison value is an invariant register, then try to find
6063 its value from the insns before the start of the loop. */
6065 final_value = comparison_value;
6066 if (REG_P (comparison_value)
6067 && loop_invariant_p (loop, comparison_value))
6069 final_value = loop_find_equiv_value (loop, comparison_value);
6071 /* If we don't get an invariant final value, we are better
6072 off with the original register. */
6073 if (! loop_invariant_p (loop, final_value))
6074 final_value = comparison_value;
6077 /* Calculate the approximate final value of the induction variable
6078 (on the last successful iteration). The exact final value
6079 depends on the branch operator, and increment sign. It will be
6080 wrong if the iteration variable is not incremented by one each
6081 time through the loop and (comparison_value + off_by_one -
6082 initial_value) % increment != 0.
6083 ??? Note that the final_value may overflow and thus final_larger
6084 will be bogus. A potentially infinite loop will be classified
6085 as immediate, e.g. for (i = 0x7ffffff0; i <= 0x7fffffff; i++) */
6086 if (off_by_one)
6087 final_value = plus_constant (final_value, off_by_one);
6089 /* Save the calculated values describing this loop's bounds, in case
6090 precondition_loop_p will need them later. These values can not be
6091 recalculated inside precondition_loop_p because strength reduction
6092 optimizations may obscure the loop's structure.
6094 These values are only required by precondition_loop_p and insert_bct
6095 whenever the number of iterations cannot be computed at compile time.
6096 Only the difference between final_value and initial_value is
6097 important. Note that final_value is only approximate. */
6098 loop_info->initial_value = initial_value;
6099 loop_info->comparison_value = comparison_value;
6100 loop_info->final_value = plus_constant (comparison_value, off_by_one);
6101 loop_info->increment = increment;
6102 loop_info->iteration_var = iteration_var;
6103 loop_info->comparison_code = comparison_code;
6104 loop_info->iv = bl;
6106 /* Try to determine the iteration count for loops such
6107 as (for i = init; i < init + const; i++). When running the
6108 loop optimization twice, the first pass often converts simple
6109 loops into this form. */
6111 if (REG_P (initial_value))
6113 rtx reg1;
6114 rtx reg2;
6115 rtx const2;
6117 reg1 = initial_value;
6118 if (GET_CODE (final_value) == PLUS)
6119 reg2 = XEXP (final_value, 0), const2 = XEXP (final_value, 1);
6120 else
6121 reg2 = final_value, const2 = const0_rtx;
6123 /* Check for initial_value = reg1, final_value = reg2 + const2,
6124 where reg1 != reg2. */
6125 if (REG_P (reg2) && reg2 != reg1)
6127 rtx temp;
6129 /* Find what reg1 is equivalent to. Hopefully it will
6130 either be reg2 or reg2 plus a constant. */
6131 temp = loop_find_equiv_value (loop, reg1);
6133 if (find_common_reg_term (temp, reg2))
6134 initial_value = temp;
6135 else if (loop_invariant_p (loop, reg2))
6137 /* Find what reg2 is equivalent to. Hopefully it will
6138 either be reg1 or reg1 plus a constant. Let's ignore
6139 the latter case for now since it is not so common. */
6140 temp = loop_find_equiv_value (loop, reg2);
6142 if (temp == loop_info->iteration_var)
6143 temp = initial_value;
6144 if (temp == reg1)
6145 final_value = (const2 == const0_rtx)
6146 ? reg1 : gen_rtx_PLUS (GET_MODE (reg1), reg1, const2);
6151 loop_info->initial_equiv_value = initial_value;
6152 loop_info->final_equiv_value = final_value;
6154 /* For EQ comparison loops, we don't have a valid final value.
6155 Check this now so that we won't leave an invalid value if we
6156 return early for any other reason. */
6157 if (comparison_code == EQ)
6158 loop_info->final_equiv_value = loop_info->final_value = 0;
6160 if (increment == 0)
6162 if (dump_file)
6163 fprintf (dump_file,
6164 "Loop iterations: Increment value can't be calculated.\n");
6165 return 0;
6168 if (GET_CODE (increment) != CONST_INT)
6170 /* If we have a REG, check to see if REG holds a constant value. */
6171 /* ??? Other RTL, such as (neg (reg)) is possible here, but it isn't
6172 clear if it is worthwhile to try to handle such RTL. */
6173 if (REG_P (increment) || GET_CODE (increment) == SUBREG)
6174 increment = loop_find_equiv_value (loop, increment);
6176 if (GET_CODE (increment) != CONST_INT)
6178 if (dump_file)
6180 fprintf (dump_file,
6181 "Loop iterations: Increment value not constant ");
6182 print_simple_rtl (dump_file, increment);
6183 fprintf (dump_file, ".\n");
6185 return 0;
6187 loop_info->increment = increment;
6190 if (GET_CODE (initial_value) != CONST_INT)
6192 if (dump_file)
6194 fprintf (dump_file,
6195 "Loop iterations: Initial value not constant ");
6196 print_simple_rtl (dump_file, initial_value);
6197 fprintf (dump_file, ".\n");
6199 return 0;
6201 else if (GET_CODE (final_value) != CONST_INT)
6203 if (dump_file)
6205 fprintf (dump_file,
6206 "Loop iterations: Final value not constant ");
6207 print_simple_rtl (dump_file, final_value);
6208 fprintf (dump_file, ".\n");
6210 return 0;
6212 else if (comparison_code == EQ)
6214 rtx inc_once;
6216 if (dump_file)
6217 fprintf (dump_file, "Loop iterations: EQ comparison loop.\n");
6219 inc_once = gen_int_mode (INTVAL (initial_value) + INTVAL (increment),
6220 GET_MODE (iteration_var));
6222 if (inc_once == final_value)
6224 /* The iterator value once through the loop is equal to the
6225 comparison value. Either we have an infinite loop, or
6226 we'll loop twice. */
6227 if (increment == const0_rtx)
6228 return 0;
6229 loop_info->n_iterations = 2;
6231 else
6232 loop_info->n_iterations = 1;
6234 if (GET_CODE (loop_info->initial_value) == CONST_INT)
6235 loop_info->final_value
6236 = gen_int_mode ((INTVAL (loop_info->initial_value)
6237 + loop_info->n_iterations * INTVAL (increment)),
6238 GET_MODE (iteration_var));
6239 else
6240 loop_info->final_value
6241 = plus_constant (loop_info->initial_value,
6242 loop_info->n_iterations * INTVAL (increment));
6243 loop_info->final_equiv_value
6244 = gen_int_mode ((INTVAL (initial_value)
6245 + loop_info->n_iterations * INTVAL (increment)),
6246 GET_MODE (iteration_var));
6247 return loop_info->n_iterations;
6250 /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1. */
6251 if (unsigned_p)
6252 final_larger
6253 = ((unsigned HOST_WIDE_INT) INTVAL (final_value)
6254 > (unsigned HOST_WIDE_INT) INTVAL (initial_value))
6255 - ((unsigned HOST_WIDE_INT) INTVAL (final_value)
6256 < (unsigned HOST_WIDE_INT) INTVAL (initial_value));
6257 else
6258 final_larger = (INTVAL (final_value) > INTVAL (initial_value))
6259 - (INTVAL (final_value) < INTVAL (initial_value));
6261 if (INTVAL (increment) > 0)
6262 increment_dir = 1;
6263 else if (INTVAL (increment) == 0)
6264 increment_dir = 0;
6265 else
6266 increment_dir = -1;
6268 /* There are 27 different cases: compare_dir = -1, 0, 1;
6269 final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
6270 There are 4 normal cases, 4 reverse cases (where the iteration variable
6271 will overflow before the loop exits), 4 infinite loop cases, and 15
6272 immediate exit (0 or 1 iteration depending on loop type) cases.
6273 Only try to optimize the normal cases. */
6275 /* (compare_dir/final_larger/increment_dir)
6276 Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
6277 Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
6278 Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
6279 Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
6281 /* ?? If the meaning of reverse loops (where the iteration variable
6282 will overflow before the loop exits) is undefined, then could
6283 eliminate all of these special checks, and just always assume
6284 the loops are normal/immediate/infinite. Note that this means
6285 the sign of increment_dir does not have to be known. Also,
6286 since it does not really hurt if immediate exit loops or infinite loops
6287 are optimized, then that case could be ignored also, and hence all
6288 loops can be optimized.
6290 According to ANSI Spec, the reverse loop case result is undefined,
6291 because the action on overflow is undefined.
6293 See also the special test for NE loops below. */
6295 if (final_larger == increment_dir && final_larger != 0
6296 && (final_larger == compare_dir || compare_dir == 0))
6297 /* Normal case. */
6299 else
6301 if (dump_file)
6302 fprintf (dump_file, "Loop iterations: Not normal loop.\n");
6303 return 0;
6306 /* Calculate the number of iterations, final_value is only an approximation,
6307 so correct for that. Note that abs_diff and n_iterations are
6308 unsigned, because they can be as large as 2^n - 1. */
6310 inc = INTVAL (increment);
6311 gcc_assert (inc);
6312 if (inc > 0)
6314 abs_diff = INTVAL (final_value) - INTVAL (initial_value);
6315 abs_inc = inc;
6317 else
6319 abs_diff = INTVAL (initial_value) - INTVAL (final_value);
6320 abs_inc = -inc;
6323 /* Given that iteration_var is going to iterate over its own mode,
6324 not HOST_WIDE_INT, disregard higher bits that might have come
6325 into the picture due to sign extension of initial and final
6326 values. */
6327 abs_diff &= ((unsigned HOST_WIDE_INT) 1
6328 << (GET_MODE_BITSIZE (GET_MODE (iteration_var)) - 1)
6329 << 1) - 1;
6331 /* For NE tests, make sure that the iteration variable won't miss
6332 the final value. If abs_diff mod abs_incr is not zero, then the
6333 iteration variable will overflow before the loop exits, and we
6334 can not calculate the number of iterations. */
6335 if (compare_dir == 0 && (abs_diff % abs_inc) != 0)
6336 return 0;
6338 /* Note that the number of iterations could be calculated using
6339 (abs_diff + abs_inc - 1) / abs_inc, provided care was taken to
6340 handle potential overflow of the summation. */
6341 loop_info->n_iterations = abs_diff / abs_inc + ((abs_diff % abs_inc) != 0);
6342 return loop_info->n_iterations;
6345 /* Perform strength reduction and induction variable elimination.
6347 Pseudo registers created during this function will be beyond the
6348 last valid index in several tables including
6349 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
6350 problem here, because the added registers cannot be givs outside of
6351 their loop, and hence will never be reconsidered. But scan_loop
6352 must check regnos to make sure they are in bounds. */
6354 static void
6355 strength_reduce (struct loop *loop, int flags)
6357 struct loop_info *loop_info = LOOP_INFO (loop);
6358 struct loop_regs *regs = LOOP_REGS (loop);
6359 struct loop_ivs *ivs = LOOP_IVS (loop);
6360 rtx p;
6361 /* Temporary list pointer for traversing ivs->list. */
6362 struct iv_class *bl;
6363 /* Ratio of extra register life span we can justify
6364 for saving an instruction. More if loop doesn't call subroutines
6365 since in that case saving an insn makes more difference
6366 and more registers are available. */
6367 /* ??? could set this to last value of threshold in move_movables */
6368 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
6369 /* Map of pseudo-register replacements. */
6370 rtx *reg_map = NULL;
6371 int reg_map_size;
6372 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
6373 int insn_count = count_insns_in_loop (loop);
6375 addr_placeholder = gen_reg_rtx (Pmode);
6377 ivs->n_regs = max_reg_before_loop;
6378 ivs->regs = XCNEWVEC (struct iv, ivs->n_regs);
6380 /* Find all BIVs in loop. */
6381 loop_bivs_find (loop);
6383 /* Exit if there are no bivs. */
6384 if (! ivs->list)
6386 loop_ivs_free (loop);
6387 return;
6390 /* Determine how BIVS are initialized by looking through pre-header
6391 extended basic block. */
6392 loop_bivs_init_find (loop);
6394 /* Look at the each biv and see if we can say anything better about its
6395 initial value from any initializing insns set up above. */
6396 loop_bivs_check (loop);
6398 /* Search the loop for general induction variables. */
6399 loop_givs_find (loop);
6401 /* Try to calculate and save the number of loop iterations. This is
6402 set to zero if the actual number can not be calculated. This must
6403 be called after all giv's have been identified, since otherwise it may
6404 fail if the iteration variable is a giv. */
6405 loop_iterations (loop);
6407 #ifdef HAVE_prefetch
6408 if (flags & LOOP_PREFETCH)
6409 emit_prefetch_instructions (loop);
6410 #endif
6412 /* Now for each giv for which we still don't know whether or not it is
6413 replaceable, check to see if it is replaceable because its final value
6414 can be calculated. This must be done after loop_iterations is called,
6415 so that final_giv_value will work correctly. */
6416 loop_givs_check (loop);
6418 /* Try to prove that the loop counter variable (if any) is always
6419 nonnegative; if so, record that fact with a REG_NONNEG note
6420 so that "decrement and branch until zero" insn can be used. */
6421 check_dbra_loop (loop, insn_count);
6423 /* Create reg_map to hold substitutions for replaceable giv regs.
6424 Some givs might have been made from biv increments, so look at
6425 ivs->reg_iv_type for a suitable size. */
6426 reg_map_size = ivs->n_regs;
6427 reg_map = XCNEWVEC (rtx, reg_map_size);
6429 /* Examine each iv class for feasibility of strength reduction/induction
6430 variable elimination. */
6432 for (bl = ivs->list; bl; bl = bl->next)
6434 struct induction *v;
6435 int benefit;
6437 /* Test whether it will be possible to eliminate this biv
6438 provided all givs are reduced. */
6439 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
6441 /* This will be true at the end, if all givs which depend on this
6442 biv have been strength reduced.
6443 We can't (currently) eliminate the biv unless this is so. */
6444 bl->all_reduced = 1;
6446 /* Check each extension dependent giv in this class to see if its
6447 root biv is safe from wrapping in the interior mode. */
6448 check_ext_dependent_givs (loop, bl);
6450 /* Combine all giv's for this iv_class. */
6451 combine_givs (regs, bl);
6453 for (v = bl->giv; v; v = v->next_iv)
6455 struct induction *tv;
6457 if (v->ignore || v->same)
6458 continue;
6460 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
6462 /* If an insn is not to be strength reduced, then set its ignore
6463 flag, and clear bl->all_reduced. */
6465 /* A giv that depends on a reversed biv must be reduced if it is
6466 used after the loop exit, otherwise, it would have the wrong
6467 value after the loop exit. To make it simple, just reduce all
6468 of such giv's whether or not we know they are used after the loop
6469 exit. */
6471 if (v->lifetime * threshold * benefit < insn_count
6472 && ! bl->reversed)
6474 if (dump_file)
6475 fprintf (dump_file,
6476 "giv of insn %d not worth while, %d vs %d.\n",
6477 INSN_UID (v->insn),
6478 v->lifetime * threshold * benefit, insn_count);
6479 v->ignore = 1;
6480 bl->all_reduced = 0;
6482 else if (!v->always_computable
6483 && (may_trap_or_fault_p (v->add_val)
6484 || may_trap_or_fault_p (v->mult_val)))
6486 if (dump_file)
6487 fprintf (dump_file,
6488 "giv of insn %d: not always computable.\n",
6489 INSN_UID (v->insn));
6490 v->ignore = 1;
6491 bl->all_reduced = 0;
6493 else
6495 /* Check that we can increment the reduced giv without a
6496 multiply insn. If not, reject it. */
6498 for (tv = bl->biv; tv; tv = tv->next_iv)
6499 if (tv->mult_val == const1_rtx
6500 && ! product_cheap_p (tv->add_val, v->mult_val))
6502 if (dump_file)
6503 fprintf (dump_file,
6504 "giv of insn %d: would need a multiply.\n",
6505 INSN_UID (v->insn));
6506 v->ignore = 1;
6507 bl->all_reduced = 0;
6508 break;
6513 /* Check for givs whose first use is their definition and whose
6514 last use is the definition of another giv. If so, it is likely
6515 dead and should not be used to derive another giv nor to
6516 eliminate a biv. */
6517 loop_givs_dead_check (loop, bl);
6519 /* Reduce each giv that we decided to reduce. */
6520 loop_givs_reduce (loop, bl);
6522 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
6523 as not reduced.
6525 For each giv register that can be reduced now: if replaceable,
6526 substitute reduced reg wherever the old giv occurs;
6527 else add new move insn "giv_reg = reduced_reg". */
6528 loop_givs_rescan (loop, bl, reg_map);
6530 /* All the givs based on the biv bl have been reduced if they
6531 merit it. */
6533 /* For each giv not marked as maybe dead that has been combined with a
6534 second giv, clear any "maybe dead" mark on that second giv.
6535 v->new_reg will either be or refer to the register of the giv it
6536 combined with.
6538 Doing this clearing avoids problems in biv elimination where
6539 a giv's new_reg is a complex value that can't be put in the
6540 insn but the giv combined with (with a reg as new_reg) is
6541 marked maybe_dead. Since the register will be used in either
6542 case, we'd prefer it be used from the simpler giv. */
6544 for (v = bl->giv; v; v = v->next_iv)
6545 if (! v->maybe_dead && v->same)
6546 v->same->maybe_dead = 0;
6548 /* Try to eliminate the biv, if it is a candidate.
6549 This won't work if ! bl->all_reduced,
6550 since the givs we planned to use might not have been reduced.
6552 We have to be careful that we didn't initially think we could
6553 eliminate this biv because of a giv that we now think may be
6554 dead and shouldn't be used as a biv replacement.
6556 Also, there is the possibility that we may have a giv that looks
6557 like it can be used to eliminate a biv, but the resulting insn
6558 isn't valid. This can happen, for example, on the 88k, where a
6559 JUMP_INSN can compare a register only with zero. Attempts to
6560 replace it with a compare with a constant will fail.
6562 Note that in cases where this call fails, we may have replaced some
6563 of the occurrences of the biv with a giv, but no harm was done in
6564 doing so in the rare cases where it can occur. */
6566 if (bl->all_reduced == 1 && bl->eliminable
6567 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
6569 /* ?? If we created a new test to bypass the loop entirely,
6570 or otherwise drop straight in, based on this test, then
6571 we might want to rewrite it also. This way some later
6572 pass has more hope of removing the initialization of this
6573 biv entirely. */
6575 /* If final_value != 0, then the biv may be used after loop end
6576 and we must emit an insn to set it just in case.
6578 Reversed bivs already have an insn after the loop setting their
6579 value, so we don't need another one. We can't calculate the
6580 proper final value for such a biv here anyways. */
6581 if (bl->final_value && ! bl->reversed)
6582 loop_insn_sink_or_swim (loop,
6583 gen_load_of_final_value (bl->biv->dest_reg,
6584 bl->final_value));
6586 if (dump_file)
6587 fprintf (dump_file, "Reg %d: biv eliminated\n",
6588 bl->regno);
6590 /* See above note wrt final_value. But since we couldn't eliminate
6591 the biv, we must set the value after the loop instead of before. */
6592 else if (bl->final_value && ! bl->reversed)
6593 loop_insn_sink (loop, gen_load_of_final_value (bl->biv->dest_reg,
6594 bl->final_value));
6597 /* Go through all the instructions in the loop, making all the
6598 register substitutions scheduled in REG_MAP. */
6600 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
6601 if (INSN_P (p))
6603 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
6604 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
6605 INSN_CODE (p) = -1;
6608 if (dump_file)
6609 fprintf (dump_file, "\n");
6611 loop_ivs_free (loop);
6612 if (reg_map)
6613 free (reg_map);
6616 /*Record all basic induction variables calculated in the insn. */
6617 static rtx
6618 check_insn_for_bivs (struct loop *loop, rtx p, int not_every_iteration,
6619 int maybe_multiple)
6621 struct loop_ivs *ivs = LOOP_IVS (loop);
6622 rtx set;
6623 rtx dest_reg;
6624 rtx inc_val;
6625 rtx mult_val;
6626 rtx *location;
6628 if (NONJUMP_INSN_P (p)
6629 && (set = single_set (p))
6630 && REG_P (SET_DEST (set)))
6632 dest_reg = SET_DEST (set);
6633 if (REGNO (dest_reg) < max_reg_before_loop
6634 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
6635 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
6637 if (basic_induction_var (loop, SET_SRC (set),
6638 GET_MODE (SET_SRC (set)),
6639 dest_reg, p, &inc_val, &mult_val,
6640 &location))
6642 /* It is a possible basic induction variable.
6643 Create and initialize an induction structure for it. */
6645 struct induction *v = XNEW (struct induction);
6647 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
6648 not_every_iteration, maybe_multiple);
6649 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
6651 else if (REGNO (dest_reg) < ivs->n_regs)
6652 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
6655 return p;
6658 /* Record all givs calculated in the insn.
6659 A register is a giv if: it is only set once, it is a function of a
6660 biv and a constant (or invariant), and it is not a biv. */
6661 static rtx
6662 check_insn_for_givs (struct loop *loop, rtx p, int not_every_iteration,
6663 int maybe_multiple)
6665 struct loop_regs *regs = LOOP_REGS (loop);
6667 rtx set;
6668 /* Look for a general induction variable in a register. */
6669 if (NONJUMP_INSN_P (p)
6670 && (set = single_set (p))
6671 && REG_P (SET_DEST (set))
6672 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
6674 rtx src_reg;
6675 rtx dest_reg;
6676 rtx add_val;
6677 rtx mult_val;
6678 rtx ext_val;
6679 int benefit;
6680 rtx regnote = 0;
6681 rtx last_consec_insn;
6683 dest_reg = SET_DEST (set);
6684 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
6685 return p;
6687 if (/* SET_SRC is a giv. */
6688 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
6689 &mult_val, &ext_val, 0, &benefit, VOIDmode)
6690 /* Equivalent expression is a giv. */
6691 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
6692 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
6693 &add_val, &mult_val, &ext_val, 0,
6694 &benefit, VOIDmode)))
6695 /* Don't try to handle any regs made by loop optimization.
6696 We have nothing on them in regno_first_uid, etc. */
6697 && REGNO (dest_reg) < max_reg_before_loop
6698 /* Don't recognize a BASIC_INDUCT_VAR here. */
6699 && dest_reg != src_reg
6700 /* This must be the only place where the register is set. */
6701 && (regs->array[REGNO (dest_reg)].n_times_set == 1
6702 /* or all sets must be consecutive and make a giv. */
6703 || (benefit = consec_sets_giv (loop, benefit, p,
6704 src_reg, dest_reg,
6705 &add_val, &mult_val, &ext_val,
6706 &last_consec_insn))))
6708 struct induction *v = XNEW (struct induction);
6710 /* If this is a library call, increase benefit. */
6711 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6712 benefit += libcall_benefit (p);
6714 /* Skip the consecutive insns, if there are any. */
6715 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
6716 p = last_consec_insn;
6718 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
6719 ext_val, benefit, DEST_REG, not_every_iteration,
6720 maybe_multiple, (rtx*) 0);
6725 /* Look for givs which are memory addresses. */
6726 if (NONJUMP_INSN_P (p))
6727 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
6728 maybe_multiple);
6730 /* Update the status of whether giv can derive other givs. This can
6731 change when we pass a label or an insn that updates a biv. */
6732 if (INSN_P (p) || LABEL_P (p))
6733 update_giv_derive (loop, p);
6734 return p;
6737 /* Return 1 if X is a valid source for an initial value (or as value being
6738 compared against in an initial test).
6740 X must be either a register or constant and must not be clobbered between
6741 the current insn and the start of the loop.
6743 INSN is the insn containing X. */
6745 static int
6746 valid_initial_value_p (rtx x, rtx insn, int call_seen, rtx loop_start)
6748 if (CONSTANT_P (x))
6749 return 1;
6751 /* Only consider pseudos we know about initialized in insns whose luids
6752 we know. */
6753 if (!REG_P (x)
6754 || REGNO (x) >= max_reg_before_loop)
6755 return 0;
6757 /* Don't use call-clobbered registers across a call which clobbers it. On
6758 some machines, don't use any hard registers at all. */
6759 if (REGNO (x) < FIRST_PSEUDO_REGISTER
6760 && (SMALL_REGISTER_CLASSES
6761 || (call_seen && call_used_regs[REGNO (x)])))
6762 return 0;
6764 /* Don't use registers that have been clobbered before the start of the
6765 loop. */
6766 if (reg_set_between_p (x, insn, loop_start))
6767 return 0;
6769 return 1;
6772 /* Scan X for memory refs and check each memory address
6773 as a possible giv. INSN is the insn whose pattern X comes from.
6774 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
6775 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
6776 more than once in each loop iteration. */
6778 static void
6779 find_mem_givs (const struct loop *loop, rtx x, rtx insn,
6780 int not_every_iteration, int maybe_multiple)
6782 int i, j;
6783 enum rtx_code code;
6784 const char *fmt;
6786 if (x == 0)
6787 return;
6789 code = GET_CODE (x);
6790 switch (code)
6792 case REG:
6793 case CONST_INT:
6794 case CONST:
6795 case CONST_DOUBLE:
6796 case SYMBOL_REF:
6797 case LABEL_REF:
6798 case PC:
6799 case CC0:
6800 case ADDR_VEC:
6801 case ADDR_DIFF_VEC:
6802 case USE:
6803 case CLOBBER:
6804 return;
6806 case MEM:
6808 rtx src_reg;
6809 rtx add_val;
6810 rtx mult_val;
6811 rtx ext_val;
6812 int benefit;
6814 /* This code used to disable creating GIVs with mult_val == 1 and
6815 add_val == 0. However, this leads to lost optimizations when
6816 it comes time to combine a set of related DEST_ADDR GIVs, since
6817 this one would not be seen. */
6819 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
6820 &mult_val, &ext_val, 1, &benefit,
6821 GET_MODE (x)))
6823 /* Found one; record it. */
6824 struct induction *v = XNEW (struct induction);
6826 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
6827 add_val, ext_val, benefit, DEST_ADDR,
6828 not_every_iteration, maybe_multiple, &XEXP (x, 0));
6830 v->mem = x;
6833 return;
6835 default:
6836 break;
6839 /* Recursively scan the subexpressions for other mem refs. */
6841 fmt = GET_RTX_FORMAT (code);
6842 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6843 if (fmt[i] == 'e')
6844 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
6845 maybe_multiple);
6846 else if (fmt[i] == 'E')
6847 for (j = 0; j < XVECLEN (x, i); j++)
6848 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
6849 maybe_multiple);
6852 /* Fill in the data about one biv update.
6853 V is the `struct induction' in which we record the biv. (It is
6854 allocated by the caller, with alloca.)
6855 INSN is the insn that sets it.
6856 DEST_REG is the biv's reg.
6858 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
6859 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
6860 being set to INC_VAL.
6862 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
6863 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
6864 can be executed more than once per iteration. If MAYBE_MULTIPLE
6865 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
6866 executed exactly once per iteration. */
6868 static void
6869 record_biv (struct loop *loop, struct induction *v, rtx insn, rtx dest_reg,
6870 rtx inc_val, rtx mult_val, rtx *location,
6871 int not_every_iteration, int maybe_multiple)
6873 struct loop_ivs *ivs = LOOP_IVS (loop);
6874 struct iv_class *bl;
6876 v->insn = insn;
6877 v->src_reg = dest_reg;
6878 v->dest_reg = dest_reg;
6879 v->mult_val = mult_val;
6880 v->add_val = inc_val;
6881 v->ext_dependent = NULL_RTX;
6882 v->location = location;
6883 v->mode = GET_MODE (dest_reg);
6884 v->always_computable = ! not_every_iteration;
6885 v->always_executed = ! not_every_iteration;
6886 v->maybe_multiple = maybe_multiple;
6887 v->same = 0;
6889 /* Add this to the reg's iv_class, creating a class
6890 if this is the first incrementation of the reg. */
6892 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
6893 if (bl == 0)
6895 /* Create and initialize new iv_class. */
6897 bl = XNEW (struct iv_class);
6899 bl->regno = REGNO (dest_reg);
6900 bl->biv = 0;
6901 bl->giv = 0;
6902 bl->biv_count = 0;
6903 bl->giv_count = 0;
6905 /* Set initial value to the reg itself. */
6906 bl->initial_value = dest_reg;
6907 bl->final_value = 0;
6908 /* We haven't seen the initializing insn yet. */
6909 bl->init_insn = 0;
6910 bl->init_set = 0;
6911 bl->initial_test = 0;
6912 bl->incremented = 0;
6913 bl->eliminable = 0;
6914 bl->nonneg = 0;
6915 bl->reversed = 0;
6916 bl->total_benefit = 0;
6918 /* Add this class to ivs->list. */
6919 bl->next = ivs->list;
6920 ivs->list = bl;
6922 /* Put it in the array of biv register classes. */
6923 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
6925 else
6927 /* Check if location is the same as a previous one. */
6928 struct induction *induction;
6929 for (induction = bl->biv; induction; induction = induction->next_iv)
6930 if (location == induction->location)
6932 v->same = induction;
6933 break;
6937 /* Update IV_CLASS entry for this biv. */
6938 v->next_iv = bl->biv;
6939 bl->biv = v;
6940 bl->biv_count++;
6941 if (mult_val == const1_rtx)
6942 bl->incremented = 1;
6944 if (dump_file)
6945 loop_biv_dump (v, dump_file, 0);
6948 /* Fill in the data about one giv.
6949 V is the `struct induction' in which we record the giv. (It is
6950 allocated by the caller, with alloca.)
6951 INSN is the insn that sets it.
6952 BENEFIT estimates the savings from deleting this insn.
6953 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
6954 into a register or is used as a memory address.
6956 SRC_REG is the biv reg which the giv is computed from.
6957 DEST_REG is the giv's reg (if the giv is stored in a reg).
6958 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
6959 LOCATION points to the place where this giv's value appears in INSN. */
6961 static void
6962 record_giv (const struct loop *loop, struct induction *v, rtx insn,
6963 rtx src_reg, rtx dest_reg, rtx mult_val, rtx add_val,
6964 rtx ext_val, int benefit, enum g_types type,
6965 int not_every_iteration, int maybe_multiple, rtx *location)
6967 struct loop_ivs *ivs = LOOP_IVS (loop);
6968 struct induction *b;
6969 struct iv_class *bl;
6970 rtx set = single_set (insn);
6971 rtx temp;
6973 /* Attempt to prove constantness of the values. Don't let simplify_rtx
6974 undo the MULT canonicalization that we performed earlier. */
6975 temp = simplify_rtx (add_val);
6976 if (temp
6977 && ! (GET_CODE (add_val) == MULT
6978 && GET_CODE (temp) == ASHIFT))
6979 add_val = temp;
6981 v->insn = insn;
6982 v->src_reg = src_reg;
6983 v->giv_type = type;
6984 v->dest_reg = dest_reg;
6985 v->mult_val = mult_val;
6986 v->add_val = add_val;
6987 v->ext_dependent = ext_val;
6988 v->benefit = benefit;
6989 v->location = location;
6990 v->cant_derive = 0;
6991 v->combined_with = 0;
6992 v->maybe_multiple = maybe_multiple;
6993 v->maybe_dead = 0;
6994 v->derive_adjustment = 0;
6995 v->same = 0;
6996 v->ignore = 0;
6997 v->new_reg = 0;
6998 v->final_value = 0;
6999 v->same_insn = 0;
7000 v->auto_inc_opt = 0;
7001 v->shared = 0;
7003 /* The v->always_computable field is used in update_giv_derive, to
7004 determine whether a giv can be used to derive another giv. For a
7005 DEST_REG giv, INSN computes a new value for the giv, so its value
7006 isn't computable if INSN insn't executed every iteration.
7007 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
7008 it does not compute a new value. Hence the value is always computable
7009 regardless of whether INSN is executed each iteration. */
7011 if (type == DEST_ADDR)
7012 v->always_computable = 1;
7013 else
7014 v->always_computable = ! not_every_iteration;
7016 v->always_executed = ! not_every_iteration;
7018 if (type == DEST_ADDR)
7020 v->mode = GET_MODE (*location);
7021 v->lifetime = 1;
7023 else /* type == DEST_REG */
7025 v->mode = GET_MODE (SET_DEST (set));
7027 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
7029 /* If the lifetime is zero, it means that this register is
7030 really a dead store. So mark this as a giv that can be
7031 ignored. This will not prevent the biv from being eliminated. */
7032 if (v->lifetime == 0)
7033 v->ignore = 1;
7035 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
7036 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
7039 /* Add the giv to the class of givs computed from one biv. */
7041 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
7042 gcc_assert (bl);
7043 v->next_iv = bl->giv;
7044 bl->giv = v;
7046 /* Don't count DEST_ADDR. This is supposed to count the number of
7047 insns that calculate givs. */
7048 if (type == DEST_REG)
7049 bl->giv_count++;
7050 bl->total_benefit += benefit;
7052 if (type == DEST_ADDR)
7054 v->replaceable = 1;
7055 v->not_replaceable = 0;
7057 else
7059 /* The giv can be replaced outright by the reduced register only if all
7060 of the following conditions are true:
7061 - the insn that sets the giv is always executed on any iteration
7062 on which the giv is used at all
7063 (there are two ways to deduce this:
7064 either the insn is executed on every iteration,
7065 or all uses follow that insn in the same basic block),
7066 - the giv is not used outside the loop
7067 - no assignments to the biv occur during the giv's lifetime. */
7069 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
7070 /* Previous line always fails if INSN was moved by loop opt. */
7071 && REGNO_LAST_LUID (REGNO (dest_reg))
7072 < INSN_LUID (loop->end)
7073 && (! not_every_iteration
7074 || last_use_this_basic_block (dest_reg, insn)))
7076 /* Now check that there are no assignments to the biv within the
7077 giv's lifetime. This requires two separate checks. */
7079 /* Check each biv update, and fail if any are between the first
7080 and last use of the giv.
7082 If this loop contains an inner loop that was unrolled, then
7083 the insn modifying the biv may have been emitted by the loop
7084 unrolling code, and hence does not have a valid luid. Just
7085 mark the biv as not replaceable in this case. It is not very
7086 useful as a biv, because it is used in two different loops.
7087 It is very unlikely that we would be able to optimize the giv
7088 using this biv anyways. */
7090 v->replaceable = 1;
7091 v->not_replaceable = 0;
7092 for (b = bl->biv; b; b = b->next_iv)
7094 if (INSN_UID (b->insn) >= max_uid_for_loop
7095 || ((INSN_LUID (b->insn)
7096 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
7097 && (INSN_LUID (b->insn)
7098 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
7100 v->replaceable = 0;
7101 v->not_replaceable = 1;
7102 break;
7106 /* If there are any backwards branches that go from after the
7107 biv update to before it, then this giv is not replaceable. */
7108 if (v->replaceable)
7109 for (b = bl->biv; b; b = b->next_iv)
7110 if (back_branch_in_range_p (loop, b->insn))
7112 v->replaceable = 0;
7113 v->not_replaceable = 1;
7114 break;
7117 else
7119 /* May still be replaceable, we don't have enough info here to
7120 decide. */
7121 v->replaceable = 0;
7122 v->not_replaceable = 0;
7126 /* Record whether the add_val contains a const_int, for later use by
7127 combine_givs. */
7129 rtx tem = add_val;
7131 v->no_const_addval = 1;
7132 if (tem == const0_rtx)
7134 else if (CONSTANT_P (add_val))
7135 v->no_const_addval = 0;
7136 if (GET_CODE (tem) == PLUS)
7138 while (1)
7140 if (GET_CODE (XEXP (tem, 0)) == PLUS)
7141 tem = XEXP (tem, 0);
7142 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
7143 tem = XEXP (tem, 1);
7144 else
7145 break;
7147 if (CONSTANT_P (XEXP (tem, 1)))
7148 v->no_const_addval = 0;
7152 if (dump_file)
7153 loop_giv_dump (v, dump_file, 0);
7156 /* Try to calculate the final value of the giv, the value it will have at
7157 the end of the loop. If we can do it, return that value. */
7159 static rtx
7160 final_giv_value (const struct loop *loop, struct induction *v)
7162 struct loop_ivs *ivs = LOOP_IVS (loop);
7163 struct iv_class *bl;
7164 rtx insn;
7165 rtx increment, tem;
7166 rtx seq;
7167 rtx loop_end = loop->end;
7168 unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
7170 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
7172 /* The final value for givs which depend on reversed bivs must be calculated
7173 differently than for ordinary givs. In this case, there is already an
7174 insn after the loop which sets this giv's final value (if necessary),
7175 and there are no other loop exits, so we can return any value. */
7176 if (bl->reversed)
7178 if (dump_file)
7179 fprintf (dump_file,
7180 "Final giv value for %d, depends on reversed biv\n",
7181 REGNO (v->dest_reg));
7182 return const0_rtx;
7185 /* Try to calculate the final value as a function of the biv it depends
7186 upon. The only exit from the loop must be the fall through at the bottom
7187 and the insn that sets the giv must be executed on every iteration
7188 (otherwise the giv may not have its final value when the loop exits). */
7190 /* ??? Can calculate the final giv value by subtracting off the
7191 extra biv increments times the giv's mult_val. The loop must have
7192 only one exit for this to work, but the loop iterations does not need
7193 to be known. */
7195 if (n_iterations != 0
7196 && ! loop->exit_count
7197 && v->always_executed)
7199 /* ?? It is tempting to use the biv's value here since these insns will
7200 be put after the loop, and hence the biv will have its final value
7201 then. However, this fails if the biv is subsequently eliminated.
7202 Perhaps determine whether biv's are eliminable before trying to
7203 determine whether giv's are replaceable so that we can use the
7204 biv value here if it is not eliminable. */
7206 /* We are emitting code after the end of the loop, so we must make
7207 sure that bl->initial_value is still valid then. It will still
7208 be valid if it is invariant. */
7210 increment = biv_total_increment (bl);
7212 if (increment && loop_invariant_p (loop, increment)
7213 && loop_invariant_p (loop, bl->initial_value))
7215 /* Can calculate the loop exit value of its biv as
7216 (n_iterations * increment) + initial_value */
7218 /* The loop exit value of the giv is then
7219 (final_biv_value - extra increments) * mult_val + add_val.
7220 The extra increments are any increments to the biv which
7221 occur in the loop after the giv's value is calculated.
7222 We must search from the insn that sets the giv to the end
7223 of the loop to calculate this value. */
7225 /* Put the final biv value in tem. */
7226 tem = gen_reg_rtx (v->mode);
7227 record_base_value (REGNO (tem), bl->biv->add_val, 0);
7228 loop_iv_add_mult_sink (loop, extend_value_for_giv (v, increment),
7229 GEN_INT (n_iterations),
7230 extend_value_for_giv (v, bl->initial_value),
7231 tem);
7233 /* Subtract off extra increments as we find them. */
7234 for (insn = NEXT_INSN (v->insn); insn != loop_end;
7235 insn = NEXT_INSN (insn))
7237 struct induction *biv;
7239 for (biv = bl->biv; biv; biv = biv->next_iv)
7240 if (biv->insn == insn)
7242 start_sequence ();
7243 tem = expand_simple_binop (GET_MODE (tem), MINUS, tem,
7244 biv->add_val, NULL_RTX, 0,
7245 OPTAB_LIB_WIDEN);
7246 seq = get_insns ();
7247 end_sequence ();
7248 loop_insn_sink (loop, seq);
7252 /* Now calculate the giv's final value. */
7253 loop_iv_add_mult_sink (loop, tem, v->mult_val, v->add_val, tem);
7255 if (dump_file)
7256 fprintf (dump_file,
7257 "Final giv value for %d, calc from biv's value.\n",
7258 REGNO (v->dest_reg));
7260 return tem;
7264 /* Replaceable giv's should never reach here. */
7265 gcc_assert (!v->replaceable);
7267 /* Check to see if the biv is dead at all loop exits. */
7268 if (reg_dead_after_loop (loop, v->dest_reg))
7270 if (dump_file)
7271 fprintf (dump_file,
7272 "Final giv value for %d, giv dead after loop exit.\n",
7273 REGNO (v->dest_reg));
7275 return const0_rtx;
7278 return 0;
7281 /* All this does is determine whether a giv can be made replaceable because
7282 its final value can be calculated. This code can not be part of record_giv
7283 above, because final_giv_value requires that the number of loop iterations
7284 be known, and that can not be accurately calculated until after all givs
7285 have been identified. */
7287 static void
7288 check_final_value (const struct loop *loop, struct induction *v)
7290 rtx final_value = 0;
7292 /* DEST_ADDR givs will never reach here, because they are always marked
7293 replaceable above in record_giv. */
7295 /* The giv can be replaced outright by the reduced register only if all
7296 of the following conditions are true:
7297 - the insn that sets the giv is always executed on any iteration
7298 on which the giv is used at all
7299 (there are two ways to deduce this:
7300 either the insn is executed on every iteration,
7301 or all uses follow that insn in the same basic block),
7302 - its final value can be calculated (this condition is different
7303 than the one above in record_giv)
7304 - it's not used before the it's set
7305 - no assignments to the biv occur during the giv's lifetime. */
7307 #if 0
7308 /* This is only called now when replaceable is known to be false. */
7309 /* Clear replaceable, so that it won't confuse final_giv_value. */
7310 v->replaceable = 0;
7311 #endif
7313 if ((final_value = final_giv_value (loop, v))
7314 && (v->always_executed
7315 || last_use_this_basic_block (v->dest_reg, v->insn)))
7317 int biv_increment_seen = 0, before_giv_insn = 0;
7318 rtx p = v->insn;
7319 rtx last_giv_use;
7321 v->replaceable = 1;
7322 v->not_replaceable = 0;
7324 /* When trying to determine whether or not a biv increment occurs
7325 during the lifetime of the giv, we can ignore uses of the variable
7326 outside the loop because final_value is true. Hence we can not
7327 use regno_last_uid and regno_first_uid as above in record_giv. */
7329 /* Search the loop to determine whether any assignments to the
7330 biv occur during the giv's lifetime. Start with the insn
7331 that sets the giv, and search around the loop until we come
7332 back to that insn again.
7334 Also fail if there is a jump within the giv's lifetime that jumps
7335 to somewhere outside the lifetime but still within the loop. This
7336 catches spaghetti code where the execution order is not linear, and
7337 hence the above test fails. Here we assume that the giv lifetime
7338 does not extend from one iteration of the loop to the next, so as
7339 to make the test easier. Since the lifetime isn't known yet,
7340 this requires two loops. See also record_giv above. */
7342 last_giv_use = v->insn;
7344 while (1)
7346 p = NEXT_INSN (p);
7347 if (p == loop->end)
7349 before_giv_insn = 1;
7350 p = NEXT_INSN (loop->start);
7352 if (p == v->insn)
7353 break;
7355 if (INSN_P (p))
7357 /* It is possible for the BIV increment to use the GIV if we
7358 have a cycle. Thus we must be sure to check each insn for
7359 both BIV and GIV uses, and we must check for BIV uses
7360 first. */
7362 if (! biv_increment_seen
7363 && reg_set_p (v->src_reg, PATTERN (p)))
7364 biv_increment_seen = 1;
7366 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
7368 if (biv_increment_seen || before_giv_insn)
7370 v->replaceable = 0;
7371 v->not_replaceable = 1;
7372 break;
7374 last_giv_use = p;
7379 /* Now that the lifetime of the giv is known, check for branches
7380 from within the lifetime to outside the lifetime if it is still
7381 replaceable. */
7383 if (v->replaceable)
7385 p = v->insn;
7386 while (1)
7388 p = NEXT_INSN (p);
7389 if (p == loop->end)
7390 p = NEXT_INSN (loop->start);
7391 if (p == last_giv_use)
7392 break;
7394 if (JUMP_P (p) && JUMP_LABEL (p)
7395 && LABEL_NAME (JUMP_LABEL (p))
7396 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
7397 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
7398 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
7399 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
7401 v->replaceable = 0;
7402 v->not_replaceable = 1;
7404 if (dump_file)
7405 fprintf (dump_file,
7406 "Found branch outside giv lifetime.\n");
7408 break;
7413 /* If it is replaceable, then save the final value. */
7414 if (v->replaceable)
7415 v->final_value = final_value;
7418 if (dump_file && v->replaceable)
7419 fprintf (dump_file, "Insn %d: giv reg %d final_value replaceable\n",
7420 INSN_UID (v->insn), REGNO (v->dest_reg));
7423 /* Update the status of whether a giv can derive other givs.
7425 We need to do something special if there is or may be an update to the biv
7426 between the time the giv is defined and the time it is used to derive
7427 another giv.
7429 In addition, a giv that is only conditionally set is not allowed to
7430 derive another giv once a label has been passed.
7432 The cases we look at are when a label or an update to a biv is passed. */
7434 static void
7435 update_giv_derive (const struct loop *loop, rtx p)
7437 struct loop_ivs *ivs = LOOP_IVS (loop);
7438 struct iv_class *bl;
7439 struct induction *biv, *giv;
7440 rtx tem;
7441 int dummy;
7443 /* Search all IV classes, then all bivs, and finally all givs.
7445 There are three cases we are concerned with. First we have the situation
7446 of a giv that is only updated conditionally. In that case, it may not
7447 derive any givs after a label is passed.
7449 The second case is when a biv update occurs, or may occur, after the
7450 definition of a giv. For certain biv updates (see below) that are
7451 known to occur between the giv definition and use, we can adjust the
7452 giv definition. For others, or when the biv update is conditional,
7453 we must prevent the giv from deriving any other givs. There are two
7454 sub-cases within this case.
7456 If this is a label, we are concerned with any biv update that is done
7457 conditionally, since it may be done after the giv is defined followed by
7458 a branch here (actually, we need to pass both a jump and a label, but
7459 this extra tracking doesn't seem worth it).
7461 If this is a jump, we are concerned about any biv update that may be
7462 executed multiple times. We are actually only concerned about
7463 backward jumps, but it is probably not worth performing the test
7464 on the jump again here.
7466 If this is a biv update, we must adjust the giv status to show that a
7467 subsequent biv update was performed. If this adjustment cannot be done,
7468 the giv cannot derive further givs. */
7470 for (bl = ivs->list; bl; bl = bl->next)
7471 for (biv = bl->biv; biv; biv = biv->next_iv)
7472 if (LABEL_P (p) || JUMP_P (p)
7473 || biv->insn == p)
7475 /* Skip if location is the same as a previous one. */
7476 if (biv->same)
7477 continue;
7479 for (giv = bl->giv; giv; giv = giv->next_iv)
7481 /* If cant_derive is already true, there is no point in
7482 checking all of these conditions again. */
7483 if (giv->cant_derive)
7484 continue;
7486 /* If this giv is conditionally set and we have passed a label,
7487 it cannot derive anything. */
7488 if (LABEL_P (p) && ! giv->always_computable)
7489 giv->cant_derive = 1;
7491 /* Skip givs that have mult_val == 0, since
7492 they are really invariants. Also skip those that are
7493 replaceable, since we know their lifetime doesn't contain
7494 any biv update. */
7495 else if (giv->mult_val == const0_rtx || giv->replaceable)
7496 continue;
7498 /* The only way we can allow this giv to derive another
7499 is if this is a biv increment and we can form the product
7500 of biv->add_val and giv->mult_val. In this case, we will
7501 be able to compute a compensation. */
7502 else if (biv->insn == p)
7504 rtx ext_val_dummy;
7506 tem = 0;
7507 if (biv->mult_val == const1_rtx)
7508 tem = simplify_giv_expr (loop,
7509 gen_rtx_MULT (giv->mode,
7510 biv->add_val,
7511 giv->mult_val),
7512 &ext_val_dummy, &dummy);
7514 if (tem && giv->derive_adjustment)
7515 tem = simplify_giv_expr
7516 (loop,
7517 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
7518 &ext_val_dummy, &dummy);
7520 if (tem)
7521 giv->derive_adjustment = tem;
7522 else
7523 giv->cant_derive = 1;
7525 else if ((LABEL_P (p) && ! biv->always_computable)
7526 || (JUMP_P (p) && biv->maybe_multiple))
7527 giv->cant_derive = 1;
7532 /* Check whether an insn is an increment legitimate for a basic induction var.
7533 X is the source of insn P, or a part of it.
7534 MODE is the mode in which X should be interpreted.
7536 DEST_REG is the putative biv, also the destination of the insn.
7537 We accept patterns of these forms:
7538 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
7539 REG = INVARIANT + REG
7541 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
7542 store the additive term into *INC_VAL, and store the place where
7543 we found the additive term into *LOCATION.
7545 If X is an assignment of an invariant into DEST_REG, we set
7546 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
7548 We also want to detect a BIV when it corresponds to a variable
7549 whose mode was promoted. In that case, an increment
7550 of the variable may be a PLUS that adds a SUBREG of that variable to
7551 an invariant and then sign- or zero-extends the result of the PLUS
7552 into the variable.
7554 Most GIVs in such cases will be in the promoted mode, since that is the
7555 probably the natural computation mode (and almost certainly the mode
7556 used for addresses) on the machine. So we view the pseudo-reg containing
7557 the variable as the BIV, as if it were simply incremented.
7559 Note that treating the entire pseudo as a BIV will result in making
7560 simple increments to any GIVs based on it. However, if the variable
7561 overflows in its declared mode but not its promoted mode, the result will
7562 be incorrect. This is acceptable if the variable is signed, since
7563 overflows in such cases are undefined, but not if it is unsigned, since
7564 those overflows are defined. So we only check for SIGN_EXTEND and
7565 not ZERO_EXTEND.
7567 If we cannot find a biv, we return 0. */
7569 static int
7570 basic_induction_var (const struct loop *loop, rtx x, enum machine_mode mode,
7571 rtx dest_reg, rtx p, rtx *inc_val, rtx *mult_val,
7572 rtx **location)
7574 enum rtx_code code;
7575 rtx *argp, arg;
7576 rtx insn, set = 0, last, inc;
7578 code = GET_CODE (x);
7579 *location = NULL;
7580 switch (code)
7582 case PLUS:
7583 if (rtx_equal_p (XEXP (x, 0), dest_reg)
7584 || (GET_CODE (XEXP (x, 0)) == SUBREG
7585 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
7586 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
7588 argp = &XEXP (x, 1);
7590 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
7591 || (GET_CODE (XEXP (x, 1)) == SUBREG
7592 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
7593 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
7595 argp = &XEXP (x, 0);
7597 else
7598 return 0;
7600 arg = *argp;
7601 if (loop_invariant_p (loop, arg) != 1)
7602 return 0;
7604 /* convert_modes can emit new instructions, e.g. when arg is a loop
7605 invariant MEM and dest_reg has a different mode.
7606 These instructions would be emitted after the end of the function
7607 and then *inc_val would be an uninitialized pseudo.
7608 Detect this and bail in this case.
7609 Other alternatives to solve this can be introducing a convert_modes
7610 variant which is allowed to fail but not allowed to emit new
7611 instructions, emit these instructions before loop start and let
7612 it be garbage collected if *inc_val is never used or saving the
7613 *inc_val initialization sequence generated here and when *inc_val
7614 is going to be actually used, emit it at some suitable place. */
7615 last = get_last_insn ();
7616 inc = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
7617 if (get_last_insn () != last)
7619 delete_insns_since (last);
7620 return 0;
7623 *inc_val = inc;
7624 *mult_val = const1_rtx;
7625 *location = argp;
7626 return 1;
7628 case SUBREG:
7629 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
7630 handle addition of promoted variables.
7631 ??? The comment at the start of this function is wrong: promoted
7632 variable increments don't look like it says they do. */
7633 return basic_induction_var (loop, SUBREG_REG (x),
7634 GET_MODE (SUBREG_REG (x)),
7635 dest_reg, p, inc_val, mult_val, location);
7637 case REG:
7638 /* If this register is assigned in a previous insn, look at its
7639 source, but don't go outside the loop or past a label. */
7641 /* If this sets a register to itself, we would repeat any previous
7642 biv increment if we applied this strategy blindly. */
7643 if (rtx_equal_p (dest_reg, x))
7644 return 0;
7646 insn = p;
7647 while (1)
7649 rtx dest;
7652 insn = PREV_INSN (insn);
7654 while (insn && NOTE_P (insn)
7655 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
7657 if (!insn)
7658 break;
7659 set = single_set (insn);
7660 if (set == 0)
7661 break;
7662 dest = SET_DEST (set);
7663 if (dest == x
7664 || (GET_CODE (dest) == SUBREG
7665 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
7666 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
7667 && SUBREG_REG (dest) == x))
7668 return basic_induction_var (loop, SET_SRC (set),
7669 (GET_MODE (SET_SRC (set)) == VOIDmode
7670 ? GET_MODE (x)
7671 : GET_MODE (SET_SRC (set))),
7672 dest_reg, insn,
7673 inc_val, mult_val, location);
7675 while (GET_CODE (dest) == SUBREG
7676 || GET_CODE (dest) == ZERO_EXTRACT
7677 || GET_CODE (dest) == STRICT_LOW_PART)
7678 dest = XEXP (dest, 0);
7679 if (dest == x)
7680 break;
7682 /* Fall through. */
7684 /* Can accept constant setting of biv only when inside inner most loop.
7685 Otherwise, a biv of an inner loop may be incorrectly recognized
7686 as a biv of the outer loop,
7687 causing code to be moved INTO the inner loop. */
7688 case MEM:
7689 if (loop_invariant_p (loop, x) != 1)
7690 return 0;
7691 case CONST_INT:
7692 case SYMBOL_REF:
7693 case CONST:
7694 /* convert_modes dies if we try to convert to or from CCmode, so just
7695 exclude that case. It is very unlikely that a condition code value
7696 would be a useful iterator anyways. convert_modes dies if we try to
7697 convert a float mode to non-float or vice versa too. */
7698 if (loop->level == 1
7699 && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
7700 && GET_MODE_CLASS (mode) != MODE_CC)
7702 /* Possible bug here? Perhaps we don't know the mode of X. */
7703 last = get_last_insn ();
7704 inc = convert_modes (GET_MODE (dest_reg), mode, x, 0);
7705 if (get_last_insn () != last)
7707 delete_insns_since (last);
7708 return 0;
7711 *inc_val = inc;
7712 *mult_val = const0_rtx;
7713 return 1;
7715 else
7716 return 0;
7718 case SIGN_EXTEND:
7719 /* Ignore this BIV if signed arithmetic overflow is defined. */
7720 if (flag_wrapv)
7721 return 0;
7722 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
7723 dest_reg, p, inc_val, mult_val, location);
7725 case ASHIFTRT:
7726 /* Similar, since this can be a sign extension. */
7727 for (insn = PREV_INSN (p);
7728 (insn && NOTE_P (insn)
7729 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
7730 insn = PREV_INSN (insn))
7733 if (insn)
7734 set = single_set (insn);
7736 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
7737 && set && SET_DEST (set) == XEXP (x, 0)
7738 && GET_CODE (XEXP (x, 1)) == CONST_INT
7739 && INTVAL (XEXP (x, 1)) >= 0
7740 && GET_CODE (SET_SRC (set)) == ASHIFT
7741 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
7742 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
7743 GET_MODE (XEXP (x, 0)),
7744 dest_reg, insn, inc_val, mult_val,
7745 location);
7746 return 0;
7748 default:
7749 return 0;
7753 /* A general induction variable (giv) is any quantity that is a linear
7754 function of a basic induction variable,
7755 i.e. giv = biv * mult_val + add_val.
7756 The coefficients can be any loop invariant quantity.
7757 A giv need not be computed directly from the biv;
7758 it can be computed by way of other givs. */
7760 /* Determine whether X computes a giv.
7761 If it does, return a nonzero value
7762 which is the benefit from eliminating the computation of X;
7763 set *SRC_REG to the register of the biv that it is computed from;
7764 set *ADD_VAL and *MULT_VAL to the coefficients,
7765 such that the value of X is biv * mult + add; */
7767 static int
7768 general_induction_var (const struct loop *loop, rtx x, rtx *src_reg,
7769 rtx *add_val, rtx *mult_val, rtx *ext_val,
7770 int is_addr, int *pbenefit,
7771 enum machine_mode addr_mode)
7773 struct loop_ivs *ivs = LOOP_IVS (loop);
7774 rtx orig_x = x;
7776 /* If this is an invariant, forget it, it isn't a giv. */
7777 if (loop_invariant_p (loop, x) == 1)
7778 return 0;
7780 *pbenefit = 0;
7781 *ext_val = NULL_RTX;
7782 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
7783 if (x == 0)
7784 return 0;
7786 switch (GET_CODE (x))
7788 case USE:
7789 case CONST_INT:
7790 /* Since this is now an invariant and wasn't before, it must be a giv
7791 with MULT_VAL == 0. It doesn't matter which BIV we associate this
7792 with. */
7793 *src_reg = ivs->list->biv->dest_reg;
7794 *mult_val = const0_rtx;
7795 *add_val = x;
7796 break;
7798 case REG:
7799 /* This is equivalent to a BIV. */
7800 *src_reg = x;
7801 *mult_val = const1_rtx;
7802 *add_val = const0_rtx;
7803 break;
7805 case PLUS:
7806 /* Either (plus (biv) (invar)) or
7807 (plus (mult (biv) (invar_1)) (invar_2)). */
7808 if (GET_CODE (XEXP (x, 0)) == MULT)
7810 *src_reg = XEXP (XEXP (x, 0), 0);
7811 *mult_val = XEXP (XEXP (x, 0), 1);
7813 else
7815 *src_reg = XEXP (x, 0);
7816 *mult_val = const1_rtx;
7818 *add_val = XEXP (x, 1);
7819 break;
7821 case MULT:
7822 /* ADD_VAL is zero. */
7823 *src_reg = XEXP (x, 0);
7824 *mult_val = XEXP (x, 1);
7825 *add_val = const0_rtx;
7826 break;
7828 default:
7829 gcc_unreachable ();
7832 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
7833 unless they are CONST_INT). */
7834 if (GET_CODE (*add_val) == USE)
7835 *add_val = XEXP (*add_val, 0);
7836 if (GET_CODE (*mult_val) == USE)
7837 *mult_val = XEXP (*mult_val, 0);
7839 if (is_addr)
7840 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
7841 else
7842 *pbenefit += rtx_cost (orig_x, SET);
7844 /* Always return true if this is a giv so it will be detected as such,
7845 even if the benefit is zero or negative. This allows elimination
7846 of bivs that might otherwise not be eliminated. */
7847 return 1;
7850 /* Given an expression, X, try to form it as a linear function of a biv.
7851 We will canonicalize it to be of the form
7852 (plus (mult (BIV) (invar_1))
7853 (invar_2))
7854 with possible degeneracies.
7856 The invariant expressions must each be of a form that can be used as a
7857 machine operand. We surround then with a USE rtx (a hack, but localized
7858 and certainly unambiguous!) if not a CONST_INT for simplicity in this
7859 routine; it is the caller's responsibility to strip them.
7861 If no such canonicalization is possible (i.e., two biv's are used or an
7862 expression that is neither invariant nor a biv or giv), this routine
7863 returns 0.
7865 For a nonzero return, the result will have a code of CONST_INT, USE,
7866 REG (for a BIV), PLUS, or MULT. No other codes will occur.
7868 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
7870 static rtx sge_plus (enum machine_mode, rtx, rtx);
7871 static rtx sge_plus_constant (rtx, rtx);
7873 static rtx
7874 simplify_giv_expr (const struct loop *loop, rtx x, rtx *ext_val, int *benefit)
7876 struct loop_ivs *ivs = LOOP_IVS (loop);
7877 struct loop_regs *regs = LOOP_REGS (loop);
7878 enum machine_mode mode = GET_MODE (x);
7879 rtx arg0, arg1;
7880 rtx tem;
7882 /* If this is not an integer mode, or if we cannot do arithmetic in this
7883 mode, this can't be a giv. */
7884 if (mode != VOIDmode
7885 && (GET_MODE_CLASS (mode) != MODE_INT
7886 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
7887 return NULL_RTX;
7889 switch (GET_CODE (x))
7891 case PLUS:
7892 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
7893 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
7894 if (arg0 == 0 || arg1 == 0)
7895 return NULL_RTX;
7897 /* Put constant last, CONST_INT last if both constant. */
7898 if ((GET_CODE (arg0) == USE
7899 || GET_CODE (arg0) == CONST_INT)
7900 && ! ((GET_CODE (arg0) == USE
7901 && GET_CODE (arg1) == USE)
7902 || GET_CODE (arg1) == CONST_INT))
7903 tem = arg0, arg0 = arg1, arg1 = tem;
7905 /* Handle addition of zero, then addition of an invariant. */
7906 if (arg1 == const0_rtx)
7907 return arg0;
7908 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
7909 switch (GET_CODE (arg0))
7911 case CONST_INT:
7912 case USE:
7913 /* Adding two invariants must result in an invariant, so enclose
7914 addition operation inside a USE and return it. */
7915 if (GET_CODE (arg0) == USE)
7916 arg0 = XEXP (arg0, 0);
7917 if (GET_CODE (arg1) == USE)
7918 arg1 = XEXP (arg1, 0);
7920 if (GET_CODE (arg0) == CONST_INT)
7921 tem = arg0, arg0 = arg1, arg1 = tem;
7922 if (GET_CODE (arg1) == CONST_INT)
7923 tem = sge_plus_constant (arg0, arg1);
7924 else
7925 tem = sge_plus (mode, arg0, arg1);
7927 if (GET_CODE (tem) != CONST_INT)
7928 tem = gen_rtx_USE (mode, tem);
7929 return tem;
7931 case REG:
7932 case MULT:
7933 /* biv + invar or mult + invar. Return sum. */
7934 return gen_rtx_PLUS (mode, arg0, arg1);
7936 case PLUS:
7937 /* (a + invar_1) + invar_2. Associate. */
7938 return
7939 simplify_giv_expr (loop,
7940 gen_rtx_PLUS (mode,
7941 XEXP (arg0, 0),
7942 gen_rtx_PLUS (mode,
7943 XEXP (arg0, 1),
7944 arg1)),
7945 ext_val, benefit);
7947 default:
7948 gcc_unreachable ();
7951 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
7952 MULT to reduce cases. */
7953 if (REG_P (arg0))
7954 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
7955 if (REG_P (arg1))
7956 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
7958 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
7959 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
7960 Recurse to associate the second PLUS. */
7961 if (GET_CODE (arg1) == MULT)
7962 tem = arg0, arg0 = arg1, arg1 = tem;
7964 if (GET_CODE (arg1) == PLUS)
7965 return
7966 simplify_giv_expr (loop,
7967 gen_rtx_PLUS (mode,
7968 gen_rtx_PLUS (mode, arg0,
7969 XEXP (arg1, 0)),
7970 XEXP (arg1, 1)),
7971 ext_val, benefit);
7973 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
7974 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
7975 return NULL_RTX;
7977 if (!rtx_equal_p (arg0, arg1))
7978 return NULL_RTX;
7980 return simplify_giv_expr (loop,
7981 gen_rtx_MULT (mode,
7982 XEXP (arg0, 0),
7983 gen_rtx_PLUS (mode,
7984 XEXP (arg0, 1),
7985 XEXP (arg1, 1))),
7986 ext_val, benefit);
7988 case MINUS:
7989 /* Handle "a - b" as "a + b * (-1)". */
7990 return simplify_giv_expr (loop,
7991 gen_rtx_PLUS (mode,
7992 XEXP (x, 0),
7993 gen_rtx_MULT (mode,
7994 XEXP (x, 1),
7995 constm1_rtx)),
7996 ext_val, benefit);
7998 case MULT:
7999 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
8000 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
8001 if (arg0 == 0 || arg1 == 0)
8002 return NULL_RTX;
8004 /* Put constant last, CONST_INT last if both constant. */
8005 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
8006 && GET_CODE (arg1) != CONST_INT)
8007 tem = arg0, arg0 = arg1, arg1 = tem;
8009 /* If second argument is not now constant, not giv. */
8010 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
8011 return NULL_RTX;
8013 /* Handle multiply by 0 or 1. */
8014 if (arg1 == const0_rtx)
8015 return const0_rtx;
8017 else if (arg1 == const1_rtx)
8018 return arg0;
8020 switch (GET_CODE (arg0))
8022 case REG:
8023 /* biv * invar. Done. */
8024 return gen_rtx_MULT (mode, arg0, arg1);
8026 case CONST_INT:
8027 /* Product of two constants. */
8028 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
8030 case USE:
8031 /* invar * invar is a giv, but attempt to simplify it somehow. */
8032 if (GET_CODE (arg1) != CONST_INT)
8033 return NULL_RTX;
8035 arg0 = XEXP (arg0, 0);
8036 if (GET_CODE (arg0) == MULT)
8038 /* (invar_0 * invar_1) * invar_2. Associate. */
8039 return simplify_giv_expr (loop,
8040 gen_rtx_MULT (mode,
8041 XEXP (arg0, 0),
8042 gen_rtx_MULT (mode,
8043 XEXP (arg0,
8045 arg1)),
8046 ext_val, benefit);
8048 /* Propagate the MULT expressions to the innermost nodes. */
8049 else if (GET_CODE (arg0) == PLUS)
8051 /* (invar_0 + invar_1) * invar_2. Distribute. */
8052 return simplify_giv_expr (loop,
8053 gen_rtx_PLUS (mode,
8054 gen_rtx_MULT (mode,
8055 XEXP (arg0,
8057 arg1),
8058 gen_rtx_MULT (mode,
8059 XEXP (arg0,
8061 arg1)),
8062 ext_val, benefit);
8064 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
8066 case MULT:
8067 /* (a * invar_1) * invar_2. Associate. */
8068 return simplify_giv_expr (loop,
8069 gen_rtx_MULT (mode,
8070 XEXP (arg0, 0),
8071 gen_rtx_MULT (mode,
8072 XEXP (arg0, 1),
8073 arg1)),
8074 ext_val, benefit);
8076 case PLUS:
8077 /* (a + invar_1) * invar_2. Distribute. */
8078 return simplify_giv_expr (loop,
8079 gen_rtx_PLUS (mode,
8080 gen_rtx_MULT (mode,
8081 XEXP (arg0, 0),
8082 arg1),
8083 gen_rtx_MULT (mode,
8084 XEXP (arg0, 1),
8085 arg1)),
8086 ext_val, benefit);
8088 default:
8089 gcc_unreachable ();
8092 case ASHIFT:
8093 /* Shift by constant is multiply by power of two. */
8094 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
8095 return 0;
8097 return
8098 simplify_giv_expr (loop,
8099 gen_rtx_MULT (mode,
8100 XEXP (x, 0),
8101 GEN_INT ((HOST_WIDE_INT) 1
8102 << INTVAL (XEXP (x, 1)))),
8103 ext_val, benefit);
8105 case NEG:
8106 /* "-a" is "a * (-1)" */
8107 return simplify_giv_expr (loop,
8108 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
8109 ext_val, benefit);
8111 case NOT:
8112 /* "~a" is "-a - 1". Silly, but easy. */
8113 return simplify_giv_expr (loop,
8114 gen_rtx_MINUS (mode,
8115 gen_rtx_NEG (mode, XEXP (x, 0)),
8116 const1_rtx),
8117 ext_val, benefit);
8119 case USE:
8120 /* Already in proper form for invariant. */
8121 return x;
8123 case SIGN_EXTEND:
8124 case ZERO_EXTEND:
8125 case TRUNCATE:
8126 /* Conditionally recognize extensions of simple IVs. After we've
8127 computed loop traversal counts and verified the range of the
8128 source IV, we'll reevaluate this as a GIV. */
8129 if (*ext_val == NULL_RTX)
8131 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
8132 if (arg0 && *ext_val == NULL_RTX && REG_P (arg0))
8134 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
8135 return arg0;
8138 goto do_default;
8140 case REG:
8141 /* If this is a new register, we can't deal with it. */
8142 if (REGNO (x) >= max_reg_before_loop)
8143 return 0;
8145 /* Check for biv or giv. */
8146 switch (REG_IV_TYPE (ivs, REGNO (x)))
8148 case BASIC_INDUCT:
8149 return x;
8150 case GENERAL_INDUCT:
8152 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
8154 /* Form expression from giv and add benefit. Ensure this giv
8155 can derive another and subtract any needed adjustment if so. */
8157 /* Increasing the benefit here is risky. The only case in which it
8158 is arguably correct is if this is the only use of V. In other
8159 cases, this will artificially inflate the benefit of the current
8160 giv, and lead to suboptimal code. Thus, it is disabled, since
8161 potentially not reducing an only marginally beneficial giv is
8162 less harmful than reducing many givs that are not really
8163 beneficial. */
8165 rtx single_use = regs->array[REGNO (x)].single_usage;
8166 if (single_use && single_use != const0_rtx)
8167 *benefit += v->benefit;
8170 if (v->cant_derive)
8171 return 0;
8173 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
8174 v->src_reg, v->mult_val),
8175 v->add_val);
8177 if (v->derive_adjustment)
8178 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
8179 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
8180 if (*ext_val)
8182 if (!v->ext_dependent)
8183 return arg0;
8185 else
8187 *ext_val = v->ext_dependent;
8188 return arg0;
8190 return 0;
8193 default:
8194 do_default:
8195 /* If it isn't an induction variable, and it is invariant, we
8196 may be able to simplify things further by looking through
8197 the bits we just moved outside the loop. */
8198 if (loop_invariant_p (loop, x) == 1)
8200 struct movable *m;
8201 struct loop_movables *movables = LOOP_MOVABLES (loop);
8203 for (m = movables->head; m; m = m->next)
8204 if (rtx_equal_p (x, m->set_dest))
8206 /* Ok, we found a match. Substitute and simplify. */
8208 /* If we match another movable, we must use that, as
8209 this one is going away. */
8210 if (m->match)
8211 return simplify_giv_expr (loop, m->match->set_dest,
8212 ext_val, benefit);
8214 /* If consec is nonzero, this is a member of a group of
8215 instructions that were moved together. We handle this
8216 case only to the point of seeking to the last insn and
8217 looking for a REG_EQUAL. Fail if we don't find one. */
8218 if (m->consec != 0)
8220 int i = m->consec;
8221 tem = m->insn;
8224 tem = NEXT_INSN (tem);
8226 while (--i > 0);
8228 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
8229 if (tem)
8230 tem = XEXP (tem, 0);
8232 else
8234 tem = single_set (m->insn);
8235 if (tem)
8236 tem = SET_SRC (tem);
8239 if (tem)
8241 /* What we are most interested in is pointer
8242 arithmetic on invariants -- only take
8243 patterns we may be able to do something with. */
8244 if (GET_CODE (tem) == PLUS
8245 || GET_CODE (tem) == MULT
8246 || GET_CODE (tem) == ASHIFT
8247 || GET_CODE (tem) == CONST_INT
8248 || GET_CODE (tem) == SYMBOL_REF)
8250 tem = simplify_giv_expr (loop, tem, ext_val,
8251 benefit);
8252 if (tem)
8253 return tem;
8255 else if (GET_CODE (tem) == CONST
8256 && GET_CODE (XEXP (tem, 0)) == PLUS
8257 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
8258 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
8260 tem = simplify_giv_expr (loop, XEXP (tem, 0),
8261 ext_val, benefit);
8262 if (tem)
8263 return tem;
8266 break;
8269 break;
8272 /* Fall through to general case. */
8273 default:
8274 /* If invariant, return as USE (unless CONST_INT).
8275 Otherwise, not giv. */
8276 if (GET_CODE (x) == USE)
8277 x = XEXP (x, 0);
8279 if (loop_invariant_p (loop, x) == 1)
8281 if (GET_CODE (x) == CONST_INT)
8282 return x;
8283 if (GET_CODE (x) == CONST
8284 && GET_CODE (XEXP (x, 0)) == PLUS
8285 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
8286 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
8287 x = XEXP (x, 0);
8288 return gen_rtx_USE (mode, x);
8290 else
8291 return 0;
8295 /* This routine folds invariants such that there is only ever one
8296 CONST_INT in the summation. It is only used by simplify_giv_expr. */
8298 static rtx
8299 sge_plus_constant (rtx x, rtx c)
8301 if (GET_CODE (x) == CONST_INT)
8302 return GEN_INT (INTVAL (x) + INTVAL (c));
8303 else if (GET_CODE (x) != PLUS)
8304 return gen_rtx_PLUS (GET_MODE (x), x, c);
8305 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8307 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
8308 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
8310 else if (GET_CODE (XEXP (x, 0)) == PLUS
8311 || GET_CODE (XEXP (x, 1)) != PLUS)
8313 return gen_rtx_PLUS (GET_MODE (x),
8314 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
8316 else
8318 return gen_rtx_PLUS (GET_MODE (x),
8319 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
8323 static rtx
8324 sge_plus (enum machine_mode mode, rtx x, rtx y)
8326 while (GET_CODE (y) == PLUS)
8328 rtx a = XEXP (y, 0);
8329 if (GET_CODE (a) == CONST_INT)
8330 x = sge_plus_constant (x, a);
8331 else
8332 x = gen_rtx_PLUS (mode, x, a);
8333 y = XEXP (y, 1);
8335 if (GET_CODE (y) == CONST_INT)
8336 x = sge_plus_constant (x, y);
8337 else
8338 x = gen_rtx_PLUS (mode, x, y);
8339 return x;
8342 /* Help detect a giv that is calculated by several consecutive insns;
8343 for example,
8344 giv = biv * M
8345 giv = giv + A
8346 The caller has already identified the first insn P as having a giv as dest;
8347 we check that all other insns that set the same register follow
8348 immediately after P, that they alter nothing else,
8349 and that the result of the last is still a giv.
8351 The value is 0 if the reg set in P is not really a giv.
8352 Otherwise, the value is the amount gained by eliminating
8353 all the consecutive insns that compute the value.
8355 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
8356 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
8358 The coefficients of the ultimate giv value are stored in
8359 *MULT_VAL and *ADD_VAL. */
8361 static int
8362 consec_sets_giv (const struct loop *loop, int first_benefit, rtx p,
8363 rtx src_reg, rtx dest_reg, rtx *add_val, rtx *mult_val,
8364 rtx *ext_val, rtx *last_consec_insn)
8366 struct loop_ivs *ivs = LOOP_IVS (loop);
8367 struct loop_regs *regs = LOOP_REGS (loop);
8368 int count;
8369 enum rtx_code code;
8370 int benefit;
8371 rtx temp;
8372 rtx set;
8374 /* Indicate that this is a giv so that we can update the value produced in
8375 each insn of the multi-insn sequence.
8377 This induction structure will be used only by the call to
8378 general_induction_var below, so we can allocate it on our stack.
8379 If this is a giv, our caller will replace the induct var entry with
8380 a new induction structure. */
8381 struct induction *v;
8383 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
8384 return 0;
8386 v = alloca (sizeof (struct induction));
8387 v->src_reg = src_reg;
8388 v->mult_val = *mult_val;
8389 v->add_val = *add_val;
8390 v->benefit = first_benefit;
8391 v->cant_derive = 0;
8392 v->derive_adjustment = 0;
8393 v->ext_dependent = NULL_RTX;
8395 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
8396 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
8398 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
8400 while (count > 0)
8402 p = NEXT_INSN (p);
8403 code = GET_CODE (p);
8405 /* If libcall, skip to end of call sequence. */
8406 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
8407 p = XEXP (temp, 0);
8409 if (code == INSN
8410 && (set = single_set (p))
8411 && REG_P (SET_DEST (set))
8412 && SET_DEST (set) == dest_reg
8413 && (general_induction_var (loop, SET_SRC (set), &src_reg,
8414 add_val, mult_val, ext_val, 0,
8415 &benefit, VOIDmode)
8416 /* Giv created by equivalent expression. */
8417 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
8418 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
8419 add_val, mult_val, ext_val, 0,
8420 &benefit, VOIDmode)))
8421 && src_reg == v->src_reg)
8423 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
8424 benefit += libcall_benefit (p);
8426 count--;
8427 v->mult_val = *mult_val;
8428 v->add_val = *add_val;
8429 v->benefit += benefit;
8431 else if (code != NOTE)
8433 /* Allow insns that set something other than this giv to a
8434 constant. Such insns are needed on machines which cannot
8435 include long constants and should not disqualify a giv. */
8436 if (code == INSN
8437 && (set = single_set (p))
8438 && SET_DEST (set) != dest_reg
8439 && CONSTANT_P (SET_SRC (set)))
8440 continue;
8442 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
8443 return 0;
8447 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
8448 *last_consec_insn = p;
8449 return v->benefit;
8452 /* Return an rtx, if any, that expresses giv G2 as a function of the register
8453 represented by G1. If no such expression can be found, or it is clear that
8454 it cannot possibly be a valid address, 0 is returned.
8456 To perform the computation, we note that
8457 G1 = x * v + a and
8458 G2 = y * v + b
8459 where `v' is the biv.
8461 So G2 = (y/b) * G1 + (b - a*y/x).
8463 Note that MULT = y/x.
8465 Update: A and B are now allowed to be additive expressions such that
8466 B contains all variables in A. That is, computing B-A will not require
8467 subtracting variables. */
8469 static rtx
8470 express_from_1 (rtx a, rtx b, rtx mult)
8472 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
8474 if (mult == const0_rtx)
8475 return b;
8477 /* If MULT is not 1, we cannot handle A with non-constants, since we
8478 would then be required to subtract multiples of the registers in A.
8479 This is theoretically possible, and may even apply to some Fortran
8480 constructs, but it is a lot of work and we do not attempt it here. */
8482 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
8483 return NULL_RTX;
8485 /* In general these structures are sorted top to bottom (down the PLUS
8486 chain), but not left to right across the PLUS. If B is a higher
8487 order giv than A, we can strip one level and recurse. If A is higher
8488 order, we'll eventually bail out, but won't know that until the end.
8489 If they are the same, we'll strip one level around this loop. */
8491 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
8493 rtx ra, rb, oa, ob, tmp;
8495 ra = XEXP (a, 0), oa = XEXP (a, 1);
8496 if (GET_CODE (ra) == PLUS)
8497 tmp = ra, ra = oa, oa = tmp;
8499 rb = XEXP (b, 0), ob = XEXP (b, 1);
8500 if (GET_CODE (rb) == PLUS)
8501 tmp = rb, rb = ob, ob = tmp;
8503 if (rtx_equal_p (ra, rb))
8504 /* We matched: remove one reg completely. */
8505 a = oa, b = ob;
8506 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
8507 /* An alternate match. */
8508 a = oa, b = rb;
8509 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
8510 /* An alternate match. */
8511 a = ra, b = ob;
8512 else
8514 /* Indicates an extra register in B. Strip one level from B and
8515 recurse, hoping B was the higher order expression. */
8516 ob = express_from_1 (a, ob, mult);
8517 if (ob == NULL_RTX)
8518 return NULL_RTX;
8519 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
8523 /* Here we are at the last level of A, go through the cases hoping to
8524 get rid of everything but a constant. */
8526 if (GET_CODE (a) == PLUS)
8528 rtx ra, oa;
8530 ra = XEXP (a, 0), oa = XEXP (a, 1);
8531 if (rtx_equal_p (oa, b))
8532 oa = ra;
8533 else if (!rtx_equal_p (ra, b))
8534 return NULL_RTX;
8536 if (GET_CODE (oa) != CONST_INT)
8537 return NULL_RTX;
8539 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
8541 else if (GET_CODE (a) == CONST_INT)
8543 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
8545 else if (CONSTANT_P (a))
8547 enum machine_mode mode_a = GET_MODE (a);
8548 enum machine_mode mode_b = GET_MODE (b);
8549 enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
8550 return simplify_gen_binary (MINUS, mode, b, a);
8552 else if (GET_CODE (b) == PLUS)
8554 if (rtx_equal_p (a, XEXP (b, 0)))
8555 return XEXP (b, 1);
8556 else if (rtx_equal_p (a, XEXP (b, 1)))
8557 return XEXP (b, 0);
8558 else
8559 return NULL_RTX;
8561 else if (rtx_equal_p (a, b))
8562 return const0_rtx;
8564 return NULL_RTX;
8567 static rtx
8568 express_from (struct induction *g1, struct induction *g2)
8570 rtx mult, add;
8572 /* The value that G1 will be multiplied by must be a constant integer. Also,
8573 the only chance we have of getting a valid address is if b*c/a (see above
8574 for notation) is also an integer. */
8575 if (GET_CODE (g1->mult_val) == CONST_INT
8576 && GET_CODE (g2->mult_val) == CONST_INT)
8578 if (g1->mult_val == const0_rtx
8579 || (g1->mult_val == constm1_rtx
8580 && INTVAL (g2->mult_val)
8581 == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
8582 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
8583 return NULL_RTX;
8584 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
8586 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
8587 mult = const1_rtx;
8588 else
8590 /* ??? Find out if the one is a multiple of the other? */
8591 return NULL_RTX;
8594 add = express_from_1 (g1->add_val, g2->add_val, mult);
8595 if (add == NULL_RTX)
8597 /* Failed. If we've got a multiplication factor between G1 and G2,
8598 scale G1's addend and try again. */
8599 if (INTVAL (mult) > 1)
8601 rtx g1_add_val = g1->add_val;
8602 if (GET_CODE (g1_add_val) == MULT
8603 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
8605 HOST_WIDE_INT m;
8606 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
8607 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
8608 XEXP (g1_add_val, 0), GEN_INT (m));
8610 else
8612 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
8613 mult);
8616 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
8619 if (add == NULL_RTX)
8620 return NULL_RTX;
8622 /* Form simplified final result. */
8623 if (mult == const0_rtx)
8624 return add;
8625 else if (mult == const1_rtx)
8626 mult = g1->dest_reg;
8627 else
8628 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
8630 if (add == const0_rtx)
8631 return mult;
8632 else
8634 if (GET_CODE (add) == PLUS
8635 && CONSTANT_P (XEXP (add, 1)))
8637 rtx tem = XEXP (add, 1);
8638 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
8639 add = tem;
8642 return gen_rtx_PLUS (g2->mode, mult, add);
8646 /* Return an rtx, if any, that expresses giv G2 as a function of the register
8647 represented by G1. This indicates that G2 should be combined with G1 and
8648 that G2 can use (either directly or via an address expression) a register
8649 used to represent G1. */
8651 static rtx
8652 combine_givs_p (struct induction *g1, struct induction *g2)
8654 rtx comb, ret;
8656 /* With the introduction of ext dependent givs, we must care for modes.
8657 G2 must not use a wider mode than G1. */
8658 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
8659 return NULL_RTX;
8661 ret = comb = express_from (g1, g2);
8662 if (comb == NULL_RTX)
8663 return NULL_RTX;
8664 if (g1->mode != g2->mode)
8665 ret = gen_lowpart (g2->mode, comb);
8667 /* If these givs are identical, they can be combined. We use the results
8668 of express_from because the addends are not in a canonical form, so
8669 rtx_equal_p is a weaker test. */
8670 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
8671 combination to be the other way round. */
8672 if (comb == g1->dest_reg
8673 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
8675 return ret;
8678 /* If G2 can be expressed as a function of G1 and that function is valid
8679 as an address and no more expensive than using a register for G2,
8680 the expression of G2 in terms of G1 can be used. */
8681 if (ret != NULL_RTX
8682 && g2->giv_type == DEST_ADDR
8683 && memory_address_p (GET_MODE (g2->mem), ret))
8684 return ret;
8686 return NULL_RTX;
8689 /* See if BL is monotonic and has a constant per-iteration increment.
8690 Return the increment if so, otherwise return 0. */
8692 static HOST_WIDE_INT
8693 get_monotonic_increment (struct iv_class *bl)
8695 struct induction *v;
8696 rtx incr;
8698 /* Get the total increment and check that it is constant. */
8699 incr = biv_total_increment (bl);
8700 if (incr == 0 || GET_CODE (incr) != CONST_INT)
8701 return 0;
8703 for (v = bl->biv; v != 0; v = v->next_iv)
8705 if (GET_CODE (v->add_val) != CONST_INT)
8706 return 0;
8708 if (INTVAL (v->add_val) < 0 && INTVAL (incr) >= 0)
8709 return 0;
8711 if (INTVAL (v->add_val) > 0 && INTVAL (incr) <= 0)
8712 return 0;
8714 return INTVAL (incr);
8718 /* Subroutine of biv_fits_mode_p. Return true if biv BL, when biased by
8719 BIAS, will never exceed the unsigned range of MODE. LOOP is the loop
8720 to which the biv belongs and INCR is its per-iteration increment. */
8722 static bool
8723 biased_biv_fits_mode_p (const struct loop *loop, struct iv_class *bl,
8724 HOST_WIDE_INT incr, enum machine_mode mode,
8725 unsigned HOST_WIDE_INT bias)
8727 unsigned HOST_WIDE_INT initial, maximum, span, delta;
8729 /* We need to be able to manipulate MODE-size constants. */
8730 if (HOST_BITS_PER_WIDE_INT < GET_MODE_BITSIZE (mode))
8731 return false;
8733 /* The number of loop iterations must be constant. */
8734 if (LOOP_INFO (loop)->n_iterations == 0)
8735 return false;
8737 /* So must the biv's initial value. */
8738 if (bl->initial_value == 0 || GET_CODE (bl->initial_value) != CONST_INT)
8739 return false;
8741 initial = bias + INTVAL (bl->initial_value);
8742 maximum = GET_MODE_MASK (mode);
8744 /* Make sure that the initial value is within range. */
8745 if (initial > maximum)
8746 return false;
8748 /* Set up DELTA and SPAN such that the number of iterations * DELTA
8749 (calculated to arbitrary precision) must be <= SPAN. */
8750 if (incr < 0)
8752 delta = -incr;
8753 span = initial;
8755 else
8757 delta = incr;
8758 /* Handle the special case in which MAXIMUM is the largest
8759 unsigned HOST_WIDE_INT and INITIAL is 0. */
8760 if (maximum + 1 == initial)
8761 span = LOOP_INFO (loop)->n_iterations * delta;
8762 else
8763 span = maximum + 1 - initial;
8765 return (span / LOOP_INFO (loop)->n_iterations >= delta);
8769 /* Return true if biv BL will never exceed the bounds of MODE. LOOP is
8770 the loop to which BL belongs and INCR is its per-iteration increment.
8771 UNSIGNEDP is true if the biv should be treated as unsigned. */
8773 static bool
8774 biv_fits_mode_p (const struct loop *loop, struct iv_class *bl,
8775 HOST_WIDE_INT incr, enum machine_mode mode, bool unsignedp)
8777 struct loop_info *loop_info;
8778 unsigned HOST_WIDE_INT bias;
8780 /* A biv's value will always be limited to its natural mode.
8781 Larger modes will observe the same wrap-around. */
8782 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (bl->biv->src_reg)))
8783 mode = GET_MODE (bl->biv->src_reg);
8785 loop_info = LOOP_INFO (loop);
8787 bias = (unsignedp ? 0 : (GET_MODE_MASK (mode) >> 1) + 1);
8788 if (biased_biv_fits_mode_p (loop, bl, incr, mode, bias))
8789 return true;
8791 if (mode == GET_MODE (bl->biv->src_reg)
8792 && bl->biv->src_reg == loop_info->iteration_var
8793 && loop_info->comparison_value
8794 && loop_invariant_p (loop, loop_info->comparison_value))
8796 /* If the increment is +1, and the exit test is a <, the BIV
8797 cannot overflow. (For <=, we have the problematic case that
8798 the comparison value might be the maximum value of the range.) */
8799 if (incr == 1)
8801 if (loop_info->comparison_code == LT)
8802 return true;
8803 if (loop_info->comparison_code == LTU && unsignedp)
8804 return true;
8807 /* Likewise for increment -1 and exit test >. */
8808 if (incr == -1)
8810 if (loop_info->comparison_code == GT)
8811 return true;
8812 if (loop_info->comparison_code == GTU && unsignedp)
8813 return true;
8816 return false;
8820 /* Return false iff it is provable that biv BL plus BIAS will not wrap
8821 at any point in its update sequence. Note that at the rtl level we
8822 may not have information about the signedness of BL; in that case,
8823 check for both signed and unsigned overflow. */
8825 static bool
8826 biased_biv_may_wrap_p (const struct loop *loop, struct iv_class *bl,
8827 unsigned HOST_WIDE_INT bias)
8829 HOST_WIDE_INT incr;
8830 bool check_signed, check_unsigned;
8831 enum machine_mode mode;
8833 /* If the increment is not monotonic, we'd have to check separately
8834 at each increment step. Not Worth It. */
8835 incr = get_monotonic_increment (bl);
8836 if (incr == 0)
8837 return true;
8839 /* If this biv is the loop iteration variable, then we may be able to
8840 deduce a sign based on the loop condition. */
8841 /* ??? This is not 100% reliable; consider an unsigned biv that is cast
8842 to signed for the comparison. However, this same bug appears all
8843 through loop.c. */
8844 check_signed = check_unsigned = true;
8845 if (bl->biv->src_reg == LOOP_INFO (loop)->iteration_var)
8847 switch (LOOP_INFO (loop)->comparison_code)
8849 case GTU: case GEU: case LTU: case LEU:
8850 check_signed = false;
8851 break;
8852 case GT: case GE: case LT: case LE:
8853 check_unsigned = false;
8854 break;
8855 default:
8856 break;
8860 mode = GET_MODE (bl->biv->src_reg);
8862 if (check_unsigned
8863 && !biased_biv_fits_mode_p (loop, bl, incr, mode, bias))
8864 return true;
8866 if (check_signed)
8868 bias += (GET_MODE_MASK (mode) >> 1) + 1;
8869 if (!biased_biv_fits_mode_p (loop, bl, incr, mode, bias))
8870 return true;
8873 return false;
8877 /* Given that X is an extension or truncation of BL, return true
8878 if it is unaffected by overflow. LOOP is the loop to which
8879 BL belongs and INCR is its per-iteration increment. */
8881 static bool
8882 extension_within_bounds_p (const struct loop *loop, struct iv_class *bl,
8883 HOST_WIDE_INT incr, rtx x)
8885 enum machine_mode mode;
8886 bool signedp, unsignedp;
8888 switch (GET_CODE (x))
8890 case SIGN_EXTEND:
8891 case ZERO_EXTEND:
8892 mode = GET_MODE (XEXP (x, 0));
8893 signedp = (GET_CODE (x) == SIGN_EXTEND);
8894 unsignedp = (GET_CODE (x) == ZERO_EXTEND);
8895 break;
8897 case TRUNCATE:
8898 /* We don't know whether this value is being used as signed
8899 or unsigned, so check the conditions for both. */
8900 mode = GET_MODE (x);
8901 signedp = unsignedp = true;
8902 break;
8904 default:
8905 gcc_unreachable ();
8908 return ((!signedp || biv_fits_mode_p (loop, bl, incr, mode, false))
8909 && (!unsignedp || biv_fits_mode_p (loop, bl, incr, mode, true)));
8913 /* Check each extension dependent giv in this class to see if its
8914 root biv is safe from wrapping in the interior mode, which would
8915 make the giv illegal. */
8917 static void
8918 check_ext_dependent_givs (const struct loop *loop, struct iv_class *bl)
8920 struct induction *v;
8921 HOST_WIDE_INT incr;
8923 incr = get_monotonic_increment (bl);
8925 /* Invalidate givs that fail the tests. */
8926 for (v = bl->giv; v; v = v->next_iv)
8927 if (v->ext_dependent)
8929 if (incr != 0
8930 && extension_within_bounds_p (loop, bl, incr, v->ext_dependent))
8932 if (dump_file)
8933 fprintf (dump_file,
8934 "Verified ext dependent giv at %d of reg %d\n",
8935 INSN_UID (v->insn), bl->regno);
8937 else
8939 if (dump_file)
8940 fprintf (dump_file,
8941 "Failed ext dependent giv at %d\n",
8942 INSN_UID (v->insn));
8944 v->ignore = 1;
8945 bl->all_reduced = 0;
8950 /* Generate a version of VALUE in a mode appropriate for initializing V. */
8952 static rtx
8953 extend_value_for_giv (struct induction *v, rtx value)
8955 rtx ext_dep = v->ext_dependent;
8957 if (! ext_dep)
8958 return value;
8960 /* Recall that check_ext_dependent_givs verified that the known bounds
8961 of a biv did not overflow or wrap with respect to the extension for
8962 the giv. Therefore, constants need no additional adjustment. */
8963 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
8964 return value;
8966 /* Otherwise, we must adjust the value to compensate for the
8967 differing modes of the biv and the giv. */
8968 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
8971 struct combine_givs_stats
8973 int giv_number;
8974 int total_benefit;
8977 static int
8978 cmp_combine_givs_stats (const void *xp, const void *yp)
8980 const struct combine_givs_stats * const x =
8981 (const struct combine_givs_stats *) xp;
8982 const struct combine_givs_stats * const y =
8983 (const struct combine_givs_stats *) yp;
8984 int d;
8985 d = y->total_benefit - x->total_benefit;
8986 /* Stabilize the sort. */
8987 if (!d)
8988 d = x->giv_number - y->giv_number;
8989 return d;
8992 /* Check all pairs of givs for iv_class BL and see if any can be combined with
8993 any other. If so, point SAME to the giv combined with and set NEW_REG to
8994 be an expression (in terms of the other giv's DEST_REG) equivalent to the
8995 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
8997 static void
8998 combine_givs (struct loop_regs *regs, struct iv_class *bl)
9000 /* Additional benefit to add for being combined multiple times. */
9001 const int extra_benefit = 3;
9003 struct induction *g1, *g2, **giv_array;
9004 int i, j, k, giv_count;
9005 struct combine_givs_stats *stats;
9006 rtx *can_combine;
9008 /* Count givs, because bl->giv_count is incorrect here. */
9009 giv_count = 0;
9010 for (g1 = bl->giv; g1; g1 = g1->next_iv)
9011 if (!g1->ignore)
9012 giv_count++;
9014 giv_array = alloca (giv_count * sizeof (struct induction *));
9015 i = 0;
9016 for (g1 = bl->giv; g1; g1 = g1->next_iv)
9017 if (!g1->ignore)
9018 giv_array[i++] = g1;
9020 stats = XCNEWVEC (struct combine_givs_stats, giv_count);
9021 can_combine = XCNEWVEC (rtx, giv_count * giv_count);
9023 for (i = 0; i < giv_count; i++)
9025 int this_benefit;
9026 rtx single_use;
9028 g1 = giv_array[i];
9029 stats[i].giv_number = i;
9031 /* If a DEST_REG GIV is used only once, do not allow it to combine
9032 with anything, for in doing so we will gain nothing that cannot
9033 be had by simply letting the GIV with which we would have combined
9034 to be reduced on its own. The lossage shows up in particular with
9035 DEST_ADDR targets on hosts with reg+reg addressing, though it can
9036 be seen elsewhere as well. */
9037 if (g1->giv_type == DEST_REG
9038 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
9039 && single_use != const0_rtx)
9040 continue;
9042 this_benefit = g1->benefit;
9043 /* Add an additional weight for zero addends. */
9044 if (g1->no_const_addval)
9045 this_benefit += 1;
9047 for (j = 0; j < giv_count; j++)
9049 rtx this_combine;
9051 g2 = giv_array[j];
9052 if (g1 != g2
9053 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
9055 can_combine[i * giv_count + j] = this_combine;
9056 this_benefit += g2->benefit + extra_benefit;
9059 stats[i].total_benefit = this_benefit;
9062 /* Iterate, combining until we can't. */
9063 restart:
9064 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
9066 if (dump_file)
9068 fprintf (dump_file, "Sorted combine statistics:\n");
9069 for (k = 0; k < giv_count; k++)
9071 g1 = giv_array[stats[k].giv_number];
9072 if (!g1->combined_with && !g1->same)
9073 fprintf (dump_file, " {%d, %d}",
9074 INSN_UID (giv_array[stats[k].giv_number]->insn),
9075 stats[k].total_benefit);
9077 putc ('\n', dump_file);
9080 for (k = 0; k < giv_count; k++)
9082 int g1_add_benefit = 0;
9084 i = stats[k].giv_number;
9085 g1 = giv_array[i];
9087 /* If it has already been combined, skip. */
9088 if (g1->combined_with || g1->same)
9089 continue;
9091 for (j = 0; j < giv_count; j++)
9093 g2 = giv_array[j];
9094 if (g1 != g2 && can_combine[i * giv_count + j]
9095 /* If it has already been combined, skip. */
9096 && ! g2->same && ! g2->combined_with)
9098 int l;
9100 g2->new_reg = can_combine[i * giv_count + j];
9101 g2->same = g1;
9102 /* For destination, we now may replace by mem expression instead
9103 of register. This changes the costs considerably, so add the
9104 compensation. */
9105 if (g2->giv_type == DEST_ADDR)
9106 g2->benefit = (g2->benefit + reg_address_cost
9107 - address_cost (g2->new_reg,
9108 GET_MODE (g2->mem)));
9109 g1->combined_with++;
9110 g1->lifetime += g2->lifetime;
9112 g1_add_benefit += g2->benefit;
9114 /* ??? The new final_[bg]iv_value code does a much better job
9115 of finding replaceable giv's, and hence this code may no
9116 longer be necessary. */
9117 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
9118 g1_add_benefit -= copy_cost;
9120 /* To help optimize the next set of combinations, remove
9121 this giv from the benefits of other potential mates. */
9122 for (l = 0; l < giv_count; ++l)
9124 int m = stats[l].giv_number;
9125 if (can_combine[m * giv_count + j])
9126 stats[l].total_benefit -= g2->benefit + extra_benefit;
9129 if (dump_file)
9130 fprintf (dump_file,
9131 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
9132 INSN_UID (g2->insn), INSN_UID (g1->insn),
9133 g1->benefit, g1_add_benefit, g1->lifetime);
9137 /* To help optimize the next set of combinations, remove
9138 this giv from the benefits of other potential mates. */
9139 if (g1->combined_with)
9141 for (j = 0; j < giv_count; ++j)
9143 int m = stats[j].giv_number;
9144 if (can_combine[m * giv_count + i])
9145 stats[j].total_benefit -= g1->benefit + extra_benefit;
9148 g1->benefit += g1_add_benefit;
9150 /* We've finished with this giv, and everything it touched.
9151 Restart the combination so that proper weights for the
9152 rest of the givs are properly taken into account. */
9153 /* ??? Ideally we would compact the arrays at this point, so
9154 as to not cover old ground. But sanely compacting
9155 can_combine is tricky. */
9156 goto restart;
9160 /* Clean up. */
9161 free (stats);
9162 free (can_combine);
9165 /* Generate sequence for REG = B * M + A. B is the initial value of
9166 the basic induction variable, M a multiplicative constant, A an
9167 additive constant and REG the destination register. */
9169 static rtx
9170 gen_add_mult (rtx b, rtx m, rtx a, rtx reg)
9172 rtx seq;
9173 rtx result;
9175 start_sequence ();
9176 /* Use unsigned arithmetic. */
9177 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
9178 if (reg != result)
9179 emit_move_insn (reg, result);
9180 seq = get_insns ();
9181 end_sequence ();
9183 return seq;
9187 /* Update registers created in insn sequence SEQ. */
9189 static void
9190 loop_regs_update (const struct loop *loop ATTRIBUTE_UNUSED, rtx seq)
9192 rtx insn;
9194 /* Update register info for alias analysis. */
9196 insn = seq;
9197 while (insn != NULL_RTX)
9199 rtx set = single_set (insn);
9201 if (set && REG_P (SET_DEST (set)))
9202 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
9204 insn = NEXT_INSN (insn);
9209 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. B
9210 is the initial value of the basic induction variable, M a
9211 multiplicative constant, A an additive constant and REG the
9212 destination register. */
9214 static void
9215 loop_iv_add_mult_emit_before (const struct loop *loop, rtx b, rtx m, rtx a,
9216 rtx reg, basic_block before_bb, rtx before_insn)
9218 rtx seq;
9220 if (! before_insn)
9222 loop_iv_add_mult_hoist (loop, b, m, a, reg);
9223 return;
9226 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9227 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
9229 /* Increase the lifetime of any invariants moved further in code. */
9230 update_reg_last_use (a, before_insn);
9231 update_reg_last_use (b, before_insn);
9232 update_reg_last_use (m, before_insn);
9234 /* It is possible that the expansion created lots of new registers.
9235 Iterate over the sequence we just created and record them all. We
9236 must do this before inserting the sequence. */
9237 loop_regs_update (loop, seq);
9239 loop_insn_emit_before (loop, before_bb, before_insn, seq);
9243 /* Emit insns in loop pre-header to set REG = B * M + A. B is the
9244 initial value of the basic induction variable, M a multiplicative
9245 constant, A an additive constant and REG the destination
9246 register. */
9248 static void
9249 loop_iv_add_mult_sink (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
9251 rtx seq;
9253 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9254 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
9256 /* Increase the lifetime of any invariants moved further in code.
9257 ???? Is this really necessary? */
9258 update_reg_last_use (a, loop->sink);
9259 update_reg_last_use (b, loop->sink);
9260 update_reg_last_use (m, loop->sink);
9262 /* It is possible that the expansion created lots of new registers.
9263 Iterate over the sequence we just created and record them all. We
9264 must do this before inserting the sequence. */
9265 loop_regs_update (loop, seq);
9267 loop_insn_sink (loop, seq);
9271 /* Emit insns after loop to set REG = B * M + A. B is the initial
9272 value of the basic induction variable, M a multiplicative constant,
9273 A an additive constant and REG the destination register. */
9275 static void
9276 loop_iv_add_mult_hoist (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
9278 rtx seq;
9280 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9281 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
9283 /* It is possible that the expansion created lots of new registers.
9284 Iterate over the sequence we just created and record them all. We
9285 must do this before inserting the sequence. */
9286 loop_regs_update (loop, seq);
9288 loop_insn_hoist (loop, seq);
9293 /* Similar to gen_add_mult, but compute cost rather than generating
9294 sequence. */
9296 static int
9297 iv_add_mult_cost (rtx b, rtx m, rtx a, rtx reg)
9299 int cost = 0;
9300 rtx last, result;
9302 start_sequence ();
9303 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
9304 if (reg != result)
9305 emit_move_insn (reg, result);
9306 last = get_last_insn ();
9307 while (last)
9309 rtx t = single_set (last);
9310 if (t)
9311 cost += rtx_cost (SET_SRC (t), SET);
9312 last = PREV_INSN (last);
9314 end_sequence ();
9315 return cost;
9318 /* Test whether A * B can be computed without
9319 an actual multiply insn. Value is 1 if so.
9321 ??? This function stinks because it generates a ton of wasted RTL
9322 ??? and as a result fragments GC memory to no end. There are other
9323 ??? places in the compiler which are invoked a lot and do the same
9324 ??? thing, generate wasted RTL just to see if something is possible. */
9326 static int
9327 product_cheap_p (rtx a, rtx b)
9329 rtx tmp;
9330 int win, n_insns;
9332 /* If only one is constant, make it B. */
9333 if (GET_CODE (a) == CONST_INT)
9334 tmp = a, a = b, b = tmp;
9336 /* If first constant, both constant, so don't need multiply. */
9337 if (GET_CODE (a) == CONST_INT)
9338 return 1;
9340 /* If second not constant, neither is constant, so would need multiply. */
9341 if (GET_CODE (b) != CONST_INT)
9342 return 0;
9344 /* One operand is constant, so might not need multiply insn. Generate the
9345 code for the multiply and see if a call or multiply, or long sequence
9346 of insns is generated. */
9348 start_sequence ();
9349 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
9350 tmp = get_insns ();
9351 end_sequence ();
9353 win = 1;
9354 if (tmp == NULL_RTX)
9356 else if (INSN_P (tmp))
9358 n_insns = 0;
9359 while (tmp != NULL_RTX)
9361 rtx next = NEXT_INSN (tmp);
9363 if (++n_insns > 3
9364 || !NONJUMP_INSN_P (tmp)
9365 || (GET_CODE (PATTERN (tmp)) == SET
9366 && GET_CODE (SET_SRC (PATTERN (tmp))) == MULT)
9367 || (GET_CODE (PATTERN (tmp)) == PARALLEL
9368 && GET_CODE (XVECEXP (PATTERN (tmp), 0, 0)) == SET
9369 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp), 0, 0))) == MULT))
9371 win = 0;
9372 break;
9375 tmp = next;
9378 else if (GET_CODE (tmp) == SET
9379 && GET_CODE (SET_SRC (tmp)) == MULT)
9380 win = 0;
9381 else if (GET_CODE (tmp) == PARALLEL
9382 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
9383 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
9384 win = 0;
9386 return win;
9389 /* Check to see if loop can be terminated by a "decrement and branch until
9390 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
9391 Also try reversing an increment loop to a decrement loop
9392 to see if the optimization can be performed.
9393 Value is nonzero if optimization was performed. */
9395 /* This is useful even if the architecture doesn't have such an insn,
9396 because it might change a loops which increments from 0 to n to a loop
9397 which decrements from n to 0. A loop that decrements to zero is usually
9398 faster than one that increments from zero. */
9400 /* ??? This could be rewritten to use some of the loop unrolling procedures,
9401 such as approx_final_value, biv_total_increment, loop_iterations, and
9402 final_[bg]iv_value. */
9404 static int
9405 check_dbra_loop (struct loop *loop, int insn_count)
9407 struct loop_info *loop_info = LOOP_INFO (loop);
9408 struct loop_regs *regs = LOOP_REGS (loop);
9409 struct loop_ivs *ivs = LOOP_IVS (loop);
9410 struct iv_class *bl;
9411 rtx reg;
9412 enum machine_mode mode;
9413 rtx jump_label;
9414 rtx final_value;
9415 rtx start_value;
9416 rtx new_add_val;
9417 rtx comparison;
9418 rtx before_comparison;
9419 rtx p;
9420 rtx jump;
9421 rtx first_compare;
9422 int compare_and_branch;
9423 rtx loop_start = loop->start;
9424 rtx loop_end = loop->end;
9426 /* If last insn is a conditional branch, and the insn before tests a
9427 register value, try to optimize it. Otherwise, we can't do anything. */
9429 jump = PREV_INSN (loop_end);
9430 comparison = get_condition_for_loop (loop, jump);
9431 if (comparison == 0)
9432 return 0;
9433 if (!onlyjump_p (jump))
9434 return 0;
9436 /* Try to compute whether the compare/branch at the loop end is one or
9437 two instructions. */
9438 get_condition (jump, &first_compare, false, true);
9439 if (first_compare == jump)
9440 compare_and_branch = 1;
9441 else if (first_compare == prev_nonnote_insn (jump))
9442 compare_and_branch = 2;
9443 else
9444 return 0;
9447 /* If more than one condition is present to control the loop, then
9448 do not proceed, as this function does not know how to rewrite
9449 loop tests with more than one condition.
9451 Look backwards from the first insn in the last comparison
9452 sequence and see if we've got another comparison sequence. */
9454 rtx jump1;
9455 if ((jump1 = prev_nonnote_insn (first_compare))
9456 && JUMP_P (jump1))
9457 return 0;
9460 /* Check all of the bivs to see if the compare uses one of them.
9461 Skip biv's set more than once because we can't guarantee that
9462 it will be zero on the last iteration. Also skip if the biv is
9463 used between its update and the test insn. */
9465 for (bl = ivs->list; bl; bl = bl->next)
9467 if (bl->biv_count == 1
9468 && ! bl->biv->maybe_multiple
9469 && bl->biv->dest_reg == XEXP (comparison, 0)
9470 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
9471 first_compare))
9472 break;
9475 /* Try swapping the comparison to identify a suitable biv. */
9476 if (!bl)
9477 for (bl = ivs->list; bl; bl = bl->next)
9478 if (bl->biv_count == 1
9479 && ! bl->biv->maybe_multiple
9480 && bl->biv->dest_reg == XEXP (comparison, 1)
9481 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
9482 first_compare))
9484 comparison = gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)),
9485 VOIDmode,
9486 XEXP (comparison, 1),
9487 XEXP (comparison, 0));
9488 break;
9491 if (! bl)
9492 return 0;
9494 /* Look for the case where the basic induction variable is always
9495 nonnegative, and equals zero on the last iteration.
9496 In this case, add a reg_note REG_NONNEG, which allows the
9497 m68k DBRA instruction to be used. */
9499 if (((GET_CODE (comparison) == GT && XEXP (comparison, 1) == constm1_rtx)
9500 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
9501 && GET_CODE (bl->biv->add_val) == CONST_INT
9502 && INTVAL (bl->biv->add_val) < 0)
9504 /* Initial value must be greater than 0,
9505 init_val % -dec_value == 0 to ensure that it equals zero on
9506 the last iteration */
9508 if (GET_CODE (bl->initial_value) == CONST_INT
9509 && INTVAL (bl->initial_value) > 0
9510 && (INTVAL (bl->initial_value)
9511 % (-INTVAL (bl->biv->add_val))) == 0)
9513 /* Register always nonnegative, add REG_NOTE to branch. */
9514 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
9515 REG_NOTES (jump)
9516 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
9517 REG_NOTES (jump));
9518 bl->nonneg = 1;
9520 return 1;
9523 /* If the decrement is 1 and the value was tested as >= 0 before
9524 the loop, then we can safely optimize. */
9525 for (p = loop_start; p; p = PREV_INSN (p))
9527 if (LABEL_P (p))
9528 break;
9529 if (!JUMP_P (p))
9530 continue;
9532 before_comparison = get_condition_for_loop (loop, p);
9533 if (before_comparison
9534 && XEXP (before_comparison, 0) == bl->biv->dest_reg
9535 && (GET_CODE (before_comparison) == LT
9536 || GET_CODE (before_comparison) == LTU)
9537 && XEXP (before_comparison, 1) == const0_rtx
9538 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
9539 && INTVAL (bl->biv->add_val) == -1)
9541 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
9542 REG_NOTES (jump)
9543 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
9544 REG_NOTES (jump));
9545 bl->nonneg = 1;
9547 return 1;
9551 else if (GET_CODE (bl->biv->add_val) == CONST_INT
9552 && INTVAL (bl->biv->add_val) > 0)
9554 /* Try to change inc to dec, so can apply above optimization. */
9555 /* Can do this if:
9556 all registers modified are induction variables or invariant,
9557 all memory references have non-overlapping addresses
9558 (obviously true if only one write)
9559 allow 2 insns for the compare/jump at the end of the loop. */
9560 /* Also, we must avoid any instructions which use both the reversed
9561 biv and another biv. Such instructions will fail if the loop is
9562 reversed. We meet this condition by requiring that either
9563 no_use_except_counting is true, or else that there is only
9564 one biv. */
9565 int num_nonfixed_reads = 0;
9566 /* 1 if the iteration var is used only to count iterations. */
9567 int no_use_except_counting = 0;
9568 /* 1 if the loop has no memory store, or it has a single memory store
9569 which is reversible. */
9570 int reversible_mem_store = 1;
9572 if (bl->giv_count == 0
9573 && !loop->exit_count
9574 && !loop_info->has_multiple_exit_targets)
9576 rtx bivreg = regno_reg_rtx[bl->regno];
9577 struct iv_class *blt;
9579 /* If there are no givs for this biv, and the only exit is the
9580 fall through at the end of the loop, then
9581 see if perhaps there are no uses except to count. */
9582 no_use_except_counting = 1;
9583 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
9584 if (INSN_P (p))
9586 rtx set = single_set (p);
9588 if (set && REG_P (SET_DEST (set))
9589 && REGNO (SET_DEST (set)) == bl->regno)
9590 /* An insn that sets the biv is okay. */
9592 else if (!reg_mentioned_p (bivreg, PATTERN (p)))
9593 /* An insn that doesn't mention the biv is okay. */
9595 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
9596 || p == prev_nonnote_insn (loop_end))
9598 /* If either of these insns uses the biv and sets a pseudo
9599 that has more than one usage, then the biv has uses
9600 other than counting since it's used to derive a value
9601 that is used more than one time. */
9602 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
9603 regs);
9604 if (regs->multiple_uses)
9606 no_use_except_counting = 0;
9607 break;
9610 else
9612 no_use_except_counting = 0;
9613 break;
9617 /* A biv has uses besides counting if it is used to set
9618 another biv. */
9619 for (blt = ivs->list; blt; blt = blt->next)
9620 if (blt->init_set
9621 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
9623 no_use_except_counting = 0;
9624 break;
9628 if (no_use_except_counting)
9629 /* No need to worry about MEMs. */
9631 else if (loop_info->num_mem_sets <= 1)
9633 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
9634 if (INSN_P (p))
9635 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
9637 /* If the loop has a single store, and the destination address is
9638 invariant, then we can't reverse the loop, because this address
9639 might then have the wrong value at loop exit.
9640 This would work if the source was invariant also, however, in that
9641 case, the insn should have been moved out of the loop. */
9643 if (loop_info->num_mem_sets == 1)
9645 struct induction *v;
9647 /* If we could prove that each of the memory locations
9648 written to was different, then we could reverse the
9649 store -- but we don't presently have any way of
9650 knowing that. */
9651 reversible_mem_store = 0;
9653 /* If the store depends on a register that is set after the
9654 store, it depends on the initial value, and is thus not
9655 reversible. */
9656 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
9658 if (v->giv_type == DEST_REG
9659 && reg_mentioned_p (v->dest_reg,
9660 PATTERN (loop_info->first_loop_store_insn))
9661 && loop_insn_first_p (loop_info->first_loop_store_insn,
9662 v->insn))
9663 reversible_mem_store = 0;
9667 else
9668 return 0;
9670 /* This code only acts for innermost loops. Also it simplifies
9671 the memory address check by only reversing loops with
9672 zero or one memory access.
9673 Two memory accesses could involve parts of the same array,
9674 and that can't be reversed.
9675 If the biv is used only for counting, than we don't need to worry
9676 about all these things. */
9678 if ((num_nonfixed_reads <= 1
9679 && ! loop_info->has_nonconst_call
9680 && ! loop_info->has_prefetch
9681 && ! loop_info->has_volatile
9682 && reversible_mem_store
9683 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
9684 + num_unmoved_movables (loop) + compare_and_branch == insn_count)
9685 && (bl == ivs->list && bl->next == 0))
9686 || (no_use_except_counting && ! loop_info->has_prefetch))
9688 rtx tem;
9690 /* Loop can be reversed. */
9691 if (dump_file)
9692 fprintf (dump_file, "Can reverse loop\n");
9694 /* Now check other conditions:
9696 The increment must be a constant, as must the initial value,
9697 and the comparison code must be LT.
9699 This test can probably be improved since +/- 1 in the constant
9700 can be obtained by changing LT to LE and vice versa; this is
9701 confusing. */
9703 if (comparison
9704 /* for constants, LE gets turned into LT */
9705 && (GET_CODE (comparison) == LT
9706 || (GET_CODE (comparison) == LE
9707 && no_use_except_counting)
9708 || GET_CODE (comparison) == LTU))
9710 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
9711 rtx initial_value, comparison_value;
9712 int nonneg = 0;
9713 enum rtx_code cmp_code;
9714 int comparison_const_width;
9715 unsigned HOST_WIDE_INT comparison_sign_mask;
9716 bool keep_first_compare;
9718 add_val = INTVAL (bl->biv->add_val);
9719 comparison_value = XEXP (comparison, 1);
9720 if (GET_MODE (comparison_value) == VOIDmode)
9721 comparison_const_width
9722 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
9723 else
9724 comparison_const_width
9725 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
9726 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
9727 comparison_const_width = HOST_BITS_PER_WIDE_INT;
9728 comparison_sign_mask
9729 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
9731 /* If the comparison value is not a loop invariant, then we
9732 can not reverse this loop.
9734 ??? If the insns which initialize the comparison value as
9735 a whole compute an invariant result, then we could move
9736 them out of the loop and proceed with loop reversal. */
9737 if (! loop_invariant_p (loop, comparison_value))
9738 return 0;
9740 if (GET_CODE (comparison_value) == CONST_INT)
9741 comparison_val = INTVAL (comparison_value);
9742 initial_value = bl->initial_value;
9744 /* Normalize the initial value if it is an integer and
9745 has no other use except as a counter. This will allow
9746 a few more loops to be reversed. */
9747 if (no_use_except_counting
9748 && GET_CODE (comparison_value) == CONST_INT
9749 && GET_CODE (initial_value) == CONST_INT)
9751 comparison_val = comparison_val - INTVAL (bl->initial_value);
9752 /* The code below requires comparison_val to be a multiple
9753 of add_val in order to do the loop reversal, so
9754 round up comparison_val to a multiple of add_val.
9755 Since comparison_value is constant, we know that the
9756 current comparison code is LT. */
9757 comparison_val = comparison_val + add_val - 1;
9758 comparison_val
9759 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
9760 /* We postpone overflow checks for COMPARISON_VAL here;
9761 even if there is an overflow, we might still be able to
9762 reverse the loop, if converting the loop exit test to
9763 NE is possible. */
9764 initial_value = const0_rtx;
9767 /* First check if we can do a vanilla loop reversal. */
9768 if (initial_value == const0_rtx
9769 && GET_CODE (comparison_value) == CONST_INT
9770 /* Now do postponed overflow checks on COMPARISON_VAL. */
9771 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
9772 & comparison_sign_mask))
9774 /* Register will always be nonnegative, with value
9775 0 on last iteration */
9776 add_adjust = add_val;
9777 nonneg = 1;
9778 cmp_code = GE;
9780 else
9781 return 0;
9783 if (GET_CODE (comparison) == LE)
9784 add_adjust -= add_val;
9786 /* If the initial value is not zero, or if the comparison
9787 value is not an exact multiple of the increment, then we
9788 can not reverse this loop. */
9789 if (initial_value == const0_rtx
9790 && GET_CODE (comparison_value) == CONST_INT)
9792 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
9793 return 0;
9795 else
9797 if (! no_use_except_counting || add_val != 1)
9798 return 0;
9801 final_value = comparison_value;
9803 /* Reset these in case we normalized the initial value
9804 and comparison value above. */
9805 if (GET_CODE (comparison_value) == CONST_INT
9806 && GET_CODE (initial_value) == CONST_INT)
9808 comparison_value = GEN_INT (comparison_val);
9809 final_value
9810 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
9812 bl->initial_value = initial_value;
9814 /* Save some info needed to produce the new insns. */
9815 reg = bl->biv->dest_reg;
9816 mode = GET_MODE (reg);
9817 jump_label = condjump_label (PREV_INSN (loop_end));
9818 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
9820 /* Set start_value; if this is not a CONST_INT, we need
9821 to generate a SUB.
9822 Initialize biv to start_value before loop start.
9823 The old initializing insn will be deleted as a
9824 dead store by flow.c. */
9825 if (initial_value == const0_rtx
9826 && GET_CODE (comparison_value) == CONST_INT)
9828 start_value
9829 = gen_int_mode (comparison_val - add_adjust, mode);
9830 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
9832 else if (GET_CODE (initial_value) == CONST_INT)
9834 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
9835 rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
9837 if (add_insn == 0)
9838 return 0;
9840 start_value
9841 = gen_rtx_PLUS (mode, comparison_value, offset);
9842 loop_insn_hoist (loop, add_insn);
9843 if (GET_CODE (comparison) == LE)
9844 final_value = gen_rtx_PLUS (mode, comparison_value,
9845 GEN_INT (add_val));
9847 else if (! add_adjust)
9849 rtx sub_insn = gen_sub3_insn (reg, comparison_value,
9850 initial_value);
9852 if (sub_insn == 0)
9853 return 0;
9854 start_value
9855 = gen_rtx_MINUS (mode, comparison_value, initial_value);
9856 loop_insn_hoist (loop, sub_insn);
9858 else
9859 /* We could handle the other cases too, but it'll be
9860 better to have a testcase first. */
9861 return 0;
9863 /* We may not have a single insn which can increment a reg, so
9864 create a sequence to hold all the insns from expand_inc. */
9865 start_sequence ();
9866 expand_inc (reg, new_add_val);
9867 tem = get_insns ();
9868 end_sequence ();
9870 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
9871 delete_insn (bl->biv->insn);
9873 /* Update biv info to reflect its new status. */
9874 bl->biv->insn = p;
9875 bl->initial_value = start_value;
9876 bl->biv->add_val = new_add_val;
9878 /* Update loop info. */
9879 loop_info->initial_value = reg;
9880 loop_info->initial_equiv_value = reg;
9881 loop_info->final_value = const0_rtx;
9882 loop_info->final_equiv_value = const0_rtx;
9883 loop_info->comparison_value = const0_rtx;
9884 loop_info->comparison_code = cmp_code;
9885 loop_info->increment = new_add_val;
9887 /* Inc LABEL_NUSES so that delete_insn will
9888 not delete the label. */
9889 LABEL_NUSES (XEXP (jump_label, 0))++;
9891 /* If we have a separate comparison insn that does more
9892 than just set cc0, the result of the comparison might
9893 be used outside the loop. */
9894 keep_first_compare = (compare_and_branch == 2
9895 #ifdef HAVE_CC0
9896 && sets_cc0_p (first_compare) <= 0
9897 #endif
9900 /* Emit an insn after the end of the loop to set the biv's
9901 proper exit value if it is used anywhere outside the loop. */
9902 if (keep_first_compare
9903 || (REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
9904 || ! bl->init_insn
9905 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
9906 loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
9908 if (keep_first_compare)
9909 loop_insn_sink (loop, PATTERN (first_compare));
9911 /* Delete compare/branch at end of loop. */
9912 delete_related_insns (PREV_INSN (loop_end));
9913 if (compare_and_branch == 2)
9914 delete_related_insns (first_compare);
9916 /* Add new compare/branch insn at end of loop. */
9917 start_sequence ();
9918 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
9919 mode, 0,
9920 XEXP (jump_label, 0));
9921 tem = get_insns ();
9922 end_sequence ();
9923 emit_jump_insn_before (tem, loop_end);
9925 for (tem = PREV_INSN (loop_end);
9926 tem && !JUMP_P (tem);
9927 tem = PREV_INSN (tem))
9930 if (tem)
9931 JUMP_LABEL (tem) = XEXP (jump_label, 0);
9933 if (nonneg)
9935 if (tem)
9937 /* Increment of LABEL_NUSES done above. */
9938 /* Register is now always nonnegative,
9939 so add REG_NONNEG note to the branch. */
9940 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
9941 REG_NOTES (tem));
9943 bl->nonneg = 1;
9946 /* No insn may reference both the reversed and another biv or it
9947 will fail (see comment near the top of the loop reversal
9948 code).
9949 Earlier on, we have verified that the biv has no use except
9950 counting, or it is the only biv in this function.
9951 However, the code that computes no_use_except_counting does
9952 not verify reg notes. It's possible to have an insn that
9953 references another biv, and has a REG_EQUAL note with an
9954 expression based on the reversed biv. To avoid this case,
9955 remove all REG_EQUAL notes based on the reversed biv
9956 here. */
9957 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
9958 if (INSN_P (p))
9960 rtx *pnote;
9961 rtx set = single_set (p);
9962 /* If this is a set of a GIV based on the reversed biv, any
9963 REG_EQUAL notes should still be correct. */
9964 if (! set
9965 || !REG_P (SET_DEST (set))
9966 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
9967 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
9968 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
9969 for (pnote = &REG_NOTES (p); *pnote;)
9971 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
9972 && reg_mentioned_p (regno_reg_rtx[bl->regno],
9973 XEXP (*pnote, 0)))
9974 *pnote = XEXP (*pnote, 1);
9975 else
9976 pnote = &XEXP (*pnote, 1);
9980 /* Mark that this biv has been reversed. Each giv which depends
9981 on this biv, and which is also live past the end of the loop
9982 will have to be fixed up. */
9984 bl->reversed = 1;
9986 if (dump_file)
9988 fprintf (dump_file, "Reversed loop");
9989 if (bl->nonneg)
9990 fprintf (dump_file, " and added reg_nonneg\n");
9991 else
9992 fprintf (dump_file, "\n");
9995 return 1;
10000 return 0;
10003 /* Verify whether the biv BL appears to be eliminable,
10004 based on the insns in the loop that refer to it.
10006 If ELIMINATE_P is nonzero, actually do the elimination.
10008 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
10009 determine whether invariant insns should be placed inside or at the
10010 start of the loop. */
10012 static int
10013 maybe_eliminate_biv (const struct loop *loop, struct iv_class *bl,
10014 int eliminate_p, int threshold, int insn_count)
10016 struct loop_ivs *ivs = LOOP_IVS (loop);
10017 rtx reg = bl->biv->dest_reg;
10018 rtx p;
10020 /* Scan all insns in the loop, stopping if we find one that uses the
10021 biv in a way that we cannot eliminate. */
10023 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
10025 enum rtx_code code = GET_CODE (p);
10026 basic_block where_bb = 0;
10027 rtx where_insn = threshold >= insn_count ? 0 : p;
10028 rtx note;
10030 /* If this is a libcall that sets a giv, skip ahead to its end. */
10031 if (INSN_P (p))
10033 note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
10035 if (note)
10037 rtx last = XEXP (note, 0);
10038 rtx set = single_set (last);
10040 if (set && REG_P (SET_DEST (set)))
10042 unsigned int regno = REGNO (SET_DEST (set));
10044 if (regno < ivs->n_regs
10045 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
10046 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
10047 p = last;
10052 /* Closely examine the insn if the biv is mentioned. */
10053 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
10054 && reg_mentioned_p (reg, PATTERN (p))
10055 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
10056 eliminate_p, where_bb, where_insn))
10058 if (dump_file)
10059 fprintf (dump_file,
10060 "Cannot eliminate biv %d: biv used in insn %d.\n",
10061 bl->regno, INSN_UID (p));
10062 break;
10065 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
10066 if (eliminate_p
10067 && (note = find_reg_note (p, REG_EQUAL, NULL_RTX)) != NULL_RTX
10068 && reg_mentioned_p (reg, XEXP (note, 0)))
10069 remove_note (p, note);
10072 if (p == loop->end)
10074 if (dump_file)
10075 fprintf (dump_file, "biv %d %s eliminated.\n",
10076 bl->regno, eliminate_p ? "was" : "can be");
10077 return 1;
10080 return 0;
10083 /* INSN and REFERENCE are instructions in the same insn chain.
10084 Return nonzero if INSN is first. */
10086 static int
10087 loop_insn_first_p (rtx insn, rtx reference)
10089 rtx p, q;
10091 for (p = insn, q = reference;;)
10093 /* Start with test for not first so that INSN == REFERENCE yields not
10094 first. */
10095 if (q == insn || ! p)
10096 return 0;
10097 if (p == reference || ! q)
10098 return 1;
10100 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
10101 previous insn, hence the <= comparison below does not work if
10102 P is a note. */
10103 if (INSN_UID (p) < max_uid_for_loop
10104 && INSN_UID (q) < max_uid_for_loop
10105 && !NOTE_P (p))
10106 return INSN_LUID (p) <= INSN_LUID (q);
10108 if (INSN_UID (p) >= max_uid_for_loop
10109 || NOTE_P (p))
10110 p = NEXT_INSN (p);
10111 if (INSN_UID (q) >= max_uid_for_loop)
10112 q = NEXT_INSN (q);
10116 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
10117 the offset that we have to take into account due to auto-increment /
10118 div derivation is zero. */
10119 static int
10120 biv_elimination_giv_has_0_offset (struct induction *biv,
10121 struct induction *giv, rtx insn)
10123 /* If the giv V had the auto-inc address optimization applied
10124 to it, and INSN occurs between the giv insn and the biv
10125 insn, then we'd have to adjust the value used here.
10126 This is rare, so we don't bother to make this possible. */
10127 if (giv->auto_inc_opt
10128 && ((loop_insn_first_p (giv->insn, insn)
10129 && loop_insn_first_p (insn, biv->insn))
10130 || (loop_insn_first_p (biv->insn, insn)
10131 && loop_insn_first_p (insn, giv->insn))))
10132 return 0;
10134 return 1;
10137 /* If BL appears in X (part of the pattern of INSN), see if we can
10138 eliminate its use. If so, return 1. If not, return 0.
10140 If BIV does not appear in X, return 1.
10142 If ELIMINATE_P is nonzero, actually do the elimination.
10143 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
10144 Depending on how many items have been moved out of the loop, it
10145 will either be before INSN (when WHERE_INSN is nonzero) or at the
10146 start of the loop (when WHERE_INSN is zero). */
10148 static int
10149 maybe_eliminate_biv_1 (const struct loop *loop, rtx x, rtx insn,
10150 struct iv_class *bl, int eliminate_p,
10151 basic_block where_bb, rtx where_insn)
10153 enum rtx_code code = GET_CODE (x);
10154 rtx reg = bl->biv->dest_reg;
10155 enum machine_mode mode = GET_MODE (reg);
10156 struct induction *v;
10157 rtx arg, tem;
10158 #ifdef HAVE_cc0
10159 rtx new;
10160 #endif
10161 int arg_operand;
10162 const char *fmt;
10163 int i, j;
10165 switch (code)
10167 case REG:
10168 /* If we haven't already been able to do something with this BIV,
10169 we can't eliminate it. */
10170 if (x == reg)
10171 return 0;
10172 return 1;
10174 case SET:
10175 /* If this sets the BIV, it is not a problem. */
10176 if (SET_DEST (x) == reg)
10177 return 1;
10179 /* If this is an insn that defines a giv, it is also ok because
10180 it will go away when the giv is reduced. */
10181 for (v = bl->giv; v; v = v->next_iv)
10182 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
10183 return 1;
10185 #ifdef HAVE_cc0
10186 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
10188 /* Can replace with any giv that was reduced and
10189 that has (MULT_VAL != 0) and (ADD_VAL == 0).
10190 Require a constant for MULT_VAL, so we know it's nonzero.
10191 ??? We disable this optimization to avoid potential
10192 overflows. */
10194 for (v = bl->giv; v; v = v->next_iv)
10195 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
10196 && v->add_val == const0_rtx
10197 && ! v->ignore && ! v->maybe_dead && v->always_computable
10198 && v->mode == mode
10199 && 0)
10201 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10202 continue;
10204 if (! eliminate_p)
10205 return 1;
10207 /* If the giv has the opposite direction of change,
10208 then reverse the comparison. */
10209 if (INTVAL (v->mult_val) < 0)
10210 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
10211 const0_rtx, v->new_reg);
10212 else
10213 new = v->new_reg;
10215 /* We can probably test that giv's reduced reg. */
10216 if (validate_change (insn, &SET_SRC (x), new, 0))
10217 return 1;
10220 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
10221 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
10222 Require a constant for MULT_VAL, so we know it's nonzero.
10223 ??? Do this only if ADD_VAL is a pointer to avoid a potential
10224 overflow problem. */
10226 for (v = bl->giv; v; v = v->next_iv)
10227 if (GET_CODE (v->mult_val) == CONST_INT
10228 && v->mult_val != const0_rtx
10229 && ! v->ignore && ! v->maybe_dead && v->always_computable
10230 && v->mode == mode
10231 && (GET_CODE (v->add_val) == SYMBOL_REF
10232 || GET_CODE (v->add_val) == LABEL_REF
10233 || GET_CODE (v->add_val) == CONST
10234 || (REG_P (v->add_val)
10235 && REG_POINTER (v->add_val))))
10237 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10238 continue;
10240 if (! eliminate_p)
10241 return 1;
10243 /* If the giv has the opposite direction of change,
10244 then reverse the comparison. */
10245 if (INTVAL (v->mult_val) < 0)
10246 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
10247 v->new_reg);
10248 else
10249 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
10250 copy_rtx (v->add_val));
10252 /* Replace biv with the giv's reduced register. */
10253 update_reg_last_use (v->add_val, insn);
10254 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
10255 return 1;
10257 /* Insn doesn't support that constant or invariant. Copy it
10258 into a register (it will be a loop invariant.) */
10259 tem = gen_reg_rtx (GET_MODE (v->new_reg));
10261 loop_insn_emit_before (loop, 0, where_insn,
10262 gen_move_insn (tem,
10263 copy_rtx (v->add_val)));
10265 /* Substitute the new register for its invariant value in
10266 the compare expression. */
10267 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
10268 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
10269 return 1;
10272 #endif
10273 break;
10275 case COMPARE:
10276 case EQ: case NE:
10277 case GT: case GE: case GTU: case GEU:
10278 case LT: case LE: case LTU: case LEU:
10279 /* See if either argument is the biv. */
10280 if (XEXP (x, 0) == reg)
10281 arg = XEXP (x, 1), arg_operand = 1;
10282 else if (XEXP (x, 1) == reg)
10283 arg = XEXP (x, 0), arg_operand = 0;
10284 else
10285 break;
10287 if (GET_CODE (arg) != CONST_INT)
10288 return 0;
10290 /* Unless we're dealing with an equality comparison, if we can't
10291 determine that the original biv doesn't wrap, then we must not
10292 apply the transformation. */
10293 /* ??? Actually, what we must do is verify that the transformed
10294 giv doesn't wrap. But the general case of this transformation
10295 was disabled long ago due to wrapping problems, and there's no
10296 point reviving it this close to end-of-life for loop.c. The
10297 only case still enabled is known (via the check on add_val) to
10298 be pointer arithmetic, which in theory never overflows for
10299 valid programs. */
10300 /* Without lifetime analysis, we don't know how COMPARE will be
10301 used, so we must assume the worst. */
10302 if (code != EQ && code != NE
10303 && biased_biv_may_wrap_p (loop, bl, INTVAL (arg)))
10304 return 0;
10306 /* Try to replace with any giv that has constant positive mult_val
10307 and a pointer add_val. */
10308 for (v = bl->giv; v; v = v->next_iv)
10309 if (GET_CODE (v->mult_val) == CONST_INT
10310 && INTVAL (v->mult_val) > 0
10311 && (GET_CODE (v->add_val) == SYMBOL_REF
10312 || GET_CODE (v->add_val) == LABEL_REF
10313 || GET_CODE (v->add_val) == CONST
10314 || (REG_P (v->add_val) && REG_POINTER (v->add_val)))
10315 && ! v->ignore && ! v->maybe_dead && v->always_computable
10316 && v->mode == mode)
10318 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
10319 continue;
10321 if (! eliminate_p)
10322 return 1;
10324 /* Replace biv with the giv's reduced reg. */
10325 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
10327 /* Load the value into a register. */
10328 tem = gen_reg_rtx (mode);
10329 loop_iv_add_mult_emit_before (loop, arg, v->mult_val, v->add_val,
10330 tem, where_bb, where_insn);
10332 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
10334 if (apply_change_group ())
10335 return 1;
10338 /* If we get here, the biv can't be eliminated. */
10339 return 0;
10341 case MEM:
10342 /* If this address is a DEST_ADDR giv, it doesn't matter if the
10343 biv is used in it, since it will be replaced. */
10344 for (v = bl->giv; v; v = v->next_iv)
10345 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
10346 return 1;
10347 break;
10349 default:
10350 break;
10353 /* See if any subexpression fails elimination. */
10354 fmt = GET_RTX_FORMAT (code);
10355 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10357 switch (fmt[i])
10359 case 'e':
10360 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
10361 eliminate_p, where_bb, where_insn))
10362 return 0;
10363 break;
10365 case 'E':
10366 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10367 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
10368 eliminate_p, where_bb, where_insn))
10369 return 0;
10370 break;
10374 return 1;
10377 /* Return nonzero if the last use of REG
10378 is in an insn following INSN in the same basic block. */
10380 static int
10381 last_use_this_basic_block (rtx reg, rtx insn)
10383 rtx n;
10384 for (n = insn;
10385 n && !LABEL_P (n) && !JUMP_P (n);
10386 n = NEXT_INSN (n))
10388 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
10389 return 1;
10391 return 0;
10394 /* Called via `note_stores' to record the initial value of a biv. Here we
10395 just record the location of the set and process it later. */
10397 static void
10398 record_initial (rtx dest, rtx set, void *data ATTRIBUTE_UNUSED)
10400 struct loop_ivs *ivs = (struct loop_ivs *) data;
10401 struct iv_class *bl;
10403 if (!REG_P (dest)
10404 || REGNO (dest) >= ivs->n_regs
10405 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
10406 return;
10408 bl = REG_IV_CLASS (ivs, REGNO (dest));
10410 /* If this is the first set found, record it. */
10411 if (bl->init_insn == 0)
10413 bl->init_insn = note_insn;
10414 bl->init_set = set;
10418 /* If any of the registers in X are "old" and currently have a last use earlier
10419 than INSN, update them to have a last use of INSN. Their actual last use
10420 will be the previous insn but it will not have a valid uid_luid so we can't
10421 use it. X must be a source expression only. */
10423 static void
10424 update_reg_last_use (rtx x, rtx insn)
10426 /* Check for the case where INSN does not have a valid luid. In this case,
10427 there is no need to modify the regno_last_uid, as this can only happen
10428 when code is inserted after the loop_end to set a pseudo's final value,
10429 and hence this insn will never be the last use of x.
10430 ???? This comment is not correct. See for example loop_givs_reduce.
10431 This may insert an insn before another new insn. */
10432 if (REG_P (x) && REGNO (x) < max_reg_before_loop
10433 && INSN_UID (insn) < max_uid_for_loop
10434 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
10436 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
10438 else
10440 int i, j;
10441 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10442 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
10444 if (fmt[i] == 'e')
10445 update_reg_last_use (XEXP (x, i), insn);
10446 else if (fmt[i] == 'E')
10447 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10448 update_reg_last_use (XVECEXP (x, i, j), insn);
10453 /* Similar to rtlanal.c:get_condition, except that we also put an
10454 invariant last unless both operands are invariants. */
10456 static rtx
10457 get_condition_for_loop (const struct loop *loop, rtx x)
10459 rtx comparison = get_condition (x, (rtx*) 0, false, true);
10461 if (comparison == 0
10462 || ! loop_invariant_p (loop, XEXP (comparison, 0))
10463 || loop_invariant_p (loop, XEXP (comparison, 1)))
10464 return comparison;
10466 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
10467 XEXP (comparison, 1), XEXP (comparison, 0));
10470 /* Scan the function and determine whether it has indirect (computed) jumps.
10472 This is taken mostly from flow.c; similar code exists elsewhere
10473 in the compiler. It may be useful to put this into rtlanal.c. */
10474 static int
10475 indirect_jump_in_function_p (rtx start)
10477 rtx insn;
10479 for (insn = start; insn; insn = NEXT_INSN (insn))
10480 if (computed_jump_p (insn))
10481 return 1;
10483 return 0;
10486 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
10487 documentation for LOOP_MEMS for the definition of `appropriate'.
10488 This function is called from prescan_loop via for_each_rtx. */
10490 static int
10491 insert_loop_mem (rtx *mem, void *data ATTRIBUTE_UNUSED)
10493 struct loop_info *loop_info = data;
10494 int i;
10495 rtx m = *mem;
10497 if (m == NULL_RTX)
10498 return 0;
10500 switch (GET_CODE (m))
10502 case MEM:
10503 break;
10505 case CLOBBER:
10506 /* We're not interested in MEMs that are only clobbered. */
10507 return -1;
10509 case CONST_DOUBLE:
10510 /* We're not interested in the MEM associated with a
10511 CONST_DOUBLE, so there's no need to traverse into this. */
10512 return -1;
10514 case EXPR_LIST:
10515 /* We're not interested in any MEMs that only appear in notes. */
10516 return -1;
10518 default:
10519 /* This is not a MEM. */
10520 return 0;
10523 /* See if we've already seen this MEM. */
10524 for (i = 0; i < loop_info->mems_idx; ++i)
10525 if (rtx_equal_p (m, loop_info->mems[i].mem))
10527 if (MEM_VOLATILE_P (m) && !MEM_VOLATILE_P (loop_info->mems[i].mem))
10528 loop_info->mems[i].mem = m;
10529 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
10530 /* The modes of the two memory accesses are different. If
10531 this happens, something tricky is going on, and we just
10532 don't optimize accesses to this MEM. */
10533 loop_info->mems[i].optimize = 0;
10535 return 0;
10538 /* Resize the array, if necessary. */
10539 if (loop_info->mems_idx == loop_info->mems_allocated)
10541 if (loop_info->mems_allocated != 0)
10542 loop_info->mems_allocated *= 2;
10543 else
10544 loop_info->mems_allocated = 32;
10546 loop_info->mems = xrealloc (loop_info->mems,
10547 loop_info->mems_allocated * sizeof (loop_mem_info));
10550 /* Actually insert the MEM. */
10551 loop_info->mems[loop_info->mems_idx].mem = m;
10552 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
10553 because we can't put it in a register. We still store it in the
10554 table, though, so that if we see the same address later, but in a
10555 non-BLK mode, we'll not think we can optimize it at that point. */
10556 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
10557 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
10558 ++loop_info->mems_idx;
10560 return 0;
10564 /* Allocate REGS->ARRAY or reallocate it if it is too small.
10566 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
10567 register that is modified by an insn between FROM and TO. If the
10568 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
10569 more, stop incrementing it, to avoid overflow.
10571 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
10572 register I is used, if it is only used once. Otherwise, it is set
10573 to 0 (for no uses) or const0_rtx for more than one use. This
10574 parameter may be zero, in which case this processing is not done.
10576 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
10577 optimize register I. */
10579 static void
10580 loop_regs_scan (const struct loop *loop, int extra_size)
10582 struct loop_regs *regs = LOOP_REGS (loop);
10583 int old_nregs;
10584 /* last_set[n] is nonzero iff reg n has been set in the current
10585 basic block. In that case, it is the insn that last set reg n. */
10586 rtx *last_set;
10587 rtx insn;
10588 int i;
10590 old_nregs = regs->num;
10591 regs->num = max_reg_num ();
10593 /* Grow the regs array if not allocated or too small. */
10594 if (regs->num >= regs->size)
10596 regs->size = regs->num + extra_size;
10598 regs->array = xrealloc (regs->array, regs->size * sizeof (*regs->array));
10600 /* Zero the new elements. */
10601 memset (regs->array + old_nregs, 0,
10602 (regs->size - old_nregs) * sizeof (*regs->array));
10605 /* Clear previously scanned fields but do not clear n_times_set. */
10606 for (i = 0; i < old_nregs; i++)
10608 regs->array[i].set_in_loop = 0;
10609 regs->array[i].may_not_optimize = 0;
10610 regs->array[i].single_usage = NULL_RTX;
10613 last_set = XCNEWVEC (rtx, regs->num);
10615 /* Scan the loop, recording register usage. */
10616 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
10617 insn = NEXT_INSN (insn))
10619 if (INSN_P (insn))
10621 /* Record registers that have exactly one use. */
10622 find_single_use_in_loop (regs, insn, PATTERN (insn));
10624 /* Include uses in REG_EQUAL notes. */
10625 if (REG_NOTES (insn))
10626 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
10628 if (GET_CODE (PATTERN (insn)) == SET
10629 || GET_CODE (PATTERN (insn)) == CLOBBER)
10630 count_one_set (regs, insn, PATTERN (insn), last_set);
10631 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
10633 int i;
10634 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
10635 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
10636 last_set);
10640 if (LABEL_P (insn) || JUMP_P (insn))
10641 memset (last_set, 0, regs->num * sizeof (rtx));
10643 /* Invalidate all registers used for function argument passing.
10644 We check rtx_varies_p for the same reason as below, to allow
10645 optimizing PIC calculations. */
10646 if (CALL_P (insn))
10648 rtx link;
10649 for (link = CALL_INSN_FUNCTION_USAGE (insn);
10650 link;
10651 link = XEXP (link, 1))
10653 rtx op, reg;
10655 if (GET_CODE (op = XEXP (link, 0)) == USE
10656 && REG_P (reg = XEXP (op, 0))
10657 && rtx_varies_p (reg, 1))
10658 regs->array[REGNO (reg)].may_not_optimize = 1;
10663 /* Invalidate all hard registers clobbered by calls. With one exception:
10664 a call-clobbered PIC register is still function-invariant for our
10665 purposes, since we can hoist any PIC calculations out of the loop.
10666 Thus the call to rtx_varies_p. */
10667 if (LOOP_INFO (loop)->has_call)
10668 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10669 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
10670 && rtx_varies_p (regno_reg_rtx[i], 1))
10672 regs->array[i].may_not_optimize = 1;
10673 regs->array[i].set_in_loop = 1;
10676 #ifdef AVOID_CCMODE_COPIES
10677 /* Don't try to move insns which set CC registers if we should not
10678 create CCmode register copies. */
10679 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
10680 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
10681 regs->array[i].may_not_optimize = 1;
10682 #endif
10684 /* Set regs->array[I].n_times_set for the new registers. */
10685 for (i = old_nregs; i < regs->num; i++)
10686 regs->array[i].n_times_set = regs->array[i].set_in_loop;
10688 free (last_set);
10691 /* Returns the number of real INSNs in the LOOP. */
10693 static int
10694 count_insns_in_loop (const struct loop *loop)
10696 int count = 0;
10697 rtx insn;
10699 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
10700 insn = NEXT_INSN (insn))
10701 if (INSN_P (insn))
10702 ++count;
10704 return count;
10707 /* Move MEMs into registers for the duration of the loop. */
10709 static void
10710 load_mems (const struct loop *loop)
10712 struct loop_info *loop_info = LOOP_INFO (loop);
10713 struct loop_regs *regs = LOOP_REGS (loop);
10714 int maybe_never = 0;
10715 int i;
10716 rtx p, prev_ebb_head;
10717 rtx label = NULL_RTX;
10718 rtx end_label;
10719 /* Nonzero if the next instruction may never be executed. */
10720 int next_maybe_never = 0;
10721 unsigned int last_max_reg = max_reg_num ();
10723 if (loop_info->mems_idx == 0)
10724 return;
10726 /* We cannot use next_label here because it skips over normal insns. */
10727 end_label = next_nonnote_insn (loop->end);
10728 if (end_label && !LABEL_P (end_label))
10729 end_label = NULL_RTX;
10731 /* Check to see if it's possible that some instructions in the loop are
10732 never executed. Also check if there is a goto out of the loop other
10733 than right after the end of the loop. */
10734 for (p = next_insn_in_loop (loop, loop->scan_start);
10735 p != NULL_RTX;
10736 p = next_insn_in_loop (loop, p))
10738 if (LABEL_P (p))
10739 maybe_never = 1;
10740 else if (JUMP_P (p)
10741 /* If we enter the loop in the middle, and scan
10742 around to the beginning, don't set maybe_never
10743 for that. This must be an unconditional jump,
10744 otherwise the code at the top of the loop might
10745 never be executed. Unconditional jumps are
10746 followed a by barrier then loop end. */
10747 && ! (JUMP_P (p)
10748 && JUMP_LABEL (p) == loop->top
10749 && NEXT_INSN (NEXT_INSN (p)) == loop->end
10750 && any_uncondjump_p (p)))
10752 /* If this is a jump outside of the loop but not right
10753 after the end of the loop, we would have to emit new fixup
10754 sequences for each such label. */
10755 if (/* If we can't tell where control might go when this
10756 JUMP_INSN is executed, we must be conservative. */
10757 !JUMP_LABEL (p)
10758 || (JUMP_LABEL (p) != end_label
10759 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
10760 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
10761 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
10762 return;
10764 if (!any_condjump_p (p))
10765 /* Something complicated. */
10766 maybe_never = 1;
10767 else
10768 /* If there are any more instructions in the loop, they
10769 might not be reached. */
10770 next_maybe_never = 1;
10772 else if (next_maybe_never)
10773 maybe_never = 1;
10776 /* Find start of the extended basic block that enters the loop. */
10777 for (p = loop->start;
10778 PREV_INSN (p) && !LABEL_P (p);
10779 p = PREV_INSN (p))
10781 prev_ebb_head = p;
10783 cselib_init (true);
10785 /* Build table of mems that get set to constant values before the
10786 loop. */
10787 for (; p != loop->start; p = NEXT_INSN (p))
10788 cselib_process_insn (p);
10790 /* Actually move the MEMs. */
10791 for (i = 0; i < loop_info->mems_idx; ++i)
10793 regset_head load_copies;
10794 regset_head store_copies;
10795 int written = 0;
10796 rtx reg;
10797 rtx mem = loop_info->mems[i].mem;
10798 rtx mem_list_entry;
10800 if (MEM_VOLATILE_P (mem)
10801 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
10802 /* There's no telling whether or not MEM is modified. */
10803 loop_info->mems[i].optimize = 0;
10805 /* Go through the MEMs written to in the loop to see if this
10806 one is aliased by one of them. */
10807 mem_list_entry = loop_info->store_mems;
10808 while (mem_list_entry)
10810 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
10811 written = 1;
10812 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
10813 mem, rtx_varies_p))
10815 /* MEM is indeed aliased by this store. */
10816 loop_info->mems[i].optimize = 0;
10817 break;
10819 mem_list_entry = XEXP (mem_list_entry, 1);
10822 if (flag_float_store && written
10823 && SCALAR_FLOAT_MODE_P (GET_MODE (mem)))
10824 loop_info->mems[i].optimize = 0;
10826 /* If this MEM is written to, we must be sure that there
10827 are no reads from another MEM that aliases this one. */
10828 if (loop_info->mems[i].optimize && written)
10830 int j;
10832 for (j = 0; j < loop_info->mems_idx; ++j)
10834 if (j == i)
10835 continue;
10836 else if (true_dependence (mem,
10837 VOIDmode,
10838 loop_info->mems[j].mem,
10839 rtx_varies_p))
10841 /* It's not safe to hoist loop_info->mems[i] out of
10842 the loop because writes to it might not be
10843 seen by reads from loop_info->mems[j]. */
10844 loop_info->mems[i].optimize = 0;
10845 break;
10850 if (maybe_never && may_trap_p (mem))
10851 /* We can't access the MEM outside the loop; it might
10852 cause a trap that wouldn't have happened otherwise. */
10853 loop_info->mems[i].optimize = 0;
10855 if (!loop_info->mems[i].optimize)
10856 /* We thought we were going to lift this MEM out of the
10857 loop, but later discovered that we could not. */
10858 continue;
10860 INIT_REG_SET (&load_copies);
10861 INIT_REG_SET (&store_copies);
10863 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
10864 order to keep scan_loop from moving stores to this MEM
10865 out of the loop just because this REG is neither a
10866 user-variable nor used in the loop test. */
10867 reg = gen_reg_rtx (GET_MODE (mem));
10868 REG_USERVAR_P (reg) = 1;
10869 loop_info->mems[i].reg = reg;
10871 /* Now, replace all references to the MEM with the
10872 corresponding pseudos. */
10873 maybe_never = 0;
10874 for (p = next_insn_in_loop (loop, loop->scan_start);
10875 p != NULL_RTX;
10876 p = next_insn_in_loop (loop, p))
10878 if (INSN_P (p))
10880 rtx set;
10882 set = single_set (p);
10884 /* See if this copies the mem into a register that isn't
10885 modified afterwards. We'll try to do copy propagation
10886 a little further on. */
10887 if (set
10888 /* @@@ This test is _way_ too conservative. */
10889 && ! maybe_never
10890 && REG_P (SET_DEST (set))
10891 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
10892 && REGNO (SET_DEST (set)) < last_max_reg
10893 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
10894 && rtx_equal_p (SET_SRC (set), mem))
10895 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
10897 /* See if this copies the mem from a register that isn't
10898 modified afterwards. We'll try to remove the
10899 redundant copy later on by doing a little register
10900 renaming and copy propagation. This will help
10901 to untangle things for the BIV detection code. */
10902 if (set
10903 && ! maybe_never
10904 && REG_P (SET_SRC (set))
10905 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
10906 && REGNO (SET_SRC (set)) < last_max_reg
10907 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
10908 && rtx_equal_p (SET_DEST (set), mem))
10909 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
10911 /* If this is a call which uses / clobbers this memory
10912 location, we must not change the interface here. */
10913 if (CALL_P (p)
10914 && reg_mentioned_p (loop_info->mems[i].mem,
10915 CALL_INSN_FUNCTION_USAGE (p)))
10917 cancel_changes (0);
10918 loop_info->mems[i].optimize = 0;
10919 break;
10921 else
10922 /* Replace the memory reference with the shadow register. */
10923 replace_loop_mems (p, loop_info->mems[i].mem,
10924 loop_info->mems[i].reg, written);
10927 if (LABEL_P (p)
10928 || JUMP_P (p))
10929 maybe_never = 1;
10932 if (! loop_info->mems[i].optimize)
10933 ; /* We found we couldn't do the replacement, so do nothing. */
10934 else if (! apply_change_group ())
10935 /* We couldn't replace all occurrences of the MEM. */
10936 loop_info->mems[i].optimize = 0;
10937 else
10939 /* Load the memory immediately before LOOP->START, which is
10940 the NOTE_LOOP_BEG. */
10941 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
10942 rtx set;
10943 rtx best = mem;
10944 unsigned j;
10945 struct elt_loc_list *const_equiv = 0;
10946 reg_set_iterator rsi;
10948 if (e)
10950 struct elt_loc_list *equiv;
10951 struct elt_loc_list *best_equiv = 0;
10952 for (equiv = e->locs; equiv; equiv = equiv->next)
10954 if (CONSTANT_P (equiv->loc))
10955 const_equiv = equiv;
10956 else if (REG_P (equiv->loc)
10957 /* Extending hard register lifetimes causes crash
10958 on SRC targets. Doing so on non-SRC is
10959 probably also not good idea, since we most
10960 probably have pseudoregister equivalence as
10961 well. */
10962 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
10963 best_equiv = equiv;
10965 /* Use the constant equivalence if that is cheap enough. */
10966 if (! best_equiv)
10967 best_equiv = const_equiv;
10968 else if (const_equiv
10969 && (rtx_cost (const_equiv->loc, SET)
10970 <= rtx_cost (best_equiv->loc, SET)))
10972 best_equiv = const_equiv;
10973 const_equiv = 0;
10976 /* If best_equiv is nonzero, we know that MEM is set to a
10977 constant or register before the loop. We will use this
10978 knowledge to initialize the shadow register with that
10979 constant or reg rather than by loading from MEM. */
10980 if (best_equiv)
10981 best = copy_rtx (best_equiv->loc);
10984 set = gen_move_insn (reg, best);
10985 set = loop_insn_hoist (loop, set);
10986 if (REG_P (best))
10988 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
10989 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
10991 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
10992 break;
10996 if (const_equiv)
10997 set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
10999 if (written)
11001 if (label == NULL_RTX)
11003 label = gen_label_rtx ();
11004 emit_label_after (label, loop->end);
11007 /* Store the memory immediately after END, which is
11008 the NOTE_LOOP_END. */
11009 set = gen_move_insn (copy_rtx (mem), reg);
11010 loop_insn_emit_after (loop, 0, label, set);
11013 if (dump_file)
11015 fprintf (dump_file, "Hoisted regno %d %s from ",
11016 REGNO (reg), (written ? "r/w" : "r/o"));
11017 print_rtl (dump_file, mem);
11018 fputc ('\n', dump_file);
11021 /* Attempt a bit of copy propagation. This helps untangle the
11022 data flow, and enables {basic,general}_induction_var to find
11023 more bivs/givs. */
11024 EXECUTE_IF_SET_IN_REG_SET
11025 (&load_copies, FIRST_PSEUDO_REGISTER, j, rsi)
11027 try_copy_prop (loop, reg, j);
11029 CLEAR_REG_SET (&load_copies);
11031 EXECUTE_IF_SET_IN_REG_SET
11032 (&store_copies, FIRST_PSEUDO_REGISTER, j, rsi)
11034 try_swap_copy_prop (loop, reg, j);
11036 CLEAR_REG_SET (&store_copies);
11040 /* Now, we need to replace all references to the previous exit
11041 label with the new one. */
11042 if (label != NULL_RTX && end_label != NULL_RTX)
11043 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
11044 if (JUMP_P (p) && JUMP_LABEL (p) == end_label)
11045 redirect_jump (p, label, false);
11047 cselib_finish ();
11050 /* For communication between note_reg_stored and its caller. */
11051 struct note_reg_stored_arg
11053 int set_seen;
11054 rtx reg;
11057 /* Called via note_stores, record in SET_SEEN whether X, which is written,
11058 is equal to ARG. */
11059 static void
11060 note_reg_stored (rtx x, rtx setter ATTRIBUTE_UNUSED, void *arg)
11062 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
11063 if (t->reg == x)
11064 t->set_seen = 1;
11067 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
11068 There must be exactly one insn that sets this pseudo; it will be
11069 deleted if all replacements succeed and we can prove that the register
11070 is not used after the loop. */
11072 static void
11073 try_copy_prop (const struct loop *loop, rtx replacement, unsigned int regno)
11075 /* This is the reg that we are copying from. */
11076 rtx reg_rtx = regno_reg_rtx[regno];
11077 rtx init_insn = 0;
11078 rtx insn;
11079 /* These help keep track of whether we replaced all uses of the reg. */
11080 int replaced_last = 0;
11081 int store_is_first = 0;
11083 for (insn = next_insn_in_loop (loop, loop->scan_start);
11084 insn != NULL_RTX;
11085 insn = next_insn_in_loop (loop, insn))
11087 rtx set;
11089 /* Only substitute within one extended basic block from the initializing
11090 insn. */
11091 if (LABEL_P (insn) && init_insn)
11092 break;
11094 if (! INSN_P (insn))
11095 continue;
11097 /* Is this the initializing insn? */
11098 set = single_set (insn);
11099 if (set
11100 && REG_P (SET_DEST (set))
11101 && REGNO (SET_DEST (set)) == regno)
11103 gcc_assert (!init_insn);
11105 init_insn = insn;
11106 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
11107 store_is_first = 1;
11110 /* Only substitute after seeing the initializing insn. */
11111 if (init_insn && insn != init_insn)
11113 struct note_reg_stored_arg arg;
11115 replace_loop_regs (insn, reg_rtx, replacement);
11116 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
11117 replaced_last = 1;
11119 /* Stop replacing when REPLACEMENT is modified. */
11120 arg.reg = replacement;
11121 arg.set_seen = 0;
11122 note_stores (PATTERN (insn), note_reg_stored, &arg);
11123 if (arg.set_seen)
11125 rtx note = find_reg_note (insn, REG_EQUAL, NULL);
11127 /* It is possible that we've turned previously valid REG_EQUAL to
11128 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
11129 REPLACEMENT is modified, we get different meaning. */
11130 if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
11131 remove_note (insn, note);
11132 break;
11136 gcc_assert (init_insn);
11137 if (apply_change_group ())
11139 if (dump_file)
11140 fprintf (dump_file, " Replaced reg %d", regno);
11141 if (store_is_first && replaced_last)
11143 rtx first;
11144 rtx retval_note;
11146 /* Assume we're just deleting INIT_INSN. */
11147 first = init_insn;
11148 /* Look for REG_RETVAL note. If we're deleting the end of
11149 the libcall sequence, the whole sequence can go. */
11150 retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
11151 /* If we found a REG_RETVAL note, find the first instruction
11152 in the sequence. */
11153 if (retval_note)
11154 first = XEXP (retval_note, 0);
11156 /* Delete the instructions. */
11157 loop_delete_insns (first, init_insn);
11159 if (dump_file)
11160 fprintf (dump_file, ".\n");
11164 /* Replace all the instructions from FIRST up to and including LAST
11165 with NOTE_INSN_DELETED notes. */
11167 static void
11168 loop_delete_insns (rtx first, rtx last)
11170 while (1)
11172 if (dump_file)
11173 fprintf (dump_file, ", deleting init_insn (%d)",
11174 INSN_UID (first));
11175 delete_insn (first);
11177 /* If this was the LAST instructions we're supposed to delete,
11178 we're done. */
11179 if (first == last)
11180 break;
11182 first = NEXT_INSN (first);
11186 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
11187 loop LOOP if the order of the sets of these registers can be
11188 swapped. There must be exactly one insn within the loop that sets
11189 this pseudo followed immediately by a move insn that sets
11190 REPLACEMENT with REGNO. */
11191 static void
11192 try_swap_copy_prop (const struct loop *loop, rtx replacement,
11193 unsigned int regno)
11195 rtx insn;
11196 rtx set = NULL_RTX;
11197 unsigned int new_regno;
11199 new_regno = REGNO (replacement);
11201 for (insn = next_insn_in_loop (loop, loop->scan_start);
11202 insn != NULL_RTX;
11203 insn = next_insn_in_loop (loop, insn))
11205 /* Search for the insn that copies REGNO to NEW_REGNO? */
11206 if (INSN_P (insn)
11207 && (set = single_set (insn))
11208 && REG_P (SET_DEST (set))
11209 && REGNO (SET_DEST (set)) == new_regno
11210 && REG_P (SET_SRC (set))
11211 && REGNO (SET_SRC (set)) == regno)
11212 break;
11215 if (insn != NULL_RTX)
11217 rtx prev_insn;
11218 rtx prev_set;
11220 /* Some DEF-USE info would come in handy here to make this
11221 function more general. For now, just check the previous insn
11222 which is the most likely candidate for setting REGNO. */
11224 prev_insn = PREV_INSN (insn);
11226 if (INSN_P (insn)
11227 && (prev_set = single_set (prev_insn))
11228 && REG_P (SET_DEST (prev_set))
11229 && REGNO (SET_DEST (prev_set)) == regno)
11231 /* We have:
11232 (set (reg regno) (expr))
11233 (set (reg new_regno) (reg regno))
11235 so try converting this to:
11236 (set (reg new_regno) (expr))
11237 (set (reg regno) (reg new_regno))
11239 The former construct is often generated when a global
11240 variable used for an induction variable is shadowed by a
11241 register (NEW_REGNO). The latter construct improves the
11242 chances of GIV replacement and BIV elimination. */
11244 validate_change (prev_insn, &SET_DEST (prev_set),
11245 replacement, 1);
11246 validate_change (insn, &SET_DEST (set),
11247 SET_SRC (set), 1);
11248 validate_change (insn, &SET_SRC (set),
11249 replacement, 1);
11251 if (apply_change_group ())
11253 if (dump_file)
11254 fprintf (dump_file,
11255 " Swapped set of reg %d at %d with reg %d at %d.\n",
11256 regno, INSN_UID (insn),
11257 new_regno, INSN_UID (prev_insn));
11259 /* Update first use of REGNO. */
11260 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
11261 REGNO_FIRST_UID (regno) = INSN_UID (insn);
11263 /* Now perform copy propagation to hopefully
11264 remove all uses of REGNO within the loop. */
11265 try_copy_prop (loop, replacement, regno);
11271 /* Worker function for find_mem_in_note, called via for_each_rtx. */
11273 static int
11274 find_mem_in_note_1 (rtx *x, void *data)
11276 if (*x != NULL_RTX && MEM_P (*x))
11278 rtx *res = (rtx *) data;
11279 *res = *x;
11280 return 1;
11282 return 0;
11285 /* Returns the first MEM found in NOTE by depth-first search. */
11287 static rtx
11288 find_mem_in_note (rtx note)
11290 if (note && for_each_rtx (&note, find_mem_in_note_1, &note))
11291 return note;
11292 return NULL_RTX;
11295 /* Replace MEM with its associated pseudo register. This function is
11296 called from load_mems via for_each_rtx. DATA is actually a pointer
11297 to a structure describing the instruction currently being scanned
11298 and the MEM we are currently replacing. */
11300 static int
11301 replace_loop_mem (rtx *mem, void *data)
11303 loop_replace_args *args = (loop_replace_args *) data;
11304 rtx m = *mem;
11306 if (m == NULL_RTX)
11307 return 0;
11309 switch (GET_CODE (m))
11311 case MEM:
11312 break;
11314 case CONST_DOUBLE:
11315 /* We're not interested in the MEM associated with a
11316 CONST_DOUBLE, so there's no need to traverse into one. */
11317 return -1;
11319 default:
11320 /* This is not a MEM. */
11321 return 0;
11324 if (!rtx_equal_p (args->match, m))
11325 /* This is not the MEM we are currently replacing. */
11326 return 0;
11328 /* Actually replace the MEM. */
11329 validate_change (args->insn, mem, args->replacement, 1);
11331 return 0;
11334 static void
11335 replace_loop_mems (rtx insn, rtx mem, rtx reg, int written)
11337 loop_replace_args args;
11339 args.insn = insn;
11340 args.match = mem;
11341 args.replacement = reg;
11343 for_each_rtx (&insn, replace_loop_mem, &args);
11345 /* If we hoist a mem write out of the loop, then REG_EQUAL
11346 notes referring to the mem are no longer valid. */
11347 if (written)
11349 rtx note, sub;
11350 rtx *link;
11352 for (link = &REG_NOTES (insn); (note = *link); link = &XEXP (note, 1))
11354 if (REG_NOTE_KIND (note) == REG_EQUAL
11355 && (sub = find_mem_in_note (note))
11356 && true_dependence (mem, VOIDmode, sub, rtx_varies_p))
11358 /* Remove the note. */
11359 validate_change (NULL_RTX, link, XEXP (note, 1), 1);
11360 break;
11366 /* Replace one register with another. Called through for_each_rtx; PX points
11367 to the rtx being scanned. DATA is actually a pointer to
11368 a structure of arguments. */
11370 static int
11371 replace_loop_reg (rtx *px, void *data)
11373 rtx x = *px;
11374 loop_replace_args *args = (loop_replace_args *) data;
11376 if (x == NULL_RTX)
11377 return 0;
11379 if (x == args->match)
11380 validate_change (args->insn, px, args->replacement, 1);
11382 return 0;
11385 static void
11386 replace_loop_regs (rtx insn, rtx reg, rtx replacement)
11388 loop_replace_args args;
11390 args.insn = insn;
11391 args.match = reg;
11392 args.replacement = replacement;
11394 for_each_rtx (&insn, replace_loop_reg, &args);
11397 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
11398 (ignored in the interim). */
11400 static rtx
11401 loop_insn_emit_after (const struct loop *loop ATTRIBUTE_UNUSED,
11402 basic_block where_bb ATTRIBUTE_UNUSED, rtx where_insn,
11403 rtx pattern)
11405 return emit_insn_after (pattern, where_insn);
11409 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
11410 in basic block WHERE_BB (ignored in the interim) within the loop
11411 otherwise hoist PATTERN into the loop pre-header. */
11413 static rtx
11414 loop_insn_emit_before (const struct loop *loop,
11415 basic_block where_bb ATTRIBUTE_UNUSED,
11416 rtx where_insn, rtx pattern)
11418 if (! where_insn)
11419 return loop_insn_hoist (loop, pattern);
11420 return emit_insn_before (pattern, where_insn);
11424 /* Emit call insn for PATTERN before WHERE_INSN in basic block
11425 WHERE_BB (ignored in the interim) within the loop. */
11427 static rtx
11428 loop_call_insn_emit_before (const struct loop *loop ATTRIBUTE_UNUSED,
11429 basic_block where_bb ATTRIBUTE_UNUSED,
11430 rtx where_insn, rtx pattern)
11432 return emit_call_insn_before (pattern, where_insn);
11436 /* Hoist insn for PATTERN into the loop pre-header. */
11438 static rtx
11439 loop_insn_hoist (const struct loop *loop, rtx pattern)
11441 return loop_insn_emit_before (loop, 0, loop->start, pattern);
11445 /* Hoist call insn for PATTERN into the loop pre-header. */
11447 static rtx
11448 loop_call_insn_hoist (const struct loop *loop, rtx pattern)
11450 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
11454 /* Sink insn for PATTERN after the loop end. */
11456 static rtx
11457 loop_insn_sink (const struct loop *loop, rtx pattern)
11459 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
11462 /* bl->final_value can be either general_operand or PLUS of general_operand
11463 and constant. Emit sequence of instructions to load it into REG. */
11464 static rtx
11465 gen_load_of_final_value (rtx reg, rtx final_value)
11467 rtx seq;
11468 start_sequence ();
11469 final_value = force_operand (final_value, reg);
11470 if (final_value != reg)
11471 emit_move_insn (reg, final_value);
11472 seq = get_insns ();
11473 end_sequence ();
11474 return seq;
11477 /* If the loop has multiple exits, emit insn for PATTERN before the
11478 loop to ensure that it will always be executed no matter how the
11479 loop exits. Otherwise, emit the insn for PATTERN after the loop,
11480 since this is slightly more efficient. */
11482 static rtx
11483 loop_insn_sink_or_swim (const struct loop *loop, rtx pattern)
11485 if (loop->exit_count)
11486 return loop_insn_hoist (loop, pattern);
11487 else
11488 return loop_insn_sink (loop, pattern);
11491 static void
11492 loop_ivs_dump (const struct loop *loop, FILE *file, int verbose)
11494 struct iv_class *bl;
11495 int iv_num = 0;
11497 if (! loop || ! file)
11498 return;
11500 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
11501 iv_num++;
11503 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
11505 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
11507 loop_iv_class_dump (bl, file, verbose);
11508 fputc ('\n', file);
11513 static void
11514 loop_iv_class_dump (const struct iv_class *bl, FILE *file,
11515 int verbose ATTRIBUTE_UNUSED)
11517 struct induction *v;
11518 rtx incr;
11519 int i;
11521 if (! bl || ! file)
11522 return;
11524 fprintf (file, "IV class for reg %d, benefit %d\n",
11525 bl->regno, bl->total_benefit);
11527 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
11528 if (bl->initial_value)
11530 fprintf (file, ", init val: ");
11531 print_simple_rtl (file, bl->initial_value);
11533 if (bl->initial_test)
11535 fprintf (file, ", init test: ");
11536 print_simple_rtl (file, bl->initial_test);
11538 fputc ('\n', file);
11540 if (bl->final_value)
11542 fprintf (file, " Final val: ");
11543 print_simple_rtl (file, bl->final_value);
11544 fputc ('\n', file);
11547 if ((incr = biv_total_increment (bl)))
11549 fprintf (file, " Total increment: ");
11550 print_simple_rtl (file, incr);
11551 fputc ('\n', file);
11554 /* List the increments. */
11555 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
11557 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
11558 print_simple_rtl (file, v->add_val);
11559 fputc ('\n', file);
11562 /* List the givs. */
11563 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
11565 fprintf (file, " Giv%d: insn %d, benefit %d, ",
11566 i, INSN_UID (v->insn), v->benefit);
11567 if (v->giv_type == DEST_ADDR)
11568 print_simple_rtl (file, v->mem);
11569 else
11570 print_simple_rtl (file, single_set (v->insn));
11571 fputc ('\n', file);
11576 static void
11577 loop_biv_dump (const struct induction *v, FILE *file, int verbose)
11579 if (! v || ! file)
11580 return;
11582 fprintf (file,
11583 "Biv %d: insn %d",
11584 REGNO (v->dest_reg), INSN_UID (v->insn));
11585 fprintf (file, " const ");
11586 print_simple_rtl (file, v->add_val);
11588 if (verbose && v->final_value)
11590 fputc ('\n', file);
11591 fprintf (file, " final ");
11592 print_simple_rtl (file, v->final_value);
11595 fputc ('\n', file);
11599 static void
11600 loop_giv_dump (const struct induction *v, FILE *file, int verbose)
11602 if (! v || ! file)
11603 return;
11605 if (v->giv_type == DEST_REG)
11606 fprintf (file, "Giv %d: insn %d",
11607 REGNO (v->dest_reg), INSN_UID (v->insn));
11608 else
11609 fprintf (file, "Dest address: insn %d",
11610 INSN_UID (v->insn));
11612 fprintf (file, " src reg %d benefit %d",
11613 REGNO (v->src_reg), v->benefit);
11614 fprintf (file, " lifetime %d",
11615 v->lifetime);
11617 if (v->replaceable)
11618 fprintf (file, " replaceable");
11620 if (v->no_const_addval)
11621 fprintf (file, " ncav");
11623 if (v->ext_dependent)
11625 switch (GET_CODE (v->ext_dependent))
11627 case SIGN_EXTEND:
11628 fprintf (file, " ext se");
11629 break;
11630 case ZERO_EXTEND:
11631 fprintf (file, " ext ze");
11632 break;
11633 case TRUNCATE:
11634 fprintf (file, " ext tr");
11635 break;
11636 default:
11637 gcc_unreachable ();
11641 fputc ('\n', file);
11642 fprintf (file, " mult ");
11643 print_simple_rtl (file, v->mult_val);
11645 fputc ('\n', file);
11646 fprintf (file, " add ");
11647 print_simple_rtl (file, v->add_val);
11649 if (verbose && v->final_value)
11651 fputc ('\n', file);
11652 fprintf (file, " final ");
11653 print_simple_rtl (file, v->final_value);
11656 fputc ('\n', file);
11660 void
11661 debug_ivs (const struct loop *loop)
11663 loop_ivs_dump (loop, stderr, 1);
11667 void
11668 debug_iv_class (const struct iv_class *bl)
11670 loop_iv_class_dump (bl, stderr, 1);
11674 void
11675 debug_biv (const struct induction *v)
11677 loop_biv_dump (v, stderr, 1);
11681 void
11682 debug_giv (const struct induction *v)
11684 loop_giv_dump (v, stderr, 1);
11688 #define LOOP_BLOCK_NUM_1(INSN) \
11689 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
11691 /* The notes do not have an assigned block, so look at the next insn. */
11692 #define LOOP_BLOCK_NUM(INSN) \
11693 ((INSN) ? (NOTE_P (INSN) \
11694 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
11695 : LOOP_BLOCK_NUM_1 (INSN)) \
11696 : -1)
11698 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
11700 static void
11701 loop_dump_aux (const struct loop *loop, FILE *file,
11702 int verbose ATTRIBUTE_UNUSED)
11704 rtx label;
11706 if (! loop || ! file || !BB_HEAD (loop->first))
11707 return;
11709 /* Print diagnostics to compare our concept of a loop with
11710 what the loop notes say. */
11711 if (! PREV_INSN (BB_HEAD (loop->first))
11712 || !NOTE_P (PREV_INSN (BB_HEAD (loop->first)))
11713 || NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (loop->first)))
11714 != NOTE_INSN_LOOP_BEG)
11715 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
11716 INSN_UID (PREV_INSN (BB_HEAD (loop->first))));
11717 if (! NEXT_INSN (BB_END (loop->last))
11718 || !NOTE_P (NEXT_INSN (BB_END (loop->last)))
11719 || NOTE_LINE_NUMBER (NEXT_INSN (BB_END (loop->last)))
11720 != NOTE_INSN_LOOP_END)
11721 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
11722 INSN_UID (NEXT_INSN (BB_END (loop->last))));
11724 if (loop->start)
11726 fprintf (file,
11727 ";; start %d (%d), end %d (%d)\n",
11728 LOOP_BLOCK_NUM (loop->start),
11729 LOOP_INSN_UID (loop->start),
11730 LOOP_BLOCK_NUM (loop->end),
11731 LOOP_INSN_UID (loop->end));
11732 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
11733 LOOP_BLOCK_NUM (loop->top),
11734 LOOP_INSN_UID (loop->top),
11735 LOOP_BLOCK_NUM (loop->scan_start),
11736 LOOP_INSN_UID (loop->scan_start));
11737 fprintf (file, ";; exit_count %d", loop->exit_count);
11738 if (loop->exit_count)
11740 fputs (", labels:", file);
11741 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
11743 fprintf (file, " %d ",
11744 LOOP_INSN_UID (XEXP (label, 0)));
11747 fputs ("\n", file);
11751 /* Call this function from the debugger to dump LOOP. */
11753 void
11754 debug_loop (const struct loop *loop)
11756 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
11759 /* Call this function from the debugger to dump LOOPS. */
11761 void
11762 debug_loops (const struct loops *loops)
11764 flow_loops_dump (loops, stderr, loop_dump_aux, 1);
11767 static bool
11768 gate_handle_loop_optimize (void)
11770 return (optimize > 0 && flag_loop_optimize);
11773 /* Move constant computations out of loops. */
11774 static void
11775 rest_of_handle_loop_optimize (void)
11777 int do_prefetch;
11779 /* CFG is no longer maintained up-to-date. */
11780 free_bb_for_insn ();
11781 profile_status = PROFILE_ABSENT;
11783 do_prefetch = flag_prefetch_loop_arrays == 2 ? LOOP_PREFETCH : 0;
11785 if (flag_rerun_loop_opt)
11787 cleanup_barriers ();
11789 /* We only want to perform unrolling once. */
11790 loop_optimize (get_insns (), 0);
11792 /* The first call to loop_optimize makes some instructions
11793 trivially dead. We delete those instructions now in the
11794 hope that doing so will make the heuristics in loop work
11795 better and possibly speed up compilation. */
11796 delete_trivially_dead_insns (get_insns (), max_reg_num ());
11798 /* The regscan pass is currently necessary as the alias
11799 analysis code depends on this information. */
11800 reg_scan (get_insns (), max_reg_num ());
11802 cleanup_barriers ();
11803 loop_optimize (get_insns (), do_prefetch);
11805 /* Loop can create trivially dead instructions. */
11806 delete_trivially_dead_insns (get_insns (), max_reg_num ());
11807 find_basic_blocks (get_insns ());
11810 struct tree_opt_pass pass_loop_optimize =
11812 "old-loop", /* name */
11813 gate_handle_loop_optimize, /* gate */
11814 rest_of_handle_loop_optimize, /* execute */
11815 NULL, /* sub */
11816 NULL, /* next */
11817 0, /* static_pass_number */
11818 TV_LOOP, /* tv_id */
11819 0, /* properties_required */
11820 0, /* properties_provided */
11821 0, /* properties_destroyed */
11822 0, /* todo_flags_start */
11823 TODO_dump_func |
11824 TODO_ggc_collect, /* todo_flags_finish */
11825 'L' /* letter */