1 /* Perform various loop optimizations, including strength reduction.
2 Copyright (C) 1987, 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This is the loop optimization pass of the compiler.
23 It finds invariant computations within loops and moves them
24 to the beginning of the loop. Then it identifies basic and
25 general induction variables.
27 Basic induction variables (BIVs) are a pseudo registers which are set within
28 a loop only by incrementing or decrementing its value. General induction
29 variables (GIVs) are pseudo registers with a value which is a linear function
30 of a basic induction variable. BIVs are recognized by `basic_induction_var';
31 GIVs by `general_induction_var'.
33 Once induction variables are identified, strength reduction is applied to the
34 general induction variables, and induction variable elimination is applied to
35 the basic induction variables.
37 It also finds cases where
38 a register is set within the loop by zero-extending a narrower value
39 and changes these to zero the entire register once before the loop
40 and merely copy the low part within the loop.
42 Most of the complexity is in heuristics to decide when it is worth
43 while to do these things. */
47 #include "coretypes.h"
53 #include "hard-reg-set.h"
54 #include "basic-block.h"
55 #include "insn-config.h"
65 #include "insn-flags.h"
70 /* Not really meaningful values, but at least something. */
71 #ifndef SIMULTANEOUS_PREFETCHES
72 #define SIMULTANEOUS_PREFETCHES 3
74 #ifndef PREFETCH_BLOCK
75 #define PREFETCH_BLOCK 32
78 #define HAVE_prefetch 0
79 #define CODE_FOR_prefetch 0
80 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
83 /* Give up the prefetch optimizations once we exceed a given threshold.
84 It is unlikely that we would be able to optimize something in a loop
85 with so many detected prefetches. */
86 #define MAX_PREFETCHES 100
87 /* The number of prefetch blocks that are beneficial to fetch at once before
88 a loop with a known (and low) iteration count. */
89 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
90 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
91 since it is likely that the data are already in the cache. */
92 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
94 /* Parameterize some prefetch heuristics so they can be turned on and off
95 easily for performance testing on new architectures. These can be
96 defined in target-dependent files. */
98 /* Prefetch is worthwhile only when loads/stores are dense. */
99 #ifndef PREFETCH_ONLY_DENSE_MEM
100 #define PREFETCH_ONLY_DENSE_MEM 1
103 /* Define what we mean by "dense" loads and stores; This value divided by 256
104 is the minimum percentage of memory references that worth prefetching. */
105 #ifndef PREFETCH_DENSE_MEM
106 #define PREFETCH_DENSE_MEM 220
109 /* Do not prefetch for a loop whose iteration count is known to be low. */
110 #ifndef PREFETCH_NO_LOW_LOOPCNT
111 #define PREFETCH_NO_LOW_LOOPCNT 1
114 /* Define what we mean by a "low" iteration count. */
115 #ifndef PREFETCH_LOW_LOOPCNT
116 #define PREFETCH_LOW_LOOPCNT 32
119 /* Do not prefetch for a loop that contains a function call; such a loop is
120 probably not an internal loop. */
121 #ifndef PREFETCH_NO_CALL
122 #define PREFETCH_NO_CALL 1
125 /* Do not prefetch accesses with an extreme stride. */
126 #ifndef PREFETCH_NO_EXTREME_STRIDE
127 #define PREFETCH_NO_EXTREME_STRIDE 1
130 /* Define what we mean by an "extreme" stride. */
131 #ifndef PREFETCH_EXTREME_STRIDE
132 #define PREFETCH_EXTREME_STRIDE 4096
135 /* Define a limit to how far apart indices can be and still be merged
136 into a single prefetch. */
137 #ifndef PREFETCH_EXTREME_DIFFERENCE
138 #define PREFETCH_EXTREME_DIFFERENCE 4096
141 /* Issue prefetch instructions before the loop to fetch data to be used
142 in the first few loop iterations. */
143 #ifndef PREFETCH_BEFORE_LOOP
144 #define PREFETCH_BEFORE_LOOP 1
147 /* Do not handle reversed order prefetches (negative stride). */
148 #ifndef PREFETCH_NO_REVERSE_ORDER
149 #define PREFETCH_NO_REVERSE_ORDER 1
152 /* Prefetch even if the GIV is in conditional code. */
153 #ifndef PREFETCH_CONDITIONAL
154 #define PREFETCH_CONDITIONAL 1
157 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
158 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
160 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
161 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
162 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
164 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
165 ((REGNO) < FIRST_PSEUDO_REGISTER \
166 ? (int) HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
169 /* Vector mapping INSN_UIDs to luids.
170 The luids are like uids but increase monotonically always.
171 We use them to see whether a jump comes from outside a given loop. */
175 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
176 number the insn is contained in. */
178 struct loop
**uid_loop
;
180 /* 1 + largest uid of any insn. */
182 int max_uid_for_loop
;
184 /* Number of loops detected in current function. Used as index to the
187 static int max_loop_num
;
189 /* Bound on pseudo register number before loop optimization.
190 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
191 unsigned int max_reg_before_loop
;
193 /* The value to pass to the next call of reg_scan_update. */
194 static int loop_max_reg
;
196 /* During the analysis of a loop, a chain of `struct movable's
197 is made to record all the movable insns found.
198 Then the entire chain can be scanned to decide which to move. */
202 rtx insn
; /* A movable insn */
203 rtx set_src
; /* The expression this reg is set from. */
204 rtx set_dest
; /* The destination of this SET. */
205 rtx dependencies
; /* When INSN is libcall, this is an EXPR_LIST
206 of any registers used within the LIBCALL. */
207 int consec
; /* Number of consecutive following insns
208 that must be moved with this one. */
209 unsigned int regno
; /* The register it sets */
210 short lifetime
; /* lifetime of that register;
211 may be adjusted when matching movables
212 that load the same value are found. */
213 short savings
; /* Number of insns we can move for this reg,
214 including other movables that force this
215 or match this one. */
216 ENUM_BITFIELD(machine_mode
) savemode
: 8; /* Nonzero means it is a mode for
217 a low part that we should avoid changing when
218 clearing the rest of the reg. */
219 unsigned int cond
: 1; /* 1 if only conditionally movable */
220 unsigned int force
: 1; /* 1 means MUST move this insn */
221 unsigned int global
: 1; /* 1 means reg is live outside this loop */
222 /* If PARTIAL is 1, GLOBAL means something different:
223 that the reg is live outside the range from where it is set
224 to the following label. */
225 unsigned int done
: 1; /* 1 inhibits further processing of this */
227 unsigned int partial
: 1; /* 1 means this reg is used for zero-extending.
228 In particular, moving it does not make it
230 unsigned int move_insn
: 1; /* 1 means that we call emit_move_insn to
231 load SRC, rather than copying INSN. */
232 unsigned int move_insn_first
:1;/* Same as above, if this is necessary for the
233 first insn of a consecutive sets group. */
234 unsigned int is_equiv
: 1; /* 1 means a REG_EQUIV is present on INSN. */
235 unsigned int insert_temp
: 1; /* 1 means we copy to a new pseudo and replace
236 the original insn with a copy from that
237 pseudo, rather than deleting it. */
238 struct movable
*match
; /* First entry for same value */
239 struct movable
*forces
; /* An insn that must be moved if this is */
240 struct movable
*next
;
244 FILE *loop_dump_stream
;
246 /* Forward declarations. */
248 static void invalidate_loops_containing_label (rtx
);
249 static void find_and_verify_loops (rtx
, struct loops
*);
250 static void mark_loop_jump (rtx
, struct loop
*);
251 static void prescan_loop (struct loop
*);
252 static int reg_in_basic_block_p (rtx
, rtx
);
253 static int consec_sets_invariant_p (const struct loop
*, rtx
, int, rtx
);
254 static int labels_in_range_p (rtx
, int);
255 static void count_one_set (struct loop_regs
*, rtx
, rtx
, rtx
*);
256 static void note_addr_stored (rtx
, rtx
, void *);
257 static void note_set_pseudo_multiple_uses (rtx
, rtx
, void *);
258 static int loop_reg_used_before_p (const struct loop
*, rtx
, rtx
);
259 static rtx
find_regs_nested (rtx
, rtx
);
260 static void scan_loop (struct loop
*, int);
262 static void replace_call_address (rtx
, rtx
, rtx
);
264 static rtx
skip_consec_insns (rtx
, int);
265 static int libcall_benefit (rtx
);
266 static void ignore_some_movables (struct loop_movables
*);
267 static void force_movables (struct loop_movables
*);
268 static void combine_movables (struct loop_movables
*, struct loop_regs
*);
269 static int num_unmoved_movables (const struct loop
*);
270 static int regs_match_p (rtx
, rtx
, struct loop_movables
*);
271 static int rtx_equal_for_loop_p (rtx
, rtx
, struct loop_movables
*,
273 static void add_label_notes (rtx
, rtx
);
274 static void move_movables (struct loop
*loop
, struct loop_movables
*, int,
276 static void loop_movables_add (struct loop_movables
*, struct movable
*);
277 static void loop_movables_free (struct loop_movables
*);
278 static int count_nonfixed_reads (const struct loop
*, rtx
);
279 static void loop_bivs_find (struct loop
*);
280 static void loop_bivs_init_find (struct loop
*);
281 static void loop_bivs_check (struct loop
*);
282 static void loop_givs_find (struct loop
*);
283 static void loop_givs_check (struct loop
*);
284 static int loop_biv_eliminable_p (struct loop
*, struct iv_class
*, int, int);
285 static int loop_giv_reduce_benefit (struct loop
*, struct iv_class
*,
286 struct induction
*, rtx
);
287 static void loop_givs_dead_check (struct loop
*, struct iv_class
*);
288 static void loop_givs_reduce (struct loop
*, struct iv_class
*);
289 static void loop_givs_rescan (struct loop
*, struct iv_class
*, rtx
*);
290 static void loop_ivs_free (struct loop
*);
291 static void strength_reduce (struct loop
*, int);
292 static void find_single_use_in_loop (struct loop_regs
*, rtx
, rtx
);
293 static int valid_initial_value_p (rtx
, rtx
, int, rtx
);
294 static void find_mem_givs (const struct loop
*, rtx
, rtx
, int, int);
295 static void record_biv (struct loop
*, struct induction
*, rtx
, rtx
, rtx
,
296 rtx
, rtx
*, int, int);
297 static void check_final_value (const struct loop
*, struct induction
*);
298 static void loop_ivs_dump (const struct loop
*, FILE *, int);
299 static void loop_iv_class_dump (const struct iv_class
*, FILE *, int);
300 static void loop_biv_dump (const struct induction
*, FILE *, int);
301 static void loop_giv_dump (const struct induction
*, FILE *, int);
302 static void record_giv (const struct loop
*, struct induction
*, rtx
, rtx
,
303 rtx
, rtx
, rtx
, rtx
, int, enum g_types
, int, int,
305 static void update_giv_derive (const struct loop
*, rtx
);
306 static HOST_WIDE_INT
get_monotonic_increment (struct iv_class
*);
307 static bool biased_biv_fits_mode_p (const struct loop
*, struct iv_class
*,
308 HOST_WIDE_INT
, enum machine_mode
,
309 unsigned HOST_WIDE_INT
);
310 static bool biv_fits_mode_p (const struct loop
*, struct iv_class
*,
311 HOST_WIDE_INT
, enum machine_mode
, bool);
312 static bool extension_within_bounds_p (const struct loop
*, struct iv_class
*,
314 static void check_ext_dependent_givs (const struct loop
*, struct iv_class
*);
315 static int basic_induction_var (const struct loop
*, rtx
, enum machine_mode
,
316 rtx
, rtx
, rtx
*, rtx
*, rtx
**);
317 static rtx
simplify_giv_expr (const struct loop
*, rtx
, rtx
*, int *);
318 static int general_induction_var (const struct loop
*loop
, rtx
, rtx
*, rtx
*,
319 rtx
*, rtx
*, int, int *, enum machine_mode
);
320 static int consec_sets_giv (const struct loop
*, int, rtx
, rtx
, rtx
, rtx
*,
321 rtx
*, rtx
*, rtx
*);
322 static int check_dbra_loop (struct loop
*, int);
323 static rtx
express_from_1 (rtx
, rtx
, rtx
);
324 static rtx
combine_givs_p (struct induction
*, struct induction
*);
325 static int cmp_combine_givs_stats (const void *, const void *);
326 static void combine_givs (struct loop_regs
*, struct iv_class
*);
327 static int product_cheap_p (rtx
, rtx
);
328 static int maybe_eliminate_biv (const struct loop
*, struct iv_class
*, int,
330 static int maybe_eliminate_biv_1 (const struct loop
*, rtx
, rtx
,
331 struct iv_class
*, int, basic_block
, rtx
);
332 static int last_use_this_basic_block (rtx
, rtx
);
333 static void record_initial (rtx
, rtx
, void *);
334 static void update_reg_last_use (rtx
, rtx
);
335 static rtx
next_insn_in_loop (const struct loop
*, rtx
);
336 static void loop_regs_scan (const struct loop
*, int);
337 static int count_insns_in_loop (const struct loop
*);
338 static int find_mem_in_note_1 (rtx
*, void *);
339 static rtx
find_mem_in_note (rtx
);
340 static void load_mems (const struct loop
*);
341 static int insert_loop_mem (rtx
*, void *);
342 static int replace_loop_mem (rtx
*, void *);
343 static void replace_loop_mems (rtx
, rtx
, rtx
, int);
344 static int replace_loop_reg (rtx
*, void *);
345 static void replace_loop_regs (rtx insn
, rtx
, rtx
);
346 static void note_reg_stored (rtx
, rtx
, void *);
347 static void try_copy_prop (const struct loop
*, rtx
, unsigned int);
348 static void try_swap_copy_prop (const struct loop
*, rtx
, unsigned int);
349 static rtx
check_insn_for_givs (struct loop
*, rtx
, int, int);
350 static rtx
check_insn_for_bivs (struct loop
*, rtx
, int, int);
351 static rtx
gen_add_mult (rtx
, rtx
, rtx
, rtx
);
352 static void loop_regs_update (const struct loop
*, rtx
);
353 static int iv_add_mult_cost (rtx
, rtx
, rtx
, rtx
);
355 static rtx
loop_insn_emit_after (const struct loop
*, basic_block
, rtx
, rtx
);
356 static rtx
loop_call_insn_emit_before (const struct loop
*, basic_block
,
358 static rtx
loop_call_insn_hoist (const struct loop
*, rtx
);
359 static rtx
loop_insn_sink_or_swim (const struct loop
*, rtx
);
361 static void loop_dump_aux (const struct loop
*, FILE *, int);
362 static void loop_delete_insns (rtx
, rtx
);
363 static HOST_WIDE_INT
remove_constant_addition (rtx
*);
364 static rtx
gen_load_of_final_value (rtx
, rtx
);
365 void debug_ivs (const struct loop
*);
366 void debug_iv_class (const struct iv_class
*);
367 void debug_biv (const struct induction
*);
368 void debug_giv (const struct induction
*);
369 void debug_loop (const struct loop
*);
370 void debug_loops (const struct loops
*);
372 typedef struct loop_replace_args
379 /* Nonzero iff INSN is between START and END, inclusive. */
380 #define INSN_IN_RANGE_P(INSN, START, END) \
381 (INSN_UID (INSN) < max_uid_for_loop \
382 && INSN_LUID (INSN) >= INSN_LUID (START) \
383 && INSN_LUID (INSN) <= INSN_LUID (END))
385 /* Indirect_jump_in_function is computed once per function. */
386 static int indirect_jump_in_function
;
387 static int indirect_jump_in_function_p (rtx
);
389 static int compute_luids (rtx
, rtx
, int);
391 static int biv_elimination_giv_has_0_offset (struct induction
*,
392 struct induction
*, rtx
);
394 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
395 copy the value of the strength reduced giv to its original register. */
396 static int copy_cost
;
398 /* Cost of using a register, to normalize the benefits of a giv. */
399 static int reg_address_cost
;
404 rtx reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
406 reg_address_cost
= address_cost (reg
, SImode
);
408 copy_cost
= COSTS_N_INSNS (1);
411 /* Compute the mapping from uids to luids.
412 LUIDs are numbers assigned to insns, like uids,
413 except that luids increase monotonically through the code.
414 Start at insn START and stop just before END. Assign LUIDs
415 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
417 compute_luids (rtx start
, rtx end
, int prev_luid
)
422 for (insn
= start
, i
= prev_luid
; insn
!= end
; insn
= NEXT_INSN (insn
))
424 if (INSN_UID (insn
) >= max_uid_for_loop
)
426 /* Don't assign luids to line-number NOTEs, so that the distance in
427 luids between two insns is not affected by -g. */
428 if (GET_CODE (insn
) != NOTE
429 || NOTE_LINE_NUMBER (insn
) <= 0)
430 uid_luid
[INSN_UID (insn
)] = ++i
;
432 /* Give a line number note the same luid as preceding insn. */
433 uid_luid
[INSN_UID (insn
)] = i
;
438 /* Entry point of this file. Perform loop optimization
439 on the current function. F is the first insn of the function
440 and DUMPFILE is a stream for output of a trace of actions taken
441 (or 0 if none should be output). */
444 loop_optimize (rtx f
, FILE *dumpfile
, int flags
)
448 struct loops loops_data
;
449 struct loops
*loops
= &loops_data
;
450 struct loop_info
*loops_info
;
452 loop_dump_stream
= dumpfile
;
454 init_recog_no_volatile ();
456 max_reg_before_loop
= max_reg_num ();
457 loop_max_reg
= max_reg_before_loop
;
461 /* Count the number of loops. */
464 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
466 if (GET_CODE (insn
) == NOTE
467 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
471 /* Don't waste time if no loops. */
472 if (max_loop_num
== 0)
475 loops
->num
= max_loop_num
;
477 /* Get size to use for tables indexed by uids.
478 Leave some space for labels allocated by find_and_verify_loops. */
479 max_uid_for_loop
= get_max_uid () + 1 + max_loop_num
* 32;
481 uid_luid
= xcalloc (max_uid_for_loop
, sizeof (int));
482 uid_loop
= xcalloc (max_uid_for_loop
, sizeof (struct loop
*));
484 /* Allocate storage for array of loops. */
485 loops
->array
= xcalloc (loops
->num
, sizeof (struct loop
));
487 /* Find and process each loop.
488 First, find them, and record them in order of their beginnings. */
489 find_and_verify_loops (f
, loops
);
491 /* Allocate and initialize auxiliary loop information. */
492 loops_info
= xcalloc (loops
->num
, sizeof (struct loop_info
));
493 for (i
= 0; i
< (int) loops
->num
; i
++)
494 loops
->array
[i
].aux
= loops_info
+ i
;
496 /* Now find all register lifetimes. This must be done after
497 find_and_verify_loops, because it might reorder the insns in the
499 reg_scan (f
, max_reg_before_loop
, 1);
501 /* This must occur after reg_scan so that registers created by gcse
502 will have entries in the register tables.
504 We could have added a call to reg_scan after gcse_main in toplev.c,
505 but moving this call to init_alias_analysis is more efficient. */
506 init_alias_analysis ();
508 /* See if we went too far. Note that get_max_uid already returns
509 one more that the maximum uid of all insn. */
510 if (get_max_uid () > max_uid_for_loop
)
512 /* Now reset it to the actual size we need. See above. */
513 max_uid_for_loop
= get_max_uid ();
515 /* find_and_verify_loops has already called compute_luids, but it
516 might have rearranged code afterwards, so we need to recompute
518 compute_luids (f
, NULL_RTX
, 0);
520 /* Don't leave gaps in uid_luid for insns that have been
521 deleted. It is possible that the first or last insn
522 using some register has been deleted by cross-jumping.
523 Make sure that uid_luid for that former insn's uid
524 points to the general area where that insn used to be. */
525 for (i
= 0; i
< max_uid_for_loop
; i
++)
527 uid_luid
[0] = uid_luid
[i
];
528 if (uid_luid
[0] != 0)
531 for (i
= 0; i
< max_uid_for_loop
; i
++)
532 if (uid_luid
[i
] == 0)
533 uid_luid
[i
] = uid_luid
[i
- 1];
535 /* Determine if the function has indirect jump. On some systems
536 this prevents low overhead loop instructions from being used. */
537 indirect_jump_in_function
= indirect_jump_in_function_p (f
);
539 /* Now scan the loops, last ones first, since this means inner ones are done
540 before outer ones. */
541 for (i
= max_loop_num
- 1; i
>= 0; i
--)
543 struct loop
*loop
= &loops
->array
[i
];
545 if (! loop
->invalid
&& loop
->end
)
547 scan_loop (loop
, flags
);
552 end_alias_analysis ();
555 for (i
= 0; i
< (int) loops
->num
; i
++)
556 free (loops_info
[i
].mems
);
564 /* Returns the next insn, in execution order, after INSN. START and
565 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
566 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
567 insn-stream; it is used with loops that are entered near the
571 next_insn_in_loop (const struct loop
*loop
, rtx insn
)
573 insn
= NEXT_INSN (insn
);
575 if (insn
== loop
->end
)
578 /* Go to the top of the loop, and continue there. */
585 if (insn
== loop
->scan_start
)
592 /* Find any register references hidden inside X and add them to
593 the dependency list DEPS. This is used to look inside CLOBBER (MEM
594 when checking whether a PARALLEL can be pulled out of a loop. */
597 find_regs_nested (rtx deps
, rtx x
)
599 enum rtx_code code
= GET_CODE (x
);
601 deps
= gen_rtx_EXPR_LIST (VOIDmode
, x
, deps
);
604 const char *fmt
= GET_RTX_FORMAT (code
);
606 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
609 deps
= find_regs_nested (deps
, XEXP (x
, i
));
610 else if (fmt
[i
] == 'E')
611 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
612 deps
= find_regs_nested (deps
, XVECEXP (x
, i
, j
));
618 /* Optimize one loop described by LOOP. */
620 /* ??? Could also move memory writes out of loops if the destination address
621 is invariant, the source is invariant, the memory write is not volatile,
622 and if we can prove that no read inside the loop can read this address
623 before the write occurs. If there is a read of this address after the
624 write, then we can also mark the memory read as invariant. */
627 scan_loop (struct loop
*loop
, int flags
)
629 struct loop_info
*loop_info
= LOOP_INFO (loop
);
630 struct loop_regs
*regs
= LOOP_REGS (loop
);
632 rtx loop_start
= loop
->start
;
633 rtx loop_end
= loop
->end
;
635 /* 1 if we are scanning insns that could be executed zero times. */
637 /* 1 if we are scanning insns that might never be executed
638 due to a subroutine call which might exit before they are reached. */
640 /* Number of insns in the loop. */
643 rtx temp
, update_start
, update_end
;
644 /* The SET from an insn, if it is the only SET in the insn. */
646 /* Chain describing insns movable in current loop. */
647 struct loop_movables
*movables
= LOOP_MOVABLES (loop
);
648 /* Ratio of extra register life span we can justify
649 for saving an instruction. More if loop doesn't call subroutines
650 since in that case saving an insn makes more difference
651 and more registers are available. */
653 /* Nonzero if we are scanning instructions in a sub-loop. */
662 /* Determine whether this loop starts with a jump down to a test at
663 the end. This will occur for a small number of loops with a test
664 that is too complex to duplicate in front of the loop.
666 We search for the first insn or label in the loop, skipping NOTEs.
667 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
668 (because we might have a loop executed only once that contains a
669 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
670 (in case we have a degenerate loop).
672 Note that if we mistakenly think that a loop is entered at the top
673 when, in fact, it is entered at the exit test, the only effect will be
674 slightly poorer optimization. Making the opposite error can generate
675 incorrect code. Since very few loops now start with a jump to the
676 exit test, the code here to detect that case is very conservative. */
678 for (p
= NEXT_INSN (loop_start
);
680 && GET_CODE (p
) != CODE_LABEL
&& ! INSN_P (p
)
681 && (GET_CODE (p
) != NOTE
682 || (NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_BEG
683 && NOTE_LINE_NUMBER (p
) != NOTE_INSN_LOOP_END
));
687 loop
->scan_start
= p
;
689 /* If loop end is the end of the current function, then emit a
690 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
691 note insn. This is the position we use when sinking insns out of
693 if (NEXT_INSN (loop
->end
) != 0)
694 loop
->sink
= NEXT_INSN (loop
->end
);
696 loop
->sink
= emit_note_after (NOTE_INSN_DELETED
, loop
->end
);
698 /* Set up variables describing this loop. */
700 threshold
= (loop_info
->has_call
? 1 : 2) * (1 + n_non_fixed_regs
);
702 /* If loop has a jump before the first label,
703 the true entry is the target of that jump.
704 Start scan from there.
705 But record in LOOP->TOP the place where the end-test jumps
706 back to so we can scan that after the end of the loop. */
707 if (GET_CODE (p
) == JUMP_INSN
708 /* Loop entry must be unconditional jump (and not a RETURN) */
709 && any_uncondjump_p (p
)
710 && JUMP_LABEL (p
) != 0
711 /* Check to see whether the jump actually
712 jumps out of the loop (meaning it's no loop).
713 This case can happen for things like
714 do {..} while (0). If this label was generated previously
715 by loop, we can't tell anything about it and have to reject
717 && INSN_IN_RANGE_P (JUMP_LABEL (p
), loop_start
, loop_end
))
719 loop
->top
= next_label (loop
->scan_start
);
720 loop
->scan_start
= JUMP_LABEL (p
);
723 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
724 as required by loop_reg_used_before_p. So skip such loops. (This
725 test may never be true, but it's best to play it safe.)
727 Also, skip loops where we do not start scanning at a label. This
728 test also rejects loops starting with a JUMP_INSN that failed the
731 if (INSN_UID (loop
->scan_start
) >= max_uid_for_loop
732 || GET_CODE (loop
->scan_start
) != CODE_LABEL
)
734 if (loop_dump_stream
)
735 fprintf (loop_dump_stream
, "\nLoop from %d to %d is phony.\n\n",
736 INSN_UID (loop_start
), INSN_UID (loop_end
));
740 /* Allocate extra space for REGs that might be created by load_mems.
741 We allocate a little extra slop as well, in the hopes that we
742 won't have to reallocate the regs array. */
743 loop_regs_scan (loop
, loop_info
->mems_idx
+ 16);
744 insn_count
= count_insns_in_loop (loop
);
746 if (loop_dump_stream
)
748 fprintf (loop_dump_stream
, "\nLoop from %d to %d: %d real insns.\n",
749 INSN_UID (loop_start
), INSN_UID (loop_end
), insn_count
);
751 fprintf (loop_dump_stream
, "Continue at insn %d.\n",
752 INSN_UID (loop
->cont
));
755 /* Scan through the loop finding insns that are safe to move.
756 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
757 this reg will be considered invariant for subsequent insns.
758 We consider whether subsequent insns use the reg
759 in deciding whether it is worth actually moving.
761 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
762 and therefore it is possible that the insns we are scanning
763 would never be executed. At such times, we must make sure
764 that it is safe to execute the insn once instead of zero times.
765 When MAYBE_NEVER is 0, all insns will be executed at least once
766 so that is not a problem. */
768 for (in_libcall
= 0, p
= next_insn_in_loop (loop
, loop
->scan_start
);
770 p
= next_insn_in_loop (loop
, p
))
772 if (in_libcall
&& INSN_P (p
) && find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
774 if (GET_CODE (p
) == INSN
)
776 /* Do not scan past an optimization barrier. */
777 if (GET_CODE (PATTERN (p
)) == ASM_INPUT
)
779 temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
783 && (set
= single_set (p
))
784 && GET_CODE (SET_DEST (set
)) == REG
785 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
786 && SET_DEST (set
) != pic_offset_table_rtx
788 && ! regs
->array
[REGNO (SET_DEST (set
))].may_not_optimize
)
794 rtx src
= SET_SRC (set
);
795 rtx dependencies
= 0;
797 /* Figure out what to use as a source of this insn. If a
798 REG_EQUIV note is given or if a REG_EQUAL note with a
799 constant operand is specified, use it as the source and
800 mark that we should move this insn by calling
801 emit_move_insn rather that duplicating the insn.
803 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
805 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
807 src
= XEXP (temp
, 0), move_insn
= 1;
810 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
811 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
812 src
= XEXP (temp
, 0), move_insn
= 1;
813 if (temp
&& find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
815 src
= XEXP (temp
, 0);
816 /* A libcall block can use regs that don't appear in
817 the equivalent expression. To move the libcall,
818 we must move those regs too. */
819 dependencies
= libcall_other_reg (p
, src
);
823 /* For parallels, add any possible uses to the dependencies, as
824 we can't move the insn without resolving them first.
825 MEMs inside CLOBBERs may also reference registers; these
826 count as implicit uses. */
827 if (GET_CODE (PATTERN (p
)) == PARALLEL
)
829 for (i
= 0; i
< XVECLEN (PATTERN (p
), 0); i
++)
831 rtx x
= XVECEXP (PATTERN (p
), 0, i
);
832 if (GET_CODE (x
) == USE
)
834 = gen_rtx_EXPR_LIST (VOIDmode
, XEXP (x
, 0),
836 else if (GET_CODE (x
) == CLOBBER
837 && GET_CODE (XEXP (x
, 0)) == MEM
)
838 dependencies
= find_regs_nested (dependencies
,
839 XEXP (XEXP (x
, 0), 0));
843 if (/* The register is used in basic blocks other
844 than the one where it is set (meaning that
845 something after this point in the loop might
846 depend on its value before the set). */
847 ! reg_in_basic_block_p (p
, SET_DEST (set
))
848 /* And the set is not guaranteed to be executed once
849 the loop starts, or the value before the set is
850 needed before the set occurs...
852 ??? Note we have quadratic behavior here, mitigated
853 by the fact that the previous test will often fail for
854 large loops. Rather than re-scanning the entire loop
855 each time for register usage, we should build tables
856 of the register usage and use them here instead. */
858 || loop_reg_used_before_p (loop
, set
, p
)))
859 /* It is unsafe to move the set. However, it may be OK to
860 move the source into a new pseudo, and substitute a
861 reg-to-reg copy for the original insn.
863 This code used to consider it OK to move a set of a variable
864 which was not created by the user and not used in an exit
866 That behavior is incorrect and was removed. */
869 /* Don't try to optimize a MODE_CC set with a constant
870 source. It probably will be combined with a conditional
872 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set
))) == MODE_CC
875 /* Don't try to optimize a register that was made
876 by loop-optimization for an inner loop.
877 We don't know its life-span, so we can't compute
879 else if (REGNO (SET_DEST (set
)) >= max_reg_before_loop
)
881 /* Don't move the source and add a reg-to-reg copy:
882 - with -Os (this certainly increases size),
883 - if the mode doesn't support copy operations (obviously),
884 - if the source is already a reg (the motion will gain nothing),
885 - if the source is a legitimate constant (likewise). */
888 || ! can_copy_p (GET_MODE (SET_SRC (set
)))
889 || GET_CODE (SET_SRC (set
)) == REG
890 || (CONSTANT_P (SET_SRC (set
))
891 && LEGITIMATE_CONSTANT_P (SET_SRC (set
)))))
893 else if ((tem
= loop_invariant_p (loop
, src
))
894 && (dependencies
== 0
896 = loop_invariant_p (loop
, dependencies
)) != 0)
897 && (regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
== 1
899 = consec_sets_invariant_p
900 (loop
, SET_DEST (set
),
901 regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
,
903 /* If the insn can cause a trap (such as divide by zero),
904 can't move it unless it's guaranteed to be executed
905 once loop is entered. Even a function call might
906 prevent the trap insn from being reached
907 (since it might exit!) */
908 && ! ((maybe_never
|| call_passed
)
909 && may_trap_p (src
)))
912 int regno
= REGNO (SET_DEST (set
));
914 /* A potential lossage is where we have a case where two insns
915 can be combined as long as they are both in the loop, but
916 we move one of them outside the loop. For large loops,
917 this can lose. The most common case of this is the address
918 of a function being called.
920 Therefore, if this register is marked as being used
921 exactly once if we are in a loop with calls
922 (a "large loop"), see if we can replace the usage of
923 this register with the source of this SET. If we can,
926 Don't do this if P has a REG_RETVAL note or if we have
927 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
929 if (loop_info
->has_call
930 && regs
->array
[regno
].single_usage
!= 0
931 && regs
->array
[regno
].single_usage
!= const0_rtx
932 && REGNO_FIRST_UID (regno
) == INSN_UID (p
)
933 && (REGNO_LAST_UID (regno
)
934 == INSN_UID (regs
->array
[regno
].single_usage
))
935 && regs
->array
[regno
].set_in_loop
== 1
936 && GET_CODE (SET_SRC (set
)) != ASM_OPERANDS
937 && ! side_effects_p (SET_SRC (set
))
938 && ! find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
939 && (! SMALL_REGISTER_CLASSES
940 || (! (GET_CODE (SET_SRC (set
)) == REG
941 && (REGNO (SET_SRC (set
))
942 < FIRST_PSEUDO_REGISTER
))))
943 && regno
>= FIRST_PSEUDO_REGISTER
944 /* This test is not redundant; SET_SRC (set) might be
945 a call-clobbered register and the life of REGNO
946 might span a call. */
947 && ! modified_between_p (SET_SRC (set
), p
,
948 regs
->array
[regno
].single_usage
)
949 && no_labels_between_p (p
,
950 regs
->array
[regno
].single_usage
)
951 && validate_replace_rtx (SET_DEST (set
), SET_SRC (set
),
952 regs
->array
[regno
].single_usage
))
954 /* Replace any usage in a REG_EQUAL note. Must copy
955 the new source, so that we don't get rtx sharing
956 between the SET_SOURCE and REG_NOTES of insn p. */
957 REG_NOTES (regs
->array
[regno
].single_usage
)
959 (REG_NOTES (regs
->array
[regno
].single_usage
),
960 SET_DEST (set
), copy_rtx (SET_SRC (set
))));
963 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
));
965 regs
->array
[regno
+i
].set_in_loop
= 0;
969 m
= xmalloc (sizeof (struct movable
));
973 m
->dependencies
= dependencies
;
974 m
->set_dest
= SET_DEST (set
);
977 = regs
->array
[REGNO (SET_DEST (set
))].set_in_loop
- 1;
981 m
->move_insn
= move_insn
;
982 m
->move_insn_first
= 0;
983 m
->insert_temp
= insert_temp
;
984 m
->is_equiv
= (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
985 m
->savemode
= VOIDmode
;
987 /* Set M->cond if either loop_invariant_p
988 or consec_sets_invariant_p returned 2
989 (only conditionally invariant). */
990 m
->cond
= ((tem
| tem1
| tem2
) > 1);
991 m
->global
= LOOP_REG_GLOBAL_P (loop
, regno
);
993 m
->lifetime
= LOOP_REG_LIFETIME (loop
, regno
);
994 m
->savings
= regs
->array
[regno
].n_times_set
;
995 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
996 m
->savings
+= libcall_benefit (p
);
997 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
)); i
++)
998 regs
->array
[regno
+i
].set_in_loop
= move_insn
? -2 : -1;
999 /* Add M to the end of the chain MOVABLES. */
1000 loop_movables_add (movables
, m
);
1004 /* It is possible for the first instruction to have a
1005 REG_EQUAL note but a non-invariant SET_SRC, so we must
1006 remember the status of the first instruction in case
1007 the last instruction doesn't have a REG_EQUAL note. */
1008 m
->move_insn_first
= m
->move_insn
;
1010 /* Skip this insn, not checking REG_LIBCALL notes. */
1011 p
= next_nonnote_insn (p
);
1012 /* Skip the consecutive insns, if there are any. */
1013 p
= skip_consec_insns (p
, m
->consec
);
1014 /* Back up to the last insn of the consecutive group. */
1015 p
= prev_nonnote_insn (p
);
1017 /* We must now reset m->move_insn, m->is_equiv, and
1018 possibly m->set_src to correspond to the effects of
1020 temp
= find_reg_note (p
, REG_EQUIV
, NULL_RTX
);
1022 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
1025 temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
);
1026 if (temp
&& CONSTANT_P (XEXP (temp
, 0)))
1027 m
->set_src
= XEXP (temp
, 0), m
->move_insn
= 1;
1033 = (find_reg_note (p
, REG_EQUIV
, NULL_RTX
) != 0);
1036 /* If this register is always set within a STRICT_LOW_PART
1037 or set to zero, then its high bytes are constant.
1038 So clear them outside the loop and within the loop
1039 just load the low bytes.
1040 We must check that the machine has an instruction to do so.
1041 Also, if the value loaded into the register
1042 depends on the same register, this cannot be done. */
1043 else if (SET_SRC (set
) == const0_rtx
1044 && GET_CODE (NEXT_INSN (p
)) == INSN
1045 && (set1
= single_set (NEXT_INSN (p
)))
1046 && GET_CODE (set1
) == SET
1047 && (GET_CODE (SET_DEST (set1
)) == STRICT_LOW_PART
)
1048 && (GET_CODE (XEXP (SET_DEST (set1
), 0)) == SUBREG
)
1049 && (SUBREG_REG (XEXP (SET_DEST (set1
), 0))
1051 && !reg_mentioned_p (SET_DEST (set
), SET_SRC (set1
)))
1053 int regno
= REGNO (SET_DEST (set
));
1054 if (regs
->array
[regno
].set_in_loop
== 2)
1057 m
= xmalloc (sizeof (struct movable
));
1060 m
->set_dest
= SET_DEST (set
);
1061 m
->dependencies
= 0;
1067 m
->move_insn_first
= 0;
1068 m
->insert_temp
= insert_temp
;
1070 /* If the insn may not be executed on some cycles,
1071 we can't clear the whole reg; clear just high part.
1072 Not even if the reg is used only within this loop.
1079 Clearing x before the inner loop could clobber a value
1080 being saved from the last time around the outer loop.
1081 However, if the reg is not used outside this loop
1082 and all uses of the register are in the same
1083 basic block as the store, there is no problem.
1085 If this insn was made by loop, we don't know its
1086 INSN_LUID and hence must make a conservative
1088 m
->global
= (INSN_UID (p
) >= max_uid_for_loop
1089 || LOOP_REG_GLOBAL_P (loop
, regno
)
1090 || (labels_in_range_p
1091 (p
, REGNO_FIRST_LUID (regno
))));
1092 if (maybe_never
&& m
->global
)
1093 m
->savemode
= GET_MODE (SET_SRC (set1
));
1095 m
->savemode
= VOIDmode
;
1099 m
->lifetime
= LOOP_REG_LIFETIME (loop
, regno
);
1102 i
< LOOP_REGNO_NREGS (regno
, SET_DEST (set
));
1104 regs
->array
[regno
+i
].set_in_loop
= -1;
1105 /* Add M to the end of the chain MOVABLES. */
1106 loop_movables_add (movables
, m
);
1111 /* Past a call insn, we get to insns which might not be executed
1112 because the call might exit. This matters for insns that trap.
1113 Constant and pure call insns always return, so they don't count. */
1114 else if (GET_CODE (p
) == CALL_INSN
&& ! CONST_OR_PURE_CALL_P (p
))
1116 /* Past a label or a jump, we get to insns for which we
1117 can't count on whether or how many times they will be
1118 executed during each iteration. Therefore, we can
1119 only move out sets of trivial variables
1120 (those not used after the loop). */
1121 /* Similar code appears twice in strength_reduce. */
1122 else if ((GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
)
1123 /* If we enter the loop in the middle, and scan around to the
1124 beginning, don't set maybe_never for that. This must be an
1125 unconditional jump, otherwise the code at the top of the
1126 loop might never be executed. Unconditional jumps are
1127 followed by a barrier then the loop_end. */
1128 && ! (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == loop
->top
1129 && NEXT_INSN (NEXT_INSN (p
)) == loop_end
1130 && any_uncondjump_p (p
)))
1132 else if (GET_CODE (p
) == NOTE
)
1134 /* At the virtual top of a converted loop, insns are again known to
1135 be executed: logically, the loop begins here even though the exit
1136 code has been duplicated. */
1137 if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
&& loop_depth
== 0)
1138 maybe_never
= call_passed
= 0;
1139 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
1141 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
1146 /* If one movable subsumes another, ignore that other. */
1148 ignore_some_movables (movables
);
1150 /* For each movable insn, see if the reg that it loads
1151 leads when it dies right into another conditionally movable insn.
1152 If so, record that the second insn "forces" the first one,
1153 since the second can be moved only if the first is. */
1155 force_movables (movables
);
1157 /* See if there are multiple movable insns that load the same value.
1158 If there are, make all but the first point at the first one
1159 through the `match' field, and add the priorities of them
1160 all together as the priority of the first. */
1162 combine_movables (movables
, regs
);
1164 /* Now consider each movable insn to decide whether it is worth moving.
1165 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1167 For machines with few registers this increases code size, so do not
1168 move moveables when optimizing for code size on such machines.
1169 (The 18 below is the value for i386.) */
1172 || (reg_class_size
[GENERAL_REGS
] > 18 && !loop_info
->has_call
))
1174 move_movables (loop
, movables
, threshold
, insn_count
);
1176 /* Recalculate regs->array if move_movables has created new
1178 if (max_reg_num () > regs
->num
)
1180 loop_regs_scan (loop
, 0);
1181 for (update_start
= loop_start
;
1182 PREV_INSN (update_start
)
1183 && GET_CODE (PREV_INSN (update_start
)) != CODE_LABEL
;
1184 update_start
= PREV_INSN (update_start
))
1186 update_end
= NEXT_INSN (loop_end
);
1188 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1189 loop_max_reg
= max_reg_num ();
1193 /* Now candidates that still are negative are those not moved.
1194 Change regs->array[I].set_in_loop to indicate that those are not actually
1196 for (i
= 0; i
< regs
->num
; i
++)
1197 if (regs
->array
[i
].set_in_loop
< 0)
1198 regs
->array
[i
].set_in_loop
= regs
->array
[i
].n_times_set
;
1200 /* Now that we've moved some things out of the loop, we might be able to
1201 hoist even more memory references. */
1204 /* Recalculate regs->array if load_mems has created new registers. */
1205 if (max_reg_num () > regs
->num
)
1206 loop_regs_scan (loop
, 0);
1208 for (update_start
= loop_start
;
1209 PREV_INSN (update_start
)
1210 && GET_CODE (PREV_INSN (update_start
)) != CODE_LABEL
;
1211 update_start
= PREV_INSN (update_start
))
1213 update_end
= NEXT_INSN (loop_end
);
1215 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1216 loop_max_reg
= max_reg_num ();
1218 if (flag_strength_reduce
)
1220 if (update_end
&& GET_CODE (update_end
) == CODE_LABEL
)
1221 /* Ensure our label doesn't go away. */
1222 LABEL_NUSES (update_end
)++;
1224 strength_reduce (loop
, flags
);
1226 reg_scan_update (update_start
, update_end
, loop_max_reg
);
1227 loop_max_reg
= max_reg_num ();
1229 if (update_end
&& GET_CODE (update_end
) == CODE_LABEL
1230 && --LABEL_NUSES (update_end
) == 0)
1231 delete_related_insns (update_end
);
1235 /* The movable information is required for strength reduction. */
1236 loop_movables_free (movables
);
1243 /* Add elements to *OUTPUT to record all the pseudo-regs
1244 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1247 record_excess_regs (rtx in_this
, rtx not_in_this
, rtx
*output
)
1253 code
= GET_CODE (in_this
);
1267 if (REGNO (in_this
) >= FIRST_PSEUDO_REGISTER
1268 && ! reg_mentioned_p (in_this
, not_in_this
))
1269 *output
= gen_rtx_EXPR_LIST (VOIDmode
, in_this
, *output
);
1276 fmt
= GET_RTX_FORMAT (code
);
1277 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1284 for (j
= 0; j
< XVECLEN (in_this
, i
); j
++)
1285 record_excess_regs (XVECEXP (in_this
, i
, j
), not_in_this
, output
);
1289 record_excess_regs (XEXP (in_this
, i
), not_in_this
, output
);
1295 /* Check what regs are referred to in the libcall block ending with INSN,
1296 aside from those mentioned in the equivalent value.
1297 If there are none, return 0.
1298 If there are one or more, return an EXPR_LIST containing all of them. */
1301 libcall_other_reg (rtx insn
, rtx equiv
)
1303 rtx note
= find_reg_note (insn
, REG_RETVAL
, NULL_RTX
);
1304 rtx p
= XEXP (note
, 0);
1307 /* First, find all the regs used in the libcall block
1308 that are not mentioned as inputs to the result. */
1312 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
1313 || GET_CODE (p
) == CALL_INSN
)
1314 record_excess_regs (PATTERN (p
), equiv
, &output
);
1321 /* Return 1 if all uses of REG
1322 are between INSN and the end of the basic block. */
1325 reg_in_basic_block_p (rtx insn
, rtx reg
)
1327 int regno
= REGNO (reg
);
1330 if (REGNO_FIRST_UID (regno
) != INSN_UID (insn
))
1333 /* Search this basic block for the already recorded last use of the reg. */
1334 for (p
= insn
; p
; p
= NEXT_INSN (p
))
1336 switch (GET_CODE (p
))
1343 /* Ordinary insn: if this is the last use, we win. */
1344 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1349 /* Jump insn: if this is the last use, we win. */
1350 if (REGNO_LAST_UID (regno
) == INSN_UID (p
))
1352 /* Otherwise, it's the end of the basic block, so we lose. */
1357 /* It's the end of the basic block, so we lose. */
1365 /* The "last use" that was recorded can't be found after the first
1366 use. This can happen when the last use was deleted while
1367 processing an inner loop, this inner loop was then completely
1368 unrolled, and the outer loop is always exited after the inner loop,
1369 so that everything after the first use becomes a single basic block. */
1373 /* Compute the benefit of eliminating the insns in the block whose
1374 last insn is LAST. This may be a group of insns used to compute a
1375 value directly or can contain a library call. */
1378 libcall_benefit (rtx last
)
1383 for (insn
= XEXP (find_reg_note (last
, REG_RETVAL
, NULL_RTX
), 0);
1384 insn
!= last
; insn
= NEXT_INSN (insn
))
1386 if (GET_CODE (insn
) == CALL_INSN
)
1387 benefit
+= 10; /* Assume at least this many insns in a library
1389 else if (GET_CODE (insn
) == INSN
1390 && GET_CODE (PATTERN (insn
)) != USE
1391 && GET_CODE (PATTERN (insn
)) != CLOBBER
)
1398 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1401 skip_consec_insns (rtx insn
, int count
)
1403 for (; count
> 0; count
--)
1407 /* If first insn of libcall sequence, skip to end. */
1408 /* Do this at start of loop, since INSN is guaranteed to
1410 if (GET_CODE (insn
) != NOTE
1411 && (temp
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
)))
1412 insn
= XEXP (temp
, 0);
1415 insn
= NEXT_INSN (insn
);
1416 while (GET_CODE (insn
) == NOTE
);
1422 /* Ignore any movable whose insn falls within a libcall
1423 which is part of another movable.
1424 We make use of the fact that the movable for the libcall value
1425 was made later and so appears later on the chain. */
1428 ignore_some_movables (struct loop_movables
*movables
)
1430 struct movable
*m
, *m1
;
1432 for (m
= movables
->head
; m
; m
= m
->next
)
1434 /* Is this a movable for the value of a libcall? */
1435 rtx note
= find_reg_note (m
->insn
, REG_RETVAL
, NULL_RTX
);
1439 /* Check for earlier movables inside that range,
1440 and mark them invalid. We cannot use LUIDs here because
1441 insns created by loop.c for prior loops don't have LUIDs.
1442 Rather than reject all such insns from movables, we just
1443 explicitly check each insn in the libcall (since invariant
1444 libcalls aren't that common). */
1445 for (insn
= XEXP (note
, 0); insn
!= m
->insn
; insn
= NEXT_INSN (insn
))
1446 for (m1
= movables
->head
; m1
!= m
; m1
= m1
->next
)
1447 if (m1
->insn
== insn
)
1453 /* For each movable insn, see if the reg that it loads
1454 leads when it dies right into another conditionally movable insn.
1455 If so, record that the second insn "forces" the first one,
1456 since the second can be moved only if the first is. */
1459 force_movables (struct loop_movables
*movables
)
1461 struct movable
*m
, *m1
;
1463 for (m1
= movables
->head
; m1
; m1
= m1
->next
)
1464 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1465 if (!m1
->partial
&& !m1
->done
)
1467 int regno
= m1
->regno
;
1468 for (m
= m1
->next
; m
; m
= m
->next
)
1469 /* ??? Could this be a bug? What if CSE caused the
1470 register of M1 to be used after this insn?
1471 Since CSE does not update regno_last_uid,
1472 this insn M->insn might not be where it dies.
1473 But very likely this doesn't matter; what matters is
1474 that M's reg is computed from M1's reg. */
1475 if (INSN_UID (m
->insn
) == REGNO_LAST_UID (regno
)
1478 if (m
!= 0 && m
->set_src
== m1
->set_dest
1479 /* If m->consec, m->set_src isn't valid. */
1483 /* Increase the priority of the moving the first insn
1484 since it permits the second to be moved as well.
1485 Likewise for insns already forced by the first insn. */
1491 for (m2
= m1
; m2
; m2
= m2
->forces
)
1493 m2
->lifetime
+= m
->lifetime
;
1494 m2
->savings
+= m
->savings
;
1500 /* Find invariant expressions that are equal and can be combined into
1504 combine_movables (struct loop_movables
*movables
, struct loop_regs
*regs
)
1507 char *matched_regs
= xmalloc (regs
->num
);
1508 enum machine_mode mode
;
1510 /* Regs that are set more than once are not allowed to match
1511 or be matched. I'm no longer sure why not. */
1512 /* Only pseudo registers are allowed to match or be matched,
1513 since move_movables does not validate the change. */
1514 /* Perhaps testing m->consec_sets would be more appropriate here? */
1516 for (m
= movables
->head
; m
; m
= m
->next
)
1517 if (m
->match
== 0 && regs
->array
[m
->regno
].n_times_set
== 1
1518 && m
->regno
>= FIRST_PSEUDO_REGISTER
1523 int regno
= m
->regno
;
1525 memset (matched_regs
, 0, regs
->num
);
1526 matched_regs
[regno
] = 1;
1528 /* We want later insns to match the first one. Don't make the first
1529 one match any later ones. So start this loop at m->next. */
1530 for (m1
= m
->next
; m1
; m1
= m1
->next
)
1531 if (m
!= m1
&& m1
->match
== 0
1533 && regs
->array
[m1
->regno
].n_times_set
== 1
1534 && m1
->regno
>= FIRST_PSEUDO_REGISTER
1535 /* A reg used outside the loop mustn't be eliminated. */
1537 /* A reg used for zero-extending mustn't be eliminated. */
1539 && (matched_regs
[m1
->regno
]
1542 /* Can combine regs with different modes loaded from the
1543 same constant only if the modes are the same or
1544 if both are integer modes with M wider or the same
1545 width as M1. The check for integer is redundant, but
1546 safe, since the only case of differing destination
1547 modes with equal sources is when both sources are
1548 VOIDmode, i.e., CONST_INT. */
1549 (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
)
1550 || (GET_MODE_CLASS (GET_MODE (m
->set_dest
)) == MODE_INT
1551 && GET_MODE_CLASS (GET_MODE (m1
->set_dest
)) == MODE_INT
1552 && (GET_MODE_BITSIZE (GET_MODE (m
->set_dest
))
1553 >= GET_MODE_BITSIZE (GET_MODE (m1
->set_dest
)))))
1554 /* See if the source of M1 says it matches M. */
1555 && ((GET_CODE (m1
->set_src
) == REG
1556 && matched_regs
[REGNO (m1
->set_src
)])
1557 || rtx_equal_for_loop_p (m
->set_src
, m1
->set_src
,
1559 && ((m
->dependencies
== m1
->dependencies
)
1560 || rtx_equal_p (m
->dependencies
, m1
->dependencies
)))
1562 m
->lifetime
+= m1
->lifetime
;
1563 m
->savings
+= m1
->savings
;
1566 matched_regs
[m1
->regno
] = 1;
1570 /* Now combine the regs used for zero-extension.
1571 This can be done for those not marked `global'
1572 provided their lives don't overlap. */
1574 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1575 mode
= GET_MODE_WIDER_MODE (mode
))
1577 struct movable
*m0
= 0;
1579 /* Combine all the registers for extension from mode MODE.
1580 Don't combine any that are used outside this loop. */
1581 for (m
= movables
->head
; m
; m
= m
->next
)
1582 if (m
->partial
&& ! m
->global
1583 && mode
== GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m
->insn
)))))
1587 int first
= REGNO_FIRST_LUID (m
->regno
);
1588 int last
= REGNO_LAST_LUID (m
->regno
);
1592 /* First one: don't check for overlap, just record it. */
1597 /* Make sure they extend to the same mode.
1598 (Almost always true.) */
1599 if (GET_MODE (m
->set_dest
) != GET_MODE (m0
->set_dest
))
1602 /* We already have one: check for overlap with those
1603 already combined together. */
1604 for (m1
= movables
->head
; m1
!= m
; m1
= m1
->next
)
1605 if (m1
== m0
|| (m1
->partial
&& m1
->match
== m0
))
1606 if (! (REGNO_FIRST_LUID (m1
->regno
) > last
1607 || REGNO_LAST_LUID (m1
->regno
) < first
))
1610 /* No overlap: we can combine this with the others. */
1611 m0
->lifetime
+= m
->lifetime
;
1612 m0
->savings
+= m
->savings
;
1622 free (matched_regs
);
1625 /* Returns the number of movable instructions in LOOP that were not
1626 moved outside the loop. */
1629 num_unmoved_movables (const struct loop
*loop
)
1634 for (m
= LOOP_MOVABLES (loop
)->head
; m
; m
= m
->next
)
1642 /* Return 1 if regs X and Y will become the same if moved. */
1645 regs_match_p (rtx x
, rtx y
, struct loop_movables
*movables
)
1647 unsigned int xn
= REGNO (x
);
1648 unsigned int yn
= REGNO (y
);
1649 struct movable
*mx
, *my
;
1651 for (mx
= movables
->head
; mx
; mx
= mx
->next
)
1652 if (mx
->regno
== xn
)
1655 for (my
= movables
->head
; my
; my
= my
->next
)
1656 if (my
->regno
== yn
)
1660 && ((mx
->match
== my
->match
&& mx
->match
!= 0)
1662 || mx
== my
->match
));
1665 /* Return 1 if X and Y are identical-looking rtx's.
1666 This is the Lisp function EQUAL for rtx arguments.
1668 If two registers are matching movables or a movable register and an
1669 equivalent constant, consider them equal. */
1672 rtx_equal_for_loop_p (rtx x
, rtx y
, struct loop_movables
*movables
,
1673 struct loop_regs
*regs
)
1683 if (x
== 0 || y
== 0)
1686 code
= GET_CODE (x
);
1688 /* If we have a register and a constant, they may sometimes be
1690 if (GET_CODE (x
) == REG
&& regs
->array
[REGNO (x
)].set_in_loop
== -2
1693 for (m
= movables
->head
; m
; m
= m
->next
)
1694 if (m
->move_insn
&& m
->regno
== REGNO (x
)
1695 && rtx_equal_p (m
->set_src
, y
))
1698 else if (GET_CODE (y
) == REG
&& regs
->array
[REGNO (y
)].set_in_loop
== -2
1701 for (m
= movables
->head
; m
; m
= m
->next
)
1702 if (m
->move_insn
&& m
->regno
== REGNO (y
)
1703 && rtx_equal_p (m
->set_src
, x
))
1707 /* Otherwise, rtx's of different codes cannot be equal. */
1708 if (code
!= GET_CODE (y
))
1711 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1712 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1714 if (GET_MODE (x
) != GET_MODE (y
))
1717 /* These three types of rtx's can be compared nonrecursively. */
1719 return (REGNO (x
) == REGNO (y
) || regs_match_p (x
, y
, movables
));
1721 if (code
== LABEL_REF
)
1722 return XEXP (x
, 0) == XEXP (y
, 0);
1723 if (code
== SYMBOL_REF
)
1724 return XSTR (x
, 0) == XSTR (y
, 0);
1726 /* Compare the elements. If any pair of corresponding elements
1727 fail to match, return 0 for the whole things. */
1729 fmt
= GET_RTX_FORMAT (code
);
1730 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1735 if (XWINT (x
, i
) != XWINT (y
, i
))
1740 if (XINT (x
, i
) != XINT (y
, i
))
1745 /* Two vectors must have the same length. */
1746 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1749 /* And the corresponding elements must match. */
1750 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1751 if (rtx_equal_for_loop_p (XVECEXP (x
, i
, j
), XVECEXP (y
, i
, j
),
1752 movables
, regs
) == 0)
1757 if (rtx_equal_for_loop_p (XEXP (x
, i
), XEXP (y
, i
), movables
, regs
)
1763 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1768 /* These are just backpointers, so they don't matter. */
1774 /* It is believed that rtx's at this level will never
1775 contain anything but integers and other rtx's,
1776 except for within LABEL_REFs and SYMBOL_REFs. */
1784 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1785 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1786 references is incremented once for each added note. */
1789 add_label_notes (rtx x
, rtx insns
)
1791 enum rtx_code code
= GET_CODE (x
);
1796 if (code
== LABEL_REF
&& !LABEL_REF_NONLOCAL_P (x
))
1798 /* This code used to ignore labels that referred to dispatch tables to
1799 avoid flow generating (slightly) worse code.
1801 We no longer ignore such label references (see LABEL_REF handling in
1802 mark_jump_label for additional information). */
1803 for (insn
= insns
; insn
; insn
= NEXT_INSN (insn
))
1804 if (reg_mentioned_p (XEXP (x
, 0), insn
))
1806 REG_NOTES (insn
) = gen_rtx_INSN_LIST (REG_LABEL
, XEXP (x
, 0),
1808 if (LABEL_P (XEXP (x
, 0)))
1809 LABEL_NUSES (XEXP (x
, 0))++;
1813 fmt
= GET_RTX_FORMAT (code
);
1814 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1817 add_label_notes (XEXP (x
, i
), insns
);
1818 else if (fmt
[i
] == 'E')
1819 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1820 add_label_notes (XVECEXP (x
, i
, j
), insns
);
1824 /* Scan MOVABLES, and move the insns that deserve to be moved.
1825 If two matching movables are combined, replace one reg with the
1826 other throughout. */
1829 move_movables (struct loop
*loop
, struct loop_movables
*movables
,
1830 int threshold
, int insn_count
)
1832 struct loop_regs
*regs
= LOOP_REGS (loop
);
1833 int nregs
= regs
->num
;
1837 rtx loop_start
= loop
->start
;
1838 rtx loop_end
= loop
->end
;
1839 /* Map of pseudo-register replacements to handle combining
1840 when we move several insns that load the same value
1841 into different pseudo-registers. */
1842 rtx
*reg_map
= xcalloc (nregs
, sizeof (rtx
));
1843 char *already_moved
= xcalloc (nregs
, sizeof (char));
1845 for (m
= movables
->head
; m
; m
= m
->next
)
1847 /* Describe this movable insn. */
1849 if (loop_dump_stream
)
1851 fprintf (loop_dump_stream
, "Insn %d: regno %d (life %d), ",
1852 INSN_UID (m
->insn
), m
->regno
, m
->lifetime
);
1854 fprintf (loop_dump_stream
, "consec %d, ", m
->consec
);
1856 fprintf (loop_dump_stream
, "cond ");
1858 fprintf (loop_dump_stream
, "force ");
1860 fprintf (loop_dump_stream
, "global ");
1862 fprintf (loop_dump_stream
, "done ");
1864 fprintf (loop_dump_stream
, "move-insn ");
1866 fprintf (loop_dump_stream
, "matches %d ",
1867 INSN_UID (m
->match
->insn
));
1869 fprintf (loop_dump_stream
, "forces %d ",
1870 INSN_UID (m
->forces
->insn
));
1873 /* Ignore the insn if it's already done (it matched something else).
1874 Otherwise, see if it is now safe to move. */
1878 || (1 == loop_invariant_p (loop
, m
->set_src
)
1879 && (m
->dependencies
== 0
1880 || 1 == loop_invariant_p (loop
, m
->dependencies
))
1882 || 1 == consec_sets_invariant_p (loop
, m
->set_dest
,
1885 && (! m
->forces
|| m
->forces
->done
))
1889 int savings
= m
->savings
;
1891 /* We have an insn that is safe to move.
1892 Compute its desirability. */
1897 if (loop_dump_stream
)
1898 fprintf (loop_dump_stream
, "savings %d ", savings
);
1900 if (regs
->array
[regno
].moved_once
&& loop_dump_stream
)
1901 fprintf (loop_dump_stream
, "halved since already moved ");
1903 /* An insn MUST be moved if we already moved something else
1904 which is safe only if this one is moved too: that is,
1905 if already_moved[REGNO] is nonzero. */
1907 /* An insn is desirable to move if the new lifetime of the
1908 register is no more than THRESHOLD times the old lifetime.
1909 If it's not desirable, it means the loop is so big
1910 that moving won't speed things up much,
1911 and it is liable to make register usage worse. */
1913 /* It is also desirable to move if it can be moved at no
1914 extra cost because something else was already moved. */
1916 if (already_moved
[regno
]
1917 || flag_move_all_movables
1918 || (threshold
* savings
* m
->lifetime
) >=
1919 (regs
->array
[regno
].moved_once
? insn_count
* 2 : insn_count
)
1920 || (m
->forces
&& m
->forces
->done
1921 && regs
->array
[m
->forces
->regno
].n_times_set
== 1))
1925 rtx first
= NULL_RTX
;
1926 rtx newreg
= NULL_RTX
;
1929 newreg
= gen_reg_rtx (GET_MODE (m
->set_dest
));
1931 /* Now move the insns that set the reg. */
1933 if (m
->partial
&& m
->match
)
1937 /* Find the end of this chain of matching regs.
1938 Thus, we load each reg in the chain from that one reg.
1939 And that reg is loaded with 0 directly,
1940 since it has ->match == 0. */
1941 for (m1
= m
; m1
->match
; m1
= m1
->match
);
1942 newpat
= gen_move_insn (SET_DEST (PATTERN (m
->insn
)),
1943 SET_DEST (PATTERN (m1
->insn
)));
1944 i1
= loop_insn_hoist (loop
, newpat
);
1946 /* Mark the moved, invariant reg as being allowed to
1947 share a hard reg with the other matching invariant. */
1948 REG_NOTES (i1
) = REG_NOTES (m
->insn
);
1949 r1
= SET_DEST (PATTERN (m
->insn
));
1950 r2
= SET_DEST (PATTERN (m1
->insn
));
1952 = gen_rtx_EXPR_LIST (VOIDmode
, r1
,
1953 gen_rtx_EXPR_LIST (VOIDmode
, r2
,
1955 delete_insn (m
->insn
);
1960 if (loop_dump_stream
)
1961 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
1963 /* If we are to re-generate the item being moved with a
1964 new move insn, first delete what we have and then emit
1965 the move insn before the loop. */
1966 else if (m
->move_insn
)
1970 for (count
= m
->consec
; count
>= 0; count
--)
1972 /* If this is the first insn of a library call sequence,
1973 something is very wrong. */
1974 if (GET_CODE (p
) != NOTE
1975 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
1978 /* If this is the last insn of a libcall sequence, then
1979 delete every insn in the sequence except the last.
1980 The last insn is handled in the normal manner. */
1981 if (GET_CODE (p
) != NOTE
1982 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
1984 temp
= XEXP (temp
, 0);
1986 temp
= delete_insn (temp
);
1990 p
= delete_insn (p
);
1992 /* simplify_giv_expr expects that it can walk the insns
1993 at m->insn forwards and see this old sequence we are
1994 tossing here. delete_insn does preserve the next
1995 pointers, but when we skip over a NOTE we must fix
1996 it up. Otherwise that code walks into the non-deleted
1998 while (p
&& GET_CODE (p
) == NOTE
)
1999 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
2003 /* Replace the original insn with a move from
2004 our newly created temp. */
2006 emit_move_insn (m
->set_dest
, newreg
);
2009 emit_insn_before (seq
, p
);
2014 emit_move_insn (m
->insert_temp
? newreg
: m
->set_dest
,
2019 add_label_notes (m
->set_src
, seq
);
2021 i1
= loop_insn_hoist (loop
, seq
);
2022 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2023 set_unique_reg_note (i1
,
2024 m
->is_equiv
? REG_EQUIV
: REG_EQUAL
,
2027 if (loop_dump_stream
)
2028 fprintf (loop_dump_stream
, " moved to %d", INSN_UID (i1
));
2030 /* The more regs we move, the less we like moving them. */
2035 for (count
= m
->consec
; count
>= 0; count
--)
2039 /* If first insn of libcall sequence, skip to end. */
2040 /* Do this at start of loop, since p is guaranteed to
2042 if (GET_CODE (p
) != NOTE
2043 && (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
2046 /* If last insn of libcall sequence, move all
2047 insns except the last before the loop. The last
2048 insn is handled in the normal manner. */
2049 if (GET_CODE (p
) != NOTE
2050 && (temp
= find_reg_note (p
, REG_RETVAL
, NULL_RTX
)))
2054 rtx fn_address_insn
= 0;
2057 for (temp
= XEXP (temp
, 0); temp
!= p
;
2058 temp
= NEXT_INSN (temp
))
2064 if (GET_CODE (temp
) == NOTE
)
2067 body
= PATTERN (temp
);
2069 /* Find the next insn after TEMP,
2070 not counting USE or NOTE insns. */
2071 for (next
= NEXT_INSN (temp
); next
!= p
;
2072 next
= NEXT_INSN (next
))
2073 if (! (GET_CODE (next
) == INSN
2074 && GET_CODE (PATTERN (next
)) == USE
)
2075 && GET_CODE (next
) != NOTE
)
2078 /* If that is the call, this may be the insn
2079 that loads the function address.
2081 Extract the function address from the insn
2082 that loads it into a register.
2083 If this insn was cse'd, we get incorrect code.
2085 So emit a new move insn that copies the
2086 function address into the register that the
2087 call insn will use. flow.c will delete any
2088 redundant stores that we have created. */
2089 if (GET_CODE (next
) == CALL_INSN
2090 && GET_CODE (body
) == SET
2091 && GET_CODE (SET_DEST (body
)) == REG
2092 && (n
= find_reg_note (temp
, REG_EQUAL
,
2095 fn_reg
= SET_SRC (body
);
2096 if (GET_CODE (fn_reg
) != REG
)
2097 fn_reg
= SET_DEST (body
);
2098 fn_address
= XEXP (n
, 0);
2099 fn_address_insn
= temp
;
2101 /* We have the call insn.
2102 If it uses the register we suspect it might,
2103 load it with the correct address directly. */
2104 if (GET_CODE (temp
) == CALL_INSN
2106 && reg_referenced_p (fn_reg
, body
))
2107 loop_insn_emit_after (loop
, 0, fn_address_insn
,
2109 (fn_reg
, fn_address
));
2111 if (GET_CODE (temp
) == CALL_INSN
)
2113 i1
= loop_call_insn_hoist (loop
, body
);
2114 /* Because the USAGE information potentially
2115 contains objects other than hard registers
2116 we need to copy it. */
2117 if (CALL_INSN_FUNCTION_USAGE (temp
))
2118 CALL_INSN_FUNCTION_USAGE (i1
)
2119 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp
));
2122 i1
= loop_insn_hoist (loop
, body
);
2125 if (temp
== fn_address_insn
)
2126 fn_address_insn
= i1
;
2127 REG_NOTES (i1
) = REG_NOTES (temp
);
2128 REG_NOTES (temp
) = NULL
;
2134 if (m
->savemode
!= VOIDmode
)
2136 /* P sets REG to zero; but we should clear only
2137 the bits that are not covered by the mode
2139 rtx reg
= m
->set_dest
;
2144 tem
= expand_simple_binop
2145 (GET_MODE (reg
), AND
, reg
,
2146 GEN_INT ((((HOST_WIDE_INT
) 1
2147 << GET_MODE_BITSIZE (m
->savemode
)))
2149 reg
, 1, OPTAB_LIB_WIDEN
);
2153 emit_move_insn (reg
, tem
);
2154 sequence
= get_insns ();
2156 i1
= loop_insn_hoist (loop
, sequence
);
2158 else if (GET_CODE (p
) == CALL_INSN
)
2160 i1
= loop_call_insn_hoist (loop
, PATTERN (p
));
2161 /* Because the USAGE information potentially
2162 contains objects other than hard registers
2163 we need to copy it. */
2164 if (CALL_INSN_FUNCTION_USAGE (p
))
2165 CALL_INSN_FUNCTION_USAGE (i1
)
2166 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p
));
2168 else if (count
== m
->consec
&& m
->move_insn_first
)
2171 /* The SET_SRC might not be invariant, so we must
2172 use the REG_EQUAL note. */
2174 emit_move_insn (m
->insert_temp
? newreg
: m
->set_dest
,
2179 add_label_notes (m
->set_src
, seq
);
2181 i1
= loop_insn_hoist (loop
, seq
);
2182 if (! find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2183 set_unique_reg_note (i1
, m
->is_equiv
? REG_EQUIV
2184 : REG_EQUAL
, m
->set_src
);
2186 else if (m
->insert_temp
)
2188 rtx
*reg_map2
= xcalloc (REGNO (newreg
),
2190 reg_map2
[m
->regno
] = newreg
;
2192 i1
= loop_insn_hoist (loop
, copy_rtx (PATTERN (p
)));
2193 replace_regs (i1
, reg_map2
, REGNO (newreg
), 1);
2197 i1
= loop_insn_hoist (loop
, PATTERN (p
));
2199 if (REG_NOTES (i1
) == 0)
2201 REG_NOTES (i1
) = REG_NOTES (p
);
2202 REG_NOTES (p
) = NULL
;
2204 /* If there is a REG_EQUAL note present whose value
2205 is not loop invariant, then delete it, since it
2206 may cause problems with later optimization passes.
2207 It is possible for cse to create such notes
2208 like this as a result of record_jump_cond. */
2210 if ((temp
= find_reg_note (i1
, REG_EQUAL
, NULL_RTX
))
2211 && ! loop_invariant_p (loop
, XEXP (temp
, 0)))
2212 remove_note (i1
, temp
);
2218 if (loop_dump_stream
)
2219 fprintf (loop_dump_stream
, " moved to %d",
2222 /* If library call, now fix the REG_NOTES that contain
2223 insn pointers, namely REG_LIBCALL on FIRST
2224 and REG_RETVAL on I1. */
2225 if ((temp
= find_reg_note (i1
, REG_RETVAL
, NULL_RTX
)))
2227 XEXP (temp
, 0) = first
;
2228 temp
= find_reg_note (first
, REG_LIBCALL
, NULL_RTX
);
2229 XEXP (temp
, 0) = i1
;
2236 /* simplify_giv_expr expects that it can walk the insns
2237 at m->insn forwards and see this old sequence we are
2238 tossing here. delete_insn does preserve the next
2239 pointers, but when we skip over a NOTE we must fix
2240 it up. Otherwise that code walks into the non-deleted
2242 while (p
&& GET_CODE (p
) == NOTE
)
2243 p
= NEXT_INSN (temp
) = NEXT_INSN (p
);
2248 /* Replace the original insn with a move from
2249 our newly created temp. */
2251 emit_move_insn (m
->set_dest
, newreg
);
2254 emit_insn_before (seq
, p
);
2258 /* The more regs we move, the less we like moving them. */
2264 if (!m
->insert_temp
)
2266 /* Any other movable that loads the same register
2268 already_moved
[regno
] = 1;
2270 /* This reg has been moved out of one loop. */
2271 regs
->array
[regno
].moved_once
= 1;
2273 /* The reg set here is now invariant. */
2277 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, m
->set_dest
); i
++)
2278 regs
->array
[regno
+i
].set_in_loop
= 0;
2281 /* Change the length-of-life info for the register
2282 to say it lives at least the full length of this loop.
2283 This will help guide optimizations in outer loops. */
2285 if (REGNO_FIRST_LUID (regno
) > INSN_LUID (loop_start
))
2286 /* This is the old insn before all the moved insns.
2287 We can't use the moved insn because it is out of range
2288 in uid_luid. Only the old insns have luids. */
2289 REGNO_FIRST_UID (regno
) = INSN_UID (loop_start
);
2290 if (REGNO_LAST_LUID (regno
) < INSN_LUID (loop_end
))
2291 REGNO_LAST_UID (regno
) = INSN_UID (loop_end
);
2294 /* Combine with this moved insn any other matching movables. */
2297 for (m1
= movables
->head
; m1
; m1
= m1
->next
)
2302 /* Schedule the reg loaded by M1
2303 for replacement so that shares the reg of M.
2304 If the modes differ (only possible in restricted
2305 circumstances, make a SUBREG.
2307 Note this assumes that the target dependent files
2308 treat REG and SUBREG equally, including within
2309 GO_IF_LEGITIMATE_ADDRESS and in all the
2310 predicates since we never verify that replacing the
2311 original register with a SUBREG results in a
2312 recognizable insn. */
2313 if (GET_MODE (m
->set_dest
) == GET_MODE (m1
->set_dest
))
2314 reg_map
[m1
->regno
] = m
->set_dest
;
2317 = gen_lowpart_common (GET_MODE (m1
->set_dest
),
2320 /* Get rid of the matching insn
2321 and prevent further processing of it. */
2324 /* If library call, delete all insns. */
2325 if ((temp
= find_reg_note (m1
->insn
, REG_RETVAL
,
2327 delete_insn_chain (XEXP (temp
, 0), m1
->insn
);
2329 delete_insn (m1
->insn
);
2331 /* Any other movable that loads the same register
2333 already_moved
[m1
->regno
] = 1;
2335 /* The reg merged here is now invariant,
2336 if the reg it matches is invariant. */
2341 i
< LOOP_REGNO_NREGS (regno
, m1
->set_dest
);
2343 regs
->array
[m1
->regno
+i
].set_in_loop
= 0;
2347 else if (loop_dump_stream
)
2348 fprintf (loop_dump_stream
, "not desirable");
2350 else if (loop_dump_stream
&& !m
->match
)
2351 fprintf (loop_dump_stream
, "not safe");
2353 if (loop_dump_stream
)
2354 fprintf (loop_dump_stream
, "\n");
2358 new_start
= loop_start
;
2360 /* Go through all the instructions in the loop, making
2361 all the register substitutions scheduled in REG_MAP. */
2362 for (p
= new_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
2363 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
2364 || GET_CODE (p
) == CALL_INSN
)
2366 replace_regs (PATTERN (p
), reg_map
, nregs
, 0);
2367 replace_regs (REG_NOTES (p
), reg_map
, nregs
, 0);
2373 free (already_moved
);
2378 loop_movables_add (struct loop_movables
*movables
, struct movable
*m
)
2380 if (movables
->head
== 0)
2383 movables
->last
->next
= m
;
2389 loop_movables_free (struct loop_movables
*movables
)
2392 struct movable
*m_next
;
2394 for (m
= movables
->head
; m
; m
= m_next
)
2402 /* Scan X and replace the address of any MEM in it with ADDR.
2403 REG is the address that MEM should have before the replacement. */
2406 replace_call_address (rtx x
, rtx reg
, rtx addr
)
2414 code
= GET_CODE (x
);
2428 /* Short cut for very common case. */
2429 replace_call_address (XEXP (x
, 1), reg
, addr
);
2433 /* Short cut for very common case. */
2434 replace_call_address (XEXP (x
, 0), reg
, addr
);
2438 /* If this MEM uses a reg other than the one we expected,
2439 something is wrong. */
2440 if (XEXP (x
, 0) != reg
)
2449 fmt
= GET_RTX_FORMAT (code
);
2450 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2453 replace_call_address (XEXP (x
, i
), reg
, addr
);
2454 else if (fmt
[i
] == 'E')
2457 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2458 replace_call_address (XVECEXP (x
, i
, j
), reg
, addr
);
2464 /* Return the number of memory refs to addresses that vary
2468 count_nonfixed_reads (const struct loop
*loop
, rtx x
)
2478 code
= GET_CODE (x
);
2492 return ((loop_invariant_p (loop
, XEXP (x
, 0)) != 1)
2493 + count_nonfixed_reads (loop
, XEXP (x
, 0)));
2500 fmt
= GET_RTX_FORMAT (code
);
2501 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
2504 value
+= count_nonfixed_reads (loop
, XEXP (x
, i
));
2508 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
2509 value
+= count_nonfixed_reads (loop
, XVECEXP (x
, i
, j
));
2515 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2516 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2517 `unknown_address_altered', `unknown_constant_address_altered', and
2518 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2519 list `store_mems' in LOOP. */
2522 prescan_loop (struct loop
*loop
)
2526 struct loop_info
*loop_info
= LOOP_INFO (loop
);
2527 rtx start
= loop
->start
;
2528 rtx end
= loop
->end
;
2529 /* The label after END. Jumping here is just like falling off the
2530 end of the loop. We use next_nonnote_insn instead of next_label
2531 as a hedge against the (pathological) case where some actual insn
2532 might end up between the two. */
2533 rtx exit_target
= next_nonnote_insn (end
);
2535 loop_info
->has_indirect_jump
= indirect_jump_in_function
;
2536 loop_info
->pre_header_has_call
= 0;
2537 loop_info
->has_call
= 0;
2538 loop_info
->has_nonconst_call
= 0;
2539 loop_info
->has_prefetch
= 0;
2540 loop_info
->has_volatile
= 0;
2541 loop_info
->has_tablejump
= 0;
2542 loop_info
->has_multiple_exit_targets
= 0;
2545 loop_info
->unknown_address_altered
= 0;
2546 loop_info
->unknown_constant_address_altered
= 0;
2547 loop_info
->store_mems
= NULL_RTX
;
2548 loop_info
->first_loop_store_insn
= NULL_RTX
;
2549 loop_info
->mems_idx
= 0;
2550 loop_info
->num_mem_sets
= 0;
2551 /* If loop opts run twice, this was set on 1st pass for 2nd. */
2552 loop_info
->preconditioned
= NOTE_PRECONDITIONED (end
);
2554 for (insn
= start
; insn
&& GET_CODE (insn
) != CODE_LABEL
;
2555 insn
= PREV_INSN (insn
))
2557 if (GET_CODE (insn
) == CALL_INSN
)
2559 loop_info
->pre_header_has_call
= 1;
2564 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2565 insn
= NEXT_INSN (insn
))
2567 switch (GET_CODE (insn
))
2570 if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_BEG
)
2573 /* Count number of loops contained in this one. */
2576 else if (NOTE_LINE_NUMBER (insn
) == NOTE_INSN_LOOP_END
)
2581 if (! CONST_OR_PURE_CALL_P (insn
))
2583 loop_info
->unknown_address_altered
= 1;
2584 loop_info
->has_nonconst_call
= 1;
2586 else if (pure_call_p (insn
))
2587 loop_info
->has_nonconst_call
= 1;
2588 loop_info
->has_call
= 1;
2589 if (can_throw_internal (insn
))
2590 loop_info
->has_multiple_exit_targets
= 1;
2592 /* Calls initializing constant objects have CLOBBER of MEM /u in the
2593 attached FUNCTION_USAGE expression list, not accounted for by the
2594 code above. We should note these to avoid missing dependencies in
2595 later references. */
2599 for (fusage_entry
= CALL_INSN_FUNCTION_USAGE (insn
);
2600 fusage_entry
; fusage_entry
= XEXP (fusage_entry
, 1))
2602 rtx fusage
= XEXP (fusage_entry
, 0);
2604 if (GET_CODE (fusage
) == CLOBBER
2605 && GET_CODE (XEXP (fusage
, 0)) == MEM
2606 && RTX_UNCHANGING_P (XEXP (fusage
, 0)))
2608 note_stores (fusage
, note_addr_stored
, loop_info
);
2609 if (! loop_info
->first_loop_store_insn
2610 && loop_info
->store_mems
)
2611 loop_info
->first_loop_store_insn
= insn
;
2618 if (! loop_info
->has_multiple_exit_targets
)
2620 rtx set
= pc_set (insn
);
2624 rtx src
= SET_SRC (set
);
2627 if (GET_CODE (src
) == IF_THEN_ELSE
)
2629 label1
= XEXP (src
, 1);
2630 label2
= XEXP (src
, 2);
2640 if (label1
&& label1
!= pc_rtx
)
2642 if (GET_CODE (label1
) != LABEL_REF
)
2644 /* Something tricky. */
2645 loop_info
->has_multiple_exit_targets
= 1;
2648 else if (XEXP (label1
, 0) != exit_target
2649 && LABEL_OUTSIDE_LOOP_P (label1
))
2651 /* A jump outside the current loop. */
2652 loop_info
->has_multiple_exit_targets
= 1;
2664 /* A return, or something tricky. */
2665 loop_info
->has_multiple_exit_targets
= 1;
2671 if (volatile_refs_p (PATTERN (insn
)))
2672 loop_info
->has_volatile
= 1;
2674 if (GET_CODE (insn
) == JUMP_INSN
2675 && (GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
2676 || GET_CODE (PATTERN (insn
)) == ADDR_VEC
))
2677 loop_info
->has_tablejump
= 1;
2679 note_stores (PATTERN (insn
), note_addr_stored
, loop_info
);
2680 if (! loop_info
->first_loop_store_insn
&& loop_info
->store_mems
)
2681 loop_info
->first_loop_store_insn
= insn
;
2683 if (flag_non_call_exceptions
&& can_throw_internal (insn
))
2684 loop_info
->has_multiple_exit_targets
= 1;
2692 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2693 if (/* An exception thrown by a called function might land us
2695 ! loop_info
->has_nonconst_call
2696 /* We don't want loads for MEMs moved to a location before the
2697 one at which their stack memory becomes allocated. (Note
2698 that this is not a problem for malloc, etc., since those
2699 require actual function calls. */
2700 && ! current_function_calls_alloca
2701 /* There are ways to leave the loop other than falling off the
2703 && ! loop_info
->has_multiple_exit_targets
)
2704 for (insn
= NEXT_INSN (start
); insn
!= NEXT_INSN (end
);
2705 insn
= NEXT_INSN (insn
))
2706 for_each_rtx (&insn
, insert_loop_mem
, loop_info
);
2708 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2709 that loop_invariant_p and load_mems can use true_dependence
2710 to determine what is really clobbered. */
2711 if (loop_info
->unknown_address_altered
)
2713 rtx mem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
2715 loop_info
->store_mems
2716 = gen_rtx_EXPR_LIST (VOIDmode
, mem
, loop_info
->store_mems
);
2718 if (loop_info
->unknown_constant_address_altered
)
2720 rtx mem
= gen_rtx_MEM (BLKmode
, const0_rtx
);
2722 RTX_UNCHANGING_P (mem
) = 1;
2723 loop_info
->store_mems
2724 = gen_rtx_EXPR_LIST (VOIDmode
, mem
, loop_info
->store_mems
);
2728 /* Invalidate all loops containing LABEL. */
2731 invalidate_loops_containing_label (rtx label
)
2734 for (loop
= uid_loop
[INSN_UID (label
)]; loop
; loop
= loop
->outer
)
2738 /* Scan the function looking for loops. Record the start and end of each loop.
2739 Also mark as invalid loops any loops that contain a setjmp or are branched
2740 to from outside the loop. */
2743 find_and_verify_loops (rtx f
, struct loops
*loops
)
2748 struct loop
*current_loop
;
2749 struct loop
*next_loop
;
2752 num_loops
= loops
->num
;
2754 compute_luids (f
, NULL_RTX
, 0);
2756 /* If there are jumps to undefined labels,
2757 treat them as jumps out of any/all loops.
2758 This also avoids writing past end of tables when there are no loops. */
2761 /* Find boundaries of loops, mark which loops are contained within
2762 loops, and invalidate loops that have setjmp. */
2765 current_loop
= NULL
;
2766 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2768 if (GET_CODE (insn
) == NOTE
)
2769 switch (NOTE_LINE_NUMBER (insn
))
2771 case NOTE_INSN_LOOP_BEG
:
2772 next_loop
= loops
->array
+ num_loops
;
2773 next_loop
->num
= num_loops
;
2775 next_loop
->start
= insn
;
2776 next_loop
->outer
= current_loop
;
2777 current_loop
= next_loop
;
2780 case NOTE_INSN_LOOP_CONT
:
2781 current_loop
->cont
= insn
;
2784 case NOTE_INSN_LOOP_VTOP
:
2785 current_loop
->vtop
= insn
;
2788 case NOTE_INSN_LOOP_END
:
2792 current_loop
->end
= insn
;
2793 current_loop
= current_loop
->outer
;
2800 if (GET_CODE (insn
) == CALL_INSN
2801 && find_reg_note (insn
, REG_SETJMP
, NULL
))
2803 /* In this case, we must invalidate our current loop and any
2805 for (loop
= current_loop
; loop
; loop
= loop
->outer
)
2808 if (loop_dump_stream
)
2809 fprintf (loop_dump_stream
,
2810 "\nLoop at %d ignored due to setjmp.\n",
2811 INSN_UID (loop
->start
));
2815 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2816 enclosing loop, but this doesn't matter. */
2817 uid_loop
[INSN_UID (insn
)] = current_loop
;
2820 /* Any loop containing a label used in an initializer must be invalidated,
2821 because it can be jumped into from anywhere. */
2822 for (label
= forced_labels
; label
; label
= XEXP (label
, 1))
2823 invalidate_loops_containing_label (XEXP (label
, 0));
2825 /* Any loop containing a label used for an exception handler must be
2826 invalidated, because it can be jumped into from anywhere. */
2827 for_each_eh_label (invalidate_loops_containing_label
);
2829 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2830 loop that it is not contained within, that loop is marked invalid.
2831 If any INSN or CALL_INSN uses a label's address, then the loop containing
2832 that label is marked invalid, because it could be jumped into from
2835 Also look for blocks of code ending in an unconditional branch that
2836 exits the loop. If such a block is surrounded by a conditional
2837 branch around the block, move the block elsewhere (see below) and
2838 invert the jump to point to the code block. This may eliminate a
2839 label in our loop and will simplify processing by both us and a
2840 possible second cse pass. */
2842 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
2845 struct loop
*this_loop
= uid_loop
[INSN_UID (insn
)];
2847 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
2849 rtx note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
2851 invalidate_loops_containing_label (XEXP (note
, 0));
2854 if (GET_CODE (insn
) != JUMP_INSN
)
2857 mark_loop_jump (PATTERN (insn
), this_loop
);
2859 /* See if this is an unconditional branch outside the loop. */
2861 && (GET_CODE (PATTERN (insn
)) == RETURN
2862 || (any_uncondjump_p (insn
)
2863 && onlyjump_p (insn
)
2864 && (uid_loop
[INSN_UID (JUMP_LABEL (insn
))]
2866 && get_max_uid () < max_uid_for_loop
)
2869 rtx our_next
= next_real_insn (insn
);
2870 rtx last_insn_to_move
= NEXT_INSN (insn
);
2871 struct loop
*dest_loop
;
2872 struct loop
*outer_loop
= NULL
;
2874 /* Go backwards until we reach the start of the loop, a label,
2876 for (p
= PREV_INSN (insn
);
2877 GET_CODE (p
) != CODE_LABEL
2878 && ! (GET_CODE (p
) == NOTE
2879 && NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
2880 && GET_CODE (p
) != JUMP_INSN
;
2884 /* Check for the case where we have a jump to an inner nested
2885 loop, and do not perform the optimization in that case. */
2887 if (JUMP_LABEL (insn
))
2889 dest_loop
= uid_loop
[INSN_UID (JUMP_LABEL (insn
))];
2892 for (outer_loop
= dest_loop
; outer_loop
;
2893 outer_loop
= outer_loop
->outer
)
2894 if (outer_loop
== this_loop
)
2899 /* Make sure that the target of P is within the current loop. */
2901 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
2902 && uid_loop
[INSN_UID (JUMP_LABEL (p
))] != this_loop
)
2903 outer_loop
= this_loop
;
2905 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2906 we have a block of code to try to move.
2908 We look backward and then forward from the target of INSN
2909 to find a BARRIER at the same loop depth as the target.
2910 If we find such a BARRIER, we make a new label for the start
2911 of the block, invert the jump in P and point it to that label,
2912 and move the block of code to the spot we found. */
2915 && GET_CODE (p
) == JUMP_INSN
2916 && JUMP_LABEL (p
) != 0
2917 /* Just ignore jumps to labels that were never emitted.
2918 These always indicate compilation errors. */
2919 && INSN_UID (JUMP_LABEL (p
)) != 0
2920 && any_condjump_p (p
) && onlyjump_p (p
)
2921 && next_real_insn (JUMP_LABEL (p
)) == our_next
2922 /* If it's not safe to move the sequence, then we
2924 && insns_safe_to_move_p (p
, NEXT_INSN (insn
),
2925 &last_insn_to_move
))
2928 = JUMP_LABEL (insn
) ? JUMP_LABEL (insn
) : get_last_insn ();
2929 struct loop
*target_loop
= uid_loop
[INSN_UID (target
)];
2933 /* Search for possible garbage past the conditional jumps
2934 and look for the last barrier. */
2935 for (tmp
= last_insn_to_move
;
2936 tmp
&& GET_CODE (tmp
) != CODE_LABEL
; tmp
= NEXT_INSN (tmp
))
2937 if (GET_CODE (tmp
) == BARRIER
)
2938 last_insn_to_move
= tmp
;
2940 for (loc
= target
; loc
; loc
= PREV_INSN (loc
))
2941 if (GET_CODE (loc
) == BARRIER
2942 /* Don't move things inside a tablejump. */
2943 && ((loc2
= next_nonnote_insn (loc
)) == 0
2944 || GET_CODE (loc2
) != CODE_LABEL
2945 || (loc2
= next_nonnote_insn (loc2
)) == 0
2946 || GET_CODE (loc2
) != JUMP_INSN
2947 || (GET_CODE (PATTERN (loc2
)) != ADDR_VEC
2948 && GET_CODE (PATTERN (loc2
)) != ADDR_DIFF_VEC
))
2949 && uid_loop
[INSN_UID (loc
)] == target_loop
)
2953 for (loc
= target
; loc
; loc
= NEXT_INSN (loc
))
2954 if (GET_CODE (loc
) == BARRIER
2955 /* Don't move things inside a tablejump. */
2956 && ((loc2
= next_nonnote_insn (loc
)) == 0
2957 || GET_CODE (loc2
) != CODE_LABEL
2958 || (loc2
= next_nonnote_insn (loc2
)) == 0
2959 || GET_CODE (loc2
) != JUMP_INSN
2960 || (GET_CODE (PATTERN (loc2
)) != ADDR_VEC
2961 && GET_CODE (PATTERN (loc2
)) != ADDR_DIFF_VEC
))
2962 && uid_loop
[INSN_UID (loc
)] == target_loop
)
2967 rtx cond_label
= JUMP_LABEL (p
);
2968 rtx new_label
= get_label_after (p
);
2970 /* Ensure our label doesn't go away. */
2971 LABEL_NUSES (cond_label
)++;
2973 /* Verify that uid_loop is large enough and that
2975 if (invert_jump (p
, new_label
, 1))
2979 /* If no suitable BARRIER was found, create a suitable
2980 one before TARGET. Since TARGET is a fall through
2981 path, we'll need to insert a jump around our block
2982 and add a BARRIER before TARGET.
2984 This creates an extra unconditional jump outside
2985 the loop. However, the benefits of removing rarely
2986 executed instructions from inside the loop usually
2987 outweighs the cost of the extra unconditional jump
2988 outside the loop. */
2993 temp
= gen_jump (JUMP_LABEL (insn
));
2994 temp
= emit_jump_insn_before (temp
, target
);
2995 JUMP_LABEL (temp
) = JUMP_LABEL (insn
);
2996 LABEL_NUSES (JUMP_LABEL (insn
))++;
2997 loc
= emit_barrier_before (target
);
3000 /* Include the BARRIER after INSN and copy the
3002 if (squeeze_notes (&new_label
, &last_insn_to_move
))
3004 reorder_insns (new_label
, last_insn_to_move
, loc
);
3006 /* All those insns are now in TARGET_LOOP. */
3008 q
!= NEXT_INSN (last_insn_to_move
);
3010 uid_loop
[INSN_UID (q
)] = target_loop
;
3012 /* The label jumped to by INSN is no longer a loop
3013 exit. Unless INSN does not have a label (e.g.,
3014 it is a RETURN insn), search loop->exit_labels
3015 to find its label_ref, and remove it. Also turn
3016 off LABEL_OUTSIDE_LOOP_P bit. */
3017 if (JUMP_LABEL (insn
))
3019 for (q
= 0, r
= this_loop
->exit_labels
;
3021 q
= r
, r
= LABEL_NEXTREF (r
))
3022 if (XEXP (r
, 0) == JUMP_LABEL (insn
))
3024 LABEL_OUTSIDE_LOOP_P (r
) = 0;
3026 LABEL_NEXTREF (q
) = LABEL_NEXTREF (r
);
3028 this_loop
->exit_labels
= LABEL_NEXTREF (r
);
3032 for (loop
= this_loop
; loop
&& loop
!= target_loop
;
3036 /* If we didn't find it, then something is
3042 /* P is now a jump outside the loop, so it must be put
3043 in loop->exit_labels, and marked as such.
3044 The easiest way to do this is to just call
3045 mark_loop_jump again for P. */
3046 mark_loop_jump (PATTERN (p
), this_loop
);
3048 /* If INSN now jumps to the insn after it,
3050 if (JUMP_LABEL (insn
) != 0
3051 && (next_real_insn (JUMP_LABEL (insn
))
3052 == next_real_insn (insn
)))
3053 delete_related_insns (insn
);
3056 /* Continue the loop after where the conditional
3057 branch used to jump, since the only branch insn
3058 in the block (if it still remains) is an inter-loop
3059 branch and hence needs no processing. */
3060 insn
= NEXT_INSN (cond_label
);
3062 if (--LABEL_NUSES (cond_label
) == 0)
3063 delete_related_insns (cond_label
);
3065 /* This loop will be continued with NEXT_INSN (insn). */
3066 insn
= PREV_INSN (insn
);
3073 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3074 loops it is contained in, mark the target loop invalid.
3076 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3079 mark_loop_jump (rtx x
, struct loop
*loop
)
3081 struct loop
*dest_loop
;
3082 struct loop
*outer_loop
;
3085 switch (GET_CODE (x
))
3098 /* There could be a label reference in here. */
3099 mark_loop_jump (XEXP (x
, 0), loop
);
3105 mark_loop_jump (XEXP (x
, 0), loop
);
3106 mark_loop_jump (XEXP (x
, 1), loop
);
3110 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3111 mark_loop_jump (XEXP (x
, 1), loop
);
3116 mark_loop_jump (XEXP (x
, 0), loop
);
3120 dest_loop
= uid_loop
[INSN_UID (XEXP (x
, 0))];
3122 /* Link together all labels that branch outside the loop. This
3123 is used by final_[bg]iv_value and the loop unrolling code. Also
3124 mark this LABEL_REF so we know that this branch should predict
3127 /* A check to make sure the label is not in an inner nested loop,
3128 since this does not count as a loop exit. */
3131 for (outer_loop
= dest_loop
; outer_loop
;
3132 outer_loop
= outer_loop
->outer
)
3133 if (outer_loop
== loop
)
3139 if (loop
&& ! outer_loop
)
3141 LABEL_OUTSIDE_LOOP_P (x
) = 1;
3142 LABEL_NEXTREF (x
) = loop
->exit_labels
;
3143 loop
->exit_labels
= x
;
3145 for (outer_loop
= loop
;
3146 outer_loop
&& outer_loop
!= dest_loop
;
3147 outer_loop
= outer_loop
->outer
)
3148 outer_loop
->exit_count
++;
3151 /* If this is inside a loop, but not in the current loop or one enclosed
3152 by it, it invalidates at least one loop. */
3157 /* We must invalidate every nested loop containing the target of this
3158 label, except those that also contain the jump insn. */
3160 for (; dest_loop
; dest_loop
= dest_loop
->outer
)
3162 /* Stop when we reach a loop that also contains the jump insn. */
3163 for (outer_loop
= loop
; outer_loop
; outer_loop
= outer_loop
->outer
)
3164 if (dest_loop
== outer_loop
)
3167 /* If we get here, we know we need to invalidate a loop. */
3168 if (loop_dump_stream
&& ! dest_loop
->invalid
)
3169 fprintf (loop_dump_stream
,
3170 "\nLoop at %d ignored due to multiple entry points.\n",
3171 INSN_UID (dest_loop
->start
));
3173 dest_loop
->invalid
= 1;
3178 /* If this is not setting pc, ignore. */
3179 if (SET_DEST (x
) == pc_rtx
)
3180 mark_loop_jump (SET_SRC (x
), loop
);
3184 mark_loop_jump (XEXP (x
, 1), loop
);
3185 mark_loop_jump (XEXP (x
, 2), loop
);
3190 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
3191 mark_loop_jump (XVECEXP (x
, 0, i
), loop
);
3195 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
3196 mark_loop_jump (XVECEXP (x
, 1, i
), loop
);
3200 /* Strictly speaking this is not a jump into the loop, only a possible
3201 jump out of the loop. However, we have no way to link the destination
3202 of this jump onto the list of exit labels. To be safe we mark this
3203 loop and any containing loops as invalid. */
3206 for (outer_loop
= loop
; outer_loop
; outer_loop
= outer_loop
->outer
)
3208 if (loop_dump_stream
&& ! outer_loop
->invalid
)
3209 fprintf (loop_dump_stream
,
3210 "\nLoop at %d ignored due to unknown exit jump.\n",
3211 INSN_UID (outer_loop
->start
));
3212 outer_loop
->invalid
= 1;
3219 /* Return nonzero if there is a label in the range from
3220 insn INSN to and including the insn whose luid is END
3221 INSN must have an assigned luid (i.e., it must not have
3222 been previously created by loop.c). */
3225 labels_in_range_p (rtx insn
, int end
)
3227 while (insn
&& INSN_LUID (insn
) <= end
)
3229 if (GET_CODE (insn
) == CODE_LABEL
)
3231 insn
= NEXT_INSN (insn
);
3237 /* Record that a memory reference X is being set. */
3240 note_addr_stored (rtx x
, rtx y ATTRIBUTE_UNUSED
,
3241 void *data ATTRIBUTE_UNUSED
)
3243 struct loop_info
*loop_info
= data
;
3245 if (x
== 0 || GET_CODE (x
) != MEM
)
3248 /* Count number of memory writes.
3249 This affects heuristics in strength_reduce. */
3250 loop_info
->num_mem_sets
++;
3252 /* BLKmode MEM means all memory is clobbered. */
3253 if (GET_MODE (x
) == BLKmode
)
3255 if (RTX_UNCHANGING_P (x
))
3256 loop_info
->unknown_constant_address_altered
= 1;
3258 loop_info
->unknown_address_altered
= 1;
3263 loop_info
->store_mems
= gen_rtx_EXPR_LIST (VOIDmode
, x
,
3264 loop_info
->store_mems
);
3267 /* X is a value modified by an INSN that references a biv inside a loop
3268 exit test (ie, X is somehow related to the value of the biv). If X
3269 is a pseudo that is used more than once, then the biv is (effectively)
3270 used more than once. DATA is a pointer to a loop_regs structure. */
3273 note_set_pseudo_multiple_uses (rtx x
, rtx y ATTRIBUTE_UNUSED
, void *data
)
3275 struct loop_regs
*regs
= (struct loop_regs
*) data
;
3280 while (GET_CODE (x
) == STRICT_LOW_PART
3281 || GET_CODE (x
) == SIGN_EXTRACT
3282 || GET_CODE (x
) == ZERO_EXTRACT
3283 || GET_CODE (x
) == SUBREG
)
3286 if (GET_CODE (x
) != REG
|| REGNO (x
) < FIRST_PSEUDO_REGISTER
)
3289 /* If we do not have usage information, or if we know the register
3290 is used more than once, note that fact for check_dbra_loop. */
3291 if (REGNO (x
) >= max_reg_before_loop
3292 || ! regs
->array
[REGNO (x
)].single_usage
3293 || regs
->array
[REGNO (x
)].single_usage
== const0_rtx
)
3294 regs
->multiple_uses
= 1;
3297 /* Return nonzero if the rtx X is invariant over the current loop.
3299 The value is 2 if we refer to something only conditionally invariant.
3301 A memory ref is invariant if it is not volatile and does not conflict
3302 with anything stored in `loop_info->store_mems'. */
3305 loop_invariant_p (const struct loop
*loop
, rtx x
)
3307 struct loop_info
*loop_info
= LOOP_INFO (loop
);
3308 struct loop_regs
*regs
= LOOP_REGS (loop
);
3312 int conditional
= 0;
3317 code
= GET_CODE (x
);
3327 /* A LABEL_REF is normally invariant, however, if we are unrolling
3328 loops, and this label is inside the loop, then it isn't invariant.
3329 This is because each unrolled copy of the loop body will have
3330 a copy of this label. If this was invariant, then an insn loading
3331 the address of this label into a register might get moved outside
3332 the loop, and then each loop body would end up using the same label.
3334 We don't know the loop bounds here though, so just fail for all
3336 if (flag_old_unroll_loops
)
3343 case UNSPEC_VOLATILE
:
3347 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3348 since the reg might be set by initialization within the loop. */
3350 if ((x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
3351 || x
== arg_pointer_rtx
|| x
== pic_offset_table_rtx
)
3352 && ! current_function_has_nonlocal_goto
)
3355 if (LOOP_INFO (loop
)->has_call
3356 && REGNO (x
) < FIRST_PSEUDO_REGISTER
&& call_used_regs
[REGNO (x
)])
3359 /* Out-of-range regs can occur when we are called from unrolling.
3360 These registers created by the unroller are set in the loop,
3361 hence are never invariant.
3362 Other out-of-range regs can be generated by load_mems; those that
3363 are written to in the loop are not invariant, while those that are
3364 not written to are invariant. It would be easy for load_mems
3365 to set n_times_set correctly for these registers, however, there
3366 is no easy way to distinguish them from registers created by the
3369 if (REGNO (x
) >= (unsigned) regs
->num
)
3372 if (regs
->array
[REGNO (x
)].set_in_loop
< 0)
3375 return regs
->array
[REGNO (x
)].set_in_loop
== 0;
3378 /* Volatile memory references must be rejected. Do this before
3379 checking for read-only items, so that volatile read-only items
3380 will be rejected also. */
3381 if (MEM_VOLATILE_P (x
))
3384 /* See if there is any dependence between a store and this load. */
3385 mem_list_entry
= loop_info
->store_mems
;
3386 while (mem_list_entry
)
3388 if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
3392 mem_list_entry
= XEXP (mem_list_entry
, 1);
3395 /* It's not invalidated by a store in memory
3396 but we must still verify the address is invariant. */
3400 /* Don't mess with insns declared volatile. */
3401 if (MEM_VOLATILE_P (x
))
3409 fmt
= GET_RTX_FORMAT (code
);
3410 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3414 int tem
= loop_invariant_p (loop
, XEXP (x
, i
));
3420 else if (fmt
[i
] == 'E')
3423 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3425 int tem
= loop_invariant_p (loop
, XVECEXP (x
, i
, j
));
3435 return 1 + conditional
;
3438 /* Return nonzero if all the insns in the loop that set REG
3439 are INSN and the immediately following insns,
3440 and if each of those insns sets REG in an invariant way
3441 (not counting uses of REG in them).
3443 The value is 2 if some of these insns are only conditionally invariant.
3445 We assume that INSN itself is the first set of REG
3446 and that its source is invariant. */
3449 consec_sets_invariant_p (const struct loop
*loop
, rtx reg
, int n_sets
,
3452 struct loop_regs
*regs
= LOOP_REGS (loop
);
3454 unsigned int regno
= REGNO (reg
);
3456 /* Number of sets we have to insist on finding after INSN. */
3457 int count
= n_sets
- 1;
3458 int old
= regs
->array
[regno
].set_in_loop
;
3462 /* If N_SETS hit the limit, we can't rely on its value. */
3466 regs
->array
[regno
].set_in_loop
= 0;
3474 code
= GET_CODE (p
);
3476 /* If library call, skip to end of it. */
3477 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
3482 && (set
= single_set (p
))
3483 && GET_CODE (SET_DEST (set
)) == REG
3484 && REGNO (SET_DEST (set
)) == regno
)
3486 this = loop_invariant_p (loop
, SET_SRC (set
));
3489 else if ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)))
3491 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3492 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3494 this = (CONSTANT_P (XEXP (temp
, 0))
3495 || (find_reg_note (p
, REG_RETVAL
, NULL_RTX
)
3496 && loop_invariant_p (loop
, XEXP (temp
, 0))));
3503 else if (code
!= NOTE
)
3505 regs
->array
[regno
].set_in_loop
= old
;
3510 regs
->array
[regno
].set_in_loop
= old
;
3511 /* If loop_invariant_p ever returned 2, we return 2. */
3512 return 1 + (value
& 2);
3516 /* I don't think this condition is sufficient to allow INSN
3517 to be moved, so we no longer test it. */
3519 /* Return 1 if all insns in the basic block of INSN and following INSN
3520 that set REG are invariant according to TABLE. */
3523 all_sets_invariant_p (rtx reg
, rtx insn
, short *table
)
3526 int regno
= REGNO (reg
);
3532 code
= GET_CODE (p
);
3533 if (code
== CODE_LABEL
|| code
== JUMP_INSN
)
3535 if (code
== INSN
&& GET_CODE (PATTERN (p
)) == SET
3536 && GET_CODE (SET_DEST (PATTERN (p
))) == REG
3537 && REGNO (SET_DEST (PATTERN (p
))) == regno
)
3539 if (! loop_invariant_p (loop
, SET_SRC (PATTERN (p
)), table
))
3546 /* Look at all uses (not sets) of registers in X. For each, if it is
3547 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3548 a different insn, set USAGE[REGNO] to const0_rtx. */
3551 find_single_use_in_loop (struct loop_regs
*regs
, rtx insn
, rtx x
)
3553 enum rtx_code code
= GET_CODE (x
);
3554 const char *fmt
= GET_RTX_FORMAT (code
);
3558 regs
->array
[REGNO (x
)].single_usage
3559 = (regs
->array
[REGNO (x
)].single_usage
!= 0
3560 && regs
->array
[REGNO (x
)].single_usage
!= insn
)
3561 ? const0_rtx
: insn
;
3563 else if (code
== SET
)
3565 /* Don't count SET_DEST if it is a REG; otherwise count things
3566 in SET_DEST because if a register is partially modified, it won't
3567 show up as a potential movable so we don't care how USAGE is set
3569 if (GET_CODE (SET_DEST (x
)) != REG
)
3570 find_single_use_in_loop (regs
, insn
, SET_DEST (x
));
3571 find_single_use_in_loop (regs
, insn
, SET_SRC (x
));
3574 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3576 if (fmt
[i
] == 'e' && XEXP (x
, i
) != 0)
3577 find_single_use_in_loop (regs
, insn
, XEXP (x
, i
));
3578 else if (fmt
[i
] == 'E')
3579 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
3580 find_single_use_in_loop (regs
, insn
, XVECEXP (x
, i
, j
));
3584 /* Count and record any set in X which is contained in INSN. Update
3585 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3589 count_one_set (struct loop_regs
*regs
, rtx insn
, rtx x
, rtx
*last_set
)
3591 if (GET_CODE (x
) == CLOBBER
&& GET_CODE (XEXP (x
, 0)) == REG
)
3592 /* Don't move a reg that has an explicit clobber.
3593 It's not worth the pain to try to do it correctly. */
3594 regs
->array
[REGNO (XEXP (x
, 0))].may_not_optimize
= 1;
3596 if (GET_CODE (x
) == SET
|| GET_CODE (x
) == CLOBBER
)
3598 rtx dest
= SET_DEST (x
);
3599 while (GET_CODE (dest
) == SUBREG
3600 || GET_CODE (dest
) == ZERO_EXTRACT
3601 || GET_CODE (dest
) == SIGN_EXTRACT
3602 || GET_CODE (dest
) == STRICT_LOW_PART
)
3603 dest
= XEXP (dest
, 0);
3604 if (GET_CODE (dest
) == REG
)
3607 int regno
= REGNO (dest
);
3608 for (i
= 0; i
< LOOP_REGNO_NREGS (regno
, dest
); i
++)
3610 /* If this is the first setting of this reg
3611 in current basic block, and it was set before,
3612 it must be set in two basic blocks, so it cannot
3613 be moved out of the loop. */
3614 if (regs
->array
[regno
].set_in_loop
> 0
3615 && last_set
[regno
] == 0)
3616 regs
->array
[regno
+i
].may_not_optimize
= 1;
3617 /* If this is not first setting in current basic block,
3618 see if reg was used in between previous one and this.
3619 If so, neither one can be moved. */
3620 if (last_set
[regno
] != 0
3621 && reg_used_between_p (dest
, last_set
[regno
], insn
))
3622 regs
->array
[regno
+i
].may_not_optimize
= 1;
3623 if (regs
->array
[regno
+i
].set_in_loop
< 127)
3624 ++regs
->array
[regno
+i
].set_in_loop
;
3625 last_set
[regno
+i
] = insn
;
3631 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3632 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3633 contained in insn INSN is used by any insn that precedes INSN in
3634 cyclic order starting from the loop entry point.
3636 We don't want to use INSN_LUID here because if we restrict INSN to those
3637 that have a valid INSN_LUID, it means we cannot move an invariant out
3638 from an inner loop past two loops. */
3641 loop_reg_used_before_p (const struct loop
*loop
, rtx set
, rtx insn
)
3643 rtx reg
= SET_DEST (set
);
3646 /* Scan forward checking for register usage. If we hit INSN, we
3647 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3648 for (p
= loop
->scan_start
; p
!= insn
; p
= NEXT_INSN (p
))
3650 if (INSN_P (p
) && reg_overlap_mentioned_p (reg
, PATTERN (p
)))
3661 /* Information we collect about arrays that we might want to prefetch. */
3662 struct prefetch_info
3664 struct iv_class
*class; /* Class this prefetch is based on. */
3665 struct induction
*giv
; /* GIV this prefetch is based on. */
3666 rtx base_address
; /* Start prefetching from this address plus
3668 HOST_WIDE_INT index
;
3669 HOST_WIDE_INT stride
; /* Prefetch stride in bytes in each
3671 unsigned int bytes_accessed
; /* Sum of sizes of all accesses to this
3672 prefetch area in one iteration. */
3673 unsigned int total_bytes
; /* Total bytes loop will access in this block.
3674 This is set only for loops with known
3675 iteration counts and is 0xffffffff
3677 int prefetch_in_loop
; /* Number of prefetch insns in loop. */
3678 int prefetch_before_loop
; /* Number of prefetch insns before loop. */
3679 unsigned int write
: 1; /* 1 for read/write prefetches. */
3682 /* Data used by check_store function. */
3683 struct check_store_data
3689 static void check_store (rtx
, rtx
, void *);
3690 static void emit_prefetch_instructions (struct loop
*);
3691 static int rtx_equal_for_prefetch_p (rtx
, rtx
);
3693 /* Set mem_write when mem_address is found. Used as callback to
3696 check_store (rtx x
, rtx pat ATTRIBUTE_UNUSED
, void *data
)
3698 struct check_store_data
*d
= (struct check_store_data
*) data
;
3700 if ((GET_CODE (x
) == MEM
) && rtx_equal_p (d
->mem_address
, XEXP (x
, 0)))
3704 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3705 important to get some addresses combined. Later more sophisticated
3706 transformations can be added when necessary.
3708 ??? Same trick with swapping operand is done at several other places.
3709 It can be nice to develop some common way to handle this. */
3712 rtx_equal_for_prefetch_p (rtx x
, rtx y
)
3716 enum rtx_code code
= GET_CODE (x
);
3721 if (code
!= GET_CODE (y
))
3724 code
= GET_CODE (x
);
3726 if (GET_RTX_CLASS (code
) == 'c')
3728 return ((rtx_equal_for_prefetch_p (XEXP (x
, 0), XEXP (y
, 0))
3729 && rtx_equal_for_prefetch_p (XEXP (x
, 1), XEXP (y
, 1)))
3730 || (rtx_equal_for_prefetch_p (XEXP (x
, 0), XEXP (y
, 1))
3731 && rtx_equal_for_prefetch_p (XEXP (x
, 1), XEXP (y
, 0))));
3733 /* Compare the elements. If any pair of corresponding elements fails to
3734 match, return 0 for the whole thing. */
3736 fmt
= GET_RTX_FORMAT (code
);
3737 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
3742 if (XWINT (x
, i
) != XWINT (y
, i
))
3747 if (XINT (x
, i
) != XINT (y
, i
))
3752 /* Two vectors must have the same length. */
3753 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
3756 /* And the corresponding elements must match. */
3757 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
3758 if (rtx_equal_for_prefetch_p (XVECEXP (x
, i
, j
),
3759 XVECEXP (y
, i
, j
)) == 0)
3764 if (rtx_equal_for_prefetch_p (XEXP (x
, i
), XEXP (y
, i
)) == 0)
3769 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
3774 /* These are just backpointers, so they don't matter. */
3780 /* It is believed that rtx's at this level will never
3781 contain anything but integers and other rtx's,
3782 except for within LABEL_REFs and SYMBOL_REFs. */
3790 /* Remove constant addition value from the expression X (when present)
3793 static HOST_WIDE_INT
3794 remove_constant_addition (rtx
*x
)
3796 HOST_WIDE_INT addval
= 0;
3799 /* Avoid clobbering a shared CONST expression. */
3800 if (GET_CODE (exp
) == CONST
)
3802 if (GET_CODE (XEXP (exp
, 0)) == PLUS
3803 && GET_CODE (XEXP (XEXP (exp
, 0), 0)) == SYMBOL_REF
3804 && GET_CODE (XEXP (XEXP (exp
, 0), 1)) == CONST_INT
)
3806 *x
= XEXP (XEXP (exp
, 0), 0);
3807 return INTVAL (XEXP (XEXP (exp
, 0), 1));
3812 if (GET_CODE (exp
) == CONST_INT
)
3814 addval
= INTVAL (exp
);
3818 /* For plus expression recurse on ourself. */
3819 else if (GET_CODE (exp
) == PLUS
)
3821 addval
+= remove_constant_addition (&XEXP (exp
, 0));
3822 addval
+= remove_constant_addition (&XEXP (exp
, 1));
3824 /* In case our parameter was constant, remove extra zero from the
3826 if (XEXP (exp
, 0) == const0_rtx
)
3828 else if (XEXP (exp
, 1) == const0_rtx
)
3835 /* Attempt to identify accesses to arrays that are most likely to cause cache
3836 misses, and emit prefetch instructions a few prefetch blocks forward.
3838 To detect the arrays we use the GIV information that was collected by the
3839 strength reduction pass.
3841 The prefetch instructions are generated after the GIV information is done
3842 and before the strength reduction process. The new GIVs are injected into
3843 the strength reduction tables, so the prefetch addresses are optimized as
3846 GIVs are split into base address, stride, and constant addition values.
3847 GIVs with the same address, stride and close addition values are combined
3848 into a single prefetch. Also writes to GIVs are detected, so that prefetch
3849 for write instructions can be used for the block we write to, on machines
3850 that support write prefetches.
3852 Several heuristics are used to determine when to prefetch. They are
3853 controlled by defined symbols that can be overridden for each target. */
3856 emit_prefetch_instructions (struct loop
*loop
)
3858 int num_prefetches
= 0;
3859 int num_real_prefetches
= 0;
3860 int num_real_write_prefetches
= 0;
3861 int num_prefetches_before
= 0;
3862 int num_write_prefetches_before
= 0;
3865 struct iv_class
*bl
;
3866 struct induction
*iv
;
3867 struct prefetch_info info
[MAX_PREFETCHES
];
3868 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
3870 if (!HAVE_prefetch
|| PREFETCH_BLOCK
== 0)
3873 /* Consider only loops w/o calls. When a call is done, the loop is probably
3874 slow enough to read the memory. */
3875 if (PREFETCH_NO_CALL
&& LOOP_INFO (loop
)->has_call
)
3877 if (loop_dump_stream
)
3878 fprintf (loop_dump_stream
, "Prefetch: ignoring loop: has call.\n");
3883 /* Don't prefetch in loops known to have few iterations. */
3884 if (PREFETCH_NO_LOW_LOOPCNT
3885 && LOOP_INFO (loop
)->n_iterations
3886 && LOOP_INFO (loop
)->n_iterations
<= PREFETCH_LOW_LOOPCNT
)
3888 if (loop_dump_stream
)
3889 fprintf (loop_dump_stream
,
3890 "Prefetch: ignoring loop: not enough iterations.\n");
3894 /* Search all induction variables and pick those interesting for the prefetch
3896 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
3898 struct induction
*biv
= bl
->biv
, *biv1
;
3903 /* Expect all BIVs to be executed in each iteration. This makes our
3904 analysis more conservative. */
3907 /* Discard non-constant additions that we can't handle well yet, and
3908 BIVs that are executed multiple times; such BIVs ought to be
3909 handled in the nested loop. We accept not_every_iteration BIVs,
3910 since these only result in larger strides and make our
3911 heuristics more conservative. */
3912 if (GET_CODE (biv
->add_val
) != CONST_INT
)
3914 if (loop_dump_stream
)
3916 fprintf (loop_dump_stream
,
3917 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
3918 REGNO (biv
->src_reg
), INSN_UID (biv
->insn
));
3919 print_rtl (loop_dump_stream
, biv
->add_val
);
3920 fprintf (loop_dump_stream
, "\n");
3925 if (biv
->maybe_multiple
)
3927 if (loop_dump_stream
)
3929 fprintf (loop_dump_stream
,
3930 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
3931 REGNO (biv
->src_reg
), INSN_UID (biv
->insn
));
3932 print_rtl (loop_dump_stream
, biv
->add_val
);
3933 fprintf (loop_dump_stream
, "\n");
3938 basestride
+= INTVAL (biv1
->add_val
);
3939 biv1
= biv1
->next_iv
;
3942 if (biv1
|| !basestride
)
3945 for (iv
= bl
->giv
; iv
; iv
= iv
->next_iv
)
3949 HOST_WIDE_INT index
= 0;
3951 HOST_WIDE_INT stride
= 0;
3952 int stride_sign
= 1;
3953 struct check_store_data d
;
3954 const char *ignore_reason
= NULL
;
3955 int size
= GET_MODE_SIZE (GET_MODE (iv
));
3957 /* See whether an induction variable is interesting to us and if
3958 not, report the reason. */
3959 if (iv
->giv_type
!= DEST_ADDR
)
3960 ignore_reason
= "giv is not a destination address";
3962 /* We are interested only in constant stride memory references
3963 in order to be able to compute density easily. */
3964 else if (GET_CODE (iv
->mult_val
) != CONST_INT
)
3965 ignore_reason
= "stride is not constant";
3969 stride
= INTVAL (iv
->mult_val
) * basestride
;
3976 /* On some targets, reversed order prefetches are not
3978 if (PREFETCH_NO_REVERSE_ORDER
&& stride_sign
< 0)
3979 ignore_reason
= "reversed order stride";
3981 /* Prefetch of accesses with an extreme stride might not be
3982 worthwhile, either. */
3983 else if (PREFETCH_NO_EXTREME_STRIDE
3984 && stride
> PREFETCH_EXTREME_STRIDE
)
3985 ignore_reason
= "extreme stride";
3987 /* Ignore GIVs with varying add values; we can't predict the
3988 value for the next iteration. */
3989 else if (!loop_invariant_p (loop
, iv
->add_val
))
3990 ignore_reason
= "giv has varying add value";
3992 /* Ignore GIVs in the nested loops; they ought to have been
3994 else if (iv
->maybe_multiple
)
3995 ignore_reason
= "giv is in nested loop";
3998 if (ignore_reason
!= NULL
)
4000 if (loop_dump_stream
)
4001 fprintf (loop_dump_stream
,
4002 "Prefetch: ignoring giv at %d: %s.\n",
4003 INSN_UID (iv
->insn
), ignore_reason
);
4007 /* Determine the pointer to the basic array we are examining. It is
4008 the sum of the BIV's initial value and the GIV's add_val. */
4009 address
= copy_rtx (iv
->add_val
);
4010 temp
= copy_rtx (bl
->initial_value
);
4012 address
= simplify_gen_binary (PLUS
, Pmode
, temp
, address
);
4013 index
= remove_constant_addition (&address
);
4016 d
.mem_address
= *iv
->location
;
4018 /* When the GIV is not always executed, we might be better off by
4019 not dirtying the cache pages. */
4020 if (PREFETCH_CONDITIONAL
|| iv
->always_executed
)
4021 note_stores (PATTERN (iv
->insn
), check_store
, &d
);
4024 if (loop_dump_stream
)
4025 fprintf (loop_dump_stream
, "Prefetch: Ignoring giv at %d: %s\n",
4026 INSN_UID (iv
->insn
), "in conditional code.");
4030 /* Attempt to find another prefetch to the same array and see if we
4031 can merge this one. */
4032 for (i
= 0; i
< num_prefetches
; i
++)
4033 if (rtx_equal_for_prefetch_p (address
, info
[i
].base_address
)
4034 && stride
== info
[i
].stride
)
4036 /* In case both access same array (same location
4037 just with small difference in constant indexes), merge
4038 the prefetches. Just do the later and the earlier will
4039 get prefetched from previous iteration.
4040 The artificial threshold should not be too small,
4041 but also not bigger than small portion of memory usually
4042 traversed by single loop. */
4043 if (index
>= info
[i
].index
4044 && index
- info
[i
].index
< PREFETCH_EXTREME_DIFFERENCE
)
4046 info
[i
].write
|= d
.mem_write
;
4047 info
[i
].bytes_accessed
+= size
;
4048 info
[i
].index
= index
;
4051 info
[num_prefetches
].base_address
= address
;
4056 if (index
< info
[i
].index
4057 && info
[i
].index
- index
< PREFETCH_EXTREME_DIFFERENCE
)
4059 info
[i
].write
|= d
.mem_write
;
4060 info
[i
].bytes_accessed
+= size
;
4066 /* Merging failed. */
4069 info
[num_prefetches
].giv
= iv
;
4070 info
[num_prefetches
].class = bl
;
4071 info
[num_prefetches
].index
= index
;
4072 info
[num_prefetches
].stride
= stride
;
4073 info
[num_prefetches
].base_address
= address
;
4074 info
[num_prefetches
].write
= d
.mem_write
;
4075 info
[num_prefetches
].bytes_accessed
= size
;
4077 if (num_prefetches
>= MAX_PREFETCHES
)
4079 if (loop_dump_stream
)
4080 fprintf (loop_dump_stream
,
4081 "Maximal number of prefetches exceeded.\n");
4088 for (i
= 0; i
< num_prefetches
; i
++)
4092 /* Attempt to calculate the total number of bytes fetched by all
4093 iterations of the loop. Avoid overflow. */
4094 if (LOOP_INFO (loop
)->n_iterations
4095 && ((unsigned HOST_WIDE_INT
) (0xffffffff / info
[i
].stride
)
4096 >= LOOP_INFO (loop
)->n_iterations
))
4097 info
[i
].total_bytes
= info
[i
].stride
* LOOP_INFO (loop
)->n_iterations
;
4099 info
[i
].total_bytes
= 0xffffffff;
4101 density
= info
[i
].bytes_accessed
* 100 / info
[i
].stride
;
4103 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4104 if (PREFETCH_ONLY_DENSE_MEM
)
4105 if (density
* 256 > PREFETCH_DENSE_MEM
* 100
4106 && (info
[i
].total_bytes
/ PREFETCH_BLOCK
4107 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN
))
4109 info
[i
].prefetch_before_loop
= 1;
4110 info
[i
].prefetch_in_loop
4111 = (info
[i
].total_bytes
/ PREFETCH_BLOCK
4112 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX
);
4116 info
[i
].prefetch_in_loop
= 0, info
[i
].prefetch_before_loop
= 0;
4117 if (loop_dump_stream
)
4118 fprintf (loop_dump_stream
,
4119 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4120 INSN_UID (info
[i
].giv
->insn
), density
);
4123 info
[i
].prefetch_in_loop
= 1, info
[i
].prefetch_before_loop
= 1;
4125 /* Find how many prefetch instructions we'll use within the loop. */
4126 if (info
[i
].prefetch_in_loop
!= 0)
4128 info
[i
].prefetch_in_loop
= ((info
[i
].stride
+ PREFETCH_BLOCK
- 1)
4130 num_real_prefetches
+= info
[i
].prefetch_in_loop
;
4132 num_real_write_prefetches
+= info
[i
].prefetch_in_loop
;
4136 /* Determine how many iterations ahead to prefetch within the loop, based
4137 on how many prefetches we currently expect to do within the loop. */
4138 if (num_real_prefetches
!= 0)
4140 if ((ahead
= SIMULTANEOUS_PREFETCHES
/ num_real_prefetches
) == 0)
4142 if (loop_dump_stream
)
4143 fprintf (loop_dump_stream
,
4144 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4145 SIMULTANEOUS_PREFETCHES
, num_real_prefetches
);
4146 num_real_prefetches
= 0, num_real_write_prefetches
= 0;
4149 /* We'll also use AHEAD to determine how many prefetch instructions to
4150 emit before a loop, so don't leave it zero. */
4152 ahead
= PREFETCH_BLOCKS_BEFORE_LOOP_MAX
;
4154 for (i
= 0; i
< num_prefetches
; i
++)
4156 /* Update if we've decided not to prefetch anything within the loop. */
4157 if (num_real_prefetches
== 0)
4158 info
[i
].prefetch_in_loop
= 0;
4160 /* Find how many prefetch instructions we'll use before the loop. */
4161 if (info
[i
].prefetch_before_loop
!= 0)
4163 int n
= info
[i
].total_bytes
/ PREFETCH_BLOCK
;
4166 info
[i
].prefetch_before_loop
= n
;
4167 num_prefetches_before
+= n
;
4169 num_write_prefetches_before
+= n
;
4172 if (loop_dump_stream
)
4174 if (info
[i
].prefetch_in_loop
== 0
4175 && info
[i
].prefetch_before_loop
== 0)
4177 fprintf (loop_dump_stream
, "Prefetch insn: %d",
4178 INSN_UID (info
[i
].giv
->insn
));
4179 fprintf (loop_dump_stream
,
4180 "; in loop: %d; before: %d; %s\n",
4181 info
[i
].prefetch_in_loop
,
4182 info
[i
].prefetch_before_loop
,
4183 info
[i
].write
? "read/write" : "read only");
4184 fprintf (loop_dump_stream
,
4185 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4186 (int) (info
[i
].bytes_accessed
* 100 / info
[i
].stride
),
4187 info
[i
].bytes_accessed
, info
[i
].total_bytes
);
4188 fprintf (loop_dump_stream
, " index: " HOST_WIDE_INT_PRINT_DEC
4189 "; stride: " HOST_WIDE_INT_PRINT_DEC
"; address: ",
4190 info
[i
].index
, info
[i
].stride
);
4191 print_rtl (loop_dump_stream
, info
[i
].base_address
);
4192 fprintf (loop_dump_stream
, "\n");
4196 if (num_real_prefetches
+ num_prefetches_before
> 0)
4198 /* Record that this loop uses prefetch instructions. */
4199 LOOP_INFO (loop
)->has_prefetch
= 1;
4201 if (loop_dump_stream
)
4203 fprintf (loop_dump_stream
, "Real prefetches needed within loop: %d (write: %d)\n",
4204 num_real_prefetches
, num_real_write_prefetches
);
4205 fprintf (loop_dump_stream
, "Real prefetches needed before loop: %d (write: %d)\n",
4206 num_prefetches_before
, num_write_prefetches_before
);
4210 for (i
= 0; i
< num_prefetches
; i
++)
4214 for (y
= 0; y
< info
[i
].prefetch_in_loop
; y
++)
4216 rtx loc
= copy_rtx (*info
[i
].giv
->location
);
4218 int bytes_ahead
= PREFETCH_BLOCK
* (ahead
+ y
);
4219 rtx before_insn
= info
[i
].giv
->insn
;
4220 rtx prev_insn
= PREV_INSN (info
[i
].giv
->insn
);
4223 /* We can save some effort by offsetting the address on
4224 architectures with offsettable memory references. */
4225 if (offsettable_address_p (0, VOIDmode
, loc
))
4226 loc
= plus_constant (loc
, bytes_ahead
);
4229 rtx reg
= gen_reg_rtx (Pmode
);
4230 loop_iv_add_mult_emit_before (loop
, loc
, const1_rtx
,
4231 GEN_INT (bytes_ahead
), reg
,
4237 /* Make sure the address operand is valid for prefetch. */
4238 if (! (*insn_data
[(int)CODE_FOR_prefetch
].operand
[0].predicate
)
4239 (loc
, insn_data
[(int)CODE_FOR_prefetch
].operand
[0].mode
))
4240 loc
= force_reg (Pmode
, loc
);
4241 emit_insn (gen_prefetch (loc
, GEN_INT (info
[i
].write
),
4245 emit_insn_before (seq
, before_insn
);
4247 /* Check all insns emitted and record the new GIV
4249 insn
= NEXT_INSN (prev_insn
);
4250 while (insn
!= before_insn
)
4252 insn
= check_insn_for_givs (loop
, insn
,
4253 info
[i
].giv
->always_executed
,
4254 info
[i
].giv
->maybe_multiple
);
4255 insn
= NEXT_INSN (insn
);
4259 if (PREFETCH_BEFORE_LOOP
)
4261 /* Emit insns before the loop to fetch the first cache lines or,
4262 if we're not prefetching within the loop, everything we expect
4264 for (y
= 0; y
< info
[i
].prefetch_before_loop
; y
++)
4266 rtx reg
= gen_reg_rtx (Pmode
);
4267 rtx loop_start
= loop
->start
;
4268 rtx init_val
= info
[i
].class->initial_value
;
4269 rtx add_val
= simplify_gen_binary (PLUS
, Pmode
,
4270 info
[i
].giv
->add_val
,
4271 GEN_INT (y
* PREFETCH_BLOCK
));
4273 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4274 non-constant INIT_VAL to have the same mode as REG, which
4275 in this case we know to be Pmode. */
4276 if (GET_MODE (init_val
) != Pmode
&& !CONSTANT_P (init_val
))
4281 init_val
= convert_to_mode (Pmode
, init_val
, 0);
4284 loop_insn_emit_before (loop
, 0, loop_start
, seq
);
4286 loop_iv_add_mult_emit_before (loop
, init_val
,
4287 info
[i
].giv
->mult_val
,
4288 add_val
, reg
, 0, loop_start
);
4289 emit_insn_before (gen_prefetch (reg
, GEN_INT (info
[i
].write
),
4299 /* Communication with routines called via `note_stores'. */
4301 static rtx note_insn
;
4303 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4305 static rtx addr_placeholder
;
4307 /* ??? Unfinished optimizations, and possible future optimizations,
4308 for the strength reduction code. */
4310 /* ??? The interaction of biv elimination, and recognition of 'constant'
4311 bivs, may cause problems. */
4313 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4314 performance problems.
4316 Perhaps don't eliminate things that can be combined with an addressing
4317 mode. Find all givs that have the same biv, mult_val, and add_val;
4318 then for each giv, check to see if its only use dies in a following
4319 memory address. If so, generate a new memory address and check to see
4320 if it is valid. If it is valid, then store the modified memory address,
4321 otherwise, mark the giv as not done so that it will get its own iv. */
4323 /* ??? Could try to optimize branches when it is known that a biv is always
4326 /* ??? When replace a biv in a compare insn, we should replace with closest
4327 giv so that an optimized branch can still be recognized by the combiner,
4328 e.g. the VAX acb insn. */
4330 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4331 was rerun in loop_optimize whenever a register was added or moved.
4332 Also, some of the optimizations could be a little less conservative. */
4334 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4335 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4338 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4339 least once for every loop iteration except for the last one.
4341 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4345 for_each_insn_in_loop (struct loop
*loop
, loop_insn_callback fncall
)
4347 int not_every_iteration
= 0;
4348 int maybe_multiple
= 0;
4349 int past_loop_latch
= 0;
4353 /* If loop_scan_start points to the loop exit test, we have to be wary of
4354 subversive use of gotos inside expression statements. */
4355 if (prev_nonnote_insn (loop
->scan_start
) != prev_nonnote_insn (loop
->start
))
4356 maybe_multiple
= back_branch_in_range_p (loop
, loop
->scan_start
);
4358 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4359 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
4361 p
= next_insn_in_loop (loop
, p
))
4363 p
= fncall (loop
, p
, not_every_iteration
, maybe_multiple
);
4365 /* Past CODE_LABEL, we get to insns that may be executed multiple
4366 times. The only way we can be sure that they can't is if every
4367 jump insn between here and the end of the loop either
4368 returns, exits the loop, is a jump to a location that is still
4369 behind the label, or is a jump to the loop start. */
4371 if (GET_CODE (p
) == CODE_LABEL
)
4379 insn
= NEXT_INSN (insn
);
4380 if (insn
== loop
->scan_start
)
4382 if (insn
== loop
->end
)
4388 if (insn
== loop
->scan_start
)
4392 if (GET_CODE (insn
) == JUMP_INSN
4393 && GET_CODE (PATTERN (insn
)) != RETURN
4394 && (!any_condjump_p (insn
)
4395 || (JUMP_LABEL (insn
) != 0
4396 && JUMP_LABEL (insn
) != loop
->scan_start
4397 && !loop_insn_first_p (p
, JUMP_LABEL (insn
)))))
4405 /* Past a jump, we get to insns for which we can't count
4406 on whether they will be executed during each iteration. */
4407 /* This code appears twice in strength_reduce. There is also similar
4408 code in scan_loop. */
4409 if (GET_CODE (p
) == JUMP_INSN
4410 /* If we enter the loop in the middle, and scan around to the
4411 beginning, don't set not_every_iteration for that.
4412 This can be any kind of jump, since we want to know if insns
4413 will be executed if the loop is executed. */
4414 && !(JUMP_LABEL (p
) == loop
->top
4415 && ((NEXT_INSN (NEXT_INSN (p
)) == loop
->end
4416 && any_uncondjump_p (p
))
4417 || (NEXT_INSN (p
) == loop
->end
&& any_condjump_p (p
)))))
4421 /* If this is a jump outside the loop, then it also doesn't
4422 matter. Check to see if the target of this branch is on the
4423 loop->exits_labels list. */
4425 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
4426 if (XEXP (label
, 0) == JUMP_LABEL (p
))
4430 not_every_iteration
= 1;
4433 else if (GET_CODE (p
) == NOTE
)
4435 /* At the virtual top of a converted loop, insns are again known to
4436 be executed each iteration: logically, the loop begins here
4437 even though the exit code has been duplicated.
4439 Insns are also again known to be executed each iteration at
4440 the LOOP_CONT note. */
4441 if ((NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_VTOP
4442 || NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_CONT
)
4444 not_every_iteration
= 0;
4445 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_BEG
)
4447 else if (NOTE_LINE_NUMBER (p
) == NOTE_INSN_LOOP_END
)
4451 /* Note if we pass a loop latch. If we do, then we can not clear
4452 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4453 a loop since a jump before the last CODE_LABEL may have started
4454 a new loop iteration.
4456 Note that LOOP_TOP is only set for rotated loops and we need
4457 this check for all loops, so compare against the CODE_LABEL
4458 which immediately follows LOOP_START. */
4459 if (GET_CODE (p
) == JUMP_INSN
4460 && JUMP_LABEL (p
) == NEXT_INSN (loop
->start
))
4461 past_loop_latch
= 1;
4463 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4464 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4465 or not an insn is known to be executed each iteration of the
4466 loop, whether or not any iterations are known to occur.
4468 Therefore, if we have just passed a label and have no more labels
4469 between here and the test insn of the loop, and we have not passed
4470 a jump to the top of the loop, then we know these insns will be
4471 executed each iteration. */
4473 if (not_every_iteration
4475 && GET_CODE (p
) == CODE_LABEL
4476 && no_labels_between_p (p
, loop
->end
)
4477 && loop_insn_first_p (p
, loop
->cont
))
4478 not_every_iteration
= 0;
4483 loop_bivs_find (struct loop
*loop
)
4485 struct loop_regs
*regs
= LOOP_REGS (loop
);
4486 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4487 /* Temporary list pointers for traversing ivs->list. */
4488 struct iv_class
*bl
, **backbl
;
4492 for_each_insn_in_loop (loop
, check_insn_for_bivs
);
4494 /* Scan ivs->list to remove all regs that proved not to be bivs.
4495 Make a sanity check against regs->n_times_set. */
4496 for (backbl
= &ivs
->list
, bl
= *backbl
; bl
; bl
= bl
->next
)
4498 if (REG_IV_TYPE (ivs
, bl
->regno
) != BASIC_INDUCT
4499 /* Above happens if register modified by subreg, etc. */
4500 /* Make sure it is not recognized as a basic induction var: */
4501 || regs
->array
[bl
->regno
].n_times_set
!= bl
->biv_count
4502 /* If never incremented, it is invariant that we decided not to
4503 move. So leave it alone. */
4504 || ! bl
->incremented
)
4506 if (loop_dump_stream
)
4507 fprintf (loop_dump_stream
, "Biv %d: discarded, %s\n",
4509 (REG_IV_TYPE (ivs
, bl
->regno
) != BASIC_INDUCT
4510 ? "not induction variable"
4511 : (! bl
->incremented
? "never incremented"
4514 REG_IV_TYPE (ivs
, bl
->regno
) = NOT_BASIC_INDUCT
;
4521 if (loop_dump_stream
)
4522 fprintf (loop_dump_stream
, "Biv %d: verified\n", bl
->regno
);
4528 /* Determine how BIVS are initialized by looking through pre-header
4529 extended basic block. */
4531 loop_bivs_init_find (struct loop
*loop
)
4533 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4534 /* Temporary list pointers for traversing ivs->list. */
4535 struct iv_class
*bl
;
4539 /* Find initial value for each biv by searching backwards from loop_start,
4540 halting at first label. Also record any test condition. */
4543 for (p
= loop
->start
; p
&& GET_CODE (p
) != CODE_LABEL
; p
= PREV_INSN (p
))
4549 if (GET_CODE (p
) == CALL_INSN
)
4553 note_stores (PATTERN (p
), record_initial
, ivs
);
4555 /* Record any test of a biv that branches around the loop if no store
4556 between it and the start of loop. We only care about tests with
4557 constants and registers and only certain of those. */
4558 if (GET_CODE (p
) == JUMP_INSN
4559 && JUMP_LABEL (p
) != 0
4560 && next_real_insn (JUMP_LABEL (p
)) == next_real_insn (loop
->end
)
4561 && (test
= get_condition_for_loop (loop
, p
)) != 0
4562 && GET_CODE (XEXP (test
, 0)) == REG
4563 && REGNO (XEXP (test
, 0)) < max_reg_before_loop
4564 && (bl
= REG_IV_CLASS (ivs
, REGNO (XEXP (test
, 0)))) != 0
4565 && valid_initial_value_p (XEXP (test
, 1), p
, call_seen
, loop
->start
)
4566 && bl
->init_insn
== 0)
4568 /* If an NE test, we have an initial value! */
4569 if (GET_CODE (test
) == NE
)
4572 bl
->init_set
= gen_rtx_SET (VOIDmode
,
4573 XEXP (test
, 0), XEXP (test
, 1));
4576 bl
->initial_test
= test
;
4582 /* Look at the each biv and see if we can say anything better about its
4583 initial value from any initializing insns set up above. (This is done
4584 in two passes to avoid missing SETs in a PARALLEL.) */
4586 loop_bivs_check (struct loop
*loop
)
4588 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4589 /* Temporary list pointers for traversing ivs->list. */
4590 struct iv_class
*bl
;
4591 struct iv_class
**backbl
;
4593 for (backbl
= &ivs
->list
; (bl
= *backbl
); backbl
= &bl
->next
)
4598 if (! bl
->init_insn
)
4601 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4602 is a constant, use the value of that. */
4603 if (((note
= find_reg_note (bl
->init_insn
, REG_EQUAL
, 0)) != NULL
4604 && CONSTANT_P (XEXP (note
, 0)))
4605 || ((note
= find_reg_note (bl
->init_insn
, REG_EQUIV
, 0)) != NULL
4606 && CONSTANT_P (XEXP (note
, 0))))
4607 src
= XEXP (note
, 0);
4609 src
= SET_SRC (bl
->init_set
);
4611 if (loop_dump_stream
)
4612 fprintf (loop_dump_stream
,
4613 "Biv %d: initialized at insn %d: initial value ",
4614 bl
->regno
, INSN_UID (bl
->init_insn
));
4616 if ((GET_MODE (src
) == GET_MODE (regno_reg_rtx
[bl
->regno
])
4617 || GET_MODE (src
) == VOIDmode
)
4618 && valid_initial_value_p (src
, bl
->init_insn
,
4619 LOOP_INFO (loop
)->pre_header_has_call
,
4622 bl
->initial_value
= src
;
4624 if (loop_dump_stream
)
4626 print_simple_rtl (loop_dump_stream
, src
);
4627 fputc ('\n', loop_dump_stream
);
4630 /* If we can't make it a giv,
4631 let biv keep initial value of "itself". */
4632 else if (loop_dump_stream
)
4633 fprintf (loop_dump_stream
, "is complex\n");
4638 /* Search the loop for general induction variables. */
4641 loop_givs_find (struct loop
* loop
)
4643 for_each_insn_in_loop (loop
, check_insn_for_givs
);
4647 /* For each giv for which we still don't know whether or not it is
4648 replaceable, check to see if it is replaceable because its final value
4649 can be calculated. */
4652 loop_givs_check (struct loop
*loop
)
4654 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
4655 struct iv_class
*bl
;
4657 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
4659 struct induction
*v
;
4661 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4662 if (! v
->replaceable
&& ! v
->not_replaceable
)
4663 check_final_value (loop
, v
);
4668 /* Return nonzero if it is possible to eliminate the biv BL provided
4669 all givs are reduced. This is possible if either the reg is not
4670 used outside the loop, or we can compute what its final value will
4674 loop_biv_eliminable_p (struct loop
*loop
, struct iv_class
*bl
,
4675 int threshold
, int insn_count
)
4677 /* For architectures with a decrement_and_branch_until_zero insn,
4678 don't do this if we put a REG_NONNEG note on the endtest for this
4681 #ifdef HAVE_decrement_and_branch_until_zero
4684 if (loop_dump_stream
)
4685 fprintf (loop_dump_stream
,
4686 "Cannot eliminate nonneg biv %d.\n", bl
->regno
);
4691 /* Check that biv is used outside loop or if it has a final value.
4692 Compare against bl->init_insn rather than loop->start. We aren't
4693 concerned with any uses of the biv between init_insn and
4694 loop->start since these won't be affected by the value of the biv
4695 elsewhere in the function, so long as init_insn doesn't use the
4698 if ((REGNO_LAST_LUID (bl
->regno
) < INSN_LUID (loop
->end
)
4700 && INSN_UID (bl
->init_insn
) < max_uid_for_loop
4701 && REGNO_FIRST_LUID (bl
->regno
) >= INSN_LUID (bl
->init_insn
)
4702 && ! reg_mentioned_p (bl
->biv
->dest_reg
, SET_SRC (bl
->init_set
)))
4703 || (bl
->final_value
= final_biv_value (loop
, bl
)))
4704 return maybe_eliminate_biv (loop
, bl
, 0, threshold
, insn_count
);
4706 if (loop_dump_stream
)
4708 fprintf (loop_dump_stream
,
4709 "Cannot eliminate biv %d.\n",
4711 fprintf (loop_dump_stream
,
4712 "First use: insn %d, last use: insn %d.\n",
4713 REGNO_FIRST_UID (bl
->regno
),
4714 REGNO_LAST_UID (bl
->regno
));
4720 /* Reduce each giv of BL that we have decided to reduce. */
4723 loop_givs_reduce (struct loop
*loop
, struct iv_class
*bl
)
4725 struct induction
*v
;
4727 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4729 struct induction
*tv
;
4730 if (! v
->ignore
&& v
->same
== 0)
4732 int auto_inc_opt
= 0;
4734 /* If the code for derived givs immediately below has already
4735 allocated a new_reg, we must keep it. */
4737 v
->new_reg
= gen_reg_rtx (v
->mode
);
4740 /* If the target has auto-increment addressing modes, and
4741 this is an address giv, then try to put the increment
4742 immediately after its use, so that flow can create an
4743 auto-increment addressing mode. */
4744 if (v
->giv_type
== DEST_ADDR
&& bl
->biv_count
== 1
4745 && bl
->biv
->always_executed
&& ! bl
->biv
->maybe_multiple
4746 /* We don't handle reversed biv's because bl->biv->insn
4747 does not have a valid INSN_LUID. */
4749 && v
->always_executed
&& ! v
->maybe_multiple
4750 && INSN_UID (v
->insn
) < max_uid_for_loop
)
4752 /* If other giv's have been combined with this one, then
4753 this will work only if all uses of the other giv's occur
4754 before this giv's insn. This is difficult to check.
4756 We simplify this by looking for the common case where
4757 there is one DEST_REG giv, and this giv's insn is the
4758 last use of the dest_reg of that DEST_REG giv. If the
4759 increment occurs after the address giv, then we can
4760 perform the optimization. (Otherwise, the increment
4761 would have to go before other_giv, and we would not be
4762 able to combine it with the address giv to get an
4763 auto-inc address.) */
4764 if (v
->combined_with
)
4766 struct induction
*other_giv
= 0;
4768 for (tv
= bl
->giv
; tv
; tv
= tv
->next_iv
)
4776 if (! tv
&& other_giv
4777 && REGNO (other_giv
->dest_reg
) < max_reg_before_loop
4778 && (REGNO_LAST_UID (REGNO (other_giv
->dest_reg
))
4779 == INSN_UID (v
->insn
))
4780 && INSN_LUID (v
->insn
) < INSN_LUID (bl
->biv
->insn
))
4783 /* Check for case where increment is before the address
4784 giv. Do this test in "loop order". */
4785 else if ((INSN_LUID (v
->insn
) > INSN_LUID (bl
->biv
->insn
)
4786 && (INSN_LUID (v
->insn
) < INSN_LUID (loop
->scan_start
)
4787 || (INSN_LUID (bl
->biv
->insn
)
4788 > INSN_LUID (loop
->scan_start
))))
4789 || (INSN_LUID (v
->insn
) < INSN_LUID (loop
->scan_start
)
4790 && (INSN_LUID (loop
->scan_start
)
4791 < INSN_LUID (bl
->biv
->insn
))))
4800 /* We can't put an insn immediately after one setting
4801 cc0, or immediately before one using cc0. */
4802 if ((auto_inc_opt
== 1 && sets_cc0_p (PATTERN (v
->insn
)))
4803 || (auto_inc_opt
== -1
4804 && (prev
= prev_nonnote_insn (v
->insn
)) != 0
4806 && sets_cc0_p (PATTERN (prev
))))
4812 v
->auto_inc_opt
= 1;
4816 /* For each place where the biv is incremented, add an insn
4817 to increment the new, reduced reg for the giv. */
4818 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
4822 /* Skip if location is the same as a previous one. */
4826 insert_before
= NEXT_INSN (tv
->insn
);
4827 else if (auto_inc_opt
== 1)
4828 insert_before
= NEXT_INSN (v
->insn
);
4830 insert_before
= v
->insn
;
4832 if (tv
->mult_val
== const1_rtx
)
4833 loop_iv_add_mult_emit_before (loop
, tv
->add_val
, v
->mult_val
,
4834 v
->new_reg
, v
->new_reg
,
4836 else /* tv->mult_val == const0_rtx */
4837 /* A multiply is acceptable here
4838 since this is presumed to be seldom executed. */
4839 loop_iv_add_mult_emit_before (loop
, tv
->add_val
, v
->mult_val
,
4840 v
->add_val
, v
->new_reg
,
4844 /* Add code at loop start to initialize giv's reduced reg. */
4846 loop_iv_add_mult_hoist (loop
,
4847 extend_value_for_giv (v
, bl
->initial_value
),
4848 v
->mult_val
, v
->add_val
, v
->new_reg
);
4854 /* Check for givs whose first use is their definition and whose
4855 last use is the definition of another giv. If so, it is likely
4856 dead and should not be used to derive another giv nor to
4860 loop_givs_dead_check (struct loop
*loop ATTRIBUTE_UNUSED
, struct iv_class
*bl
)
4862 struct induction
*v
;
4864 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4867 || (v
->same
&& v
->same
->ignore
))
4870 if (v
->giv_type
== DEST_REG
4871 && REGNO_FIRST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v
->insn
))
4873 struct induction
*v1
;
4875 for (v1
= bl
->giv
; v1
; v1
= v1
->next_iv
)
4876 if (REGNO_LAST_UID (REGNO (v
->dest_reg
)) == INSN_UID (v1
->insn
))
4884 loop_givs_rescan (struct loop
*loop
, struct iv_class
*bl
, rtx
*reg_map
)
4886 struct induction
*v
;
4888 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
4890 if (v
->same
&& v
->same
->ignore
)
4896 /* Update expression if this was combined, in case other giv was
4899 v
->new_reg
= replace_rtx (v
->new_reg
,
4900 v
->same
->dest_reg
, v
->same
->new_reg
);
4902 /* See if this register is known to be a pointer to something. If
4903 so, see if we can find the alignment. First see if there is a
4904 destination register that is a pointer. If so, this shares the
4905 alignment too. Next see if we can deduce anything from the
4906 computational information. If not, and this is a DEST_ADDR
4907 giv, at least we know that it's a pointer, though we don't know
4909 if (GET_CODE (v
->new_reg
) == REG
4910 && v
->giv_type
== DEST_REG
4911 && REG_POINTER (v
->dest_reg
))
4912 mark_reg_pointer (v
->new_reg
,
4913 REGNO_POINTER_ALIGN (REGNO (v
->dest_reg
)));
4914 else if (GET_CODE (v
->new_reg
) == REG
4915 && REG_POINTER (v
->src_reg
))
4917 unsigned int align
= REGNO_POINTER_ALIGN (REGNO (v
->src_reg
));
4920 || GET_CODE (v
->add_val
) != CONST_INT
4921 || INTVAL (v
->add_val
) % (align
/ BITS_PER_UNIT
) != 0)
4924 mark_reg_pointer (v
->new_reg
, align
);
4926 else if (GET_CODE (v
->new_reg
) == REG
4927 && GET_CODE (v
->add_val
) == REG
4928 && REG_POINTER (v
->add_val
))
4930 unsigned int align
= REGNO_POINTER_ALIGN (REGNO (v
->add_val
));
4932 if (align
== 0 || GET_CODE (v
->mult_val
) != CONST_INT
4933 || INTVAL (v
->mult_val
) % (align
/ BITS_PER_UNIT
) != 0)
4936 mark_reg_pointer (v
->new_reg
, align
);
4938 else if (GET_CODE (v
->new_reg
) == REG
&& v
->giv_type
== DEST_ADDR
)
4939 mark_reg_pointer (v
->new_reg
, 0);
4941 if (v
->giv_type
== DEST_ADDR
)
4942 /* Store reduced reg as the address in the memref where we found
4944 validate_change (v
->insn
, v
->location
, v
->new_reg
, 0);
4945 else if (v
->replaceable
)
4947 reg_map
[REGNO (v
->dest_reg
)] = v
->new_reg
;
4951 rtx original_insn
= v
->insn
;
4954 /* Not replaceable; emit an insn to set the original giv reg from
4955 the reduced giv, same as above. */
4956 v
->insn
= loop_insn_emit_after (loop
, 0, original_insn
,
4957 gen_move_insn (v
->dest_reg
,
4960 /* The original insn may have a REG_EQUAL note. This note is
4961 now incorrect and may result in invalid substitutions later.
4962 The original insn is dead, but may be part of a libcall
4963 sequence, which doesn't seem worth the bother of handling. */
4964 note
= find_reg_note (original_insn
, REG_EQUAL
, NULL_RTX
);
4966 remove_note (original_insn
, note
);
4969 /* When a loop is reversed, givs which depend on the reversed
4970 biv, and which are live outside the loop, must be set to their
4971 correct final value. This insn is only needed if the giv is
4972 not replaceable. The correct final value is the same as the
4973 value that the giv starts the reversed loop with. */
4974 if (bl
->reversed
&& ! v
->replaceable
)
4975 loop_iv_add_mult_sink (loop
,
4976 extend_value_for_giv (v
, bl
->initial_value
),
4977 v
->mult_val
, v
->add_val
, v
->dest_reg
);
4978 else if (v
->final_value
)
4979 loop_insn_sink_or_swim (loop
,
4980 gen_load_of_final_value (v
->dest_reg
,
4983 if (loop_dump_stream
)
4985 fprintf (loop_dump_stream
, "giv at %d reduced to ",
4986 INSN_UID (v
->insn
));
4987 print_simple_rtl (loop_dump_stream
, v
->new_reg
);
4988 fprintf (loop_dump_stream
, "\n");
4995 loop_giv_reduce_benefit (struct loop
*loop ATTRIBUTE_UNUSED
,
4996 struct iv_class
*bl
, struct induction
*v
,
5002 benefit
= v
->benefit
;
5003 PUT_MODE (test_reg
, v
->mode
);
5004 add_cost
= iv_add_mult_cost (bl
->biv
->add_val
, v
->mult_val
,
5005 test_reg
, test_reg
);
5007 /* Reduce benefit if not replaceable, since we will insert a
5008 move-insn to replace the insn that calculates this giv. Don't do
5009 this unless the giv is a user variable, since it will often be
5010 marked non-replaceable because of the duplication of the exit
5011 code outside the loop. In such a case, the copies we insert are
5012 dead and will be deleted. So they don't have a cost. Similar
5013 situations exist. */
5014 /* ??? The new final_[bg]iv_value code does a much better job of
5015 finding replaceable giv's, and hence this code may no longer be
5017 if (! v
->replaceable
&& ! bl
->eliminable
5018 && REG_USERVAR_P (v
->dest_reg
))
5019 benefit
-= copy_cost
;
5021 /* Decrease the benefit to count the add-insns that we will insert
5022 to increment the reduced reg for the giv. ??? This can
5023 overestimate the run-time cost of the additional insns, e.g. if
5024 there are multiple basic blocks that increment the biv, but only
5025 one of these blocks is executed during each iteration. There is
5026 no good way to detect cases like this with the current structure
5027 of the loop optimizer. This code is more accurate for
5028 determining code size than run-time benefits. */
5029 benefit
-= add_cost
* bl
->biv_count
;
5031 /* Decide whether to strength-reduce this giv or to leave the code
5032 unchanged (recompute it from the biv each time it is used). This
5033 decision can be made independently for each giv. */
5036 /* Attempt to guess whether autoincrement will handle some of the
5037 new add insns; if so, increase BENEFIT (undo the subtraction of
5038 add_cost that was done above). */
5039 if (v
->giv_type
== DEST_ADDR
5040 /* Increasing the benefit is risky, since this is only a guess.
5041 Avoid increasing register pressure in cases where there would
5042 be no other benefit from reducing this giv. */
5044 && GET_CODE (v
->mult_val
) == CONST_INT
)
5046 int size
= GET_MODE_SIZE (GET_MODE (v
->mem
));
5048 if (HAVE_POST_INCREMENT
5049 && INTVAL (v
->mult_val
) == size
)
5050 benefit
+= add_cost
* bl
->biv_count
;
5051 else if (HAVE_PRE_INCREMENT
5052 && INTVAL (v
->mult_val
) == size
)
5053 benefit
+= add_cost
* bl
->biv_count
;
5054 else if (HAVE_POST_DECREMENT
5055 && -INTVAL (v
->mult_val
) == size
)
5056 benefit
+= add_cost
* bl
->biv_count
;
5057 else if (HAVE_PRE_DECREMENT
5058 && -INTVAL (v
->mult_val
) == size
)
5059 benefit
+= add_cost
* bl
->biv_count
;
5067 /* Free IV structures for LOOP. */
5070 loop_ivs_free (struct loop
*loop
)
5072 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5073 struct iv_class
*iv
= ivs
->list
;
5079 struct iv_class
*next
= iv
->next
;
5080 struct induction
*induction
;
5081 struct induction
*next_induction
;
5083 for (induction
= iv
->biv
; induction
; induction
= next_induction
)
5085 next_induction
= induction
->next_iv
;
5088 for (induction
= iv
->giv
; induction
; induction
= next_induction
)
5090 next_induction
= induction
->next_iv
;
5100 /* Perform strength reduction and induction variable elimination.
5102 Pseudo registers created during this function will be beyond the
5103 last valid index in several tables including
5104 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
5105 problem here, because the added registers cannot be givs outside of
5106 their loop, and hence will never be reconsidered. But scan_loop
5107 must check regnos to make sure they are in bounds. */
5110 strength_reduce (struct loop
*loop
, int flags
)
5112 struct loop_info
*loop_info
= LOOP_INFO (loop
);
5113 struct loop_regs
*regs
= LOOP_REGS (loop
);
5114 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5116 /* Temporary list pointer for traversing ivs->list. */
5117 struct iv_class
*bl
;
5118 /* Ratio of extra register life span we can justify
5119 for saving an instruction. More if loop doesn't call subroutines
5120 since in that case saving an insn makes more difference
5121 and more registers are available. */
5122 /* ??? could set this to last value of threshold in move_movables */
5123 int threshold
= (loop_info
->has_call
? 1 : 2) * (3 + n_non_fixed_regs
);
5124 /* Map of pseudo-register replacements. */
5125 rtx
*reg_map
= NULL
;
5127 int unrolled_insn_copies
= 0;
5128 rtx test_reg
= gen_rtx_REG (word_mode
, LAST_VIRTUAL_REGISTER
+ 1);
5129 int insn_count
= count_insns_in_loop (loop
);
5131 addr_placeholder
= gen_reg_rtx (Pmode
);
5133 ivs
->n_regs
= max_reg_before_loop
;
5134 ivs
->regs
= xcalloc (ivs
->n_regs
, sizeof (struct iv
));
5136 /* Find all BIVs in loop. */
5137 loop_bivs_find (loop
);
5139 /* Exit if there are no bivs. */
5142 /* Can still unroll the loop anyways, but indicate that there is no
5143 strength reduction info available. */
5144 if (flags
& LOOP_UNROLL
)
5145 unroll_loop (loop
, insn_count
, 0);
5147 loop_ivs_free (loop
);
5151 /* Determine how BIVS are initialized by looking through pre-header
5152 extended basic block. */
5153 loop_bivs_init_find (loop
);
5155 /* Look at the each biv and see if we can say anything better about its
5156 initial value from any initializing insns set up above. */
5157 loop_bivs_check (loop
);
5159 /* Search the loop for general induction variables. */
5160 loop_givs_find (loop
);
5162 /* Try to calculate and save the number of loop iterations. This is
5163 set to zero if the actual number can not be calculated. This must
5164 be called after all giv's have been identified, since otherwise it may
5165 fail if the iteration variable is a giv. */
5166 loop_iterations (loop
);
5168 #ifdef HAVE_prefetch
5169 if (flags
& LOOP_PREFETCH
)
5170 emit_prefetch_instructions (loop
);
5173 /* Now for each giv for which we still don't know whether or not it is
5174 replaceable, check to see if it is replaceable because its final value
5175 can be calculated. This must be done after loop_iterations is called,
5176 so that final_giv_value will work correctly. */
5177 loop_givs_check (loop
);
5179 /* Try to prove that the loop counter variable (if any) is always
5180 nonnegative; if so, record that fact with a REG_NONNEG note
5181 so that "decrement and branch until zero" insn can be used. */
5182 check_dbra_loop (loop
, insn_count
);
5184 /* Create reg_map to hold substitutions for replaceable giv regs.
5185 Some givs might have been made from biv increments, so look at
5186 ivs->reg_iv_type for a suitable size. */
5187 reg_map_size
= ivs
->n_regs
;
5188 reg_map
= xcalloc (reg_map_size
, sizeof (rtx
));
5190 /* Examine each iv class for feasibility of strength reduction/induction
5191 variable elimination. */
5193 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
5195 struct induction
*v
;
5198 /* Test whether it will be possible to eliminate this biv
5199 provided all givs are reduced. */
5200 bl
->eliminable
= loop_biv_eliminable_p (loop
, bl
, threshold
, insn_count
);
5202 /* This will be true at the end, if all givs which depend on this
5203 biv have been strength reduced.
5204 We can't (currently) eliminate the biv unless this is so. */
5205 bl
->all_reduced
= 1;
5207 /* Check each extension dependent giv in this class to see if its
5208 root biv is safe from wrapping in the interior mode. */
5209 check_ext_dependent_givs (loop
, bl
);
5211 /* Combine all giv's for this iv_class. */
5212 combine_givs (regs
, bl
);
5214 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5216 struct induction
*tv
;
5218 if (v
->ignore
|| v
->same
)
5221 benefit
= loop_giv_reduce_benefit (loop
, bl
, v
, test_reg
);
5223 /* If an insn is not to be strength reduced, then set its ignore
5224 flag, and clear bl->all_reduced. */
5226 /* A giv that depends on a reversed biv must be reduced if it is
5227 used after the loop exit, otherwise, it would have the wrong
5228 value after the loop exit. To make it simple, just reduce all
5229 of such giv's whether or not we know they are used after the loop
5232 if (! flag_reduce_all_givs
5233 && v
->lifetime
* threshold
* benefit
< insn_count
5236 if (loop_dump_stream
)
5237 fprintf (loop_dump_stream
,
5238 "giv of insn %d not worth while, %d vs %d.\n",
5240 v
->lifetime
* threshold
* benefit
, insn_count
);
5242 bl
->all_reduced
= 0;
5246 /* Check that we can increment the reduced giv without a
5247 multiply insn. If not, reject it. */
5249 for (tv
= bl
->biv
; tv
; tv
= tv
->next_iv
)
5250 if (tv
->mult_val
== const1_rtx
5251 && ! product_cheap_p (tv
->add_val
, v
->mult_val
))
5253 if (loop_dump_stream
)
5254 fprintf (loop_dump_stream
,
5255 "giv of insn %d: would need a multiply.\n",
5256 INSN_UID (v
->insn
));
5258 bl
->all_reduced
= 0;
5264 /* Check for givs whose first use is their definition and whose
5265 last use is the definition of another giv. If so, it is likely
5266 dead and should not be used to derive another giv nor to
5268 loop_givs_dead_check (loop
, bl
);
5270 /* Reduce each giv that we decided to reduce. */
5271 loop_givs_reduce (loop
, bl
);
5273 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
5276 For each giv register that can be reduced now: if replaceable,
5277 substitute reduced reg wherever the old giv occurs;
5278 else add new move insn "giv_reg = reduced_reg". */
5279 loop_givs_rescan (loop
, bl
, reg_map
);
5281 /* All the givs based on the biv bl have been reduced if they
5284 /* For each giv not marked as maybe dead that has been combined with a
5285 second giv, clear any "maybe dead" mark on that second giv.
5286 v->new_reg will either be or refer to the register of the giv it
5289 Doing this clearing avoids problems in biv elimination where
5290 a giv's new_reg is a complex value that can't be put in the
5291 insn but the giv combined with (with a reg as new_reg) is
5292 marked maybe_dead. Since the register will be used in either
5293 case, we'd prefer it be used from the simpler giv. */
5295 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
5296 if (! v
->maybe_dead
&& v
->same
)
5297 v
->same
->maybe_dead
= 0;
5299 /* Try to eliminate the biv, if it is a candidate.
5300 This won't work if ! bl->all_reduced,
5301 since the givs we planned to use might not have been reduced.
5303 We have to be careful that we didn't initially think we could
5304 eliminate this biv because of a giv that we now think may be
5305 dead and shouldn't be used as a biv replacement.
5307 Also, there is the possibility that we may have a giv that looks
5308 like it can be used to eliminate a biv, but the resulting insn
5309 isn't valid. This can happen, for example, on the 88k, where a
5310 JUMP_INSN can compare a register only with zero. Attempts to
5311 replace it with a compare with a constant will fail.
5313 Note that in cases where this call fails, we may have replaced some
5314 of the occurrences of the biv with a giv, but no harm was done in
5315 doing so in the rare cases where it can occur. */
5317 if (bl
->all_reduced
== 1 && bl
->eliminable
5318 && maybe_eliminate_biv (loop
, bl
, 1, threshold
, insn_count
))
5320 /* ?? If we created a new test to bypass the loop entirely,
5321 or otherwise drop straight in, based on this test, then
5322 we might want to rewrite it also. This way some later
5323 pass has more hope of removing the initialization of this
5326 /* If final_value != 0, then the biv may be used after loop end
5327 and we must emit an insn to set it just in case.
5329 Reversed bivs already have an insn after the loop setting their
5330 value, so we don't need another one. We can't calculate the
5331 proper final value for such a biv here anyways. */
5332 if (bl
->final_value
&& ! bl
->reversed
)
5333 loop_insn_sink_or_swim (loop
,
5334 gen_load_of_final_value (bl
->biv
->dest_reg
,
5337 if (loop_dump_stream
)
5338 fprintf (loop_dump_stream
, "Reg %d: biv eliminated\n",
5341 /* See above note wrt final_value. But since we couldn't eliminate
5342 the biv, we must set the value after the loop instead of before. */
5343 else if (bl
->final_value
&& ! bl
->reversed
)
5344 loop_insn_sink (loop
, gen_load_of_final_value (bl
->biv
->dest_reg
,
5348 /* Go through all the instructions in the loop, making all the
5349 register substitutions scheduled in REG_MAP. */
5351 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
5352 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
5353 || GET_CODE (p
) == CALL_INSN
)
5355 replace_regs (PATTERN (p
), reg_map
, reg_map_size
, 0);
5356 replace_regs (REG_NOTES (p
), reg_map
, reg_map_size
, 0);
5360 if (loop_info
->n_iterations
> 0)
5362 /* When we completely unroll a loop we will likely not need the increment
5363 of the loop BIV and we will not need the conditional branch at the
5365 unrolled_insn_copies
= insn_count
- 2;
5368 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5369 need the comparison before the conditional branch at the end of the
5371 unrolled_insn_copies
-= 1;
5374 /* We'll need one copy for each loop iteration. */
5375 unrolled_insn_copies
*= loop_info
->n_iterations
;
5377 /* A little slop to account for the ability to remove initialization
5378 code, better CSE, and other secondary benefits of completely
5379 unrolling some loops. */
5380 unrolled_insn_copies
-= 1;
5382 /* Clamp the value. */
5383 if (unrolled_insn_copies
< 0)
5384 unrolled_insn_copies
= 0;
5387 /* Unroll loops from within strength reduction so that we can use the
5388 induction variable information that strength_reduce has already
5389 collected. Always unroll loops that would be as small or smaller
5390 unrolled than when rolled. */
5391 if ((flags
& LOOP_UNROLL
)
5392 || ((flags
& LOOP_AUTO_UNROLL
)
5393 && loop_info
->n_iterations
> 0
5394 && unrolled_insn_copies
<= insn_count
))
5395 unroll_loop (loop
, insn_count
, 1);
5397 #ifdef HAVE_doloop_end
5398 if (HAVE_doloop_end
&& (flags
& LOOP_BCT
) && flag_branch_on_count_reg
)
5399 doloop_optimize (loop
);
5400 #endif /* HAVE_doloop_end */
5402 /* In case number of iterations is known, drop branch prediction note
5403 in the branch. Do that only in second loop pass, as loop unrolling
5404 may change the number of iterations performed. */
5405 if (flags
& LOOP_BCT
)
5407 unsigned HOST_WIDE_INT n
5408 = loop_info
->n_iterations
/ loop_info
->unroll_number
;
5410 predict_insn (prev_nonnote_insn (loop
->end
), PRED_LOOP_ITERATIONS
,
5411 REG_BR_PROB_BASE
- REG_BR_PROB_BASE
/ n
);
5414 if (loop_dump_stream
)
5415 fprintf (loop_dump_stream
, "\n");
5417 loop_ivs_free (loop
);
5422 /*Record all basic induction variables calculated in the insn. */
5424 check_insn_for_bivs (struct loop
*loop
, rtx p
, int not_every_iteration
,
5427 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5434 if (GET_CODE (p
) == INSN
5435 && (set
= single_set (p
))
5436 && GET_CODE (SET_DEST (set
)) == REG
)
5438 dest_reg
= SET_DEST (set
);
5439 if (REGNO (dest_reg
) < max_reg_before_loop
5440 && REGNO (dest_reg
) >= FIRST_PSEUDO_REGISTER
5441 && REG_IV_TYPE (ivs
, REGNO (dest_reg
)) != NOT_BASIC_INDUCT
)
5443 if (basic_induction_var (loop
, SET_SRC (set
),
5444 GET_MODE (SET_SRC (set
)),
5445 dest_reg
, p
, &inc_val
, &mult_val
,
5448 /* It is a possible basic induction variable.
5449 Create and initialize an induction structure for it. */
5451 struct induction
*v
= xmalloc (sizeof (struct induction
));
5453 record_biv (loop
, v
, p
, dest_reg
, inc_val
, mult_val
, location
,
5454 not_every_iteration
, maybe_multiple
);
5455 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = BASIC_INDUCT
;
5457 else if (REGNO (dest_reg
) < ivs
->n_regs
)
5458 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = NOT_BASIC_INDUCT
;
5464 /* Record all givs calculated in the insn.
5465 A register is a giv if: it is only set once, it is a function of a
5466 biv and a constant (or invariant), and it is not a biv. */
5468 check_insn_for_givs (struct loop
*loop
, rtx p
, int not_every_iteration
,
5471 struct loop_regs
*regs
= LOOP_REGS (loop
);
5474 /* Look for a general induction variable in a register. */
5475 if (GET_CODE (p
) == INSN
5476 && (set
= single_set (p
))
5477 && GET_CODE (SET_DEST (set
)) == REG
5478 && ! regs
->array
[REGNO (SET_DEST (set
))].may_not_optimize
)
5487 rtx last_consec_insn
;
5489 dest_reg
= SET_DEST (set
);
5490 if (REGNO (dest_reg
) < FIRST_PSEUDO_REGISTER
)
5493 if (/* SET_SRC is a giv. */
5494 (general_induction_var (loop
, SET_SRC (set
), &src_reg
, &add_val
,
5495 &mult_val
, &ext_val
, 0, &benefit
, VOIDmode
)
5496 /* Equivalent expression is a giv. */
5497 || ((regnote
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
5498 && general_induction_var (loop
, XEXP (regnote
, 0), &src_reg
,
5499 &add_val
, &mult_val
, &ext_val
, 0,
5500 &benefit
, VOIDmode
)))
5501 /* Don't try to handle any regs made by loop optimization.
5502 We have nothing on them in regno_first_uid, etc. */
5503 && REGNO (dest_reg
) < max_reg_before_loop
5504 /* Don't recognize a BASIC_INDUCT_VAR here. */
5505 && dest_reg
!= src_reg
5506 /* This must be the only place where the register is set. */
5507 && (regs
->array
[REGNO (dest_reg
)].n_times_set
== 1
5508 /* or all sets must be consecutive and make a giv. */
5509 || (benefit
= consec_sets_giv (loop
, benefit
, p
,
5511 &add_val
, &mult_val
, &ext_val
,
5512 &last_consec_insn
))))
5514 struct induction
*v
= xmalloc (sizeof (struct induction
));
5516 /* If this is a library call, increase benefit. */
5517 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
5518 benefit
+= libcall_benefit (p
);
5520 /* Skip the consecutive insns, if there are any. */
5521 if (regs
->array
[REGNO (dest_reg
)].n_times_set
!= 1)
5522 p
= last_consec_insn
;
5524 record_giv (loop
, v
, p
, src_reg
, dest_reg
, mult_val
, add_val
,
5525 ext_val
, benefit
, DEST_REG
, not_every_iteration
,
5526 maybe_multiple
, (rtx
*) 0);
5531 /* Look for givs which are memory addresses. */
5532 if (GET_CODE (p
) == INSN
)
5533 find_mem_givs (loop
, PATTERN (p
), p
, not_every_iteration
,
5536 /* Update the status of whether giv can derive other givs. This can
5537 change when we pass a label or an insn that updates a biv. */
5538 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
5539 || GET_CODE (p
) == CODE_LABEL
)
5540 update_giv_derive (loop
, p
);
5544 /* Return 1 if X is a valid source for an initial value (or as value being
5545 compared against in an initial test).
5547 X must be either a register or constant and must not be clobbered between
5548 the current insn and the start of the loop.
5550 INSN is the insn containing X. */
5553 valid_initial_value_p (rtx x
, rtx insn
, int call_seen
, rtx loop_start
)
5558 /* Only consider pseudos we know about initialized in insns whose luids
5560 if (GET_CODE (x
) != REG
5561 || REGNO (x
) >= max_reg_before_loop
)
5564 /* Don't use call-clobbered registers across a call which clobbers it. On
5565 some machines, don't use any hard registers at all. */
5566 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
5567 && (SMALL_REGISTER_CLASSES
5568 || (call_used_regs
[REGNO (x
)] && call_seen
)))
5571 /* Don't use registers that have been clobbered before the start of the
5573 if (reg_set_between_p (x
, insn
, loop_start
))
5579 /* Scan X for memory refs and check each memory address
5580 as a possible giv. INSN is the insn whose pattern X comes from.
5581 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5582 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5583 more than once in each loop iteration. */
5586 find_mem_givs (const struct loop
*loop
, rtx x
, rtx insn
,
5587 int not_every_iteration
, int maybe_multiple
)
5596 code
= GET_CODE (x
);
5621 /* This code used to disable creating GIVs with mult_val == 1 and
5622 add_val == 0. However, this leads to lost optimizations when
5623 it comes time to combine a set of related DEST_ADDR GIVs, since
5624 this one would not be seen. */
5626 if (general_induction_var (loop
, XEXP (x
, 0), &src_reg
, &add_val
,
5627 &mult_val
, &ext_val
, 1, &benefit
,
5630 /* Found one; record it. */
5631 struct induction
*v
= xmalloc (sizeof (struct induction
));
5633 record_giv (loop
, v
, insn
, src_reg
, addr_placeholder
, mult_val
,
5634 add_val
, ext_val
, benefit
, DEST_ADDR
,
5635 not_every_iteration
, maybe_multiple
, &XEXP (x
, 0));
5646 /* Recursively scan the subexpressions for other mem refs. */
5648 fmt
= GET_RTX_FORMAT (code
);
5649 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
5651 find_mem_givs (loop
, XEXP (x
, i
), insn
, not_every_iteration
,
5653 else if (fmt
[i
] == 'E')
5654 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
5655 find_mem_givs (loop
, XVECEXP (x
, i
, j
), insn
, not_every_iteration
,
5659 /* Fill in the data about one biv update.
5660 V is the `struct induction' in which we record the biv. (It is
5661 allocated by the caller, with alloca.)
5662 INSN is the insn that sets it.
5663 DEST_REG is the biv's reg.
5665 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5666 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5667 being set to INC_VAL.
5669 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5670 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5671 can be executed more than once per iteration. If MAYBE_MULTIPLE
5672 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5673 executed exactly once per iteration. */
5676 record_biv (struct loop
*loop
, struct induction
*v
, rtx insn
, rtx dest_reg
,
5677 rtx inc_val
, rtx mult_val
, rtx
*location
,
5678 int not_every_iteration
, int maybe_multiple
)
5680 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5681 struct iv_class
*bl
;
5684 v
->src_reg
= dest_reg
;
5685 v
->dest_reg
= dest_reg
;
5686 v
->mult_val
= mult_val
;
5687 v
->add_val
= inc_val
;
5688 v
->ext_dependent
= NULL_RTX
;
5689 v
->location
= location
;
5690 v
->mode
= GET_MODE (dest_reg
);
5691 v
->always_computable
= ! not_every_iteration
;
5692 v
->always_executed
= ! not_every_iteration
;
5693 v
->maybe_multiple
= maybe_multiple
;
5696 /* Add this to the reg's iv_class, creating a class
5697 if this is the first incrementation of the reg. */
5699 bl
= REG_IV_CLASS (ivs
, REGNO (dest_reg
));
5702 /* Create and initialize new iv_class. */
5704 bl
= xmalloc (sizeof (struct iv_class
));
5706 bl
->regno
= REGNO (dest_reg
);
5712 /* Set initial value to the reg itself. */
5713 bl
->initial_value
= dest_reg
;
5714 bl
->final_value
= 0;
5715 /* We haven't seen the initializing insn yet. */
5718 bl
->initial_test
= 0;
5719 bl
->incremented
= 0;
5723 bl
->total_benefit
= 0;
5725 /* Add this class to ivs->list. */
5726 bl
->next
= ivs
->list
;
5729 /* Put it in the array of biv register classes. */
5730 REG_IV_CLASS (ivs
, REGNO (dest_reg
)) = bl
;
5734 /* Check if location is the same as a previous one. */
5735 struct induction
*induction
;
5736 for (induction
= bl
->biv
; induction
; induction
= induction
->next_iv
)
5737 if (location
== induction
->location
)
5739 v
->same
= induction
;
5744 /* Update IV_CLASS entry for this biv. */
5745 v
->next_iv
= bl
->biv
;
5748 if (mult_val
== const1_rtx
)
5749 bl
->incremented
= 1;
5751 if (loop_dump_stream
)
5752 loop_biv_dump (v
, loop_dump_stream
, 0);
5755 /* Fill in the data about one giv.
5756 V is the `struct induction' in which we record the giv. (It is
5757 allocated by the caller, with alloca.)
5758 INSN is the insn that sets it.
5759 BENEFIT estimates the savings from deleting this insn.
5760 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5761 into a register or is used as a memory address.
5763 SRC_REG is the biv reg which the giv is computed from.
5764 DEST_REG is the giv's reg (if the giv is stored in a reg).
5765 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5766 LOCATION points to the place where this giv's value appears in INSN. */
5769 record_giv (const struct loop
*loop
, struct induction
*v
, rtx insn
,
5770 rtx src_reg
, rtx dest_reg
, rtx mult_val
, rtx add_val
,
5771 rtx ext_val
, int benefit
, enum g_types type
,
5772 int not_every_iteration
, int maybe_multiple
, rtx
*location
)
5774 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
5775 struct induction
*b
;
5776 struct iv_class
*bl
;
5777 rtx set
= single_set (insn
);
5780 /* Attempt to prove constantness of the values. Don't let simplify_rtx
5781 undo the MULT canonicalization that we performed earlier. */
5782 temp
= simplify_rtx (add_val
);
5784 && ! (GET_CODE (add_val
) == MULT
5785 && GET_CODE (temp
) == ASHIFT
))
5789 v
->src_reg
= src_reg
;
5791 v
->dest_reg
= dest_reg
;
5792 v
->mult_val
= mult_val
;
5793 v
->add_val
= add_val
;
5794 v
->ext_dependent
= ext_val
;
5795 v
->benefit
= benefit
;
5796 v
->location
= location
;
5798 v
->combined_with
= 0;
5799 v
->maybe_multiple
= maybe_multiple
;
5801 v
->derive_adjustment
= 0;
5807 v
->auto_inc_opt
= 0;
5811 /* The v->always_computable field is used in update_giv_derive, to
5812 determine whether a giv can be used to derive another giv. For a
5813 DEST_REG giv, INSN computes a new value for the giv, so its value
5814 isn't computable if INSN insn't executed every iteration.
5815 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5816 it does not compute a new value. Hence the value is always computable
5817 regardless of whether INSN is executed each iteration. */
5819 if (type
== DEST_ADDR
)
5820 v
->always_computable
= 1;
5822 v
->always_computable
= ! not_every_iteration
;
5824 v
->always_executed
= ! not_every_iteration
;
5826 if (type
== DEST_ADDR
)
5828 v
->mode
= GET_MODE (*location
);
5831 else /* type == DEST_REG */
5833 v
->mode
= GET_MODE (SET_DEST (set
));
5835 v
->lifetime
= LOOP_REG_LIFETIME (loop
, REGNO (dest_reg
));
5837 /* If the lifetime is zero, it means that this register is
5838 really a dead store. So mark this as a giv that can be
5839 ignored. This will not prevent the biv from being eliminated. */
5840 if (v
->lifetime
== 0)
5843 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = GENERAL_INDUCT
;
5844 REG_IV_INFO (ivs
, REGNO (dest_reg
)) = v
;
5847 /* Add the giv to the class of givs computed from one biv. */
5849 bl
= REG_IV_CLASS (ivs
, REGNO (src_reg
));
5852 v
->next_iv
= bl
->giv
;
5854 /* Don't count DEST_ADDR. This is supposed to count the number of
5855 insns that calculate givs. */
5856 if (type
== DEST_REG
)
5858 bl
->total_benefit
+= benefit
;
5861 /* Fatal error, biv missing for this giv? */
5864 if (type
== DEST_ADDR
)
5867 v
->not_replaceable
= 0;
5871 /* The giv can be replaced outright by the reduced register only if all
5872 of the following conditions are true:
5873 - the insn that sets the giv is always executed on any iteration
5874 on which the giv is used at all
5875 (there are two ways to deduce this:
5876 either the insn is executed on every iteration,
5877 or all uses follow that insn in the same basic block),
5878 - the giv is not used outside the loop
5879 - no assignments to the biv occur during the giv's lifetime. */
5881 if (REGNO_FIRST_UID (REGNO (dest_reg
)) == INSN_UID (insn
)
5882 /* Previous line always fails if INSN was moved by loop opt. */
5883 && REGNO_LAST_LUID (REGNO (dest_reg
))
5884 < INSN_LUID (loop
->end
)
5885 && (! not_every_iteration
5886 || last_use_this_basic_block (dest_reg
, insn
)))
5888 /* Now check that there are no assignments to the biv within the
5889 giv's lifetime. This requires two separate checks. */
5891 /* Check each biv update, and fail if any are between the first
5892 and last use of the giv.
5894 If this loop contains an inner loop that was unrolled, then
5895 the insn modifying the biv may have been emitted by the loop
5896 unrolling code, and hence does not have a valid luid. Just
5897 mark the biv as not replaceable in this case. It is not very
5898 useful as a biv, because it is used in two different loops.
5899 It is very unlikely that we would be able to optimize the giv
5900 using this biv anyways. */
5903 v
->not_replaceable
= 0;
5904 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
5906 if (INSN_UID (b
->insn
) >= max_uid_for_loop
5907 || ((INSN_LUID (b
->insn
)
5908 >= REGNO_FIRST_LUID (REGNO (dest_reg
)))
5909 && (INSN_LUID (b
->insn
)
5910 <= REGNO_LAST_LUID (REGNO (dest_reg
)))))
5913 v
->not_replaceable
= 1;
5918 /* If there are any backwards branches that go from after the
5919 biv update to before it, then this giv is not replaceable. */
5921 for (b
= bl
->biv
; b
; b
= b
->next_iv
)
5922 if (back_branch_in_range_p (loop
, b
->insn
))
5925 v
->not_replaceable
= 1;
5931 /* May still be replaceable, we don't have enough info here to
5934 v
->not_replaceable
= 0;
5938 /* Record whether the add_val contains a const_int, for later use by
5943 v
->no_const_addval
= 1;
5944 if (tem
== const0_rtx
)
5946 else if (CONSTANT_P (add_val
))
5947 v
->no_const_addval
= 0;
5948 if (GET_CODE (tem
) == PLUS
)
5952 if (GET_CODE (XEXP (tem
, 0)) == PLUS
)
5953 tem
= XEXP (tem
, 0);
5954 else if (GET_CODE (XEXP (tem
, 1)) == PLUS
)
5955 tem
= XEXP (tem
, 1);
5959 if (CONSTANT_P (XEXP (tem
, 1)))
5960 v
->no_const_addval
= 0;
5964 if (loop_dump_stream
)
5965 loop_giv_dump (v
, loop_dump_stream
, 0);
5968 /* All this does is determine whether a giv can be made replaceable because
5969 its final value can be calculated. This code can not be part of record_giv
5970 above, because final_giv_value requires that the number of loop iterations
5971 be known, and that can not be accurately calculated until after all givs
5972 have been identified. */
5975 check_final_value (const struct loop
*loop
, struct induction
*v
)
5977 rtx final_value
= 0;
5979 /* DEST_ADDR givs will never reach here, because they are always marked
5980 replaceable above in record_giv. */
5982 /* The giv can be replaced outright by the reduced register only if all
5983 of the following conditions are true:
5984 - the insn that sets the giv is always executed on any iteration
5985 on which the giv is used at all
5986 (there are two ways to deduce this:
5987 either the insn is executed on every iteration,
5988 or all uses follow that insn in the same basic block),
5989 - its final value can be calculated (this condition is different
5990 than the one above in record_giv)
5991 - it's not used before the it's set
5992 - no assignments to the biv occur during the giv's lifetime. */
5995 /* This is only called now when replaceable is known to be false. */
5996 /* Clear replaceable, so that it won't confuse final_giv_value. */
6000 if ((final_value
= final_giv_value (loop
, v
))
6001 && (v
->always_executed
6002 || last_use_this_basic_block (v
->dest_reg
, v
->insn
)))
6004 int biv_increment_seen
= 0, before_giv_insn
= 0;
6009 v
->not_replaceable
= 0;
6011 /* When trying to determine whether or not a biv increment occurs
6012 during the lifetime of the giv, we can ignore uses of the variable
6013 outside the loop because final_value is true. Hence we can not
6014 use regno_last_uid and regno_first_uid as above in record_giv. */
6016 /* Search the loop to determine whether any assignments to the
6017 biv occur during the giv's lifetime. Start with the insn
6018 that sets the giv, and search around the loop until we come
6019 back to that insn again.
6021 Also fail if there is a jump within the giv's lifetime that jumps
6022 to somewhere outside the lifetime but still within the loop. This
6023 catches spaghetti code where the execution order is not linear, and
6024 hence the above test fails. Here we assume that the giv lifetime
6025 does not extend from one iteration of the loop to the next, so as
6026 to make the test easier. Since the lifetime isn't known yet,
6027 this requires two loops. See also record_giv above. */
6029 last_giv_use
= v
->insn
;
6036 before_giv_insn
= 1;
6037 p
= NEXT_INSN (loop
->start
);
6042 if (GET_CODE (p
) == INSN
|| GET_CODE (p
) == JUMP_INSN
6043 || GET_CODE (p
) == CALL_INSN
)
6045 /* It is possible for the BIV increment to use the GIV if we
6046 have a cycle. Thus we must be sure to check each insn for
6047 both BIV and GIV uses, and we must check for BIV uses
6050 if (! biv_increment_seen
6051 && reg_set_p (v
->src_reg
, PATTERN (p
)))
6052 biv_increment_seen
= 1;
6054 if (reg_mentioned_p (v
->dest_reg
, PATTERN (p
)))
6056 if (biv_increment_seen
|| before_giv_insn
)
6059 v
->not_replaceable
= 1;
6067 /* Now that the lifetime of the giv is known, check for branches
6068 from within the lifetime to outside the lifetime if it is still
6078 p
= NEXT_INSN (loop
->start
);
6079 if (p
== last_giv_use
)
6082 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
)
6083 && LABEL_NAME (JUMP_LABEL (p
))
6084 && ((loop_insn_first_p (JUMP_LABEL (p
), v
->insn
)
6085 && loop_insn_first_p (loop
->start
, JUMP_LABEL (p
)))
6086 || (loop_insn_first_p (last_giv_use
, JUMP_LABEL (p
))
6087 && loop_insn_first_p (JUMP_LABEL (p
), loop
->end
))))
6090 v
->not_replaceable
= 1;
6092 if (loop_dump_stream
)
6093 fprintf (loop_dump_stream
,
6094 "Found branch outside giv lifetime.\n");
6101 /* If it is replaceable, then save the final value. */
6103 v
->final_value
= final_value
;
6106 if (loop_dump_stream
&& v
->replaceable
)
6107 fprintf (loop_dump_stream
, "Insn %d: giv reg %d final_value replaceable\n",
6108 INSN_UID (v
->insn
), REGNO (v
->dest_reg
));
6111 /* Update the status of whether a giv can derive other givs.
6113 We need to do something special if there is or may be an update to the biv
6114 between the time the giv is defined and the time it is used to derive
6117 In addition, a giv that is only conditionally set is not allowed to
6118 derive another giv once a label has been passed.
6120 The cases we look at are when a label or an update to a biv is passed. */
6123 update_giv_derive (const struct loop
*loop
, rtx p
)
6125 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6126 struct iv_class
*bl
;
6127 struct induction
*biv
, *giv
;
6131 /* Search all IV classes, then all bivs, and finally all givs.
6133 There are three cases we are concerned with. First we have the situation
6134 of a giv that is only updated conditionally. In that case, it may not
6135 derive any givs after a label is passed.
6137 The second case is when a biv update occurs, or may occur, after the
6138 definition of a giv. For certain biv updates (see below) that are
6139 known to occur between the giv definition and use, we can adjust the
6140 giv definition. For others, or when the biv update is conditional,
6141 we must prevent the giv from deriving any other givs. There are two
6142 sub-cases within this case.
6144 If this is a label, we are concerned with any biv update that is done
6145 conditionally, since it may be done after the giv is defined followed by
6146 a branch here (actually, we need to pass both a jump and a label, but
6147 this extra tracking doesn't seem worth it).
6149 If this is a jump, we are concerned about any biv update that may be
6150 executed multiple times. We are actually only concerned about
6151 backward jumps, but it is probably not worth performing the test
6152 on the jump again here.
6154 If this is a biv update, we must adjust the giv status to show that a
6155 subsequent biv update was performed. If this adjustment cannot be done,
6156 the giv cannot derive further givs. */
6158 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
6159 for (biv
= bl
->biv
; biv
; biv
= biv
->next_iv
)
6160 if (GET_CODE (p
) == CODE_LABEL
|| GET_CODE (p
) == JUMP_INSN
6163 /* Skip if location is the same as a previous one. */
6167 for (giv
= bl
->giv
; giv
; giv
= giv
->next_iv
)
6169 /* If cant_derive is already true, there is no point in
6170 checking all of these conditions again. */
6171 if (giv
->cant_derive
)
6174 /* If this giv is conditionally set and we have passed a label,
6175 it cannot derive anything. */
6176 if (GET_CODE (p
) == CODE_LABEL
&& ! giv
->always_computable
)
6177 giv
->cant_derive
= 1;
6179 /* Skip givs that have mult_val == 0, since
6180 they are really invariants. Also skip those that are
6181 replaceable, since we know their lifetime doesn't contain
6183 else if (giv
->mult_val
== const0_rtx
|| giv
->replaceable
)
6186 /* The only way we can allow this giv to derive another
6187 is if this is a biv increment and we can form the product
6188 of biv->add_val and giv->mult_val. In this case, we will
6189 be able to compute a compensation. */
6190 else if (biv
->insn
== p
)
6195 if (biv
->mult_val
== const1_rtx
)
6196 tem
= simplify_giv_expr (loop
,
6197 gen_rtx_MULT (giv
->mode
,
6200 &ext_val_dummy
, &dummy
);
6202 if (tem
&& giv
->derive_adjustment
)
6203 tem
= simplify_giv_expr
6205 gen_rtx_PLUS (giv
->mode
, tem
, giv
->derive_adjustment
),
6206 &ext_val_dummy
, &dummy
);
6209 giv
->derive_adjustment
= tem
;
6211 giv
->cant_derive
= 1;
6213 else if ((GET_CODE (p
) == CODE_LABEL
&& ! biv
->always_computable
)
6214 || (GET_CODE (p
) == JUMP_INSN
&& biv
->maybe_multiple
))
6215 giv
->cant_derive
= 1;
6220 /* Check whether an insn is an increment legitimate for a basic induction var.
6221 X is the source of insn P, or a part of it.
6222 MODE is the mode in which X should be interpreted.
6224 DEST_REG is the putative biv, also the destination of the insn.
6225 We accept patterns of these forms:
6226 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6227 REG = INVARIANT + REG
6229 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6230 store the additive term into *INC_VAL, and store the place where
6231 we found the additive term into *LOCATION.
6233 If X is an assignment of an invariant into DEST_REG, we set
6234 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6236 We also want to detect a BIV when it corresponds to a variable
6237 whose mode was promoted. In that case, an increment
6238 of the variable may be a PLUS that adds a SUBREG of that variable to
6239 an invariant and then sign- or zero-extends the result of the PLUS
6242 Most GIVs in such cases will be in the promoted mode, since that is the
6243 probably the natural computation mode (and almost certainly the mode
6244 used for addresses) on the machine. So we view the pseudo-reg containing
6245 the variable as the BIV, as if it were simply incremented.
6247 Note that treating the entire pseudo as a BIV will result in making
6248 simple increments to any GIVs based on it. However, if the variable
6249 overflows in its declared mode but not its promoted mode, the result will
6250 be incorrect. This is acceptable if the variable is signed, since
6251 overflows in such cases are undefined, but not if it is unsigned, since
6252 those overflows are defined. So we only check for SIGN_EXTEND and
6255 If we cannot find a biv, we return 0. */
6258 basic_induction_var (const struct loop
*loop
, rtx x
, enum machine_mode mode
,
6259 rtx dest_reg
, rtx p
, rtx
*inc_val
, rtx
*mult_val
,
6264 rtx insn
, set
= 0, last
, inc
;
6266 code
= GET_CODE (x
);
6271 if (rtx_equal_p (XEXP (x
, 0), dest_reg
)
6272 || (GET_CODE (XEXP (x
, 0)) == SUBREG
6273 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 0))
6274 && SUBREG_REG (XEXP (x
, 0)) == dest_reg
))
6276 argp
= &XEXP (x
, 1);
6278 else if (rtx_equal_p (XEXP (x
, 1), dest_reg
)
6279 || (GET_CODE (XEXP (x
, 1)) == SUBREG
6280 && SUBREG_PROMOTED_VAR_P (XEXP (x
, 1))
6281 && SUBREG_REG (XEXP (x
, 1)) == dest_reg
))
6283 argp
= &XEXP (x
, 0);
6289 if (loop_invariant_p (loop
, arg
) != 1)
6292 /* convert_modes can emit new instructions, e.g. when arg is a loop
6293 invariant MEM and dest_reg has a different mode.
6294 These instructions would be emitted after the end of the function
6295 and then *inc_val would be an uninitialized pseudo.
6296 Detect this and bail in this case.
6297 Other alternatives to solve this can be introducing a convert_modes
6298 variant which is allowed to fail but not allowed to emit new
6299 instructions, emit these instructions before loop start and let
6300 it be garbage collected if *inc_val is never used or saving the
6301 *inc_val initialization sequence generated here and when *inc_val
6302 is going to be actually used, emit it at some suitable place. */
6303 last
= get_last_insn ();
6304 inc
= convert_modes (GET_MODE (dest_reg
), GET_MODE (x
), arg
, 0);
6305 if (get_last_insn () != last
)
6307 delete_insns_since (last
);
6312 *mult_val
= const1_rtx
;
6317 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
6318 handle addition of promoted variables.
6319 ??? The comment at the start of this function is wrong: promoted
6320 variable increments don't look like it says they do. */
6321 return basic_induction_var (loop
, SUBREG_REG (x
),
6322 GET_MODE (SUBREG_REG (x
)),
6323 dest_reg
, p
, inc_val
, mult_val
, location
);
6326 /* If this register is assigned in a previous insn, look at its
6327 source, but don't go outside the loop or past a label. */
6329 /* If this sets a register to itself, we would repeat any previous
6330 biv increment if we applied this strategy blindly. */
6331 if (rtx_equal_p (dest_reg
, x
))
6340 insn
= PREV_INSN (insn
);
6342 while (insn
&& GET_CODE (insn
) == NOTE
6343 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
6347 set
= single_set (insn
);
6350 dest
= SET_DEST (set
);
6352 || (GET_CODE (dest
) == SUBREG
6353 && (GET_MODE_SIZE (GET_MODE (dest
)) <= UNITS_PER_WORD
)
6354 && (GET_MODE_CLASS (GET_MODE (dest
)) == MODE_INT
)
6355 && SUBREG_REG (dest
) == x
))
6356 return basic_induction_var (loop
, SET_SRC (set
),
6357 (GET_MODE (SET_SRC (set
)) == VOIDmode
6359 : GET_MODE (SET_SRC (set
))),
6361 inc_val
, mult_val
, location
);
6363 while (GET_CODE (dest
) == SIGN_EXTRACT
6364 || GET_CODE (dest
) == ZERO_EXTRACT
6365 || GET_CODE (dest
) == SUBREG
6366 || GET_CODE (dest
) == STRICT_LOW_PART
)
6367 dest
= XEXP (dest
, 0);
6373 /* Can accept constant setting of biv only when inside inner most loop.
6374 Otherwise, a biv of an inner loop may be incorrectly recognized
6375 as a biv of the outer loop,
6376 causing code to be moved INTO the inner loop. */
6378 if (loop_invariant_p (loop
, x
) != 1)
6383 /* convert_modes aborts if we try to convert to or from CCmode, so just
6384 exclude that case. It is very unlikely that a condition code value
6385 would be a useful iterator anyways. convert_modes aborts if we try to
6386 convert a float mode to non-float or vice versa too. */
6387 if (loop
->level
== 1
6388 && GET_MODE_CLASS (mode
) == GET_MODE_CLASS (GET_MODE (dest_reg
))
6389 && GET_MODE_CLASS (mode
) != MODE_CC
)
6391 /* Possible bug here? Perhaps we don't know the mode of X. */
6392 last
= get_last_insn ();
6393 inc
= convert_modes (GET_MODE (dest_reg
), mode
, x
, 0);
6394 if (get_last_insn () != last
)
6396 delete_insns_since (last
);
6401 *mult_val
= const0_rtx
;
6408 /* Ignore this BIV if signed arithmetic overflow is defined. */
6411 return basic_induction_var (loop
, XEXP (x
, 0), GET_MODE (XEXP (x
, 0)),
6412 dest_reg
, p
, inc_val
, mult_val
, location
);
6415 /* Similar, since this can be a sign extension. */
6416 for (insn
= PREV_INSN (p
);
6417 (insn
&& GET_CODE (insn
) == NOTE
6418 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_LOOP_BEG
);
6419 insn
= PREV_INSN (insn
))
6423 set
= single_set (insn
);
6425 if (! rtx_equal_p (dest_reg
, XEXP (x
, 0))
6426 && set
&& SET_DEST (set
) == XEXP (x
, 0)
6427 && GET_CODE (XEXP (x
, 1)) == CONST_INT
6428 && INTVAL (XEXP (x
, 1)) >= 0
6429 && GET_CODE (SET_SRC (set
)) == ASHIFT
6430 && XEXP (x
, 1) == XEXP (SET_SRC (set
), 1))
6431 return basic_induction_var (loop
, XEXP (SET_SRC (set
), 0),
6432 GET_MODE (XEXP (x
, 0)),
6433 dest_reg
, insn
, inc_val
, mult_val
,
6442 /* A general induction variable (giv) is any quantity that is a linear
6443 function of a basic induction variable,
6444 i.e. giv = biv * mult_val + add_val.
6445 The coefficients can be any loop invariant quantity.
6446 A giv need not be computed directly from the biv;
6447 it can be computed by way of other givs. */
6449 /* Determine whether X computes a giv.
6450 If it does, return a nonzero value
6451 which is the benefit from eliminating the computation of X;
6452 set *SRC_REG to the register of the biv that it is computed from;
6453 set *ADD_VAL and *MULT_VAL to the coefficients,
6454 such that the value of X is biv * mult + add; */
6457 general_induction_var (const struct loop
*loop
, rtx x
, rtx
*src_reg
,
6458 rtx
*add_val
, rtx
*mult_val
, rtx
*ext_val
,
6459 int is_addr
, int *pbenefit
,
6460 enum machine_mode addr_mode
)
6462 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6465 /* If this is an invariant, forget it, it isn't a giv. */
6466 if (loop_invariant_p (loop
, x
) == 1)
6470 *ext_val
= NULL_RTX
;
6471 x
= simplify_giv_expr (loop
, x
, ext_val
, pbenefit
);
6475 switch (GET_CODE (x
))
6479 /* Since this is now an invariant and wasn't before, it must be a giv
6480 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6482 *src_reg
= ivs
->list
->biv
->dest_reg
;
6483 *mult_val
= const0_rtx
;
6488 /* This is equivalent to a BIV. */
6490 *mult_val
= const1_rtx
;
6491 *add_val
= const0_rtx
;
6495 /* Either (plus (biv) (invar)) or
6496 (plus (mult (biv) (invar_1)) (invar_2)). */
6497 if (GET_CODE (XEXP (x
, 0)) == MULT
)
6499 *src_reg
= XEXP (XEXP (x
, 0), 0);
6500 *mult_val
= XEXP (XEXP (x
, 0), 1);
6504 *src_reg
= XEXP (x
, 0);
6505 *mult_val
= const1_rtx
;
6507 *add_val
= XEXP (x
, 1);
6511 /* ADD_VAL is zero. */
6512 *src_reg
= XEXP (x
, 0);
6513 *mult_val
= XEXP (x
, 1);
6514 *add_val
= const0_rtx
;
6521 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6522 unless they are CONST_INT). */
6523 if (GET_CODE (*add_val
) == USE
)
6524 *add_val
= XEXP (*add_val
, 0);
6525 if (GET_CODE (*mult_val
) == USE
)
6526 *mult_val
= XEXP (*mult_val
, 0);
6529 *pbenefit
+= address_cost (orig_x
, addr_mode
) - reg_address_cost
;
6531 *pbenefit
+= rtx_cost (orig_x
, SET
);
6533 /* Always return true if this is a giv so it will be detected as such,
6534 even if the benefit is zero or negative. This allows elimination
6535 of bivs that might otherwise not be eliminated. */
6539 /* Given an expression, X, try to form it as a linear function of a biv.
6540 We will canonicalize it to be of the form
6541 (plus (mult (BIV) (invar_1))
6543 with possible degeneracies.
6545 The invariant expressions must each be of a form that can be used as a
6546 machine operand. We surround then with a USE rtx (a hack, but localized
6547 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6548 routine; it is the caller's responsibility to strip them.
6550 If no such canonicalization is possible (i.e., two biv's are used or an
6551 expression that is neither invariant nor a biv or giv), this routine
6554 For a nonzero return, the result will have a code of CONST_INT, USE,
6555 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6557 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6559 static rtx
sge_plus (enum machine_mode
, rtx
, rtx
);
6560 static rtx
sge_plus_constant (rtx
, rtx
);
6563 simplify_giv_expr (const struct loop
*loop
, rtx x
, rtx
*ext_val
, int *benefit
)
6565 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
6566 struct loop_regs
*regs
= LOOP_REGS (loop
);
6567 enum machine_mode mode
= GET_MODE (x
);
6571 /* If this is not an integer mode, or if we cannot do arithmetic in this
6572 mode, this can't be a giv. */
6573 if (mode
!= VOIDmode
6574 && (GET_MODE_CLASS (mode
) != MODE_INT
6575 || GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
))
6578 switch (GET_CODE (x
))
6581 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
6582 arg1
= simplify_giv_expr (loop
, XEXP (x
, 1), ext_val
, benefit
);
6583 if (arg0
== 0 || arg1
== 0)
6586 /* Put constant last, CONST_INT last if both constant. */
6587 if ((GET_CODE (arg0
) == USE
6588 || GET_CODE (arg0
) == CONST_INT
)
6589 && ! ((GET_CODE (arg0
) == USE
6590 && GET_CODE (arg1
) == USE
)
6591 || GET_CODE (arg1
) == CONST_INT
))
6592 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6594 /* Handle addition of zero, then addition of an invariant. */
6595 if (arg1
== const0_rtx
)
6597 else if (GET_CODE (arg1
) == CONST_INT
|| GET_CODE (arg1
) == USE
)
6598 switch (GET_CODE (arg0
))
6602 /* Adding two invariants must result in an invariant, so enclose
6603 addition operation inside a USE and return it. */
6604 if (GET_CODE (arg0
) == USE
)
6605 arg0
= XEXP (arg0
, 0);
6606 if (GET_CODE (arg1
) == USE
)
6607 arg1
= XEXP (arg1
, 0);
6609 if (GET_CODE (arg0
) == CONST_INT
)
6610 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6611 if (GET_CODE (arg1
) == CONST_INT
)
6612 tem
= sge_plus_constant (arg0
, arg1
);
6614 tem
= sge_plus (mode
, arg0
, arg1
);
6616 if (GET_CODE (tem
) != CONST_INT
)
6617 tem
= gen_rtx_USE (mode
, tem
);
6622 /* biv + invar or mult + invar. Return sum. */
6623 return gen_rtx_PLUS (mode
, arg0
, arg1
);
6626 /* (a + invar_1) + invar_2. Associate. */
6628 simplify_giv_expr (loop
,
6640 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6641 MULT to reduce cases. */
6642 if (GET_CODE (arg0
) == REG
)
6643 arg0
= gen_rtx_MULT (mode
, arg0
, const1_rtx
);
6644 if (GET_CODE (arg1
) == REG
)
6645 arg1
= gen_rtx_MULT (mode
, arg1
, const1_rtx
);
6647 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6648 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6649 Recurse to associate the second PLUS. */
6650 if (GET_CODE (arg1
) == MULT
)
6651 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6653 if (GET_CODE (arg1
) == PLUS
)
6655 simplify_giv_expr (loop
,
6657 gen_rtx_PLUS (mode
, arg0
,
6662 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6663 if (GET_CODE (arg0
) != MULT
|| GET_CODE (arg1
) != MULT
)
6666 if (!rtx_equal_p (arg0
, arg1
))
6669 return simplify_giv_expr (loop
,
6678 /* Handle "a - b" as "a + b * (-1)". */
6679 return simplify_giv_expr (loop
,
6688 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
6689 arg1
= simplify_giv_expr (loop
, XEXP (x
, 1), ext_val
, benefit
);
6690 if (arg0
== 0 || arg1
== 0)
6693 /* Put constant last, CONST_INT last if both constant. */
6694 if ((GET_CODE (arg0
) == USE
|| GET_CODE (arg0
) == CONST_INT
)
6695 && GET_CODE (arg1
) != CONST_INT
)
6696 tem
= arg0
, arg0
= arg1
, arg1
= tem
;
6698 /* If second argument is not now constant, not giv. */
6699 if (GET_CODE (arg1
) != USE
&& GET_CODE (arg1
) != CONST_INT
)
6702 /* Handle multiply by 0 or 1. */
6703 if (arg1
== const0_rtx
)
6706 else if (arg1
== const1_rtx
)
6709 switch (GET_CODE (arg0
))
6712 /* biv * invar. Done. */
6713 return gen_rtx_MULT (mode
, arg0
, arg1
);
6716 /* Product of two constants. */
6717 return GEN_INT (INTVAL (arg0
) * INTVAL (arg1
));
6720 /* invar * invar is a giv, but attempt to simplify it somehow. */
6721 if (GET_CODE (arg1
) != CONST_INT
)
6724 arg0
= XEXP (arg0
, 0);
6725 if (GET_CODE (arg0
) == MULT
)
6727 /* (invar_0 * invar_1) * invar_2. Associate. */
6728 return simplify_giv_expr (loop
,
6737 /* Propagate the MULT expressions to the innermost nodes. */
6738 else if (GET_CODE (arg0
) == PLUS
)
6740 /* (invar_0 + invar_1) * invar_2. Distribute. */
6741 return simplify_giv_expr (loop
,
6753 return gen_rtx_USE (mode
, gen_rtx_MULT (mode
, arg0
, arg1
));
6756 /* (a * invar_1) * invar_2. Associate. */
6757 return simplify_giv_expr (loop
,
6766 /* (a + invar_1) * invar_2. Distribute. */
6767 return simplify_giv_expr (loop
,
6782 /* Shift by constant is multiply by power of two. */
6783 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
6787 simplify_giv_expr (loop
,
6790 GEN_INT ((HOST_WIDE_INT
) 1
6791 << INTVAL (XEXP (x
, 1)))),
6795 /* "-a" is "a * (-1)" */
6796 return simplify_giv_expr (loop
,
6797 gen_rtx_MULT (mode
, XEXP (x
, 0), constm1_rtx
),
6801 /* "~a" is "-a - 1". Silly, but easy. */
6802 return simplify_giv_expr (loop
,
6803 gen_rtx_MINUS (mode
,
6804 gen_rtx_NEG (mode
, XEXP (x
, 0)),
6809 /* Already in proper form for invariant. */
6815 /* Conditionally recognize extensions of simple IVs. After we've
6816 computed loop traversal counts and verified the range of the
6817 source IV, we'll reevaluate this as a GIV. */
6818 if (*ext_val
== NULL_RTX
)
6820 arg0
= simplify_giv_expr (loop
, XEXP (x
, 0), ext_val
, benefit
);
6821 if (arg0
&& *ext_val
== NULL_RTX
&& GET_CODE (arg0
) == REG
)
6823 *ext_val
= gen_rtx_fmt_e (GET_CODE (x
), mode
, arg0
);
6830 /* If this is a new register, we can't deal with it. */
6831 if (REGNO (x
) >= max_reg_before_loop
)
6834 /* Check for biv or giv. */
6835 switch (REG_IV_TYPE (ivs
, REGNO (x
)))
6839 case GENERAL_INDUCT
:
6841 struct induction
*v
= REG_IV_INFO (ivs
, REGNO (x
));
6843 /* Form expression from giv and add benefit. Ensure this giv
6844 can derive another and subtract any needed adjustment if so. */
6846 /* Increasing the benefit here is risky. The only case in which it
6847 is arguably correct is if this is the only use of V. In other
6848 cases, this will artificially inflate the benefit of the current
6849 giv, and lead to suboptimal code. Thus, it is disabled, since
6850 potentially not reducing an only marginally beneficial giv is
6851 less harmful than reducing many givs that are not really
6854 rtx single_use
= regs
->array
[REGNO (x
)].single_usage
;
6855 if (single_use
&& single_use
!= const0_rtx
)
6856 *benefit
+= v
->benefit
;
6862 tem
= gen_rtx_PLUS (mode
, gen_rtx_MULT (mode
,
6863 v
->src_reg
, v
->mult_val
),
6866 if (v
->derive_adjustment
)
6867 tem
= gen_rtx_MINUS (mode
, tem
, v
->derive_adjustment
);
6868 arg0
= simplify_giv_expr (loop
, tem
, ext_val
, benefit
);
6871 if (!v
->ext_dependent
)
6876 *ext_val
= v
->ext_dependent
;
6884 /* If it isn't an induction variable, and it is invariant, we
6885 may be able to simplify things further by looking through
6886 the bits we just moved outside the loop. */
6887 if (loop_invariant_p (loop
, x
) == 1)
6890 struct loop_movables
*movables
= LOOP_MOVABLES (loop
);
6892 for (m
= movables
->head
; m
; m
= m
->next
)
6893 if (rtx_equal_p (x
, m
->set_dest
))
6895 /* Ok, we found a match. Substitute and simplify. */
6897 /* If we match another movable, we must use that, as
6898 this one is going away. */
6900 return simplify_giv_expr (loop
, m
->match
->set_dest
,
6903 /* If consec is nonzero, this is a member of a group of
6904 instructions that were moved together. We handle this
6905 case only to the point of seeking to the last insn and
6906 looking for a REG_EQUAL. Fail if we don't find one. */
6913 tem
= NEXT_INSN (tem
);
6917 tem
= find_reg_note (tem
, REG_EQUAL
, NULL_RTX
);
6919 tem
= XEXP (tem
, 0);
6923 tem
= single_set (m
->insn
);
6925 tem
= SET_SRC (tem
);
6930 /* What we are most interested in is pointer
6931 arithmetic on invariants -- only take
6932 patterns we may be able to do something with. */
6933 if (GET_CODE (tem
) == PLUS
6934 || GET_CODE (tem
) == MULT
6935 || GET_CODE (tem
) == ASHIFT
6936 || GET_CODE (tem
) == CONST_INT
6937 || GET_CODE (tem
) == SYMBOL_REF
)
6939 tem
= simplify_giv_expr (loop
, tem
, ext_val
,
6944 else if (GET_CODE (tem
) == CONST
6945 && GET_CODE (XEXP (tem
, 0)) == PLUS
6946 && GET_CODE (XEXP (XEXP (tem
, 0), 0)) == SYMBOL_REF
6947 && GET_CODE (XEXP (XEXP (tem
, 0), 1)) == CONST_INT
)
6949 tem
= simplify_giv_expr (loop
, XEXP (tem
, 0),
6961 /* Fall through to general case. */
6963 /* If invariant, return as USE (unless CONST_INT).
6964 Otherwise, not giv. */
6965 if (GET_CODE (x
) == USE
)
6968 if (loop_invariant_p (loop
, x
) == 1)
6970 if (GET_CODE (x
) == CONST_INT
)
6972 if (GET_CODE (x
) == CONST
6973 && GET_CODE (XEXP (x
, 0)) == PLUS
6974 && GET_CODE (XEXP (XEXP (x
, 0), 0)) == SYMBOL_REF
6975 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
)
6977 return gen_rtx_USE (mode
, x
);
6984 /* This routine folds invariants such that there is only ever one
6985 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6988 sge_plus_constant (rtx x
, rtx c
)
6990 if (GET_CODE (x
) == CONST_INT
)
6991 return GEN_INT (INTVAL (x
) + INTVAL (c
));
6992 else if (GET_CODE (x
) != PLUS
)
6993 return gen_rtx_PLUS (GET_MODE (x
), x
, c
);
6994 else if (GET_CODE (XEXP (x
, 1)) == CONST_INT
)
6996 return gen_rtx_PLUS (GET_MODE (x
), XEXP (x
, 0),
6997 GEN_INT (INTVAL (XEXP (x
, 1)) + INTVAL (c
)));
6999 else if (GET_CODE (XEXP (x
, 0)) == PLUS
7000 || GET_CODE (XEXP (x
, 1)) != PLUS
)
7002 return gen_rtx_PLUS (GET_MODE (x
),
7003 sge_plus_constant (XEXP (x
, 0), c
), XEXP (x
, 1));
7007 return gen_rtx_PLUS (GET_MODE (x
),
7008 sge_plus_constant (XEXP (x
, 1), c
), XEXP (x
, 0));
7013 sge_plus (enum machine_mode mode
, rtx x
, rtx y
)
7015 while (GET_CODE (y
) == PLUS
)
7017 rtx a
= XEXP (y
, 0);
7018 if (GET_CODE (a
) == CONST_INT
)
7019 x
= sge_plus_constant (x
, a
);
7021 x
= gen_rtx_PLUS (mode
, x
, a
);
7024 if (GET_CODE (y
) == CONST_INT
)
7025 x
= sge_plus_constant (x
, y
);
7027 x
= gen_rtx_PLUS (mode
, x
, y
);
7031 /* Help detect a giv that is calculated by several consecutive insns;
7035 The caller has already identified the first insn P as having a giv as dest;
7036 we check that all other insns that set the same register follow
7037 immediately after P, that they alter nothing else,
7038 and that the result of the last is still a giv.
7040 The value is 0 if the reg set in P is not really a giv.
7041 Otherwise, the value is the amount gained by eliminating
7042 all the consecutive insns that compute the value.
7044 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
7045 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
7047 The coefficients of the ultimate giv value are stored in
7048 *MULT_VAL and *ADD_VAL. */
7051 consec_sets_giv (const struct loop
*loop
, int first_benefit
, rtx p
,
7052 rtx src_reg
, rtx dest_reg
, rtx
*add_val
, rtx
*mult_val
,
7053 rtx
*ext_val
, rtx
*last_consec_insn
)
7055 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
7056 struct loop_regs
*regs
= LOOP_REGS (loop
);
7063 /* Indicate that this is a giv so that we can update the value produced in
7064 each insn of the multi-insn sequence.
7066 This induction structure will be used only by the call to
7067 general_induction_var below, so we can allocate it on our stack.
7068 If this is a giv, our caller will replace the induct var entry with
7069 a new induction structure. */
7070 struct induction
*v
;
7072 if (REG_IV_TYPE (ivs
, REGNO (dest_reg
)) != UNKNOWN_INDUCT
)
7075 v
= alloca (sizeof (struct induction
));
7076 v
->src_reg
= src_reg
;
7077 v
->mult_val
= *mult_val
;
7078 v
->add_val
= *add_val
;
7079 v
->benefit
= first_benefit
;
7081 v
->derive_adjustment
= 0;
7082 v
->ext_dependent
= NULL_RTX
;
7084 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = GENERAL_INDUCT
;
7085 REG_IV_INFO (ivs
, REGNO (dest_reg
)) = v
;
7087 count
= regs
->array
[REGNO (dest_reg
)].n_times_set
- 1;
7092 code
= GET_CODE (p
);
7094 /* If libcall, skip to end of call sequence. */
7095 if (code
== INSN
&& (temp
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
)))
7099 && (set
= single_set (p
))
7100 && GET_CODE (SET_DEST (set
)) == REG
7101 && SET_DEST (set
) == dest_reg
7102 && (general_induction_var (loop
, SET_SRC (set
), &src_reg
,
7103 add_val
, mult_val
, ext_val
, 0,
7105 /* Giv created by equivalent expression. */
7106 || ((temp
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
))
7107 && general_induction_var (loop
, XEXP (temp
, 0), &src_reg
,
7108 add_val
, mult_val
, ext_val
, 0,
7109 &benefit
, VOIDmode
)))
7110 && src_reg
== v
->src_reg
)
7112 if (find_reg_note (p
, REG_RETVAL
, NULL_RTX
))
7113 benefit
+= libcall_benefit (p
);
7116 v
->mult_val
= *mult_val
;
7117 v
->add_val
= *add_val
;
7118 v
->benefit
+= benefit
;
7120 else if (code
!= NOTE
)
7122 /* Allow insns that set something other than this giv to a
7123 constant. Such insns are needed on machines which cannot
7124 include long constants and should not disqualify a giv. */
7126 && (set
= single_set (p
))
7127 && SET_DEST (set
) != dest_reg
7128 && CONSTANT_P (SET_SRC (set
)))
7131 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
7136 REG_IV_TYPE (ivs
, REGNO (dest_reg
)) = UNKNOWN_INDUCT
;
7137 *last_consec_insn
= p
;
7141 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7142 represented by G1. If no such expression can be found, or it is clear that
7143 it cannot possibly be a valid address, 0 is returned.
7145 To perform the computation, we note that
7148 where `v' is the biv.
7150 So G2 = (y/b) * G1 + (b - a*y/x).
7152 Note that MULT = y/x.
7154 Update: A and B are now allowed to be additive expressions such that
7155 B contains all variables in A. That is, computing B-A will not require
7156 subtracting variables. */
7159 express_from_1 (rtx a
, rtx b
, rtx mult
)
7161 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
7163 if (mult
== const0_rtx
)
7166 /* If MULT is not 1, we cannot handle A with non-constants, since we
7167 would then be required to subtract multiples of the registers in A.
7168 This is theoretically possible, and may even apply to some Fortran
7169 constructs, but it is a lot of work and we do not attempt it here. */
7171 if (mult
!= const1_rtx
&& GET_CODE (a
) != CONST_INT
)
7174 /* In general these structures are sorted top to bottom (down the PLUS
7175 chain), but not left to right across the PLUS. If B is a higher
7176 order giv than A, we can strip one level and recurse. If A is higher
7177 order, we'll eventually bail out, but won't know that until the end.
7178 If they are the same, we'll strip one level around this loop. */
7180 while (GET_CODE (a
) == PLUS
&& GET_CODE (b
) == PLUS
)
7182 rtx ra
, rb
, oa
, ob
, tmp
;
7184 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
7185 if (GET_CODE (ra
) == PLUS
)
7186 tmp
= ra
, ra
= oa
, oa
= tmp
;
7188 rb
= XEXP (b
, 0), ob
= XEXP (b
, 1);
7189 if (GET_CODE (rb
) == PLUS
)
7190 tmp
= rb
, rb
= ob
, ob
= tmp
;
7192 if (rtx_equal_p (ra
, rb
))
7193 /* We matched: remove one reg completely. */
7195 else if (GET_CODE (ob
) != PLUS
&& rtx_equal_p (ra
, ob
))
7196 /* An alternate match. */
7198 else if (GET_CODE (oa
) != PLUS
&& rtx_equal_p (oa
, rb
))
7199 /* An alternate match. */
7203 /* Indicates an extra register in B. Strip one level from B and
7204 recurse, hoping B was the higher order expression. */
7205 ob
= express_from_1 (a
, ob
, mult
);
7208 return gen_rtx_PLUS (GET_MODE (b
), rb
, ob
);
7212 /* Here we are at the last level of A, go through the cases hoping to
7213 get rid of everything but a constant. */
7215 if (GET_CODE (a
) == PLUS
)
7219 ra
= XEXP (a
, 0), oa
= XEXP (a
, 1);
7220 if (rtx_equal_p (oa
, b
))
7222 else if (!rtx_equal_p (ra
, b
))
7225 if (GET_CODE (oa
) != CONST_INT
)
7228 return GEN_INT (-INTVAL (oa
) * INTVAL (mult
));
7230 else if (GET_CODE (a
) == CONST_INT
)
7232 return plus_constant (b
, -INTVAL (a
) * INTVAL (mult
));
7234 else if (CONSTANT_P (a
))
7236 enum machine_mode mode_a
= GET_MODE (a
);
7237 enum machine_mode mode_b
= GET_MODE (b
);
7238 enum machine_mode mode
= mode_b
== VOIDmode
? mode_a
: mode_b
;
7239 return simplify_gen_binary (MINUS
, mode
, b
, a
);
7241 else if (GET_CODE (b
) == PLUS
)
7243 if (rtx_equal_p (a
, XEXP (b
, 0)))
7245 else if (rtx_equal_p (a
, XEXP (b
, 1)))
7250 else if (rtx_equal_p (a
, b
))
7257 express_from (struct induction
*g1
, struct induction
*g2
)
7261 /* The value that G1 will be multiplied by must be a constant integer. Also,
7262 the only chance we have of getting a valid address is if b*c/a (see above
7263 for notation) is also an integer. */
7264 if (GET_CODE (g1
->mult_val
) == CONST_INT
7265 && GET_CODE (g2
->mult_val
) == CONST_INT
)
7267 if (g1
->mult_val
== const0_rtx
7268 || (g1
->mult_val
== constm1_rtx
7269 && INTVAL (g2
->mult_val
)
7270 == (HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1))
7271 || INTVAL (g2
->mult_val
) % INTVAL (g1
->mult_val
) != 0)
7273 mult
= GEN_INT (INTVAL (g2
->mult_val
) / INTVAL (g1
->mult_val
));
7275 else if (rtx_equal_p (g1
->mult_val
, g2
->mult_val
))
7279 /* ??? Find out if the one is a multiple of the other? */
7283 add
= express_from_1 (g1
->add_val
, g2
->add_val
, mult
);
7284 if (add
== NULL_RTX
)
7286 /* Failed. If we've got a multiplication factor between G1 and G2,
7287 scale G1's addend and try again. */
7288 if (INTVAL (mult
) > 1)
7290 rtx g1_add_val
= g1
->add_val
;
7291 if (GET_CODE (g1_add_val
) == MULT
7292 && GET_CODE (XEXP (g1_add_val
, 1)) == CONST_INT
)
7295 m
= INTVAL (mult
) * INTVAL (XEXP (g1_add_val
, 1));
7296 g1_add_val
= gen_rtx_MULT (GET_MODE (g1_add_val
),
7297 XEXP (g1_add_val
, 0), GEN_INT (m
));
7301 g1_add_val
= gen_rtx_MULT (GET_MODE (g1_add_val
), g1_add_val
,
7305 add
= express_from_1 (g1_add_val
, g2
->add_val
, const1_rtx
);
7308 if (add
== NULL_RTX
)
7311 /* Form simplified final result. */
7312 if (mult
== const0_rtx
)
7314 else if (mult
== const1_rtx
)
7315 mult
= g1
->dest_reg
;
7317 mult
= gen_rtx_MULT (g2
->mode
, g1
->dest_reg
, mult
);
7319 if (add
== const0_rtx
)
7323 if (GET_CODE (add
) == PLUS
7324 && CONSTANT_P (XEXP (add
, 1)))
7326 rtx tem
= XEXP (add
, 1);
7327 mult
= gen_rtx_PLUS (g2
->mode
, mult
, XEXP (add
, 0));
7331 return gen_rtx_PLUS (g2
->mode
, mult
, add
);
7335 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7336 represented by G1. This indicates that G2 should be combined with G1 and
7337 that G2 can use (either directly or via an address expression) a register
7338 used to represent G1. */
7341 combine_givs_p (struct induction
*g1
, struct induction
*g2
)
7345 /* With the introduction of ext dependent givs, we must care for modes.
7346 G2 must not use a wider mode than G1. */
7347 if (GET_MODE_SIZE (g1
->mode
) < GET_MODE_SIZE (g2
->mode
))
7350 ret
= comb
= express_from (g1
, g2
);
7351 if (comb
== NULL_RTX
)
7353 if (g1
->mode
!= g2
->mode
)
7354 ret
= gen_lowpart (g2
->mode
, comb
);
7356 /* If these givs are identical, they can be combined. We use the results
7357 of express_from because the addends are not in a canonical form, so
7358 rtx_equal_p is a weaker test. */
7359 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7360 combination to be the other way round. */
7361 if (comb
== g1
->dest_reg
7362 && (g1
->giv_type
== DEST_REG
|| g2
->giv_type
== DEST_ADDR
))
7367 /* If G2 can be expressed as a function of G1 and that function is valid
7368 as an address and no more expensive than using a register for G2,
7369 the expression of G2 in terms of G1 can be used. */
7371 && g2
->giv_type
== DEST_ADDR
7372 && memory_address_p (GET_MODE (g2
->mem
), ret
))
7378 /* See if BL is monotonic and has a constant per-iteration increment.
7379 Return the increment if so, otherwise return 0. */
7381 static HOST_WIDE_INT
7382 get_monotonic_increment (struct iv_class
*bl
)
7384 struct induction
*v
;
7387 /* Get the total increment and check that it is constant. */
7388 incr
= biv_total_increment (bl
);
7389 if (incr
== 0 || GET_CODE (incr
) != CONST_INT
)
7392 for (v
= bl
->biv
; v
!= 0; v
= v
->next_iv
)
7394 if (GET_CODE (v
->add_val
) != CONST_INT
)
7397 if (INTVAL (v
->add_val
) < 0 && INTVAL (incr
) >= 0)
7400 if (INTVAL (v
->add_val
) > 0 && INTVAL (incr
) <= 0)
7403 return INTVAL (incr
);
7407 /* Subroutine of biv_fits_mode_p. Return true if biv BL, when biased by
7408 BIAS, will never exceed the unsigned range of MODE. LOOP is the loop
7409 to which the biv belongs and INCR is its per-iteration increment. */
7412 biased_biv_fits_mode_p (const struct loop
*loop
, struct iv_class
*bl
,
7413 HOST_WIDE_INT incr
, enum machine_mode mode
,
7414 unsigned HOST_WIDE_INT bias
)
7416 unsigned HOST_WIDE_INT initial
, maximum
, span
, delta
;
7418 /* We need to be able to manipulate MODE-size constants. */
7419 if (HOST_BITS_PER_WIDE_INT
< GET_MODE_BITSIZE (mode
))
7422 /* The number of loop iterations must be constant. */
7423 if (LOOP_INFO (loop
)->n_iterations
== 0)
7426 /* So must the biv's initial value. */
7427 if (bl
->initial_value
== 0 || GET_CODE (bl
->initial_value
) != CONST_INT
)
7430 initial
= bias
+ INTVAL (bl
->initial_value
);
7431 maximum
= GET_MODE_MASK (mode
);
7433 /* Make sure that the initial value is within range. */
7434 if (initial
> maximum
)
7437 /* Set up DELTA and SPAN such that the number of iterations * DELTA
7438 (calculated to arbitrary precision) must be <= SPAN. */
7447 /* Handle the special case in which MAXIMUM is the largest
7448 unsigned HOST_WIDE_INT and INITIAL is 0. */
7449 if (maximum
+ 1 == initial
)
7450 span
= LOOP_INFO (loop
)->n_iterations
* delta
;
7452 span
= maximum
+ 1 - initial
;
7454 return (span
/ LOOP_INFO (loop
)->n_iterations
>= delta
);
7458 /* Return true if biv BL will never exceed the bounds of MODE. LOOP is
7459 the loop to which BL belongs and INCR is its per-iteration increment.
7460 UNSIGNEDP is true if the biv should be treated as unsigned. */
7463 biv_fits_mode_p (const struct loop
*loop
, struct iv_class
*bl
,
7464 HOST_WIDE_INT incr
, enum machine_mode mode
, bool unsignedp
)
7466 struct loop_info
*loop_info
;
7467 unsigned HOST_WIDE_INT bias
;
7469 /* A biv's value will always be limited to its natural mode.
7470 Larger modes will observe the same wrap-around. */
7471 if (GET_MODE_SIZE (mode
) > GET_MODE_SIZE (GET_MODE (bl
->biv
->src_reg
)))
7472 mode
= GET_MODE (bl
->biv
->src_reg
);
7474 loop_info
= LOOP_INFO (loop
);
7476 bias
= (unsignedp
? 0 : (GET_MODE_MASK (mode
) >> 1) + 1);
7477 if (biased_biv_fits_mode_p (loop
, bl
, incr
, mode
, bias
))
7480 if (mode
== GET_MODE (bl
->biv
->src_reg
)
7481 && bl
->biv
->src_reg
== loop_info
->iteration_var
7482 && loop_info
->comparison_value
7483 && loop_invariant_p (loop
, loop_info
->comparison_value
))
7485 /* If the increment is +1, and the exit test is a <, the BIV
7486 cannot overflow. (For <=, we have the problematic case that
7487 the comparison value might be the maximum value of the range.) */
7490 if (loop_info
->comparison_code
== LT
)
7492 if (loop_info
->comparison_code
== LTU
&& unsignedp
)
7496 /* Likewise for increment -1 and exit test >. */
7499 if (loop_info
->comparison_code
== GT
)
7501 if (loop_info
->comparison_code
== GTU
&& unsignedp
)
7509 /* Given that X is an extension or truncation of BL, return true
7510 if it is unaffected by overflow. LOOP is the loop to which
7511 BL belongs and INCR is its per-iteration increment. */
7514 extension_within_bounds_p (const struct loop
*loop
, struct iv_class
*bl
,
7515 HOST_WIDE_INT incr
, rtx x
)
7517 enum machine_mode mode
;
7518 bool signedp
, unsignedp
;
7520 switch (GET_CODE (x
))
7524 mode
= GET_MODE (XEXP (x
, 0));
7525 signedp
= (GET_CODE (x
) == SIGN_EXTEND
);
7526 unsignedp
= (GET_CODE (x
) == ZERO_EXTEND
);
7530 /* We don't know whether this value is being used as signed
7531 or unsigned, so check the conditions for both. */
7532 mode
= GET_MODE (x
);
7533 signedp
= unsignedp
= true;
7540 return ((!signedp
|| biv_fits_mode_p (loop
, bl
, incr
, mode
, false))
7541 && (!unsignedp
|| biv_fits_mode_p (loop
, bl
, incr
, mode
, true)));
7545 /* Check each extension dependent giv in this class to see if its
7546 root biv is safe from wrapping in the interior mode, which would
7547 make the giv illegal. */
7550 check_ext_dependent_givs (const struct loop
*loop
, struct iv_class
*bl
)
7552 struct induction
*v
;
7555 incr
= get_monotonic_increment (bl
);
7557 /* Invalidate givs that fail the tests. */
7558 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
7559 if (v
->ext_dependent
)
7562 && extension_within_bounds_p (loop
, bl
, incr
, v
->ext_dependent
))
7564 if (loop_dump_stream
)
7565 fprintf (loop_dump_stream
,
7566 "Verified ext dependent giv at %d of reg %d\n",
7567 INSN_UID (v
->insn
), bl
->regno
);
7571 if (loop_dump_stream
)
7572 fprintf (loop_dump_stream
,
7573 "Failed ext dependent giv at %d\n",
7574 INSN_UID (v
->insn
));
7577 bl
->all_reduced
= 0;
7582 /* Generate a version of VALUE in a mode appropriate for initializing V. */
7585 extend_value_for_giv (struct induction
*v
, rtx value
)
7587 rtx ext_dep
= v
->ext_dependent
;
7592 /* Recall that check_ext_dependent_givs verified that the known bounds
7593 of a biv did not overflow or wrap with respect to the extension for
7594 the giv. Therefore, constants need no additional adjustment. */
7595 if (CONSTANT_P (value
) && GET_MODE (value
) == VOIDmode
)
7598 /* Otherwise, we must adjust the value to compensate for the
7599 differing modes of the biv and the giv. */
7600 return gen_rtx_fmt_e (GET_CODE (ext_dep
), GET_MODE (ext_dep
), value
);
7603 struct combine_givs_stats
7610 cmp_combine_givs_stats (const void *xp
, const void *yp
)
7612 const struct combine_givs_stats
* const x
=
7613 (const struct combine_givs_stats
*) xp
;
7614 const struct combine_givs_stats
* const y
=
7615 (const struct combine_givs_stats
*) yp
;
7617 d
= y
->total_benefit
- x
->total_benefit
;
7618 /* Stabilize the sort. */
7620 d
= x
->giv_number
- y
->giv_number
;
7624 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7625 any other. If so, point SAME to the giv combined with and set NEW_REG to
7626 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7627 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7630 combine_givs (struct loop_regs
*regs
, struct iv_class
*bl
)
7632 /* Additional benefit to add for being combined multiple times. */
7633 const int extra_benefit
= 3;
7635 struct induction
*g1
, *g2
, **giv_array
;
7636 int i
, j
, k
, giv_count
;
7637 struct combine_givs_stats
*stats
;
7640 /* Count givs, because bl->giv_count is incorrect here. */
7642 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
7646 giv_array
= alloca (giv_count
* sizeof (struct induction
*));
7648 for (g1
= bl
->giv
; g1
; g1
= g1
->next_iv
)
7650 giv_array
[i
++] = g1
;
7652 stats
= xcalloc (giv_count
, sizeof (*stats
));
7653 can_combine
= xcalloc (giv_count
, giv_count
* sizeof (rtx
));
7655 for (i
= 0; i
< giv_count
; i
++)
7661 stats
[i
].giv_number
= i
;
7663 /* If a DEST_REG GIV is used only once, do not allow it to combine
7664 with anything, for in doing so we will gain nothing that cannot
7665 be had by simply letting the GIV with which we would have combined
7666 to be reduced on its own. The losage shows up in particular with
7667 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7668 be seen elsewhere as well. */
7669 if (g1
->giv_type
== DEST_REG
7670 && (single_use
= regs
->array
[REGNO (g1
->dest_reg
)].single_usage
)
7671 && single_use
!= const0_rtx
)
7674 this_benefit
= g1
->benefit
;
7675 /* Add an additional weight for zero addends. */
7676 if (g1
->no_const_addval
)
7679 for (j
= 0; j
< giv_count
; j
++)
7685 && (this_combine
= combine_givs_p (g1
, g2
)) != NULL_RTX
)
7687 can_combine
[i
* giv_count
+ j
] = this_combine
;
7688 this_benefit
+= g2
->benefit
+ extra_benefit
;
7691 stats
[i
].total_benefit
= this_benefit
;
7694 /* Iterate, combining until we can't. */
7696 qsort (stats
, giv_count
, sizeof (*stats
), cmp_combine_givs_stats
);
7698 if (loop_dump_stream
)
7700 fprintf (loop_dump_stream
, "Sorted combine statistics:\n");
7701 for (k
= 0; k
< giv_count
; k
++)
7703 g1
= giv_array
[stats
[k
].giv_number
];
7704 if (!g1
->combined_with
&& !g1
->same
)
7705 fprintf (loop_dump_stream
, " {%d, %d}",
7706 INSN_UID (giv_array
[stats
[k
].giv_number
]->insn
),
7707 stats
[k
].total_benefit
);
7709 putc ('\n', loop_dump_stream
);
7712 for (k
= 0; k
< giv_count
; k
++)
7714 int g1_add_benefit
= 0;
7716 i
= stats
[k
].giv_number
;
7719 /* If it has already been combined, skip. */
7720 if (g1
->combined_with
|| g1
->same
)
7723 for (j
= 0; j
< giv_count
; j
++)
7726 if (g1
!= g2
&& can_combine
[i
* giv_count
+ j
]
7727 /* If it has already been combined, skip. */
7728 && ! g2
->same
&& ! g2
->combined_with
)
7732 g2
->new_reg
= can_combine
[i
* giv_count
+ j
];
7734 /* For destination, we now may replace by mem expression instead
7735 of register. This changes the costs considerably, so add the
7737 if (g2
->giv_type
== DEST_ADDR
)
7738 g2
->benefit
= (g2
->benefit
+ reg_address_cost
7739 - address_cost (g2
->new_reg
,
7740 GET_MODE (g2
->mem
)));
7741 g1
->combined_with
++;
7742 g1
->lifetime
+= g2
->lifetime
;
7744 g1_add_benefit
+= g2
->benefit
;
7746 /* ??? The new final_[bg]iv_value code does a much better job
7747 of finding replaceable giv's, and hence this code may no
7748 longer be necessary. */
7749 if (! g2
->replaceable
&& REG_USERVAR_P (g2
->dest_reg
))
7750 g1_add_benefit
-= copy_cost
;
7752 /* To help optimize the next set of combinations, remove
7753 this giv from the benefits of other potential mates. */
7754 for (l
= 0; l
< giv_count
; ++l
)
7756 int m
= stats
[l
].giv_number
;
7757 if (can_combine
[m
* giv_count
+ j
])
7758 stats
[l
].total_benefit
-= g2
->benefit
+ extra_benefit
;
7761 if (loop_dump_stream
)
7762 fprintf (loop_dump_stream
,
7763 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7764 INSN_UID (g2
->insn
), INSN_UID (g1
->insn
),
7765 g1
->benefit
, g1_add_benefit
, g1
->lifetime
);
7769 /* To help optimize the next set of combinations, remove
7770 this giv from the benefits of other potential mates. */
7771 if (g1
->combined_with
)
7773 for (j
= 0; j
< giv_count
; ++j
)
7775 int m
= stats
[j
].giv_number
;
7776 if (can_combine
[m
* giv_count
+ i
])
7777 stats
[j
].total_benefit
-= g1
->benefit
+ extra_benefit
;
7780 g1
->benefit
+= g1_add_benefit
;
7782 /* We've finished with this giv, and everything it touched.
7783 Restart the combination so that proper weights for the
7784 rest of the givs are properly taken into account. */
7785 /* ??? Ideally we would compact the arrays at this point, so
7786 as to not cover old ground. But sanely compacting
7787 can_combine is tricky. */
7797 /* Generate sequence for REG = B * M + A. B is the initial value of
7798 the basic induction variable, M a multiplicative constant, A an
7799 additive constant and REG the destination register. */
7802 gen_add_mult (rtx b
, rtx m
, rtx a
, rtx reg
)
7808 /* Use unsigned arithmetic. */
7809 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 1);
7811 emit_move_insn (reg
, result
);
7819 /* Update registers created in insn sequence SEQ. */
7822 loop_regs_update (const struct loop
*loop ATTRIBUTE_UNUSED
, rtx seq
)
7826 /* Update register info for alias analysis. */
7829 while (insn
!= NULL_RTX
)
7831 rtx set
= single_set (insn
);
7833 if (set
&& GET_CODE (SET_DEST (set
)) == REG
)
7834 record_base_value (REGNO (SET_DEST (set
)), SET_SRC (set
), 0);
7836 insn
= NEXT_INSN (insn
);
7841 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. B
7842 is the initial value of the basic induction variable, M a
7843 multiplicative constant, A an additive constant and REG the
7844 destination register. */
7847 loop_iv_add_mult_emit_before (const struct loop
*loop
, rtx b
, rtx m
, rtx a
,
7848 rtx reg
, basic_block before_bb
, rtx before_insn
)
7854 loop_iv_add_mult_hoist (loop
, b
, m
, a
, reg
);
7858 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7859 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
7861 /* Increase the lifetime of any invariants moved further in code. */
7862 update_reg_last_use (a
, before_insn
);
7863 update_reg_last_use (b
, before_insn
);
7864 update_reg_last_use (m
, before_insn
);
7866 /* It is possible that the expansion created lots of new registers.
7867 Iterate over the sequence we just created and record them all. We
7868 must do this before inserting the sequence. */
7869 loop_regs_update (loop
, seq
);
7871 loop_insn_emit_before (loop
, before_bb
, before_insn
, seq
);
7875 /* Emit insns in loop pre-header to set REG = B * M + A. B is the
7876 initial value of the basic induction variable, M a multiplicative
7877 constant, A an additive constant and REG the destination
7881 loop_iv_add_mult_sink (const struct loop
*loop
, rtx b
, rtx m
, rtx a
, rtx reg
)
7885 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7886 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
7888 /* Increase the lifetime of any invariants moved further in code.
7889 ???? Is this really necessary? */
7890 update_reg_last_use (a
, loop
->sink
);
7891 update_reg_last_use (b
, loop
->sink
);
7892 update_reg_last_use (m
, loop
->sink
);
7894 /* It is possible that the expansion created lots of new registers.
7895 Iterate over the sequence we just created and record them all. We
7896 must do this before inserting the sequence. */
7897 loop_regs_update (loop
, seq
);
7899 loop_insn_sink (loop
, seq
);
7903 /* Emit insns after loop to set REG = B * M + A. B is the initial
7904 value of the basic induction variable, M a multiplicative constant,
7905 A an additive constant and REG the destination register. */
7908 loop_iv_add_mult_hoist (const struct loop
*loop
, rtx b
, rtx m
, rtx a
, rtx reg
)
7912 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7913 seq
= gen_add_mult (copy_rtx (b
), copy_rtx (m
), copy_rtx (a
), reg
);
7915 /* It is possible that the expansion created lots of new registers.
7916 Iterate over the sequence we just created and record them all. We
7917 must do this before inserting the sequence. */
7918 loop_regs_update (loop
, seq
);
7920 loop_insn_hoist (loop
, seq
);
7925 /* Similar to gen_add_mult, but compute cost rather than generating
7929 iv_add_mult_cost (rtx b
, rtx m
, rtx a
, rtx reg
)
7935 result
= expand_mult_add (b
, reg
, m
, a
, GET_MODE (reg
), 1);
7937 emit_move_insn (reg
, result
);
7938 last
= get_last_insn ();
7941 rtx t
= single_set (last
);
7943 cost
+= rtx_cost (SET_SRC (t
), SET
);
7944 last
= PREV_INSN (last
);
7950 /* Test whether A * B can be computed without
7951 an actual multiply insn. Value is 1 if so.
7953 ??? This function stinks because it generates a ton of wasted RTL
7954 ??? and as a result fragments GC memory to no end. There are other
7955 ??? places in the compiler which are invoked a lot and do the same
7956 ??? thing, generate wasted RTL just to see if something is possible. */
7959 product_cheap_p (rtx a
, rtx b
)
7964 /* If only one is constant, make it B. */
7965 if (GET_CODE (a
) == CONST_INT
)
7966 tmp
= a
, a
= b
, b
= tmp
;
7968 /* If first constant, both constant, so don't need multiply. */
7969 if (GET_CODE (a
) == CONST_INT
)
7972 /* If second not constant, neither is constant, so would need multiply. */
7973 if (GET_CODE (b
) != CONST_INT
)
7976 /* One operand is constant, so might not need multiply insn. Generate the
7977 code for the multiply and see if a call or multiply, or long sequence
7978 of insns is generated. */
7981 expand_mult (GET_MODE (a
), a
, b
, NULL_RTX
, 1);
7989 while (tmp
!= NULL_RTX
)
7991 rtx next
= NEXT_INSN (tmp
);
7994 || GET_CODE (tmp
) != INSN
7995 || (GET_CODE (PATTERN (tmp
)) == SET
7996 && GET_CODE (SET_SRC (PATTERN (tmp
))) == MULT
)
7997 || (GET_CODE (PATTERN (tmp
)) == PARALLEL
7998 && GET_CODE (XVECEXP (PATTERN (tmp
), 0, 0)) == SET
7999 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp
), 0, 0))) == MULT
))
8008 else if (GET_CODE (tmp
) == SET
8009 && GET_CODE (SET_SRC (tmp
)) == MULT
)
8011 else if (GET_CODE (tmp
) == PARALLEL
8012 && GET_CODE (XVECEXP (tmp
, 0, 0)) == SET
8013 && GET_CODE (SET_SRC (XVECEXP (tmp
, 0, 0))) == MULT
)
8019 /* Check to see if loop can be terminated by a "decrement and branch until
8020 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
8021 Also try reversing an increment loop to a decrement loop
8022 to see if the optimization can be performed.
8023 Value is nonzero if optimization was performed. */
8025 /* This is useful even if the architecture doesn't have such an insn,
8026 because it might change a loops which increments from 0 to n to a loop
8027 which decrements from n to 0. A loop that decrements to zero is usually
8028 faster than one that increments from zero. */
8030 /* ??? This could be rewritten to use some of the loop unrolling procedures,
8031 such as approx_final_value, biv_total_increment, loop_iterations, and
8032 final_[bg]iv_value. */
8035 check_dbra_loop (struct loop
*loop
, int insn_count
)
8037 struct loop_info
*loop_info
= LOOP_INFO (loop
);
8038 struct loop_regs
*regs
= LOOP_REGS (loop
);
8039 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
8040 struct iv_class
*bl
;
8042 enum machine_mode mode
;
8048 rtx before_comparison
;
8052 int compare_and_branch
;
8053 rtx loop_start
= loop
->start
;
8054 rtx loop_end
= loop
->end
;
8056 /* If last insn is a conditional branch, and the insn before tests a
8057 register value, try to optimize it. Otherwise, we can't do anything. */
8059 jump
= PREV_INSN (loop_end
);
8060 comparison
= get_condition_for_loop (loop
, jump
);
8061 if (comparison
== 0)
8063 if (!onlyjump_p (jump
))
8066 /* Try to compute whether the compare/branch at the loop end is one or
8067 two instructions. */
8068 get_condition (jump
, &first_compare
, false);
8069 if (first_compare
== jump
)
8070 compare_and_branch
= 1;
8071 else if (first_compare
== prev_nonnote_insn (jump
))
8072 compare_and_branch
= 2;
8077 /* If more than one condition is present to control the loop, then
8078 do not proceed, as this function does not know how to rewrite
8079 loop tests with more than one condition.
8081 Look backwards from the first insn in the last comparison
8082 sequence and see if we've got another comparison sequence. */
8085 if ((jump1
= prev_nonnote_insn (first_compare
)) != loop
->cont
)
8086 if (GET_CODE (jump1
) == JUMP_INSN
)
8090 /* Check all of the bivs to see if the compare uses one of them.
8091 Skip biv's set more than once because we can't guarantee that
8092 it will be zero on the last iteration. Also skip if the biv is
8093 used between its update and the test insn. */
8095 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
8097 if (bl
->biv_count
== 1
8098 && ! bl
->biv
->maybe_multiple
8099 && bl
->biv
->dest_reg
== XEXP (comparison
, 0)
8100 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
8105 /* Try swapping the comparison to identify a suitable biv. */
8107 for (bl
= ivs
->list
; bl
; bl
= bl
->next
)
8108 if (bl
->biv_count
== 1
8109 && ! bl
->biv
->maybe_multiple
8110 && bl
->biv
->dest_reg
== XEXP (comparison
, 1)
8111 && ! reg_used_between_p (regno_reg_rtx
[bl
->regno
], bl
->biv
->insn
,
8114 comparison
= gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison
)),
8116 XEXP (comparison
, 1),
8117 XEXP (comparison
, 0));
8124 /* Look for the case where the basic induction variable is always
8125 nonnegative, and equals zero on the last iteration.
8126 In this case, add a reg_note REG_NONNEG, which allows the
8127 m68k DBRA instruction to be used. */
8129 if (((GET_CODE (comparison
) == GT
&& XEXP (comparison
, 1) == constm1_rtx
)
8130 || (GET_CODE (comparison
) == NE
&& XEXP (comparison
, 1) == const0_rtx
))
8131 && GET_CODE (bl
->biv
->add_val
) == CONST_INT
8132 && INTVAL (bl
->biv
->add_val
) < 0)
8134 /* Initial value must be greater than 0,
8135 init_val % -dec_value == 0 to ensure that it equals zero on
8136 the last iteration */
8138 if (GET_CODE (bl
->initial_value
) == CONST_INT
8139 && INTVAL (bl
->initial_value
) > 0
8140 && (INTVAL (bl
->initial_value
)
8141 % (-INTVAL (bl
->biv
->add_val
))) == 0)
8143 /* Register always nonnegative, add REG_NOTE to branch. */
8144 if (! find_reg_note (jump
, REG_NONNEG
, NULL_RTX
))
8146 = gen_rtx_EXPR_LIST (REG_NONNEG
, bl
->biv
->dest_reg
,
8153 /* If the decrement is 1 and the value was tested as >= 0 before
8154 the loop, then we can safely optimize. */
8155 for (p
= loop_start
; p
; p
= PREV_INSN (p
))
8157 if (GET_CODE (p
) == CODE_LABEL
)
8159 if (GET_CODE (p
) != JUMP_INSN
)
8162 before_comparison
= get_condition_for_loop (loop
, p
);
8163 if (before_comparison
8164 && XEXP (before_comparison
, 0) == bl
->biv
->dest_reg
8165 && (GET_CODE (before_comparison
) == LT
8166 || GET_CODE (before_comparison
) == LTU
)
8167 && XEXP (before_comparison
, 1) == const0_rtx
8168 && ! reg_set_between_p (bl
->biv
->dest_reg
, p
, loop_start
)
8169 && INTVAL (bl
->biv
->add_val
) == -1)
8171 if (! find_reg_note (jump
, REG_NONNEG
, NULL_RTX
))
8173 = gen_rtx_EXPR_LIST (REG_NONNEG
, bl
->biv
->dest_reg
,
8181 else if (GET_CODE (bl
->biv
->add_val
) == CONST_INT
8182 && INTVAL (bl
->biv
->add_val
) > 0)
8184 /* Try to change inc to dec, so can apply above optimization. */
8186 all registers modified are induction variables or invariant,
8187 all memory references have non-overlapping addresses
8188 (obviously true if only one write)
8189 allow 2 insns for the compare/jump at the end of the loop. */
8190 /* Also, we must avoid any instructions which use both the reversed
8191 biv and another biv. Such instructions will fail if the loop is
8192 reversed. We meet this condition by requiring that either
8193 no_use_except_counting is true, or else that there is only
8195 int num_nonfixed_reads
= 0;
8196 /* 1 if the iteration var is used only to count iterations. */
8197 int no_use_except_counting
= 0;
8198 /* 1 if the loop has no memory store, or it has a single memory store
8199 which is reversible. */
8200 int reversible_mem_store
= 1;
8202 if (bl
->giv_count
== 0
8203 && !loop
->exit_count
8204 && !loop_info
->has_multiple_exit_targets
)
8206 rtx bivreg
= regno_reg_rtx
[bl
->regno
];
8207 struct iv_class
*blt
;
8209 /* If there are no givs for this biv, and the only exit is the
8210 fall through at the end of the loop, then
8211 see if perhaps there are no uses except to count. */
8212 no_use_except_counting
= 1;
8213 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
8216 rtx set
= single_set (p
);
8218 if (set
&& GET_CODE (SET_DEST (set
)) == REG
8219 && REGNO (SET_DEST (set
)) == bl
->regno
)
8220 /* An insn that sets the biv is okay. */
8222 else if (!reg_mentioned_p (bivreg
, PATTERN (p
)))
8223 /* An insn that doesn't mention the biv is okay. */
8225 else if (p
== prev_nonnote_insn (prev_nonnote_insn (loop_end
))
8226 || p
== prev_nonnote_insn (loop_end
))
8228 /* If either of these insns uses the biv and sets a pseudo
8229 that has more than one usage, then the biv has uses
8230 other than counting since it's used to derive a value
8231 that is used more than one time. */
8232 note_stores (PATTERN (p
), note_set_pseudo_multiple_uses
,
8234 if (regs
->multiple_uses
)
8236 no_use_except_counting
= 0;
8242 no_use_except_counting
= 0;
8247 /* A biv has uses besides counting if it is used to set
8249 for (blt
= ivs
->list
; blt
; blt
= blt
->next
)
8251 && reg_mentioned_p (bivreg
, SET_SRC (blt
->init_set
)))
8253 no_use_except_counting
= 0;
8258 if (no_use_except_counting
)
8259 /* No need to worry about MEMs. */
8261 else if (loop_info
->num_mem_sets
<= 1)
8263 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
8265 num_nonfixed_reads
+= count_nonfixed_reads (loop
, PATTERN (p
));
8267 /* If the loop has a single store, and the destination address is
8268 invariant, then we can't reverse the loop, because this address
8269 might then have the wrong value at loop exit.
8270 This would work if the source was invariant also, however, in that
8271 case, the insn should have been moved out of the loop. */
8273 if (loop_info
->num_mem_sets
== 1)
8275 struct induction
*v
;
8277 /* If we could prove that each of the memory locations
8278 written to was different, then we could reverse the
8279 store -- but we don't presently have any way of
8281 reversible_mem_store
= 0;
8283 /* If the store depends on a register that is set after the
8284 store, it depends on the initial value, and is thus not
8286 for (v
= bl
->giv
; reversible_mem_store
&& v
; v
= v
->next_iv
)
8288 if (v
->giv_type
== DEST_REG
8289 && reg_mentioned_p (v
->dest_reg
,
8290 PATTERN (loop_info
->first_loop_store_insn
))
8291 && loop_insn_first_p (loop_info
->first_loop_store_insn
,
8293 reversible_mem_store
= 0;
8300 /* This code only acts for innermost loops. Also it simplifies
8301 the memory address check by only reversing loops with
8302 zero or one memory access.
8303 Two memory accesses could involve parts of the same array,
8304 and that can't be reversed.
8305 If the biv is used only for counting, than we don't need to worry
8306 about all these things. */
8308 if ((num_nonfixed_reads
<= 1
8309 && ! loop_info
->has_nonconst_call
8310 && ! loop_info
->has_prefetch
8311 && ! loop_info
->has_volatile
8312 && reversible_mem_store
8313 && (bl
->giv_count
+ bl
->biv_count
+ loop_info
->num_mem_sets
8314 + num_unmoved_movables (loop
) + compare_and_branch
== insn_count
)
8315 && (bl
== ivs
->list
&& bl
->next
== 0))
8316 || (no_use_except_counting
&& ! loop_info
->has_prefetch
))
8320 /* Loop can be reversed. */
8321 if (loop_dump_stream
)
8322 fprintf (loop_dump_stream
, "Can reverse loop\n");
8324 /* Now check other conditions:
8326 The increment must be a constant, as must the initial value,
8327 and the comparison code must be LT.
8329 This test can probably be improved since +/- 1 in the constant
8330 can be obtained by changing LT to LE and vice versa; this is
8334 /* for constants, LE gets turned into LT */
8335 && (GET_CODE (comparison
) == LT
8336 || (GET_CODE (comparison
) == LE
8337 && no_use_except_counting
)
8338 || GET_CODE (comparison
) == LTU
))
8340 HOST_WIDE_INT add_val
, add_adjust
, comparison_val
= 0;
8341 rtx initial_value
, comparison_value
;
8343 enum rtx_code cmp_code
;
8344 int comparison_const_width
;
8345 unsigned HOST_WIDE_INT comparison_sign_mask
;
8346 bool keep_first_compare
;
8348 add_val
= INTVAL (bl
->biv
->add_val
);
8349 comparison_value
= XEXP (comparison
, 1);
8350 if (GET_MODE (comparison_value
) == VOIDmode
)
8351 comparison_const_width
8352 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison
, 0)));
8354 comparison_const_width
8355 = GET_MODE_BITSIZE (GET_MODE (comparison_value
));
8356 if (comparison_const_width
> HOST_BITS_PER_WIDE_INT
)
8357 comparison_const_width
= HOST_BITS_PER_WIDE_INT
;
8358 comparison_sign_mask
8359 = (unsigned HOST_WIDE_INT
) 1 << (comparison_const_width
- 1);
8361 /* If the comparison value is not a loop invariant, then we
8362 can not reverse this loop.
8364 ??? If the insns which initialize the comparison value as
8365 a whole compute an invariant result, then we could move
8366 them out of the loop and proceed with loop reversal. */
8367 if (! loop_invariant_p (loop
, comparison_value
))
8370 if (GET_CODE (comparison_value
) == CONST_INT
)
8371 comparison_val
= INTVAL (comparison_value
);
8372 initial_value
= bl
->initial_value
;
8374 /* Normalize the initial value if it is an integer and
8375 has no other use except as a counter. This will allow
8376 a few more loops to be reversed. */
8377 if (no_use_except_counting
8378 && GET_CODE (comparison_value
) == CONST_INT
8379 && GET_CODE (initial_value
) == CONST_INT
)
8381 comparison_val
= comparison_val
- INTVAL (bl
->initial_value
);
8382 /* The code below requires comparison_val to be a multiple
8383 of add_val in order to do the loop reversal, so
8384 round up comparison_val to a multiple of add_val.
8385 Since comparison_value is constant, we know that the
8386 current comparison code is LT. */
8387 comparison_val
= comparison_val
+ add_val
- 1;
8389 -= (unsigned HOST_WIDE_INT
) comparison_val
% add_val
;
8390 /* We postpone overflow checks for COMPARISON_VAL here;
8391 even if there is an overflow, we might still be able to
8392 reverse the loop, if converting the loop exit test to
8394 initial_value
= const0_rtx
;
8397 /* First check if we can do a vanilla loop reversal. */
8398 if (initial_value
== const0_rtx
8399 && GET_CODE (comparison_value
) == CONST_INT
8400 /* Now do postponed overflow checks on COMPARISON_VAL. */
8401 && ! (((comparison_val
- add_val
) ^ INTVAL (comparison_value
))
8402 & comparison_sign_mask
))
8404 /* Register will always be nonnegative, with value
8405 0 on last iteration */
8406 add_adjust
= add_val
;
8413 if (GET_CODE (comparison
) == LE
)
8414 add_adjust
-= add_val
;
8416 /* If the initial value is not zero, or if the comparison
8417 value is not an exact multiple of the increment, then we
8418 can not reverse this loop. */
8419 if (initial_value
== const0_rtx
8420 && GET_CODE (comparison_value
) == CONST_INT
)
8422 if (((unsigned HOST_WIDE_INT
) comparison_val
% add_val
) != 0)
8427 if (! no_use_except_counting
|| add_val
!= 1)
8431 final_value
= comparison_value
;
8433 /* Reset these in case we normalized the initial value
8434 and comparison value above. */
8435 if (GET_CODE (comparison_value
) == CONST_INT
8436 && GET_CODE (initial_value
) == CONST_INT
)
8438 comparison_value
= GEN_INT (comparison_val
);
8440 = GEN_INT (comparison_val
+ INTVAL (bl
->initial_value
));
8442 bl
->initial_value
= initial_value
;
8444 /* Save some info needed to produce the new insns. */
8445 reg
= bl
->biv
->dest_reg
;
8446 mode
= GET_MODE (reg
);
8447 jump_label
= condjump_label (PREV_INSN (loop_end
));
8448 new_add_val
= GEN_INT (-INTVAL (bl
->biv
->add_val
));
8450 /* Set start_value; if this is not a CONST_INT, we need
8452 Initialize biv to start_value before loop start.
8453 The old initializing insn will be deleted as a
8454 dead store by flow.c. */
8455 if (initial_value
== const0_rtx
8456 && GET_CODE (comparison_value
) == CONST_INT
)
8459 = gen_int_mode (comparison_val
- add_adjust
, mode
);
8460 loop_insn_hoist (loop
, gen_move_insn (reg
, start_value
));
8462 else if (GET_CODE (initial_value
) == CONST_INT
)
8464 rtx offset
= GEN_INT (-INTVAL (initial_value
) - add_adjust
);
8465 rtx add_insn
= gen_add3_insn (reg
, comparison_value
, offset
);
8471 = gen_rtx_PLUS (mode
, comparison_value
, offset
);
8472 loop_insn_hoist (loop
, add_insn
);
8473 if (GET_CODE (comparison
) == LE
)
8474 final_value
= gen_rtx_PLUS (mode
, comparison_value
,
8477 else if (! add_adjust
)
8479 rtx sub_insn
= gen_sub3_insn (reg
, comparison_value
,
8485 = gen_rtx_MINUS (mode
, comparison_value
, initial_value
);
8486 loop_insn_hoist (loop
, sub_insn
);
8489 /* We could handle the other cases too, but it'll be
8490 better to have a testcase first. */
8493 /* We may not have a single insn which can increment a reg, so
8494 create a sequence to hold all the insns from expand_inc. */
8496 expand_inc (reg
, new_add_val
);
8500 p
= loop_insn_emit_before (loop
, 0, bl
->biv
->insn
, tem
);
8501 delete_insn (bl
->biv
->insn
);
8503 /* Update biv info to reflect its new status. */
8505 bl
->initial_value
= start_value
;
8506 bl
->biv
->add_val
= new_add_val
;
8508 /* Update loop info. */
8509 loop_info
->initial_value
= reg
;
8510 loop_info
->initial_equiv_value
= reg
;
8511 loop_info
->final_value
= const0_rtx
;
8512 loop_info
->final_equiv_value
= const0_rtx
;
8513 loop_info
->comparison_value
= const0_rtx
;
8514 loop_info
->comparison_code
= cmp_code
;
8515 loop_info
->increment
= new_add_val
;
8517 /* Inc LABEL_NUSES so that delete_insn will
8518 not delete the label. */
8519 LABEL_NUSES (XEXP (jump_label
, 0))++;
8521 /* If we have a separate comparison insn that does more
8522 than just set cc0, the result of the comparison might
8523 be used outside the loop. */
8524 keep_first_compare
= (compare_and_branch
== 2
8526 && sets_cc0_p (first_compare
) <= 0
8530 /* Emit an insn after the end of the loop to set the biv's
8531 proper exit value if it is used anywhere outside the loop. */
8532 if (keep_first_compare
8533 || (REGNO_LAST_UID (bl
->regno
) != INSN_UID (first_compare
))
8535 || REGNO_FIRST_UID (bl
->regno
) != INSN_UID (bl
->init_insn
))
8536 loop_insn_sink (loop
, gen_load_of_final_value (reg
, final_value
));
8538 if (keep_first_compare
)
8539 loop_insn_sink (loop
, PATTERN (first_compare
));
8541 /* Delete compare/branch at end of loop. */
8542 delete_related_insns (PREV_INSN (loop_end
));
8543 if (compare_and_branch
== 2)
8544 delete_related_insns (first_compare
);
8546 /* Add new compare/branch insn at end of loop. */
8548 emit_cmp_and_jump_insns (reg
, const0_rtx
, cmp_code
, NULL_RTX
,
8550 XEXP (jump_label
, 0));
8553 emit_jump_insn_before (tem
, loop_end
);
8555 for (tem
= PREV_INSN (loop_end
);
8556 tem
&& GET_CODE (tem
) != JUMP_INSN
;
8557 tem
= PREV_INSN (tem
))
8561 JUMP_LABEL (tem
) = XEXP (jump_label
, 0);
8567 /* Increment of LABEL_NUSES done above. */
8568 /* Register is now always nonnegative,
8569 so add REG_NONNEG note to the branch. */
8570 REG_NOTES (tem
) = gen_rtx_EXPR_LIST (REG_NONNEG
, reg
,
8576 /* No insn may reference both the reversed and another biv or it
8577 will fail (see comment near the top of the loop reversal
8579 Earlier on, we have verified that the biv has no use except
8580 counting, or it is the only biv in this function.
8581 However, the code that computes no_use_except_counting does
8582 not verify reg notes. It's possible to have an insn that
8583 references another biv, and has a REG_EQUAL note with an
8584 expression based on the reversed biv. To avoid this case,
8585 remove all REG_EQUAL notes based on the reversed biv
8587 for (p
= loop_start
; p
!= loop_end
; p
= NEXT_INSN (p
))
8591 rtx set
= single_set (p
);
8592 /* If this is a set of a GIV based on the reversed biv, any
8593 REG_EQUAL notes should still be correct. */
8595 || GET_CODE (SET_DEST (set
)) != REG
8596 || (size_t) REGNO (SET_DEST (set
)) >= ivs
->n_regs
8597 || REG_IV_TYPE (ivs
, REGNO (SET_DEST (set
))) != GENERAL_INDUCT
8598 || REG_IV_INFO (ivs
, REGNO (SET_DEST (set
)))->src_reg
!= bl
->biv
->src_reg
)
8599 for (pnote
= ®_NOTES (p
); *pnote
;)
8601 if (REG_NOTE_KIND (*pnote
) == REG_EQUAL
8602 && reg_mentioned_p (regno_reg_rtx
[bl
->regno
],
8604 *pnote
= XEXP (*pnote
, 1);
8606 pnote
= &XEXP (*pnote
, 1);
8610 /* Mark that this biv has been reversed. Each giv which depends
8611 on this biv, and which is also live past the end of the loop
8612 will have to be fixed up. */
8616 if (loop_dump_stream
)
8618 fprintf (loop_dump_stream
, "Reversed loop");
8620 fprintf (loop_dump_stream
, " and added reg_nonneg\n");
8622 fprintf (loop_dump_stream
, "\n");
8633 /* Verify whether the biv BL appears to be eliminable,
8634 based on the insns in the loop that refer to it.
8636 If ELIMINATE_P is nonzero, actually do the elimination.
8638 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8639 determine whether invariant insns should be placed inside or at the
8640 start of the loop. */
8643 maybe_eliminate_biv (const struct loop
*loop
, struct iv_class
*bl
,
8644 int eliminate_p
, int threshold
, int insn_count
)
8646 struct loop_ivs
*ivs
= LOOP_IVS (loop
);
8647 rtx reg
= bl
->biv
->dest_reg
;
8650 /* Scan all insns in the loop, stopping if we find one that uses the
8651 biv in a way that we cannot eliminate. */
8653 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
8655 enum rtx_code code
= GET_CODE (p
);
8656 basic_block where_bb
= 0;
8657 rtx where_insn
= threshold
>= insn_count
? 0 : p
;
8660 /* If this is a libcall that sets a giv, skip ahead to its end. */
8661 if (GET_RTX_CLASS (code
) == 'i')
8663 note
= find_reg_note (p
, REG_LIBCALL
, NULL_RTX
);
8667 rtx last
= XEXP (note
, 0);
8668 rtx set
= single_set (last
);
8670 if (set
&& GET_CODE (SET_DEST (set
)) == REG
)
8672 unsigned int regno
= REGNO (SET_DEST (set
));
8674 if (regno
< ivs
->n_regs
8675 && REG_IV_TYPE (ivs
, regno
) == GENERAL_INDUCT
8676 && REG_IV_INFO (ivs
, regno
)->src_reg
== bl
->biv
->src_reg
)
8682 /* Closely examine the insn if the biv is mentioned. */
8683 if ((code
== INSN
|| code
== JUMP_INSN
|| code
== CALL_INSN
)
8684 && reg_mentioned_p (reg
, PATTERN (p
))
8685 && ! maybe_eliminate_biv_1 (loop
, PATTERN (p
), p
, bl
,
8686 eliminate_p
, where_bb
, where_insn
))
8688 if (loop_dump_stream
)
8689 fprintf (loop_dump_stream
,
8690 "Cannot eliminate biv %d: biv used in insn %d.\n",
8691 bl
->regno
, INSN_UID (p
));
8695 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
8697 && (note
= find_reg_note (p
, REG_EQUAL
, NULL_RTX
)) != NULL_RTX
8698 && reg_mentioned_p (reg
, XEXP (note
, 0)))
8699 remove_note (p
, note
);
8704 if (loop_dump_stream
)
8705 fprintf (loop_dump_stream
, "biv %d %s eliminated.\n",
8706 bl
->regno
, eliminate_p
? "was" : "can be");
8713 /* INSN and REFERENCE are instructions in the same insn chain.
8714 Return nonzero if INSN is first. */
8717 loop_insn_first_p (rtx insn
, rtx reference
)
8721 for (p
= insn
, q
= reference
;;)
8723 /* Start with test for not first so that INSN == REFERENCE yields not
8725 if (q
== insn
|| ! p
)
8727 if (p
== reference
|| ! q
)
8730 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8731 previous insn, hence the <= comparison below does not work if
8733 if (INSN_UID (p
) < max_uid_for_loop
8734 && INSN_UID (q
) < max_uid_for_loop
8735 && GET_CODE (p
) != NOTE
)
8736 return INSN_LUID (p
) <= INSN_LUID (q
);
8738 if (INSN_UID (p
) >= max_uid_for_loop
8739 || GET_CODE (p
) == NOTE
)
8741 if (INSN_UID (q
) >= max_uid_for_loop
)
8746 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
8747 the offset that we have to take into account due to auto-increment /
8748 div derivation is zero. */
8750 biv_elimination_giv_has_0_offset (struct induction
*biv
,
8751 struct induction
*giv
, rtx insn
)
8753 /* If the giv V had the auto-inc address optimization applied
8754 to it, and INSN occurs between the giv insn and the biv
8755 insn, then we'd have to adjust the value used here.
8756 This is rare, so we don't bother to make this possible. */
8757 if (giv
->auto_inc_opt
8758 && ((loop_insn_first_p (giv
->insn
, insn
)
8759 && loop_insn_first_p (insn
, biv
->insn
))
8760 || (loop_insn_first_p (biv
->insn
, insn
)
8761 && loop_insn_first_p (insn
, giv
->insn
))))
8767 /* If BL appears in X (part of the pattern of INSN), see if we can
8768 eliminate its use. If so, return 1. If not, return 0.
8770 If BIV does not appear in X, return 1.
8772 If ELIMINATE_P is nonzero, actually do the elimination.
8773 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8774 Depending on how many items have been moved out of the loop, it
8775 will either be before INSN (when WHERE_INSN is nonzero) or at the
8776 start of the loop (when WHERE_INSN is zero). */
8779 maybe_eliminate_biv_1 (const struct loop
*loop
, rtx x
, rtx insn
,
8780 struct iv_class
*bl
, int eliminate_p
,
8781 basic_block where_bb
, rtx where_insn
)
8783 enum rtx_code code
= GET_CODE (x
);
8784 rtx reg
= bl
->biv
->dest_reg
;
8785 enum machine_mode mode
= GET_MODE (reg
);
8786 struct induction
*v
;
8798 /* If we haven't already been able to do something with this BIV,
8799 we can't eliminate it. */
8805 /* If this sets the BIV, it is not a problem. */
8806 if (SET_DEST (x
) == reg
)
8809 /* If this is an insn that defines a giv, it is also ok because
8810 it will go away when the giv is reduced. */
8811 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8812 if (v
->giv_type
== DEST_REG
&& SET_DEST (x
) == v
->dest_reg
)
8816 if (SET_DEST (x
) == cc0_rtx
&& SET_SRC (x
) == reg
)
8818 /* Can replace with any giv that was reduced and
8819 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8820 Require a constant for MULT_VAL, so we know it's nonzero.
8821 ??? We disable this optimization to avoid potential
8824 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8825 if (GET_CODE (v
->mult_val
) == CONST_INT
&& v
->mult_val
!= const0_rtx
8826 && v
->add_val
== const0_rtx
8827 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8831 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8837 /* If the giv has the opposite direction of change,
8838 then reverse the comparison. */
8839 if (INTVAL (v
->mult_val
) < 0)
8840 new = gen_rtx_COMPARE (GET_MODE (v
->new_reg
),
8841 const0_rtx
, v
->new_reg
);
8845 /* We can probably test that giv's reduced reg. */
8846 if (validate_change (insn
, &SET_SRC (x
), new, 0))
8850 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8851 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8852 Require a constant for MULT_VAL, so we know it's nonzero.
8853 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8854 overflow problem. */
8856 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8857 if (GET_CODE (v
->mult_val
) == CONST_INT
8858 && v
->mult_val
!= const0_rtx
8859 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8861 && (GET_CODE (v
->add_val
) == SYMBOL_REF
8862 || GET_CODE (v
->add_val
) == LABEL_REF
8863 || GET_CODE (v
->add_val
) == CONST
8864 || (GET_CODE (v
->add_val
) == REG
8865 && REG_POINTER (v
->add_val
))))
8867 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8873 /* If the giv has the opposite direction of change,
8874 then reverse the comparison. */
8875 if (INTVAL (v
->mult_val
) < 0)
8876 new = gen_rtx_COMPARE (VOIDmode
, copy_rtx (v
->add_val
),
8879 new = gen_rtx_COMPARE (VOIDmode
, v
->new_reg
,
8880 copy_rtx (v
->add_val
));
8882 /* Replace biv with the giv's reduced register. */
8883 update_reg_last_use (v
->add_val
, insn
);
8884 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
8887 /* Insn doesn't support that constant or invariant. Copy it
8888 into a register (it will be a loop invariant.) */
8889 tem
= gen_reg_rtx (GET_MODE (v
->new_reg
));
8891 loop_insn_emit_before (loop
, 0, where_insn
,
8893 copy_rtx (v
->add_val
)));
8895 /* Substitute the new register for its invariant value in
8896 the compare expression. */
8897 XEXP (new, (INTVAL (v
->mult_val
) < 0) ? 0 : 1) = tem
;
8898 if (validate_change (insn
, &SET_SRC (PATTERN (insn
)), new, 0))
8907 case GT
: case GE
: case GTU
: case GEU
:
8908 case LT
: case LE
: case LTU
: case LEU
:
8909 /* See if either argument is the biv. */
8910 if (XEXP (x
, 0) == reg
)
8911 arg
= XEXP (x
, 1), arg_operand
= 1;
8912 else if (XEXP (x
, 1) == reg
)
8913 arg
= XEXP (x
, 0), arg_operand
= 0;
8917 if (CONSTANT_P (arg
))
8919 /* First try to replace with any giv that has constant positive
8920 mult_val and constant add_val. We might be able to support
8921 negative mult_val, but it seems complex to do it in general. */
8923 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8924 if (GET_CODE (v
->mult_val
) == CONST_INT
8925 && INTVAL (v
->mult_val
) > 0
8926 && (GET_CODE (v
->add_val
) == SYMBOL_REF
8927 || GET_CODE (v
->add_val
) == LABEL_REF
8928 || GET_CODE (v
->add_val
) == CONST
8929 || (GET_CODE (v
->add_val
) == REG
8930 && REG_POINTER (v
->add_val
)))
8931 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8934 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
8937 /* Don't eliminate if the linear combination that makes up
8938 the giv overflows when it is applied to ARG. */
8939 if (GET_CODE (arg
) == CONST_INT
)
8943 if (GET_CODE (v
->add_val
) == CONST_INT
)
8944 add_val
= v
->add_val
;
8946 add_val
= const0_rtx
;
8948 if (const_mult_add_overflow_p (arg
, v
->mult_val
,
8956 /* Replace biv with the giv's reduced reg. */
8957 validate_change (insn
, &XEXP (x
, 1 - arg_operand
), v
->new_reg
, 1);
8959 /* If all constants are actually constant integers and
8960 the derived constant can be directly placed in the COMPARE,
8962 if (GET_CODE (arg
) == CONST_INT
8963 && GET_CODE (v
->add_val
) == CONST_INT
)
8965 tem
= expand_mult_add (arg
, NULL_RTX
, v
->mult_val
,
8966 v
->add_val
, mode
, 1);
8970 /* Otherwise, load it into a register. */
8971 tem
= gen_reg_rtx (mode
);
8972 loop_iv_add_mult_emit_before (loop
, arg
,
8973 v
->mult_val
, v
->add_val
,
8974 tem
, where_bb
, where_insn
);
8977 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
8979 if (apply_change_group ())
8983 /* Look for giv with positive constant mult_val and nonconst add_val.
8984 Insert insns to calculate new compare value.
8985 ??? Turn this off due to possible overflow. */
8987 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
8988 if (GET_CODE (v
->mult_val
) == CONST_INT
8989 && INTVAL (v
->mult_val
) > 0
8990 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
8996 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9002 tem
= gen_reg_rtx (mode
);
9004 /* Replace biv with giv's reduced register. */
9005 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
9008 /* Compute value to compare against. */
9009 loop_iv_add_mult_emit_before (loop
, arg
,
9010 v
->mult_val
, v
->add_val
,
9011 tem
, where_bb
, where_insn
);
9012 /* Use it in this insn. */
9013 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
9014 if (apply_change_group ())
9018 else if (GET_CODE (arg
) == REG
|| GET_CODE (arg
) == MEM
)
9020 if (loop_invariant_p (loop
, arg
) == 1)
9022 /* Look for giv with constant positive mult_val and nonconst
9023 add_val. Insert insns to compute new compare value.
9024 ??? Turn this off due to possible overflow. */
9026 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9027 if (GET_CODE (v
->mult_val
) == CONST_INT
&& INTVAL (v
->mult_val
) > 0
9028 && ! v
->ignore
&& ! v
->maybe_dead
&& v
->always_computable
9034 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9040 tem
= gen_reg_rtx (mode
);
9042 /* Replace biv with giv's reduced register. */
9043 validate_change (insn
, &XEXP (x
, 1 - arg_operand
),
9046 /* Compute value to compare against. */
9047 loop_iv_add_mult_emit_before (loop
, arg
,
9048 v
->mult_val
, v
->add_val
,
9049 tem
, where_bb
, where_insn
);
9050 validate_change (insn
, &XEXP (x
, arg_operand
), tem
, 1);
9051 if (apply_change_group ())
9056 /* This code has problems. Basically, you can't know when
9057 seeing if we will eliminate BL, whether a particular giv
9058 of ARG will be reduced. If it isn't going to be reduced,
9059 we can't eliminate BL. We can try forcing it to be reduced,
9060 but that can generate poor code.
9062 The problem is that the benefit of reducing TV, below should
9063 be increased if BL can actually be eliminated, but this means
9064 we might have to do a topological sort of the order in which
9065 we try to process biv. It doesn't seem worthwhile to do
9066 this sort of thing now. */
9069 /* Otherwise the reg compared with had better be a biv. */
9070 if (GET_CODE (arg
) != REG
9071 || REG_IV_TYPE (ivs
, REGNO (arg
)) != BASIC_INDUCT
)
9074 /* Look for a pair of givs, one for each biv,
9075 with identical coefficients. */
9076 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9078 struct induction
*tv
;
9080 if (v
->ignore
|| v
->maybe_dead
|| v
->mode
!= mode
)
9083 for (tv
= REG_IV_CLASS (ivs
, REGNO (arg
))->giv
; tv
;
9085 if (! tv
->ignore
&& ! tv
->maybe_dead
9086 && rtx_equal_p (tv
->mult_val
, v
->mult_val
)
9087 && rtx_equal_p (tv
->add_val
, v
->add_val
)
9088 && tv
->mode
== mode
)
9090 if (! biv_elimination_giv_has_0_offset (bl
->biv
, v
, insn
))
9096 /* Replace biv with its giv's reduced reg. */
9097 XEXP (x
, 1 - arg_operand
) = v
->new_reg
;
9098 /* Replace other operand with the other giv's
9100 XEXP (x
, arg_operand
) = tv
->new_reg
;
9107 /* If we get here, the biv can't be eliminated. */
9111 /* If this address is a DEST_ADDR giv, it doesn't matter if the
9112 biv is used in it, since it will be replaced. */
9113 for (v
= bl
->giv
; v
; v
= v
->next_iv
)
9114 if (v
->giv_type
== DEST_ADDR
&& v
->location
== &XEXP (x
, 0))
9122 /* See if any subexpression fails elimination. */
9123 fmt
= GET_RTX_FORMAT (code
);
9124 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
9129 if (! maybe_eliminate_biv_1 (loop
, XEXP (x
, i
), insn
, bl
,
9130 eliminate_p
, where_bb
, where_insn
))
9135 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
9136 if (! maybe_eliminate_biv_1 (loop
, XVECEXP (x
, i
, j
), insn
, bl
,
9137 eliminate_p
, where_bb
, where_insn
))
9146 /* Return nonzero if the last use of REG
9147 is in an insn following INSN in the same basic block. */
9150 last_use_this_basic_block (rtx reg
, rtx insn
)
9154 n
&& GET_CODE (n
) != CODE_LABEL
&& GET_CODE (n
) != JUMP_INSN
;
9157 if (REGNO_LAST_UID (REGNO (reg
)) == INSN_UID (n
))
9163 /* Called via `note_stores' to record the initial value of a biv. Here we
9164 just record the location of the set and process it later. */
9167 record_initial (rtx dest
, rtx set
, void *data ATTRIBUTE_UNUSED
)
9169 struct loop_ivs
*ivs
= (struct loop_ivs
*) data
;
9170 struct iv_class
*bl
;
9172 if (GET_CODE (dest
) != REG
9173 || REGNO (dest
) >= ivs
->n_regs
9174 || REG_IV_TYPE (ivs
, REGNO (dest
)) != BASIC_INDUCT
)
9177 bl
= REG_IV_CLASS (ivs
, REGNO (dest
));
9179 /* If this is the first set found, record it. */
9180 if (bl
->init_insn
== 0)
9182 bl
->init_insn
= note_insn
;
9187 /* If any of the registers in X are "old" and currently have a last use earlier
9188 than INSN, update them to have a last use of INSN. Their actual last use
9189 will be the previous insn but it will not have a valid uid_luid so we can't
9190 use it. X must be a source expression only. */
9193 update_reg_last_use (rtx x
, rtx insn
)
9195 /* Check for the case where INSN does not have a valid luid. In this case,
9196 there is no need to modify the regno_last_uid, as this can only happen
9197 when code is inserted after the loop_end to set a pseudo's final value,
9198 and hence this insn will never be the last use of x.
9199 ???? This comment is not correct. See for example loop_givs_reduce.
9200 This may insert an insn before another new insn. */
9201 if (GET_CODE (x
) == REG
&& REGNO (x
) < max_reg_before_loop
9202 && INSN_UID (insn
) < max_uid_for_loop
9203 && REGNO_LAST_LUID (REGNO (x
)) < INSN_LUID (insn
))
9205 REGNO_LAST_UID (REGNO (x
)) = INSN_UID (insn
);
9210 const char *fmt
= GET_RTX_FORMAT (GET_CODE (x
));
9211 for (i
= GET_RTX_LENGTH (GET_CODE (x
)) - 1; i
>= 0; i
--)
9214 update_reg_last_use (XEXP (x
, i
), insn
);
9215 else if (fmt
[i
] == 'E')
9216 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
9217 update_reg_last_use (XVECEXP (x
, i
, j
), insn
);
9222 /* Given an insn INSN and condition COND, return the condition in a
9223 canonical form to simplify testing by callers. Specifically:
9225 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9226 (2) Both operands will be machine operands; (cc0) will have been replaced.
9227 (3) If an operand is a constant, it will be the second operand.
9228 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9229 for GE, GEU, and LEU.
9231 If the condition cannot be understood, or is an inequality floating-point
9232 comparison which needs to be reversed, 0 will be returned.
9234 If REVERSE is nonzero, then reverse the condition prior to canonizing it.
9236 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9237 insn used in locating the condition was found. If a replacement test
9238 of the condition is desired, it should be placed in front of that
9239 insn and we will be sure that the inputs are still valid.
9241 If WANT_REG is nonzero, we wish the condition to be relative to that
9242 register, if possible. Therefore, do not canonicalize the condition
9243 further. If ALLOW_CC_MODE is nonzero, allow the condition returned
9244 to be a compare to a CC mode register. */
9247 canonicalize_condition (rtx insn
, rtx cond
, int reverse
, rtx
*earliest
,
9248 rtx want_reg
, int allow_cc_mode
)
9255 int reverse_code
= 0;
9256 enum machine_mode mode
;
9258 code
= GET_CODE (cond
);
9259 mode
= GET_MODE (cond
);
9260 op0
= XEXP (cond
, 0);
9261 op1
= XEXP (cond
, 1);
9264 code
= reversed_comparison_code (cond
, insn
);
9265 if (code
== UNKNOWN
)
9271 /* If we are comparing a register with zero, see if the register is set
9272 in the previous insn to a COMPARE or a comparison operation. Perform
9273 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9276 while (GET_RTX_CLASS (code
) == '<'
9277 && op1
== CONST0_RTX (GET_MODE (op0
))
9280 /* Set nonzero when we find something of interest. */
9284 /* If comparison with cc0, import actual comparison from compare
9288 if ((prev
= prev_nonnote_insn (prev
)) == 0
9289 || GET_CODE (prev
) != INSN
9290 || (set
= single_set (prev
)) == 0
9291 || SET_DEST (set
) != cc0_rtx
)
9294 op0
= SET_SRC (set
);
9295 op1
= CONST0_RTX (GET_MODE (op0
));
9301 /* If this is a COMPARE, pick up the two things being compared. */
9302 if (GET_CODE (op0
) == COMPARE
)
9304 op1
= XEXP (op0
, 1);
9305 op0
= XEXP (op0
, 0);
9308 else if (GET_CODE (op0
) != REG
)
9311 /* Go back to the previous insn. Stop if it is not an INSN. We also
9312 stop if it isn't a single set or if it has a REG_INC note because
9313 we don't want to bother dealing with it. */
9315 if ((prev
= prev_nonnote_insn (prev
)) == 0
9316 || GET_CODE (prev
) != INSN
9317 || FIND_REG_INC_NOTE (prev
, NULL_RTX
))
9320 set
= set_of (op0
, prev
);
9323 && (GET_CODE (set
) != SET
9324 || !rtx_equal_p (SET_DEST (set
), op0
)))
9327 /* If this is setting OP0, get what it sets it to if it looks
9331 enum machine_mode inner_mode
= GET_MODE (SET_DEST (set
));
9332 #ifdef FLOAT_STORE_FLAG_VALUE
9333 REAL_VALUE_TYPE fsfv
;
9336 /* ??? We may not combine comparisons done in a CCmode with
9337 comparisons not done in a CCmode. This is to aid targets
9338 like Alpha that have an IEEE compliant EQ instruction, and
9339 a non-IEEE compliant BEQ instruction. The use of CCmode is
9340 actually artificial, simply to prevent the combination, but
9341 should not affect other platforms.
9343 However, we must allow VOIDmode comparisons to match either
9344 CCmode or non-CCmode comparison, because some ports have
9345 modeless comparisons inside branch patterns.
9347 ??? This mode check should perhaps look more like the mode check
9348 in simplify_comparison in combine. */
9350 if ((GET_CODE (SET_SRC (set
)) == COMPARE
9353 && GET_MODE_CLASS (inner_mode
) == MODE_INT
9354 && (GET_MODE_BITSIZE (inner_mode
)
9355 <= HOST_BITS_PER_WIDE_INT
)
9356 && (STORE_FLAG_VALUE
9357 & ((HOST_WIDE_INT
) 1
9358 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
9359 #ifdef FLOAT_STORE_FLAG_VALUE
9361 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
9362 && (fsfv
= FLOAT_STORE_FLAG_VALUE (inner_mode
),
9363 REAL_VALUE_NEGATIVE (fsfv
)))
9366 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<'))
9367 && (((GET_MODE_CLASS (mode
) == MODE_CC
)
9368 == (GET_MODE_CLASS (inner_mode
) == MODE_CC
))
9369 || mode
== VOIDmode
|| inner_mode
== VOIDmode
))
9371 else if (((code
== EQ
9373 && (GET_MODE_BITSIZE (inner_mode
)
9374 <= HOST_BITS_PER_WIDE_INT
)
9375 && GET_MODE_CLASS (inner_mode
) == MODE_INT
9376 && (STORE_FLAG_VALUE
9377 & ((HOST_WIDE_INT
) 1
9378 << (GET_MODE_BITSIZE (inner_mode
) - 1))))
9379 #ifdef FLOAT_STORE_FLAG_VALUE
9381 && GET_MODE_CLASS (inner_mode
) == MODE_FLOAT
9382 && (fsfv
= FLOAT_STORE_FLAG_VALUE (inner_mode
),
9383 REAL_VALUE_NEGATIVE (fsfv
)))
9386 && GET_RTX_CLASS (GET_CODE (SET_SRC (set
))) == '<'
9387 && (((GET_MODE_CLASS (mode
) == MODE_CC
)
9388 == (GET_MODE_CLASS (inner_mode
) == MODE_CC
))
9389 || mode
== VOIDmode
|| inner_mode
== VOIDmode
))
9399 else if (reg_set_p (op0
, prev
))
9400 /* If this sets OP0, but not directly, we have to give up. */
9405 if (GET_RTX_CLASS (GET_CODE (x
)) == '<')
9406 code
= GET_CODE (x
);
9409 code
= reversed_comparison_code (x
, prev
);
9410 if (code
== UNKNOWN
)
9415 op0
= XEXP (x
, 0), op1
= XEXP (x
, 1);
9421 /* If constant is first, put it last. */
9422 if (CONSTANT_P (op0
))
9423 code
= swap_condition (code
), tem
= op0
, op0
= op1
, op1
= tem
;
9425 /* If OP0 is the result of a comparison, we weren't able to find what
9426 was really being compared, so fail. */
9428 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
)
9431 /* Canonicalize any ordered comparison with integers involving equality
9432 if we can do computations in the relevant mode and we do not
9435 if (GET_MODE_CLASS (GET_MODE (op0
)) != MODE_CC
9436 && GET_CODE (op1
) == CONST_INT
9437 && GET_MODE (op0
) != VOIDmode
9438 && GET_MODE_BITSIZE (GET_MODE (op0
)) <= HOST_BITS_PER_WIDE_INT
)
9440 HOST_WIDE_INT const_val
= INTVAL (op1
);
9441 unsigned HOST_WIDE_INT uconst_val
= const_val
;
9442 unsigned HOST_WIDE_INT max_val
9443 = (unsigned HOST_WIDE_INT
) GET_MODE_MASK (GET_MODE (op0
));
9448 if ((unsigned HOST_WIDE_INT
) const_val
!= max_val
>> 1)
9449 code
= LT
, op1
= gen_int_mode (const_val
+ 1, GET_MODE (op0
));
9452 /* When cross-compiling, const_val might be sign-extended from
9453 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9455 if ((HOST_WIDE_INT
) (const_val
& max_val
)
9456 != (((HOST_WIDE_INT
) 1
9457 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
9458 code
= GT
, op1
= gen_int_mode (const_val
- 1, GET_MODE (op0
));
9462 if (uconst_val
< max_val
)
9463 code
= LTU
, op1
= gen_int_mode (uconst_val
+ 1, GET_MODE (op0
));
9467 if (uconst_val
!= 0)
9468 code
= GTU
, op1
= gen_int_mode (uconst_val
- 1, GET_MODE (op0
));
9476 /* Never return CC0; return zero instead. */
9480 return gen_rtx_fmt_ee (code
, VOIDmode
, op0
, op1
);
9483 /* Given a jump insn JUMP, return the condition that will cause it to branch
9484 to its JUMP_LABEL. If the condition cannot be understood, or is an
9485 inequality floating-point comparison which needs to be reversed, 0 will
9488 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9489 insn used in locating the condition was found. If a replacement test
9490 of the condition is desired, it should be placed in front of that
9491 insn and we will be sure that the inputs are still valid.
9493 If ALLOW_CC_MODE is nonzero, allow the condition returned to be a
9494 compare CC mode register. */
9497 get_condition (rtx jump
, rtx
*earliest
, int allow_cc_mode
)
9503 /* If this is not a standard conditional jump, we can't parse it. */
9504 if (GET_CODE (jump
) != JUMP_INSN
9505 || ! any_condjump_p (jump
))
9507 set
= pc_set (jump
);
9509 cond
= XEXP (SET_SRC (set
), 0);
9511 /* If this branches to JUMP_LABEL when the condition is false, reverse
9514 = GET_CODE (XEXP (SET_SRC (set
), 2)) == LABEL_REF
9515 && XEXP (XEXP (SET_SRC (set
), 2), 0) == JUMP_LABEL (jump
);
9517 return canonicalize_condition (jump
, cond
, reverse
, earliest
, NULL_RTX
,
9521 /* Similar to above routine, except that we also put an invariant last
9522 unless both operands are invariants. */
9525 get_condition_for_loop (const struct loop
*loop
, rtx x
)
9527 rtx comparison
= get_condition (x
, (rtx
*) 0, false);
9530 || ! loop_invariant_p (loop
, XEXP (comparison
, 0))
9531 || loop_invariant_p (loop
, XEXP (comparison
, 1)))
9534 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison
)), VOIDmode
,
9535 XEXP (comparison
, 1), XEXP (comparison
, 0));
9538 /* Scan the function and determine whether it has indirect (computed) jumps.
9540 This is taken mostly from flow.c; similar code exists elsewhere
9541 in the compiler. It may be useful to put this into rtlanal.c. */
9543 indirect_jump_in_function_p (rtx start
)
9547 for (insn
= start
; insn
; insn
= NEXT_INSN (insn
))
9548 if (computed_jump_p (insn
))
9554 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9555 documentation for LOOP_MEMS for the definition of `appropriate'.
9556 This function is called from prescan_loop via for_each_rtx. */
9559 insert_loop_mem (rtx
*mem
, void *data ATTRIBUTE_UNUSED
)
9561 struct loop_info
*loop_info
= data
;
9568 switch (GET_CODE (m
))
9574 /* We're not interested in MEMs that are only clobbered. */
9578 /* We're not interested in the MEM associated with a
9579 CONST_DOUBLE, so there's no need to traverse into this. */
9583 /* We're not interested in any MEMs that only appear in notes. */
9587 /* This is not a MEM. */
9591 /* See if we've already seen this MEM. */
9592 for (i
= 0; i
< loop_info
->mems_idx
; ++i
)
9593 if (rtx_equal_p (m
, loop_info
->mems
[i
].mem
))
9595 if (MEM_VOLATILE_P (m
) && !MEM_VOLATILE_P (loop_info
->mems
[i
].mem
))
9596 loop_info
->mems
[i
].mem
= m
;
9597 if (GET_MODE (m
) != GET_MODE (loop_info
->mems
[i
].mem
))
9598 /* The modes of the two memory accesses are different. If
9599 this happens, something tricky is going on, and we just
9600 don't optimize accesses to this MEM. */
9601 loop_info
->mems
[i
].optimize
= 0;
9606 /* Resize the array, if necessary. */
9607 if (loop_info
->mems_idx
== loop_info
->mems_allocated
)
9609 if (loop_info
->mems_allocated
!= 0)
9610 loop_info
->mems_allocated
*= 2;
9612 loop_info
->mems_allocated
= 32;
9614 loop_info
->mems
= xrealloc (loop_info
->mems
,
9615 loop_info
->mems_allocated
* sizeof (loop_mem_info
));
9618 /* Actually insert the MEM. */
9619 loop_info
->mems
[loop_info
->mems_idx
].mem
= m
;
9620 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9621 because we can't put it in a register. We still store it in the
9622 table, though, so that if we see the same address later, but in a
9623 non-BLK mode, we'll not think we can optimize it at that point. */
9624 loop_info
->mems
[loop_info
->mems_idx
].optimize
= (GET_MODE (m
) != BLKmode
);
9625 loop_info
->mems
[loop_info
->mems_idx
].reg
= NULL_RTX
;
9626 ++loop_info
->mems_idx
;
9632 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9634 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9635 register that is modified by an insn between FROM and TO. If the
9636 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9637 more, stop incrementing it, to avoid overflow.
9639 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9640 register I is used, if it is only used once. Otherwise, it is set
9641 to 0 (for no uses) or const0_rtx for more than one use. This
9642 parameter may be zero, in which case this processing is not done.
9644 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9645 optimize register I. */
9648 loop_regs_scan (const struct loop
*loop
, int extra_size
)
9650 struct loop_regs
*regs
= LOOP_REGS (loop
);
9652 /* last_set[n] is nonzero iff reg n has been set in the current
9653 basic block. In that case, it is the insn that last set reg n. */
9658 old_nregs
= regs
->num
;
9659 regs
->num
= max_reg_num ();
9661 /* Grow the regs array if not allocated or too small. */
9662 if (regs
->num
>= regs
->size
)
9664 regs
->size
= regs
->num
+ extra_size
;
9666 regs
->array
= xrealloc (regs
->array
, regs
->size
* sizeof (*regs
->array
));
9668 /* Zero the new elements. */
9669 memset (regs
->array
+ old_nregs
, 0,
9670 (regs
->size
- old_nregs
) * sizeof (*regs
->array
));
9673 /* Clear previously scanned fields but do not clear n_times_set. */
9674 for (i
= 0; i
< old_nregs
; i
++)
9676 regs
->array
[i
].set_in_loop
= 0;
9677 regs
->array
[i
].may_not_optimize
= 0;
9678 regs
->array
[i
].single_usage
= NULL_RTX
;
9681 last_set
= xcalloc (regs
->num
, sizeof (rtx
));
9683 /* Scan the loop, recording register usage. */
9684 for (insn
= loop
->top
? loop
->top
: loop
->start
; insn
!= loop
->end
;
9685 insn
= NEXT_INSN (insn
))
9689 /* Record registers that have exactly one use. */
9690 find_single_use_in_loop (regs
, insn
, PATTERN (insn
));
9692 /* Include uses in REG_EQUAL notes. */
9693 if (REG_NOTES (insn
))
9694 find_single_use_in_loop (regs
, insn
, REG_NOTES (insn
));
9696 if (GET_CODE (PATTERN (insn
)) == SET
9697 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
9698 count_one_set (regs
, insn
, PATTERN (insn
), last_set
);
9699 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
)
9702 for (i
= XVECLEN (PATTERN (insn
), 0) - 1; i
>= 0; i
--)
9703 count_one_set (regs
, insn
, XVECEXP (PATTERN (insn
), 0, i
),
9708 if (GET_CODE (insn
) == CODE_LABEL
|| GET_CODE (insn
) == JUMP_INSN
)
9709 memset (last_set
, 0, regs
->num
* sizeof (rtx
));
9711 /* Invalidate all registers used for function argument passing.
9712 We check rtx_varies_p for the same reason as below, to allow
9713 optimizing PIC calculations. */
9714 if (GET_CODE (insn
) == CALL_INSN
)
9717 for (link
= CALL_INSN_FUNCTION_USAGE (insn
);
9719 link
= XEXP (link
, 1))
9723 if (GET_CODE (op
= XEXP (link
, 0)) == USE
9724 && GET_CODE (reg
= XEXP (op
, 0)) == REG
9725 && rtx_varies_p (reg
, 1))
9726 regs
->array
[REGNO (reg
)].may_not_optimize
= 1;
9731 /* Invalidate all hard registers clobbered by calls. With one exception:
9732 a call-clobbered PIC register is still function-invariant for our
9733 purposes, since we can hoist any PIC calculations out of the loop.
9734 Thus the call to rtx_varies_p. */
9735 if (LOOP_INFO (loop
)->has_call
)
9736 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
9737 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
)
9738 && rtx_varies_p (regno_reg_rtx
[i
], 1))
9740 regs
->array
[i
].may_not_optimize
= 1;
9741 regs
->array
[i
].set_in_loop
= 1;
9744 #ifdef AVOID_CCMODE_COPIES
9745 /* Don't try to move insns which set CC registers if we should not
9746 create CCmode register copies. */
9747 for (i
= regs
->num
- 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
9748 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx
[i
])) == MODE_CC
)
9749 regs
->array
[i
].may_not_optimize
= 1;
9752 /* Set regs->array[I].n_times_set for the new registers. */
9753 for (i
= old_nregs
; i
< regs
->num
; i
++)
9754 regs
->array
[i
].n_times_set
= regs
->array
[i
].set_in_loop
;
9759 /* Returns the number of real INSNs in the LOOP. */
9762 count_insns_in_loop (const struct loop
*loop
)
9767 for (insn
= loop
->top
? loop
->top
: loop
->start
; insn
!= loop
->end
;
9768 insn
= NEXT_INSN (insn
))
9775 /* Move MEMs into registers for the duration of the loop. */
9778 load_mems (const struct loop
*loop
)
9780 struct loop_info
*loop_info
= LOOP_INFO (loop
);
9781 struct loop_regs
*regs
= LOOP_REGS (loop
);
9782 int maybe_never
= 0;
9784 rtx p
, prev_ebb_head
;
9785 rtx label
= NULL_RTX
;
9787 /* Nonzero if the next instruction may never be executed. */
9788 int next_maybe_never
= 0;
9789 unsigned int last_max_reg
= max_reg_num ();
9791 if (loop_info
->mems_idx
== 0)
9794 /* We cannot use next_label here because it skips over normal insns. */
9795 end_label
= next_nonnote_insn (loop
->end
);
9796 if (end_label
&& GET_CODE (end_label
) != CODE_LABEL
)
9797 end_label
= NULL_RTX
;
9799 /* Check to see if it's possible that some instructions in the loop are
9800 never executed. Also check if there is a goto out of the loop other
9801 than right after the end of the loop. */
9802 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
9804 p
= next_insn_in_loop (loop
, p
))
9806 if (GET_CODE (p
) == CODE_LABEL
)
9808 else if (GET_CODE (p
) == JUMP_INSN
9809 /* If we enter the loop in the middle, and scan
9810 around to the beginning, don't set maybe_never
9811 for that. This must be an unconditional jump,
9812 otherwise the code at the top of the loop might
9813 never be executed. Unconditional jumps are
9814 followed a by barrier then loop end. */
9815 && ! (GET_CODE (p
) == JUMP_INSN
9816 && JUMP_LABEL (p
) == loop
->top
9817 && NEXT_INSN (NEXT_INSN (p
)) == loop
->end
9818 && any_uncondjump_p (p
)))
9820 /* If this is a jump outside of the loop but not right
9821 after the end of the loop, we would have to emit new fixup
9822 sequences for each such label. */
9823 if (/* If we can't tell where control might go when this
9824 JUMP_INSN is executed, we must be conservative. */
9826 || (JUMP_LABEL (p
) != end_label
9827 && (INSN_UID (JUMP_LABEL (p
)) >= max_uid_for_loop
9828 || INSN_LUID (JUMP_LABEL (p
)) < INSN_LUID (loop
->start
)
9829 || INSN_LUID (JUMP_LABEL (p
)) > INSN_LUID (loop
->end
))))
9832 if (!any_condjump_p (p
))
9833 /* Something complicated. */
9836 /* If there are any more instructions in the loop, they
9837 might not be reached. */
9838 next_maybe_never
= 1;
9840 else if (next_maybe_never
)
9844 /* Find start of the extended basic block that enters the loop. */
9845 for (p
= loop
->start
;
9846 PREV_INSN (p
) && GET_CODE (p
) != CODE_LABEL
;
9853 /* Build table of mems that get set to constant values before the
9855 for (; p
!= loop
->start
; p
= NEXT_INSN (p
))
9856 cselib_process_insn (p
);
9858 /* Actually move the MEMs. */
9859 for (i
= 0; i
< loop_info
->mems_idx
; ++i
)
9861 regset_head load_copies
;
9862 regset_head store_copies
;
9865 rtx mem
= loop_info
->mems
[i
].mem
;
9868 if (MEM_VOLATILE_P (mem
)
9869 || loop_invariant_p (loop
, XEXP (mem
, 0)) != 1)
9870 /* There's no telling whether or not MEM is modified. */
9871 loop_info
->mems
[i
].optimize
= 0;
9873 /* Go through the MEMs written to in the loop to see if this
9874 one is aliased by one of them. */
9875 mem_list_entry
= loop_info
->store_mems
;
9876 while (mem_list_entry
)
9878 if (rtx_equal_p (mem
, XEXP (mem_list_entry
, 0)))
9880 else if (true_dependence (XEXP (mem_list_entry
, 0), VOIDmode
,
9883 /* MEM is indeed aliased by this store. */
9884 loop_info
->mems
[i
].optimize
= 0;
9887 mem_list_entry
= XEXP (mem_list_entry
, 1);
9890 if (flag_float_store
&& written
9891 && GET_MODE_CLASS (GET_MODE (mem
)) == MODE_FLOAT
)
9892 loop_info
->mems
[i
].optimize
= 0;
9894 /* If this MEM is written to, we must be sure that there
9895 are no reads from another MEM that aliases this one. */
9896 if (loop_info
->mems
[i
].optimize
&& written
)
9900 for (j
= 0; j
< loop_info
->mems_idx
; ++j
)
9904 else if (true_dependence (mem
,
9906 loop_info
->mems
[j
].mem
,
9909 /* It's not safe to hoist loop_info->mems[i] out of
9910 the loop because writes to it might not be
9911 seen by reads from loop_info->mems[j]. */
9912 loop_info
->mems
[i
].optimize
= 0;
9918 if (maybe_never
&& may_trap_p (mem
))
9919 /* We can't access the MEM outside the loop; it might
9920 cause a trap that wouldn't have happened otherwise. */
9921 loop_info
->mems
[i
].optimize
= 0;
9923 if (!loop_info
->mems
[i
].optimize
)
9924 /* We thought we were going to lift this MEM out of the
9925 loop, but later discovered that we could not. */
9928 INIT_REG_SET (&load_copies
);
9929 INIT_REG_SET (&store_copies
);
9931 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9932 order to keep scan_loop from moving stores to this MEM
9933 out of the loop just because this REG is neither a
9934 user-variable nor used in the loop test. */
9935 reg
= gen_reg_rtx (GET_MODE (mem
));
9936 REG_USERVAR_P (reg
) = 1;
9937 loop_info
->mems
[i
].reg
= reg
;
9939 /* Now, replace all references to the MEM with the
9940 corresponding pseudos. */
9942 for (p
= next_insn_in_loop (loop
, loop
->scan_start
);
9944 p
= next_insn_in_loop (loop
, p
))
9950 set
= single_set (p
);
9952 /* See if this copies the mem into a register that isn't
9953 modified afterwards. We'll try to do copy propagation
9954 a little further on. */
9956 /* @@@ This test is _way_ too conservative. */
9958 && GET_CODE (SET_DEST (set
)) == REG
9959 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
9960 && REGNO (SET_DEST (set
)) < last_max_reg
9961 && regs
->array
[REGNO (SET_DEST (set
))].n_times_set
== 1
9962 && rtx_equal_p (SET_SRC (set
), mem
))
9963 SET_REGNO_REG_SET (&load_copies
, REGNO (SET_DEST (set
)));
9965 /* See if this copies the mem from a register that isn't
9966 modified afterwards. We'll try to remove the
9967 redundant copy later on by doing a little register
9968 renaming and copy propagation. This will help
9969 to untangle things for the BIV detection code. */
9972 && GET_CODE (SET_SRC (set
)) == REG
9973 && REGNO (SET_SRC (set
)) >= FIRST_PSEUDO_REGISTER
9974 && REGNO (SET_SRC (set
)) < last_max_reg
9975 && regs
->array
[REGNO (SET_SRC (set
))].n_times_set
== 1
9976 && rtx_equal_p (SET_DEST (set
), mem
))
9977 SET_REGNO_REG_SET (&store_copies
, REGNO (SET_SRC (set
)));
9979 /* If this is a call which uses / clobbers this memory
9980 location, we must not change the interface here. */
9981 if (GET_CODE (p
) == CALL_INSN
9982 && reg_mentioned_p (loop_info
->mems
[i
].mem
,
9983 CALL_INSN_FUNCTION_USAGE (p
)))
9986 loop_info
->mems
[i
].optimize
= 0;
9990 /* Replace the memory reference with the shadow register. */
9991 replace_loop_mems (p
, loop_info
->mems
[i
].mem
,
9992 loop_info
->mems
[i
].reg
, written
);
9995 if (GET_CODE (p
) == CODE_LABEL
9996 || GET_CODE (p
) == JUMP_INSN
)
10000 if (! loop_info
->mems
[i
].optimize
)
10001 ; /* We found we couldn't do the replacement, so do nothing. */
10002 else if (! apply_change_group ())
10003 /* We couldn't replace all occurrences of the MEM. */
10004 loop_info
->mems
[i
].optimize
= 0;
10007 /* Load the memory immediately before LOOP->START, which is
10008 the NOTE_LOOP_BEG. */
10009 cselib_val
*e
= cselib_lookup (mem
, VOIDmode
, 0);
10013 struct elt_loc_list
*const_equiv
= 0;
10017 struct elt_loc_list
*equiv
;
10018 struct elt_loc_list
*best_equiv
= 0;
10019 for (equiv
= e
->locs
; equiv
; equiv
= equiv
->next
)
10021 if (CONSTANT_P (equiv
->loc
))
10022 const_equiv
= equiv
;
10023 else if (GET_CODE (equiv
->loc
) == REG
10024 /* Extending hard register lifetimes causes crash
10025 on SRC targets. Doing so on non-SRC is
10026 probably also not good idea, since we most
10027 probably have pseudoregister equivalence as
10029 && REGNO (equiv
->loc
) >= FIRST_PSEUDO_REGISTER
)
10030 best_equiv
= equiv
;
10032 /* Use the constant equivalence if that is cheap enough. */
10034 best_equiv
= const_equiv
;
10035 else if (const_equiv
10036 && (rtx_cost (const_equiv
->loc
, SET
)
10037 <= rtx_cost (best_equiv
->loc
, SET
)))
10039 best_equiv
= const_equiv
;
10043 /* If best_equiv is nonzero, we know that MEM is set to a
10044 constant or register before the loop. We will use this
10045 knowledge to initialize the shadow register with that
10046 constant or reg rather than by loading from MEM. */
10048 best
= copy_rtx (best_equiv
->loc
);
10051 set
= gen_move_insn (reg
, best
);
10052 set
= loop_insn_hoist (loop
, set
);
10055 for (p
= prev_ebb_head
; p
!= loop
->start
; p
= NEXT_INSN (p
))
10056 if (REGNO_LAST_UID (REGNO (best
)) == INSN_UID (p
))
10058 REGNO_LAST_UID (REGNO (best
)) = INSN_UID (set
);
10064 set_unique_reg_note (set
, REG_EQUAL
, copy_rtx (const_equiv
->loc
));
10068 if (label
== NULL_RTX
)
10070 label
= gen_label_rtx ();
10071 emit_label_after (label
, loop
->end
);
10074 /* Store the memory immediately after END, which is
10075 the NOTE_LOOP_END. */
10076 set
= gen_move_insn (copy_rtx (mem
), reg
);
10077 loop_insn_emit_after (loop
, 0, label
, set
);
10080 if (loop_dump_stream
)
10082 fprintf (loop_dump_stream
, "Hoisted regno %d %s from ",
10083 REGNO (reg
), (written
? "r/w" : "r/o"));
10084 print_rtl (loop_dump_stream
, mem
);
10085 fputc ('\n', loop_dump_stream
);
10088 /* Attempt a bit of copy propagation. This helps untangle the
10089 data flow, and enables {basic,general}_induction_var to find
10091 EXECUTE_IF_SET_IN_REG_SET
10092 (&load_copies
, FIRST_PSEUDO_REGISTER
, j
,
10094 try_copy_prop (loop
, reg
, j
);
10096 CLEAR_REG_SET (&load_copies
);
10098 EXECUTE_IF_SET_IN_REG_SET
10099 (&store_copies
, FIRST_PSEUDO_REGISTER
, j
,
10101 try_swap_copy_prop (loop
, reg
, j
);
10103 CLEAR_REG_SET (&store_copies
);
10107 /* Now, we need to replace all references to the previous exit
10108 label with the new one. */
10109 if (label
!= NULL_RTX
&& end_label
!= NULL_RTX
)
10110 for (p
= loop
->start
; p
!= loop
->end
; p
= NEXT_INSN (p
))
10111 if (GET_CODE (p
) == JUMP_INSN
&& JUMP_LABEL (p
) == end_label
)
10112 redirect_jump (p
, label
, false);
10117 /* For communication between note_reg_stored and its caller. */
10118 struct note_reg_stored_arg
10124 /* Called via note_stores, record in SET_SEEN whether X, which is written,
10125 is equal to ARG. */
10127 note_reg_stored (rtx x
, rtx setter ATTRIBUTE_UNUSED
, void *arg
)
10129 struct note_reg_stored_arg
*t
= (struct note_reg_stored_arg
*) arg
;
10134 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
10135 There must be exactly one insn that sets this pseudo; it will be
10136 deleted if all replacements succeed and we can prove that the register
10137 is not used after the loop. */
10140 try_copy_prop (const struct loop
*loop
, rtx replacement
, unsigned int regno
)
10142 /* This is the reg that we are copying from. */
10143 rtx reg_rtx
= regno_reg_rtx
[regno
];
10146 /* These help keep track of whether we replaced all uses of the reg. */
10147 int replaced_last
= 0;
10148 int store_is_first
= 0;
10150 for (insn
= next_insn_in_loop (loop
, loop
->scan_start
);
10152 insn
= next_insn_in_loop (loop
, insn
))
10156 /* Only substitute within one extended basic block from the initializing
10158 if (GET_CODE (insn
) == CODE_LABEL
&& init_insn
)
10161 if (! INSN_P (insn
))
10164 /* Is this the initializing insn? */
10165 set
= single_set (insn
);
10167 && GET_CODE (SET_DEST (set
)) == REG
10168 && REGNO (SET_DEST (set
)) == regno
)
10174 if (REGNO_FIRST_UID (regno
) == INSN_UID (insn
))
10175 store_is_first
= 1;
10178 /* Only substitute after seeing the initializing insn. */
10179 if (init_insn
&& insn
!= init_insn
)
10181 struct note_reg_stored_arg arg
;
10183 replace_loop_regs (insn
, reg_rtx
, replacement
);
10184 if (REGNO_LAST_UID (regno
) == INSN_UID (insn
))
10187 /* Stop replacing when REPLACEMENT is modified. */
10188 arg
.reg
= replacement
;
10190 note_stores (PATTERN (insn
), note_reg_stored
, &arg
);
10193 rtx note
= find_reg_note (insn
, REG_EQUAL
, NULL
);
10195 /* It is possible that we've turned previously valid REG_EQUAL to
10196 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10197 REPLACEMENT is modified, we get different meaning. */
10198 if (note
&& reg_mentioned_p (replacement
, XEXP (note
, 0)))
10199 remove_note (insn
, note
);
10206 if (apply_change_group ())
10208 if (loop_dump_stream
)
10209 fprintf (loop_dump_stream
, " Replaced reg %d", regno
);
10210 if (store_is_first
&& replaced_last
)
10215 /* Assume we're just deleting INIT_INSN. */
10217 /* Look for REG_RETVAL note. If we're deleting the end of
10218 the libcall sequence, the whole sequence can go. */
10219 retval_note
= find_reg_note (init_insn
, REG_RETVAL
, NULL_RTX
);
10220 /* If we found a REG_RETVAL note, find the first instruction
10221 in the sequence. */
10223 first
= XEXP (retval_note
, 0);
10225 /* Delete the instructions. */
10226 loop_delete_insns (first
, init_insn
);
10228 if (loop_dump_stream
)
10229 fprintf (loop_dump_stream
, ".\n");
10233 /* Replace all the instructions from FIRST up to and including LAST
10234 with NOTE_INSN_DELETED notes. */
10237 loop_delete_insns (rtx first
, rtx last
)
10241 if (loop_dump_stream
)
10242 fprintf (loop_dump_stream
, ", deleting init_insn (%d)",
10244 delete_insn (first
);
10246 /* If this was the LAST instructions we're supposed to delete,
10251 first
= NEXT_INSN (first
);
10255 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10256 loop LOOP if the order of the sets of these registers can be
10257 swapped. There must be exactly one insn within the loop that sets
10258 this pseudo followed immediately by a move insn that sets
10259 REPLACEMENT with REGNO. */
10261 try_swap_copy_prop (const struct loop
*loop
, rtx replacement
,
10262 unsigned int regno
)
10265 rtx set
= NULL_RTX
;
10266 unsigned int new_regno
;
10268 new_regno
= REGNO (replacement
);
10270 for (insn
= next_insn_in_loop (loop
, loop
->scan_start
);
10272 insn
= next_insn_in_loop (loop
, insn
))
10274 /* Search for the insn that copies REGNO to NEW_REGNO? */
10276 && (set
= single_set (insn
))
10277 && GET_CODE (SET_DEST (set
)) == REG
10278 && REGNO (SET_DEST (set
)) == new_regno
10279 && GET_CODE (SET_SRC (set
)) == REG
10280 && REGNO (SET_SRC (set
)) == regno
)
10284 if (insn
!= NULL_RTX
)
10289 /* Some DEF-USE info would come in handy here to make this
10290 function more general. For now, just check the previous insn
10291 which is the most likely candidate for setting REGNO. */
10293 prev_insn
= PREV_INSN (insn
);
10296 && (prev_set
= single_set (prev_insn
))
10297 && GET_CODE (SET_DEST (prev_set
)) == REG
10298 && REGNO (SET_DEST (prev_set
)) == regno
)
10301 (set (reg regno) (expr))
10302 (set (reg new_regno) (reg regno))
10304 so try converting this to:
10305 (set (reg new_regno) (expr))
10306 (set (reg regno) (reg new_regno))
10308 The former construct is often generated when a global
10309 variable used for an induction variable is shadowed by a
10310 register (NEW_REGNO). The latter construct improves the
10311 chances of GIV replacement and BIV elimination. */
10313 validate_change (prev_insn
, &SET_DEST (prev_set
),
10315 validate_change (insn
, &SET_DEST (set
),
10317 validate_change (insn
, &SET_SRC (set
),
10320 if (apply_change_group ())
10322 if (loop_dump_stream
)
10323 fprintf (loop_dump_stream
,
10324 " Swapped set of reg %d at %d with reg %d at %d.\n",
10325 regno
, INSN_UID (insn
),
10326 new_regno
, INSN_UID (prev_insn
));
10328 /* Update first use of REGNO. */
10329 if (REGNO_FIRST_UID (regno
) == INSN_UID (prev_insn
))
10330 REGNO_FIRST_UID (regno
) = INSN_UID (insn
);
10332 /* Now perform copy propagation to hopefully
10333 remove all uses of REGNO within the loop. */
10334 try_copy_prop (loop
, replacement
, regno
);
10340 /* Worker function for find_mem_in_note, called via for_each_rtx. */
10343 find_mem_in_note_1 (rtx
*x
, void *data
)
10345 if (*x
!= NULL_RTX
&& GET_CODE (*x
) == MEM
)
10347 rtx
*res
= (rtx
*) data
;
10354 /* Returns the first MEM found in NOTE by depth-first search. */
10357 find_mem_in_note (rtx note
)
10359 if (note
&& for_each_rtx (¬e
, find_mem_in_note_1
, ¬e
))
10364 /* Replace MEM with its associated pseudo register. This function is
10365 called from load_mems via for_each_rtx. DATA is actually a pointer
10366 to a structure describing the instruction currently being scanned
10367 and the MEM we are currently replacing. */
10370 replace_loop_mem (rtx
*mem
, void *data
)
10372 loop_replace_args
*args
= (loop_replace_args
*) data
;
10378 switch (GET_CODE (m
))
10384 /* We're not interested in the MEM associated with a
10385 CONST_DOUBLE, so there's no need to traverse into one. */
10389 /* This is not a MEM. */
10393 if (!rtx_equal_p (args
->match
, m
))
10394 /* This is not the MEM we are currently replacing. */
10397 /* Actually replace the MEM. */
10398 validate_change (args
->insn
, mem
, args
->replacement
, 1);
10404 replace_loop_mems (rtx insn
, rtx mem
, rtx reg
, int written
)
10406 loop_replace_args args
;
10410 args
.replacement
= reg
;
10412 for_each_rtx (&insn
, replace_loop_mem
, &args
);
10414 /* If we hoist a mem write out of the loop, then REG_EQUAL
10415 notes referring to the mem are no longer valid. */
10421 for (link
= ®_NOTES (insn
); (note
= *link
); link
= &XEXP (note
, 1))
10423 if (REG_NOTE_KIND (note
) == REG_EQUAL
10424 && (sub
= find_mem_in_note (note
))
10425 && true_dependence (mem
, VOIDmode
, sub
, rtx_varies_p
))
10427 /* Remove the note. */
10428 validate_change (NULL_RTX
, link
, XEXP (note
, 1), 1);
10435 /* Replace one register with another. Called through for_each_rtx; PX points
10436 to the rtx being scanned. DATA is actually a pointer to
10437 a structure of arguments. */
10440 replace_loop_reg (rtx
*px
, void *data
)
10443 loop_replace_args
*args
= (loop_replace_args
*) data
;
10448 if (x
== args
->match
)
10449 validate_change (args
->insn
, px
, args
->replacement
, 1);
10455 replace_loop_regs (rtx insn
, rtx reg
, rtx replacement
)
10457 loop_replace_args args
;
10461 args
.replacement
= replacement
;
10463 for_each_rtx (&insn
, replace_loop_reg
, &args
);
10466 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10467 (ignored in the interim). */
10470 loop_insn_emit_after (const struct loop
*loop ATTRIBUTE_UNUSED
,
10471 basic_block where_bb ATTRIBUTE_UNUSED
, rtx where_insn
,
10474 return emit_insn_after (pattern
, where_insn
);
10478 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
10479 in basic block WHERE_BB (ignored in the interim) within the loop
10480 otherwise hoist PATTERN into the loop pre-header. */
10483 loop_insn_emit_before (const struct loop
*loop
,
10484 basic_block where_bb ATTRIBUTE_UNUSED
,
10485 rtx where_insn
, rtx pattern
)
10488 return loop_insn_hoist (loop
, pattern
);
10489 return emit_insn_before (pattern
, where_insn
);
10493 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10494 WHERE_BB (ignored in the interim) within the loop. */
10497 loop_call_insn_emit_before (const struct loop
*loop ATTRIBUTE_UNUSED
,
10498 basic_block where_bb ATTRIBUTE_UNUSED
,
10499 rtx where_insn
, rtx pattern
)
10501 return emit_call_insn_before (pattern
, where_insn
);
10505 /* Hoist insn for PATTERN into the loop pre-header. */
10508 loop_insn_hoist (const struct loop
*loop
, rtx pattern
)
10510 return loop_insn_emit_before (loop
, 0, loop
->start
, pattern
);
10514 /* Hoist call insn for PATTERN into the loop pre-header. */
10517 loop_call_insn_hoist (const struct loop
*loop
, rtx pattern
)
10519 return loop_call_insn_emit_before (loop
, 0, loop
->start
, pattern
);
10523 /* Sink insn for PATTERN after the loop end. */
10526 loop_insn_sink (const struct loop
*loop
, rtx pattern
)
10528 return loop_insn_emit_before (loop
, 0, loop
->sink
, pattern
);
10531 /* bl->final_value can be either general_operand or PLUS of general_operand
10532 and constant. Emit sequence of instructions to load it into REG. */
10534 gen_load_of_final_value (rtx reg
, rtx final_value
)
10538 final_value
= force_operand (final_value
, reg
);
10539 if (final_value
!= reg
)
10540 emit_move_insn (reg
, final_value
);
10541 seq
= get_insns ();
10546 /* If the loop has multiple exits, emit insn for PATTERN before the
10547 loop to ensure that it will always be executed no matter how the
10548 loop exits. Otherwise, emit the insn for PATTERN after the loop,
10549 since this is slightly more efficient. */
10552 loop_insn_sink_or_swim (const struct loop
*loop
, rtx pattern
)
10554 if (loop
->exit_count
)
10555 return loop_insn_hoist (loop
, pattern
);
10557 return loop_insn_sink (loop
, pattern
);
10561 loop_ivs_dump (const struct loop
*loop
, FILE *file
, int verbose
)
10563 struct iv_class
*bl
;
10566 if (! loop
|| ! file
)
10569 for (bl
= LOOP_IVS (loop
)->list
; bl
; bl
= bl
->next
)
10572 fprintf (file
, "Loop %d: %d IV classes\n", loop
->num
, iv_num
);
10574 for (bl
= LOOP_IVS (loop
)->list
; bl
; bl
= bl
->next
)
10576 loop_iv_class_dump (bl
, file
, verbose
);
10577 fputc ('\n', file
);
10583 loop_iv_class_dump (const struct iv_class
*bl
, FILE *file
,
10584 int verbose ATTRIBUTE_UNUSED
)
10586 struct induction
*v
;
10590 if (! bl
|| ! file
)
10593 fprintf (file
, "IV class for reg %d, benefit %d\n",
10594 bl
->regno
, bl
->total_benefit
);
10596 fprintf (file
, " Init insn %d", INSN_UID (bl
->init_insn
));
10597 if (bl
->initial_value
)
10599 fprintf (file
, ", init val: ");
10600 print_simple_rtl (file
, bl
->initial_value
);
10602 if (bl
->initial_test
)
10604 fprintf (file
, ", init test: ");
10605 print_simple_rtl (file
, bl
->initial_test
);
10607 fputc ('\n', file
);
10609 if (bl
->final_value
)
10611 fprintf (file
, " Final val: ");
10612 print_simple_rtl (file
, bl
->final_value
);
10613 fputc ('\n', file
);
10616 if ((incr
= biv_total_increment (bl
)))
10618 fprintf (file
, " Total increment: ");
10619 print_simple_rtl (file
, incr
);
10620 fputc ('\n', file
);
10623 /* List the increments. */
10624 for (i
= 0, v
= bl
->biv
; v
; v
= v
->next_iv
, i
++)
10626 fprintf (file
, " Inc%d: insn %d, incr: ", i
, INSN_UID (v
->insn
));
10627 print_simple_rtl (file
, v
->add_val
);
10628 fputc ('\n', file
);
10631 /* List the givs. */
10632 for (i
= 0, v
= bl
->giv
; v
; v
= v
->next_iv
, i
++)
10634 fprintf (file
, " Giv%d: insn %d, benefit %d, ",
10635 i
, INSN_UID (v
->insn
), v
->benefit
);
10636 if (v
->giv_type
== DEST_ADDR
)
10637 print_simple_rtl (file
, v
->mem
);
10639 print_simple_rtl (file
, single_set (v
->insn
));
10640 fputc ('\n', file
);
10646 loop_biv_dump (const struct induction
*v
, FILE *file
, int verbose
)
10653 REGNO (v
->dest_reg
), INSN_UID (v
->insn
));
10654 fprintf (file
, " const ");
10655 print_simple_rtl (file
, v
->add_val
);
10657 if (verbose
&& v
->final_value
)
10659 fputc ('\n', file
);
10660 fprintf (file
, " final ");
10661 print_simple_rtl (file
, v
->final_value
);
10664 fputc ('\n', file
);
10669 loop_giv_dump (const struct induction
*v
, FILE *file
, int verbose
)
10674 if (v
->giv_type
== DEST_REG
)
10675 fprintf (file
, "Giv %d: insn %d",
10676 REGNO (v
->dest_reg
), INSN_UID (v
->insn
));
10678 fprintf (file
, "Dest address: insn %d",
10679 INSN_UID (v
->insn
));
10681 fprintf (file
, " src reg %d benefit %d",
10682 REGNO (v
->src_reg
), v
->benefit
);
10683 fprintf (file
, " lifetime %d",
10686 if (v
->replaceable
)
10687 fprintf (file
, " replaceable");
10689 if (v
->no_const_addval
)
10690 fprintf (file
, " ncav");
10692 if (v
->ext_dependent
)
10694 switch (GET_CODE (v
->ext_dependent
))
10697 fprintf (file
, " ext se");
10700 fprintf (file
, " ext ze");
10703 fprintf (file
, " ext tr");
10710 fputc ('\n', file
);
10711 fprintf (file
, " mult ");
10712 print_simple_rtl (file
, v
->mult_val
);
10714 fputc ('\n', file
);
10715 fprintf (file
, " add ");
10716 print_simple_rtl (file
, v
->add_val
);
10718 if (verbose
&& v
->final_value
)
10720 fputc ('\n', file
);
10721 fprintf (file
, " final ");
10722 print_simple_rtl (file
, v
->final_value
);
10725 fputc ('\n', file
);
10730 debug_ivs (const struct loop
*loop
)
10732 loop_ivs_dump (loop
, stderr
, 1);
10737 debug_iv_class (const struct iv_class
*bl
)
10739 loop_iv_class_dump (bl
, stderr
, 1);
10744 debug_biv (const struct induction
*v
)
10746 loop_biv_dump (v
, stderr
, 1);
10751 debug_giv (const struct induction
*v
)
10753 loop_giv_dump (v
, stderr
, 1);
10757 #define LOOP_BLOCK_NUM_1(INSN) \
10758 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10760 /* The notes do not have an assigned block, so look at the next insn. */
10761 #define LOOP_BLOCK_NUM(INSN) \
10762 ((INSN) ? (GET_CODE (INSN) == NOTE \
10763 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10764 : LOOP_BLOCK_NUM_1 (INSN)) \
10767 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10770 loop_dump_aux (const struct loop
*loop
, FILE *file
,
10771 int verbose ATTRIBUTE_UNUSED
)
10775 if (! loop
|| ! file
)
10778 /* Print diagnostics to compare our concept of a loop with
10779 what the loop notes say. */
10780 if (! PREV_INSN (BB_HEAD (loop
->first
))
10781 || GET_CODE (PREV_INSN (BB_HEAD (loop
->first
))) != NOTE
10782 || NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (loop
->first
)))
10783 != NOTE_INSN_LOOP_BEG
)
10784 fprintf (file
, ";; No NOTE_INSN_LOOP_BEG at %d\n",
10785 INSN_UID (PREV_INSN (BB_HEAD (loop
->first
))));
10786 if (! NEXT_INSN (BB_END (loop
->last
))
10787 || GET_CODE (NEXT_INSN (BB_END (loop
->last
))) != NOTE
10788 || NOTE_LINE_NUMBER (NEXT_INSN (BB_END (loop
->last
)))
10789 != NOTE_INSN_LOOP_END
)
10790 fprintf (file
, ";; No NOTE_INSN_LOOP_END at %d\n",
10791 INSN_UID (NEXT_INSN (BB_END (loop
->last
))));
10796 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10797 LOOP_BLOCK_NUM (loop
->start
),
10798 LOOP_INSN_UID (loop
->start
),
10799 LOOP_BLOCK_NUM (loop
->cont
),
10800 LOOP_INSN_UID (loop
->cont
),
10801 LOOP_BLOCK_NUM (loop
->cont
),
10802 LOOP_INSN_UID (loop
->cont
),
10803 LOOP_BLOCK_NUM (loop
->vtop
),
10804 LOOP_INSN_UID (loop
->vtop
),
10805 LOOP_BLOCK_NUM (loop
->end
),
10806 LOOP_INSN_UID (loop
->end
));
10807 fprintf (file
, ";; top %d (%d), scan start %d (%d)\n",
10808 LOOP_BLOCK_NUM (loop
->top
),
10809 LOOP_INSN_UID (loop
->top
),
10810 LOOP_BLOCK_NUM (loop
->scan_start
),
10811 LOOP_INSN_UID (loop
->scan_start
));
10812 fprintf (file
, ";; exit_count %d", loop
->exit_count
);
10813 if (loop
->exit_count
)
10815 fputs (", labels:", file
);
10816 for (label
= loop
->exit_labels
; label
; label
= LABEL_NEXTREF (label
))
10818 fprintf (file
, " %d ",
10819 LOOP_INSN_UID (XEXP (label
, 0)));
10822 fputs ("\n", file
);
10824 /* This can happen when a marked loop appears as two nested loops,
10825 say from while (a || b) {}. The inner loop won't match
10826 the loop markers but the outer one will. */
10827 if (LOOP_BLOCK_NUM (loop
->cont
) != loop
->latch
->index
)
10828 fprintf (file
, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
10832 /* Call this function from the debugger to dump LOOP. */
10835 debug_loop (const struct loop
*loop
)
10837 flow_loop_dump (loop
, stderr
, loop_dump_aux
, 1);
10840 /* Call this function from the debugger to dump LOOPS. */
10843 debug_loops (const struct loops
*loops
)
10845 flow_loops_dump (loops
, stderr
, loop_dump_aux
, 1);