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
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
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, 59 Temple Place - Suite 330, Boston, MA
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. */
48 #include "coretypes.h"
54 #include "hard-reg-set.h"
55 #include "basic-block.h"
56 #include "insn-config.h"
65 #include "insn-flags.h"
70 /* Get the loop info pointer of a loop. */
71 #define LOOP_INFO(LOOP) ((struct loop_info *) (LOOP)->aux)
73 /* Get a pointer to the loop movables structure. */
74 #define LOOP_MOVABLES(LOOP) (&LOOP_INFO (LOOP)->movables)
76 /* Get a pointer to the loop registers structure. */
77 #define LOOP_REGS(LOOP) (&LOOP_INFO (LOOP)->regs)
79 /* Get a pointer to the loop induction variables structure. */
80 #define LOOP_IVS(LOOP) (&LOOP_INFO (LOOP)->ivs)
82 /* Get the luid of an insn. Catch the error of trying to reference the LUID
83 of an insn added during loop, since these don't have LUIDs. */
85 #define INSN_LUID(INSN) \
86 (INSN_UID (INSN) < max_uid_for_loop ? uid_luid[INSN_UID (INSN)] \
89 #define REGNO_FIRST_LUID(REGNO) \
90 (REGNO_FIRST_UID (REGNO) < max_uid_for_loop \
91 ? uid_luid[REGNO_FIRST_UID (REGNO)] \
93 #define REGNO_LAST_LUID(REGNO) \
94 (REGNO_LAST_UID (REGNO) < max_uid_for_loop \
95 ? uid_luid[REGNO_LAST_UID (REGNO)] \
98 /* A "basic induction variable" or biv is a pseudo reg that is set
99 (within this loop) only by incrementing or decrementing it. */
100 /* A "general induction variable" or giv is a pseudo reg whose
101 value is a linear function of a biv. */
103 /* Bivs are recognized by `basic_induction_var';
104 Givs by `general_induction_var'. */
106 /* An enum for the two different types of givs, those that are used
107 as memory addresses and those that are calculated into registers. */
115 /* A `struct induction' is created for every instruction that sets
116 an induction variable (either a biv or a giv). */
120 rtx insn
; /* The insn that sets a biv or giv */
121 rtx new_reg
; /* New register, containing strength reduced
122 version of this giv. */
123 rtx src_reg
; /* Biv from which this giv is computed.
124 (If this is a biv, then this is the biv.) */
125 enum g_types giv_type
; /* Indicate whether DEST_ADDR or DEST_REG */
126 rtx dest_reg
; /* Destination register for insn: this is the
127 register which was the biv or giv.
128 For a biv, this equals src_reg.
129 For a DEST_ADDR type giv, this is 0. */
130 rtx
*location
; /* Place in the insn where this giv occurs.
131 If GIV_TYPE is DEST_REG, this is 0. */
132 /* For a biv, this is the place where add_val
134 enum machine_mode mode
; /* The mode of this biv or giv */
135 rtx mem
; /* For DEST_ADDR, the memory object. */
136 rtx mult_val
; /* Multiplicative factor for src_reg. */
137 rtx add_val
; /* Additive constant for that product. */
138 int benefit
; /* Gain from eliminating this insn. */
139 rtx final_value
; /* If the giv is used outside the loop, and its
140 final value could be calculated, it is put
141 here, and the giv is made replaceable. Set
142 the giv to this value before the loop. */
143 unsigned combined_with
; /* The number of givs this giv has been
144 combined with. If nonzero, this giv
145 cannot combine with any other giv. */
146 unsigned replaceable
: 1; /* 1 if we can substitute the strength-reduced
147 variable for the original variable.
148 0 means they must be kept separate and the
149 new one must be copied into the old pseudo
150 reg each time the old one is set. */
151 unsigned not_replaceable
: 1; /* Used to prevent duplicating work. This is
152 1 if we know that the giv definitely can
153 not be made replaceable, in which case we
154 don't bother checking the variable again
155 even if further info is available.
156 Both this and the above can be zero. */
157 unsigned ignore
: 1; /* 1 prohibits further processing of giv */
158 unsigned always_computable
: 1;/* 1 if this value is computable every
160 unsigned always_executed
: 1; /* 1 if this set occurs each iteration. */
161 unsigned maybe_multiple
: 1; /* Only used for a biv and 1 if this biv
162 update may be done multiple times per
164 unsigned cant_derive
: 1; /* For giv's, 1 if this giv cannot derive
165 another giv. This occurs in many cases
166 where a giv's lifetime spans an update to
168 unsigned maybe_dead
: 1; /* 1 if this giv might be dead. In that case,
169 we won't use it to eliminate a biv, it
170 would probably lose. */
171 unsigned auto_inc_opt
: 1; /* 1 if this giv had its increment output next
172 to it to try to form an auto-inc address. */
174 unsigned no_const_addval
: 1; /* 1 if add_val does not contain a const. */
175 int lifetime
; /* Length of life of this giv */
176 rtx derive_adjustment
; /* If nonzero, is an adjustment to be
177 subtracted from add_val when this giv
178 derives another. This occurs when the
179 giv spans a biv update by incrementation. */
180 rtx ext_dependent
; /* If nonzero, is a sign or zero extension
181 if a biv on which this giv is dependent. */
182 struct induction
*next_iv
; /* For givs, links together all givs that are
183 based on the same biv. For bivs, links
184 together all biv entries that refer to the
185 same biv register. */
186 struct induction
*same
; /* For givs, if the giv has been combined with
187 another giv, this points to the base giv.
188 The base giv will have COMBINED_WITH nonzero.
189 For bivs, if the biv has the same LOCATION
190 than another biv, this points to the base
192 struct induction
*same_insn
; /* If there are multiple identical givs in
193 the same insn, then all but one have this
194 field set, and they all point to the giv
195 that doesn't have this field set. */
196 rtx last_use
; /* For a giv made from a biv increment, this is
197 a substitute for the lifetime information. */
201 /* A `struct iv_class' is created for each biv. */
205 unsigned int regno
; /* Pseudo reg which is the biv. */
206 int biv_count
; /* Number of insns setting this reg. */
207 struct induction
*biv
; /* List of all insns that set this reg. */
208 int giv_count
; /* Number of DEST_REG givs computed from this
209 biv. The resulting count is only used in
211 struct induction
*giv
; /* List of all insns that compute a giv
213 int total_benefit
; /* Sum of BENEFITs of all those givs. */
214 rtx initial_value
; /* Value of reg at loop start. */
215 rtx initial_test
; /* Test performed on BIV before loop. */
216 rtx final_value
; /* Value of reg at loop end, if known. */
217 struct iv_class
*next
; /* Links all class structures together. */
218 rtx init_insn
; /* insn which initializes biv, 0 if none. */
219 rtx init_set
; /* SET of INIT_INSN, if any. */
220 unsigned incremented
: 1; /* 1 if somewhere incremented/decremented */
221 unsigned eliminable
: 1; /* 1 if plausible candidate for
223 unsigned nonneg
: 1; /* 1 if we added a REG_NONNEG note for
225 unsigned reversed
: 1; /* 1 if we reversed the loop that this
227 unsigned all_reduced
: 1; /* 1 if all givs using this biv have
232 /* Definitions used by the basic induction variable discovery code. */
242 /* A `struct iv' is created for every register. */
249 struct iv_class
*class;
250 struct induction
*info
;
255 #define REG_IV_TYPE(ivs, n) ivs->regs[n].type
256 #define REG_IV_INFO(ivs, n) ivs->regs[n].iv.info
257 #define REG_IV_CLASS(ivs, n) ivs->regs[n].iv.class
262 /* Indexed by register number, contains pointer to `struct
263 iv' if register is an induction variable. */
266 /* Size of regs array. */
269 /* The head of a list which links together (via the next field)
270 every iv class for the current loop. */
271 struct iv_class
*list
;
275 typedef struct loop_mem_info
277 rtx mem
; /* The MEM itself. */
278 rtx reg
; /* Corresponding pseudo, if any. */
279 int optimize
; /* Nonzero if we can optimize access to this MEM. */
286 /* Number of times the reg is set during the loop being scanned.
287 During code motion, a negative value indicates a reg that has
288 been made a candidate; in particular -2 means that it is an
289 candidate that we know is equal to a constant and -1 means that
290 it is a candidate not known equal to a constant. After code
291 motion, regs moved have 0 (which is accurate now) while the
292 failed candidates have the original number of times set.
294 Therefore, at all times, == 0 indicates an invariant register;
295 < 0 a conditionally invariant one. */
298 /* Original value of set_in_loop; same except that this value
299 is not set negative for a reg whose sets have been made candidates
300 and not set to 0 for a reg that is moved. */
303 /* Contains the insn in which a register was used if it was used
304 exactly once; contains const0_rtx if it was used more than once. */
307 /* Nonzero indicates that the register cannot be moved or strength
309 char may_not_optimize
;
311 /* Nonzero means reg N has already been moved out of one loop.
312 This reduces the desire to move it out of another. */
319 int num
; /* Number of regs used in table. */
320 int size
; /* Size of table. */
321 struct loop_reg
*array
; /* Register usage info. array. */
322 int multiple_uses
; /* Nonzero if a reg has multiple uses. */
329 /* Head of movable chain. */
330 struct movable
*head
;
331 /* Last movable in chain. */
332 struct movable
*last
;
336 /* Information pertaining to a loop. */
340 /* Nonzero if there is a subroutine call in the current loop. */
342 /* Nonzero if there is a libcall in the current loop. */
344 /* Nonzero if there is a non constant call in the current loop. */
345 int has_nonconst_call
;
346 /* Nonzero if there is a prefetch instruction in the current loop. */
348 /* Nonzero if there is a volatile memory reference in the current
351 /* Nonzero if there is a tablejump in the current loop. */
353 /* Nonzero if there are ways to leave the loop other than falling
355 int has_multiple_exit_targets
;
356 /* Nonzero if there is an indirect jump in the current function. */
357 int has_indirect_jump
;
358 /* Register or constant initial loop value. */
360 /* Register or constant value used for comparison test. */
361 rtx comparison_value
;
362 /* Register or constant approximate final value. */
364 /* Register or constant initial loop value with term common to
365 final_value removed. */
366 rtx initial_equiv_value
;
367 /* Register or constant final loop value with term common to
368 initial_value removed. */
369 rtx final_equiv_value
;
370 /* Register corresponding to iteration variable. */
372 /* Constant loop increment. */
374 enum rtx_code comparison_code
;
375 /* Holds the number of loop iterations. It is zero if the number
376 could not be calculated. Must be unsigned since the number of
377 iterations can be as high as 2^wordsize - 1. For loops with a
378 wider iterator, this number will be zero if the number of loop
379 iterations is too large for an unsigned integer to hold. */
380 unsigned HOST_WIDE_INT n_iterations
;
381 int used_count_register
;
382 /* The loop iterator induction variable. */
384 /* List of MEMs that are stored in this loop. */
386 /* Array of MEMs that are used (read or written) in this loop, but
387 cannot be aliased by anything in this loop, except perhaps
388 themselves. In other words, if mems[i] is altered during
389 the loop, it is altered by an expression that is rtx_equal_p to
392 /* The index of the next available slot in MEMS. */
394 /* The number of elements allocated in MEMS. */
396 /* Nonzero if we don't know what MEMs were changed in the current
397 loop. This happens if the loop contains a call (in which case
398 `has_call' will also be set) or if we store into more than
400 int unknown_address_altered
;
401 /* The above doesn't count any readonly memory locations that are
402 stored. This does. */
403 int unknown_constant_address_altered
;
404 /* Count of memory write instructions discovered in the loop. */
406 /* The insn where the first of these was found. */
407 rtx first_loop_store_insn
;
408 /* The chain of movable insns in loop. */
409 struct loop_movables movables
;
410 /* The registers used the in loop. */
411 struct loop_regs regs
;
412 /* The induction variable information in loop. */
414 /* Nonzero if call is in pre_header extended basic block. */
415 int pre_header_has_call
;
418 /* Not really meaningful values, but at least something. */
419 #ifndef SIMULTANEOUS_PREFETCHES
420 #define SIMULTANEOUS_PREFETCHES 3
422 #ifndef PREFETCH_BLOCK
423 #define PREFETCH_BLOCK 32
425 #ifndef HAVE_prefetch
426 #define HAVE_prefetch 0
427 #define CODE_FOR_prefetch 0
428 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
431 /* Give up the prefetch optimizations once we exceed a given threshold.
432 It is unlikely that we would be able to optimize something in a loop
433 with so many detected prefetches. */
434 #define MAX_PREFETCHES 100
435 /* The number of prefetch blocks that are beneficial to fetch at once before
436 a loop with a known (and low) iteration count. */
437 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
438 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
439 since it is likely that the data are already in the cache. */
440 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
442 /* Parameterize some prefetch heuristics so they can be turned on and off
443 easily for performance testing on new architectures. These can be
444 defined in target-dependent files. */
446 /* Prefetch is worthwhile only when loads/stores are dense. */
447 #ifndef PREFETCH_ONLY_DENSE_MEM
448 #define PREFETCH_ONLY_DENSE_MEM 1
451 /* Define what we mean by "dense" loads and stores; This value divided by 256
452 is the minimum percentage of memory references that worth prefetching. */
453 #ifndef PREFETCH_DENSE_MEM
454 #define PREFETCH_DENSE_MEM 220
457 /* Do not prefetch for a loop whose iteration count is known to be low. */
458 #ifndef PREFETCH_NO_LOW_LOOPCNT
459 #define PREFETCH_NO_LOW_LOOPCNT 1
462 /* Define what we mean by a "low" iteration count. */
463 #ifndef PREFETCH_LOW_LOOPCNT
464 #define PREFETCH_LOW_LOOPCNT 32
467 /* Do not prefetch for a loop that contains a function call; such a loop is
468 probably not an internal loop. */
469 #ifndef PREFETCH_NO_CALL
470 #define PREFETCH_NO_CALL 1
473 /* Do not prefetch accesses with an extreme stride. */
474 #ifndef PREFETCH_NO_EXTREME_STRIDE
475 #define PREFETCH_NO_EXTREME_STRIDE 1
478 /* Define what we mean by an "extreme" stride. */
479 #ifndef PREFETCH_EXTREME_STRIDE
480 #define PREFETCH_EXTREME_STRIDE 4096
483 /* Define a limit to how far apart indices can be and still be merged
484 into a single prefetch. */
485 #ifndef PREFETCH_EXTREME_DIFFERENCE
486 #define PREFETCH_EXTREME_DIFFERENCE 4096
489 /* Issue prefetch instructions before the loop to fetch data to be used
490 in the first few loop iterations. */
491 #ifndef PREFETCH_BEFORE_LOOP
492 #define PREFETCH_BEFORE_LOOP 1
495 /* Do not handle reversed order prefetches (negative stride). */
496 #ifndef PREFETCH_NO_REVERSE_ORDER
497 #define PREFETCH_NO_REVERSE_ORDER 1
500 /* Prefetch even if the GIV is in conditional code. */
501 #ifndef PREFETCH_CONDITIONAL
502 #define PREFETCH_CONDITIONAL 1
505 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
506 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
508 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
509 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
510 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
512 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
513 ((REGNO) < FIRST_PSEUDO_REGISTER \
514 ? (int) hard_regno_nregs[(REGNO)][GET_MODE (SET_DEST)] : 1)
517 /* Vector mapping INSN_UIDs to luids.
518 The luids are like uids but increase monotonically always.
519 We use them to see whether a jump comes from outside a given loop. */
521 static int *uid_luid
;
523 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
524 number the insn is contained in. */
526 static struct loop
**uid_loop
;
528 /* 1 + largest uid of any insn. */
530 static int max_uid_for_loop
;
532 /* Number of loops detected in current function. Used as index to the
535 static int max_loop_num
;
537 /* Bound on pseudo register number before loop optimization.
538 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
539 static unsigned int max_reg_before_loop
;
541 /* The value to pass to the next call of reg_scan_update. */
542 static int loop_max_reg
;
544 /* During the analysis of a loop, a chain of `struct movable's
545 is made to record all the movable insns found.
546 Then the entire chain can be scanned to decide which to move. */
550 rtx insn
; /* A movable insn */
551 rtx set_src
; /* The expression this reg is set from. */
552 rtx set_dest
; /* The destination of this SET. */
553 rtx dependencies
; /* When INSN is libcall, this is an EXPR_LIST
554 of any registers used within the LIBCALL. */
555 int consec
; /* Number of consecutive following insns
556 that must be moved with this one. */
557 unsigned int regno
; /* The register it sets */
558 short lifetime
; /* lifetime of that register;
559 may be adjusted when matching movables
560 that load the same value are found. */
561 short savings
; /* Number of insns we can move for this reg,
562 including other movables that force this
563 or match this one. */
564 ENUM_BITFIELD(machine_mode
) savemode
: 8; /* Nonzero means it is a mode for
565 a low part that we should avoid changing when
566 clearing the rest of the reg. */
567 unsigned int cond
: 1; /* 1 if only conditionally movable */
568 unsigned int force
: 1; /* 1 means MUST move this insn */
569 unsigned int global
: 1; /* 1 means reg is live outside this loop */
570 /* If PARTIAL is 1, GLOBAL means something different:
571 that the reg is live outside the range from where it is set
572 to the following label. */
573 unsigned int done
: 1; /* 1 inhibits further processing of this */
575 unsigned int partial
: 1; /* 1 means this reg is used for zero-extending.
576 In particular, moving it does not make it
578 unsigned int move_insn
: 1; /* 1 means that we call emit_move_insn to
579 load SRC, rather than copying INSN. */
580 unsigned int move_insn_first
:1;/* Same as above, if this is necessary for the
581 first insn of a consecutive sets group. */
582 unsigned int is_equiv
: 1; /* 1 means a REG_EQUIV is present on INSN. */
583 unsigned int insert_temp
: 1; /* 1 means we copy to a new pseudo and replace
584 the original insn with a copy from that
585 pseudo, rather than deleting it. */
586 struct movable
*match
; /* First entry for same value */
587 struct movable
*forces
; /* An insn that must be moved if this is */
588 struct movable
*next
;
592 static FILE *loop_dump_stream
;
594 /* Forward declarations. */
596 static void invalidate_loops_containing_label (rtx
);
597 static void find_and_verify_loops (rtx
, struct loops
*);
598 static void mark_loop_jump (rtx
, struct loop
*);
599 static void prescan_loop (struct loop
*);
600 static int reg_in_basic_block_p (rtx
, rtx
);
601 static int consec_sets_invariant_p (const struct loop
*, rtx
, int, rtx
);
602 static int labels_in_range_p (rtx
, int);
603 static void count_one_set (struct loop_regs
*, rtx
, rtx
, rtx
*);
604 static void note_addr_stored (rtx
, rtx
, void *);
605 static void note_set_pseudo_multiple_uses (rtx
, rtx
, void *);
606 static int loop_reg_used_before_p (const struct loop
*, rtx
, rtx
);
607 static rtx
find_regs_nested (rtx
, rtx
);
608 static void scan_loop (struct loop
*, int);
610 static void replace_call_address (rtx
, rtx
, rtx
);
612 static rtx
skip_consec_insns (rtx
, int);
613 static int libcall_benefit (rtx
);
614 static rtx
libcall_other_reg (rtx
, rtx
);
615 static void record_excess_regs (rtx
, rtx
, rtx
*);
616 static void ignore_some_movables (struct loop_movables
*);
617 static void force_movables (struct loop_movables
*);
618 static void combine_movables (struct loop_movables
*, struct loop_regs
*);
619 static int num_unmoved_movables (const struct loop
*);
620 static int regs_match_p (rtx
, rtx
, struct loop_movables
*);
621 static int rtx_equal_for_loop_p (rtx
, rtx
, struct loop_movables
*,
623 static void add_label_notes (rtx
, rtx
);
624 static void move_movables (struct loop
*loop
, struct loop_movables
*, int,
626 static void loop_movables_add (struct loop_movables
*, struct movable
*);
627 static void loop_movables_free (struct loop_movables
*);
628 static int count_nonfixed_reads (const struct loop
*, rtx
);
629 static void loop_bivs_find (struct loop
*);
630 static void loop_bivs_init_find (struct loop
*);
631 static void loop_bivs_check (struct loop
*);
632 static void loop_givs_find (struct loop
*);
633 static void loop_givs_check (struct loop
*);
634 static int loop_biv_eliminable_p (struct loop
*, struct iv_class
*, int, int);
635 static int loop_giv_reduce_benefit (struct loop
*, struct iv_class
*,
636 struct induction
*, rtx
);
637 static void loop_givs_dead_check (struct loop
*, struct iv_class
*);
638 static void loop_givs_reduce (struct loop
*, struct iv_class
*);
639 static void loop_givs_rescan (struct loop
*, struct iv_class
*, rtx
*);
640 static void loop_ivs_free (struct loop
*);
641 static void strength_reduce (struct loop
*, int);
642 static void find_single_use_in_loop (struct loop_regs
*, rtx
, rtx
);
643 static int valid_initial_value_p (rtx
, rtx
, int, rtx
);
644 static void find_mem_givs (const struct loop
*, rtx
, rtx
, int, int);
645 static void record_biv (struct loop
*, struct induction
*, rtx
, rtx
, rtx
,
646 rtx
, rtx
*, int, int);
647 static void check_final_value (const struct loop
*, struct induction
*);
648 static void loop_ivs_dump (const struct loop
*, FILE *, int);
649 static void loop_iv_class_dump (const struct iv_class
*, FILE *, int);
650 static void loop_biv_dump (const struct induction
*, FILE *, int);
651 static void loop_giv_dump (const struct induction
*, FILE *, int);
652 static void record_giv (const struct loop
*, struct induction
*, rtx
, rtx
,
653 rtx
, rtx
, rtx
, rtx
, int, enum g_types
, int, int,
655 static void update_giv_derive (const struct loop
*, rtx
);
656 static HOST_WIDE_INT
get_monotonic_increment (struct iv_class
*);
657 static bool biased_biv_fits_mode_p (const struct loop
*, struct iv_class
*,
658 HOST_WIDE_INT
, enum machine_mode
,
659 unsigned HOST_WIDE_INT
);
660 static bool biv_fits_mode_p (const struct loop
*, struct iv_class
*,
661 HOST_WIDE_INT
, enum machine_mode
, bool);
662 static bool extension_within_bounds_p (const struct loop
*, struct iv_class
*,
664 static void check_ext_dependent_givs (const struct loop
*, struct iv_class
*);
665 static int basic_induction_var (const struct loop
*, rtx
, enum machine_mode
,
666 rtx
, rtx
, rtx
*, rtx
*, rtx
**);
667 static rtx
simplify_giv_expr (const struct loop
*, rtx
, rtx
*, int *);
668 static int general_induction_var (const struct loop
*loop
, rtx
, rtx
*, rtx
*,
669 rtx
*, rtx
*, int, int *, enum machine_mode
);
670 static int consec_sets_giv (const struct loop
*, int, rtx
, rtx
, rtx
, rtx
*,
671 rtx
*, rtx
*, rtx
*);
672 static int check_dbra_loop (struct loop
*, int);
673 static rtx
express_from_1 (rtx
, rtx
, rtx
);
674 static rtx
combine_givs_p (struct induction
*, struct induction
*);
675 static int cmp_combine_givs_stats (const void *, const void *);
676 static void combine_givs (struct loop_regs
*, struct iv_class
*);
677 static int product_cheap_p (rtx
, rtx
);
678 static int maybe_eliminate_biv (const struct loop
*, struct iv_class
*, int,
680 static int maybe_eliminate_biv_1 (const struct loop
*, rtx
, rtx
,
681 struct iv_class
*, int, basic_block
, rtx
);
682 static int last_use_this_basic_block (rtx
, rtx
);
683 static void record_initial (rtx
, rtx
, void *);
684 static void update_reg_last_use (rtx
, rtx
);
685 static rtx
next_insn_in_loop (const struct loop
*, rtx
);
686 static void loop_regs_scan (const struct loop
*, int);
687 static int count_insns_in_loop (const struct loop
*);
688 static int find_mem_in_note_1 (rtx
*, void *);
689 static rtx
find_mem_in_note (rtx
);
690 static void load_mems (const struct loop
*);
691 static int insert_loop_mem (rtx
*, void *);
692 static int replace_loop_mem (rtx
*, void *);
693 static void replace_loop_mems (rtx
, rtx
, rtx
, int);
694 static int replace_loop_reg (rtx
*, void *);
695 static void replace_loop_regs (rtx insn
, rtx
, rtx
);
696 static void note_reg_stored (rtx
, rtx
, void *);
697 static void try_copy_prop (const struct loop
*, rtx
, unsigned int);
698 static void try_swap_copy_prop (const struct loop
*, rtx
, unsigned int);
699 static rtx
check_insn_for_givs (struct loop
*, rtx
, int, int);
700 static rtx
check_insn_for_bivs (struct loop
*, rtx
, int, int);
701 static rtx
gen_add_mult (rtx
, rtx
, rtx
, rtx
);
702 static void loop_regs_update (const struct loop
*, rtx
);
703 static int iv_add_mult_cost (rtx
, rtx
, rtx
, rtx
);
704 static int loop_invariant_p (const struct loop
*, rtx
);
705 static rtx
loop_insn_hoist (const struct loop
*, rtx
);
706 static void loop_iv_add_mult_emit_before (const struct loop
*, rtx
, rtx
, rtx
,
707 rtx
, basic_block
, rtx
);
708 static rtx
loop_insn_emit_before (const struct loop
*, basic_block
,
710 static int loop_insn_first_p (rtx
, rtx
);
711 static rtx
get_condition_for_loop (const struct loop
*, rtx
);
712 static void loop_iv_add_mult_sink (const struct loop
*, rtx
, rtx
, rtx
, rtx
);
713 static void loop_iv_add_mult_hoist (const struct loop
*, rtx
, rtx
, rtx
, rtx
);
714 static rtx
extend_value_for_giv (struct induction
*, rtx
);
715 static rtx
loop_insn_sink (const struct loop
*, rtx
);
717 static rtx
loop_insn_emit_after (const struct loop
*, basic_block
, rtx
, rtx
);
718 static rtx
loop_call_insn_emit_before (const struct loop
*, basic_block
,
720 static rtx
loop_call_insn_hoist (const struct loop
*, rtx
);
721 static rtx
loop_insn_sink_or_swim (const struct loop
*, rtx
);
723 static void loop_dump_aux (const struct loop
*, FILE *, int);
724 static void loop_delete_insns (rtx
, rtx
);
725 static HOST_WIDE_INT
remove_constant_addition (rtx
*);
726 static rtx
gen_load_of_final_value (rtx
, rtx
);
727 void debug_ivs (const struct loop
*);
728 void debug_iv_class (const struct iv_class
*);
729 void debug_biv (const struct induction
*);
730 void debug_giv (const struct induction
*);
731 void debug_loop (const struct loop
*);
732 void debug_loops (const struct loops
*);
734 typedef struct loop_replace_args
741 /* Nonzero iff INSN is between START and END, inclusive. */
742 #define INSN_IN_RANGE_P(INSN, START, END) \
743 (INSN_UID (INSN) < max_uid_for_loop \
744 && INSN_LUID (INSN) >= INSN_LUID (START) \
745 && INSN_LUID (INSN) <= INSN_LUID (END))
747 /* Indirect_jump_in_function is computed once per function. */
748 static int indirect_jump_in_function
;
749 static int indirect_jump_in_function_p (rtx
);
751 static int compute_luids (rtx
, rtx
, int);
753 static int biv_elimination_giv_has_0_offset (struct induction
*,
754 struct induction
*, rtx
);
756 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
757 copy the value of the strength reduced giv to its original register. */
758 static int copy_cost
;
760 /* Cost of using a register, to normalize the benefits of a giv. */
761 static int reg_address_cost
;
766 rtx reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
768 reg_address_cost
= address_cost (reg
, SImode
);
770 copy_cost
= COSTS_N_INSNS (1);
773 /* Compute the mapping from uids to luids.
774 LUIDs are numbers assigned to insns, like uids,
775 except that luids increase monotonically through the code.
776 Start at insn START and stop just before END. Assign LUIDs
777 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
779 compute_luids (rtx start
, rtx end
, int prev_luid
)
784 for (insn
= start
, i
= prev_luid
; insn
!= end
; insn
= NEXT_INSN (insn
))
786 if (INSN_UID (insn
) >= max_uid_for_loop
)
788 /* Don't assign luids to line-number NOTEs, so that the distance in
789 luids between two insns is not affected by -g. */
791 || NOTE_LINE_NUMBER (insn
) <= 0)
792 uid_luid
[INSN_UID (insn
)] = ++i
;
794 /* Give a line number note the same luid as preceding insn. */
795 uid_luid
[INSN_UID (insn
)] = i
;
800 /* Entry point of this file. Perform loop optimization
801 on the current function. F is the first insn of the function
802 and DUMPFILE is a stream for output of a trace of actions taken
803 (or 0 if none should be output). */
806 loop_optimize (rtx f
, FILE *dumpfile
, int flags
)
810 struct loops loops_data
;
811 struct loops
*loops
= &loops_data
;
812 struct loop_info
*loops_info
;
814 loop_dump_stream
= dumpfile
;
816 init_recog_no_volatile ();
818 max_reg_before_loop
= max_reg_num ();
819 loop_max_reg
= max_reg_before_loop
;
823 /* Count the number of loops. */
826 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
829 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
833 /* Don't waste time if no loops. */
834 if (max_loop_num
== 0)
837 loops
->num
= max_loop_num
;
839 /* Get size to use for tables indexed by uids.
840 Leave some space for labels allocated by find_and_verify_loops. */
841 max_uid_for_loop
= get_max_uid () + 1 + max_loop_num
* 32;
843 uid_luid
= xcalloc (max_uid_for_loop
, sizeof (int));
844 uid_loop
= xcalloc (max_uid_for_loop
, sizeof (struct loop
*));
846 /* Allocate storage for array of loops. */
847 loops
->array
= xcalloc (loops
->num
, sizeof (struct loop
));
849 /* Find and process each loop.
850 First, find them, and record them in order of their beginnings. */
851 find_and_verify_loops (f
, loops
);
853 /* Allocate and initialize auxiliary loop information. */
854 loops_info
= xcalloc (loops
->num
, sizeof (struct loop_info
));
855 for (i
= 0; i
< (int) loops
->num
; i
++)
856 loops
->array
[i
].aux
= loops_info
+ i
;
858 /* Now find all register lifetimes. This must be done after
859 find_and_verify_loops, because it might reorder the insns in the
861 reg_scan (f
, max_reg_before_loop
);
863 /* This must occur after reg_scan so that registers created by gcse
864 will have entries in the register tables.
866 We could have added a call to reg_scan after gcse_main in toplev.c,
867 but moving this call to init_alias_analysis is more efficient. */
868 init_alias_analysis ();
870 /* See if we went too far. Note that get_max_uid already returns
871 one more that the maximum uid of all insn. */
872 if (get_max_uid () > max_uid_for_loop
)
874 /* Now reset it to the actual size we need. See above. */
875 max_uid_for_loop
= get_max_uid ();
877 /* find_and_verify_loops has already called compute_luids, but it
878 might have rearranged code afterwards, so we need to recompute
880 compute_luids (f
, NULL_RTX
, 0);
882 /* Don't leave gaps in uid_luid for insns that have been
883 deleted. It is possible that the first or last insn
884 using some register has been deleted by cross-jumping.
885 Make sure that uid_luid for that former insn's uid
886 points to the general area where that insn used to be. */
887 for (i
= 0; i
< max_uid_for_loop
; i
++)
889 uid_luid
[0] = uid_luid
[i
];
890 if (uid_luid
[0] != 0)
893 for (i
= 0; i
< max_uid_for_loop
; i
++)
894 if (uid_luid
[i
] == 0)
895 uid_luid
[i
] = uid_luid
[i
- 1];
897 /* Determine if the function has indirect jump. On some systems
898 this prevents low overhead loop instructions from being used. */
899 indirect_jump_in_function
= indirect_jump_in_function_p (f
);
901 /* Now scan the loops, last ones first, since this means inner ones are done
902 before outer ones. */
903 for (i
= max_loop_num
- 1; i
>= 0; i
--)
905 struct loop
*loop
= &loops
->array
[i
];
907 if (! loop
->invalid
&& loop
->end
)
909 scan_loop (loop
, flags
);
914 end_alias_analysis ();
917 for (i
= 0; i
< (int) loops
->num
; i
++)
918 free (loops_info
[i
].mems
);
926 /* Returns the next insn, in execution order, after INSN. START and
927 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
928 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
929 insn-stream; it is used with loops that are entered near the
933 next_insn_in_loop (const struct loop
*loop
, rtx insn
)
935 insn
= NEXT_INSN (insn
);
937 if (insn
== loop
->end
)
940 /* Go to the top of the loop, and continue there. */
947 if (insn
== loop
->scan_start
)
954 /* Find any register references hidden inside X and add them to
955 the dependency list DEPS. This is used to look inside CLOBBER (MEM
956 when checking whether a PARALLEL can be pulled out of a loop. */
959 find_regs_nested (rtx deps
, rtx x
)
961 enum rtx_code code
= GET_CODE (x
);
963 deps
= gen_rtx_EXPR_LIST (VOIDmode
, x
, deps
);
966 const char *fmt
= GET_RTX_FORMAT (code
);
968 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
971 deps
= find_regs_nested (deps
, XEXP (x
, i
));
972 else if (fmt
[i
] == 'E')
973 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
974 deps
= find_regs_nested (deps
, XVECEXP (x
, i
, j
));
980 /* Optimize one loop described by LOOP. */
982 /* ??? Could also move memory writes out of loops if the destination address
983 is invariant, the source is invariant, the memory write is not volatile,
984 and if we can prove that no read inside the loop can read this address
985 before the write occurs. If there is a read of this address after the
986 write, then we can also mark the memory read as invariant. */
989 scan_loop (struct loop
*loop
, int flags
)
991 struct loop_info
*loop_info
= LOOP_INFO (loop
);
992 struct loop_regs
*regs
= LOOP_REGS (loop
);
994 rtx loop_start
= loop
->start
;
995 rtx loop_end
= loop
->end
;
997 /* 1 if we are scanning insns that could be executed zero times. */
999 /* 1 if we are scanning insns that might never be executed
1000 due to a subroutine call which might exit before they are reached. */
1001 int call_passed
= 0;
1002 /* Number of insns in the loop. */
1005 rtx temp
, update_start
, update_end
;
1006 /* The SET from an insn, if it is the only SET in the insn. */
1008 /* Chain describing insns movable in current loop. */
1009 struct loop_movables
*movables
= LOOP_MOVABLES (loop
);
1010 /* Ratio of extra register life span we can justify
1011 for saving an instruction. More if loop doesn't call subroutines
1012 since in that case saving an insn makes more difference
1013 and more registers are available. */
1022 /* Determine whether this loop starts with a jump down to a test at
1023 the end. This will occur for a small number of loops with a test
1024 that is too complex to duplicate in front of the loop.
1026 We search for the first insn or label in the loop, skipping NOTEs.
1027 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
1028 (because we might have a loop executed only once that contains a
1029 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
1030 (in case we have a degenerate loop).
1032 Note that if we mistakenly think that a loop is entered at the top
1033 when, in fact, it is entered at the exit test, the only effect will be
1034 slightly poorer optimization. Making the opposite error can generate
1035 incorrect code. Since very few loops now start with a jump to the
1036 exit test, the code here to detect that case is very conservative. */
1038 for (p
= NEXT_INSN (loop_start
);
1040 && !LABEL_P (p
) && ! INSN_P (p
)
1042 || (NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_BEG
1043 && NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_END
));
1047 loop
->scan_start
= p
;
1049 /* If loop end is the end of the current function, then emit a
1050 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
1051 note insn. This is the position we use when sinking insns out of
1053 if (NEXT_INSN (loop
->end
) != 0)
1054 loop
->sink
= NEXT_INSN (loop
->end
);
1056 loop
->sink
= emit_note_after (NOTE_INSN_DELETED
, loop
->end
);
1058 /* Set up variables describing this loop. */
1059 prescan_loop (loop
);
1060 threshold
= (loop_info
->has_call
? 1 : 2) * (1 + n_non_fixed_regs
);
1062 /* If loop has a jump before the first label,
1063 the true entry is the target of that jump.
1064 Start scan from there.
1065 But record in LOOP->TOP the place where the end-test jumps
1066 back to so we can scan that after the end of the loop. */
1068 /* Loop entry must be unconditional jump (and not a RETURN) */
1069 && any_uncondjump_p (p
)
1070 && JUMP_LABEL (p
) != 0
1071 /* Check to see whether the jump actually
1072 jumps out of the loop (meaning it's no loop).
1073 This case can happen for things like
1074 do {..} while (0). If this label was generated previously
1075 by loop, we can't tell anything about it and have to reject
1077 && INSN_IN_RANGE_P (JUMP_LABEL (p
), loop_start
, loop_end
))
1079 loop
->top
= next_label (loop
->scan_start
);
1080 loop
->scan_start
= JUMP_LABEL (p
);
1083 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
1084 as required by loop_reg_used_before_p. So skip such loops. (This
1085 test may never be true, but it's best to play it safe.)
1087 Also, skip loops where we do not start scanning at a label. This
1088 test also rejects loops starting with a JUMP_INSN that failed the
1091 if (INSN_UID (loop
->scan_start
) >= max_uid_for_loop
1092 || !LABEL_P (loop
->scan_start
))
1094 if (loop_dump_stream
)
1095 fprintf (loop_dump_stream
, "\nLoop from %d to %d is phony.\n\n",
1096 INSN_UID (loop_start
), INSN_UID (loop_end
));
1100 /* Allocate extra space for REGs that might be created by load_mems.
1101 We allocate a little extra slop as well, in the hopes that we
1102 won't have to reallocate the regs array. */
1103 loop_regs_scan (loop
, loop_info
->mems_idx
+ 16);
1104 insn_count
= count_insns_in_loop (loop
);
1106 if (loop_dump_stream
)
1107 fprintf (loop_dump_stream
, "\nLoop from %d to %d: %d real insns.\n",
1108 INSN_UID (loop_start
), INSN_UID (loop_end
), insn_count
);
1110 /* Scan through the loop finding insns that are safe to move.
1111 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
1112 this reg will be considered invariant for subsequent insns.
1113 We consider whether subsequent insns use the reg
1114 in deciding whether it is worth actually moving.
1116 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
1117 and therefore it is possible that the insns we are scanning
1118 would never be executed. At such times, we must make sure
1119 that it is safe to execute the insn once instead of zero times.
1120 When MAYBE_NEVER is 0, all insns will be executed at least once
1121 so that is not a problem. */
1123 for (in_libcall
= 0, p
= next_insn_in_loop (loop
, loop
->scan_start
);
1125 p
= next_insn_in_loop (loop
, p
))
1127 if (in_libcall
&& INSN_P (p
) && find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
1129 if (NONJUMP_INSN_P (p
))
1131 /* Do not scan past an optimization barrier. */
1132 if (GET_CODE (PATTERN (p
)) == ASM_INPUT
)
1134 temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
1138 && (set
= single_set (p
))
1139 && REG_P (SET_DEST (set
))
1140 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
1141 && SET_DEST (set
) != pic_offset_table_rtx
1143 && ! regs
->array
[REGNO (SET_DEST (set
))].may_not_optimize
)
1148 int insert_temp
= 0;
1149 rtx src
= SET_SRC (set
);
1150 rtx dependencies
= 0;
1152 /* Figure out what to use as a source of this insn. If a
1153 REG_EQUIV note is given or if a REG_EQUAL note with a
1154 constant operand is specified, use it as the source and
1155 mark that we should move this insn by calling
1156 emit_move_insn rather that duplicating the insn.
1158 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
1160 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
1162 src
= XEXP (temp
, 0), move_insn
= 1;
1165 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
1166 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
1167 src
= XEXP (temp
, 0), move_insn
= 1;
1168 if (temp
&& find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
1170 src
= XEXP (temp
, 0);
1171 /* A libcall block can use regs that don't appear in
1172 the equivalent expression. To move the libcall,
1173 we must move those regs too. */
1174 dependencies
= libcall_other_reg (p
, src
);
1178 /* For parallels, add any possible uses to the dependencies, as
1179 we can't move the insn without resolving them first.
1180 MEMs inside CLOBBERs may also reference registers; these
1181 count as implicit uses. */
1182 if (GET_CODE (PATTERN (p
)) == PARALLEL
)
1184 for (i
= 0; i
< XVECLEN (PATTERN (p
), 0); i
++)
1186 rtx x
= XVECEXP (PATTERN (p
), 0, i
);
1187 if (GET_CODE (x
) == USE
)
1189 = gen_rtx_EXPR_LIST (VOIDmode
, XEXP (x
, 0),
1191 else if (GET_CODE (x
) == CLOBBER
1192 && MEM_P (XEXP (x
, 0)))
1193 dependencies
= find_regs_nested (dependencies
,
1194 XEXP (XEXP (x
, 0), 0));
1198 if (/* The register is used in basic blocks other
1199 than the one where it is set (meaning that
1200 something after this point in the loop might
1201 depend on its value before the set). */
1202 ! reg_in_basic_block_p (p
, SET_DEST (set
))
1203 /* And the set is not guaranteed to be executed once
1204 the loop starts, or the value before the set is
1205 needed before the set occurs...
1207 ??? Note we have quadratic behavior here, mitigated
1208 by the fact that the previous test will often fail for
1209 large loops. Rather than re-scanning the entire loop
1210 each time for register usage, we should build tables
1211 of the register usage and use them here instead. */
1213 || loop_reg_used_before_p (loop
, set
, p
)))
1214 /* It is unsafe to move the set. However, it may be OK to
1215 move the source into a new pseudo, and substitute a
1216 reg-to-reg copy for the original insn.
1218 This code used to consider it OK to move a set of a variable
1219 which was not created by the user and not used in an exit
1221 That behavior is incorrect and was removed. */
1224 /* Don't try to optimize a MODE_CC set with a constant
1225 source. It probably will be combined with a conditional
1227 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set
))) == MODE_CC
1228 && CONSTANT_P (src
))
1230 /* Don't try to optimize a register that was made
1231 by loop-optimization for an inner loop.
1232 We don't know its life-span, so we can't compute
1234 else if (REGNO (SET_DEST (set
)) >= max_reg_before_loop
)
1236 /* Don't move the source and add a reg-to-reg copy:
1237 - with -Os (this certainly increases size),
1238 - if the mode doesn't support copy operations (obviously),
1239 - if the source is already a reg (the motion will gain nothing),
1240 - if the source is a legitimate constant (likewise). */
1241 else if (insert_temp
1243 || ! can_copy_p (GET_MODE (SET_SRC (set
)))
1244 || REG_P (SET_SRC (set
))
1245 || (CONSTANT_P (SET_SRC (set
))
1246 && LEGITIMATE_CONSTANT_P (SET_SRC (set
)))))
1248 else if ((tem
= loop_invariant_p (loop
, src
))
1249 && (dependencies
== 0
1251 = loop_invariant_p (loop
, dependencies
)) != 0)
1252 && (regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
== 1
1254 = consec_sets_invariant_p
1255 (loop
, SET_DEST (set
),
1256 regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
,
1258 /* If the insn can cause a trap (such as divide by zero),
1259 can't move it unless it's guaranteed to be executed
1260 once loop is entered. Even a function call might
1261 prevent the trap insn from being reached
1262 (since it might exit!) */
1263 && ! ((maybe_never
|| call_passed
)
1264 && may_trap_p (src
)))
1267 int regno
= REGNO (SET_DEST (set
));
1269 /* A potential lossage is where we have a case where two insns
1270 can be combined as long as they are both in the loop, but
1271 we move one of them outside the loop. For large loops,
1272 this can lose. The most common case of this is the address
1273 of a function being called.
1275 Therefore, if this register is marked as being used
1276 exactly once if we are in a loop with calls
1277 (a "large loop"), see if we can replace the usage of
1278 this register with the source of this SET. If we can,
1281 Don't do this if P has a REG_RETVAL note or if we have
1282 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
1284 if (loop_info
->has_call
1285 && regs
->array
[regno
].single_usage
!= 0
1286 && regs
->array
[regno
].single_usage
!= const0_rtx
1287 && REGNO_FIRST_UID (regno
) == INSN_UID (p
)
1288 && (REGNO_LAST_UID (regno
)
1289 == INSN_UID (regs
->array
[regno
].single_usage
))
1290 && regs
->array
[regno
].set_in_loop
== 1
1291 && GET_CODE (SET_SRC (set
)) != ASM_OPERANDS
1292 && ! side_effects_p (SET_SRC (set
))
1293 && ! find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
1294 && (! SMALL_REGISTER_CLASSES
1295 || (! (REG_P (SET_SRC (set
))
1296 && (REGNO (SET_SRC (set
))
1297 < FIRST_PSEUDO_REGISTER
))))
1298 && regno
>= FIRST_PSEUDO_REGISTER
1299 /* This test is not redundant; SET_SRC (set) might be
1300 a call-clobbered register and the life of REGNO
1301 might span a call. */
1302 && ! modified_between_p (SET_SRC (set
), p
,
1303 regs
->array
[regno
].single_usage
)
1304 && no_labels_between_p (p
,
1305 regs
->array
[regno
].single_usage
)
1306 && validate_replace_rtx (SET_DEST (set
), SET_SRC (set
),
1307 regs
->array
[regno
].single_usage
))
1309 /* Replace any usage in a REG_EQUAL note. Must copy
1310 the new source, so that we don't get rtx sharing
1311 between the SET_SOURCE and REG_NOTES of insn p. */
1312 REG_NOTES (regs
->array
[regno
].single_usage
)
1314 (REG_NOTES (regs
->array
[regno
].single_usage
),
1315 SET_DEST (set
), copy_rtx (SET_SRC (set
))));
1318 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
));
1320 regs
->array
[regno
+i
].set_in_loop
= 0;
1324 m
= xmalloc (sizeof (struct movable
));
1328 m
->dependencies
= dependencies
;
1329 m
->set_dest
= SET_DEST (set
);
1332 = regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
- 1;
1336 m
->move_insn
= move_insn
;
1337 m
->move_insn_first
= 0;
1338 m
->insert_temp
= insert_temp
;
1339 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
1340 m
->savemode
= VOIDmode
;
1342 /* Set M->cond if either loop_invariant_p
1343 or consec_sets_invariant_p returned 2
1344 (only conditionally invariant). */
1345 m
->cond
= ((tem
| tem1
| tem2
) > 1);
1346 m
->global
= LOOP_REG_GLOBAL_P (loop
, regno
);
1348 m
->lifetime
= LOOP_REG_LIFETIME (loop
, regno
);
1349 m
->savings
= regs
->array
[regno
].n_times_set
;
1350 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
1351 m
->savings
+= libcall_benefit (p
);
1352 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
)); i
++)
1353 regs
->array
[regno
+i
].set_in_loop
= move_insn
? -2 : -1;
1354 /* Add M to the end of the chain MOVABLES. */
1355 loop_movables_add (movables
, m
);
1359 /* It is possible for the first instruction to have a
1360 REG_EQUAL note but a non-invariant SET_SRC, so we must
1361 remember the status of the first instruction in case
1362 the last instruction doesn't have a REG_EQUAL note. */
1363 m
->move_insn_first
= m
->move_insn
;
1365 /* Skip this insn, not checking REG_LIBCALL notes. */
1366 p
= next_nonnote_insn (p
);
1367 /* Skip the consecutive insns, if there are any. */
1368 p
= skip_consec_insns (p
, m
->consec
);
1369 /* Back up to the last insn of the consecutive group. */
1370 p
= prev_nonnote_insn (p
);
1372 /* We must now reset m->move_insn, m->is_equiv, and
1373 possibly m->set_src to correspond to the effects of
1375 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
1377 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
1380 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
1381 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
1382 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
1388 = (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
1391 /* If this register is always set within a STRICT_LOW_PART
1392 or set to zero, then its high bytes are constant.
1393 So clear them outside the loop and within the loop
1394 just load the low bytes.
1395 We must check that the machine has an instruction to do so.
1396 Also, if the value loaded into the register
1397 depends on the same register, this cannot be done. */
1398 else if (SET_SRC (set
) == const0_rtx
1399 && NONJUMP_INSN_P (NEXT_INSN (p
))
1400 && (set1
= single_set (NEXT_INSN (p
)))
1401 && GET_CODE (set1
) == SET
1402 && (GET_CODE (SET_DEST (set1
)) == STRICT_LOW_PART
)
1403 && (GET_CODE (XEXP (SET_DEST (set1
), 0)) == SUBREG
)
1404 && (SUBREG_REG (XEXP (SET_DEST (set1
), 0))
1406 && !reg_mentioned_p (SET_DEST (set
), SET_SRC (set1
)))
1408 int regno
= REGNO (SET_DEST (set
));
1409 if (regs
->array
[regno
].set_in_loop
== 2)
1412 m
= xmalloc (sizeof (struct movable
));
1415 m
->set_dest
= SET_DEST (set
);
1416 m
->dependencies
= 0;
1422 m
->move_insn_first
= 0;
1423 m
->insert_temp
= insert_temp
;
1425 /* If the insn may not be executed on some cycles,
1426 we can't clear the whole reg; clear just high part.
1427 Not even if the reg is used only within this loop.
1434 Clearing x before the inner loop could clobber a value
1435 being saved from the last time around the outer loop.
1436 However, if the reg is not used outside this loop
1437 and all uses of the register are in the same
1438 basic block as the store, there is no problem.
1440 If this insn was made by loop, we don't know its
1441 INSN_LUID and hence must make a conservative
1443 m
->global
= (INSN_UID (p
) >= max_uid_for_loop
1444 || LOOP_REG_GLOBAL_P (loop
, regno
)
1445 || (labels_in_range_p
1446 (p
, REGNO_FIRST_LUID (regno
))));
1447 if (maybe_never
&& m
->global
)
1448 m
->savemode
= GET_MODE (SET_SRC (set1
));
1450 m
->savemode
= VOIDmode
;
1454 m
->lifetime
= LOOP_REG_LIFETIME (loop
, regno
);
1457 i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
));
1459 regs
->array
[regno
+i
].set_in_loop
= -1;
1460 /* Add M to the end of the chain MOVABLES. */
1461 loop_movables_add (movables
, m
);
1466 /* Past a call insn, we get to insns which might not be executed
1467 because the call might exit. This matters for insns that trap.
1468 Constant and pure call insns always return, so they don't count. */
1469 else if (CALL_P (p
) && ! CONST_OR_PURE_CALL_P (p
))
1471 /* Past a label or a jump, we get to insns for which we
1472 can't count on whether or how many times they will be
1473 executed during each iteration. Therefore, we can
1474 only move out sets of trivial variables
1475 (those not used after the loop). */
1476 /* Similar code appears twice in strength_reduce. */
1477 else if ((LABEL_P (p
) || JUMP_P (p
))
1478 /* If we enter the loop in the middle, and scan around to the
1479 beginning, don't set maybe_never for that. This must be an
1480 unconditional jump, otherwise the code at the top of the
1481 loop might never be executed. Unconditional jumps are
1482 followed by a barrier then the loop_end. */
1483 && ! (JUMP_P (p
) && JUMP_LABEL (p
) == loop
->top
1484 && NEXT_INSN (NEXT_INSN (p
)) == loop_end
1485 && any_uncondjump_p (p
)))
1489 /* If one movable subsumes another, ignore that other. */
1491 ignore_some_movables (movables
);
1493 /* For each movable insn, see if the reg that it loads
1494 leads when it dies right into another conditionally movable insn.
1495 If so, record that the second insn "forces" the first one,
1496 since the second can be moved only if the first is. */
1498 force_movables (movables
);
1500 /* See if there are multiple movable insns that load the same value.
1501 If there are, make all but the first point at the first one
1502 through the `match' field, and add the priorities of them
1503 all together as the priority of the first. */
1505 combine_movables (movables
, regs
);
1507 /* Now consider each movable insn to decide whether it is worth moving.
1508 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1510 For machines with few registers this increases code size, so do not
1511 move moveables when optimizing for code size on such machines.
1512 (The 18 below is the value for i386.) */
1515 || (reg_class_size
[GENERAL_REGS
] > 18 && !loop_info
->has_call
))
1517 move_movables (loop
, movables
, threshold
, insn_count
);
1519 /* Recalculate regs->array if move_movables has created new
1521 if (max_reg_num () > regs
->num
)
1523 loop_regs_scan (loop
, 0);
1524 for (update_start
= loop_start
;
1525 PREV_INSN (update_start
)
1526 && !LABEL_P (PREV_INSN (update_start
));
1527 update_start
= PREV_INSN (update_start
))
1529 update_end
= NEXT_INSN (loop_end
);
1531 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1532 loop_max_reg
= max_reg_num ();
1536 /* Now candidates that still are negative are those not moved.
1537 Change regs->array[I].set_in_loop to indicate that those are not actually
1539 for (i
= 0; i
< regs
->num
; i
++)
1540 if (regs
->array
[i
].set_in_loop
< 0)
1541 regs
->array
[i
].set_in_loop
= regs
->array
[i
].n_times_set
;
1543 /* Now that we've moved some things out of the loop, we might be able to
1544 hoist even more memory references. */
1547 /* Recalculate regs->array if load_mems has created new registers. */
1548 if (max_reg_num () > regs
->num
)
1549 loop_regs_scan (loop
, 0);
1551 for (update_start
= loop_start
;
1552 PREV_INSN (update_start
)
1553 && !LABEL_P (PREV_INSN (update_start
));
1554 update_start
= PREV_INSN (update_start
))
1556 update_end
= NEXT_INSN (loop_end
);
1558 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1559 loop_max_reg
= max_reg_num ();
1561 if (flag_strength_reduce
)
1563 if (update_end
&& LABEL_P (update_end
))
1564 /* Ensure our label doesn't go away. */
1565 LABEL_NUSES (update_end
)++;
1567 strength_reduce (loop
, flags
);
1569 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1570 loop_max_reg
= max_reg_num ();
1572 if (update_end
&& LABEL_P (update_end
)
1573 && --LABEL_NUSES (update_end
) == 0)
1574 delete_related_insns (update_end
);
1578 /* The movable information is required for strength reduction. */
1579 loop_movables_free (movables
);
1586 /* Add elements to *OUTPUT to record all the pseudo-regs
1587 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1590 record_excess_regs (rtx in_this
, rtx not_in_this
, rtx
*output
)
1596 code
= GET_CODE (in_this
);
1610 if (REGNO (in_this
) >= FIRST_PSEUDO_REGISTER
1611 && ! reg_mentioned_p (in_this
, not_in_this
))
1612 *output
= gen_rtx_EXPR_LIST (VOIDmode
, in_this
, *output
);
1619 fmt
= GET_RTX_FORMAT (code
);
1620 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1627 for (j
= 0; j
< XVECLEN (in_this
, i
); j
++)
1628 record_excess_regs (XVECEXP (in_this
, i
, j
), not_in_this
, output
);
1632 record_excess_regs (XEXP (in_this
, i
), not_in_this
, output
);
1638 /* Check what regs are referred to in the libcall block ending with INSN,
1639 aside from those mentioned in the equivalent value.
1640 If there are none, return 0.
1641 If there are one or more, return an EXPR_LIST containing all of them. */
1644 libcall_other_reg (rtx insn
, rtx equiv
)
1646 rtx note
= find_reg_note (insn
, REG_RETVAL
, NULL_RTX
);
1647 rtx p
= XEXP (note
, 0);
1650 /* First, find all the regs used in the libcall block
1651 that are not mentioned as inputs to the result. */
1656 record_excess_regs (PATTERN (p
), equiv
, &output
);
1663 /* Return 1 if all uses of REG
1664 are between INSN and the end of the basic block. */
1667 reg_in_basic_block_p (rtx insn
, rtx reg
)
1669 int regno
= REGNO (reg
);
1672 if (REGNO_FIRST_UID (regno
) != INSN_UID (insn
))
1675 /* Search this basic block for the already recorded last use of the reg. */
1676 for (p
= insn
; p
; p
= NEXT_INSN (p
))
1678 switch (GET_CODE (p
))
1685 /* Ordinary insn: if this is the last use, we win. */
1686 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1691 /* Jump insn: if this is the last use, we win. */
1692 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1694 /* Otherwise, it's the end of the basic block, so we lose. */
1699 /* It's the end of the basic block, so we lose. */
1707 /* The "last use" that was recorded can't be found after the first
1708 use. This can happen when the last use was deleted while
1709 processing an inner loop, this inner loop was then completely
1710 unrolled, and the outer loop is always exited after the inner loop,
1711 so that everything after the first use becomes a single basic block. */
1715 /* Compute the benefit of eliminating the insns in the block whose
1716 last insn is LAST. This may be a group of insns used to compute a
1717 value directly or can contain a library call. */
1720 libcall_benefit (rtx last
)
1725 for (insn
= XEXP (find_reg_note (last
, REG_RETVAL
, NULL_RTX
), 0);
1726 insn
!= last
; insn
= NEXT_INSN (insn
))
1729 benefit
+= 10; /* Assume at least this many insns in a library
1731 else if (NONJUMP_INSN_P (insn
)
1732 && GET_CODE (PATTERN (insn
)) != USE
1733 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
1740 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1743 skip_consec_insns (rtx insn
, int count
)
1745 for (; count
> 0; count
--)
1749 /* If first insn of libcall sequence, skip to end. */
1750 /* Do this at start of loop, since INSN is guaranteed to
1753 && (temp
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)))
1754 insn
= XEXP (temp
, 0);
1757 insn
= NEXT_INSN (insn
);
1758 while (NOTE_P (insn
));
1764 /* Ignore any movable whose insn falls within a libcall
1765 which is part of another movable.
1766 We make use of the fact that the movable for the libcall value
1767 was made later and so appears later on the chain. */
1770 ignore_some_movables (struct loop_movables
*movables
)
1772 struct movable
*m
, *m1
;
1774 for (m
= movables
->head
; m
; m
= m
->next
)
1776 /* Is this a movable for the value of a libcall? */
1777 rtx note
= find_reg_note (m
->insn
, REG_RETVAL
, NULL_RTX
);
1781 /* Check for earlier movables inside that range,
1782 and mark them invalid. We cannot use LUIDs here because
1783 insns created by loop.c for prior loops don't have LUIDs.
1784 Rather than reject all such insns from movables, we just
1785 explicitly check each insn in the libcall (since invariant
1786 libcalls aren't that common). */
1787 for (insn
= XEXP (note
, 0); insn
!= m
->insn
; insn
= NEXT_INSN (insn
))
1788 for (m1
= movables
->head
; m1
!= m
; m1
= m1
->next
)
1789 if (m1
->insn
== insn
)
1795 /* For each movable insn, see if the reg that it loads
1796 leads when it dies right into another conditionally movable insn.
1797 If so, record that the second insn "forces" the first one,
1798 since the second can be moved only if the first is. */
1801 force_movables (struct loop_movables
*movables
)
1803 struct movable
*m
, *m1
;
1805 for (m1
= movables
->head
; m1
; m1
= m1
->next
)
1806 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1807 if (!m1
->partial
&& !m1
->done
)
1809 int regno
= m1
->regno
;
1810 for (m
= m1
->next
; m
; m
= m
->next
)
1811 /* ??? Could this be a bug? What if CSE caused the
1812 register of M1 to be used after this insn?
1813 Since CSE does not update regno_last_uid,
1814 this insn M->insn might not be where it dies.
1815 But very likely this doesn't matter; what matters is
1816 that M's reg is computed from M1's reg. */
1817 if (INSN_UID (m
->insn
) == REGNO_LAST_UID (regno
)
1820 if (m
!= 0 && m
->set_src
== m1
->set_dest
1821 /* If m->consec, m->set_src isn't valid. */
1825 /* Increase the priority of the moving the first insn
1826 since it permits the second to be moved as well.
1827 Likewise for insns already forced by the first insn. */
1833 for (m2
= m1
; m2
; m2
= m2
->forces
)
1835 m2
->lifetime
+= m
->lifetime
;
1836 m2
->savings
+= m
->savings
;
1842 /* Find invariant expressions that are equal and can be combined into
1846 combine_movables (struct loop_movables
*movables
, struct loop_regs
*regs
)
1849 char *matched_regs
= xmalloc (regs
->num
);
1850 enum machine_mode mode
;
1852 /* Regs that are set more than once are not allowed to match
1853 or be matched. I'm no longer sure why not. */
1854 /* Only pseudo registers are allowed to match or be matched,
1855 since move_movables does not validate the change. */
1856 /* Perhaps testing m->consec_sets would be more appropriate here? */
1858 for (m
= movables
->head
; m
; m
= m
->next
)
1859 if (m
->match
== 0 && regs
->array
[m
->regno
].n_times_set
== 1
1860 && m
->regno
>= FIRST_PSEUDO_REGISTER
1865 int regno
= m
->regno
;
1867 memset (matched_regs
, 0, regs
->num
);
1868 matched_regs
[regno
] = 1;
1870 /* We want later insns to match the first one. Don't make the first
1871 one match any later ones. So start this loop at m->next. */
1872 for (m1
= m
->next
; m1
; m1
= m1
->next
)
1873 if (m
!= m1
&& m1
->match
== 0
1875 && regs
->array
[m1
->regno
].n_times_set
== 1
1876 && m1
->regno
>= FIRST_PSEUDO_REGISTER
1877 /* A reg used outside the loop mustn't be eliminated. */
1879 /* A reg used for zero-extending mustn't be eliminated. */
1881 && (matched_regs
[m1
->regno
]
1884 /* Can combine regs with different modes loaded from the
1885 same constant only if the modes are the same or
1886 if both are integer modes with M wider or the same
1887 width as M1. The check for integer is redundant, but
1888 safe, since the only case of differing destination
1889 modes with equal sources is when both sources are
1890 VOIDmode, i.e., CONST_INT. */
1891 (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
)
1892 || (GET_MODE_CLASS (GET_MODE (m
->set_dest
)) == MODE_INT
1893 && GET_MODE_CLASS (GET_MODE (m1
->set_dest
)) == MODE_INT
1894 && (GET_MODE_BITSIZE (GET_MODE (m
->set_dest
))
1895 >= GET_MODE_BITSIZE (GET_MODE (m1
->set_dest
)))))
1896 /* See if the source of M1 says it matches M. */
1897 && ((REG_P (m1
->set_src
)
1898 && matched_regs
[REGNO (m1
->set_src
)])
1899 || rtx_equal_for_loop_p (m
->set_src
, m1
->set_src
,
1901 && ((m
->dependencies
== m1
->dependencies
)
1902 || rtx_equal_p (m
->dependencies
, m1
->dependencies
)))
1904 m
->lifetime
+= m1
->lifetime
;
1905 m
->savings
+= m1
->savings
;
1908 matched_regs
[m1
->regno
] = 1;
1912 /* Now combine the regs used for zero-extension.
1913 This can be done for those not marked `global'
1914 provided their lives don't overlap. */
1916 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1917 mode
= GET_MODE_WIDER_MODE (mode
))
1919 struct movable
*m0
= 0;
1921 /* Combine all the registers for extension from mode MODE.
1922 Don't combine any that are used outside this loop. */
1923 for (m
= movables
->head
; m
; m
= m
->next
)
1924 if (m
->partial
&& ! m
->global
1925 && mode
== GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m
->insn
)))))
1929 int first
= REGNO_FIRST_LUID (m
->regno
);
1930 int last
= REGNO_LAST_LUID (m
->regno
);
1934 /* First one: don't check for overlap, just record it. */
1939 /* Make sure they extend to the same mode.
1940 (Almost always true.) */
1941 if (GET_MODE (m
->set_dest
) != GET_MODE (m0
->set_dest
))
1944 /* We already have one: check for overlap with those
1945 already combined together. */
1946 for (m1
= movables
->head
; m1
!= m
; m1
= m1
->next
)
1947 if (m1
== m0
|| (m1
->partial
&& m1
->match
== m0
))
1948 if (! (REGNO_FIRST_LUID (m1
->regno
) > last
1949 || REGNO_LAST_LUID (m1
->regno
) < first
))
1952 /* No overlap: we can combine this with the others. */
1953 m0
->lifetime
+= m
->lifetime
;
1954 m0
->savings
+= m
->savings
;
1964 free (matched_regs
);
1967 /* Returns the number of movable instructions in LOOP that were not
1968 moved outside the loop. */
1971 num_unmoved_movables (const struct loop
*loop
)
1976 for (m
= LOOP_MOVABLES (loop
)->head
; m
; m
= m
->next
)
1984 /* Return 1 if regs X and Y will become the same if moved. */
1987 regs_match_p (rtx x
, rtx y
, struct loop_movables
*movables
)
1989 unsigned int xn
= REGNO (x
);
1990 unsigned int yn
= REGNO (y
);
1991 struct movable
*mx
, *my
;
1993 for (mx
= movables
->head
; mx
; mx
= mx
->next
)
1994 if (mx
->regno
== xn
)
1997 for (my
= movables
->head
; my
; my
= my
->next
)
1998 if (my
->regno
== yn
)
2002 && ((mx
->match
== my
->match
&& mx
->match
!= 0)
2004 || mx
== my
->match
));
2007 /* Return 1 if X and Y are identical-looking rtx's.
2008 This is the Lisp function EQUAL for rtx arguments.
2010 If two registers are matching movables or a movable register and an
2011 equivalent constant, consider them equal. */
2014 rtx_equal_for_loop_p (rtx x
, rtx y
, struct loop_movables
*movables
,
2015 struct loop_regs
*regs
)
2025 if (x
== 0 || y
== 0)
2028 code
= GET_CODE (x
);
2030 /* If we have a register and a constant, they may sometimes be
2032 if (REG_P (x
) && regs
->array
[REGNO (x
)].set_in_loop
== -2
2035 for (m
= movables
->head
; m
; m
= m
->next
)
2036 if (m
->move_insn
&& m
->regno
== REGNO (x
)
2037 && rtx_equal_p (m
->set_src
, y
))
2040 else if (REG_P (y
) && regs
->array
[REGNO (y
)].set_in_loop
== -2
2043 for (m
= movables
->head
; m
; m
= m
->next
)
2044 if (m
->move_insn
&& m
->regno
== REGNO (y
)
2045 && rtx_equal_p (m
->set_src
, x
))
2049 /* Otherwise, rtx's of different codes cannot be equal. */
2050 if (code
!= GET_CODE (y
))
2053 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
2054 (REG:SI x) and (REG:HI x) are NOT equivalent. */
2056 if (GET_MODE (x
) != GET_MODE (y
))
2059 /* These three types of rtx's can be compared nonrecursively. */
2061 return (REGNO (x
) == REGNO (y
) || regs_match_p (x
, y
, movables
));
2063 if (code
== LABEL_REF
)
2064 return XEXP (x
, 0) == XEXP (y
, 0);
2065 if (code
== SYMBOL_REF
)
2066 return XSTR (x
, 0) == XSTR (y
, 0);
2068 /* Compare the elements. If any pair of corresponding elements
2069 fail to match, return 0 for the whole things. */
2071 fmt
= GET_RTX_FORMAT (code
);
2072 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2077 if (XWINT (x
, i
) != XWINT (y
, i
))
2082 if (XINT (x
, i
) != XINT (y
, i
))
2087 /* Two vectors must have the same length. */
2088 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
2091 /* And the corresponding elements must match. */
2092 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2093 if (rtx_equal_for_loop_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
),
2094 movables
, regs
) == 0)
2099 if (rtx_equal_for_loop_p (XEXP (x
, i
), XEXP (y
, i
), movables
, regs
)
2105 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
2110 /* These are just backpointers, so they don't matter. */
2116 /* It is believed that rtx's at this level will never
2117 contain anything but integers and other rtx's,
2118 except for within LABEL_REFs and SYMBOL_REFs. */
2126 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
2127 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
2128 references is incremented once for each added note. */
2131 add_label_notes (rtx x
, rtx insns
)
2133 enum rtx_code code
= GET_CODE (x
);
2138 if (code
== LABEL_REF
&& !LABEL_REF_NONLOCAL_P (x
))
2140 /* This code used to ignore labels that referred to dispatch tables to
2141 avoid flow generating (slightly) worse code.
2143 We no longer ignore such label references (see LABEL_REF handling in
2144 mark_jump_label for additional information). */
2145 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
2146 if (reg_mentioned_p (XEXP (x
, 0), insn
))
2148 REG_NOTES (insn
) = gen_rtx_INSN_LIST (REG_LABEL
, XEXP (x
, 0),
2150 if (LABEL_P (XEXP (x
, 0)))
2151 LABEL_NUSES (XEXP (x
, 0))++;
2155 fmt
= GET_RTX_FORMAT (code
);
2156 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2159 add_label_notes (XEXP (x
, i
), insns
);
2160 else if (fmt
[i
] == 'E')
2161 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
2162 add_label_notes (XVECEXP (x
, i
, j
), insns
);
2166 /* Scan MOVABLES, and move the insns that deserve to be moved.
2167 If two matching movables are combined, replace one reg with the
2168 other throughout. */
2171 move_movables (struct loop
*loop
, struct loop_movables
*movables
,
2172 int threshold
, int insn_count
)
2174 struct loop_regs
*regs
= LOOP_REGS (loop
);
2175 int nregs
= regs
->num
;
2179 rtx loop_start
= loop
->start
;
2180 rtx loop_end
= loop
->end
;
2181 /* Map of pseudo-register replacements to handle combining
2182 when we move several insns that load the same value
2183 into different pseudo-registers. */
2184 rtx
*reg_map
= xcalloc (nregs
, sizeof (rtx
));
2185 char *already_moved
= xcalloc (nregs
, sizeof (char));
2187 for (m
= movables
->head
; m
; m
= m
->next
)
2189 /* Describe this movable insn. */
2191 if (loop_dump_stream
)
2193 fprintf (loop_dump_stream
, "Insn %d: regno %d (life %d), ",
2194 INSN_UID (m
->insn
), m
->regno
, m
->lifetime
);
2196 fprintf (loop_dump_stream
, "consec %d, ", m
->consec
);
2198 fprintf (loop_dump_stream
, "cond ");
2200 fprintf (loop_dump_stream
, "force ");
2202 fprintf (loop_dump_stream
, "global ");
2204 fprintf (loop_dump_stream
, "done ");
2206 fprintf (loop_dump_stream
, "move-insn ");
2208 fprintf (loop_dump_stream
, "matches %d ",
2209 INSN_UID (m
->match
->insn
));
2211 fprintf (loop_dump_stream
, "forces %d ",
2212 INSN_UID (m
->forces
->insn
));
2215 /* Ignore the insn if it's already done (it matched something else).
2216 Otherwise, see if it is now safe to move. */
2220 || (1 == loop_invariant_p (loop
, m
->set_src
)
2221 && (m
->dependencies
== 0
2222 || 1 == loop_invariant_p (loop
, m
->dependencies
))
2224 || 1 == consec_sets_invariant_p (loop
, m
->set_dest
,
2227 && (! m
->forces
|| m
->forces
->done
))
2231 int savings
= m
->savings
;
2233 /* We have an insn that is safe to move.
2234 Compute its desirability. */
2239 if (loop_dump_stream
)
2240 fprintf (loop_dump_stream
, "savings %d ", savings
);
2242 if (regs
->array
[regno
].moved_once
&& loop_dump_stream
)
2243 fprintf (loop_dump_stream
, "halved since already moved ");
2245 /* An insn MUST be moved if we already moved something else
2246 which is safe only if this one is moved too: that is,
2247 if already_moved[REGNO] is nonzero. */
2249 /* An insn is desirable to move if the new lifetime of the
2250 register is no more than THRESHOLD times the old lifetime.
2251 If it's not desirable, it means the loop is so big
2252 that moving won't speed things up much,
2253 and it is liable to make register usage worse. */
2255 /* It is also desirable to move if it can be moved at no
2256 extra cost because something else was already moved. */
2258 if (already_moved
[regno
]
2259 || (threshold
* savings
* m
->lifetime
) >=
2260 (regs
->array
[regno
].moved_once
? insn_count
* 2 : insn_count
)
2261 || (m
->forces
&& m
->forces
->done
2262 && regs
->array
[m
->forces
->regno
].n_times_set
== 1))
2266 rtx first
= NULL_RTX
;
2267 rtx newreg
= NULL_RTX
;
2270 newreg
= gen_reg_rtx (GET_MODE (m
->set_dest
));
2272 /* Now move the insns that set the reg. */
2274 if (m
->partial
&& m
->match
)
2278 /* Find the end of this chain of matching regs.
2279 Thus, we load each reg in the chain from that one reg.
2280 And that reg is loaded with 0 directly,
2281 since it has ->match == 0. */
2282 for (m1
= m
; m1
->match
; m1
= m1
->match
);
2283 newpat
= gen_move_insn (SET_DEST (PATTERN (m
->insn
)),
2284 SET_DEST (PATTERN (m1
->insn
)));
2285 i1
= loop_insn_hoist (loop
, newpat
);
2287 /* Mark the moved, invariant reg as being allowed to
2288 share a hard reg with the other matching invariant. */
2289 REG_NOTES (i1
) = REG_NOTES (m
->insn
);
2290 r1
= SET_DEST (PATTERN (m
->insn
));
2291 r2
= SET_DEST (PATTERN (m1
->insn
));
2293 = gen_rtx_EXPR_LIST (VOIDmode
, r1
,
2294 gen_rtx_EXPR_LIST (VOIDmode
, r2
,
2296 delete_insn (m
->insn
);
2301 if (loop_dump_stream
)
2302 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
2304 /* If we are to re-generate the item being moved with a
2305 new move insn, first delete what we have and then emit
2306 the move insn before the loop. */
2307 else if (m
->move_insn
)
2311 for (count
= m
->consec
; count
>= 0; count
--)
2313 /* If this is the first insn of a library call sequence,
2314 something is very wrong. */
2316 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
2319 /* If this is the last insn of a libcall sequence, then
2320 delete every insn in the sequence except the last.
2321 The last insn is handled in the normal manner. */
2323 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
2325 temp
= XEXP (temp
, 0);
2327 temp
= delete_insn (temp
);
2331 p
= delete_insn (p
);
2333 /* simplify_giv_expr expects that it can walk the insns
2334 at m->insn forwards and see this old sequence we are
2335 tossing here. delete_insn does preserve the next
2336 pointers, but when we skip over a NOTE we must fix
2337 it up. Otherwise that code walks into the non-deleted
2339 while (p
&& NOTE_P (p
))
2340 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
2344 /* Replace the original insn with a move from
2345 our newly created temp. */
2347 emit_move_insn (m
->set_dest
, newreg
);
2350 emit_insn_before (seq
, p
);
2355 emit_move_insn (m
->insert_temp
? newreg
: m
->set_dest
,
2360 add_label_notes (m
->set_src
, seq
);
2362 i1
= loop_insn_hoist (loop
, seq
);
2363 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2364 set_unique_reg_note (i1
,
2365 m
->is_equiv
? REG_EQUIV
: REG_EQUAL
,
2368 if (loop_dump_stream
)
2369 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
2371 /* The more regs we move, the less we like moving them. */
2376 for (count
= m
->consec
; count
>= 0; count
--)
2380 /* If first insn of libcall sequence, skip to end. */
2381 /* Do this at start of loop, since p is guaranteed to
2384 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
2387 /* If last insn of libcall sequence, move all
2388 insns except the last before the loop. The last
2389 insn is handled in the normal manner. */
2391 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
2395 rtx fn_address_insn
= 0;
2398 for (temp
= XEXP (temp
, 0); temp
!= p
;
2399 temp
= NEXT_INSN (temp
))
2408 body
= PATTERN (temp
);
2410 /* Find the next insn after TEMP,
2411 not counting USE or NOTE insns. */
2412 for (next
= NEXT_INSN (temp
); next
!= p
;
2413 next
= NEXT_INSN (next
))
2414 if (! (NONJUMP_INSN_P (next
)
2415 && GET_CODE (PATTERN (next
)) == USE
)
2419 /* If that is the call, this may be the insn
2420 that loads the function address.
2422 Extract the function address from the insn
2423 that loads it into a register.
2424 If this insn was cse'd, we get incorrect code.
2426 So emit a new move insn that copies the
2427 function address into the register that the
2428 call insn will use. flow.c will delete any
2429 redundant stores that we have created. */
2431 && GET_CODE (body
) == SET
2432 && REG_P (SET_DEST (body
))
2433 && (n
= find_reg_note (temp
, REG_EQUAL
,
2436 fn_reg
= SET_SRC (body
);
2437 if (!REG_P (fn_reg
))
2438 fn_reg
= SET_DEST (body
);
2439 fn_address
= XEXP (n
, 0);
2440 fn_address_insn
= temp
;
2442 /* We have the call insn.
2443 If it uses the register we suspect it might,
2444 load it with the correct address directly. */
2447 && reg_referenced_p (fn_reg
, body
))
2448 loop_insn_emit_after (loop
, 0, fn_address_insn
,
2450 (fn_reg
, fn_address
));
2454 i1
= loop_call_insn_hoist (loop
, body
);
2455 /* Because the USAGE information potentially
2456 contains objects other than hard registers
2457 we need to copy it. */
2458 if (CALL_INSN_FUNCTION_USAGE (temp
))
2459 CALL_INSN_FUNCTION_USAGE (i1
)
2460 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp
));
2463 i1
= loop_insn_hoist (loop
, body
);
2466 if (temp
== fn_address_insn
)
2467 fn_address_insn
= i1
;
2468 REG_NOTES (i1
) = REG_NOTES (temp
);
2469 REG_NOTES (temp
) = NULL
;
2475 if (m
->savemode
!= VOIDmode
)
2477 /* P sets REG to zero; but we should clear only
2478 the bits that are not covered by the mode
2480 rtx reg
= m
->set_dest
;
2485 tem
= expand_simple_binop
2486 (GET_MODE (reg
), AND
, reg
,
2487 GEN_INT ((((HOST_WIDE_INT
) 1
2488 << GET_MODE_BITSIZE (m
->savemode
)))
2490 reg
, 1, OPTAB_LIB_WIDEN
);
2494 emit_move_insn (reg
, tem
);
2495 sequence
= get_insns ();
2497 i1
= loop_insn_hoist (loop
, sequence
);
2499 else if (CALL_P (p
))
2501 i1
= loop_call_insn_hoist (loop
, PATTERN (p
));
2502 /* Because the USAGE information potentially
2503 contains objects other than hard registers
2504 we need to copy it. */
2505 if (CALL_INSN_FUNCTION_USAGE (p
))
2506 CALL_INSN_FUNCTION_USAGE (i1
)
2507 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p
));
2509 else if (count
== m
->consec
&& m
->move_insn_first
)
2512 /* The SET_SRC might not be invariant, so we must
2513 use the REG_EQUAL note. */
2515 emit_move_insn (m
->insert_temp
? newreg
: m
->set_dest
,
2520 add_label_notes (m
->set_src
, seq
);
2522 i1
= loop_insn_hoist (loop
, seq
);
2523 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2524 set_unique_reg_note (i1
, m
->is_equiv
? REG_EQUIV
2525 : REG_EQUAL
, m
->set_src
);
2527 else if (m
->insert_temp
)
2529 rtx
*reg_map2
= xcalloc (REGNO (newreg
),
2531 reg_map2
[m
->regno
] = newreg
;
2533 i1
= loop_insn_hoist (loop
, copy_rtx (PATTERN (p
)));
2534 replace_regs (i1
, reg_map2
, REGNO (newreg
), 1);
2538 i1
= loop_insn_hoist (loop
, PATTERN (p
));
2540 if (REG_NOTES (i1
) == 0)
2542 REG_NOTES (i1
) = REG_NOTES (p
);
2543 REG_NOTES (p
) = NULL
;
2545 /* If there is a REG_EQUAL note present whose value
2546 is not loop invariant, then delete it, since it
2547 may cause problems with later optimization passes.
2548 It is possible for cse to create such notes
2549 like this as a result of record_jump_cond. */
2551 if ((temp
= find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2552 && ! loop_invariant_p (loop
, XEXP (temp
, 0)))
2553 remove_note (i1
, temp
);
2559 if (loop_dump_stream
)
2560 fprintf (loop_dump_stream
, " moved to %d",
2563 /* If library call, now fix the REG_NOTES that contain
2564 insn pointers, namely REG_LIBCALL on FIRST
2565 and REG_RETVAL on I1. */
2566 if ((temp
= find_reg_note (i1
, REG_RETVAL
, NULL_RTX
)))
2568 XEXP (temp
, 0) = first
;
2569 temp
= find_reg_note (first
, REG_LIBCALL
, NULL_RTX
);
2570 XEXP (temp
, 0) = i1
;
2577 /* simplify_giv_expr expects that it can walk the insns
2578 at m->insn forwards and see this old sequence we are
2579 tossing here. delete_insn does preserve the next
2580 pointers, but when we skip over a NOTE we must fix
2581 it up. Otherwise that code walks into the non-deleted
2583 while (p
&& NOTE_P (p
))
2584 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
2589 /* Replace the original insn with a move from
2590 our newly created temp. */
2592 emit_move_insn (m
->set_dest
, newreg
);
2595 emit_insn_before (seq
, p
);
2599 /* The more regs we move, the less we like moving them. */
2605 if (!m
->insert_temp
)
2607 /* Any other movable that loads the same register
2609 already_moved
[regno
] = 1;
2611 /* This reg has been moved out of one loop. */
2612 regs
->array
[regno
].moved_once
= 1;
2614 /* The reg set here is now invariant. */
2618 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, m
->set_dest
); i
++)
2619 regs
->array
[regno
+i
].set_in_loop
= 0;
2622 /* Change the length-of-life info for the register
2623 to say it lives at least the full length of this loop.
2624 This will help guide optimizations in outer loops. */
2626 if (REGNO_FIRST_LUID (regno
) > INSN_LUID (loop_start
))
2627 /* This is the old insn before all the moved insns.
2628 We can't use the moved insn because it is out of range
2629 in uid_luid. Only the old insns have luids. */
2630 REGNO_FIRST_UID (regno
) = INSN_UID (loop_start
);
2631 if (REGNO_LAST_LUID (regno
) < INSN_LUID (loop_end
))
2632 REGNO_LAST_UID (regno
) = INSN_UID (loop_end
);
2635 /* Combine with this moved insn any other matching movables. */
2638 for (m1
= movables
->head
; m1
; m1
= m1
->next
)
2643 /* Schedule the reg loaded by M1
2644 for replacement so that shares the reg of M.
2645 If the modes differ (only possible in restricted
2646 circumstances, make a SUBREG.
2648 Note this assumes that the target dependent files
2649 treat REG and SUBREG equally, including within
2650 GO_IF_LEGITIMATE_ADDRESS and in all the
2651 predicates since we never verify that replacing the
2652 original register with a SUBREG results in a
2653 recognizable insn. */
2654 if (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
))
2655 reg_map
[m1
->regno
] = m
->set_dest
;
2658 = gen_lowpart_common (GET_MODE (m1
->set_dest
),
2661 /* Get rid of the matching insn
2662 and prevent further processing of it. */
2665 /* If library call, delete all insns. */
2666 if ((temp
= find_reg_note (m1
->insn
, REG_RETVAL
,
2668 delete_insn_chain (XEXP (temp
, 0), m1
->insn
);
2670 delete_insn (m1
->insn
);
2672 /* Any other movable that loads the same register
2674 already_moved
[m1
->regno
] = 1;
2676 /* The reg merged here is now invariant,
2677 if the reg it matches is invariant. */
2682 i
< LOOP_REGNO_NREGS (regno
, m1
->set_dest
);
2684 regs
->array
[m1
->regno
+i
].set_in_loop
= 0;
2688 else if (loop_dump_stream
)
2689 fprintf (loop_dump_stream
, "not desirable");
2691 else if (loop_dump_stream
&& !m
->match
)
2692 fprintf (loop_dump_stream
, "not safe");
2694 if (loop_dump_stream
)
2695 fprintf (loop_dump_stream
, "\n");
2699 new_start
= loop_start
;
2701 /* Go through all the instructions in the loop, making
2702 all the register substitutions scheduled in REG_MAP. */
2703 for (p
= new_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
2706 replace_regs (PATTERN (p
), reg_map
, nregs
, 0);
2707 replace_regs (REG_NOTES (p
), reg_map
, nregs
, 0);
2713 free (already_moved
);
2718 loop_movables_add (struct loop_movables
*movables
, struct movable
*m
)
2720 if (movables
->head
== 0)
2723 movables
->last
->next
= m
;
2729 loop_movables_free (struct loop_movables
*movables
)
2732 struct movable
*m_next
;
2734 for (m
= movables
->head
; m
; m
= m_next
)
2742 /* Scan X and replace the address of any MEM in it with ADDR.
2743 REG is the address that MEM should have before the replacement. */
2746 replace_call_address (rtx x
, rtx reg
, rtx addr
)
2754 code
= GET_CODE (x
);
2768 /* Short cut for very common case. */
2769 replace_call_address (XEXP (x
, 1), reg
, addr
);
2773 /* Short cut for very common case. */
2774 replace_call_address (XEXP (x
, 0), reg
, addr
);
2778 /* If this MEM uses a reg other than the one we expected,
2779 something is wrong. */
2780 if (XEXP (x
, 0) != reg
)
2789 fmt
= GET_RTX_FORMAT (code
);
2790 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2793 replace_call_address (XEXP (x
, i
), reg
, addr
);
2794 else if (fmt
[i
] == 'E')
2797 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2798 replace_call_address (XVECEXP (x
, i
, j
), reg
, addr
);
2804 /* Return the number of memory refs to addresses that vary
2808 count_nonfixed_reads (const struct loop
*loop
, rtx x
)
2818 code
= GET_CODE (x
);
2832 return ((loop_invariant_p (loop
, XEXP (x
, 0)) != 1)
2833 + count_nonfixed_reads (loop
, XEXP (x
, 0)));
2840 fmt
= GET_RTX_FORMAT (code
);
2841 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2844 value
+= count_nonfixed_reads (loop
, XEXP (x
, i
));
2848 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2849 value
+= count_nonfixed_reads (loop
, XVECEXP (x
, i
, j
));
2855 /* Scan a loop setting the elements `loops_enclosed',
2856 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2857 `unknown_address_altered', `unknown_constant_address_altered', and
2858 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2859 list `store_mems' in LOOP. */
2862 prescan_loop (struct loop
*loop
)
2866 struct loop_info
*loop_info
= LOOP_INFO (loop
);
2867 rtx start
= loop
->start
;
2868 rtx end
= loop
->end
;
2869 /* The label after END. Jumping here is just like falling off the
2870 end of the loop. We use next_nonnote_insn instead of next_label
2871 as a hedge against the (pathological) case where some actual insn
2872 might end up between the two. */
2873 rtx exit_target
= next_nonnote_insn (end
);
2875 loop_info
->has_indirect_jump
= indirect_jump_in_function
;
2876 loop_info
->pre_header_has_call
= 0;
2877 loop_info
->has_call
= 0;
2878 loop_info
->has_nonconst_call
= 0;
2879 loop_info
->has_prefetch
= 0;
2880 loop_info
->has_volatile
= 0;
2881 loop_info
->has_tablejump
= 0;
2882 loop_info
->has_multiple_exit_targets
= 0;
2885 loop_info
->unknown_address_altered
= 0;
2886 loop_info
->unknown_constant_address_altered
= 0;
2887 loop_info
->store_mems
= NULL_RTX
;
2888 loop_info
->first_loop_store_insn
= NULL_RTX
;
2889 loop_info
->mems_idx
= 0;
2890 loop_info
->num_mem_sets
= 0;
2892 for (insn
= start
; insn
&& !LABEL_P (insn
);
2893 insn
= PREV_INSN (insn
))
2897 loop_info
->pre_header_has_call
= 1;
2902 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2903 insn
= NEXT_INSN (insn
))
2905 switch (GET_CODE (insn
))
2908 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
2911 /* Count number of loops contained in this one. */
2914 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
2919 if (! CONST_OR_PURE_CALL_P (insn
))
2921 loop_info
->unknown_address_altered
= 1;
2922 loop_info
->has_nonconst_call
= 1;
2924 else if (pure_call_p (insn
))
2925 loop_info
->has_nonconst_call
= 1;
2926 loop_info
->has_call
= 1;
2927 if (can_throw_internal (insn
))
2928 loop_info
->has_multiple_exit_targets
= 1;
2932 if (! loop_info
->has_multiple_exit_targets
)
2934 rtx set
= pc_set (insn
);
2938 rtx src
= SET_SRC (set
);
2941 if (GET_CODE (src
) == IF_THEN_ELSE
)
2943 label1
= XEXP (src
, 1);
2944 label2
= XEXP (src
, 2);
2954 if (label1
&& label1
!= pc_rtx
)
2956 if (GET_CODE (label1
) != LABEL_REF
)
2958 /* Something tricky. */
2959 loop_info
->has_multiple_exit_targets
= 1;
2962 else if (XEXP (label1
, 0) != exit_target
2963 && LABEL_OUTSIDE_LOOP_P (label1
))
2965 /* A jump outside the current loop. */
2966 loop_info
->has_multiple_exit_targets
= 1;
2978 /* A return, or something tricky. */
2979 loop_info
->has_multiple_exit_targets
= 1;
2985 if (volatile_refs_p (PATTERN (insn
)))
2986 loop_info
->has_volatile
= 1;
2989 && (GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
2990 || GET_CODE (PATTERN (insn
)) == ADDR_VEC
))
2991 loop_info
->has_tablejump
= 1;
2993 note_stores (PATTERN (insn
), note_addr_stored
, loop_info
);
2994 if (! loop_info
->first_loop_store_insn
&& loop_info
->store_mems
)
2995 loop_info
->first_loop_store_insn
= insn
;
2997 if (flag_non_call_exceptions
&& can_throw_internal (insn
))
2998 loop_info
->has_multiple_exit_targets
= 1;
3006 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
3007 if (/* An exception thrown by a called function might land us
3009 ! loop_info
->has_nonconst_call
3010 /* We don't want loads for MEMs moved to a location before the
3011 one at which their stack memory becomes allocated. (Note
3012 that this is not a problem for malloc, etc., since those
3013 require actual function calls. */
3014 && ! current_function_calls_alloca
3015 /* There are ways to leave the loop other than falling off the
3017 && ! loop_info
->has_multiple_exit_targets
)
3018 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
3019 insn
= NEXT_INSN (insn
))
3020 for_each_rtx (&insn
, insert_loop_mem
, loop_info
);
3022 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
3023 that loop_invariant_p and load_mems can use true_dependence
3024 to determine what is really clobbered. */
3025 if (loop_info
->unknown_address_altered
)
3027 rtx mem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
3029 loop_info
->store_mems
3030 = gen_rtx_EXPR_LIST (VOIDmode
, mem
, loop_info
->store_mems
);
3032 if (loop_info
->unknown_constant_address_altered
)
3034 rtx mem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
3035 MEM_READONLY_P (mem
) = 1;
3036 loop_info
->store_mems
3037 = gen_rtx_EXPR_LIST (VOIDmode
, mem
, loop_info
->store_mems
);
3041 /* Invalidate all loops containing LABEL. */
3044 invalidate_loops_containing_label (rtx label
)
3047 for (loop
= uid_loop
[INSN_UID (label
)]; loop
; loop
= loop
->outer
)
3051 /* Scan the function looking for loops. Record the start and end of each loop.
3052 Also mark as invalid loops any loops that contain a setjmp or are branched
3053 to from outside the loop. */
3056 find_and_verify_loops (rtx f
, struct loops
*loops
)
3061 struct loop
*current_loop
;
3062 struct loop
*next_loop
;
3065 num_loops
= loops
->num
;
3067 compute_luids (f
, NULL_RTX
, 0);
3069 /* If there are jumps to undefined labels,
3070 treat them as jumps out of any/all loops.
3071 This also avoids writing past end of tables when there are no loops. */
3074 /* Find boundaries of loops, mark which loops are contained within
3075 loops, and invalidate loops that have setjmp. */
3078 current_loop
= NULL
;
3079 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
3082 switch (NOTE_LINE_NUMBER (insn
))
3084 case NOTE_INSN_LOOP_BEG
:
3085 next_loop
= loops
->array
+ num_loops
;
3086 next_loop
->num
= num_loops
;
3088 next_loop
->start
= insn
;
3089 next_loop
->outer
= current_loop
;
3090 current_loop
= next_loop
;
3093 case NOTE_INSN_LOOP_END
:
3097 current_loop
->end
= insn
;
3098 current_loop
= current_loop
->outer
;
3106 && find_reg_note (insn
, REG_SETJMP
, NULL
))
3108 /* In this case, we must invalidate our current loop and any
3110 for (loop
= current_loop
; loop
; loop
= loop
->outer
)
3113 if (loop_dump_stream
)
3114 fprintf (loop_dump_stream
,
3115 "\nLoop at %d ignored due to setjmp.\n",
3116 INSN_UID (loop
->start
));
3120 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
3121 enclosing loop, but this doesn't matter. */
3122 uid_loop
[INSN_UID (insn
)] = current_loop
;
3125 /* Any loop containing a label used in an initializer must be invalidated,
3126 because it can be jumped into from anywhere. */
3127 for (label
= forced_labels
; label
; label
= XEXP (label
, 1))
3128 invalidate_loops_containing_label (XEXP (label
, 0));
3130 /* Any loop containing a label used for an exception handler must be
3131 invalidated, because it can be jumped into from anywhere. */
3132 for_each_eh_label (invalidate_loops_containing_label
);
3134 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
3135 loop that it is not contained within, that loop is marked invalid.
3136 If any INSN or CALL_INSN uses a label's address, then the loop containing
3137 that label is marked invalid, because it could be jumped into from
3140 Also look for blocks of code ending in an unconditional branch that
3141 exits the loop. If such a block is surrounded by a conditional
3142 branch around the block, move the block elsewhere (see below) and
3143 invert the jump to point to the code block. This may eliminate a
3144 label in our loop and will simplify processing by both us and a
3145 possible second cse pass. */
3147 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
3150 struct loop
*this_loop
= uid_loop
[INSN_UID (insn
)];
3152 if (NONJUMP_INSN_P (insn
) || CALL_P (insn
))
3154 rtx note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
3156 invalidate_loops_containing_label (XEXP (note
, 0));
3162 mark_loop_jump (PATTERN (insn
), this_loop
);
3164 /* See if this is an unconditional branch outside the loop. */
3166 && (GET_CODE (PATTERN (insn
)) == RETURN
3167 || (any_uncondjump_p (insn
)
3168 && onlyjump_p (insn
)
3169 && (uid_loop
[INSN_UID (JUMP_LABEL (insn
))]
3171 && get_max_uid () < max_uid_for_loop
)
3174 rtx our_next
= next_real_insn (insn
);
3175 rtx last_insn_to_move
= NEXT_INSN (insn
);
3176 struct loop
*dest_loop
;
3177 struct loop
*outer_loop
= NULL
;
3179 /* Go backwards until we reach the start of the loop, a label,
3181 for (p
= PREV_INSN (insn
);
3184 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
3189 /* Check for the case where we have a jump to an inner nested
3190 loop, and do not perform the optimization in that case. */
3192 if (JUMP_LABEL (insn
))
3194 dest_loop
= uid_loop
[INSN_UID (JUMP_LABEL (insn
))];
3197 for (outer_loop
= dest_loop
; outer_loop
;
3198 outer_loop
= outer_loop
->outer
)
3199 if (outer_loop
== this_loop
)
3204 /* Make sure that the target of P is within the current loop. */
3206 if (JUMP_P (p
) && JUMP_LABEL (p
)
3207 && uid_loop
[INSN_UID (JUMP_LABEL (p
))] != this_loop
)
3208 outer_loop
= this_loop
;
3210 /* If we stopped on a JUMP_INSN to the next insn after INSN,
3211 we have a block of code to try to move.
3213 We look backward and then forward from the target of INSN
3214 to find a BARRIER at the same loop depth as the target.
3215 If we find such a BARRIER, we make a new label for the start
3216 of the block, invert the jump in P and point it to that label,
3217 and move the block of code to the spot we found. */
3221 && JUMP_LABEL (p
) != 0
3222 /* Just ignore jumps to labels that were never emitted.
3223 These always indicate compilation errors. */
3224 && INSN_UID (JUMP_LABEL (p
)) != 0
3225 && any_condjump_p (p
) && onlyjump_p (p
)
3226 && next_real_insn (JUMP_LABEL (p
)) == our_next
3227 /* If it's not safe to move the sequence, then we
3229 && insns_safe_to_move_p (p
, NEXT_INSN (insn
),
3230 &last_insn_to_move
))
3233 = JUMP_LABEL (insn
) ? JUMP_LABEL (insn
) : get_last_insn ();
3234 struct loop
*target_loop
= uid_loop
[INSN_UID (target
)];
3238 /* Search for possible garbage past the conditional jumps
3239 and look for the last barrier. */
3240 for (tmp
= last_insn_to_move
;
3241 tmp
&& !LABEL_P (tmp
); tmp
= NEXT_INSN (tmp
))
3242 if (BARRIER_P (tmp
))
3243 last_insn_to_move
= tmp
;
3245 for (loc
= target
; loc
; loc
= PREV_INSN (loc
))
3247 /* Don't move things inside a tablejump. */
3248 && ((loc2
= next_nonnote_insn (loc
)) == 0
3250 || (loc2
= next_nonnote_insn (loc2
)) == 0
3252 || (GET_CODE (PATTERN (loc2
)) != ADDR_VEC
3253 && GET_CODE (PATTERN (loc2
)) != ADDR_DIFF_VEC
))
3254 && uid_loop
[INSN_UID (loc
)] == target_loop
)
3258 for (loc
= target
; loc
; loc
= NEXT_INSN (loc
))
3260 /* Don't move things inside a tablejump. */
3261 && ((loc2
= next_nonnote_insn (loc
)) == 0
3263 || (loc2
= next_nonnote_insn (loc2
)) == 0
3265 || (GET_CODE (PATTERN (loc2
)) != ADDR_VEC
3266 && GET_CODE (PATTERN (loc2
)) != ADDR_DIFF_VEC
))
3267 && uid_loop
[INSN_UID (loc
)] == target_loop
)
3272 rtx cond_label
= JUMP_LABEL (p
);
3273 rtx new_label
= get_label_after (p
);
3275 /* Ensure our label doesn't go away. */
3276 LABEL_NUSES (cond_label
)++;
3278 /* Verify that uid_loop is large enough and that
3280 if (invert_jump (p
, new_label
, 1))
3284 /* If no suitable BARRIER was found, create a suitable
3285 one before TARGET. Since TARGET is a fall through
3286 path, we'll need to insert a jump around our block
3287 and add a BARRIER before TARGET.
3289 This creates an extra unconditional jump outside
3290 the loop. However, the benefits of removing rarely
3291 executed instructions from inside the loop usually
3292 outweighs the cost of the extra unconditional jump
3293 outside the loop. */
3298 temp
= gen_jump (JUMP_LABEL (insn
));
3299 temp
= emit_jump_insn_before (temp
, target
);
3300 JUMP_LABEL (temp
) = JUMP_LABEL (insn
);
3301 LABEL_NUSES (JUMP_LABEL (insn
))++;
3302 loc
= emit_barrier_before (target
);
3305 /* Include the BARRIER after INSN and copy the
3307 if (squeeze_notes (&new_label
, &last_insn_to_move
))
3309 reorder_insns (new_label
, last_insn_to_move
, loc
);
3311 /* All those insns are now in TARGET_LOOP. */
3313 q
!= NEXT_INSN (last_insn_to_move
);
3315 uid_loop
[INSN_UID (q
)] = target_loop
;
3317 /* The label jumped to by INSN is no longer a loop
3318 exit. Unless INSN does not have a label (e.g.,
3319 it is a RETURN insn), search loop->exit_labels
3320 to find its label_ref, and remove it. Also turn
3321 off LABEL_OUTSIDE_LOOP_P bit. */
3322 if (JUMP_LABEL (insn
))
3324 for (q
= 0, r
= this_loop
->exit_labels
;
3326 q
= r
, r
= LABEL_NEXTREF (r
))
3327 if (XEXP (r
, 0) == JUMP_LABEL (insn
))
3329 LABEL_OUTSIDE_LOOP_P (r
) = 0;
3331 LABEL_NEXTREF (q
) = LABEL_NEXTREF (r
);
3333 this_loop
->exit_labels
= LABEL_NEXTREF (r
);
3337 for (loop
= this_loop
; loop
&& loop
!= target_loop
;
3341 /* If we didn't find it, then something is
3347 /* P is now a jump outside the loop, so it must be put
3348 in loop->exit_labels, and marked as such.
3349 The easiest way to do this is to just call
3350 mark_loop_jump again for P. */
3351 mark_loop_jump (PATTERN (p
), this_loop
);
3353 /* If INSN now jumps to the insn after it,
3355 if (JUMP_LABEL (insn
) != 0
3356 && (next_real_insn (JUMP_LABEL (insn
))
3357 == next_real_insn (insn
)))
3358 delete_related_insns (insn
);
3361 /* Continue the loop after where the conditional
3362 branch used to jump, since the only branch insn
3363 in the block (if it still remains) is an inter-loop
3364 branch and hence needs no processing. */
3365 insn
= NEXT_INSN (cond_label
);
3367 if (--LABEL_NUSES (cond_label
) == 0)
3368 delete_related_insns (cond_label
);
3370 /* This loop will be continued with NEXT_INSN (insn). */
3371 insn
= PREV_INSN (insn
);
3378 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3379 loops it is contained in, mark the target loop invalid.
3381 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3384 mark_loop_jump (rtx x
, struct loop
*loop
)
3386 struct loop
*dest_loop
;
3387 struct loop
*outer_loop
;
3390 switch (GET_CODE (x
))
3403 /* There could be a label reference in here. */
3404 mark_loop_jump (XEXP (x
, 0), loop
);
3410 mark_loop_jump (XEXP (x
, 0), loop
);
3411 mark_loop_jump (XEXP (x
, 1), loop
);
3415 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3416 mark_loop_jump (XEXP (x
, 1), loop
);
3421 mark_loop_jump (XEXP (x
, 0), loop
);
3425 dest_loop
= uid_loop
[INSN_UID (XEXP (x
, 0))];
3427 /* Link together all labels that branch outside the loop. This
3428 is used by final_[bg]iv_value and the loop unrolling code. Also
3429 mark this LABEL_REF so we know that this branch should predict
3432 /* A check to make sure the label is not in an inner nested loop,
3433 since this does not count as a loop exit. */
3436 for (outer_loop
= dest_loop
; outer_loop
;
3437 outer_loop
= outer_loop
->outer
)
3438 if (outer_loop
== loop
)
3444 if (loop
&& ! outer_loop
)
3446 LABEL_OUTSIDE_LOOP_P (x
) = 1;
3447 LABEL_NEXTREF (x
) = loop
->exit_labels
;
3448 loop
->exit_labels
= x
;
3450 for (outer_loop
= loop
;
3451 outer_loop
&& outer_loop
!= dest_loop
;
3452 outer_loop
= outer_loop
->outer
)
3453 outer_loop
->exit_count
++;
3456 /* If this is inside a loop, but not in the current loop or one enclosed
3457 by it, it invalidates at least one loop. */
3462 /* We must invalidate every nested loop containing the target of this
3463 label, except those that also contain the jump insn. */
3465 for (; dest_loop
; dest_loop
= dest_loop
->outer
)
3467 /* Stop when we reach a loop that also contains the jump insn. */
3468 for (outer_loop
= loop
; outer_loop
; outer_loop
= outer_loop
->outer
)
3469 if (dest_loop
== outer_loop
)
3472 /* If we get here, we know we need to invalidate a loop. */
3473 if (loop_dump_stream
&& ! dest_loop
->invalid
)
3474 fprintf (loop_dump_stream
,
3475 "\nLoop at %d ignored due to multiple entry points.\n",
3476 INSN_UID (dest_loop
->start
));
3478 dest_loop
->invalid
= 1;
3483 /* If this is not setting pc, ignore. */
3484 if (SET_DEST (x
) == pc_rtx
)
3485 mark_loop_jump (SET_SRC (x
), loop
);
3489 mark_loop_jump (XEXP (x
, 1), loop
);
3490 mark_loop_jump (XEXP (x
, 2), loop
);
3495 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
3496 mark_loop_jump (XVECEXP (x
, 0, i
), loop
);
3500 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
3501 mark_loop_jump (XVECEXP (x
, 1, i
), loop
);
3505 /* Strictly speaking this is not a jump into the loop, only a possible
3506 jump out of the loop. However, we have no way to link the destination
3507 of this jump onto the list of exit labels. To be safe we mark this
3508 loop and any containing loops as invalid. */
3511 for (outer_loop
= loop
; outer_loop
; outer_loop
= outer_loop
->outer
)
3513 if (loop_dump_stream
&& ! outer_loop
->invalid
)
3514 fprintf (loop_dump_stream
,
3515 "\nLoop at %d ignored due to unknown exit jump.\n",
3516 INSN_UID (outer_loop
->start
));
3517 outer_loop
->invalid
= 1;
3524 /* Return nonzero if there is a label in the range from
3525 insn INSN to and including the insn whose luid is END
3526 INSN must have an assigned luid (i.e., it must not have
3527 been previously created by loop.c). */
3530 labels_in_range_p (rtx insn
, int end
)
3532 while (insn
&& INSN_LUID (insn
) <= end
)
3536 insn
= NEXT_INSN (insn
);
3542 /* Record that a memory reference X is being set. */
3545 note_addr_stored (rtx x
, rtx y ATTRIBUTE_UNUSED
,
3546 void *data ATTRIBUTE_UNUSED
)
3548 struct loop_info
*loop_info
= data
;
3550 if (x
== 0 || !MEM_P (x
))
3553 /* Count number of memory writes.
3554 This affects heuristics in strength_reduce. */
3555 loop_info
->num_mem_sets
++;
3557 /* BLKmode MEM means all memory is clobbered. */
3558 if (GET_MODE (x
) == BLKmode
)
3560 if (MEM_READONLY_P (x
))
3561 loop_info
->unknown_constant_address_altered
= 1;
3563 loop_info
->unknown_address_altered
= 1;
3568 loop_info
->store_mems
= gen_rtx_EXPR_LIST (VOIDmode
, x
,
3569 loop_info
->store_mems
);
3572 /* X is a value modified by an INSN that references a biv inside a loop
3573 exit test (i.e., X is somehow related to the value of the biv). If X
3574 is a pseudo that is used more than once, then the biv is (effectively)
3575 used more than once. DATA is a pointer to a loop_regs structure. */
3578 note_set_pseudo_multiple_uses (rtx x
, rtx y ATTRIBUTE_UNUSED
, void *data
)
3580 struct loop_regs
*regs
= (struct loop_regs
*) data
;
3585 while (GET_CODE (x
) == STRICT_LOW_PART
3586 || GET_CODE (x
) == SIGN_EXTRACT
3587 || GET_CODE (x
) == ZERO_EXTRACT
3588 || GET_CODE (x
) == SUBREG
)
3591 if (!REG_P (x
) || REGNO (x
) < FIRST_PSEUDO_REGISTER
)
3594 /* If we do not have usage information, or if we know the register
3595 is used more than once, note that fact for check_dbra_loop. */
3596 if (REGNO (x
) >= max_reg_before_loop
3597 || ! regs
->array
[REGNO (x
)].single_usage
3598 || regs
->array
[REGNO (x
)].single_usage
== const0_rtx
)
3599 regs
->multiple_uses
= 1;
3602 /* Return nonzero if the rtx X is invariant over the current loop.
3604 The value is 2 if we refer to something only conditionally invariant.
3606 A memory ref is invariant if it is not volatile and does not conflict
3607 with anything stored in `loop_info->store_mems'. */
3610 loop_invariant_p (const struct loop
*loop
, rtx x
)
3612 struct loop_info
*loop_info
= LOOP_INFO (loop
);
3613 struct loop_regs
*regs
= LOOP_REGS (loop
);
3617 int conditional
= 0;
3622 code
= GET_CODE (x
);
3636 case UNSPEC_VOLATILE
:
3640 if ((x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
3641 || x
== arg_pointer_rtx
|| x
== pic_offset_table_rtx
)
3642 && ! current_function_has_nonlocal_goto
)
3645 if (LOOP_INFO (loop
)->has_call
3646 && REGNO (x
) < FIRST_PSEUDO_REGISTER
&& call_used_regs
[REGNO (x
)])
3649 /* Out-of-range regs can occur when we are called from unrolling.
3650 These registers created by the unroller are set in the loop,
3651 hence are never invariant.
3652 Other out-of-range regs can be generated by load_mems; those that
3653 are written to in the loop are not invariant, while those that are
3654 not written to are invariant. It would be easy for load_mems
3655 to set n_times_set correctly for these registers, however, there
3656 is no easy way to distinguish them from registers created by the
3659 if (REGNO (x
) >= (unsigned) regs
->num
)
3662 if (regs
->array
[REGNO (x
)].set_in_loop
< 0)
3665 return regs
->array
[REGNO (x
)].set_in_loop
== 0;
3668 /* Volatile memory references must be rejected. Do this before
3669 checking for read-only items, so that volatile read-only items
3670 will be rejected also. */
3671 if (MEM_VOLATILE_P (x
))
3674 /* See if there is any dependence between a store and this load. */
3675 mem_list_entry
= loop_info
->store_mems
;
3676 while (mem_list_entry
)
3678 if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
3682 mem_list_entry
= XEXP (mem_list_entry
, 1);
3685 /* It's not invalidated by a store in memory
3686 but we must still verify the address is invariant. */
3690 /* Don't mess with insns declared volatile. */
3691 if (MEM_VOLATILE_P (x
))
3699 fmt
= GET_RTX_FORMAT (code
);
3700 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3704 int tem
= loop_invariant_p (loop
, XEXP (x
, i
));
3710 else if (fmt
[i
] == 'E')
3713 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3715 int tem
= loop_invariant_p (loop
, XVECEXP (x
, i
, j
));
3725 return 1 + conditional
;
3728 /* Return nonzero if all the insns in the loop that set REG
3729 are INSN and the immediately following insns,
3730 and if each of those insns sets REG in an invariant way
3731 (not counting uses of REG in them).
3733 The value is 2 if some of these insns are only conditionally invariant.
3735 We assume that INSN itself is the first set of REG
3736 and that its source is invariant. */
3739 consec_sets_invariant_p (const struct loop
*loop
, rtx reg
, int n_sets
,
3742 struct loop_regs
*regs
= LOOP_REGS (loop
);
3744 unsigned int regno
= REGNO (reg
);
3746 /* Number of sets we have to insist on finding after INSN. */
3747 int count
= n_sets
- 1;
3748 int old
= regs
->array
[regno
].set_in_loop
;
3752 /* If N_SETS hit the limit, we can't rely on its value. */
3756 regs
->array
[regno
].set_in_loop
= 0;
3764 code
= GET_CODE (p
);
3766 /* If library call, skip to end of it. */
3767 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
3772 && (set
= single_set (p
))
3773 && REG_P (SET_DEST (set
))
3774 && REGNO (SET_DEST (set
)) == regno
)
3776 this = loop_invariant_p (loop
, SET_SRC (set
));
3779 else if ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)))
3781 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3782 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3784 this = (CONSTANT_P (XEXP (temp
, 0))
3785 || (find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
3786 && loop_invariant_p (loop
, XEXP (temp
, 0))));
3793 else if (code
!= NOTE
)
3795 regs
->array
[regno
].set_in_loop
= old
;
3800 regs
->array
[regno
].set_in_loop
= old
;
3801 /* If loop_invariant_p ever returned 2, we return 2. */
3802 return 1 + (value
& 2);
3805 /* Look at all uses (not sets) of registers in X. For each, if it is
3806 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3807 a different insn, set USAGE[REGNO] to const0_rtx. */
3810 find_single_use_in_loop (struct loop_regs
*regs
, rtx insn
, rtx x
)
3812 enum rtx_code code
= GET_CODE (x
);
3813 const char *fmt
= GET_RTX_FORMAT (code
);
3817 regs
->array
[REGNO (x
)].single_usage
3818 = (regs
->array
[REGNO (x
)].single_usage
!= 0
3819 && regs
->array
[REGNO (x
)].single_usage
!= insn
)
3820 ? const0_rtx
: insn
;
3822 else if (code
== SET
)
3824 /* Don't count SET_DEST if it is a REG; otherwise count things
3825 in SET_DEST because if a register is partially modified, it won't
3826 show up as a potential movable so we don't care how USAGE is set
3828 if (!REG_P (SET_DEST (x
)))
3829 find_single_use_in_loop (regs
, insn
, SET_DEST (x
));
3830 find_single_use_in_loop (regs
, insn
, SET_SRC (x
));
3833 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3835 if (fmt
[i
] == 'e' && XEXP (x
, i
) != 0)
3836 find_single_use_in_loop (regs
, insn
, XEXP (x
, i
));
3837 else if (fmt
[i
] == 'E')
3838 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
3839 find_single_use_in_loop (regs
, insn
, XVECEXP (x
, i
, j
));
3843 /* Count and record any set in X which is contained in INSN. Update
3844 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3848 count_one_set (struct loop_regs
*regs
, rtx insn
, rtx x
, rtx
*last_set
)
3850 if (GET_CODE (x
) == CLOBBER
&& REG_P (XEXP (x
, 0)))
3851 /* Don't move a reg that has an explicit clobber.
3852 It's not worth the pain to try to do it correctly. */
3853 regs
->array
[REGNO (XEXP (x
, 0))].may_not_optimize
= 1;
3855 if (GET_CODE (x
) == SET
|| GET_CODE (x
) == CLOBBER
)
3857 rtx dest
= SET_DEST (x
);
3858 while (GET_CODE (dest
) == SUBREG
3859 || GET_CODE (dest
) == ZERO_EXTRACT
3860 || GET_CODE (dest
) == STRICT_LOW_PART
)
3861 dest
= XEXP (dest
, 0);
3865 int regno
= REGNO (dest
);
3866 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, dest
); i
++)
3868 /* If this is the first setting of this reg
3869 in current basic block, and it was set before,
3870 it must be set in two basic blocks, so it cannot
3871 be moved out of the loop. */
3872 if (regs
->array
[regno
].set_in_loop
> 0
3873 && last_set
[regno
] == 0)
3874 regs
->array
[regno
+i
].may_not_optimize
= 1;
3875 /* If this is not first setting in current basic block,
3876 see if reg was used in between previous one and this.
3877 If so, neither one can be moved. */
3878 if (last_set
[regno
] != 0
3879 && reg_used_between_p (dest
, last_set
[regno
], insn
))
3880 regs
->array
[regno
+i
].may_not_optimize
= 1;
3881 if (regs
->array
[regno
+i
].set_in_loop
< 127)
3882 ++regs
->array
[regno
+i
].set_in_loop
;
3883 last_set
[regno
+i
] = insn
;
3889 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3890 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3891 contained in insn INSN is used by any insn that precedes INSN in
3892 cyclic order starting from the loop entry point.
3894 We don't want to use INSN_LUID here because if we restrict INSN to those
3895 that have a valid INSN_LUID, it means we cannot move an invariant out
3896 from an inner loop past two loops. */
3899 loop_reg_used_before_p (const struct loop
*loop
, rtx set
, rtx insn
)
3901 rtx reg
= SET_DEST (set
);
3904 /* Scan forward checking for register usage. If we hit INSN, we
3905 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3906 for (p
= loop
->scan_start
; p
!= insn
; p
= NEXT_INSN (p
))
3908 if (INSN_P (p
) && reg_overlap_mentioned_p (reg
, PATTERN (p
)))
3919 /* Information we collect about arrays that we might want to prefetch. */
3920 struct prefetch_info
3922 struct iv_class
*class; /* Class this prefetch is based on. */
3923 struct induction
*giv
; /* GIV this prefetch is based on. */
3924 rtx base_address
; /* Start prefetching from this address plus
3926 HOST_WIDE_INT index
;
3927 HOST_WIDE_INT stride
; /* Prefetch stride in bytes in each
3929 unsigned int bytes_accessed
; /* Sum of sizes of all accesses to this
3930 prefetch area in one iteration. */
3931 unsigned int total_bytes
; /* Total bytes loop will access in this block.
3932 This is set only for loops with known
3933 iteration counts and is 0xffffffff
3935 int prefetch_in_loop
; /* Number of prefetch insns in loop. */
3936 int prefetch_before_loop
; /* Number of prefetch insns before loop. */
3937 unsigned int write
: 1; /* 1 for read/write prefetches. */
3940 /* Data used by check_store function. */
3941 struct check_store_data
3947 static void check_store (rtx
, rtx
, void *);
3948 static void emit_prefetch_instructions (struct loop
*);
3949 static int rtx_equal_for_prefetch_p (rtx
, rtx
);
3951 /* Set mem_write when mem_address is found. Used as callback to
3954 check_store (rtx x
, rtx pat ATTRIBUTE_UNUSED
, void *data
)
3956 struct check_store_data
*d
= (struct check_store_data
*) data
;
3958 if ((MEM_P (x
)) && rtx_equal_p (d
->mem_address
, XEXP (x
, 0)))
3962 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3963 important to get some addresses combined. Later more sophisticated
3964 transformations can be added when necessary.
3966 ??? Same trick with swapping operand is done at several other places.
3967 It can be nice to develop some common way to handle this. */
3970 rtx_equal_for_prefetch_p (rtx x
, rtx y
)
3974 enum rtx_code code
= GET_CODE (x
);
3979 if (code
!= GET_CODE (y
))
3982 if (COMMUTATIVE_ARITH_P (x
))
3984 return ((rtx_equal_for_prefetch_p (XEXP (x
, 0), XEXP (y
, 0))
3985 && rtx_equal_for_prefetch_p (XEXP (x
, 1), XEXP (y
, 1)))
3986 || (rtx_equal_for_prefetch_p (XEXP (x
, 0), XEXP (y
, 1))
3987 && rtx_equal_for_prefetch_p (XEXP (x
, 1), XEXP (y
, 0))));
3990 /* Compare the elements. If any pair of corresponding elements fails to
3991 match, return 0 for the whole thing. */
3993 fmt
= GET_RTX_FORMAT (code
);
3994 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3999 if (XWINT (x
, i
) != XWINT (y
, i
))
4004 if (XINT (x
, i
) != XINT (y
, i
))
4009 /* Two vectors must have the same length. */
4010 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
4013 /* And the corresponding elements must match. */
4014 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
4015 if (rtx_equal_for_prefetch_p (XVECEXP (x
, i
, j
),
4016 XVECEXP (y
, i
, j
)) == 0)
4021 if (rtx_equal_for_prefetch_p (XEXP (x
, i
), XEXP (y
, i
)) == 0)
4026 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
4031 /* These are just backpointers, so they don't matter. */
4037 /* It is believed that rtx's at this level will never
4038 contain anything but integers and other rtx's,
4039 except for within LABEL_REFs and SYMBOL_REFs. */
4047 /* Remove constant addition value from the expression X (when present)
4050 static HOST_WIDE_INT
4051 remove_constant_addition (rtx
*x
)
4053 HOST_WIDE_INT addval
= 0;
4056 /* Avoid clobbering a shared CONST expression. */
4057 if (GET_CODE (exp
) == CONST
)
4059 if (GET_CODE (XEXP (exp
, 0)) == PLUS
4060 && GET_CODE (XEXP (XEXP (exp
, 0), 0)) == SYMBOL_REF
4061 && GET_CODE (XEXP (XEXP (exp
, 0), 1)) == CONST_INT
)
4063 *x
= XEXP (XEXP (exp
, 0), 0);
4064 return INTVAL (XEXP (XEXP (exp
, 0), 1));
4069 if (GET_CODE (exp
) == CONST_INT
)
4071 addval
= INTVAL (exp
);
4075 /* For plus expression recurse on ourself. */
4076 else if (GET_CODE (exp
) == PLUS
)
4078 addval
+= remove_constant_addition (&XEXP (exp
, 0));
4079 addval
+= remove_constant_addition (&XEXP (exp
, 1));
4081 /* In case our parameter was constant, remove extra zero from the
4083 if (XEXP (exp
, 0) == const0_rtx
)
4085 else if (XEXP (exp
, 1) == const0_rtx
)
4092 /* Attempt to identify accesses to arrays that are most likely to cause cache
4093 misses, and emit prefetch instructions a few prefetch blocks forward.
4095 To detect the arrays we use the GIV information that was collected by the
4096 strength reduction pass.
4098 The prefetch instructions are generated after the GIV information is done
4099 and before the strength reduction process. The new GIVs are injected into
4100 the strength reduction tables, so the prefetch addresses are optimized as
4103 GIVs are split into base address, stride, and constant addition values.
4104 GIVs with the same address, stride and close addition values are combined
4105 into a single prefetch. Also writes to GIVs are detected, so that prefetch
4106 for write instructions can be used for the block we write to, on machines
4107 that support write prefetches.
4109 Several heuristics are used to determine when to prefetch. They are
4110 controlled by defined symbols that can be overridden for each target. */
4113 emit_prefetch_instructions (struct loop
*loop
)
4115 int num_prefetches
= 0;
4116 int num_real_prefetches
= 0;
4117 int num_real_write_prefetches
= 0;
4118 int num_prefetches_before
= 0;
4119 int num_write_prefetches_before
= 0;
4122 struct iv_class
*bl
;
4123 struct induction
*iv
;
4124 struct prefetch_info info
[MAX_PREFETCHES
];
4125 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4127 if (!HAVE_prefetch
|| PREFETCH_BLOCK
== 0)
4130 /* Consider only loops w/o calls. When a call is done, the loop is probably
4131 slow enough to read the memory. */
4132 if (PREFETCH_NO_CALL
&& LOOP_INFO (loop
)->has_call
)
4134 if (loop_dump_stream
)
4135 fprintf (loop_dump_stream
, "Prefetch: ignoring loop: has call.\n");
4140 /* Don't prefetch in loops known to have few iterations. */
4141 if (PREFETCH_NO_LOW_LOOPCNT
4142 && LOOP_INFO (loop
)->n_iterations
4143 && LOOP_INFO (loop
)->n_iterations
<= PREFETCH_LOW_LOOPCNT
)
4145 if (loop_dump_stream
)
4146 fprintf (loop_dump_stream
,
4147 "Prefetch: ignoring loop: not enough iterations.\n");
4151 /* Search all induction variables and pick those interesting for the prefetch
4153 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
4155 struct induction
*biv
= bl
->biv
, *biv1
;
4160 /* Expect all BIVs to be executed in each iteration. This makes our
4161 analysis more conservative. */
4164 /* Discard non-constant additions that we can't handle well yet, and
4165 BIVs that are executed multiple times; such BIVs ought to be
4166 handled in the nested loop. We accept not_every_iteration BIVs,
4167 since these only result in larger strides and make our
4168 heuristics more conservative. */
4169 if (GET_CODE (biv
->add_val
) != CONST_INT
)
4171 if (loop_dump_stream
)
4173 fprintf (loop_dump_stream
,
4174 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
4175 REGNO (biv
->src_reg
), INSN_UID (biv
->insn
));
4176 print_rtl (loop_dump_stream
, biv
->add_val
);
4177 fprintf (loop_dump_stream
, "\n");
4182 if (biv
->maybe_multiple
)
4184 if (loop_dump_stream
)
4186 fprintf (loop_dump_stream
,
4187 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
4188 REGNO (biv
->src_reg
), INSN_UID (biv
->insn
));
4189 print_rtl (loop_dump_stream
, biv
->add_val
);
4190 fprintf (loop_dump_stream
, "\n");
4195 basestride
+= INTVAL (biv1
->add_val
);
4196 biv1
= biv1
->next_iv
;
4199 if (biv1
|| !basestride
)
4202 for (iv
= bl
->giv
; iv
; iv
= iv
->next_iv
)
4206 HOST_WIDE_INT index
= 0;
4208 HOST_WIDE_INT stride
= 0;
4209 int stride_sign
= 1;
4210 struct check_store_data d
;
4211 const char *ignore_reason
= NULL
;
4212 int size
= GET_MODE_SIZE (GET_MODE (iv
));
4214 /* See whether an induction variable is interesting to us and if
4215 not, report the reason. */
4216 if (iv
->giv_type
!= DEST_ADDR
)
4217 ignore_reason
= "giv is not a destination address";
4219 /* We are interested only in constant stride memory references
4220 in order to be able to compute density easily. */
4221 else if (GET_CODE (iv
->mult_val
) != CONST_INT
)
4222 ignore_reason
= "stride is not constant";
4226 stride
= INTVAL (iv
->mult_val
) * basestride
;
4233 /* On some targets, reversed order prefetches are not
4235 if (PREFETCH_NO_REVERSE_ORDER
&& stride_sign
< 0)
4236 ignore_reason
= "reversed order stride";
4238 /* Prefetch of accesses with an extreme stride might not be
4239 worthwhile, either. */
4240 else if (PREFETCH_NO_EXTREME_STRIDE
4241 && stride
> PREFETCH_EXTREME_STRIDE
)
4242 ignore_reason
= "extreme stride";
4244 /* Ignore GIVs with varying add values; we can't predict the
4245 value for the next iteration. */
4246 else if (!loop_invariant_p (loop
, iv
->add_val
))
4247 ignore_reason
= "giv has varying add value";
4249 /* Ignore GIVs in the nested loops; they ought to have been
4251 else if (iv
->maybe_multiple
)
4252 ignore_reason
= "giv is in nested loop";
4255 if (ignore_reason
!= NULL
)
4257 if (loop_dump_stream
)
4258 fprintf (loop_dump_stream
,
4259 "Prefetch: ignoring giv at %d: %s.\n",
4260 INSN_UID (iv
->insn
), ignore_reason
);
4264 /* Determine the pointer to the basic array we are examining. It is
4265 the sum of the BIV's initial value and the GIV's add_val. */
4266 address
= copy_rtx (iv
->add_val
);
4267 temp
= copy_rtx (bl
->initial_value
);
4269 address
= simplify_gen_binary (PLUS
, Pmode
, temp
, address
);
4270 index
= remove_constant_addition (&address
);
4273 d
.mem_address
= *iv
->location
;
4275 /* When the GIV is not always executed, we might be better off by
4276 not dirtying the cache pages. */
4277 if (PREFETCH_CONDITIONAL
|| iv
->always_executed
)
4278 note_stores (PATTERN (iv
->insn
), check_store
, &d
);
4281 if (loop_dump_stream
)
4282 fprintf (loop_dump_stream
, "Prefetch: Ignoring giv at %d: %s\n",
4283 INSN_UID (iv
->insn
), "in conditional code.");
4287 /* Attempt to find another prefetch to the same array and see if we
4288 can merge this one. */
4289 for (i
= 0; i
< num_prefetches
; i
++)
4290 if (rtx_equal_for_prefetch_p (address
, info
[i
].base_address
)
4291 && stride
== info
[i
].stride
)
4293 /* In case both access same array (same location
4294 just with small difference in constant indexes), merge
4295 the prefetches. Just do the later and the earlier will
4296 get prefetched from previous iteration.
4297 The artificial threshold should not be too small,
4298 but also not bigger than small portion of memory usually
4299 traversed by single loop. */
4300 if (index
>= info
[i
].index
4301 && index
- info
[i
].index
< PREFETCH_EXTREME_DIFFERENCE
)
4303 info
[i
].write
|= d
.mem_write
;
4304 info
[i
].bytes_accessed
+= size
;
4305 info
[i
].index
= index
;
4308 info
[num_prefetches
].base_address
= address
;
4313 if (index
< info
[i
].index
4314 && info
[i
].index
- index
< PREFETCH_EXTREME_DIFFERENCE
)
4316 info
[i
].write
|= d
.mem_write
;
4317 info
[i
].bytes_accessed
+= size
;
4323 /* Merging failed. */
4326 info
[num_prefetches
].giv
= iv
;
4327 info
[num_prefetches
].class = bl
;
4328 info
[num_prefetches
].index
= index
;
4329 info
[num_prefetches
].stride
= stride
;
4330 info
[num_prefetches
].base_address
= address
;
4331 info
[num_prefetches
].write
= d
.mem_write
;
4332 info
[num_prefetches
].bytes_accessed
= size
;
4334 if (num_prefetches
>= MAX_PREFETCHES
)
4336 if (loop_dump_stream
)
4337 fprintf (loop_dump_stream
,
4338 "Maximal number of prefetches exceeded.\n");
4345 for (i
= 0; i
< num_prefetches
; i
++)
4349 /* Attempt to calculate the total number of bytes fetched by all
4350 iterations of the loop. Avoid overflow. */
4351 if (LOOP_INFO (loop
)->n_iterations
4352 && ((unsigned HOST_WIDE_INT
) (0xffffffff / info
[i
].stride
)
4353 >= LOOP_INFO (loop
)->n_iterations
))
4354 info
[i
].total_bytes
= info
[i
].stride
* LOOP_INFO (loop
)->n_iterations
;
4356 info
[i
].total_bytes
= 0xffffffff;
4358 density
= info
[i
].bytes_accessed
* 100 / info
[i
].stride
;
4360 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4361 if (PREFETCH_ONLY_DENSE_MEM
)
4362 if (density
* 256 > PREFETCH_DENSE_MEM
* 100
4363 && (info
[i
].total_bytes
/ PREFETCH_BLOCK
4364 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN
))
4366 info
[i
].prefetch_before_loop
= 1;
4367 info
[i
].prefetch_in_loop
4368 = (info
[i
].total_bytes
/ PREFETCH_BLOCK
4369 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX
);
4373 info
[i
].prefetch_in_loop
= 0, info
[i
].prefetch_before_loop
= 0;
4374 if (loop_dump_stream
)
4375 fprintf (loop_dump_stream
,
4376 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4377 INSN_UID (info
[i
].giv
->insn
), density
);
4380 info
[i
].prefetch_in_loop
= 1, info
[i
].prefetch_before_loop
= 1;
4382 /* Find how many prefetch instructions we'll use within the loop. */
4383 if (info
[i
].prefetch_in_loop
!= 0)
4385 info
[i
].prefetch_in_loop
= ((info
[i
].stride
+ PREFETCH_BLOCK
- 1)
4387 num_real_prefetches
+= info
[i
].prefetch_in_loop
;
4389 num_real_write_prefetches
+= info
[i
].prefetch_in_loop
;
4393 /* Determine how many iterations ahead to prefetch within the loop, based
4394 on how many prefetches we currently expect to do within the loop. */
4395 if (num_real_prefetches
!= 0)
4397 if ((ahead
= SIMULTANEOUS_PREFETCHES
/ num_real_prefetches
) == 0)
4399 if (loop_dump_stream
)
4400 fprintf (loop_dump_stream
,
4401 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4402 SIMULTANEOUS_PREFETCHES
, num_real_prefetches
);
4403 num_real_prefetches
= 0, num_real_write_prefetches
= 0;
4406 /* We'll also use AHEAD to determine how many prefetch instructions to
4407 emit before a loop, so don't leave it zero. */
4409 ahead
= PREFETCH_BLOCKS_BEFORE_LOOP_MAX
;
4411 for (i
= 0; i
< num_prefetches
; i
++)
4413 /* Update if we've decided not to prefetch anything within the loop. */
4414 if (num_real_prefetches
== 0)
4415 info
[i
].prefetch_in_loop
= 0;
4417 /* Find how many prefetch instructions we'll use before the loop. */
4418 if (info
[i
].prefetch_before_loop
!= 0)
4420 int n
= info
[i
].total_bytes
/ PREFETCH_BLOCK
;
4423 info
[i
].prefetch_before_loop
= n
;
4424 num_prefetches_before
+= n
;
4426 num_write_prefetches_before
+= n
;
4429 if (loop_dump_stream
)
4431 if (info
[i
].prefetch_in_loop
== 0
4432 && info
[i
].prefetch_before_loop
== 0)
4434 fprintf (loop_dump_stream
, "Prefetch insn: %d",
4435 INSN_UID (info
[i
].giv
->insn
));
4436 fprintf (loop_dump_stream
,
4437 "; in loop: %d; before: %d; %s\n",
4438 info
[i
].prefetch_in_loop
,
4439 info
[i
].prefetch_before_loop
,
4440 info
[i
].write
? "read/write" : "read only");
4441 fprintf (loop_dump_stream
,
4442 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4443 (int) (info
[i
].bytes_accessed
* 100 / info
[i
].stride
),
4444 info
[i
].bytes_accessed
, info
[i
].total_bytes
);
4445 fprintf (loop_dump_stream
, " index: " HOST_WIDE_INT_PRINT_DEC
4446 "; stride: " HOST_WIDE_INT_PRINT_DEC
"; address: ",
4447 info
[i
].index
, info
[i
].stride
);
4448 print_rtl (loop_dump_stream
, info
[i
].base_address
);
4449 fprintf (loop_dump_stream
, "\n");
4453 if (num_real_prefetches
+ num_prefetches_before
> 0)
4455 /* Record that this loop uses prefetch instructions. */
4456 LOOP_INFO (loop
)->has_prefetch
= 1;
4458 if (loop_dump_stream
)
4460 fprintf (loop_dump_stream
, "Real prefetches needed within loop: %d (write: %d)\n",
4461 num_real_prefetches
, num_real_write_prefetches
);
4462 fprintf (loop_dump_stream
, "Real prefetches needed before loop: %d (write: %d)\n",
4463 num_prefetches_before
, num_write_prefetches_before
);
4467 for (i
= 0; i
< num_prefetches
; i
++)
4471 for (y
= 0; y
< info
[i
].prefetch_in_loop
; y
++)
4473 rtx loc
= copy_rtx (*info
[i
].giv
->location
);
4475 int bytes_ahead
= PREFETCH_BLOCK
* (ahead
+ y
);
4476 rtx before_insn
= info
[i
].giv
->insn
;
4477 rtx prev_insn
= PREV_INSN (info
[i
].giv
->insn
);
4480 /* We can save some effort by offsetting the address on
4481 architectures with offsettable memory references. */
4482 if (offsettable_address_p (0, VOIDmode
, loc
))
4483 loc
= plus_constant (loc
, bytes_ahead
);
4486 rtx reg
= gen_reg_rtx (Pmode
);
4487 loop_iv_add_mult_emit_before (loop
, loc
, const1_rtx
,
4488 GEN_INT (bytes_ahead
), reg
,
4494 /* Make sure the address operand is valid for prefetch. */
4495 if (! (*insn_data
[(int)CODE_FOR_prefetch
].operand
[0].predicate
)
4496 (loc
, insn_data
[(int)CODE_FOR_prefetch
].operand
[0].mode
))
4497 loc
= force_reg (Pmode
, loc
);
4498 emit_insn (gen_prefetch (loc
, GEN_INT (info
[i
].write
),
4502 emit_insn_before (seq
, before_insn
);
4504 /* Check all insns emitted and record the new GIV
4506 insn
= NEXT_INSN (prev_insn
);
4507 while (insn
!= before_insn
)
4509 insn
= check_insn_for_givs (loop
, insn
,
4510 info
[i
].giv
->always_executed
,
4511 info
[i
].giv
->maybe_multiple
);
4512 insn
= NEXT_INSN (insn
);
4516 if (PREFETCH_BEFORE_LOOP
)
4518 /* Emit insns before the loop to fetch the first cache lines or,
4519 if we're not prefetching within the loop, everything we expect
4521 for (y
= 0; y
< info
[i
].prefetch_before_loop
; y
++)
4523 rtx reg
= gen_reg_rtx (Pmode
);
4524 rtx loop_start
= loop
->start
;
4525 rtx init_val
= info
[i
].class->initial_value
;
4526 rtx add_val
= simplify_gen_binary (PLUS
, Pmode
,
4527 info
[i
].giv
->add_val
,
4528 GEN_INT (y
* PREFETCH_BLOCK
));
4530 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4531 non-constant INIT_VAL to have the same mode as REG, which
4532 in this case we know to be Pmode. */
4533 if (GET_MODE (init_val
) != Pmode
&& !CONSTANT_P (init_val
))
4538 init_val
= convert_to_mode (Pmode
, init_val
, 0);
4541 loop_insn_emit_before (loop
, 0, loop_start
, seq
);
4543 loop_iv_add_mult_emit_before (loop
, init_val
,
4544 info
[i
].giv
->mult_val
,
4545 add_val
, reg
, 0, loop_start
);
4546 emit_insn_before (gen_prefetch (reg
, GEN_INT (info
[i
].write
),
4556 /* Communication with routines called via `note_stores'. */
4558 static rtx note_insn
;
4560 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4562 static rtx addr_placeholder
;
4564 /* ??? Unfinished optimizations, and possible future optimizations,
4565 for the strength reduction code. */
4567 /* ??? The interaction of biv elimination, and recognition of 'constant'
4568 bivs, may cause problems. */
4570 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4571 performance problems.
4573 Perhaps don't eliminate things that can be combined with an addressing
4574 mode. Find all givs that have the same biv, mult_val, and add_val;
4575 then for each giv, check to see if its only use dies in a following
4576 memory address. If so, generate a new memory address and check to see
4577 if it is valid. If it is valid, then store the modified memory address,
4578 otherwise, mark the giv as not done so that it will get its own iv. */
4580 /* ??? Could try to optimize branches when it is known that a biv is always
4583 /* ??? When replace a biv in a compare insn, we should replace with closest
4584 giv so that an optimized branch can still be recognized by the combiner,
4585 e.g. the VAX acb insn. */
4587 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4588 was rerun in loop_optimize whenever a register was added or moved.
4589 Also, some of the optimizations could be a little less conservative. */
4591 /* Searches the insns between INSN and LOOP->END. Returns 1 if there
4592 is a backward branch in that range that branches to somewhere between
4593 LOOP->START and INSN. Returns 0 otherwise. */
4595 /* ??? This is quadratic algorithm. Could be rewritten to be linear.
4596 In practice, this is not a problem, because this function is seldom called,
4597 and uses a negligible amount of CPU time on average. */
4600 back_branch_in_range_p (const struct loop
*loop
, rtx insn
)
4602 rtx p
, q
, target_insn
;
4603 rtx loop_start
= loop
->start
;
4604 rtx loop_end
= loop
->end
;
4605 rtx orig_loop_end
= loop
->end
;
4607 /* Stop before we get to the backward branch at the end of the loop. */
4608 loop_end
= prev_nonnote_insn (loop_end
);
4609 if (BARRIER_P (loop_end
))
4610 loop_end
= PREV_INSN (loop_end
);
4612 /* Check in case insn has been deleted, search forward for first non
4613 deleted insn following it. */
4614 while (INSN_DELETED_P (insn
))
4615 insn
= NEXT_INSN (insn
);
4617 /* Check for the case where insn is the last insn in the loop. Deal
4618 with the case where INSN was a deleted loop test insn, in which case
4619 it will now be the NOTE_LOOP_END. */
4620 if (insn
== loop_end
|| insn
== orig_loop_end
)
4623 for (p
= NEXT_INSN (insn
); p
!= loop_end
; p
= NEXT_INSN (p
))
4627 target_insn
= JUMP_LABEL (p
);
4629 /* Search from loop_start to insn, to see if one of them is
4630 the target_insn. We can't use INSN_LUID comparisons here,
4631 since insn may not have an LUID entry. */
4632 for (q
= loop_start
; q
!= insn
; q
= NEXT_INSN (q
))
4633 if (q
== target_insn
)
4641 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4642 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4645 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4646 least once for every loop iteration except for the last one.
4648 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4651 typedef rtx (*loop_insn_callback
) (struct loop
*, rtx
, int, int);
4653 for_each_insn_in_loop (struct loop
*loop
, loop_insn_callback fncall
)
4655 int not_every_iteration
= 0;
4656 int maybe_multiple
= 0;
4657 int past_loop_latch
= 0;
4660 /* If loop_scan_start points to the loop exit test, we have to be wary of
4661 subversive use of gotos inside expression statements. */
4662 if (prev_nonnote_insn (loop
->scan_start
) != prev_nonnote_insn (loop
->start
))
4663 maybe_multiple
= back_branch_in_range_p (loop
, loop
->scan_start
);
4665 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4666 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
4668 p
= next_insn_in_loop (loop
, p
))
4670 p
= fncall (loop
, p
, not_every_iteration
, maybe_multiple
);
4672 /* Past CODE_LABEL, we get to insns that may be executed multiple
4673 times. The only way we can be sure that they can't is if every
4674 jump insn between here and the end of the loop either
4675 returns, exits the loop, is a jump to a location that is still
4676 behind the label, or is a jump to the loop start. */
4686 insn
= NEXT_INSN (insn
);
4687 if (insn
== loop
->scan_start
)
4689 if (insn
== loop
->end
)
4695 if (insn
== loop
->scan_start
)
4700 && GET_CODE (PATTERN (insn
)) != RETURN
4701 && (!any_condjump_p (insn
)
4702 || (JUMP_LABEL (insn
) != 0
4703 && JUMP_LABEL (insn
) != loop
->scan_start
4704 && !loop_insn_first_p (p
, JUMP_LABEL (insn
)))))
4712 /* Past a jump, we get to insns for which we can't count
4713 on whether they will be executed during each iteration. */
4714 /* This code appears twice in strength_reduce. There is also similar
4715 code in scan_loop. */
4717 /* If we enter the loop in the middle, and scan around to the
4718 beginning, don't set not_every_iteration for that.
4719 This can be any kind of jump, since we want to know if insns
4720 will be executed if the loop is executed. */
4721 && !(JUMP_LABEL (p
) == loop
->top
4722 && ((NEXT_INSN (NEXT_INSN (p
)) == loop
->end
4723 && any_uncondjump_p (p
))
4724 || (NEXT_INSN (p
) == loop
->end
&& any_condjump_p (p
)))))
4728 /* If this is a jump outside the loop, then it also doesn't
4729 matter. Check to see if the target of this branch is on the
4730 loop->exits_labels list. */
4732 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
4733 if (XEXP (label
, 0) == JUMP_LABEL (p
))
4737 not_every_iteration
= 1;
4740 /* Note if we pass a loop latch. If we do, then we can not clear
4741 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4742 a loop since a jump before the last CODE_LABEL may have started
4743 a new loop iteration.
4745 Note that LOOP_TOP is only set for rotated loops and we need
4746 this check for all loops, so compare against the CODE_LABEL
4747 which immediately follows LOOP_START. */
4749 && JUMP_LABEL (p
) == NEXT_INSN (loop
->start
))
4750 past_loop_latch
= 1;
4752 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4753 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4754 or not an insn is known to be executed each iteration of the
4755 loop, whether or not any iterations are known to occur.
4757 Therefore, if we have just passed a label and have no more labels
4758 between here and the test insn of the loop, and we have not passed
4759 a jump to the top of the loop, then we know these insns will be
4760 executed each iteration. */
4762 if (not_every_iteration
4765 && no_labels_between_p (p
, loop
->end
))
4766 not_every_iteration
= 0;
4771 loop_bivs_find (struct loop
*loop
)
4773 struct loop_regs
*regs
= LOOP_REGS (loop
);
4774 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4775 /* Temporary list pointers for traversing ivs->list. */
4776 struct iv_class
*bl
, **backbl
;
4780 for_each_insn_in_loop (loop
, check_insn_for_bivs
);
4782 /* Scan ivs->list to remove all regs that proved not to be bivs.
4783 Make a sanity check against regs->n_times_set. */
4784 for (backbl
= &ivs
->list
, bl
= *backbl
; bl
; bl
= bl
->next
)
4786 if (REG_IV_TYPE (ivs
, bl
->regno
) != BASIC_INDUCT
4787 /* Above happens if register modified by subreg, etc. */
4788 /* Make sure it is not recognized as a basic induction var: */
4789 || regs
->array
[bl
->regno
].n_times_set
!= bl
->biv_count
4790 /* If never incremented, it is invariant that we decided not to
4791 move. So leave it alone. */
4792 || ! bl
->incremented
)
4794 if (loop_dump_stream
)
4795 fprintf (loop_dump_stream
, "Biv %d: discarded, %s\n",
4797 (REG_IV_TYPE (ivs
, bl
->regno
) != BASIC_INDUCT
4798 ? "not induction variable"
4799 : (! bl
->incremented
? "never incremented"
4802 REG_IV_TYPE (ivs
, bl
->regno
) = NOT_BASIC_INDUCT
;
4809 if (loop_dump_stream
)
4810 fprintf (loop_dump_stream
, "Biv %d: verified\n", bl
->regno
);
4816 /* Determine how BIVS are initialized by looking through pre-header
4817 extended basic block. */
4819 loop_bivs_init_find (struct loop
*loop
)
4821 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4822 /* Temporary list pointers for traversing ivs->list. */
4823 struct iv_class
*bl
;
4827 /* Find initial value for each biv by searching backwards from loop_start,
4828 halting at first label. Also record any test condition. */
4831 for (p
= loop
->start
; p
&& !LABEL_P (p
); p
= PREV_INSN (p
))
4841 note_stores (PATTERN (p
), record_initial
, ivs
);
4843 /* Record any test of a biv that branches around the loop if no store
4844 between it and the start of loop. We only care about tests with
4845 constants and registers and only certain of those. */
4847 && JUMP_LABEL (p
) != 0
4848 && next_real_insn (JUMP_LABEL (p
)) == next_real_insn (loop
->end
)
4849 && (test
= get_condition_for_loop (loop
, p
)) != 0
4850 && REG_P (XEXP (test
, 0))
4851 && REGNO (XEXP (test
, 0)) < max_reg_before_loop
4852 && (bl
= REG_IV_CLASS (ivs
, REGNO (XEXP (test
, 0)))) != 0
4853 && valid_initial_value_p (XEXP (test
, 1), p
, call_seen
, loop
->start
)
4854 && bl
->init_insn
== 0)
4856 /* If an NE test, we have an initial value! */
4857 if (GET_CODE (test
) == NE
)
4860 bl
->init_set
= gen_rtx_SET (VOIDmode
,
4861 XEXP (test
, 0), XEXP (test
, 1));
4864 bl
->initial_test
= test
;
4870 /* Look at the each biv and see if we can say anything better about its
4871 initial value from any initializing insns set up above. (This is done
4872 in two passes to avoid missing SETs in a PARALLEL.) */
4874 loop_bivs_check (struct loop
*loop
)
4876 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4877 /* Temporary list pointers for traversing ivs->list. */
4878 struct iv_class
*bl
;
4879 struct iv_class
**backbl
;
4881 for (backbl
= &ivs
->list
; (bl
= *backbl
); backbl
= &bl
->next
)
4886 if (! bl
->init_insn
)
4889 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4890 is a constant, use the value of that. */
4891 if (((note
= find_reg_note (bl
->init_insn
, REG_EQUAL
, 0)) != NULL
4892 && CONSTANT_P (XEXP (note
, 0)))
4893 || ((note
= find_reg_note (bl
->init_insn
, REG_EQUIV
, 0)) != NULL
4894 && CONSTANT_P (XEXP (note
, 0))))
4895 src
= XEXP (note
, 0);
4897 src
= SET_SRC (bl
->init_set
);
4899 if (loop_dump_stream
)
4900 fprintf (loop_dump_stream
,
4901 "Biv %d: initialized at insn %d: initial value ",
4902 bl
->regno
, INSN_UID (bl
->init_insn
));
4904 if ((GET_MODE (src
) == GET_MODE (regno_reg_rtx
[bl
->regno
])
4905 || GET_MODE (src
) == VOIDmode
)
4906 && valid_initial_value_p (src
, bl
->init_insn
,
4907 LOOP_INFO (loop
)->pre_header_has_call
,
4910 bl
->initial_value
= src
;
4912 if (loop_dump_stream
)
4914 print_simple_rtl (loop_dump_stream
, src
);
4915 fputc ('\n', loop_dump_stream
);
4918 /* If we can't make it a giv,
4919 let biv keep initial value of "itself". */
4920 else if (loop_dump_stream
)
4921 fprintf (loop_dump_stream
, "is complex\n");
4926 /* Search the loop for general induction variables. */
4929 loop_givs_find (struct loop
* loop
)
4931 for_each_insn_in_loop (loop
, check_insn_for_givs
);
4935 /* For each giv for which we still don't know whether or not it is
4936 replaceable, check to see if it is replaceable because its final value
4937 can be calculated. */
4940 loop_givs_check (struct loop
*loop
)
4942 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4943 struct iv_class
*bl
;
4945 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
4947 struct induction
*v
;
4949 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4950 if (! v
->replaceable
&& ! v
->not_replaceable
)
4951 check_final_value (loop
, v
);
4955 /* Try to generate the simplest rtx for the expression
4956 (PLUS (MULT mult1 mult2) add1). This is used to calculate the initial
4960 fold_rtx_mult_add (rtx mult1
, rtx mult2
, rtx add1
, enum machine_mode mode
)
4965 /* The modes must all be the same. This should always be true. For now,
4966 check to make sure. */
4967 if ((GET_MODE (mult1
) != mode
&& GET_MODE (mult1
) != VOIDmode
)
4968 || (GET_MODE (mult2
) != mode
&& GET_MODE (mult2
) != VOIDmode
)
4969 || (GET_MODE (add1
) != mode
&& GET_MODE (add1
) != VOIDmode
))
4972 /* Ensure that if at least one of mult1/mult2 are constant, then mult2
4973 will be a constant. */
4974 if (GET_CODE (mult1
) == CONST_INT
)
4981 mult_res
= simplify_binary_operation (MULT
, mode
, mult1
, mult2
);
4983 mult_res
= gen_rtx_MULT (mode
, mult1
, mult2
);
4985 /* Again, put the constant second. */
4986 if (GET_CODE (add1
) == CONST_INT
)
4993 result
= simplify_binary_operation (PLUS
, mode
, add1
, mult_res
);
4995 result
= gen_rtx_PLUS (mode
, add1
, mult_res
);
5000 /* Searches the list of induction struct's for the biv BL, to try to calculate
5001 the total increment value for one iteration of the loop as a constant.
5003 Returns the increment value as an rtx, simplified as much as possible,
5004 if it can be calculated. Otherwise, returns 0. */
5007 biv_total_increment (const struct iv_class
*bl
)
5009 struct induction
*v
;
5012 /* For increment, must check every instruction that sets it. Each
5013 instruction must be executed only once each time through the loop.
5014 To verify this, we check that the insn is always executed, and that
5015 there are no backward branches after the insn that branch to before it.
5016 Also, the insn must have a mult_val of one (to make sure it really is
5019 result
= const0_rtx
;
5020 for (v
= bl
->biv
; v
; v
= v
->next_iv
)
5022 if (v
->always_computable
&& v
->mult_val
== const1_rtx
5023 && ! v
->maybe_multiple
5024 && SCALAR_INT_MODE_P (v
->mode
))
5026 /* If we have already counted it, skip it. */
5030 result
= fold_rtx_mult_add (result
, const1_rtx
, v
->add_val
, v
->mode
);
5039 /* Try to prove that the register is dead after the loop exits. Trace every
5040 loop exit looking for an insn that will always be executed, which sets
5041 the register to some value, and appears before the first use of the register
5042 is found. If successful, then return 1, otherwise return 0. */
5044 /* ?? Could be made more intelligent in the handling of jumps, so that
5045 it can search past if statements and other similar structures. */
5048 reg_dead_after_loop (const struct loop
*loop
, rtx reg
)
5052 int label_count
= 0;
5054 /* In addition to checking all exits of this loop, we must also check
5055 all exits of inner nested loops that would exit this loop. We don't
5056 have any way to identify those, so we just give up if there are any
5057 such inner loop exits. */
5059 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
5062 if (label_count
!= loop
->exit_count
)
5065 /* HACK: Must also search the loop fall through exit, create a label_ref
5066 here which points to the loop->end, and append the loop_number_exit_labels
5068 label
= gen_rtx_LABEL_REF (VOIDmode
, loop
->end
);
5069 LABEL_NEXTREF (label
) = loop
->exit_labels
;
5071 for (; label
; label
= LABEL_NEXTREF (label
))
5073 /* Succeed if find an insn which sets the biv or if reach end of
5074 function. Fail if find an insn that uses the biv, or if come to
5075 a conditional jump. */
5077 insn
= NEXT_INSN (XEXP (label
, 0));
5084 if (reg_referenced_p (reg
, PATTERN (insn
)))
5087 note
= find_reg_equal_equiv_note (insn
);
5088 if (note
&& reg_overlap_mentioned_p (reg
, XEXP (note
, 0)))
5091 set
= single_set (insn
);
5092 if (set
&& rtx_equal_p (SET_DEST (set
), reg
))
5097 if (GET_CODE (PATTERN (insn
)) == RETURN
)
5099 else if (!any_uncondjump_p (insn
)
5100 /* Prevent infinite loop following infinite loops. */
5101 || jump_count
++ > 20)
5104 insn
= JUMP_LABEL (insn
);
5108 insn
= NEXT_INSN (insn
);
5112 /* Success, the register is dead on all loop exits. */
5116 /* Try to calculate the final value of the biv, the value it will have at
5117 the end of the loop. If we can do it, return that value. */
5120 final_biv_value (const struct loop
*loop
, struct iv_class
*bl
)
5122 unsigned HOST_WIDE_INT n_iterations
= LOOP_INFO (loop
)->n_iterations
;
5125 /* ??? This only works for MODE_INT biv's. Reject all others for now. */
5127 if (GET_MODE_CLASS (bl
->biv
->mode
) != MODE_INT
)
5130 /* The final value for reversed bivs must be calculated differently than
5131 for ordinary bivs. In this case, there is already an insn after the
5132 loop which sets this biv's final value (if necessary), and there are
5133 no other loop exits, so we can return any value. */
5136 if (loop_dump_stream
)
5137 fprintf (loop_dump_stream
,
5138 "Final biv value for %d, reversed biv.\n", bl
->regno
);
5143 /* Try to calculate the final value as initial value + (number of iterations
5144 * increment). For this to work, increment must be invariant, the only
5145 exit from the loop must be the fall through at the bottom (otherwise
5146 it may not have its final value when the loop exits), and the initial
5147 value of the biv must be invariant. */
5149 if (n_iterations
!= 0
5150 && ! loop
->exit_count
5151 && loop_invariant_p (loop
, bl
->initial_value
))
5153 increment
= biv_total_increment (bl
);
5155 if (increment
&& loop_invariant_p (loop
, increment
))
5157 /* Can calculate the loop exit value, emit insns after loop
5158 end to calculate this value into a temporary register in
5159 case it is needed later. */
5161 tem
= gen_reg_rtx (bl
->biv
->mode
);
5162 record_base_value (REGNO (tem
), bl
->biv
->add_val
, 0);
5163 loop_iv_add_mult_sink (loop
, increment
, GEN_INT (n_iterations
),
5164 bl
->initial_value
, tem
);
5166 if (loop_dump_stream
)
5167 fprintf (loop_dump_stream
,
5168 "Final biv value for %d, calculated.\n", bl
->regno
);
5174 /* Check to see if the biv is dead at all loop exits. */
5175 if (reg_dead_after_loop (loop
, bl
->biv
->src_reg
))
5177 if (loop_dump_stream
)
5178 fprintf (loop_dump_stream
,
5179 "Final biv value for %d, biv dead after loop exit.\n",
5188 /* Return nonzero if it is possible to eliminate the biv BL provided
5189 all givs are reduced. This is possible if either the reg is not
5190 used outside the loop, or we can compute what its final value will
5194 loop_biv_eliminable_p (struct loop
*loop
, struct iv_class
*bl
,
5195 int threshold
, int insn_count
)
5197 /* For architectures with a decrement_and_branch_until_zero insn,
5198 don't do this if we put a REG_NONNEG note on the endtest for this
5201 #ifdef HAVE_decrement_and_branch_until_zero
5204 if (loop_dump_stream
)
5205 fprintf (loop_dump_stream
,
5206 "Cannot eliminate nonneg biv %d.\n", bl
->regno
);
5211 /* Check that biv is used outside loop or if it has a final value.
5212 Compare against bl->init_insn rather than loop->start. We aren't
5213 concerned with any uses of the biv between init_insn and
5214 loop->start since these won't be affected by the value of the biv
5215 elsewhere in the function, so long as init_insn doesn't use the
5218 if ((REGNO_LAST_LUID (bl
->regno
) < INSN_LUID (loop
->end
)
5220 && INSN_UID (bl
->init_insn
) < max_uid_for_loop
5221 && REGNO_FIRST_LUID (bl
->regno
) >= INSN_LUID (bl
->init_insn
)
5222 && ! reg_mentioned_p (bl
->biv
->dest_reg
, SET_SRC (bl
->init_set
)))
5223 || (bl
->final_value
= final_biv_value (loop
, bl
)))
5224 return maybe_eliminate_biv (loop
, bl
, 0, threshold
, insn_count
);
5226 if (loop_dump_stream
)
5228 fprintf (loop_dump_stream
,
5229 "Cannot eliminate biv %d.\n",
5231 fprintf (loop_dump_stream
,
5232 "First use: insn %d, last use: insn %d.\n",
5233 REGNO_FIRST_UID (bl
->regno
),
5234 REGNO_LAST_UID (bl
->regno
));
5240 /* Reduce each giv of BL that we have decided to reduce. */
5243 loop_givs_reduce (struct loop
*loop
, struct iv_class
*bl
)
5245 struct induction
*v
;
5247 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5249 struct induction
*tv
;
5250 if (! v
->ignore
&& v
->same
== 0)
5252 int auto_inc_opt
= 0;
5254 /* If the code for derived givs immediately below has already
5255 allocated a new_reg, we must keep it. */
5257 v
->new_reg
= gen_reg_rtx (v
->mode
);
5260 /* If the target has auto-increment addressing modes, and
5261 this is an address giv, then try to put the increment
5262 immediately after its use, so that flow can create an
5263 auto-increment addressing mode. */
5264 /* Don't do this for loops entered at the bottom, to avoid
5265 this invalid transformation:
5274 if (v
->giv_type
== DEST_ADDR
&& bl
->biv_count
== 1
5275 && bl
->biv
->always_executed
&& ! bl
->biv
->maybe_multiple
5276 /* We don't handle reversed biv's because bl->biv->insn
5277 does not have a valid INSN_LUID. */
5279 && v
->always_executed
&& ! v
->maybe_multiple
5280 && INSN_UID (v
->insn
) < max_uid_for_loop
5283 /* If other giv's have been combined with this one, then
5284 this will work only if all uses of the other giv's occur
5285 before this giv's insn. This is difficult to check.
5287 We simplify this by looking for the common case where
5288 there is one DEST_REG giv, and this giv's insn is the
5289 last use of the dest_reg of that DEST_REG giv. If the
5290 increment occurs after the address giv, then we can
5291 perform the optimization. (Otherwise, the increment
5292 would have to go before other_giv, and we would not be
5293 able to combine it with the address giv to get an
5294 auto-inc address.) */
5295 if (v
->combined_with
)
5297 struct induction
*other_giv
= 0;
5299 for (tv
= bl
->giv
; tv
; tv
= tv
->next_iv
)
5307 if (! tv
&& other_giv
5308 && REGNO (other_giv
->dest_reg
) < max_reg_before_loop
5309 && (REGNO_LAST_UID (REGNO (other_giv
->dest_reg
))
5310 == INSN_UID (v
->insn
))
5311 && INSN_LUID (v
->insn
) < INSN_LUID (bl
->biv
->insn
))
5314 /* Check for case where increment is before the address
5315 giv. Do this test in "loop order". */
5316 else if ((INSN_LUID (v
->insn
) > INSN_LUID (bl
->biv
->insn
)
5317 && (INSN_LUID (v
->insn
) < INSN_LUID (loop
->scan_start
)
5318 || (INSN_LUID (bl
->biv
->insn
)
5319 > INSN_LUID (loop
->scan_start
))))
5320 || (INSN_LUID (v
->insn
) < INSN_LUID (loop
->scan_start
)
5321 && (INSN_LUID (loop
->scan_start
)
5322 < INSN_LUID (bl
->biv
->insn
))))
5331 /* We can't put an insn immediately after one setting
5332 cc0, or immediately before one using cc0. */
5333 if ((auto_inc_opt
== 1 && sets_cc0_p (PATTERN (v
->insn
)))
5334 || (auto_inc_opt
== -1
5335 && (prev
= prev_nonnote_insn (v
->insn
)) != 0
5337 && sets_cc0_p (PATTERN (prev
))))
5343 v
->auto_inc_opt
= 1;
5347 /* For each place where the biv is incremented, add an insn
5348 to increment the new, reduced reg for the giv. */
5349 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
5353 /* Skip if location is the same as a previous one. */
5357 insert_before
= NEXT_INSN (tv
->insn
);
5358 else if (auto_inc_opt
== 1)
5359 insert_before
= NEXT_INSN (v
->insn
);
5361 insert_before
= v
->insn
;
5363 if (tv
->mult_val
== const1_rtx
)
5364 loop_iv_add_mult_emit_before (loop
, tv
->add_val
, v
->mult_val
,
5365 v
->new_reg
, v
->new_reg
,
5367 else /* tv->mult_val == const0_rtx */
5368 /* A multiply is acceptable here
5369 since this is presumed to be seldom executed. */
5370 loop_iv_add_mult_emit_before (loop
, tv
->add_val
, v
->mult_val
,
5371 v
->add_val
, v
->new_reg
,
5375 /* Add code at loop start to initialize giv's reduced reg. */
5377 loop_iv_add_mult_hoist (loop
,
5378 extend_value_for_giv (v
, bl
->initial_value
),
5379 v
->mult_val
, v
->add_val
, v
->new_reg
);
5385 /* Check for givs whose first use is their definition and whose
5386 last use is the definition of another giv. If so, it is likely
5387 dead and should not be used to derive another giv nor to
5391 loop_givs_dead_check (struct loop
*loop ATTRIBUTE_UNUSED
, struct iv_class
*bl
)
5393 struct induction
*v
;
5395 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5398 || (v
->same
&& v
->same
->ignore
))
5401 if (v
->giv_type
== DEST_REG
5402 && REGNO_FIRST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v
->insn
))
5404 struct induction
*v1
;
5406 for (v1
= bl
->giv
; v1
; v1
= v1
->next_iv
)
5407 if (REGNO_LAST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v1
->insn
))
5415 loop_givs_rescan (struct loop
*loop
, struct iv_class
*bl
, rtx
*reg_map
)
5417 struct induction
*v
;
5419 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5421 if (v
->same
&& v
->same
->ignore
)
5427 /* Update expression if this was combined, in case other giv was
5430 v
->new_reg
= replace_rtx (v
->new_reg
,
5431 v
->same
->dest_reg
, v
->same
->new_reg
);
5433 /* See if this register is known to be a pointer to something. If
5434 so, see if we can find the alignment. First see if there is a
5435 destination register that is a pointer. If so, this shares the
5436 alignment too. Next see if we can deduce anything from the
5437 computational information. If not, and this is a DEST_ADDR
5438 giv, at least we know that it's a pointer, though we don't know
5440 if (REG_P (v
->new_reg
)
5441 && v
->giv_type
== DEST_REG
5442 && REG_POINTER (v
->dest_reg
))
5443 mark_reg_pointer (v
->new_reg
,
5444 REGNO_POINTER_ALIGN (REGNO (v
->dest_reg
)));
5445 else if (REG_P (v
->new_reg
)
5446 && REG_POINTER (v
->src_reg
))
5448 unsigned int align
= REGNO_POINTER_ALIGN (REGNO (v
->src_reg
));
5451 || GET_CODE (v
->add_val
) != CONST_INT
5452 || INTVAL (v
->add_val
) % (align
/ BITS_PER_UNIT
) != 0)
5455 mark_reg_pointer (v
->new_reg
, align
);
5457 else if (REG_P (v
->new_reg
)
5458 && REG_P (v
->add_val
)
5459 && REG_POINTER (v
->add_val
))
5461 unsigned int align
= REGNO_POINTER_ALIGN (REGNO (v
->add_val
));
5463 if (align
== 0 || GET_CODE (v
->mult_val
) != CONST_INT
5464 || INTVAL (v
->mult_val
) % (align
/ BITS_PER_UNIT
) != 0)
5467 mark_reg_pointer (v
->new_reg
, align
);
5469 else if (REG_P (v
->new_reg
) && v
->giv_type
== DEST_ADDR
)
5470 mark_reg_pointer (v
->new_reg
, 0);
5472 if (v
->giv_type
== DEST_ADDR
)
5473 /* Store reduced reg as the address in the memref where we found
5475 validate_change (v
->insn
, v
->location
, v
->new_reg
, 0);
5476 else if (v
->replaceable
)
5478 reg_map
[REGNO (v
->dest_reg
)] = v
->new_reg
;
5482 rtx original_insn
= v
->insn
;
5485 /* Not replaceable; emit an insn to set the original giv reg from
5486 the reduced giv, same as above. */
5487 v
->insn
= loop_insn_emit_after (loop
, 0, original_insn
,
5488 gen_move_insn (v
->dest_reg
,
5491 /* The original insn may have a REG_EQUAL note. This note is
5492 now incorrect and may result in invalid substitutions later.
5493 The original insn is dead, but may be part of a libcall
5494 sequence, which doesn't seem worth the bother of handling. */
5495 note
= find_reg_note (original_insn
, REG_EQUAL
, NULL_RTX
);
5497 remove_note (original_insn
, note
);
5500 /* When a loop is reversed, givs which depend on the reversed
5501 biv, and which are live outside the loop, must be set to their
5502 correct final value. This insn is only needed if the giv is
5503 not replaceable. The correct final value is the same as the
5504 value that the giv starts the reversed loop with. */
5505 if (bl
->reversed
&& ! v
->replaceable
)
5506 loop_iv_add_mult_sink (loop
,
5507 extend_value_for_giv (v
, bl
->initial_value
),
5508 v
->mult_val
, v
->add_val
, v
->dest_reg
);
5509 else if (v
->final_value
)
5510 loop_insn_sink_or_swim (loop
,
5511 gen_load_of_final_value (v
->dest_reg
,
5514 if (loop_dump_stream
)
5516 fprintf (loop_dump_stream
, "giv at %d reduced to ",
5517 INSN_UID (v
->insn
));
5518 print_simple_rtl (loop_dump_stream
, v
->new_reg
);
5519 fprintf (loop_dump_stream
, "\n");
5526 loop_giv_reduce_benefit (struct loop
*loop ATTRIBUTE_UNUSED
,
5527 struct iv_class
*bl
, struct induction
*v
,
5533 benefit
= v
->benefit
;
5534 PUT_MODE (test_reg
, v
->mode
);
5535 add_cost
= iv_add_mult_cost (bl
->biv
->add_val
, v
->mult_val
,
5536 test_reg
, test_reg
);
5538 /* Reduce benefit if not replaceable, since we will insert a
5539 move-insn to replace the insn that calculates this giv. Don't do
5540 this unless the giv is a user variable, since it will often be
5541 marked non-replaceable because of the duplication of the exit
5542 code outside the loop. In such a case, the copies we insert are
5543 dead and will be deleted. So they don't have a cost. Similar
5544 situations exist. */
5545 /* ??? The new final_[bg]iv_value code does a much better job of
5546 finding replaceable giv's, and hence this code may no longer be
5548 if (! v
->replaceable
&& ! bl
->eliminable
5549 && REG_USERVAR_P (v
->dest_reg
))
5550 benefit
-= copy_cost
;
5552 /* Decrease the benefit to count the add-insns that we will insert
5553 to increment the reduced reg for the giv. ??? This can
5554 overestimate the run-time cost of the additional insns, e.g. if
5555 there are multiple basic blocks that increment the biv, but only
5556 one of these blocks is executed during each iteration. There is
5557 no good way to detect cases like this with the current structure
5558 of the loop optimizer. This code is more accurate for
5559 determining code size than run-time benefits. */
5560 benefit
-= add_cost
* bl
->biv_count
;
5562 /* Decide whether to strength-reduce this giv or to leave the code
5563 unchanged (recompute it from the biv each time it is used). This
5564 decision can be made independently for each giv. */
5567 /* Attempt to guess whether autoincrement will handle some of the
5568 new add insns; if so, increase BENEFIT (undo the subtraction of
5569 add_cost that was done above). */
5570 if (v
->giv_type
== DEST_ADDR
5571 /* Increasing the benefit is risky, since this is only a guess.
5572 Avoid increasing register pressure in cases where there would
5573 be no other benefit from reducing this giv. */
5575 && GET_CODE (v
->mult_val
) == CONST_INT
)
5577 int size
= GET_MODE_SIZE (GET_MODE (v
->mem
));
5579 if (HAVE_POST_INCREMENT
5580 && INTVAL (v
->mult_val
) == size
)
5581 benefit
+= add_cost
* bl
->biv_count
;
5582 else if (HAVE_PRE_INCREMENT
5583 && INTVAL (v
->mult_val
) == size
)
5584 benefit
+= add_cost
* bl
->biv_count
;
5585 else if (HAVE_POST_DECREMENT
5586 && -INTVAL (v
->mult_val
) == size
)
5587 benefit
+= add_cost
* bl
->biv_count
;
5588 else if (HAVE_PRE_DECREMENT
5589 && -INTVAL (v
->mult_val
) == size
)
5590 benefit
+= add_cost
* bl
->biv_count
;
5598 /* Free IV structures for LOOP. */
5601 loop_ivs_free (struct loop
*loop
)
5603 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5604 struct iv_class
*iv
= ivs
->list
;
5610 struct iv_class
*next
= iv
->next
;
5611 struct induction
*induction
;
5612 struct induction
*next_induction
;
5614 for (induction
= iv
->biv
; induction
; induction
= next_induction
)
5616 next_induction
= induction
->next_iv
;
5619 for (induction
= iv
->giv
; induction
; induction
= next_induction
)
5621 next_induction
= induction
->next_iv
;
5630 /* Look back before LOOP->START for the insn that sets REG and return
5631 the equivalent constant if there is a REG_EQUAL note otherwise just
5632 the SET_SRC of REG. */
5635 loop_find_equiv_value (const struct loop
*loop
, rtx reg
)
5637 rtx loop_start
= loop
->start
;
5642 for (insn
= PREV_INSN (loop_start
); insn
; insn
= PREV_INSN (insn
))
5647 else if (INSN_P (insn
) && reg_set_p (reg
, insn
))
5649 /* We found the last insn before the loop that sets the register.
5650 If it sets the entire register, and has a REG_EQUAL note,
5651 then use the value of the REG_EQUAL note. */
5652 if ((set
= single_set (insn
))
5653 && (SET_DEST (set
) == reg
))
5655 rtx note
= find_reg_note (insn
, REG_EQUAL
, NULL_RTX
);
5657 /* Only use the REG_EQUAL note if it is a constant.
5658 Other things, divide in particular, will cause
5659 problems later if we use them. */
5660 if (note
&& GET_CODE (XEXP (note
, 0)) != EXPR_LIST
5661 && CONSTANT_P (XEXP (note
, 0)))
5662 ret
= XEXP (note
, 0);
5664 ret
= SET_SRC (set
);
5666 /* We cannot do this if it changes between the
5667 assignment and loop start though. */
5668 if (modified_between_p (ret
, insn
, loop_start
))
5677 /* Find and return register term common to both expressions OP0 and
5678 OP1 or NULL_RTX if no such term exists. Each expression must be a
5679 REG or a PLUS of a REG. */
5682 find_common_reg_term (rtx op0
, rtx op1
)
5684 if ((REG_P (op0
) || GET_CODE (op0
) == PLUS
)
5685 && (REG_P (op1
) || GET_CODE (op1
) == PLUS
))
5692 if (GET_CODE (op0
) == PLUS
)
5693 op01
= XEXP (op0
, 1), op00
= XEXP (op0
, 0);
5695 op01
= const0_rtx
, op00
= op0
;
5697 if (GET_CODE (op1
) == PLUS
)
5698 op11
= XEXP (op1
, 1), op10
= XEXP (op1
, 0);
5700 op11
= const0_rtx
, op10
= op1
;
5702 /* Find and return common register term if present. */
5703 if (REG_P (op00
) && (op00
== op10
|| op00
== op11
))
5705 else if (REG_P (op01
) && (op01
== op10
|| op01
== op11
))
5709 /* No common register term found. */
5713 /* Determine the loop iterator and calculate the number of loop
5714 iterations. Returns the exact number of loop iterations if it can
5715 be calculated, otherwise returns zero. */
5717 static unsigned HOST_WIDE_INT
5718 loop_iterations (struct loop
*loop
)
5720 struct loop_info
*loop_info
= LOOP_INFO (loop
);
5721 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5722 rtx comparison
, comparison_value
;
5723 rtx iteration_var
, initial_value
, increment
, final_value
;
5724 enum rtx_code comparison_code
;
5726 unsigned HOST_WIDE_INT abs_inc
;
5727 unsigned HOST_WIDE_INT abs_diff
;
5730 int unsigned_p
, compare_dir
, final_larger
;
5732 struct iv_class
*bl
;
5734 loop_info
->n_iterations
= 0;
5735 loop_info
->initial_value
= 0;
5736 loop_info
->initial_equiv_value
= 0;
5737 loop_info
->comparison_value
= 0;
5738 loop_info
->final_value
= 0;
5739 loop_info
->final_equiv_value
= 0;
5740 loop_info
->increment
= 0;
5741 loop_info
->iteration_var
= 0;
5744 /* We used to use prev_nonnote_insn here, but that fails because it might
5745 accidentally get the branch for a contained loop if the branch for this
5746 loop was deleted. We can only trust branches immediately before the
5748 last_loop_insn
= PREV_INSN (loop
->end
);
5750 /* ??? We should probably try harder to find the jump insn
5751 at the end of the loop. The following code assumes that
5752 the last loop insn is a jump to the top of the loop. */
5753 if (!JUMP_P (last_loop_insn
))
5755 if (loop_dump_stream
)
5756 fprintf (loop_dump_stream
,
5757 "Loop iterations: No final conditional branch found.\n");
5761 /* If there is a more than a single jump to the top of the loop
5762 we cannot (easily) determine the iteration count. */
5763 if (LABEL_NUSES (JUMP_LABEL (last_loop_insn
)) > 1)
5765 if (loop_dump_stream
)
5766 fprintf (loop_dump_stream
,
5767 "Loop iterations: Loop has multiple back edges.\n");
5771 /* Find the iteration variable. If the last insn is a conditional
5772 branch, and the insn before tests a register value, make that the
5773 iteration variable. */
5775 comparison
= get_condition_for_loop (loop
, last_loop_insn
);
5776 if (comparison
== 0)
5778 if (loop_dump_stream
)
5779 fprintf (loop_dump_stream
,
5780 "Loop iterations: No final comparison found.\n");
5784 /* ??? Get_condition may switch position of induction variable and
5785 invariant register when it canonicalizes the comparison. */
5787 comparison_code
= GET_CODE (comparison
);
5788 iteration_var
= XEXP (comparison
, 0);
5789 comparison_value
= XEXP (comparison
, 1);
5791 if (!REG_P (iteration_var
))
5793 if (loop_dump_stream
)
5794 fprintf (loop_dump_stream
,
5795 "Loop iterations: Comparison not against register.\n");
5799 /* The only new registers that are created before loop iterations
5800 are givs made from biv increments or registers created by
5801 load_mems. In the latter case, it is possible that try_copy_prop
5802 will propagate a new pseudo into the old iteration register but
5803 this will be marked by having the REG_USERVAR_P bit set. */
5805 if ((unsigned) REGNO (iteration_var
) >= ivs
->n_regs
5806 && ! REG_USERVAR_P (iteration_var
))
5809 /* Determine the initial value of the iteration variable, and the amount
5810 that it is incremented each loop. Use the tables constructed by
5811 the strength reduction pass to calculate these values. */
5813 /* Clear the result values, in case no answer can be found. */
5817 /* The iteration variable can be either a giv or a biv. Check to see
5818 which it is, and compute the variable's initial value, and increment
5819 value if possible. */
5821 /* If this is a new register, can't handle it since we don't have any
5822 reg_iv_type entry for it. */
5823 if ((unsigned) REGNO (iteration_var
) >= ivs
->n_regs
)
5825 if (loop_dump_stream
)
5826 fprintf (loop_dump_stream
,
5827 "Loop iterations: No reg_iv_type entry for iteration var.\n");
5831 /* Reject iteration variables larger than the host wide int size, since they
5832 could result in a number of iterations greater than the range of our
5833 `unsigned HOST_WIDE_INT' variable loop_info->n_iterations. */
5834 else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var
))
5835 > HOST_BITS_PER_WIDE_INT
))
5837 if (loop_dump_stream
)
5838 fprintf (loop_dump_stream
,
5839 "Loop iterations: Iteration var rejected because mode too large.\n");
5842 else if (GET_MODE_CLASS (GET_MODE (iteration_var
)) != MODE_INT
)
5844 if (loop_dump_stream
)
5845 fprintf (loop_dump_stream
,
5846 "Loop iterations: Iteration var not an integer.\n");
5850 /* Try swapping the comparison to identify a suitable iv. */
5851 if (REG_IV_TYPE (ivs
, REGNO (iteration_var
)) != BASIC_INDUCT
5852 && REG_IV_TYPE (ivs
, REGNO (iteration_var
)) != GENERAL_INDUCT
5853 && REG_P (comparison_value
)
5854 && REGNO (comparison_value
) < ivs
->n_regs
)
5856 rtx temp
= comparison_value
;
5857 comparison_code
= swap_condition (comparison_code
);
5858 comparison_value
= iteration_var
;
5859 iteration_var
= temp
;
5862 if (REG_IV_TYPE (ivs
, REGNO (iteration_var
)) == BASIC_INDUCT
)
5864 if (REGNO (iteration_var
) >= ivs
->n_regs
)
5867 /* Grab initial value, only useful if it is a constant. */
5868 bl
= REG_IV_CLASS (ivs
, REGNO (iteration_var
));
5869 initial_value
= bl
->initial_value
;
5870 if (!bl
->biv
->always_executed
|| bl
->biv
->maybe_multiple
)
5872 if (loop_dump_stream
)
5873 fprintf (loop_dump_stream
,
5874 "Loop iterations: Basic induction var not set once in each iteration.\n");
5878 increment
= biv_total_increment (bl
);
5880 else if (REG_IV_TYPE (ivs
, REGNO (iteration_var
)) == GENERAL_INDUCT
)
5882 HOST_WIDE_INT offset
= 0;
5883 struct induction
*v
= REG_IV_INFO (ivs
, REGNO (iteration_var
));
5884 rtx biv_initial_value
;
5886 if (REGNO (v
->src_reg
) >= ivs
->n_regs
)
5889 if (!v
->always_executed
|| v
->maybe_multiple
)
5891 if (loop_dump_stream
)
5892 fprintf (loop_dump_stream
,
5893 "Loop iterations: General induction var not set once in each iteration.\n");
5897 bl
= REG_IV_CLASS (ivs
, REGNO (v
->src_reg
));
5899 /* Increment value is mult_val times the increment value of the biv. */
5901 increment
= biv_total_increment (bl
);
5904 struct induction
*biv_inc
;
5906 increment
= fold_rtx_mult_add (v
->mult_val
,
5907 extend_value_for_giv (v
, increment
),
5908 const0_rtx
, v
->mode
);
5909 /* The caller assumes that one full increment has occurred at the
5910 first loop test. But that's not true when the biv is incremented
5911 after the giv is set (which is the usual case), e.g.:
5912 i = 6; do {;} while (i++ < 9) .
5913 Therefore, we bias the initial value by subtracting the amount of
5914 the increment that occurs between the giv set and the giv test. */
5915 for (biv_inc
= bl
->biv
; biv_inc
; biv_inc
= biv_inc
->next_iv
)
5917 if (loop_insn_first_p (v
->insn
, biv_inc
->insn
))
5919 if (REG_P (biv_inc
->add_val
))
5921 if (loop_dump_stream
)
5922 fprintf (loop_dump_stream
,
5923 "Loop iterations: Basic induction var add_val is REG %d.\n",
5924 REGNO (biv_inc
->add_val
));
5928 /* If we have already counted it, skip it. */
5932 offset
-= INTVAL (biv_inc
->add_val
);
5936 if (loop_dump_stream
)
5937 fprintf (loop_dump_stream
,
5938 "Loop iterations: Giv iterator, initial value bias %ld.\n",
5941 /* Initial value is mult_val times the biv's initial value plus
5942 add_val. Only useful if it is a constant. */
5943 biv_initial_value
= extend_value_for_giv (v
, bl
->initial_value
);
5945 = fold_rtx_mult_add (v
->mult_val
,
5946 plus_constant (biv_initial_value
, offset
),
5947 v
->add_val
, v
->mode
);
5951 if (loop_dump_stream
)
5952 fprintf (loop_dump_stream
,
5953 "Loop iterations: Not basic or general induction var.\n");
5957 if (initial_value
== 0)
5962 switch (comparison_code
)
5977 /* Cannot determine loop iterations with this case. */
5997 /* If the comparison value is an invariant register, then try to find
5998 its value from the insns before the start of the loop. */
6000 final_value
= comparison_value
;
6001 if (REG_P (comparison_value
)
6002 && loop_invariant_p (loop
, comparison_value
))
6004 final_value
= loop_find_equiv_value (loop
, comparison_value
);
6006 /* If we don't get an invariant final value, we are better
6007 off with the original register. */
6008 if (! loop_invariant_p (loop
, final_value
))
6009 final_value
= comparison_value
;
6012 /* Calculate the approximate final value of the induction variable
6013 (on the last successful iteration). The exact final value
6014 depends on the branch operator, and increment sign. It will be
6015 wrong if the iteration variable is not incremented by one each
6016 time through the loop and (comparison_value + off_by_one -
6017 initial_value) % increment != 0.
6018 ??? Note that the final_value may overflow and thus final_larger
6019 will be bogus. A potentially infinite loop will be classified
6020 as immediate, e.g. for (i = 0x7ffffff0; i <= 0x7fffffff; i++) */
6022 final_value
= plus_constant (final_value
, off_by_one
);
6024 /* Save the calculated values describing this loop's bounds, in case
6025 precondition_loop_p will need them later. These values can not be
6026 recalculated inside precondition_loop_p because strength reduction
6027 optimizations may obscure the loop's structure.
6029 These values are only required by precondition_loop_p and insert_bct
6030 whenever the number of iterations cannot be computed at compile time.
6031 Only the difference between final_value and initial_value is
6032 important. Note that final_value is only approximate. */
6033 loop_info
->initial_value
= initial_value
;
6034 loop_info
->comparison_value
= comparison_value
;
6035 loop_info
->final_value
= plus_constant (comparison_value
, off_by_one
);
6036 loop_info
->increment
= increment
;
6037 loop_info
->iteration_var
= iteration_var
;
6038 loop_info
->comparison_code
= comparison_code
;
6041 /* Try to determine the iteration count for loops such
6042 as (for i = init; i < init + const; i++). When running the
6043 loop optimization twice, the first pass often converts simple
6044 loops into this form. */
6046 if (REG_P (initial_value
))
6052 reg1
= initial_value
;
6053 if (GET_CODE (final_value
) == PLUS
)
6054 reg2
= XEXP (final_value
, 0), const2
= XEXP (final_value
, 1);
6056 reg2
= final_value
, const2
= const0_rtx
;
6058 /* Check for initial_value = reg1, final_value = reg2 + const2,
6059 where reg1 != reg2. */
6060 if (REG_P (reg2
) && reg2
!= reg1
)
6064 /* Find what reg1 is equivalent to. Hopefully it will
6065 either be reg2 or reg2 plus a constant. */
6066 temp
= loop_find_equiv_value (loop
, reg1
);
6068 if (find_common_reg_term (temp
, reg2
))
6069 initial_value
= temp
;
6070 else if (loop_invariant_p (loop
, reg2
))
6072 /* Find what reg2 is equivalent to. Hopefully it will
6073 either be reg1 or reg1 plus a constant. Let's ignore
6074 the latter case for now since it is not so common. */
6075 temp
= loop_find_equiv_value (loop
, reg2
);
6077 if (temp
== loop_info
->iteration_var
)
6078 temp
= initial_value
;
6080 final_value
= (const2
== const0_rtx
)
6081 ? reg1
: gen_rtx_PLUS (GET_MODE (reg1
), reg1
, const2
);
6086 loop_info
->initial_equiv_value
= initial_value
;
6087 loop_info
->final_equiv_value
= final_value
;
6089 /* For EQ comparison loops, we don't have a valid final value.
6090 Check this now so that we won't leave an invalid value if we
6091 return early for any other reason. */
6092 if (comparison_code
== EQ
)
6093 loop_info
->final_equiv_value
= loop_info
->final_value
= 0;
6097 if (loop_dump_stream
)
6098 fprintf (loop_dump_stream
,
6099 "Loop iterations: Increment value can't be calculated.\n");
6103 if (GET_CODE (increment
) != CONST_INT
)
6105 /* If we have a REG, check to see if REG holds a constant value. */
6106 /* ??? Other RTL, such as (neg (reg)) is possible here, but it isn't
6107 clear if it is worthwhile to try to handle such RTL. */
6108 if (REG_P (increment
) || GET_CODE (increment
) == SUBREG
)
6109 increment
= loop_find_equiv_value (loop
, increment
);
6111 if (GET_CODE (increment
) != CONST_INT
)
6113 if (loop_dump_stream
)
6115 fprintf (loop_dump_stream
,
6116 "Loop iterations: Increment value not constant ");
6117 print_simple_rtl (loop_dump_stream
, increment
);
6118 fprintf (loop_dump_stream
, ".\n");
6122 loop_info
->increment
= increment
;
6125 if (GET_CODE (initial_value
) != CONST_INT
)
6127 if (loop_dump_stream
)
6129 fprintf (loop_dump_stream
,
6130 "Loop iterations: Initial value not constant ");
6131 print_simple_rtl (loop_dump_stream
, initial_value
);
6132 fprintf (loop_dump_stream
, ".\n");
6136 else if (GET_CODE (final_value
) != CONST_INT
)
6138 if (loop_dump_stream
)
6140 fprintf (loop_dump_stream
,
6141 "Loop iterations: Final value not constant ");
6142 print_simple_rtl (loop_dump_stream
, final_value
);
6143 fprintf (loop_dump_stream
, ".\n");
6147 else if (comparison_code
== EQ
)
6151 if (loop_dump_stream
)
6152 fprintf (loop_dump_stream
, "Loop iterations: EQ comparison loop.\n");
6154 inc_once
= gen_int_mode (INTVAL (initial_value
) + INTVAL (increment
),
6155 GET_MODE (iteration_var
));
6157 if (inc_once
== final_value
)
6159 /* The iterator value once through the loop is equal to the
6160 comparison value. Either we have an infinite loop, or
6161 we'll loop twice. */
6162 if (increment
== const0_rtx
)
6164 loop_info
->n_iterations
= 2;
6167 loop_info
->n_iterations
= 1;
6169 if (GET_CODE (loop_info
->initial_value
) == CONST_INT
)
6170 loop_info
->final_value
6171 = gen_int_mode ((INTVAL (loop_info
->initial_value
)
6172 + loop_info
->n_iterations
* INTVAL (increment
)),
6173 GET_MODE (iteration_var
));
6175 loop_info
->final_value
6176 = plus_constant (loop_info
->initial_value
,
6177 loop_info
->n_iterations
* INTVAL (increment
));
6178 loop_info
->final_equiv_value
6179 = gen_int_mode ((INTVAL (initial_value
)
6180 + loop_info
->n_iterations
* INTVAL (increment
)),
6181 GET_MODE (iteration_var
));
6182 return loop_info
->n_iterations
;
6185 /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1. */
6188 = ((unsigned HOST_WIDE_INT
) INTVAL (final_value
)
6189 > (unsigned HOST_WIDE_INT
) INTVAL (initial_value
))
6190 - ((unsigned HOST_WIDE_INT
) INTVAL (final_value
)
6191 < (unsigned HOST_WIDE_INT
) INTVAL (initial_value
));
6193 final_larger
= (INTVAL (final_value
) > INTVAL (initial_value
))
6194 - (INTVAL (final_value
) < INTVAL (initial_value
));
6196 if (INTVAL (increment
) > 0)
6198 else if (INTVAL (increment
) == 0)
6203 /* There are 27 different cases: compare_dir = -1, 0, 1;
6204 final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
6205 There are 4 normal cases, 4 reverse cases (where the iteration variable
6206 will overflow before the loop exits), 4 infinite loop cases, and 15
6207 immediate exit (0 or 1 iteration depending on loop type) cases.
6208 Only try to optimize the normal cases. */
6210 /* (compare_dir/final_larger/increment_dir)
6211 Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
6212 Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
6213 Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
6214 Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
6216 /* ?? If the meaning of reverse loops (where the iteration variable
6217 will overflow before the loop exits) is undefined, then could
6218 eliminate all of these special checks, and just always assume
6219 the loops are normal/immediate/infinite. Note that this means
6220 the sign of increment_dir does not have to be known. Also,
6221 since it does not really hurt if immediate exit loops or infinite loops
6222 are optimized, then that case could be ignored also, and hence all
6223 loops can be optimized.
6225 According to ANSI Spec, the reverse loop case result is undefined,
6226 because the action on overflow is undefined.
6228 See also the special test for NE loops below. */
6230 if (final_larger
== increment_dir
&& final_larger
!= 0
6231 && (final_larger
== compare_dir
|| compare_dir
== 0))
6236 if (loop_dump_stream
)
6237 fprintf (loop_dump_stream
, "Loop iterations: Not normal loop.\n");
6241 /* Calculate the number of iterations, final_value is only an approximation,
6242 so correct for that. Note that abs_diff and n_iterations are
6243 unsigned, because they can be as large as 2^n - 1. */
6245 inc
= INTVAL (increment
);
6248 abs_diff
= INTVAL (final_value
) - INTVAL (initial_value
);
6253 abs_diff
= INTVAL (initial_value
) - INTVAL (final_value
);
6259 /* Given that iteration_var is going to iterate over its own mode,
6260 not HOST_WIDE_INT, disregard higher bits that might have come
6261 into the picture due to sign extension of initial and final
6263 abs_diff
&= ((unsigned HOST_WIDE_INT
) 1
6264 << (GET_MODE_BITSIZE (GET_MODE (iteration_var
)) - 1)
6267 /* For NE tests, make sure that the iteration variable won't miss
6268 the final value. If abs_diff mod abs_incr is not zero, then the
6269 iteration variable will overflow before the loop exits, and we
6270 can not calculate the number of iterations. */
6271 if (compare_dir
== 0 && (abs_diff
% abs_inc
) != 0)
6274 /* Note that the number of iterations could be calculated using
6275 (abs_diff + abs_inc - 1) / abs_inc, provided care was taken to
6276 handle potential overflow of the summation. */
6277 loop_info
->n_iterations
= abs_diff
/ abs_inc
+ ((abs_diff
% abs_inc
) != 0);
6278 return loop_info
->n_iterations
;
6281 /* Perform strength reduction and induction variable elimination.
6283 Pseudo registers created during this function will be beyond the
6284 last valid index in several tables including
6285 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
6286 problem here, because the added registers cannot be givs outside of
6287 their loop, and hence will never be reconsidered. But scan_loop
6288 must check regnos to make sure they are in bounds. */
6291 strength_reduce (struct loop
*loop
, int flags
)
6293 struct loop_info
*loop_info
= LOOP_INFO (loop
);
6294 struct loop_regs
*regs
= LOOP_REGS (loop
);
6295 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6297 /* Temporary list pointer for traversing ivs->list. */
6298 struct iv_class
*bl
;
6299 /* Ratio of extra register life span we can justify
6300 for saving an instruction. More if loop doesn't call subroutines
6301 since in that case saving an insn makes more difference
6302 and more registers are available. */
6303 /* ??? could set this to last value of threshold in move_movables */
6304 int threshold
= (loop_info
->has_call
? 1 : 2) * (3 + n_non_fixed_regs
);
6305 /* Map of pseudo-register replacements. */
6306 rtx
*reg_map
= NULL
;
6308 rtx test_reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
6309 int insn_count
= count_insns_in_loop (loop
);
6311 addr_placeholder
= gen_reg_rtx (Pmode
);
6313 ivs
->n_regs
= max_reg_before_loop
;
6314 ivs
->regs
= xcalloc (ivs
->n_regs
, sizeof (struct iv
));
6316 /* Find all BIVs in loop. */
6317 loop_bivs_find (loop
);
6319 /* Exit if there are no bivs. */
6322 loop_ivs_free (loop
);
6326 /* Determine how BIVS are initialized by looking through pre-header
6327 extended basic block. */
6328 loop_bivs_init_find (loop
);
6330 /* Look at the each biv and see if we can say anything better about its
6331 initial value from any initializing insns set up above. */
6332 loop_bivs_check (loop
);
6334 /* Search the loop for general induction variables. */
6335 loop_givs_find (loop
);
6337 /* Try to calculate and save the number of loop iterations. This is
6338 set to zero if the actual number can not be calculated. This must
6339 be called after all giv's have been identified, since otherwise it may
6340 fail if the iteration variable is a giv. */
6341 loop_iterations (loop
);
6343 #ifdef HAVE_prefetch
6344 if (flags
& LOOP_PREFETCH
)
6345 emit_prefetch_instructions (loop
);
6348 /* Now for each giv for which we still don't know whether or not it is
6349 replaceable, check to see if it is replaceable because its final value
6350 can be calculated. This must be done after loop_iterations is called,
6351 so that final_giv_value will work correctly. */
6352 loop_givs_check (loop
);
6354 /* Try to prove that the loop counter variable (if any) is always
6355 nonnegative; if so, record that fact with a REG_NONNEG note
6356 so that "decrement and branch until zero" insn can be used. */
6357 check_dbra_loop (loop
, insn_count
);
6359 /* Create reg_map to hold substitutions for replaceable giv regs.
6360 Some givs might have been made from biv increments, so look at
6361 ivs->reg_iv_type for a suitable size. */
6362 reg_map_size
= ivs
->n_regs
;
6363 reg_map
= xcalloc (reg_map_size
, sizeof (rtx
));
6365 /* Examine each iv class for feasibility of strength reduction/induction
6366 variable elimination. */
6368 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
6370 struct induction
*v
;
6373 /* Test whether it will be possible to eliminate this biv
6374 provided all givs are reduced. */
6375 bl
->eliminable
= loop_biv_eliminable_p (loop
, bl
, threshold
, insn_count
);
6377 /* This will be true at the end, if all givs which depend on this
6378 biv have been strength reduced.
6379 We can't (currently) eliminate the biv unless this is so. */
6380 bl
->all_reduced
= 1;
6382 /* Check each extension dependent giv in this class to see if its
6383 root biv is safe from wrapping in the interior mode. */
6384 check_ext_dependent_givs (loop
, bl
);
6386 /* Combine all giv's for this iv_class. */
6387 combine_givs (regs
, bl
);
6389 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
6391 struct induction
*tv
;
6393 if (v
->ignore
|| v
->same
)
6396 benefit
= loop_giv_reduce_benefit (loop
, bl
, v
, test_reg
);
6398 /* If an insn is not to be strength reduced, then set its ignore
6399 flag, and clear bl->all_reduced. */
6401 /* A giv that depends on a reversed biv must be reduced if it is
6402 used after the loop exit, otherwise, it would have the wrong
6403 value after the loop exit. To make it simple, just reduce all
6404 of such giv's whether or not we know they are used after the loop
6407 if (v
->lifetime
* threshold
* benefit
< insn_count
6410 if (loop_dump_stream
)
6411 fprintf (loop_dump_stream
,
6412 "giv of insn %d not worth while, %d vs %d.\n",
6414 v
->lifetime
* threshold
* benefit
, insn_count
);
6416 bl
->all_reduced
= 0;
6420 /* Check that we can increment the reduced giv without a
6421 multiply insn. If not, reject it. */
6423 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
6424 if (tv
->mult_val
== const1_rtx
6425 && ! product_cheap_p (tv
->add_val
, v
->mult_val
))
6427 if (loop_dump_stream
)
6428 fprintf (loop_dump_stream
,
6429 "giv of insn %d: would need a multiply.\n",
6430 INSN_UID (v
->insn
));
6432 bl
->all_reduced
= 0;
6438 /* Check for givs whose first use is their definition and whose
6439 last use is the definition of another giv. If so, it is likely
6440 dead and should not be used to derive another giv nor to
6442 loop_givs_dead_check (loop
, bl
);
6444 /* Reduce each giv that we decided to reduce. */
6445 loop_givs_reduce (loop
, bl
);
6447 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
6450 For each giv register that can be reduced now: if replaceable,
6451 substitute reduced reg wherever the old giv occurs;
6452 else add new move insn "giv_reg = reduced_reg". */
6453 loop_givs_rescan (loop
, bl
, reg_map
);
6455 /* All the givs based on the biv bl have been reduced if they
6458 /* For each giv not marked as maybe dead that has been combined with a
6459 second giv, clear any "maybe dead" mark on that second giv.
6460 v->new_reg will either be or refer to the register of the giv it
6463 Doing this clearing avoids problems in biv elimination where
6464 a giv's new_reg is a complex value that can't be put in the
6465 insn but the giv combined with (with a reg as new_reg) is
6466 marked maybe_dead. Since the register will be used in either
6467 case, we'd prefer it be used from the simpler giv. */
6469 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
6470 if (! v
->maybe_dead
&& v
->same
)
6471 v
->same
->maybe_dead
= 0;
6473 /* Try to eliminate the biv, if it is a candidate.
6474 This won't work if ! bl->all_reduced,
6475 since the givs we planned to use might not have been reduced.
6477 We have to be careful that we didn't initially think we could
6478 eliminate this biv because of a giv that we now think may be
6479 dead and shouldn't be used as a biv replacement.
6481 Also, there is the possibility that we may have a giv that looks
6482 like it can be used to eliminate a biv, but the resulting insn
6483 isn't valid. This can happen, for example, on the 88k, where a
6484 JUMP_INSN can compare a register only with zero. Attempts to
6485 replace it with a compare with a constant will fail.
6487 Note that in cases where this call fails, we may have replaced some
6488 of the occurrences of the biv with a giv, but no harm was done in
6489 doing so in the rare cases where it can occur. */
6491 if (bl
->all_reduced
== 1 && bl
->eliminable
6492 && maybe_eliminate_biv (loop
, bl
, 1, threshold
, insn_count
))
6494 /* ?? If we created a new test to bypass the loop entirely,
6495 or otherwise drop straight in, based on this test, then
6496 we might want to rewrite it also. This way some later
6497 pass has more hope of removing the initialization of this
6500 /* If final_value != 0, then the biv may be used after loop end
6501 and we must emit an insn to set it just in case.
6503 Reversed bivs already have an insn after the loop setting their
6504 value, so we don't need another one. We can't calculate the
6505 proper final value for such a biv here anyways. */
6506 if (bl
->final_value
&& ! bl
->reversed
)
6507 loop_insn_sink_or_swim (loop
,
6508 gen_load_of_final_value (bl
->biv
->dest_reg
,
6511 if (loop_dump_stream
)
6512 fprintf (loop_dump_stream
, "Reg %d: biv eliminated\n",
6515 /* See above note wrt final_value. But since we couldn't eliminate
6516 the biv, we must set the value after the loop instead of before. */
6517 else if (bl
->final_value
&& ! bl
->reversed
)
6518 loop_insn_sink (loop
, gen_load_of_final_value (bl
->biv
->dest_reg
,
6522 /* Go through all the instructions in the loop, making all the
6523 register substitutions scheduled in REG_MAP. */
6525 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
6528 replace_regs (PATTERN (p
), reg_map
, reg_map_size
, 0);
6529 replace_regs (REG_NOTES (p
), reg_map
, reg_map_size
, 0);
6533 if (loop_dump_stream
)
6534 fprintf (loop_dump_stream
, "\n");
6536 loop_ivs_free (loop
);
6541 /*Record all basic induction variables calculated in the insn. */
6543 check_insn_for_bivs (struct loop
*loop
, rtx p
, int not_every_iteration
,
6546 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6553 if (NONJUMP_INSN_P (p
)
6554 && (set
= single_set (p
))
6555 && REG_P (SET_DEST (set
)))
6557 dest_reg
= SET_DEST (set
);
6558 if (REGNO (dest_reg
) < max_reg_before_loop
6559 && REGNO (dest_reg
) >= FIRST_PSEUDO_REGISTER
6560 && REG_IV_TYPE (ivs
, REGNO (dest_reg
)) != NOT_BASIC_INDUCT
)
6562 if (basic_induction_var (loop
, SET_SRC (set
),
6563 GET_MODE (SET_SRC (set
)),
6564 dest_reg
, p
, &inc_val
, &mult_val
,
6567 /* It is a possible basic induction variable.
6568 Create and initialize an induction structure for it. */
6570 struct induction
*v
= xmalloc (sizeof (struct induction
));
6572 record_biv (loop
, v
, p
, dest_reg
, inc_val
, mult_val
, location
,
6573 not_every_iteration
, maybe_multiple
);
6574 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = BASIC_INDUCT
;
6576 else if (REGNO (dest_reg
) < ivs
->n_regs
)
6577 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = NOT_BASIC_INDUCT
;
6583 /* Record all givs calculated in the insn.
6584 A register is a giv if: it is only set once, it is a function of a
6585 biv and a constant (or invariant), and it is not a biv. */
6587 check_insn_for_givs (struct loop
*loop
, rtx p
, int not_every_iteration
,
6590 struct loop_regs
*regs
= LOOP_REGS (loop
);
6593 /* Look for a general induction variable in a register. */
6594 if (NONJUMP_INSN_P (p
)
6595 && (set
= single_set (p
))
6596 && REG_P (SET_DEST (set
))
6597 && ! regs
->array
[REGNO (SET_DEST (set
))].may_not_optimize
)
6606 rtx last_consec_insn
;
6608 dest_reg
= SET_DEST (set
);
6609 if (REGNO (dest_reg
) < FIRST_PSEUDO_REGISTER
)
6612 if (/* SET_SRC is a giv. */
6613 (general_induction_var (loop
, SET_SRC (set
), &src_reg
, &add_val
,
6614 &mult_val
, &ext_val
, 0, &benefit
, VOIDmode
)
6615 /* Equivalent expression is a giv. */
6616 || ((regnote
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
6617 && general_induction_var (loop
, XEXP (regnote
, 0), &src_reg
,
6618 &add_val
, &mult_val
, &ext_val
, 0,
6619 &benefit
, VOIDmode
)))
6620 /* Don't try to handle any regs made by loop optimization.
6621 We have nothing on them in regno_first_uid, etc. */
6622 && REGNO (dest_reg
) < max_reg_before_loop
6623 /* Don't recognize a BASIC_INDUCT_VAR here. */
6624 && dest_reg
!= src_reg
6625 /* This must be the only place where the register is set. */
6626 && (regs
->array
[REGNO (dest_reg
)].n_times_set
== 1
6627 /* or all sets must be consecutive and make a giv. */
6628 || (benefit
= consec_sets_giv (loop
, benefit
, p
,
6630 &add_val
, &mult_val
, &ext_val
,
6631 &last_consec_insn
))))
6633 struct induction
*v
= xmalloc (sizeof (struct induction
));
6635 /* If this is a library call, increase benefit. */
6636 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
6637 benefit
+= libcall_benefit (p
);
6639 /* Skip the consecutive insns, if there are any. */
6640 if (regs
->array
[REGNO (dest_reg
)].n_times_set
!= 1)
6641 p
= last_consec_insn
;
6643 record_giv (loop
, v
, p
, src_reg
, dest_reg
, mult_val
, add_val
,
6644 ext_val
, benefit
, DEST_REG
, not_every_iteration
,
6645 maybe_multiple
, (rtx
*) 0);
6650 /* Look for givs which are memory addresses. */
6651 if (NONJUMP_INSN_P (p
))
6652 find_mem_givs (loop
, PATTERN (p
), p
, not_every_iteration
,
6655 /* Update the status of whether giv can derive other givs. This can
6656 change when we pass a label or an insn that updates a biv. */
6657 if (INSN_P (p
) || LABEL_P (p
))
6658 update_giv_derive (loop
, p
);
6662 /* Return 1 if X is a valid source for an initial value (or as value being
6663 compared against in an initial test).
6665 X must be either a register or constant and must not be clobbered between
6666 the current insn and the start of the loop.
6668 INSN is the insn containing X. */
6671 valid_initial_value_p (rtx x
, rtx insn
, int call_seen
, rtx loop_start
)
6676 /* Only consider pseudos we know about initialized in insns whose luids
6679 || REGNO (x
) >= max_reg_before_loop
)
6682 /* Don't use call-clobbered registers across a call which clobbers it. On
6683 some machines, don't use any hard registers at all. */
6684 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
6685 && (SMALL_REGISTER_CLASSES
6686 || (call_used_regs
[REGNO (x
)] && call_seen
)))
6689 /* Don't use registers that have been clobbered before the start of the
6691 if (reg_set_between_p (x
, insn
, loop_start
))
6697 /* Scan X for memory refs and check each memory address
6698 as a possible giv. INSN is the insn whose pattern X comes from.
6699 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
6700 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
6701 more than once in each loop iteration. */
6704 find_mem_givs (const struct loop
*loop
, rtx x
, rtx insn
,
6705 int not_every_iteration
, int maybe_multiple
)
6714 code
= GET_CODE (x
);
6739 /* This code used to disable creating GIVs with mult_val == 1 and
6740 add_val == 0. However, this leads to lost optimizations when
6741 it comes time to combine a set of related DEST_ADDR GIVs, since
6742 this one would not be seen. */
6744 if (general_induction_var (loop
, XEXP (x
, 0), &src_reg
, &add_val
,
6745 &mult_val
, &ext_val
, 1, &benefit
,
6748 /* Found one; record it. */
6749 struct induction
*v
= xmalloc (sizeof (struct induction
));
6751 record_giv (loop
, v
, insn
, src_reg
, addr_placeholder
, mult_val
,
6752 add_val
, ext_val
, benefit
, DEST_ADDR
,
6753 not_every_iteration
, maybe_multiple
, &XEXP (x
, 0));
6764 /* Recursively scan the subexpressions for other mem refs. */
6766 fmt
= GET_RTX_FORMAT (code
);
6767 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
6769 find_mem_givs (loop
, XEXP (x
, i
), insn
, not_every_iteration
,
6771 else if (fmt
[i
] == 'E')
6772 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
6773 find_mem_givs (loop
, XVECEXP (x
, i
, j
), insn
, not_every_iteration
,
6777 /* Fill in the data about one biv update.
6778 V is the `struct induction' in which we record the biv. (It is
6779 allocated by the caller, with alloca.)
6780 INSN is the insn that sets it.
6781 DEST_REG is the biv's reg.
6783 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
6784 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
6785 being set to INC_VAL.
6787 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
6788 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
6789 can be executed more than once per iteration. If MAYBE_MULTIPLE
6790 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
6791 executed exactly once per iteration. */
6794 record_biv (struct loop
*loop
, struct induction
*v
, rtx insn
, rtx dest_reg
,
6795 rtx inc_val
, rtx mult_val
, rtx
*location
,
6796 int not_every_iteration
, int maybe_multiple
)
6798 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6799 struct iv_class
*bl
;
6802 v
->src_reg
= dest_reg
;
6803 v
->dest_reg
= dest_reg
;
6804 v
->mult_val
= mult_val
;
6805 v
->add_val
= inc_val
;
6806 v
->ext_dependent
= NULL_RTX
;
6807 v
->location
= location
;
6808 v
->mode
= GET_MODE (dest_reg
);
6809 v
->always_computable
= ! not_every_iteration
;
6810 v
->always_executed
= ! not_every_iteration
;
6811 v
->maybe_multiple
= maybe_multiple
;
6814 /* Add this to the reg's iv_class, creating a class
6815 if this is the first incrementation of the reg. */
6817 bl
= REG_IV_CLASS (ivs
, REGNO (dest_reg
));
6820 /* Create and initialize new iv_class. */
6822 bl
= xmalloc (sizeof (struct iv_class
));
6824 bl
->regno
= REGNO (dest_reg
);
6830 /* Set initial value to the reg itself. */
6831 bl
->initial_value
= dest_reg
;
6832 bl
->final_value
= 0;
6833 /* We haven't seen the initializing insn yet. */
6836 bl
->initial_test
= 0;
6837 bl
->incremented
= 0;
6841 bl
->total_benefit
= 0;
6843 /* Add this class to ivs->list. */
6844 bl
->next
= ivs
->list
;
6847 /* Put it in the array of biv register classes. */
6848 REG_IV_CLASS (ivs
, REGNO (dest_reg
)) = bl
;
6852 /* Check if location is the same as a previous one. */
6853 struct induction
*induction
;
6854 for (induction
= bl
->biv
; induction
; induction
= induction
->next_iv
)
6855 if (location
== induction
->location
)
6857 v
->same
= induction
;
6862 /* Update IV_CLASS entry for this biv. */
6863 v
->next_iv
= bl
->biv
;
6866 if (mult_val
== const1_rtx
)
6867 bl
->incremented
= 1;
6869 if (loop_dump_stream
)
6870 loop_biv_dump (v
, loop_dump_stream
, 0);
6873 /* Fill in the data about one giv.
6874 V is the `struct induction' in which we record the giv. (It is
6875 allocated by the caller, with alloca.)
6876 INSN is the insn that sets it.
6877 BENEFIT estimates the savings from deleting this insn.
6878 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
6879 into a register or is used as a memory address.
6881 SRC_REG is the biv reg which the giv is computed from.
6882 DEST_REG is the giv's reg (if the giv is stored in a reg).
6883 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
6884 LOCATION points to the place where this giv's value appears in INSN. */
6887 record_giv (const struct loop
*loop
, struct induction
*v
, rtx insn
,
6888 rtx src_reg
, rtx dest_reg
, rtx mult_val
, rtx add_val
,
6889 rtx ext_val
, int benefit
, enum g_types type
,
6890 int not_every_iteration
, int maybe_multiple
, rtx
*location
)
6892 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6893 struct induction
*b
;
6894 struct iv_class
*bl
;
6895 rtx set
= single_set (insn
);
6898 /* Attempt to prove constantness of the values. Don't let simplify_rtx
6899 undo the MULT canonicalization that we performed earlier. */
6900 temp
= simplify_rtx (add_val
);
6902 && ! (GET_CODE (add_val
) == MULT
6903 && GET_CODE (temp
) == ASHIFT
))
6907 v
->src_reg
= src_reg
;
6909 v
->dest_reg
= dest_reg
;
6910 v
->mult_val
= mult_val
;
6911 v
->add_val
= add_val
;
6912 v
->ext_dependent
= ext_val
;
6913 v
->benefit
= benefit
;
6914 v
->location
= location
;
6916 v
->combined_with
= 0;
6917 v
->maybe_multiple
= maybe_multiple
;
6919 v
->derive_adjustment
= 0;
6925 v
->auto_inc_opt
= 0;
6928 /* The v->always_computable field is used in update_giv_derive, to
6929 determine whether a giv can be used to derive another giv. For a
6930 DEST_REG giv, INSN computes a new value for the giv, so its value
6931 isn't computable if INSN insn't executed every iteration.
6932 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
6933 it does not compute a new value. Hence the value is always computable
6934 regardless of whether INSN is executed each iteration. */
6936 if (type
== DEST_ADDR
)
6937 v
->always_computable
= 1;
6939 v
->always_computable
= ! not_every_iteration
;
6941 v
->always_executed
= ! not_every_iteration
;
6943 if (type
== DEST_ADDR
)
6945 v
->mode
= GET_MODE (*location
);
6948 else /* type == DEST_REG */
6950 v
->mode
= GET_MODE (SET_DEST (set
));
6952 v
->lifetime
= LOOP_REG_LIFETIME (loop
, REGNO (dest_reg
));
6954 /* If the lifetime is zero, it means that this register is
6955 really a dead store. So mark this as a giv that can be
6956 ignored. This will not prevent the biv from being eliminated. */
6957 if (v
->lifetime
== 0)
6960 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = GENERAL_INDUCT
;
6961 REG_IV_INFO (ivs
, REGNO (dest_reg
)) = v
;
6964 /* Add the giv to the class of givs computed from one biv. */
6966 bl
= REG_IV_CLASS (ivs
, REGNO (src_reg
));
6969 v
->next_iv
= bl
->giv
;
6971 /* Don't count DEST_ADDR. This is supposed to count the number of
6972 insns that calculate givs. */
6973 if (type
== DEST_REG
)
6975 bl
->total_benefit
+= benefit
;
6978 /* Fatal error, biv missing for this giv? */
6981 if (type
== DEST_ADDR
)
6984 v
->not_replaceable
= 0;
6988 /* The giv can be replaced outright by the reduced register only if all
6989 of the following conditions are true:
6990 - the insn that sets the giv is always executed on any iteration
6991 on which the giv is used at all
6992 (there are two ways to deduce this:
6993 either the insn is executed on every iteration,
6994 or all uses follow that insn in the same basic block),
6995 - the giv is not used outside the loop
6996 - no assignments to the biv occur during the giv's lifetime. */
6998 if (REGNO_FIRST_UID (REGNO (dest_reg
)) == INSN_UID (insn
)
6999 /* Previous line always fails if INSN was moved by loop opt. */
7000 && REGNO_LAST_LUID (REGNO (dest_reg
))
7001 < INSN_LUID (loop
->end
)
7002 && (! not_every_iteration
7003 || last_use_this_basic_block (dest_reg
, insn
)))
7005 /* Now check that there are no assignments to the biv within the
7006 giv's lifetime. This requires two separate checks. */
7008 /* Check each biv update, and fail if any are between the first
7009 and last use of the giv.
7011 If this loop contains an inner loop that was unrolled, then
7012 the insn modifying the biv may have been emitted by the loop
7013 unrolling code, and hence does not have a valid luid. Just
7014 mark the biv as not replaceable in this case. It is not very
7015 useful as a biv, because it is used in two different loops.
7016 It is very unlikely that we would be able to optimize the giv
7017 using this biv anyways. */
7020 v
->not_replaceable
= 0;
7021 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
7023 if (INSN_UID (b
->insn
) >= max_uid_for_loop
7024 || ((INSN_LUID (b
->insn
)
7025 >= REGNO_FIRST_LUID (REGNO (dest_reg
)))
7026 && (INSN_LUID (b
->insn
)
7027 <= REGNO_LAST_LUID (REGNO (dest_reg
)))))
7030 v
->not_replaceable
= 1;
7035 /* If there are any backwards branches that go from after the
7036 biv update to before it, then this giv is not replaceable. */
7038 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
7039 if (back_branch_in_range_p (loop
, b
->insn
))
7042 v
->not_replaceable
= 1;
7048 /* May still be replaceable, we don't have enough info here to
7051 v
->not_replaceable
= 0;
7055 /* Record whether the add_val contains a const_int, for later use by
7060 v
->no_const_addval
= 1;
7061 if (tem
== const0_rtx
)
7063 else if (CONSTANT_P (add_val
))
7064 v
->no_const_addval
= 0;
7065 if (GET_CODE (tem
) == PLUS
)
7069 if (GET_CODE (XEXP (tem
, 0)) == PLUS
)
7070 tem
= XEXP (tem
, 0);
7071 else if (GET_CODE (XEXP (tem
, 1)) == PLUS
)
7072 tem
= XEXP (tem
, 1);
7076 if (CONSTANT_P (XEXP (tem
, 1)))
7077 v
->no_const_addval
= 0;
7081 if (loop_dump_stream
)
7082 loop_giv_dump (v
, loop_dump_stream
, 0);
7085 /* Try to calculate the final value of the giv, the value it will have at
7086 the end of the loop. If we can do it, return that value. */
7089 final_giv_value (const struct loop
*loop
, struct induction
*v
)
7091 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
7092 struct iv_class
*bl
;
7096 rtx loop_end
= loop
->end
;
7097 unsigned HOST_WIDE_INT n_iterations
= LOOP_INFO (loop
)->n_iterations
;
7099 bl
= REG_IV_CLASS (ivs
, REGNO (v
->src_reg
));
7101 /* The final value for givs which depend on reversed bivs must be calculated
7102 differently than for ordinary givs. In this case, there is already an
7103 insn after the loop which sets this giv's final value (if necessary),
7104 and there are no other loop exits, so we can return any value. */
7107 if (loop_dump_stream
)
7108 fprintf (loop_dump_stream
,
7109 "Final giv value for %d, depends on reversed biv\n",
7110 REGNO (v
->dest_reg
));
7114 /* Try to calculate the final value as a function of the biv it depends
7115 upon. The only exit from the loop must be the fall through at the bottom
7116 and the insn that sets the giv must be executed on every iteration
7117 (otherwise the giv may not have its final value when the loop exits). */
7119 /* ??? Can calculate the final giv value by subtracting off the
7120 extra biv increments times the giv's mult_val. The loop must have
7121 only one exit for this to work, but the loop iterations does not need
7124 if (n_iterations
!= 0
7125 && ! loop
->exit_count
7126 && v
->always_executed
)
7128 /* ?? It is tempting to use the biv's value here since these insns will
7129 be put after the loop, and hence the biv will have its final value
7130 then. However, this fails if the biv is subsequently eliminated.
7131 Perhaps determine whether biv's are eliminable before trying to
7132 determine whether giv's are replaceable so that we can use the
7133 biv value here if it is not eliminable. */
7135 /* We are emitting code after the end of the loop, so we must make
7136 sure that bl->initial_value is still valid then. It will still
7137 be valid if it is invariant. */
7139 increment
= biv_total_increment (bl
);
7141 if (increment
&& loop_invariant_p (loop
, increment
)
7142 && loop_invariant_p (loop
, bl
->initial_value
))
7144 /* Can calculate the loop exit value of its biv as
7145 (n_iterations * increment) + initial_value */
7147 /* The loop exit value of the giv is then
7148 (final_biv_value - extra increments) * mult_val + add_val.
7149 The extra increments are any increments to the biv which
7150 occur in the loop after the giv's value is calculated.
7151 We must search from the insn that sets the giv to the end
7152 of the loop to calculate this value. */
7154 /* Put the final biv value in tem. */
7155 tem
= gen_reg_rtx (v
->mode
);
7156 record_base_value (REGNO (tem
), bl
->biv
->add_val
, 0);
7157 loop_iv_add_mult_sink (loop
, extend_value_for_giv (v
, increment
),
7158 GEN_INT (n_iterations
),
7159 extend_value_for_giv (v
, bl
->initial_value
),
7162 /* Subtract off extra increments as we find them. */
7163 for (insn
= NEXT_INSN (v
->insn
); insn
!= loop_end
;
7164 insn
= NEXT_INSN (insn
))
7166 struct induction
*biv
;
7168 for (biv
= bl
->biv
; biv
; biv
= biv
->next_iv
)
7169 if (biv
->insn
== insn
)
7172 tem
= expand_simple_binop (GET_MODE (tem
), MINUS
, tem
,
7173 biv
->add_val
, NULL_RTX
, 0,
7177 loop_insn_sink (loop
, seq
);
7181 /* Now calculate the giv's final value. */
7182 loop_iv_add_mult_sink (loop
, tem
, v
->mult_val
, v
->add_val
, tem
);
7184 if (loop_dump_stream
)
7185 fprintf (loop_dump_stream
,
7186 "Final giv value for %d, calc from biv's value.\n",
7187 REGNO (v
->dest_reg
));
7193 /* Replaceable giv's should never reach here. */
7197 /* Check to see if the biv is dead at all loop exits. */
7198 if (reg_dead_after_loop (loop
, v
->dest_reg
))
7200 if (loop_dump_stream
)
7201 fprintf (loop_dump_stream
,
7202 "Final giv value for %d, giv dead after loop exit.\n",
7203 REGNO (v
->dest_reg
));
7211 /* All this does is determine whether a giv can be made replaceable because
7212 its final value can be calculated. This code can not be part of record_giv
7213 above, because final_giv_value requires that the number of loop iterations
7214 be known, and that can not be accurately calculated until after all givs
7215 have been identified. */
7218 check_final_value (const struct loop
*loop
, struct induction
*v
)
7220 rtx final_value
= 0;
7222 /* DEST_ADDR givs will never reach here, because they are always marked
7223 replaceable above in record_giv. */
7225 /* The giv can be replaced outright by the reduced register only if all
7226 of the following conditions are true:
7227 - the insn that sets the giv is always executed on any iteration
7228 on which the giv is used at all
7229 (there are two ways to deduce this:
7230 either the insn is executed on every iteration,
7231 or all uses follow that insn in the same basic block),
7232 - its final value can be calculated (this condition is different
7233 than the one above in record_giv)
7234 - it's not used before the it's set
7235 - no assignments to the biv occur during the giv's lifetime. */
7238 /* This is only called now when replaceable is known to be false. */
7239 /* Clear replaceable, so that it won't confuse final_giv_value. */
7243 if ((final_value
= final_giv_value (loop
, v
))
7244 && (v
->always_executed
7245 || last_use_this_basic_block (v
->dest_reg
, v
->insn
)))
7247 int biv_increment_seen
= 0, before_giv_insn
= 0;
7252 v
->not_replaceable
= 0;
7254 /* When trying to determine whether or not a biv increment occurs
7255 during the lifetime of the giv, we can ignore uses of the variable
7256 outside the loop because final_value is true. Hence we can not
7257 use regno_last_uid and regno_first_uid as above in record_giv. */
7259 /* Search the loop to determine whether any assignments to the
7260 biv occur during the giv's lifetime. Start with the insn
7261 that sets the giv, and search around the loop until we come
7262 back to that insn again.
7264 Also fail if there is a jump within the giv's lifetime that jumps
7265 to somewhere outside the lifetime but still within the loop. This
7266 catches spaghetti code where the execution order is not linear, and
7267 hence the above test fails. Here we assume that the giv lifetime
7268 does not extend from one iteration of the loop to the next, so as
7269 to make the test easier. Since the lifetime isn't known yet,
7270 this requires two loops. See also record_giv above. */
7272 last_giv_use
= v
->insn
;
7279 before_giv_insn
= 1;
7280 p
= NEXT_INSN (loop
->start
);
7287 /* It is possible for the BIV increment to use the GIV if we
7288 have a cycle. Thus we must be sure to check each insn for
7289 both BIV and GIV uses, and we must check for BIV uses
7292 if (! biv_increment_seen
7293 && reg_set_p (v
->src_reg
, PATTERN (p
)))
7294 biv_increment_seen
= 1;
7296 if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
7298 if (biv_increment_seen
|| before_giv_insn
)
7301 v
->not_replaceable
= 1;
7309 /* Now that the lifetime of the giv is known, check for branches
7310 from within the lifetime to outside the lifetime if it is still
7320 p
= NEXT_INSN (loop
->start
);
7321 if (p
== last_giv_use
)
7324 if (JUMP_P (p
) && JUMP_LABEL (p
)
7325 && LABEL_NAME (JUMP_LABEL (p
))
7326 && ((loop_insn_first_p (JUMP_LABEL (p
), v
->insn
)
7327 && loop_insn_first_p (loop
->start
, JUMP_LABEL (p
)))
7328 || (loop_insn_first_p (last_giv_use
, JUMP_LABEL (p
))
7329 && loop_insn_first_p (JUMP_LABEL (p
), loop
->end
))))
7332 v
->not_replaceable
= 1;
7334 if (loop_dump_stream
)
7335 fprintf (loop_dump_stream
,
7336 "Found branch outside giv lifetime.\n");
7343 /* If it is replaceable, then save the final value. */
7345 v
->final_value
= final_value
;
7348 if (loop_dump_stream
&& v
->replaceable
)
7349 fprintf (loop_dump_stream
, "Insn %d: giv reg %d final_value replaceable\n",
7350 INSN_UID (v
->insn
), REGNO (v
->dest_reg
));
7353 /* Update the status of whether a giv can derive other givs.
7355 We need to do something special if there is or may be an update to the biv
7356 between the time the giv is defined and the time it is used to derive
7359 In addition, a giv that is only conditionally set is not allowed to
7360 derive another giv once a label has been passed.
7362 The cases we look at are when a label or an update to a biv is passed. */
7365 update_giv_derive (const struct loop
*loop
, rtx p
)
7367 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
7368 struct iv_class
*bl
;
7369 struct induction
*biv
, *giv
;
7373 /* Search all IV classes, then all bivs, and finally all givs.
7375 There are three cases we are concerned with. First we have the situation
7376 of a giv that is only updated conditionally. In that case, it may not
7377 derive any givs after a label is passed.
7379 The second case is when a biv update occurs, or may occur, after the
7380 definition of a giv. For certain biv updates (see below) that are
7381 known to occur between the giv definition and use, we can adjust the
7382 giv definition. For others, or when the biv update is conditional,
7383 we must prevent the giv from deriving any other givs. There are two
7384 sub-cases within this case.
7386 If this is a label, we are concerned with any biv update that is done
7387 conditionally, since it may be done after the giv is defined followed by
7388 a branch here (actually, we need to pass both a jump and a label, but
7389 this extra tracking doesn't seem worth it).
7391 If this is a jump, we are concerned about any biv update that may be
7392 executed multiple times. We are actually only concerned about
7393 backward jumps, but it is probably not worth performing the test
7394 on the jump again here.
7396 If this is a biv update, we must adjust the giv status to show that a
7397 subsequent biv update was performed. If this adjustment cannot be done,
7398 the giv cannot derive further givs. */
7400 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
7401 for (biv
= bl
->biv
; biv
; biv
= biv
->next_iv
)
7402 if (LABEL_P (p
) || JUMP_P (p
)
7405 /* Skip if location is the same as a previous one. */
7409 for (giv
= bl
->giv
; giv
; giv
= giv
->next_iv
)
7411 /* If cant_derive is already true, there is no point in
7412 checking all of these conditions again. */
7413 if (giv
->cant_derive
)
7416 /* If this giv is conditionally set and we have passed a label,
7417 it cannot derive anything. */
7418 if (LABEL_P (p
) && ! giv
->always_computable
)
7419 giv
->cant_derive
= 1;
7421 /* Skip givs that have mult_val == 0, since
7422 they are really invariants. Also skip those that are
7423 replaceable, since we know their lifetime doesn't contain
7425 else if (giv
->mult_val
== const0_rtx
|| giv
->replaceable
)
7428 /* The only way we can allow this giv to derive another
7429 is if this is a biv increment and we can form the product
7430 of biv->add_val and giv->mult_val. In this case, we will
7431 be able to compute a compensation. */
7432 else if (biv
->insn
== p
)
7437 if (biv
->mult_val
== const1_rtx
)
7438 tem
= simplify_giv_expr (loop
,
7439 gen_rtx_MULT (giv
->mode
,
7442 &ext_val_dummy
, &dummy
);
7444 if (tem
&& giv
->derive_adjustment
)
7445 tem
= simplify_giv_expr
7447 gen_rtx_PLUS (giv
->mode
, tem
, giv
->derive_adjustment
),
7448 &ext_val_dummy
, &dummy
);
7451 giv
->derive_adjustment
= tem
;
7453 giv
->cant_derive
= 1;
7455 else if ((LABEL_P (p
) && ! biv
->always_computable
)
7456 || (JUMP_P (p
) && biv
->maybe_multiple
))
7457 giv
->cant_derive
= 1;
7462 /* Check whether an insn is an increment legitimate for a basic induction var.
7463 X is the source of insn P, or a part of it.
7464 MODE is the mode in which X should be interpreted.
7466 DEST_REG is the putative biv, also the destination of the insn.
7467 We accept patterns of these forms:
7468 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
7469 REG = INVARIANT + REG
7471 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
7472 store the additive term into *INC_VAL, and store the place where
7473 we found the additive term into *LOCATION.
7475 If X is an assignment of an invariant into DEST_REG, we set
7476 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
7478 We also want to detect a BIV when it corresponds to a variable
7479 whose mode was promoted. In that case, an increment
7480 of the variable may be a PLUS that adds a SUBREG of that variable to
7481 an invariant and then sign- or zero-extends the result of the PLUS
7484 Most GIVs in such cases will be in the promoted mode, since that is the
7485 probably the natural computation mode (and almost certainly the mode
7486 used for addresses) on the machine. So we view the pseudo-reg containing
7487 the variable as the BIV, as if it were simply incremented.
7489 Note that treating the entire pseudo as a BIV will result in making
7490 simple increments to any GIVs based on it. However, if the variable
7491 overflows in its declared mode but not its promoted mode, the result will
7492 be incorrect. This is acceptable if the variable is signed, since
7493 overflows in such cases are undefined, but not if it is unsigned, since
7494 those overflows are defined. So we only check for SIGN_EXTEND and
7497 If we cannot find a biv, we return 0. */
7500 basic_induction_var (const struct loop
*loop
, rtx x
, enum machine_mode mode
,
7501 rtx dest_reg
, rtx p
, rtx
*inc_val
, rtx
*mult_val
,
7506 rtx insn
, set
= 0, last
, inc
;
7508 code
= GET_CODE (x
);
7513 if (rtx_equal_p (XEXP (x
, 0), dest_reg
)
7514 || (GET_CODE (XEXP (x
, 0)) == SUBREG
7515 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 0))
7516 && SUBREG_REG (XEXP (x
, 0)) == dest_reg
))
7518 argp
= &XEXP (x
, 1);
7520 else if (rtx_equal_p (XEXP (x
, 1), dest_reg
)
7521 || (GET_CODE (XEXP (x
, 1)) == SUBREG
7522 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 1))
7523 && SUBREG_REG (XEXP (x
, 1)) == dest_reg
))
7525 argp
= &XEXP (x
, 0);
7531 if (loop_invariant_p (loop
, arg
) != 1)
7534 /* convert_modes can emit new instructions, e.g. when arg is a loop
7535 invariant MEM and dest_reg has a different mode.
7536 These instructions would be emitted after the end of the function
7537 and then *inc_val would be an uninitialized pseudo.
7538 Detect this and bail in this case.
7539 Other alternatives to solve this can be introducing a convert_modes
7540 variant which is allowed to fail but not allowed to emit new
7541 instructions, emit these instructions before loop start and let
7542 it be garbage collected if *inc_val is never used or saving the
7543 *inc_val initialization sequence generated here and when *inc_val
7544 is going to be actually used, emit it at some suitable place. */
7545 last
= get_last_insn ();
7546 inc
= convert_modes (GET_MODE (dest_reg
), GET_MODE (x
), arg
, 0);
7547 if (get_last_insn () != last
)
7549 delete_insns_since (last
);
7554 *mult_val
= const1_rtx
;
7559 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
7560 handle addition of promoted variables.
7561 ??? The comment at the start of this function is wrong: promoted
7562 variable increments don't look like it says they do. */
7563 return basic_induction_var (loop
, SUBREG_REG (x
),
7564 GET_MODE (SUBREG_REG (x
)),
7565 dest_reg
, p
, inc_val
, mult_val
, location
);
7568 /* If this register is assigned in a previous insn, look at its
7569 source, but don't go outside the loop or past a label. */
7571 /* If this sets a register to itself, we would repeat any previous
7572 biv increment if we applied this strategy blindly. */
7573 if (rtx_equal_p (dest_reg
, x
))
7582 insn
= PREV_INSN (insn
);
7584 while (insn
&& NOTE_P (insn
)
7585 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
7589 set
= single_set (insn
);
7592 dest
= SET_DEST (set
);
7594 || (GET_CODE (dest
) == SUBREG
7595 && (GET_MODE_SIZE (GET_MODE (dest
)) <= UNITS_PER_WORD
)
7596 && (GET_MODE_CLASS (GET_MODE (dest
)) == MODE_INT
)
7597 && SUBREG_REG (dest
) == x
))
7598 return basic_induction_var (loop
, SET_SRC (set
),
7599 (GET_MODE (SET_SRC (set
)) == VOIDmode
7601 : GET_MODE (SET_SRC (set
))),
7603 inc_val
, mult_val
, location
);
7605 while (GET_CODE (dest
) == SUBREG
7606 || GET_CODE (dest
) == ZERO_EXTRACT
7607 || GET_CODE (dest
) == STRICT_LOW_PART
)
7608 dest
= XEXP (dest
, 0);
7614 /* Can accept constant setting of biv only when inside inner most loop.
7615 Otherwise, a biv of an inner loop may be incorrectly recognized
7616 as a biv of the outer loop,
7617 causing code to be moved INTO the inner loop. */
7619 if (loop_invariant_p (loop
, x
) != 1)
7624 /* convert_modes aborts if we try to convert to or from CCmode, so just
7625 exclude that case. It is very unlikely that a condition code value
7626 would be a useful iterator anyways. convert_modes aborts if we try to
7627 convert a float mode to non-float or vice versa too. */
7628 if (loop
->level
== 1
7629 && GET_MODE_CLASS (mode
) == GET_MODE_CLASS (GET_MODE (dest_reg
))
7630 && GET_MODE_CLASS (mode
) != MODE_CC
)
7632 /* Possible bug here? Perhaps we don't know the mode of X. */
7633 last
= get_last_insn ();
7634 inc
= convert_modes (GET_MODE (dest_reg
), mode
, x
, 0);
7635 if (get_last_insn () != last
)
7637 delete_insns_since (last
);
7642 *mult_val
= const0_rtx
;
7649 /* Ignore this BIV if signed arithmetic overflow is defined. */
7652 return basic_induction_var (loop
, XEXP (x
, 0), GET_MODE (XEXP (x
, 0)),
7653 dest_reg
, p
, inc_val
, mult_val
, location
);
7656 /* Similar, since this can be a sign extension. */
7657 for (insn
= PREV_INSN (p
);
7658 (insn
&& NOTE_P (insn
)
7659 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
7660 insn
= PREV_INSN (insn
))
7664 set
= single_set (insn
);
7666 if (! rtx_equal_p (dest_reg
, XEXP (x
, 0))
7667 && set
&& SET_DEST (set
) == XEXP (x
, 0)
7668 && GET_CODE (XEXP (x
, 1)) == CONST_INT
7669 && INTVAL (XEXP (x
, 1)) >= 0
7670 && GET_CODE (SET_SRC (set
)) == ASHIFT
7671 && XEXP (x
, 1) == XEXP (SET_SRC (set
), 1))
7672 return basic_induction_var (loop
, XEXP (SET_SRC (set
), 0),
7673 GET_MODE (XEXP (x
, 0)),
7674 dest_reg
, insn
, inc_val
, mult_val
,
7683 /* A general induction variable (giv) is any quantity that is a linear
7684 function of a basic induction variable,
7685 i.e. giv = biv * mult_val + add_val.
7686 The coefficients can be any loop invariant quantity.
7687 A giv need not be computed directly from the biv;
7688 it can be computed by way of other givs. */
7690 /* Determine whether X computes a giv.
7691 If it does, return a nonzero value
7692 which is the benefit from eliminating the computation of X;
7693 set *SRC_REG to the register of the biv that it is computed from;
7694 set *ADD_VAL and *MULT_VAL to the coefficients,
7695 such that the value of X is biv * mult + add; */
7698 general_induction_var (const struct loop
*loop
, rtx x
, rtx
*src_reg
,
7699 rtx
*add_val
, rtx
*mult_val
, rtx
*ext_val
,
7700 int is_addr
, int *pbenefit
,
7701 enum machine_mode addr_mode
)
7703 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
7706 /* If this is an invariant, forget it, it isn't a giv. */
7707 if (loop_invariant_p (loop
, x
) == 1)
7711 *ext_val
= NULL_RTX
;
7712 x
= simplify_giv_expr (loop
, x
, ext_val
, pbenefit
);
7716 switch (GET_CODE (x
))
7720 /* Since this is now an invariant and wasn't before, it must be a giv
7721 with MULT_VAL == 0. It doesn't matter which BIV we associate this
7723 *src_reg
= ivs
->list
->biv
->dest_reg
;
7724 *mult_val
= const0_rtx
;
7729 /* This is equivalent to a BIV. */
7731 *mult_val
= const1_rtx
;
7732 *add_val
= const0_rtx
;
7736 /* Either (plus (biv) (invar)) or
7737 (plus (mult (biv) (invar_1)) (invar_2)). */
7738 if (GET_CODE (XEXP (x
, 0)) == MULT
)
7740 *src_reg
= XEXP (XEXP (x
, 0), 0);
7741 *mult_val
= XEXP (XEXP (x
, 0), 1);
7745 *src_reg
= XEXP (x
, 0);
7746 *mult_val
= const1_rtx
;
7748 *add_val
= XEXP (x
, 1);
7752 /* ADD_VAL is zero. */
7753 *src_reg
= XEXP (x
, 0);
7754 *mult_val
= XEXP (x
, 1);
7755 *add_val
= const0_rtx
;
7762 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
7763 unless they are CONST_INT). */
7764 if (GET_CODE (*add_val
) == USE
)
7765 *add_val
= XEXP (*add_val
, 0);
7766 if (GET_CODE (*mult_val
) == USE
)
7767 *mult_val
= XEXP (*mult_val
, 0);
7770 *pbenefit
+= address_cost (orig_x
, addr_mode
) - reg_address_cost
;
7772 *pbenefit
+= rtx_cost (orig_x
, SET
);
7774 /* Always return true if this is a giv so it will be detected as such,
7775 even if the benefit is zero or negative. This allows elimination
7776 of bivs that might otherwise not be eliminated. */
7780 /* Given an expression, X, try to form it as a linear function of a biv.
7781 We will canonicalize it to be of the form
7782 (plus (mult (BIV) (invar_1))
7784 with possible degeneracies.
7786 The invariant expressions must each be of a form that can be used as a
7787 machine operand. We surround then with a USE rtx (a hack, but localized
7788 and certainly unambiguous!) if not a CONST_INT for simplicity in this
7789 routine; it is the caller's responsibility to strip them.
7791 If no such canonicalization is possible (i.e., two biv's are used or an
7792 expression that is neither invariant nor a biv or giv), this routine
7795 For a nonzero return, the result will have a code of CONST_INT, USE,
7796 REG (for a BIV), PLUS, or MULT. No other codes will occur.
7798 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
7800 static rtx
sge_plus (enum machine_mode
, rtx
, rtx
);
7801 static rtx
sge_plus_constant (rtx
, rtx
);
7804 simplify_giv_expr (const struct loop
*loop
, rtx x
, rtx
*ext_val
, int *benefit
)
7806 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
7807 struct loop_regs
*regs
= LOOP_REGS (loop
);
7808 enum machine_mode mode
= GET_MODE (x
);
7812 /* If this is not an integer mode, or if we cannot do arithmetic in this
7813 mode, this can't be a giv. */
7814 if (mode
!= VOIDmode
7815 && (GET_MODE_CLASS (mode
) != MODE_INT
7816 || GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
))
7819 switch (GET_CODE (x
))
7822 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
7823 arg1
= simplify_giv_expr (loop
, XEXP (x
, 1), ext_val
, benefit
);
7824 if (arg0
== 0 || arg1
== 0)
7827 /* Put constant last, CONST_INT last if both constant. */
7828 if ((GET_CODE (arg0
) == USE
7829 || GET_CODE (arg0
) == CONST_INT
)
7830 && ! ((GET_CODE (arg0
) == USE
7831 && GET_CODE (arg1
) == USE
)
7832 || GET_CODE (arg1
) == CONST_INT
))
7833 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
7835 /* Handle addition of zero, then addition of an invariant. */
7836 if (arg1
== const0_rtx
)
7838 else if (GET_CODE (arg1
) == CONST_INT
|| GET_CODE (arg1
) == USE
)
7839 switch (GET_CODE (arg0
))
7843 /* Adding two invariants must result in an invariant, so enclose
7844 addition operation inside a USE and return it. */
7845 if (GET_CODE (arg0
) == USE
)
7846 arg0
= XEXP (arg0
, 0);
7847 if (GET_CODE (arg1
) == USE
)
7848 arg1
= XEXP (arg1
, 0);
7850 if (GET_CODE (arg0
) == CONST_INT
)
7851 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
7852 if (GET_CODE (arg1
) == CONST_INT
)
7853 tem
= sge_plus_constant (arg0
, arg1
);
7855 tem
= sge_plus (mode
, arg0
, arg1
);
7857 if (GET_CODE (tem
) != CONST_INT
)
7858 tem
= gen_rtx_USE (mode
, tem
);
7863 /* biv + invar or mult + invar. Return sum. */
7864 return gen_rtx_PLUS (mode
, arg0
, arg1
);
7867 /* (a + invar_1) + invar_2. Associate. */
7869 simplify_giv_expr (loop
,
7881 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
7882 MULT to reduce cases. */
7884 arg0
= gen_rtx_MULT (mode
, arg0
, const1_rtx
);
7886 arg1
= gen_rtx_MULT (mode
, arg1
, const1_rtx
);
7888 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
7889 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
7890 Recurse to associate the second PLUS. */
7891 if (GET_CODE (arg1
) == MULT
)
7892 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
7894 if (GET_CODE (arg1
) == PLUS
)
7896 simplify_giv_expr (loop
,
7898 gen_rtx_PLUS (mode
, arg0
,
7903 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
7904 if (GET_CODE (arg0
) != MULT
|| GET_CODE (arg1
) != MULT
)
7907 if (!rtx_equal_p (arg0
, arg1
))
7910 return simplify_giv_expr (loop
,
7919 /* Handle "a - b" as "a + b * (-1)". */
7920 return simplify_giv_expr (loop
,
7929 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
7930 arg1
= simplify_giv_expr (loop
, XEXP (x
, 1), ext_val
, benefit
);
7931 if (arg0
== 0 || arg1
== 0)
7934 /* Put constant last, CONST_INT last if both constant. */
7935 if ((GET_CODE (arg0
) == USE
|| GET_CODE (arg0
) == CONST_INT
)
7936 && GET_CODE (arg1
) != CONST_INT
)
7937 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
7939 /* If second argument is not now constant, not giv. */
7940 if (GET_CODE (arg1
) != USE
&& GET_CODE (arg1
) != CONST_INT
)
7943 /* Handle multiply by 0 or 1. */
7944 if (arg1
== const0_rtx
)
7947 else if (arg1
== const1_rtx
)
7950 switch (GET_CODE (arg0
))
7953 /* biv * invar. Done. */
7954 return gen_rtx_MULT (mode
, arg0
, arg1
);
7957 /* Product of two constants. */
7958 return GEN_INT (INTVAL (arg0
) * INTVAL (arg1
));
7961 /* invar * invar is a giv, but attempt to simplify it somehow. */
7962 if (GET_CODE (arg1
) != CONST_INT
)
7965 arg0
= XEXP (arg0
, 0);
7966 if (GET_CODE (arg0
) == MULT
)
7968 /* (invar_0 * invar_1) * invar_2. Associate. */
7969 return simplify_giv_expr (loop
,
7978 /* Propagate the MULT expressions to the innermost nodes. */
7979 else if (GET_CODE (arg0
) == PLUS
)
7981 /* (invar_0 + invar_1) * invar_2. Distribute. */
7982 return simplify_giv_expr (loop
,
7994 return gen_rtx_USE (mode
, gen_rtx_MULT (mode
, arg0
, arg1
));
7997 /* (a * invar_1) * invar_2. Associate. */
7998 return simplify_giv_expr (loop
,
8007 /* (a + invar_1) * invar_2. Distribute. */
8008 return simplify_giv_expr (loop
,
8023 /* Shift by constant is multiply by power of two. */
8024 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
8028 simplify_giv_expr (loop
,
8031 GEN_INT ((HOST_WIDE_INT
) 1
8032 << INTVAL (XEXP (x
, 1)))),
8036 /* "-a" is "a * (-1)" */
8037 return simplify_giv_expr (loop
,
8038 gen_rtx_MULT (mode
, XEXP (x
, 0), constm1_rtx
),
8042 /* "~a" is "-a - 1". Silly, but easy. */
8043 return simplify_giv_expr (loop
,
8044 gen_rtx_MINUS (mode
,
8045 gen_rtx_NEG (mode
, XEXP (x
, 0)),
8050 /* Already in proper form for invariant. */
8056 /* Conditionally recognize extensions of simple IVs. After we've
8057 computed loop traversal counts and verified the range of the
8058 source IV, we'll reevaluate this as a GIV. */
8059 if (*ext_val
== NULL_RTX
)
8061 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
8062 if (arg0
&& *ext_val
== NULL_RTX
&& REG_P (arg0
))
8064 *ext_val
= gen_rtx_fmt_e (GET_CODE (x
), mode
, arg0
);
8071 /* If this is a new register, we can't deal with it. */
8072 if (REGNO (x
) >= max_reg_before_loop
)
8075 /* Check for biv or giv. */
8076 switch (REG_IV_TYPE (ivs
, REGNO (x
)))
8080 case GENERAL_INDUCT
:
8082 struct induction
*v
= REG_IV_INFO (ivs
, REGNO (x
));
8084 /* Form expression from giv and add benefit. Ensure this giv
8085 can derive another and subtract any needed adjustment if so. */
8087 /* Increasing the benefit here is risky. The only case in which it
8088 is arguably correct is if this is the only use of V. In other
8089 cases, this will artificially inflate the benefit of the current
8090 giv, and lead to suboptimal code. Thus, it is disabled, since
8091 potentially not reducing an only marginally beneficial giv is
8092 less harmful than reducing many givs that are not really
8095 rtx single_use
= regs
->array
[REGNO (x
)].single_usage
;
8096 if (single_use
&& single_use
!= const0_rtx
)
8097 *benefit
+= v
->benefit
;
8103 tem
= gen_rtx_PLUS (mode
, gen_rtx_MULT (mode
,
8104 v
->src_reg
, v
->mult_val
),
8107 if (v
->derive_adjustment
)
8108 tem
= gen_rtx_MINUS (mode
, tem
, v
->derive_adjustment
);
8109 arg0
= simplify_giv_expr (loop
, tem
, ext_val
, benefit
);
8112 if (!v
->ext_dependent
)
8117 *ext_val
= v
->ext_dependent
;
8125 /* If it isn't an induction variable, and it is invariant, we
8126 may be able to simplify things further by looking through
8127 the bits we just moved outside the loop. */
8128 if (loop_invariant_p (loop
, x
) == 1)
8131 struct loop_movables
*movables
= LOOP_MOVABLES (loop
);
8133 for (m
= movables
->head
; m
; m
= m
->next
)
8134 if (rtx_equal_p (x
, m
->set_dest
))
8136 /* Ok, we found a match. Substitute and simplify. */
8138 /* If we match another movable, we must use that, as
8139 this one is going away. */
8141 return simplify_giv_expr (loop
, m
->match
->set_dest
,
8144 /* If consec is nonzero, this is a member of a group of
8145 instructions that were moved together. We handle this
8146 case only to the point of seeking to the last insn and
8147 looking for a REG_EQUAL. Fail if we don't find one. */
8154 tem
= NEXT_INSN (tem
);
8158 tem
= find_reg_note (tem
, REG_EQUAL
, NULL_RTX
);
8160 tem
= XEXP (tem
, 0);
8164 tem
= single_set (m
->insn
);
8166 tem
= SET_SRC (tem
);
8171 /* What we are most interested in is pointer
8172 arithmetic on invariants -- only take
8173 patterns we may be able to do something with. */
8174 if (GET_CODE (tem
) == PLUS
8175 || GET_CODE (tem
) == MULT
8176 || GET_CODE (tem
) == ASHIFT
8177 || GET_CODE (tem
) == CONST_INT
8178 || GET_CODE (tem
) == SYMBOL_REF
)
8180 tem
= simplify_giv_expr (loop
, tem
, ext_val
,
8185 else if (GET_CODE (tem
) == CONST
8186 && GET_CODE (XEXP (tem
, 0)) == PLUS
8187 && GET_CODE (XEXP (XEXP (tem
, 0), 0)) == SYMBOL_REF
8188 && GET_CODE (XEXP (XEXP (tem
, 0), 1)) == CONST_INT
)
8190 tem
= simplify_giv_expr (loop
, XEXP (tem
, 0),
8202 /* Fall through to general case. */
8204 /* If invariant, return as USE (unless CONST_INT).
8205 Otherwise, not giv. */
8206 if (GET_CODE (x
) == USE
)
8209 if (loop_invariant_p (loop
, x
) == 1)
8211 if (GET_CODE (x
) == CONST_INT
)
8213 if (GET_CODE (x
) == CONST
8214 && GET_CODE (XEXP (x
, 0)) == PLUS
8215 && GET_CODE (XEXP (XEXP (x
, 0), 0)) == SYMBOL_REF
8216 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
)
8218 return gen_rtx_USE (mode
, x
);
8225 /* This routine folds invariants such that there is only ever one
8226 CONST_INT in the summation. It is only used by simplify_giv_expr. */
8229 sge_plus_constant (rtx x
, rtx c
)
8231 if (GET_CODE (x
) == CONST_INT
)
8232 return GEN_INT (INTVAL (x
) + INTVAL (c
));
8233 else if (GET_CODE (x
) != PLUS
)
8234 return gen_rtx_PLUS (GET_MODE (x
), x
, c
);
8235 else if (GET_CODE (XEXP (x
, 1)) == CONST_INT
)
8237 return gen_rtx_PLUS (GET_MODE (x
), XEXP (x
, 0),
8238 GEN_INT (INTVAL (XEXP (x
, 1)) + INTVAL (c
)));
8240 else if (GET_CODE (XEXP (x
, 0)) == PLUS
8241 || GET_CODE (XEXP (x
, 1)) != PLUS
)
8243 return gen_rtx_PLUS (GET_MODE (x
),
8244 sge_plus_constant (XEXP (x
, 0), c
), XEXP (x
, 1));
8248 return gen_rtx_PLUS (GET_MODE (x
),
8249 sge_plus_constant (XEXP (x
, 1), c
), XEXP (x
, 0));
8254 sge_plus (enum machine_mode mode
, rtx x
, rtx y
)
8256 while (GET_CODE (y
) == PLUS
)
8258 rtx a
= XEXP (y
, 0);
8259 if (GET_CODE (a
) == CONST_INT
)
8260 x
= sge_plus_constant (x
, a
);
8262 x
= gen_rtx_PLUS (mode
, x
, a
);
8265 if (GET_CODE (y
) == CONST_INT
)
8266 x
= sge_plus_constant (x
, y
);
8268 x
= gen_rtx_PLUS (mode
, x
, y
);
8272 /* Help detect a giv that is calculated by several consecutive insns;
8276 The caller has already identified the first insn P as having a giv as dest;
8277 we check that all other insns that set the same register follow
8278 immediately after P, that they alter nothing else,
8279 and that the result of the last is still a giv.
8281 The value is 0 if the reg set in P is not really a giv.
8282 Otherwise, the value is the amount gained by eliminating
8283 all the consecutive insns that compute the value.
8285 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
8286 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
8288 The coefficients of the ultimate giv value are stored in
8289 *MULT_VAL and *ADD_VAL. */
8292 consec_sets_giv (const struct loop
*loop
, int first_benefit
, rtx p
,
8293 rtx src_reg
, rtx dest_reg
, rtx
*add_val
, rtx
*mult_val
,
8294 rtx
*ext_val
, rtx
*last_consec_insn
)
8296 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
8297 struct loop_regs
*regs
= LOOP_REGS (loop
);
8304 /* Indicate that this is a giv so that we can update the value produced in
8305 each insn of the multi-insn sequence.
8307 This induction structure will be used only by the call to
8308 general_induction_var below, so we can allocate it on our stack.
8309 If this is a giv, our caller will replace the induct var entry with
8310 a new induction structure. */
8311 struct induction
*v
;
8313 if (REG_IV_TYPE (ivs
, REGNO (dest_reg
)) != UNKNOWN_INDUCT
)
8316 v
= alloca (sizeof (struct induction
));
8317 v
->src_reg
= src_reg
;
8318 v
->mult_val
= *mult_val
;
8319 v
->add_val
= *add_val
;
8320 v
->benefit
= first_benefit
;
8322 v
->derive_adjustment
= 0;
8323 v
->ext_dependent
= NULL_RTX
;
8325 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = GENERAL_INDUCT
;
8326 REG_IV_INFO (ivs
, REGNO (dest_reg
)) = v
;
8328 count
= regs
->array
[REGNO (dest_reg
)].n_times_set
- 1;
8333 code
= GET_CODE (p
);
8335 /* If libcall, skip to end of call sequence. */
8336 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
8340 && (set
= single_set (p
))
8341 && REG_P (SET_DEST (set
))
8342 && SET_DEST (set
) == dest_reg
8343 && (general_induction_var (loop
, SET_SRC (set
), &src_reg
,
8344 add_val
, mult_val
, ext_val
, 0,
8346 /* Giv created by equivalent expression. */
8347 || ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
8348 && general_induction_var (loop
, XEXP (temp
, 0), &src_reg
,
8349 add_val
, mult_val
, ext_val
, 0,
8350 &benefit
, VOIDmode
)))
8351 && src_reg
== v
->src_reg
)
8353 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
8354 benefit
+= libcall_benefit (p
);
8357 v
->mult_val
= *mult_val
;
8358 v
->add_val
= *add_val
;
8359 v
->benefit
+= benefit
;
8361 else if (code
!= NOTE
)
8363 /* Allow insns that set something other than this giv to a
8364 constant. Such insns are needed on machines which cannot
8365 include long constants and should not disqualify a giv. */
8367 && (set
= single_set (p
))
8368 && SET_DEST (set
) != dest_reg
8369 && CONSTANT_P (SET_SRC (set
)))
8372 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
8377 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
8378 *last_consec_insn
= p
;
8382 /* Return an rtx, if any, that expresses giv G2 as a function of the register
8383 represented by G1. If no such expression can be found, or it is clear that
8384 it cannot possibly be a valid address, 0 is returned.
8386 To perform the computation, we note that
8389 where `v' is the biv.
8391 So G2 = (y/b) * G1 + (b - a*y/x).
8393 Note that MULT = y/x.
8395 Update: A and B are now allowed to be additive expressions such that
8396 B contains all variables in A. That is, computing B-A will not require
8397 subtracting variables. */
8400 express_from_1 (rtx a
, rtx b
, rtx mult
)
8402 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
8404 if (mult
== const0_rtx
)
8407 /* If MULT is not 1, we cannot handle A with non-constants, since we
8408 would then be required to subtract multiples of the registers in A.
8409 This is theoretically possible, and may even apply to some Fortran
8410 constructs, but it is a lot of work and we do not attempt it here. */
8412 if (mult
!= const1_rtx
&& GET_CODE (a
) != CONST_INT
)
8415 /* In general these structures are sorted top to bottom (down the PLUS
8416 chain), but not left to right across the PLUS. If B is a higher
8417 order giv than A, we can strip one level and recurse. If A is higher
8418 order, we'll eventually bail out, but won't know that until the end.
8419 If they are the same, we'll strip one level around this loop. */
8421 while (GET_CODE (a
) == PLUS
&& GET_CODE (b
) == PLUS
)
8423 rtx ra
, rb
, oa
, ob
, tmp
;
8425 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
8426 if (GET_CODE (ra
) == PLUS
)
8427 tmp
= ra
, ra
= oa
, oa
= tmp
;
8429 rb
= XEXP (b
, 0), ob
= XEXP (b
, 1);
8430 if (GET_CODE (rb
) == PLUS
)
8431 tmp
= rb
, rb
= ob
, ob
= tmp
;
8433 if (rtx_equal_p (ra
, rb
))
8434 /* We matched: remove one reg completely. */
8436 else if (GET_CODE (ob
) != PLUS
&& rtx_equal_p (ra
, ob
))
8437 /* An alternate match. */
8439 else if (GET_CODE (oa
) != PLUS
&& rtx_equal_p (oa
, rb
))
8440 /* An alternate match. */
8444 /* Indicates an extra register in B. Strip one level from B and
8445 recurse, hoping B was the higher order expression. */
8446 ob
= express_from_1 (a
, ob
, mult
);
8449 return gen_rtx_PLUS (GET_MODE (b
), rb
, ob
);
8453 /* Here we are at the last level of A, go through the cases hoping to
8454 get rid of everything but a constant. */
8456 if (GET_CODE (a
) == PLUS
)
8460 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
8461 if (rtx_equal_p (oa
, b
))
8463 else if (!rtx_equal_p (ra
, b
))
8466 if (GET_CODE (oa
) != CONST_INT
)
8469 return GEN_INT (-INTVAL (oa
) * INTVAL (mult
));
8471 else if (GET_CODE (a
) == CONST_INT
)
8473 return plus_constant (b
, -INTVAL (a
) * INTVAL (mult
));
8475 else if (CONSTANT_P (a
))
8477 enum machine_mode mode_a
= GET_MODE (a
);
8478 enum machine_mode mode_b
= GET_MODE (b
);
8479 enum machine_mode mode
= mode_b
== VOIDmode
? mode_a
: mode_b
;
8480 return simplify_gen_binary (MINUS
, mode
, b
, a
);
8482 else if (GET_CODE (b
) == PLUS
)
8484 if (rtx_equal_p (a
, XEXP (b
, 0)))
8486 else if (rtx_equal_p (a
, XEXP (b
, 1)))
8491 else if (rtx_equal_p (a
, b
))
8498 express_from (struct induction
*g1
, struct induction
*g2
)
8502 /* The value that G1 will be multiplied by must be a constant integer. Also,
8503 the only chance we have of getting a valid address is if b*c/a (see above
8504 for notation) is also an integer. */
8505 if (GET_CODE (g1
->mult_val
) == CONST_INT
8506 && GET_CODE (g2
->mult_val
) == CONST_INT
)
8508 if (g1
->mult_val
== const0_rtx
8509 || (g1
->mult_val
== constm1_rtx
8510 && INTVAL (g2
->mult_val
)
8511 == (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1))
8512 || INTVAL (g2
->mult_val
) % INTVAL (g1
->mult_val
) != 0)
8514 mult
= GEN_INT (INTVAL (g2
->mult_val
) / INTVAL (g1
->mult_val
));
8516 else if (rtx_equal_p (g1
->mult_val
, g2
->mult_val
))
8520 /* ??? Find out if the one is a multiple of the other? */
8524 add
= express_from_1 (g1
->add_val
, g2
->add_val
, mult
);
8525 if (add
== NULL_RTX
)
8527 /* Failed. If we've got a multiplication factor between G1 and G2,
8528 scale G1's addend and try again. */
8529 if (INTVAL (mult
) > 1)
8531 rtx g1_add_val
= g1
->add_val
;
8532 if (GET_CODE (g1_add_val
) == MULT
8533 && GET_CODE (XEXP (g1_add_val
, 1)) == CONST_INT
)
8536 m
= INTVAL (mult
) * INTVAL (XEXP (g1_add_val
, 1));
8537 g1_add_val
= gen_rtx_MULT (GET_MODE (g1_add_val
),
8538 XEXP (g1_add_val
, 0), GEN_INT (m
));
8542 g1_add_val
= gen_rtx_MULT (GET_MODE (g1_add_val
), g1_add_val
,
8546 add
= express_from_1 (g1_add_val
, g2
->add_val
, const1_rtx
);
8549 if (add
== NULL_RTX
)
8552 /* Form simplified final result. */
8553 if (mult
== const0_rtx
)
8555 else if (mult
== const1_rtx
)
8556 mult
= g1
->dest_reg
;
8558 mult
= gen_rtx_MULT (g2
->mode
, g1
->dest_reg
, mult
);
8560 if (add
== const0_rtx
)
8564 if (GET_CODE (add
) == PLUS
8565 && CONSTANT_P (XEXP (add
, 1)))
8567 rtx tem
= XEXP (add
, 1);
8568 mult
= gen_rtx_PLUS (g2
->mode
, mult
, XEXP (add
, 0));
8572 return gen_rtx_PLUS (g2
->mode
, mult
, add
);
8576 /* Return an rtx, if any, that expresses giv G2 as a function of the register
8577 represented by G1. This indicates that G2 should be combined with G1 and
8578 that G2 can use (either directly or via an address expression) a register
8579 used to represent G1. */
8582 combine_givs_p (struct induction
*g1
, struct induction
*g2
)
8586 /* With the introduction of ext dependent givs, we must care for modes.
8587 G2 must not use a wider mode than G1. */
8588 if (GET_MODE_SIZE (g1
->mode
) < GET_MODE_SIZE (g2
->mode
))
8591 ret
= comb
= express_from (g1
, g2
);
8592 if (comb
== NULL_RTX
)
8594 if (g1
->mode
!= g2
->mode
)
8595 ret
= gen_lowpart (g2
->mode
, comb
);
8597 /* If these givs are identical, they can be combined. We use the results
8598 of express_from because the addends are not in a canonical form, so
8599 rtx_equal_p is a weaker test. */
8600 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
8601 combination to be the other way round. */
8602 if (comb
== g1
->dest_reg
8603 && (g1
->giv_type
== DEST_REG
|| g2
->giv_type
== DEST_ADDR
))
8608 /* If G2 can be expressed as a function of G1 and that function is valid
8609 as an address and no more expensive than using a register for G2,
8610 the expression of G2 in terms of G1 can be used. */
8612 && g2
->giv_type
== DEST_ADDR
8613 && memory_address_p (GET_MODE (g2
->mem
), ret
))
8619 /* See if BL is monotonic and has a constant per-iteration increment.
8620 Return the increment if so, otherwise return 0. */
8622 static HOST_WIDE_INT
8623 get_monotonic_increment (struct iv_class
*bl
)
8625 struct induction
*v
;
8628 /* Get the total increment and check that it is constant. */
8629 incr
= biv_total_increment (bl
);
8630 if (incr
== 0 || GET_CODE (incr
) != CONST_INT
)
8633 for (v
= bl
->biv
; v
!= 0; v
= v
->next_iv
)
8635 if (GET_CODE (v
->add_val
) != CONST_INT
)
8638 if (INTVAL (v
->add_val
) < 0 && INTVAL (incr
) >= 0)
8641 if (INTVAL (v
->add_val
) > 0 && INTVAL (incr
) <= 0)
8644 return INTVAL (incr
);
8648 /* Subroutine of biv_fits_mode_p. Return true if biv BL, when biased by
8649 BIAS, will never exceed the unsigned range of MODE. LOOP is the loop
8650 to which the biv belongs and INCR is its per-iteration increment. */
8653 biased_biv_fits_mode_p (const struct loop
*loop
, struct iv_class
*bl
,
8654 HOST_WIDE_INT incr
, enum machine_mode mode
,
8655 unsigned HOST_WIDE_INT bias
)
8657 unsigned HOST_WIDE_INT initial
, maximum
, span
, delta
;
8659 /* We need to be able to manipulate MODE-size constants. */
8660 if (HOST_BITS_PER_WIDE_INT
< GET_MODE_BITSIZE (mode
))
8663 /* The number of loop iterations must be constant. */
8664 if (LOOP_INFO (loop
)->n_iterations
== 0)
8667 /* So must the biv's initial value. */
8668 if (bl
->initial_value
== 0 || GET_CODE (bl
->initial_value
) != CONST_INT
)
8671 initial
= bias
+ INTVAL (bl
->initial_value
);
8672 maximum
= GET_MODE_MASK (mode
);
8674 /* Make sure that the initial value is within range. */
8675 if (initial
> maximum
)
8678 /* Set up DELTA and SPAN such that the number of iterations * DELTA
8679 (calculated to arbitrary precision) must be <= SPAN. */
8688 /* Handle the special case in which MAXIMUM is the largest
8689 unsigned HOST_WIDE_INT and INITIAL is 0. */
8690 if (maximum
+ 1 == initial
)
8691 span
= LOOP_INFO (loop
)->n_iterations
* delta
;
8693 span
= maximum
+ 1 - initial
;
8695 return (span
/ LOOP_INFO (loop
)->n_iterations
>= delta
);
8699 /* Return true if biv BL will never exceed the bounds of MODE. LOOP is
8700 the loop to which BL belongs and INCR is its per-iteration increment.
8701 UNSIGNEDP is true if the biv should be treated as unsigned. */
8704 biv_fits_mode_p (const struct loop
*loop
, struct iv_class
*bl
,
8705 HOST_WIDE_INT incr
, enum machine_mode mode
, bool unsignedp
)
8707 struct loop_info
*loop_info
;
8708 unsigned HOST_WIDE_INT bias
;
8710 /* A biv's value will always be limited to its natural mode.
8711 Larger modes will observe the same wrap-around. */
8712 if (GET_MODE_SIZE (mode
) > GET_MODE_SIZE (GET_MODE (bl
->biv
->src_reg
)))
8713 mode
= GET_MODE (bl
->biv
->src_reg
);
8715 loop_info
= LOOP_INFO (loop
);
8717 bias
= (unsignedp
? 0 : (GET_MODE_MASK (mode
) >> 1) + 1);
8718 if (biased_biv_fits_mode_p (loop
, bl
, incr
, mode
, bias
))
8721 if (mode
== GET_MODE (bl
->biv
->src_reg
)
8722 && bl
->biv
->src_reg
== loop_info
->iteration_var
8723 && loop_info
->comparison_value
8724 && loop_invariant_p (loop
, loop_info
->comparison_value
))
8726 /* If the increment is +1, and the exit test is a <, the BIV
8727 cannot overflow. (For <=, we have the problematic case that
8728 the comparison value might be the maximum value of the range.) */
8731 if (loop_info
->comparison_code
== LT
)
8733 if (loop_info
->comparison_code
== LTU
&& unsignedp
)
8737 /* Likewise for increment -1 and exit test >. */
8740 if (loop_info
->comparison_code
== GT
)
8742 if (loop_info
->comparison_code
== GTU
&& unsignedp
)
8750 /* Given that X is an extension or truncation of BL, return true
8751 if it is unaffected by overflow. LOOP is the loop to which
8752 BL belongs and INCR is its per-iteration increment. */
8755 extension_within_bounds_p (const struct loop
*loop
, struct iv_class
*bl
,
8756 HOST_WIDE_INT incr
, rtx x
)
8758 enum machine_mode mode
;
8759 bool signedp
, unsignedp
;
8761 switch (GET_CODE (x
))
8765 mode
= GET_MODE (XEXP (x
, 0));
8766 signedp
= (GET_CODE (x
) == SIGN_EXTEND
);
8767 unsignedp
= (GET_CODE (x
) == ZERO_EXTEND
);
8771 /* We don't know whether this value is being used as signed
8772 or unsigned, so check the conditions for both. */
8773 mode
= GET_MODE (x
);
8774 signedp
= unsignedp
= true;
8781 return ((!signedp
|| biv_fits_mode_p (loop
, bl
, incr
, mode
, false))
8782 && (!unsignedp
|| biv_fits_mode_p (loop
, bl
, incr
, mode
, true)));
8786 /* Check each extension dependent giv in this class to see if its
8787 root biv is safe from wrapping in the interior mode, which would
8788 make the giv illegal. */
8791 check_ext_dependent_givs (const struct loop
*loop
, struct iv_class
*bl
)
8793 struct induction
*v
;
8796 incr
= get_monotonic_increment (bl
);
8798 /* Invalidate givs that fail the tests. */
8799 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8800 if (v
->ext_dependent
)
8803 && extension_within_bounds_p (loop
, bl
, incr
, v
->ext_dependent
))
8805 if (loop_dump_stream
)
8806 fprintf (loop_dump_stream
,
8807 "Verified ext dependent giv at %d of reg %d\n",
8808 INSN_UID (v
->insn
), bl
->regno
);
8812 if (loop_dump_stream
)
8813 fprintf (loop_dump_stream
,
8814 "Failed ext dependent giv at %d\n",
8815 INSN_UID (v
->insn
));
8818 bl
->all_reduced
= 0;
8823 /* Generate a version of VALUE in a mode appropriate for initializing V. */
8826 extend_value_for_giv (struct induction
*v
, rtx value
)
8828 rtx ext_dep
= v
->ext_dependent
;
8833 /* Recall that check_ext_dependent_givs verified that the known bounds
8834 of a biv did not overflow or wrap with respect to the extension for
8835 the giv. Therefore, constants need no additional adjustment. */
8836 if (CONSTANT_P (value
) && GET_MODE (value
) == VOIDmode
)
8839 /* Otherwise, we must adjust the value to compensate for the
8840 differing modes of the biv and the giv. */
8841 return gen_rtx_fmt_e (GET_CODE (ext_dep
), GET_MODE (ext_dep
), value
);
8844 struct combine_givs_stats
8851 cmp_combine_givs_stats (const void *xp
, const void *yp
)
8853 const struct combine_givs_stats
* const x
=
8854 (const struct combine_givs_stats
*) xp
;
8855 const struct combine_givs_stats
* const y
=
8856 (const struct combine_givs_stats
*) yp
;
8858 d
= y
->total_benefit
- x
->total_benefit
;
8859 /* Stabilize the sort. */
8861 d
= x
->giv_number
- y
->giv_number
;
8865 /* Check all pairs of givs for iv_class BL and see if any can be combined with
8866 any other. If so, point SAME to the giv combined with and set NEW_REG to
8867 be an expression (in terms of the other giv's DEST_REG) equivalent to the
8868 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
8871 combine_givs (struct loop_regs
*regs
, struct iv_class
*bl
)
8873 /* Additional benefit to add for being combined multiple times. */
8874 const int extra_benefit
= 3;
8876 struct induction
*g1
, *g2
, **giv_array
;
8877 int i
, j
, k
, giv_count
;
8878 struct combine_givs_stats
*stats
;
8881 /* Count givs, because bl->giv_count is incorrect here. */
8883 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
8887 giv_array
= alloca (giv_count
* sizeof (struct induction
*));
8889 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
8891 giv_array
[i
++] = g1
;
8893 stats
= xcalloc (giv_count
, sizeof (*stats
));
8894 can_combine
= xcalloc (giv_count
, giv_count
* sizeof (rtx
));
8896 for (i
= 0; i
< giv_count
; i
++)
8902 stats
[i
].giv_number
= i
;
8904 /* If a DEST_REG GIV is used only once, do not allow it to combine
8905 with anything, for in doing so we will gain nothing that cannot
8906 be had by simply letting the GIV with which we would have combined
8907 to be reduced on its own. The losage shows up in particular with
8908 DEST_ADDR targets on hosts with reg+reg addressing, though it can
8909 be seen elsewhere as well. */
8910 if (g1
->giv_type
== DEST_REG
8911 && (single_use
= regs
->array
[REGNO (g1
->dest_reg
)].single_usage
)
8912 && single_use
!= const0_rtx
)
8915 this_benefit
= g1
->benefit
;
8916 /* Add an additional weight for zero addends. */
8917 if (g1
->no_const_addval
)
8920 for (j
= 0; j
< giv_count
; j
++)
8926 && (this_combine
= combine_givs_p (g1
, g2
)) != NULL_RTX
)
8928 can_combine
[i
* giv_count
+ j
] = this_combine
;
8929 this_benefit
+= g2
->benefit
+ extra_benefit
;
8932 stats
[i
].total_benefit
= this_benefit
;
8935 /* Iterate, combining until we can't. */
8937 qsort (stats
, giv_count
, sizeof (*stats
), cmp_combine_givs_stats
);
8939 if (loop_dump_stream
)
8941 fprintf (loop_dump_stream
, "Sorted combine statistics:\n");
8942 for (k
= 0; k
< giv_count
; k
++)
8944 g1
= giv_array
[stats
[k
].giv_number
];
8945 if (!g1
->combined_with
&& !g1
->same
)
8946 fprintf (loop_dump_stream
, " {%d, %d}",
8947 INSN_UID (giv_array
[stats
[k
].giv_number
]->insn
),
8948 stats
[k
].total_benefit
);
8950 putc ('\n', loop_dump_stream
);
8953 for (k
= 0; k
< giv_count
; k
++)
8955 int g1_add_benefit
= 0;
8957 i
= stats
[k
].giv_number
;
8960 /* If it has already been combined, skip. */
8961 if (g1
->combined_with
|| g1
->same
)
8964 for (j
= 0; j
< giv_count
; j
++)
8967 if (g1
!= g2
&& can_combine
[i
* giv_count
+ j
]
8968 /* If it has already been combined, skip. */
8969 && ! g2
->same
&& ! g2
->combined_with
)
8973 g2
->new_reg
= can_combine
[i
* giv_count
+ j
];
8975 /* For destination, we now may replace by mem expression instead
8976 of register. This changes the costs considerably, so add the
8978 if (g2
->giv_type
== DEST_ADDR
)
8979 g2
->benefit
= (g2
->benefit
+ reg_address_cost
8980 - address_cost (g2
->new_reg
,
8981 GET_MODE (g2
->mem
)));
8982 g1
->combined_with
++;
8983 g1
->lifetime
+= g2
->lifetime
;
8985 g1_add_benefit
+= g2
->benefit
;
8987 /* ??? The new final_[bg]iv_value code does a much better job
8988 of finding replaceable giv's, and hence this code may no
8989 longer be necessary. */
8990 if (! g2
->replaceable
&& REG_USERVAR_P (g2
->dest_reg
))
8991 g1_add_benefit
-= copy_cost
;
8993 /* To help optimize the next set of combinations, remove
8994 this giv from the benefits of other potential mates. */
8995 for (l
= 0; l
< giv_count
; ++l
)
8997 int m
= stats
[l
].giv_number
;
8998 if (can_combine
[m
* giv_count
+ j
])
8999 stats
[l
].total_benefit
-= g2
->benefit
+ extra_benefit
;
9002 if (loop_dump_stream
)
9003 fprintf (loop_dump_stream
,
9004 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
9005 INSN_UID (g2
->insn
), INSN_UID (g1
->insn
),
9006 g1
->benefit
, g1_add_benefit
, g1
->lifetime
);
9010 /* To help optimize the next set of combinations, remove
9011 this giv from the benefits of other potential mates. */
9012 if (g1
->combined_with
)
9014 for (j
= 0; j
< giv_count
; ++j
)
9016 int m
= stats
[j
].giv_number
;
9017 if (can_combine
[m
* giv_count
+ i
])
9018 stats
[j
].total_benefit
-= g1
->benefit
+ extra_benefit
;
9021 g1
->benefit
+= g1_add_benefit
;
9023 /* We've finished with this giv, and everything it touched.
9024 Restart the combination so that proper weights for the
9025 rest of the givs are properly taken into account. */
9026 /* ??? Ideally we would compact the arrays at this point, so
9027 as to not cover old ground. But sanely compacting
9028 can_combine is tricky. */
9038 /* Generate sequence for REG = B * M + A. B is the initial value of
9039 the basic induction variable, M a multiplicative constant, A an
9040 additive constant and REG the destination register. */
9043 gen_add_mult (rtx b
, rtx m
, rtx a
, rtx reg
)
9049 /* Use unsigned arithmetic. */
9050 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 1);
9052 emit_move_insn (reg
, result
);
9060 /* Update registers created in insn sequence SEQ. */
9063 loop_regs_update (const struct loop
*loop ATTRIBUTE_UNUSED
, rtx seq
)
9067 /* Update register info for alias analysis. */
9070 while (insn
!= NULL_RTX
)
9072 rtx set
= single_set (insn
);
9074 if (set
&& REG_P (SET_DEST (set
)))
9075 record_base_value (REGNO (SET_DEST (set
)), SET_SRC (set
), 0);
9077 insn
= NEXT_INSN (insn
);
9082 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. B
9083 is the initial value of the basic induction variable, M a
9084 multiplicative constant, A an additive constant and REG the
9085 destination register. */
9088 loop_iv_add_mult_emit_before (const struct loop
*loop
, rtx b
, rtx m
, rtx a
,
9089 rtx reg
, basic_block before_bb
, rtx before_insn
)
9095 loop_iv_add_mult_hoist (loop
, b
, m
, a
, reg
);
9099 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9100 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
9102 /* Increase the lifetime of any invariants moved further in code. */
9103 update_reg_last_use (a
, before_insn
);
9104 update_reg_last_use (b
, before_insn
);
9105 update_reg_last_use (m
, before_insn
);
9107 /* It is possible that the expansion created lots of new registers.
9108 Iterate over the sequence we just created and record them all. We
9109 must do this before inserting the sequence. */
9110 loop_regs_update (loop
, seq
);
9112 loop_insn_emit_before (loop
, before_bb
, before_insn
, seq
);
9116 /* Emit insns in loop pre-header to set REG = B * M + A. B is the
9117 initial value of the basic induction variable, M a multiplicative
9118 constant, A an additive constant and REG the destination
9122 loop_iv_add_mult_sink (const struct loop
*loop
, rtx b
, rtx m
, rtx a
, rtx reg
)
9126 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9127 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
9129 /* Increase the lifetime of any invariants moved further in code.
9130 ???? Is this really necessary? */
9131 update_reg_last_use (a
, loop
->sink
);
9132 update_reg_last_use (b
, loop
->sink
);
9133 update_reg_last_use (m
, loop
->sink
);
9135 /* It is possible that the expansion created lots of new registers.
9136 Iterate over the sequence we just created and record them all. We
9137 must do this before inserting the sequence. */
9138 loop_regs_update (loop
, seq
);
9140 loop_insn_sink (loop
, seq
);
9144 /* Emit insns after loop to set REG = B * M + A. B is the initial
9145 value of the basic induction variable, M a multiplicative constant,
9146 A an additive constant and REG the destination register. */
9149 loop_iv_add_mult_hoist (const struct loop
*loop
, rtx b
, rtx m
, rtx a
, rtx reg
)
9153 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
9154 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
9156 /* It is possible that the expansion created lots of new registers.
9157 Iterate over the sequence we just created and record them all. We
9158 must do this before inserting the sequence. */
9159 loop_regs_update (loop
, seq
);
9161 loop_insn_hoist (loop
, seq
);
9166 /* Similar to gen_add_mult, but compute cost rather than generating
9170 iv_add_mult_cost (rtx b
, rtx m
, rtx a
, rtx reg
)
9176 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 1);
9178 emit_move_insn (reg
, result
);
9179 last
= get_last_insn ();
9182 rtx t
= single_set (last
);
9184 cost
+= rtx_cost (SET_SRC (t
), SET
);
9185 last
= PREV_INSN (last
);
9191 /* Test whether A * B can be computed without
9192 an actual multiply insn. Value is 1 if so.
9194 ??? This function stinks because it generates a ton of wasted RTL
9195 ??? and as a result fragments GC memory to no end. There are other
9196 ??? places in the compiler which are invoked a lot and do the same
9197 ??? thing, generate wasted RTL just to see if something is possible. */
9200 product_cheap_p (rtx a
, rtx b
)
9205 /* If only one is constant, make it B. */
9206 if (GET_CODE (a
) == CONST_INT
)
9207 tmp
= a
, a
= b
, b
= tmp
;
9209 /* If first constant, both constant, so don't need multiply. */
9210 if (GET_CODE (a
) == CONST_INT
)
9213 /* If second not constant, neither is constant, so would need multiply. */
9214 if (GET_CODE (b
) != CONST_INT
)
9217 /* One operand is constant, so might not need multiply insn. Generate the
9218 code for the multiply and see if a call or multiply, or long sequence
9219 of insns is generated. */
9222 expand_mult (GET_MODE (a
), a
, b
, NULL_RTX
, 1);
9227 if (tmp
== NULL_RTX
)
9229 else if (INSN_P (tmp
))
9232 while (tmp
!= NULL_RTX
)
9234 rtx next
= NEXT_INSN (tmp
);
9237 || !NONJUMP_INSN_P (tmp
)
9238 || (GET_CODE (PATTERN (tmp
)) == SET
9239 && GET_CODE (SET_SRC (PATTERN (tmp
))) == MULT
)
9240 || (GET_CODE (PATTERN (tmp
)) == PARALLEL
9241 && GET_CODE (XVECEXP (PATTERN (tmp
), 0, 0)) == SET
9242 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp
), 0, 0))) == MULT
))
9251 else if (GET_CODE (tmp
) == SET
9252 && GET_CODE (SET_SRC (tmp
)) == MULT
)
9254 else if (GET_CODE (tmp
) == PARALLEL
9255 && GET_CODE (XVECEXP (tmp
, 0, 0)) == SET
9256 && GET_CODE (SET_SRC (XVECEXP (tmp
, 0, 0))) == MULT
)
9262 /* Check to see if loop can be terminated by a "decrement and branch until
9263 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
9264 Also try reversing an increment loop to a decrement loop
9265 to see if the optimization can be performed.
9266 Value is nonzero if optimization was performed. */
9268 /* This is useful even if the architecture doesn't have such an insn,
9269 because it might change a loops which increments from 0 to n to a loop
9270 which decrements from n to 0. A loop that decrements to zero is usually
9271 faster than one that increments from zero. */
9273 /* ??? This could be rewritten to use some of the loop unrolling procedures,
9274 such as approx_final_value, biv_total_increment, loop_iterations, and
9275 final_[bg]iv_value. */
9278 check_dbra_loop (struct loop
*loop
, int insn_count
)
9280 struct loop_info
*loop_info
= LOOP_INFO (loop
);
9281 struct loop_regs
*regs
= LOOP_REGS (loop
);
9282 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
9283 struct iv_class
*bl
;
9285 enum machine_mode mode
;
9291 rtx before_comparison
;
9295 int compare_and_branch
;
9296 rtx loop_start
= loop
->start
;
9297 rtx loop_end
= loop
->end
;
9299 /* If last insn is a conditional branch, and the insn before tests a
9300 register value, try to optimize it. Otherwise, we can't do anything. */
9302 jump
= PREV_INSN (loop_end
);
9303 comparison
= get_condition_for_loop (loop
, jump
);
9304 if (comparison
== 0)
9306 if (!onlyjump_p (jump
))
9309 /* Try to compute whether the compare/branch at the loop end is one or
9310 two instructions. */
9311 get_condition (jump
, &first_compare
, false, true);
9312 if (first_compare
== jump
)
9313 compare_and_branch
= 1;
9314 else if (first_compare
== prev_nonnote_insn (jump
))
9315 compare_and_branch
= 2;
9320 /* If more than one condition is present to control the loop, then
9321 do not proceed, as this function does not know how to rewrite
9322 loop tests with more than one condition.
9324 Look backwards from the first insn in the last comparison
9325 sequence and see if we've got another comparison sequence. */
9328 if ((jump1
= prev_nonnote_insn (first_compare
))
9333 /* Check all of the bivs to see if the compare uses one of them.
9334 Skip biv's set more than once because we can't guarantee that
9335 it will be zero on the last iteration. Also skip if the biv is
9336 used between its update and the test insn. */
9338 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
9340 if (bl
->biv_count
== 1
9341 && ! bl
->biv
->maybe_multiple
9342 && bl
->biv
->dest_reg
== XEXP (comparison
, 0)
9343 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
9348 /* Try swapping the comparison to identify a suitable biv. */
9350 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
9351 if (bl
->biv_count
== 1
9352 && ! bl
->biv
->maybe_multiple
9353 && bl
->biv
->dest_reg
== XEXP (comparison
, 1)
9354 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
9357 comparison
= gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison
)),
9359 XEXP (comparison
, 1),
9360 XEXP (comparison
, 0));
9367 /* Look for the case where the basic induction variable is always
9368 nonnegative, and equals zero on the last iteration.
9369 In this case, add a reg_note REG_NONNEG, which allows the
9370 m68k DBRA instruction to be used. */
9372 if (((GET_CODE (comparison
) == GT
&& XEXP (comparison
, 1) == constm1_rtx
)
9373 || (GET_CODE (comparison
) == NE
&& XEXP (comparison
, 1) == const0_rtx
))
9374 && GET_CODE (bl
->biv
->add_val
) == CONST_INT
9375 && INTVAL (bl
->biv
->add_val
) < 0)
9377 /* Initial value must be greater than 0,
9378 init_val % -dec_value == 0 to ensure that it equals zero on
9379 the last iteration */
9381 if (GET_CODE (bl
->initial_value
) == CONST_INT
9382 && INTVAL (bl
->initial_value
) > 0
9383 && (INTVAL (bl
->initial_value
)
9384 % (-INTVAL (bl
->biv
->add_val
))) == 0)
9386 /* Register always nonnegative, add REG_NOTE to branch. */
9387 if (! find_reg_note (jump
, REG_NONNEG
, NULL_RTX
))
9389 = gen_rtx_EXPR_LIST (REG_NONNEG
, bl
->biv
->dest_reg
,
9396 /* If the decrement is 1 and the value was tested as >= 0 before
9397 the loop, then we can safely optimize. */
9398 for (p
= loop_start
; p
; p
= PREV_INSN (p
))
9405 before_comparison
= get_condition_for_loop (loop
, p
);
9406 if (before_comparison
9407 && XEXP (before_comparison
, 0) == bl
->biv
->dest_reg
9408 && (GET_CODE (before_comparison
) == LT
9409 || GET_CODE (before_comparison
) == LTU
)
9410 && XEXP (before_comparison
, 1) == const0_rtx
9411 && ! reg_set_between_p (bl
->biv
->dest_reg
, p
, loop_start
)
9412 && INTVAL (bl
->biv
->add_val
) == -1)
9414 if (! find_reg_note (jump
, REG_NONNEG
, NULL_RTX
))
9416 = gen_rtx_EXPR_LIST (REG_NONNEG
, bl
->biv
->dest_reg
,
9424 else if (GET_CODE (bl
->biv
->add_val
) == CONST_INT
9425 && INTVAL (bl
->biv
->add_val
) > 0)
9427 /* Try to change inc to dec, so can apply above optimization. */
9429 all registers modified are induction variables or invariant,
9430 all memory references have non-overlapping addresses
9431 (obviously true if only one write)
9432 allow 2 insns for the compare/jump at the end of the loop. */
9433 /* Also, we must avoid any instructions which use both the reversed
9434 biv and another biv. Such instructions will fail if the loop is
9435 reversed. We meet this condition by requiring that either
9436 no_use_except_counting is true, or else that there is only
9438 int num_nonfixed_reads
= 0;
9439 /* 1 if the iteration var is used only to count iterations. */
9440 int no_use_except_counting
= 0;
9441 /* 1 if the loop has no memory store, or it has a single memory store
9442 which is reversible. */
9443 int reversible_mem_store
= 1;
9445 if (bl
->giv_count
== 0
9446 && !loop
->exit_count
9447 && !loop_info
->has_multiple_exit_targets
)
9449 rtx bivreg
= regno_reg_rtx
[bl
->regno
];
9450 struct iv_class
*blt
;
9452 /* If there are no givs for this biv, and the only exit is the
9453 fall through at the end of the loop, then
9454 see if perhaps there are no uses except to count. */
9455 no_use_except_counting
= 1;
9456 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
9459 rtx set
= single_set (p
);
9461 if (set
&& REG_P (SET_DEST (set
))
9462 && REGNO (SET_DEST (set
)) == bl
->regno
)
9463 /* An insn that sets the biv is okay. */
9465 else if (!reg_mentioned_p (bivreg
, PATTERN (p
)))
9466 /* An insn that doesn't mention the biv is okay. */
9468 else if (p
== prev_nonnote_insn (prev_nonnote_insn (loop_end
))
9469 || p
== prev_nonnote_insn (loop_end
))
9471 /* If either of these insns uses the biv and sets a pseudo
9472 that has more than one usage, then the biv has uses
9473 other than counting since it's used to derive a value
9474 that is used more than one time. */
9475 note_stores (PATTERN (p
), note_set_pseudo_multiple_uses
,
9477 if (regs
->multiple_uses
)
9479 no_use_except_counting
= 0;
9485 no_use_except_counting
= 0;
9490 /* A biv has uses besides counting if it is used to set
9492 for (blt
= ivs
->list
; blt
; blt
= blt
->next
)
9494 && reg_mentioned_p (bivreg
, SET_SRC (blt
->init_set
)))
9496 no_use_except_counting
= 0;
9501 if (no_use_except_counting
)
9502 /* No need to worry about MEMs. */
9504 else if (loop_info
->num_mem_sets
<= 1)
9506 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
9508 num_nonfixed_reads
+= count_nonfixed_reads (loop
, PATTERN (p
));
9510 /* If the loop has a single store, and the destination address is
9511 invariant, then we can't reverse the loop, because this address
9512 might then have the wrong value at loop exit.
9513 This would work if the source was invariant also, however, in that
9514 case, the insn should have been moved out of the loop. */
9516 if (loop_info
->num_mem_sets
== 1)
9518 struct induction
*v
;
9520 /* If we could prove that each of the memory locations
9521 written to was different, then we could reverse the
9522 store -- but we don't presently have any way of
9524 reversible_mem_store
= 0;
9526 /* If the store depends on a register that is set after the
9527 store, it depends on the initial value, and is thus not
9529 for (v
= bl
->giv
; reversible_mem_store
&& v
; v
= v
->next_iv
)
9531 if (v
->giv_type
== DEST_REG
9532 && reg_mentioned_p (v
->dest_reg
,
9533 PATTERN (loop_info
->first_loop_store_insn
))
9534 && loop_insn_first_p (loop_info
->first_loop_store_insn
,
9536 reversible_mem_store
= 0;
9543 /* This code only acts for innermost loops. Also it simplifies
9544 the memory address check by only reversing loops with
9545 zero or one memory access.
9546 Two memory accesses could involve parts of the same array,
9547 and that can't be reversed.
9548 If the biv is used only for counting, than we don't need to worry
9549 about all these things. */
9551 if ((num_nonfixed_reads
<= 1
9552 && ! loop_info
->has_nonconst_call
9553 && ! loop_info
->has_prefetch
9554 && ! loop_info
->has_volatile
9555 && reversible_mem_store
9556 && (bl
->giv_count
+ bl
->biv_count
+ loop_info
->num_mem_sets
9557 + num_unmoved_movables (loop
) + compare_and_branch
== insn_count
)
9558 && (bl
== ivs
->list
&& bl
->next
== 0))
9559 || (no_use_except_counting
&& ! loop_info
->has_prefetch
))
9563 /* Loop can be reversed. */
9564 if (loop_dump_stream
)
9565 fprintf (loop_dump_stream
, "Can reverse loop\n");
9567 /* Now check other conditions:
9569 The increment must be a constant, as must the initial value,
9570 and the comparison code must be LT.
9572 This test can probably be improved since +/- 1 in the constant
9573 can be obtained by changing LT to LE and vice versa; this is
9577 /* for constants, LE gets turned into LT */
9578 && (GET_CODE (comparison
) == LT
9579 || (GET_CODE (comparison
) == LE
9580 && no_use_except_counting
)
9581 || GET_CODE (comparison
) == LTU
))
9583 HOST_WIDE_INT add_val
, add_adjust
, comparison_val
= 0;
9584 rtx initial_value
, comparison_value
;
9586 enum rtx_code cmp_code
;
9587 int comparison_const_width
;
9588 unsigned HOST_WIDE_INT comparison_sign_mask
;
9589 bool keep_first_compare
;
9591 add_val
= INTVAL (bl
->biv
->add_val
);
9592 comparison_value
= XEXP (comparison
, 1);
9593 if (GET_MODE (comparison_value
) == VOIDmode
)
9594 comparison_const_width
9595 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison
, 0)));
9597 comparison_const_width
9598 = GET_MODE_BITSIZE (GET_MODE (comparison_value
));
9599 if (comparison_const_width
> HOST_BITS_PER_WIDE_INT
)
9600 comparison_const_width
= HOST_BITS_PER_WIDE_INT
;
9601 comparison_sign_mask
9602 = (unsigned HOST_WIDE_INT
) 1 << (comparison_const_width
- 1);
9604 /* If the comparison value is not a loop invariant, then we
9605 can not reverse this loop.
9607 ??? If the insns which initialize the comparison value as
9608 a whole compute an invariant result, then we could move
9609 them out of the loop and proceed with loop reversal. */
9610 if (! loop_invariant_p (loop
, comparison_value
))
9613 if (GET_CODE (comparison_value
) == CONST_INT
)
9614 comparison_val
= INTVAL (comparison_value
);
9615 initial_value
= bl
->initial_value
;
9617 /* Normalize the initial value if it is an integer and
9618 has no other use except as a counter. This will allow
9619 a few more loops to be reversed. */
9620 if (no_use_except_counting
9621 && GET_CODE (comparison_value
) == CONST_INT
9622 && GET_CODE (initial_value
) == CONST_INT
)
9624 comparison_val
= comparison_val
- INTVAL (bl
->initial_value
);
9625 /* The code below requires comparison_val to be a multiple
9626 of add_val in order to do the loop reversal, so
9627 round up comparison_val to a multiple of add_val.
9628 Since comparison_value is constant, we know that the
9629 current comparison code is LT. */
9630 comparison_val
= comparison_val
+ add_val
- 1;
9632 -= (unsigned HOST_WIDE_INT
) comparison_val
% add_val
;
9633 /* We postpone overflow checks for COMPARISON_VAL here;
9634 even if there is an overflow, we might still be able to
9635 reverse the loop, if converting the loop exit test to
9637 initial_value
= const0_rtx
;
9640 /* First check if we can do a vanilla loop reversal. */
9641 if (initial_value
== const0_rtx
9642 && GET_CODE (comparison_value
) == CONST_INT
9643 /* Now do postponed overflow checks on COMPARISON_VAL. */
9644 && ! (((comparison_val
- add_val
) ^ INTVAL (comparison_value
))
9645 & comparison_sign_mask
))
9647 /* Register will always be nonnegative, with value
9648 0 on last iteration */
9649 add_adjust
= add_val
;
9656 if (GET_CODE (comparison
) == LE
)
9657 add_adjust
-= add_val
;
9659 /* If the initial value is not zero, or if the comparison
9660 value is not an exact multiple of the increment, then we
9661 can not reverse this loop. */
9662 if (initial_value
== const0_rtx
9663 && GET_CODE (comparison_value
) == CONST_INT
)
9665 if (((unsigned HOST_WIDE_INT
) comparison_val
% add_val
) != 0)
9670 if (! no_use_except_counting
|| add_val
!= 1)
9674 final_value
= comparison_value
;
9676 /* Reset these in case we normalized the initial value
9677 and comparison value above. */
9678 if (GET_CODE (comparison_value
) == CONST_INT
9679 && GET_CODE (initial_value
) == CONST_INT
)
9681 comparison_value
= GEN_INT (comparison_val
);
9683 = GEN_INT (comparison_val
+ INTVAL (bl
->initial_value
));
9685 bl
->initial_value
= initial_value
;
9687 /* Save some info needed to produce the new insns. */
9688 reg
= bl
->biv
->dest_reg
;
9689 mode
= GET_MODE (reg
);
9690 jump_label
= condjump_label (PREV_INSN (loop_end
));
9691 new_add_val
= GEN_INT (-INTVAL (bl
->biv
->add_val
));
9693 /* Set start_value; if this is not a CONST_INT, we need
9695 Initialize biv to start_value before loop start.
9696 The old initializing insn will be deleted as a
9697 dead store by flow.c. */
9698 if (initial_value
== const0_rtx
9699 && GET_CODE (comparison_value
) == CONST_INT
)
9702 = gen_int_mode (comparison_val
- add_adjust
, mode
);
9703 loop_insn_hoist (loop
, gen_move_insn (reg
, start_value
));
9705 else if (GET_CODE (initial_value
) == CONST_INT
)
9707 rtx offset
= GEN_INT (-INTVAL (initial_value
) - add_adjust
);
9708 rtx add_insn
= gen_add3_insn (reg
, comparison_value
, offset
);
9714 = gen_rtx_PLUS (mode
, comparison_value
, offset
);
9715 loop_insn_hoist (loop
, add_insn
);
9716 if (GET_CODE (comparison
) == LE
)
9717 final_value
= gen_rtx_PLUS (mode
, comparison_value
,
9720 else if (! add_adjust
)
9722 rtx sub_insn
= gen_sub3_insn (reg
, comparison_value
,
9728 = gen_rtx_MINUS (mode
, comparison_value
, initial_value
);
9729 loop_insn_hoist (loop
, sub_insn
);
9732 /* We could handle the other cases too, but it'll be
9733 better to have a testcase first. */
9736 /* We may not have a single insn which can increment a reg, so
9737 create a sequence to hold all the insns from expand_inc. */
9739 expand_inc (reg
, new_add_val
);
9743 p
= loop_insn_emit_before (loop
, 0, bl
->biv
->insn
, tem
);
9744 delete_insn (bl
->biv
->insn
);
9746 /* Update biv info to reflect its new status. */
9748 bl
->initial_value
= start_value
;
9749 bl
->biv
->add_val
= new_add_val
;
9751 /* Update loop info. */
9752 loop_info
->initial_value
= reg
;
9753 loop_info
->initial_equiv_value
= reg
;
9754 loop_info
->final_value
= const0_rtx
;
9755 loop_info
->final_equiv_value
= const0_rtx
;
9756 loop_info
->comparison_value
= const0_rtx
;
9757 loop_info
->comparison_code
= cmp_code
;
9758 loop_info
->increment
= new_add_val
;
9760 /* Inc LABEL_NUSES so that delete_insn will
9761 not delete the label. */
9762 LABEL_NUSES (XEXP (jump_label
, 0))++;
9764 /* If we have a separate comparison insn that does more
9765 than just set cc0, the result of the comparison might
9766 be used outside the loop. */
9767 keep_first_compare
= (compare_and_branch
== 2
9769 && sets_cc0_p (first_compare
) <= 0
9773 /* Emit an insn after the end of the loop to set the biv's
9774 proper exit value if it is used anywhere outside the loop. */
9775 if (keep_first_compare
9776 || (REGNO_LAST_UID (bl
->regno
) != INSN_UID (first_compare
))
9778 || REGNO_FIRST_UID (bl
->regno
) != INSN_UID (bl
->init_insn
))
9779 loop_insn_sink (loop
, gen_load_of_final_value (reg
, final_value
));
9781 if (keep_first_compare
)
9782 loop_insn_sink (loop
, PATTERN (first_compare
));
9784 /* Delete compare/branch at end of loop. */
9785 delete_related_insns (PREV_INSN (loop_end
));
9786 if (compare_and_branch
== 2)
9787 delete_related_insns (first_compare
);
9789 /* Add new compare/branch insn at end of loop. */
9791 emit_cmp_and_jump_insns (reg
, const0_rtx
, cmp_code
, NULL_RTX
,
9793 XEXP (jump_label
, 0));
9796 emit_jump_insn_before (tem
, loop_end
);
9798 for (tem
= PREV_INSN (loop_end
);
9799 tem
&& !JUMP_P (tem
);
9800 tem
= PREV_INSN (tem
))
9804 JUMP_LABEL (tem
) = XEXP (jump_label
, 0);
9810 /* Increment of LABEL_NUSES done above. */
9811 /* Register is now always nonnegative,
9812 so add REG_NONNEG note to the branch. */
9813 REG_NOTES (tem
) = gen_rtx_EXPR_LIST (REG_NONNEG
, reg
,
9819 /* No insn may reference both the reversed and another biv or it
9820 will fail (see comment near the top of the loop reversal
9822 Earlier on, we have verified that the biv has no use except
9823 counting, or it is the only biv in this function.
9824 However, the code that computes no_use_except_counting does
9825 not verify reg notes. It's possible to have an insn that
9826 references another biv, and has a REG_EQUAL note with an
9827 expression based on the reversed biv. To avoid this case,
9828 remove all REG_EQUAL notes based on the reversed biv
9830 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
9834 rtx set
= single_set (p
);
9835 /* If this is a set of a GIV based on the reversed biv, any
9836 REG_EQUAL notes should still be correct. */
9838 || !REG_P (SET_DEST (set
))
9839 || (size_t) REGNO (SET_DEST (set
)) >= ivs
->n_regs
9840 || REG_IV_TYPE (ivs
, REGNO (SET_DEST (set
))) != GENERAL_INDUCT
9841 || REG_IV_INFO (ivs
, REGNO (SET_DEST (set
)))->src_reg
!= bl
->biv
->src_reg
)
9842 for (pnote
= ®_NOTES (p
); *pnote
;)
9844 if (REG_NOTE_KIND (*pnote
) == REG_EQUAL
9845 && reg_mentioned_p (regno_reg_rtx
[bl
->regno
],
9847 *pnote
= XEXP (*pnote
, 1);
9849 pnote
= &XEXP (*pnote
, 1);
9853 /* Mark that this biv has been reversed. Each giv which depends
9854 on this biv, and which is also live past the end of the loop
9855 will have to be fixed up. */
9859 if (loop_dump_stream
)
9861 fprintf (loop_dump_stream
, "Reversed loop");
9863 fprintf (loop_dump_stream
, " and added reg_nonneg\n");
9865 fprintf (loop_dump_stream
, "\n");
9876 /* Verify whether the biv BL appears to be eliminable,
9877 based on the insns in the loop that refer to it.
9879 If ELIMINATE_P is nonzero, actually do the elimination.
9881 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
9882 determine whether invariant insns should be placed inside or at the
9883 start of the loop. */
9886 maybe_eliminate_biv (const struct loop
*loop
, struct iv_class
*bl
,
9887 int eliminate_p
, int threshold
, int insn_count
)
9889 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
9890 rtx reg
= bl
->biv
->dest_reg
;
9893 /* Scan all insns in the loop, stopping if we find one that uses the
9894 biv in a way that we cannot eliminate. */
9896 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
9898 enum rtx_code code
= GET_CODE (p
);
9899 basic_block where_bb
= 0;
9900 rtx where_insn
= threshold
>= insn_count
? 0 : p
;
9903 /* If this is a libcall that sets a giv, skip ahead to its end. */
9906 note
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
9910 rtx last
= XEXP (note
, 0);
9911 rtx set
= single_set (last
);
9913 if (set
&& REG_P (SET_DEST (set
)))
9915 unsigned int regno
= REGNO (SET_DEST (set
));
9917 if (regno
< ivs
->n_regs
9918 && REG_IV_TYPE (ivs
, regno
) == GENERAL_INDUCT
9919 && REG_IV_INFO (ivs
, regno
)->src_reg
== bl
->biv
->src_reg
)
9925 /* Closely examine the insn if the biv is mentioned. */
9926 if ((code
== INSN
|| code
== JUMP_INSN
|| code
== CALL_INSN
)
9927 && reg_mentioned_p (reg
, PATTERN (p
))
9928 && ! maybe_eliminate_biv_1 (loop
, PATTERN (p
), p
, bl
,
9929 eliminate_p
, where_bb
, where_insn
))
9931 if (loop_dump_stream
)
9932 fprintf (loop_dump_stream
,
9933 "Cannot eliminate biv %d: biv used in insn %d.\n",
9934 bl
->regno
, INSN_UID (p
));
9938 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
9940 && (note
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)) != NULL_RTX
9941 && reg_mentioned_p (reg
, XEXP (note
, 0)))
9942 remove_note (p
, note
);
9947 if (loop_dump_stream
)
9948 fprintf (loop_dump_stream
, "biv %d %s eliminated.\n",
9949 bl
->regno
, eliminate_p
? "was" : "can be");
9956 /* INSN and REFERENCE are instructions in the same insn chain.
9957 Return nonzero if INSN is first. */
9960 loop_insn_first_p (rtx insn
, rtx reference
)
9964 for (p
= insn
, q
= reference
;;)
9966 /* Start with test for not first so that INSN == REFERENCE yields not
9968 if (q
== insn
|| ! p
)
9970 if (p
== reference
|| ! q
)
9973 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
9974 previous insn, hence the <= comparison below does not work if
9976 if (INSN_UID (p
) < max_uid_for_loop
9977 && INSN_UID (q
) < max_uid_for_loop
9979 return INSN_LUID (p
) <= INSN_LUID (q
);
9981 if (INSN_UID (p
) >= max_uid_for_loop
9984 if (INSN_UID (q
) >= max_uid_for_loop
)
9989 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
9990 the offset that we have to take into account due to auto-increment /
9991 div derivation is zero. */
9993 biv_elimination_giv_has_0_offset (struct induction
*biv
,
9994 struct induction
*giv
, rtx insn
)
9996 /* If the giv V had the auto-inc address optimization applied
9997 to it, and INSN occurs between the giv insn and the biv
9998 insn, then we'd have to adjust the value used here.
9999 This is rare, so we don't bother to make this possible. */
10000 if (giv
->auto_inc_opt
10001 && ((loop_insn_first_p (giv
->insn
, insn
)
10002 && loop_insn_first_p (insn
, biv
->insn
))
10003 || (loop_insn_first_p (biv
->insn
, insn
)
10004 && loop_insn_first_p (insn
, giv
->insn
))))
10010 /* If BL appears in X (part of the pattern of INSN), see if we can
10011 eliminate its use. If so, return 1. If not, return 0.
10013 If BIV does not appear in X, return 1.
10015 If ELIMINATE_P is nonzero, actually do the elimination.
10016 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
10017 Depending on how many items have been moved out of the loop, it
10018 will either be before INSN (when WHERE_INSN is nonzero) or at the
10019 start of the loop (when WHERE_INSN is zero). */
10022 maybe_eliminate_biv_1 (const struct loop
*loop
, rtx x
, rtx insn
,
10023 struct iv_class
*bl
, int eliminate_p
,
10024 basic_block where_bb
, rtx where_insn
)
10026 enum rtx_code code
= GET_CODE (x
);
10027 rtx reg
= bl
->biv
->dest_reg
;
10028 enum machine_mode mode
= GET_MODE (reg
);
10029 struct induction
*v
;
10041 /* If we haven't already been able to do something with this BIV,
10042 we can't eliminate it. */
10048 /* If this sets the BIV, it is not a problem. */
10049 if (SET_DEST (x
) == reg
)
10052 /* If this is an insn that defines a giv, it is also ok because
10053 it will go away when the giv is reduced. */
10054 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10055 if (v
->giv_type
== DEST_REG
&& SET_DEST (x
) == v
->dest_reg
)
10059 if (SET_DEST (x
) == cc0_rtx
&& SET_SRC (x
) == reg
)
10061 /* Can replace with any giv that was reduced and
10062 that has (MULT_VAL != 0) and (ADD_VAL == 0).
10063 Require a constant for MULT_VAL, so we know it's nonzero.
10064 ??? We disable this optimization to avoid potential
10067 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10068 if (GET_CODE (v
->mult_val
) == CONST_INT
&& v
->mult_val
!= const0_rtx
10069 && v
->add_val
== const0_rtx
10070 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
10074 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
10080 /* If the giv has the opposite direction of change,
10081 then reverse the comparison. */
10082 if (INTVAL (v
->mult_val
) < 0)
10083 new = gen_rtx_COMPARE (GET_MODE (v
->new_reg
),
10084 const0_rtx
, v
->new_reg
);
10088 /* We can probably test that giv's reduced reg. */
10089 if (validate_change (insn
, &SET_SRC (x
), new, 0))
10093 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
10094 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
10095 Require a constant for MULT_VAL, so we know it's nonzero.
10096 ??? Do this only if ADD_VAL is a pointer to avoid a potential
10097 overflow problem. */
10099 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10100 if (GET_CODE (v
->mult_val
) == CONST_INT
10101 && v
->mult_val
!= const0_rtx
10102 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
10104 && (GET_CODE (v
->add_val
) == SYMBOL_REF
10105 || GET_CODE (v
->add_val
) == LABEL_REF
10106 || GET_CODE (v
->add_val
) == CONST
10107 || (REG_P (v
->add_val
)
10108 && REG_POINTER (v
->add_val
))))
10110 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
10116 /* If the giv has the opposite direction of change,
10117 then reverse the comparison. */
10118 if (INTVAL (v
->mult_val
) < 0)
10119 new = gen_rtx_COMPARE (VOIDmode
, copy_rtx (v
->add_val
),
10122 new = gen_rtx_COMPARE (VOIDmode
, v
->new_reg
,
10123 copy_rtx (v
->add_val
));
10125 /* Replace biv with the giv's reduced register. */
10126 update_reg_last_use (v
->add_val
, insn
);
10127 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
10130 /* Insn doesn't support that constant or invariant. Copy it
10131 into a register (it will be a loop invariant.) */
10132 tem
= gen_reg_rtx (GET_MODE (v
->new_reg
));
10134 loop_insn_emit_before (loop
, 0, where_insn
,
10135 gen_move_insn (tem
,
10136 copy_rtx (v
->add_val
)));
10138 /* Substitute the new register for its invariant value in
10139 the compare expression. */
10140 XEXP (new, (INTVAL (v
->mult_val
) < 0) ? 0 : 1) = tem
;
10141 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
10150 case GT
: case GE
: case GTU
: case GEU
:
10151 case LT
: case LE
: case LTU
: case LEU
:
10152 /* See if either argument is the biv. */
10153 if (XEXP (x
, 0) == reg
)
10154 arg
= XEXP (x
, 1), arg_operand
= 1;
10155 else if (XEXP (x
, 1) == reg
)
10156 arg
= XEXP (x
, 0), arg_operand
= 0;
10160 if (CONSTANT_P (arg
))
10162 /* First try to replace with any giv that has constant positive
10163 mult_val and constant add_val. We might be able to support
10164 negative mult_val, but it seems complex to do it in general. */
10166 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10167 if (GET_CODE (v
->mult_val
) == CONST_INT
10168 && INTVAL (v
->mult_val
) > 0
10169 && (GET_CODE (v
->add_val
) == SYMBOL_REF
10170 || GET_CODE (v
->add_val
) == LABEL_REF
10171 || GET_CODE (v
->add_val
) == CONST
10172 || (REG_P (v
->add_val
)
10173 && REG_POINTER (v
->add_val
)))
10174 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
10175 && v
->mode
== mode
)
10177 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
10180 /* Don't eliminate if the linear combination that makes up
10181 the giv overflows when it is applied to ARG. */
10182 if (GET_CODE (arg
) == CONST_INT
)
10186 if (GET_CODE (v
->add_val
) == CONST_INT
)
10187 add_val
= v
->add_val
;
10189 add_val
= const0_rtx
;
10191 if (const_mult_add_overflow_p (arg
, v
->mult_val
,
10199 /* Replace biv with the giv's reduced reg. */
10200 validate_change (insn
, &XEXP (x
, 1 - arg_operand
), v
->new_reg
, 1);
10202 /* If all constants are actually constant integers and
10203 the derived constant can be directly placed in the COMPARE,
10205 if (GET_CODE (arg
) == CONST_INT
10206 && GET_CODE (v
->add_val
) == CONST_INT
)
10208 tem
= expand_mult_add (arg
, NULL_RTX
, v
->mult_val
,
10209 v
->add_val
, mode
, 1);
10213 /* Otherwise, load it into a register. */
10214 tem
= gen_reg_rtx (mode
);
10215 loop_iv_add_mult_emit_before (loop
, arg
,
10216 v
->mult_val
, v
->add_val
,
10217 tem
, where_bb
, where_insn
);
10220 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
10222 if (apply_change_group ())
10226 /* Look for giv with positive constant mult_val and nonconst add_val.
10227 Insert insns to calculate new compare value.
10228 ??? Turn this off due to possible overflow. */
10230 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10231 if (GET_CODE (v
->mult_val
) == CONST_INT
10232 && INTVAL (v
->mult_val
) > 0
10233 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
10239 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
10245 tem
= gen_reg_rtx (mode
);
10247 /* Replace biv with giv's reduced register. */
10248 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
10251 /* Compute value to compare against. */
10252 loop_iv_add_mult_emit_before (loop
, arg
,
10253 v
->mult_val
, v
->add_val
,
10254 tem
, where_bb
, where_insn
);
10255 /* Use it in this insn. */
10256 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
10257 if (apply_change_group ())
10261 else if (REG_P (arg
) || MEM_P (arg
))
10263 if (loop_invariant_p (loop
, arg
) == 1)
10265 /* Look for giv with constant positive mult_val and nonconst
10266 add_val. Insert insns to compute new compare value.
10267 ??? Turn this off due to possible overflow. */
10269 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10270 if (GET_CODE (v
->mult_val
) == CONST_INT
&& INTVAL (v
->mult_val
) > 0
10271 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
10277 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
10283 tem
= gen_reg_rtx (mode
);
10285 /* Replace biv with giv's reduced register. */
10286 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
10289 /* Compute value to compare against. */
10290 loop_iv_add_mult_emit_before (loop
, arg
,
10291 v
->mult_val
, v
->add_val
,
10292 tem
, where_bb
, where_insn
);
10293 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
10294 if (apply_change_group ())
10299 /* This code has problems. Basically, you can't know when
10300 seeing if we will eliminate BL, whether a particular giv
10301 of ARG will be reduced. If it isn't going to be reduced,
10302 we can't eliminate BL. We can try forcing it to be reduced,
10303 but that can generate poor code.
10305 The problem is that the benefit of reducing TV, below should
10306 be increased if BL can actually be eliminated, but this means
10307 we might have to do a topological sort of the order in which
10308 we try to process biv. It doesn't seem worthwhile to do
10309 this sort of thing now. */
10312 /* Otherwise the reg compared with had better be a biv. */
10314 || REG_IV_TYPE (ivs
, REGNO (arg
)) != BASIC_INDUCT
)
10317 /* Look for a pair of givs, one for each biv,
10318 with identical coefficients. */
10319 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10321 struct induction
*tv
;
10323 if (v
->ignore
|| v
->maybe_dead
|| v
->mode
!= mode
)
10326 for (tv
= REG_IV_CLASS (ivs
, REGNO (arg
))->giv
; tv
;
10328 if (! tv
->ignore
&& ! tv
->maybe_dead
10329 && rtx_equal_p (tv
->mult_val
, v
->mult_val
)
10330 && rtx_equal_p (tv
->add_val
, v
->add_val
)
10331 && tv
->mode
== mode
)
10333 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
10339 /* Replace biv with its giv's reduced reg. */
10340 XEXP (x
, 1 - arg_operand
) = v
->new_reg
;
10341 /* Replace other operand with the other giv's
10343 XEXP (x
, arg_operand
) = tv
->new_reg
;
10350 /* If we get here, the biv can't be eliminated. */
10354 /* If this address is a DEST_ADDR giv, it doesn't matter if the
10355 biv is used in it, since it will be replaced. */
10356 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
10357 if (v
->giv_type
== DEST_ADDR
&& v
->location
== &XEXP (x
, 0))
10365 /* See if any subexpression fails elimination. */
10366 fmt
= GET_RTX_FORMAT (code
);
10367 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
10372 if (! maybe_eliminate_biv_1 (loop
, XEXP (x
, i
), insn
, bl
,
10373 eliminate_p
, where_bb
, where_insn
))
10378 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
10379 if (! maybe_eliminate_biv_1 (loop
, XVECEXP (x
, i
, j
), insn
, bl
,
10380 eliminate_p
, where_bb
, where_insn
))
10389 /* Return nonzero if the last use of REG
10390 is in an insn following INSN in the same basic block. */
10393 last_use_this_basic_block (rtx reg
, rtx insn
)
10397 n
&& !LABEL_P (n
) && !JUMP_P (n
);
10400 if (REGNO_LAST_UID (REGNO (reg
)) == INSN_UID (n
))
10406 /* Called via `note_stores' to record the initial value of a biv. Here we
10407 just record the location of the set and process it later. */
10410 record_initial (rtx dest
, rtx set
, void *data ATTRIBUTE_UNUSED
)
10412 struct loop_ivs
*ivs
= (struct loop_ivs
*) data
;
10413 struct iv_class
*bl
;
10416 || REGNO (dest
) >= ivs
->n_regs
10417 || REG_IV_TYPE (ivs
, REGNO (dest
)) != BASIC_INDUCT
)
10420 bl
= REG_IV_CLASS (ivs
, REGNO (dest
));
10422 /* If this is the first set found, record it. */
10423 if (bl
->init_insn
== 0)
10425 bl
->init_insn
= note_insn
;
10426 bl
->init_set
= set
;
10430 /* If any of the registers in X are "old" and currently have a last use earlier
10431 than INSN, update them to have a last use of INSN. Their actual last use
10432 will be the previous insn but it will not have a valid uid_luid so we can't
10433 use it. X must be a source expression only. */
10436 update_reg_last_use (rtx x
, rtx insn
)
10438 /* Check for the case where INSN does not have a valid luid. In this case,
10439 there is no need to modify the regno_last_uid, as this can only happen
10440 when code is inserted after the loop_end to set a pseudo's final value,
10441 and hence this insn will never be the last use of x.
10442 ???? This comment is not correct. See for example loop_givs_reduce.
10443 This may insert an insn before another new insn. */
10444 if (REG_P (x
) && REGNO (x
) < max_reg_before_loop
10445 && INSN_UID (insn
) < max_uid_for_loop
10446 && REGNO_LAST_LUID (REGNO (x
)) < INSN_LUID (insn
))
10448 REGNO_LAST_UID (REGNO (x
)) = INSN_UID (insn
);
10453 const char *fmt
= GET_RTX_FORMAT (GET_CODE (x
));
10454 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0; i
--)
10457 update_reg_last_use (XEXP (x
, i
), insn
);
10458 else if (fmt
[i
] == 'E')
10459 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
10460 update_reg_last_use (XVECEXP (x
, i
, j
), insn
);
10465 /* Similar to rtlanal.c:get_condition, except that we also put an
10466 invariant last unless both operands are invariants. */
10469 get_condition_for_loop (const struct loop
*loop
, rtx x
)
10471 rtx comparison
= get_condition (x
, (rtx
*) 0, false, true);
10473 if (comparison
== 0
10474 || ! loop_invariant_p (loop
, XEXP (comparison
, 0))
10475 || loop_invariant_p (loop
, XEXP (comparison
, 1)))
10478 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison
)), VOIDmode
,
10479 XEXP (comparison
, 1), XEXP (comparison
, 0));
10482 /* Scan the function and determine whether it has indirect (computed) jumps.
10484 This is taken mostly from flow.c; similar code exists elsewhere
10485 in the compiler. It may be useful to put this into rtlanal.c. */
10487 indirect_jump_in_function_p (rtx start
)
10491 for (insn
= start
; insn
; insn
= NEXT_INSN (insn
))
10492 if (computed_jump_p (insn
))
10498 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
10499 documentation for LOOP_MEMS for the definition of `appropriate'.
10500 This function is called from prescan_loop via for_each_rtx. */
10503 insert_loop_mem (rtx
*mem
, void *data ATTRIBUTE_UNUSED
)
10505 struct loop_info
*loop_info
= data
;
10512 switch (GET_CODE (m
))
10518 /* We're not interested in MEMs that are only clobbered. */
10522 /* We're not interested in the MEM associated with a
10523 CONST_DOUBLE, so there's no need to traverse into this. */
10527 /* We're not interested in any MEMs that only appear in notes. */
10531 /* This is not a MEM. */
10535 /* See if we've already seen this MEM. */
10536 for (i
= 0; i
< loop_info
->mems_idx
; ++i
)
10537 if (rtx_equal_p (m
, loop_info
->mems
[i
].mem
))
10539 if (MEM_VOLATILE_P (m
) && !MEM_VOLATILE_P (loop_info
->mems
[i
].mem
))
10540 loop_info
->mems
[i
].mem
= m
;
10541 if (GET_MODE (m
) != GET_MODE (loop_info
->mems
[i
].mem
))
10542 /* The modes of the two memory accesses are different. If
10543 this happens, something tricky is going on, and we just
10544 don't optimize accesses to this MEM. */
10545 loop_info
->mems
[i
].optimize
= 0;
10550 /* Resize the array, if necessary. */
10551 if (loop_info
->mems_idx
== loop_info
->mems_allocated
)
10553 if (loop_info
->mems_allocated
!= 0)
10554 loop_info
->mems_allocated
*= 2;
10556 loop_info
->mems_allocated
= 32;
10558 loop_info
->mems
= xrealloc (loop_info
->mems
,
10559 loop_info
->mems_allocated
* sizeof (loop_mem_info
));
10562 /* Actually insert the MEM. */
10563 loop_info
->mems
[loop_info
->mems_idx
].mem
= m
;
10564 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
10565 because we can't put it in a register. We still store it in the
10566 table, though, so that if we see the same address later, but in a
10567 non-BLK mode, we'll not think we can optimize it at that point. */
10568 loop_info
->mems
[loop_info
->mems_idx
].optimize
= (GET_MODE (m
) != BLKmode
);
10569 loop_info
->mems
[loop_info
->mems_idx
].reg
= NULL_RTX
;
10570 ++loop_info
->mems_idx
;
10576 /* Allocate REGS->ARRAY or reallocate it if it is too small.
10578 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
10579 register that is modified by an insn between FROM and TO. If the
10580 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
10581 more, stop incrementing it, to avoid overflow.
10583 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
10584 register I is used, if it is only used once. Otherwise, it is set
10585 to 0 (for no uses) or const0_rtx for more than one use. This
10586 parameter may be zero, in which case this processing is not done.
10588 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
10589 optimize register I. */
10592 loop_regs_scan (const struct loop
*loop
, int extra_size
)
10594 struct loop_regs
*regs
= LOOP_REGS (loop
);
10596 /* last_set[n] is nonzero iff reg n has been set in the current
10597 basic block. In that case, it is the insn that last set reg n. */
10602 old_nregs
= regs
->num
;
10603 regs
->num
= max_reg_num ();
10605 /* Grow the regs array if not allocated or too small. */
10606 if (regs
->num
>= regs
->size
)
10608 regs
->size
= regs
->num
+ extra_size
;
10610 regs
->array
= xrealloc (regs
->array
, regs
->size
* sizeof (*regs
->array
));
10612 /* Zero the new elements. */
10613 memset (regs
->array
+ old_nregs
, 0,
10614 (regs
->size
- old_nregs
) * sizeof (*regs
->array
));
10617 /* Clear previously scanned fields but do not clear n_times_set. */
10618 for (i
= 0; i
< old_nregs
; i
++)
10620 regs
->array
[i
].set_in_loop
= 0;
10621 regs
->array
[i
].may_not_optimize
= 0;
10622 regs
->array
[i
].single_usage
= NULL_RTX
;
10625 last_set
= xcalloc (regs
->num
, sizeof (rtx
));
10627 /* Scan the loop, recording register usage. */
10628 for (insn
= loop
->top
? loop
->top
: loop
->start
; insn
!= loop
->end
;
10629 insn
= NEXT_INSN (insn
))
10633 /* Record registers that have exactly one use. */
10634 find_single_use_in_loop (regs
, insn
, PATTERN (insn
));
10636 /* Include uses in REG_EQUAL notes. */
10637 if (REG_NOTES (insn
))
10638 find_single_use_in_loop (regs
, insn
, REG_NOTES (insn
));
10640 if (GET_CODE (PATTERN (insn
)) == SET
10641 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
10642 count_one_set (regs
, insn
, PATTERN (insn
), last_set
);
10643 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
10646 for (i
= XVECLEN (PATTERN (insn
), 0) - 1; i
>= 0; i
--)
10647 count_one_set (regs
, insn
, XVECEXP (PATTERN (insn
), 0, i
),
10652 if (LABEL_P (insn
) || JUMP_P (insn
))
10653 memset (last_set
, 0, regs
->num
* sizeof (rtx
));
10655 /* Invalidate all registers used for function argument passing.
10656 We check rtx_varies_p for the same reason as below, to allow
10657 optimizing PIC calculations. */
10661 for (link
= CALL_INSN_FUNCTION_USAGE (insn
);
10663 link
= XEXP (link
, 1))
10667 if (GET_CODE (op
= XEXP (link
, 0)) == USE
10668 && REG_P (reg
= XEXP (op
, 0))
10669 && rtx_varies_p (reg
, 1))
10670 regs
->array
[REGNO (reg
)].may_not_optimize
= 1;
10675 /* Invalidate all hard registers clobbered by calls. With one exception:
10676 a call-clobbered PIC register is still function-invariant for our
10677 purposes, since we can hoist any PIC calculations out of the loop.
10678 Thus the call to rtx_varies_p. */
10679 if (LOOP_INFO (loop
)->has_call
)
10680 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
10681 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
)
10682 && rtx_varies_p (regno_reg_rtx
[i
], 1))
10684 regs
->array
[i
].may_not_optimize
= 1;
10685 regs
->array
[i
].set_in_loop
= 1;
10688 #ifdef AVOID_CCMODE_COPIES
10689 /* Don't try to move insns which set CC registers if we should not
10690 create CCmode register copies. */
10691 for (i
= regs
->num
- 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
10692 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx
[i
])) == MODE_CC
)
10693 regs
->array
[i
].may_not_optimize
= 1;
10696 /* Set regs->array[I].n_times_set for the new registers. */
10697 for (i
= old_nregs
; i
< regs
->num
; i
++)
10698 regs
->array
[i
].n_times_set
= regs
->array
[i
].set_in_loop
;
10703 /* Returns the number of real INSNs in the LOOP. */
10706 count_insns_in_loop (const struct loop
*loop
)
10711 for (insn
= loop
->top
? loop
->top
: loop
->start
; insn
!= loop
->end
;
10712 insn
= NEXT_INSN (insn
))
10719 /* Move MEMs into registers for the duration of the loop. */
10722 load_mems (const struct loop
*loop
)
10724 struct loop_info
*loop_info
= LOOP_INFO (loop
);
10725 struct loop_regs
*regs
= LOOP_REGS (loop
);
10726 int maybe_never
= 0;
10728 rtx p
, prev_ebb_head
;
10729 rtx label
= NULL_RTX
;
10731 /* Nonzero if the next instruction may never be executed. */
10732 int next_maybe_never
= 0;
10733 unsigned int last_max_reg
= max_reg_num ();
10735 if (loop_info
->mems_idx
== 0)
10738 /* We cannot use next_label here because it skips over normal insns. */
10739 end_label
= next_nonnote_insn (loop
->end
);
10740 if (end_label
&& !LABEL_P (end_label
))
10741 end_label
= NULL_RTX
;
10743 /* Check to see if it's possible that some instructions in the loop are
10744 never executed. Also check if there is a goto out of the loop other
10745 than right after the end of the loop. */
10746 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
10748 p
= next_insn_in_loop (loop
, p
))
10752 else if (JUMP_P (p
)
10753 /* If we enter the loop in the middle, and scan
10754 around to the beginning, don't set maybe_never
10755 for that. This must be an unconditional jump,
10756 otherwise the code at the top of the loop might
10757 never be executed. Unconditional jumps are
10758 followed a by barrier then loop end. */
10760 && JUMP_LABEL (p
) == loop
->top
10761 && NEXT_INSN (NEXT_INSN (p
)) == loop
->end
10762 && any_uncondjump_p (p
)))
10764 /* If this is a jump outside of the loop but not right
10765 after the end of the loop, we would have to emit new fixup
10766 sequences for each such label. */
10767 if (/* If we can't tell where control might go when this
10768 JUMP_INSN is executed, we must be conservative. */
10770 || (JUMP_LABEL (p
) != end_label
10771 && (INSN_UID (JUMP_LABEL (p
)) >= max_uid_for_loop
10772 || INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (loop
->start
)
10773 || INSN_LUID (JUMP_LABEL (p
)) > INSN_LUID (loop
->end
))))
10776 if (!any_condjump_p (p
))
10777 /* Something complicated. */
10780 /* If there are any more instructions in the loop, they
10781 might not be reached. */
10782 next_maybe_never
= 1;
10784 else if (next_maybe_never
)
10788 /* Find start of the extended basic block that enters the loop. */
10789 for (p
= loop
->start
;
10790 PREV_INSN (p
) && !LABEL_P (p
);
10795 cselib_init (true);
10797 /* Build table of mems that get set to constant values before the
10799 for (; p
!= loop
->start
; p
= NEXT_INSN (p
))
10800 cselib_process_insn (p
);
10802 /* Actually move the MEMs. */
10803 for (i
= 0; i
< loop_info
->mems_idx
; ++i
)
10805 regset_head load_copies
;
10806 regset_head store_copies
;
10809 rtx mem
= loop_info
->mems
[i
].mem
;
10810 rtx mem_list_entry
;
10812 if (MEM_VOLATILE_P (mem
)
10813 || loop_invariant_p (loop
, XEXP (mem
, 0)) != 1)
10814 /* There's no telling whether or not MEM is modified. */
10815 loop_info
->mems
[i
].optimize
= 0;
10817 /* Go through the MEMs written to in the loop to see if this
10818 one is aliased by one of them. */
10819 mem_list_entry
= loop_info
->store_mems
;
10820 while (mem_list_entry
)
10822 if (rtx_equal_p (mem
, XEXP (mem_list_entry
, 0)))
10824 else if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
10825 mem
, rtx_varies_p
))
10827 /* MEM is indeed aliased by this store. */
10828 loop_info
->mems
[i
].optimize
= 0;
10831 mem_list_entry
= XEXP (mem_list_entry
, 1);
10834 if (flag_float_store
&& written
10835 && GET_MODE_CLASS (GET_MODE (mem
)) == MODE_FLOAT
)
10836 loop_info
->mems
[i
].optimize
= 0;
10838 /* If this MEM is written to, we must be sure that there
10839 are no reads from another MEM that aliases this one. */
10840 if (loop_info
->mems
[i
].optimize
&& written
)
10844 for (j
= 0; j
< loop_info
->mems_idx
; ++j
)
10848 else if (true_dependence (mem
,
10850 loop_info
->mems
[j
].mem
,
10853 /* It's not safe to hoist loop_info->mems[i] out of
10854 the loop because writes to it might not be
10855 seen by reads from loop_info->mems[j]. */
10856 loop_info
->mems
[i
].optimize
= 0;
10862 if (maybe_never
&& may_trap_p (mem
))
10863 /* We can't access the MEM outside the loop; it might
10864 cause a trap that wouldn't have happened otherwise. */
10865 loop_info
->mems
[i
].optimize
= 0;
10867 if (!loop_info
->mems
[i
].optimize
)
10868 /* We thought we were going to lift this MEM out of the
10869 loop, but later discovered that we could not. */
10872 INIT_REG_SET (&load_copies
);
10873 INIT_REG_SET (&store_copies
);
10875 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
10876 order to keep scan_loop from moving stores to this MEM
10877 out of the loop just because this REG is neither a
10878 user-variable nor used in the loop test. */
10879 reg
= gen_reg_rtx (GET_MODE (mem
));
10880 REG_USERVAR_P (reg
) = 1;
10881 loop_info
->mems
[i
].reg
= reg
;
10883 /* Now, replace all references to the MEM with the
10884 corresponding pseudos. */
10886 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
10888 p
= next_insn_in_loop (loop
, p
))
10894 set
= single_set (p
);
10896 /* See if this copies the mem into a register that isn't
10897 modified afterwards. We'll try to do copy propagation
10898 a little further on. */
10900 /* @@@ This test is _way_ too conservative. */
10902 && REG_P (SET_DEST (set
))
10903 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
10904 && REGNO (SET_DEST (set
)) < last_max_reg
10905 && regs
->array
[REGNO (SET_DEST (set
))].n_times_set
== 1
10906 && rtx_equal_p (SET_SRC (set
), mem
))
10907 SET_REGNO_REG_SET (&load_copies
, REGNO (SET_DEST (set
)));
10909 /* See if this copies the mem from a register that isn't
10910 modified afterwards. We'll try to remove the
10911 redundant copy later on by doing a little register
10912 renaming and copy propagation. This will help
10913 to untangle things for the BIV detection code. */
10916 && REG_P (SET_SRC (set
))
10917 && REGNO (SET_SRC (set
)) >= FIRST_PSEUDO_REGISTER
10918 && REGNO (SET_SRC (set
)) < last_max_reg
10919 && regs
->array
[REGNO (SET_SRC (set
))].n_times_set
== 1
10920 && rtx_equal_p (SET_DEST (set
), mem
))
10921 SET_REGNO_REG_SET (&store_copies
, REGNO (SET_SRC (set
)));
10923 /* If this is a call which uses / clobbers this memory
10924 location, we must not change the interface here. */
10926 && reg_mentioned_p (loop_info
->mems
[i
].mem
,
10927 CALL_INSN_FUNCTION_USAGE (p
)))
10929 cancel_changes (0);
10930 loop_info
->mems
[i
].optimize
= 0;
10934 /* Replace the memory reference with the shadow register. */
10935 replace_loop_mems (p
, loop_info
->mems
[i
].mem
,
10936 loop_info
->mems
[i
].reg
, written
);
10944 if (! loop_info
->mems
[i
].optimize
)
10945 ; /* We found we couldn't do the replacement, so do nothing. */
10946 else if (! apply_change_group ())
10947 /* We couldn't replace all occurrences of the MEM. */
10948 loop_info
->mems
[i
].optimize
= 0;
10951 /* Load the memory immediately before LOOP->START, which is
10952 the NOTE_LOOP_BEG. */
10953 cselib_val
*e
= cselib_lookup (mem
, VOIDmode
, 0);
10957 struct elt_loc_list
*const_equiv
= 0;
10958 reg_set_iterator rsi
;
10962 struct elt_loc_list
*equiv
;
10963 struct elt_loc_list
*best_equiv
= 0;
10964 for (equiv
= e
->locs
; equiv
; equiv
= equiv
->next
)
10966 if (CONSTANT_P (equiv
->loc
))
10967 const_equiv
= equiv
;
10968 else if (REG_P (equiv
->loc
)
10969 /* Extending hard register lifetimes causes crash
10970 on SRC targets. Doing so on non-SRC is
10971 probably also not good idea, since we most
10972 probably have pseudoregister equivalence as
10974 && REGNO (equiv
->loc
) >= FIRST_PSEUDO_REGISTER
)
10975 best_equiv
= equiv
;
10977 /* Use the constant equivalence if that is cheap enough. */
10979 best_equiv
= const_equiv
;
10980 else if (const_equiv
10981 && (rtx_cost (const_equiv
->loc
, SET
)
10982 <= rtx_cost (best_equiv
->loc
, SET
)))
10984 best_equiv
= const_equiv
;
10988 /* If best_equiv is nonzero, we know that MEM is set to a
10989 constant or register before the loop. We will use this
10990 knowledge to initialize the shadow register with that
10991 constant or reg rather than by loading from MEM. */
10993 best
= copy_rtx (best_equiv
->loc
);
10996 set
= gen_move_insn (reg
, best
);
10997 set
= loop_insn_hoist (loop
, set
);
11000 for (p
= prev_ebb_head
; p
!= loop
->start
; p
= NEXT_INSN (p
))
11001 if (REGNO_LAST_UID (REGNO (best
)) == INSN_UID (p
))
11003 REGNO_LAST_UID (REGNO (best
)) = INSN_UID (set
);
11009 set_unique_reg_note (set
, REG_EQUAL
, copy_rtx (const_equiv
->loc
));
11013 if (label
== NULL_RTX
)
11015 label
= gen_label_rtx ();
11016 emit_label_after (label
, loop
->end
);
11019 /* Store the memory immediately after END, which is
11020 the NOTE_LOOP_END. */
11021 set
= gen_move_insn (copy_rtx (mem
), reg
);
11022 loop_insn_emit_after (loop
, 0, label
, set
);
11025 if (loop_dump_stream
)
11027 fprintf (loop_dump_stream
, "Hoisted regno %d %s from ",
11028 REGNO (reg
), (written
? "r/w" : "r/o"));
11029 print_rtl (loop_dump_stream
, mem
);
11030 fputc ('\n', loop_dump_stream
);
11033 /* Attempt a bit of copy propagation. This helps untangle the
11034 data flow, and enables {basic,general}_induction_var to find
11036 EXECUTE_IF_SET_IN_REG_SET
11037 (&load_copies
, FIRST_PSEUDO_REGISTER
, j
, rsi
)
11039 try_copy_prop (loop
, reg
, j
);
11041 CLEAR_REG_SET (&load_copies
);
11043 EXECUTE_IF_SET_IN_REG_SET
11044 (&store_copies
, FIRST_PSEUDO_REGISTER
, j
, rsi
)
11046 try_swap_copy_prop (loop
, reg
, j
);
11048 CLEAR_REG_SET (&store_copies
);
11052 /* Now, we need to replace all references to the previous exit
11053 label with the new one. */
11054 if (label
!= NULL_RTX
&& end_label
!= NULL_RTX
)
11055 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
11056 if (JUMP_P (p
) && JUMP_LABEL (p
) == end_label
)
11057 redirect_jump (p
, label
, false);
11062 /* For communication between note_reg_stored and its caller. */
11063 struct note_reg_stored_arg
11069 /* Called via note_stores, record in SET_SEEN whether X, which is written,
11070 is equal to ARG. */
11072 note_reg_stored (rtx x
, rtx setter ATTRIBUTE_UNUSED
, void *arg
)
11074 struct note_reg_stored_arg
*t
= (struct note_reg_stored_arg
*) arg
;
11079 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
11080 There must be exactly one insn that sets this pseudo; it will be
11081 deleted if all replacements succeed and we can prove that the register
11082 is not used after the loop. */
11085 try_copy_prop (const struct loop
*loop
, rtx replacement
, unsigned int regno
)
11087 /* This is the reg that we are copying from. */
11088 rtx reg_rtx
= regno_reg_rtx
[regno
];
11091 /* These help keep track of whether we replaced all uses of the reg. */
11092 int replaced_last
= 0;
11093 int store_is_first
= 0;
11095 for (insn
= next_insn_in_loop (loop
, loop
->scan_start
);
11097 insn
= next_insn_in_loop (loop
, insn
))
11101 /* Only substitute within one extended basic block from the initializing
11103 if (LABEL_P (insn
) && init_insn
)
11106 if (! INSN_P (insn
))
11109 /* Is this the initializing insn? */
11110 set
= single_set (insn
);
11112 && REG_P (SET_DEST (set
))
11113 && REGNO (SET_DEST (set
)) == regno
)
11119 if (REGNO_FIRST_UID (regno
) == INSN_UID (insn
))
11120 store_is_first
= 1;
11123 /* Only substitute after seeing the initializing insn. */
11124 if (init_insn
&& insn
!= init_insn
)
11126 struct note_reg_stored_arg arg
;
11128 replace_loop_regs (insn
, reg_rtx
, replacement
);
11129 if (REGNO_LAST_UID (regno
) == INSN_UID (insn
))
11132 /* Stop replacing when REPLACEMENT is modified. */
11133 arg
.reg
= replacement
;
11135 note_stores (PATTERN (insn
), note_reg_stored
, &arg
);
11138 rtx note
= find_reg_note (insn
, REG_EQUAL
, NULL
);
11140 /* It is possible that we've turned previously valid REG_EQUAL to
11141 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
11142 REPLACEMENT is modified, we get different meaning. */
11143 if (note
&& reg_mentioned_p (replacement
, XEXP (note
, 0)))
11144 remove_note (insn
, note
);
11151 if (apply_change_group ())
11153 if (loop_dump_stream
)
11154 fprintf (loop_dump_stream
, " Replaced reg %d", regno
);
11155 if (store_is_first
&& replaced_last
)
11160 /* Assume we're just deleting INIT_INSN. */
11162 /* Look for REG_RETVAL note. If we're deleting the end of
11163 the libcall sequence, the whole sequence can go. */
11164 retval_note
= find_reg_note (init_insn
, REG_RETVAL
, NULL_RTX
);
11165 /* If we found a REG_RETVAL note, find the first instruction
11166 in the sequence. */
11168 first
= XEXP (retval_note
, 0);
11170 /* Delete the instructions. */
11171 loop_delete_insns (first
, init_insn
);
11173 if (loop_dump_stream
)
11174 fprintf (loop_dump_stream
, ".\n");
11178 /* Replace all the instructions from FIRST up to and including LAST
11179 with NOTE_INSN_DELETED notes. */
11182 loop_delete_insns (rtx first
, rtx last
)
11186 if (loop_dump_stream
)
11187 fprintf (loop_dump_stream
, ", deleting init_insn (%d)",
11189 delete_insn (first
);
11191 /* If this was the LAST instructions we're supposed to delete,
11196 first
= NEXT_INSN (first
);
11200 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
11201 loop LOOP if the order of the sets of these registers can be
11202 swapped. There must be exactly one insn within the loop that sets
11203 this pseudo followed immediately by a move insn that sets
11204 REPLACEMENT with REGNO. */
11206 try_swap_copy_prop (const struct loop
*loop
, rtx replacement
,
11207 unsigned int regno
)
11210 rtx set
= NULL_RTX
;
11211 unsigned int new_regno
;
11213 new_regno
= REGNO (replacement
);
11215 for (insn
= next_insn_in_loop (loop
, loop
->scan_start
);
11217 insn
= next_insn_in_loop (loop
, insn
))
11219 /* Search for the insn that copies REGNO to NEW_REGNO? */
11221 && (set
= single_set (insn
))
11222 && REG_P (SET_DEST (set
))
11223 && REGNO (SET_DEST (set
)) == new_regno
11224 && REG_P (SET_SRC (set
))
11225 && REGNO (SET_SRC (set
)) == regno
)
11229 if (insn
!= NULL_RTX
)
11234 /* Some DEF-USE info would come in handy here to make this
11235 function more general. For now, just check the previous insn
11236 which is the most likely candidate for setting REGNO. */
11238 prev_insn
= PREV_INSN (insn
);
11241 && (prev_set
= single_set (prev_insn
))
11242 && REG_P (SET_DEST (prev_set
))
11243 && REGNO (SET_DEST (prev_set
)) == regno
)
11246 (set (reg regno) (expr))
11247 (set (reg new_regno) (reg regno))
11249 so try converting this to:
11250 (set (reg new_regno) (expr))
11251 (set (reg regno) (reg new_regno))
11253 The former construct is often generated when a global
11254 variable used for an induction variable is shadowed by a
11255 register (NEW_REGNO). The latter construct improves the
11256 chances of GIV replacement and BIV elimination. */
11258 validate_change (prev_insn
, &SET_DEST (prev_set
),
11260 validate_change (insn
, &SET_DEST (set
),
11262 validate_change (insn
, &SET_SRC (set
),
11265 if (apply_change_group ())
11267 if (loop_dump_stream
)
11268 fprintf (loop_dump_stream
,
11269 " Swapped set of reg %d at %d with reg %d at %d.\n",
11270 regno
, INSN_UID (insn
),
11271 new_regno
, INSN_UID (prev_insn
));
11273 /* Update first use of REGNO. */
11274 if (REGNO_FIRST_UID (regno
) == INSN_UID (prev_insn
))
11275 REGNO_FIRST_UID (regno
) = INSN_UID (insn
);
11277 /* Now perform copy propagation to hopefully
11278 remove all uses of REGNO within the loop. */
11279 try_copy_prop (loop
, replacement
, regno
);
11285 /* Worker function for find_mem_in_note, called via for_each_rtx. */
11288 find_mem_in_note_1 (rtx
*x
, void *data
)
11290 if (*x
!= NULL_RTX
&& MEM_P (*x
))
11292 rtx
*res
= (rtx
*) data
;
11299 /* Returns the first MEM found in NOTE by depth-first search. */
11302 find_mem_in_note (rtx note
)
11304 if (note
&& for_each_rtx (¬e
, find_mem_in_note_1
, ¬e
))
11309 /* Replace MEM with its associated pseudo register. This function is
11310 called from load_mems via for_each_rtx. DATA is actually a pointer
11311 to a structure describing the instruction currently being scanned
11312 and the MEM we are currently replacing. */
11315 replace_loop_mem (rtx
*mem
, void *data
)
11317 loop_replace_args
*args
= (loop_replace_args
*) data
;
11323 switch (GET_CODE (m
))
11329 /* We're not interested in the MEM associated with a
11330 CONST_DOUBLE, so there's no need to traverse into one. */
11334 /* This is not a MEM. */
11338 if (!rtx_equal_p (args
->match
, m
))
11339 /* This is not the MEM we are currently replacing. */
11342 /* Actually replace the MEM. */
11343 validate_change (args
->insn
, mem
, args
->replacement
, 1);
11349 replace_loop_mems (rtx insn
, rtx mem
, rtx reg
, int written
)
11351 loop_replace_args args
;
11355 args
.replacement
= reg
;
11357 for_each_rtx (&insn
, replace_loop_mem
, &args
);
11359 /* If we hoist a mem write out of the loop, then REG_EQUAL
11360 notes referring to the mem are no longer valid. */
11366 for (link
= ®_NOTES (insn
); (note
= *link
); link
= &XEXP (note
, 1))
11368 if (REG_NOTE_KIND (note
) == REG_EQUAL
11369 && (sub
= find_mem_in_note (note
))
11370 && true_dependence (mem
, VOIDmode
, sub
, rtx_varies_p
))
11372 /* Remove the note. */
11373 validate_change (NULL_RTX
, link
, XEXP (note
, 1), 1);
11380 /* Replace one register with another. Called through for_each_rtx; PX points
11381 to the rtx being scanned. DATA is actually a pointer to
11382 a structure of arguments. */
11385 replace_loop_reg (rtx
*px
, void *data
)
11388 loop_replace_args
*args
= (loop_replace_args
*) data
;
11393 if (x
== args
->match
)
11394 validate_change (args
->insn
, px
, args
->replacement
, 1);
11400 replace_loop_regs (rtx insn
, rtx reg
, rtx replacement
)
11402 loop_replace_args args
;
11406 args
.replacement
= replacement
;
11408 for_each_rtx (&insn
, replace_loop_reg
, &args
);
11411 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
11412 (ignored in the interim). */
11415 loop_insn_emit_after (const struct loop
*loop ATTRIBUTE_UNUSED
,
11416 basic_block where_bb ATTRIBUTE_UNUSED
, rtx where_insn
,
11419 return emit_insn_after (pattern
, where_insn
);
11423 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
11424 in basic block WHERE_BB (ignored in the interim) within the loop
11425 otherwise hoist PATTERN into the loop pre-header. */
11428 loop_insn_emit_before (const struct loop
*loop
,
11429 basic_block where_bb ATTRIBUTE_UNUSED
,
11430 rtx where_insn
, rtx pattern
)
11433 return loop_insn_hoist (loop
, pattern
);
11434 return emit_insn_before (pattern
, where_insn
);
11438 /* Emit call insn for PATTERN before WHERE_INSN in basic block
11439 WHERE_BB (ignored in the interim) within the loop. */
11442 loop_call_insn_emit_before (const struct loop
*loop ATTRIBUTE_UNUSED
,
11443 basic_block where_bb ATTRIBUTE_UNUSED
,
11444 rtx where_insn
, rtx pattern
)
11446 return emit_call_insn_before (pattern
, where_insn
);
11450 /* Hoist insn for PATTERN into the loop pre-header. */
11453 loop_insn_hoist (const struct loop
*loop
, rtx pattern
)
11455 return loop_insn_emit_before (loop
, 0, loop
->start
, pattern
);
11459 /* Hoist call insn for PATTERN into the loop pre-header. */
11462 loop_call_insn_hoist (const struct loop
*loop
, rtx pattern
)
11464 return loop_call_insn_emit_before (loop
, 0, loop
->start
, pattern
);
11468 /* Sink insn for PATTERN after the loop end. */
11471 loop_insn_sink (const struct loop
*loop
, rtx pattern
)
11473 return loop_insn_emit_before (loop
, 0, loop
->sink
, pattern
);
11476 /* bl->final_value can be either general_operand or PLUS of general_operand
11477 and constant. Emit sequence of instructions to load it into REG. */
11479 gen_load_of_final_value (rtx reg
, rtx final_value
)
11483 final_value
= force_operand (final_value
, reg
);
11484 if (final_value
!= reg
)
11485 emit_move_insn (reg
, final_value
);
11486 seq
= get_insns ();
11491 /* If the loop has multiple exits, emit insn for PATTERN before the
11492 loop to ensure that it will always be executed no matter how the
11493 loop exits. Otherwise, emit the insn for PATTERN after the loop,
11494 since this is slightly more efficient. */
11497 loop_insn_sink_or_swim (const struct loop
*loop
, rtx pattern
)
11499 if (loop
->exit_count
)
11500 return loop_insn_hoist (loop
, pattern
);
11502 return loop_insn_sink (loop
, pattern
);
11506 loop_ivs_dump (const struct loop
*loop
, FILE *file
, int verbose
)
11508 struct iv_class
*bl
;
11511 if (! loop
|| ! file
)
11514 for (bl
= LOOP_IVS (loop
)->list
; bl
; bl
= bl
->next
)
11517 fprintf (file
, "Loop %d: %d IV classes\n", loop
->num
, iv_num
);
11519 for (bl
= LOOP_IVS (loop
)->list
; bl
; bl
= bl
->next
)
11521 loop_iv_class_dump (bl
, file
, verbose
);
11522 fputc ('\n', file
);
11528 loop_iv_class_dump (const struct iv_class
*bl
, FILE *file
,
11529 int verbose ATTRIBUTE_UNUSED
)
11531 struct induction
*v
;
11535 if (! bl
|| ! file
)
11538 fprintf (file
, "IV class for reg %d, benefit %d\n",
11539 bl
->regno
, bl
->total_benefit
);
11541 fprintf (file
, " Init insn %d", INSN_UID (bl
->init_insn
));
11542 if (bl
->initial_value
)
11544 fprintf (file
, ", init val: ");
11545 print_simple_rtl (file
, bl
->initial_value
);
11547 if (bl
->initial_test
)
11549 fprintf (file
, ", init test: ");
11550 print_simple_rtl (file
, bl
->initial_test
);
11552 fputc ('\n', file
);
11554 if (bl
->final_value
)
11556 fprintf (file
, " Final val: ");
11557 print_simple_rtl (file
, bl
->final_value
);
11558 fputc ('\n', file
);
11561 if ((incr
= biv_total_increment (bl
)))
11563 fprintf (file
, " Total increment: ");
11564 print_simple_rtl (file
, incr
);
11565 fputc ('\n', file
);
11568 /* List the increments. */
11569 for (i
= 0, v
= bl
->biv
; v
; v
= v
->next_iv
, i
++)
11571 fprintf (file
, " Inc%d: insn %d, incr: ", i
, INSN_UID (v
->insn
));
11572 print_simple_rtl (file
, v
->add_val
);
11573 fputc ('\n', file
);
11576 /* List the givs. */
11577 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
, i
++)
11579 fprintf (file
, " Giv%d: insn %d, benefit %d, ",
11580 i
, INSN_UID (v
->insn
), v
->benefit
);
11581 if (v
->giv_type
== DEST_ADDR
)
11582 print_simple_rtl (file
, v
->mem
);
11584 print_simple_rtl (file
, single_set (v
->insn
));
11585 fputc ('\n', file
);
11591 loop_biv_dump (const struct induction
*v
, FILE *file
, int verbose
)
11598 REGNO (v
->dest_reg
), INSN_UID (v
->insn
));
11599 fprintf (file
, " const ");
11600 print_simple_rtl (file
, v
->add_val
);
11602 if (verbose
&& v
->final_value
)
11604 fputc ('\n', file
);
11605 fprintf (file
, " final ");
11606 print_simple_rtl (file
, v
->final_value
);
11609 fputc ('\n', file
);
11614 loop_giv_dump (const struct induction
*v
, FILE *file
, int verbose
)
11619 if (v
->giv_type
== DEST_REG
)
11620 fprintf (file
, "Giv %d: insn %d",
11621 REGNO (v
->dest_reg
), INSN_UID (v
->insn
));
11623 fprintf (file
, "Dest address: insn %d",
11624 INSN_UID (v
->insn
));
11626 fprintf (file
, " src reg %d benefit %d",
11627 REGNO (v
->src_reg
), v
->benefit
);
11628 fprintf (file
, " lifetime %d",
11631 if (v
->replaceable
)
11632 fprintf (file
, " replaceable");
11634 if (v
->no_const_addval
)
11635 fprintf (file
, " ncav");
11637 if (v
->ext_dependent
)
11639 switch (GET_CODE (v
->ext_dependent
))
11642 fprintf (file
, " ext se");
11645 fprintf (file
, " ext ze");
11648 fprintf (file
, " ext tr");
11655 fputc ('\n', file
);
11656 fprintf (file
, " mult ");
11657 print_simple_rtl (file
, v
->mult_val
);
11659 fputc ('\n', file
);
11660 fprintf (file
, " add ");
11661 print_simple_rtl (file
, v
->add_val
);
11663 if (verbose
&& v
->final_value
)
11665 fputc ('\n', file
);
11666 fprintf (file
, " final ");
11667 print_simple_rtl (file
, v
->final_value
);
11670 fputc ('\n', file
);
11675 debug_ivs (const struct loop
*loop
)
11677 loop_ivs_dump (loop
, stderr
, 1);
11682 debug_iv_class (const struct iv_class
*bl
)
11684 loop_iv_class_dump (bl
, stderr
, 1);
11689 debug_biv (const struct induction
*v
)
11691 loop_biv_dump (v
, stderr
, 1);
11696 debug_giv (const struct induction
*v
)
11698 loop_giv_dump (v
, stderr
, 1);
11702 #define LOOP_BLOCK_NUM_1(INSN) \
11703 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
11705 /* The notes do not have an assigned block, so look at the next insn. */
11706 #define LOOP_BLOCK_NUM(INSN) \
11707 ((INSN) ? (NOTE_P (INSN) \
11708 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
11709 : LOOP_BLOCK_NUM_1 (INSN)) \
11712 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
11715 loop_dump_aux (const struct loop
*loop
, FILE *file
,
11716 int verbose ATTRIBUTE_UNUSED
)
11720 if (! loop
|| ! file
|| !BB_HEAD (loop
->first
))
11723 /* Print diagnostics to compare our concept of a loop with
11724 what the loop notes say. */
11725 if (! PREV_INSN (BB_HEAD (loop
->first
))
11726 || !NOTE_P (PREV_INSN (BB_HEAD (loop
->first
)))
11727 || NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (loop
->first
)))
11728 != NOTE_INSN_LOOP_BEG
)
11729 fprintf (file
, ";; No NOTE_INSN_LOOP_BEG at %d\n",
11730 INSN_UID (PREV_INSN (BB_HEAD (loop
->first
))));
11731 if (! NEXT_INSN (BB_END (loop
->last
))
11732 || !NOTE_P (NEXT_INSN (BB_END (loop
->last
)))
11733 || NOTE_LINE_NUMBER (NEXT_INSN (BB_END (loop
->last
)))
11734 != NOTE_INSN_LOOP_END
)
11735 fprintf (file
, ";; No NOTE_INSN_LOOP_END at %d\n",
11736 INSN_UID (NEXT_INSN (BB_END (loop
->last
))));
11741 ";; start %d (%d), end %d (%d)\n",
11742 LOOP_BLOCK_NUM (loop
->start
),
11743 LOOP_INSN_UID (loop
->start
),
11744 LOOP_BLOCK_NUM (loop
->end
),
11745 LOOP_INSN_UID (loop
->end
));
11746 fprintf (file
, ";; top %d (%d), scan start %d (%d)\n",
11747 LOOP_BLOCK_NUM (loop
->top
),
11748 LOOP_INSN_UID (loop
->top
),
11749 LOOP_BLOCK_NUM (loop
->scan_start
),
11750 LOOP_INSN_UID (loop
->scan_start
));
11751 fprintf (file
, ";; exit_count %d", loop
->exit_count
);
11752 if (loop
->exit_count
)
11754 fputs (", labels:", file
);
11755 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
11757 fprintf (file
, " %d ",
11758 LOOP_INSN_UID (XEXP (label
, 0)));
11761 fputs ("\n", file
);
11765 /* Call this function from the debugger to dump LOOP. */
11768 debug_loop (const struct loop
*loop
)
11770 flow_loop_dump (loop
, stderr
, loop_dump_aux
, 1);
11773 /* Call this function from the debugger to dump LOOPS. */
11776 debug_loops (const struct loops
*loops
)
11778 flow_loops_dump (loops
, stderr
, loop_dump_aux
, 1);