* config/arm/arm.h (REG_CLASS_NAMES): Add missing comma.
[official-gcc.git] / gcc / loop.c
blob55c9834b2885c787c1f563f655a625d1db80d2da
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
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. */
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "rtl.h"
50 #include "tm_p.h"
51 #include "function.h"
52 #include "expr.h"
53 #include "hard-reg-set.h"
54 #include "basic-block.h"
55 #include "insn-config.h"
56 #include "regs.h"
57 #include "recog.h"
58 #include "flags.h"
59 #include "real.h"
60 #include "loop.h"
61 #include "cselib.h"
62 #include "except.h"
63 #include "toplev.h"
64 #include "predict.h"
65 #include "insn-flags.h"
66 #include "optabs.h"
67 #include "cfgloop.h"
68 #include "ggc.h"
70 /* Not really meaningful values, but at least something. */
71 #ifndef SIMULTANEOUS_PREFETCHES
72 #define SIMULTANEOUS_PREFETCHES 3
73 #endif
74 #ifndef PREFETCH_BLOCK
75 #define PREFETCH_BLOCK 32
76 #endif
77 #ifndef HAVE_prefetch
78 #define HAVE_prefetch 0
79 #define CODE_FOR_prefetch 0
80 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
81 #endif
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
101 #endif
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
107 #endif
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
112 #endif
114 /* Define what we mean by a "low" iteration count. */
115 #ifndef PREFETCH_LOW_LOOPCNT
116 #define PREFETCH_LOW_LOOPCNT 32
117 #endif
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
123 #endif
125 /* Do not prefetch accesses with an extreme stride. */
126 #ifndef PREFETCH_NO_EXTREME_STRIDE
127 #define PREFETCH_NO_EXTREME_STRIDE 1
128 #endif
130 /* Define what we mean by an "extreme" stride. */
131 #ifndef PREFETCH_EXTREME_STRIDE
132 #define PREFETCH_EXTREME_STRIDE 4096
133 #endif
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
139 #endif
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
145 #endif
147 /* Do not handle reversed order prefetches (negative stride). */
148 #ifndef PREFETCH_NO_REVERSE_ORDER
149 #define PREFETCH_NO_REVERSE_ORDER 1
150 #endif
152 /* Prefetch even if the GIV is in conditional code. */
153 #ifndef PREFETCH_CONDITIONAL
154 #define PREFETCH_CONDITIONAL 1
155 #endif
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. */
173 int *uid_luid;
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
185 next few tables. */
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. */
200 struct movable
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
229 invariant. */
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);
261 #if 0
262 static void replace_call_address (rtx, rtx, rtx);
263 #endif
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 *,
272 struct loop_regs *);
273 static void add_label_notes (rtx, rtx);
274 static void move_movables (struct loop *loop, struct loop_movables *, int,
275 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,
304 rtx *);
305 static void update_giv_derive (const struct loop *, rtx);
306 static void check_ext_dependent_givs (const struct loop *, struct iv_class *);
307 static int basic_induction_var (const struct loop *, rtx, enum machine_mode,
308 rtx, rtx, rtx *, rtx *, rtx **);
309 static rtx simplify_giv_expr (const struct loop *, rtx, rtx *, int *);
310 static int general_induction_var (const struct loop *loop, rtx, rtx *, rtx *,
311 rtx *, rtx *, int, int *, enum machine_mode);
312 static int consec_sets_giv (const struct loop *, int, rtx, rtx, rtx, rtx *,
313 rtx *, rtx *, rtx *);
314 static int check_dbra_loop (struct loop *, int);
315 static rtx express_from_1 (rtx, rtx, rtx);
316 static rtx combine_givs_p (struct induction *, struct induction *);
317 static int cmp_combine_givs_stats (const void *, const void *);
318 static void combine_givs (struct loop_regs *, struct iv_class *);
319 static int product_cheap_p (rtx, rtx);
320 static int maybe_eliminate_biv (const struct loop *, struct iv_class *, int,
321 int, int);
322 static int maybe_eliminate_biv_1 (const struct loop *, rtx, rtx,
323 struct iv_class *, int, basic_block, rtx);
324 static int last_use_this_basic_block (rtx, rtx);
325 static void record_initial (rtx, rtx, void *);
326 static void update_reg_last_use (rtx, rtx);
327 static rtx next_insn_in_loop (const struct loop *, rtx);
328 static void loop_regs_scan (const struct loop *, int);
329 static int count_insns_in_loop (const struct loop *);
330 static int find_mem_in_note_1 (rtx *, void *);
331 static rtx find_mem_in_note (rtx);
332 static void load_mems (const struct loop *);
333 static int insert_loop_mem (rtx *, void *);
334 static int replace_loop_mem (rtx *, void *);
335 static void replace_loop_mems (rtx, rtx, rtx, int);
336 static int replace_loop_reg (rtx *, void *);
337 static void replace_loop_regs (rtx insn, rtx, rtx);
338 static void note_reg_stored (rtx, rtx, void *);
339 static void try_copy_prop (const struct loop *, rtx, unsigned int);
340 static void try_swap_copy_prop (const struct loop *, rtx, unsigned int);
341 static rtx check_insn_for_givs (struct loop *, rtx, int, int);
342 static rtx check_insn_for_bivs (struct loop *, rtx, int, int);
343 static rtx gen_add_mult (rtx, rtx, rtx, rtx);
344 static void loop_regs_update (const struct loop *, rtx);
345 static int iv_add_mult_cost (rtx, rtx, rtx, rtx);
347 static rtx loop_insn_emit_after (const struct loop *, basic_block, rtx, rtx);
348 static rtx loop_call_insn_emit_before (const struct loop *, basic_block,
349 rtx, rtx);
350 static rtx loop_call_insn_hoist (const struct loop *, rtx);
351 static rtx loop_insn_sink_or_swim (const struct loop *, rtx);
353 static void loop_dump_aux (const struct loop *, FILE *, int);
354 static void loop_delete_insns (rtx, rtx);
355 static HOST_WIDE_INT remove_constant_addition (rtx *);
356 static rtx gen_load_of_final_value (rtx, rtx);
357 void debug_ivs (const struct loop *);
358 void debug_iv_class (const struct iv_class *);
359 void debug_biv (const struct induction *);
360 void debug_giv (const struct induction *);
361 void debug_loop (const struct loop *);
362 void debug_loops (const struct loops *);
364 typedef struct loop_replace_args
366 rtx match;
367 rtx replacement;
368 rtx insn;
369 } loop_replace_args;
371 /* Nonzero iff INSN is between START and END, inclusive. */
372 #define INSN_IN_RANGE_P(INSN, START, END) \
373 (INSN_UID (INSN) < max_uid_for_loop \
374 && INSN_LUID (INSN) >= INSN_LUID (START) \
375 && INSN_LUID (INSN) <= INSN_LUID (END))
377 /* Indirect_jump_in_function is computed once per function. */
378 static int indirect_jump_in_function;
379 static int indirect_jump_in_function_p (rtx);
381 static int compute_luids (rtx, rtx, int);
383 static int biv_elimination_giv_has_0_offset (struct induction *,
384 struct induction *, rtx);
386 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
387 copy the value of the strength reduced giv to its original register. */
388 static int copy_cost;
390 /* Cost of using a register, to normalize the benefits of a giv. */
391 static int reg_address_cost;
393 void
394 init_loop (void)
396 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
398 reg_address_cost = address_cost (reg, SImode);
400 copy_cost = COSTS_N_INSNS (1);
403 /* Compute the mapping from uids to luids.
404 LUIDs are numbers assigned to insns, like uids,
405 except that luids increase monotonically through the code.
406 Start at insn START and stop just before END. Assign LUIDs
407 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
408 static int
409 compute_luids (rtx start, rtx end, int prev_luid)
411 int i;
412 rtx insn;
414 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
416 if (INSN_UID (insn) >= max_uid_for_loop)
417 continue;
418 /* Don't assign luids to line-number NOTEs, so that the distance in
419 luids between two insns is not affected by -g. */
420 if (GET_CODE (insn) != NOTE
421 || NOTE_LINE_NUMBER (insn) <= 0)
422 uid_luid[INSN_UID (insn)] = ++i;
423 else
424 /* Give a line number note the same luid as preceding insn. */
425 uid_luid[INSN_UID (insn)] = i;
427 return i + 1;
430 /* Entry point of this file. Perform loop optimization
431 on the current function. F is the first insn of the function
432 and DUMPFILE is a stream for output of a trace of actions taken
433 (or 0 if none should be output). */
435 void
436 loop_optimize (rtx f, FILE *dumpfile, int flags)
438 rtx insn;
439 int i;
440 struct loops loops_data;
441 struct loops *loops = &loops_data;
442 struct loop_info *loops_info;
444 loop_dump_stream = dumpfile;
446 init_recog_no_volatile ();
448 max_reg_before_loop = max_reg_num ();
449 loop_max_reg = max_reg_before_loop;
451 regs_may_share = 0;
453 /* Count the number of loops. */
455 max_loop_num = 0;
456 for (insn = f; insn; insn = NEXT_INSN (insn))
458 if (GET_CODE (insn) == NOTE
459 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
460 max_loop_num++;
463 /* Don't waste time if no loops. */
464 if (max_loop_num == 0)
465 return;
467 loops->num = max_loop_num;
469 /* Get size to use for tables indexed by uids.
470 Leave some space for labels allocated by find_and_verify_loops. */
471 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
473 uid_luid = xcalloc (max_uid_for_loop, sizeof (int));
474 uid_loop = xcalloc (max_uid_for_loop, sizeof (struct loop *));
476 /* Allocate storage for array of loops. */
477 loops->array = xcalloc (loops->num, sizeof (struct loop));
479 /* Find and process each loop.
480 First, find them, and record them in order of their beginnings. */
481 find_and_verify_loops (f, loops);
483 /* Allocate and initialize auxiliary loop information. */
484 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
485 for (i = 0; i < (int) loops->num; i++)
486 loops->array[i].aux = loops_info + i;
488 /* Now find all register lifetimes. This must be done after
489 find_and_verify_loops, because it might reorder the insns in the
490 function. */
491 reg_scan (f, max_reg_before_loop, 1);
493 /* This must occur after reg_scan so that registers created by gcse
494 will have entries in the register tables.
496 We could have added a call to reg_scan after gcse_main in toplev.c,
497 but moving this call to init_alias_analysis is more efficient. */
498 init_alias_analysis ();
500 /* See if we went too far. Note that get_max_uid already returns
501 one more that the maximum uid of all insn. */
502 if (get_max_uid () > max_uid_for_loop)
503 abort ();
504 /* Now reset it to the actual size we need. See above. */
505 max_uid_for_loop = get_max_uid ();
507 /* find_and_verify_loops has already called compute_luids, but it
508 might have rearranged code afterwards, so we need to recompute
509 the luids now. */
510 compute_luids (f, NULL_RTX, 0);
512 /* Don't leave gaps in uid_luid for insns that have been
513 deleted. It is possible that the first or last insn
514 using some register has been deleted by cross-jumping.
515 Make sure that uid_luid for that former insn's uid
516 points to the general area where that insn used to be. */
517 for (i = 0; i < max_uid_for_loop; i++)
519 uid_luid[0] = uid_luid[i];
520 if (uid_luid[0] != 0)
521 break;
523 for (i = 0; i < max_uid_for_loop; i++)
524 if (uid_luid[i] == 0)
525 uid_luid[i] = uid_luid[i - 1];
527 /* Determine if the function has indirect jump. On some systems
528 this prevents low overhead loop instructions from being used. */
529 indirect_jump_in_function = indirect_jump_in_function_p (f);
531 /* Now scan the loops, last ones first, since this means inner ones are done
532 before outer ones. */
533 for (i = max_loop_num - 1; i >= 0; i--)
535 struct loop *loop = &loops->array[i];
537 if (! loop->invalid && loop->end)
539 scan_loop (loop, flags);
540 ggc_collect ();
544 end_alias_analysis ();
546 /* Clean up. */
547 for (i = 0; i < (int) loops->num; i++)
548 free (loops_info[i].mems);
550 free (uid_luid);
551 free (uid_loop);
552 free (loops_info);
553 free (loops->array);
556 /* Returns the next insn, in execution order, after INSN. START and
557 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
558 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
559 insn-stream; it is used with loops that are entered near the
560 bottom. */
562 static rtx
563 next_insn_in_loop (const struct loop *loop, rtx insn)
565 insn = NEXT_INSN (insn);
567 if (insn == loop->end)
569 if (loop->top)
570 /* Go to the top of the loop, and continue there. */
571 insn = loop->top;
572 else
573 /* We're done. */
574 insn = NULL_RTX;
577 if (insn == loop->scan_start)
578 /* We're done. */
579 insn = NULL_RTX;
581 return insn;
584 /* Find any register references hidden inside X and add them to
585 the dependency list DEPS. This is used to look inside CLOBBER (MEM
586 when checking whether a PARALLEL can be pulled out of a loop. */
588 static rtx
589 find_regs_nested (rtx deps, rtx x)
591 enum rtx_code code = GET_CODE (x);
592 if (code == REG)
593 deps = gen_rtx_EXPR_LIST (VOIDmode, x, deps);
594 else
596 const char *fmt = GET_RTX_FORMAT (code);
597 int i, j;
598 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
600 if (fmt[i] == 'e')
601 deps = find_regs_nested (deps, XEXP (x, i));
602 else if (fmt[i] == 'E')
603 for (j = 0; j < XVECLEN (x, i); j++)
604 deps = find_regs_nested (deps, XVECEXP (x, i, j));
607 return deps;
610 /* Optimize one loop described by LOOP. */
612 /* ??? Could also move memory writes out of loops if the destination address
613 is invariant, the source is invariant, the memory write is not volatile,
614 and if we can prove that no read inside the loop can read this address
615 before the write occurs. If there is a read of this address after the
616 write, then we can also mark the memory read as invariant. */
618 static void
619 scan_loop (struct loop *loop, int flags)
621 struct loop_info *loop_info = LOOP_INFO (loop);
622 struct loop_regs *regs = LOOP_REGS (loop);
623 int i;
624 rtx loop_start = loop->start;
625 rtx loop_end = loop->end;
626 rtx p;
627 /* 1 if we are scanning insns that could be executed zero times. */
628 int maybe_never = 0;
629 /* 1 if we are scanning insns that might never be executed
630 due to a subroutine call which might exit before they are reached. */
631 int call_passed = 0;
632 /* Number of insns in the loop. */
633 int insn_count;
634 int tem;
635 rtx temp, update_start, update_end;
636 /* The SET from an insn, if it is the only SET in the insn. */
637 rtx set, set1;
638 /* Chain describing insns movable in current loop. */
639 struct loop_movables *movables = LOOP_MOVABLES (loop);
640 /* Ratio of extra register life span we can justify
641 for saving an instruction. More if loop doesn't call subroutines
642 since in that case saving an insn makes more difference
643 and more registers are available. */
644 int threshold;
645 /* Nonzero if we are scanning instructions in a sub-loop. */
646 int loop_depth = 0;
647 int in_libcall;
649 loop->top = 0;
651 movables->head = 0;
652 movables->last = 0;
654 /* Determine whether this loop starts with a jump down to a test at
655 the end. This will occur for a small number of loops with a test
656 that is too complex to duplicate in front of the loop.
658 We search for the first insn or label in the loop, skipping NOTEs.
659 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
660 (because we might have a loop executed only once that contains a
661 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
662 (in case we have a degenerate loop).
664 Note that if we mistakenly think that a loop is entered at the top
665 when, in fact, it is entered at the exit test, the only effect will be
666 slightly poorer optimization. Making the opposite error can generate
667 incorrect code. Since very few loops now start with a jump to the
668 exit test, the code here to detect that case is very conservative. */
670 for (p = NEXT_INSN (loop_start);
671 p != loop_end
672 && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
673 && (GET_CODE (p) != NOTE
674 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
675 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
676 p = NEXT_INSN (p))
679 loop->scan_start = p;
681 /* If loop end is the end of the current function, then emit a
682 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
683 note insn. This is the position we use when sinking insns out of
684 the loop. */
685 if (NEXT_INSN (loop->end) != 0)
686 loop->sink = NEXT_INSN (loop->end);
687 else
688 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
690 /* Set up variables describing this loop. */
691 prescan_loop (loop);
692 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
694 /* If loop has a jump before the first label,
695 the true entry is the target of that jump.
696 Start scan from there.
697 But record in LOOP->TOP the place where the end-test jumps
698 back to so we can scan that after the end of the loop. */
699 if (GET_CODE (p) == JUMP_INSN
700 /* Loop entry must be unconditional jump (and not a RETURN) */
701 && any_uncondjump_p (p)
702 && JUMP_LABEL (p) != 0
703 /* Check to see whether the jump actually
704 jumps out of the loop (meaning it's no loop).
705 This case can happen for things like
706 do {..} while (0). If this label was generated previously
707 by loop, we can't tell anything about it and have to reject
708 the loop. */
709 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
711 loop->top = next_label (loop->scan_start);
712 loop->scan_start = JUMP_LABEL (p);
715 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
716 as required by loop_reg_used_before_p. So skip such loops. (This
717 test may never be true, but it's best to play it safe.)
719 Also, skip loops where we do not start scanning at a label. This
720 test also rejects loops starting with a JUMP_INSN that failed the
721 test above. */
723 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
724 || GET_CODE (loop->scan_start) != CODE_LABEL)
726 if (loop_dump_stream)
727 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
728 INSN_UID (loop_start), INSN_UID (loop_end));
729 return;
732 /* Allocate extra space for REGs that might be created by load_mems.
733 We allocate a little extra slop as well, in the hopes that we
734 won't have to reallocate the regs array. */
735 loop_regs_scan (loop, loop_info->mems_idx + 16);
736 insn_count = count_insns_in_loop (loop);
738 if (loop_dump_stream)
740 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
741 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
742 if (loop->cont)
743 fprintf (loop_dump_stream, "Continue at insn %d.\n",
744 INSN_UID (loop->cont));
747 /* Scan through the loop finding insns that are safe to move.
748 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
749 this reg will be considered invariant for subsequent insns.
750 We consider whether subsequent insns use the reg
751 in deciding whether it is worth actually moving.
753 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
754 and therefore it is possible that the insns we are scanning
755 would never be executed. At such times, we must make sure
756 that it is safe to execute the insn once instead of zero times.
757 When MAYBE_NEVER is 0, all insns will be executed at least once
758 so that is not a problem. */
760 for (in_libcall = 0, p = next_insn_in_loop (loop, loop->scan_start);
761 p != NULL_RTX;
762 p = next_insn_in_loop (loop, p))
764 if (in_libcall && INSN_P (p) && find_reg_note (p, REG_RETVAL, NULL_RTX))
765 in_libcall--;
766 if (GET_CODE (p) == INSN)
768 temp = find_reg_note (p, REG_LIBCALL, NULL_RTX);
769 if (temp)
770 in_libcall++;
771 if (! in_libcall
772 && (set = single_set (p))
773 && GET_CODE (SET_DEST (set)) == REG
774 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
775 && SET_DEST (set) != pic_offset_table_rtx
776 #endif
777 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
779 int tem1 = 0;
780 int tem2 = 0;
781 int move_insn = 0;
782 int insert_temp = 0;
783 rtx src = SET_SRC (set);
784 rtx dependencies = 0;
786 /* Figure out what to use as a source of this insn. If a
787 REG_EQUIV note is given or if a REG_EQUAL note with a
788 constant operand is specified, use it as the source and
789 mark that we should move this insn by calling
790 emit_move_insn rather that duplicating the insn.
792 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL
793 note is present. */
794 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
795 if (temp)
796 src = XEXP (temp, 0), move_insn = 1;
797 else
799 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
800 if (temp && CONSTANT_P (XEXP (temp, 0)))
801 src = XEXP (temp, 0), move_insn = 1;
802 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
804 src = XEXP (temp, 0);
805 /* A libcall block can use regs that don't appear in
806 the equivalent expression. To move the libcall,
807 we must move those regs too. */
808 dependencies = libcall_other_reg (p, src);
812 /* For parallels, add any possible uses to the dependencies, as
813 we can't move the insn without resolving them first.
814 MEMs inside CLOBBERs may also reference registers; these
815 count as implicit uses. */
816 if (GET_CODE (PATTERN (p)) == PARALLEL)
818 for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
820 rtx x = XVECEXP (PATTERN (p), 0, i);
821 if (GET_CODE (x) == USE)
822 dependencies
823 = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0),
824 dependencies);
825 else if (GET_CODE (x) == CLOBBER
826 && GET_CODE (XEXP (x, 0)) == MEM)
827 dependencies = find_regs_nested (dependencies,
828 XEXP (XEXP (x, 0), 0));
832 if (/* The register is used in basic blocks other
833 than the one where it is set (meaning that
834 something after this point in the loop might
835 depend on its value before the set). */
836 ! reg_in_basic_block_p (p, SET_DEST (set))
837 /* And the set is not guaranteed to be executed once
838 the loop starts, or the value before the set is
839 needed before the set occurs...
841 ??? Note we have quadratic behavior here, mitigated
842 by the fact that the previous test will often fail for
843 large loops. Rather than re-scanning the entire loop
844 each time for register usage, we should build tables
845 of the register usage and use them here instead. */
846 && (maybe_never
847 || loop_reg_used_before_p (loop, set, p)))
848 /* It is unsafe to move the set. However, it may be OK to
849 move the source into a new pseudo, and substitute a
850 reg-to-reg copy for the original insn.
852 This code used to consider it OK to move a set of a variable
853 which was not created by the user and not used in an exit
854 test.
855 That behavior is incorrect and was removed. */
856 insert_temp = 1;
858 /* Don't try to optimize a MODE_CC set with a constant
859 source. It probably will be combined with a conditional
860 jump. */
861 if (GET_MODE_CLASS (GET_MODE (SET_DEST (set))) == MODE_CC
862 && CONSTANT_P (src))
864 /* Don't try to optimize a register that was made
865 by loop-optimization for an inner loop.
866 We don't know its life-span, so we can't compute
867 the benefit. */
868 else if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
870 /* Don't move the source and add a reg-to-reg copy:
871 - with -Os (this certainly increases size),
872 - if the mode doesn't support copy operations (obviously),
873 - if the source is already a reg (the motion will gain nothing),
874 - if the source is a legitimate constant (likewise). */
875 else if (insert_temp
876 && (optimize_size
877 || ! can_copy_p (GET_MODE (SET_SRC (set)))
878 || GET_CODE (SET_SRC (set)) == REG
879 || (CONSTANT_P (SET_SRC (set))
880 && LEGITIMATE_CONSTANT_P (SET_SRC (set)))))
882 else if ((tem = loop_invariant_p (loop, src))
883 && (dependencies == 0
884 || (tem2
885 = loop_invariant_p (loop, dependencies)) != 0)
886 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
887 || (tem1
888 = consec_sets_invariant_p
889 (loop, SET_DEST (set),
890 regs->array[REGNO (SET_DEST (set))].set_in_loop,
891 p)))
892 /* If the insn can cause a trap (such as divide by zero),
893 can't move it unless it's guaranteed to be executed
894 once loop is entered. Even a function call might
895 prevent the trap insn from being reached
896 (since it might exit!) */
897 && ! ((maybe_never || call_passed)
898 && may_trap_p (src)))
900 struct movable *m;
901 int regno = REGNO (SET_DEST (set));
903 /* A potential lossage is where we have a case where two insns
904 can be combined as long as they are both in the loop, but
905 we move one of them outside the loop. For large loops,
906 this can lose. The most common case of this is the address
907 of a function being called.
909 Therefore, if this register is marked as being used
910 exactly once if we are in a loop with calls
911 (a "large loop"), see if we can replace the usage of
912 this register with the source of this SET. If we can,
913 delete this insn.
915 Don't do this if P has a REG_RETVAL note or if we have
916 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
918 if (loop_info->has_call
919 && regs->array[regno].single_usage != 0
920 && regs->array[regno].single_usage != const0_rtx
921 && REGNO_FIRST_UID (regno) == INSN_UID (p)
922 && (REGNO_LAST_UID (regno)
923 == INSN_UID (regs->array[regno].single_usage))
924 && regs->array[regno].set_in_loop == 1
925 && GET_CODE (SET_SRC (set)) != ASM_OPERANDS
926 && ! side_effects_p (SET_SRC (set))
927 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
928 && (! SMALL_REGISTER_CLASSES
929 || (! (GET_CODE (SET_SRC (set)) == REG
930 && (REGNO (SET_SRC (set))
931 < FIRST_PSEUDO_REGISTER))))
932 /* This test is not redundant; SET_SRC (set) might be
933 a call-clobbered register and the life of REGNO
934 might span a call. */
935 && ! modified_between_p (SET_SRC (set), p,
936 regs->array[regno].single_usage)
937 && no_labels_between_p (p,
938 regs->array[regno].single_usage)
939 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
940 regs->array[regno].single_usage))
942 /* Replace any usage in a REG_EQUAL note. Must copy
943 the new source, so that we don't get rtx sharing
944 between the SET_SOURCE and REG_NOTES of insn p. */
945 REG_NOTES (regs->array[regno].single_usage)
946 = (replace_rtx
947 (REG_NOTES (regs->array[regno].single_usage),
948 SET_DEST (set), copy_rtx (SET_SRC (set))));
950 delete_insn (p);
951 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set));
952 i++)
953 regs->array[regno+i].set_in_loop = 0;
954 continue;
957 m = xmalloc (sizeof (struct movable));
958 m->next = 0;
959 m->insn = p;
960 m->set_src = src;
961 m->dependencies = dependencies;
962 m->set_dest = SET_DEST (set);
963 m->force = 0;
964 m->consec
965 = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
966 m->done = 0;
967 m->forces = 0;
968 m->partial = 0;
969 m->move_insn = move_insn;
970 m->move_insn_first = 0;
971 m->insert_temp = insert_temp;
972 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
973 m->savemode = VOIDmode;
974 m->regno = regno;
975 /* Set M->cond if either loop_invariant_p
976 or consec_sets_invariant_p returned 2
977 (only conditionally invariant). */
978 m->cond = ((tem | tem1 | tem2) > 1);
979 m->global = LOOP_REG_GLOBAL_P (loop, regno);
980 m->match = 0;
981 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
982 m->savings = regs->array[regno].n_times_set;
983 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
984 m->savings += libcall_benefit (p);
985 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
986 regs->array[regno+i].set_in_loop = move_insn ? -2 : -1;
987 /* Add M to the end of the chain MOVABLES. */
988 loop_movables_add (movables, m);
990 if (m->consec > 0)
992 /* It is possible for the first instruction to have a
993 REG_EQUAL note but a non-invariant SET_SRC, so we must
994 remember the status of the first instruction in case
995 the last instruction doesn't have a REG_EQUAL note. */
996 m->move_insn_first = m->move_insn;
998 /* Skip this insn, not checking REG_LIBCALL notes. */
999 p = next_nonnote_insn (p);
1000 /* Skip the consecutive insns, if there are any. */
1001 p = skip_consec_insns (p, m->consec);
1002 /* Back up to the last insn of the consecutive group. */
1003 p = prev_nonnote_insn (p);
1005 /* We must now reset m->move_insn, m->is_equiv, and
1006 possibly m->set_src to correspond to the effects of
1007 all the insns. */
1008 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
1009 if (temp)
1010 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1011 else
1013 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
1014 if (temp && CONSTANT_P (XEXP (temp, 0)))
1015 m->set_src = XEXP (temp, 0), m->move_insn = 1;
1016 else
1017 m->move_insn = 0;
1020 m->is_equiv
1021 = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
1024 /* If this register is always set within a STRICT_LOW_PART
1025 or set to zero, then its high bytes are constant.
1026 So clear them outside the loop and within the loop
1027 just load the low bytes.
1028 We must check that the machine has an instruction to do so.
1029 Also, if the value loaded into the register
1030 depends on the same register, this cannot be done. */
1031 else if (SET_SRC (set) == const0_rtx
1032 && GET_CODE (NEXT_INSN (p)) == INSN
1033 && (set1 = single_set (NEXT_INSN (p)))
1034 && GET_CODE (set1) == SET
1035 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
1036 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
1037 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
1038 == SET_DEST (set))
1039 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
1041 int regno = REGNO (SET_DEST (set));
1042 if (regs->array[regno].set_in_loop == 2)
1044 struct movable *m;
1045 m = xmalloc (sizeof (struct movable));
1046 m->next = 0;
1047 m->insn = p;
1048 m->set_dest = SET_DEST (set);
1049 m->dependencies = 0;
1050 m->force = 0;
1051 m->consec = 0;
1052 m->done = 0;
1053 m->forces = 0;
1054 m->move_insn = 0;
1055 m->move_insn_first = 0;
1056 m->insert_temp = insert_temp;
1057 m->partial = 1;
1058 /* If the insn may not be executed on some cycles,
1059 we can't clear the whole reg; clear just high part.
1060 Not even if the reg is used only within this loop.
1061 Consider this:
1062 while (1)
1063 while (s != t) {
1064 if (foo ()) x = *s;
1065 use (x);
1067 Clearing x before the inner loop could clobber a value
1068 being saved from the last time around the outer loop.
1069 However, if the reg is not used outside this loop
1070 and all uses of the register are in the same
1071 basic block as the store, there is no problem.
1073 If this insn was made by loop, we don't know its
1074 INSN_LUID and hence must make a conservative
1075 assumption. */
1076 m->global = (INSN_UID (p) >= max_uid_for_loop
1077 || LOOP_REG_GLOBAL_P (loop, regno)
1078 || (labels_in_range_p
1079 (p, REGNO_FIRST_LUID (regno))));
1080 if (maybe_never && m->global)
1081 m->savemode = GET_MODE (SET_SRC (set1));
1082 else
1083 m->savemode = VOIDmode;
1084 m->regno = regno;
1085 m->cond = 0;
1086 m->match = 0;
1087 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1088 m->savings = 1;
1089 for (i = 0;
1090 i < LOOP_REGNO_NREGS (regno, SET_DEST (set));
1091 i++)
1092 regs->array[regno+i].set_in_loop = -1;
1093 /* Add M to the end of the chain MOVABLES. */
1094 loop_movables_add (movables, m);
1099 /* Past a call insn, we get to insns which might not be executed
1100 because the call might exit. This matters for insns that trap.
1101 Constant and pure call insns always return, so they don't count. */
1102 else if (GET_CODE (p) == CALL_INSN && ! CONST_OR_PURE_CALL_P (p))
1103 call_passed = 1;
1104 /* Past a label or a jump, we get to insns for which we
1105 can't count on whether or how many times they will be
1106 executed during each iteration. Therefore, we can
1107 only move out sets of trivial variables
1108 (those not used after the loop). */
1109 /* Similar code appears twice in strength_reduce. */
1110 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1111 /* If we enter the loop in the middle, and scan around to the
1112 beginning, don't set maybe_never for that. This must be an
1113 unconditional jump, otherwise the code at the top of the
1114 loop might never be executed. Unconditional jumps are
1115 followed by a barrier then the loop_end. */
1116 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
1117 && NEXT_INSN (NEXT_INSN (p)) == loop_end
1118 && any_uncondjump_p (p)))
1119 maybe_never = 1;
1120 else if (GET_CODE (p) == NOTE)
1122 /* At the virtual top of a converted loop, insns are again known to
1123 be executed: logically, the loop begins here even though the exit
1124 code has been duplicated. */
1125 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1126 maybe_never = call_passed = 0;
1127 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1128 loop_depth++;
1129 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1130 loop_depth--;
1134 /* If one movable subsumes another, ignore that other. */
1136 ignore_some_movables (movables);
1138 /* For each movable insn, see if the reg that it loads
1139 leads when it dies right into another conditionally movable insn.
1140 If so, record that the second insn "forces" the first one,
1141 since the second can be moved only if the first is. */
1143 force_movables (movables);
1145 /* See if there are multiple movable insns that load the same value.
1146 If there are, make all but the first point at the first one
1147 through the `match' field, and add the priorities of them
1148 all together as the priority of the first. */
1150 combine_movables (movables, regs);
1152 /* Now consider each movable insn to decide whether it is worth moving.
1153 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1155 For machines with few registers this increases code size, so do not
1156 move moveables when optimizing for code size on such machines.
1157 (The 18 below is the value for i386.) */
1159 if (!optimize_size
1160 || (reg_class_size[GENERAL_REGS] > 18 && !loop_info->has_call))
1162 move_movables (loop, movables, threshold, insn_count);
1164 /* Recalculate regs->array if move_movables has created new
1165 registers. */
1166 if (max_reg_num () > regs->num)
1168 loop_regs_scan (loop, 0);
1169 for (update_start = loop_start;
1170 PREV_INSN (update_start)
1171 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1172 update_start = PREV_INSN (update_start))
1174 update_end = NEXT_INSN (loop_end);
1176 reg_scan_update (update_start, update_end, loop_max_reg);
1177 loop_max_reg = max_reg_num ();
1181 /* Now candidates that still are negative are those not moved.
1182 Change regs->array[I].set_in_loop to indicate that those are not actually
1183 invariant. */
1184 for (i = 0; i < regs->num; i++)
1185 if (regs->array[i].set_in_loop < 0)
1186 regs->array[i].set_in_loop = regs->array[i].n_times_set;
1188 /* Now that we've moved some things out of the loop, we might be able to
1189 hoist even more memory references. */
1190 load_mems (loop);
1192 /* Recalculate regs->array if load_mems has created new registers. */
1193 if (max_reg_num () > regs->num)
1194 loop_regs_scan (loop, 0);
1196 for (update_start = loop_start;
1197 PREV_INSN (update_start)
1198 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1199 update_start = PREV_INSN (update_start))
1201 update_end = NEXT_INSN (loop_end);
1203 reg_scan_update (update_start, update_end, loop_max_reg);
1204 loop_max_reg = max_reg_num ();
1206 if (flag_strength_reduce)
1208 if (update_end && GET_CODE (update_end) == CODE_LABEL)
1209 /* Ensure our label doesn't go away. */
1210 LABEL_NUSES (update_end)++;
1212 strength_reduce (loop, flags);
1214 reg_scan_update (update_start, update_end, loop_max_reg);
1215 loop_max_reg = max_reg_num ();
1217 if (update_end && GET_CODE (update_end) == CODE_LABEL
1218 && --LABEL_NUSES (update_end) == 0)
1219 delete_related_insns (update_end);
1223 /* The movable information is required for strength reduction. */
1224 loop_movables_free (movables);
1226 free (regs->array);
1227 regs->array = 0;
1228 regs->num = 0;
1231 /* Add elements to *OUTPUT to record all the pseudo-regs
1232 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1234 void
1235 record_excess_regs (rtx in_this, rtx not_in_this, rtx *output)
1237 enum rtx_code code;
1238 const char *fmt;
1239 int i;
1241 code = GET_CODE (in_this);
1243 switch (code)
1245 case PC:
1246 case CC0:
1247 case CONST_INT:
1248 case CONST_DOUBLE:
1249 case CONST:
1250 case SYMBOL_REF:
1251 case LABEL_REF:
1252 return;
1254 case REG:
1255 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1256 && ! reg_mentioned_p (in_this, not_in_this))
1257 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1258 return;
1260 default:
1261 break;
1264 fmt = GET_RTX_FORMAT (code);
1265 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1267 int j;
1269 switch (fmt[i])
1271 case 'E':
1272 for (j = 0; j < XVECLEN (in_this, i); j++)
1273 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1274 break;
1276 case 'e':
1277 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1278 break;
1283 /* Check what regs are referred to in the libcall block ending with INSN,
1284 aside from those mentioned in the equivalent value.
1285 If there are none, return 0.
1286 If there are one or more, return an EXPR_LIST containing all of them. */
1289 libcall_other_reg (rtx insn, rtx equiv)
1291 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1292 rtx p = XEXP (note, 0);
1293 rtx output = 0;
1295 /* First, find all the regs used in the libcall block
1296 that are not mentioned as inputs to the result. */
1298 while (p != insn)
1300 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1301 || GET_CODE (p) == CALL_INSN)
1302 record_excess_regs (PATTERN (p), equiv, &output);
1303 p = NEXT_INSN (p);
1306 return output;
1309 /* Return 1 if all uses of REG
1310 are between INSN and the end of the basic block. */
1312 static int
1313 reg_in_basic_block_p (rtx insn, rtx reg)
1315 int regno = REGNO (reg);
1316 rtx p;
1318 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1319 return 0;
1321 /* Search this basic block for the already recorded last use of the reg. */
1322 for (p = insn; p; p = NEXT_INSN (p))
1324 switch (GET_CODE (p))
1326 case NOTE:
1327 break;
1329 case INSN:
1330 case CALL_INSN:
1331 /* Ordinary insn: if this is the last use, we win. */
1332 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1333 return 1;
1334 break;
1336 case JUMP_INSN:
1337 /* Jump insn: if this is the last use, we win. */
1338 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1339 return 1;
1340 /* Otherwise, it's the end of the basic block, so we lose. */
1341 return 0;
1343 case CODE_LABEL:
1344 case BARRIER:
1345 /* It's the end of the basic block, so we lose. */
1346 return 0;
1348 default:
1349 break;
1353 /* The "last use" that was recorded can't be found after the first
1354 use. This can happen when the last use was deleted while
1355 processing an inner loop, this inner loop was then completely
1356 unrolled, and the outer loop is always exited after the inner loop,
1357 so that everything after the first use becomes a single basic block. */
1358 return 1;
1361 /* Compute the benefit of eliminating the insns in the block whose
1362 last insn is LAST. This may be a group of insns used to compute a
1363 value directly or can contain a library call. */
1365 static int
1366 libcall_benefit (rtx last)
1368 rtx insn;
1369 int benefit = 0;
1371 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1372 insn != last; insn = NEXT_INSN (insn))
1374 if (GET_CODE (insn) == CALL_INSN)
1375 benefit += 10; /* Assume at least this many insns in a library
1376 routine. */
1377 else if (GET_CODE (insn) == INSN
1378 && GET_CODE (PATTERN (insn)) != USE
1379 && GET_CODE (PATTERN (insn)) != CLOBBER)
1380 benefit++;
1383 return benefit;
1386 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1388 static rtx
1389 skip_consec_insns (rtx insn, int count)
1391 for (; count > 0; count--)
1393 rtx temp;
1395 /* If first insn of libcall sequence, skip to end. */
1396 /* Do this at start of loop, since INSN is guaranteed to
1397 be an insn here. */
1398 if (GET_CODE (insn) != NOTE
1399 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1400 insn = XEXP (temp, 0);
1403 insn = NEXT_INSN (insn);
1404 while (GET_CODE (insn) == NOTE);
1407 return insn;
1410 /* Ignore any movable whose insn falls within a libcall
1411 which is part of another movable.
1412 We make use of the fact that the movable for the libcall value
1413 was made later and so appears later on the chain. */
1415 static void
1416 ignore_some_movables (struct loop_movables *movables)
1418 struct movable *m, *m1;
1420 for (m = movables->head; m; m = m->next)
1422 /* Is this a movable for the value of a libcall? */
1423 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1424 if (note)
1426 rtx insn;
1427 /* Check for earlier movables inside that range,
1428 and mark them invalid. We cannot use LUIDs here because
1429 insns created by loop.c for prior loops don't have LUIDs.
1430 Rather than reject all such insns from movables, we just
1431 explicitly check each insn in the libcall (since invariant
1432 libcalls aren't that common). */
1433 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1434 for (m1 = movables->head; m1 != m; m1 = m1->next)
1435 if (m1->insn == insn)
1436 m1->done = 1;
1441 /* For each movable insn, see if the reg that it loads
1442 leads when it dies right into another conditionally movable insn.
1443 If so, record that the second insn "forces" the first one,
1444 since the second can be moved only if the first is. */
1446 static void
1447 force_movables (struct loop_movables *movables)
1449 struct movable *m, *m1;
1451 for (m1 = movables->head; m1; m1 = m1->next)
1452 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1453 if (!m1->partial && !m1->done)
1455 int regno = m1->regno;
1456 for (m = m1->next; m; m = m->next)
1457 /* ??? Could this be a bug? What if CSE caused the
1458 register of M1 to be used after this insn?
1459 Since CSE does not update regno_last_uid,
1460 this insn M->insn might not be where it dies.
1461 But very likely this doesn't matter; what matters is
1462 that M's reg is computed from M1's reg. */
1463 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1464 && !m->done)
1465 break;
1466 if (m != 0 && m->set_src == m1->set_dest
1467 /* If m->consec, m->set_src isn't valid. */
1468 && m->consec == 0)
1469 m = 0;
1471 /* Increase the priority of the moving the first insn
1472 since it permits the second to be moved as well. */
1473 if (m != 0)
1475 m->forces = m1;
1476 m1->lifetime += m->lifetime;
1477 m1->savings += m->savings;
1482 /* Find invariant expressions that are equal and can be combined into
1483 one register. */
1485 static void
1486 combine_movables (struct loop_movables *movables, struct loop_regs *regs)
1488 struct movable *m;
1489 char *matched_regs = xmalloc (regs->num);
1490 enum machine_mode mode;
1492 /* Regs that are set more than once are not allowed to match
1493 or be matched. I'm no longer sure why not. */
1494 /* Only pseudo registers are allowed to match or be matched,
1495 since move_movables does not validate the change. */
1496 /* Perhaps testing m->consec_sets would be more appropriate here? */
1498 for (m = movables->head; m; m = m->next)
1499 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1500 && m->regno >= FIRST_PSEUDO_REGISTER
1501 && !m->insert_temp
1502 && !m->partial)
1504 struct movable *m1;
1505 int regno = m->regno;
1507 memset (matched_regs, 0, regs->num);
1508 matched_regs[regno] = 1;
1510 /* We want later insns to match the first one. Don't make the first
1511 one match any later ones. So start this loop at m->next. */
1512 for (m1 = m->next; m1; m1 = m1->next)
1513 if (m != m1 && m1->match == 0
1514 && !m1->insert_temp
1515 && regs->array[m1->regno].n_times_set == 1
1516 && m1->regno >= FIRST_PSEUDO_REGISTER
1517 /* A reg used outside the loop mustn't be eliminated. */
1518 && !m1->global
1519 /* A reg used for zero-extending mustn't be eliminated. */
1520 && !m1->partial
1521 && (matched_regs[m1->regno]
1524 /* Can combine regs with different modes loaded from the
1525 same constant only if the modes are the same or
1526 if both are integer modes with M wider or the same
1527 width as M1. The check for integer is redundant, but
1528 safe, since the only case of differing destination
1529 modes with equal sources is when both sources are
1530 VOIDmode, i.e., CONST_INT. */
1531 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1532 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1533 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1534 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1535 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1536 /* See if the source of M1 says it matches M. */
1537 && ((GET_CODE (m1->set_src) == REG
1538 && matched_regs[REGNO (m1->set_src)])
1539 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1540 movables, regs))))
1541 && ((m->dependencies == m1->dependencies)
1542 || rtx_equal_p (m->dependencies, m1->dependencies)))
1544 m->lifetime += m1->lifetime;
1545 m->savings += m1->savings;
1546 m1->done = 1;
1547 m1->match = m;
1548 matched_regs[m1->regno] = 1;
1552 /* Now combine the regs used for zero-extension.
1553 This can be done for those not marked `global'
1554 provided their lives don't overlap. */
1556 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1557 mode = GET_MODE_WIDER_MODE (mode))
1559 struct movable *m0 = 0;
1561 /* Combine all the registers for extension from mode MODE.
1562 Don't combine any that are used outside this loop. */
1563 for (m = movables->head; m; m = m->next)
1564 if (m->partial && ! m->global
1565 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1567 struct movable *m1;
1569 int first = REGNO_FIRST_LUID (m->regno);
1570 int last = REGNO_LAST_LUID (m->regno);
1572 if (m0 == 0)
1574 /* First one: don't check for overlap, just record it. */
1575 m0 = m;
1576 continue;
1579 /* Make sure they extend to the same mode.
1580 (Almost always true.) */
1581 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1582 continue;
1584 /* We already have one: check for overlap with those
1585 already combined together. */
1586 for (m1 = movables->head; m1 != m; m1 = m1->next)
1587 if (m1 == m0 || (m1->partial && m1->match == m0))
1588 if (! (REGNO_FIRST_LUID (m1->regno) > last
1589 || REGNO_LAST_LUID (m1->regno) < first))
1590 goto overlap;
1592 /* No overlap: we can combine this with the others. */
1593 m0->lifetime += m->lifetime;
1594 m0->savings += m->savings;
1595 m->done = 1;
1596 m->match = m0;
1598 overlap:
1603 /* Clean up. */
1604 free (matched_regs);
1607 /* Returns the number of movable instructions in LOOP that were not
1608 moved outside the loop. */
1610 static int
1611 num_unmoved_movables (const struct loop *loop)
1613 int num = 0;
1614 struct movable *m;
1616 for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1617 if (!m->done)
1618 ++num;
1620 return num;
1624 /* Return 1 if regs X and Y will become the same if moved. */
1626 static int
1627 regs_match_p (rtx x, rtx y, struct loop_movables *movables)
1629 unsigned int xn = REGNO (x);
1630 unsigned int yn = REGNO (y);
1631 struct movable *mx, *my;
1633 for (mx = movables->head; mx; mx = mx->next)
1634 if (mx->regno == xn)
1635 break;
1637 for (my = movables->head; my; my = my->next)
1638 if (my->regno == yn)
1639 break;
1641 return (mx && my
1642 && ((mx->match == my->match && mx->match != 0)
1643 || mx->match == my
1644 || mx == my->match));
1647 /* Return 1 if X and Y are identical-looking rtx's.
1648 This is the Lisp function EQUAL for rtx arguments.
1650 If two registers are matching movables or a movable register and an
1651 equivalent constant, consider them equal. */
1653 static int
1654 rtx_equal_for_loop_p (rtx x, rtx y, struct loop_movables *movables,
1655 struct loop_regs *regs)
1657 int i;
1658 int j;
1659 struct movable *m;
1660 enum rtx_code code;
1661 const char *fmt;
1663 if (x == y)
1664 return 1;
1665 if (x == 0 || y == 0)
1666 return 0;
1668 code = GET_CODE (x);
1670 /* If we have a register and a constant, they may sometimes be
1671 equal. */
1672 if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1673 && CONSTANT_P (y))
1675 for (m = movables->head; m; m = m->next)
1676 if (m->move_insn && m->regno == REGNO (x)
1677 && rtx_equal_p (m->set_src, y))
1678 return 1;
1680 else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1681 && CONSTANT_P (x))
1683 for (m = movables->head; m; m = m->next)
1684 if (m->move_insn && m->regno == REGNO (y)
1685 && rtx_equal_p (m->set_src, x))
1686 return 1;
1689 /* Otherwise, rtx's of different codes cannot be equal. */
1690 if (code != GET_CODE (y))
1691 return 0;
1693 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1694 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1696 if (GET_MODE (x) != GET_MODE (y))
1697 return 0;
1699 /* These three types of rtx's can be compared nonrecursively. */
1700 if (code == REG)
1701 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1703 if (code == LABEL_REF)
1704 return XEXP (x, 0) == XEXP (y, 0);
1705 if (code == SYMBOL_REF)
1706 return XSTR (x, 0) == XSTR (y, 0);
1708 /* Compare the elements. If any pair of corresponding elements
1709 fail to match, return 0 for the whole things. */
1711 fmt = GET_RTX_FORMAT (code);
1712 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1714 switch (fmt[i])
1716 case 'w':
1717 if (XWINT (x, i) != XWINT (y, i))
1718 return 0;
1719 break;
1721 case 'i':
1722 if (XINT (x, i) != XINT (y, i))
1723 return 0;
1724 break;
1726 case 'E':
1727 /* Two vectors must have the same length. */
1728 if (XVECLEN (x, i) != XVECLEN (y, i))
1729 return 0;
1731 /* And the corresponding elements must match. */
1732 for (j = 0; j < XVECLEN (x, i); j++)
1733 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1734 movables, regs) == 0)
1735 return 0;
1736 break;
1738 case 'e':
1739 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1740 == 0)
1741 return 0;
1742 break;
1744 case 's':
1745 if (strcmp (XSTR (x, i), XSTR (y, i)))
1746 return 0;
1747 break;
1749 case 'u':
1750 /* These are just backpointers, so they don't matter. */
1751 break;
1753 case '0':
1754 break;
1756 /* It is believed that rtx's at this level will never
1757 contain anything but integers and other rtx's,
1758 except for within LABEL_REFs and SYMBOL_REFs. */
1759 default:
1760 abort ();
1763 return 1;
1766 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1767 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1768 references is incremented once for each added note. */
1770 static void
1771 add_label_notes (rtx x, rtx insns)
1773 enum rtx_code code = GET_CODE (x);
1774 int i, j;
1775 const char *fmt;
1776 rtx insn;
1778 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1780 /* This code used to ignore labels that referred to dispatch tables to
1781 avoid flow generating (slightly) worse code.
1783 We no longer ignore such label references (see LABEL_REF handling in
1784 mark_jump_label for additional information). */
1785 for (insn = insns; insn; insn = NEXT_INSN (insn))
1786 if (reg_mentioned_p (XEXP (x, 0), insn))
1788 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
1789 REG_NOTES (insn));
1790 if (LABEL_P (XEXP (x, 0)))
1791 LABEL_NUSES (XEXP (x, 0))++;
1795 fmt = GET_RTX_FORMAT (code);
1796 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1798 if (fmt[i] == 'e')
1799 add_label_notes (XEXP (x, i), insns);
1800 else if (fmt[i] == 'E')
1801 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1802 add_label_notes (XVECEXP (x, i, j), insns);
1806 /* Scan MOVABLES, and move the insns that deserve to be moved.
1807 If two matching movables are combined, replace one reg with the
1808 other throughout. */
1810 static void
1811 move_movables (struct loop *loop, struct loop_movables *movables,
1812 int threshold, int insn_count)
1814 struct loop_regs *regs = LOOP_REGS (loop);
1815 int nregs = regs->num;
1816 rtx new_start = 0;
1817 struct movable *m;
1818 rtx p;
1819 rtx loop_start = loop->start;
1820 rtx loop_end = loop->end;
1821 /* Map of pseudo-register replacements to handle combining
1822 when we move several insns that load the same value
1823 into different pseudo-registers. */
1824 rtx *reg_map = xcalloc (nregs, sizeof (rtx));
1825 char *already_moved = xcalloc (nregs, sizeof (char));
1827 for (m = movables->head; m; m = m->next)
1829 /* Describe this movable insn. */
1831 if (loop_dump_stream)
1833 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1834 INSN_UID (m->insn), m->regno, m->lifetime);
1835 if (m->consec > 0)
1836 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1837 if (m->cond)
1838 fprintf (loop_dump_stream, "cond ");
1839 if (m->force)
1840 fprintf (loop_dump_stream, "force ");
1841 if (m->global)
1842 fprintf (loop_dump_stream, "global ");
1843 if (m->done)
1844 fprintf (loop_dump_stream, "done ");
1845 if (m->move_insn)
1846 fprintf (loop_dump_stream, "move-insn ");
1847 if (m->match)
1848 fprintf (loop_dump_stream, "matches %d ",
1849 INSN_UID (m->match->insn));
1850 if (m->forces)
1851 fprintf (loop_dump_stream, "forces %d ",
1852 INSN_UID (m->forces->insn));
1855 /* Ignore the insn if it's already done (it matched something else).
1856 Otherwise, see if it is now safe to move. */
1858 if (!m->done
1859 && (! m->cond
1860 || (1 == loop_invariant_p (loop, m->set_src)
1861 && (m->dependencies == 0
1862 || 1 == loop_invariant_p (loop, m->dependencies))
1863 && (m->consec == 0
1864 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1865 m->consec + 1,
1866 m->insn))))
1867 && (! m->forces || m->forces->done))
1869 int regno;
1870 rtx p;
1871 int savings = m->savings;
1873 /* We have an insn that is safe to move.
1874 Compute its desirability. */
1876 p = m->insn;
1877 regno = m->regno;
1879 if (loop_dump_stream)
1880 fprintf (loop_dump_stream, "savings %d ", savings);
1882 if (regs->array[regno].moved_once && loop_dump_stream)
1883 fprintf (loop_dump_stream, "halved since already moved ");
1885 /* An insn MUST be moved if we already moved something else
1886 which is safe only if this one is moved too: that is,
1887 if already_moved[REGNO] is nonzero. */
1889 /* An insn is desirable to move if the new lifetime of the
1890 register is no more than THRESHOLD times the old lifetime.
1891 If it's not desirable, it means the loop is so big
1892 that moving won't speed things up much,
1893 and it is liable to make register usage worse. */
1895 /* It is also desirable to move if it can be moved at no
1896 extra cost because something else was already moved. */
1898 if (already_moved[regno]
1899 || flag_move_all_movables
1900 || (threshold * savings * m->lifetime) >=
1901 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1902 || (m->forces && m->forces->done
1903 && regs->array[m->forces->regno].n_times_set == 1))
1905 int count;
1906 struct movable *m1;
1907 rtx first = NULL_RTX;
1908 rtx newreg = NULL_RTX;
1910 if (m->insert_temp)
1911 newreg = gen_reg_rtx (GET_MODE (m->set_dest));
1913 /* Now move the insns that set the reg. */
1915 if (m->partial && m->match)
1917 rtx newpat, i1;
1918 rtx r1, r2;
1919 /* Find the end of this chain of matching regs.
1920 Thus, we load each reg in the chain from that one reg.
1921 And that reg is loaded with 0 directly,
1922 since it has ->match == 0. */
1923 for (m1 = m; m1->match; m1 = m1->match);
1924 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1925 SET_DEST (PATTERN (m1->insn)));
1926 i1 = loop_insn_hoist (loop, newpat);
1928 /* Mark the moved, invariant reg as being allowed to
1929 share a hard reg with the other matching invariant. */
1930 REG_NOTES (i1) = REG_NOTES (m->insn);
1931 r1 = SET_DEST (PATTERN (m->insn));
1932 r2 = SET_DEST (PATTERN (m1->insn));
1933 regs_may_share
1934 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1935 gen_rtx_EXPR_LIST (VOIDmode, r2,
1936 regs_may_share));
1937 delete_insn (m->insn);
1939 if (new_start == 0)
1940 new_start = i1;
1942 if (loop_dump_stream)
1943 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1945 /* If we are to re-generate the item being moved with a
1946 new move insn, first delete what we have and then emit
1947 the move insn before the loop. */
1948 else if (m->move_insn)
1950 rtx i1, temp, seq;
1952 for (count = m->consec; count >= 0; count--)
1954 /* If this is the first insn of a library call sequence,
1955 something is very wrong. */
1956 if (GET_CODE (p) != NOTE
1957 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1958 abort ();
1960 /* If this is the last insn of a libcall sequence, then
1961 delete every insn in the sequence except the last.
1962 The last insn is handled in the normal manner. */
1963 if (GET_CODE (p) != NOTE
1964 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1966 temp = XEXP (temp, 0);
1967 while (temp != p)
1968 temp = delete_insn (temp);
1971 temp = p;
1972 p = delete_insn (p);
1974 /* simplify_giv_expr expects that it can walk the insns
1975 at m->insn forwards and see this old sequence we are
1976 tossing here. delete_insn does preserve the next
1977 pointers, but when we skip over a NOTE we must fix
1978 it up. Otherwise that code walks into the non-deleted
1979 insn stream. */
1980 while (p && GET_CODE (p) == NOTE)
1981 p = NEXT_INSN (temp) = NEXT_INSN (p);
1983 if (m->insert_temp)
1985 /* Replace the original insn with a move from
1986 our newly created temp. */
1987 start_sequence ();
1988 emit_move_insn (m->set_dest, newreg);
1989 seq = get_insns ();
1990 end_sequence ();
1991 emit_insn_before (seq, p);
1995 start_sequence ();
1996 emit_move_insn (m->insert_temp ? newreg : m->set_dest,
1997 m->set_src);
1998 seq = get_insns ();
1999 end_sequence ();
2001 add_label_notes (m->set_src, seq);
2003 i1 = loop_insn_hoist (loop, seq);
2004 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2005 set_unique_reg_note (i1,
2006 m->is_equiv ? REG_EQUIV : REG_EQUAL,
2007 m->set_src);
2009 if (loop_dump_stream)
2010 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
2012 /* The more regs we move, the less we like moving them. */
2013 threshold -= 3;
2015 else
2017 for (count = m->consec; count >= 0; count--)
2019 rtx i1, temp;
2021 /* If first insn of libcall sequence, skip to end. */
2022 /* Do this at start of loop, since p is guaranteed to
2023 be an insn here. */
2024 if (GET_CODE (p) != NOTE
2025 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
2026 p = XEXP (temp, 0);
2028 /* If last insn of libcall sequence, move all
2029 insns except the last before the loop. The last
2030 insn is handled in the normal manner. */
2031 if (GET_CODE (p) != NOTE
2032 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
2034 rtx fn_address = 0;
2035 rtx fn_reg = 0;
2036 rtx fn_address_insn = 0;
2038 first = 0;
2039 for (temp = XEXP (temp, 0); temp != p;
2040 temp = NEXT_INSN (temp))
2042 rtx body;
2043 rtx n;
2044 rtx next;
2046 if (GET_CODE (temp) == NOTE)
2047 continue;
2049 body = PATTERN (temp);
2051 /* Find the next insn after TEMP,
2052 not counting USE or NOTE insns. */
2053 for (next = NEXT_INSN (temp); next != p;
2054 next = NEXT_INSN (next))
2055 if (! (GET_CODE (next) == INSN
2056 && GET_CODE (PATTERN (next)) == USE)
2057 && GET_CODE (next) != NOTE)
2058 break;
2060 /* If that is the call, this may be the insn
2061 that loads the function address.
2063 Extract the function address from the insn
2064 that loads it into a register.
2065 If this insn was cse'd, we get incorrect code.
2067 So emit a new move insn that copies the
2068 function address into the register that the
2069 call insn will use. flow.c will delete any
2070 redundant stores that we have created. */
2071 if (GET_CODE (next) == CALL_INSN
2072 && GET_CODE (body) == SET
2073 && GET_CODE (SET_DEST (body)) == REG
2074 && (n = find_reg_note (temp, REG_EQUAL,
2075 NULL_RTX)))
2077 fn_reg = SET_SRC (body);
2078 if (GET_CODE (fn_reg) != REG)
2079 fn_reg = SET_DEST (body);
2080 fn_address = XEXP (n, 0);
2081 fn_address_insn = temp;
2083 /* We have the call insn.
2084 If it uses the register we suspect it might,
2085 load it with the correct address directly. */
2086 if (GET_CODE (temp) == CALL_INSN
2087 && fn_address != 0
2088 && reg_referenced_p (fn_reg, body))
2089 loop_insn_emit_after (loop, 0, fn_address_insn,
2090 gen_move_insn
2091 (fn_reg, fn_address));
2093 if (GET_CODE (temp) == CALL_INSN)
2095 i1 = loop_call_insn_hoist (loop, body);
2096 /* Because the USAGE information potentially
2097 contains objects other than hard registers
2098 we need to copy it. */
2099 if (CALL_INSN_FUNCTION_USAGE (temp))
2100 CALL_INSN_FUNCTION_USAGE (i1)
2101 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2103 else
2104 i1 = loop_insn_hoist (loop, body);
2105 if (first == 0)
2106 first = i1;
2107 if (temp == fn_address_insn)
2108 fn_address_insn = i1;
2109 REG_NOTES (i1) = REG_NOTES (temp);
2110 REG_NOTES (temp) = NULL;
2111 delete_insn (temp);
2113 if (new_start == 0)
2114 new_start = first;
2116 if (m->savemode != VOIDmode)
2118 /* P sets REG to zero; but we should clear only
2119 the bits that are not covered by the mode
2120 m->savemode. */
2121 rtx reg = m->set_dest;
2122 rtx sequence;
2123 rtx tem;
2125 start_sequence ();
2126 tem = expand_simple_binop
2127 (GET_MODE (reg), AND, reg,
2128 GEN_INT ((((HOST_WIDE_INT) 1
2129 << GET_MODE_BITSIZE (m->savemode)))
2130 - 1),
2131 reg, 1, OPTAB_LIB_WIDEN);
2132 if (tem == 0)
2133 abort ();
2134 if (tem != reg)
2135 emit_move_insn (reg, tem);
2136 sequence = get_insns ();
2137 end_sequence ();
2138 i1 = loop_insn_hoist (loop, sequence);
2140 else if (GET_CODE (p) == CALL_INSN)
2142 i1 = loop_call_insn_hoist (loop, PATTERN (p));
2143 /* Because the USAGE information potentially
2144 contains objects other than hard registers
2145 we need to copy it. */
2146 if (CALL_INSN_FUNCTION_USAGE (p))
2147 CALL_INSN_FUNCTION_USAGE (i1)
2148 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2150 else if (count == m->consec && m->move_insn_first)
2152 rtx seq;
2153 /* The SET_SRC might not be invariant, so we must
2154 use the REG_EQUAL note. */
2155 start_sequence ();
2156 emit_move_insn (m->insert_temp ? newreg : m->set_dest,
2157 m->set_src);
2158 seq = get_insns ();
2159 end_sequence ();
2161 add_label_notes (m->set_src, seq);
2163 i1 = loop_insn_hoist (loop, seq);
2164 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2165 set_unique_reg_note (i1, m->is_equiv ? REG_EQUIV
2166 : REG_EQUAL, m->set_src);
2168 else if (m->insert_temp)
2170 rtx *reg_map2 = xcalloc (REGNO (newreg),
2171 sizeof(rtx));
2172 reg_map2 [m->regno] = newreg;
2174 i1 = loop_insn_hoist (loop, copy_rtx (PATTERN (p)));
2175 replace_regs (i1, reg_map2, REGNO (newreg), 1);
2176 free (reg_map2);
2178 else
2179 i1 = loop_insn_hoist (loop, PATTERN (p));
2181 if (REG_NOTES (i1) == 0)
2183 REG_NOTES (i1) = REG_NOTES (p);
2184 REG_NOTES (p) = NULL;
2186 /* If there is a REG_EQUAL note present whose value
2187 is not loop invariant, then delete it, since it
2188 may cause problems with later optimization passes.
2189 It is possible for cse to create such notes
2190 like this as a result of record_jump_cond. */
2192 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2193 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2194 remove_note (i1, temp);
2197 if (new_start == 0)
2198 new_start = i1;
2200 if (loop_dump_stream)
2201 fprintf (loop_dump_stream, " moved to %d",
2202 INSN_UID (i1));
2204 /* If library call, now fix the REG_NOTES that contain
2205 insn pointers, namely REG_LIBCALL on FIRST
2206 and REG_RETVAL on I1. */
2207 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2209 XEXP (temp, 0) = first;
2210 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2211 XEXP (temp, 0) = i1;
2214 temp = p;
2215 delete_insn (p);
2216 p = NEXT_INSN (p);
2218 /* simplify_giv_expr expects that it can walk the insns
2219 at m->insn forwards and see this old sequence we are
2220 tossing here. delete_insn does preserve the next
2221 pointers, but when we skip over a NOTE we must fix
2222 it up. Otherwise that code walks into the non-deleted
2223 insn stream. */
2224 while (p && GET_CODE (p) == NOTE)
2225 p = NEXT_INSN (temp) = NEXT_INSN (p);
2227 if (m->insert_temp)
2229 rtx seq;
2230 /* Replace the original insn with a move from
2231 our newly created temp. */
2232 start_sequence ();
2233 emit_move_insn (m->set_dest, newreg);
2234 seq = get_insns ();
2235 end_sequence ();
2236 emit_insn_before (seq, p);
2240 /* The more regs we move, the less we like moving them. */
2241 threshold -= 3;
2244 m->done = 1;
2246 if (!m->insert_temp)
2248 /* Any other movable that loads the same register
2249 MUST be moved. */
2250 already_moved[regno] = 1;
2252 /* This reg has been moved out of one loop. */
2253 regs->array[regno].moved_once = 1;
2255 /* The reg set here is now invariant. */
2256 if (! m->partial)
2258 int i;
2259 for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2260 regs->array[regno+i].set_in_loop = 0;
2263 /* Change the length-of-life info for the register
2264 to say it lives at least the full length of this loop.
2265 This will help guide optimizations in outer loops. */
2267 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2268 /* This is the old insn before all the moved insns.
2269 We can't use the moved insn because it is out of range
2270 in uid_luid. Only the old insns have luids. */
2271 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2272 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2273 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2276 /* Combine with this moved insn any other matching movables. */
2278 if (! m->partial)
2279 for (m1 = movables->head; m1; m1 = m1->next)
2280 if (m1->match == m)
2282 rtx temp;
2284 /* Schedule the reg loaded by M1
2285 for replacement so that shares the reg of M.
2286 If the modes differ (only possible in restricted
2287 circumstances, make a SUBREG.
2289 Note this assumes that the target dependent files
2290 treat REG and SUBREG equally, including within
2291 GO_IF_LEGITIMATE_ADDRESS and in all the
2292 predicates since we never verify that replacing the
2293 original register with a SUBREG results in a
2294 recognizable insn. */
2295 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2296 reg_map[m1->regno] = m->set_dest;
2297 else
2298 reg_map[m1->regno]
2299 = gen_lowpart_common (GET_MODE (m1->set_dest),
2300 m->set_dest);
2302 /* Get rid of the matching insn
2303 and prevent further processing of it. */
2304 m1->done = 1;
2306 /* If library call, delete all insns. */
2307 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2308 NULL_RTX)))
2309 delete_insn_chain (XEXP (temp, 0), m1->insn);
2310 else
2311 delete_insn (m1->insn);
2313 /* Any other movable that loads the same register
2314 MUST be moved. */
2315 already_moved[m1->regno] = 1;
2317 /* The reg merged here is now invariant,
2318 if the reg it matches is invariant. */
2319 if (! m->partial)
2321 int i;
2322 for (i = 0;
2323 i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2324 i++)
2325 regs->array[m1->regno+i].set_in_loop = 0;
2329 else if (loop_dump_stream)
2330 fprintf (loop_dump_stream, "not desirable");
2332 else if (loop_dump_stream && !m->match)
2333 fprintf (loop_dump_stream, "not safe");
2335 if (loop_dump_stream)
2336 fprintf (loop_dump_stream, "\n");
2339 if (new_start == 0)
2340 new_start = loop_start;
2342 /* Go through all the instructions in the loop, making
2343 all the register substitutions scheduled in REG_MAP. */
2344 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2345 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2346 || GET_CODE (p) == CALL_INSN)
2348 replace_regs (PATTERN (p), reg_map, nregs, 0);
2349 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2350 INSN_CODE (p) = -1;
2353 /* Clean up. */
2354 free (reg_map);
2355 free (already_moved);
2359 static void
2360 loop_movables_add (struct loop_movables *movables, struct movable *m)
2362 if (movables->head == 0)
2363 movables->head = m;
2364 else
2365 movables->last->next = m;
2366 movables->last = m;
2370 static void
2371 loop_movables_free (struct loop_movables *movables)
2373 struct movable *m;
2374 struct movable *m_next;
2376 for (m = movables->head; m; m = m_next)
2378 m_next = m->next;
2379 free (m);
2383 #if 0
2384 /* Scan X and replace the address of any MEM in it with ADDR.
2385 REG is the address that MEM should have before the replacement. */
2387 static void
2388 replace_call_address (rtx x, rtx reg, rtx addr)
2390 enum rtx_code code;
2391 int i;
2392 const char *fmt;
2394 if (x == 0)
2395 return;
2396 code = GET_CODE (x);
2397 switch (code)
2399 case PC:
2400 case CC0:
2401 case CONST_INT:
2402 case CONST_DOUBLE:
2403 case CONST:
2404 case SYMBOL_REF:
2405 case LABEL_REF:
2406 case REG:
2407 return;
2409 case SET:
2410 /* Short cut for very common case. */
2411 replace_call_address (XEXP (x, 1), reg, addr);
2412 return;
2414 case CALL:
2415 /* Short cut for very common case. */
2416 replace_call_address (XEXP (x, 0), reg, addr);
2417 return;
2419 case MEM:
2420 /* If this MEM uses a reg other than the one we expected,
2421 something is wrong. */
2422 if (XEXP (x, 0) != reg)
2423 abort ();
2424 XEXP (x, 0) = addr;
2425 return;
2427 default:
2428 break;
2431 fmt = GET_RTX_FORMAT (code);
2432 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2434 if (fmt[i] == 'e')
2435 replace_call_address (XEXP (x, i), reg, addr);
2436 else if (fmt[i] == 'E')
2438 int j;
2439 for (j = 0; j < XVECLEN (x, i); j++)
2440 replace_call_address (XVECEXP (x, i, j), reg, addr);
2444 #endif
2446 /* Return the number of memory refs to addresses that vary
2447 in the rtx X. */
2449 static int
2450 count_nonfixed_reads (const struct loop *loop, rtx x)
2452 enum rtx_code code;
2453 int i;
2454 const char *fmt;
2455 int value;
2457 if (x == 0)
2458 return 0;
2460 code = GET_CODE (x);
2461 switch (code)
2463 case PC:
2464 case CC0:
2465 case CONST_INT:
2466 case CONST_DOUBLE:
2467 case CONST:
2468 case SYMBOL_REF:
2469 case LABEL_REF:
2470 case REG:
2471 return 0;
2473 case MEM:
2474 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2475 + count_nonfixed_reads (loop, XEXP (x, 0)));
2477 default:
2478 break;
2481 value = 0;
2482 fmt = GET_RTX_FORMAT (code);
2483 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2485 if (fmt[i] == 'e')
2486 value += count_nonfixed_reads (loop, XEXP (x, i));
2487 if (fmt[i] == 'E')
2489 int j;
2490 for (j = 0; j < XVECLEN (x, i); j++)
2491 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2494 return value;
2497 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2498 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2499 `unknown_address_altered', `unknown_constant_address_altered', and
2500 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2501 list `store_mems' in LOOP. */
2503 static void
2504 prescan_loop (struct loop *loop)
2506 int level = 1;
2507 rtx insn;
2508 struct loop_info *loop_info = LOOP_INFO (loop);
2509 rtx start = loop->start;
2510 rtx end = loop->end;
2511 /* The label after END. Jumping here is just like falling off the
2512 end of the loop. We use next_nonnote_insn instead of next_label
2513 as a hedge against the (pathological) case where some actual insn
2514 might end up between the two. */
2515 rtx exit_target = next_nonnote_insn (end);
2517 loop_info->has_indirect_jump = indirect_jump_in_function;
2518 loop_info->pre_header_has_call = 0;
2519 loop_info->has_call = 0;
2520 loop_info->has_nonconst_call = 0;
2521 loop_info->has_prefetch = 0;
2522 loop_info->has_volatile = 0;
2523 loop_info->has_tablejump = 0;
2524 loop_info->has_multiple_exit_targets = 0;
2525 loop->level = 1;
2527 loop_info->unknown_address_altered = 0;
2528 loop_info->unknown_constant_address_altered = 0;
2529 loop_info->store_mems = NULL_RTX;
2530 loop_info->first_loop_store_insn = NULL_RTX;
2531 loop_info->mems_idx = 0;
2532 loop_info->num_mem_sets = 0;
2533 /* If loop opts run twice, this was set on 1st pass for 2nd. */
2534 loop_info->preconditioned = NOTE_PRECONDITIONED (end);
2536 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2537 insn = PREV_INSN (insn))
2539 if (GET_CODE (insn) == CALL_INSN)
2541 loop_info->pre_header_has_call = 1;
2542 break;
2546 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2547 insn = NEXT_INSN (insn))
2549 switch (GET_CODE (insn))
2551 case NOTE:
2552 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2554 ++level;
2555 /* Count number of loops contained in this one. */
2556 loop->level++;
2558 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2559 --level;
2560 break;
2562 case CALL_INSN:
2563 if (! CONST_OR_PURE_CALL_P (insn))
2565 loop_info->unknown_address_altered = 1;
2566 loop_info->has_nonconst_call = 1;
2568 else if (pure_call_p (insn))
2569 loop_info->has_nonconst_call = 1;
2570 loop_info->has_call = 1;
2571 if (can_throw_internal (insn))
2572 loop_info->has_multiple_exit_targets = 1;
2574 /* Calls initializing constant objects have CLOBBER of MEM /u in the
2575 attached FUNCTION_USAGE expression list, not accounted for by the
2576 code above. We should note these to avoid missing dependencies in
2577 later references. */
2579 rtx fusage_entry;
2581 for (fusage_entry = CALL_INSN_FUNCTION_USAGE (insn);
2582 fusage_entry; fusage_entry = XEXP (fusage_entry, 1))
2584 rtx fusage = XEXP (fusage_entry, 0);
2586 if (GET_CODE (fusage) == CLOBBER
2587 && GET_CODE (XEXP (fusage, 0)) == MEM
2588 && RTX_UNCHANGING_P (XEXP (fusage, 0)))
2590 note_stores (fusage, note_addr_stored, loop_info);
2591 if (! loop_info->first_loop_store_insn
2592 && loop_info->store_mems)
2593 loop_info->first_loop_store_insn = insn;
2597 break;
2599 case JUMP_INSN:
2600 if (! loop_info->has_multiple_exit_targets)
2602 rtx set = pc_set (insn);
2604 if (set)
2606 rtx src = SET_SRC (set);
2607 rtx label1, label2;
2609 if (GET_CODE (src) == IF_THEN_ELSE)
2611 label1 = XEXP (src, 1);
2612 label2 = XEXP (src, 2);
2614 else
2616 label1 = src;
2617 label2 = NULL_RTX;
2622 if (label1 && label1 != pc_rtx)
2624 if (GET_CODE (label1) != LABEL_REF)
2626 /* Something tricky. */
2627 loop_info->has_multiple_exit_targets = 1;
2628 break;
2630 else if (XEXP (label1, 0) != exit_target
2631 && LABEL_OUTSIDE_LOOP_P (label1))
2633 /* A jump outside the current loop. */
2634 loop_info->has_multiple_exit_targets = 1;
2635 break;
2639 label1 = label2;
2640 label2 = NULL_RTX;
2642 while (label1);
2644 else
2646 /* A return, or something tricky. */
2647 loop_info->has_multiple_exit_targets = 1;
2650 /* Fall through. */
2652 case INSN:
2653 if (volatile_refs_p (PATTERN (insn)))
2654 loop_info->has_volatile = 1;
2656 if (GET_CODE (insn) == JUMP_INSN
2657 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2658 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2659 loop_info->has_tablejump = 1;
2661 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2662 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2663 loop_info->first_loop_store_insn = insn;
2665 if (flag_non_call_exceptions && can_throw_internal (insn))
2666 loop_info->has_multiple_exit_targets = 1;
2667 break;
2669 default:
2670 break;
2674 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2675 if (/* An exception thrown by a called function might land us
2676 anywhere. */
2677 ! loop_info->has_nonconst_call
2678 /* We don't want loads for MEMs moved to a location before the
2679 one at which their stack memory becomes allocated. (Note
2680 that this is not a problem for malloc, etc., since those
2681 require actual function calls. */
2682 && ! current_function_calls_alloca
2683 /* There are ways to leave the loop other than falling off the
2684 end. */
2685 && ! loop_info->has_multiple_exit_targets)
2686 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2687 insn = NEXT_INSN (insn))
2688 for_each_rtx (&insn, insert_loop_mem, loop_info);
2690 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2691 that loop_invariant_p and load_mems can use true_dependence
2692 to determine what is really clobbered. */
2693 if (loop_info->unknown_address_altered)
2695 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2697 loop_info->store_mems
2698 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2700 if (loop_info->unknown_constant_address_altered)
2702 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2704 RTX_UNCHANGING_P (mem) = 1;
2705 loop_info->store_mems
2706 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2710 /* Invalidate all loops containing LABEL. */
2712 static void
2713 invalidate_loops_containing_label (rtx label)
2715 struct loop *loop;
2716 for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
2717 loop->invalid = 1;
2720 /* Scan the function looking for loops. Record the start and end of each loop.
2721 Also mark as invalid loops any loops that contain a setjmp or are branched
2722 to from outside the loop. */
2724 static void
2725 find_and_verify_loops (rtx f, struct loops *loops)
2727 rtx insn;
2728 rtx label;
2729 int num_loops;
2730 struct loop *current_loop;
2731 struct loop *next_loop;
2732 struct loop *loop;
2734 num_loops = loops->num;
2736 compute_luids (f, NULL_RTX, 0);
2738 /* If there are jumps to undefined labels,
2739 treat them as jumps out of any/all loops.
2740 This also avoids writing past end of tables when there are no loops. */
2741 uid_loop[0] = NULL;
2743 /* Find boundaries of loops, mark which loops are contained within
2744 loops, and invalidate loops that have setjmp. */
2746 num_loops = 0;
2747 current_loop = NULL;
2748 for (insn = f; insn; insn = NEXT_INSN (insn))
2750 if (GET_CODE (insn) == NOTE)
2751 switch (NOTE_LINE_NUMBER (insn))
2753 case NOTE_INSN_LOOP_BEG:
2754 next_loop = loops->array + num_loops;
2755 next_loop->num = num_loops;
2756 num_loops++;
2757 next_loop->start = insn;
2758 next_loop->outer = current_loop;
2759 current_loop = next_loop;
2760 break;
2762 case NOTE_INSN_LOOP_CONT:
2763 current_loop->cont = insn;
2764 break;
2766 case NOTE_INSN_LOOP_VTOP:
2767 current_loop->vtop = insn;
2768 break;
2770 case NOTE_INSN_LOOP_END:
2771 if (! current_loop)
2772 abort ();
2774 current_loop->end = insn;
2775 current_loop = current_loop->outer;
2776 break;
2778 default:
2779 break;
2782 if (GET_CODE (insn) == CALL_INSN
2783 && find_reg_note (insn, REG_SETJMP, NULL))
2785 /* In this case, we must invalidate our current loop and any
2786 enclosing loop. */
2787 for (loop = current_loop; loop; loop = loop->outer)
2789 loop->invalid = 1;
2790 if (loop_dump_stream)
2791 fprintf (loop_dump_stream,
2792 "\nLoop at %d ignored due to setjmp.\n",
2793 INSN_UID (loop->start));
2797 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2798 enclosing loop, but this doesn't matter. */
2799 uid_loop[INSN_UID (insn)] = current_loop;
2802 /* Any loop containing a label used in an initializer must be invalidated,
2803 because it can be jumped into from anywhere. */
2804 for (label = forced_labels; label; label = XEXP (label, 1))
2805 invalidate_loops_containing_label (XEXP (label, 0));
2807 /* Any loop containing a label used for an exception handler must be
2808 invalidated, because it can be jumped into from anywhere. */
2809 for_each_eh_label (invalidate_loops_containing_label);
2811 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2812 loop that it is not contained within, that loop is marked invalid.
2813 If any INSN or CALL_INSN uses a label's address, then the loop containing
2814 that label is marked invalid, because it could be jumped into from
2815 anywhere.
2817 Also look for blocks of code ending in an unconditional branch that
2818 exits the loop. If such a block is surrounded by a conditional
2819 branch around the block, move the block elsewhere (see below) and
2820 invert the jump to point to the code block. This may eliminate a
2821 label in our loop and will simplify processing by both us and a
2822 possible second cse pass. */
2824 for (insn = f; insn; insn = NEXT_INSN (insn))
2825 if (INSN_P (insn))
2827 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2829 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2831 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2832 if (note)
2833 invalidate_loops_containing_label (XEXP (note, 0));
2836 if (GET_CODE (insn) != JUMP_INSN)
2837 continue;
2839 mark_loop_jump (PATTERN (insn), this_loop);
2841 /* See if this is an unconditional branch outside the loop. */
2842 if (this_loop
2843 && (GET_CODE (PATTERN (insn)) == RETURN
2844 || (any_uncondjump_p (insn)
2845 && onlyjump_p (insn)
2846 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2847 != this_loop)))
2848 && get_max_uid () < max_uid_for_loop)
2850 rtx p;
2851 rtx our_next = next_real_insn (insn);
2852 rtx last_insn_to_move = NEXT_INSN (insn);
2853 struct loop *dest_loop;
2854 struct loop *outer_loop = NULL;
2856 /* Go backwards until we reach the start of the loop, a label,
2857 or a JUMP_INSN. */
2858 for (p = PREV_INSN (insn);
2859 GET_CODE (p) != CODE_LABEL
2860 && ! (GET_CODE (p) == NOTE
2861 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2862 && GET_CODE (p) != JUMP_INSN;
2863 p = PREV_INSN (p))
2866 /* Check for the case where we have a jump to an inner nested
2867 loop, and do not perform the optimization in that case. */
2869 if (JUMP_LABEL (insn))
2871 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2872 if (dest_loop)
2874 for (outer_loop = dest_loop; outer_loop;
2875 outer_loop = outer_loop->outer)
2876 if (outer_loop == this_loop)
2877 break;
2881 /* Make sure that the target of P is within the current loop. */
2883 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2884 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2885 outer_loop = this_loop;
2887 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2888 we have a block of code to try to move.
2890 We look backward and then forward from the target of INSN
2891 to find a BARRIER at the same loop depth as the target.
2892 If we find such a BARRIER, we make a new label for the start
2893 of the block, invert the jump in P and point it to that label,
2894 and move the block of code to the spot we found. */
2896 if (! outer_loop
2897 && GET_CODE (p) == JUMP_INSN
2898 && JUMP_LABEL (p) != 0
2899 /* Just ignore jumps to labels that were never emitted.
2900 These always indicate compilation errors. */
2901 && INSN_UID (JUMP_LABEL (p)) != 0
2902 && any_condjump_p (p) && onlyjump_p (p)
2903 && next_real_insn (JUMP_LABEL (p)) == our_next
2904 /* If it's not safe to move the sequence, then we
2905 mustn't try. */
2906 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2907 &last_insn_to_move))
2909 rtx target
2910 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2911 struct loop *target_loop = uid_loop[INSN_UID (target)];
2912 rtx loc, loc2;
2913 rtx tmp;
2915 /* Search for possible garbage past the conditional jumps
2916 and look for the last barrier. */
2917 for (tmp = last_insn_to_move;
2918 tmp && GET_CODE (tmp) != CODE_LABEL; tmp = NEXT_INSN (tmp))
2919 if (GET_CODE (tmp) == BARRIER)
2920 last_insn_to_move = tmp;
2922 for (loc = target; loc; loc = PREV_INSN (loc))
2923 if (GET_CODE (loc) == BARRIER
2924 /* Don't move things inside a tablejump. */
2925 && ((loc2 = next_nonnote_insn (loc)) == 0
2926 || GET_CODE (loc2) != CODE_LABEL
2927 || (loc2 = next_nonnote_insn (loc2)) == 0
2928 || GET_CODE (loc2) != JUMP_INSN
2929 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2930 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2931 && uid_loop[INSN_UID (loc)] == target_loop)
2932 break;
2934 if (loc == 0)
2935 for (loc = target; loc; loc = NEXT_INSN (loc))
2936 if (GET_CODE (loc) == BARRIER
2937 /* Don't move things inside a tablejump. */
2938 && ((loc2 = next_nonnote_insn (loc)) == 0
2939 || GET_CODE (loc2) != CODE_LABEL
2940 || (loc2 = next_nonnote_insn (loc2)) == 0
2941 || GET_CODE (loc2) != JUMP_INSN
2942 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2943 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2944 && uid_loop[INSN_UID (loc)] == target_loop)
2945 break;
2947 if (loc)
2949 rtx cond_label = JUMP_LABEL (p);
2950 rtx new_label = get_label_after (p);
2952 /* Ensure our label doesn't go away. */
2953 LABEL_NUSES (cond_label)++;
2955 /* Verify that uid_loop is large enough and that
2956 we can invert P. */
2957 if (invert_jump (p, new_label, 1))
2959 rtx q, r;
2961 /* If no suitable BARRIER was found, create a suitable
2962 one before TARGET. Since TARGET is a fall through
2963 path, we'll need to insert a jump around our block
2964 and add a BARRIER before TARGET.
2966 This creates an extra unconditional jump outside
2967 the loop. However, the benefits of removing rarely
2968 executed instructions from inside the loop usually
2969 outweighs the cost of the extra unconditional jump
2970 outside the loop. */
2971 if (loc == 0)
2973 rtx temp;
2975 temp = gen_jump (JUMP_LABEL (insn));
2976 temp = emit_jump_insn_before (temp, target);
2977 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2978 LABEL_NUSES (JUMP_LABEL (insn))++;
2979 loc = emit_barrier_before (target);
2982 /* Include the BARRIER after INSN and copy the
2983 block after LOC. */
2984 if (squeeze_notes (&new_label, &last_insn_to_move))
2985 abort ();
2986 reorder_insns (new_label, last_insn_to_move, loc);
2988 /* All those insns are now in TARGET_LOOP. */
2989 for (q = new_label;
2990 q != NEXT_INSN (last_insn_to_move);
2991 q = NEXT_INSN (q))
2992 uid_loop[INSN_UID (q)] = target_loop;
2994 /* The label jumped to by INSN is no longer a loop
2995 exit. Unless INSN does not have a label (e.g.,
2996 it is a RETURN insn), search loop->exit_labels
2997 to find its label_ref, and remove it. Also turn
2998 off LABEL_OUTSIDE_LOOP_P bit. */
2999 if (JUMP_LABEL (insn))
3001 for (q = 0, r = this_loop->exit_labels;
3003 q = r, r = LABEL_NEXTREF (r))
3004 if (XEXP (r, 0) == JUMP_LABEL (insn))
3006 LABEL_OUTSIDE_LOOP_P (r) = 0;
3007 if (q)
3008 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
3009 else
3010 this_loop->exit_labels = LABEL_NEXTREF (r);
3011 break;
3014 for (loop = this_loop; loop && loop != target_loop;
3015 loop = loop->outer)
3016 loop->exit_count--;
3018 /* If we didn't find it, then something is
3019 wrong. */
3020 if (! r)
3021 abort ();
3024 /* P is now a jump outside the loop, so it must be put
3025 in loop->exit_labels, and marked as such.
3026 The easiest way to do this is to just call
3027 mark_loop_jump again for P. */
3028 mark_loop_jump (PATTERN (p), this_loop);
3030 /* If INSN now jumps to the insn after it,
3031 delete INSN. */
3032 if (JUMP_LABEL (insn) != 0
3033 && (next_real_insn (JUMP_LABEL (insn))
3034 == next_real_insn (insn)))
3035 delete_related_insns (insn);
3038 /* Continue the loop after where the conditional
3039 branch used to jump, since the only branch insn
3040 in the block (if it still remains) is an inter-loop
3041 branch and hence needs no processing. */
3042 insn = NEXT_INSN (cond_label);
3044 if (--LABEL_NUSES (cond_label) == 0)
3045 delete_related_insns (cond_label);
3047 /* This loop will be continued with NEXT_INSN (insn). */
3048 insn = PREV_INSN (insn);
3055 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
3056 loops it is contained in, mark the target loop invalid.
3058 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
3060 static void
3061 mark_loop_jump (rtx x, struct loop *loop)
3063 struct loop *dest_loop;
3064 struct loop *outer_loop;
3065 int i;
3067 switch (GET_CODE (x))
3069 case PC:
3070 case USE:
3071 case CLOBBER:
3072 case REG:
3073 case MEM:
3074 case CONST_INT:
3075 case CONST_DOUBLE:
3076 case RETURN:
3077 return;
3079 case CONST:
3080 /* There could be a label reference in here. */
3081 mark_loop_jump (XEXP (x, 0), loop);
3082 return;
3084 case PLUS:
3085 case MINUS:
3086 case MULT:
3087 mark_loop_jump (XEXP (x, 0), loop);
3088 mark_loop_jump (XEXP (x, 1), loop);
3089 return;
3091 case LO_SUM:
3092 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3093 mark_loop_jump (XEXP (x, 1), loop);
3094 return;
3096 case SIGN_EXTEND:
3097 case ZERO_EXTEND:
3098 mark_loop_jump (XEXP (x, 0), loop);
3099 return;
3101 case LABEL_REF:
3102 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3104 /* Link together all labels that branch outside the loop. This
3105 is used by final_[bg]iv_value and the loop unrolling code. Also
3106 mark this LABEL_REF so we know that this branch should predict
3107 false. */
3109 /* A check to make sure the label is not in an inner nested loop,
3110 since this does not count as a loop exit. */
3111 if (dest_loop)
3113 for (outer_loop = dest_loop; outer_loop;
3114 outer_loop = outer_loop->outer)
3115 if (outer_loop == loop)
3116 break;
3118 else
3119 outer_loop = NULL;
3121 if (loop && ! outer_loop)
3123 LABEL_OUTSIDE_LOOP_P (x) = 1;
3124 LABEL_NEXTREF (x) = loop->exit_labels;
3125 loop->exit_labels = x;
3127 for (outer_loop = loop;
3128 outer_loop && outer_loop != dest_loop;
3129 outer_loop = outer_loop->outer)
3130 outer_loop->exit_count++;
3133 /* If this is inside a loop, but not in the current loop or one enclosed
3134 by it, it invalidates at least one loop. */
3136 if (! dest_loop)
3137 return;
3139 /* We must invalidate every nested loop containing the target of this
3140 label, except those that also contain the jump insn. */
3142 for (; dest_loop; dest_loop = dest_loop->outer)
3144 /* Stop when we reach a loop that also contains the jump insn. */
3145 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3146 if (dest_loop == outer_loop)
3147 return;
3149 /* If we get here, we know we need to invalidate a loop. */
3150 if (loop_dump_stream && ! dest_loop->invalid)
3151 fprintf (loop_dump_stream,
3152 "\nLoop at %d ignored due to multiple entry points.\n",
3153 INSN_UID (dest_loop->start));
3155 dest_loop->invalid = 1;
3157 return;
3159 case SET:
3160 /* If this is not setting pc, ignore. */
3161 if (SET_DEST (x) == pc_rtx)
3162 mark_loop_jump (SET_SRC (x), loop);
3163 return;
3165 case IF_THEN_ELSE:
3166 mark_loop_jump (XEXP (x, 1), loop);
3167 mark_loop_jump (XEXP (x, 2), loop);
3168 return;
3170 case PARALLEL:
3171 case ADDR_VEC:
3172 for (i = 0; i < XVECLEN (x, 0); i++)
3173 mark_loop_jump (XVECEXP (x, 0, i), loop);
3174 return;
3176 case ADDR_DIFF_VEC:
3177 for (i = 0; i < XVECLEN (x, 1); i++)
3178 mark_loop_jump (XVECEXP (x, 1, i), loop);
3179 return;
3181 default:
3182 /* Strictly speaking this is not a jump into the loop, only a possible
3183 jump out of the loop. However, we have no way to link the destination
3184 of this jump onto the list of exit labels. To be safe we mark this
3185 loop and any containing loops as invalid. */
3186 if (loop)
3188 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3190 if (loop_dump_stream && ! outer_loop->invalid)
3191 fprintf (loop_dump_stream,
3192 "\nLoop at %d ignored due to unknown exit jump.\n",
3193 INSN_UID (outer_loop->start));
3194 outer_loop->invalid = 1;
3197 return;
3201 /* Return nonzero if there is a label in the range from
3202 insn INSN to and including the insn whose luid is END
3203 INSN must have an assigned luid (i.e., it must not have
3204 been previously created by loop.c). */
3206 static int
3207 labels_in_range_p (rtx insn, int end)
3209 while (insn && INSN_LUID (insn) <= end)
3211 if (GET_CODE (insn) == CODE_LABEL)
3212 return 1;
3213 insn = NEXT_INSN (insn);
3216 return 0;
3219 /* Record that a memory reference X is being set. */
3221 static void
3222 note_addr_stored (rtx x, rtx y ATTRIBUTE_UNUSED,
3223 void *data ATTRIBUTE_UNUSED)
3225 struct loop_info *loop_info = data;
3227 if (x == 0 || GET_CODE (x) != MEM)
3228 return;
3230 /* Count number of memory writes.
3231 This affects heuristics in strength_reduce. */
3232 loop_info->num_mem_sets++;
3234 /* BLKmode MEM means all memory is clobbered. */
3235 if (GET_MODE (x) == BLKmode)
3237 if (RTX_UNCHANGING_P (x))
3238 loop_info->unknown_constant_address_altered = 1;
3239 else
3240 loop_info->unknown_address_altered = 1;
3242 return;
3245 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3246 loop_info->store_mems);
3249 /* X is a value modified by an INSN that references a biv inside a loop
3250 exit test (ie, X is somehow related to the value of the biv). If X
3251 is a pseudo that is used more than once, then the biv is (effectively)
3252 used more than once. DATA is a pointer to a loop_regs structure. */
3254 static void
3255 note_set_pseudo_multiple_uses (rtx x, rtx y ATTRIBUTE_UNUSED, void *data)
3257 struct loop_regs *regs = (struct loop_regs *) data;
3259 if (x == 0)
3260 return;
3262 while (GET_CODE (x) == STRICT_LOW_PART
3263 || GET_CODE (x) == SIGN_EXTRACT
3264 || GET_CODE (x) == ZERO_EXTRACT
3265 || GET_CODE (x) == SUBREG)
3266 x = XEXP (x, 0);
3268 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3269 return;
3271 /* If we do not have usage information, or if we know the register
3272 is used more than once, note that fact for check_dbra_loop. */
3273 if (REGNO (x) >= max_reg_before_loop
3274 || ! regs->array[REGNO (x)].single_usage
3275 || regs->array[REGNO (x)].single_usage == const0_rtx)
3276 regs->multiple_uses = 1;
3279 /* Return nonzero if the rtx X is invariant over the current loop.
3281 The value is 2 if we refer to something only conditionally invariant.
3283 A memory ref is invariant if it is not volatile and does not conflict
3284 with anything stored in `loop_info->store_mems'. */
3287 loop_invariant_p (const struct loop *loop, rtx x)
3289 struct loop_info *loop_info = LOOP_INFO (loop);
3290 struct loop_regs *regs = LOOP_REGS (loop);
3291 int i;
3292 enum rtx_code code;
3293 const char *fmt;
3294 int conditional = 0;
3295 rtx mem_list_entry;
3297 if (x == 0)
3298 return 1;
3299 code = GET_CODE (x);
3300 switch (code)
3302 case CONST_INT:
3303 case CONST_DOUBLE:
3304 case SYMBOL_REF:
3305 case CONST:
3306 return 1;
3308 case LABEL_REF:
3309 /* A LABEL_REF is normally invariant, however, if we are unrolling
3310 loops, and this label is inside the loop, then it isn't invariant.
3311 This is because each unrolled copy of the loop body will have
3312 a copy of this label. If this was invariant, then an insn loading
3313 the address of this label into a register might get moved outside
3314 the loop, and then each loop body would end up using the same label.
3316 We don't know the loop bounds here though, so just fail for all
3317 labels. */
3318 if (flag_old_unroll_loops)
3319 return 0;
3320 else
3321 return 1;
3323 case PC:
3324 case CC0:
3325 case UNSPEC_VOLATILE:
3326 return 0;
3328 case REG:
3329 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3330 since the reg might be set by initialization within the loop. */
3332 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3333 || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3334 && ! current_function_has_nonlocal_goto)
3335 return 1;
3337 if (LOOP_INFO (loop)->has_call
3338 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3339 return 0;
3341 /* Out-of-range regs can occur when we are called from unrolling.
3342 These registers created by the unroller are set in the loop,
3343 hence are never invariant.
3344 Other out-of-range regs can be generated by load_mems; those that
3345 are written to in the loop are not invariant, while those that are
3346 not written to are invariant. It would be easy for load_mems
3347 to set n_times_set correctly for these registers, however, there
3348 is no easy way to distinguish them from registers created by the
3349 unroller. */
3351 if (REGNO (x) >= (unsigned) regs->num)
3352 return 0;
3354 if (regs->array[REGNO (x)].set_in_loop < 0)
3355 return 2;
3357 return regs->array[REGNO (x)].set_in_loop == 0;
3359 case MEM:
3360 /* Volatile memory references must be rejected. Do this before
3361 checking for read-only items, so that volatile read-only items
3362 will be rejected also. */
3363 if (MEM_VOLATILE_P (x))
3364 return 0;
3366 /* See if there is any dependence between a store and this load. */
3367 mem_list_entry = loop_info->store_mems;
3368 while (mem_list_entry)
3370 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3371 x, rtx_varies_p))
3372 return 0;
3374 mem_list_entry = XEXP (mem_list_entry, 1);
3377 /* It's not invalidated by a store in memory
3378 but we must still verify the address is invariant. */
3379 break;
3381 case ASM_OPERANDS:
3382 /* Don't mess with insns declared volatile. */
3383 if (MEM_VOLATILE_P (x))
3384 return 0;
3385 break;
3387 default:
3388 break;
3391 fmt = GET_RTX_FORMAT (code);
3392 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3394 if (fmt[i] == 'e')
3396 int tem = loop_invariant_p (loop, XEXP (x, i));
3397 if (tem == 0)
3398 return 0;
3399 if (tem == 2)
3400 conditional = 1;
3402 else if (fmt[i] == 'E')
3404 int j;
3405 for (j = 0; j < XVECLEN (x, i); j++)
3407 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3408 if (tem == 0)
3409 return 0;
3410 if (tem == 2)
3411 conditional = 1;
3417 return 1 + conditional;
3420 /* Return nonzero if all the insns in the loop that set REG
3421 are INSN and the immediately following insns,
3422 and if each of those insns sets REG in an invariant way
3423 (not counting uses of REG in them).
3425 The value is 2 if some of these insns are only conditionally invariant.
3427 We assume that INSN itself is the first set of REG
3428 and that its source is invariant. */
3430 static int
3431 consec_sets_invariant_p (const struct loop *loop, rtx reg, int n_sets,
3432 rtx insn)
3434 struct loop_regs *regs = LOOP_REGS (loop);
3435 rtx p = insn;
3436 unsigned int regno = REGNO (reg);
3437 rtx temp;
3438 /* Number of sets we have to insist on finding after INSN. */
3439 int count = n_sets - 1;
3440 int old = regs->array[regno].set_in_loop;
3441 int value = 0;
3442 int this;
3444 /* If N_SETS hit the limit, we can't rely on its value. */
3445 if (n_sets == 127)
3446 return 0;
3448 regs->array[regno].set_in_loop = 0;
3450 while (count > 0)
3452 enum rtx_code code;
3453 rtx set;
3455 p = NEXT_INSN (p);
3456 code = GET_CODE (p);
3458 /* If library call, skip to end of it. */
3459 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3460 p = XEXP (temp, 0);
3462 this = 0;
3463 if (code == INSN
3464 && (set = single_set (p))
3465 && GET_CODE (SET_DEST (set)) == REG
3466 && REGNO (SET_DEST (set)) == regno)
3468 this = loop_invariant_p (loop, SET_SRC (set));
3469 if (this != 0)
3470 value |= this;
3471 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3473 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3474 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3475 notes are OK. */
3476 this = (CONSTANT_P (XEXP (temp, 0))
3477 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3478 && loop_invariant_p (loop, XEXP (temp, 0))));
3479 if (this != 0)
3480 value |= this;
3483 if (this != 0)
3484 count--;
3485 else if (code != NOTE)
3487 regs->array[regno].set_in_loop = old;
3488 return 0;
3492 regs->array[regno].set_in_loop = old;
3493 /* If loop_invariant_p ever returned 2, we return 2. */
3494 return 1 + (value & 2);
3497 #if 0
3498 /* I don't think this condition is sufficient to allow INSN
3499 to be moved, so we no longer test it. */
3501 /* Return 1 if all insns in the basic block of INSN and following INSN
3502 that set REG are invariant according to TABLE. */
3504 static int
3505 all_sets_invariant_p (rtx reg, rtx insn, short *table)
3507 rtx p = insn;
3508 int regno = REGNO (reg);
3510 while (1)
3512 enum rtx_code code;
3513 p = NEXT_INSN (p);
3514 code = GET_CODE (p);
3515 if (code == CODE_LABEL || code == JUMP_INSN)
3516 return 1;
3517 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3518 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3519 && REGNO (SET_DEST (PATTERN (p))) == regno)
3521 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3522 return 0;
3526 #endif /* 0 */
3528 /* Look at all uses (not sets) of registers in X. For each, if it is
3529 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3530 a different insn, set USAGE[REGNO] to const0_rtx. */
3532 static void
3533 find_single_use_in_loop (struct loop_regs *regs, rtx insn, rtx x)
3535 enum rtx_code code = GET_CODE (x);
3536 const char *fmt = GET_RTX_FORMAT (code);
3537 int i, j;
3539 if (code == REG)
3540 regs->array[REGNO (x)].single_usage
3541 = (regs->array[REGNO (x)].single_usage != 0
3542 && regs->array[REGNO (x)].single_usage != insn)
3543 ? const0_rtx : insn;
3545 else if (code == SET)
3547 /* Don't count SET_DEST if it is a REG; otherwise count things
3548 in SET_DEST because if a register is partially modified, it won't
3549 show up as a potential movable so we don't care how USAGE is set
3550 for it. */
3551 if (GET_CODE (SET_DEST (x)) != REG)
3552 find_single_use_in_loop (regs, insn, SET_DEST (x));
3553 find_single_use_in_loop (regs, insn, SET_SRC (x));
3555 else
3556 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3558 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3559 find_single_use_in_loop (regs, insn, XEXP (x, i));
3560 else if (fmt[i] == 'E')
3561 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3562 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3566 /* Count and record any set in X which is contained in INSN. Update
3567 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3568 in X. */
3570 static void
3571 count_one_set (struct loop_regs *regs, rtx insn, rtx x, rtx *last_set)
3573 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3574 /* Don't move a reg that has an explicit clobber.
3575 It's not worth the pain to try to do it correctly. */
3576 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3578 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3580 rtx dest = SET_DEST (x);
3581 while (GET_CODE (dest) == SUBREG
3582 || GET_CODE (dest) == ZERO_EXTRACT
3583 || GET_CODE (dest) == SIGN_EXTRACT
3584 || GET_CODE (dest) == STRICT_LOW_PART)
3585 dest = XEXP (dest, 0);
3586 if (GET_CODE (dest) == REG)
3588 int i;
3589 int regno = REGNO (dest);
3590 for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3592 /* If this is the first setting of this reg
3593 in current basic block, and it was set before,
3594 it must be set in two basic blocks, so it cannot
3595 be moved out of the loop. */
3596 if (regs->array[regno].set_in_loop > 0
3597 && last_set[regno] == 0)
3598 regs->array[regno+i].may_not_optimize = 1;
3599 /* If this is not first setting in current basic block,
3600 see if reg was used in between previous one and this.
3601 If so, neither one can be moved. */
3602 if (last_set[regno] != 0
3603 && reg_used_between_p (dest, last_set[regno], insn))
3604 regs->array[regno+i].may_not_optimize = 1;
3605 if (regs->array[regno+i].set_in_loop < 127)
3606 ++regs->array[regno+i].set_in_loop;
3607 last_set[regno+i] = insn;
3613 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3614 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3615 contained in insn INSN is used by any insn that precedes INSN in
3616 cyclic order starting from the loop entry point.
3618 We don't want to use INSN_LUID here because if we restrict INSN to those
3619 that have a valid INSN_LUID, it means we cannot move an invariant out
3620 from an inner loop past two loops. */
3622 static int
3623 loop_reg_used_before_p (const struct loop *loop, rtx set, rtx insn)
3625 rtx reg = SET_DEST (set);
3626 rtx p;
3628 /* Scan forward checking for register usage. If we hit INSN, we
3629 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3630 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3632 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3633 return 1;
3635 if (p == loop->end)
3636 p = loop->start;
3639 return 0;
3643 /* Information we collect about arrays that we might want to prefetch. */
3644 struct prefetch_info
3646 struct iv_class *class; /* Class this prefetch is based on. */
3647 struct induction *giv; /* GIV this prefetch is based on. */
3648 rtx base_address; /* Start prefetching from this address plus
3649 index. */
3650 HOST_WIDE_INT index;
3651 HOST_WIDE_INT stride; /* Prefetch stride in bytes in each
3652 iteration. */
3653 unsigned int bytes_accessed; /* Sum of sizes of all accesses to this
3654 prefetch area in one iteration. */
3655 unsigned int total_bytes; /* Total bytes loop will access in this block.
3656 This is set only for loops with known
3657 iteration counts and is 0xffffffff
3658 otherwise. */
3659 int prefetch_in_loop; /* Number of prefetch insns in loop. */
3660 int prefetch_before_loop; /* Number of prefetch insns before loop. */
3661 unsigned int write : 1; /* 1 for read/write prefetches. */
3664 /* Data used by check_store function. */
3665 struct check_store_data
3667 rtx mem_address;
3668 int mem_write;
3671 static void check_store (rtx, rtx, void *);
3672 static void emit_prefetch_instructions (struct loop *);
3673 static int rtx_equal_for_prefetch_p (rtx, rtx);
3675 /* Set mem_write when mem_address is found. Used as callback to
3676 note_stores. */
3677 static void
3678 check_store (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
3680 struct check_store_data *d = (struct check_store_data *) data;
3682 if ((GET_CODE (x) == MEM) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3683 d->mem_write = 1;
3686 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3687 important to get some addresses combined. Later more sophisticated
3688 transformations can be added when necessary.
3690 ??? Same trick with swapping operand is done at several other places.
3691 It can be nice to develop some common way to handle this. */
3693 static int
3694 rtx_equal_for_prefetch_p (rtx x, rtx y)
3696 int i;
3697 int j;
3698 enum rtx_code code = GET_CODE (x);
3699 const char *fmt;
3701 if (x == y)
3702 return 1;
3703 if (code != GET_CODE (y))
3704 return 0;
3706 code = GET_CODE (x);
3708 if (GET_RTX_CLASS (code) == 'c')
3710 return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3711 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3712 || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3713 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3715 /* Compare the elements. If any pair of corresponding elements fails to
3716 match, return 0 for the whole thing. */
3718 fmt = GET_RTX_FORMAT (code);
3719 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3721 switch (fmt[i])
3723 case 'w':
3724 if (XWINT (x, i) != XWINT (y, i))
3725 return 0;
3726 break;
3728 case 'i':
3729 if (XINT (x, i) != XINT (y, i))
3730 return 0;
3731 break;
3733 case 'E':
3734 /* Two vectors must have the same length. */
3735 if (XVECLEN (x, i) != XVECLEN (y, i))
3736 return 0;
3738 /* And the corresponding elements must match. */
3739 for (j = 0; j < XVECLEN (x, i); j++)
3740 if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
3741 XVECEXP (y, i, j)) == 0)
3742 return 0;
3743 break;
3745 case 'e':
3746 if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
3747 return 0;
3748 break;
3750 case 's':
3751 if (strcmp (XSTR (x, i), XSTR (y, i)))
3752 return 0;
3753 break;
3755 case 'u':
3756 /* These are just backpointers, so they don't matter. */
3757 break;
3759 case '0':
3760 break;
3762 /* It is believed that rtx's at this level will never
3763 contain anything but integers and other rtx's,
3764 except for within LABEL_REFs and SYMBOL_REFs. */
3765 default:
3766 abort ();
3769 return 1;
3772 /* Remove constant addition value from the expression X (when present)
3773 and return it. */
3775 static HOST_WIDE_INT
3776 remove_constant_addition (rtx *x)
3778 HOST_WIDE_INT addval = 0;
3779 rtx exp = *x;
3781 /* Avoid clobbering a shared CONST expression. */
3782 if (GET_CODE (exp) == CONST)
3784 if (GET_CODE (XEXP (exp, 0)) == PLUS
3785 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
3786 && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
3788 *x = XEXP (XEXP (exp, 0), 0);
3789 return INTVAL (XEXP (XEXP (exp, 0), 1));
3791 return 0;
3794 if (GET_CODE (exp) == CONST_INT)
3796 addval = INTVAL (exp);
3797 *x = const0_rtx;
3800 /* For plus expression recurse on ourself. */
3801 else if (GET_CODE (exp) == PLUS)
3803 addval += remove_constant_addition (&XEXP (exp, 0));
3804 addval += remove_constant_addition (&XEXP (exp, 1));
3806 /* In case our parameter was constant, remove extra zero from the
3807 expression. */
3808 if (XEXP (exp, 0) == const0_rtx)
3809 *x = XEXP (exp, 1);
3810 else if (XEXP (exp, 1) == const0_rtx)
3811 *x = XEXP (exp, 0);
3814 return addval;
3817 /* Attempt to identify accesses to arrays that are most likely to cause cache
3818 misses, and emit prefetch instructions a few prefetch blocks forward.
3820 To detect the arrays we use the GIV information that was collected by the
3821 strength reduction pass.
3823 The prefetch instructions are generated after the GIV information is done
3824 and before the strength reduction process. The new GIVs are injected into
3825 the strength reduction tables, so the prefetch addresses are optimized as
3826 well.
3828 GIVs are split into base address, stride, and constant addition values.
3829 GIVs with the same address, stride and close addition values are combined
3830 into a single prefetch. Also writes to GIVs are detected, so that prefetch
3831 for write instructions can be used for the block we write to, on machines
3832 that support write prefetches.
3834 Several heuristics are used to determine when to prefetch. They are
3835 controlled by defined symbols that can be overridden for each target. */
3837 static void
3838 emit_prefetch_instructions (struct loop *loop)
3840 int num_prefetches = 0;
3841 int num_real_prefetches = 0;
3842 int num_real_write_prefetches = 0;
3843 int num_prefetches_before = 0;
3844 int num_write_prefetches_before = 0;
3845 int ahead = 0;
3846 int i;
3847 struct iv_class *bl;
3848 struct induction *iv;
3849 struct prefetch_info info[MAX_PREFETCHES];
3850 struct loop_ivs *ivs = LOOP_IVS (loop);
3852 if (!HAVE_prefetch)
3853 return;
3855 /* Consider only loops w/o calls. When a call is done, the loop is probably
3856 slow enough to read the memory. */
3857 if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
3859 if (loop_dump_stream)
3860 fprintf (loop_dump_stream, "Prefetch: ignoring loop: has call.\n");
3862 return;
3865 /* Don't prefetch in loops known to have few iterations. */
3866 if (PREFETCH_NO_LOW_LOOPCNT
3867 && LOOP_INFO (loop)->n_iterations
3868 && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
3870 if (loop_dump_stream)
3871 fprintf (loop_dump_stream,
3872 "Prefetch: ignoring loop: not enough iterations.\n");
3873 return;
3876 /* Search all induction variables and pick those interesting for the prefetch
3877 machinery. */
3878 for (bl = ivs->list; bl; bl = bl->next)
3880 struct induction *biv = bl->biv, *biv1;
3881 int basestride = 0;
3883 biv1 = biv;
3885 /* Expect all BIVs to be executed in each iteration. This makes our
3886 analysis more conservative. */
3887 while (biv1)
3889 /* Discard non-constant additions that we can't handle well yet, and
3890 BIVs that are executed multiple times; such BIVs ought to be
3891 handled in the nested loop. We accept not_every_iteration BIVs,
3892 since these only result in larger strides and make our
3893 heuristics more conservative. */
3894 if (GET_CODE (biv->add_val) != CONST_INT)
3896 if (loop_dump_stream)
3898 fprintf (loop_dump_stream,
3899 "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
3900 REGNO (biv->src_reg), INSN_UID (biv->insn));
3901 print_rtl (loop_dump_stream, biv->add_val);
3902 fprintf (loop_dump_stream, "\n");
3904 break;
3907 if (biv->maybe_multiple)
3909 if (loop_dump_stream)
3911 fprintf (loop_dump_stream,
3912 "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
3913 REGNO (biv->src_reg), INSN_UID (biv->insn));
3914 print_rtl (loop_dump_stream, biv->add_val);
3915 fprintf (loop_dump_stream, "\n");
3917 break;
3920 basestride += INTVAL (biv1->add_val);
3921 biv1 = biv1->next_iv;
3924 if (biv1 || !basestride)
3925 continue;
3927 for (iv = bl->giv; iv; iv = iv->next_iv)
3929 rtx address;
3930 rtx temp;
3931 HOST_WIDE_INT index = 0;
3932 int add = 1;
3933 HOST_WIDE_INT stride = 0;
3934 int stride_sign = 1;
3935 struct check_store_data d;
3936 const char *ignore_reason = NULL;
3937 int size = GET_MODE_SIZE (GET_MODE (iv));
3939 /* See whether an induction variable is interesting to us and if
3940 not, report the reason. */
3941 if (iv->giv_type != DEST_ADDR)
3942 ignore_reason = "giv is not a destination address";
3944 /* We are interested only in constant stride memory references
3945 in order to be able to compute density easily. */
3946 else if (GET_CODE (iv->mult_val) != CONST_INT)
3947 ignore_reason = "stride is not constant";
3949 else
3951 stride = INTVAL (iv->mult_val) * basestride;
3952 if (stride < 0)
3954 stride = -stride;
3955 stride_sign = -1;
3958 /* On some targets, reversed order prefetches are not
3959 worthwhile. */
3960 if (PREFETCH_NO_REVERSE_ORDER && stride_sign < 0)
3961 ignore_reason = "reversed order stride";
3963 /* Prefetch of accesses with an extreme stride might not be
3964 worthwhile, either. */
3965 else if (PREFETCH_NO_EXTREME_STRIDE
3966 && stride > PREFETCH_EXTREME_STRIDE)
3967 ignore_reason = "extreme stride";
3969 /* Ignore GIVs with varying add values; we can't predict the
3970 value for the next iteration. */
3971 else if (!loop_invariant_p (loop, iv->add_val))
3972 ignore_reason = "giv has varying add value";
3974 /* Ignore GIVs in the nested loops; they ought to have been
3975 handled already. */
3976 else if (iv->maybe_multiple)
3977 ignore_reason = "giv is in nested loop";
3980 if (ignore_reason != NULL)
3982 if (loop_dump_stream)
3983 fprintf (loop_dump_stream,
3984 "Prefetch: ignoring giv at %d: %s.\n",
3985 INSN_UID (iv->insn), ignore_reason);
3986 continue;
3989 /* Determine the pointer to the basic array we are examining. It is
3990 the sum of the BIV's initial value and the GIV's add_val. */
3991 address = copy_rtx (iv->add_val);
3992 temp = copy_rtx (bl->initial_value);
3994 address = simplify_gen_binary (PLUS, Pmode, temp, address);
3995 index = remove_constant_addition (&address);
3997 d.mem_write = 0;
3998 d.mem_address = *iv->location;
4000 /* When the GIV is not always executed, we might be better off by
4001 not dirtying the cache pages. */
4002 if (PREFETCH_CONDITIONAL || iv->always_executed)
4003 note_stores (PATTERN (iv->insn), check_store, &d);
4004 else
4006 if (loop_dump_stream)
4007 fprintf (loop_dump_stream, "Prefetch: Ignoring giv at %d: %s\n",
4008 INSN_UID (iv->insn), "in conditional code.");
4009 continue;
4012 /* Attempt to find another prefetch to the same array and see if we
4013 can merge this one. */
4014 for (i = 0; i < num_prefetches; i++)
4015 if (rtx_equal_for_prefetch_p (address, info[i].base_address)
4016 && stride == info[i].stride)
4018 /* In case both access same array (same location
4019 just with small difference in constant indexes), merge
4020 the prefetches. Just do the later and the earlier will
4021 get prefetched from previous iteration.
4022 The artificial threshold should not be too small,
4023 but also not bigger than small portion of memory usually
4024 traversed by single loop. */
4025 if (index >= info[i].index
4026 && index - info[i].index < PREFETCH_EXTREME_DIFFERENCE)
4028 info[i].write |= d.mem_write;
4029 info[i].bytes_accessed += size;
4030 info[i].index = index;
4031 info[i].giv = iv;
4032 info[i].class = bl;
4033 info[num_prefetches].base_address = address;
4034 add = 0;
4035 break;
4038 if (index < info[i].index
4039 && info[i].index - index < PREFETCH_EXTREME_DIFFERENCE)
4041 info[i].write |= d.mem_write;
4042 info[i].bytes_accessed += size;
4043 add = 0;
4044 break;
4048 /* Merging failed. */
4049 if (add)
4051 info[num_prefetches].giv = iv;
4052 info[num_prefetches].class = bl;
4053 info[num_prefetches].index = index;
4054 info[num_prefetches].stride = stride;
4055 info[num_prefetches].base_address = address;
4056 info[num_prefetches].write = d.mem_write;
4057 info[num_prefetches].bytes_accessed = size;
4058 num_prefetches++;
4059 if (num_prefetches >= MAX_PREFETCHES)
4061 if (loop_dump_stream)
4062 fprintf (loop_dump_stream,
4063 "Maximal number of prefetches exceeded.\n");
4064 return;
4070 for (i = 0; i < num_prefetches; i++)
4072 int density;
4074 /* Attempt to calculate the total number of bytes fetched by all
4075 iterations of the loop. Avoid overflow. */
4076 if (LOOP_INFO (loop)->n_iterations
4077 && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
4078 >= LOOP_INFO (loop)->n_iterations))
4079 info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
4080 else
4081 info[i].total_bytes = 0xffffffff;
4083 density = info[i].bytes_accessed * 100 / info[i].stride;
4085 /* Prefetch might be worthwhile only when the loads/stores are dense. */
4086 if (PREFETCH_ONLY_DENSE_MEM)
4087 if (density * 256 > PREFETCH_DENSE_MEM * 100
4088 && (info[i].total_bytes / PREFETCH_BLOCK
4089 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
4091 info[i].prefetch_before_loop = 1;
4092 info[i].prefetch_in_loop
4093 = (info[i].total_bytes / PREFETCH_BLOCK
4094 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
4096 else
4098 info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
4099 if (loop_dump_stream)
4100 fprintf (loop_dump_stream,
4101 "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4102 INSN_UID (info[i].giv->insn), density);
4104 else
4105 info[i].prefetch_in_loop = 1, info[i].prefetch_before_loop = 1;
4107 /* Find how many prefetch instructions we'll use within the loop. */
4108 if (info[i].prefetch_in_loop != 0)
4110 info[i].prefetch_in_loop = ((info[i].stride + PREFETCH_BLOCK - 1)
4111 / PREFETCH_BLOCK);
4112 num_real_prefetches += info[i].prefetch_in_loop;
4113 if (info[i].write)
4114 num_real_write_prefetches += info[i].prefetch_in_loop;
4118 /* Determine how many iterations ahead to prefetch within the loop, based
4119 on how many prefetches we currently expect to do within the loop. */
4120 if (num_real_prefetches != 0)
4122 if ((ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches) == 0)
4124 if (loop_dump_stream)
4125 fprintf (loop_dump_stream,
4126 "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4127 SIMULTANEOUS_PREFETCHES, num_real_prefetches);
4128 num_real_prefetches = 0, num_real_write_prefetches = 0;
4131 /* We'll also use AHEAD to determine how many prefetch instructions to
4132 emit before a loop, so don't leave it zero. */
4133 if (ahead == 0)
4134 ahead = PREFETCH_BLOCKS_BEFORE_LOOP_MAX;
4136 for (i = 0; i < num_prefetches; i++)
4138 /* Update if we've decided not to prefetch anything within the loop. */
4139 if (num_real_prefetches == 0)
4140 info[i].prefetch_in_loop = 0;
4142 /* Find how many prefetch instructions we'll use before the loop. */
4143 if (info[i].prefetch_before_loop != 0)
4145 int n = info[i].total_bytes / PREFETCH_BLOCK;
4146 if (n > ahead)
4147 n = ahead;
4148 info[i].prefetch_before_loop = n;
4149 num_prefetches_before += n;
4150 if (info[i].write)
4151 num_write_prefetches_before += n;
4154 if (loop_dump_stream)
4156 if (info[i].prefetch_in_loop == 0
4157 && info[i].prefetch_before_loop == 0)
4158 continue;
4159 fprintf (loop_dump_stream, "Prefetch insn: %d",
4160 INSN_UID (info[i].giv->insn));
4161 fprintf (loop_dump_stream,
4162 "; in loop: %d; before: %d; %s\n",
4163 info[i].prefetch_in_loop,
4164 info[i].prefetch_before_loop,
4165 info[i].write ? "read/write" : "read only");
4166 fprintf (loop_dump_stream,
4167 " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4168 (int) (info[i].bytes_accessed * 100 / info[i].stride),
4169 info[i].bytes_accessed, info[i].total_bytes);
4170 fprintf (loop_dump_stream, " index: " HOST_WIDE_INT_PRINT_DEC
4171 "; stride: " HOST_WIDE_INT_PRINT_DEC "; address: ",
4172 info[i].index, info[i].stride);
4173 print_rtl (loop_dump_stream, info[i].base_address);
4174 fprintf (loop_dump_stream, "\n");
4178 if (num_real_prefetches + num_prefetches_before > 0)
4180 /* Record that this loop uses prefetch instructions. */
4181 LOOP_INFO (loop)->has_prefetch = 1;
4183 if (loop_dump_stream)
4185 fprintf (loop_dump_stream, "Real prefetches needed within loop: %d (write: %d)\n",
4186 num_real_prefetches, num_real_write_prefetches);
4187 fprintf (loop_dump_stream, "Real prefetches needed before loop: %d (write: %d)\n",
4188 num_prefetches_before, num_write_prefetches_before);
4192 for (i = 0; i < num_prefetches; i++)
4194 int y;
4196 for (y = 0; y < info[i].prefetch_in_loop; y++)
4198 rtx loc = copy_rtx (*info[i].giv->location);
4199 rtx insn;
4200 int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4201 rtx before_insn = info[i].giv->insn;
4202 rtx prev_insn = PREV_INSN (info[i].giv->insn);
4203 rtx seq;
4205 /* We can save some effort by offsetting the address on
4206 architectures with offsettable memory references. */
4207 if (offsettable_address_p (0, VOIDmode, loc))
4208 loc = plus_constant (loc, bytes_ahead);
4209 else
4211 rtx reg = gen_reg_rtx (Pmode);
4212 loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4213 GEN_INT (bytes_ahead), reg,
4214 0, before_insn);
4215 loc = reg;
4218 start_sequence ();
4219 /* Make sure the address operand is valid for prefetch. */
4220 if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4221 (loc, insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4222 loc = force_reg (Pmode, loc);
4223 emit_insn (gen_prefetch (loc, GEN_INT (info[i].write),
4224 GEN_INT (3)));
4225 seq = get_insns ();
4226 end_sequence ();
4227 emit_insn_before (seq, before_insn);
4229 /* Check all insns emitted and record the new GIV
4230 information. */
4231 insn = NEXT_INSN (prev_insn);
4232 while (insn != before_insn)
4234 insn = check_insn_for_givs (loop, insn,
4235 info[i].giv->always_executed,
4236 info[i].giv->maybe_multiple);
4237 insn = NEXT_INSN (insn);
4241 if (PREFETCH_BEFORE_LOOP)
4243 /* Emit insns before the loop to fetch the first cache lines or,
4244 if we're not prefetching within the loop, everything we expect
4245 to need. */
4246 for (y = 0; y < info[i].prefetch_before_loop; y++)
4248 rtx reg = gen_reg_rtx (Pmode);
4249 rtx loop_start = loop->start;
4250 rtx init_val = info[i].class->initial_value;
4251 rtx add_val = simplify_gen_binary (PLUS, Pmode,
4252 info[i].giv->add_val,
4253 GEN_INT (y * PREFETCH_BLOCK));
4255 /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4256 non-constant INIT_VAL to have the same mode as REG, which
4257 in this case we know to be Pmode. */
4258 if (GET_MODE (init_val) != Pmode && !CONSTANT_P (init_val))
4260 rtx seq;
4262 start_sequence ();
4263 init_val = convert_to_mode (Pmode, init_val, 0);
4264 seq = get_insns ();
4265 end_sequence ();
4266 loop_insn_emit_before (loop, 0, loop_start, seq);
4268 loop_iv_add_mult_emit_before (loop, init_val,
4269 info[i].giv->mult_val,
4270 add_val, reg, 0, loop_start);
4271 emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4272 GEN_INT (3)),
4273 loop_start);
4278 return;
4281 /* Communication with routines called via `note_stores'. */
4283 static rtx note_insn;
4285 /* Dummy register to have nonzero DEST_REG for DEST_ADDR type givs. */
4287 static rtx addr_placeholder;
4289 /* ??? Unfinished optimizations, and possible future optimizations,
4290 for the strength reduction code. */
4292 /* ??? The interaction of biv elimination, and recognition of 'constant'
4293 bivs, may cause problems. */
4295 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4296 performance problems.
4298 Perhaps don't eliminate things that can be combined with an addressing
4299 mode. Find all givs that have the same biv, mult_val, and add_val;
4300 then for each giv, check to see if its only use dies in a following
4301 memory address. If so, generate a new memory address and check to see
4302 if it is valid. If it is valid, then store the modified memory address,
4303 otherwise, mark the giv as not done so that it will get its own iv. */
4305 /* ??? Could try to optimize branches when it is known that a biv is always
4306 positive. */
4308 /* ??? When replace a biv in a compare insn, we should replace with closest
4309 giv so that an optimized branch can still be recognized by the combiner,
4310 e.g. the VAX acb insn. */
4312 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4313 was rerun in loop_optimize whenever a register was added or moved.
4314 Also, some of the optimizations could be a little less conservative. */
4316 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4317 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4318 callback.
4320 NOT_EVERY_ITERATION is 1 if current insn is not known to be executed at
4321 least once for every loop iteration except for the last one.
4323 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4324 loop iteration.
4326 void
4327 for_each_insn_in_loop (struct loop *loop, loop_insn_callback fncall)
4329 int not_every_iteration = 0;
4330 int maybe_multiple = 0;
4331 int past_loop_latch = 0;
4332 int loop_depth = 0;
4333 rtx p;
4335 /* If loop_scan_start points to the loop exit test, we have to be wary of
4336 subversive use of gotos inside expression statements. */
4337 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4338 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4340 /* Scan through loop and update NOT_EVERY_ITERATION and MAYBE_MULTIPLE. */
4341 for (p = next_insn_in_loop (loop, loop->scan_start);
4342 p != NULL_RTX;
4343 p = next_insn_in_loop (loop, p))
4345 p = fncall (loop, p, not_every_iteration, maybe_multiple);
4347 /* Past CODE_LABEL, we get to insns that may be executed multiple
4348 times. The only way we can be sure that they can't is if every
4349 jump insn between here and the end of the loop either
4350 returns, exits the loop, is a jump to a location that is still
4351 behind the label, or is a jump to the loop start. */
4353 if (GET_CODE (p) == CODE_LABEL)
4355 rtx insn = p;
4357 maybe_multiple = 0;
4359 while (1)
4361 insn = NEXT_INSN (insn);
4362 if (insn == loop->scan_start)
4363 break;
4364 if (insn == loop->end)
4366 if (loop->top != 0)
4367 insn = loop->top;
4368 else
4369 break;
4370 if (insn == loop->scan_start)
4371 break;
4374 if (GET_CODE (insn) == JUMP_INSN
4375 && GET_CODE (PATTERN (insn)) != RETURN
4376 && (!any_condjump_p (insn)
4377 || (JUMP_LABEL (insn) != 0
4378 && JUMP_LABEL (insn) != loop->scan_start
4379 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4381 maybe_multiple = 1;
4382 break;
4387 /* Past a jump, we get to insns for which we can't count
4388 on whether they will be executed during each iteration. */
4389 /* This code appears twice in strength_reduce. There is also similar
4390 code in scan_loop. */
4391 if (GET_CODE (p) == JUMP_INSN
4392 /* If we enter the loop in the middle, and scan around to the
4393 beginning, don't set not_every_iteration for that.
4394 This can be any kind of jump, since we want to know if insns
4395 will be executed if the loop is executed. */
4396 && !(JUMP_LABEL (p) == loop->top
4397 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4398 && any_uncondjump_p (p))
4399 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
4401 rtx label = 0;
4403 /* If this is a jump outside the loop, then it also doesn't
4404 matter. Check to see if the target of this branch is on the
4405 loop->exits_labels list. */
4407 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4408 if (XEXP (label, 0) == JUMP_LABEL (p))
4409 break;
4411 if (!label)
4412 not_every_iteration = 1;
4415 else if (GET_CODE (p) == NOTE)
4417 /* At the virtual top of a converted loop, insns are again known to
4418 be executed each iteration: logically, the loop begins here
4419 even though the exit code has been duplicated.
4421 Insns are also again known to be executed each iteration at
4422 the LOOP_CONT note. */
4423 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4424 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4425 && loop_depth == 0)
4426 not_every_iteration = 0;
4427 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4428 loop_depth++;
4429 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4430 loop_depth--;
4433 /* Note if we pass a loop latch. If we do, then we can not clear
4434 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4435 a loop since a jump before the last CODE_LABEL may have started
4436 a new loop iteration.
4438 Note that LOOP_TOP is only set for rotated loops and we need
4439 this check for all loops, so compare against the CODE_LABEL
4440 which immediately follows LOOP_START. */
4441 if (GET_CODE (p) == JUMP_INSN
4442 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4443 past_loop_latch = 1;
4445 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4446 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4447 or not an insn is known to be executed each iteration of the
4448 loop, whether or not any iterations are known to occur.
4450 Therefore, if we have just passed a label and have no more labels
4451 between here and the test insn of the loop, and we have not passed
4452 a jump to the top of the loop, then we know these insns will be
4453 executed each iteration. */
4455 if (not_every_iteration
4456 && !past_loop_latch
4457 && GET_CODE (p) == CODE_LABEL
4458 && no_labels_between_p (p, loop->end)
4459 && loop_insn_first_p (p, loop->cont))
4460 not_every_iteration = 0;
4464 static void
4465 loop_bivs_find (struct loop *loop)
4467 struct loop_regs *regs = LOOP_REGS (loop);
4468 struct loop_ivs *ivs = LOOP_IVS (loop);
4469 /* Temporary list pointers for traversing ivs->list. */
4470 struct iv_class *bl, **backbl;
4472 ivs->list = 0;
4474 for_each_insn_in_loop (loop, check_insn_for_bivs);
4476 /* Scan ivs->list to remove all regs that proved not to be bivs.
4477 Make a sanity check against regs->n_times_set. */
4478 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4480 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4481 /* Above happens if register modified by subreg, etc. */
4482 /* Make sure it is not recognized as a basic induction var: */
4483 || regs->array[bl->regno].n_times_set != bl->biv_count
4484 /* If never incremented, it is invariant that we decided not to
4485 move. So leave it alone. */
4486 || ! bl->incremented)
4488 if (loop_dump_stream)
4489 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
4490 bl->regno,
4491 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4492 ? "not induction variable"
4493 : (! bl->incremented ? "never incremented"
4494 : "count error")));
4496 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4497 *backbl = bl->next;
4499 else
4501 backbl = &bl->next;
4503 if (loop_dump_stream)
4504 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
4510 /* Determine how BIVS are initialized by looking through pre-header
4511 extended basic block. */
4512 static void
4513 loop_bivs_init_find (struct loop *loop)
4515 struct loop_ivs *ivs = LOOP_IVS (loop);
4516 /* Temporary list pointers for traversing ivs->list. */
4517 struct iv_class *bl;
4518 int call_seen;
4519 rtx p;
4521 /* Find initial value for each biv by searching backwards from loop_start,
4522 halting at first label. Also record any test condition. */
4524 call_seen = 0;
4525 for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
4527 rtx test;
4529 note_insn = p;
4531 if (GET_CODE (p) == CALL_INSN)
4532 call_seen = 1;
4534 if (INSN_P (p))
4535 note_stores (PATTERN (p), record_initial, ivs);
4537 /* Record any test of a biv that branches around the loop if no store
4538 between it and the start of loop. We only care about tests with
4539 constants and registers and only certain of those. */
4540 if (GET_CODE (p) == JUMP_INSN
4541 && JUMP_LABEL (p) != 0
4542 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4543 && (test = get_condition_for_loop (loop, p)) != 0
4544 && GET_CODE (XEXP (test, 0)) == REG
4545 && REGNO (XEXP (test, 0)) < max_reg_before_loop
4546 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4547 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4548 && bl->init_insn == 0)
4550 /* If an NE test, we have an initial value! */
4551 if (GET_CODE (test) == NE)
4553 bl->init_insn = p;
4554 bl->init_set = gen_rtx_SET (VOIDmode,
4555 XEXP (test, 0), XEXP (test, 1));
4557 else
4558 bl->initial_test = test;
4564 /* Look at the each biv and see if we can say anything better about its
4565 initial value from any initializing insns set up above. (This is done
4566 in two passes to avoid missing SETs in a PARALLEL.) */
4567 static void
4568 loop_bivs_check (struct loop *loop)
4570 struct loop_ivs *ivs = LOOP_IVS (loop);
4571 /* Temporary list pointers for traversing ivs->list. */
4572 struct iv_class *bl;
4573 struct iv_class **backbl;
4575 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4577 rtx src;
4578 rtx note;
4580 if (! bl->init_insn)
4581 continue;
4583 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4584 is a constant, use the value of that. */
4585 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4586 && CONSTANT_P (XEXP (note, 0)))
4587 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4588 && CONSTANT_P (XEXP (note, 0))))
4589 src = XEXP (note, 0);
4590 else
4591 src = SET_SRC (bl->init_set);
4593 if (loop_dump_stream)
4594 fprintf (loop_dump_stream,
4595 "Biv %d: initialized at insn %d: initial value ",
4596 bl->regno, INSN_UID (bl->init_insn));
4598 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4599 || GET_MODE (src) == VOIDmode)
4600 && valid_initial_value_p (src, bl->init_insn,
4601 LOOP_INFO (loop)->pre_header_has_call,
4602 loop->start))
4604 bl->initial_value = src;
4606 if (loop_dump_stream)
4608 print_simple_rtl (loop_dump_stream, src);
4609 fputc ('\n', loop_dump_stream);
4612 /* If we can't make it a giv,
4613 let biv keep initial value of "itself". */
4614 else if (loop_dump_stream)
4615 fprintf (loop_dump_stream, "is complex\n");
4620 /* Search the loop for general induction variables. */
4622 static void
4623 loop_givs_find (struct loop* loop)
4625 for_each_insn_in_loop (loop, check_insn_for_givs);
4629 /* For each giv for which we still don't know whether or not it is
4630 replaceable, check to see if it is replaceable because its final value
4631 can be calculated. */
4633 static void
4634 loop_givs_check (struct loop *loop)
4636 struct loop_ivs *ivs = LOOP_IVS (loop);
4637 struct iv_class *bl;
4639 for (bl = ivs->list; bl; bl = bl->next)
4641 struct induction *v;
4643 for (v = bl->giv; v; v = v->next_iv)
4644 if (! v->replaceable && ! v->not_replaceable)
4645 check_final_value (loop, v);
4650 /* Return nonzero if it is possible to eliminate the biv BL provided
4651 all givs are reduced. This is possible if either the reg is not
4652 used outside the loop, or we can compute what its final value will
4653 be. */
4655 static int
4656 loop_biv_eliminable_p (struct loop *loop, struct iv_class *bl,
4657 int threshold, int insn_count)
4659 /* For architectures with a decrement_and_branch_until_zero insn,
4660 don't do this if we put a REG_NONNEG note on the endtest for this
4661 biv. */
4663 #ifdef HAVE_decrement_and_branch_until_zero
4664 if (bl->nonneg)
4666 if (loop_dump_stream)
4667 fprintf (loop_dump_stream,
4668 "Cannot eliminate nonneg biv %d.\n", bl->regno);
4669 return 0;
4671 #endif
4673 /* Check that biv is used outside loop or if it has a final value.
4674 Compare against bl->init_insn rather than loop->start. We aren't
4675 concerned with any uses of the biv between init_insn and
4676 loop->start since these won't be affected by the value of the biv
4677 elsewhere in the function, so long as init_insn doesn't use the
4678 biv itself. */
4680 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
4681 && bl->init_insn
4682 && INSN_UID (bl->init_insn) < max_uid_for_loop
4683 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
4684 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4685 || (bl->final_value = final_biv_value (loop, bl)))
4686 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
4688 if (loop_dump_stream)
4690 fprintf (loop_dump_stream,
4691 "Cannot eliminate biv %d.\n",
4692 bl->regno);
4693 fprintf (loop_dump_stream,
4694 "First use: insn %d, last use: insn %d.\n",
4695 REGNO_FIRST_UID (bl->regno),
4696 REGNO_LAST_UID (bl->regno));
4698 return 0;
4702 /* Reduce each giv of BL that we have decided to reduce. */
4704 static void
4705 loop_givs_reduce (struct loop *loop, struct iv_class *bl)
4707 struct induction *v;
4709 for (v = bl->giv; v; v = v->next_iv)
4711 struct induction *tv;
4712 if (! v->ignore && v->same == 0)
4714 int auto_inc_opt = 0;
4716 /* If the code for derived givs immediately below has already
4717 allocated a new_reg, we must keep it. */
4718 if (! v->new_reg)
4719 v->new_reg = gen_reg_rtx (v->mode);
4721 #ifdef AUTO_INC_DEC
4722 /* If the target has auto-increment addressing modes, and
4723 this is an address giv, then try to put the increment
4724 immediately after its use, so that flow can create an
4725 auto-increment addressing mode. */
4726 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4727 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4728 /* We don't handle reversed biv's because bl->biv->insn
4729 does not have a valid INSN_LUID. */
4730 && ! bl->reversed
4731 && v->always_executed && ! v->maybe_multiple
4732 && INSN_UID (v->insn) < max_uid_for_loop)
4734 /* If other giv's have been combined with this one, then
4735 this will work only if all uses of the other giv's occur
4736 before this giv's insn. This is difficult to check.
4738 We simplify this by looking for the common case where
4739 there is one DEST_REG giv, and this giv's insn is the
4740 last use of the dest_reg of that DEST_REG giv. If the
4741 increment occurs after the address giv, then we can
4742 perform the optimization. (Otherwise, the increment
4743 would have to go before other_giv, and we would not be
4744 able to combine it with the address giv to get an
4745 auto-inc address.) */
4746 if (v->combined_with)
4748 struct induction *other_giv = 0;
4750 for (tv = bl->giv; tv; tv = tv->next_iv)
4751 if (tv->same == v)
4753 if (other_giv)
4754 break;
4755 else
4756 other_giv = tv;
4758 if (! tv && other_giv
4759 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4760 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4761 == INSN_UID (v->insn))
4762 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4763 auto_inc_opt = 1;
4765 /* Check for case where increment is before the address
4766 giv. Do this test in "loop order". */
4767 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4768 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4769 || (INSN_LUID (bl->biv->insn)
4770 > INSN_LUID (loop->scan_start))))
4771 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4772 && (INSN_LUID (loop->scan_start)
4773 < INSN_LUID (bl->biv->insn))))
4774 auto_inc_opt = -1;
4775 else
4776 auto_inc_opt = 1;
4778 #ifdef HAVE_cc0
4780 rtx prev;
4782 /* We can't put an insn immediately after one setting
4783 cc0, or immediately before one using cc0. */
4784 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4785 || (auto_inc_opt == -1
4786 && (prev = prev_nonnote_insn (v->insn)) != 0
4787 && INSN_P (prev)
4788 && sets_cc0_p (PATTERN (prev))))
4789 auto_inc_opt = 0;
4791 #endif
4793 if (auto_inc_opt)
4794 v->auto_inc_opt = 1;
4796 #endif
4798 /* For each place where the biv is incremented, add an insn
4799 to increment the new, reduced reg for the giv. */
4800 for (tv = bl->biv; tv; tv = tv->next_iv)
4802 rtx insert_before;
4804 /* Skip if location is the same as a previous one. */
4805 if (tv->same)
4806 continue;
4807 if (! auto_inc_opt)
4808 insert_before = NEXT_INSN (tv->insn);
4809 else if (auto_inc_opt == 1)
4810 insert_before = NEXT_INSN (v->insn);
4811 else
4812 insert_before = v->insn;
4814 if (tv->mult_val == const1_rtx)
4815 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4816 v->new_reg, v->new_reg,
4817 0, insert_before);
4818 else /* tv->mult_val == const0_rtx */
4819 /* A multiply is acceptable here
4820 since this is presumed to be seldom executed. */
4821 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4822 v->add_val, v->new_reg,
4823 0, insert_before);
4826 /* Add code at loop start to initialize giv's reduced reg. */
4828 loop_iv_add_mult_hoist (loop,
4829 extend_value_for_giv (v, bl->initial_value),
4830 v->mult_val, v->add_val, v->new_reg);
4836 /* Check for givs whose first use is their definition and whose
4837 last use is the definition of another giv. If so, it is likely
4838 dead and should not be used to derive another giv nor to
4839 eliminate a biv. */
4841 static void
4842 loop_givs_dead_check (struct loop *loop ATTRIBUTE_UNUSED, struct iv_class *bl)
4844 struct induction *v;
4846 for (v = bl->giv; v; v = v->next_iv)
4848 if (v->ignore
4849 || (v->same && v->same->ignore))
4850 continue;
4852 if (v->giv_type == DEST_REG
4853 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4855 struct induction *v1;
4857 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4858 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4859 v->maybe_dead = 1;
4865 static void
4866 loop_givs_rescan (struct loop *loop, struct iv_class *bl, rtx *reg_map)
4868 struct induction *v;
4870 for (v = bl->giv; v; v = v->next_iv)
4872 if (v->same && v->same->ignore)
4873 v->ignore = 1;
4875 if (v->ignore)
4876 continue;
4878 /* Update expression if this was combined, in case other giv was
4879 replaced. */
4880 if (v->same)
4881 v->new_reg = replace_rtx (v->new_reg,
4882 v->same->dest_reg, v->same->new_reg);
4884 /* See if this register is known to be a pointer to something. If
4885 so, see if we can find the alignment. First see if there is a
4886 destination register that is a pointer. If so, this shares the
4887 alignment too. Next see if we can deduce anything from the
4888 computational information. If not, and this is a DEST_ADDR
4889 giv, at least we know that it's a pointer, though we don't know
4890 the alignment. */
4891 if (GET_CODE (v->new_reg) == REG
4892 && v->giv_type == DEST_REG
4893 && REG_POINTER (v->dest_reg))
4894 mark_reg_pointer (v->new_reg,
4895 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4896 else if (GET_CODE (v->new_reg) == REG
4897 && REG_POINTER (v->src_reg))
4899 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4901 if (align == 0
4902 || GET_CODE (v->add_val) != CONST_INT
4903 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4904 align = 0;
4906 mark_reg_pointer (v->new_reg, align);
4908 else if (GET_CODE (v->new_reg) == REG
4909 && GET_CODE (v->add_val) == REG
4910 && REG_POINTER (v->add_val))
4912 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4914 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4915 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4916 align = 0;
4918 mark_reg_pointer (v->new_reg, align);
4920 else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4921 mark_reg_pointer (v->new_reg, 0);
4923 if (v->giv_type == DEST_ADDR)
4924 /* Store reduced reg as the address in the memref where we found
4925 this giv. */
4926 validate_change (v->insn, v->location, v->new_reg, 0);
4927 else if (v->replaceable)
4929 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4931 else
4933 rtx original_insn = v->insn;
4934 rtx note;
4936 /* Not replaceable; emit an insn to set the original giv reg from
4937 the reduced giv, same as above. */
4938 v->insn = loop_insn_emit_after (loop, 0, original_insn,
4939 gen_move_insn (v->dest_reg,
4940 v->new_reg));
4942 /* The original insn may have a REG_EQUAL note. This note is
4943 now incorrect and may result in invalid substitutions later.
4944 The original insn is dead, but may be part of a libcall
4945 sequence, which doesn't seem worth the bother of handling. */
4946 note = find_reg_note (original_insn, REG_EQUAL, NULL_RTX);
4947 if (note)
4948 remove_note (original_insn, note);
4951 /* When a loop is reversed, givs which depend on the reversed
4952 biv, and which are live outside the loop, must be set to their
4953 correct final value. This insn is only needed if the giv is
4954 not replaceable. The correct final value is the same as the
4955 value that the giv starts the reversed loop with. */
4956 if (bl->reversed && ! v->replaceable)
4957 loop_iv_add_mult_sink (loop,
4958 extend_value_for_giv (v, bl->initial_value),
4959 v->mult_val, v->add_val, v->dest_reg);
4960 else if (v->final_value)
4961 loop_insn_sink_or_swim (loop,
4962 gen_load_of_final_value (v->dest_reg,
4963 v->final_value));
4965 if (loop_dump_stream)
4967 fprintf (loop_dump_stream, "giv at %d reduced to ",
4968 INSN_UID (v->insn));
4969 print_simple_rtl (loop_dump_stream, v->new_reg);
4970 fprintf (loop_dump_stream, "\n");
4976 static int
4977 loop_giv_reduce_benefit (struct loop *loop ATTRIBUTE_UNUSED,
4978 struct iv_class *bl, struct induction *v,
4979 rtx test_reg)
4981 int add_cost;
4982 int benefit;
4984 benefit = v->benefit;
4985 PUT_MODE (test_reg, v->mode);
4986 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4987 test_reg, test_reg);
4989 /* Reduce benefit if not replaceable, since we will insert a
4990 move-insn to replace the insn that calculates this giv. Don't do
4991 this unless the giv is a user variable, since it will often be
4992 marked non-replaceable because of the duplication of the exit
4993 code outside the loop. In such a case, the copies we insert are
4994 dead and will be deleted. So they don't have a cost. Similar
4995 situations exist. */
4996 /* ??? The new final_[bg]iv_value code does a much better job of
4997 finding replaceable giv's, and hence this code may no longer be
4998 necessary. */
4999 if (! v->replaceable && ! bl->eliminable
5000 && REG_USERVAR_P (v->dest_reg))
5001 benefit -= copy_cost;
5003 /* Decrease the benefit to count the add-insns that we will insert
5004 to increment the reduced reg for the giv. ??? This can
5005 overestimate the run-time cost of the additional insns, e.g. if
5006 there are multiple basic blocks that increment the biv, but only
5007 one of these blocks is executed during each iteration. There is
5008 no good way to detect cases like this with the current structure
5009 of the loop optimizer. This code is more accurate for
5010 determining code size than run-time benefits. */
5011 benefit -= add_cost * bl->biv_count;
5013 /* Decide whether to strength-reduce this giv or to leave the code
5014 unchanged (recompute it from the biv each time it is used). This
5015 decision can be made independently for each giv. */
5017 #ifdef AUTO_INC_DEC
5018 /* Attempt to guess whether autoincrement will handle some of the
5019 new add insns; if so, increase BENEFIT (undo the subtraction of
5020 add_cost that was done above). */
5021 if (v->giv_type == DEST_ADDR
5022 /* Increasing the benefit is risky, since this is only a guess.
5023 Avoid increasing register pressure in cases where there would
5024 be no other benefit from reducing this giv. */
5025 && benefit > 0
5026 && GET_CODE (v->mult_val) == CONST_INT)
5028 int size = GET_MODE_SIZE (GET_MODE (v->mem));
5030 if (HAVE_POST_INCREMENT
5031 && INTVAL (v->mult_val) == size)
5032 benefit += add_cost * bl->biv_count;
5033 else if (HAVE_PRE_INCREMENT
5034 && INTVAL (v->mult_val) == size)
5035 benefit += add_cost * bl->biv_count;
5036 else if (HAVE_POST_DECREMENT
5037 && -INTVAL (v->mult_val) == size)
5038 benefit += add_cost * bl->biv_count;
5039 else if (HAVE_PRE_DECREMENT
5040 && -INTVAL (v->mult_val) == size)
5041 benefit += add_cost * bl->biv_count;
5043 #endif
5045 return benefit;
5049 /* Free IV structures for LOOP. */
5051 static void
5052 loop_ivs_free (struct loop *loop)
5054 struct loop_ivs *ivs = LOOP_IVS (loop);
5055 struct iv_class *iv = ivs->list;
5057 free (ivs->regs);
5059 while (iv)
5061 struct iv_class *next = iv->next;
5062 struct induction *induction;
5063 struct induction *next_induction;
5065 for (induction = iv->biv; induction; induction = next_induction)
5067 next_induction = induction->next_iv;
5068 free (induction);
5070 for (induction = iv->giv; induction; induction = next_induction)
5072 next_induction = induction->next_iv;
5073 free (induction);
5076 free (iv);
5077 iv = next;
5082 /* Perform strength reduction and induction variable elimination.
5084 Pseudo registers created during this function will be beyond the
5085 last valid index in several tables including
5086 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
5087 problem here, because the added registers cannot be givs outside of
5088 their loop, and hence will never be reconsidered. But scan_loop
5089 must check regnos to make sure they are in bounds. */
5091 static void
5092 strength_reduce (struct loop *loop, int flags)
5094 struct loop_info *loop_info = LOOP_INFO (loop);
5095 struct loop_regs *regs = LOOP_REGS (loop);
5096 struct loop_ivs *ivs = LOOP_IVS (loop);
5097 rtx p;
5098 /* Temporary list pointer for traversing ivs->list. */
5099 struct iv_class *bl;
5100 /* Ratio of extra register life span we can justify
5101 for saving an instruction. More if loop doesn't call subroutines
5102 since in that case saving an insn makes more difference
5103 and more registers are available. */
5104 /* ??? could set this to last value of threshold in move_movables */
5105 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
5106 /* Map of pseudo-register replacements. */
5107 rtx *reg_map = NULL;
5108 int reg_map_size;
5109 int unrolled_insn_copies = 0;
5110 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
5111 int insn_count = count_insns_in_loop (loop);
5113 addr_placeholder = gen_reg_rtx (Pmode);
5115 ivs->n_regs = max_reg_before_loop;
5116 ivs->regs = xcalloc (ivs->n_regs, sizeof (struct iv));
5118 /* Find all BIVs in loop. */
5119 loop_bivs_find (loop);
5121 /* Exit if there are no bivs. */
5122 if (! ivs->list)
5124 /* Can still unroll the loop anyways, but indicate that there is no
5125 strength reduction info available. */
5126 if (flags & LOOP_UNROLL)
5127 unroll_loop (loop, insn_count, 0);
5129 loop_ivs_free (loop);
5130 return;
5133 /* Determine how BIVS are initialized by looking through pre-header
5134 extended basic block. */
5135 loop_bivs_init_find (loop);
5137 /* Look at the each biv and see if we can say anything better about its
5138 initial value from any initializing insns set up above. */
5139 loop_bivs_check (loop);
5141 /* Search the loop for general induction variables. */
5142 loop_givs_find (loop);
5144 /* Try to calculate and save the number of loop iterations. This is
5145 set to zero if the actual number can not be calculated. This must
5146 be called after all giv's have been identified, since otherwise it may
5147 fail if the iteration variable is a giv. */
5148 loop_iterations (loop);
5150 #ifdef HAVE_prefetch
5151 if (flags & LOOP_PREFETCH)
5152 emit_prefetch_instructions (loop);
5153 #endif
5155 /* Now for each giv for which we still don't know whether or not it is
5156 replaceable, check to see if it is replaceable because its final value
5157 can be calculated. This must be done after loop_iterations is called,
5158 so that final_giv_value will work correctly. */
5159 loop_givs_check (loop);
5161 /* Try to prove that the loop counter variable (if any) is always
5162 nonnegative; if so, record that fact with a REG_NONNEG note
5163 so that "decrement and branch until zero" insn can be used. */
5164 check_dbra_loop (loop, insn_count);
5166 /* Create reg_map to hold substitutions for replaceable giv regs.
5167 Some givs might have been made from biv increments, so look at
5168 ivs->reg_iv_type for a suitable size. */
5169 reg_map_size = ivs->n_regs;
5170 reg_map = xcalloc (reg_map_size, sizeof (rtx));
5172 /* Examine each iv class for feasibility of strength reduction/induction
5173 variable elimination. */
5175 for (bl = ivs->list; bl; bl = bl->next)
5177 struct induction *v;
5178 int benefit;
5180 /* Test whether it will be possible to eliminate this biv
5181 provided all givs are reduced. */
5182 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
5184 /* This will be true at the end, if all givs which depend on this
5185 biv have been strength reduced.
5186 We can't (currently) eliminate the biv unless this is so. */
5187 bl->all_reduced = 1;
5189 /* Check each extension dependent giv in this class to see if its
5190 root biv is safe from wrapping in the interior mode. */
5191 check_ext_dependent_givs (loop, bl);
5193 /* Combine all giv's for this iv_class. */
5194 combine_givs (regs, bl);
5196 for (v = bl->giv; v; v = v->next_iv)
5198 struct induction *tv;
5200 if (v->ignore || v->same)
5201 continue;
5203 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
5205 /* If an insn is not to be strength reduced, then set its ignore
5206 flag, and clear bl->all_reduced. */
5208 /* A giv that depends on a reversed biv must be reduced if it is
5209 used after the loop exit, otherwise, it would have the wrong
5210 value after the loop exit. To make it simple, just reduce all
5211 of such giv's whether or not we know they are used after the loop
5212 exit. */
5214 if (! flag_reduce_all_givs
5215 && v->lifetime * threshold * benefit < insn_count
5216 && ! bl->reversed)
5218 if (loop_dump_stream)
5219 fprintf (loop_dump_stream,
5220 "giv of insn %d not worth while, %d vs %d.\n",
5221 INSN_UID (v->insn),
5222 v->lifetime * threshold * benefit, insn_count);
5223 v->ignore = 1;
5224 bl->all_reduced = 0;
5226 else
5228 /* Check that we can increment the reduced giv without a
5229 multiply insn. If not, reject it. */
5231 for (tv = bl->biv; tv; tv = tv->next_iv)
5232 if (tv->mult_val == const1_rtx
5233 && ! product_cheap_p (tv->add_val, v->mult_val))
5235 if (loop_dump_stream)
5236 fprintf (loop_dump_stream,
5237 "giv of insn %d: would need a multiply.\n",
5238 INSN_UID (v->insn));
5239 v->ignore = 1;
5240 bl->all_reduced = 0;
5241 break;
5246 /* Check for givs whose first use is their definition and whose
5247 last use is the definition of another giv. If so, it is likely
5248 dead and should not be used to derive another giv nor to
5249 eliminate a biv. */
5250 loop_givs_dead_check (loop, bl);
5252 /* Reduce each giv that we decided to reduce. */
5253 loop_givs_reduce (loop, bl);
5255 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
5256 as not reduced.
5258 For each giv register that can be reduced now: if replaceable,
5259 substitute reduced reg wherever the old giv occurs;
5260 else add new move insn "giv_reg = reduced_reg". */
5261 loop_givs_rescan (loop, bl, reg_map);
5263 /* All the givs based on the biv bl have been reduced if they
5264 merit it. */
5266 /* For each giv not marked as maybe dead that has been combined with a
5267 second giv, clear any "maybe dead" mark on that second giv.
5268 v->new_reg will either be or refer to the register of the giv it
5269 combined with.
5271 Doing this clearing avoids problems in biv elimination where
5272 a giv's new_reg is a complex value that can't be put in the
5273 insn but the giv combined with (with a reg as new_reg) is
5274 marked maybe_dead. Since the register will be used in either
5275 case, we'd prefer it be used from the simpler giv. */
5277 for (v = bl->giv; v; v = v->next_iv)
5278 if (! v->maybe_dead && v->same)
5279 v->same->maybe_dead = 0;
5281 /* Try to eliminate the biv, if it is a candidate.
5282 This won't work if ! bl->all_reduced,
5283 since the givs we planned to use might not have been reduced.
5285 We have to be careful that we didn't initially think we could
5286 eliminate this biv because of a giv that we now think may be
5287 dead and shouldn't be used as a biv replacement.
5289 Also, there is the possibility that we may have a giv that looks
5290 like it can be used to eliminate a biv, but the resulting insn
5291 isn't valid. This can happen, for example, on the 88k, where a
5292 JUMP_INSN can compare a register only with zero. Attempts to
5293 replace it with a compare with a constant will fail.
5295 Note that in cases where this call fails, we may have replaced some
5296 of the occurrences of the biv with a giv, but no harm was done in
5297 doing so in the rare cases where it can occur. */
5299 if (bl->all_reduced == 1 && bl->eliminable
5300 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
5302 /* ?? If we created a new test to bypass the loop entirely,
5303 or otherwise drop straight in, based on this test, then
5304 we might want to rewrite it also. This way some later
5305 pass has more hope of removing the initialization of this
5306 biv entirely. */
5308 /* If final_value != 0, then the biv may be used after loop end
5309 and we must emit an insn to set it just in case.
5311 Reversed bivs already have an insn after the loop setting their
5312 value, so we don't need another one. We can't calculate the
5313 proper final value for such a biv here anyways. */
5314 if (bl->final_value && ! bl->reversed)
5315 loop_insn_sink_or_swim (loop,
5316 gen_load_of_final_value (bl->biv->dest_reg,
5317 bl->final_value));
5319 if (loop_dump_stream)
5320 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5321 bl->regno);
5323 /* See above note wrt final_value. But since we couldn't eliminate
5324 the biv, we must set the value after the loop instead of before. */
5325 else if (bl->final_value && ! bl->reversed)
5326 loop_insn_sink (loop, gen_load_of_final_value (bl->biv->dest_reg,
5327 bl->final_value));
5330 /* Go through all the instructions in the loop, making all the
5331 register substitutions scheduled in REG_MAP. */
5333 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
5334 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5335 || GET_CODE (p) == CALL_INSN)
5337 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5338 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
5339 INSN_CODE (p) = -1;
5342 if (loop_info->n_iterations > 0)
5344 /* When we completely unroll a loop we will likely not need the increment
5345 of the loop BIV and we will not need the conditional branch at the
5346 end of the loop. */
5347 unrolled_insn_copies = insn_count - 2;
5349 #ifdef HAVE_cc0
5350 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5351 need the comparison before the conditional branch at the end of the
5352 loop. */
5353 unrolled_insn_copies -= 1;
5354 #endif
5356 /* We'll need one copy for each loop iteration. */
5357 unrolled_insn_copies *= loop_info->n_iterations;
5359 /* A little slop to account for the ability to remove initialization
5360 code, better CSE, and other secondary benefits of completely
5361 unrolling some loops. */
5362 unrolled_insn_copies -= 1;
5364 /* Clamp the value. */
5365 if (unrolled_insn_copies < 0)
5366 unrolled_insn_copies = 0;
5369 /* Unroll loops from within strength reduction so that we can use the
5370 induction variable information that strength_reduce has already
5371 collected. Always unroll loops that would be as small or smaller
5372 unrolled than when rolled. */
5373 if ((flags & LOOP_UNROLL)
5374 || ((flags & LOOP_AUTO_UNROLL)
5375 && loop_info->n_iterations > 0
5376 && unrolled_insn_copies <= insn_count))
5377 unroll_loop (loop, insn_count, 1);
5379 #ifdef HAVE_doloop_end
5380 if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
5381 doloop_optimize (loop);
5382 #endif /* HAVE_doloop_end */
5384 /* In case number of iterations is known, drop branch prediction note
5385 in the branch. Do that only in second loop pass, as loop unrolling
5386 may change the number of iterations performed. */
5387 if (flags & LOOP_BCT)
5389 unsigned HOST_WIDE_INT n
5390 = loop_info->n_iterations / loop_info->unroll_number;
5391 if (n > 1)
5392 predict_insn (prev_nonnote_insn (loop->end), PRED_LOOP_ITERATIONS,
5393 REG_BR_PROB_BASE - REG_BR_PROB_BASE / n);
5396 if (loop_dump_stream)
5397 fprintf (loop_dump_stream, "\n");
5399 loop_ivs_free (loop);
5400 if (reg_map)
5401 free (reg_map);
5404 /*Record all basic induction variables calculated in the insn. */
5405 static rtx
5406 check_insn_for_bivs (struct loop *loop, rtx p, int not_every_iteration,
5407 int maybe_multiple)
5409 struct loop_ivs *ivs = LOOP_IVS (loop);
5410 rtx set;
5411 rtx dest_reg;
5412 rtx inc_val;
5413 rtx mult_val;
5414 rtx *location;
5416 if (GET_CODE (p) == INSN
5417 && (set = single_set (p))
5418 && GET_CODE (SET_DEST (set)) == REG)
5420 dest_reg = SET_DEST (set);
5421 if (REGNO (dest_reg) < max_reg_before_loop
5422 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
5423 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
5425 if (basic_induction_var (loop, SET_SRC (set),
5426 GET_MODE (SET_SRC (set)),
5427 dest_reg, p, &inc_val, &mult_val,
5428 &location))
5430 /* It is a possible basic induction variable.
5431 Create and initialize an induction structure for it. */
5433 struct induction *v = xmalloc (sizeof (struct induction));
5435 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
5436 not_every_iteration, maybe_multiple);
5437 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
5439 else if (REGNO (dest_reg) < ivs->n_regs)
5440 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
5443 return p;
5446 /* Record all givs calculated in the insn.
5447 A register is a giv if: it is only set once, it is a function of a
5448 biv and a constant (or invariant), and it is not a biv. */
5449 static rtx
5450 check_insn_for_givs (struct loop *loop, rtx p, int not_every_iteration,
5451 int maybe_multiple)
5453 struct loop_regs *regs = LOOP_REGS (loop);
5455 rtx set;
5456 /* Look for a general induction variable in a register. */
5457 if (GET_CODE (p) == INSN
5458 && (set = single_set (p))
5459 && GET_CODE (SET_DEST (set)) == REG
5460 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
5462 rtx src_reg;
5463 rtx dest_reg;
5464 rtx add_val;
5465 rtx mult_val;
5466 rtx ext_val;
5467 int benefit;
5468 rtx regnote = 0;
5469 rtx last_consec_insn;
5471 dest_reg = SET_DEST (set);
5472 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
5473 return p;
5475 if (/* SET_SRC is a giv. */
5476 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
5477 &mult_val, &ext_val, 0, &benefit, VOIDmode)
5478 /* Equivalent expression is a giv. */
5479 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
5480 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
5481 &add_val, &mult_val, &ext_val, 0,
5482 &benefit, VOIDmode)))
5483 /* Don't try to handle any regs made by loop optimization.
5484 We have nothing on them in regno_first_uid, etc. */
5485 && REGNO (dest_reg) < max_reg_before_loop
5486 /* Don't recognize a BASIC_INDUCT_VAR here. */
5487 && dest_reg != src_reg
5488 /* This must be the only place where the register is set. */
5489 && (regs->array[REGNO (dest_reg)].n_times_set == 1
5490 /* or all sets must be consecutive and make a giv. */
5491 || (benefit = consec_sets_giv (loop, benefit, p,
5492 src_reg, dest_reg,
5493 &add_val, &mult_val, &ext_val,
5494 &last_consec_insn))))
5496 struct induction *v = xmalloc (sizeof (struct induction));
5498 /* If this is a library call, increase benefit. */
5499 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5500 benefit += libcall_benefit (p);
5502 /* Skip the consecutive insns, if there are any. */
5503 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
5504 p = last_consec_insn;
5506 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
5507 ext_val, benefit, DEST_REG, not_every_iteration,
5508 maybe_multiple, (rtx*) 0);
5513 /* Look for givs which are memory addresses. */
5514 if (GET_CODE (p) == INSN)
5515 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
5516 maybe_multiple);
5518 /* Update the status of whether giv can derive other givs. This can
5519 change when we pass a label or an insn that updates a biv. */
5520 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5521 || GET_CODE (p) == CODE_LABEL)
5522 update_giv_derive (loop, p);
5523 return p;
5526 /* Return 1 if X is a valid source for an initial value (or as value being
5527 compared against in an initial test).
5529 X must be either a register or constant and must not be clobbered between
5530 the current insn and the start of the loop.
5532 INSN is the insn containing X. */
5534 static int
5535 valid_initial_value_p (rtx x, rtx insn, int call_seen, rtx loop_start)
5537 if (CONSTANT_P (x))
5538 return 1;
5540 /* Only consider pseudos we know about initialized in insns whose luids
5541 we know. */
5542 if (GET_CODE (x) != REG
5543 || REGNO (x) >= max_reg_before_loop)
5544 return 0;
5546 /* Don't use call-clobbered registers across a call which clobbers it. On
5547 some machines, don't use any hard registers at all. */
5548 if (REGNO (x) < FIRST_PSEUDO_REGISTER
5549 && (SMALL_REGISTER_CLASSES
5550 || (call_used_regs[REGNO (x)] && call_seen)))
5551 return 0;
5553 /* Don't use registers that have been clobbered before the start of the
5554 loop. */
5555 if (reg_set_between_p (x, insn, loop_start))
5556 return 0;
5558 return 1;
5561 /* Scan X for memory refs and check each memory address
5562 as a possible giv. INSN is the insn whose pattern X comes from.
5563 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5564 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5565 more than once in each loop iteration. */
5567 static void
5568 find_mem_givs (const struct loop *loop, rtx x, rtx insn,
5569 int not_every_iteration, int maybe_multiple)
5571 int i, j;
5572 enum rtx_code code;
5573 const char *fmt;
5575 if (x == 0)
5576 return;
5578 code = GET_CODE (x);
5579 switch (code)
5581 case REG:
5582 case CONST_INT:
5583 case CONST:
5584 case CONST_DOUBLE:
5585 case SYMBOL_REF:
5586 case LABEL_REF:
5587 case PC:
5588 case CC0:
5589 case ADDR_VEC:
5590 case ADDR_DIFF_VEC:
5591 case USE:
5592 case CLOBBER:
5593 return;
5595 case MEM:
5597 rtx src_reg;
5598 rtx add_val;
5599 rtx mult_val;
5600 rtx ext_val;
5601 int benefit;
5603 /* This code used to disable creating GIVs with mult_val == 1 and
5604 add_val == 0. However, this leads to lost optimizations when
5605 it comes time to combine a set of related DEST_ADDR GIVs, since
5606 this one would not be seen. */
5608 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
5609 &mult_val, &ext_val, 1, &benefit,
5610 GET_MODE (x)))
5612 /* Found one; record it. */
5613 struct induction *v = xmalloc (sizeof (struct induction));
5615 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
5616 add_val, ext_val, benefit, DEST_ADDR,
5617 not_every_iteration, maybe_multiple, &XEXP (x, 0));
5619 v->mem = x;
5622 return;
5624 default:
5625 break;
5628 /* Recursively scan the subexpressions for other mem refs. */
5630 fmt = GET_RTX_FORMAT (code);
5631 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5632 if (fmt[i] == 'e')
5633 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
5634 maybe_multiple);
5635 else if (fmt[i] == 'E')
5636 for (j = 0; j < XVECLEN (x, i); j++)
5637 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
5638 maybe_multiple);
5641 /* Fill in the data about one biv update.
5642 V is the `struct induction' in which we record the biv. (It is
5643 allocated by the caller, with alloca.)
5644 INSN is the insn that sets it.
5645 DEST_REG is the biv's reg.
5647 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5648 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5649 being set to INC_VAL.
5651 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5652 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5653 can be executed more than once per iteration. If MAYBE_MULTIPLE
5654 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5655 executed exactly once per iteration. */
5657 static void
5658 record_biv (struct loop *loop, struct induction *v, rtx insn, rtx dest_reg,
5659 rtx inc_val, rtx mult_val, rtx *location,
5660 int not_every_iteration, int maybe_multiple)
5662 struct loop_ivs *ivs = LOOP_IVS (loop);
5663 struct iv_class *bl;
5665 v->insn = insn;
5666 v->src_reg = dest_reg;
5667 v->dest_reg = dest_reg;
5668 v->mult_val = mult_val;
5669 v->add_val = inc_val;
5670 v->ext_dependent = NULL_RTX;
5671 v->location = location;
5672 v->mode = GET_MODE (dest_reg);
5673 v->always_computable = ! not_every_iteration;
5674 v->always_executed = ! not_every_iteration;
5675 v->maybe_multiple = maybe_multiple;
5676 v->same = 0;
5678 /* Add this to the reg's iv_class, creating a class
5679 if this is the first incrementation of the reg. */
5681 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
5682 if (bl == 0)
5684 /* Create and initialize new iv_class. */
5686 bl = xmalloc (sizeof (struct iv_class));
5688 bl->regno = REGNO (dest_reg);
5689 bl->biv = 0;
5690 bl->giv = 0;
5691 bl->biv_count = 0;
5692 bl->giv_count = 0;
5694 /* Set initial value to the reg itself. */
5695 bl->initial_value = dest_reg;
5696 bl->final_value = 0;
5697 /* We haven't seen the initializing insn yet. */
5698 bl->init_insn = 0;
5699 bl->init_set = 0;
5700 bl->initial_test = 0;
5701 bl->incremented = 0;
5702 bl->eliminable = 0;
5703 bl->nonneg = 0;
5704 bl->reversed = 0;
5705 bl->total_benefit = 0;
5707 /* Add this class to ivs->list. */
5708 bl->next = ivs->list;
5709 ivs->list = bl;
5711 /* Put it in the array of biv register classes. */
5712 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
5714 else
5716 /* Check if location is the same as a previous one. */
5717 struct induction *induction;
5718 for (induction = bl->biv; induction; induction = induction->next_iv)
5719 if (location == induction->location)
5721 v->same = induction;
5722 break;
5726 /* Update IV_CLASS entry for this biv. */
5727 v->next_iv = bl->biv;
5728 bl->biv = v;
5729 bl->biv_count++;
5730 if (mult_val == const1_rtx)
5731 bl->incremented = 1;
5733 if (loop_dump_stream)
5734 loop_biv_dump (v, loop_dump_stream, 0);
5737 /* Fill in the data about one giv.
5738 V is the `struct induction' in which we record the giv. (It is
5739 allocated by the caller, with alloca.)
5740 INSN is the insn that sets it.
5741 BENEFIT estimates the savings from deleting this insn.
5742 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5743 into a register or is used as a memory address.
5745 SRC_REG is the biv reg which the giv is computed from.
5746 DEST_REG is the giv's reg (if the giv is stored in a reg).
5747 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5748 LOCATION points to the place where this giv's value appears in INSN. */
5750 static void
5751 record_giv (const struct loop *loop, struct induction *v, rtx insn,
5752 rtx src_reg, rtx dest_reg, rtx mult_val, rtx add_val,
5753 rtx ext_val, int benefit, enum g_types type,
5754 int not_every_iteration, int maybe_multiple, rtx *location)
5756 struct loop_ivs *ivs = LOOP_IVS (loop);
5757 struct induction *b;
5758 struct iv_class *bl;
5759 rtx set = single_set (insn);
5760 rtx temp;
5762 /* Attempt to prove constantness of the values. Don't let simplify_rtx
5763 undo the MULT canonicalization that we performed earlier. */
5764 temp = simplify_rtx (add_val);
5765 if (temp
5766 && ! (GET_CODE (add_val) == MULT
5767 && GET_CODE (temp) == ASHIFT))
5768 add_val = temp;
5770 v->insn = insn;
5771 v->src_reg = src_reg;
5772 v->giv_type = type;
5773 v->dest_reg = dest_reg;
5774 v->mult_val = mult_val;
5775 v->add_val = add_val;
5776 v->ext_dependent = ext_val;
5777 v->benefit = benefit;
5778 v->location = location;
5779 v->cant_derive = 0;
5780 v->combined_with = 0;
5781 v->maybe_multiple = maybe_multiple;
5782 v->maybe_dead = 0;
5783 v->derive_adjustment = 0;
5784 v->same = 0;
5785 v->ignore = 0;
5786 v->new_reg = 0;
5787 v->final_value = 0;
5788 v->same_insn = 0;
5789 v->auto_inc_opt = 0;
5790 v->unrolled = 0;
5791 v->shared = 0;
5793 /* The v->always_computable field is used in update_giv_derive, to
5794 determine whether a giv can be used to derive another giv. For a
5795 DEST_REG giv, INSN computes a new value for the giv, so its value
5796 isn't computable if INSN insn't executed every iteration.
5797 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5798 it does not compute a new value. Hence the value is always computable
5799 regardless of whether INSN is executed each iteration. */
5801 if (type == DEST_ADDR)
5802 v->always_computable = 1;
5803 else
5804 v->always_computable = ! not_every_iteration;
5806 v->always_executed = ! not_every_iteration;
5808 if (type == DEST_ADDR)
5810 v->mode = GET_MODE (*location);
5811 v->lifetime = 1;
5813 else /* type == DEST_REG */
5815 v->mode = GET_MODE (SET_DEST (set));
5817 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
5819 /* If the lifetime is zero, it means that this register is
5820 really a dead store. So mark this as a giv that can be
5821 ignored. This will not prevent the biv from being eliminated. */
5822 if (v->lifetime == 0)
5823 v->ignore = 1;
5825 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
5826 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
5829 /* Add the giv to the class of givs computed from one biv. */
5831 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
5832 if (bl)
5834 v->next_iv = bl->giv;
5835 bl->giv = v;
5836 /* Don't count DEST_ADDR. This is supposed to count the number of
5837 insns that calculate givs. */
5838 if (type == DEST_REG)
5839 bl->giv_count++;
5840 bl->total_benefit += benefit;
5842 else
5843 /* Fatal error, biv missing for this giv? */
5844 abort ();
5846 if (type == DEST_ADDR)
5848 v->replaceable = 1;
5849 v->not_replaceable = 0;
5851 else
5853 /* The giv can be replaced outright by the reduced register only if all
5854 of the following conditions are true:
5855 - the insn that sets the giv is always executed on any iteration
5856 on which the giv is used at all
5857 (there are two ways to deduce this:
5858 either the insn is executed on every iteration,
5859 or all uses follow that insn in the same basic block),
5860 - the giv is not used outside the loop
5861 - no assignments to the biv occur during the giv's lifetime. */
5863 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5864 /* Previous line always fails if INSN was moved by loop opt. */
5865 && REGNO_LAST_LUID (REGNO (dest_reg))
5866 < INSN_LUID (loop->end)
5867 && (! not_every_iteration
5868 || last_use_this_basic_block (dest_reg, insn)))
5870 /* Now check that there are no assignments to the biv within the
5871 giv's lifetime. This requires two separate checks. */
5873 /* Check each biv update, and fail if any are between the first
5874 and last use of the giv.
5876 If this loop contains an inner loop that was unrolled, then
5877 the insn modifying the biv may have been emitted by the loop
5878 unrolling code, and hence does not have a valid luid. Just
5879 mark the biv as not replaceable in this case. It is not very
5880 useful as a biv, because it is used in two different loops.
5881 It is very unlikely that we would be able to optimize the giv
5882 using this biv anyways. */
5884 v->replaceable = 1;
5885 v->not_replaceable = 0;
5886 for (b = bl->biv; b; b = b->next_iv)
5888 if (INSN_UID (b->insn) >= max_uid_for_loop
5889 || ((INSN_LUID (b->insn)
5890 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
5891 && (INSN_LUID (b->insn)
5892 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
5894 v->replaceable = 0;
5895 v->not_replaceable = 1;
5896 break;
5900 /* If there are any backwards branches that go from after the
5901 biv update to before it, then this giv is not replaceable. */
5902 if (v->replaceable)
5903 for (b = bl->biv; b; b = b->next_iv)
5904 if (back_branch_in_range_p (loop, b->insn))
5906 v->replaceable = 0;
5907 v->not_replaceable = 1;
5908 break;
5911 else
5913 /* May still be replaceable, we don't have enough info here to
5914 decide. */
5915 v->replaceable = 0;
5916 v->not_replaceable = 0;
5920 /* Record whether the add_val contains a const_int, for later use by
5921 combine_givs. */
5923 rtx tem = add_val;
5925 v->no_const_addval = 1;
5926 if (tem == const0_rtx)
5928 else if (CONSTANT_P (add_val))
5929 v->no_const_addval = 0;
5930 if (GET_CODE (tem) == PLUS)
5932 while (1)
5934 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5935 tem = XEXP (tem, 0);
5936 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5937 tem = XEXP (tem, 1);
5938 else
5939 break;
5941 if (CONSTANT_P (XEXP (tem, 1)))
5942 v->no_const_addval = 0;
5946 if (loop_dump_stream)
5947 loop_giv_dump (v, loop_dump_stream, 0);
5950 /* All this does is determine whether a giv can be made replaceable because
5951 its final value can be calculated. This code can not be part of record_giv
5952 above, because final_giv_value requires that the number of loop iterations
5953 be known, and that can not be accurately calculated until after all givs
5954 have been identified. */
5956 static void
5957 check_final_value (const struct loop *loop, struct induction *v)
5959 rtx final_value = 0;
5961 /* DEST_ADDR givs will never reach here, because they are always marked
5962 replaceable above in record_giv. */
5964 /* The giv can be replaced outright by the reduced register only if all
5965 of the following conditions are true:
5966 - the insn that sets the giv is always executed on any iteration
5967 on which the giv is used at all
5968 (there are two ways to deduce this:
5969 either the insn is executed on every iteration,
5970 or all uses follow that insn in the same basic block),
5971 - its final value can be calculated (this condition is different
5972 than the one above in record_giv)
5973 - it's not used before the it's set
5974 - no assignments to the biv occur during the giv's lifetime. */
5976 #if 0
5977 /* This is only called now when replaceable is known to be false. */
5978 /* Clear replaceable, so that it won't confuse final_giv_value. */
5979 v->replaceable = 0;
5980 #endif
5982 if ((final_value = final_giv_value (loop, v))
5983 && (v->always_executed
5984 || last_use_this_basic_block (v->dest_reg, v->insn)))
5986 int biv_increment_seen = 0, before_giv_insn = 0;
5987 rtx p = v->insn;
5988 rtx last_giv_use;
5990 v->replaceable = 1;
5991 v->not_replaceable = 0;
5993 /* When trying to determine whether or not a biv increment occurs
5994 during the lifetime of the giv, we can ignore uses of the variable
5995 outside the loop because final_value is true. Hence we can not
5996 use regno_last_uid and regno_first_uid as above in record_giv. */
5998 /* Search the loop to determine whether any assignments to the
5999 biv occur during the giv's lifetime. Start with the insn
6000 that sets the giv, and search around the loop until we come
6001 back to that insn again.
6003 Also fail if there is a jump within the giv's lifetime that jumps
6004 to somewhere outside the lifetime but still within the loop. This
6005 catches spaghetti code where the execution order is not linear, and
6006 hence the above test fails. Here we assume that the giv lifetime
6007 does not extend from one iteration of the loop to the next, so as
6008 to make the test easier. Since the lifetime isn't known yet,
6009 this requires two loops. See also record_giv above. */
6011 last_giv_use = v->insn;
6013 while (1)
6015 p = NEXT_INSN (p);
6016 if (p == loop->end)
6018 before_giv_insn = 1;
6019 p = NEXT_INSN (loop->start);
6021 if (p == v->insn)
6022 break;
6024 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
6025 || GET_CODE (p) == CALL_INSN)
6027 /* It is possible for the BIV increment to use the GIV if we
6028 have a cycle. Thus we must be sure to check each insn for
6029 both BIV and GIV uses, and we must check for BIV uses
6030 first. */
6032 if (! biv_increment_seen
6033 && reg_set_p (v->src_reg, PATTERN (p)))
6034 biv_increment_seen = 1;
6036 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
6038 if (biv_increment_seen || before_giv_insn)
6040 v->replaceable = 0;
6041 v->not_replaceable = 1;
6042 break;
6044 last_giv_use = p;
6049 /* Now that the lifetime of the giv is known, check for branches
6050 from within the lifetime to outside the lifetime if it is still
6051 replaceable. */
6053 if (v->replaceable)
6055 p = v->insn;
6056 while (1)
6058 p = NEXT_INSN (p);
6059 if (p == loop->end)
6060 p = NEXT_INSN (loop->start);
6061 if (p == last_giv_use)
6062 break;
6064 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
6065 && LABEL_NAME (JUMP_LABEL (p))
6066 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
6067 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
6068 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
6069 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
6071 v->replaceable = 0;
6072 v->not_replaceable = 1;
6074 if (loop_dump_stream)
6075 fprintf (loop_dump_stream,
6076 "Found branch outside giv lifetime.\n");
6078 break;
6083 /* If it is replaceable, then save the final value. */
6084 if (v->replaceable)
6085 v->final_value = final_value;
6088 if (loop_dump_stream && v->replaceable)
6089 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
6090 INSN_UID (v->insn), REGNO (v->dest_reg));
6093 /* Update the status of whether a giv can derive other givs.
6095 We need to do something special if there is or may be an update to the biv
6096 between the time the giv is defined and the time it is used to derive
6097 another giv.
6099 In addition, a giv that is only conditionally set is not allowed to
6100 derive another giv once a label has been passed.
6102 The cases we look at are when a label or an update to a biv is passed. */
6104 static void
6105 update_giv_derive (const struct loop *loop, rtx p)
6107 struct loop_ivs *ivs = LOOP_IVS (loop);
6108 struct iv_class *bl;
6109 struct induction *biv, *giv;
6110 rtx tem;
6111 int dummy;
6113 /* Search all IV classes, then all bivs, and finally all givs.
6115 There are three cases we are concerned with. First we have the situation
6116 of a giv that is only updated conditionally. In that case, it may not
6117 derive any givs after a label is passed.
6119 The second case is when a biv update occurs, or may occur, after the
6120 definition of a giv. For certain biv updates (see below) that are
6121 known to occur between the giv definition and use, we can adjust the
6122 giv definition. For others, or when the biv update is conditional,
6123 we must prevent the giv from deriving any other givs. There are two
6124 sub-cases within this case.
6126 If this is a label, we are concerned with any biv update that is done
6127 conditionally, since it may be done after the giv is defined followed by
6128 a branch here (actually, we need to pass both a jump and a label, but
6129 this extra tracking doesn't seem worth it).
6131 If this is a jump, we are concerned about any biv update that may be
6132 executed multiple times. We are actually only concerned about
6133 backward jumps, but it is probably not worth performing the test
6134 on the jump again here.
6136 If this is a biv update, we must adjust the giv status to show that a
6137 subsequent biv update was performed. If this adjustment cannot be done,
6138 the giv cannot derive further givs. */
6140 for (bl = ivs->list; bl; bl = bl->next)
6141 for (biv = bl->biv; biv; biv = biv->next_iv)
6142 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
6143 || biv->insn == p)
6145 /* Skip if location is the same as a previous one. */
6146 if (biv->same)
6147 continue;
6149 for (giv = bl->giv; giv; giv = giv->next_iv)
6151 /* If cant_derive is already true, there is no point in
6152 checking all of these conditions again. */
6153 if (giv->cant_derive)
6154 continue;
6156 /* If this giv is conditionally set and we have passed a label,
6157 it cannot derive anything. */
6158 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
6159 giv->cant_derive = 1;
6161 /* Skip givs that have mult_val == 0, since
6162 they are really invariants. Also skip those that are
6163 replaceable, since we know their lifetime doesn't contain
6164 any biv update. */
6165 else if (giv->mult_val == const0_rtx || giv->replaceable)
6166 continue;
6168 /* The only way we can allow this giv to derive another
6169 is if this is a biv increment and we can form the product
6170 of biv->add_val and giv->mult_val. In this case, we will
6171 be able to compute a compensation. */
6172 else if (biv->insn == p)
6174 rtx ext_val_dummy;
6176 tem = 0;
6177 if (biv->mult_val == const1_rtx)
6178 tem = simplify_giv_expr (loop,
6179 gen_rtx_MULT (giv->mode,
6180 biv->add_val,
6181 giv->mult_val),
6182 &ext_val_dummy, &dummy);
6184 if (tem && giv->derive_adjustment)
6185 tem = simplify_giv_expr
6186 (loop,
6187 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
6188 &ext_val_dummy, &dummy);
6190 if (tem)
6191 giv->derive_adjustment = tem;
6192 else
6193 giv->cant_derive = 1;
6195 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
6196 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
6197 giv->cant_derive = 1;
6202 /* Check whether an insn is an increment legitimate for a basic induction var.
6203 X is the source of insn P, or a part of it.
6204 MODE is the mode in which X should be interpreted.
6206 DEST_REG is the putative biv, also the destination of the insn.
6207 We accept patterns of these forms:
6208 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6209 REG = INVARIANT + REG
6211 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6212 store the additive term into *INC_VAL, and store the place where
6213 we found the additive term into *LOCATION.
6215 If X is an assignment of an invariant into DEST_REG, we set
6216 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6218 We also want to detect a BIV when it corresponds to a variable
6219 whose mode was promoted. In that case, an increment
6220 of the variable may be a PLUS that adds a SUBREG of that variable to
6221 an invariant and then sign- or zero-extends the result of the PLUS
6222 into the variable.
6224 Most GIVs in such cases will be in the promoted mode, since that is the
6225 probably the natural computation mode (and almost certainly the mode
6226 used for addresses) on the machine. So we view the pseudo-reg containing
6227 the variable as the BIV, as if it were simply incremented.
6229 Note that treating the entire pseudo as a BIV will result in making
6230 simple increments to any GIVs based on it. However, if the variable
6231 overflows in its declared mode but not its promoted mode, the result will
6232 be incorrect. This is acceptable if the variable is signed, since
6233 overflows in such cases are undefined, but not if it is unsigned, since
6234 those overflows are defined. So we only check for SIGN_EXTEND and
6235 not ZERO_EXTEND.
6237 If we cannot find a biv, we return 0. */
6239 static int
6240 basic_induction_var (const struct loop *loop, rtx x, enum machine_mode mode,
6241 rtx dest_reg, rtx p, rtx *inc_val, rtx *mult_val,
6242 rtx **location)
6244 enum rtx_code code;
6245 rtx *argp, arg;
6246 rtx insn, set = 0, last, inc;
6248 code = GET_CODE (x);
6249 *location = NULL;
6250 switch (code)
6252 case PLUS:
6253 if (rtx_equal_p (XEXP (x, 0), dest_reg)
6254 || (GET_CODE (XEXP (x, 0)) == SUBREG
6255 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
6256 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
6258 argp = &XEXP (x, 1);
6260 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
6261 || (GET_CODE (XEXP (x, 1)) == SUBREG
6262 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
6263 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
6265 argp = &XEXP (x, 0);
6267 else
6268 return 0;
6270 arg = *argp;
6271 if (loop_invariant_p (loop, arg) != 1)
6272 return 0;
6274 /* convert_modes can emit new instructions, e.g. when arg is a loop
6275 invariant MEM and dest_reg has a different mode.
6276 These instructions would be emitted after the end of the function
6277 and then *inc_val would be an uninitialized pseudo.
6278 Detect this and bail in this case.
6279 Other alternatives to solve this can be introducing a convert_modes
6280 variant which is allowed to fail but not allowed to emit new
6281 instructions, emit these instructions before loop start and let
6282 it be garbage collected if *inc_val is never used or saving the
6283 *inc_val initialization sequence generated here and when *inc_val
6284 is going to be actually used, emit it at some suitable place. */
6285 last = get_last_insn ();
6286 inc = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
6287 if (get_last_insn () != last)
6289 delete_insns_since (last);
6290 return 0;
6293 *inc_val = inc;
6294 *mult_val = const1_rtx;
6295 *location = argp;
6296 return 1;
6298 case SUBREG:
6299 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
6300 handle addition of promoted variables.
6301 ??? The comment at the start of this function is wrong: promoted
6302 variable increments don't look like it says they do. */
6303 return basic_induction_var (loop, SUBREG_REG (x),
6304 GET_MODE (SUBREG_REG (x)),
6305 dest_reg, p, inc_val, mult_val, location);
6307 case REG:
6308 /* If this register is assigned in a previous insn, look at its
6309 source, but don't go outside the loop or past a label. */
6311 /* If this sets a register to itself, we would repeat any previous
6312 biv increment if we applied this strategy blindly. */
6313 if (rtx_equal_p (dest_reg, x))
6314 return 0;
6316 insn = p;
6317 while (1)
6319 rtx dest;
6322 insn = PREV_INSN (insn);
6324 while (insn && GET_CODE (insn) == NOTE
6325 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6327 if (!insn)
6328 break;
6329 set = single_set (insn);
6330 if (set == 0)
6331 break;
6332 dest = SET_DEST (set);
6333 if (dest == x
6334 || (GET_CODE (dest) == SUBREG
6335 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
6336 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
6337 && SUBREG_REG (dest) == x))
6338 return basic_induction_var (loop, SET_SRC (set),
6339 (GET_MODE (SET_SRC (set)) == VOIDmode
6340 ? GET_MODE (x)
6341 : GET_MODE (SET_SRC (set))),
6342 dest_reg, insn,
6343 inc_val, mult_val, location);
6345 while (GET_CODE (dest) == SIGN_EXTRACT
6346 || GET_CODE (dest) == ZERO_EXTRACT
6347 || GET_CODE (dest) == SUBREG
6348 || GET_CODE (dest) == STRICT_LOW_PART)
6349 dest = XEXP (dest, 0);
6350 if (dest == x)
6351 break;
6353 /* Fall through. */
6355 /* Can accept constant setting of biv only when inside inner most loop.
6356 Otherwise, a biv of an inner loop may be incorrectly recognized
6357 as a biv of the outer loop,
6358 causing code to be moved INTO the inner loop. */
6359 case MEM:
6360 if (loop_invariant_p (loop, x) != 1)
6361 return 0;
6362 case CONST_INT:
6363 case SYMBOL_REF:
6364 case CONST:
6365 /* convert_modes aborts if we try to convert to or from CCmode, so just
6366 exclude that case. It is very unlikely that a condition code value
6367 would be a useful iterator anyways. convert_modes aborts if we try to
6368 convert a float mode to non-float or vice versa too. */
6369 if (loop->level == 1
6370 && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
6371 && GET_MODE_CLASS (mode) != MODE_CC)
6373 /* Possible bug here? Perhaps we don't know the mode of X. */
6374 last = get_last_insn ();
6375 inc = convert_modes (GET_MODE (dest_reg), mode, x, 0);
6376 if (get_last_insn () != last)
6378 delete_insns_since (last);
6379 return 0;
6382 *inc_val = inc;
6383 *mult_val = const0_rtx;
6384 return 1;
6386 else
6387 return 0;
6389 case SIGN_EXTEND:
6390 /* Ignore this BIV if signed arithmetic overflow is defined. */
6391 if (flag_wrapv)
6392 return 0;
6393 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6394 dest_reg, p, inc_val, mult_val, location);
6396 case ASHIFTRT:
6397 /* Similar, since this can be a sign extension. */
6398 for (insn = PREV_INSN (p);
6399 (insn && GET_CODE (insn) == NOTE
6400 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6401 insn = PREV_INSN (insn))
6404 if (insn)
6405 set = single_set (insn);
6407 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
6408 && set && SET_DEST (set) == XEXP (x, 0)
6409 && GET_CODE (XEXP (x, 1)) == CONST_INT
6410 && INTVAL (XEXP (x, 1)) >= 0
6411 && GET_CODE (SET_SRC (set)) == ASHIFT
6412 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
6413 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
6414 GET_MODE (XEXP (x, 0)),
6415 dest_reg, insn, inc_val, mult_val,
6416 location);
6417 return 0;
6419 default:
6420 return 0;
6424 /* A general induction variable (giv) is any quantity that is a linear
6425 function of a basic induction variable,
6426 i.e. giv = biv * mult_val + add_val.
6427 The coefficients can be any loop invariant quantity.
6428 A giv need not be computed directly from the biv;
6429 it can be computed by way of other givs. */
6431 /* Determine whether X computes a giv.
6432 If it does, return a nonzero value
6433 which is the benefit from eliminating the computation of X;
6434 set *SRC_REG to the register of the biv that it is computed from;
6435 set *ADD_VAL and *MULT_VAL to the coefficients,
6436 such that the value of X is biv * mult + add; */
6438 static int
6439 general_induction_var (const struct loop *loop, rtx x, rtx *src_reg,
6440 rtx *add_val, rtx *mult_val, rtx *ext_val,
6441 int is_addr, int *pbenefit,
6442 enum machine_mode addr_mode)
6444 struct loop_ivs *ivs = LOOP_IVS (loop);
6445 rtx orig_x = x;
6447 /* If this is an invariant, forget it, it isn't a giv. */
6448 if (loop_invariant_p (loop, x) == 1)
6449 return 0;
6451 *pbenefit = 0;
6452 *ext_val = NULL_RTX;
6453 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
6454 if (x == 0)
6455 return 0;
6457 switch (GET_CODE (x))
6459 case USE:
6460 case CONST_INT:
6461 /* Since this is now an invariant and wasn't before, it must be a giv
6462 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6463 with. */
6464 *src_reg = ivs->list->biv->dest_reg;
6465 *mult_val = const0_rtx;
6466 *add_val = x;
6467 break;
6469 case REG:
6470 /* This is equivalent to a BIV. */
6471 *src_reg = x;
6472 *mult_val = const1_rtx;
6473 *add_val = const0_rtx;
6474 break;
6476 case PLUS:
6477 /* Either (plus (biv) (invar)) or
6478 (plus (mult (biv) (invar_1)) (invar_2)). */
6479 if (GET_CODE (XEXP (x, 0)) == MULT)
6481 *src_reg = XEXP (XEXP (x, 0), 0);
6482 *mult_val = XEXP (XEXP (x, 0), 1);
6484 else
6486 *src_reg = XEXP (x, 0);
6487 *mult_val = const1_rtx;
6489 *add_val = XEXP (x, 1);
6490 break;
6492 case MULT:
6493 /* ADD_VAL is zero. */
6494 *src_reg = XEXP (x, 0);
6495 *mult_val = XEXP (x, 1);
6496 *add_val = const0_rtx;
6497 break;
6499 default:
6500 abort ();
6503 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6504 unless they are CONST_INT). */
6505 if (GET_CODE (*add_val) == USE)
6506 *add_val = XEXP (*add_val, 0);
6507 if (GET_CODE (*mult_val) == USE)
6508 *mult_val = XEXP (*mult_val, 0);
6510 if (is_addr)
6511 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
6512 else
6513 *pbenefit += rtx_cost (orig_x, SET);
6515 /* Always return true if this is a giv so it will be detected as such,
6516 even if the benefit is zero or negative. This allows elimination
6517 of bivs that might otherwise not be eliminated. */
6518 return 1;
6521 /* Given an expression, X, try to form it as a linear function of a biv.
6522 We will canonicalize it to be of the form
6523 (plus (mult (BIV) (invar_1))
6524 (invar_2))
6525 with possible degeneracies.
6527 The invariant expressions must each be of a form that can be used as a
6528 machine operand. We surround then with a USE rtx (a hack, but localized
6529 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6530 routine; it is the caller's responsibility to strip them.
6532 If no such canonicalization is possible (i.e., two biv's are used or an
6533 expression that is neither invariant nor a biv or giv), this routine
6534 returns 0.
6536 For a nonzero return, the result will have a code of CONST_INT, USE,
6537 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6539 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6541 static rtx sge_plus (enum machine_mode, rtx, rtx);
6542 static rtx sge_plus_constant (rtx, rtx);
6544 static rtx
6545 simplify_giv_expr (const struct loop *loop, rtx x, rtx *ext_val, int *benefit)
6547 struct loop_ivs *ivs = LOOP_IVS (loop);
6548 struct loop_regs *regs = LOOP_REGS (loop);
6549 enum machine_mode mode = GET_MODE (x);
6550 rtx arg0, arg1;
6551 rtx tem;
6553 /* If this is not an integer mode, or if we cannot do arithmetic in this
6554 mode, this can't be a giv. */
6555 if (mode != VOIDmode
6556 && (GET_MODE_CLASS (mode) != MODE_INT
6557 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6558 return NULL_RTX;
6560 switch (GET_CODE (x))
6562 case PLUS:
6563 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6564 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6565 if (arg0 == 0 || arg1 == 0)
6566 return NULL_RTX;
6568 /* Put constant last, CONST_INT last if both constant. */
6569 if ((GET_CODE (arg0) == USE
6570 || GET_CODE (arg0) == CONST_INT)
6571 && ! ((GET_CODE (arg0) == USE
6572 && GET_CODE (arg1) == USE)
6573 || GET_CODE (arg1) == CONST_INT))
6574 tem = arg0, arg0 = arg1, arg1 = tem;
6576 /* Handle addition of zero, then addition of an invariant. */
6577 if (arg1 == const0_rtx)
6578 return arg0;
6579 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6580 switch (GET_CODE (arg0))
6582 case CONST_INT:
6583 case USE:
6584 /* Adding two invariants must result in an invariant, so enclose
6585 addition operation inside a USE and return it. */
6586 if (GET_CODE (arg0) == USE)
6587 arg0 = XEXP (arg0, 0);
6588 if (GET_CODE (arg1) == USE)
6589 arg1 = XEXP (arg1, 0);
6591 if (GET_CODE (arg0) == CONST_INT)
6592 tem = arg0, arg0 = arg1, arg1 = tem;
6593 if (GET_CODE (arg1) == CONST_INT)
6594 tem = sge_plus_constant (arg0, arg1);
6595 else
6596 tem = sge_plus (mode, arg0, arg1);
6598 if (GET_CODE (tem) != CONST_INT)
6599 tem = gen_rtx_USE (mode, tem);
6600 return tem;
6602 case REG:
6603 case MULT:
6604 /* biv + invar or mult + invar. Return sum. */
6605 return gen_rtx_PLUS (mode, arg0, arg1);
6607 case PLUS:
6608 /* (a + invar_1) + invar_2. Associate. */
6609 return
6610 simplify_giv_expr (loop,
6611 gen_rtx_PLUS (mode,
6612 XEXP (arg0, 0),
6613 gen_rtx_PLUS (mode,
6614 XEXP (arg0, 1),
6615 arg1)),
6616 ext_val, benefit);
6618 default:
6619 abort ();
6622 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6623 MULT to reduce cases. */
6624 if (GET_CODE (arg0) == REG)
6625 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6626 if (GET_CODE (arg1) == REG)
6627 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6629 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6630 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6631 Recurse to associate the second PLUS. */
6632 if (GET_CODE (arg1) == MULT)
6633 tem = arg0, arg0 = arg1, arg1 = tem;
6635 if (GET_CODE (arg1) == PLUS)
6636 return
6637 simplify_giv_expr (loop,
6638 gen_rtx_PLUS (mode,
6639 gen_rtx_PLUS (mode, arg0,
6640 XEXP (arg1, 0)),
6641 XEXP (arg1, 1)),
6642 ext_val, benefit);
6644 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6645 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6646 return NULL_RTX;
6648 if (!rtx_equal_p (arg0, arg1))
6649 return NULL_RTX;
6651 return simplify_giv_expr (loop,
6652 gen_rtx_MULT (mode,
6653 XEXP (arg0, 0),
6654 gen_rtx_PLUS (mode,
6655 XEXP (arg0, 1),
6656 XEXP (arg1, 1))),
6657 ext_val, benefit);
6659 case MINUS:
6660 /* Handle "a - b" as "a + b * (-1)". */
6661 return simplify_giv_expr (loop,
6662 gen_rtx_PLUS (mode,
6663 XEXP (x, 0),
6664 gen_rtx_MULT (mode,
6665 XEXP (x, 1),
6666 constm1_rtx)),
6667 ext_val, benefit);
6669 case MULT:
6670 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6671 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6672 if (arg0 == 0 || arg1 == 0)
6673 return NULL_RTX;
6675 /* Put constant last, CONST_INT last if both constant. */
6676 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6677 && GET_CODE (arg1) != CONST_INT)
6678 tem = arg0, arg0 = arg1, arg1 = tem;
6680 /* If second argument is not now constant, not giv. */
6681 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6682 return NULL_RTX;
6684 /* Handle multiply by 0 or 1. */
6685 if (arg1 == const0_rtx)
6686 return const0_rtx;
6688 else if (arg1 == const1_rtx)
6689 return arg0;
6691 switch (GET_CODE (arg0))
6693 case REG:
6694 /* biv * invar. Done. */
6695 return gen_rtx_MULT (mode, arg0, arg1);
6697 case CONST_INT:
6698 /* Product of two constants. */
6699 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6701 case USE:
6702 /* invar * invar is a giv, but attempt to simplify it somehow. */
6703 if (GET_CODE (arg1) != CONST_INT)
6704 return NULL_RTX;
6706 arg0 = XEXP (arg0, 0);
6707 if (GET_CODE (arg0) == MULT)
6709 /* (invar_0 * invar_1) * invar_2. Associate. */
6710 return simplify_giv_expr (loop,
6711 gen_rtx_MULT (mode,
6712 XEXP (arg0, 0),
6713 gen_rtx_MULT (mode,
6714 XEXP (arg0,
6716 arg1)),
6717 ext_val, benefit);
6719 /* Propagate the MULT expressions to the innermost nodes. */
6720 else if (GET_CODE (arg0) == PLUS)
6722 /* (invar_0 + invar_1) * invar_2. Distribute. */
6723 return simplify_giv_expr (loop,
6724 gen_rtx_PLUS (mode,
6725 gen_rtx_MULT (mode,
6726 XEXP (arg0,
6728 arg1),
6729 gen_rtx_MULT (mode,
6730 XEXP (arg0,
6732 arg1)),
6733 ext_val, benefit);
6735 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
6737 case MULT:
6738 /* (a * invar_1) * invar_2. Associate. */
6739 return simplify_giv_expr (loop,
6740 gen_rtx_MULT (mode,
6741 XEXP (arg0, 0),
6742 gen_rtx_MULT (mode,
6743 XEXP (arg0, 1),
6744 arg1)),
6745 ext_val, benefit);
6747 case PLUS:
6748 /* (a + invar_1) * invar_2. Distribute. */
6749 return simplify_giv_expr (loop,
6750 gen_rtx_PLUS (mode,
6751 gen_rtx_MULT (mode,
6752 XEXP (arg0, 0),
6753 arg1),
6754 gen_rtx_MULT (mode,
6755 XEXP (arg0, 1),
6756 arg1)),
6757 ext_val, benefit);
6759 default:
6760 abort ();
6763 case ASHIFT:
6764 /* Shift by constant is multiply by power of two. */
6765 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6766 return 0;
6768 return
6769 simplify_giv_expr (loop,
6770 gen_rtx_MULT (mode,
6771 XEXP (x, 0),
6772 GEN_INT ((HOST_WIDE_INT) 1
6773 << INTVAL (XEXP (x, 1)))),
6774 ext_val, benefit);
6776 case NEG:
6777 /* "-a" is "a * (-1)" */
6778 return simplify_giv_expr (loop,
6779 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6780 ext_val, benefit);
6782 case NOT:
6783 /* "~a" is "-a - 1". Silly, but easy. */
6784 return simplify_giv_expr (loop,
6785 gen_rtx_MINUS (mode,
6786 gen_rtx_NEG (mode, XEXP (x, 0)),
6787 const1_rtx),
6788 ext_val, benefit);
6790 case USE:
6791 /* Already in proper form for invariant. */
6792 return x;
6794 case SIGN_EXTEND:
6795 case ZERO_EXTEND:
6796 case TRUNCATE:
6797 /* Conditionally recognize extensions of simple IVs. After we've
6798 computed loop traversal counts and verified the range of the
6799 source IV, we'll reevaluate this as a GIV. */
6800 if (*ext_val == NULL_RTX)
6802 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6803 if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
6805 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
6806 return arg0;
6809 goto do_default;
6811 case REG:
6812 /* If this is a new register, we can't deal with it. */
6813 if (REGNO (x) >= max_reg_before_loop)
6814 return 0;
6816 /* Check for biv or giv. */
6817 switch (REG_IV_TYPE (ivs, REGNO (x)))
6819 case BASIC_INDUCT:
6820 return x;
6821 case GENERAL_INDUCT:
6823 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
6825 /* Form expression from giv and add benefit. Ensure this giv
6826 can derive another and subtract any needed adjustment if so. */
6828 /* Increasing the benefit here is risky. The only case in which it
6829 is arguably correct is if this is the only use of V. In other
6830 cases, this will artificially inflate the benefit of the current
6831 giv, and lead to suboptimal code. Thus, it is disabled, since
6832 potentially not reducing an only marginally beneficial giv is
6833 less harmful than reducing many givs that are not really
6834 beneficial. */
6836 rtx single_use = regs->array[REGNO (x)].single_usage;
6837 if (single_use && single_use != const0_rtx)
6838 *benefit += v->benefit;
6841 if (v->cant_derive)
6842 return 0;
6844 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
6845 v->src_reg, v->mult_val),
6846 v->add_val);
6848 if (v->derive_adjustment)
6849 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6850 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
6851 if (*ext_val)
6853 if (!v->ext_dependent)
6854 return arg0;
6856 else
6858 *ext_val = v->ext_dependent;
6859 return arg0;
6861 return 0;
6864 default:
6865 do_default:
6866 /* If it isn't an induction variable, and it is invariant, we
6867 may be able to simplify things further by looking through
6868 the bits we just moved outside the loop. */
6869 if (loop_invariant_p (loop, x) == 1)
6871 struct movable *m;
6872 struct loop_movables *movables = LOOP_MOVABLES (loop);
6874 for (m = movables->head; m; m = m->next)
6875 if (rtx_equal_p (x, m->set_dest))
6877 /* Ok, we found a match. Substitute and simplify. */
6879 /* If we match another movable, we must use that, as
6880 this one is going away. */
6881 if (m->match)
6882 return simplify_giv_expr (loop, m->match->set_dest,
6883 ext_val, benefit);
6885 /* If consec is nonzero, this is a member of a group of
6886 instructions that were moved together. We handle this
6887 case only to the point of seeking to the last insn and
6888 looking for a REG_EQUAL. Fail if we don't find one. */
6889 if (m->consec != 0)
6891 int i = m->consec;
6892 tem = m->insn;
6895 tem = NEXT_INSN (tem);
6897 while (--i > 0);
6899 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6900 if (tem)
6901 tem = XEXP (tem, 0);
6903 else
6905 tem = single_set (m->insn);
6906 if (tem)
6907 tem = SET_SRC (tem);
6910 if (tem)
6912 /* What we are most interested in is pointer
6913 arithmetic on invariants -- only take
6914 patterns we may be able to do something with. */
6915 if (GET_CODE (tem) == PLUS
6916 || GET_CODE (tem) == MULT
6917 || GET_CODE (tem) == ASHIFT
6918 || GET_CODE (tem) == CONST_INT
6919 || GET_CODE (tem) == SYMBOL_REF)
6921 tem = simplify_giv_expr (loop, tem, ext_val,
6922 benefit);
6923 if (tem)
6924 return tem;
6926 else if (GET_CODE (tem) == CONST
6927 && GET_CODE (XEXP (tem, 0)) == PLUS
6928 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6929 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6931 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6932 ext_val, benefit);
6933 if (tem)
6934 return tem;
6937 break;
6940 break;
6943 /* Fall through to general case. */
6944 default:
6945 /* If invariant, return as USE (unless CONST_INT).
6946 Otherwise, not giv. */
6947 if (GET_CODE (x) == USE)
6948 x = XEXP (x, 0);
6950 if (loop_invariant_p (loop, x) == 1)
6952 if (GET_CODE (x) == CONST_INT)
6953 return x;
6954 if (GET_CODE (x) == CONST
6955 && GET_CODE (XEXP (x, 0)) == PLUS
6956 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6957 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6958 x = XEXP (x, 0);
6959 return gen_rtx_USE (mode, x);
6961 else
6962 return 0;
6966 /* This routine folds invariants such that there is only ever one
6967 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6969 static rtx
6970 sge_plus_constant (rtx x, rtx c)
6972 if (GET_CODE (x) == CONST_INT)
6973 return GEN_INT (INTVAL (x) + INTVAL (c));
6974 else if (GET_CODE (x) != PLUS)
6975 return gen_rtx_PLUS (GET_MODE (x), x, c);
6976 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6978 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6979 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6981 else if (GET_CODE (XEXP (x, 0)) == PLUS
6982 || GET_CODE (XEXP (x, 1)) != PLUS)
6984 return gen_rtx_PLUS (GET_MODE (x),
6985 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6987 else
6989 return gen_rtx_PLUS (GET_MODE (x),
6990 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6994 static rtx
6995 sge_plus (enum machine_mode mode, rtx x, rtx y)
6997 while (GET_CODE (y) == PLUS)
6999 rtx a = XEXP (y, 0);
7000 if (GET_CODE (a) == CONST_INT)
7001 x = sge_plus_constant (x, a);
7002 else
7003 x = gen_rtx_PLUS (mode, x, a);
7004 y = XEXP (y, 1);
7006 if (GET_CODE (y) == CONST_INT)
7007 x = sge_plus_constant (x, y);
7008 else
7009 x = gen_rtx_PLUS (mode, x, y);
7010 return x;
7013 /* Help detect a giv that is calculated by several consecutive insns;
7014 for example,
7015 giv = biv * M
7016 giv = giv + A
7017 The caller has already identified the first insn P as having a giv as dest;
7018 we check that all other insns that set the same register follow
7019 immediately after P, that they alter nothing else,
7020 and that the result of the last is still a giv.
7022 The value is 0 if the reg set in P is not really a giv.
7023 Otherwise, the value is the amount gained by eliminating
7024 all the consecutive insns that compute the value.
7026 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
7027 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
7029 The coefficients of the ultimate giv value are stored in
7030 *MULT_VAL and *ADD_VAL. */
7032 static int
7033 consec_sets_giv (const struct loop *loop, int first_benefit, rtx p,
7034 rtx src_reg, rtx dest_reg, rtx *add_val, rtx *mult_val,
7035 rtx *ext_val, rtx *last_consec_insn)
7037 struct loop_ivs *ivs = LOOP_IVS (loop);
7038 struct loop_regs *regs = LOOP_REGS (loop);
7039 int count;
7040 enum rtx_code code;
7041 int benefit;
7042 rtx temp;
7043 rtx set;
7045 /* Indicate that this is a giv so that we can update the value produced in
7046 each insn of the multi-insn sequence.
7048 This induction structure will be used only by the call to
7049 general_induction_var below, so we can allocate it on our stack.
7050 If this is a giv, our caller will replace the induct var entry with
7051 a new induction structure. */
7052 struct induction *v;
7054 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
7055 return 0;
7057 v = alloca (sizeof (struct induction));
7058 v->src_reg = src_reg;
7059 v->mult_val = *mult_val;
7060 v->add_val = *add_val;
7061 v->benefit = first_benefit;
7062 v->cant_derive = 0;
7063 v->derive_adjustment = 0;
7064 v->ext_dependent = NULL_RTX;
7066 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
7067 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
7069 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
7071 while (count > 0)
7073 p = NEXT_INSN (p);
7074 code = GET_CODE (p);
7076 /* If libcall, skip to end of call sequence. */
7077 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
7078 p = XEXP (temp, 0);
7080 if (code == INSN
7081 && (set = single_set (p))
7082 && GET_CODE (SET_DEST (set)) == REG
7083 && SET_DEST (set) == dest_reg
7084 && (general_induction_var (loop, SET_SRC (set), &src_reg,
7085 add_val, mult_val, ext_val, 0,
7086 &benefit, VOIDmode)
7087 /* Giv created by equivalent expression. */
7088 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
7089 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
7090 add_val, mult_val, ext_val, 0,
7091 &benefit, VOIDmode)))
7092 && src_reg == v->src_reg)
7094 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
7095 benefit += libcall_benefit (p);
7097 count--;
7098 v->mult_val = *mult_val;
7099 v->add_val = *add_val;
7100 v->benefit += benefit;
7102 else if (code != NOTE)
7104 /* Allow insns that set something other than this giv to a
7105 constant. Such insns are needed on machines which cannot
7106 include long constants and should not disqualify a giv. */
7107 if (code == INSN
7108 && (set = single_set (p))
7109 && SET_DEST (set) != dest_reg
7110 && CONSTANT_P (SET_SRC (set)))
7111 continue;
7113 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7114 return 0;
7118 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7119 *last_consec_insn = p;
7120 return v->benefit;
7123 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7124 represented by G1. If no such expression can be found, or it is clear that
7125 it cannot possibly be a valid address, 0 is returned.
7127 To perform the computation, we note that
7128 G1 = x * v + a and
7129 G2 = y * v + b
7130 where `v' is the biv.
7132 So G2 = (y/b) * G1 + (b - a*y/x).
7134 Note that MULT = y/x.
7136 Update: A and B are now allowed to be additive expressions such that
7137 B contains all variables in A. That is, computing B-A will not require
7138 subtracting variables. */
7140 static rtx
7141 express_from_1 (rtx a, rtx b, rtx mult)
7143 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
7145 if (mult == const0_rtx)
7146 return b;
7148 /* If MULT is not 1, we cannot handle A with non-constants, since we
7149 would then be required to subtract multiples of the registers in A.
7150 This is theoretically possible, and may even apply to some Fortran
7151 constructs, but it is a lot of work and we do not attempt it here. */
7153 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
7154 return NULL_RTX;
7156 /* In general these structures are sorted top to bottom (down the PLUS
7157 chain), but not left to right across the PLUS. If B is a higher
7158 order giv than A, we can strip one level and recurse. If A is higher
7159 order, we'll eventually bail out, but won't know that until the end.
7160 If they are the same, we'll strip one level around this loop. */
7162 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
7164 rtx ra, rb, oa, ob, tmp;
7166 ra = XEXP (a, 0), oa = XEXP (a, 1);
7167 if (GET_CODE (ra) == PLUS)
7168 tmp = ra, ra = oa, oa = tmp;
7170 rb = XEXP (b, 0), ob = XEXP (b, 1);
7171 if (GET_CODE (rb) == PLUS)
7172 tmp = rb, rb = ob, ob = tmp;
7174 if (rtx_equal_p (ra, rb))
7175 /* We matched: remove one reg completely. */
7176 a = oa, b = ob;
7177 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
7178 /* An alternate match. */
7179 a = oa, b = rb;
7180 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
7181 /* An alternate match. */
7182 a = ra, b = ob;
7183 else
7185 /* Indicates an extra register in B. Strip one level from B and
7186 recurse, hoping B was the higher order expression. */
7187 ob = express_from_1 (a, ob, mult);
7188 if (ob == NULL_RTX)
7189 return NULL_RTX;
7190 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
7194 /* Here we are at the last level of A, go through the cases hoping to
7195 get rid of everything but a constant. */
7197 if (GET_CODE (a) == PLUS)
7199 rtx ra, oa;
7201 ra = XEXP (a, 0), oa = XEXP (a, 1);
7202 if (rtx_equal_p (oa, b))
7203 oa = ra;
7204 else if (!rtx_equal_p (ra, b))
7205 return NULL_RTX;
7207 if (GET_CODE (oa) != CONST_INT)
7208 return NULL_RTX;
7210 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
7212 else if (GET_CODE (a) == CONST_INT)
7214 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
7216 else if (CONSTANT_P (a))
7218 enum machine_mode mode_a = GET_MODE (a);
7219 enum machine_mode mode_b = GET_MODE (b);
7220 enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
7221 return simplify_gen_binary (MINUS, mode, b, a);
7223 else if (GET_CODE (b) == PLUS)
7225 if (rtx_equal_p (a, XEXP (b, 0)))
7226 return XEXP (b, 1);
7227 else if (rtx_equal_p (a, XEXP (b, 1)))
7228 return XEXP (b, 0);
7229 else
7230 return NULL_RTX;
7232 else if (rtx_equal_p (a, b))
7233 return const0_rtx;
7235 return NULL_RTX;
7239 express_from (struct induction *g1, struct induction *g2)
7241 rtx mult, add;
7243 /* The value that G1 will be multiplied by must be a constant integer. Also,
7244 the only chance we have of getting a valid address is if b*c/a (see above
7245 for notation) is also an integer. */
7246 if (GET_CODE (g1->mult_val) == CONST_INT
7247 && GET_CODE (g2->mult_val) == CONST_INT)
7249 if (g1->mult_val == const0_rtx
7250 || (g1->mult_val == constm1_rtx
7251 && INTVAL (g2->mult_val)
7252 == (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1))
7253 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
7254 return NULL_RTX;
7255 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
7257 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
7258 mult = const1_rtx;
7259 else
7261 /* ??? Find out if the one is a multiple of the other? */
7262 return NULL_RTX;
7265 add = express_from_1 (g1->add_val, g2->add_val, mult);
7266 if (add == NULL_RTX)
7268 /* Failed. If we've got a multiplication factor between G1 and G2,
7269 scale G1's addend and try again. */
7270 if (INTVAL (mult) > 1)
7272 rtx g1_add_val = g1->add_val;
7273 if (GET_CODE (g1_add_val) == MULT
7274 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
7276 HOST_WIDE_INT m;
7277 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
7278 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
7279 XEXP (g1_add_val, 0), GEN_INT (m));
7281 else
7283 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
7284 mult);
7287 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
7290 if (add == NULL_RTX)
7291 return NULL_RTX;
7293 /* Form simplified final result. */
7294 if (mult == const0_rtx)
7295 return add;
7296 else if (mult == const1_rtx)
7297 mult = g1->dest_reg;
7298 else
7299 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
7301 if (add == const0_rtx)
7302 return mult;
7303 else
7305 if (GET_CODE (add) == PLUS
7306 && CONSTANT_P (XEXP (add, 1)))
7308 rtx tem = XEXP (add, 1);
7309 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
7310 add = tem;
7313 return gen_rtx_PLUS (g2->mode, mult, add);
7317 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7318 represented by G1. This indicates that G2 should be combined with G1 and
7319 that G2 can use (either directly or via an address expression) a register
7320 used to represent G1. */
7322 static rtx
7323 combine_givs_p (struct induction *g1, struct induction *g2)
7325 rtx comb, ret;
7327 /* With the introduction of ext dependent givs, we must care for modes.
7328 G2 must not use a wider mode than G1. */
7329 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
7330 return NULL_RTX;
7332 ret = comb = express_from (g1, g2);
7333 if (comb == NULL_RTX)
7334 return NULL_RTX;
7335 if (g1->mode != g2->mode)
7336 ret = gen_lowpart (g2->mode, comb);
7338 /* If these givs are identical, they can be combined. We use the results
7339 of express_from because the addends are not in a canonical form, so
7340 rtx_equal_p is a weaker test. */
7341 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7342 combination to be the other way round. */
7343 if (comb == g1->dest_reg
7344 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
7346 return ret;
7349 /* If G2 can be expressed as a function of G1 and that function is valid
7350 as an address and no more expensive than using a register for G2,
7351 the expression of G2 in terms of G1 can be used. */
7352 if (ret != NULL_RTX
7353 && g2->giv_type == DEST_ADDR
7354 && memory_address_p (GET_MODE (g2->mem), ret))
7355 return ret;
7357 return NULL_RTX;
7360 /* Check each extension dependent giv in this class to see if its
7361 root biv is safe from wrapping in the interior mode, which would
7362 make the giv illegal. */
7364 static void
7365 check_ext_dependent_givs (const struct loop *loop, struct iv_class *bl)
7367 struct loop_info *loop_info = LOOP_INFO (loop);
7368 int ze_ok = 0, se_ok = 0, info_ok = 0;
7369 enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
7370 HOST_WIDE_INT start_val;
7371 unsigned HOST_WIDE_INT u_end_val = 0;
7372 unsigned HOST_WIDE_INT u_start_val = 0;
7373 rtx incr = pc_rtx;
7374 struct induction *v;
7376 /* Make sure the iteration data is available. We must have
7377 constants in order to be certain of no overflow. */
7378 if (loop_info->n_iterations > 0
7379 && bl->initial_value
7380 && GET_CODE (bl->initial_value) == CONST_INT
7381 && (incr = biv_total_increment (bl))
7382 && GET_CODE (incr) == CONST_INT
7383 /* Make sure the host can represent the arithmetic. */
7384 && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
7386 unsigned HOST_WIDE_INT abs_incr, total_incr;
7387 HOST_WIDE_INT s_end_val;
7388 int neg_incr;
7390 info_ok = 1;
7391 start_val = INTVAL (bl->initial_value);
7392 u_start_val = start_val;
7394 neg_incr = 0, abs_incr = INTVAL (incr);
7395 if (INTVAL (incr) < 0)
7396 neg_incr = 1, abs_incr = -abs_incr;
7397 total_incr = abs_incr * loop_info->n_iterations;
7399 /* Check for host arithmetic overflow. */
7400 if (total_incr / loop_info->n_iterations == abs_incr)
7402 unsigned HOST_WIDE_INT u_max;
7403 HOST_WIDE_INT s_max;
7405 u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
7406 s_end_val = u_end_val;
7407 u_max = GET_MODE_MASK (biv_mode);
7408 s_max = u_max >> 1;
7410 /* Check zero extension of biv ok. */
7411 if (start_val >= 0
7412 /* Check for host arithmetic overflow. */
7413 && (neg_incr
7414 ? u_end_val < u_start_val
7415 : u_end_val > u_start_val)
7416 /* Check for target arithmetic overflow. */
7417 && (neg_incr
7418 ? 1 /* taken care of with host overflow */
7419 : u_end_val <= u_max))
7421 ze_ok = 1;
7424 /* Check sign extension of biv ok. */
7425 /* ??? While it is true that overflow with signed and pointer
7426 arithmetic is undefined, I fear too many programmers don't
7427 keep this fact in mind -- myself included on occasion.
7428 So leave alone with the signed overflow optimizations. */
7429 if (start_val >= -s_max - 1
7430 /* Check for host arithmetic overflow. */
7431 && (neg_incr
7432 ? s_end_val < start_val
7433 : s_end_val > start_val)
7434 /* Check for target arithmetic overflow. */
7435 && (neg_incr
7436 ? s_end_val >= -s_max - 1
7437 : s_end_val <= s_max))
7439 se_ok = 1;
7444 /* If we know the BIV is compared at run-time against an
7445 invariant value, and the increment is +/- 1, we may also
7446 be able to prove that the BIV cannot overflow. */
7447 else if (bl->biv->src_reg == loop_info->iteration_var
7448 && loop_info->comparison_value
7449 && loop_invariant_p (loop, loop_info->comparison_value)
7450 && (incr = biv_total_increment (bl))
7451 && GET_CODE (incr) == CONST_INT)
7453 /* If the increment is +1, and the exit test is a <,
7454 the BIV cannot overflow. (For <=, we have the
7455 problematic case that the comparison value might
7456 be the maximum value of the range.) */
7457 if (INTVAL (incr) == 1)
7459 if (loop_info->comparison_code == LT)
7460 se_ok = ze_ok = 1;
7461 else if (loop_info->comparison_code == LTU)
7462 ze_ok = 1;
7465 /* Likewise for increment -1 and exit test >. */
7466 if (INTVAL (incr) == -1)
7468 if (loop_info->comparison_code == GT)
7469 se_ok = ze_ok = 1;
7470 else if (loop_info->comparison_code == GTU)
7471 ze_ok = 1;
7475 /* Invalidate givs that fail the tests. */
7476 for (v = bl->giv; v; v = v->next_iv)
7477 if (v->ext_dependent)
7479 enum rtx_code code = GET_CODE (v->ext_dependent);
7480 int ok = 0;
7482 switch (code)
7484 case SIGN_EXTEND:
7485 ok = se_ok;
7486 break;
7487 case ZERO_EXTEND:
7488 ok = ze_ok;
7489 break;
7491 case TRUNCATE:
7492 /* We don't know whether this value is being used as either
7493 signed or unsigned, so to safely truncate we must satisfy
7494 both. The initial check here verifies the BIV itself;
7495 once that is successful we may check its range wrt the
7496 derived GIV. This works only if we were able to determine
7497 constant start and end values above. */
7498 if (se_ok && ze_ok && info_ok)
7500 enum machine_mode outer_mode = GET_MODE (v->ext_dependent);
7501 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
7503 /* We know from the above that both endpoints are nonnegative,
7504 and that there is no wrapping. Verify that both endpoints
7505 are within the (signed) range of the outer mode. */
7506 if (u_start_val <= max && u_end_val <= max)
7507 ok = 1;
7509 break;
7511 default:
7512 abort ();
7515 if (ok)
7517 if (loop_dump_stream)
7519 fprintf (loop_dump_stream,
7520 "Verified ext dependent giv at %d of reg %d\n",
7521 INSN_UID (v->insn), bl->regno);
7524 else
7526 if (loop_dump_stream)
7528 const char *why;
7530 if (info_ok)
7531 why = "biv iteration values overflowed";
7532 else
7534 if (incr == pc_rtx)
7535 incr = biv_total_increment (bl);
7536 if (incr == const1_rtx)
7537 why = "biv iteration info incomplete; incr by 1";
7538 else
7539 why = "biv iteration info incomplete";
7542 fprintf (loop_dump_stream,
7543 "Failed ext dependent giv at %d, %s\n",
7544 INSN_UID (v->insn), why);
7546 v->ignore = 1;
7547 bl->all_reduced = 0;
7552 /* Generate a version of VALUE in a mode appropriate for initializing V. */
7555 extend_value_for_giv (struct induction *v, rtx value)
7557 rtx ext_dep = v->ext_dependent;
7559 if (! ext_dep)
7560 return value;
7562 /* Recall that check_ext_dependent_givs verified that the known bounds
7563 of a biv did not overflow or wrap with respect to the extension for
7564 the giv. Therefore, constants need no additional adjustment. */
7565 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
7566 return value;
7568 /* Otherwise, we must adjust the value to compensate for the
7569 differing modes of the biv and the giv. */
7570 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
7573 struct combine_givs_stats
7575 int giv_number;
7576 int total_benefit;
7579 static int
7580 cmp_combine_givs_stats (const void *xp, const void *yp)
7582 const struct combine_givs_stats * const x =
7583 (const struct combine_givs_stats *) xp;
7584 const struct combine_givs_stats * const y =
7585 (const struct combine_givs_stats *) yp;
7586 int d;
7587 d = y->total_benefit - x->total_benefit;
7588 /* Stabilize the sort. */
7589 if (!d)
7590 d = x->giv_number - y->giv_number;
7591 return d;
7594 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7595 any other. If so, point SAME to the giv combined with and set NEW_REG to
7596 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7597 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7599 static void
7600 combine_givs (struct loop_regs *regs, struct iv_class *bl)
7602 /* Additional benefit to add for being combined multiple times. */
7603 const int extra_benefit = 3;
7605 struct induction *g1, *g2, **giv_array;
7606 int i, j, k, giv_count;
7607 struct combine_givs_stats *stats;
7608 rtx *can_combine;
7610 /* Count givs, because bl->giv_count is incorrect here. */
7611 giv_count = 0;
7612 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7613 if (!g1->ignore)
7614 giv_count++;
7616 giv_array = alloca (giv_count * sizeof (struct induction *));
7617 i = 0;
7618 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7619 if (!g1->ignore)
7620 giv_array[i++] = g1;
7622 stats = xcalloc (giv_count, sizeof (*stats));
7623 can_combine = xcalloc (giv_count, giv_count * sizeof (rtx));
7625 for (i = 0; i < giv_count; i++)
7627 int this_benefit;
7628 rtx single_use;
7630 g1 = giv_array[i];
7631 stats[i].giv_number = i;
7633 /* If a DEST_REG GIV is used only once, do not allow it to combine
7634 with anything, for in doing so we will gain nothing that cannot
7635 be had by simply letting the GIV with which we would have combined
7636 to be reduced on its own. The losage shows up in particular with
7637 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7638 be seen elsewhere as well. */
7639 if (g1->giv_type == DEST_REG
7640 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
7641 && single_use != const0_rtx)
7642 continue;
7644 this_benefit = g1->benefit;
7645 /* Add an additional weight for zero addends. */
7646 if (g1->no_const_addval)
7647 this_benefit += 1;
7649 for (j = 0; j < giv_count; j++)
7651 rtx this_combine;
7653 g2 = giv_array[j];
7654 if (g1 != g2
7655 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
7657 can_combine[i * giv_count + j] = this_combine;
7658 this_benefit += g2->benefit + extra_benefit;
7661 stats[i].total_benefit = this_benefit;
7664 /* Iterate, combining until we can't. */
7665 restart:
7666 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
7668 if (loop_dump_stream)
7670 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
7671 for (k = 0; k < giv_count; k++)
7673 g1 = giv_array[stats[k].giv_number];
7674 if (!g1->combined_with && !g1->same)
7675 fprintf (loop_dump_stream, " {%d, %d}",
7676 INSN_UID (giv_array[stats[k].giv_number]->insn),
7677 stats[k].total_benefit);
7679 putc ('\n', loop_dump_stream);
7682 for (k = 0; k < giv_count; k++)
7684 int g1_add_benefit = 0;
7686 i = stats[k].giv_number;
7687 g1 = giv_array[i];
7689 /* If it has already been combined, skip. */
7690 if (g1->combined_with || g1->same)
7691 continue;
7693 for (j = 0; j < giv_count; j++)
7695 g2 = giv_array[j];
7696 if (g1 != g2 && can_combine[i * giv_count + j]
7697 /* If it has already been combined, skip. */
7698 && ! g2->same && ! g2->combined_with)
7700 int l;
7702 g2->new_reg = can_combine[i * giv_count + j];
7703 g2->same = g1;
7704 /* For destination, we now may replace by mem expression instead
7705 of register. This changes the costs considerably, so add the
7706 compensation. */
7707 if (g2->giv_type == DEST_ADDR)
7708 g2->benefit = (g2->benefit + reg_address_cost
7709 - address_cost (g2->new_reg,
7710 GET_MODE (g2->mem)));
7711 g1->combined_with++;
7712 g1->lifetime += g2->lifetime;
7714 g1_add_benefit += g2->benefit;
7716 /* ??? The new final_[bg]iv_value code does a much better job
7717 of finding replaceable giv's, and hence this code may no
7718 longer be necessary. */
7719 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7720 g1_add_benefit -= copy_cost;
7722 /* To help optimize the next set of combinations, remove
7723 this giv from the benefits of other potential mates. */
7724 for (l = 0; l < giv_count; ++l)
7726 int m = stats[l].giv_number;
7727 if (can_combine[m * giv_count + j])
7728 stats[l].total_benefit -= g2->benefit + extra_benefit;
7731 if (loop_dump_stream)
7732 fprintf (loop_dump_stream,
7733 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7734 INSN_UID (g2->insn), INSN_UID (g1->insn),
7735 g1->benefit, g1_add_benefit, g1->lifetime);
7739 /* To help optimize the next set of combinations, remove
7740 this giv from the benefits of other potential mates. */
7741 if (g1->combined_with)
7743 for (j = 0; j < giv_count; ++j)
7745 int m = stats[j].giv_number;
7746 if (can_combine[m * giv_count + i])
7747 stats[j].total_benefit -= g1->benefit + extra_benefit;
7750 g1->benefit += g1_add_benefit;
7752 /* We've finished with this giv, and everything it touched.
7753 Restart the combination so that proper weights for the
7754 rest of the givs are properly taken into account. */
7755 /* ??? Ideally we would compact the arrays at this point, so
7756 as to not cover old ground. But sanely compacting
7757 can_combine is tricky. */
7758 goto restart;
7762 /* Clean up. */
7763 free (stats);
7764 free (can_combine);
7767 /* Generate sequence for REG = B * M + A. B is the initial value of
7768 the basic induction variable, M a multiplicative constant, A an
7769 additive constant and REG the destination register. */
7771 static rtx
7772 gen_add_mult (rtx b, rtx m, rtx a, rtx reg)
7774 rtx seq;
7775 rtx result;
7777 start_sequence ();
7778 /* Use unsigned arithmetic. */
7779 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7780 if (reg != result)
7781 emit_move_insn (reg, result);
7782 seq = get_insns ();
7783 end_sequence ();
7785 return seq;
7789 /* Update registers created in insn sequence SEQ. */
7791 static void
7792 loop_regs_update (const struct loop *loop ATTRIBUTE_UNUSED, rtx seq)
7794 rtx insn;
7796 /* Update register info for alias analysis. */
7798 insn = seq;
7799 while (insn != NULL_RTX)
7801 rtx set = single_set (insn);
7803 if (set && GET_CODE (SET_DEST (set)) == REG)
7804 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7806 insn = NEXT_INSN (insn);
7811 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. B
7812 is the initial value of the basic induction variable, M a
7813 multiplicative constant, A an additive constant and REG the
7814 destination register. */
7816 void
7817 loop_iv_add_mult_emit_before (const struct loop *loop, rtx b, rtx m, rtx a,
7818 rtx reg, basic_block before_bb, rtx before_insn)
7820 rtx seq;
7822 if (! before_insn)
7824 loop_iv_add_mult_hoist (loop, b, m, a, reg);
7825 return;
7828 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7829 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7831 /* Increase the lifetime of any invariants moved further in code. */
7832 update_reg_last_use (a, before_insn);
7833 update_reg_last_use (b, before_insn);
7834 update_reg_last_use (m, before_insn);
7836 /* It is possible that the expansion created lots of new registers.
7837 Iterate over the sequence we just created and record them all. We
7838 must do this before inserting the sequence. */
7839 loop_regs_update (loop, seq);
7841 loop_insn_emit_before (loop, before_bb, before_insn, seq);
7845 /* Emit insns in loop pre-header to set REG = B * M + A. B is the
7846 initial value of the basic induction variable, M a multiplicative
7847 constant, A an additive constant and REG the destination
7848 register. */
7850 void
7851 loop_iv_add_mult_sink (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
7853 rtx seq;
7855 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7856 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7858 /* Increase the lifetime of any invariants moved further in code.
7859 ???? Is this really necessary? */
7860 update_reg_last_use (a, loop->sink);
7861 update_reg_last_use (b, loop->sink);
7862 update_reg_last_use (m, loop->sink);
7864 /* It is possible that the expansion created lots of new registers.
7865 Iterate over the sequence we just created and record them all. We
7866 must do this before inserting the sequence. */
7867 loop_regs_update (loop, seq);
7869 loop_insn_sink (loop, seq);
7873 /* Emit insns after loop to set REG = B * M + A. B is the initial
7874 value of the basic induction variable, M a multiplicative constant,
7875 A an additive constant and REG the destination register. */
7877 void
7878 loop_iv_add_mult_hoist (const struct loop *loop, rtx b, rtx m, rtx a, rtx reg)
7880 rtx seq;
7882 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7883 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7885 /* It is possible that the expansion created lots of new registers.
7886 Iterate over the sequence we just created and record them all. We
7887 must do this before inserting the sequence. */
7888 loop_regs_update (loop, seq);
7890 loop_insn_hoist (loop, seq);
7895 /* Similar to gen_add_mult, but compute cost rather than generating
7896 sequence. */
7898 static int
7899 iv_add_mult_cost (rtx b, rtx m, rtx a, rtx reg)
7901 int cost = 0;
7902 rtx last, result;
7904 start_sequence ();
7905 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7906 if (reg != result)
7907 emit_move_insn (reg, result);
7908 last = get_last_insn ();
7909 while (last)
7911 rtx t = single_set (last);
7912 if (t)
7913 cost += rtx_cost (SET_SRC (t), SET);
7914 last = PREV_INSN (last);
7916 end_sequence ();
7917 return cost;
7920 /* Test whether A * B can be computed without
7921 an actual multiply insn. Value is 1 if so.
7923 ??? This function stinks because it generates a ton of wasted RTL
7924 ??? and as a result fragments GC memory to no end. There are other
7925 ??? places in the compiler which are invoked a lot and do the same
7926 ??? thing, generate wasted RTL just to see if something is possible. */
7928 static int
7929 product_cheap_p (rtx a, rtx b)
7931 rtx tmp;
7932 int win, n_insns;
7934 /* If only one is constant, make it B. */
7935 if (GET_CODE (a) == CONST_INT)
7936 tmp = a, a = b, b = tmp;
7938 /* If first constant, both constant, so don't need multiply. */
7939 if (GET_CODE (a) == CONST_INT)
7940 return 1;
7942 /* If second not constant, neither is constant, so would need multiply. */
7943 if (GET_CODE (b) != CONST_INT)
7944 return 0;
7946 /* One operand is constant, so might not need multiply insn. Generate the
7947 code for the multiply and see if a call or multiply, or long sequence
7948 of insns is generated. */
7950 start_sequence ();
7951 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7952 tmp = get_insns ();
7953 end_sequence ();
7955 win = 1;
7956 if (INSN_P (tmp))
7958 n_insns = 0;
7959 while (tmp != NULL_RTX)
7961 rtx next = NEXT_INSN (tmp);
7963 if (++n_insns > 3
7964 || GET_CODE (tmp) != INSN
7965 || (GET_CODE (PATTERN (tmp)) == SET
7966 && GET_CODE (SET_SRC (PATTERN (tmp))) == MULT)
7967 || (GET_CODE (PATTERN (tmp)) == PARALLEL
7968 && GET_CODE (XVECEXP (PATTERN (tmp), 0, 0)) == SET
7969 && GET_CODE (SET_SRC (XVECEXP (PATTERN (tmp), 0, 0))) == MULT))
7971 win = 0;
7972 break;
7975 tmp = next;
7978 else if (GET_CODE (tmp) == SET
7979 && GET_CODE (SET_SRC (tmp)) == MULT)
7980 win = 0;
7981 else if (GET_CODE (tmp) == PARALLEL
7982 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7983 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7984 win = 0;
7986 return win;
7989 /* Check to see if loop can be terminated by a "decrement and branch until
7990 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7991 Also try reversing an increment loop to a decrement loop
7992 to see if the optimization can be performed.
7993 Value is nonzero if optimization was performed. */
7995 /* This is useful even if the architecture doesn't have such an insn,
7996 because it might change a loops which increments from 0 to n to a loop
7997 which decrements from n to 0. A loop that decrements to zero is usually
7998 faster than one that increments from zero. */
8000 /* ??? This could be rewritten to use some of the loop unrolling procedures,
8001 such as approx_final_value, biv_total_increment, loop_iterations, and
8002 final_[bg]iv_value. */
8004 static int
8005 check_dbra_loop (struct loop *loop, int insn_count)
8007 struct loop_info *loop_info = LOOP_INFO (loop);
8008 struct loop_regs *regs = LOOP_REGS (loop);
8009 struct loop_ivs *ivs = LOOP_IVS (loop);
8010 struct iv_class *bl;
8011 rtx reg;
8012 rtx jump_label;
8013 rtx final_value;
8014 rtx start_value;
8015 rtx new_add_val;
8016 rtx comparison;
8017 rtx before_comparison;
8018 rtx p;
8019 rtx jump;
8020 rtx first_compare;
8021 int compare_and_branch;
8022 rtx loop_start = loop->start;
8023 rtx loop_end = loop->end;
8025 /* If last insn is a conditional branch, and the insn before tests a
8026 register value, try to optimize it. Otherwise, we can't do anything. */
8028 jump = PREV_INSN (loop_end);
8029 comparison = get_condition_for_loop (loop, jump);
8030 if (comparison == 0)
8031 return 0;
8032 if (!onlyjump_p (jump))
8033 return 0;
8035 /* Try to compute whether the compare/branch at the loop end is one or
8036 two instructions. */
8037 get_condition (jump, &first_compare, false);
8038 if (first_compare == jump)
8039 compare_and_branch = 1;
8040 else if (first_compare == prev_nonnote_insn (jump))
8041 compare_and_branch = 2;
8042 else
8043 return 0;
8046 /* If more than one condition is present to control the loop, then
8047 do not proceed, as this function does not know how to rewrite
8048 loop tests with more than one condition.
8050 Look backwards from the first insn in the last comparison
8051 sequence and see if we've got another comparison sequence. */
8053 rtx jump1;
8054 if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
8055 if (GET_CODE (jump1) == JUMP_INSN)
8056 return 0;
8059 /* Check all of the bivs to see if the compare uses one of them.
8060 Skip biv's set more than once because we can't guarantee that
8061 it will be zero on the last iteration. Also skip if the biv is
8062 used between its update and the test insn. */
8064 for (bl = ivs->list; bl; bl = bl->next)
8066 if (bl->biv_count == 1
8067 && ! bl->biv->maybe_multiple
8068 && bl->biv->dest_reg == XEXP (comparison, 0)
8069 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
8070 first_compare))
8071 break;
8074 /* Try swapping the comparison to identify a suitable biv. */
8075 if (!bl)
8076 for (bl = ivs->list; bl; bl = bl->next)
8077 if (bl->biv_count == 1
8078 && ! bl->biv->maybe_multiple
8079 && bl->biv->dest_reg == XEXP (comparison, 1)
8080 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
8081 first_compare))
8083 comparison = gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)),
8084 VOIDmode,
8085 XEXP (comparison, 1),
8086 XEXP (comparison, 0));
8087 break;
8090 if (! bl)
8091 return 0;
8093 /* Look for the case where the basic induction variable is always
8094 nonnegative, and equals zero on the last iteration.
8095 In this case, add a reg_note REG_NONNEG, which allows the
8096 m68k DBRA instruction to be used. */
8098 if (((GET_CODE (comparison) == GT && XEXP (comparison, 1) == constm1_rtx)
8099 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
8100 && GET_CODE (bl->biv->add_val) == CONST_INT
8101 && INTVAL (bl->biv->add_val) < 0)
8103 /* Initial value must be greater than 0,
8104 init_val % -dec_value == 0 to ensure that it equals zero on
8105 the last iteration */
8107 if (GET_CODE (bl->initial_value) == CONST_INT
8108 && INTVAL (bl->initial_value) > 0
8109 && (INTVAL (bl->initial_value)
8110 % (-INTVAL (bl->biv->add_val))) == 0)
8112 /* Register always nonnegative, add REG_NOTE to branch. */
8113 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8114 REG_NOTES (jump)
8115 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8116 REG_NOTES (jump));
8117 bl->nonneg = 1;
8119 return 1;
8122 /* If the decrement is 1 and the value was tested as >= 0 before
8123 the loop, then we can safely optimize. */
8124 for (p = loop_start; p; p = PREV_INSN (p))
8126 if (GET_CODE (p) == CODE_LABEL)
8127 break;
8128 if (GET_CODE (p) != JUMP_INSN)
8129 continue;
8131 before_comparison = get_condition_for_loop (loop, p);
8132 if (before_comparison
8133 && XEXP (before_comparison, 0) == bl->biv->dest_reg
8134 && (GET_CODE (before_comparison) == LT
8135 || GET_CODE (before_comparison) == LTU)
8136 && XEXP (before_comparison, 1) == const0_rtx
8137 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
8138 && INTVAL (bl->biv->add_val) == -1)
8140 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8141 REG_NOTES (jump)
8142 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8143 REG_NOTES (jump));
8144 bl->nonneg = 1;
8146 return 1;
8150 else if (GET_CODE (bl->biv->add_val) == CONST_INT
8151 && INTVAL (bl->biv->add_val) > 0)
8153 /* Try to change inc to dec, so can apply above optimization. */
8154 /* Can do this if:
8155 all registers modified are induction variables or invariant,
8156 all memory references have non-overlapping addresses
8157 (obviously true if only one write)
8158 allow 2 insns for the compare/jump at the end of the loop. */
8159 /* Also, we must avoid any instructions which use both the reversed
8160 biv and another biv. Such instructions will fail if the loop is
8161 reversed. We meet this condition by requiring that either
8162 no_use_except_counting is true, or else that there is only
8163 one biv. */
8164 int num_nonfixed_reads = 0;
8165 /* 1 if the iteration var is used only to count iterations. */
8166 int no_use_except_counting = 0;
8167 /* 1 if the loop has no memory store, or it has a single memory store
8168 which is reversible. */
8169 int reversible_mem_store = 1;
8171 if (bl->giv_count == 0
8172 && !loop->exit_count
8173 && !loop_info->has_multiple_exit_targets)
8175 rtx bivreg = regno_reg_rtx[bl->regno];
8176 struct iv_class *blt;
8178 /* If there are no givs for this biv, and the only exit is the
8179 fall through at the end of the loop, then
8180 see if perhaps there are no uses except to count. */
8181 no_use_except_counting = 1;
8182 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8183 if (INSN_P (p))
8185 rtx set = single_set (p);
8187 if (set && GET_CODE (SET_DEST (set)) == REG
8188 && REGNO (SET_DEST (set)) == bl->regno)
8189 /* An insn that sets the biv is okay. */
8191 else if (!reg_mentioned_p (bivreg, PATTERN (p)))
8192 /* An insn that doesn't mention the biv is okay. */
8194 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
8195 || p == prev_nonnote_insn (loop_end))
8197 /* If either of these insns uses the biv and sets a pseudo
8198 that has more than one usage, then the biv has uses
8199 other than counting since it's used to derive a value
8200 that is used more than one time. */
8201 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
8202 regs);
8203 if (regs->multiple_uses)
8205 no_use_except_counting = 0;
8206 break;
8209 else
8211 no_use_except_counting = 0;
8212 break;
8216 /* A biv has uses besides counting if it is used to set
8217 another biv. */
8218 for (blt = ivs->list; blt; blt = blt->next)
8219 if (blt->init_set
8220 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
8222 no_use_except_counting = 0;
8223 break;
8227 if (no_use_except_counting)
8228 /* No need to worry about MEMs. */
8230 else if (loop_info->num_mem_sets <= 1)
8232 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8233 if (INSN_P (p))
8234 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
8236 /* If the loop has a single store, and the destination address is
8237 invariant, then we can't reverse the loop, because this address
8238 might then have the wrong value at loop exit.
8239 This would work if the source was invariant also, however, in that
8240 case, the insn should have been moved out of the loop. */
8242 if (loop_info->num_mem_sets == 1)
8244 struct induction *v;
8246 /* If we could prove that each of the memory locations
8247 written to was different, then we could reverse the
8248 store -- but we don't presently have any way of
8249 knowing that. */
8250 reversible_mem_store = 0;
8252 /* If the store depends on a register that is set after the
8253 store, it depends on the initial value, and is thus not
8254 reversible. */
8255 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
8257 if (v->giv_type == DEST_REG
8258 && reg_mentioned_p (v->dest_reg,
8259 PATTERN (loop_info->first_loop_store_insn))
8260 && loop_insn_first_p (loop_info->first_loop_store_insn,
8261 v->insn))
8262 reversible_mem_store = 0;
8266 else
8267 return 0;
8269 /* This code only acts for innermost loops. Also it simplifies
8270 the memory address check by only reversing loops with
8271 zero or one memory access.
8272 Two memory accesses could involve parts of the same array,
8273 and that can't be reversed.
8274 If the biv is used only for counting, than we don't need to worry
8275 about all these things. */
8277 if ((num_nonfixed_reads <= 1
8278 && ! loop_info->has_nonconst_call
8279 && ! loop_info->has_prefetch
8280 && ! loop_info->has_volatile
8281 && reversible_mem_store
8282 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
8283 + num_unmoved_movables (loop) + compare_and_branch == insn_count)
8284 && (bl == ivs->list && bl->next == 0))
8285 || (no_use_except_counting && ! loop_info->has_prefetch))
8287 rtx tem;
8289 /* Loop can be reversed. */
8290 if (loop_dump_stream)
8291 fprintf (loop_dump_stream, "Can reverse loop\n");
8293 /* Now check other conditions:
8295 The increment must be a constant, as must the initial value,
8296 and the comparison code must be LT.
8298 This test can probably be improved since +/- 1 in the constant
8299 can be obtained by changing LT to LE and vice versa; this is
8300 confusing. */
8302 if (comparison
8303 /* for constants, LE gets turned into LT */
8304 && (GET_CODE (comparison) == LT
8305 || (GET_CODE (comparison) == LE
8306 && no_use_except_counting)
8307 || GET_CODE (comparison) == LTU))
8309 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
8310 rtx initial_value, comparison_value;
8311 int nonneg = 0;
8312 enum rtx_code cmp_code;
8313 int comparison_const_width;
8314 unsigned HOST_WIDE_INT comparison_sign_mask;
8316 add_val = INTVAL (bl->biv->add_val);
8317 comparison_value = XEXP (comparison, 1);
8318 if (GET_MODE (comparison_value) == VOIDmode)
8319 comparison_const_width
8320 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
8321 else
8322 comparison_const_width
8323 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
8324 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
8325 comparison_const_width = HOST_BITS_PER_WIDE_INT;
8326 comparison_sign_mask
8327 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
8329 /* If the comparison value is not a loop invariant, then we
8330 can not reverse this loop.
8332 ??? If the insns which initialize the comparison value as
8333 a whole compute an invariant result, then we could move
8334 them out of the loop and proceed with loop reversal. */
8335 if (! loop_invariant_p (loop, comparison_value))
8336 return 0;
8338 if (GET_CODE (comparison_value) == CONST_INT)
8339 comparison_val = INTVAL (comparison_value);
8340 initial_value = bl->initial_value;
8342 /* Normalize the initial value if it is an integer and
8343 has no other use except as a counter. This will allow
8344 a few more loops to be reversed. */
8345 if (no_use_except_counting
8346 && GET_CODE (comparison_value) == CONST_INT
8347 && GET_CODE (initial_value) == CONST_INT)
8349 comparison_val = comparison_val - INTVAL (bl->initial_value);
8350 /* The code below requires comparison_val to be a multiple
8351 of add_val in order to do the loop reversal, so
8352 round up comparison_val to a multiple of add_val.
8353 Since comparison_value is constant, we know that the
8354 current comparison code is LT. */
8355 comparison_val = comparison_val + add_val - 1;
8356 comparison_val
8357 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
8358 /* We postpone overflow checks for COMPARISON_VAL here;
8359 even if there is an overflow, we might still be able to
8360 reverse the loop, if converting the loop exit test to
8361 NE is possible. */
8362 initial_value = const0_rtx;
8365 /* First check if we can do a vanilla loop reversal. */
8366 if (initial_value == const0_rtx
8367 /* If we have a decrement_and_branch_on_count,
8368 prefer the NE test, since this will allow that
8369 instruction to be generated. Note that we must
8370 use a vanilla loop reversal if the biv is used to
8371 calculate a giv or has a non-counting use. */
8372 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8373 && defined (HAVE_decrement_and_branch_on_count)
8374 && (! (add_val == 1 && loop->vtop
8375 && (bl->biv_count == 0
8376 || no_use_except_counting)))
8377 #endif
8378 && GET_CODE (comparison_value) == CONST_INT
8379 /* Now do postponed overflow checks on COMPARISON_VAL. */
8380 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
8381 & comparison_sign_mask))
8383 /* Register will always be nonnegative, with value
8384 0 on last iteration */
8385 add_adjust = add_val;
8386 nonneg = 1;
8387 cmp_code = GE;
8389 else if (add_val == 1 && loop->vtop
8390 && (bl->biv_count == 0
8391 || no_use_except_counting))
8393 add_adjust = 0;
8394 cmp_code = NE;
8396 else
8397 return 0;
8399 if (GET_CODE (comparison) == LE)
8400 add_adjust -= add_val;
8402 /* If the initial value is not zero, or if the comparison
8403 value is not an exact multiple of the increment, then we
8404 can not reverse this loop. */
8405 if (initial_value == const0_rtx
8406 && GET_CODE (comparison_value) == CONST_INT)
8408 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
8409 return 0;
8411 else
8413 if (! no_use_except_counting || add_val != 1)
8414 return 0;
8417 final_value = comparison_value;
8419 /* Reset these in case we normalized the initial value
8420 and comparison value above. */
8421 if (GET_CODE (comparison_value) == CONST_INT
8422 && GET_CODE (initial_value) == CONST_INT)
8424 comparison_value = GEN_INT (comparison_val);
8425 final_value
8426 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
8428 bl->initial_value = initial_value;
8430 /* Save some info needed to produce the new insns. */
8431 reg = bl->biv->dest_reg;
8432 jump_label = condjump_label (PREV_INSN (loop_end));
8433 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
8435 /* Set start_value; if this is not a CONST_INT, we need
8436 to generate a SUB.
8437 Initialize biv to start_value before loop start.
8438 The old initializing insn will be deleted as a
8439 dead store by flow.c. */
8440 if (initial_value == const0_rtx
8441 && GET_CODE (comparison_value) == CONST_INT)
8443 start_value = GEN_INT (comparison_val - add_adjust);
8444 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
8446 else if (GET_CODE (initial_value) == CONST_INT)
8448 enum machine_mode mode = GET_MODE (reg);
8449 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8450 rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
8452 if (add_insn == 0)
8453 return 0;
8455 start_value
8456 = gen_rtx_PLUS (mode, comparison_value, offset);
8457 loop_insn_hoist (loop, add_insn);
8458 if (GET_CODE (comparison) == LE)
8459 final_value = gen_rtx_PLUS (mode, comparison_value,
8460 GEN_INT (add_val));
8462 else if (! add_adjust)
8464 enum machine_mode mode = GET_MODE (reg);
8465 rtx sub_insn = gen_sub3_insn (reg, comparison_value,
8466 initial_value);
8468 if (sub_insn == 0)
8469 return 0;
8470 start_value
8471 = gen_rtx_MINUS (mode, comparison_value, initial_value);
8472 loop_insn_hoist (loop, sub_insn);
8474 else
8475 /* We could handle the other cases too, but it'll be
8476 better to have a testcase first. */
8477 return 0;
8479 /* We may not have a single insn which can increment a reg, so
8480 create a sequence to hold all the insns from expand_inc. */
8481 start_sequence ();
8482 expand_inc (reg, new_add_val);
8483 tem = get_insns ();
8484 end_sequence ();
8486 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
8487 delete_insn (bl->biv->insn);
8489 /* Update biv info to reflect its new status. */
8490 bl->biv->insn = p;
8491 bl->initial_value = start_value;
8492 bl->biv->add_val = new_add_val;
8494 /* Update loop info. */
8495 loop_info->initial_value = reg;
8496 loop_info->initial_equiv_value = reg;
8497 loop_info->final_value = const0_rtx;
8498 loop_info->final_equiv_value = const0_rtx;
8499 loop_info->comparison_value = const0_rtx;
8500 loop_info->comparison_code = cmp_code;
8501 loop_info->increment = new_add_val;
8503 /* Inc LABEL_NUSES so that delete_insn will
8504 not delete the label. */
8505 LABEL_NUSES (XEXP (jump_label, 0))++;
8507 /* Emit an insn after the end of the loop to set the biv's
8508 proper exit value if it is used anywhere outside the loop. */
8509 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8510 || ! bl->init_insn
8511 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8512 loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
8514 /* Delete compare/branch at end of loop. */
8515 delete_related_insns (PREV_INSN (loop_end));
8516 if (compare_and_branch == 2)
8517 delete_related_insns (first_compare);
8519 /* Add new compare/branch insn at end of loop. */
8520 start_sequence ();
8521 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8522 GET_MODE (reg), 0,
8523 XEXP (jump_label, 0));
8524 tem = get_insns ();
8525 end_sequence ();
8526 emit_jump_insn_before (tem, loop_end);
8528 for (tem = PREV_INSN (loop_end);
8529 tem && GET_CODE (tem) != JUMP_INSN;
8530 tem = PREV_INSN (tem))
8533 if (tem)
8534 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8536 if (nonneg)
8538 if (tem)
8540 /* Increment of LABEL_NUSES done above. */
8541 /* Register is now always nonnegative,
8542 so add REG_NONNEG note to the branch. */
8543 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
8544 REG_NOTES (tem));
8546 bl->nonneg = 1;
8549 /* No insn may reference both the reversed and another biv or it
8550 will fail (see comment near the top of the loop reversal
8551 code).
8552 Earlier on, we have verified that the biv has no use except
8553 counting, or it is the only biv in this function.
8554 However, the code that computes no_use_except_counting does
8555 not verify reg notes. It's possible to have an insn that
8556 references another biv, and has a REG_EQUAL note with an
8557 expression based on the reversed biv. To avoid this case,
8558 remove all REG_EQUAL notes based on the reversed biv
8559 here. */
8560 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8561 if (INSN_P (p))
8563 rtx *pnote;
8564 rtx set = single_set (p);
8565 /* If this is a set of a GIV based on the reversed biv, any
8566 REG_EQUAL notes should still be correct. */
8567 if (! set
8568 || GET_CODE (SET_DEST (set)) != REG
8569 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
8570 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
8571 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
8572 for (pnote = &REG_NOTES (p); *pnote;)
8574 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
8575 && reg_mentioned_p (regno_reg_rtx[bl->regno],
8576 XEXP (*pnote, 0)))
8577 *pnote = XEXP (*pnote, 1);
8578 else
8579 pnote = &XEXP (*pnote, 1);
8583 /* Mark that this biv has been reversed. Each giv which depends
8584 on this biv, and which is also live past the end of the loop
8585 will have to be fixed up. */
8587 bl->reversed = 1;
8589 if (loop_dump_stream)
8591 fprintf (loop_dump_stream, "Reversed loop");
8592 if (bl->nonneg)
8593 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8594 else
8595 fprintf (loop_dump_stream, "\n");
8598 return 1;
8603 return 0;
8606 /* Verify whether the biv BL appears to be eliminable,
8607 based on the insns in the loop that refer to it.
8609 If ELIMINATE_P is nonzero, actually do the elimination.
8611 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8612 determine whether invariant insns should be placed inside or at the
8613 start of the loop. */
8615 static int
8616 maybe_eliminate_biv (const struct loop *loop, struct iv_class *bl,
8617 int eliminate_p, int threshold, int insn_count)
8619 struct loop_ivs *ivs = LOOP_IVS (loop);
8620 rtx reg = bl->biv->dest_reg;
8621 rtx p;
8623 /* Scan all insns in the loop, stopping if we find one that uses the
8624 biv in a way that we cannot eliminate. */
8626 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
8628 enum rtx_code code = GET_CODE (p);
8629 basic_block where_bb = 0;
8630 rtx where_insn = threshold >= insn_count ? 0 : p;
8631 rtx note;
8633 /* If this is a libcall that sets a giv, skip ahead to its end. */
8634 if (GET_RTX_CLASS (code) == 'i')
8636 note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8638 if (note)
8640 rtx last = XEXP (note, 0);
8641 rtx set = single_set (last);
8643 if (set && GET_CODE (SET_DEST (set)) == REG)
8645 unsigned int regno = REGNO (SET_DEST (set));
8647 if (regno < ivs->n_regs
8648 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
8649 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
8650 p = last;
8655 /* Closely examine the insn if the biv is mentioned. */
8656 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8657 && reg_mentioned_p (reg, PATTERN (p))
8658 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
8659 eliminate_p, where_bb, where_insn))
8661 if (loop_dump_stream)
8662 fprintf (loop_dump_stream,
8663 "Cannot eliminate biv %d: biv used in insn %d.\n",
8664 bl->regno, INSN_UID (p));
8665 break;
8668 /* If we are eliminating, kill REG_EQUAL notes mentioning the biv. */
8669 if (eliminate_p
8670 && (note = find_reg_note (p, REG_EQUAL, NULL_RTX)) != NULL_RTX
8671 && reg_mentioned_p (reg, XEXP (note, 0)))
8672 remove_note (p, note);
8675 if (p == loop->end)
8677 if (loop_dump_stream)
8678 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8679 bl->regno, eliminate_p ? "was" : "can be");
8680 return 1;
8683 return 0;
8686 /* INSN and REFERENCE are instructions in the same insn chain.
8687 Return nonzero if INSN is first. */
8690 loop_insn_first_p (rtx insn, rtx reference)
8692 rtx p, q;
8694 for (p = insn, q = reference;;)
8696 /* Start with test for not first so that INSN == REFERENCE yields not
8697 first. */
8698 if (q == insn || ! p)
8699 return 0;
8700 if (p == reference || ! q)
8701 return 1;
8703 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8704 previous insn, hence the <= comparison below does not work if
8705 P is a note. */
8706 if (INSN_UID (p) < max_uid_for_loop
8707 && INSN_UID (q) < max_uid_for_loop
8708 && GET_CODE (p) != NOTE)
8709 return INSN_LUID (p) <= INSN_LUID (q);
8711 if (INSN_UID (p) >= max_uid_for_loop
8712 || GET_CODE (p) == NOTE)
8713 p = NEXT_INSN (p);
8714 if (INSN_UID (q) >= max_uid_for_loop)
8715 q = NEXT_INSN (q);
8719 /* We are trying to eliminate BIV in INSN using GIV. Return nonzero if
8720 the offset that we have to take into account due to auto-increment /
8721 div derivation is zero. */
8722 static int
8723 biv_elimination_giv_has_0_offset (struct induction *biv,
8724 struct induction *giv, rtx insn)
8726 /* If the giv V had the auto-inc address optimization applied
8727 to it, and INSN occurs between the giv insn and the biv
8728 insn, then we'd have to adjust the value used here.
8729 This is rare, so we don't bother to make this possible. */
8730 if (giv->auto_inc_opt
8731 && ((loop_insn_first_p (giv->insn, insn)
8732 && loop_insn_first_p (insn, biv->insn))
8733 || (loop_insn_first_p (biv->insn, insn)
8734 && loop_insn_first_p (insn, giv->insn))))
8735 return 0;
8737 return 1;
8740 /* If BL appears in X (part of the pattern of INSN), see if we can
8741 eliminate its use. If so, return 1. If not, return 0.
8743 If BIV does not appear in X, return 1.
8745 If ELIMINATE_P is nonzero, actually do the elimination.
8746 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8747 Depending on how many items have been moved out of the loop, it
8748 will either be before INSN (when WHERE_INSN is nonzero) or at the
8749 start of the loop (when WHERE_INSN is zero). */
8751 static int
8752 maybe_eliminate_biv_1 (const struct loop *loop, rtx x, rtx insn,
8753 struct iv_class *bl, int eliminate_p,
8754 basic_block where_bb, rtx where_insn)
8756 enum rtx_code code = GET_CODE (x);
8757 rtx reg = bl->biv->dest_reg;
8758 enum machine_mode mode = GET_MODE (reg);
8759 struct induction *v;
8760 rtx arg, tem;
8761 #ifdef HAVE_cc0
8762 rtx new;
8763 #endif
8764 int arg_operand;
8765 const char *fmt;
8766 int i, j;
8768 switch (code)
8770 case REG:
8771 /* If we haven't already been able to do something with this BIV,
8772 we can't eliminate it. */
8773 if (x == reg)
8774 return 0;
8775 return 1;
8777 case SET:
8778 /* If this sets the BIV, it is not a problem. */
8779 if (SET_DEST (x) == reg)
8780 return 1;
8782 /* If this is an insn that defines a giv, it is also ok because
8783 it will go away when the giv is reduced. */
8784 for (v = bl->giv; v; v = v->next_iv)
8785 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8786 return 1;
8788 #ifdef HAVE_cc0
8789 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8791 /* Can replace with any giv that was reduced and
8792 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8793 Require a constant for MULT_VAL, so we know it's nonzero.
8794 ??? We disable this optimization to avoid potential
8795 overflows. */
8797 for (v = bl->giv; v; v = v->next_iv)
8798 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8799 && v->add_val == const0_rtx
8800 && ! v->ignore && ! v->maybe_dead && v->always_computable
8801 && v->mode == mode
8802 && 0)
8804 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8805 continue;
8807 if (! eliminate_p)
8808 return 1;
8810 /* If the giv has the opposite direction of change,
8811 then reverse the comparison. */
8812 if (INTVAL (v->mult_val) < 0)
8813 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8814 const0_rtx, v->new_reg);
8815 else
8816 new = v->new_reg;
8818 /* We can probably test that giv's reduced reg. */
8819 if (validate_change (insn, &SET_SRC (x), new, 0))
8820 return 1;
8823 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8824 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8825 Require a constant for MULT_VAL, so we know it's nonzero.
8826 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8827 overflow problem. */
8829 for (v = bl->giv; v; v = v->next_iv)
8830 if (GET_CODE (v->mult_val) == CONST_INT
8831 && v->mult_val != const0_rtx
8832 && ! v->ignore && ! v->maybe_dead && v->always_computable
8833 && v->mode == mode
8834 && (GET_CODE (v->add_val) == SYMBOL_REF
8835 || GET_CODE (v->add_val) == LABEL_REF
8836 || GET_CODE (v->add_val) == CONST
8837 || (GET_CODE (v->add_val) == REG
8838 && REG_POINTER (v->add_val))))
8840 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8841 continue;
8843 if (! eliminate_p)
8844 return 1;
8846 /* If the giv has the opposite direction of change,
8847 then reverse the comparison. */
8848 if (INTVAL (v->mult_val) < 0)
8849 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8850 v->new_reg);
8851 else
8852 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8853 copy_rtx (v->add_val));
8855 /* Replace biv with the giv's reduced register. */
8856 update_reg_last_use (v->add_val, insn);
8857 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8858 return 1;
8860 /* Insn doesn't support that constant or invariant. Copy it
8861 into a register (it will be a loop invariant.) */
8862 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8864 loop_insn_emit_before (loop, 0, where_insn,
8865 gen_move_insn (tem,
8866 copy_rtx (v->add_val)));
8868 /* Substitute the new register for its invariant value in
8869 the compare expression. */
8870 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8871 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8872 return 1;
8875 #endif
8876 break;
8878 case COMPARE:
8879 case EQ: case NE:
8880 case GT: case GE: case GTU: case GEU:
8881 case LT: case LE: case LTU: case LEU:
8882 /* See if either argument is the biv. */
8883 if (XEXP (x, 0) == reg)
8884 arg = XEXP (x, 1), arg_operand = 1;
8885 else if (XEXP (x, 1) == reg)
8886 arg = XEXP (x, 0), arg_operand = 0;
8887 else
8888 break;
8890 if (CONSTANT_P (arg))
8892 /* First try to replace with any giv that has constant positive
8893 mult_val and constant add_val. We might be able to support
8894 negative mult_val, but it seems complex to do it in general. */
8896 for (v = bl->giv; v; v = v->next_iv)
8897 if (GET_CODE (v->mult_val) == CONST_INT
8898 && INTVAL (v->mult_val) > 0
8899 && (GET_CODE (v->add_val) == SYMBOL_REF
8900 || GET_CODE (v->add_val) == LABEL_REF
8901 || GET_CODE (v->add_val) == CONST
8902 || (GET_CODE (v->add_val) == REG
8903 && REG_POINTER (v->add_val)))
8904 && ! v->ignore && ! v->maybe_dead && v->always_computable
8905 && v->mode == mode)
8907 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8908 continue;
8910 /* Don't eliminate if the linear combination that makes up
8911 the giv overflows when it is applied to ARG. */
8912 if (GET_CODE (arg) == CONST_INT)
8914 rtx add_val;
8916 if (GET_CODE (v->add_val) == CONST_INT)
8917 add_val = v->add_val;
8918 else
8919 add_val = const0_rtx;
8921 if (const_mult_add_overflow_p (arg, v->mult_val,
8922 add_val, mode, 1))
8923 continue;
8926 if (! eliminate_p)
8927 return 1;
8929 /* Replace biv with the giv's reduced reg. */
8930 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
8932 /* If all constants are actually constant integers and
8933 the derived constant can be directly placed in the COMPARE,
8934 do so. */
8935 if (GET_CODE (arg) == CONST_INT
8936 && GET_CODE (v->add_val) == CONST_INT)
8938 tem = expand_mult_add (arg, NULL_RTX, v->mult_val,
8939 v->add_val, mode, 1);
8941 else
8943 /* Otherwise, load it into a register. */
8944 tem = gen_reg_rtx (mode);
8945 loop_iv_add_mult_emit_before (loop, arg,
8946 v->mult_val, v->add_val,
8947 tem, where_bb, where_insn);
8950 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8952 if (apply_change_group ())
8953 return 1;
8956 /* Look for giv with positive constant mult_val and nonconst add_val.
8957 Insert insns to calculate new compare value.
8958 ??? Turn this off due to possible overflow. */
8960 for (v = bl->giv; v; v = v->next_iv)
8961 if (GET_CODE (v->mult_val) == CONST_INT
8962 && INTVAL (v->mult_val) > 0
8963 && ! v->ignore && ! v->maybe_dead && v->always_computable
8964 && v->mode == mode
8965 && 0)
8967 rtx tem;
8969 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8970 continue;
8972 if (! eliminate_p)
8973 return 1;
8975 tem = gen_reg_rtx (mode);
8977 /* Replace biv with giv's reduced register. */
8978 validate_change (insn, &XEXP (x, 1 - arg_operand),
8979 v->new_reg, 1);
8981 /* Compute value to compare against. */
8982 loop_iv_add_mult_emit_before (loop, arg,
8983 v->mult_val, v->add_val,
8984 tem, where_bb, where_insn);
8985 /* Use it in this insn. */
8986 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8987 if (apply_change_group ())
8988 return 1;
8991 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8993 if (loop_invariant_p (loop, arg) == 1)
8995 /* Look for giv with constant positive mult_val and nonconst
8996 add_val. Insert insns to compute new compare value.
8997 ??? Turn this off due to possible overflow. */
8999 for (v = bl->giv; v; v = v->next_iv)
9000 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
9001 && ! v->ignore && ! v->maybe_dead && v->always_computable
9002 && v->mode == mode
9003 && 0)
9005 rtx tem;
9007 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
9008 continue;
9010 if (! eliminate_p)
9011 return 1;
9013 tem = gen_reg_rtx (mode);
9015 /* Replace biv with giv's reduced register. */
9016 validate_change (insn, &XEXP (x, 1 - arg_operand),
9017 v->new_reg, 1);
9019 /* Compute value to compare against. */
9020 loop_iv_add_mult_emit_before (loop, arg,
9021 v->mult_val, v->add_val,
9022 tem, where_bb, where_insn);
9023 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
9024 if (apply_change_group ())
9025 return 1;
9029 /* This code has problems. Basically, you can't know when
9030 seeing if we will eliminate BL, whether a particular giv
9031 of ARG will be reduced. If it isn't going to be reduced,
9032 we can't eliminate BL. We can try forcing it to be reduced,
9033 but that can generate poor code.
9035 The problem is that the benefit of reducing TV, below should
9036 be increased if BL can actually be eliminated, but this means
9037 we might have to do a topological sort of the order in which
9038 we try to process biv. It doesn't seem worthwhile to do
9039 this sort of thing now. */
9041 #if 0
9042 /* Otherwise the reg compared with had better be a biv. */
9043 if (GET_CODE (arg) != REG
9044 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
9045 return 0;
9047 /* Look for a pair of givs, one for each biv,
9048 with identical coefficients. */
9049 for (v = bl->giv; v; v = v->next_iv)
9051 struct induction *tv;
9053 if (v->ignore || v->maybe_dead || v->mode != mode)
9054 continue;
9056 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
9057 tv = tv->next_iv)
9058 if (! tv->ignore && ! tv->maybe_dead
9059 && rtx_equal_p (tv->mult_val, v->mult_val)
9060 && rtx_equal_p (tv->add_val, v->add_val)
9061 && tv->mode == mode)
9063 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
9064 continue;
9066 if (! eliminate_p)
9067 return 1;
9069 /* Replace biv with its giv's reduced reg. */
9070 XEXP (x, 1 - arg_operand) = v->new_reg;
9071 /* Replace other operand with the other giv's
9072 reduced reg. */
9073 XEXP (x, arg_operand) = tv->new_reg;
9074 return 1;
9077 #endif
9080 /* If we get here, the biv can't be eliminated. */
9081 return 0;
9083 case MEM:
9084 /* If this address is a DEST_ADDR giv, it doesn't matter if the
9085 biv is used in it, since it will be replaced. */
9086 for (v = bl->giv; v; v = v->next_iv)
9087 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
9088 return 1;
9089 break;
9091 default:
9092 break;
9095 /* See if any subexpression fails elimination. */
9096 fmt = GET_RTX_FORMAT (code);
9097 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9099 switch (fmt[i])
9101 case 'e':
9102 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
9103 eliminate_p, where_bb, where_insn))
9104 return 0;
9105 break;
9107 case 'E':
9108 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9109 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
9110 eliminate_p, where_bb, where_insn))
9111 return 0;
9112 break;
9116 return 1;
9119 /* Return nonzero if the last use of REG
9120 is in an insn following INSN in the same basic block. */
9122 static int
9123 last_use_this_basic_block (rtx reg, rtx insn)
9125 rtx n;
9126 for (n = insn;
9127 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
9128 n = NEXT_INSN (n))
9130 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
9131 return 1;
9133 return 0;
9136 /* Called via `note_stores' to record the initial value of a biv. Here we
9137 just record the location of the set and process it later. */
9139 static void
9140 record_initial (rtx dest, rtx set, void *data ATTRIBUTE_UNUSED)
9142 struct loop_ivs *ivs = (struct loop_ivs *) data;
9143 struct iv_class *bl;
9145 if (GET_CODE (dest) != REG
9146 || REGNO (dest) >= ivs->n_regs
9147 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
9148 return;
9150 bl = REG_IV_CLASS (ivs, REGNO (dest));
9152 /* If this is the first set found, record it. */
9153 if (bl->init_insn == 0)
9155 bl->init_insn = note_insn;
9156 bl->init_set = set;
9160 /* If any of the registers in X are "old" and currently have a last use earlier
9161 than INSN, update them to have a last use of INSN. Their actual last use
9162 will be the previous insn but it will not have a valid uid_luid so we can't
9163 use it. X must be a source expression only. */
9165 static void
9166 update_reg_last_use (rtx x, rtx insn)
9168 /* Check for the case where INSN does not have a valid luid. In this case,
9169 there is no need to modify the regno_last_uid, as this can only happen
9170 when code is inserted after the loop_end to set a pseudo's final value,
9171 and hence this insn will never be the last use of x.
9172 ???? This comment is not correct. See for example loop_givs_reduce.
9173 This may insert an insn before another new insn. */
9174 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
9175 && INSN_UID (insn) < max_uid_for_loop
9176 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
9178 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
9180 else
9182 int i, j;
9183 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
9184 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9186 if (fmt[i] == 'e')
9187 update_reg_last_use (XEXP (x, i), insn);
9188 else if (fmt[i] == 'E')
9189 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9190 update_reg_last_use (XVECEXP (x, i, j), insn);
9195 /* Given an insn INSN and condition COND, return the condition in a
9196 canonical form to simplify testing by callers. Specifically:
9198 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9199 (2) Both operands will be machine operands; (cc0) will have been replaced.
9200 (3) If an operand is a constant, it will be the second operand.
9201 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9202 for GE, GEU, and LEU.
9204 If the condition cannot be understood, or is an inequality floating-point
9205 comparison which needs to be reversed, 0 will be returned.
9207 If REVERSE is nonzero, then reverse the condition prior to canonizing it.
9209 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9210 insn used in locating the condition was found. If a replacement test
9211 of the condition is desired, it should be placed in front of that
9212 insn and we will be sure that the inputs are still valid.
9214 If WANT_REG is nonzero, we wish the condition to be relative to that
9215 register, if possible. Therefore, do not canonicalize the condition
9216 further. If ALLOW_CC_MODE is nonzero, allow the condition returned
9217 to be a compare to a CC mode register. */
9220 canonicalize_condition (rtx insn, rtx cond, int reverse, rtx *earliest,
9221 rtx want_reg, int allow_cc_mode)
9223 enum rtx_code code;
9224 rtx prev = insn;
9225 rtx set;
9226 rtx tem;
9227 rtx op0, op1;
9228 int reverse_code = 0;
9229 enum machine_mode mode;
9231 code = GET_CODE (cond);
9232 mode = GET_MODE (cond);
9233 op0 = XEXP (cond, 0);
9234 op1 = XEXP (cond, 1);
9236 if (reverse)
9237 code = reversed_comparison_code (cond, insn);
9238 if (code == UNKNOWN)
9239 return 0;
9241 if (earliest)
9242 *earliest = insn;
9244 /* If we are comparing a register with zero, see if the register is set
9245 in the previous insn to a COMPARE or a comparison operation. Perform
9246 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9247 in cse.c */
9249 while (GET_RTX_CLASS (code) == '<'
9250 && op1 == CONST0_RTX (GET_MODE (op0))
9251 && op0 != want_reg)
9253 /* Set nonzero when we find something of interest. */
9254 rtx x = 0;
9256 #ifdef HAVE_cc0
9257 /* If comparison with cc0, import actual comparison from compare
9258 insn. */
9259 if (op0 == cc0_rtx)
9261 if ((prev = prev_nonnote_insn (prev)) == 0
9262 || GET_CODE (prev) != INSN
9263 || (set = single_set (prev)) == 0
9264 || SET_DEST (set) != cc0_rtx)
9265 return 0;
9267 op0 = SET_SRC (set);
9268 op1 = CONST0_RTX (GET_MODE (op0));
9269 if (earliest)
9270 *earliest = prev;
9272 #endif
9274 /* If this is a COMPARE, pick up the two things being compared. */
9275 if (GET_CODE (op0) == COMPARE)
9277 op1 = XEXP (op0, 1);
9278 op0 = XEXP (op0, 0);
9279 continue;
9281 else if (GET_CODE (op0) != REG)
9282 break;
9284 /* Go back to the previous insn. Stop if it is not an INSN. We also
9285 stop if it isn't a single set or if it has a REG_INC note because
9286 we don't want to bother dealing with it. */
9288 if ((prev = prev_nonnote_insn (prev)) == 0
9289 || GET_CODE (prev) != INSN
9290 || FIND_REG_INC_NOTE (prev, NULL_RTX))
9291 break;
9293 set = set_of (op0, prev);
9295 if (set
9296 && (GET_CODE (set) != SET
9297 || !rtx_equal_p (SET_DEST (set), op0)))
9298 break;
9300 /* If this is setting OP0, get what it sets it to if it looks
9301 relevant. */
9302 if (set)
9304 enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
9305 #ifdef FLOAT_STORE_FLAG_VALUE
9306 REAL_VALUE_TYPE fsfv;
9307 #endif
9309 /* ??? We may not combine comparisons done in a CCmode with
9310 comparisons not done in a CCmode. This is to aid targets
9311 like Alpha that have an IEEE compliant EQ instruction, and
9312 a non-IEEE compliant BEQ instruction. The use of CCmode is
9313 actually artificial, simply to prevent the combination, but
9314 should not affect other platforms.
9316 However, we must allow VOIDmode comparisons to match either
9317 CCmode or non-CCmode comparison, because some ports have
9318 modeless comparisons inside branch patterns.
9320 ??? This mode check should perhaps look more like the mode check
9321 in simplify_comparison in combine. */
9323 if ((GET_CODE (SET_SRC (set)) == COMPARE
9324 || (((code == NE
9325 || (code == LT
9326 && GET_MODE_CLASS (inner_mode) == MODE_INT
9327 && (GET_MODE_BITSIZE (inner_mode)
9328 <= HOST_BITS_PER_WIDE_INT)
9329 && (STORE_FLAG_VALUE
9330 & ((HOST_WIDE_INT) 1
9331 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9332 #ifdef FLOAT_STORE_FLAG_VALUE
9333 || (code == LT
9334 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9335 && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
9336 REAL_VALUE_NEGATIVE (fsfv)))
9337 #endif
9339 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
9340 && (((GET_MODE_CLASS (mode) == MODE_CC)
9341 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9342 || mode == VOIDmode || inner_mode == VOIDmode))
9343 x = SET_SRC (set);
9344 else if (((code == EQ
9345 || (code == GE
9346 && (GET_MODE_BITSIZE (inner_mode)
9347 <= HOST_BITS_PER_WIDE_INT)
9348 && GET_MODE_CLASS (inner_mode) == MODE_INT
9349 && (STORE_FLAG_VALUE
9350 & ((HOST_WIDE_INT) 1
9351 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9352 #ifdef FLOAT_STORE_FLAG_VALUE
9353 || (code == GE
9354 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9355 && (fsfv = FLOAT_STORE_FLAG_VALUE (inner_mode),
9356 REAL_VALUE_NEGATIVE (fsfv)))
9357 #endif
9359 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
9360 && (((GET_MODE_CLASS (mode) == MODE_CC)
9361 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9362 || mode == VOIDmode || inner_mode == VOIDmode))
9365 reverse_code = 1;
9366 x = SET_SRC (set);
9368 else
9369 break;
9372 else if (reg_set_p (op0, prev))
9373 /* If this sets OP0, but not directly, we have to give up. */
9374 break;
9376 if (x)
9378 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9379 code = GET_CODE (x);
9380 if (reverse_code)
9382 code = reversed_comparison_code (x, prev);
9383 if (code == UNKNOWN)
9384 return 0;
9385 reverse_code = 0;
9388 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
9389 if (earliest)
9390 *earliest = prev;
9394 /* If constant is first, put it last. */
9395 if (CONSTANT_P (op0))
9396 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
9398 /* If OP0 is the result of a comparison, we weren't able to find what
9399 was really being compared, so fail. */
9400 if (!allow_cc_mode
9401 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
9402 return 0;
9404 /* Canonicalize any ordered comparison with integers involving equality
9405 if we can do computations in the relevant mode and we do not
9406 overflow. */
9408 if (GET_MODE_CLASS (GET_MODE (op0)) != MODE_CC
9409 && GET_CODE (op1) == CONST_INT
9410 && GET_MODE (op0) != VOIDmode
9411 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
9413 HOST_WIDE_INT const_val = INTVAL (op1);
9414 unsigned HOST_WIDE_INT uconst_val = const_val;
9415 unsigned HOST_WIDE_INT max_val
9416 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
9418 switch (code)
9420 case LE:
9421 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
9422 code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
9423 break;
9425 /* When cross-compiling, const_val might be sign-extended from
9426 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9427 case GE:
9428 if ((HOST_WIDE_INT) (const_val & max_val)
9429 != (((HOST_WIDE_INT) 1
9430 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9431 code = GT, op1 = gen_int_mode (const_val - 1, GET_MODE (op0));
9432 break;
9434 case LEU:
9435 if (uconst_val < max_val)
9436 code = LTU, op1 = gen_int_mode (uconst_val + 1, GET_MODE (op0));
9437 break;
9439 case GEU:
9440 if (uconst_val != 0)
9441 code = GTU, op1 = gen_int_mode (uconst_val - 1, GET_MODE (op0));
9442 break;
9444 default:
9445 break;
9449 /* Never return CC0; return zero instead. */
9450 if (CC0_P (op0))
9451 return 0;
9453 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
9456 /* Given a jump insn JUMP, return the condition that will cause it to branch
9457 to its JUMP_LABEL. If the condition cannot be understood, or is an
9458 inequality floating-point comparison which needs to be reversed, 0 will
9459 be returned.
9461 If EARLIEST is nonzero, it is a pointer to a place where the earliest
9462 insn used in locating the condition was found. If a replacement test
9463 of the condition is desired, it should be placed in front of that
9464 insn and we will be sure that the inputs are still valid.
9466 If ALLOW_CC_MODE is nonzero, allow the condition returned to be a
9467 compare CC mode register. */
9470 get_condition (rtx jump, rtx *earliest, int allow_cc_mode)
9472 rtx cond;
9473 int reverse;
9474 rtx set;
9476 /* If this is not a standard conditional jump, we can't parse it. */
9477 if (GET_CODE (jump) != JUMP_INSN
9478 || ! any_condjump_p (jump))
9479 return 0;
9480 set = pc_set (jump);
9482 cond = XEXP (SET_SRC (set), 0);
9484 /* If this branches to JUMP_LABEL when the condition is false, reverse
9485 the condition. */
9486 reverse
9487 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
9488 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
9490 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX,
9491 allow_cc_mode);
9494 /* Similar to above routine, except that we also put an invariant last
9495 unless both operands are invariants. */
9498 get_condition_for_loop (const struct loop *loop, rtx x)
9500 rtx comparison = get_condition (x, (rtx*) 0, false);
9502 if (comparison == 0
9503 || ! loop_invariant_p (loop, XEXP (comparison, 0))
9504 || loop_invariant_p (loop, XEXP (comparison, 1)))
9505 return comparison;
9507 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
9508 XEXP (comparison, 1), XEXP (comparison, 0));
9511 /* Scan the function and determine whether it has indirect (computed) jumps.
9513 This is taken mostly from flow.c; similar code exists elsewhere
9514 in the compiler. It may be useful to put this into rtlanal.c. */
9515 static int
9516 indirect_jump_in_function_p (rtx start)
9518 rtx insn;
9520 for (insn = start; insn; insn = NEXT_INSN (insn))
9521 if (computed_jump_p (insn))
9522 return 1;
9524 return 0;
9527 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9528 documentation for LOOP_MEMS for the definition of `appropriate'.
9529 This function is called from prescan_loop via for_each_rtx. */
9531 static int
9532 insert_loop_mem (rtx *mem, void *data ATTRIBUTE_UNUSED)
9534 struct loop_info *loop_info = data;
9535 int i;
9536 rtx m = *mem;
9538 if (m == NULL_RTX)
9539 return 0;
9541 switch (GET_CODE (m))
9543 case MEM:
9544 break;
9546 case CLOBBER:
9547 /* We're not interested in MEMs that are only clobbered. */
9548 return -1;
9550 case CONST_DOUBLE:
9551 /* We're not interested in the MEM associated with a
9552 CONST_DOUBLE, so there's no need to traverse into this. */
9553 return -1;
9555 case EXPR_LIST:
9556 /* We're not interested in any MEMs that only appear in notes. */
9557 return -1;
9559 default:
9560 /* This is not a MEM. */
9561 return 0;
9564 /* See if we've already seen this MEM. */
9565 for (i = 0; i < loop_info->mems_idx; ++i)
9566 if (rtx_equal_p (m, loop_info->mems[i].mem))
9568 if (MEM_VOLATILE_P (m) && !MEM_VOLATILE_P (loop_info->mems[i].mem))
9569 loop_info->mems[i].mem = m;
9570 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
9571 /* The modes of the two memory accesses are different. If
9572 this happens, something tricky is going on, and we just
9573 don't optimize accesses to this MEM. */
9574 loop_info->mems[i].optimize = 0;
9576 return 0;
9579 /* Resize the array, if necessary. */
9580 if (loop_info->mems_idx == loop_info->mems_allocated)
9582 if (loop_info->mems_allocated != 0)
9583 loop_info->mems_allocated *= 2;
9584 else
9585 loop_info->mems_allocated = 32;
9587 loop_info->mems = xrealloc (loop_info->mems,
9588 loop_info->mems_allocated * sizeof (loop_mem_info));
9591 /* Actually insert the MEM. */
9592 loop_info->mems[loop_info->mems_idx].mem = m;
9593 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9594 because we can't put it in a register. We still store it in the
9595 table, though, so that if we see the same address later, but in a
9596 non-BLK mode, we'll not think we can optimize it at that point. */
9597 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
9598 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
9599 ++loop_info->mems_idx;
9601 return 0;
9605 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9607 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9608 register that is modified by an insn between FROM and TO. If the
9609 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9610 more, stop incrementing it, to avoid overflow.
9612 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9613 register I is used, if it is only used once. Otherwise, it is set
9614 to 0 (for no uses) or const0_rtx for more than one use. This
9615 parameter may be zero, in which case this processing is not done.
9617 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9618 optimize register I. */
9620 static void
9621 loop_regs_scan (const struct loop *loop, int extra_size)
9623 struct loop_regs *regs = LOOP_REGS (loop);
9624 int old_nregs;
9625 /* last_set[n] is nonzero iff reg n has been set in the current
9626 basic block. In that case, it is the insn that last set reg n. */
9627 rtx *last_set;
9628 rtx insn;
9629 int i;
9631 old_nregs = regs->num;
9632 regs->num = max_reg_num ();
9634 /* Grow the regs array if not allocated or too small. */
9635 if (regs->num >= regs->size)
9637 regs->size = regs->num + extra_size;
9639 regs->array = xrealloc (regs->array, regs->size * sizeof (*regs->array));
9641 /* Zero the new elements. */
9642 memset (regs->array + old_nregs, 0,
9643 (regs->size - old_nregs) * sizeof (*regs->array));
9646 /* Clear previously scanned fields but do not clear n_times_set. */
9647 for (i = 0; i < old_nregs; i++)
9649 regs->array[i].set_in_loop = 0;
9650 regs->array[i].may_not_optimize = 0;
9651 regs->array[i].single_usage = NULL_RTX;
9654 last_set = xcalloc (regs->num, sizeof (rtx));
9656 /* Scan the loop, recording register usage. */
9657 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9658 insn = NEXT_INSN (insn))
9660 if (INSN_P (insn))
9662 /* Record registers that have exactly one use. */
9663 find_single_use_in_loop (regs, insn, PATTERN (insn));
9665 /* Include uses in REG_EQUAL notes. */
9666 if (REG_NOTES (insn))
9667 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
9669 if (GET_CODE (PATTERN (insn)) == SET
9670 || GET_CODE (PATTERN (insn)) == CLOBBER)
9671 count_one_set (regs, insn, PATTERN (insn), last_set);
9672 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
9674 int i;
9675 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
9676 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
9677 last_set);
9681 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
9682 memset (last_set, 0, regs->num * sizeof (rtx));
9684 /* Invalidate all registers used for function argument passing.
9685 We check rtx_varies_p for the same reason as below, to allow
9686 optimizing PIC calculations. */
9687 if (GET_CODE (insn) == CALL_INSN)
9689 rtx link;
9690 for (link = CALL_INSN_FUNCTION_USAGE (insn);
9691 link;
9692 link = XEXP (link, 1))
9694 rtx op, reg;
9696 if (GET_CODE (op = XEXP (link, 0)) == USE
9697 && GET_CODE (reg = XEXP (op, 0)) == REG
9698 && rtx_varies_p (reg, 1))
9699 regs->array[REGNO (reg)].may_not_optimize = 1;
9704 /* Invalidate all hard registers clobbered by calls. With one exception:
9705 a call-clobbered PIC register is still function-invariant for our
9706 purposes, since we can hoist any PIC calculations out of the loop.
9707 Thus the call to rtx_varies_p. */
9708 if (LOOP_INFO (loop)->has_call)
9709 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9710 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
9711 && rtx_varies_p (regno_reg_rtx[i], 1))
9713 regs->array[i].may_not_optimize = 1;
9714 regs->array[i].set_in_loop = 1;
9717 #ifdef AVOID_CCMODE_COPIES
9718 /* Don't try to move insns which set CC registers if we should not
9719 create CCmode register copies. */
9720 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9721 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9722 regs->array[i].may_not_optimize = 1;
9723 #endif
9725 /* Set regs->array[I].n_times_set for the new registers. */
9726 for (i = old_nregs; i < regs->num; i++)
9727 regs->array[i].n_times_set = regs->array[i].set_in_loop;
9729 free (last_set);
9732 /* Returns the number of real INSNs in the LOOP. */
9734 static int
9735 count_insns_in_loop (const struct loop *loop)
9737 int count = 0;
9738 rtx insn;
9740 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9741 insn = NEXT_INSN (insn))
9742 if (INSN_P (insn))
9743 ++count;
9745 return count;
9748 /* Move MEMs into registers for the duration of the loop. */
9750 static void
9751 load_mems (const struct loop *loop)
9753 struct loop_info *loop_info = LOOP_INFO (loop);
9754 struct loop_regs *regs = LOOP_REGS (loop);
9755 int maybe_never = 0;
9756 int i;
9757 rtx p, prev_ebb_head;
9758 rtx label = NULL_RTX;
9759 rtx end_label;
9760 /* Nonzero if the next instruction may never be executed. */
9761 int next_maybe_never = 0;
9762 unsigned int last_max_reg = max_reg_num ();
9764 if (loop_info->mems_idx == 0)
9765 return;
9767 /* We cannot use next_label here because it skips over normal insns. */
9768 end_label = next_nonnote_insn (loop->end);
9769 if (end_label && GET_CODE (end_label) != CODE_LABEL)
9770 end_label = NULL_RTX;
9772 /* Check to see if it's possible that some instructions in the loop are
9773 never executed. Also check if there is a goto out of the loop other
9774 than right after the end of the loop. */
9775 for (p = next_insn_in_loop (loop, loop->scan_start);
9776 p != NULL_RTX;
9777 p = next_insn_in_loop (loop, p))
9779 if (GET_CODE (p) == CODE_LABEL)
9780 maybe_never = 1;
9781 else if (GET_CODE (p) == JUMP_INSN
9782 /* If we enter the loop in the middle, and scan
9783 around to the beginning, don't set maybe_never
9784 for that. This must be an unconditional jump,
9785 otherwise the code at the top of the loop might
9786 never be executed. Unconditional jumps are
9787 followed a by barrier then loop end. */
9788 && ! (GET_CODE (p) == JUMP_INSN
9789 && JUMP_LABEL (p) == loop->top
9790 && NEXT_INSN (NEXT_INSN (p)) == loop->end
9791 && any_uncondjump_p (p)))
9793 /* If this is a jump outside of the loop but not right
9794 after the end of the loop, we would have to emit new fixup
9795 sequences for each such label. */
9796 if (/* If we can't tell where control might go when this
9797 JUMP_INSN is executed, we must be conservative. */
9798 !JUMP_LABEL (p)
9799 || (JUMP_LABEL (p) != end_label
9800 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
9801 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
9802 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
9803 return;
9805 if (!any_condjump_p (p))
9806 /* Something complicated. */
9807 maybe_never = 1;
9808 else
9809 /* If there are any more instructions in the loop, they
9810 might not be reached. */
9811 next_maybe_never = 1;
9813 else if (next_maybe_never)
9814 maybe_never = 1;
9817 /* Find start of the extended basic block that enters the loop. */
9818 for (p = loop->start;
9819 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
9820 p = PREV_INSN (p))
9822 prev_ebb_head = p;
9824 cselib_init ();
9826 /* Build table of mems that get set to constant values before the
9827 loop. */
9828 for (; p != loop->start; p = NEXT_INSN (p))
9829 cselib_process_insn (p);
9831 /* Actually move the MEMs. */
9832 for (i = 0; i < loop_info->mems_idx; ++i)
9834 regset_head load_copies;
9835 regset_head store_copies;
9836 int written = 0;
9837 rtx reg;
9838 rtx mem = loop_info->mems[i].mem;
9839 rtx mem_list_entry;
9841 if (MEM_VOLATILE_P (mem)
9842 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
9843 /* There's no telling whether or not MEM is modified. */
9844 loop_info->mems[i].optimize = 0;
9846 /* Go through the MEMs written to in the loop to see if this
9847 one is aliased by one of them. */
9848 mem_list_entry = loop_info->store_mems;
9849 while (mem_list_entry)
9851 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9852 written = 1;
9853 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9854 mem, rtx_varies_p))
9856 /* MEM is indeed aliased by this store. */
9857 loop_info->mems[i].optimize = 0;
9858 break;
9860 mem_list_entry = XEXP (mem_list_entry, 1);
9863 if (flag_float_store && written
9864 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
9865 loop_info->mems[i].optimize = 0;
9867 /* If this MEM is written to, we must be sure that there
9868 are no reads from another MEM that aliases this one. */
9869 if (loop_info->mems[i].optimize && written)
9871 int j;
9873 for (j = 0; j < loop_info->mems_idx; ++j)
9875 if (j == i)
9876 continue;
9877 else if (true_dependence (mem,
9878 VOIDmode,
9879 loop_info->mems[j].mem,
9880 rtx_varies_p))
9882 /* It's not safe to hoist loop_info->mems[i] out of
9883 the loop because writes to it might not be
9884 seen by reads from loop_info->mems[j]. */
9885 loop_info->mems[i].optimize = 0;
9886 break;
9891 if (maybe_never && may_trap_p (mem))
9892 /* We can't access the MEM outside the loop; it might
9893 cause a trap that wouldn't have happened otherwise. */
9894 loop_info->mems[i].optimize = 0;
9896 if (!loop_info->mems[i].optimize)
9897 /* We thought we were going to lift this MEM out of the
9898 loop, but later discovered that we could not. */
9899 continue;
9901 INIT_REG_SET (&load_copies);
9902 INIT_REG_SET (&store_copies);
9904 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9905 order to keep scan_loop from moving stores to this MEM
9906 out of the loop just because this REG is neither a
9907 user-variable nor used in the loop test. */
9908 reg = gen_reg_rtx (GET_MODE (mem));
9909 REG_USERVAR_P (reg) = 1;
9910 loop_info->mems[i].reg = reg;
9912 /* Now, replace all references to the MEM with the
9913 corresponding pseudos. */
9914 maybe_never = 0;
9915 for (p = next_insn_in_loop (loop, loop->scan_start);
9916 p != NULL_RTX;
9917 p = next_insn_in_loop (loop, p))
9919 if (INSN_P (p))
9921 rtx set;
9923 set = single_set (p);
9925 /* See if this copies the mem into a register that isn't
9926 modified afterwards. We'll try to do copy propagation
9927 a little further on. */
9928 if (set
9929 /* @@@ This test is _way_ too conservative. */
9930 && ! maybe_never
9931 && GET_CODE (SET_DEST (set)) == REG
9932 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
9933 && REGNO (SET_DEST (set)) < last_max_reg
9934 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
9935 && rtx_equal_p (SET_SRC (set), mem))
9936 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
9938 /* See if this copies the mem from a register that isn't
9939 modified afterwards. We'll try to remove the
9940 redundant copy later on by doing a little register
9941 renaming and copy propagation. This will help
9942 to untangle things for the BIV detection code. */
9943 if (set
9944 && ! maybe_never
9945 && GET_CODE (SET_SRC (set)) == REG
9946 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
9947 && REGNO (SET_SRC (set)) < last_max_reg
9948 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
9949 && rtx_equal_p (SET_DEST (set), mem))
9950 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
9952 /* If this is a call which uses / clobbers this memory
9953 location, we must not change the interface here. */
9954 if (GET_CODE (p) == CALL_INSN
9955 && reg_mentioned_p (loop_info->mems[i].mem,
9956 CALL_INSN_FUNCTION_USAGE (p)))
9958 cancel_changes (0);
9959 loop_info->mems[i].optimize = 0;
9960 break;
9962 else
9963 /* Replace the memory reference with the shadow register. */
9964 replace_loop_mems (p, loop_info->mems[i].mem,
9965 loop_info->mems[i].reg, written);
9968 if (GET_CODE (p) == CODE_LABEL
9969 || GET_CODE (p) == JUMP_INSN)
9970 maybe_never = 1;
9973 if (! loop_info->mems[i].optimize)
9974 ; /* We found we couldn't do the replacement, so do nothing. */
9975 else if (! apply_change_group ())
9976 /* We couldn't replace all occurrences of the MEM. */
9977 loop_info->mems[i].optimize = 0;
9978 else
9980 /* Load the memory immediately before LOOP->START, which is
9981 the NOTE_LOOP_BEG. */
9982 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9983 rtx set;
9984 rtx best = mem;
9985 int j;
9986 struct elt_loc_list *const_equiv = 0;
9988 if (e)
9990 struct elt_loc_list *equiv;
9991 struct elt_loc_list *best_equiv = 0;
9992 for (equiv = e->locs; equiv; equiv = equiv->next)
9994 if (CONSTANT_P (equiv->loc))
9995 const_equiv = equiv;
9996 else if (GET_CODE (equiv->loc) == REG
9997 /* Extending hard register lifetimes causes crash
9998 on SRC targets. Doing so on non-SRC is
9999 probably also not good idea, since we most
10000 probably have pseudoregister equivalence as
10001 well. */
10002 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
10003 best_equiv = equiv;
10005 /* Use the constant equivalence if that is cheap enough. */
10006 if (! best_equiv)
10007 best_equiv = const_equiv;
10008 else if (const_equiv
10009 && (rtx_cost (const_equiv->loc, SET)
10010 <= rtx_cost (best_equiv->loc, SET)))
10012 best_equiv = const_equiv;
10013 const_equiv = 0;
10016 /* If best_equiv is nonzero, we know that MEM is set to a
10017 constant or register before the loop. We will use this
10018 knowledge to initialize the shadow register with that
10019 constant or reg rather than by loading from MEM. */
10020 if (best_equiv)
10021 best = copy_rtx (best_equiv->loc);
10024 set = gen_move_insn (reg, best);
10025 set = loop_insn_hoist (loop, set);
10026 if (REG_P (best))
10028 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
10029 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
10031 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
10032 break;
10036 if (const_equiv)
10037 set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
10039 if (written)
10041 if (label == NULL_RTX)
10043 label = gen_label_rtx ();
10044 emit_label_after (label, loop->end);
10047 /* Store the memory immediately after END, which is
10048 the NOTE_LOOP_END. */
10049 set = gen_move_insn (copy_rtx (mem), reg);
10050 loop_insn_emit_after (loop, 0, label, set);
10053 if (loop_dump_stream)
10055 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
10056 REGNO (reg), (written ? "r/w" : "r/o"));
10057 print_rtl (loop_dump_stream, mem);
10058 fputc ('\n', loop_dump_stream);
10061 /* Attempt a bit of copy propagation. This helps untangle the
10062 data flow, and enables {basic,general}_induction_var to find
10063 more bivs/givs. */
10064 EXECUTE_IF_SET_IN_REG_SET
10065 (&load_copies, FIRST_PSEUDO_REGISTER, j,
10067 try_copy_prop (loop, reg, j);
10069 CLEAR_REG_SET (&load_copies);
10071 EXECUTE_IF_SET_IN_REG_SET
10072 (&store_copies, FIRST_PSEUDO_REGISTER, j,
10074 try_swap_copy_prop (loop, reg, j);
10076 CLEAR_REG_SET (&store_copies);
10080 /* Now, we need to replace all references to the previous exit
10081 label with the new one. */
10082 if (label != NULL_RTX && end_label != NULL_RTX)
10083 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
10084 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
10085 redirect_jump (p, label, false);
10087 cselib_finish ();
10090 /* For communication between note_reg_stored and its caller. */
10091 struct note_reg_stored_arg
10093 int set_seen;
10094 rtx reg;
10097 /* Called via note_stores, record in SET_SEEN whether X, which is written,
10098 is equal to ARG. */
10099 static void
10100 note_reg_stored (rtx x, rtx setter ATTRIBUTE_UNUSED, void *arg)
10102 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
10103 if (t->reg == x)
10104 t->set_seen = 1;
10107 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
10108 There must be exactly one insn that sets this pseudo; it will be
10109 deleted if all replacements succeed and we can prove that the register
10110 is not used after the loop. */
10112 static void
10113 try_copy_prop (const struct loop *loop, rtx replacement, unsigned int regno)
10115 /* This is the reg that we are copying from. */
10116 rtx reg_rtx = regno_reg_rtx[regno];
10117 rtx init_insn = 0;
10118 rtx insn;
10119 /* These help keep track of whether we replaced all uses of the reg. */
10120 int replaced_last = 0;
10121 int store_is_first = 0;
10123 for (insn = next_insn_in_loop (loop, loop->scan_start);
10124 insn != NULL_RTX;
10125 insn = next_insn_in_loop (loop, insn))
10127 rtx set;
10129 /* Only substitute within one extended basic block from the initializing
10130 insn. */
10131 if (GET_CODE (insn) == CODE_LABEL && init_insn)
10132 break;
10134 if (! INSN_P (insn))
10135 continue;
10137 /* Is this the initializing insn? */
10138 set = single_set (insn);
10139 if (set
10140 && GET_CODE (SET_DEST (set)) == REG
10141 && REGNO (SET_DEST (set)) == regno)
10143 if (init_insn)
10144 abort ();
10146 init_insn = insn;
10147 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
10148 store_is_first = 1;
10151 /* Only substitute after seeing the initializing insn. */
10152 if (init_insn && insn != init_insn)
10154 struct note_reg_stored_arg arg;
10156 replace_loop_regs (insn, reg_rtx, replacement);
10157 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
10158 replaced_last = 1;
10160 /* Stop replacing when REPLACEMENT is modified. */
10161 arg.reg = replacement;
10162 arg.set_seen = 0;
10163 note_stores (PATTERN (insn), note_reg_stored, &arg);
10164 if (arg.set_seen)
10166 rtx note = find_reg_note (insn, REG_EQUAL, NULL);
10168 /* It is possible that we've turned previously valid REG_EQUAL to
10169 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10170 REPLACEMENT is modified, we get different meaning. */
10171 if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
10172 remove_note (insn, note);
10173 break;
10177 if (! init_insn)
10178 abort ();
10179 if (apply_change_group ())
10181 if (loop_dump_stream)
10182 fprintf (loop_dump_stream, " Replaced reg %d", regno);
10183 if (store_is_first && replaced_last)
10185 rtx first;
10186 rtx retval_note;
10188 /* Assume we're just deleting INIT_INSN. */
10189 first = init_insn;
10190 /* Look for REG_RETVAL note. If we're deleting the end of
10191 the libcall sequence, the whole sequence can go. */
10192 retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
10193 /* If we found a REG_RETVAL note, find the first instruction
10194 in the sequence. */
10195 if (retval_note)
10196 first = XEXP (retval_note, 0);
10198 /* Delete the instructions. */
10199 loop_delete_insns (first, init_insn);
10201 if (loop_dump_stream)
10202 fprintf (loop_dump_stream, ".\n");
10206 /* Replace all the instructions from FIRST up to and including LAST
10207 with NOTE_INSN_DELETED notes. */
10209 static void
10210 loop_delete_insns (rtx first, rtx last)
10212 while (1)
10214 if (loop_dump_stream)
10215 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
10216 INSN_UID (first));
10217 delete_insn (first);
10219 /* If this was the LAST instructions we're supposed to delete,
10220 we're done. */
10221 if (first == last)
10222 break;
10224 first = NEXT_INSN (first);
10228 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10229 loop LOOP if the order of the sets of these registers can be
10230 swapped. There must be exactly one insn within the loop that sets
10231 this pseudo followed immediately by a move insn that sets
10232 REPLACEMENT with REGNO. */
10233 static void
10234 try_swap_copy_prop (const struct loop *loop, rtx replacement,
10235 unsigned int regno)
10237 rtx insn;
10238 rtx set = NULL_RTX;
10239 unsigned int new_regno;
10241 new_regno = REGNO (replacement);
10243 for (insn = next_insn_in_loop (loop, loop->scan_start);
10244 insn != NULL_RTX;
10245 insn = next_insn_in_loop (loop, insn))
10247 /* Search for the insn that copies REGNO to NEW_REGNO? */
10248 if (INSN_P (insn)
10249 && (set = single_set (insn))
10250 && GET_CODE (SET_DEST (set)) == REG
10251 && REGNO (SET_DEST (set)) == new_regno
10252 && GET_CODE (SET_SRC (set)) == REG
10253 && REGNO (SET_SRC (set)) == regno)
10254 break;
10257 if (insn != NULL_RTX)
10259 rtx prev_insn;
10260 rtx prev_set;
10262 /* Some DEF-USE info would come in handy here to make this
10263 function more general. For now, just check the previous insn
10264 which is the most likely candidate for setting REGNO. */
10266 prev_insn = PREV_INSN (insn);
10268 if (INSN_P (insn)
10269 && (prev_set = single_set (prev_insn))
10270 && GET_CODE (SET_DEST (prev_set)) == REG
10271 && REGNO (SET_DEST (prev_set)) == regno)
10273 /* We have:
10274 (set (reg regno) (expr))
10275 (set (reg new_regno) (reg regno))
10277 so try converting this to:
10278 (set (reg new_regno) (expr))
10279 (set (reg regno) (reg new_regno))
10281 The former construct is often generated when a global
10282 variable used for an induction variable is shadowed by a
10283 register (NEW_REGNO). The latter construct improves the
10284 chances of GIV replacement and BIV elimination. */
10286 validate_change (prev_insn, &SET_DEST (prev_set),
10287 replacement, 1);
10288 validate_change (insn, &SET_DEST (set),
10289 SET_SRC (set), 1);
10290 validate_change (insn, &SET_SRC (set),
10291 replacement, 1);
10293 if (apply_change_group ())
10295 if (loop_dump_stream)
10296 fprintf (loop_dump_stream,
10297 " Swapped set of reg %d at %d with reg %d at %d.\n",
10298 regno, INSN_UID (insn),
10299 new_regno, INSN_UID (prev_insn));
10301 /* Update first use of REGNO. */
10302 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
10303 REGNO_FIRST_UID (regno) = INSN_UID (insn);
10305 /* Now perform copy propagation to hopefully
10306 remove all uses of REGNO within the loop. */
10307 try_copy_prop (loop, replacement, regno);
10313 /* Worker function for find_mem_in_note, called via for_each_rtx. */
10315 static int
10316 find_mem_in_note_1 (rtx *x, void *data)
10318 if (*x != NULL_RTX && GET_CODE (*x) == MEM)
10320 rtx *res = (rtx *) data;
10321 *res = *x;
10322 return 1;
10324 return 0;
10327 /* Returns the first MEM found in NOTE by depth-first search. */
10329 static rtx
10330 find_mem_in_note (rtx note)
10332 if (note && for_each_rtx (&note, find_mem_in_note_1, &note))
10333 return note;
10334 return NULL_RTX;
10337 /* Replace MEM with its associated pseudo register. This function is
10338 called from load_mems via for_each_rtx. DATA is actually a pointer
10339 to a structure describing the instruction currently being scanned
10340 and the MEM we are currently replacing. */
10342 static int
10343 replace_loop_mem (rtx *mem, void *data)
10345 loop_replace_args *args = (loop_replace_args *) data;
10346 rtx m = *mem;
10348 if (m == NULL_RTX)
10349 return 0;
10351 switch (GET_CODE (m))
10353 case MEM:
10354 break;
10356 case CONST_DOUBLE:
10357 /* We're not interested in the MEM associated with a
10358 CONST_DOUBLE, so there's no need to traverse into one. */
10359 return -1;
10361 default:
10362 /* This is not a MEM. */
10363 return 0;
10366 if (!rtx_equal_p (args->match, m))
10367 /* This is not the MEM we are currently replacing. */
10368 return 0;
10370 /* Actually replace the MEM. */
10371 validate_change (args->insn, mem, args->replacement, 1);
10373 return 0;
10376 static void
10377 replace_loop_mems (rtx insn, rtx mem, rtx reg, int written)
10379 loop_replace_args args;
10381 args.insn = insn;
10382 args.match = mem;
10383 args.replacement = reg;
10385 for_each_rtx (&insn, replace_loop_mem, &args);
10387 /* If we hoist a mem write out of the loop, then REG_EQUAL
10388 notes referring to the mem are no longer valid. */
10389 if (written)
10391 rtx note, sub;
10392 rtx *link;
10394 for (link = &REG_NOTES (insn); (note = *link); link = &XEXP (note, 1))
10396 if (REG_NOTE_KIND (note) == REG_EQUAL
10397 && (sub = find_mem_in_note (note))
10398 && true_dependence (mem, VOIDmode, sub, rtx_varies_p))
10400 /* Remove the note. */
10401 validate_change (NULL_RTX, link, XEXP (note, 1), 1);
10402 break;
10408 /* Replace one register with another. Called through for_each_rtx; PX points
10409 to the rtx being scanned. DATA is actually a pointer to
10410 a structure of arguments. */
10412 static int
10413 replace_loop_reg (rtx *px, void *data)
10415 rtx x = *px;
10416 loop_replace_args *args = (loop_replace_args *) data;
10418 if (x == NULL_RTX)
10419 return 0;
10421 if (x == args->match)
10422 validate_change (args->insn, px, args->replacement, 1);
10424 return 0;
10427 static void
10428 replace_loop_regs (rtx insn, rtx reg, rtx replacement)
10430 loop_replace_args args;
10432 args.insn = insn;
10433 args.match = reg;
10434 args.replacement = replacement;
10436 for_each_rtx (&insn, replace_loop_reg, &args);
10439 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10440 (ignored in the interim). */
10442 static rtx
10443 loop_insn_emit_after (const struct loop *loop ATTRIBUTE_UNUSED,
10444 basic_block where_bb ATTRIBUTE_UNUSED, rtx where_insn,
10445 rtx pattern)
10447 return emit_insn_after (pattern, where_insn);
10451 /* If WHERE_INSN is nonzero emit insn for PATTERN before WHERE_INSN
10452 in basic block WHERE_BB (ignored in the interim) within the loop
10453 otherwise hoist PATTERN into the loop pre-header. */
10456 loop_insn_emit_before (const struct loop *loop,
10457 basic_block where_bb ATTRIBUTE_UNUSED,
10458 rtx where_insn, rtx pattern)
10460 if (! where_insn)
10461 return loop_insn_hoist (loop, pattern);
10462 return emit_insn_before (pattern, where_insn);
10466 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10467 WHERE_BB (ignored in the interim) within the loop. */
10469 static rtx
10470 loop_call_insn_emit_before (const struct loop *loop ATTRIBUTE_UNUSED,
10471 basic_block where_bb ATTRIBUTE_UNUSED,
10472 rtx where_insn, rtx pattern)
10474 return emit_call_insn_before (pattern, where_insn);
10478 /* Hoist insn for PATTERN into the loop pre-header. */
10481 loop_insn_hoist (const struct loop *loop, rtx pattern)
10483 return loop_insn_emit_before (loop, 0, loop->start, pattern);
10487 /* Hoist call insn for PATTERN into the loop pre-header. */
10489 static rtx
10490 loop_call_insn_hoist (const struct loop *loop, rtx pattern)
10492 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
10496 /* Sink insn for PATTERN after the loop end. */
10499 loop_insn_sink (const struct loop *loop, rtx pattern)
10501 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
10504 /* bl->final_value can be either general_operand or PLUS of general_operand
10505 and constant. Emit sequence of instructions to load it into REG. */
10506 static rtx
10507 gen_load_of_final_value (rtx reg, rtx final_value)
10509 rtx seq;
10510 start_sequence ();
10511 final_value = force_operand (final_value, reg);
10512 if (final_value != reg)
10513 emit_move_insn (reg, final_value);
10514 seq = get_insns ();
10515 end_sequence ();
10516 return seq;
10519 /* If the loop has multiple exits, emit insn for PATTERN before the
10520 loop to ensure that it will always be executed no matter how the
10521 loop exits. Otherwise, emit the insn for PATTERN after the loop,
10522 since this is slightly more efficient. */
10524 static rtx
10525 loop_insn_sink_or_swim (const struct loop *loop, rtx pattern)
10527 if (loop->exit_count)
10528 return loop_insn_hoist (loop, pattern);
10529 else
10530 return loop_insn_sink (loop, pattern);
10533 static void
10534 loop_ivs_dump (const struct loop *loop, FILE *file, int verbose)
10536 struct iv_class *bl;
10537 int iv_num = 0;
10539 if (! loop || ! file)
10540 return;
10542 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10543 iv_num++;
10545 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
10547 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10549 loop_iv_class_dump (bl, file, verbose);
10550 fputc ('\n', file);
10555 static void
10556 loop_iv_class_dump (const struct iv_class *bl, FILE *file,
10557 int verbose ATTRIBUTE_UNUSED)
10559 struct induction *v;
10560 rtx incr;
10561 int i;
10563 if (! bl || ! file)
10564 return;
10566 fprintf (file, "IV class for reg %d, benefit %d\n",
10567 bl->regno, bl->total_benefit);
10569 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
10570 if (bl->initial_value)
10572 fprintf (file, ", init val: ");
10573 print_simple_rtl (file, bl->initial_value);
10575 if (bl->initial_test)
10577 fprintf (file, ", init test: ");
10578 print_simple_rtl (file, bl->initial_test);
10580 fputc ('\n', file);
10582 if (bl->final_value)
10584 fprintf (file, " Final val: ");
10585 print_simple_rtl (file, bl->final_value);
10586 fputc ('\n', file);
10589 if ((incr = biv_total_increment (bl)))
10591 fprintf (file, " Total increment: ");
10592 print_simple_rtl (file, incr);
10593 fputc ('\n', file);
10596 /* List the increments. */
10597 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
10599 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
10600 print_simple_rtl (file, v->add_val);
10601 fputc ('\n', file);
10604 /* List the givs. */
10605 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
10607 fprintf (file, " Giv%d: insn %d, benefit %d, ",
10608 i, INSN_UID (v->insn), v->benefit);
10609 if (v->giv_type == DEST_ADDR)
10610 print_simple_rtl (file, v->mem);
10611 else
10612 print_simple_rtl (file, single_set (v->insn));
10613 fputc ('\n', file);
10618 static void
10619 loop_biv_dump (const struct induction *v, FILE *file, int verbose)
10621 if (! v || ! file)
10622 return;
10624 fprintf (file,
10625 "Biv %d: insn %d",
10626 REGNO (v->dest_reg), INSN_UID (v->insn));
10627 fprintf (file, " const ");
10628 print_simple_rtl (file, v->add_val);
10630 if (verbose && v->final_value)
10632 fputc ('\n', file);
10633 fprintf (file, " final ");
10634 print_simple_rtl (file, v->final_value);
10637 fputc ('\n', file);
10641 static void
10642 loop_giv_dump (const struct induction *v, FILE *file, int verbose)
10644 if (! v || ! file)
10645 return;
10647 if (v->giv_type == DEST_REG)
10648 fprintf (file, "Giv %d: insn %d",
10649 REGNO (v->dest_reg), INSN_UID (v->insn));
10650 else
10651 fprintf (file, "Dest address: insn %d",
10652 INSN_UID (v->insn));
10654 fprintf (file, " src reg %d benefit %d",
10655 REGNO (v->src_reg), v->benefit);
10656 fprintf (file, " lifetime %d",
10657 v->lifetime);
10659 if (v->replaceable)
10660 fprintf (file, " replaceable");
10662 if (v->no_const_addval)
10663 fprintf (file, " ncav");
10665 if (v->ext_dependent)
10667 switch (GET_CODE (v->ext_dependent))
10669 case SIGN_EXTEND:
10670 fprintf (file, " ext se");
10671 break;
10672 case ZERO_EXTEND:
10673 fprintf (file, " ext ze");
10674 break;
10675 case TRUNCATE:
10676 fprintf (file, " ext tr");
10677 break;
10678 default:
10679 abort ();
10683 fputc ('\n', file);
10684 fprintf (file, " mult ");
10685 print_simple_rtl (file, v->mult_val);
10687 fputc ('\n', file);
10688 fprintf (file, " add ");
10689 print_simple_rtl (file, v->add_val);
10691 if (verbose && v->final_value)
10693 fputc ('\n', file);
10694 fprintf (file, " final ");
10695 print_simple_rtl (file, v->final_value);
10698 fputc ('\n', file);
10702 void
10703 debug_ivs (const struct loop *loop)
10705 loop_ivs_dump (loop, stderr, 1);
10709 void
10710 debug_iv_class (const struct iv_class *bl)
10712 loop_iv_class_dump (bl, stderr, 1);
10716 void
10717 debug_biv (const struct induction *v)
10719 loop_biv_dump (v, stderr, 1);
10723 void
10724 debug_giv (const struct induction *v)
10726 loop_giv_dump (v, stderr, 1);
10730 #define LOOP_BLOCK_NUM_1(INSN) \
10731 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10733 /* The notes do not have an assigned block, so look at the next insn. */
10734 #define LOOP_BLOCK_NUM(INSN) \
10735 ((INSN) ? (GET_CODE (INSN) == NOTE \
10736 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10737 : LOOP_BLOCK_NUM_1 (INSN)) \
10738 : -1)
10740 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10742 static void
10743 loop_dump_aux (const struct loop *loop, FILE *file,
10744 int verbose ATTRIBUTE_UNUSED)
10746 rtx label;
10748 if (! loop || ! file)
10749 return;
10751 /* Print diagnostics to compare our concept of a loop with
10752 what the loop notes say. */
10753 if (! PREV_INSN (BB_HEAD (loop->first))
10754 || GET_CODE (PREV_INSN (BB_HEAD (loop->first))) != NOTE
10755 || NOTE_LINE_NUMBER (PREV_INSN (BB_HEAD (loop->first)))
10756 != NOTE_INSN_LOOP_BEG)
10757 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
10758 INSN_UID (PREV_INSN (BB_HEAD (loop->first))));
10759 if (! NEXT_INSN (BB_END (loop->last))
10760 || GET_CODE (NEXT_INSN (BB_END (loop->last))) != NOTE
10761 || NOTE_LINE_NUMBER (NEXT_INSN (BB_END (loop->last)))
10762 != NOTE_INSN_LOOP_END)
10763 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
10764 INSN_UID (NEXT_INSN (BB_END (loop->last))));
10766 if (loop->start)
10768 fprintf (file,
10769 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10770 LOOP_BLOCK_NUM (loop->start),
10771 LOOP_INSN_UID (loop->start),
10772 LOOP_BLOCK_NUM (loop->cont),
10773 LOOP_INSN_UID (loop->cont),
10774 LOOP_BLOCK_NUM (loop->cont),
10775 LOOP_INSN_UID (loop->cont),
10776 LOOP_BLOCK_NUM (loop->vtop),
10777 LOOP_INSN_UID (loop->vtop),
10778 LOOP_BLOCK_NUM (loop->end),
10779 LOOP_INSN_UID (loop->end));
10780 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
10781 LOOP_BLOCK_NUM (loop->top),
10782 LOOP_INSN_UID (loop->top),
10783 LOOP_BLOCK_NUM (loop->scan_start),
10784 LOOP_INSN_UID (loop->scan_start));
10785 fprintf (file, ";; exit_count %d", loop->exit_count);
10786 if (loop->exit_count)
10788 fputs (", labels:", file);
10789 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
10791 fprintf (file, " %d ",
10792 LOOP_INSN_UID (XEXP (label, 0)));
10795 fputs ("\n", file);
10797 /* This can happen when a marked loop appears as two nested loops,
10798 say from while (a || b) {}. The inner loop won't match
10799 the loop markers but the outer one will. */
10800 if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
10801 fprintf (file, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
10805 /* Call this function from the debugger to dump LOOP. */
10807 void
10808 debug_loop (const struct loop *loop)
10810 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
10813 /* Call this function from the debugger to dump LOOPS. */
10815 void
10816 debug_loops (const struct loops *loops)
10818 flow_loops_dump (loops, stderr, loop_dump_aux, 1);