* config/xtensa/xtensa.h (ASM_OUTPUT_POOL_PROLOGUE): Emit a
[official-gcc.git] / gcc / loop.c
blob6617608cfd3078a4a0b173f5c0a81e938e307a41
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 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. Strength reduction is applied to the general
26 induction variables, and induction variable elimination is applied to
27 the basic induction variables.
29 It also finds cases where
30 a register is set within the loop by zero-extending a narrower value
31 and changes these to zero the entire register once before the loop
32 and merely copy the low part within the loop.
34 Most of the complexity is in heuristics to decide when it is worth
35 while to do these things. */
37 #include "config.h"
38 #include "system.h"
39 #include "rtl.h"
40 #include "tm_p.h"
41 #include "obstack.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "hard-reg-set.h"
45 #include "basic-block.h"
46 #include "insn-config.h"
47 #include "regs.h"
48 #include "recog.h"
49 #include "flags.h"
50 #include "real.h"
51 #include "loop.h"
52 #include "cselib.h"
53 #include "except.h"
54 #include "toplev.h"
55 #include "predict.h"
56 #include "insn-flags.h"
57 #include "optabs.h"
59 /* Not really meaningful values, but at least something. */
60 #ifndef SIMULTANEOUS_PREFETCHES
61 #define SIMULTANEOUS_PREFETCHES 3
62 #endif
63 #ifndef PREFETCH_BLOCK
64 #define PREFETCH_BLOCK 32
65 #endif
66 #ifndef HAVE_prefetch
67 #define HAVE_prefetch 0
68 #define CODE_FOR_prefetch 0
69 #define gen_prefetch(a,b,c) (abort(), NULL_RTX)
70 #endif
72 /* Give up the prefetch optimizations once we exceed a given threshhold.
73 It is unlikely that we would be able to optimize something in a loop
74 with so many detected prefetches. */
75 #define MAX_PREFETCHES 100
76 /* The number of prefetch blocks that are beneficial to fetch at once before
77 a loop with a known (and low) iteration count. */
78 #define PREFETCH_BLOCKS_BEFORE_LOOP_MAX 6
79 /* For very tiny loops it is not worthwhile to prefetch even before the loop,
80 since it is likely that the data are already in the cache. */
81 #define PREFETCH_BLOCKS_BEFORE_LOOP_MIN 2
82 /* The minimal number of prefetch blocks that a loop must consume to make
83 the emitting of prefetch instruction in the body of loop worthwhile. */
84 #define PREFETCH_BLOCKS_IN_LOOP_MIN 6
86 /* Parameterize some prefetch heuristics so they can be turned on and off
87 easily for performance testing on new architecures. These can be
88 defined in target-dependent files. */
90 /* Prefetch is worthwhile only when loads/stores are dense. */
91 #ifndef PREFETCH_ONLY_DENSE_MEM
92 #define PREFETCH_ONLY_DENSE_MEM 1
93 #endif
95 /* Define what we mean by "dense" loads and stores; This value divided by 256
96 is the minimum percentage of memory references that worth prefetching. */
97 #ifndef PREFETCH_DENSE_MEM
98 #define PREFETCH_DENSE_MEM 220
99 #endif
101 /* Do not prefetch for a loop whose iteration count is known to be low. */
102 #ifndef PREFETCH_NO_LOW_LOOPCNT
103 #define PREFETCH_NO_LOW_LOOPCNT 1
104 #endif
106 /* Define what we mean by a "low" iteration count. */
107 #ifndef PREFETCH_LOW_LOOPCNT
108 #define PREFETCH_LOW_LOOPCNT 32
109 #endif
111 /* Do not prefetch for a loop that contains a function call; such a loop is
112 probably not an internal loop. */
113 #ifndef PREFETCH_NO_CALL
114 #define PREFETCH_NO_CALL 1
115 #endif
117 /* Do not prefetch accesses with an extreme stride. */
118 #ifndef PREFETCH_NO_EXTREME_STRIDE
119 #define PREFETCH_NO_EXTREME_STRIDE 1
120 #endif
122 /* Define what we mean by an "extreme" stride. */
123 #ifndef PREFETCH_EXTREME_STRIDE
124 #define PREFETCH_EXTREME_STRIDE 4096
125 #endif
127 /* Do not handle reversed order prefetches (negative stride). */
128 #ifndef PREFETCH_NO_REVERSE_ORDER
129 #define PREFETCH_NO_REVERSE_ORDER 1
130 #endif
132 /* Prefetch even if the GIV is not always executed. */
133 #ifndef PREFETCH_NOT_ALWAYS
134 #define PREFETCH_NOT_ALWAYS 0
135 #endif
137 /* If the loop requires more prefetches than the target can process in
138 parallel then don't prefetch anything in that loop. */
139 #ifndef PREFETCH_LIMIT_TO_SIMULTANEOUS
140 #define PREFETCH_LIMIT_TO_SIMULTANEOUS 1
141 #endif
143 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
144 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
146 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
147 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
148 || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
150 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
151 ((REGNO) < FIRST_PSEUDO_REGISTER \
152 ? HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
155 /* Vector mapping INSN_UIDs to luids.
156 The luids are like uids but increase monotonically always.
157 We use them to see whether a jump comes from outside a given loop. */
159 int *uid_luid;
161 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
162 number the insn is contained in. */
164 struct loop **uid_loop;
166 /* 1 + largest uid of any insn. */
168 int max_uid_for_loop;
170 /* 1 + luid of last insn. */
172 static int max_luid;
174 /* Number of loops detected in current function. Used as index to the
175 next few tables. */
177 static int max_loop_num;
179 /* Bound on pseudo register number before loop optimization.
180 A pseudo has valid regscan info if its number is < max_reg_before_loop. */
181 unsigned int max_reg_before_loop;
183 /* The value to pass to the next call of reg_scan_update. */
184 static int loop_max_reg;
186 #define obstack_chunk_alloc xmalloc
187 #define obstack_chunk_free free
189 /* During the analysis of a loop, a chain of `struct movable's
190 is made to record all the movable insns found.
191 Then the entire chain can be scanned to decide which to move. */
193 struct movable
195 rtx insn; /* A movable insn */
196 rtx set_src; /* The expression this reg is set from. */
197 rtx set_dest; /* The destination of this SET. */
198 rtx dependencies; /* When INSN is libcall, this is an EXPR_LIST
199 of any registers used within the LIBCALL. */
200 int consec; /* Number of consecutive following insns
201 that must be moved with this one. */
202 unsigned int regno; /* The register it sets */
203 short lifetime; /* lifetime of that register;
204 may be adjusted when matching movables
205 that load the same value are found. */
206 short savings; /* Number of insns we can move for this reg,
207 including other movables that force this
208 or match this one. */
209 unsigned int cond : 1; /* 1 if only conditionally movable */
210 unsigned int force : 1; /* 1 means MUST move this insn */
211 unsigned int global : 1; /* 1 means reg is live outside this loop */
212 /* If PARTIAL is 1, GLOBAL means something different:
213 that the reg is live outside the range from where it is set
214 to the following label. */
215 unsigned int done : 1; /* 1 inhibits further processing of this */
217 unsigned int partial : 1; /* 1 means this reg is used for zero-extending.
218 In particular, moving it does not make it
219 invariant. */
220 unsigned int move_insn : 1; /* 1 means that we call emit_move_insn to
221 load SRC, rather than copying INSN. */
222 unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
223 first insn of a consecutive sets group. */
224 unsigned int is_equiv : 1; /* 1 means a REG_EQUIV is present on INSN. */
225 enum machine_mode savemode; /* Nonzero means it is a mode for a low part
226 that we should avoid changing when clearing
227 the rest of the reg. */
228 struct movable *match; /* First entry for same value */
229 struct movable *forces; /* An insn that must be moved if this is */
230 struct movable *next;
234 FILE *loop_dump_stream;
236 /* Forward declarations. */
238 static void find_and_verify_loops PARAMS ((rtx, struct loops *));
239 static void mark_loop_jump PARAMS ((rtx, struct loop *));
240 static void prescan_loop PARAMS ((struct loop *));
241 static int reg_in_basic_block_p PARAMS ((rtx, rtx));
242 static int consec_sets_invariant_p PARAMS ((const struct loop *,
243 rtx, int, rtx));
244 static int labels_in_range_p PARAMS ((rtx, int));
245 static void count_one_set PARAMS ((struct loop_regs *, rtx, rtx, rtx *));
246 static void note_addr_stored PARAMS ((rtx, rtx, void *));
247 static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
248 static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
249 static void scan_loop PARAMS ((struct loop*, int));
250 #if 0
251 static void replace_call_address PARAMS ((rtx, rtx, rtx));
252 #endif
253 static rtx skip_consec_insns PARAMS ((rtx, int));
254 static int libcall_benefit PARAMS ((rtx));
255 static void ignore_some_movables PARAMS ((struct loop_movables *));
256 static void force_movables PARAMS ((struct loop_movables *));
257 static void combine_movables PARAMS ((struct loop_movables *,
258 struct loop_regs *));
259 static int num_unmoved_movables PARAMS ((const struct loop *));
260 static int regs_match_p PARAMS ((rtx, rtx, struct loop_movables *));
261 static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct loop_movables *,
262 struct loop_regs *));
263 static void add_label_notes PARAMS ((rtx, rtx));
264 static void move_movables PARAMS ((struct loop *loop, struct loop_movables *,
265 int, int));
266 static void loop_movables_add PARAMS((struct loop_movables *,
267 struct movable *));
268 static void loop_movables_free PARAMS((struct loop_movables *));
269 static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
270 static void loop_bivs_find PARAMS((struct loop *));
271 static void loop_bivs_init_find PARAMS((struct loop *));
272 static void loop_bivs_check PARAMS((struct loop *));
273 static void loop_givs_find PARAMS((struct loop *));
274 static void loop_givs_check PARAMS((struct loop *));
275 static int loop_biv_eliminable_p PARAMS((struct loop *, struct iv_class *,
276 int, int));
277 static int loop_giv_reduce_benefit PARAMS((struct loop *, struct iv_class *,
278 struct induction *, rtx));
279 static void loop_givs_dead_check PARAMS((struct loop *, struct iv_class *));
280 static void loop_givs_reduce PARAMS((struct loop *, struct iv_class *));
281 static void loop_givs_rescan PARAMS((struct loop *, struct iv_class *,
282 rtx *));
283 static void loop_ivs_free PARAMS((struct loop *));
284 static void strength_reduce PARAMS ((struct loop *, int));
285 static void find_single_use_in_loop PARAMS ((struct loop_regs *, rtx, rtx));
286 static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
287 static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
288 static void record_biv PARAMS ((struct loop *, struct induction *,
289 rtx, rtx, rtx, rtx, rtx *,
290 int, int));
291 static void check_final_value PARAMS ((const struct loop *,
292 struct induction *));
293 static void loop_ivs_dump PARAMS((const struct loop *, FILE *, int));
294 static void loop_iv_class_dump PARAMS((const struct iv_class *, FILE *, int));
295 static void loop_biv_dump PARAMS((const struct induction *, FILE *, int));
296 static void loop_giv_dump PARAMS((const struct induction *, FILE *, int));
297 static void record_giv PARAMS ((const struct loop *, struct induction *,
298 rtx, rtx, rtx, rtx, rtx, rtx, int,
299 enum g_types, int, int, rtx *));
300 static void update_giv_derive PARAMS ((const struct loop *, rtx));
301 static void check_ext_dependent_givs PARAMS ((struct iv_class *,
302 struct loop_info *));
303 static int basic_induction_var PARAMS ((const struct loop *, rtx,
304 enum machine_mode, rtx, rtx,
305 rtx *, rtx *, rtx **));
306 static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, rtx *, int *));
307 static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
308 rtx *, rtx *, rtx *, int, int *,
309 enum machine_mode));
310 static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
311 rtx, rtx, rtx *, rtx *, rtx *, rtx *));
312 static int check_dbra_loop PARAMS ((struct loop *, int));
313 static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
314 static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
315 static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
316 static void combine_givs PARAMS ((struct loop_regs *, struct iv_class *));
317 static int product_cheap_p PARAMS ((rtx, rtx));
318 static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
319 int, int, int));
320 static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
321 struct iv_class *, int,
322 basic_block, rtx));
323 static int last_use_this_basic_block PARAMS ((rtx, rtx));
324 static void record_initial PARAMS ((rtx, rtx, void *));
325 static void update_reg_last_use PARAMS ((rtx, rtx));
326 static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
327 static void loop_regs_scan PARAMS ((const struct loop *, int));
328 static int count_insns_in_loop PARAMS ((const struct loop *));
329 static void load_mems PARAMS ((const struct loop *));
330 static int insert_loop_mem PARAMS ((rtx *, void *));
331 static int replace_loop_mem PARAMS ((rtx *, void *));
332 static void replace_loop_mems PARAMS ((rtx, rtx, rtx));
333 static int replace_loop_reg PARAMS ((rtx *, void *));
334 static void replace_loop_regs PARAMS ((rtx insn, rtx, rtx));
335 static void note_reg_stored PARAMS ((rtx, rtx, void *));
336 static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
337 static void try_swap_copy_prop PARAMS ((const struct loop *, rtx,
338 unsigned int));
339 static int replace_label PARAMS ((rtx *, void *));
340 static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
341 static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
342 static rtx gen_add_mult PARAMS ((rtx, rtx, rtx, rtx));
343 static void loop_regs_update PARAMS ((const struct loop *, rtx));
344 static int iv_add_mult_cost PARAMS ((rtx, rtx, rtx, rtx));
346 static rtx loop_insn_emit_after PARAMS((const struct loop *, basic_block,
347 rtx, rtx));
348 static rtx loop_call_insn_emit_before PARAMS((const struct loop *,
349 basic_block, rtx, rtx));
350 static rtx loop_call_insn_hoist PARAMS((const struct loop *, rtx));
351 static rtx loop_insn_sink_or_swim PARAMS((const struct loop *, rtx));
353 static void loop_dump_aux PARAMS ((const struct loop *, FILE *, int));
354 static void loop_delete_insns PARAMS ((rtx, rtx));
355 static HOST_WIDE_INT remove_constant_addition PARAMS ((rtx *));
356 void debug_ivs PARAMS ((const struct loop *));
357 void debug_iv_class PARAMS ((const struct iv_class *));
358 void debug_biv PARAMS ((const struct induction *));
359 void debug_giv PARAMS ((const struct induction *));
360 void debug_loop PARAMS ((const struct loop *));
361 void debug_loops PARAMS ((const struct loops *));
363 typedef struct rtx_pair
365 rtx r1;
366 rtx r2;
367 } rtx_pair;
369 typedef struct loop_replace_args
371 rtx match;
372 rtx replacement;
373 rtx insn;
374 } loop_replace_args;
376 /* Nonzero iff INSN is between START and END, inclusive. */
377 #define INSN_IN_RANGE_P(INSN, START, END) \
378 (INSN_UID (INSN) < max_uid_for_loop \
379 && INSN_LUID (INSN) >= INSN_LUID (START) \
380 && INSN_LUID (INSN) <= INSN_LUID (END))
382 /* Indirect_jump_in_function is computed once per function. */
383 static int indirect_jump_in_function;
384 static int indirect_jump_in_function_p PARAMS ((rtx));
386 static int compute_luids PARAMS ((rtx, rtx, int));
388 static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
389 struct induction *,
390 rtx));
392 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
393 copy the value of the strength reduced giv to its original register. */
394 static int copy_cost;
396 /* Cost of using a register, to normalize the benefits of a giv. */
397 static int reg_address_cost;
399 void
400 init_loop ()
402 rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
404 reg_address_cost = address_cost (reg, SImode);
406 copy_cost = COSTS_N_INSNS (1);
409 /* Compute the mapping from uids to luids.
410 LUIDs are numbers assigned to insns, like uids,
411 except that luids increase monotonically through the code.
412 Start at insn START and stop just before END. Assign LUIDs
413 starting with PREV_LUID + 1. Return the last assigned LUID + 1. */
414 static int
415 compute_luids (start, end, prev_luid)
416 rtx start, end;
417 int prev_luid;
419 int i;
420 rtx insn;
422 for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
424 if (INSN_UID (insn) >= max_uid_for_loop)
425 continue;
426 /* Don't assign luids to line-number NOTEs, so that the distance in
427 luids between two insns is not affected by -g. */
428 if (GET_CODE (insn) != NOTE
429 || NOTE_LINE_NUMBER (insn) <= 0)
430 uid_luid[INSN_UID (insn)] = ++i;
431 else
432 /* Give a line number note the same luid as preceding insn. */
433 uid_luid[INSN_UID (insn)] = i;
435 return i + 1;
438 /* Entry point of this file. Perform loop optimization
439 on the current function. F is the first insn of the function
440 and DUMPFILE is a stream for output of a trace of actions taken
441 (or 0 if none should be output). */
443 void
444 loop_optimize (f, dumpfile, flags)
445 /* f is the first instruction of a chain of insns for one function */
446 rtx f;
447 FILE *dumpfile;
448 int flags;
450 rtx insn;
451 int i;
452 struct loops loops_data;
453 struct loops *loops = &loops_data;
454 struct loop_info *loops_info;
456 loop_dump_stream = dumpfile;
458 init_recog_no_volatile ();
460 max_reg_before_loop = max_reg_num ();
461 loop_max_reg = max_reg_before_loop;
463 regs_may_share = 0;
465 /* Count the number of loops. */
467 max_loop_num = 0;
468 for (insn = f; insn; insn = NEXT_INSN (insn))
470 if (GET_CODE (insn) == NOTE
471 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
472 max_loop_num++;
475 /* Don't waste time if no loops. */
476 if (max_loop_num == 0)
477 return;
479 loops->num = max_loop_num;
481 /* Get size to use for tables indexed by uids.
482 Leave some space for labels allocated by find_and_verify_loops. */
483 max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
485 uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
486 uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
487 sizeof (struct loop *));
489 /* Allocate storage for array of loops. */
490 loops->array = (struct loop *)
491 xcalloc (loops->num, sizeof (struct loop));
493 /* Find and process each loop.
494 First, find them, and record them in order of their beginnings. */
495 find_and_verify_loops (f, loops);
497 /* Allocate and initialize auxiliary loop information. */
498 loops_info = xcalloc (loops->num, sizeof (struct loop_info));
499 for (i = 0; i < loops->num; i++)
500 loops->array[i].aux = loops_info + i;
502 /* Now find all register lifetimes. This must be done after
503 find_and_verify_loops, because it might reorder the insns in the
504 function. */
505 reg_scan (f, max_reg_before_loop, 1);
507 /* This must occur after reg_scan so that registers created by gcse
508 will have entries in the register tables.
510 We could have added a call to reg_scan after gcse_main in toplev.c,
511 but moving this call to init_alias_analysis is more efficient. */
512 init_alias_analysis ();
514 /* See if we went too far. Note that get_max_uid already returns
515 one more that the maximum uid of all insn. */
516 if (get_max_uid () > max_uid_for_loop)
517 abort ();
518 /* Now reset it to the actual size we need. See above. */
519 max_uid_for_loop = get_max_uid ();
521 /* find_and_verify_loops has already called compute_luids, but it
522 might have rearranged code afterwards, so we need to recompute
523 the luids now. */
524 max_luid = compute_luids (f, NULL_RTX, 0);
526 /* Don't leave gaps in uid_luid for insns that have been
527 deleted. It is possible that the first or last insn
528 using some register has been deleted by cross-jumping.
529 Make sure that uid_luid for that former insn's uid
530 points to the general area where that insn used to be. */
531 for (i = 0; i < max_uid_for_loop; i++)
533 uid_luid[0] = uid_luid[i];
534 if (uid_luid[0] != 0)
535 break;
537 for (i = 0; i < max_uid_for_loop; i++)
538 if (uid_luid[i] == 0)
539 uid_luid[i] = uid_luid[i - 1];
541 /* Determine if the function has indirect jump. On some systems
542 this prevents low overhead loop instructions from being used. */
543 indirect_jump_in_function = indirect_jump_in_function_p (f);
545 /* Now scan the loops, last ones first, since this means inner ones are done
546 before outer ones. */
547 for (i = max_loop_num - 1; i >= 0; i--)
549 struct loop *loop = &loops->array[i];
551 if (! loop->invalid && loop->end)
552 scan_loop (loop, flags);
555 /* If there were lexical blocks inside the loop, they have been
556 replicated. We will now have more than one NOTE_INSN_BLOCK_BEG
557 and NOTE_INSN_BLOCK_END for each such block. We must duplicate
558 the BLOCKs as well. */
559 if (write_symbols != NO_DEBUG)
560 reorder_blocks ();
562 end_alias_analysis ();
564 /* Clean up. */
565 free (uid_luid);
566 free (uid_loop);
567 free (loops_info);
568 free (loops->array);
571 /* Returns the next insn, in execution order, after INSN. START and
572 END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
573 respectively. LOOP->TOP, if non-NULL, is the top of the loop in the
574 insn-stream; it is used with loops that are entered near the
575 bottom. */
577 static rtx
578 next_insn_in_loop (loop, insn)
579 const struct loop *loop;
580 rtx insn;
582 insn = NEXT_INSN (insn);
584 if (insn == loop->end)
586 if (loop->top)
587 /* Go to the top of the loop, and continue there. */
588 insn = loop->top;
589 else
590 /* We're done. */
591 insn = NULL_RTX;
594 if (insn == loop->scan_start)
595 /* We're done. */
596 insn = NULL_RTX;
598 return insn;
601 /* Optimize one loop described by LOOP. */
603 /* ??? Could also move memory writes out of loops if the destination address
604 is invariant, the source is invariant, the memory write is not volatile,
605 and if we can prove that no read inside the loop can read this address
606 before the write occurs. If there is a read of this address after the
607 write, then we can also mark the memory read as invariant. */
609 static void
610 scan_loop (loop, flags)
611 struct loop *loop;
612 int flags;
614 struct loop_info *loop_info = LOOP_INFO (loop);
615 struct loop_regs *regs = LOOP_REGS (loop);
616 int i;
617 rtx loop_start = loop->start;
618 rtx loop_end = loop->end;
619 rtx p;
620 /* 1 if we are scanning insns that could be executed zero times. */
621 int maybe_never = 0;
622 /* 1 if we are scanning insns that might never be executed
623 due to a subroutine call which might exit before they are reached. */
624 int call_passed = 0;
625 /* Jump insn that enters the loop, or 0 if control drops in. */
626 rtx loop_entry_jump = 0;
627 /* Number of insns in the loop. */
628 int insn_count;
629 int tem;
630 rtx temp, update_start, update_end;
631 /* The SET from an insn, if it is the only SET in the insn. */
632 rtx set, set1;
633 /* Chain describing insns movable in current loop. */
634 struct loop_movables *movables = LOOP_MOVABLES (loop);
635 /* Ratio of extra register life span we can justify
636 for saving an instruction. More if loop doesn't call subroutines
637 since in that case saving an insn makes more difference
638 and more registers are available. */
639 int threshold;
640 /* Nonzero if we are scanning instructions in a sub-loop. */
641 int loop_depth = 0;
643 loop->top = 0;
645 movables->head = 0;
646 movables->last = 0;
648 /* Determine whether this loop starts with a jump down to a test at
649 the end. This will occur for a small number of loops with a test
650 that is too complex to duplicate in front of the loop.
652 We search for the first insn or label in the loop, skipping NOTEs.
653 However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
654 (because we might have a loop executed only once that contains a
655 loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
656 (in case we have a degenerate loop).
658 Note that if we mistakenly think that a loop is entered at the top
659 when, in fact, it is entered at the exit test, the only effect will be
660 slightly poorer optimization. Making the opposite error can generate
661 incorrect code. Since very few loops now start with a jump to the
662 exit test, the code here to detect that case is very conservative. */
664 for (p = NEXT_INSN (loop_start);
665 p != loop_end
666 && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
667 && (GET_CODE (p) != NOTE
668 || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
669 && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
670 p = NEXT_INSN (p))
673 loop->scan_start = p;
675 /* If loop end is the end of the current function, then emit a
676 NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
677 note insn. This is the position we use when sinking insns out of
678 the loop. */
679 if (NEXT_INSN (loop->end) != 0)
680 loop->sink = NEXT_INSN (loop->end);
681 else
682 loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
684 /* Set up variables describing this loop. */
685 prescan_loop (loop);
686 threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
688 /* If loop has a jump before the first label,
689 the true entry is the target of that jump.
690 Start scan from there.
691 But record in LOOP->TOP the place where the end-test jumps
692 back to so we can scan that after the end of the loop. */
693 if (GET_CODE (p) == JUMP_INSN)
695 loop_entry_jump = p;
697 /* Loop entry must be unconditional jump (and not a RETURN) */
698 if (any_uncondjump_p (p)
699 && JUMP_LABEL (p) != 0
700 /* Check to see whether the jump actually
701 jumps out of the loop (meaning it's no loop).
702 This case can happen for things like
703 do {..} while (0). If this label was generated previously
704 by loop, we can't tell anything about it and have to reject
705 the loop. */
706 && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
708 loop->top = next_label (loop->scan_start);
709 loop->scan_start = JUMP_LABEL (p);
713 /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
714 as required by loop_reg_used_before_p. So skip such loops. (This
715 test may never be true, but it's best to play it safe.)
717 Also, skip loops where we do not start scanning at a label. This
718 test also rejects loops starting with a JUMP_INSN that failed the
719 test above. */
721 if (INSN_UID (loop->scan_start) >= max_uid_for_loop
722 || GET_CODE (loop->scan_start) != CODE_LABEL)
724 if (loop_dump_stream)
725 fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
726 INSN_UID (loop_start), INSN_UID (loop_end));
727 return;
730 /* Allocate extra space for REGs that might be created by load_mems.
731 We allocate a little extra slop as well, in the hopes that we
732 won't have to reallocate the regs array. */
733 loop_regs_scan (loop, loop_info->mems_idx + 16);
734 insn_count = count_insns_in_loop (loop);
736 if (loop_dump_stream)
738 fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
739 INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
740 if (loop->cont)
741 fprintf (loop_dump_stream, "Continue at insn %d.\n",
742 INSN_UID (loop->cont));
745 /* Scan through the loop finding insns that are safe to move.
746 Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
747 this reg will be considered invariant for subsequent insns.
748 We consider whether subsequent insns use the reg
749 in deciding whether it is worth actually moving.
751 MAYBE_NEVER is nonzero if we have passed a conditional jump insn
752 and therefore it is possible that the insns we are scanning
753 would never be executed. At such times, we must make sure
754 that it is safe to execute the insn once instead of zero times.
755 When MAYBE_NEVER is 0, all insns will be executed at least once
756 so that is not a problem. */
758 for (p = next_insn_in_loop (loop, loop->scan_start);
759 p != NULL_RTX;
760 p = next_insn_in_loop (loop, p))
762 if (GET_CODE (p) == INSN
763 && (set = single_set (p))
764 && GET_CODE (SET_DEST (set)) == REG
765 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
766 && SET_DEST (set) != pic_offset_table_rtx
767 #endif
768 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
770 int tem1 = 0;
771 int tem2 = 0;
772 int move_insn = 0;
773 rtx src = SET_SRC (set);
774 rtx dependencies = 0;
776 /* Figure out what to use as a source of this insn. If a REG_EQUIV
777 note is given or if a REG_EQUAL note with a constant operand is
778 specified, use it as the source and mark that we should move
779 this insn by calling emit_move_insn rather that duplicating the
780 insn.
782 Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
783 is present. */
784 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
785 if (temp)
786 src = XEXP (temp, 0), move_insn = 1;
787 else
789 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
790 if (temp && CONSTANT_P (XEXP (temp, 0)))
791 src = XEXP (temp, 0), move_insn = 1;
792 if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
794 src = XEXP (temp, 0);
795 /* A libcall block can use regs that don't appear in
796 the equivalent expression. To move the libcall,
797 we must move those regs too. */
798 dependencies = libcall_other_reg (p, src);
802 /* For parallels, add any possible uses to the depencies, as we can't move
803 the insn without resolving them first. */
804 if (GET_CODE (PATTERN (p)) == PARALLEL)
806 for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
808 rtx x = XVECEXP (PATTERN (p), 0, i);
809 if (GET_CODE (x) == USE)
810 dependencies = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0), dependencies);
814 /* Don't try to optimize a register that was made
815 by loop-optimization for an inner loop.
816 We don't know its life-span, so we can't compute the benefit. */
817 if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
819 else if (/* The register is used in basic blocks other
820 than the one where it is set (meaning that
821 something after this point in the loop might
822 depend on its value before the set). */
823 ! reg_in_basic_block_p (p, SET_DEST (set))
824 /* And the set is not guaranteed to be executed once
825 the loop starts, or the value before the set is
826 needed before the set occurs...
828 ??? Note we have quadratic behaviour here, mitigated
829 by the fact that the previous test will often fail for
830 large loops. Rather than re-scanning the entire loop
831 each time for register usage, we should build tables
832 of the register usage and use them here instead. */
833 && (maybe_never
834 || loop_reg_used_before_p (loop, set, p)))
835 /* It is unsafe to move the set.
837 This code used to consider it OK to move a set of a variable
838 which was not created by the user and not used in an exit test.
839 That behavior is incorrect and was removed. */
841 else if ((tem = loop_invariant_p (loop, src))
842 && (dependencies == 0
843 || (tem2 = loop_invariant_p (loop, dependencies)) != 0)
844 && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
845 || (tem1
846 = consec_sets_invariant_p
847 (loop, SET_DEST (set),
848 regs->array[REGNO (SET_DEST (set))].set_in_loop,
849 p)))
850 /* If the insn can cause a trap (such as divide by zero),
851 can't move it unless it's guaranteed to be executed
852 once loop is entered. Even a function call might
853 prevent the trap insn from being reached
854 (since it might exit!) */
855 && ! ((maybe_never || call_passed)
856 && may_trap_p (src)))
858 struct movable *m;
859 int regno = REGNO (SET_DEST (set));
861 /* A potential lossage is where we have a case where two insns
862 can be combined as long as they are both in the loop, but
863 we move one of them outside the loop. For large loops,
864 this can lose. The most common case of this is the address
865 of a function being called.
867 Therefore, if this register is marked as being used exactly
868 once if we are in a loop with calls (a "large loop"), see if
869 we can replace the usage of this register with the source
870 of this SET. If we can, delete this insn.
872 Don't do this if P has a REG_RETVAL note or if we have
873 SMALL_REGISTER_CLASSES and SET_SRC is a hard register. */
875 if (loop_info->has_call
876 && regs->array[regno].single_usage != 0
877 && regs->array[regno].single_usage != const0_rtx
878 && REGNO_FIRST_UID (regno) == INSN_UID (p)
879 && (REGNO_LAST_UID (regno)
880 == INSN_UID (regs->array[regno].single_usage))
881 && regs->array[regno].set_in_loop == 1
882 && GET_CODE (SET_SRC (set)) != ASM_OPERANDS
883 && ! side_effects_p (SET_SRC (set))
884 && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
885 && (! SMALL_REGISTER_CLASSES
886 || (! (GET_CODE (SET_SRC (set)) == REG
887 && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
888 /* This test is not redundant; SET_SRC (set) might be
889 a call-clobbered register and the life of REGNO
890 might span a call. */
891 && ! modified_between_p (SET_SRC (set), p,
892 regs->array[regno].single_usage)
893 && no_labels_between_p (p, regs->array[regno].single_usage)
894 && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
895 regs->array[regno].single_usage))
897 /* Replace any usage in a REG_EQUAL note. Must copy the
898 new source, so that we don't get rtx sharing between the
899 SET_SOURCE and REG_NOTES of insn p. */
900 REG_NOTES (regs->array[regno].single_usage)
901 = replace_rtx (REG_NOTES (regs->array[regno].single_usage),
902 SET_DEST (set), copy_rtx (SET_SRC (set)));
904 delete_insn (p);
905 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
906 regs->array[regno+i].set_in_loop = 0;
907 continue;
910 m = (struct movable *) xmalloc (sizeof (struct movable));
911 m->next = 0;
912 m->insn = p;
913 m->set_src = src;
914 m->dependencies = dependencies;
915 m->set_dest = SET_DEST (set);
916 m->force = 0;
917 m->consec = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
918 m->done = 0;
919 m->forces = 0;
920 m->partial = 0;
921 m->move_insn = move_insn;
922 m->move_insn_first = 0;
923 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
924 m->savemode = VOIDmode;
925 m->regno = regno;
926 /* Set M->cond if either loop_invariant_p
927 or consec_sets_invariant_p returned 2
928 (only conditionally invariant). */
929 m->cond = ((tem | tem1 | tem2) > 1);
930 m->global = LOOP_REG_GLOBAL_P (loop, regno);
931 m->match = 0;
932 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
933 m->savings = regs->array[regno].n_times_set;
934 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
935 m->savings += libcall_benefit (p);
936 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
937 regs->array[regno+i].set_in_loop = move_insn ? -2 : -1;
938 /* Add M to the end of the chain MOVABLES. */
939 loop_movables_add (movables, m);
941 if (m->consec > 0)
943 /* It is possible for the first instruction to have a
944 REG_EQUAL note but a non-invariant SET_SRC, so we must
945 remember the status of the first instruction in case
946 the last instruction doesn't have a REG_EQUAL note. */
947 m->move_insn_first = m->move_insn;
949 /* Skip this insn, not checking REG_LIBCALL notes. */
950 p = next_nonnote_insn (p);
951 /* Skip the consecutive insns, if there are any. */
952 p = skip_consec_insns (p, m->consec);
953 /* Back up to the last insn of the consecutive group. */
954 p = prev_nonnote_insn (p);
956 /* We must now reset m->move_insn, m->is_equiv, and possibly
957 m->set_src to correspond to the effects of all the
958 insns. */
959 temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
960 if (temp)
961 m->set_src = XEXP (temp, 0), m->move_insn = 1;
962 else
964 temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
965 if (temp && CONSTANT_P (XEXP (temp, 0)))
966 m->set_src = XEXP (temp, 0), m->move_insn = 1;
967 else
968 m->move_insn = 0;
971 m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
974 /* If this register is always set within a STRICT_LOW_PART
975 or set to zero, then its high bytes are constant.
976 So clear them outside the loop and within the loop
977 just load the low bytes.
978 We must check that the machine has an instruction to do so.
979 Also, if the value loaded into the register
980 depends on the same register, this cannot be done. */
981 else if (SET_SRC (set) == const0_rtx
982 && GET_CODE (NEXT_INSN (p)) == INSN
983 && (set1 = single_set (NEXT_INSN (p)))
984 && GET_CODE (set1) == SET
985 && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
986 && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
987 && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
988 == SET_DEST (set))
989 && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
991 int regno = REGNO (SET_DEST (set));
992 if (regs->array[regno].set_in_loop == 2)
994 struct movable *m;
995 m = (struct movable *) xmalloc (sizeof (struct movable));
996 m->next = 0;
997 m->insn = p;
998 m->set_dest = SET_DEST (set);
999 m->dependencies = 0;
1000 m->force = 0;
1001 m->consec = 0;
1002 m->done = 0;
1003 m->forces = 0;
1004 m->move_insn = 0;
1005 m->move_insn_first = 0;
1006 m->partial = 1;
1007 /* If the insn may not be executed on some cycles,
1008 we can't clear the whole reg; clear just high part.
1009 Not even if the reg is used only within this loop.
1010 Consider this:
1011 while (1)
1012 while (s != t) {
1013 if (foo ()) x = *s;
1014 use (x);
1016 Clearing x before the inner loop could clobber a value
1017 being saved from the last time around the outer loop.
1018 However, if the reg is not used outside this loop
1019 and all uses of the register are in the same
1020 basic block as the store, there is no problem.
1022 If this insn was made by loop, we don't know its
1023 INSN_LUID and hence must make a conservative
1024 assumption. */
1025 m->global = (INSN_UID (p) >= max_uid_for_loop
1026 || LOOP_REG_GLOBAL_P (loop, regno)
1027 || (labels_in_range_p
1028 (p, REGNO_FIRST_LUID (regno))));
1029 if (maybe_never && m->global)
1030 m->savemode = GET_MODE (SET_SRC (set1));
1031 else
1032 m->savemode = VOIDmode;
1033 m->regno = regno;
1034 m->cond = 0;
1035 m->match = 0;
1036 m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1037 m->savings = 1;
1038 for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
1039 regs->array[regno+i].set_in_loop = -1;
1040 /* Add M to the end of the chain MOVABLES. */
1041 loop_movables_add (movables, m);
1045 /* Past a call insn, we get to insns which might not be executed
1046 because the call might exit. This matters for insns that trap.
1047 Constant and pure call insns always return, so they don't count. */
1048 else if (GET_CODE (p) == CALL_INSN && ! CONST_OR_PURE_CALL_P (p))
1049 call_passed = 1;
1050 /* Past a label or a jump, we get to insns for which we
1051 can't count on whether or how many times they will be
1052 executed during each iteration. Therefore, we can
1053 only move out sets of trivial variables
1054 (those not used after the loop). */
1055 /* Similar code appears twice in strength_reduce. */
1056 else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1057 /* If we enter the loop in the middle, and scan around to the
1058 beginning, don't set maybe_never for that. This must be an
1059 unconditional jump, otherwise the code at the top of the
1060 loop might never be executed. Unconditional jumps are
1061 followed by a barrier then the loop_end. */
1062 && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
1063 && NEXT_INSN (NEXT_INSN (p)) == loop_end
1064 && any_uncondjump_p (p)))
1065 maybe_never = 1;
1066 else if (GET_CODE (p) == NOTE)
1068 /* At the virtual top of a converted loop, insns are again known to
1069 be executed: logically, the loop begins here even though the exit
1070 code has been duplicated. */
1071 if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1072 maybe_never = call_passed = 0;
1073 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1074 loop_depth++;
1075 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1076 loop_depth--;
1080 /* If one movable subsumes another, ignore that other. */
1082 ignore_some_movables (movables);
1084 /* For each movable insn, see if the reg that it loads
1085 leads when it dies right into another conditionally movable insn.
1086 If so, record that the second insn "forces" the first one,
1087 since the second can be moved only if the first is. */
1089 force_movables (movables);
1091 /* See if there are multiple movable insns that load the same value.
1092 If there are, make all but the first point at the first one
1093 through the `match' field, and add the priorities of them
1094 all together as the priority of the first. */
1096 combine_movables (movables, regs);
1098 /* Now consider each movable insn to decide whether it is worth moving.
1099 Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1101 Generally this increases code size, so do not move moveables when
1102 optimizing for code size. */
1104 if (! optimize_size)
1106 move_movables (loop, movables, threshold, insn_count);
1108 /* Recalculate regs->array if move_movables has created new
1109 registers. */
1110 if (max_reg_num () > regs->num)
1112 loop_regs_scan (loop, 0);
1113 for (update_start = loop_start;
1114 PREV_INSN (update_start)
1115 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1116 update_start = PREV_INSN (update_start))
1118 update_end = NEXT_INSN (loop_end);
1120 reg_scan_update (update_start, update_end, loop_max_reg);
1121 loop_max_reg = max_reg_num ();
1125 /* Now candidates that still are negative are those not moved.
1126 Change regs->array[I].set_in_loop to indicate that those are not actually
1127 invariant. */
1128 for (i = 0; i < regs->num; i++)
1129 if (regs->array[i].set_in_loop < 0)
1130 regs->array[i].set_in_loop = regs->array[i].n_times_set;
1132 /* Now that we've moved some things out of the loop, we might be able to
1133 hoist even more memory references. */
1134 load_mems (loop);
1136 /* Recalculate regs->array if load_mems has created new registers. */
1137 if (max_reg_num () > regs->num)
1138 loop_regs_scan (loop, 0);
1140 for (update_start = loop_start;
1141 PREV_INSN (update_start)
1142 && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1143 update_start = PREV_INSN (update_start))
1145 update_end = NEXT_INSN (loop_end);
1147 reg_scan_update (update_start, update_end, loop_max_reg);
1148 loop_max_reg = max_reg_num ();
1150 if (flag_strength_reduce)
1152 if (update_end && GET_CODE (update_end) == CODE_LABEL)
1153 /* Ensure our label doesn't go away. */
1154 LABEL_NUSES (update_end)++;
1156 strength_reduce (loop, flags);
1158 reg_scan_update (update_start, update_end, loop_max_reg);
1159 loop_max_reg = max_reg_num ();
1161 if (update_end && GET_CODE (update_end) == CODE_LABEL
1162 && --LABEL_NUSES (update_end) == 0)
1163 delete_related_insns (update_end);
1167 /* The movable information is required for strength reduction. */
1168 loop_movables_free (movables);
1170 free (regs->array);
1171 regs->array = 0;
1172 regs->num = 0;
1175 /* Add elements to *OUTPUT to record all the pseudo-regs
1176 mentioned in IN_THIS but not mentioned in NOT_IN_THIS. */
1178 void
1179 record_excess_regs (in_this, not_in_this, output)
1180 rtx in_this, not_in_this;
1181 rtx *output;
1183 enum rtx_code code;
1184 const char *fmt;
1185 int i;
1187 code = GET_CODE (in_this);
1189 switch (code)
1191 case PC:
1192 case CC0:
1193 case CONST_INT:
1194 case CONST_DOUBLE:
1195 case CONST:
1196 case SYMBOL_REF:
1197 case LABEL_REF:
1198 return;
1200 case REG:
1201 if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1202 && ! reg_mentioned_p (in_this, not_in_this))
1203 *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1204 return;
1206 default:
1207 break;
1210 fmt = GET_RTX_FORMAT (code);
1211 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1213 int j;
1215 switch (fmt[i])
1217 case 'E':
1218 for (j = 0; j < XVECLEN (in_this, i); j++)
1219 record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1220 break;
1222 case 'e':
1223 record_excess_regs (XEXP (in_this, i), not_in_this, output);
1224 break;
1229 /* Check what regs are referred to in the libcall block ending with INSN,
1230 aside from those mentioned in the equivalent value.
1231 If there are none, return 0.
1232 If there are one or more, return an EXPR_LIST containing all of them. */
1235 libcall_other_reg (insn, equiv)
1236 rtx insn, equiv;
1238 rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1239 rtx p = XEXP (note, 0);
1240 rtx output = 0;
1242 /* First, find all the regs used in the libcall block
1243 that are not mentioned as inputs to the result. */
1245 while (p != insn)
1247 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1248 || GET_CODE (p) == CALL_INSN)
1249 record_excess_regs (PATTERN (p), equiv, &output);
1250 p = NEXT_INSN (p);
1253 return output;
1256 /* Return 1 if all uses of REG
1257 are between INSN and the end of the basic block. */
1259 static int
1260 reg_in_basic_block_p (insn, reg)
1261 rtx insn, reg;
1263 int regno = REGNO (reg);
1264 rtx p;
1266 if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1267 return 0;
1269 /* Search this basic block for the already recorded last use of the reg. */
1270 for (p = insn; p; p = NEXT_INSN (p))
1272 switch (GET_CODE (p))
1274 case NOTE:
1275 break;
1277 case INSN:
1278 case CALL_INSN:
1279 /* Ordinary insn: if this is the last use, we win. */
1280 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1281 return 1;
1282 break;
1284 case JUMP_INSN:
1285 /* Jump insn: if this is the last use, we win. */
1286 if (REGNO_LAST_UID (regno) == INSN_UID (p))
1287 return 1;
1288 /* Otherwise, it's the end of the basic block, so we lose. */
1289 return 0;
1291 case CODE_LABEL:
1292 case BARRIER:
1293 /* It's the end of the basic block, so we lose. */
1294 return 0;
1296 default:
1297 break;
1301 /* The "last use" that was recorded can't be found after the first
1302 use. This can happen when the last use was deleted while
1303 processing an inner loop, this inner loop was then completely
1304 unrolled, and the outer loop is always exited after the inner loop,
1305 so that everything after the first use becomes a single basic block. */
1306 return 1;
1309 /* Compute the benefit of eliminating the insns in the block whose
1310 last insn is LAST. This may be a group of insns used to compute a
1311 value directly or can contain a library call. */
1313 static int
1314 libcall_benefit (last)
1315 rtx last;
1317 rtx insn;
1318 int benefit = 0;
1320 for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1321 insn != last; insn = NEXT_INSN (insn))
1323 if (GET_CODE (insn) == CALL_INSN)
1324 benefit += 10; /* Assume at least this many insns in a library
1325 routine. */
1326 else if (GET_CODE (insn) == INSN
1327 && GET_CODE (PATTERN (insn)) != USE
1328 && GET_CODE (PATTERN (insn)) != CLOBBER)
1329 benefit++;
1332 return benefit;
1335 /* Skip COUNT insns from INSN, counting library calls as 1 insn. */
1337 static rtx
1338 skip_consec_insns (insn, count)
1339 rtx insn;
1340 int count;
1342 for (; count > 0; count--)
1344 rtx temp;
1346 /* If first insn of libcall sequence, skip to end. */
1347 /* Do this at start of loop, since INSN is guaranteed to
1348 be an insn here. */
1349 if (GET_CODE (insn) != NOTE
1350 && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1351 insn = XEXP (temp, 0);
1354 insn = NEXT_INSN (insn);
1355 while (GET_CODE (insn) == NOTE);
1358 return insn;
1361 /* Ignore any movable whose insn falls within a libcall
1362 which is part of another movable.
1363 We make use of the fact that the movable for the libcall value
1364 was made later and so appears later on the chain. */
1366 static void
1367 ignore_some_movables (movables)
1368 struct loop_movables *movables;
1370 struct movable *m, *m1;
1372 for (m = movables->head; m; m = m->next)
1374 /* Is this a movable for the value of a libcall? */
1375 rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1376 if (note)
1378 rtx insn;
1379 /* Check for earlier movables inside that range,
1380 and mark them invalid. We cannot use LUIDs here because
1381 insns created by loop.c for prior loops don't have LUIDs.
1382 Rather than reject all such insns from movables, we just
1383 explicitly check each insn in the libcall (since invariant
1384 libcalls aren't that common). */
1385 for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1386 for (m1 = movables->head; m1 != m; m1 = m1->next)
1387 if (m1->insn == insn)
1388 m1->done = 1;
1393 /* For each movable insn, see if the reg that it loads
1394 leads when it dies right into another conditionally movable insn.
1395 If so, record that the second insn "forces" the first one,
1396 since the second can be moved only if the first is. */
1398 static void
1399 force_movables (movables)
1400 struct loop_movables *movables;
1402 struct movable *m, *m1;
1404 for (m1 = movables->head; m1; m1 = m1->next)
1405 /* Omit this if moving just the (SET (REG) 0) of a zero-extend. */
1406 if (!m1->partial && !m1->done)
1408 int regno = m1->regno;
1409 for (m = m1->next; m; m = m->next)
1410 /* ??? Could this be a bug? What if CSE caused the
1411 register of M1 to be used after this insn?
1412 Since CSE does not update regno_last_uid,
1413 this insn M->insn might not be where it dies.
1414 But very likely this doesn't matter; what matters is
1415 that M's reg is computed from M1's reg. */
1416 if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1417 && !m->done)
1418 break;
1419 if (m != 0 && m->set_src == m1->set_dest
1420 /* If m->consec, m->set_src isn't valid. */
1421 && m->consec == 0)
1422 m = 0;
1424 /* Increase the priority of the moving the first insn
1425 since it permits the second to be moved as well. */
1426 if (m != 0)
1428 m->forces = m1;
1429 m1->lifetime += m->lifetime;
1430 m1->savings += m->savings;
1435 /* Find invariant expressions that are equal and can be combined into
1436 one register. */
1438 static void
1439 combine_movables (movables, regs)
1440 struct loop_movables *movables;
1441 struct loop_regs *regs;
1443 struct movable *m;
1444 char *matched_regs = (char *) xmalloc (regs->num);
1445 enum machine_mode mode;
1447 /* Regs that are set more than once are not allowed to match
1448 or be matched. I'm no longer sure why not. */
1449 /* Perhaps testing m->consec_sets would be more appropriate here? */
1451 for (m = movables->head; m; m = m->next)
1452 if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1453 && !m->partial)
1455 struct movable *m1;
1456 int regno = m->regno;
1458 memset (matched_regs, 0, regs->num);
1459 matched_regs[regno] = 1;
1461 /* We want later insns to match the first one. Don't make the first
1462 one match any later ones. So start this loop at m->next. */
1463 for (m1 = m->next; m1; m1 = m1->next)
1464 /* ??? HACK! move_movables does not verify that the replacement
1465 is valid, which can have disasterous effects with hard regs
1466 and match_dup. Turn combination off for now. */
1467 if (0 && m != m1 && m1->match == 0
1468 && regs->array[m1->regno].n_times_set == 1
1469 /* A reg used outside the loop mustn't be eliminated. */
1470 && !m1->global
1471 /* A reg used for zero-extending mustn't be eliminated. */
1472 && !m1->partial
1473 && (matched_regs[m1->regno]
1476 /* Can combine regs with different modes loaded from the
1477 same constant only if the modes are the same or
1478 if both are integer modes with M wider or the same
1479 width as M1. The check for integer is redundant, but
1480 safe, since the only case of differing destination
1481 modes with equal sources is when both sources are
1482 VOIDmode, i.e., CONST_INT. */
1483 (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1484 || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1485 && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1486 && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1487 >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1488 /* See if the source of M1 says it matches M. */
1489 && ((GET_CODE (m1->set_src) == REG
1490 && matched_regs[REGNO (m1->set_src)])
1491 || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1492 movables, regs))))
1493 && ((m->dependencies == m1->dependencies)
1494 || rtx_equal_p (m->dependencies, m1->dependencies)))
1496 m->lifetime += m1->lifetime;
1497 m->savings += m1->savings;
1498 m1->done = 1;
1499 m1->match = m;
1500 matched_regs[m1->regno] = 1;
1504 /* Now combine the regs used for zero-extension.
1505 This can be done for those not marked `global'
1506 provided their lives don't overlap. */
1508 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1509 mode = GET_MODE_WIDER_MODE (mode))
1511 struct movable *m0 = 0;
1513 /* Combine all the registers for extension from mode MODE.
1514 Don't combine any that are used outside this loop. */
1515 for (m = movables->head; m; m = m->next)
1516 if (m->partial && ! m->global
1517 && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1519 struct movable *m1;
1521 int first = REGNO_FIRST_LUID (m->regno);
1522 int last = REGNO_LAST_LUID (m->regno);
1524 if (m0 == 0)
1526 /* First one: don't check for overlap, just record it. */
1527 m0 = m;
1528 continue;
1531 /* Make sure they extend to the same mode.
1532 (Almost always true.) */
1533 if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1534 continue;
1536 /* We already have one: check for overlap with those
1537 already combined together. */
1538 for (m1 = movables->head; m1 != m; m1 = m1->next)
1539 if (m1 == m0 || (m1->partial && m1->match == m0))
1540 if (! (REGNO_FIRST_LUID (m1->regno) > last
1541 || REGNO_LAST_LUID (m1->regno) < first))
1542 goto overlap;
1544 /* No overlap: we can combine this with the others. */
1545 m0->lifetime += m->lifetime;
1546 m0->savings += m->savings;
1547 m->done = 1;
1548 m->match = m0;
1550 overlap:
1555 /* Clean up. */
1556 free (matched_regs);
1559 /* Returns the number of movable instructions in LOOP that were not
1560 moved outside the loop. */
1562 static int
1563 num_unmoved_movables (loop)
1564 const struct loop *loop;
1566 int num = 0;
1567 struct movable *m;
1569 for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1570 if (!m->done)
1571 ++num;
1573 return num;
1577 /* Return 1 if regs X and Y will become the same if moved. */
1579 static int
1580 regs_match_p (x, y, movables)
1581 rtx x, y;
1582 struct loop_movables *movables;
1584 unsigned int xn = REGNO (x);
1585 unsigned int yn = REGNO (y);
1586 struct movable *mx, *my;
1588 for (mx = movables->head; mx; mx = mx->next)
1589 if (mx->regno == xn)
1590 break;
1592 for (my = movables->head; my; my = my->next)
1593 if (my->regno == yn)
1594 break;
1596 return (mx && my
1597 && ((mx->match == my->match && mx->match != 0)
1598 || mx->match == my
1599 || mx == my->match));
1602 /* Return 1 if X and Y are identical-looking rtx's.
1603 This is the Lisp function EQUAL for rtx arguments.
1605 If two registers are matching movables or a movable register and an
1606 equivalent constant, consider them equal. */
1608 static int
1609 rtx_equal_for_loop_p (x, y, movables, regs)
1610 rtx x, y;
1611 struct loop_movables *movables;
1612 struct loop_regs *regs;
1614 int i;
1615 int j;
1616 struct movable *m;
1617 enum rtx_code code;
1618 const char *fmt;
1620 if (x == y)
1621 return 1;
1622 if (x == 0 || y == 0)
1623 return 0;
1625 code = GET_CODE (x);
1627 /* If we have a register and a constant, they may sometimes be
1628 equal. */
1629 if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1630 && CONSTANT_P (y))
1632 for (m = movables->head; m; m = m->next)
1633 if (m->move_insn && m->regno == REGNO (x)
1634 && rtx_equal_p (m->set_src, y))
1635 return 1;
1637 else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1638 && CONSTANT_P (x))
1640 for (m = movables->head; m; m = m->next)
1641 if (m->move_insn && m->regno == REGNO (y)
1642 && rtx_equal_p (m->set_src, x))
1643 return 1;
1646 /* Otherwise, rtx's of different codes cannot be equal. */
1647 if (code != GET_CODE (y))
1648 return 0;
1650 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1651 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1653 if (GET_MODE (x) != GET_MODE (y))
1654 return 0;
1656 /* These three types of rtx's can be compared nonrecursively. */
1657 if (code == REG)
1658 return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1660 if (code == LABEL_REF)
1661 return XEXP (x, 0) == XEXP (y, 0);
1662 if (code == SYMBOL_REF)
1663 return XSTR (x, 0) == XSTR (y, 0);
1665 /* Compare the elements. If any pair of corresponding elements
1666 fail to match, return 0 for the whole things. */
1668 fmt = GET_RTX_FORMAT (code);
1669 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1671 switch (fmt[i])
1673 case 'w':
1674 if (XWINT (x, i) != XWINT (y, i))
1675 return 0;
1676 break;
1678 case 'i':
1679 if (XINT (x, i) != XINT (y, i))
1680 return 0;
1681 break;
1683 case 'E':
1684 /* Two vectors must have the same length. */
1685 if (XVECLEN (x, i) != XVECLEN (y, i))
1686 return 0;
1688 /* And the corresponding elements must match. */
1689 for (j = 0; j < XVECLEN (x, i); j++)
1690 if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1691 movables, regs) == 0)
1692 return 0;
1693 break;
1695 case 'e':
1696 if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1697 == 0)
1698 return 0;
1699 break;
1701 case 's':
1702 if (strcmp (XSTR (x, i), XSTR (y, i)))
1703 return 0;
1704 break;
1706 case 'u':
1707 /* These are just backpointers, so they don't matter. */
1708 break;
1710 case '0':
1711 break;
1713 /* It is believed that rtx's at this level will never
1714 contain anything but integers and other rtx's,
1715 except for within LABEL_REFs and SYMBOL_REFs. */
1716 default:
1717 abort ();
1720 return 1;
1723 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1724 insns in INSNS which use the reference. LABEL_NUSES for CODE_LABEL
1725 references is incremented once for each added note. */
1727 static void
1728 add_label_notes (x, insns)
1729 rtx x;
1730 rtx insns;
1732 enum rtx_code code = GET_CODE (x);
1733 int i, j;
1734 const char *fmt;
1735 rtx insn;
1737 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1739 /* This code used to ignore labels that referred to dispatch tables to
1740 avoid flow generating (slighly) worse code.
1742 We no longer ignore such label references (see LABEL_REF handling in
1743 mark_jump_label for additional information). */
1744 for (insn = insns; insn; insn = NEXT_INSN (insn))
1745 if (reg_mentioned_p (XEXP (x, 0), insn))
1747 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
1748 REG_NOTES (insn));
1749 if (LABEL_P (XEXP (x, 0)))
1750 LABEL_NUSES (XEXP (x, 0))++;
1754 fmt = GET_RTX_FORMAT (code);
1755 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1757 if (fmt[i] == 'e')
1758 add_label_notes (XEXP (x, i), insns);
1759 else if (fmt[i] == 'E')
1760 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1761 add_label_notes (XVECEXP (x, i, j), insns);
1765 /* Scan MOVABLES, and move the insns that deserve to be moved.
1766 If two matching movables are combined, replace one reg with the
1767 other throughout. */
1769 static void
1770 move_movables (loop, movables, threshold, insn_count)
1771 struct loop *loop;
1772 struct loop_movables *movables;
1773 int threshold;
1774 int insn_count;
1776 struct loop_regs *regs = LOOP_REGS (loop);
1777 int nregs = regs->num;
1778 rtx new_start = 0;
1779 struct movable *m;
1780 rtx p;
1781 rtx loop_start = loop->start;
1782 rtx loop_end = loop->end;
1783 /* Map of pseudo-register replacements to handle combining
1784 when we move several insns that load the same value
1785 into different pseudo-registers. */
1786 rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
1787 char *already_moved = (char *) xcalloc (nregs, sizeof (char));
1789 for (m = movables->head; m; m = m->next)
1791 /* Describe this movable insn. */
1793 if (loop_dump_stream)
1795 fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1796 INSN_UID (m->insn), m->regno, m->lifetime);
1797 if (m->consec > 0)
1798 fprintf (loop_dump_stream, "consec %d, ", m->consec);
1799 if (m->cond)
1800 fprintf (loop_dump_stream, "cond ");
1801 if (m->force)
1802 fprintf (loop_dump_stream, "force ");
1803 if (m->global)
1804 fprintf (loop_dump_stream, "global ");
1805 if (m->done)
1806 fprintf (loop_dump_stream, "done ");
1807 if (m->move_insn)
1808 fprintf (loop_dump_stream, "move-insn ");
1809 if (m->match)
1810 fprintf (loop_dump_stream, "matches %d ",
1811 INSN_UID (m->match->insn));
1812 if (m->forces)
1813 fprintf (loop_dump_stream, "forces %d ",
1814 INSN_UID (m->forces->insn));
1817 /* Ignore the insn if it's already done (it matched something else).
1818 Otherwise, see if it is now safe to move. */
1820 if (!m->done
1821 && (! m->cond
1822 || (1 == loop_invariant_p (loop, m->set_src)
1823 && (m->dependencies == 0
1824 || 1 == loop_invariant_p (loop, m->dependencies))
1825 && (m->consec == 0
1826 || 1 == consec_sets_invariant_p (loop, m->set_dest,
1827 m->consec + 1,
1828 m->insn))))
1829 && (! m->forces || m->forces->done))
1831 int regno;
1832 rtx p;
1833 int savings = m->savings;
1835 /* We have an insn that is safe to move.
1836 Compute its desirability. */
1838 p = m->insn;
1839 regno = m->regno;
1841 if (loop_dump_stream)
1842 fprintf (loop_dump_stream, "savings %d ", savings);
1844 if (regs->array[regno].moved_once && loop_dump_stream)
1845 fprintf (loop_dump_stream, "halved since already moved ");
1847 /* An insn MUST be moved if we already moved something else
1848 which is safe only if this one is moved too: that is,
1849 if already_moved[REGNO] is nonzero. */
1851 /* An insn is desirable to move if the new lifetime of the
1852 register is no more than THRESHOLD times the old lifetime.
1853 If it's not desirable, it means the loop is so big
1854 that moving won't speed things up much,
1855 and it is liable to make register usage worse. */
1857 /* It is also desirable to move if it can be moved at no
1858 extra cost because something else was already moved. */
1860 if (already_moved[regno]
1861 || flag_move_all_movables
1862 || (threshold * savings * m->lifetime) >=
1863 (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1864 || (m->forces && m->forces->done
1865 && regs->array[m->forces->regno].n_times_set == 1))
1867 int count;
1868 struct movable *m1;
1869 rtx first = NULL_RTX;
1871 /* Now move the insns that set the reg. */
1873 if (m->partial && m->match)
1875 rtx newpat, i1;
1876 rtx r1, r2;
1877 /* Find the end of this chain of matching regs.
1878 Thus, we load each reg in the chain from that one reg.
1879 And that reg is loaded with 0 directly,
1880 since it has ->match == 0. */
1881 for (m1 = m; m1->match; m1 = m1->match);
1882 newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1883 SET_DEST (PATTERN (m1->insn)));
1884 i1 = loop_insn_hoist (loop, newpat);
1886 /* Mark the moved, invariant reg as being allowed to
1887 share a hard reg with the other matching invariant. */
1888 REG_NOTES (i1) = REG_NOTES (m->insn);
1889 r1 = SET_DEST (PATTERN (m->insn));
1890 r2 = SET_DEST (PATTERN (m1->insn));
1891 regs_may_share
1892 = gen_rtx_EXPR_LIST (VOIDmode, r1,
1893 gen_rtx_EXPR_LIST (VOIDmode, r2,
1894 regs_may_share));
1895 delete_insn (m->insn);
1897 if (new_start == 0)
1898 new_start = i1;
1900 if (loop_dump_stream)
1901 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1903 /* If we are to re-generate the item being moved with a
1904 new move insn, first delete what we have and then emit
1905 the move insn before the loop. */
1906 else if (m->move_insn)
1908 rtx i1, temp, seq;
1910 for (count = m->consec; count >= 0; count--)
1912 /* If this is the first insn of a library call sequence,
1913 skip to the end. */
1914 if (GET_CODE (p) != NOTE
1915 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1916 p = XEXP (temp, 0);
1918 /* If this is the last insn of a libcall sequence, then
1919 delete every insn in the sequence except the last.
1920 The last insn is handled in the normal manner. */
1921 if (GET_CODE (p) != NOTE
1922 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1924 temp = XEXP (temp, 0);
1925 while (temp != p)
1926 temp = delete_insn (temp);
1929 temp = p;
1930 p = delete_insn (p);
1932 /* simplify_giv_expr expects that it can walk the insns
1933 at m->insn forwards and see this old sequence we are
1934 tossing here. delete_insn does preserve the next
1935 pointers, but when we skip over a NOTE we must fix
1936 it up. Otherwise that code walks into the non-deleted
1937 insn stream. */
1938 while (p && GET_CODE (p) == NOTE)
1939 p = NEXT_INSN (temp) = NEXT_INSN (p);
1942 start_sequence ();
1943 emit_move_insn (m->set_dest, m->set_src);
1944 temp = get_insns ();
1945 seq = gen_sequence ();
1946 end_sequence ();
1948 add_label_notes (m->set_src, temp);
1950 i1 = loop_insn_hoist (loop, seq);
1951 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1952 set_unique_reg_note (i1,
1953 m->is_equiv ? REG_EQUIV : REG_EQUAL,
1954 m->set_src);
1956 if (loop_dump_stream)
1957 fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1959 /* The more regs we move, the less we like moving them. */
1960 threshold -= 3;
1962 else
1964 for (count = m->consec; count >= 0; count--)
1966 rtx i1, temp;
1968 /* If first insn of libcall sequence, skip to end. */
1969 /* Do this at start of loop, since p is guaranteed to
1970 be an insn here. */
1971 if (GET_CODE (p) != NOTE
1972 && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1973 p = XEXP (temp, 0);
1975 /* If last insn of libcall sequence, move all
1976 insns except the last before the loop. The last
1977 insn is handled in the normal manner. */
1978 if (GET_CODE (p) != NOTE
1979 && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1981 rtx fn_address = 0;
1982 rtx fn_reg = 0;
1983 rtx fn_address_insn = 0;
1985 first = 0;
1986 for (temp = XEXP (temp, 0); temp != p;
1987 temp = NEXT_INSN (temp))
1989 rtx body;
1990 rtx n;
1991 rtx next;
1993 if (GET_CODE (temp) == NOTE)
1994 continue;
1996 body = PATTERN (temp);
1998 /* Find the next insn after TEMP,
1999 not counting USE or NOTE insns. */
2000 for (next = NEXT_INSN (temp); next != p;
2001 next = NEXT_INSN (next))
2002 if (! (GET_CODE (next) == INSN
2003 && GET_CODE (PATTERN (next)) == USE)
2004 && GET_CODE (next) != NOTE)
2005 break;
2007 /* If that is the call, this may be the insn
2008 that loads the function address.
2010 Extract the function address from the insn
2011 that loads it into a register.
2012 If this insn was cse'd, we get incorrect code.
2014 So emit a new move insn that copies the
2015 function address into the register that the
2016 call insn will use. flow.c will delete any
2017 redundant stores that we have created. */
2018 if (GET_CODE (next) == CALL_INSN
2019 && GET_CODE (body) == SET
2020 && GET_CODE (SET_DEST (body)) == REG
2021 && (n = find_reg_note (temp, REG_EQUAL,
2022 NULL_RTX)))
2024 fn_reg = SET_SRC (body);
2025 if (GET_CODE (fn_reg) != REG)
2026 fn_reg = SET_DEST (body);
2027 fn_address = XEXP (n, 0);
2028 fn_address_insn = temp;
2030 /* We have the call insn.
2031 If it uses the register we suspect it might,
2032 load it with the correct address directly. */
2033 if (GET_CODE (temp) == CALL_INSN
2034 && fn_address != 0
2035 && reg_referenced_p (fn_reg, body))
2036 loop_insn_emit_after (loop, 0, fn_address_insn,
2037 gen_move_insn
2038 (fn_reg, fn_address));
2040 if (GET_CODE (temp) == CALL_INSN)
2042 i1 = loop_call_insn_hoist (loop, body);
2043 /* Because the USAGE information potentially
2044 contains objects other than hard registers
2045 we need to copy it. */
2046 if (CALL_INSN_FUNCTION_USAGE (temp))
2047 CALL_INSN_FUNCTION_USAGE (i1)
2048 = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2050 else
2051 i1 = loop_insn_hoist (loop, body);
2052 if (first == 0)
2053 first = i1;
2054 if (temp == fn_address_insn)
2055 fn_address_insn = i1;
2056 REG_NOTES (i1) = REG_NOTES (temp);
2057 REG_NOTES (temp) = NULL;
2058 delete_insn (temp);
2060 if (new_start == 0)
2061 new_start = first;
2063 if (m->savemode != VOIDmode)
2065 /* P sets REG to zero; but we should clear only
2066 the bits that are not covered by the mode
2067 m->savemode. */
2068 rtx reg = m->set_dest;
2069 rtx sequence;
2070 rtx tem;
2072 start_sequence ();
2073 tem = expand_simple_binop
2074 (GET_MODE (reg), AND, reg,
2075 GEN_INT ((((HOST_WIDE_INT) 1
2076 << GET_MODE_BITSIZE (m->savemode)))
2077 - 1),
2078 reg, 1, OPTAB_LIB_WIDEN);
2079 if (tem == 0)
2080 abort ();
2081 if (tem != reg)
2082 emit_move_insn (reg, tem);
2083 sequence = gen_sequence ();
2084 end_sequence ();
2085 i1 = loop_insn_hoist (loop, sequence);
2087 else if (GET_CODE (p) == CALL_INSN)
2089 i1 = loop_call_insn_hoist (loop, PATTERN (p));
2090 /* Because the USAGE information potentially
2091 contains objects other than hard registers
2092 we need to copy it. */
2093 if (CALL_INSN_FUNCTION_USAGE (p))
2094 CALL_INSN_FUNCTION_USAGE (i1)
2095 = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2097 else if (count == m->consec && m->move_insn_first)
2099 rtx seq;
2100 /* The SET_SRC might not be invariant, so we must
2101 use the REG_EQUAL note. */
2102 start_sequence ();
2103 emit_move_insn (m->set_dest, m->set_src);
2104 temp = get_insns ();
2105 seq = gen_sequence ();
2106 end_sequence ();
2108 add_label_notes (m->set_src, temp);
2110 i1 = loop_insn_hoist (loop, seq);
2111 if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2112 set_unique_reg_note (i1, m->is_equiv ? REG_EQUIV
2113 : REG_EQUAL, m->set_src);
2115 else
2116 i1 = loop_insn_hoist (loop, PATTERN (p));
2118 if (REG_NOTES (i1) == 0)
2120 REG_NOTES (i1) = REG_NOTES (p);
2121 REG_NOTES (p) = NULL;
2123 /* If there is a REG_EQUAL note present whose value
2124 is not loop invariant, then delete it, since it
2125 may cause problems with later optimization passes.
2126 It is possible for cse to create such notes
2127 like this as a result of record_jump_cond. */
2129 if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2130 && ! loop_invariant_p (loop, XEXP (temp, 0)))
2131 remove_note (i1, temp);
2134 if (new_start == 0)
2135 new_start = i1;
2137 if (loop_dump_stream)
2138 fprintf (loop_dump_stream, " moved to %d",
2139 INSN_UID (i1));
2141 /* If library call, now fix the REG_NOTES that contain
2142 insn pointers, namely REG_LIBCALL on FIRST
2143 and REG_RETVAL on I1. */
2144 if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2146 XEXP (temp, 0) = first;
2147 temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2148 XEXP (temp, 0) = i1;
2151 temp = p;
2152 delete_insn (p);
2153 p = NEXT_INSN (p);
2155 /* simplify_giv_expr expects that it can walk the insns
2156 at m->insn forwards and see this old sequence we are
2157 tossing here. delete_insn does preserve the next
2158 pointers, but when we skip over a NOTE we must fix
2159 it up. Otherwise that code walks into the non-deleted
2160 insn stream. */
2161 while (p && GET_CODE (p) == NOTE)
2162 p = NEXT_INSN (temp) = NEXT_INSN (p);
2165 /* The more regs we move, the less we like moving them. */
2166 threshold -= 3;
2169 /* Any other movable that loads the same register
2170 MUST be moved. */
2171 already_moved[regno] = 1;
2173 /* This reg has been moved out of one loop. */
2174 regs->array[regno].moved_once = 1;
2176 /* The reg set here is now invariant. */
2177 if (! m->partial)
2179 int i;
2180 for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2181 regs->array[regno+i].set_in_loop = 0;
2184 m->done = 1;
2186 /* Change the length-of-life info for the register
2187 to say it lives at least the full length of this loop.
2188 This will help guide optimizations in outer loops. */
2190 if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2191 /* This is the old insn before all the moved insns.
2192 We can't use the moved insn because it is out of range
2193 in uid_luid. Only the old insns have luids. */
2194 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2195 if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2196 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2198 /* Combine with this moved insn any other matching movables. */
2200 if (! m->partial)
2201 for (m1 = movables->head; m1; m1 = m1->next)
2202 if (m1->match == m)
2204 rtx temp;
2206 /* Schedule the reg loaded by M1
2207 for replacement so that shares the reg of M.
2208 If the modes differ (only possible in restricted
2209 circumstances, make a SUBREG.
2211 Note this assumes that the target dependent files
2212 treat REG and SUBREG equally, including within
2213 GO_IF_LEGITIMATE_ADDRESS and in all the
2214 predicates since we never verify that replacing the
2215 original register with a SUBREG results in a
2216 recognizable insn. */
2217 if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2218 reg_map[m1->regno] = m->set_dest;
2219 else
2220 reg_map[m1->regno]
2221 = gen_lowpart_common (GET_MODE (m1->set_dest),
2222 m->set_dest);
2224 /* Get rid of the matching insn
2225 and prevent further processing of it. */
2226 m1->done = 1;
2228 /* if library call, delete all insns. */
2229 if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2230 NULL_RTX)))
2231 delete_insn_chain (XEXP (temp, 0), m1->insn);
2232 else
2233 delete_insn (m1->insn);
2235 /* Any other movable that loads the same register
2236 MUST be moved. */
2237 already_moved[m1->regno] = 1;
2239 /* The reg merged here is now invariant,
2240 if the reg it matches is invariant. */
2241 if (! m->partial)
2243 int i;
2244 for (i = 0;
2245 i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2246 i++)
2247 regs->array[m1->regno+i].set_in_loop = 0;
2251 else if (loop_dump_stream)
2252 fprintf (loop_dump_stream, "not desirable");
2254 else if (loop_dump_stream && !m->match)
2255 fprintf (loop_dump_stream, "not safe");
2257 if (loop_dump_stream)
2258 fprintf (loop_dump_stream, "\n");
2261 if (new_start == 0)
2262 new_start = loop_start;
2264 /* Go through all the instructions in the loop, making
2265 all the register substitutions scheduled in REG_MAP. */
2266 for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2267 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2268 || GET_CODE (p) == CALL_INSN)
2270 replace_regs (PATTERN (p), reg_map, nregs, 0);
2271 replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2272 INSN_CODE (p) = -1;
2275 /* Clean up. */
2276 free (reg_map);
2277 free (already_moved);
2281 static void
2282 loop_movables_add (movables, m)
2283 struct loop_movables *movables;
2284 struct movable *m;
2286 if (movables->head == 0)
2287 movables->head = m;
2288 else
2289 movables->last->next = m;
2290 movables->last = m;
2294 static void
2295 loop_movables_free (movables)
2296 struct loop_movables *movables;
2298 struct movable *m;
2299 struct movable *m_next;
2301 for (m = movables->head; m; m = m_next)
2303 m_next = m->next;
2304 free (m);
2308 #if 0
2309 /* Scan X and replace the address of any MEM in it with ADDR.
2310 REG is the address that MEM should have before the replacement. */
2312 static void
2313 replace_call_address (x, reg, addr)
2314 rtx x, reg, addr;
2316 enum rtx_code code;
2317 int i;
2318 const char *fmt;
2320 if (x == 0)
2321 return;
2322 code = GET_CODE (x);
2323 switch (code)
2325 case PC:
2326 case CC0:
2327 case CONST_INT:
2328 case CONST_DOUBLE:
2329 case CONST:
2330 case SYMBOL_REF:
2331 case LABEL_REF:
2332 case REG:
2333 return;
2335 case SET:
2336 /* Short cut for very common case. */
2337 replace_call_address (XEXP (x, 1), reg, addr);
2338 return;
2340 case CALL:
2341 /* Short cut for very common case. */
2342 replace_call_address (XEXP (x, 0), reg, addr);
2343 return;
2345 case MEM:
2346 /* If this MEM uses a reg other than the one we expected,
2347 something is wrong. */
2348 if (XEXP (x, 0) != reg)
2349 abort ();
2350 XEXP (x, 0) = addr;
2351 return;
2353 default:
2354 break;
2357 fmt = GET_RTX_FORMAT (code);
2358 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2360 if (fmt[i] == 'e')
2361 replace_call_address (XEXP (x, i), reg, addr);
2362 else if (fmt[i] == 'E')
2364 int j;
2365 for (j = 0; j < XVECLEN (x, i); j++)
2366 replace_call_address (XVECEXP (x, i, j), reg, addr);
2370 #endif
2372 /* Return the number of memory refs to addresses that vary
2373 in the rtx X. */
2375 static int
2376 count_nonfixed_reads (loop, x)
2377 const struct loop *loop;
2378 rtx x;
2380 enum rtx_code code;
2381 int i;
2382 const char *fmt;
2383 int value;
2385 if (x == 0)
2386 return 0;
2388 code = GET_CODE (x);
2389 switch (code)
2391 case PC:
2392 case CC0:
2393 case CONST_INT:
2394 case CONST_DOUBLE:
2395 case CONST:
2396 case SYMBOL_REF:
2397 case LABEL_REF:
2398 case REG:
2399 return 0;
2401 case MEM:
2402 return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2403 + count_nonfixed_reads (loop, XEXP (x, 0)));
2405 default:
2406 break;
2409 value = 0;
2410 fmt = GET_RTX_FORMAT (code);
2411 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2413 if (fmt[i] == 'e')
2414 value += count_nonfixed_reads (loop, XEXP (x, i));
2415 if (fmt[i] == 'E')
2417 int j;
2418 for (j = 0; j < XVECLEN (x, i); j++)
2419 value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2422 return value;
2425 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2426 `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2427 `unknown_address_altered', `unknown_constant_address_altered', and
2428 `num_mem_sets' in LOOP. Also, fill in the array `mems' and the
2429 list `store_mems' in LOOP. */
2431 static void
2432 prescan_loop (loop)
2433 struct loop *loop;
2435 int level = 1;
2436 rtx insn;
2437 struct loop_info *loop_info = LOOP_INFO (loop);
2438 rtx start = loop->start;
2439 rtx end = loop->end;
2440 /* The label after END. Jumping here is just like falling off the
2441 end of the loop. We use next_nonnote_insn instead of next_label
2442 as a hedge against the (pathological) case where some actual insn
2443 might end up between the two. */
2444 rtx exit_target = next_nonnote_insn (end);
2446 loop_info->has_indirect_jump = indirect_jump_in_function;
2447 loop_info->pre_header_has_call = 0;
2448 loop_info->has_call = 0;
2449 loop_info->has_nonconst_call = 0;
2450 loop_info->has_volatile = 0;
2451 loop_info->has_tablejump = 0;
2452 loop_info->has_multiple_exit_targets = 0;
2453 loop->level = 1;
2455 loop_info->unknown_address_altered = 0;
2456 loop_info->unknown_constant_address_altered = 0;
2457 loop_info->store_mems = NULL_RTX;
2458 loop_info->first_loop_store_insn = NULL_RTX;
2459 loop_info->mems_idx = 0;
2460 loop_info->num_mem_sets = 0;
2463 for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2464 insn = PREV_INSN (insn))
2466 if (GET_CODE (insn) == CALL_INSN)
2468 loop_info->pre_header_has_call = 1;
2469 break;
2473 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2474 insn = NEXT_INSN (insn))
2476 switch (GET_CODE (insn))
2478 case NOTE:
2479 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2481 ++level;
2482 /* Count number of loops contained in this one. */
2483 loop->level++;
2485 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2486 --level;
2487 break;
2489 case CALL_INSN:
2490 if (! CONST_OR_PURE_CALL_P (insn))
2492 loop_info->unknown_address_altered = 1;
2493 loop_info->has_nonconst_call = 1;
2495 loop_info->has_call = 1;
2496 if (can_throw_internal (insn))
2497 loop_info->has_multiple_exit_targets = 1;
2498 break;
2500 case JUMP_INSN:
2501 if (! loop_info->has_multiple_exit_targets)
2503 rtx set = pc_set (insn);
2505 if (set)
2507 rtx src = SET_SRC (set);
2508 rtx label1, label2;
2510 if (GET_CODE (src) == IF_THEN_ELSE)
2512 label1 = XEXP (src, 1);
2513 label2 = XEXP (src, 2);
2515 else
2517 label1 = src;
2518 label2 = NULL_RTX;
2523 if (label1 && label1 != pc_rtx)
2525 if (GET_CODE (label1) != LABEL_REF)
2527 /* Something tricky. */
2528 loop_info->has_multiple_exit_targets = 1;
2529 break;
2531 else if (XEXP (label1, 0) != exit_target
2532 && LABEL_OUTSIDE_LOOP_P (label1))
2534 /* A jump outside the current loop. */
2535 loop_info->has_multiple_exit_targets = 1;
2536 break;
2540 label1 = label2;
2541 label2 = NULL_RTX;
2543 while (label1);
2545 else
2547 /* A return, or something tricky. */
2548 loop_info->has_multiple_exit_targets = 1;
2551 /* FALLTHRU */
2553 case INSN:
2554 if (volatile_refs_p (PATTERN (insn)))
2555 loop_info->has_volatile = 1;
2557 if (GET_CODE (insn) == JUMP_INSN
2558 && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2559 || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2560 loop_info->has_tablejump = 1;
2562 note_stores (PATTERN (insn), note_addr_stored, loop_info);
2563 if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2564 loop_info->first_loop_store_insn = insn;
2566 if (flag_non_call_exceptions && can_throw_internal (insn))
2567 loop_info->has_multiple_exit_targets = 1;
2568 break;
2570 default:
2571 break;
2575 /* Now, rescan the loop, setting up the LOOP_MEMS array. */
2576 if (/* An exception thrown by a called function might land us
2577 anywhere. */
2578 ! loop_info->has_nonconst_call
2579 /* We don't want loads for MEMs moved to a location before the
2580 one at which their stack memory becomes allocated. (Note
2581 that this is not a problem for malloc, etc., since those
2582 require actual function calls. */
2583 && ! current_function_calls_alloca
2584 /* There are ways to leave the loop other than falling off the
2585 end. */
2586 && ! loop_info->has_multiple_exit_targets)
2587 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2588 insn = NEXT_INSN (insn))
2589 for_each_rtx (&insn, insert_loop_mem, loop_info);
2591 /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2592 that loop_invariant_p and load_mems can use true_dependence
2593 to determine what is really clobbered. */
2594 if (loop_info->unknown_address_altered)
2596 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2598 loop_info->store_mems
2599 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2601 if (loop_info->unknown_constant_address_altered)
2603 rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2605 RTX_UNCHANGING_P (mem) = 1;
2606 loop_info->store_mems
2607 = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2611 /* Scan the function looking for loops. Record the start and end of each loop.
2612 Also mark as invalid loops any loops that contain a setjmp or are branched
2613 to from outside the loop. */
2615 static void
2616 find_and_verify_loops (f, loops)
2617 rtx f;
2618 struct loops *loops;
2620 rtx insn;
2621 rtx label;
2622 int num_loops;
2623 struct loop *current_loop;
2624 struct loop *next_loop;
2625 struct loop *loop;
2627 num_loops = loops->num;
2629 compute_luids (f, NULL_RTX, 0);
2631 /* If there are jumps to undefined labels,
2632 treat them as jumps out of any/all loops.
2633 This also avoids writing past end of tables when there are no loops. */
2634 uid_loop[0] = NULL;
2636 /* Find boundaries of loops, mark which loops are contained within
2637 loops, and invalidate loops that have setjmp. */
2639 num_loops = 0;
2640 current_loop = NULL;
2641 for (insn = f; insn; insn = NEXT_INSN (insn))
2643 if (GET_CODE (insn) == NOTE)
2644 switch (NOTE_LINE_NUMBER (insn))
2646 case NOTE_INSN_LOOP_BEG:
2647 next_loop = loops->array + num_loops;
2648 next_loop->num = num_loops;
2649 num_loops++;
2650 next_loop->start = insn;
2651 next_loop->outer = current_loop;
2652 current_loop = next_loop;
2653 break;
2655 case NOTE_INSN_LOOP_CONT:
2656 current_loop->cont = insn;
2657 break;
2659 case NOTE_INSN_LOOP_VTOP:
2660 current_loop->vtop = insn;
2661 break;
2663 case NOTE_INSN_LOOP_END:
2664 if (! current_loop)
2665 abort ();
2667 current_loop->end = insn;
2668 current_loop = current_loop->outer;
2669 break;
2671 default:
2672 break;
2675 if (GET_CODE (insn) == CALL_INSN
2676 && find_reg_note (insn, REG_SETJMP, NULL))
2678 /* In this case, we must invalidate our current loop and any
2679 enclosing loop. */
2680 for (loop = current_loop; loop; loop = loop->outer)
2682 loop->invalid = 1;
2683 if (loop_dump_stream)
2684 fprintf (loop_dump_stream,
2685 "\nLoop at %d ignored due to setjmp.\n",
2686 INSN_UID (loop->start));
2690 /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2691 enclosing loop, but this doesn't matter. */
2692 uid_loop[INSN_UID (insn)] = current_loop;
2695 /* Any loop containing a label used in an initializer must be invalidated,
2696 because it can be jumped into from anywhere. */
2698 for (label = forced_labels; label; label = XEXP (label, 1))
2700 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2701 loop; loop = loop->outer)
2702 loop->invalid = 1;
2705 /* Any loop containing a label used for an exception handler must be
2706 invalidated, because it can be jumped into from anywhere. */
2708 for (label = exception_handler_labels; label; label = XEXP (label, 1))
2710 for (loop = uid_loop[INSN_UID (XEXP (label, 0))];
2711 loop; loop = loop->outer)
2712 loop->invalid = 1;
2715 /* Now scan all insn's in the function. If any JUMP_INSN branches into a
2716 loop that it is not contained within, that loop is marked invalid.
2717 If any INSN or CALL_INSN uses a label's address, then the loop containing
2718 that label is marked invalid, because it could be jumped into from
2719 anywhere.
2721 Also look for blocks of code ending in an unconditional branch that
2722 exits the loop. If such a block is surrounded by a conditional
2723 branch around the block, move the block elsewhere (see below) and
2724 invert the jump to point to the code block. This may eliminate a
2725 label in our loop and will simplify processing by both us and a
2726 possible second cse pass. */
2728 for (insn = f; insn; insn = NEXT_INSN (insn))
2729 if (INSN_P (insn))
2731 struct loop *this_loop = uid_loop[INSN_UID (insn)];
2733 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2735 rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2736 if (note)
2738 for (loop = uid_loop[INSN_UID (XEXP (note, 0))];
2739 loop; loop = loop->outer)
2740 loop->invalid = 1;
2744 if (GET_CODE (insn) != JUMP_INSN)
2745 continue;
2747 mark_loop_jump (PATTERN (insn), this_loop);
2749 /* See if this is an unconditional branch outside the loop. */
2750 if (this_loop
2751 && (GET_CODE (PATTERN (insn)) == RETURN
2752 || (any_uncondjump_p (insn)
2753 && onlyjump_p (insn)
2754 && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2755 != this_loop)))
2756 && get_max_uid () < max_uid_for_loop)
2758 rtx p;
2759 rtx our_next = next_real_insn (insn);
2760 rtx last_insn_to_move = NEXT_INSN (insn);
2761 struct loop *dest_loop;
2762 struct loop *outer_loop = NULL;
2764 /* Go backwards until we reach the start of the loop, a label,
2765 or a JUMP_INSN. */
2766 for (p = PREV_INSN (insn);
2767 GET_CODE (p) != CODE_LABEL
2768 && ! (GET_CODE (p) == NOTE
2769 && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2770 && GET_CODE (p) != JUMP_INSN;
2771 p = PREV_INSN (p))
2774 /* Check for the case where we have a jump to an inner nested
2775 loop, and do not perform the optimization in that case. */
2777 if (JUMP_LABEL (insn))
2779 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2780 if (dest_loop)
2782 for (outer_loop = dest_loop; outer_loop;
2783 outer_loop = outer_loop->outer)
2784 if (outer_loop == this_loop)
2785 break;
2789 /* Make sure that the target of P is within the current loop. */
2791 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2792 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2793 outer_loop = this_loop;
2795 /* If we stopped on a JUMP_INSN to the next insn after INSN,
2796 we have a block of code to try to move.
2798 We look backward and then forward from the target of INSN
2799 to find a BARRIER at the same loop depth as the target.
2800 If we find such a BARRIER, we make a new label for the start
2801 of the block, invert the jump in P and point it to that label,
2802 and move the block of code to the spot we found. */
2804 if (! outer_loop
2805 && GET_CODE (p) == JUMP_INSN
2806 && JUMP_LABEL (p) != 0
2807 /* Just ignore jumps to labels that were never emitted.
2808 These always indicate compilation errors. */
2809 && INSN_UID (JUMP_LABEL (p)) != 0
2810 && any_condjump_p (p) && onlyjump_p (p)
2811 && next_real_insn (JUMP_LABEL (p)) == our_next
2812 /* If it's not safe to move the sequence, then we
2813 mustn't try. */
2814 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2815 &last_insn_to_move))
2817 rtx target
2818 = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2819 struct loop *target_loop = uid_loop[INSN_UID (target)];
2820 rtx loc, loc2;
2821 rtx tmp;
2823 /* Search for possible garbage past the conditional jumps
2824 and look for the last barrier. */
2825 for (tmp = last_insn_to_move;
2826 tmp && GET_CODE (tmp) != CODE_LABEL; tmp = NEXT_INSN (tmp))
2827 if (GET_CODE (tmp) == BARRIER)
2828 last_insn_to_move = tmp;
2830 for (loc = target; loc; loc = PREV_INSN (loc))
2831 if (GET_CODE (loc) == BARRIER
2832 /* Don't move things inside a tablejump. */
2833 && ((loc2 = next_nonnote_insn (loc)) == 0
2834 || GET_CODE (loc2) != CODE_LABEL
2835 || (loc2 = next_nonnote_insn (loc2)) == 0
2836 || GET_CODE (loc2) != JUMP_INSN
2837 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2838 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2839 && uid_loop[INSN_UID (loc)] == target_loop)
2840 break;
2842 if (loc == 0)
2843 for (loc = target; loc; loc = NEXT_INSN (loc))
2844 if (GET_CODE (loc) == BARRIER
2845 /* Don't move things inside a tablejump. */
2846 && ((loc2 = next_nonnote_insn (loc)) == 0
2847 || GET_CODE (loc2) != CODE_LABEL
2848 || (loc2 = next_nonnote_insn (loc2)) == 0
2849 || GET_CODE (loc2) != JUMP_INSN
2850 || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2851 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2852 && uid_loop[INSN_UID (loc)] == target_loop)
2853 break;
2855 if (loc)
2857 rtx cond_label = JUMP_LABEL (p);
2858 rtx new_label = get_label_after (p);
2860 /* Ensure our label doesn't go away. */
2861 LABEL_NUSES (cond_label)++;
2863 /* Verify that uid_loop is large enough and that
2864 we can invert P. */
2865 if (invert_jump (p, new_label, 1))
2867 rtx q, r;
2869 /* If no suitable BARRIER was found, create a suitable
2870 one before TARGET. Since TARGET is a fall through
2871 path, we'll need to insert an jump around our block
2872 and add a BARRIER before TARGET.
2874 This creates an extra unconditional jump outside
2875 the loop. However, the benefits of removing rarely
2876 executed instructions from inside the loop usually
2877 outweighs the cost of the extra unconditional jump
2878 outside the loop. */
2879 if (loc == 0)
2881 rtx temp;
2883 temp = gen_jump (JUMP_LABEL (insn));
2884 temp = emit_jump_insn_before (temp, target);
2885 JUMP_LABEL (temp) = JUMP_LABEL (insn);
2886 LABEL_NUSES (JUMP_LABEL (insn))++;
2887 loc = emit_barrier_before (target);
2890 /* Include the BARRIER after INSN and copy the
2891 block after LOC. */
2892 if (squeeze_notes (&new_label, &last_insn_to_move))
2893 abort ();
2894 reorder_insns (new_label, last_insn_to_move, loc);
2896 /* All those insns are now in TARGET_LOOP. */
2897 for (q = new_label;
2898 q != NEXT_INSN (last_insn_to_move);
2899 q = NEXT_INSN (q))
2900 uid_loop[INSN_UID (q)] = target_loop;
2902 /* The label jumped to by INSN is no longer a loop
2903 exit. Unless INSN does not have a label (e.g.,
2904 it is a RETURN insn), search loop->exit_labels
2905 to find its label_ref, and remove it. Also turn
2906 off LABEL_OUTSIDE_LOOP_P bit. */
2907 if (JUMP_LABEL (insn))
2909 for (q = 0, r = this_loop->exit_labels;
2911 q = r, r = LABEL_NEXTREF (r))
2912 if (XEXP (r, 0) == JUMP_LABEL (insn))
2914 LABEL_OUTSIDE_LOOP_P (r) = 0;
2915 if (q)
2916 LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2917 else
2918 this_loop->exit_labels = LABEL_NEXTREF (r);
2919 break;
2922 for (loop = this_loop; loop && loop != target_loop;
2923 loop = loop->outer)
2924 loop->exit_count--;
2926 /* If we didn't find it, then something is
2927 wrong. */
2928 if (! r)
2929 abort ();
2932 /* P is now a jump outside the loop, so it must be put
2933 in loop->exit_labels, and marked as such.
2934 The easiest way to do this is to just call
2935 mark_loop_jump again for P. */
2936 mark_loop_jump (PATTERN (p), this_loop);
2938 /* If INSN now jumps to the insn after it,
2939 delete INSN. */
2940 if (JUMP_LABEL (insn) != 0
2941 && (next_real_insn (JUMP_LABEL (insn))
2942 == next_real_insn (insn)))
2943 delete_related_insns (insn);
2946 /* Continue the loop after where the conditional
2947 branch used to jump, since the only branch insn
2948 in the block (if it still remains) is an inter-loop
2949 branch and hence needs no processing. */
2950 insn = NEXT_INSN (cond_label);
2952 if (--LABEL_NUSES (cond_label) == 0)
2953 delete_related_insns (cond_label);
2955 /* This loop will be continued with NEXT_INSN (insn). */
2956 insn = PREV_INSN (insn);
2963 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2964 loops it is contained in, mark the target loop invalid.
2966 For speed, we assume that X is part of a pattern of a JUMP_INSN. */
2968 static void
2969 mark_loop_jump (x, loop)
2970 rtx x;
2971 struct loop *loop;
2973 struct loop *dest_loop;
2974 struct loop *outer_loop;
2975 int i;
2977 switch (GET_CODE (x))
2979 case PC:
2980 case USE:
2981 case CLOBBER:
2982 case REG:
2983 case MEM:
2984 case CONST_INT:
2985 case CONST_DOUBLE:
2986 case RETURN:
2987 return;
2989 case CONST:
2990 /* There could be a label reference in here. */
2991 mark_loop_jump (XEXP (x, 0), loop);
2992 return;
2994 case PLUS:
2995 case MINUS:
2996 case MULT:
2997 mark_loop_jump (XEXP (x, 0), loop);
2998 mark_loop_jump (XEXP (x, 1), loop);
2999 return;
3001 case LO_SUM:
3002 /* This may refer to a LABEL_REF or SYMBOL_REF. */
3003 mark_loop_jump (XEXP (x, 1), loop);
3004 return;
3006 case SIGN_EXTEND:
3007 case ZERO_EXTEND:
3008 mark_loop_jump (XEXP (x, 0), loop);
3009 return;
3011 case LABEL_REF:
3012 dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3014 /* Link together all labels that branch outside the loop. This
3015 is used by final_[bg]iv_value and the loop unrolling code. Also
3016 mark this LABEL_REF so we know that this branch should predict
3017 false. */
3019 /* A check to make sure the label is not in an inner nested loop,
3020 since this does not count as a loop exit. */
3021 if (dest_loop)
3023 for (outer_loop = dest_loop; outer_loop;
3024 outer_loop = outer_loop->outer)
3025 if (outer_loop == loop)
3026 break;
3028 else
3029 outer_loop = NULL;
3031 if (loop && ! outer_loop)
3033 LABEL_OUTSIDE_LOOP_P (x) = 1;
3034 LABEL_NEXTREF (x) = loop->exit_labels;
3035 loop->exit_labels = x;
3037 for (outer_loop = loop;
3038 outer_loop && outer_loop != dest_loop;
3039 outer_loop = outer_loop->outer)
3040 outer_loop->exit_count++;
3043 /* If this is inside a loop, but not in the current loop or one enclosed
3044 by it, it invalidates at least one loop. */
3046 if (! dest_loop)
3047 return;
3049 /* We must invalidate every nested loop containing the target of this
3050 label, except those that also contain the jump insn. */
3052 for (; dest_loop; dest_loop = dest_loop->outer)
3054 /* Stop when we reach a loop that also contains the jump insn. */
3055 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3056 if (dest_loop == outer_loop)
3057 return;
3059 /* If we get here, we know we need to invalidate a loop. */
3060 if (loop_dump_stream && ! dest_loop->invalid)
3061 fprintf (loop_dump_stream,
3062 "\nLoop at %d ignored due to multiple entry points.\n",
3063 INSN_UID (dest_loop->start));
3065 dest_loop->invalid = 1;
3067 return;
3069 case SET:
3070 /* If this is not setting pc, ignore. */
3071 if (SET_DEST (x) == pc_rtx)
3072 mark_loop_jump (SET_SRC (x), loop);
3073 return;
3075 case IF_THEN_ELSE:
3076 mark_loop_jump (XEXP (x, 1), loop);
3077 mark_loop_jump (XEXP (x, 2), loop);
3078 return;
3080 case PARALLEL:
3081 case ADDR_VEC:
3082 for (i = 0; i < XVECLEN (x, 0); i++)
3083 mark_loop_jump (XVECEXP (x, 0, i), loop);
3084 return;
3086 case ADDR_DIFF_VEC:
3087 for (i = 0; i < XVECLEN (x, 1); i++)
3088 mark_loop_jump (XVECEXP (x, 1, i), loop);
3089 return;
3091 default:
3092 /* Strictly speaking this is not a jump into the loop, only a possible
3093 jump out of the loop. However, we have no way to link the destination
3094 of this jump onto the list of exit labels. To be safe we mark this
3095 loop and any containing loops as invalid. */
3096 if (loop)
3098 for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3100 if (loop_dump_stream && ! outer_loop->invalid)
3101 fprintf (loop_dump_stream,
3102 "\nLoop at %d ignored due to unknown exit jump.\n",
3103 INSN_UID (outer_loop->start));
3104 outer_loop->invalid = 1;
3107 return;
3111 /* Return nonzero if there is a label in the range from
3112 insn INSN to and including the insn whose luid is END
3113 INSN must have an assigned luid (i.e., it must not have
3114 been previously created by loop.c). */
3116 static int
3117 labels_in_range_p (insn, end)
3118 rtx insn;
3119 int end;
3121 while (insn && INSN_LUID (insn) <= end)
3123 if (GET_CODE (insn) == CODE_LABEL)
3124 return 1;
3125 insn = NEXT_INSN (insn);
3128 return 0;
3131 /* Record that a memory reference X is being set. */
3133 static void
3134 note_addr_stored (x, y, data)
3135 rtx x;
3136 rtx y ATTRIBUTE_UNUSED;
3137 void *data ATTRIBUTE_UNUSED;
3139 struct loop_info *loop_info = data;
3141 if (x == 0 || GET_CODE (x) != MEM)
3142 return;
3144 /* Count number of memory writes.
3145 This affects heuristics in strength_reduce. */
3146 loop_info->num_mem_sets++;
3148 /* BLKmode MEM means all memory is clobbered. */
3149 if (GET_MODE (x) == BLKmode)
3151 if (RTX_UNCHANGING_P (x))
3152 loop_info->unknown_constant_address_altered = 1;
3153 else
3154 loop_info->unknown_address_altered = 1;
3156 return;
3159 loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3160 loop_info->store_mems);
3163 /* X is a value modified by an INSN that references a biv inside a loop
3164 exit test (ie, X is somehow related to the value of the biv). If X
3165 is a pseudo that is used more than once, then the biv is (effectively)
3166 used more than once. DATA is a pointer to a loop_regs structure. */
3168 static void
3169 note_set_pseudo_multiple_uses (x, y, data)
3170 rtx x;
3171 rtx y ATTRIBUTE_UNUSED;
3172 void *data;
3174 struct loop_regs *regs = (struct loop_regs *) data;
3176 if (x == 0)
3177 return;
3179 while (GET_CODE (x) == STRICT_LOW_PART
3180 || GET_CODE (x) == SIGN_EXTRACT
3181 || GET_CODE (x) == ZERO_EXTRACT
3182 || GET_CODE (x) == SUBREG)
3183 x = XEXP (x, 0);
3185 if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3186 return;
3188 /* If we do not have usage information, or if we know the register
3189 is used more than once, note that fact for check_dbra_loop. */
3190 if (REGNO (x) >= max_reg_before_loop
3191 || ! regs->array[REGNO (x)].single_usage
3192 || regs->array[REGNO (x)].single_usage == const0_rtx)
3193 regs->multiple_uses = 1;
3196 /* Return nonzero if the rtx X is invariant over the current loop.
3198 The value is 2 if we refer to something only conditionally invariant.
3200 A memory ref is invariant if it is not volatile and does not conflict
3201 with anything stored in `loop_info->store_mems'. */
3204 loop_invariant_p (loop, x)
3205 const struct loop *loop;
3206 rtx x;
3208 struct loop_info *loop_info = LOOP_INFO (loop);
3209 struct loop_regs *regs = LOOP_REGS (loop);
3210 int i;
3211 enum rtx_code code;
3212 const char *fmt;
3213 int conditional = 0;
3214 rtx mem_list_entry;
3216 if (x == 0)
3217 return 1;
3218 code = GET_CODE (x);
3219 switch (code)
3221 case CONST_INT:
3222 case CONST_DOUBLE:
3223 case SYMBOL_REF:
3224 case CONST:
3225 return 1;
3227 case LABEL_REF:
3228 /* A LABEL_REF is normally invariant, however, if we are unrolling
3229 loops, and this label is inside the loop, then it isn't invariant.
3230 This is because each unrolled copy of the loop body will have
3231 a copy of this label. If this was invariant, then an insn loading
3232 the address of this label into a register might get moved outside
3233 the loop, and then each loop body would end up using the same label.
3235 We don't know the loop bounds here though, so just fail for all
3236 labels. */
3237 if (flag_unroll_loops)
3238 return 0;
3239 else
3240 return 1;
3242 case PC:
3243 case CC0:
3244 case UNSPEC_VOLATILE:
3245 return 0;
3247 case REG:
3248 /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3249 since the reg might be set by initialization within the loop. */
3251 if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3252 || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3253 && ! current_function_has_nonlocal_goto)
3254 return 1;
3256 if (LOOP_INFO (loop)->has_call
3257 && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3258 return 0;
3260 if (regs->array[REGNO (x)].set_in_loop < 0)
3261 return 2;
3263 return regs->array[REGNO (x)].set_in_loop == 0;
3265 case MEM:
3266 /* Volatile memory references must be rejected. Do this before
3267 checking for read-only items, so that volatile read-only items
3268 will be rejected also. */
3269 if (MEM_VOLATILE_P (x))
3270 return 0;
3272 /* See if there is any dependence between a store and this load. */
3273 mem_list_entry = loop_info->store_mems;
3274 while (mem_list_entry)
3276 if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3277 x, rtx_varies_p))
3278 return 0;
3280 mem_list_entry = XEXP (mem_list_entry, 1);
3283 /* It's not invalidated by a store in memory
3284 but we must still verify the address is invariant. */
3285 break;
3287 case ASM_OPERANDS:
3288 /* Don't mess with insns declared volatile. */
3289 if (MEM_VOLATILE_P (x))
3290 return 0;
3291 break;
3293 default:
3294 break;
3297 fmt = GET_RTX_FORMAT (code);
3298 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3300 if (fmt[i] == 'e')
3302 int tem = loop_invariant_p (loop, XEXP (x, i));
3303 if (tem == 0)
3304 return 0;
3305 if (tem == 2)
3306 conditional = 1;
3308 else if (fmt[i] == 'E')
3310 int j;
3311 for (j = 0; j < XVECLEN (x, i); j++)
3313 int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3314 if (tem == 0)
3315 return 0;
3316 if (tem == 2)
3317 conditional = 1;
3323 return 1 + conditional;
3326 /* Return nonzero if all the insns in the loop that set REG
3327 are INSN and the immediately following insns,
3328 and if each of those insns sets REG in an invariant way
3329 (not counting uses of REG in them).
3331 The value is 2 if some of these insns are only conditionally invariant.
3333 We assume that INSN itself is the first set of REG
3334 and that its source is invariant. */
3336 static int
3337 consec_sets_invariant_p (loop, reg, n_sets, insn)
3338 const struct loop *loop;
3339 int n_sets;
3340 rtx reg, insn;
3342 struct loop_regs *regs = LOOP_REGS (loop);
3343 rtx p = insn;
3344 unsigned int regno = REGNO (reg);
3345 rtx temp;
3346 /* Number of sets we have to insist on finding after INSN. */
3347 int count = n_sets - 1;
3348 int old = regs->array[regno].set_in_loop;
3349 int value = 0;
3350 int this;
3352 /* If N_SETS hit the limit, we can't rely on its value. */
3353 if (n_sets == 127)
3354 return 0;
3356 regs->array[regno].set_in_loop = 0;
3358 while (count > 0)
3360 enum rtx_code code;
3361 rtx set;
3363 p = NEXT_INSN (p);
3364 code = GET_CODE (p);
3366 /* If library call, skip to end of it. */
3367 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3368 p = XEXP (temp, 0);
3370 this = 0;
3371 if (code == INSN
3372 && (set = single_set (p))
3373 && GET_CODE (SET_DEST (set)) == REG
3374 && REGNO (SET_DEST (set)) == regno)
3376 this = loop_invariant_p (loop, SET_SRC (set));
3377 if (this != 0)
3378 value |= this;
3379 else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3381 /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3382 If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3383 notes are OK. */
3384 this = (CONSTANT_P (XEXP (temp, 0))
3385 || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3386 && loop_invariant_p (loop, XEXP (temp, 0))));
3387 if (this != 0)
3388 value |= this;
3391 if (this != 0)
3392 count--;
3393 else if (code != NOTE)
3395 regs->array[regno].set_in_loop = old;
3396 return 0;
3400 regs->array[regno].set_in_loop = old;
3401 /* If loop_invariant_p ever returned 2, we return 2. */
3402 return 1 + (value & 2);
3405 #if 0
3406 /* I don't think this condition is sufficient to allow INSN
3407 to be moved, so we no longer test it. */
3409 /* Return 1 if all insns in the basic block of INSN and following INSN
3410 that set REG are invariant according to TABLE. */
3412 static int
3413 all_sets_invariant_p (reg, insn, table)
3414 rtx reg, insn;
3415 short *table;
3417 rtx p = insn;
3418 int regno = REGNO (reg);
3420 while (1)
3422 enum rtx_code code;
3423 p = NEXT_INSN (p);
3424 code = GET_CODE (p);
3425 if (code == CODE_LABEL || code == JUMP_INSN)
3426 return 1;
3427 if (code == INSN && GET_CODE (PATTERN (p)) == SET
3428 && GET_CODE (SET_DEST (PATTERN (p))) == REG
3429 && REGNO (SET_DEST (PATTERN (p))) == regno)
3431 if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3432 return 0;
3436 #endif /* 0 */
3438 /* Look at all uses (not sets) of registers in X. For each, if it is
3439 the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3440 a different insn, set USAGE[REGNO] to const0_rtx. */
3442 static void
3443 find_single_use_in_loop (regs, insn, x)
3444 struct loop_regs *regs;
3445 rtx insn;
3446 rtx x;
3448 enum rtx_code code = GET_CODE (x);
3449 const char *fmt = GET_RTX_FORMAT (code);
3450 int i, j;
3452 if (code == REG)
3453 regs->array[REGNO (x)].single_usage
3454 = (regs->array[REGNO (x)].single_usage != 0
3455 && regs->array[REGNO (x)].single_usage != insn)
3456 ? const0_rtx : insn;
3458 else if (code == SET)
3460 /* Don't count SET_DEST if it is a REG; otherwise count things
3461 in SET_DEST because if a register is partially modified, it won't
3462 show up as a potential movable so we don't care how USAGE is set
3463 for it. */
3464 if (GET_CODE (SET_DEST (x)) != REG)
3465 find_single_use_in_loop (regs, insn, SET_DEST (x));
3466 find_single_use_in_loop (regs, insn, SET_SRC (x));
3468 else
3469 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3471 if (fmt[i] == 'e' && XEXP (x, i) != 0)
3472 find_single_use_in_loop (regs, insn, XEXP (x, i));
3473 else if (fmt[i] == 'E')
3474 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3475 find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3479 /* Count and record any set in X which is contained in INSN. Update
3480 REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3481 in X. */
3483 static void
3484 count_one_set (regs, insn, x, last_set)
3485 struct loop_regs *regs;
3486 rtx insn, x;
3487 rtx *last_set;
3489 if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3490 /* Don't move a reg that has an explicit clobber.
3491 It's not worth the pain to try to do it correctly. */
3492 regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3494 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3496 rtx dest = SET_DEST (x);
3497 while (GET_CODE (dest) == SUBREG
3498 || GET_CODE (dest) == ZERO_EXTRACT
3499 || GET_CODE (dest) == SIGN_EXTRACT
3500 || GET_CODE (dest) == STRICT_LOW_PART)
3501 dest = XEXP (dest, 0);
3502 if (GET_CODE (dest) == REG)
3504 int i;
3505 int regno = REGNO (dest);
3506 for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3508 /* If this is the first setting of this reg
3509 in current basic block, and it was set before,
3510 it must be set in two basic blocks, so it cannot
3511 be moved out of the loop. */
3512 if (regs->array[regno].set_in_loop > 0
3513 && last_set == 0)
3514 regs->array[regno+i].may_not_optimize = 1;
3515 /* If this is not first setting in current basic block,
3516 see if reg was used in between previous one and this.
3517 If so, neither one can be moved. */
3518 if (last_set[regno] != 0
3519 && reg_used_between_p (dest, last_set[regno], insn))
3520 regs->array[regno+i].may_not_optimize = 1;
3521 if (regs->array[regno+i].set_in_loop < 127)
3522 ++regs->array[regno+i].set_in_loop;
3523 last_set[regno+i] = insn;
3529 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3530 is entered at LOOP->SCAN_START, return 1 if the register set in SET
3531 contained in insn INSN is used by any insn that precedes INSN in
3532 cyclic order starting from the loop entry point.
3534 We don't want to use INSN_LUID here because if we restrict INSN to those
3535 that have a valid INSN_LUID, it means we cannot move an invariant out
3536 from an inner loop past two loops. */
3538 static int
3539 loop_reg_used_before_p (loop, set, insn)
3540 const struct loop *loop;
3541 rtx set, insn;
3543 rtx reg = SET_DEST (set);
3544 rtx p;
3546 /* Scan forward checking for register usage. If we hit INSN, we
3547 are done. Otherwise, if we hit LOOP->END, wrap around to LOOP->START. */
3548 for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3550 if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3551 return 1;
3553 if (p == loop->end)
3554 p = loop->start;
3557 return 0;
3561 /* Information we collect about arrays that we might want to prefetch. */
3562 struct prefetch_info
3564 struct iv_class *class; /* Class this prefetch is based on. */
3565 struct induction *giv; /* GIV this prefetch is based on. */
3566 rtx base_address; /* Start prefetching from this address plus
3567 index. */
3568 HOST_WIDE_INT index;
3569 HOST_WIDE_INT stride; /* Prefetch stride in bytes in each
3570 iteration. */
3571 unsigned int bytes_accesed; /* Sum of sizes of all acceses to this
3572 prefetch area in one iteration. */
3573 unsigned int total_bytes; /* Total bytes loop will access in this block.
3574 This is set only for loops with known
3575 iteration counts and is 0xffffffff
3576 otherwise. */
3577 unsigned int write : 1; /* 1 for read/write prefetches. */
3578 unsigned int prefetch_in_loop : 1;
3579 /* 1 for those chosen for prefetching. */
3580 unsigned int prefetch_before_loop : 1;
3581 /* 1 for those chosen for prefetching. */
3584 /* Data used by check_store function. */
3585 struct check_store_data
3587 rtx mem_address;
3588 int mem_write;
3591 static void check_store PARAMS ((rtx, rtx, void *));
3592 static void emit_prefetch_instructions PARAMS ((struct loop *));
3593 static int rtx_equal_for_prefetch_p PARAMS ((rtx, rtx));
3595 /* Set mem_write when mem_address is found. Used as callback to
3596 note_stores. */
3597 static void
3598 check_store (x, pat, data)
3599 rtx x, pat ATTRIBUTE_UNUSED;
3600 void *data;
3602 struct check_store_data *d = (struct check_store_data *) data;
3604 if ((GET_CODE (x) == MEM) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3605 d->mem_write = 1;
3608 /* Like rtx_equal_p, but attempts to swap commutative operands. This is
3609 important to get some addresses combined. Later more sophisticated
3610 transformations can be added when necesary.
3612 ??? Same trick with swapping operand is done at several other places.
3613 It can be nice to develop some common way to handle this. */
3615 static int
3616 rtx_equal_for_prefetch_p (x, y)
3617 rtx x, y;
3619 int i;
3620 int j;
3621 enum rtx_code code = GET_CODE (x);
3622 const char *fmt;
3624 if (x == y)
3625 return 1;
3626 if (code != GET_CODE (y))
3627 return 0;
3629 code = GET_CODE (x);
3631 if (GET_RTX_CLASS (code) == 'c')
3633 return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3634 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3635 || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3636 && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3638 /* Compare the elements. If any pair of corresponding elements fails to
3639 match, return 0 for the whole thing. */
3641 fmt = GET_RTX_FORMAT (code);
3642 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3644 switch (fmt[i])
3646 case 'w':
3647 if (XWINT (x, i) != XWINT (y, i))
3648 return 0;
3649 break;
3651 case 'i':
3652 if (XINT (x, i) != XINT (y, i))
3653 return 0;
3654 break;
3656 case 'E':
3657 /* Two vectors must have the same length. */
3658 if (XVECLEN (x, i) != XVECLEN (y, i))
3659 return 0;
3661 /* And the corresponding elements must match. */
3662 for (j = 0; j < XVECLEN (x, i); j++)
3663 if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
3664 XVECEXP (y, i, j)) == 0)
3665 return 0;
3666 break;
3668 case 'e':
3669 if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
3670 return 0;
3671 break;
3673 case 's':
3674 if (strcmp (XSTR (x, i), XSTR (y, i)))
3675 return 0;
3676 break;
3678 case 'u':
3679 /* These are just backpointers, so they don't matter. */
3680 break;
3682 case '0':
3683 break;
3685 /* It is believed that rtx's at this level will never
3686 contain anything but integers and other rtx's,
3687 except for within LABEL_REFs and SYMBOL_REFs. */
3688 default:
3689 abort ();
3692 return 1;
3695 /* Remove constant addition value from the expression X (when present)
3696 and return it. */
3698 static HOST_WIDE_INT
3699 remove_constant_addition (x)
3700 rtx *x;
3702 HOST_WIDE_INT addval = 0;
3703 rtx exp = *x;
3705 /* Avoid clobbering a shared CONST expression. */
3706 if (GET_CODE (exp) == CONST)
3708 if (GET_CODE (XEXP (exp, 0)) == PLUS
3709 && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
3710 && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
3712 *x = XEXP (XEXP (exp, 0), 0);
3713 return INTVAL (XEXP (XEXP (exp, 0), 1));
3715 return 0;
3718 if (GET_CODE (exp) == CONST_INT)
3720 addval = INTVAL (exp);
3721 *x = const0_rtx;
3724 /* For plus expression recurse on ourself. */
3725 else if (GET_CODE (exp) == PLUS)
3727 addval += remove_constant_addition (&XEXP (exp, 0));
3728 addval += remove_constant_addition (&XEXP (exp, 1));
3730 /* In case our parameter was constant, remove extra zero from the
3731 expression. */
3732 if (XEXP (exp, 0) == const0_rtx)
3733 *x = XEXP (exp, 1);
3734 else if (XEXP (exp, 1) == const0_rtx)
3735 *x = XEXP (exp, 0);
3738 return addval;
3741 /* Attempt to identify accesses to arrays that are most likely to cause cache
3742 misses, and emit prefetch instructions a few prefetch blocks forward.
3744 To detect the arrays we use the GIV information that was collected by the
3745 strength reduction pass.
3747 The prefetch instructions are generated after the GIV information is done
3748 and before the strength reduction process. The new GIVs are injected into
3749 the strength reduction tables, so the prefetch addresses are optimized as
3750 well.
3752 GIVs are split into base address, stride, and constant addition values.
3753 GIVs with the same address, stride and close addition values are combined
3754 into a single prefetch. Also writes to GIVs are detected, so that prefetch
3755 for write instructions can be used for the block we write to, on machines
3756 that support write prefetches.
3758 Several heuristics are used to determine when to prefetch. They are
3759 controlled by defined symbols that can be overridden for each target. */
3761 static void
3762 emit_prefetch_instructions (loop)
3763 struct loop *loop;
3765 int num_prefetches = 0;
3766 int num_real_prefetches = 0;
3767 int num_real_write_prefetches = 0;
3768 int ahead;
3769 int i;
3770 struct iv_class *bl;
3771 struct induction *iv;
3772 struct prefetch_info info[MAX_PREFETCHES];
3773 struct loop_ivs *ivs = LOOP_IVS (loop);
3775 if (!HAVE_prefetch)
3776 return;
3778 /* Consider only loops w/o calls. When a call is done, the loop is probably
3779 slow enough to read the memory. */
3780 if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
3782 if (loop_dump_stream)
3783 fprintf (loop_dump_stream, "Prefetch: ignoring loop - has call.\n");
3785 return;
3788 if (PREFETCH_NO_LOW_LOOPCNT
3789 && LOOP_INFO (loop)->n_iterations
3790 && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
3792 if (loop_dump_stream)
3793 fprintf (loop_dump_stream,
3794 "Prefetch: ignoring loop - not enought iterations.\n");
3795 return;
3798 /* Search all induction variables and pick those interesting for the prefetch
3799 machinery. */
3800 for (bl = ivs->list; bl; bl = bl->next)
3802 struct induction *biv = bl->biv, *biv1;
3803 int basestride = 0;
3805 biv1 = biv;
3807 /* Expect all BIVs to be executed in each iteration. This makes our
3808 analysis more conservative. */
3809 while (biv1)
3811 /* Discard non-constant additions that we can't handle well yet, and
3812 BIVs that are executed multiple times; such BIVs ought to be
3813 handled in the nested loop. We accept not_every_iteration BIVs,
3814 since these only result in larger strides and make our
3815 heuristics more conservative.
3816 ??? What does the last sentence mean? */
3817 if (GET_CODE (biv->add_val) != CONST_INT)
3819 if (loop_dump_stream)
3821 fprintf (loop_dump_stream,
3822 "Prefetch: biv %i ignored: non-constant addition at insn %i:",
3823 REGNO (biv->src_reg), INSN_UID (biv->insn));
3824 print_rtl (loop_dump_stream, biv->add_val);
3825 fprintf (loop_dump_stream, "\n");
3827 break;
3830 if (biv->maybe_multiple)
3832 if (loop_dump_stream)
3834 fprintf (loop_dump_stream,
3835 "Prefetch: biv %i ignored: maybe_multiple at insn %i:",
3836 REGNO (biv->src_reg), INSN_UID (biv->insn));
3837 print_rtl (loop_dump_stream, biv->add_val);
3838 fprintf (loop_dump_stream, "\n");
3840 break;
3843 basestride += INTVAL (biv1->add_val);
3844 biv1 = biv1->next_iv;
3847 if (biv1 || !basestride)
3848 continue;
3850 for (iv = bl->giv; iv; iv = iv->next_iv)
3852 rtx address;
3853 rtx temp;
3854 HOST_WIDE_INT index = 0;
3855 int add = 1;
3856 HOST_WIDE_INT stride;
3857 struct check_store_data d;
3858 int size = GET_MODE_SIZE (GET_MODE (iv));
3860 /* There are several reasons why an induction variable is not
3861 interesting to us. */
3862 if (iv->giv_type != DEST_ADDR
3863 /* We are interested only in constant stride memory references
3864 in order to be able to compute density easily. */
3865 || GET_CODE (iv->mult_val) != CONST_INT
3866 /* Don't handle reversed order prefetches, since they are usually
3867 ineffective. Later we may be able to reverse such BIVs. */
3868 || (PREFETCH_NO_REVERSE_ORDER
3869 && (stride = INTVAL (iv->mult_val) * basestride) < 0)
3870 /* Prefetching of accesses with such an extreme stride is probably
3871 not worthwhile, either. */
3872 || (PREFETCH_NO_EXTREME_STRIDE
3873 && stride > PREFETCH_EXTREME_STRIDE)
3874 /* Ignore GIVs with varying add values; we can't predict the
3875 value for the next iteration. */
3876 || !loop_invariant_p (loop, iv->add_val)
3877 /* Ignore GIVs in the nested loops; they ought to have been
3878 handled already. */
3879 || iv->maybe_multiple)
3881 if (loop_dump_stream)
3882 fprintf (loop_dump_stream, "Prefetch: Ignoring giv at %i\n",
3883 INSN_UID (iv->insn));
3884 continue;
3887 /* Determine the pointer to the basic array we are examining. It is
3888 the sum of the BIV's initial value and the GIV's add_val. */
3889 index = 0;
3891 address = copy_rtx (iv->add_val);
3892 temp = copy_rtx (bl->initial_value);
3894 address = simplify_gen_binary (PLUS, Pmode, temp, address);
3895 index = remove_constant_addition (&address);
3897 index += size;
3898 d.mem_write = 0;
3899 d.mem_address = *iv->location;
3901 /* When the GIV is not always executed, we might be better off by
3902 not dirtying the cache pages. */
3903 if (PREFETCH_NOT_ALWAYS || iv->always_executed)
3904 note_stores (PATTERN (iv->insn), check_store, &d);
3906 /* Attempt to find another prefetch to the same array and see if we
3907 can merge this one. */
3908 for (i = 0; i < num_prefetches; i++)
3909 if (rtx_equal_for_prefetch_p (address, info[i].base_address)
3910 && stride == info[i].stride)
3912 /* In case both access same array (same location
3913 just with small difference in constant indexes), merge
3914 the prefetches. Just do the later and the earlier will
3915 get prefetched from previous iteration.
3916 4096 is artificial threshold. It should not be too small,
3917 but also not bigger than small portion of memory usually
3918 traversed by single loop. */
3919 if (index >= info[i].index && index - info[i].index < 4096)
3921 info[i].write |= d.mem_write;
3922 info[i].bytes_accesed += size;
3923 info[i].index = index;
3924 info[i].giv = iv;
3925 info[i].class = bl;
3926 info[num_prefetches].base_address = address;
3927 add = 0;
3928 break;
3931 if (index < info[i].index && info[i].index - index < 4096)
3933 info[i].write |= d.mem_write;
3934 info[i].bytes_accesed += size;
3935 add = 0;
3936 break;
3940 /* Merging failed. */
3941 if (add)
3943 info[num_prefetches].giv = iv;
3944 info[num_prefetches].class = bl;
3945 info[num_prefetches].index = index;
3946 info[num_prefetches].stride = stride;
3947 info[num_prefetches].base_address = address;
3948 info[num_prefetches].write = d.mem_write;
3949 info[num_prefetches].bytes_accesed = size;
3950 num_prefetches++;
3951 if (num_prefetches >= MAX_PREFETCHES)
3953 if (loop_dump_stream)
3954 fprintf (loop_dump_stream,
3955 "Maximal number of prefetches exceeded.\n");
3956 return;
3962 for (i = 0; i < num_prefetches; i++)
3964 /* Attempt to calculate the number of bytes fetched by the loop.
3965 Avoid overflow. */
3966 if (LOOP_INFO (loop)->n_iterations
3967 && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
3968 >= LOOP_INFO (loop)->n_iterations))
3969 info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
3970 else
3971 info[i].total_bytes = 0xffffffff;
3973 /* Prefetch is worthwhile only when the loads/stores are dense. */
3974 if (PREFETCH_ONLY_DENSE_MEM
3975 && info[i].bytes_accesed * 256 / info[i].stride > PREFETCH_DENSE_MEM
3976 && (info[i].total_bytes / PREFETCH_BLOCK
3977 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
3979 info[i].prefetch_before_loop = 1;
3980 info[i].prefetch_in_loop
3981 = (info[i].total_bytes / PREFETCH_BLOCK
3982 > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
3984 else
3985 info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
3987 if (info[i].prefetch_in_loop)
3989 num_real_prefetches += ((info[i].stride + PREFETCH_BLOCK - 1)
3990 / PREFETCH_BLOCK);
3991 if (info[i].write)
3992 num_real_write_prefetches
3993 += (info[i].stride + PREFETCH_BLOCK - 1) / PREFETCH_BLOCK;
3997 if (loop_dump_stream)
3999 for (i = 0; i < num_prefetches; i++)
4001 fprintf (loop_dump_stream, "Prefetch insn %i address: ",
4002 INSN_UID (info[i].giv->insn));
4003 print_rtl (loop_dump_stream, info[i].base_address);
4004 fprintf (loop_dump_stream, " Index: ");
4005 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].index);
4006 fprintf (loop_dump_stream, " stride: ");
4007 fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].stride);
4008 fprintf (loop_dump_stream,
4009 " density: %i%% total_bytes: %u%sin loop: %s before: %s\n",
4010 (int) (info[i].bytes_accesed * 100 / info[i].stride),
4011 info[i].total_bytes,
4012 info[i].write ? " read/write " : " read only ",
4013 info[i].prefetch_in_loop ? "yes" : "no",
4014 info[i].prefetch_before_loop ? "yes" : "no");
4017 fprintf (loop_dump_stream, "Real prefetches needed: %i (write: %i)\n",
4018 num_real_prefetches, num_real_write_prefetches);
4021 if (!num_real_prefetches)
4022 return;
4024 ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches;
4026 if (!ahead)
4027 return;
4029 for (i = 0; i < num_prefetches; i++)
4031 if (info[i].prefetch_in_loop)
4033 int y;
4035 for (y = 0; y < ((info[i].stride + PREFETCH_BLOCK - 1)
4036 / PREFETCH_BLOCK); y++)
4038 rtx loc = copy_rtx (*info[i].giv->location);
4039 rtx insn;
4040 int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4041 rtx before_insn = info[i].giv->insn;
4042 rtx prev_insn = PREV_INSN (info[i].giv->insn);
4044 /* We can save some effort by offsetting the address on
4045 architectures with offsettable memory references. */
4046 if (offsettable_address_p (0, VOIDmode, loc))
4047 loc = plus_constant (loc, bytes_ahead);
4048 else
4050 rtx reg = gen_reg_rtx (Pmode);
4051 loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4052 GEN_INT (bytes_ahead), reg,
4053 0, before_insn);
4054 loc = reg;
4057 /* Make sure the address operand is valid for prefetch. */
4058 if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4059 (loc,
4060 insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4061 loc = force_reg (Pmode, loc);
4062 emit_insn_before (gen_prefetch (loc, GEN_INT (info[i].write),
4063 GEN_INT (3)),
4064 before_insn);
4066 /* Check all insns emitted and record the new GIV
4067 information. */
4068 insn = NEXT_INSN (prev_insn);
4069 while (insn != before_insn)
4071 insn = check_insn_for_givs (loop, insn,
4072 info[i].giv->always_executed,
4073 info[i].giv->maybe_multiple);
4074 insn = NEXT_INSN (insn);
4079 if (info[i].prefetch_before_loop)
4081 int y;
4083 /* Emit INSNs before the loop to fetch the first cache lines. */
4084 for (y = 0;
4085 (!info[i].prefetch_in_loop || y < ahead)
4086 && y * PREFETCH_BLOCK < (int) info[i].total_bytes; y ++)
4088 rtx reg = gen_reg_rtx (Pmode);
4089 rtx loop_start = loop->start;
4090 rtx add_val = simplify_gen_binary (PLUS, Pmode,
4091 info[i].giv->add_val,
4092 GEN_INT (y * PREFETCH_BLOCK));
4094 loop_iv_add_mult_emit_before (loop, info[i].class->initial_value,
4095 info[i].giv->mult_val,
4096 add_val, reg, 0, loop_start);
4097 emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4098 GEN_INT (3)),
4099 loop_start);
4104 return;
4107 /* A "basic induction variable" or biv is a pseudo reg that is set
4108 (within this loop) only by incrementing or decrementing it. */
4109 /* A "general induction variable" or giv is a pseudo reg whose
4110 value is a linear function of a biv. */
4112 /* Bivs are recognized by `basic_induction_var';
4113 Givs by `general_induction_var'. */
4115 /* Communication with routines called via `note_stores'. */
4117 static rtx note_insn;
4119 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs. */
4121 static rtx addr_placeholder;
4123 /* ??? Unfinished optimizations, and possible future optimizations,
4124 for the strength reduction code. */
4126 /* ??? The interaction of biv elimination, and recognition of 'constant'
4127 bivs, may cause problems. */
4129 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4130 performance problems.
4132 Perhaps don't eliminate things that can be combined with an addressing
4133 mode. Find all givs that have the same biv, mult_val, and add_val;
4134 then for each giv, check to see if its only use dies in a following
4135 memory address. If so, generate a new memory address and check to see
4136 if it is valid. If it is valid, then store the modified memory address,
4137 otherwise, mark the giv as not done so that it will get its own iv. */
4139 /* ??? Could try to optimize branches when it is known that a biv is always
4140 positive. */
4142 /* ??? When replace a biv in a compare insn, we should replace with closest
4143 giv so that an optimized branch can still be recognized by the combiner,
4144 e.g. the VAX acb insn. */
4146 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4147 was rerun in loop_optimize whenever a register was added or moved.
4148 Also, some of the optimizations could be a little less conservative. */
4150 /* Scan the loop body and call FNCALL for each insn. In the addition to the
4151 LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4152 callback.
4154 NOT_EVERY_ITERATION if current insn is not executed at least once for every
4155 loop iteration except for the last one.
4157 MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4158 loop iteration.
4160 void
4161 for_each_insn_in_loop (loop, fncall)
4162 struct loop *loop;
4163 loop_insn_callback fncall;
4165 /* This is 1 if current insn is not executed at least once for every loop
4166 iteration. */
4167 int not_every_iteration = 0;
4168 int maybe_multiple = 0;
4169 int past_loop_latch = 0;
4170 int loop_depth = 0;
4171 rtx p;
4173 /* If loop_scan_start points to the loop exit test, we have to be wary of
4174 subversive use of gotos inside expression statements. */
4175 if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4176 maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4178 /* Scan through loop to find all possible bivs. */
4180 for (p = next_insn_in_loop (loop, loop->scan_start);
4181 p != NULL_RTX;
4182 p = next_insn_in_loop (loop, p))
4184 p = fncall (loop, p, not_every_iteration, maybe_multiple);
4186 /* Past CODE_LABEL, we get to insns that may be executed multiple
4187 times. The only way we can be sure that they can't is if every
4188 jump insn between here and the end of the loop either
4189 returns, exits the loop, is a jump to a location that is still
4190 behind the label, or is a jump to the loop start. */
4192 if (GET_CODE (p) == CODE_LABEL)
4194 rtx insn = p;
4196 maybe_multiple = 0;
4198 while (1)
4200 insn = NEXT_INSN (insn);
4201 if (insn == loop->scan_start)
4202 break;
4203 if (insn == loop->end)
4205 if (loop->top != 0)
4206 insn = loop->top;
4207 else
4208 break;
4209 if (insn == loop->scan_start)
4210 break;
4213 if (GET_CODE (insn) == JUMP_INSN
4214 && GET_CODE (PATTERN (insn)) != RETURN
4215 && (!any_condjump_p (insn)
4216 || (JUMP_LABEL (insn) != 0
4217 && JUMP_LABEL (insn) != loop->scan_start
4218 && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4220 maybe_multiple = 1;
4221 break;
4226 /* Past a jump, we get to insns for which we can't count
4227 on whether they will be executed during each iteration. */
4228 /* This code appears twice in strength_reduce. There is also similar
4229 code in scan_loop. */
4230 if (GET_CODE (p) == JUMP_INSN
4231 /* If we enter the loop in the middle, and scan around to the
4232 beginning, don't set not_every_iteration for that.
4233 This can be any kind of jump, since we want to know if insns
4234 will be executed if the loop is executed. */
4235 && !(JUMP_LABEL (p) == loop->top
4236 && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4237 && any_uncondjump_p (p))
4238 || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
4240 rtx label = 0;
4242 /* If this is a jump outside the loop, then it also doesn't
4243 matter. Check to see if the target of this branch is on the
4244 loop->exits_labels list. */
4246 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4247 if (XEXP (label, 0) == JUMP_LABEL (p))
4248 break;
4250 if (!label)
4251 not_every_iteration = 1;
4254 else if (GET_CODE (p) == NOTE)
4256 /* At the virtual top of a converted loop, insns are again known to
4257 be executed each iteration: logically, the loop begins here
4258 even though the exit code has been duplicated.
4260 Insns are also again known to be executed each iteration at
4261 the LOOP_CONT note. */
4262 if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4263 || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4264 && loop_depth == 0)
4265 not_every_iteration = 0;
4266 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4267 loop_depth++;
4268 else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4269 loop_depth--;
4272 /* Note if we pass a loop latch. If we do, then we can not clear
4273 NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4274 a loop since a jump before the last CODE_LABEL may have started
4275 a new loop iteration.
4277 Note that LOOP_TOP is only set for rotated loops and we need
4278 this check for all loops, so compare against the CODE_LABEL
4279 which immediately follows LOOP_START. */
4280 if (GET_CODE (p) == JUMP_INSN
4281 && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4282 past_loop_latch = 1;
4284 /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4285 an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4286 or not an insn is known to be executed each iteration of the
4287 loop, whether or not any iterations are known to occur.
4289 Therefore, if we have just passed a label and have no more labels
4290 between here and the test insn of the loop, and we have not passed
4291 a jump to the top of the loop, then we know these insns will be
4292 executed each iteration. */
4294 if (not_every_iteration
4295 && !past_loop_latch
4296 && GET_CODE (p) == CODE_LABEL
4297 && no_labels_between_p (p, loop->end)
4298 && loop_insn_first_p (p, loop->cont))
4299 not_every_iteration = 0;
4303 static void
4304 loop_bivs_find (loop)
4305 struct loop *loop;
4307 struct loop_regs *regs = LOOP_REGS (loop);
4308 struct loop_ivs *ivs = LOOP_IVS (loop);
4309 /* Temporary list pointers for traversing ivs->list. */
4310 struct iv_class *bl, **backbl;
4312 ivs->list = 0;
4314 for_each_insn_in_loop (loop, check_insn_for_bivs);
4316 /* Scan ivs->list to remove all regs that proved not to be bivs.
4317 Make a sanity check against regs->n_times_set. */
4318 for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4320 if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4321 /* Above happens if register modified by subreg, etc. */
4322 /* Make sure it is not recognized as a basic induction var: */
4323 || regs->array[bl->regno].n_times_set != bl->biv_count
4324 /* If never incremented, it is invariant that we decided not to
4325 move. So leave it alone. */
4326 || ! bl->incremented)
4328 if (loop_dump_stream)
4329 fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
4330 bl->regno,
4331 (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4332 ? "not induction variable"
4333 : (! bl->incremented ? "never incremented"
4334 : "count error")));
4336 REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4337 *backbl = bl->next;
4339 else
4341 backbl = &bl->next;
4343 if (loop_dump_stream)
4344 fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
4350 /* Determine how BIVS are initialised by looking through pre-header
4351 extended basic block. */
4352 static void
4353 loop_bivs_init_find (loop)
4354 struct loop *loop;
4356 struct loop_ivs *ivs = LOOP_IVS (loop);
4357 /* Temporary list pointers for traversing ivs->list. */
4358 struct iv_class *bl;
4359 int call_seen;
4360 rtx p;
4362 /* Find initial value for each biv by searching backwards from loop_start,
4363 halting at first label. Also record any test condition. */
4365 call_seen = 0;
4366 for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
4368 rtx test;
4370 note_insn = p;
4372 if (GET_CODE (p) == CALL_INSN)
4373 call_seen = 1;
4375 if (INSN_P (p))
4376 note_stores (PATTERN (p), record_initial, ivs);
4378 /* Record any test of a biv that branches around the loop if no store
4379 between it and the start of loop. We only care about tests with
4380 constants and registers and only certain of those. */
4381 if (GET_CODE (p) == JUMP_INSN
4382 && JUMP_LABEL (p) != 0
4383 && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4384 && (test = get_condition_for_loop (loop, p)) != 0
4385 && GET_CODE (XEXP (test, 0)) == REG
4386 && REGNO (XEXP (test, 0)) < max_reg_before_loop
4387 && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4388 && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4389 && bl->init_insn == 0)
4391 /* If an NE test, we have an initial value! */
4392 if (GET_CODE (test) == NE)
4394 bl->init_insn = p;
4395 bl->init_set = gen_rtx_SET (VOIDmode,
4396 XEXP (test, 0), XEXP (test, 1));
4398 else
4399 bl->initial_test = test;
4405 /* Look at the each biv and see if we can say anything better about its
4406 initial value from any initializing insns set up above. (This is done
4407 in two passes to avoid missing SETs in a PARALLEL.) */
4408 static void
4409 loop_bivs_check (loop)
4410 struct loop *loop;
4412 struct loop_ivs *ivs = LOOP_IVS (loop);
4413 /* Temporary list pointers for traversing ivs->list. */
4414 struct iv_class *bl;
4415 struct iv_class **backbl;
4417 for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4419 rtx src;
4420 rtx note;
4422 if (! bl->init_insn)
4423 continue;
4425 /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4426 is a constant, use the value of that. */
4427 if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4428 && CONSTANT_P (XEXP (note, 0)))
4429 || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4430 && CONSTANT_P (XEXP (note, 0))))
4431 src = XEXP (note, 0);
4432 else
4433 src = SET_SRC (bl->init_set);
4435 if (loop_dump_stream)
4436 fprintf (loop_dump_stream,
4437 "Biv %d: initialized at insn %d: initial value ",
4438 bl->regno, INSN_UID (bl->init_insn));
4440 if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4441 || GET_MODE (src) == VOIDmode)
4442 && valid_initial_value_p (src, bl->init_insn,
4443 LOOP_INFO (loop)->pre_header_has_call,
4444 loop->start))
4446 bl->initial_value = src;
4448 if (loop_dump_stream)
4450 print_simple_rtl (loop_dump_stream, src);
4451 fputc ('\n', loop_dump_stream);
4454 /* If we can't make it a giv,
4455 let biv keep initial value of "itself". */
4456 else if (loop_dump_stream)
4457 fprintf (loop_dump_stream, "is complex\n");
4462 /* Search the loop for general induction variables. */
4464 static void
4465 loop_givs_find (loop)
4466 struct loop* loop;
4468 for_each_insn_in_loop (loop, check_insn_for_givs);
4472 /* For each giv for which we still don't know whether or not it is
4473 replaceable, check to see if it is replaceable because its final value
4474 can be calculated. */
4476 static void
4477 loop_givs_check (loop)
4478 struct loop *loop;
4480 struct loop_ivs *ivs = LOOP_IVS (loop);
4481 struct iv_class *bl;
4483 for (bl = ivs->list; bl; bl = bl->next)
4485 struct induction *v;
4487 for (v = bl->giv; v; v = v->next_iv)
4488 if (! v->replaceable && ! v->not_replaceable)
4489 check_final_value (loop, v);
4494 /* Return non-zero if it is possible to eliminate the biv BL provided
4495 all givs are reduced. This is possible if either the reg is not
4496 used outside the loop, or we can compute what its final value will
4497 be. */
4499 static int
4500 loop_biv_eliminable_p (loop, bl, threshold, insn_count)
4501 struct loop *loop;
4502 struct iv_class *bl;
4503 int threshold;
4504 int insn_count;
4506 /* For architectures with a decrement_and_branch_until_zero insn,
4507 don't do this if we put a REG_NONNEG note on the endtest for this
4508 biv. */
4510 #ifdef HAVE_decrement_and_branch_until_zero
4511 if (bl->nonneg)
4513 if (loop_dump_stream)
4514 fprintf (loop_dump_stream,
4515 "Cannot eliminate nonneg biv %d.\n", bl->regno);
4516 return 0;
4518 #endif
4520 /* Check that biv is used outside loop or if it has a final value.
4521 Compare against bl->init_insn rather than loop->start. We aren't
4522 concerned with any uses of the biv between init_insn and
4523 loop->start since these won't be affected by the value of the biv
4524 elsewhere in the function, so long as init_insn doesn't use the
4525 biv itself. */
4527 if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
4528 && bl->init_insn
4529 && INSN_UID (bl->init_insn) < max_uid_for_loop
4530 && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
4531 && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4532 || (bl->final_value = final_biv_value (loop, bl)))
4533 return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
4535 if (loop_dump_stream)
4537 fprintf (loop_dump_stream,
4538 "Cannot eliminate biv %d.\n",
4539 bl->regno);
4540 fprintf (loop_dump_stream,
4541 "First use: insn %d, last use: insn %d.\n",
4542 REGNO_FIRST_UID (bl->regno),
4543 REGNO_LAST_UID (bl->regno));
4545 return 0;
4549 /* Reduce each giv of BL that we have decided to reduce. */
4551 static void
4552 loop_givs_reduce (loop, bl)
4553 struct loop *loop;
4554 struct iv_class *bl;
4556 struct induction *v;
4558 for (v = bl->giv; v; v = v->next_iv)
4560 struct induction *tv;
4561 if (! v->ignore && v->same == 0)
4563 int auto_inc_opt = 0;
4565 /* If the code for derived givs immediately below has already
4566 allocated a new_reg, we must keep it. */
4567 if (! v->new_reg)
4568 v->new_reg = gen_reg_rtx (v->mode);
4570 #ifdef AUTO_INC_DEC
4571 /* If the target has auto-increment addressing modes, and
4572 this is an address giv, then try to put the increment
4573 immediately after its use, so that flow can create an
4574 auto-increment addressing mode. */
4575 if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4576 && bl->biv->always_executed && ! bl->biv->maybe_multiple
4577 /* We don't handle reversed biv's because bl->biv->insn
4578 does not have a valid INSN_LUID. */
4579 && ! bl->reversed
4580 && v->always_executed && ! v->maybe_multiple
4581 && INSN_UID (v->insn) < max_uid_for_loop)
4583 /* If other giv's have been combined with this one, then
4584 this will work only if all uses of the other giv's occur
4585 before this giv's insn. This is difficult to check.
4587 We simplify this by looking for the common case where
4588 there is one DEST_REG giv, and this giv's insn is the
4589 last use of the dest_reg of that DEST_REG giv. If the
4590 increment occurs after the address giv, then we can
4591 perform the optimization. (Otherwise, the increment
4592 would have to go before other_giv, and we would not be
4593 able to combine it with the address giv to get an
4594 auto-inc address.) */
4595 if (v->combined_with)
4597 struct induction *other_giv = 0;
4599 for (tv = bl->giv; tv; tv = tv->next_iv)
4600 if (tv->same == v)
4602 if (other_giv)
4603 break;
4604 else
4605 other_giv = tv;
4607 if (! tv && other_giv
4608 && REGNO (other_giv->dest_reg) < max_reg_before_loop
4609 && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4610 == INSN_UID (v->insn))
4611 && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4612 auto_inc_opt = 1;
4614 /* Check for case where increment is before the address
4615 giv. Do this test in "loop order". */
4616 else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4617 && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4618 || (INSN_LUID (bl->biv->insn)
4619 > INSN_LUID (loop->scan_start))))
4620 || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4621 && (INSN_LUID (loop->scan_start)
4622 < INSN_LUID (bl->biv->insn))))
4623 auto_inc_opt = -1;
4624 else
4625 auto_inc_opt = 1;
4627 #ifdef HAVE_cc0
4629 rtx prev;
4631 /* We can't put an insn immediately after one setting
4632 cc0, or immediately before one using cc0. */
4633 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4634 || (auto_inc_opt == -1
4635 && (prev = prev_nonnote_insn (v->insn)) != 0
4636 && INSN_P (prev)
4637 && sets_cc0_p (PATTERN (prev))))
4638 auto_inc_opt = 0;
4640 #endif
4642 if (auto_inc_opt)
4643 v->auto_inc_opt = 1;
4645 #endif
4647 /* For each place where the biv is incremented, add an insn
4648 to increment the new, reduced reg for the giv. */
4649 for (tv = bl->biv; tv; tv = tv->next_iv)
4651 rtx insert_before;
4653 if (! auto_inc_opt)
4654 insert_before = tv->insn;
4655 else if (auto_inc_opt == 1)
4656 insert_before = NEXT_INSN (v->insn);
4657 else
4658 insert_before = v->insn;
4660 if (tv->mult_val == const1_rtx)
4661 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4662 v->new_reg, v->new_reg,
4663 0, insert_before);
4664 else /* tv->mult_val == const0_rtx */
4665 /* A multiply is acceptable here
4666 since this is presumed to be seldom executed. */
4667 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4668 v->add_val, v->new_reg,
4669 0, insert_before);
4672 /* Add code at loop start to initialize giv's reduced reg. */
4674 loop_iv_add_mult_hoist (loop,
4675 extend_value_for_giv (v, bl->initial_value),
4676 v->mult_val, v->add_val, v->new_reg);
4682 /* Check for givs whose first use is their definition and whose
4683 last use is the definition of another giv. If so, it is likely
4684 dead and should not be used to derive another giv nor to
4685 eliminate a biv. */
4687 static void
4688 loop_givs_dead_check (loop, bl)
4689 struct loop *loop ATTRIBUTE_UNUSED;
4690 struct iv_class *bl;
4692 struct induction *v;
4694 for (v = bl->giv; v; v = v->next_iv)
4696 if (v->ignore
4697 || (v->same && v->same->ignore))
4698 continue;
4700 if (v->giv_type == DEST_REG
4701 && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4703 struct induction *v1;
4705 for (v1 = bl->giv; v1; v1 = v1->next_iv)
4706 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4707 v->maybe_dead = 1;
4713 static void
4714 loop_givs_rescan (loop, bl, reg_map)
4715 struct loop *loop;
4716 struct iv_class *bl;
4717 rtx *reg_map;
4719 struct induction *v;
4721 for (v = bl->giv; v; v = v->next_iv)
4723 if (v->same && v->same->ignore)
4724 v->ignore = 1;
4726 if (v->ignore)
4727 continue;
4729 /* Update expression if this was combined, in case other giv was
4730 replaced. */
4731 if (v->same)
4732 v->new_reg = replace_rtx (v->new_reg,
4733 v->same->dest_reg, v->same->new_reg);
4735 /* See if this register is known to be a pointer to something. If
4736 so, see if we can find the alignment. First see if there is a
4737 destination register that is a pointer. If so, this shares the
4738 alignment too. Next see if we can deduce anything from the
4739 computational information. If not, and this is a DEST_ADDR
4740 giv, at least we know that it's a pointer, though we don't know
4741 the alignment. */
4742 if (GET_CODE (v->new_reg) == REG
4743 && v->giv_type == DEST_REG
4744 && REG_POINTER (v->dest_reg))
4745 mark_reg_pointer (v->new_reg,
4746 REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4747 else if (GET_CODE (v->new_reg) == REG
4748 && REG_POINTER (v->src_reg))
4750 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4752 if (align == 0
4753 || GET_CODE (v->add_val) != CONST_INT
4754 || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4755 align = 0;
4757 mark_reg_pointer (v->new_reg, align);
4759 else if (GET_CODE (v->new_reg) == REG
4760 && GET_CODE (v->add_val) == REG
4761 && REG_POINTER (v->add_val))
4763 unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4765 if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4766 || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4767 align = 0;
4769 mark_reg_pointer (v->new_reg, align);
4771 else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4772 mark_reg_pointer (v->new_reg, 0);
4774 if (v->giv_type == DEST_ADDR)
4775 /* Store reduced reg as the address in the memref where we found
4776 this giv. */
4777 validate_change (v->insn, v->location, v->new_reg, 0);
4778 else if (v->replaceable)
4780 reg_map[REGNO (v->dest_reg)] = v->new_reg;
4782 else
4784 /* Not replaceable; emit an insn to set the original giv reg from
4785 the reduced giv, same as above. */
4786 loop_insn_emit_after (loop, 0, v->insn,
4787 gen_move_insn (v->dest_reg, v->new_reg));
4790 /* When a loop is reversed, givs which depend on the reversed
4791 biv, and which are live outside the loop, must be set to their
4792 correct final value. This insn is only needed if the giv is
4793 not replaceable. The correct final value is the same as the
4794 value that the giv starts the reversed loop with. */
4795 if (bl->reversed && ! v->replaceable)
4796 loop_iv_add_mult_sink (loop,
4797 extend_value_for_giv (v, bl->initial_value),
4798 v->mult_val, v->add_val, v->dest_reg);
4799 else if (v->final_value)
4800 loop_insn_sink_or_swim (loop,
4801 gen_move_insn (v->dest_reg, v->final_value));
4803 if (loop_dump_stream)
4805 fprintf (loop_dump_stream, "giv at %d reduced to ",
4806 INSN_UID (v->insn));
4807 print_simple_rtl (loop_dump_stream, v->new_reg);
4808 fprintf (loop_dump_stream, "\n");
4814 static int
4815 loop_giv_reduce_benefit (loop, bl, v, test_reg)
4816 struct loop *loop ATTRIBUTE_UNUSED;
4817 struct iv_class *bl;
4818 struct induction *v;
4819 rtx test_reg;
4821 int add_cost;
4822 int benefit;
4824 benefit = v->benefit;
4825 PUT_MODE (test_reg, v->mode);
4826 add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4827 test_reg, test_reg);
4829 /* Reduce benefit if not replaceable, since we will insert a
4830 move-insn to replace the insn that calculates this giv. Don't do
4831 this unless the giv is a user variable, since it will often be
4832 marked non-replaceable because of the duplication of the exit
4833 code outside the loop. In such a case, the copies we insert are
4834 dead and will be deleted. So they don't have a cost. Similar
4835 situations exist. */
4836 /* ??? The new final_[bg]iv_value code does a much better job of
4837 finding replaceable giv's, and hence this code may no longer be
4838 necessary. */
4839 if (! v->replaceable && ! bl->eliminable
4840 && REG_USERVAR_P (v->dest_reg))
4841 benefit -= copy_cost;
4843 /* Decrease the benefit to count the add-insns that we will insert
4844 to increment the reduced reg for the giv. ??? This can
4845 overestimate the run-time cost of the additional insns, e.g. if
4846 there are multiple basic blocks that increment the biv, but only
4847 one of these blocks is executed during each iteration. There is
4848 no good way to detect cases like this with the current structure
4849 of the loop optimizer. This code is more accurate for
4850 determining code size than run-time benefits. */
4851 benefit -= add_cost * bl->biv_count;
4853 /* Decide whether to strength-reduce this giv or to leave the code
4854 unchanged (recompute it from the biv each time it is used). This
4855 decision can be made independently for each giv. */
4857 #ifdef AUTO_INC_DEC
4858 /* Attempt to guess whether autoincrement will handle some of the
4859 new add insns; if so, increase BENEFIT (undo the subtraction of
4860 add_cost that was done above). */
4861 if (v->giv_type == DEST_ADDR
4862 /* Increasing the benefit is risky, since this is only a guess.
4863 Avoid increasing register pressure in cases where there would
4864 be no other benefit from reducing this giv. */
4865 && benefit > 0
4866 && GET_CODE (v->mult_val) == CONST_INT)
4868 int size = GET_MODE_SIZE (GET_MODE (v->mem));
4870 if (HAVE_POST_INCREMENT
4871 && INTVAL (v->mult_val) == size)
4872 benefit += add_cost * bl->biv_count;
4873 else if (HAVE_PRE_INCREMENT
4874 && INTVAL (v->mult_val) == size)
4875 benefit += add_cost * bl->biv_count;
4876 else if (HAVE_POST_DECREMENT
4877 && -INTVAL (v->mult_val) == size)
4878 benefit += add_cost * bl->biv_count;
4879 else if (HAVE_PRE_DECREMENT
4880 && -INTVAL (v->mult_val) == size)
4881 benefit += add_cost * bl->biv_count;
4883 #endif
4885 return benefit;
4889 /* Free IV structures for LOOP. */
4891 static void
4892 loop_ivs_free (loop)
4893 struct loop *loop;
4895 struct loop_ivs *ivs = LOOP_IVS (loop);
4896 struct iv_class *iv = ivs->list;
4898 free (ivs->regs);
4900 while (iv)
4902 struct iv_class *next = iv->next;
4903 struct induction *induction;
4904 struct induction *next_induction;
4906 for (induction = iv->biv; induction; induction = next_induction)
4908 next_induction = induction->next_iv;
4909 free (induction);
4911 for (induction = iv->giv; induction; induction = next_induction)
4913 next_induction = induction->next_iv;
4914 free (induction);
4917 free (iv);
4918 iv = next;
4923 /* Perform strength reduction and induction variable elimination.
4925 Pseudo registers created during this function will be beyond the
4926 last valid index in several tables including
4927 REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID. This does not cause a
4928 problem here, because the added registers cannot be givs outside of
4929 their loop, and hence will never be reconsidered. But scan_loop
4930 must check regnos to make sure they are in bounds. */
4932 static void
4933 strength_reduce (loop, flags)
4934 struct loop *loop;
4935 int flags;
4937 struct loop_info *loop_info = LOOP_INFO (loop);
4938 struct loop_regs *regs = LOOP_REGS (loop);
4939 struct loop_ivs *ivs = LOOP_IVS (loop);
4940 rtx p;
4941 /* Temporary list pointer for traversing ivs->list. */
4942 struct iv_class *bl;
4943 /* Ratio of extra register life span we can justify
4944 for saving an instruction. More if loop doesn't call subroutines
4945 since in that case saving an insn makes more difference
4946 and more registers are available. */
4947 /* ??? could set this to last value of threshold in move_movables */
4948 int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
4949 /* Map of pseudo-register replacements. */
4950 rtx *reg_map = NULL;
4951 int reg_map_size;
4952 int unrolled_insn_copies = 0;
4953 rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
4954 int insn_count = count_insns_in_loop (loop);
4956 addr_placeholder = gen_reg_rtx (Pmode);
4958 ivs->n_regs = max_reg_before_loop;
4959 ivs->regs = (struct iv *) xcalloc (ivs->n_regs, sizeof (struct iv));
4961 /* Find all BIVs in loop. */
4962 loop_bivs_find (loop);
4964 /* Exit if there are no bivs. */
4965 if (! ivs->list)
4967 /* Can still unroll the loop anyways, but indicate that there is no
4968 strength reduction info available. */
4969 if (flags & LOOP_UNROLL)
4970 unroll_loop (loop, insn_count, 0);
4972 loop_ivs_free (loop);
4973 return;
4976 /* Determine how BIVS are initialised by looking through pre-header
4977 extended basic block. */
4978 loop_bivs_init_find (loop);
4980 /* Look at the each biv and see if we can say anything better about its
4981 initial value from any initializing insns set up above. */
4982 loop_bivs_check (loop);
4984 /* Search the loop for general induction variables. */
4985 loop_givs_find (loop);
4987 /* Try to calculate and save the number of loop iterations. This is
4988 set to zero if the actual number can not be calculated. This must
4989 be called after all giv's have been identified, since otherwise it may
4990 fail if the iteration variable is a giv. */
4991 loop_iterations (loop);
4993 #ifdef HAVE_prefetch
4994 if (flags & LOOP_PREFETCH)
4995 emit_prefetch_instructions (loop);
4996 #endif
4998 /* Now for each giv for which we still don't know whether or not it is
4999 replaceable, check to see if it is replaceable because its final value
5000 can be calculated. This must be done after loop_iterations is called,
5001 so that final_giv_value will work correctly. */
5002 loop_givs_check (loop);
5004 /* Try to prove that the loop counter variable (if any) is always
5005 nonnegative; if so, record that fact with a REG_NONNEG note
5006 so that "decrement and branch until zero" insn can be used. */
5007 check_dbra_loop (loop, insn_count);
5009 /* Create reg_map to hold substitutions for replaceable giv regs.
5010 Some givs might have been made from biv increments, so look at
5011 ivs->reg_iv_type for a suitable size. */
5012 reg_map_size = ivs->n_regs;
5013 reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
5015 /* Examine each iv class for feasibility of strength reduction/induction
5016 variable elimination. */
5018 for (bl = ivs->list; bl; bl = bl->next)
5020 struct induction *v;
5021 int benefit;
5023 /* Test whether it will be possible to eliminate this biv
5024 provided all givs are reduced. */
5025 bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
5027 /* This will be true at the end, if all givs which depend on this
5028 biv have been strength reduced.
5029 We can't (currently) eliminate the biv unless this is so. */
5030 bl->all_reduced = 1;
5032 /* Check each extension dependent giv in this class to see if its
5033 root biv is safe from wrapping in the interior mode. */
5034 check_ext_dependent_givs (bl, loop_info);
5036 /* Combine all giv's for this iv_class. */
5037 combine_givs (regs, bl);
5039 for (v = bl->giv; v; v = v->next_iv)
5041 struct induction *tv;
5043 if (v->ignore || v->same)
5044 continue;
5046 benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
5048 /* If an insn is not to be strength reduced, then set its ignore
5049 flag, and clear bl->all_reduced. */
5051 /* A giv that depends on a reversed biv must be reduced if it is
5052 used after the loop exit, otherwise, it would have the wrong
5053 value after the loop exit. To make it simple, just reduce all
5054 of such giv's whether or not we know they are used after the loop
5055 exit. */
5057 if (! flag_reduce_all_givs
5058 && v->lifetime * threshold * benefit < insn_count
5059 && ! bl->reversed)
5061 if (loop_dump_stream)
5062 fprintf (loop_dump_stream,
5063 "giv of insn %d not worth while, %d vs %d.\n",
5064 INSN_UID (v->insn),
5065 v->lifetime * threshold * benefit, insn_count);
5066 v->ignore = 1;
5067 bl->all_reduced = 0;
5069 else
5071 /* Check that we can increment the reduced giv without a
5072 multiply insn. If not, reject it. */
5074 for (tv = bl->biv; tv; tv = tv->next_iv)
5075 if (tv->mult_val == const1_rtx
5076 && ! product_cheap_p (tv->add_val, v->mult_val))
5078 if (loop_dump_stream)
5079 fprintf (loop_dump_stream,
5080 "giv of insn %d: would need a multiply.\n",
5081 INSN_UID (v->insn));
5082 v->ignore = 1;
5083 bl->all_reduced = 0;
5084 break;
5089 /* Check for givs whose first use is their definition and whose
5090 last use is the definition of another giv. If so, it is likely
5091 dead and should not be used to derive another giv nor to
5092 eliminate a biv. */
5093 loop_givs_dead_check (loop, bl);
5095 /* Reduce each giv that we decided to reduce. */
5096 loop_givs_reduce (loop, bl);
5098 /* Rescan all givs. If a giv is the same as a giv not reduced, mark it
5099 as not reduced.
5101 For each giv register that can be reduced now: if replaceable,
5102 substitute reduced reg wherever the old giv occurs;
5103 else add new move insn "giv_reg = reduced_reg". */
5104 loop_givs_rescan (loop, bl, reg_map);
5106 /* All the givs based on the biv bl have been reduced if they
5107 merit it. */
5109 /* For each giv not marked as maybe dead that has been combined with a
5110 second giv, clear any "maybe dead" mark on that second giv.
5111 v->new_reg will either be or refer to the register of the giv it
5112 combined with.
5114 Doing this clearing avoids problems in biv elimination where
5115 a giv's new_reg is a complex value that can't be put in the
5116 insn but the giv combined with (with a reg as new_reg) is
5117 marked maybe_dead. Since the register will be used in either
5118 case, we'd prefer it be used from the simpler giv. */
5120 for (v = bl->giv; v; v = v->next_iv)
5121 if (! v->maybe_dead && v->same)
5122 v->same->maybe_dead = 0;
5124 /* Try to eliminate the biv, if it is a candidate.
5125 This won't work if ! bl->all_reduced,
5126 since the givs we planned to use might not have been reduced.
5128 We have to be careful that we didn't initially think we could
5129 eliminate this biv because of a giv that we now think may be
5130 dead and shouldn't be used as a biv replacement.
5132 Also, there is the possibility that we may have a giv that looks
5133 like it can be used to eliminate a biv, but the resulting insn
5134 isn't valid. This can happen, for example, on the 88k, where a
5135 JUMP_INSN can compare a register only with zero. Attempts to
5136 replace it with a compare with a constant will fail.
5138 Note that in cases where this call fails, we may have replaced some
5139 of the occurrences of the biv with a giv, but no harm was done in
5140 doing so in the rare cases where it can occur. */
5142 if (bl->all_reduced == 1 && bl->eliminable
5143 && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
5145 /* ?? If we created a new test to bypass the loop entirely,
5146 or otherwise drop straight in, based on this test, then
5147 we might want to rewrite it also. This way some later
5148 pass has more hope of removing the initialization of this
5149 biv entirely. */
5151 /* If final_value != 0, then the biv may be used after loop end
5152 and we must emit an insn to set it just in case.
5154 Reversed bivs already have an insn after the loop setting their
5155 value, so we don't need another one. We can't calculate the
5156 proper final value for such a biv here anyways. */
5157 if (bl->final_value && ! bl->reversed)
5158 loop_insn_sink_or_swim (loop, gen_move_insn
5159 (bl->biv->dest_reg, bl->final_value));
5161 if (loop_dump_stream)
5162 fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5163 bl->regno);
5165 /* See above note wrt final_value. But since we couldn't eliminate
5166 the biv, we must set the value after the loop instead of before. */
5167 else if (bl->final_value && ! bl->reversed)
5168 loop_insn_sink (loop, gen_move_insn (bl->biv->dest_reg,
5169 bl->final_value));
5172 /* Go through all the instructions in the loop, making all the
5173 register substitutions scheduled in REG_MAP. */
5175 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
5176 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5177 || GET_CODE (p) == CALL_INSN)
5179 replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5180 replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
5181 INSN_CODE (p) = -1;
5184 if (loop_info->n_iterations > 0)
5186 /* When we completely unroll a loop we will likely not need the increment
5187 of the loop BIV and we will not need the conditional branch at the
5188 end of the loop. */
5189 unrolled_insn_copies = insn_count - 2;
5191 #ifdef HAVE_cc0
5192 /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5193 need the comparison before the conditional branch at the end of the
5194 loop. */
5195 unrolled_insn_copies -= 1;
5196 #endif
5198 /* We'll need one copy for each loop iteration. */
5199 unrolled_insn_copies *= loop_info->n_iterations;
5201 /* A little slop to account for the ability to remove initialization
5202 code, better CSE, and other secondary benefits of completely
5203 unrolling some loops. */
5204 unrolled_insn_copies -= 1;
5206 /* Clamp the value. */
5207 if (unrolled_insn_copies < 0)
5208 unrolled_insn_copies = 0;
5211 /* Unroll loops from within strength reduction so that we can use the
5212 induction variable information that strength_reduce has already
5213 collected. Always unroll loops that would be as small or smaller
5214 unrolled than when rolled. */
5215 if ((flags & LOOP_UNROLL)
5216 || (loop_info->n_iterations > 0
5217 && unrolled_insn_copies <= insn_count))
5218 unroll_loop (loop, insn_count, 1);
5220 #ifdef HAVE_doloop_end
5221 if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
5222 doloop_optimize (loop);
5223 #endif /* HAVE_doloop_end */
5225 /* In case number of iterations is known, drop branch prediction note
5226 in the branch. Do that only in second loop pass, as loop unrolling
5227 may change the number of iterations performed. */
5228 if (flags & LOOP_BCT)
5230 unsigned HOST_WIDE_INT n
5231 = loop_info->n_iterations / loop_info->unroll_number;
5232 if (n > 1)
5233 predict_insn (PREV_INSN (loop->end), PRED_LOOP_ITERATIONS,
5234 REG_BR_PROB_BASE - REG_BR_PROB_BASE / n);
5237 if (loop_dump_stream)
5238 fprintf (loop_dump_stream, "\n");
5240 loop_ivs_free (loop);
5241 if (reg_map)
5242 free (reg_map);
5245 /*Record all basic induction variables calculated in the insn. */
5246 static rtx
5247 check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
5248 struct loop *loop;
5249 rtx p;
5250 int not_every_iteration;
5251 int maybe_multiple;
5253 struct loop_ivs *ivs = LOOP_IVS (loop);
5254 rtx set;
5255 rtx dest_reg;
5256 rtx inc_val;
5257 rtx mult_val;
5258 rtx *location;
5260 if (GET_CODE (p) == INSN
5261 && (set = single_set (p))
5262 && GET_CODE (SET_DEST (set)) == REG)
5264 dest_reg = SET_DEST (set);
5265 if (REGNO (dest_reg) < max_reg_before_loop
5266 && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
5267 && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
5269 if (basic_induction_var (loop, SET_SRC (set),
5270 GET_MODE (SET_SRC (set)),
5271 dest_reg, p, &inc_val, &mult_val,
5272 &location))
5274 /* It is a possible basic induction variable.
5275 Create and initialize an induction structure for it. */
5277 struct induction *v
5278 = (struct induction *) xmalloc (sizeof (struct induction));
5280 record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
5281 not_every_iteration, maybe_multiple);
5282 REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
5284 else if (REGNO (dest_reg) < ivs->n_regs)
5285 REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
5288 return p;
5291 /* Record all givs calculated in the insn.
5292 A register is a giv if: it is only set once, it is a function of a
5293 biv and a constant (or invariant), and it is not a biv. */
5294 static rtx
5295 check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
5296 struct loop *loop;
5297 rtx p;
5298 int not_every_iteration;
5299 int maybe_multiple;
5301 struct loop_regs *regs = LOOP_REGS (loop);
5303 rtx set;
5304 /* Look for a general induction variable in a register. */
5305 if (GET_CODE (p) == INSN
5306 && (set = single_set (p))
5307 && GET_CODE (SET_DEST (set)) == REG
5308 && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
5310 rtx src_reg;
5311 rtx dest_reg;
5312 rtx add_val;
5313 rtx mult_val;
5314 rtx ext_val;
5315 int benefit;
5316 rtx regnote = 0;
5317 rtx last_consec_insn;
5319 dest_reg = SET_DEST (set);
5320 if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
5321 return p;
5323 if (/* SET_SRC is a giv. */
5324 (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
5325 &mult_val, &ext_val, 0, &benefit, VOIDmode)
5326 /* Equivalent expression is a giv. */
5327 || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
5328 && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
5329 &add_val, &mult_val, &ext_val, 0,
5330 &benefit, VOIDmode)))
5331 /* Don't try to handle any regs made by loop optimization.
5332 We have nothing on them in regno_first_uid, etc. */
5333 && REGNO (dest_reg) < max_reg_before_loop
5334 /* Don't recognize a BASIC_INDUCT_VAR here. */
5335 && dest_reg != src_reg
5336 /* This must be the only place where the register is set. */
5337 && (regs->array[REGNO (dest_reg)].n_times_set == 1
5338 /* or all sets must be consecutive and make a giv. */
5339 || (benefit = consec_sets_giv (loop, benefit, p,
5340 src_reg, dest_reg,
5341 &add_val, &mult_val, &ext_val,
5342 &last_consec_insn))))
5344 struct induction *v
5345 = (struct induction *) xmalloc (sizeof (struct induction));
5347 /* If this is a library call, increase benefit. */
5348 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5349 benefit += libcall_benefit (p);
5351 /* Skip the consecutive insns, if there are any. */
5352 if (regs->array[REGNO (dest_reg)].n_times_set != 1)
5353 p = last_consec_insn;
5355 record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
5356 ext_val, benefit, DEST_REG, not_every_iteration,
5357 maybe_multiple, (rtx*) 0);
5362 #ifndef DONT_REDUCE_ADDR
5363 /* Look for givs which are memory addresses. */
5364 /* This resulted in worse code on a VAX 8600. I wonder if it
5365 still does. */
5366 if (GET_CODE (p) == INSN)
5367 find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
5368 maybe_multiple);
5369 #endif
5371 /* Update the status of whether giv can derive other givs. This can
5372 change when we pass a label or an insn that updates a biv. */
5373 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5374 || GET_CODE (p) == CODE_LABEL)
5375 update_giv_derive (loop, p);
5376 return p;
5379 /* Return 1 if X is a valid source for an initial value (or as value being
5380 compared against in an initial test).
5382 X must be either a register or constant and must not be clobbered between
5383 the current insn and the start of the loop.
5385 INSN is the insn containing X. */
5387 static int
5388 valid_initial_value_p (x, insn, call_seen, loop_start)
5389 rtx x;
5390 rtx insn;
5391 int call_seen;
5392 rtx loop_start;
5394 if (CONSTANT_P (x))
5395 return 1;
5397 /* Only consider pseudos we know about initialized in insns whose luids
5398 we know. */
5399 if (GET_CODE (x) != REG
5400 || REGNO (x) >= max_reg_before_loop)
5401 return 0;
5403 /* Don't use call-clobbered registers across a call which clobbers it. On
5404 some machines, don't use any hard registers at all. */
5405 if (REGNO (x) < FIRST_PSEUDO_REGISTER
5406 && (SMALL_REGISTER_CLASSES
5407 || (call_used_regs[REGNO (x)] && call_seen)))
5408 return 0;
5410 /* Don't use registers that have been clobbered before the start of the
5411 loop. */
5412 if (reg_set_between_p (x, insn, loop_start))
5413 return 0;
5415 return 1;
5418 /* Scan X for memory refs and check each memory address
5419 as a possible giv. INSN is the insn whose pattern X comes from.
5420 NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5421 every loop iteration. MAYBE_MULTIPLE is 1 if the insn might be executed
5422 more thanonce in each loop iteration. */
5424 static void
5425 find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
5426 const struct loop *loop;
5427 rtx x;
5428 rtx insn;
5429 int not_every_iteration, maybe_multiple;
5431 int i, j;
5432 enum rtx_code code;
5433 const char *fmt;
5435 if (x == 0)
5436 return;
5438 code = GET_CODE (x);
5439 switch (code)
5441 case REG:
5442 case CONST_INT:
5443 case CONST:
5444 case CONST_DOUBLE:
5445 case SYMBOL_REF:
5446 case LABEL_REF:
5447 case PC:
5448 case CC0:
5449 case ADDR_VEC:
5450 case ADDR_DIFF_VEC:
5451 case USE:
5452 case CLOBBER:
5453 return;
5455 case MEM:
5457 rtx src_reg;
5458 rtx add_val;
5459 rtx mult_val;
5460 rtx ext_val;
5461 int benefit;
5463 /* This code used to disable creating GIVs with mult_val == 1 and
5464 add_val == 0. However, this leads to lost optimizations when
5465 it comes time to combine a set of related DEST_ADDR GIVs, since
5466 this one would not be seen. */
5468 if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
5469 &mult_val, &ext_val, 1, &benefit,
5470 GET_MODE (x)))
5472 /* Found one; record it. */
5473 struct induction *v
5474 = (struct induction *) xmalloc (sizeof (struct induction));
5476 record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
5477 add_val, ext_val, benefit, DEST_ADDR,
5478 not_every_iteration, maybe_multiple, &XEXP (x, 0));
5480 v->mem = x;
5483 return;
5485 default:
5486 break;
5489 /* Recursively scan the subexpressions for other mem refs. */
5491 fmt = GET_RTX_FORMAT (code);
5492 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5493 if (fmt[i] == 'e')
5494 find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
5495 maybe_multiple);
5496 else if (fmt[i] == 'E')
5497 for (j = 0; j < XVECLEN (x, i); j++)
5498 find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
5499 maybe_multiple);
5502 /* Fill in the data about one biv update.
5503 V is the `struct induction' in which we record the biv. (It is
5504 allocated by the caller, with alloca.)
5505 INSN is the insn that sets it.
5506 DEST_REG is the biv's reg.
5508 MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5509 INC_VAL is the increment. Otherwise, MULT_VAL is const0_rtx and the biv is
5510 being set to INC_VAL.
5512 NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5513 executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5514 can be executed more than once per iteration. If MAYBE_MULTIPLE
5515 and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5516 executed exactly once per iteration. */
5518 static void
5519 record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
5520 not_every_iteration, maybe_multiple)
5521 struct loop *loop;
5522 struct induction *v;
5523 rtx insn;
5524 rtx dest_reg;
5525 rtx inc_val;
5526 rtx mult_val;
5527 rtx *location;
5528 int not_every_iteration;
5529 int maybe_multiple;
5531 struct loop_ivs *ivs = LOOP_IVS (loop);
5532 struct iv_class *bl;
5534 v->insn = insn;
5535 v->src_reg = dest_reg;
5536 v->dest_reg = dest_reg;
5537 v->mult_val = mult_val;
5538 v->add_val = inc_val;
5539 v->ext_dependent = NULL_RTX;
5540 v->location = location;
5541 v->mode = GET_MODE (dest_reg);
5542 v->always_computable = ! not_every_iteration;
5543 v->always_executed = ! not_every_iteration;
5544 v->maybe_multiple = maybe_multiple;
5546 /* Add this to the reg's iv_class, creating a class
5547 if this is the first incrementation of the reg. */
5549 bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
5550 if (bl == 0)
5552 /* Create and initialize new iv_class. */
5554 bl = (struct iv_class *) xmalloc (sizeof (struct iv_class));
5556 bl->regno = REGNO (dest_reg);
5557 bl->biv = 0;
5558 bl->giv = 0;
5559 bl->biv_count = 0;
5560 bl->giv_count = 0;
5562 /* Set initial value to the reg itself. */
5563 bl->initial_value = dest_reg;
5564 bl->final_value = 0;
5565 /* We haven't seen the initializing insn yet */
5566 bl->init_insn = 0;
5567 bl->init_set = 0;
5568 bl->initial_test = 0;
5569 bl->incremented = 0;
5570 bl->eliminable = 0;
5571 bl->nonneg = 0;
5572 bl->reversed = 0;
5573 bl->total_benefit = 0;
5575 /* Add this class to ivs->list. */
5576 bl->next = ivs->list;
5577 ivs->list = bl;
5579 /* Put it in the array of biv register classes. */
5580 REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
5583 /* Update IV_CLASS entry for this biv. */
5584 v->next_iv = bl->biv;
5585 bl->biv = v;
5586 bl->biv_count++;
5587 if (mult_val == const1_rtx)
5588 bl->incremented = 1;
5590 if (loop_dump_stream)
5591 loop_biv_dump (v, loop_dump_stream, 0);
5594 /* Fill in the data about one giv.
5595 V is the `struct induction' in which we record the giv. (It is
5596 allocated by the caller, with alloca.)
5597 INSN is the insn that sets it.
5598 BENEFIT estimates the savings from deleting this insn.
5599 TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5600 into a register or is used as a memory address.
5602 SRC_REG is the biv reg which the giv is computed from.
5603 DEST_REG is the giv's reg (if the giv is stored in a reg).
5604 MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5605 LOCATION points to the place where this giv's value appears in INSN. */
5607 static void
5608 record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
5609 benefit, type, not_every_iteration, maybe_multiple, location)
5610 const struct loop *loop;
5611 struct induction *v;
5612 rtx insn;
5613 rtx src_reg;
5614 rtx dest_reg;
5615 rtx mult_val, add_val, ext_val;
5616 int benefit;
5617 enum g_types type;
5618 int not_every_iteration, maybe_multiple;
5619 rtx *location;
5621 struct loop_ivs *ivs = LOOP_IVS (loop);
5622 struct induction *b;
5623 struct iv_class *bl;
5624 rtx set = single_set (insn);
5625 rtx temp;
5627 /* Attempt to prove constantness of the values. Don't let simplity_rtx
5628 undo the MULT canonicalization that we performed earlier. */
5629 temp = simplify_rtx (add_val);
5630 if (temp
5631 && ! (GET_CODE (add_val) == MULT
5632 && GET_CODE (temp) == ASHIFT))
5633 add_val = temp;
5635 v->insn = insn;
5636 v->src_reg = src_reg;
5637 v->giv_type = type;
5638 v->dest_reg = dest_reg;
5639 v->mult_val = mult_val;
5640 v->add_val = add_val;
5641 v->ext_dependent = ext_val;
5642 v->benefit = benefit;
5643 v->location = location;
5644 v->cant_derive = 0;
5645 v->combined_with = 0;
5646 v->maybe_multiple = maybe_multiple;
5647 v->maybe_dead = 0;
5648 v->derive_adjustment = 0;
5649 v->same = 0;
5650 v->ignore = 0;
5651 v->new_reg = 0;
5652 v->final_value = 0;
5653 v->same_insn = 0;
5654 v->auto_inc_opt = 0;
5655 v->unrolled = 0;
5656 v->shared = 0;
5658 /* The v->always_computable field is used in update_giv_derive, to
5659 determine whether a giv can be used to derive another giv. For a
5660 DEST_REG giv, INSN computes a new value for the giv, so its value
5661 isn't computable if INSN insn't executed every iteration.
5662 However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5663 it does not compute a new value. Hence the value is always computable
5664 regardless of whether INSN is executed each iteration. */
5666 if (type == DEST_ADDR)
5667 v->always_computable = 1;
5668 else
5669 v->always_computable = ! not_every_iteration;
5671 v->always_executed = ! not_every_iteration;
5673 if (type == DEST_ADDR)
5675 v->mode = GET_MODE (*location);
5676 v->lifetime = 1;
5678 else /* type == DEST_REG */
5680 v->mode = GET_MODE (SET_DEST (set));
5682 v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
5684 /* If the lifetime is zero, it means that this register is
5685 really a dead store. So mark this as a giv that can be
5686 ignored. This will not prevent the biv from being eliminated. */
5687 if (v->lifetime == 0)
5688 v->ignore = 1;
5690 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
5691 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
5694 /* Add the giv to the class of givs computed from one biv. */
5696 bl = REG_IV_CLASS (ivs, REGNO (src_reg));
5697 if (bl)
5699 v->next_iv = bl->giv;
5700 bl->giv = v;
5701 /* Don't count DEST_ADDR. This is supposed to count the number of
5702 insns that calculate givs. */
5703 if (type == DEST_REG)
5704 bl->giv_count++;
5705 bl->total_benefit += benefit;
5707 else
5708 /* Fatal error, biv missing for this giv? */
5709 abort ();
5711 if (type == DEST_ADDR)
5712 v->replaceable = 1;
5713 else
5715 /* The giv can be replaced outright by the reduced register only if all
5716 of the following conditions are true:
5717 - the insn that sets the giv is always executed on any iteration
5718 on which the giv is used at all
5719 (there are two ways to deduce this:
5720 either the insn is executed on every iteration,
5721 or all uses follow that insn in the same basic block),
5722 - the giv is not used outside the loop
5723 - no assignments to the biv occur during the giv's lifetime. */
5725 if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5726 /* Previous line always fails if INSN was moved by loop opt. */
5727 && REGNO_LAST_LUID (REGNO (dest_reg))
5728 < INSN_LUID (loop->end)
5729 && (! not_every_iteration
5730 || last_use_this_basic_block (dest_reg, insn)))
5732 /* Now check that there are no assignments to the biv within the
5733 giv's lifetime. This requires two separate checks. */
5735 /* Check each biv update, and fail if any are between the first
5736 and last use of the giv.
5738 If this loop contains an inner loop that was unrolled, then
5739 the insn modifying the biv may have been emitted by the loop
5740 unrolling code, and hence does not have a valid luid. Just
5741 mark the biv as not replaceable in this case. It is not very
5742 useful as a biv, because it is used in two different loops.
5743 It is very unlikely that we would be able to optimize the giv
5744 using this biv anyways. */
5746 v->replaceable = 1;
5747 for (b = bl->biv; b; b = b->next_iv)
5749 if (INSN_UID (b->insn) >= max_uid_for_loop
5750 || ((INSN_LUID (b->insn)
5751 >= REGNO_FIRST_LUID (REGNO (dest_reg)))
5752 && (INSN_LUID (b->insn)
5753 <= REGNO_LAST_LUID (REGNO (dest_reg)))))
5755 v->replaceable = 0;
5756 v->not_replaceable = 1;
5757 break;
5761 /* If there are any backwards branches that go from after the
5762 biv update to before it, then this giv is not replaceable. */
5763 if (v->replaceable)
5764 for (b = bl->biv; b; b = b->next_iv)
5765 if (back_branch_in_range_p (loop, b->insn))
5767 v->replaceable = 0;
5768 v->not_replaceable = 1;
5769 break;
5772 else
5774 /* May still be replaceable, we don't have enough info here to
5775 decide. */
5776 v->replaceable = 0;
5777 v->not_replaceable = 0;
5781 /* Record whether the add_val contains a const_int, for later use by
5782 combine_givs. */
5784 rtx tem = add_val;
5786 v->no_const_addval = 1;
5787 if (tem == const0_rtx)
5789 else if (CONSTANT_P (add_val))
5790 v->no_const_addval = 0;
5791 if (GET_CODE (tem) == PLUS)
5793 while (1)
5795 if (GET_CODE (XEXP (tem, 0)) == PLUS)
5796 tem = XEXP (tem, 0);
5797 else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5798 tem = XEXP (tem, 1);
5799 else
5800 break;
5802 if (CONSTANT_P (XEXP (tem, 1)))
5803 v->no_const_addval = 0;
5807 if (loop_dump_stream)
5808 loop_giv_dump (v, loop_dump_stream, 0);
5811 /* All this does is determine whether a giv can be made replaceable because
5812 its final value can be calculated. This code can not be part of record_giv
5813 above, because final_giv_value requires that the number of loop iterations
5814 be known, and that can not be accurately calculated until after all givs
5815 have been identified. */
5817 static void
5818 check_final_value (loop, v)
5819 const struct loop *loop;
5820 struct induction *v;
5822 struct loop_ivs *ivs = LOOP_IVS (loop);
5823 struct iv_class *bl;
5824 rtx final_value = 0;
5826 bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5828 /* DEST_ADDR givs will never reach here, because they are always marked
5829 replaceable above in record_giv. */
5831 /* The giv can be replaced outright by the reduced register only if all
5832 of the following conditions are true:
5833 - the insn that sets the giv is always executed on any iteration
5834 on which the giv is used at all
5835 (there are two ways to deduce this:
5836 either the insn is executed on every iteration,
5837 or all uses follow that insn in the same basic block),
5838 - its final value can be calculated (this condition is different
5839 than the one above in record_giv)
5840 - it's not used before the it's set
5841 - no assignments to the biv occur during the giv's lifetime. */
5843 #if 0
5844 /* This is only called now when replaceable is known to be false. */
5845 /* Clear replaceable, so that it won't confuse final_giv_value. */
5846 v->replaceable = 0;
5847 #endif
5849 if ((final_value = final_giv_value (loop, v))
5850 && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5852 int biv_increment_seen = 0, before_giv_insn = 0;
5853 rtx p = v->insn;
5854 rtx last_giv_use;
5856 v->replaceable = 1;
5858 /* When trying to determine whether or not a biv increment occurs
5859 during the lifetime of the giv, we can ignore uses of the variable
5860 outside the loop because final_value is true. Hence we can not
5861 use regno_last_uid and regno_first_uid as above in record_giv. */
5863 /* Search the loop to determine whether any assignments to the
5864 biv occur during the giv's lifetime. Start with the insn
5865 that sets the giv, and search around the loop until we come
5866 back to that insn again.
5868 Also fail if there is a jump within the giv's lifetime that jumps
5869 to somewhere outside the lifetime but still within the loop. This
5870 catches spaghetti code where the execution order is not linear, and
5871 hence the above test fails. Here we assume that the giv lifetime
5872 does not extend from one iteration of the loop to the next, so as
5873 to make the test easier. Since the lifetime isn't known yet,
5874 this requires two loops. See also record_giv above. */
5876 last_giv_use = v->insn;
5878 while (1)
5880 p = NEXT_INSN (p);
5881 if (p == loop->end)
5883 before_giv_insn = 1;
5884 p = NEXT_INSN (loop->start);
5886 if (p == v->insn)
5887 break;
5889 if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5890 || GET_CODE (p) == CALL_INSN)
5892 /* It is possible for the BIV increment to use the GIV if we
5893 have a cycle. Thus we must be sure to check each insn for
5894 both BIV and GIV uses, and we must check for BIV uses
5895 first. */
5897 if (! biv_increment_seen
5898 && reg_set_p (v->src_reg, PATTERN (p)))
5899 biv_increment_seen = 1;
5901 if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
5903 if (biv_increment_seen || before_giv_insn)
5905 v->replaceable = 0;
5906 v->not_replaceable = 1;
5907 break;
5909 last_giv_use = p;
5914 /* Now that the lifetime of the giv is known, check for branches
5915 from within the lifetime to outside the lifetime if it is still
5916 replaceable. */
5918 if (v->replaceable)
5920 p = v->insn;
5921 while (1)
5923 p = NEXT_INSN (p);
5924 if (p == loop->end)
5925 p = NEXT_INSN (loop->start);
5926 if (p == last_giv_use)
5927 break;
5929 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
5930 && LABEL_NAME (JUMP_LABEL (p))
5931 && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
5932 && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
5933 || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
5934 && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
5936 v->replaceable = 0;
5937 v->not_replaceable = 1;
5939 if (loop_dump_stream)
5940 fprintf (loop_dump_stream,
5941 "Found branch outside giv lifetime.\n");
5943 break;
5948 /* If it is replaceable, then save the final value. */
5949 if (v->replaceable)
5950 v->final_value = final_value;
5953 if (loop_dump_stream && v->replaceable)
5954 fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
5955 INSN_UID (v->insn), REGNO (v->dest_reg));
5958 /* Update the status of whether a giv can derive other givs.
5960 We need to do something special if there is or may be an update to the biv
5961 between the time the giv is defined and the time it is used to derive
5962 another giv.
5964 In addition, a giv that is only conditionally set is not allowed to
5965 derive another giv once a label has been passed.
5967 The cases we look at are when a label or an update to a biv is passed. */
5969 static void
5970 update_giv_derive (loop, p)
5971 const struct loop *loop;
5972 rtx p;
5974 struct loop_ivs *ivs = LOOP_IVS (loop);
5975 struct iv_class *bl;
5976 struct induction *biv, *giv;
5977 rtx tem;
5978 int dummy;
5980 /* Search all IV classes, then all bivs, and finally all givs.
5982 There are three cases we are concerned with. First we have the situation
5983 of a giv that is only updated conditionally. In that case, it may not
5984 derive any givs after a label is passed.
5986 The second case is when a biv update occurs, or may occur, after the
5987 definition of a giv. For certain biv updates (see below) that are
5988 known to occur between the giv definition and use, we can adjust the
5989 giv definition. For others, or when the biv update is conditional,
5990 we must prevent the giv from deriving any other givs. There are two
5991 sub-cases within this case.
5993 If this is a label, we are concerned with any biv update that is done
5994 conditionally, since it may be done after the giv is defined followed by
5995 a branch here (actually, we need to pass both a jump and a label, but
5996 this extra tracking doesn't seem worth it).
5998 If this is a jump, we are concerned about any biv update that may be
5999 executed multiple times. We are actually only concerned about
6000 backward jumps, but it is probably not worth performing the test
6001 on the jump again here.
6003 If this is a biv update, we must adjust the giv status to show that a
6004 subsequent biv update was performed. If this adjustment cannot be done,
6005 the giv cannot derive further givs. */
6007 for (bl = ivs->list; bl; bl = bl->next)
6008 for (biv = bl->biv; biv; biv = biv->next_iv)
6009 if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
6010 || biv->insn == p)
6012 for (giv = bl->giv; giv; giv = giv->next_iv)
6014 /* If cant_derive is already true, there is no point in
6015 checking all of these conditions again. */
6016 if (giv->cant_derive)
6017 continue;
6019 /* If this giv is conditionally set and we have passed a label,
6020 it cannot derive anything. */
6021 if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
6022 giv->cant_derive = 1;
6024 /* Skip givs that have mult_val == 0, since
6025 they are really invariants. Also skip those that are
6026 replaceable, since we know their lifetime doesn't contain
6027 any biv update. */
6028 else if (giv->mult_val == const0_rtx || giv->replaceable)
6029 continue;
6031 /* The only way we can allow this giv to derive another
6032 is if this is a biv increment and we can form the product
6033 of biv->add_val and giv->mult_val. In this case, we will
6034 be able to compute a compensation. */
6035 else if (biv->insn == p)
6037 rtx ext_val_dummy;
6039 tem = 0;
6040 if (biv->mult_val == const1_rtx)
6041 tem = simplify_giv_expr (loop,
6042 gen_rtx_MULT (giv->mode,
6043 biv->add_val,
6044 giv->mult_val),
6045 &ext_val_dummy, &dummy);
6047 if (tem && giv->derive_adjustment)
6048 tem = simplify_giv_expr
6049 (loop,
6050 gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
6051 &ext_val_dummy, &dummy);
6053 if (tem)
6054 giv->derive_adjustment = tem;
6055 else
6056 giv->cant_derive = 1;
6058 else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
6059 || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
6060 giv->cant_derive = 1;
6065 /* Check whether an insn is an increment legitimate for a basic induction var.
6066 X is the source of insn P, or a part of it.
6067 MODE is the mode in which X should be interpreted.
6069 DEST_REG is the putative biv, also the destination of the insn.
6070 We accept patterns of these forms:
6071 REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6072 REG = INVARIANT + REG
6074 If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6075 store the additive term into *INC_VAL, and store the place where
6076 we found the additive term into *LOCATION.
6078 If X is an assignment of an invariant into DEST_REG, we set
6079 *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6081 We also want to detect a BIV when it corresponds to a variable
6082 whose mode was promoted via PROMOTED_MODE. In that case, an increment
6083 of the variable may be a PLUS that adds a SUBREG of that variable to
6084 an invariant and then sign- or zero-extends the result of the PLUS
6085 into the variable.
6087 Most GIVs in such cases will be in the promoted mode, since that is the
6088 probably the natural computation mode (and almost certainly the mode
6089 used for addresses) on the machine. So we view the pseudo-reg containing
6090 the variable as the BIV, as if it were simply incremented.
6092 Note that treating the entire pseudo as a BIV will result in making
6093 simple increments to any GIVs based on it. However, if the variable
6094 overflows in its declared mode but not its promoted mode, the result will
6095 be incorrect. This is acceptable if the variable is signed, since
6096 overflows in such cases are undefined, but not if it is unsigned, since
6097 those overflows are defined. So we only check for SIGN_EXTEND and
6098 not ZERO_EXTEND.
6100 If we cannot find a biv, we return 0. */
6102 static int
6103 basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
6104 const struct loop *loop;
6105 rtx x;
6106 enum machine_mode mode;
6107 rtx dest_reg;
6108 rtx p;
6109 rtx *inc_val;
6110 rtx *mult_val;
6111 rtx **location;
6113 enum rtx_code code;
6114 rtx *argp, arg;
6115 rtx insn, set = 0;
6117 code = GET_CODE (x);
6118 *location = NULL;
6119 switch (code)
6121 case PLUS:
6122 if (rtx_equal_p (XEXP (x, 0), dest_reg)
6123 || (GET_CODE (XEXP (x, 0)) == SUBREG
6124 && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
6125 && SUBREG_REG (XEXP (x, 0)) == dest_reg))
6127 argp = &XEXP (x, 1);
6129 else if (rtx_equal_p (XEXP (x, 1), dest_reg)
6130 || (GET_CODE (XEXP (x, 1)) == SUBREG
6131 && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
6132 && SUBREG_REG (XEXP (x, 1)) == dest_reg))
6134 argp = &XEXP (x, 0);
6136 else
6137 return 0;
6139 arg = *argp;
6140 if (loop_invariant_p (loop, arg) != 1)
6141 return 0;
6143 *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
6144 *mult_val = const1_rtx;
6145 *location = argp;
6146 return 1;
6148 case SUBREG:
6149 /* If what's inside the SUBREG is a BIV, then the SUBREG. This will
6150 handle addition of promoted variables.
6151 ??? The comment at the start of this function is wrong: promoted
6152 variable increments don't look like it says they do. */
6153 return basic_induction_var (loop, SUBREG_REG (x),
6154 GET_MODE (SUBREG_REG (x)),
6155 dest_reg, p, inc_val, mult_val, location);
6157 case REG:
6158 /* If this register is assigned in a previous insn, look at its
6159 source, but don't go outside the loop or past a label. */
6161 /* If this sets a register to itself, we would repeat any previous
6162 biv increment if we applied this strategy blindly. */
6163 if (rtx_equal_p (dest_reg, x))
6164 return 0;
6166 insn = p;
6167 while (1)
6169 rtx dest;
6172 insn = PREV_INSN (insn);
6174 while (insn && GET_CODE (insn) == NOTE
6175 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6177 if (!insn)
6178 break;
6179 set = single_set (insn);
6180 if (set == 0)
6181 break;
6182 dest = SET_DEST (set);
6183 if (dest == x
6184 || (GET_CODE (dest) == SUBREG
6185 && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
6186 && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
6187 && SUBREG_REG (dest) == x))
6188 return basic_induction_var (loop, SET_SRC (set),
6189 (GET_MODE (SET_SRC (set)) == VOIDmode
6190 ? GET_MODE (x)
6191 : GET_MODE (SET_SRC (set))),
6192 dest_reg, insn,
6193 inc_val, mult_val, location);
6195 while (GET_CODE (dest) == SIGN_EXTRACT
6196 || GET_CODE (dest) == ZERO_EXTRACT
6197 || GET_CODE (dest) == SUBREG
6198 || GET_CODE (dest) == STRICT_LOW_PART)
6199 dest = XEXP (dest, 0);
6200 if (dest == x)
6201 break;
6203 /* Fall through. */
6205 /* Can accept constant setting of biv only when inside inner most loop.
6206 Otherwise, a biv of an inner loop may be incorrectly recognized
6207 as a biv of the outer loop,
6208 causing code to be moved INTO the inner loop. */
6209 case MEM:
6210 if (loop_invariant_p (loop, x) != 1)
6211 return 0;
6212 case CONST_INT:
6213 case SYMBOL_REF:
6214 case CONST:
6215 /* convert_modes aborts if we try to convert to or from CCmode, so just
6216 exclude that case. It is very unlikely that a condition code value
6217 would be a useful iterator anyways. convert_modes aborts if we try to
6218 convert a float mode to non-float or vice versa too. */
6219 if (loop->level == 1
6220 && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
6221 && GET_MODE_CLASS (mode) != MODE_CC)
6223 /* Possible bug here? Perhaps we don't know the mode of X. */
6224 *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
6225 *mult_val = const0_rtx;
6226 return 1;
6228 else
6229 return 0;
6231 case SIGN_EXTEND:
6232 return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6233 dest_reg, p, inc_val, mult_val, location);
6235 case ASHIFTRT:
6236 /* Similar, since this can be a sign extension. */
6237 for (insn = PREV_INSN (p);
6238 (insn && GET_CODE (insn) == NOTE
6239 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6240 insn = PREV_INSN (insn))
6243 if (insn)
6244 set = single_set (insn);
6246 if (! rtx_equal_p (dest_reg, XEXP (x, 0))
6247 && set && SET_DEST (set) == XEXP (x, 0)
6248 && GET_CODE (XEXP (x, 1)) == CONST_INT
6249 && INTVAL (XEXP (x, 1)) >= 0
6250 && GET_CODE (SET_SRC (set)) == ASHIFT
6251 && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
6252 return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
6253 GET_MODE (XEXP (x, 0)),
6254 dest_reg, insn, inc_val, mult_val,
6255 location);
6256 return 0;
6258 default:
6259 return 0;
6263 /* A general induction variable (giv) is any quantity that is a linear
6264 function of a basic induction variable,
6265 i.e. giv = biv * mult_val + add_val.
6266 The coefficients can be any loop invariant quantity.
6267 A giv need not be computed directly from the biv;
6268 it can be computed by way of other givs. */
6270 /* Determine whether X computes a giv.
6271 If it does, return a nonzero value
6272 which is the benefit from eliminating the computation of X;
6273 set *SRC_REG to the register of the biv that it is computed from;
6274 set *ADD_VAL and *MULT_VAL to the coefficients,
6275 such that the value of X is biv * mult + add; */
6277 static int
6278 general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
6279 is_addr, pbenefit, addr_mode)
6280 const struct loop *loop;
6281 rtx x;
6282 rtx *src_reg;
6283 rtx *add_val;
6284 rtx *mult_val;
6285 rtx *ext_val;
6286 int is_addr;
6287 int *pbenefit;
6288 enum machine_mode addr_mode;
6290 struct loop_ivs *ivs = LOOP_IVS (loop);
6291 rtx orig_x = x;
6293 /* If this is an invariant, forget it, it isn't a giv. */
6294 if (loop_invariant_p (loop, x) == 1)
6295 return 0;
6297 *pbenefit = 0;
6298 *ext_val = NULL_RTX;
6299 x = simplify_giv_expr (loop, x, ext_val, pbenefit);
6300 if (x == 0)
6301 return 0;
6303 switch (GET_CODE (x))
6305 case USE:
6306 case CONST_INT:
6307 /* Since this is now an invariant and wasn't before, it must be a giv
6308 with MULT_VAL == 0. It doesn't matter which BIV we associate this
6309 with. */
6310 *src_reg = ivs->list->biv->dest_reg;
6311 *mult_val = const0_rtx;
6312 *add_val = x;
6313 break;
6315 case REG:
6316 /* This is equivalent to a BIV. */
6317 *src_reg = x;
6318 *mult_val = const1_rtx;
6319 *add_val = const0_rtx;
6320 break;
6322 case PLUS:
6323 /* Either (plus (biv) (invar)) or
6324 (plus (mult (biv) (invar_1)) (invar_2)). */
6325 if (GET_CODE (XEXP (x, 0)) == MULT)
6327 *src_reg = XEXP (XEXP (x, 0), 0);
6328 *mult_val = XEXP (XEXP (x, 0), 1);
6330 else
6332 *src_reg = XEXP (x, 0);
6333 *mult_val = const1_rtx;
6335 *add_val = XEXP (x, 1);
6336 break;
6338 case MULT:
6339 /* ADD_VAL is zero. */
6340 *src_reg = XEXP (x, 0);
6341 *mult_val = XEXP (x, 1);
6342 *add_val = const0_rtx;
6343 break;
6345 default:
6346 abort ();
6349 /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6350 unless they are CONST_INT). */
6351 if (GET_CODE (*add_val) == USE)
6352 *add_val = XEXP (*add_val, 0);
6353 if (GET_CODE (*mult_val) == USE)
6354 *mult_val = XEXP (*mult_val, 0);
6356 if (is_addr)
6357 *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
6358 else
6359 *pbenefit += rtx_cost (orig_x, SET);
6361 /* Always return true if this is a giv so it will be detected as such,
6362 even if the benefit is zero or negative. This allows elimination
6363 of bivs that might otherwise not be eliminated. */
6364 return 1;
6367 /* Given an expression, X, try to form it as a linear function of a biv.
6368 We will canonicalize it to be of the form
6369 (plus (mult (BIV) (invar_1))
6370 (invar_2))
6371 with possible degeneracies.
6373 The invariant expressions must each be of a form that can be used as a
6374 machine operand. We surround then with a USE rtx (a hack, but localized
6375 and certainly unambiguous!) if not a CONST_INT for simplicity in this
6376 routine; it is the caller's responsibility to strip them.
6378 If no such canonicalization is possible (i.e., two biv's are used or an
6379 expression that is neither invariant nor a biv or giv), this routine
6380 returns 0.
6382 For a non-zero return, the result will have a code of CONST_INT, USE,
6383 REG (for a BIV), PLUS, or MULT. No other codes will occur.
6385 *BENEFIT will be incremented by the benefit of any sub-giv encountered. */
6387 static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
6388 static rtx sge_plus_constant PARAMS ((rtx, rtx));
6390 static rtx
6391 simplify_giv_expr (loop, x, ext_val, benefit)
6392 const struct loop *loop;
6393 rtx x;
6394 rtx *ext_val;
6395 int *benefit;
6397 struct loop_ivs *ivs = LOOP_IVS (loop);
6398 struct loop_regs *regs = LOOP_REGS (loop);
6399 enum machine_mode mode = GET_MODE (x);
6400 rtx arg0, arg1;
6401 rtx tem;
6403 /* If this is not an integer mode, or if we cannot do arithmetic in this
6404 mode, this can't be a giv. */
6405 if (mode != VOIDmode
6406 && (GET_MODE_CLASS (mode) != MODE_INT
6407 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6408 return NULL_RTX;
6410 switch (GET_CODE (x))
6412 case PLUS:
6413 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6414 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6415 if (arg0 == 0 || arg1 == 0)
6416 return NULL_RTX;
6418 /* Put constant last, CONST_INT last if both constant. */
6419 if ((GET_CODE (arg0) == USE
6420 || GET_CODE (arg0) == CONST_INT)
6421 && ! ((GET_CODE (arg0) == USE
6422 && GET_CODE (arg1) == USE)
6423 || GET_CODE (arg1) == CONST_INT))
6424 tem = arg0, arg0 = arg1, arg1 = tem;
6426 /* Handle addition of zero, then addition of an invariant. */
6427 if (arg1 == const0_rtx)
6428 return arg0;
6429 else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6430 switch (GET_CODE (arg0))
6432 case CONST_INT:
6433 case USE:
6434 /* Adding two invariants must result in an invariant, so enclose
6435 addition operation inside a USE and return it. */
6436 if (GET_CODE (arg0) == USE)
6437 arg0 = XEXP (arg0, 0);
6438 if (GET_CODE (arg1) == USE)
6439 arg1 = XEXP (arg1, 0);
6441 if (GET_CODE (arg0) == CONST_INT)
6442 tem = arg0, arg0 = arg1, arg1 = tem;
6443 if (GET_CODE (arg1) == CONST_INT)
6444 tem = sge_plus_constant (arg0, arg1);
6445 else
6446 tem = sge_plus (mode, arg0, arg1);
6448 if (GET_CODE (tem) != CONST_INT)
6449 tem = gen_rtx_USE (mode, tem);
6450 return tem;
6452 case REG:
6453 case MULT:
6454 /* biv + invar or mult + invar. Return sum. */
6455 return gen_rtx_PLUS (mode, arg0, arg1);
6457 case PLUS:
6458 /* (a + invar_1) + invar_2. Associate. */
6459 return
6460 simplify_giv_expr (loop,
6461 gen_rtx_PLUS (mode,
6462 XEXP (arg0, 0),
6463 gen_rtx_PLUS (mode,
6464 XEXP (arg0, 1),
6465 arg1)),
6466 ext_val, benefit);
6468 default:
6469 abort ();
6472 /* Each argument must be either REG, PLUS, or MULT. Convert REG to
6473 MULT to reduce cases. */
6474 if (GET_CODE (arg0) == REG)
6475 arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6476 if (GET_CODE (arg1) == REG)
6477 arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6479 /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6480 Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6481 Recurse to associate the second PLUS. */
6482 if (GET_CODE (arg1) == MULT)
6483 tem = arg0, arg0 = arg1, arg1 = tem;
6485 if (GET_CODE (arg1) == PLUS)
6486 return
6487 simplify_giv_expr (loop,
6488 gen_rtx_PLUS (mode,
6489 gen_rtx_PLUS (mode, arg0,
6490 XEXP (arg1, 0)),
6491 XEXP (arg1, 1)),
6492 ext_val, benefit);
6494 /* Now must have MULT + MULT. Distribute if same biv, else not giv. */
6495 if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6496 return NULL_RTX;
6498 if (!rtx_equal_p (arg0, arg1))
6499 return NULL_RTX;
6501 return simplify_giv_expr (loop,
6502 gen_rtx_MULT (mode,
6503 XEXP (arg0, 0),
6504 gen_rtx_PLUS (mode,
6505 XEXP (arg0, 1),
6506 XEXP (arg1, 1))),
6507 ext_val, benefit);
6509 case MINUS:
6510 /* Handle "a - b" as "a + b * (-1)". */
6511 return simplify_giv_expr (loop,
6512 gen_rtx_PLUS (mode,
6513 XEXP (x, 0),
6514 gen_rtx_MULT (mode,
6515 XEXP (x, 1),
6516 constm1_rtx)),
6517 ext_val, benefit);
6519 case MULT:
6520 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6521 arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6522 if (arg0 == 0 || arg1 == 0)
6523 return NULL_RTX;
6525 /* Put constant last, CONST_INT last if both constant. */
6526 if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6527 && GET_CODE (arg1) != CONST_INT)
6528 tem = arg0, arg0 = arg1, arg1 = tem;
6530 /* If second argument is not now constant, not giv. */
6531 if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6532 return NULL_RTX;
6534 /* Handle multiply by 0 or 1. */
6535 if (arg1 == const0_rtx)
6536 return const0_rtx;
6538 else if (arg1 == const1_rtx)
6539 return arg0;
6541 switch (GET_CODE (arg0))
6543 case REG:
6544 /* biv * invar. Done. */
6545 return gen_rtx_MULT (mode, arg0, arg1);
6547 case CONST_INT:
6548 /* Product of two constants. */
6549 return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6551 case USE:
6552 /* invar * invar is a giv, but attempt to simplify it somehow. */
6553 if (GET_CODE (arg1) != CONST_INT)
6554 return NULL_RTX;
6556 arg0 = XEXP (arg0, 0);
6557 if (GET_CODE (arg0) == MULT)
6559 /* (invar_0 * invar_1) * invar_2. Associate. */
6560 return simplify_giv_expr (loop,
6561 gen_rtx_MULT (mode,
6562 XEXP (arg0, 0),
6563 gen_rtx_MULT (mode,
6564 XEXP (arg0,
6566 arg1)),
6567 ext_val, benefit);
6569 /* Porpagate the MULT expressions to the intermost nodes. */
6570 else if (GET_CODE (arg0) == PLUS)
6572 /* (invar_0 + invar_1) * invar_2. Distribute. */
6573 return simplify_giv_expr (loop,
6574 gen_rtx_PLUS (mode,
6575 gen_rtx_MULT (mode,
6576 XEXP (arg0,
6578 arg1),
6579 gen_rtx_MULT (mode,
6580 XEXP (arg0,
6582 arg1)),
6583 ext_val, benefit);
6585 return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
6587 case MULT:
6588 /* (a * invar_1) * invar_2. Associate. */
6589 return simplify_giv_expr (loop,
6590 gen_rtx_MULT (mode,
6591 XEXP (arg0, 0),
6592 gen_rtx_MULT (mode,
6593 XEXP (arg0, 1),
6594 arg1)),
6595 ext_val, benefit);
6597 case PLUS:
6598 /* (a + invar_1) * invar_2. Distribute. */
6599 return simplify_giv_expr (loop,
6600 gen_rtx_PLUS (mode,
6601 gen_rtx_MULT (mode,
6602 XEXP (arg0, 0),
6603 arg1),
6604 gen_rtx_MULT (mode,
6605 XEXP (arg0, 1),
6606 arg1)),
6607 ext_val, benefit);
6609 default:
6610 abort ();
6613 case ASHIFT:
6614 /* Shift by constant is multiply by power of two. */
6615 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6616 return 0;
6618 return
6619 simplify_giv_expr (loop,
6620 gen_rtx_MULT (mode,
6621 XEXP (x, 0),
6622 GEN_INT ((HOST_WIDE_INT) 1
6623 << INTVAL (XEXP (x, 1)))),
6624 ext_val, benefit);
6626 case NEG:
6627 /* "-a" is "a * (-1)" */
6628 return simplify_giv_expr (loop,
6629 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6630 ext_val, benefit);
6632 case NOT:
6633 /* "~a" is "-a - 1". Silly, but easy. */
6634 return simplify_giv_expr (loop,
6635 gen_rtx_MINUS (mode,
6636 gen_rtx_NEG (mode, XEXP (x, 0)),
6637 const1_rtx),
6638 ext_val, benefit);
6640 case USE:
6641 /* Already in proper form for invariant. */
6642 return x;
6644 case SIGN_EXTEND:
6645 case ZERO_EXTEND:
6646 case TRUNCATE:
6647 /* Conditionally recognize extensions of simple IVs. After we've
6648 computed loop traversal counts and verified the range of the
6649 source IV, we'll reevaluate this as a GIV. */
6650 if (*ext_val == NULL_RTX)
6652 arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6653 if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
6655 *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
6656 return arg0;
6659 goto do_default;
6661 case REG:
6662 /* If this is a new register, we can't deal with it. */
6663 if (REGNO (x) >= max_reg_before_loop)
6664 return 0;
6666 /* Check for biv or giv. */
6667 switch (REG_IV_TYPE (ivs, REGNO (x)))
6669 case BASIC_INDUCT:
6670 return x;
6671 case GENERAL_INDUCT:
6673 struct induction *v = REG_IV_INFO (ivs, REGNO (x));
6675 /* Form expression from giv and add benefit. Ensure this giv
6676 can derive another and subtract any needed adjustment if so. */
6678 /* Increasing the benefit here is risky. The only case in which it
6679 is arguably correct is if this is the only use of V. In other
6680 cases, this will artificially inflate the benefit of the current
6681 giv, and lead to suboptimal code. Thus, it is disabled, since
6682 potentially not reducing an only marginally beneficial giv is
6683 less harmful than reducing many givs that are not really
6684 beneficial. */
6686 rtx single_use = regs->array[REGNO (x)].single_usage;
6687 if (single_use && single_use != const0_rtx)
6688 *benefit += v->benefit;
6691 if (v->cant_derive)
6692 return 0;
6694 tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
6695 v->src_reg, v->mult_val),
6696 v->add_val);
6698 if (v->derive_adjustment)
6699 tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6700 arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
6701 if (*ext_val)
6703 if (!v->ext_dependent)
6704 return arg0;
6706 else
6708 *ext_val = v->ext_dependent;
6709 return arg0;
6711 return 0;
6714 default:
6715 do_default:
6716 /* If it isn't an induction variable, and it is invariant, we
6717 may be able to simplify things further by looking through
6718 the bits we just moved outside the loop. */
6719 if (loop_invariant_p (loop, x) == 1)
6721 struct movable *m;
6722 struct loop_movables *movables = LOOP_MOVABLES (loop);
6724 for (m = movables->head; m; m = m->next)
6725 if (rtx_equal_p (x, m->set_dest))
6727 /* Ok, we found a match. Substitute and simplify. */
6729 /* If we match another movable, we must use that, as
6730 this one is going away. */
6731 if (m->match)
6732 return simplify_giv_expr (loop, m->match->set_dest,
6733 ext_val, benefit);
6735 /* If consec is non-zero, this is a member of a group of
6736 instructions that were moved together. We handle this
6737 case only to the point of seeking to the last insn and
6738 looking for a REG_EQUAL. Fail if we don't find one. */
6739 if (m->consec != 0)
6741 int i = m->consec;
6742 tem = m->insn;
6745 tem = NEXT_INSN (tem);
6747 while (--i > 0);
6749 tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6750 if (tem)
6751 tem = XEXP (tem, 0);
6753 else
6755 tem = single_set (m->insn);
6756 if (tem)
6757 tem = SET_SRC (tem);
6760 if (tem)
6762 /* What we are most interested in is pointer
6763 arithmetic on invariants -- only take
6764 patterns we may be able to do something with. */
6765 if (GET_CODE (tem) == PLUS
6766 || GET_CODE (tem) == MULT
6767 || GET_CODE (tem) == ASHIFT
6768 || GET_CODE (tem) == CONST_INT
6769 || GET_CODE (tem) == SYMBOL_REF)
6771 tem = simplify_giv_expr (loop, tem, ext_val,
6772 benefit);
6773 if (tem)
6774 return tem;
6776 else if (GET_CODE (tem) == CONST
6777 && GET_CODE (XEXP (tem, 0)) == PLUS
6778 && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6779 && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6781 tem = simplify_giv_expr (loop, XEXP (tem, 0),
6782 ext_val, benefit);
6783 if (tem)
6784 return tem;
6787 break;
6790 break;
6793 /* Fall through to general case. */
6794 default:
6795 /* If invariant, return as USE (unless CONST_INT).
6796 Otherwise, not giv. */
6797 if (GET_CODE (x) == USE)
6798 x = XEXP (x, 0);
6800 if (loop_invariant_p (loop, x) == 1)
6802 if (GET_CODE (x) == CONST_INT)
6803 return x;
6804 if (GET_CODE (x) == CONST
6805 && GET_CODE (XEXP (x, 0)) == PLUS
6806 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6807 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6808 x = XEXP (x, 0);
6809 return gen_rtx_USE (mode, x);
6811 else
6812 return 0;
6816 /* This routine folds invariants such that there is only ever one
6817 CONST_INT in the summation. It is only used by simplify_giv_expr. */
6819 static rtx
6820 sge_plus_constant (x, c)
6821 rtx x, c;
6823 if (GET_CODE (x) == CONST_INT)
6824 return GEN_INT (INTVAL (x) + INTVAL (c));
6825 else if (GET_CODE (x) != PLUS)
6826 return gen_rtx_PLUS (GET_MODE (x), x, c);
6827 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6829 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6830 GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6832 else if (GET_CODE (XEXP (x, 0)) == PLUS
6833 || GET_CODE (XEXP (x, 1)) != PLUS)
6835 return gen_rtx_PLUS (GET_MODE (x),
6836 sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6838 else
6840 return gen_rtx_PLUS (GET_MODE (x),
6841 sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6845 static rtx
6846 sge_plus (mode, x, y)
6847 enum machine_mode mode;
6848 rtx x, y;
6850 while (GET_CODE (y) == PLUS)
6852 rtx a = XEXP (y, 0);
6853 if (GET_CODE (a) == CONST_INT)
6854 x = sge_plus_constant (x, a);
6855 else
6856 x = gen_rtx_PLUS (mode, x, a);
6857 y = XEXP (y, 1);
6859 if (GET_CODE (y) == CONST_INT)
6860 x = sge_plus_constant (x, y);
6861 else
6862 x = gen_rtx_PLUS (mode, x, y);
6863 return x;
6866 /* Help detect a giv that is calculated by several consecutive insns;
6867 for example,
6868 giv = biv * M
6869 giv = giv + A
6870 The caller has already identified the first insn P as having a giv as dest;
6871 we check that all other insns that set the same register follow
6872 immediately after P, that they alter nothing else,
6873 and that the result of the last is still a giv.
6875 The value is 0 if the reg set in P is not really a giv.
6876 Otherwise, the value is the amount gained by eliminating
6877 all the consecutive insns that compute the value.
6879 FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6880 SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6882 The coefficients of the ultimate giv value are stored in
6883 *MULT_VAL and *ADD_VAL. */
6885 static int
6886 consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
6887 add_val, mult_val, ext_val, last_consec_insn)
6888 const struct loop *loop;
6889 int first_benefit;
6890 rtx p;
6891 rtx src_reg;
6892 rtx dest_reg;
6893 rtx *add_val;
6894 rtx *mult_val;
6895 rtx *ext_val;
6896 rtx *last_consec_insn;
6898 struct loop_ivs *ivs = LOOP_IVS (loop);
6899 struct loop_regs *regs = LOOP_REGS (loop);
6900 int count;
6901 enum rtx_code code;
6902 int benefit;
6903 rtx temp;
6904 rtx set;
6906 /* Indicate that this is a giv so that we can update the value produced in
6907 each insn of the multi-insn sequence.
6909 This induction structure will be used only by the call to
6910 general_induction_var below, so we can allocate it on our stack.
6911 If this is a giv, our caller will replace the induct var entry with
6912 a new induction structure. */
6913 struct induction *v;
6915 if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
6916 return 0;
6918 v = (struct induction *) alloca (sizeof (struct induction));
6919 v->src_reg = src_reg;
6920 v->mult_val = *mult_val;
6921 v->add_val = *add_val;
6922 v->benefit = first_benefit;
6923 v->cant_derive = 0;
6924 v->derive_adjustment = 0;
6925 v->ext_dependent = NULL_RTX;
6927 REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
6928 REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
6930 count = regs->array[REGNO (dest_reg)].n_times_set - 1;
6932 while (count > 0)
6934 p = NEXT_INSN (p);
6935 code = GET_CODE (p);
6937 /* If libcall, skip to end of call sequence. */
6938 if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
6939 p = XEXP (temp, 0);
6941 if (code == INSN
6942 && (set = single_set (p))
6943 && GET_CODE (SET_DEST (set)) == REG
6944 && SET_DEST (set) == dest_reg
6945 && (general_induction_var (loop, SET_SRC (set), &src_reg,
6946 add_val, mult_val, ext_val, 0,
6947 &benefit, VOIDmode)
6948 /* Giv created by equivalent expression. */
6949 || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
6950 && general_induction_var (loop, XEXP (temp, 0), &src_reg,
6951 add_val, mult_val, ext_val, 0,
6952 &benefit, VOIDmode)))
6953 && src_reg == v->src_reg)
6955 if (find_reg_note (p, REG_RETVAL, NULL_RTX))
6956 benefit += libcall_benefit (p);
6958 count--;
6959 v->mult_val = *mult_val;
6960 v->add_val = *add_val;
6961 v->benefit += benefit;
6963 else if (code != NOTE)
6965 /* Allow insns that set something other than this giv to a
6966 constant. Such insns are needed on machines which cannot
6967 include long constants and should not disqualify a giv. */
6968 if (code == INSN
6969 && (set = single_set (p))
6970 && SET_DEST (set) != dest_reg
6971 && CONSTANT_P (SET_SRC (set)))
6972 continue;
6974 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
6975 return 0;
6979 REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
6980 *last_consec_insn = p;
6981 return v->benefit;
6984 /* Return an rtx, if any, that expresses giv G2 as a function of the register
6985 represented by G1. If no such expression can be found, or it is clear that
6986 it cannot possibly be a valid address, 0 is returned.
6988 To perform the computation, we note that
6989 G1 = x * v + a and
6990 G2 = y * v + b
6991 where `v' is the biv.
6993 So G2 = (y/b) * G1 + (b - a*y/x).
6995 Note that MULT = y/x.
6997 Update: A and B are now allowed to be additive expressions such that
6998 B contains all variables in A. That is, computing B-A will not require
6999 subtracting variables. */
7001 static rtx
7002 express_from_1 (a, b, mult)
7003 rtx a, b, mult;
7005 /* If MULT is zero, then A*MULT is zero, and our expression is B. */
7007 if (mult == const0_rtx)
7008 return b;
7010 /* If MULT is not 1, we cannot handle A with non-constants, since we
7011 would then be required to subtract multiples of the registers in A.
7012 This is theoretically possible, and may even apply to some Fortran
7013 constructs, but it is a lot of work and we do not attempt it here. */
7015 if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
7016 return NULL_RTX;
7018 /* In general these structures are sorted top to bottom (down the PLUS
7019 chain), but not left to right across the PLUS. If B is a higher
7020 order giv than A, we can strip one level and recurse. If A is higher
7021 order, we'll eventually bail out, but won't know that until the end.
7022 If they are the same, we'll strip one level around this loop. */
7024 while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
7026 rtx ra, rb, oa, ob, tmp;
7028 ra = XEXP (a, 0), oa = XEXP (a, 1);
7029 if (GET_CODE (ra) == PLUS)
7030 tmp = ra, ra = oa, oa = tmp;
7032 rb = XEXP (b, 0), ob = XEXP (b, 1);
7033 if (GET_CODE (rb) == PLUS)
7034 tmp = rb, rb = ob, ob = tmp;
7036 if (rtx_equal_p (ra, rb))
7037 /* We matched: remove one reg completely. */
7038 a = oa, b = ob;
7039 else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
7040 /* An alternate match. */
7041 a = oa, b = rb;
7042 else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
7043 /* An alternate match. */
7044 a = ra, b = ob;
7045 else
7047 /* Indicates an extra register in B. Strip one level from B and
7048 recurse, hoping B was the higher order expression. */
7049 ob = express_from_1 (a, ob, mult);
7050 if (ob == NULL_RTX)
7051 return NULL_RTX;
7052 return gen_rtx_PLUS (GET_MODE (b), rb, ob);
7056 /* Here we are at the last level of A, go through the cases hoping to
7057 get rid of everything but a constant. */
7059 if (GET_CODE (a) == PLUS)
7061 rtx ra, oa;
7063 ra = XEXP (a, 0), oa = XEXP (a, 1);
7064 if (rtx_equal_p (oa, b))
7065 oa = ra;
7066 else if (!rtx_equal_p (ra, b))
7067 return NULL_RTX;
7069 if (GET_CODE (oa) != CONST_INT)
7070 return NULL_RTX;
7072 return GEN_INT (-INTVAL (oa) * INTVAL (mult));
7074 else if (GET_CODE (a) == CONST_INT)
7076 return plus_constant (b, -INTVAL (a) * INTVAL (mult));
7078 else if (CONSTANT_P (a))
7080 enum machine_mode mode_a = GET_MODE (a);
7081 enum machine_mode mode_b = GET_MODE (b);
7082 enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
7083 return simplify_gen_binary (MINUS, mode, b, a);
7085 else if (GET_CODE (b) == PLUS)
7087 if (rtx_equal_p (a, XEXP (b, 0)))
7088 return XEXP (b, 1);
7089 else if (rtx_equal_p (a, XEXP (b, 1)))
7090 return XEXP (b, 0);
7091 else
7092 return NULL_RTX;
7094 else if (rtx_equal_p (a, b))
7095 return const0_rtx;
7097 return NULL_RTX;
7101 express_from (g1, g2)
7102 struct induction *g1, *g2;
7104 rtx mult, add;
7106 /* The value that G1 will be multiplied by must be a constant integer. Also,
7107 the only chance we have of getting a valid address is if b*c/a (see above
7108 for notation) is also an integer. */
7109 if (GET_CODE (g1->mult_val) == CONST_INT
7110 && GET_CODE (g2->mult_val) == CONST_INT)
7112 if (g1->mult_val == const0_rtx
7113 || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
7114 return NULL_RTX;
7115 mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
7117 else if (rtx_equal_p (g1->mult_val, g2->mult_val))
7118 mult = const1_rtx;
7119 else
7121 /* ??? Find out if the one is a multiple of the other? */
7122 return NULL_RTX;
7125 add = express_from_1 (g1->add_val, g2->add_val, mult);
7126 if (add == NULL_RTX)
7128 /* Failed. If we've got a multiplication factor between G1 and G2,
7129 scale G1's addend and try again. */
7130 if (INTVAL (mult) > 1)
7132 rtx g1_add_val = g1->add_val;
7133 if (GET_CODE (g1_add_val) == MULT
7134 && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
7136 HOST_WIDE_INT m;
7137 m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
7138 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
7139 XEXP (g1_add_val, 0), GEN_INT (m));
7141 else
7143 g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
7144 mult);
7147 add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
7150 if (add == NULL_RTX)
7151 return NULL_RTX;
7153 /* Form simplified final result. */
7154 if (mult == const0_rtx)
7155 return add;
7156 else if (mult == const1_rtx)
7157 mult = g1->dest_reg;
7158 else
7159 mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
7161 if (add == const0_rtx)
7162 return mult;
7163 else
7165 if (GET_CODE (add) == PLUS
7166 && CONSTANT_P (XEXP (add, 1)))
7168 rtx tem = XEXP (add, 1);
7169 mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
7170 add = tem;
7173 return gen_rtx_PLUS (g2->mode, mult, add);
7177 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7178 represented by G1. This indicates that G2 should be combined with G1 and
7179 that G2 can use (either directly or via an address expression) a register
7180 used to represent G1. */
7182 static rtx
7183 combine_givs_p (g1, g2)
7184 struct induction *g1, *g2;
7186 rtx comb, ret;
7188 /* With the introduction of ext dependent givs, we must care for modes.
7189 G2 must not use a wider mode than G1. */
7190 if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
7191 return NULL_RTX;
7193 ret = comb = express_from (g1, g2);
7194 if (comb == NULL_RTX)
7195 return NULL_RTX;
7196 if (g1->mode != g2->mode)
7197 ret = gen_lowpart (g2->mode, comb);
7199 /* If these givs are identical, they can be combined. We use the results
7200 of express_from because the addends are not in a canonical form, so
7201 rtx_equal_p is a weaker test. */
7202 /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7203 combination to be the other way round. */
7204 if (comb == g1->dest_reg
7205 && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
7207 return ret;
7210 /* If G2 can be expressed as a function of G1 and that function is valid
7211 as an address and no more expensive than using a register for G2,
7212 the expression of G2 in terms of G1 can be used. */
7213 if (ret != NULL_RTX
7214 && g2->giv_type == DEST_ADDR
7215 && memory_address_p (GET_MODE (g2->mem), ret)
7216 /* ??? Looses, especially with -fforce-addr, where *g2->location
7217 will always be a register, and so anything more complicated
7218 gets discarded. */
7219 #if 0
7220 #ifdef ADDRESS_COST
7221 && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
7222 #else
7223 && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
7224 #endif
7225 #endif
7228 return ret;
7231 return NULL_RTX;
7234 /* Check each extension dependent giv in this class to see if its
7235 root biv is safe from wrapping in the interior mode, which would
7236 make the giv illegal. */
7238 static void
7239 check_ext_dependent_givs (bl, loop_info)
7240 struct iv_class *bl;
7241 struct loop_info *loop_info;
7243 int ze_ok = 0, se_ok = 0, info_ok = 0;
7244 enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
7245 HOST_WIDE_INT start_val;
7246 unsigned HOST_WIDE_INT u_end_val = 0;
7247 unsigned HOST_WIDE_INT u_start_val = 0;
7248 rtx incr = pc_rtx;
7249 struct induction *v;
7251 /* Make sure the iteration data is available. We must have
7252 constants in order to be certain of no overflow. */
7253 /* ??? An unknown iteration count with an increment of +-1
7254 combined with friendly exit tests of against an invariant
7255 value is also ameanable to optimization. Not implemented. */
7256 if (loop_info->n_iterations > 0
7257 && bl->initial_value
7258 && GET_CODE (bl->initial_value) == CONST_INT
7259 && (incr = biv_total_increment (bl))
7260 && GET_CODE (incr) == CONST_INT
7261 /* Make sure the host can represent the arithmetic. */
7262 && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
7264 unsigned HOST_WIDE_INT abs_incr, total_incr;
7265 HOST_WIDE_INT s_end_val;
7266 int neg_incr;
7268 info_ok = 1;
7269 start_val = INTVAL (bl->initial_value);
7270 u_start_val = start_val;
7272 neg_incr = 0, abs_incr = INTVAL (incr);
7273 if (INTVAL (incr) < 0)
7274 neg_incr = 1, abs_incr = -abs_incr;
7275 total_incr = abs_incr * loop_info->n_iterations;
7277 /* Check for host arithmatic overflow. */
7278 if (total_incr / loop_info->n_iterations == abs_incr)
7280 unsigned HOST_WIDE_INT u_max;
7281 HOST_WIDE_INT s_max;
7283 u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
7284 s_end_val = u_end_val;
7285 u_max = GET_MODE_MASK (biv_mode);
7286 s_max = u_max >> 1;
7288 /* Check zero extension of biv ok. */
7289 if (start_val >= 0
7290 /* Check for host arithmatic overflow. */
7291 && (neg_incr
7292 ? u_end_val < u_start_val
7293 : u_end_val > u_start_val)
7294 /* Check for target arithmetic overflow. */
7295 && (neg_incr
7296 ? 1 /* taken care of with host overflow */
7297 : u_end_val <= u_max))
7299 ze_ok = 1;
7302 /* Check sign extension of biv ok. */
7303 /* ??? While it is true that overflow with signed and pointer
7304 arithmetic is undefined, I fear too many programmers don't
7305 keep this fact in mind -- myself included on occasion.
7306 So leave alone with the signed overflow optimizations. */
7307 if (start_val >= -s_max - 1
7308 /* Check for host arithmatic overflow. */
7309 && (neg_incr
7310 ? s_end_val < start_val
7311 : s_end_val > start_val)
7312 /* Check for target arithmetic overflow. */
7313 && (neg_incr
7314 ? s_end_val >= -s_max - 1
7315 : s_end_val <= s_max))
7317 se_ok = 1;
7322 /* Invalidate givs that fail the tests. */
7323 for (v = bl->giv; v; v = v->next_iv)
7324 if (v->ext_dependent)
7326 enum rtx_code code = GET_CODE (v->ext_dependent);
7327 int ok = 0;
7329 switch (code)
7331 case SIGN_EXTEND:
7332 ok = se_ok;
7333 break;
7334 case ZERO_EXTEND:
7335 ok = ze_ok;
7336 break;
7338 case TRUNCATE:
7339 /* We don't know whether this value is being used as either
7340 signed or unsigned, so to safely truncate we must satisfy
7341 both. The initial check here verifies the BIV itself;
7342 once that is successful we may check its range wrt the
7343 derived GIV. */
7344 if (se_ok && ze_ok)
7346 enum machine_mode outer_mode = GET_MODE (v->ext_dependent);
7347 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
7349 /* We know from the above that both endpoints are nonnegative,
7350 and that there is no wrapping. Verify that both endpoints
7351 are within the (signed) range of the outer mode. */
7352 if (u_start_val <= max && u_end_val <= max)
7353 ok = 1;
7355 break;
7357 default:
7358 abort ();
7361 if (ok)
7363 if (loop_dump_stream)
7365 fprintf (loop_dump_stream,
7366 "Verified ext dependent giv at %d of reg %d\n",
7367 INSN_UID (v->insn), bl->regno);
7370 else
7372 if (loop_dump_stream)
7374 const char *why;
7376 if (info_ok)
7377 why = "biv iteration values overflowed";
7378 else
7380 if (incr == pc_rtx)
7381 incr = biv_total_increment (bl);
7382 if (incr == const1_rtx)
7383 why = "biv iteration info incomplete; incr by 1";
7384 else
7385 why = "biv iteration info incomplete";
7388 fprintf (loop_dump_stream,
7389 "Failed ext dependent giv at %d, %s\n",
7390 INSN_UID (v->insn), why);
7392 v->ignore = 1;
7393 bl->all_reduced = 0;
7398 /* Generate a version of VALUE in a mode appropriate for initializing V. */
7401 extend_value_for_giv (v, value)
7402 struct induction *v;
7403 rtx value;
7405 rtx ext_dep = v->ext_dependent;
7407 if (! ext_dep)
7408 return value;
7410 /* Recall that check_ext_dependent_givs verified that the known bounds
7411 of a biv did not overflow or wrap with respect to the extension for
7412 the giv. Therefore, constants need no additional adjustment. */
7413 if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
7414 return value;
7416 /* Otherwise, we must adjust the value to compensate for the
7417 differing modes of the biv and the giv. */
7418 return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
7421 struct combine_givs_stats
7423 int giv_number;
7424 int total_benefit;
7427 static int
7428 cmp_combine_givs_stats (xp, yp)
7429 const PTR xp;
7430 const PTR yp;
7432 const struct combine_givs_stats * const x =
7433 (const struct combine_givs_stats *) xp;
7434 const struct combine_givs_stats * const y =
7435 (const struct combine_givs_stats *) yp;
7436 int d;
7437 d = y->total_benefit - x->total_benefit;
7438 /* Stabilize the sort. */
7439 if (!d)
7440 d = x->giv_number - y->giv_number;
7441 return d;
7444 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7445 any other. If so, point SAME to the giv combined with and set NEW_REG to
7446 be an expression (in terms of the other giv's DEST_REG) equivalent to the
7447 giv. Also, update BENEFIT and related fields for cost/benefit analysis. */
7449 static void
7450 combine_givs (regs, bl)
7451 struct loop_regs *regs;
7452 struct iv_class *bl;
7454 /* Additional benefit to add for being combined multiple times. */
7455 const int extra_benefit = 3;
7457 struct induction *g1, *g2, **giv_array;
7458 int i, j, k, giv_count;
7459 struct combine_givs_stats *stats;
7460 rtx *can_combine;
7462 /* Count givs, because bl->giv_count is incorrect here. */
7463 giv_count = 0;
7464 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7465 if (!g1->ignore)
7466 giv_count++;
7468 giv_array
7469 = (struct induction **) alloca (giv_count * sizeof (struct induction *));
7470 i = 0;
7471 for (g1 = bl->giv; g1; g1 = g1->next_iv)
7472 if (!g1->ignore)
7473 giv_array[i++] = g1;
7475 stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
7476 can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof (rtx));
7478 for (i = 0; i < giv_count; i++)
7480 int this_benefit;
7481 rtx single_use;
7483 g1 = giv_array[i];
7484 stats[i].giv_number = i;
7486 /* If a DEST_REG GIV is used only once, do not allow it to combine
7487 with anything, for in doing so we will gain nothing that cannot
7488 be had by simply letting the GIV with which we would have combined
7489 to be reduced on its own. The losage shows up in particular with
7490 DEST_ADDR targets on hosts with reg+reg addressing, though it can
7491 be seen elsewhere as well. */
7492 if (g1->giv_type == DEST_REG
7493 && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
7494 && single_use != const0_rtx)
7495 continue;
7497 this_benefit = g1->benefit;
7498 /* Add an additional weight for zero addends. */
7499 if (g1->no_const_addval)
7500 this_benefit += 1;
7502 for (j = 0; j < giv_count; j++)
7504 rtx this_combine;
7506 g2 = giv_array[j];
7507 if (g1 != g2
7508 && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
7510 can_combine[i * giv_count + j] = this_combine;
7511 this_benefit += g2->benefit + extra_benefit;
7514 stats[i].total_benefit = this_benefit;
7517 /* Iterate, combining until we can't. */
7518 restart:
7519 qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
7521 if (loop_dump_stream)
7523 fprintf (loop_dump_stream, "Sorted combine statistics:\n");
7524 for (k = 0; k < giv_count; k++)
7526 g1 = giv_array[stats[k].giv_number];
7527 if (!g1->combined_with && !g1->same)
7528 fprintf (loop_dump_stream, " {%d, %d}",
7529 INSN_UID (giv_array[stats[k].giv_number]->insn),
7530 stats[k].total_benefit);
7532 putc ('\n', loop_dump_stream);
7535 for (k = 0; k < giv_count; k++)
7537 int g1_add_benefit = 0;
7539 i = stats[k].giv_number;
7540 g1 = giv_array[i];
7542 /* If it has already been combined, skip. */
7543 if (g1->combined_with || g1->same)
7544 continue;
7546 for (j = 0; j < giv_count; j++)
7548 g2 = giv_array[j];
7549 if (g1 != g2 && can_combine[i * giv_count + j]
7550 /* If it has already been combined, skip. */
7551 && ! g2->same && ! g2->combined_with)
7553 int l;
7555 g2->new_reg = can_combine[i * giv_count + j];
7556 g2->same = g1;
7557 /* For destination, we now may replace by mem expression instead
7558 of register. This changes the costs considerably, so add the
7559 compensation. */
7560 if (g2->giv_type == DEST_ADDR)
7561 g2->benefit = (g2->benefit + reg_address_cost
7562 - address_cost (g2->new_reg,
7563 GET_MODE (g2->mem)));
7564 g1->combined_with++;
7565 g1->lifetime += g2->lifetime;
7567 g1_add_benefit += g2->benefit;
7569 /* ??? The new final_[bg]iv_value code does a much better job
7570 of finding replaceable giv's, and hence this code may no
7571 longer be necessary. */
7572 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7573 g1_add_benefit -= copy_cost;
7575 /* To help optimize the next set of combinations, remove
7576 this giv from the benefits of other potential mates. */
7577 for (l = 0; l < giv_count; ++l)
7579 int m = stats[l].giv_number;
7580 if (can_combine[m * giv_count + j])
7581 stats[l].total_benefit -= g2->benefit + extra_benefit;
7584 if (loop_dump_stream)
7585 fprintf (loop_dump_stream,
7586 "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7587 INSN_UID (g2->insn), INSN_UID (g1->insn),
7588 g1->benefit, g1_add_benefit, g1->lifetime);
7592 /* To help optimize the next set of combinations, remove
7593 this giv from the benefits of other potential mates. */
7594 if (g1->combined_with)
7596 for (j = 0; j < giv_count; ++j)
7598 int m = stats[j].giv_number;
7599 if (can_combine[m * giv_count + i])
7600 stats[j].total_benefit -= g1->benefit + extra_benefit;
7603 g1->benefit += g1_add_benefit;
7605 /* We've finished with this giv, and everything it touched.
7606 Restart the combination so that proper weights for the
7607 rest of the givs are properly taken into account. */
7608 /* ??? Ideally we would compact the arrays at this point, so
7609 as to not cover old ground. But sanely compacting
7610 can_combine is tricky. */
7611 goto restart;
7615 /* Clean up. */
7616 free (stats);
7617 free (can_combine);
7620 /* Generate sequence for REG = B * M + A. */
7622 static rtx
7623 gen_add_mult (b, m, a, reg)
7624 rtx b; /* initial value of basic induction variable */
7625 rtx m; /* multiplicative constant */
7626 rtx a; /* additive constant */
7627 rtx reg; /* destination register */
7629 rtx seq;
7630 rtx result;
7632 start_sequence ();
7633 /* Use unsigned arithmetic. */
7634 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7635 if (reg != result)
7636 emit_move_insn (reg, result);
7637 seq = gen_sequence ();
7638 end_sequence ();
7640 return seq;
7644 /* Update registers created in insn sequence SEQ. */
7646 static void
7647 loop_regs_update (loop, seq)
7648 const struct loop *loop ATTRIBUTE_UNUSED;
7649 rtx seq;
7651 /* Update register info for alias analysis. */
7653 if (GET_CODE (seq) == SEQUENCE)
7655 int i;
7656 for (i = 0; i < XVECLEN (seq, 0); ++i)
7658 rtx set = single_set (XVECEXP (seq, 0, i));
7659 if (set && GET_CODE (SET_DEST (set)) == REG)
7660 record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7663 else
7665 if (GET_CODE (seq) == SET
7666 && GET_CODE (SET_DEST (seq)) == REG)
7667 record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
7672 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A. */
7674 void
7675 loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
7676 const struct loop *loop;
7677 rtx b; /* initial value of basic induction variable */
7678 rtx m; /* multiplicative constant */
7679 rtx a; /* additive constant */
7680 rtx reg; /* destination register */
7681 basic_block before_bb;
7682 rtx before_insn;
7684 rtx seq;
7686 if (! before_insn)
7688 loop_iv_add_mult_hoist (loop, b, m, a, reg);
7689 return;
7692 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7693 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7695 /* Increase the lifetime of any invariants moved further in code. */
7696 update_reg_last_use (a, before_insn);
7697 update_reg_last_use (b, before_insn);
7698 update_reg_last_use (m, before_insn);
7700 loop_insn_emit_before (loop, before_bb, before_insn, seq);
7702 /* It is possible that the expansion created lots of new registers.
7703 Iterate over the sequence we just created and record them all. */
7704 loop_regs_update (loop, seq);
7708 /* Emit insns in loop pre-header to set REG = B * M + A. */
7710 void
7711 loop_iv_add_mult_sink (loop, b, m, a, reg)
7712 const struct loop *loop;
7713 rtx b; /* initial value of basic induction variable */
7714 rtx m; /* multiplicative constant */
7715 rtx a; /* additive constant */
7716 rtx reg; /* destination register */
7718 rtx seq;
7720 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7721 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7723 /* Increase the lifetime of any invariants moved further in code.
7724 ???? Is this really necessary? */
7725 update_reg_last_use (a, loop->sink);
7726 update_reg_last_use (b, loop->sink);
7727 update_reg_last_use (m, loop->sink);
7729 loop_insn_sink (loop, seq);
7731 /* It is possible that the expansion created lots of new registers.
7732 Iterate over the sequence we just created and record them all. */
7733 loop_regs_update (loop, seq);
7737 /* Emit insns after loop to set REG = B * M + A. */
7739 void
7740 loop_iv_add_mult_hoist (loop, b, m, a, reg)
7741 const struct loop *loop;
7742 rtx b; /* initial value of basic induction variable */
7743 rtx m; /* multiplicative constant */
7744 rtx a; /* additive constant */
7745 rtx reg; /* destination register */
7747 rtx seq;
7749 /* Use copy_rtx to prevent unexpected sharing of these rtx. */
7750 seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7752 loop_insn_hoist (loop, seq);
7754 /* It is possible that the expansion created lots of new registers.
7755 Iterate over the sequence we just created and record them all. */
7756 loop_regs_update (loop, seq);
7761 /* Similar to gen_add_mult, but compute cost rather than generating
7762 sequence. */
7764 static int
7765 iv_add_mult_cost (b, m, a, reg)
7766 rtx b; /* initial value of basic induction variable */
7767 rtx m; /* multiplicative constant */
7768 rtx a; /* additive constant */
7769 rtx reg; /* destination register */
7771 int cost = 0;
7772 rtx last, result;
7774 start_sequence ();
7775 result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7776 if (reg != result)
7777 emit_move_insn (reg, result);
7778 last = get_last_insn ();
7779 while (last)
7781 rtx t = single_set (last);
7782 if (t)
7783 cost += rtx_cost (SET_SRC (t), SET);
7784 last = PREV_INSN (last);
7786 end_sequence ();
7787 return cost;
7790 /* Test whether A * B can be computed without
7791 an actual multiply insn. Value is 1 if so. */
7793 static int
7794 product_cheap_p (a, b)
7795 rtx a;
7796 rtx b;
7798 int i;
7799 rtx tmp;
7800 int win = 1;
7802 /* If only one is constant, make it B. */
7803 if (GET_CODE (a) == CONST_INT)
7804 tmp = a, a = b, b = tmp;
7806 /* If first constant, both constant, so don't need multiply. */
7807 if (GET_CODE (a) == CONST_INT)
7808 return 1;
7810 /* If second not constant, neither is constant, so would need multiply. */
7811 if (GET_CODE (b) != CONST_INT)
7812 return 0;
7814 /* One operand is constant, so might not need multiply insn. Generate the
7815 code for the multiply and see if a call or multiply, or long sequence
7816 of insns is generated. */
7818 start_sequence ();
7819 expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7820 tmp = gen_sequence ();
7821 end_sequence ();
7823 if (GET_CODE (tmp) == SEQUENCE)
7825 if (XVEC (tmp, 0) == 0)
7826 win = 1;
7827 else if (XVECLEN (tmp, 0) > 3)
7828 win = 0;
7829 else
7830 for (i = 0; i < XVECLEN (tmp, 0); i++)
7832 rtx insn = XVECEXP (tmp, 0, i);
7834 if (GET_CODE (insn) != INSN
7835 || (GET_CODE (PATTERN (insn)) == SET
7836 && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7837 || (GET_CODE (PATTERN (insn)) == PARALLEL
7838 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7839 && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7841 win = 0;
7842 break;
7846 else if (GET_CODE (tmp) == SET
7847 && GET_CODE (SET_SRC (tmp)) == MULT)
7848 win = 0;
7849 else if (GET_CODE (tmp) == PARALLEL
7850 && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7851 && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7852 win = 0;
7854 return win;
7857 /* Check to see if loop can be terminated by a "decrement and branch until
7858 zero" instruction. If so, add a REG_NONNEG note to the branch insn if so.
7859 Also try reversing an increment loop to a decrement loop
7860 to see if the optimization can be performed.
7861 Value is nonzero if optimization was performed. */
7863 /* This is useful even if the architecture doesn't have such an insn,
7864 because it might change a loops which increments from 0 to n to a loop
7865 which decrements from n to 0. A loop that decrements to zero is usually
7866 faster than one that increments from zero. */
7868 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7869 such as approx_final_value, biv_total_increment, loop_iterations, and
7870 final_[bg]iv_value. */
7872 static int
7873 check_dbra_loop (loop, insn_count)
7874 struct loop *loop;
7875 int insn_count;
7877 struct loop_info *loop_info = LOOP_INFO (loop);
7878 struct loop_regs *regs = LOOP_REGS (loop);
7879 struct loop_ivs *ivs = LOOP_IVS (loop);
7880 struct iv_class *bl;
7881 rtx reg;
7882 rtx jump_label;
7883 rtx final_value;
7884 rtx start_value;
7885 rtx new_add_val;
7886 rtx comparison;
7887 rtx before_comparison;
7888 rtx p;
7889 rtx jump;
7890 rtx first_compare;
7891 int compare_and_branch;
7892 rtx loop_start = loop->start;
7893 rtx loop_end = loop->end;
7895 /* If last insn is a conditional branch, and the insn before tests a
7896 register value, try to optimize it. Otherwise, we can't do anything. */
7898 jump = PREV_INSN (loop_end);
7899 comparison = get_condition_for_loop (loop, jump);
7900 if (comparison == 0)
7901 return 0;
7902 if (!onlyjump_p (jump))
7903 return 0;
7905 /* Try to compute whether the compare/branch at the loop end is one or
7906 two instructions. */
7907 get_condition (jump, &first_compare);
7908 if (first_compare == jump)
7909 compare_and_branch = 1;
7910 else if (first_compare == prev_nonnote_insn (jump))
7911 compare_and_branch = 2;
7912 else
7913 return 0;
7916 /* If more than one condition is present to control the loop, then
7917 do not proceed, as this function does not know how to rewrite
7918 loop tests with more than one condition.
7920 Look backwards from the first insn in the last comparison
7921 sequence and see if we've got another comparison sequence. */
7923 rtx jump1;
7924 if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
7925 if (GET_CODE (jump1) == JUMP_INSN)
7926 return 0;
7929 /* Check all of the bivs to see if the compare uses one of them.
7930 Skip biv's set more than once because we can't guarantee that
7931 it will be zero on the last iteration. Also skip if the biv is
7932 used between its update and the test insn. */
7934 for (bl = ivs->list; bl; bl = bl->next)
7936 if (bl->biv_count == 1
7937 && ! bl->biv->maybe_multiple
7938 && bl->biv->dest_reg == XEXP (comparison, 0)
7939 && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
7940 first_compare))
7941 break;
7944 if (! bl)
7945 return 0;
7947 /* Look for the case where the basic induction variable is always
7948 nonnegative, and equals zero on the last iteration.
7949 In this case, add a reg_note REG_NONNEG, which allows the
7950 m68k DBRA instruction to be used. */
7952 if (((GET_CODE (comparison) == GT
7953 && GET_CODE (XEXP (comparison, 1)) == CONST_INT
7954 && INTVAL (XEXP (comparison, 1)) == -1)
7955 || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
7956 && GET_CODE (bl->biv->add_val) == CONST_INT
7957 && INTVAL (bl->biv->add_val) < 0)
7959 /* Initial value must be greater than 0,
7960 init_val % -dec_value == 0 to ensure that it equals zero on
7961 the last iteration */
7963 if (GET_CODE (bl->initial_value) == CONST_INT
7964 && INTVAL (bl->initial_value) > 0
7965 && (INTVAL (bl->initial_value)
7966 % (-INTVAL (bl->biv->add_val))) == 0)
7968 /* register always nonnegative, add REG_NOTE to branch */
7969 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7970 REG_NOTES (jump)
7971 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7972 REG_NOTES (jump));
7973 bl->nonneg = 1;
7975 return 1;
7978 /* If the decrement is 1 and the value was tested as >= 0 before
7979 the loop, then we can safely optimize. */
7980 for (p = loop_start; p; p = PREV_INSN (p))
7982 if (GET_CODE (p) == CODE_LABEL)
7983 break;
7984 if (GET_CODE (p) != JUMP_INSN)
7985 continue;
7987 before_comparison = get_condition_for_loop (loop, p);
7988 if (before_comparison
7989 && XEXP (before_comparison, 0) == bl->biv->dest_reg
7990 && GET_CODE (before_comparison) == LT
7991 && XEXP (before_comparison, 1) == const0_rtx
7992 && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
7993 && INTVAL (bl->biv->add_val) == -1)
7995 if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
7996 REG_NOTES (jump)
7997 = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
7998 REG_NOTES (jump));
7999 bl->nonneg = 1;
8001 return 1;
8005 else if (GET_CODE (bl->biv->add_val) == CONST_INT
8006 && INTVAL (bl->biv->add_val) > 0)
8008 /* Try to change inc to dec, so can apply above optimization. */
8009 /* Can do this if:
8010 all registers modified are induction variables or invariant,
8011 all memory references have non-overlapping addresses
8012 (obviously true if only one write)
8013 allow 2 insns for the compare/jump at the end of the loop. */
8014 /* Also, we must avoid any instructions which use both the reversed
8015 biv and another biv. Such instructions will fail if the loop is
8016 reversed. We meet this condition by requiring that either
8017 no_use_except_counting is true, or else that there is only
8018 one biv. */
8019 int num_nonfixed_reads = 0;
8020 /* 1 if the iteration var is used only to count iterations. */
8021 int no_use_except_counting = 0;
8022 /* 1 if the loop has no memory store, or it has a single memory store
8023 which is reversible. */
8024 int reversible_mem_store = 1;
8026 if (bl->giv_count == 0
8027 && !loop->exit_count
8028 && !loop_info->has_multiple_exit_targets)
8030 rtx bivreg = regno_reg_rtx[bl->regno];
8031 struct iv_class *blt;
8033 /* If there are no givs for this biv, and the only exit is the
8034 fall through at the end of the loop, then
8035 see if perhaps there are no uses except to count. */
8036 no_use_except_counting = 1;
8037 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8038 if (INSN_P (p))
8040 rtx set = single_set (p);
8042 if (set && GET_CODE (SET_DEST (set)) == REG
8043 && REGNO (SET_DEST (set)) == bl->regno)
8044 /* An insn that sets the biv is okay. */
8046 else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
8047 || p == prev_nonnote_insn (loop_end))
8048 && reg_mentioned_p (bivreg, PATTERN (p)))
8050 /* If either of these insns uses the biv and sets a pseudo
8051 that has more than one usage, then the biv has uses
8052 other than counting since it's used to derive a value
8053 that is used more than one time. */
8054 note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
8055 regs);
8056 if (regs->multiple_uses)
8058 no_use_except_counting = 0;
8059 break;
8062 else if (reg_mentioned_p (bivreg, PATTERN (p)))
8064 no_use_except_counting = 0;
8065 break;
8069 /* A biv has uses besides counting if it is used to set
8070 another biv. */
8071 for (blt = ivs->list; blt; blt = blt->next)
8072 if (blt->init_set
8073 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
8075 no_use_except_counting = 0;
8076 break;
8080 if (no_use_except_counting)
8081 /* No need to worry about MEMs. */
8083 else if (loop_info->num_mem_sets <= 1)
8085 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8086 if (INSN_P (p))
8087 num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
8089 /* If the loop has a single store, and the destination address is
8090 invariant, then we can't reverse the loop, because this address
8091 might then have the wrong value at loop exit.
8092 This would work if the source was invariant also, however, in that
8093 case, the insn should have been moved out of the loop. */
8095 if (loop_info->num_mem_sets == 1)
8097 struct induction *v;
8099 /* If we could prove that each of the memory locations
8100 written to was different, then we could reverse the
8101 store -- but we don't presently have any way of
8102 knowing that. */
8103 reversible_mem_store = 0;
8105 /* If the store depends on a register that is set after the
8106 store, it depends on the initial value, and is thus not
8107 reversible. */
8108 for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
8110 if (v->giv_type == DEST_REG
8111 && reg_mentioned_p (v->dest_reg,
8112 PATTERN (loop_info->first_loop_store_insn))
8113 && loop_insn_first_p (loop_info->first_loop_store_insn,
8114 v->insn))
8115 reversible_mem_store = 0;
8119 else
8120 return 0;
8122 /* This code only acts for innermost loops. Also it simplifies
8123 the memory address check by only reversing loops with
8124 zero or one memory access.
8125 Two memory accesses could involve parts of the same array,
8126 and that can't be reversed.
8127 If the biv is used only for counting, than we don't need to worry
8128 about all these things. */
8130 if ((num_nonfixed_reads <= 1
8131 && ! loop_info->has_nonconst_call
8132 && ! loop_info->has_volatile
8133 && reversible_mem_store
8134 && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
8135 + num_unmoved_movables (loop) + compare_and_branch == insn_count)
8136 && (bl == ivs->list && bl->next == 0))
8137 || no_use_except_counting)
8139 rtx tem;
8141 /* Loop can be reversed. */
8142 if (loop_dump_stream)
8143 fprintf (loop_dump_stream, "Can reverse loop\n");
8145 /* Now check other conditions:
8147 The increment must be a constant, as must the initial value,
8148 and the comparison code must be LT.
8150 This test can probably be improved since +/- 1 in the constant
8151 can be obtained by changing LT to LE and vice versa; this is
8152 confusing. */
8154 if (comparison
8155 /* for constants, LE gets turned into LT */
8156 && (GET_CODE (comparison) == LT
8157 || (GET_CODE (comparison) == LE
8158 && no_use_except_counting)))
8160 HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
8161 rtx initial_value, comparison_value;
8162 int nonneg = 0;
8163 enum rtx_code cmp_code;
8164 int comparison_const_width;
8165 unsigned HOST_WIDE_INT comparison_sign_mask;
8167 add_val = INTVAL (bl->biv->add_val);
8168 comparison_value = XEXP (comparison, 1);
8169 if (GET_MODE (comparison_value) == VOIDmode)
8170 comparison_const_width
8171 = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
8172 else
8173 comparison_const_width
8174 = GET_MODE_BITSIZE (GET_MODE (comparison_value));
8175 if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
8176 comparison_const_width = HOST_BITS_PER_WIDE_INT;
8177 comparison_sign_mask
8178 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
8180 /* If the comparison value is not a loop invariant, then we
8181 can not reverse this loop.
8183 ??? If the insns which initialize the comparison value as
8184 a whole compute an invariant result, then we could move
8185 them out of the loop and proceed with loop reversal. */
8186 if (! loop_invariant_p (loop, comparison_value))
8187 return 0;
8189 if (GET_CODE (comparison_value) == CONST_INT)
8190 comparison_val = INTVAL (comparison_value);
8191 initial_value = bl->initial_value;
8193 /* Normalize the initial value if it is an integer and
8194 has no other use except as a counter. This will allow
8195 a few more loops to be reversed. */
8196 if (no_use_except_counting
8197 && GET_CODE (comparison_value) == CONST_INT
8198 && GET_CODE (initial_value) == CONST_INT)
8200 comparison_val = comparison_val - INTVAL (bl->initial_value);
8201 /* The code below requires comparison_val to be a multiple
8202 of add_val in order to do the loop reversal, so
8203 round up comparison_val to a multiple of add_val.
8204 Since comparison_value is constant, we know that the
8205 current comparison code is LT. */
8206 comparison_val = comparison_val + add_val - 1;
8207 comparison_val
8208 -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
8209 /* We postpone overflow checks for COMPARISON_VAL here;
8210 even if there is an overflow, we might still be able to
8211 reverse the loop, if converting the loop exit test to
8212 NE is possible. */
8213 initial_value = const0_rtx;
8216 /* First check if we can do a vanilla loop reversal. */
8217 if (initial_value == const0_rtx
8218 /* If we have a decrement_and_branch_on_count,
8219 prefer the NE test, since this will allow that
8220 instruction to be generated. Note that we must
8221 use a vanilla loop reversal if the biv is used to
8222 calculate a giv or has a non-counting use. */
8223 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8224 && defined (HAVE_decrement_and_branch_on_count)
8225 && (! (add_val == 1 && loop->vtop
8226 && (bl->biv_count == 0
8227 || no_use_except_counting)))
8228 #endif
8229 && GET_CODE (comparison_value) == CONST_INT
8230 /* Now do postponed overflow checks on COMPARISON_VAL. */
8231 && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
8232 & comparison_sign_mask))
8234 /* Register will always be nonnegative, with value
8235 0 on last iteration */
8236 add_adjust = add_val;
8237 nonneg = 1;
8238 cmp_code = GE;
8240 else if (add_val == 1 && loop->vtop
8241 && (bl->biv_count == 0
8242 || no_use_except_counting))
8244 add_adjust = 0;
8245 cmp_code = NE;
8247 else
8248 return 0;
8250 if (GET_CODE (comparison) == LE)
8251 add_adjust -= add_val;
8253 /* If the initial value is not zero, or if the comparison
8254 value is not an exact multiple of the increment, then we
8255 can not reverse this loop. */
8256 if (initial_value == const0_rtx
8257 && GET_CODE (comparison_value) == CONST_INT)
8259 if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
8260 return 0;
8262 else
8264 if (! no_use_except_counting || add_val != 1)
8265 return 0;
8268 final_value = comparison_value;
8270 /* Reset these in case we normalized the initial value
8271 and comparison value above. */
8272 if (GET_CODE (comparison_value) == CONST_INT
8273 && GET_CODE (initial_value) == CONST_INT)
8275 comparison_value = GEN_INT (comparison_val);
8276 final_value
8277 = GEN_INT (comparison_val + INTVAL (bl->initial_value));
8279 bl->initial_value = initial_value;
8281 /* Save some info needed to produce the new insns. */
8282 reg = bl->biv->dest_reg;
8283 jump_label = condjump_label (PREV_INSN (loop_end));
8284 new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
8286 /* Set start_value; if this is not a CONST_INT, we need
8287 to generate a SUB.
8288 Initialize biv to start_value before loop start.
8289 The old initializing insn will be deleted as a
8290 dead store by flow.c. */
8291 if (initial_value == const0_rtx
8292 && GET_CODE (comparison_value) == CONST_INT)
8294 start_value = GEN_INT (comparison_val - add_adjust);
8295 loop_insn_hoist (loop, gen_move_insn (reg, start_value));
8297 else if (GET_CODE (initial_value) == CONST_INT)
8299 enum machine_mode mode = GET_MODE (reg);
8300 rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8301 rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
8303 if (add_insn == 0)
8304 return 0;
8306 start_value
8307 = gen_rtx_PLUS (mode, comparison_value, offset);
8308 loop_insn_hoist (loop, add_insn);
8309 if (GET_CODE (comparison) == LE)
8310 final_value = gen_rtx_PLUS (mode, comparison_value,
8311 GEN_INT (add_val));
8313 else if (! add_adjust)
8315 enum machine_mode mode = GET_MODE (reg);
8316 rtx sub_insn = gen_sub3_insn (reg, comparison_value,
8317 initial_value);
8319 if (sub_insn == 0)
8320 return 0;
8321 start_value
8322 = gen_rtx_MINUS (mode, comparison_value, initial_value);
8323 loop_insn_hoist (loop, sub_insn);
8325 else
8326 /* We could handle the other cases too, but it'll be
8327 better to have a testcase first. */
8328 return 0;
8330 /* We may not have a single insn which can increment a reg, so
8331 create a sequence to hold all the insns from expand_inc. */
8332 start_sequence ();
8333 expand_inc (reg, new_add_val);
8334 tem = gen_sequence ();
8335 end_sequence ();
8337 p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
8338 delete_insn (bl->biv->insn);
8340 /* Update biv info to reflect its new status. */
8341 bl->biv->insn = p;
8342 bl->initial_value = start_value;
8343 bl->biv->add_val = new_add_val;
8345 /* Update loop info. */
8346 loop_info->initial_value = reg;
8347 loop_info->initial_equiv_value = reg;
8348 loop_info->final_value = const0_rtx;
8349 loop_info->final_equiv_value = const0_rtx;
8350 loop_info->comparison_value = const0_rtx;
8351 loop_info->comparison_code = cmp_code;
8352 loop_info->increment = new_add_val;
8354 /* Inc LABEL_NUSES so that delete_insn will
8355 not delete the label. */
8356 LABEL_NUSES (XEXP (jump_label, 0))++;
8358 /* Emit an insn after the end of the loop to set the biv's
8359 proper exit value if it is used anywhere outside the loop. */
8360 if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8361 || ! bl->init_insn
8362 || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8363 loop_insn_sink (loop, gen_move_insn (reg, final_value));
8365 /* Delete compare/branch at end of loop. */
8366 delete_related_insns (PREV_INSN (loop_end));
8367 if (compare_and_branch == 2)
8368 delete_related_insns (first_compare);
8370 /* Add new compare/branch insn at end of loop. */
8371 start_sequence ();
8372 emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8373 GET_MODE (reg), 0,
8374 XEXP (jump_label, 0));
8375 tem = gen_sequence ();
8376 end_sequence ();
8377 emit_jump_insn_before (tem, loop_end);
8379 for (tem = PREV_INSN (loop_end);
8380 tem && GET_CODE (tem) != JUMP_INSN;
8381 tem = PREV_INSN (tem))
8384 if (tem)
8385 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8387 if (nonneg)
8389 if (tem)
8391 /* Increment of LABEL_NUSES done above. */
8392 /* Register is now always nonnegative,
8393 so add REG_NONNEG note to the branch. */
8394 REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
8395 REG_NOTES (tem));
8397 bl->nonneg = 1;
8400 /* No insn may reference both the reversed and another biv or it
8401 will fail (see comment near the top of the loop reversal
8402 code).
8403 Earlier on, we have verified that the biv has no use except
8404 counting, or it is the only biv in this function.
8405 However, the code that computes no_use_except_counting does
8406 not verify reg notes. It's possible to have an insn that
8407 references another biv, and has a REG_EQUAL note with an
8408 expression based on the reversed biv. To avoid this case,
8409 remove all REG_EQUAL notes based on the reversed biv
8410 here. */
8411 for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8412 if (INSN_P (p))
8414 rtx *pnote;
8415 rtx set = single_set (p);
8416 /* If this is a set of a GIV based on the reversed biv, any
8417 REG_EQUAL notes should still be correct. */
8418 if (! set
8419 || GET_CODE (SET_DEST (set)) != REG
8420 || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
8421 || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
8422 || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
8423 for (pnote = &REG_NOTES (p); *pnote;)
8425 if (REG_NOTE_KIND (*pnote) == REG_EQUAL
8426 && reg_mentioned_p (regno_reg_rtx[bl->regno],
8427 XEXP (*pnote, 0)))
8428 *pnote = XEXP (*pnote, 1);
8429 else
8430 pnote = &XEXP (*pnote, 1);
8434 /* Mark that this biv has been reversed. Each giv which depends
8435 on this biv, and which is also live past the end of the loop
8436 will have to be fixed up. */
8438 bl->reversed = 1;
8440 if (loop_dump_stream)
8442 fprintf (loop_dump_stream, "Reversed loop");
8443 if (bl->nonneg)
8444 fprintf (loop_dump_stream, " and added reg_nonneg\n");
8445 else
8446 fprintf (loop_dump_stream, "\n");
8449 return 1;
8454 return 0;
8457 /* Verify whether the biv BL appears to be eliminable,
8458 based on the insns in the loop that refer to it.
8460 If ELIMINATE_P is non-zero, actually do the elimination.
8462 THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8463 determine whether invariant insns should be placed inside or at the
8464 start of the loop. */
8466 static int
8467 maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
8468 const struct loop *loop;
8469 struct iv_class *bl;
8470 int eliminate_p;
8471 int threshold, insn_count;
8473 struct loop_ivs *ivs = LOOP_IVS (loop);
8474 rtx reg = bl->biv->dest_reg;
8475 rtx p;
8477 /* Scan all insns in the loop, stopping if we find one that uses the
8478 biv in a way that we cannot eliminate. */
8480 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
8482 enum rtx_code code = GET_CODE (p);
8483 basic_block where_bb = 0;
8484 rtx where_insn = threshold >= insn_count ? 0 : p;
8486 /* If this is a libcall that sets a giv, skip ahead to its end. */
8487 if (GET_RTX_CLASS (code) == 'i')
8489 rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8491 if (note)
8493 rtx last = XEXP (note, 0);
8494 rtx set = single_set (last);
8496 if (set && GET_CODE (SET_DEST (set)) == REG)
8498 unsigned int regno = REGNO (SET_DEST (set));
8500 if (regno < ivs->n_regs
8501 && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
8502 && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
8503 p = last;
8507 if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8508 && reg_mentioned_p (reg, PATTERN (p))
8509 && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
8510 eliminate_p, where_bb, where_insn))
8512 if (loop_dump_stream)
8513 fprintf (loop_dump_stream,
8514 "Cannot eliminate biv %d: biv used in insn %d.\n",
8515 bl->regno, INSN_UID (p));
8516 break;
8520 if (p == loop->end)
8522 if (loop_dump_stream)
8523 fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8524 bl->regno, eliminate_p ? "was" : "can be");
8525 return 1;
8528 return 0;
8531 /* INSN and REFERENCE are instructions in the same insn chain.
8532 Return non-zero if INSN is first. */
8535 loop_insn_first_p (insn, reference)
8536 rtx insn, reference;
8538 rtx p, q;
8540 for (p = insn, q = reference;;)
8542 /* Start with test for not first so that INSN == REFERENCE yields not
8543 first. */
8544 if (q == insn || ! p)
8545 return 0;
8546 if (p == reference || ! q)
8547 return 1;
8549 /* Either of P or Q might be a NOTE. Notes have the same LUID as the
8550 previous insn, hence the <= comparison below does not work if
8551 P is a note. */
8552 if (INSN_UID (p) < max_uid_for_loop
8553 && INSN_UID (q) < max_uid_for_loop
8554 && GET_CODE (p) != NOTE)
8555 return INSN_LUID (p) <= INSN_LUID (q);
8557 if (INSN_UID (p) >= max_uid_for_loop
8558 || GET_CODE (p) == NOTE)
8559 p = NEXT_INSN (p);
8560 if (INSN_UID (q) >= max_uid_for_loop)
8561 q = NEXT_INSN (q);
8565 /* We are trying to eliminate BIV in INSN using GIV. Return non-zero if
8566 the offset that we have to take into account due to auto-increment /
8567 div derivation is zero. */
8568 static int
8569 biv_elimination_giv_has_0_offset (biv, giv, insn)
8570 struct induction *biv, *giv;
8571 rtx insn;
8573 /* If the giv V had the auto-inc address optimization applied
8574 to it, and INSN occurs between the giv insn and the biv
8575 insn, then we'd have to adjust the value used here.
8576 This is rare, so we don't bother to make this possible. */
8577 if (giv->auto_inc_opt
8578 && ((loop_insn_first_p (giv->insn, insn)
8579 && loop_insn_first_p (insn, biv->insn))
8580 || (loop_insn_first_p (biv->insn, insn)
8581 && loop_insn_first_p (insn, giv->insn))))
8582 return 0;
8584 return 1;
8587 /* If BL appears in X (part of the pattern of INSN), see if we can
8588 eliminate its use. If so, return 1. If not, return 0.
8590 If BIV does not appear in X, return 1.
8592 If ELIMINATE_P is non-zero, actually do the elimination.
8593 WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8594 Depending on how many items have been moved out of the loop, it
8595 will either be before INSN (when WHERE_INSN is non-zero) or at the
8596 start of the loop (when WHERE_INSN is zero). */
8598 static int
8599 maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
8600 const struct loop *loop;
8601 rtx x, insn;
8602 struct iv_class *bl;
8603 int eliminate_p;
8604 basic_block where_bb;
8605 rtx where_insn;
8607 enum rtx_code code = GET_CODE (x);
8608 rtx reg = bl->biv->dest_reg;
8609 enum machine_mode mode = GET_MODE (reg);
8610 struct induction *v;
8611 rtx arg, tem;
8612 #ifdef HAVE_cc0
8613 rtx new;
8614 #endif
8615 int arg_operand;
8616 const char *fmt;
8617 int i, j;
8619 switch (code)
8621 case REG:
8622 /* If we haven't already been able to do something with this BIV,
8623 we can't eliminate it. */
8624 if (x == reg)
8625 return 0;
8626 return 1;
8628 case SET:
8629 /* If this sets the BIV, it is not a problem. */
8630 if (SET_DEST (x) == reg)
8631 return 1;
8633 /* If this is an insn that defines a giv, it is also ok because
8634 it will go away when the giv is reduced. */
8635 for (v = bl->giv; v; v = v->next_iv)
8636 if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8637 return 1;
8639 #ifdef HAVE_cc0
8640 if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8642 /* Can replace with any giv that was reduced and
8643 that has (MULT_VAL != 0) and (ADD_VAL == 0).
8644 Require a constant for MULT_VAL, so we know it's nonzero.
8645 ??? We disable this optimization to avoid potential
8646 overflows. */
8648 for (v = bl->giv; v; v = v->next_iv)
8649 if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8650 && v->add_val == const0_rtx
8651 && ! v->ignore && ! v->maybe_dead && v->always_computable
8652 && v->mode == mode
8653 && 0)
8655 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8656 continue;
8658 if (! eliminate_p)
8659 return 1;
8661 /* If the giv has the opposite direction of change,
8662 then reverse the comparison. */
8663 if (INTVAL (v->mult_val) < 0)
8664 new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8665 const0_rtx, v->new_reg);
8666 else
8667 new = v->new_reg;
8669 /* We can probably test that giv's reduced reg. */
8670 if (validate_change (insn, &SET_SRC (x), new, 0))
8671 return 1;
8674 /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8675 replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8676 Require a constant for MULT_VAL, so we know it's nonzero.
8677 ??? Do this only if ADD_VAL is a pointer to avoid a potential
8678 overflow problem. */
8680 for (v = bl->giv; v; v = v->next_iv)
8681 if (GET_CODE (v->mult_val) == CONST_INT
8682 && v->mult_val != const0_rtx
8683 && ! v->ignore && ! v->maybe_dead && v->always_computable
8684 && v->mode == mode
8685 && (GET_CODE (v->add_val) == SYMBOL_REF
8686 || GET_CODE (v->add_val) == LABEL_REF
8687 || GET_CODE (v->add_val) == CONST
8688 || (GET_CODE (v->add_val) == REG
8689 && REG_POINTER (v->add_val))))
8691 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8692 continue;
8694 if (! eliminate_p)
8695 return 1;
8697 /* If the giv has the opposite direction of change,
8698 then reverse the comparison. */
8699 if (INTVAL (v->mult_val) < 0)
8700 new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8701 v->new_reg);
8702 else
8703 new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8704 copy_rtx (v->add_val));
8706 /* Replace biv with the giv's reduced register. */
8707 update_reg_last_use (v->add_val, insn);
8708 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8709 return 1;
8711 /* Insn doesn't support that constant or invariant. Copy it
8712 into a register (it will be a loop invariant.) */
8713 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8715 loop_insn_emit_before (loop, 0, where_insn,
8716 gen_move_insn (tem,
8717 copy_rtx (v->add_val)));
8719 /* Substitute the new register for its invariant value in
8720 the compare expression. */
8721 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8722 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8723 return 1;
8726 #endif
8727 break;
8729 case COMPARE:
8730 case EQ: case NE:
8731 case GT: case GE: case GTU: case GEU:
8732 case LT: case LE: case LTU: case LEU:
8733 /* See if either argument is the biv. */
8734 if (XEXP (x, 0) == reg)
8735 arg = XEXP (x, 1), arg_operand = 1;
8736 else if (XEXP (x, 1) == reg)
8737 arg = XEXP (x, 0), arg_operand = 0;
8738 else
8739 break;
8741 if (CONSTANT_P (arg))
8743 /* First try to replace with any giv that has constant positive
8744 mult_val and constant add_val. We might be able to support
8745 negative mult_val, but it seems complex to do it in general. */
8747 for (v = bl->giv; v; v = v->next_iv)
8748 if (GET_CODE (v->mult_val) == CONST_INT
8749 && INTVAL (v->mult_val) > 0
8750 && (GET_CODE (v->add_val) == SYMBOL_REF
8751 || GET_CODE (v->add_val) == LABEL_REF
8752 || GET_CODE (v->add_val) == CONST
8753 || (GET_CODE (v->add_val) == REG
8754 && REG_POINTER (v->add_val)))
8755 && ! v->ignore && ! v->maybe_dead && v->always_computable
8756 && v->mode == mode)
8758 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8759 continue;
8761 if (! eliminate_p)
8762 return 1;
8764 /* Replace biv with the giv's reduced reg. */
8765 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
8767 /* If all constants are actually constant integers and
8768 the derived constant can be directly placed in the COMPARE,
8769 do so. */
8770 if (GET_CODE (arg) == CONST_INT
8771 && GET_CODE (v->mult_val) == CONST_INT
8772 && GET_CODE (v->add_val) == CONST_INT)
8774 validate_change (insn, &XEXP (x, arg_operand),
8775 GEN_INT (INTVAL (arg)
8776 * INTVAL (v->mult_val)
8777 + INTVAL (v->add_val)), 1);
8779 else
8781 /* Otherwise, load it into a register. */
8782 tem = gen_reg_rtx (mode);
8783 loop_iv_add_mult_emit_before (loop, arg,
8784 v->mult_val, v->add_val,
8785 tem, where_bb, where_insn);
8786 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8788 if (apply_change_group ())
8789 return 1;
8792 /* Look for giv with positive constant mult_val and nonconst add_val.
8793 Insert insns to calculate new compare value.
8794 ??? Turn this off due to possible overflow. */
8796 for (v = bl->giv; v; v = v->next_iv)
8797 if (GET_CODE (v->mult_val) == CONST_INT
8798 && INTVAL (v->mult_val) > 0
8799 && ! v->ignore && ! v->maybe_dead && v->always_computable
8800 && v->mode == mode
8801 && 0)
8803 rtx tem;
8805 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8806 continue;
8808 if (! eliminate_p)
8809 return 1;
8811 tem = gen_reg_rtx (mode);
8813 /* Replace biv with giv's reduced register. */
8814 validate_change (insn, &XEXP (x, 1 - arg_operand),
8815 v->new_reg, 1);
8817 /* Compute value to compare against. */
8818 loop_iv_add_mult_emit_before (loop, arg,
8819 v->mult_val, v->add_val,
8820 tem, where_bb, where_insn);
8821 /* Use it in this insn. */
8822 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8823 if (apply_change_group ())
8824 return 1;
8827 else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8829 if (loop_invariant_p (loop, arg) == 1)
8831 /* Look for giv with constant positive mult_val and nonconst
8832 add_val. Insert insns to compute new compare value.
8833 ??? Turn this off due to possible overflow. */
8835 for (v = bl->giv; v; v = v->next_iv)
8836 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8837 && ! v->ignore && ! v->maybe_dead && v->always_computable
8838 && v->mode == mode
8839 && 0)
8841 rtx tem;
8843 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8844 continue;
8846 if (! eliminate_p)
8847 return 1;
8849 tem = gen_reg_rtx (mode);
8851 /* Replace biv with giv's reduced register. */
8852 validate_change (insn, &XEXP (x, 1 - arg_operand),
8853 v->new_reg, 1);
8855 /* Compute value to compare against. */
8856 loop_iv_add_mult_emit_before (loop, arg,
8857 v->mult_val, v->add_val,
8858 tem, where_bb, where_insn);
8859 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8860 if (apply_change_group ())
8861 return 1;
8865 /* This code has problems. Basically, you can't know when
8866 seeing if we will eliminate BL, whether a particular giv
8867 of ARG will be reduced. If it isn't going to be reduced,
8868 we can't eliminate BL. We can try forcing it to be reduced,
8869 but that can generate poor code.
8871 The problem is that the benefit of reducing TV, below should
8872 be increased if BL can actually be eliminated, but this means
8873 we might have to do a topological sort of the order in which
8874 we try to process biv. It doesn't seem worthwhile to do
8875 this sort of thing now. */
8877 #if 0
8878 /* Otherwise the reg compared with had better be a biv. */
8879 if (GET_CODE (arg) != REG
8880 || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
8881 return 0;
8883 /* Look for a pair of givs, one for each biv,
8884 with identical coefficients. */
8885 for (v = bl->giv; v; v = v->next_iv)
8887 struct induction *tv;
8889 if (v->ignore || v->maybe_dead || v->mode != mode)
8890 continue;
8892 for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
8893 tv = tv->next_iv)
8894 if (! tv->ignore && ! tv->maybe_dead
8895 && rtx_equal_p (tv->mult_val, v->mult_val)
8896 && rtx_equal_p (tv->add_val, v->add_val)
8897 && tv->mode == mode)
8899 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8900 continue;
8902 if (! eliminate_p)
8903 return 1;
8905 /* Replace biv with its giv's reduced reg. */
8906 XEXP (x, 1 - arg_operand) = v->new_reg;
8907 /* Replace other operand with the other giv's
8908 reduced reg. */
8909 XEXP (x, arg_operand) = tv->new_reg;
8910 return 1;
8913 #endif
8916 /* If we get here, the biv can't be eliminated. */
8917 return 0;
8919 case MEM:
8920 /* If this address is a DEST_ADDR giv, it doesn't matter if the
8921 biv is used in it, since it will be replaced. */
8922 for (v = bl->giv; v; v = v->next_iv)
8923 if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
8924 return 1;
8925 break;
8927 default:
8928 break;
8931 /* See if any subexpression fails elimination. */
8932 fmt = GET_RTX_FORMAT (code);
8933 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8935 switch (fmt[i])
8937 case 'e':
8938 if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
8939 eliminate_p, where_bb, where_insn))
8940 return 0;
8941 break;
8943 case 'E':
8944 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8945 if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
8946 eliminate_p, where_bb, where_insn))
8947 return 0;
8948 break;
8952 return 1;
8955 /* Return nonzero if the last use of REG
8956 is in an insn following INSN in the same basic block. */
8958 static int
8959 last_use_this_basic_block (reg, insn)
8960 rtx reg;
8961 rtx insn;
8963 rtx n;
8964 for (n = insn;
8965 n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
8966 n = NEXT_INSN (n))
8968 if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
8969 return 1;
8971 return 0;
8974 /* Called via `note_stores' to record the initial value of a biv. Here we
8975 just record the location of the set and process it later. */
8977 static void
8978 record_initial (dest, set, data)
8979 rtx dest;
8980 rtx set;
8981 void *data ATTRIBUTE_UNUSED;
8983 struct loop_ivs *ivs = (struct loop_ivs *) data;
8984 struct iv_class *bl;
8986 if (GET_CODE (dest) != REG
8987 || REGNO (dest) >= ivs->n_regs
8988 || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
8989 return;
8991 bl = REG_IV_CLASS (ivs, REGNO (dest));
8993 /* If this is the first set found, record it. */
8994 if (bl->init_insn == 0)
8996 bl->init_insn = note_insn;
8997 bl->init_set = set;
9001 /* If any of the registers in X are "old" and currently have a last use earlier
9002 than INSN, update them to have a last use of INSN. Their actual last use
9003 will be the previous insn but it will not have a valid uid_luid so we can't
9004 use it. X must be a source expression only. */
9006 static void
9007 update_reg_last_use (x, insn)
9008 rtx x;
9009 rtx insn;
9011 /* Check for the case where INSN does not have a valid luid. In this case,
9012 there is no need to modify the regno_last_uid, as this can only happen
9013 when code is inserted after the loop_end to set a pseudo's final value,
9014 and hence this insn will never be the last use of x.
9015 ???? This comment is not correct. See for example loop_givs_reduce.
9016 This may insert an insn before another new insn. */
9017 if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
9018 && INSN_UID (insn) < max_uid_for_loop
9019 && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
9021 REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
9023 else
9025 int i, j;
9026 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
9027 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9029 if (fmt[i] == 'e')
9030 update_reg_last_use (XEXP (x, i), insn);
9031 else if (fmt[i] == 'E')
9032 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9033 update_reg_last_use (XVECEXP (x, i, j), insn);
9038 /* Given an insn INSN and condition COND, return the condition in a
9039 canonical form to simplify testing by callers. Specifically:
9041 (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9042 (2) Both operands will be machine operands; (cc0) will have been replaced.
9043 (3) If an operand is a constant, it will be the second operand.
9044 (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9045 for GE, GEU, and LEU.
9047 If the condition cannot be understood, or is an inequality floating-point
9048 comparison which needs to be reversed, 0 will be returned.
9050 If REVERSE is non-zero, then reverse the condition prior to canonizing it.
9052 If EARLIEST is non-zero, it is a pointer to a place where the earliest
9053 insn used in locating the condition was found. If a replacement test
9054 of the condition is desired, it should be placed in front of that
9055 insn and we will be sure that the inputs are still valid.
9057 If WANT_REG is non-zero, we wish the condition to be relative to that
9058 register, if possible. Therefore, do not canonicalize the condition
9059 further. */
9062 canonicalize_condition (insn, cond, reverse, earliest, want_reg)
9063 rtx insn;
9064 rtx cond;
9065 int reverse;
9066 rtx *earliest;
9067 rtx want_reg;
9069 enum rtx_code code;
9070 rtx prev = insn;
9071 rtx set;
9072 rtx tem;
9073 rtx op0, op1;
9074 int reverse_code = 0;
9075 enum machine_mode mode;
9077 code = GET_CODE (cond);
9078 mode = GET_MODE (cond);
9079 op0 = XEXP (cond, 0);
9080 op1 = XEXP (cond, 1);
9082 if (reverse)
9083 code = reversed_comparison_code (cond, insn);
9084 if (code == UNKNOWN)
9085 return 0;
9087 if (earliest)
9088 *earliest = insn;
9090 /* If we are comparing a register with zero, see if the register is set
9091 in the previous insn to a COMPARE or a comparison operation. Perform
9092 the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9093 in cse.c */
9095 while (GET_RTX_CLASS (code) == '<'
9096 && op1 == CONST0_RTX (GET_MODE (op0))
9097 && op0 != want_reg)
9099 /* Set non-zero when we find something of interest. */
9100 rtx x = 0;
9102 #ifdef HAVE_cc0
9103 /* If comparison with cc0, import actual comparison from compare
9104 insn. */
9105 if (op0 == cc0_rtx)
9107 if ((prev = prev_nonnote_insn (prev)) == 0
9108 || GET_CODE (prev) != INSN
9109 || (set = single_set (prev)) == 0
9110 || SET_DEST (set) != cc0_rtx)
9111 return 0;
9113 op0 = SET_SRC (set);
9114 op1 = CONST0_RTX (GET_MODE (op0));
9115 if (earliest)
9116 *earliest = prev;
9118 #endif
9120 /* If this is a COMPARE, pick up the two things being compared. */
9121 if (GET_CODE (op0) == COMPARE)
9123 op1 = XEXP (op0, 1);
9124 op0 = XEXP (op0, 0);
9125 continue;
9127 else if (GET_CODE (op0) != REG)
9128 break;
9130 /* Go back to the previous insn. Stop if it is not an INSN. We also
9131 stop if it isn't a single set or if it has a REG_INC note because
9132 we don't want to bother dealing with it. */
9134 if ((prev = prev_nonnote_insn (prev)) == 0
9135 || GET_CODE (prev) != INSN
9136 || FIND_REG_INC_NOTE (prev, NULL_RTX))
9137 break;
9139 set = set_of (op0, prev);
9141 if (set
9142 && (GET_CODE (set) != SET
9143 || !rtx_equal_p (SET_DEST (set), op0)))
9144 break;
9146 /* If this is setting OP0, get what it sets it to if it looks
9147 relevant. */
9148 if (set)
9150 enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
9152 /* ??? We may not combine comparisons done in a CCmode with
9153 comparisons not done in a CCmode. This is to aid targets
9154 like Alpha that have an IEEE compliant EQ instruction, and
9155 a non-IEEE compliant BEQ instruction. The use of CCmode is
9156 actually artificial, simply to prevent the combination, but
9157 should not affect other platforms.
9159 However, we must allow VOIDmode comparisons to match either
9160 CCmode or non-CCmode comparison, because some ports have
9161 modeless comparisons inside branch patterns.
9163 ??? This mode check should perhaps look more like the mode check
9164 in simplify_comparison in combine. */
9166 if ((GET_CODE (SET_SRC (set)) == COMPARE
9167 || (((code == NE
9168 || (code == LT
9169 && GET_MODE_CLASS (inner_mode) == MODE_INT
9170 && (GET_MODE_BITSIZE (inner_mode)
9171 <= HOST_BITS_PER_WIDE_INT)
9172 && (STORE_FLAG_VALUE
9173 & ((HOST_WIDE_INT) 1
9174 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9175 #ifdef FLOAT_STORE_FLAG_VALUE
9176 || (code == LT
9177 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9178 && (REAL_VALUE_NEGATIVE
9179 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9180 #endif
9182 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
9183 && (((GET_MODE_CLASS (mode) == MODE_CC)
9184 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9185 || mode == VOIDmode || inner_mode == VOIDmode))
9186 x = SET_SRC (set);
9187 else if (((code == EQ
9188 || (code == GE
9189 && (GET_MODE_BITSIZE (inner_mode)
9190 <= HOST_BITS_PER_WIDE_INT)
9191 && GET_MODE_CLASS (inner_mode) == MODE_INT
9192 && (STORE_FLAG_VALUE
9193 & ((HOST_WIDE_INT) 1
9194 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9195 #ifdef FLOAT_STORE_FLAG_VALUE
9196 || (code == GE
9197 && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9198 && (REAL_VALUE_NEGATIVE
9199 (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9200 #endif
9202 && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
9203 && (((GET_MODE_CLASS (mode) == MODE_CC)
9204 == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9205 || mode == VOIDmode || inner_mode == VOIDmode))
9208 reverse_code = 1;
9209 x = SET_SRC (set);
9211 else
9212 break;
9215 else if (reg_set_p (op0, prev))
9216 /* If this sets OP0, but not directly, we have to give up. */
9217 break;
9219 if (x)
9221 if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9222 code = GET_CODE (x);
9223 if (reverse_code)
9225 code = reversed_comparison_code (x, prev);
9226 if (code == UNKNOWN)
9227 return 0;
9228 reverse_code = 0;
9231 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
9232 if (earliest)
9233 *earliest = prev;
9237 /* If constant is first, put it last. */
9238 if (CONSTANT_P (op0))
9239 code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
9241 /* If OP0 is the result of a comparison, we weren't able to find what
9242 was really being compared, so fail. */
9243 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
9244 return 0;
9246 /* Canonicalize any ordered comparison with integers involving equality
9247 if we can do computations in the relevant mode and we do not
9248 overflow. */
9250 if (GET_CODE (op1) == CONST_INT
9251 && GET_MODE (op0) != VOIDmode
9252 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
9254 HOST_WIDE_INT const_val = INTVAL (op1);
9255 unsigned HOST_WIDE_INT uconst_val = const_val;
9256 unsigned HOST_WIDE_INT max_val
9257 = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
9259 switch (code)
9261 case LE:
9262 if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
9263 code = LT, op1 = GEN_INT (const_val + 1);
9264 break;
9266 /* When cross-compiling, const_val might be sign-extended from
9267 BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9268 case GE:
9269 if ((HOST_WIDE_INT) (const_val & max_val)
9270 != (((HOST_WIDE_INT) 1
9271 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9272 code = GT, op1 = GEN_INT (const_val - 1);
9273 break;
9275 case LEU:
9276 if (uconst_val < max_val)
9277 code = LTU, op1 = GEN_INT (uconst_val + 1);
9278 break;
9280 case GEU:
9281 if (uconst_val != 0)
9282 code = GTU, op1 = GEN_INT (uconst_val - 1);
9283 break;
9285 default:
9286 break;
9290 #ifdef HAVE_cc0
9291 /* Never return CC0; return zero instead. */
9292 if (op0 == cc0_rtx)
9293 return 0;
9294 #endif
9296 return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
9299 /* Given a jump insn JUMP, return the condition that will cause it to branch
9300 to its JUMP_LABEL. If the condition cannot be understood, or is an
9301 inequality floating-point comparison which needs to be reversed, 0 will
9302 be returned.
9304 If EARLIEST is non-zero, it is a pointer to a place where the earliest
9305 insn used in locating the condition was found. If a replacement test
9306 of the condition is desired, it should be placed in front of that
9307 insn and we will be sure that the inputs are still valid. */
9310 get_condition (jump, earliest)
9311 rtx jump;
9312 rtx *earliest;
9314 rtx cond;
9315 int reverse;
9316 rtx set;
9318 /* If this is not a standard conditional jump, we can't parse it. */
9319 if (GET_CODE (jump) != JUMP_INSN
9320 || ! any_condjump_p (jump))
9321 return 0;
9322 set = pc_set (jump);
9324 cond = XEXP (SET_SRC (set), 0);
9326 /* If this branches to JUMP_LABEL when the condition is false, reverse
9327 the condition. */
9328 reverse
9329 = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
9330 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
9332 return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
9335 /* Similar to above routine, except that we also put an invariant last
9336 unless both operands are invariants. */
9339 get_condition_for_loop (loop, x)
9340 const struct loop *loop;
9341 rtx x;
9343 rtx comparison = get_condition (x, (rtx*) 0);
9345 if (comparison == 0
9346 || ! loop_invariant_p (loop, XEXP (comparison, 0))
9347 || loop_invariant_p (loop, XEXP (comparison, 1)))
9348 return comparison;
9350 return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
9351 XEXP (comparison, 1), XEXP (comparison, 0));
9354 /* Scan the function and determine whether it has indirect (computed) jumps.
9356 This is taken mostly from flow.c; similar code exists elsewhere
9357 in the compiler. It may be useful to put this into rtlanal.c. */
9358 static int
9359 indirect_jump_in_function_p (start)
9360 rtx start;
9362 rtx insn;
9364 for (insn = start; insn; insn = NEXT_INSN (insn))
9365 if (computed_jump_p (insn))
9366 return 1;
9368 return 0;
9371 /* Add MEM to the LOOP_MEMS array, if appropriate. See the
9372 documentation for LOOP_MEMS for the definition of `appropriate'.
9373 This function is called from prescan_loop via for_each_rtx. */
9375 static int
9376 insert_loop_mem (mem, data)
9377 rtx *mem;
9378 void *data ATTRIBUTE_UNUSED;
9380 struct loop_info *loop_info = data;
9381 int i;
9382 rtx m = *mem;
9384 if (m == NULL_RTX)
9385 return 0;
9387 switch (GET_CODE (m))
9389 case MEM:
9390 break;
9392 case CLOBBER:
9393 /* We're not interested in MEMs that are only clobbered. */
9394 return -1;
9396 case CONST_DOUBLE:
9397 /* We're not interested in the MEM associated with a
9398 CONST_DOUBLE, so there's no need to traverse into this. */
9399 return -1;
9401 case EXPR_LIST:
9402 /* We're not interested in any MEMs that only appear in notes. */
9403 return -1;
9405 default:
9406 /* This is not a MEM. */
9407 return 0;
9410 /* See if we've already seen this MEM. */
9411 for (i = 0; i < loop_info->mems_idx; ++i)
9412 if (rtx_equal_p (m, loop_info->mems[i].mem))
9414 if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
9415 /* The modes of the two memory accesses are different. If
9416 this happens, something tricky is going on, and we just
9417 don't optimize accesses to this MEM. */
9418 loop_info->mems[i].optimize = 0;
9420 return 0;
9423 /* Resize the array, if necessary. */
9424 if (loop_info->mems_idx == loop_info->mems_allocated)
9426 if (loop_info->mems_allocated != 0)
9427 loop_info->mems_allocated *= 2;
9428 else
9429 loop_info->mems_allocated = 32;
9431 loop_info->mems = (loop_mem_info *)
9432 xrealloc (loop_info->mems,
9433 loop_info->mems_allocated * sizeof (loop_mem_info));
9436 /* Actually insert the MEM. */
9437 loop_info->mems[loop_info->mems_idx].mem = m;
9438 /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9439 because we can't put it in a register. We still store it in the
9440 table, though, so that if we see the same address later, but in a
9441 non-BLK mode, we'll not think we can optimize it at that point. */
9442 loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
9443 loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
9444 ++loop_info->mems_idx;
9446 return 0;
9450 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9452 Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9453 register that is modified by an insn between FROM and TO. If the
9454 value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9455 more, stop incrementing it, to avoid overflow.
9457 Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9458 register I is used, if it is only used once. Otherwise, it is set
9459 to 0 (for no uses) or const0_rtx for more than one use. This
9460 parameter may be zero, in which case this processing is not done.
9462 Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9463 optimize register I. */
9465 static void
9466 loop_regs_scan (loop, extra_size)
9467 const struct loop *loop;
9468 int extra_size;
9470 struct loop_regs *regs = LOOP_REGS (loop);
9471 int old_nregs;
9472 /* last_set[n] is nonzero iff reg n has been set in the current
9473 basic block. In that case, it is the insn that last set reg n. */
9474 rtx *last_set;
9475 rtx insn;
9476 int i;
9478 old_nregs = regs->num;
9479 regs->num = max_reg_num ();
9481 /* Grow the regs array if not allocated or too small. */
9482 if (regs->num >= regs->size)
9484 regs->size = regs->num + extra_size;
9486 regs->array = (struct loop_reg *)
9487 xrealloc (regs->array, regs->size * sizeof (*regs->array));
9489 /* Zero the new elements. */
9490 memset (regs->array + old_nregs, 0,
9491 (regs->size - old_nregs) * sizeof (*regs->array));
9494 /* Clear previously scanned fields but do not clear n_times_set. */
9495 for (i = 0; i < old_nregs; i++)
9497 regs->array[i].set_in_loop = 0;
9498 regs->array[i].may_not_optimize = 0;
9499 regs->array[i].single_usage = NULL_RTX;
9502 last_set = (rtx *) xcalloc (regs->num, sizeof (rtx));
9504 /* Scan the loop, recording register usage. */
9505 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9506 insn = NEXT_INSN (insn))
9508 if (INSN_P (insn))
9510 /* Record registers that have exactly one use. */
9511 find_single_use_in_loop (regs, insn, PATTERN (insn));
9513 /* Include uses in REG_EQUAL notes. */
9514 if (REG_NOTES (insn))
9515 find_single_use_in_loop (regs, insn, REG_NOTES (insn));
9517 if (GET_CODE (PATTERN (insn)) == SET
9518 || GET_CODE (PATTERN (insn)) == CLOBBER)
9519 count_one_set (regs, insn, PATTERN (insn), last_set);
9520 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
9522 int i;
9523 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
9524 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
9525 last_set);
9529 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
9530 memset (last_set, 0, regs->num * sizeof (rtx));
9533 /* Invalidate all hard registers clobbered by calls. With one exception:
9534 a call-clobbered PIC register is still function-invariant for our
9535 purposes, since we can hoist any PIC calculations out of the loop.
9536 Thus the call to rtx_varies_p. */
9537 if (LOOP_INFO (loop)->has_call)
9538 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9539 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
9540 && rtx_varies_p (gen_rtx_REG (Pmode, i), /*for_alias=*/1))
9542 regs->array[i].may_not_optimize = 1;
9543 regs->array[i].set_in_loop = 1;
9546 #ifdef AVOID_CCMODE_COPIES
9547 /* Don't try to move insns which set CC registers if we should not
9548 create CCmode register copies. */
9549 for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9550 if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9551 regs->array[i].may_not_optimize = 1;
9552 #endif
9554 /* Set regs->array[I].n_times_set for the new registers. */
9555 for (i = old_nregs; i < regs->num; i++)
9556 regs->array[i].n_times_set = regs->array[i].set_in_loop;
9558 free (last_set);
9561 /* Returns the number of real INSNs in the LOOP. */
9563 static int
9564 count_insns_in_loop (loop)
9565 const struct loop *loop;
9567 int count = 0;
9568 rtx insn;
9570 for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9571 insn = NEXT_INSN (insn))
9572 if (INSN_P (insn))
9573 ++count;
9575 return count;
9578 /* Move MEMs into registers for the duration of the loop. */
9580 static void
9581 load_mems (loop)
9582 const struct loop *loop;
9584 struct loop_info *loop_info = LOOP_INFO (loop);
9585 struct loop_regs *regs = LOOP_REGS (loop);
9586 int maybe_never = 0;
9587 int i;
9588 rtx p, prev_ebb_head;
9589 rtx label = NULL_RTX;
9590 rtx end_label;
9591 /* Nonzero if the next instruction may never be executed. */
9592 int next_maybe_never = 0;
9593 unsigned int last_max_reg = max_reg_num ();
9595 if (loop_info->mems_idx == 0)
9596 return;
9598 /* We cannot use next_label here because it skips over normal insns. */
9599 end_label = next_nonnote_insn (loop->end);
9600 if (end_label && GET_CODE (end_label) != CODE_LABEL)
9601 end_label = NULL_RTX;
9603 /* Check to see if it's possible that some instructions in the loop are
9604 never executed. Also check if there is a goto out of the loop other
9605 than right after the end of the loop. */
9606 for (p = next_insn_in_loop (loop, loop->scan_start);
9607 p != NULL_RTX;
9608 p = next_insn_in_loop (loop, p))
9610 if (GET_CODE (p) == CODE_LABEL)
9611 maybe_never = 1;
9612 else if (GET_CODE (p) == JUMP_INSN
9613 /* If we enter the loop in the middle, and scan
9614 around to the beginning, don't set maybe_never
9615 for that. This must be an unconditional jump,
9616 otherwise the code at the top of the loop might
9617 never be executed. Unconditional jumps are
9618 followed a by barrier then loop end. */
9619 && ! (GET_CODE (p) == JUMP_INSN
9620 && JUMP_LABEL (p) == loop->top
9621 && NEXT_INSN (NEXT_INSN (p)) == loop->end
9622 && any_uncondjump_p (p)))
9624 /* If this is a jump outside of the loop but not right
9625 after the end of the loop, we would have to emit new fixup
9626 sequences for each such label. */
9627 if (/* If we can't tell where control might go when this
9628 JUMP_INSN is executed, we must be conservative. */
9629 !JUMP_LABEL (p)
9630 || (JUMP_LABEL (p) != end_label
9631 && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
9632 || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
9633 || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
9634 return;
9636 if (!any_condjump_p (p))
9637 /* Something complicated. */
9638 maybe_never = 1;
9639 else
9640 /* If there are any more instructions in the loop, they
9641 might not be reached. */
9642 next_maybe_never = 1;
9644 else if (next_maybe_never)
9645 maybe_never = 1;
9648 /* Find start of the extended basic block that enters the loop. */
9649 for (p = loop->start;
9650 PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
9651 p = PREV_INSN (p))
9653 prev_ebb_head = p;
9655 cselib_init ();
9657 /* Build table of mems that get set to constant values before the
9658 loop. */
9659 for (; p != loop->start; p = NEXT_INSN (p))
9660 cselib_process_insn (p);
9662 /* Actually move the MEMs. */
9663 for (i = 0; i < loop_info->mems_idx; ++i)
9665 regset_head load_copies;
9666 regset_head store_copies;
9667 int written = 0;
9668 rtx reg;
9669 rtx mem = loop_info->mems[i].mem;
9670 rtx mem_list_entry;
9672 if (MEM_VOLATILE_P (mem)
9673 || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
9674 /* There's no telling whether or not MEM is modified. */
9675 loop_info->mems[i].optimize = 0;
9677 /* Go through the MEMs written to in the loop to see if this
9678 one is aliased by one of them. */
9679 mem_list_entry = loop_info->store_mems;
9680 while (mem_list_entry)
9682 if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9683 written = 1;
9684 else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9685 mem, rtx_varies_p))
9687 /* MEM is indeed aliased by this store. */
9688 loop_info->mems[i].optimize = 0;
9689 break;
9691 mem_list_entry = XEXP (mem_list_entry, 1);
9694 if (flag_float_store && written
9695 && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
9696 loop_info->mems[i].optimize = 0;
9698 /* If this MEM is written to, we must be sure that there
9699 are no reads from another MEM that aliases this one. */
9700 if (loop_info->mems[i].optimize && written)
9702 int j;
9704 for (j = 0; j < loop_info->mems_idx; ++j)
9706 if (j == i)
9707 continue;
9708 else if (true_dependence (mem,
9709 VOIDmode,
9710 loop_info->mems[j].mem,
9711 rtx_varies_p))
9713 /* It's not safe to hoist loop_info->mems[i] out of
9714 the loop because writes to it might not be
9715 seen by reads from loop_info->mems[j]. */
9716 loop_info->mems[i].optimize = 0;
9717 break;
9722 if (maybe_never && may_trap_p (mem))
9723 /* We can't access the MEM outside the loop; it might
9724 cause a trap that wouldn't have happened otherwise. */
9725 loop_info->mems[i].optimize = 0;
9727 if (!loop_info->mems[i].optimize)
9728 /* We thought we were going to lift this MEM out of the
9729 loop, but later discovered that we could not. */
9730 continue;
9732 INIT_REG_SET (&load_copies);
9733 INIT_REG_SET (&store_copies);
9735 /* Allocate a pseudo for this MEM. We set REG_USERVAR_P in
9736 order to keep scan_loop from moving stores to this MEM
9737 out of the loop just because this REG is neither a
9738 user-variable nor used in the loop test. */
9739 reg = gen_reg_rtx (GET_MODE (mem));
9740 REG_USERVAR_P (reg) = 1;
9741 loop_info->mems[i].reg = reg;
9743 /* Now, replace all references to the MEM with the
9744 corresponding pseudos. */
9745 maybe_never = 0;
9746 for (p = next_insn_in_loop (loop, loop->scan_start);
9747 p != NULL_RTX;
9748 p = next_insn_in_loop (loop, p))
9750 if (INSN_P (p))
9752 rtx set;
9754 set = single_set (p);
9756 /* See if this copies the mem into a register that isn't
9757 modified afterwards. We'll try to do copy propagation
9758 a little further on. */
9759 if (set
9760 /* @@@ This test is _way_ too conservative. */
9761 && ! maybe_never
9762 && GET_CODE (SET_DEST (set)) == REG
9763 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
9764 && REGNO (SET_DEST (set)) < last_max_reg
9765 && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
9766 && rtx_equal_p (SET_SRC (set), mem))
9767 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
9769 /* See if this copies the mem from a register that isn't
9770 modified afterwards. We'll try to remove the
9771 redundant copy later on by doing a little register
9772 renaming and copy propagation. This will help
9773 to untangle things for the BIV detection code. */
9774 if (set
9775 && ! maybe_never
9776 && GET_CODE (SET_SRC (set)) == REG
9777 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
9778 && REGNO (SET_SRC (set)) < last_max_reg
9779 && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
9780 && rtx_equal_p (SET_DEST (set), mem))
9781 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
9783 /* Replace the memory reference with the shadow register. */
9784 replace_loop_mems (p, loop_info->mems[i].mem,
9785 loop_info->mems[i].reg);
9788 if (GET_CODE (p) == CODE_LABEL
9789 || GET_CODE (p) == JUMP_INSN)
9790 maybe_never = 1;
9793 if (! apply_change_group ())
9794 /* We couldn't replace all occurrences of the MEM. */
9795 loop_info->mems[i].optimize = 0;
9796 else
9798 /* Load the memory immediately before LOOP->START, which is
9799 the NOTE_LOOP_BEG. */
9800 cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9801 rtx set;
9802 rtx best = mem;
9803 int j;
9804 struct elt_loc_list *const_equiv = 0;
9806 if (e)
9808 struct elt_loc_list *equiv;
9809 struct elt_loc_list *best_equiv = 0;
9810 for (equiv = e->locs; equiv; equiv = equiv->next)
9812 if (CONSTANT_P (equiv->loc))
9813 const_equiv = equiv;
9814 else if (GET_CODE (equiv->loc) == REG
9815 /* Extending hard register lifetimes causes crash
9816 on SRC targets. Doing so on non-SRC is
9817 probably also not good idea, since we most
9818 probably have pseudoregister equivalence as
9819 well. */
9820 && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9821 best_equiv = equiv;
9823 /* Use the constant equivalence if that is cheap enough. */
9824 if (! best_equiv)
9825 best_equiv = const_equiv;
9826 else if (const_equiv
9827 && (rtx_cost (const_equiv->loc, SET)
9828 <= rtx_cost (best_equiv->loc, SET)))
9830 best_equiv = const_equiv;
9831 const_equiv = 0;
9834 /* If best_equiv is nonzero, we know that MEM is set to a
9835 constant or register before the loop. We will use this
9836 knowledge to initialize the shadow register with that
9837 constant or reg rather than by loading from MEM. */
9838 if (best_equiv)
9839 best = copy_rtx (best_equiv->loc);
9842 set = gen_move_insn (reg, best);
9843 set = loop_insn_hoist (loop, set);
9844 if (REG_P (best))
9846 for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
9847 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
9849 REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
9850 break;
9854 if (const_equiv)
9855 set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
9857 if (written)
9859 if (label == NULL_RTX)
9861 label = gen_label_rtx ();
9862 emit_label_after (label, loop->end);
9865 /* Store the memory immediately after END, which is
9866 the NOTE_LOOP_END. */
9867 set = gen_move_insn (copy_rtx (mem), reg);
9868 loop_insn_emit_after (loop, 0, label, set);
9871 if (loop_dump_stream)
9873 fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
9874 REGNO (reg), (written ? "r/w" : "r/o"));
9875 print_rtl (loop_dump_stream, mem);
9876 fputc ('\n', loop_dump_stream);
9879 /* Attempt a bit of copy propagation. This helps untangle the
9880 data flow, and enables {basic,general}_induction_var to find
9881 more bivs/givs. */
9882 EXECUTE_IF_SET_IN_REG_SET
9883 (&load_copies, FIRST_PSEUDO_REGISTER, j,
9885 try_copy_prop (loop, reg, j);
9887 CLEAR_REG_SET (&load_copies);
9889 EXECUTE_IF_SET_IN_REG_SET
9890 (&store_copies, FIRST_PSEUDO_REGISTER, j,
9892 try_swap_copy_prop (loop, reg, j);
9894 CLEAR_REG_SET (&store_copies);
9898 if (label != NULL_RTX && end_label != NULL_RTX)
9900 /* Now, we need to replace all references to the previous exit
9901 label with the new one. */
9902 rtx_pair rr;
9903 rr.r1 = end_label;
9904 rr.r2 = label;
9906 for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
9908 for_each_rtx (&p, replace_label, &rr);
9910 /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
9911 field. This is not handled by for_each_rtx because it doesn't
9912 handle unprinted ('0') fields. We need to update JUMP_LABEL
9913 because the immediately following unroll pass will use it.
9914 replace_label would not work anyways, because that only handles
9915 LABEL_REFs. */
9916 if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
9917 JUMP_LABEL (p) = label;
9921 cselib_finish ();
9924 /* For communication between note_reg_stored and its caller. */
9925 struct note_reg_stored_arg
9927 int set_seen;
9928 rtx reg;
9931 /* Called via note_stores, record in SET_SEEN whether X, which is written,
9932 is equal to ARG. */
9933 static void
9934 note_reg_stored (x, setter, arg)
9935 rtx x, setter ATTRIBUTE_UNUSED;
9936 void *arg;
9938 struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
9939 if (t->reg == x)
9940 t->set_seen = 1;
9943 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
9944 There must be exactly one insn that sets this pseudo; it will be
9945 deleted if all replacements succeed and we can prove that the register
9946 is not used after the loop. */
9948 static void
9949 try_copy_prop (loop, replacement, regno)
9950 const struct loop *loop;
9951 rtx replacement;
9952 unsigned int regno;
9954 /* This is the reg that we are copying from. */
9955 rtx reg_rtx = regno_reg_rtx[regno];
9956 rtx init_insn = 0;
9957 rtx insn;
9958 /* These help keep track of whether we replaced all uses of the reg. */
9959 int replaced_last = 0;
9960 int store_is_first = 0;
9962 for (insn = next_insn_in_loop (loop, loop->scan_start);
9963 insn != NULL_RTX;
9964 insn = next_insn_in_loop (loop, insn))
9966 rtx set;
9968 /* Only substitute within one extended basic block from the initializing
9969 insn. */
9970 if (GET_CODE (insn) == CODE_LABEL && init_insn)
9971 break;
9973 if (! INSN_P (insn))
9974 continue;
9976 /* Is this the initializing insn? */
9977 set = single_set (insn);
9978 if (set
9979 && GET_CODE (SET_DEST (set)) == REG
9980 && REGNO (SET_DEST (set)) == regno)
9982 if (init_insn)
9983 abort ();
9985 init_insn = insn;
9986 if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
9987 store_is_first = 1;
9990 /* Only substitute after seeing the initializing insn. */
9991 if (init_insn && insn != init_insn)
9993 struct note_reg_stored_arg arg;
9995 replace_loop_regs (insn, reg_rtx, replacement);
9996 if (REGNO_LAST_UID (regno) == INSN_UID (insn))
9997 replaced_last = 1;
9999 /* Stop replacing when REPLACEMENT is modified. */
10000 arg.reg = replacement;
10001 arg.set_seen = 0;
10002 note_stores (PATTERN (insn), note_reg_stored, &arg);
10003 if (arg.set_seen)
10005 rtx note = find_reg_note (insn, REG_EQUAL, NULL);
10007 /* It is possible that we've turned previously valid REG_EQUAL to
10008 invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10009 REPLACEMENT is modified, we get different meaning. */
10010 if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
10011 remove_note (insn, note);
10012 break;
10016 if (! init_insn)
10017 abort ();
10018 if (apply_change_group ())
10020 if (loop_dump_stream)
10021 fprintf (loop_dump_stream, " Replaced reg %d", regno);
10022 if (store_is_first && replaced_last)
10024 rtx first;
10025 rtx retval_note;
10027 /* Assume we're just deleting INIT_INSN. */
10028 first = init_insn;
10029 /* Look for REG_RETVAL note. If we're deleting the end of
10030 the libcall sequence, the whole sequence can go. */
10031 retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
10032 /* If we found a REG_RETVAL note, find the first instruction
10033 in the sequence. */
10034 if (retval_note)
10035 first = XEXP (retval_note, 0);
10037 /* Delete the instructions. */
10038 loop_delete_insns (first, init_insn);
10040 if (loop_dump_stream)
10041 fprintf (loop_dump_stream, ".\n");
10045 /* Replace all the instructions from FIRST up to and including LAST
10046 with NOTE_INSN_DELETED notes. */
10048 static void
10049 loop_delete_insns (first, last)
10050 rtx first;
10051 rtx last;
10053 while (1)
10055 if (loop_dump_stream)
10056 fprintf (loop_dump_stream, ", deleting init_insn (%d)",
10057 INSN_UID (first));
10058 delete_insn (first);
10060 /* If this was the LAST instructions we're supposed to delete,
10061 we're done. */
10062 if (first == last)
10063 break;
10065 first = NEXT_INSN (first);
10069 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10070 loop LOOP if the order of the sets of these registers can be
10071 swapped. There must be exactly one insn within the loop that sets
10072 this pseudo followed immediately by a move insn that sets
10073 REPLACEMENT with REGNO. */
10074 static void
10075 try_swap_copy_prop (loop, replacement, regno)
10076 const struct loop *loop;
10077 rtx replacement;
10078 unsigned int regno;
10080 rtx insn;
10081 rtx set = NULL_RTX;
10082 unsigned int new_regno;
10084 new_regno = REGNO (replacement);
10086 for (insn = next_insn_in_loop (loop, loop->scan_start);
10087 insn != NULL_RTX;
10088 insn = next_insn_in_loop (loop, insn))
10090 /* Search for the insn that copies REGNO to NEW_REGNO? */
10091 if (INSN_P (insn)
10092 && (set = single_set (insn))
10093 && GET_CODE (SET_DEST (set)) == REG
10094 && REGNO (SET_DEST (set)) == new_regno
10095 && GET_CODE (SET_SRC (set)) == REG
10096 && REGNO (SET_SRC (set)) == regno)
10097 break;
10100 if (insn != NULL_RTX)
10102 rtx prev_insn;
10103 rtx prev_set;
10105 /* Some DEF-USE info would come in handy here to make this
10106 function more general. For now, just check the previous insn
10107 which is the most likely candidate for setting REGNO. */
10109 prev_insn = PREV_INSN (insn);
10111 if (INSN_P (insn)
10112 && (prev_set = single_set (prev_insn))
10113 && GET_CODE (SET_DEST (prev_set)) == REG
10114 && REGNO (SET_DEST (prev_set)) == regno)
10116 /* We have:
10117 (set (reg regno) (expr))
10118 (set (reg new_regno) (reg regno))
10120 so try converting this to:
10121 (set (reg new_regno) (expr))
10122 (set (reg regno) (reg new_regno))
10124 The former construct is often generated when a global
10125 variable used for an induction variable is shadowed by a
10126 register (NEW_REGNO). The latter construct improves the
10127 chances of GIV replacement and BIV elimination. */
10129 validate_change (prev_insn, &SET_DEST (prev_set),
10130 replacement, 1);
10131 validate_change (insn, &SET_DEST (set),
10132 SET_SRC (set), 1);
10133 validate_change (insn, &SET_SRC (set),
10134 replacement, 1);
10136 if (apply_change_group ())
10138 if (loop_dump_stream)
10139 fprintf (loop_dump_stream,
10140 " Swapped set of reg %d at %d with reg %d at %d.\n",
10141 regno, INSN_UID (insn),
10142 new_regno, INSN_UID (prev_insn));
10144 /* Update first use of REGNO. */
10145 if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
10146 REGNO_FIRST_UID (regno) = INSN_UID (insn);
10148 /* Now perform copy propagation to hopefully
10149 remove all uses of REGNO within the loop. */
10150 try_copy_prop (loop, replacement, regno);
10156 /* Replace MEM with its associated pseudo register. This function is
10157 called from load_mems via for_each_rtx. DATA is actually a pointer
10158 to a structure describing the instruction currently being scanned
10159 and the MEM we are currently replacing. */
10161 static int
10162 replace_loop_mem (mem, data)
10163 rtx *mem;
10164 void *data;
10166 loop_replace_args *args = (loop_replace_args *) data;
10167 rtx m = *mem;
10169 if (m == NULL_RTX)
10170 return 0;
10172 switch (GET_CODE (m))
10174 case MEM:
10175 break;
10177 case CONST_DOUBLE:
10178 /* We're not interested in the MEM associated with a
10179 CONST_DOUBLE, so there's no need to traverse into one. */
10180 return -1;
10182 default:
10183 /* This is not a MEM. */
10184 return 0;
10187 if (!rtx_equal_p (args->match, m))
10188 /* This is not the MEM we are currently replacing. */
10189 return 0;
10191 /* Actually replace the MEM. */
10192 validate_change (args->insn, mem, args->replacement, 1);
10194 return 0;
10197 static void
10198 replace_loop_mems (insn, mem, reg)
10199 rtx insn;
10200 rtx mem;
10201 rtx reg;
10203 loop_replace_args args;
10205 args.insn = insn;
10206 args.match = mem;
10207 args.replacement = reg;
10209 for_each_rtx (&insn, replace_loop_mem, &args);
10212 /* Replace one register with another. Called through for_each_rtx; PX points
10213 to the rtx being scanned. DATA is actually a pointer to
10214 a structure of arguments. */
10216 static int
10217 replace_loop_reg (px, data)
10218 rtx *px;
10219 void *data;
10221 rtx x = *px;
10222 loop_replace_args *args = (loop_replace_args *) data;
10224 if (x == NULL_RTX)
10225 return 0;
10227 if (x == args->match)
10228 validate_change (args->insn, px, args->replacement, 1);
10230 return 0;
10233 static void
10234 replace_loop_regs (insn, reg, replacement)
10235 rtx insn;
10236 rtx reg;
10237 rtx replacement;
10239 loop_replace_args args;
10241 args.insn = insn;
10242 args.match = reg;
10243 args.replacement = replacement;
10245 for_each_rtx (&insn, replace_loop_reg, &args);
10248 /* Replace occurrences of the old exit label for the loop with the new
10249 one. DATA is an rtx_pair containing the old and new labels,
10250 respectively. */
10252 static int
10253 replace_label (x, data)
10254 rtx *x;
10255 void *data;
10257 rtx l = *x;
10258 rtx old_label = ((rtx_pair *) data)->r1;
10259 rtx new_label = ((rtx_pair *) data)->r2;
10261 if (l == NULL_RTX)
10262 return 0;
10264 if (GET_CODE (l) != LABEL_REF)
10265 return 0;
10267 if (XEXP (l, 0) != old_label)
10268 return 0;
10270 XEXP (l, 0) = new_label;
10271 ++LABEL_NUSES (new_label);
10272 --LABEL_NUSES (old_label);
10274 return 0;
10277 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10278 (ignored in the interim). */
10280 static rtx
10281 loop_insn_emit_after (loop, where_bb, where_insn, pattern)
10282 const struct loop *loop ATTRIBUTE_UNUSED;
10283 basic_block where_bb ATTRIBUTE_UNUSED;
10284 rtx where_insn;
10285 rtx pattern;
10287 return emit_insn_after (pattern, where_insn);
10291 /* If WHERE_INSN is non-zero emit insn for PATTERN before WHERE_INSN
10292 in basic block WHERE_BB (ignored in the interim) within the loop
10293 otherwise hoist PATTERN into the loop pre-header. */
10296 loop_insn_emit_before (loop, where_bb, where_insn, pattern)
10297 const struct loop *loop;
10298 basic_block where_bb ATTRIBUTE_UNUSED;
10299 rtx where_insn;
10300 rtx pattern;
10302 if (! where_insn)
10303 return loop_insn_hoist (loop, pattern);
10304 return emit_insn_before (pattern, where_insn);
10308 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10309 WHERE_BB (ignored in the interim) within the loop. */
10311 static rtx
10312 loop_call_insn_emit_before (loop, where_bb, where_insn, pattern)
10313 const struct loop *loop ATTRIBUTE_UNUSED;
10314 basic_block where_bb ATTRIBUTE_UNUSED;
10315 rtx where_insn;
10316 rtx pattern;
10318 return emit_call_insn_before (pattern, where_insn);
10322 /* Hoist insn for PATTERN into the loop pre-header. */
10325 loop_insn_hoist (loop, pattern)
10326 const struct loop *loop;
10327 rtx pattern;
10329 return loop_insn_emit_before (loop, 0, loop->start, pattern);
10333 /* Hoist call insn for PATTERN into the loop pre-header. */
10335 static rtx
10336 loop_call_insn_hoist (loop, pattern)
10337 const struct loop *loop;
10338 rtx pattern;
10340 return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
10344 /* Sink insn for PATTERN after the loop end. */
10347 loop_insn_sink (loop, pattern)
10348 const struct loop *loop;
10349 rtx pattern;
10351 return loop_insn_emit_before (loop, 0, loop->sink, pattern);
10355 /* If the loop has multiple exits, emit insn for PATTERN before the
10356 loop to ensure that it will always be executed no matter how the
10357 loop exits. Otherwise, emit the insn for PATTERN after the loop,
10358 since this is slightly more efficient. */
10360 static rtx
10361 loop_insn_sink_or_swim (loop, pattern)
10362 const struct loop *loop;
10363 rtx pattern;
10365 if (loop->exit_count)
10366 return loop_insn_hoist (loop, pattern);
10367 else
10368 return loop_insn_sink (loop, pattern);
10371 static void
10372 loop_ivs_dump (loop, file, verbose)
10373 const struct loop *loop;
10374 FILE *file;
10375 int verbose;
10377 struct iv_class *bl;
10378 int iv_num = 0;
10380 if (! loop || ! file)
10381 return;
10383 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10384 iv_num++;
10386 fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
10388 for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10390 loop_iv_class_dump (bl, file, verbose);
10391 fputc ('\n', file);
10396 static void
10397 loop_iv_class_dump (bl, file, verbose)
10398 const struct iv_class *bl;
10399 FILE *file;
10400 int verbose ATTRIBUTE_UNUSED;
10402 struct induction *v;
10403 rtx incr;
10404 int i;
10406 if (! bl || ! file)
10407 return;
10409 fprintf (file, "IV class for reg %d, benefit %d\n",
10410 bl->regno, bl->total_benefit);
10412 fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
10413 if (bl->initial_value)
10415 fprintf (file, ", init val: ");
10416 print_simple_rtl (file, bl->initial_value);
10418 if (bl->initial_test)
10420 fprintf (file, ", init test: ");
10421 print_simple_rtl (file, bl->initial_test);
10423 fputc ('\n', file);
10425 if (bl->final_value)
10427 fprintf (file, " Final val: ");
10428 print_simple_rtl (file, bl->final_value);
10429 fputc ('\n', file);
10432 if ((incr = biv_total_increment (bl)))
10434 fprintf (file, " Total increment: ");
10435 print_simple_rtl (file, incr);
10436 fputc ('\n', file);
10439 /* List the increments. */
10440 for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
10442 fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
10443 print_simple_rtl (file, v->add_val);
10444 fputc ('\n', file);
10447 /* List the givs. */
10448 for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
10450 fprintf (file, " Giv%d: insn %d, benefit %d, ",
10451 i, INSN_UID (v->insn), v->benefit);
10452 if (v->giv_type == DEST_ADDR)
10453 print_simple_rtl (file, v->mem);
10454 else
10455 print_simple_rtl (file, single_set (v->insn));
10456 fputc ('\n', file);
10461 static void
10462 loop_biv_dump (v, file, verbose)
10463 const struct induction *v;
10464 FILE *file;
10465 int verbose;
10467 if (! v || ! file)
10468 return;
10470 fprintf (file,
10471 "Biv %d: insn %d",
10472 REGNO (v->dest_reg), INSN_UID (v->insn));
10473 fprintf (file, " const ");
10474 print_simple_rtl (file, v->add_val);
10476 if (verbose && v->final_value)
10478 fputc ('\n', file);
10479 fprintf (file, " final ");
10480 print_simple_rtl (file, v->final_value);
10483 fputc ('\n', file);
10487 static void
10488 loop_giv_dump (v, file, verbose)
10489 const struct induction *v;
10490 FILE *file;
10491 int verbose;
10493 if (! v || ! file)
10494 return;
10496 if (v->giv_type == DEST_REG)
10497 fprintf (file, "Giv %d: insn %d",
10498 REGNO (v->dest_reg), INSN_UID (v->insn));
10499 else
10500 fprintf (file, "Dest address: insn %d",
10501 INSN_UID (v->insn));
10503 fprintf (file, " src reg %d benefit %d",
10504 REGNO (v->src_reg), v->benefit);
10505 fprintf (file, " lifetime %d",
10506 v->lifetime);
10508 if (v->replaceable)
10509 fprintf (file, " replaceable");
10511 if (v->no_const_addval)
10512 fprintf (file, " ncav");
10514 if (v->ext_dependent)
10516 switch (GET_CODE (v->ext_dependent))
10518 case SIGN_EXTEND:
10519 fprintf (file, " ext se");
10520 break;
10521 case ZERO_EXTEND:
10522 fprintf (file, " ext ze");
10523 break;
10524 case TRUNCATE:
10525 fprintf (file, " ext tr");
10526 break;
10527 default:
10528 abort ();
10532 fputc ('\n', file);
10533 fprintf (file, " mult ");
10534 print_simple_rtl (file, v->mult_val);
10536 fputc ('\n', file);
10537 fprintf (file, " add ");
10538 print_simple_rtl (file, v->add_val);
10540 if (verbose && v->final_value)
10542 fputc ('\n', file);
10543 fprintf (file, " final ");
10544 print_simple_rtl (file, v->final_value);
10547 fputc ('\n', file);
10551 void
10552 debug_ivs (loop)
10553 const struct loop *loop;
10555 loop_ivs_dump (loop, stderr, 1);
10559 void
10560 debug_iv_class (bl)
10561 const struct iv_class *bl;
10563 loop_iv_class_dump (bl, stderr, 1);
10567 void
10568 debug_biv (v)
10569 const struct induction *v;
10571 loop_biv_dump (v, stderr, 1);
10575 void
10576 debug_giv (v)
10577 const struct induction *v;
10579 loop_giv_dump (v, stderr, 1);
10583 #define LOOP_BLOCK_NUM_1(INSN) \
10584 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10586 /* The notes do not have an assigned block, so look at the next insn. */
10587 #define LOOP_BLOCK_NUM(INSN) \
10588 ((INSN) ? (GET_CODE (INSN) == NOTE \
10589 ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10590 : LOOP_BLOCK_NUM_1 (INSN)) \
10591 : -1)
10593 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10595 static void
10596 loop_dump_aux (loop, file, verbose)
10597 const struct loop *loop;
10598 FILE *file;
10599 int verbose ATTRIBUTE_UNUSED;
10601 rtx label;
10603 if (! loop || ! file)
10604 return;
10606 /* Print diagnostics to compare our concept of a loop with
10607 what the loop notes say. */
10608 if (! PREV_INSN (loop->first->head)
10609 || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
10610 || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
10611 != NOTE_INSN_LOOP_BEG)
10612 fprintf (file, ";; No NOTE_INSN_LOOP_BEG at %d\n",
10613 INSN_UID (PREV_INSN (loop->first->head)));
10614 if (! NEXT_INSN (loop->last->end)
10615 || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
10616 || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
10617 != NOTE_INSN_LOOP_END)
10618 fprintf (file, ";; No NOTE_INSN_LOOP_END at %d\n",
10619 INSN_UID (NEXT_INSN (loop->last->end)));
10621 if (loop->start)
10623 fprintf (file,
10624 ";; start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10625 LOOP_BLOCK_NUM (loop->start),
10626 LOOP_INSN_UID (loop->start),
10627 LOOP_BLOCK_NUM (loop->cont),
10628 LOOP_INSN_UID (loop->cont),
10629 LOOP_BLOCK_NUM (loop->cont),
10630 LOOP_INSN_UID (loop->cont),
10631 LOOP_BLOCK_NUM (loop->vtop),
10632 LOOP_INSN_UID (loop->vtop),
10633 LOOP_BLOCK_NUM (loop->end),
10634 LOOP_INSN_UID (loop->end));
10635 fprintf (file, ";; top %d (%d), scan start %d (%d)\n",
10636 LOOP_BLOCK_NUM (loop->top),
10637 LOOP_INSN_UID (loop->top),
10638 LOOP_BLOCK_NUM (loop->scan_start),
10639 LOOP_INSN_UID (loop->scan_start));
10640 fprintf (file, ";; exit_count %d", loop->exit_count);
10641 if (loop->exit_count)
10643 fputs (", labels:", file);
10644 for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
10646 fprintf (file, " %d ",
10647 LOOP_INSN_UID (XEXP (label, 0)));
10650 fputs ("\n", file);
10652 /* This can happen when a marked loop appears as two nested loops,
10653 say from while (a || b) {}. The inner loop won't match
10654 the loop markers but the outer one will. */
10655 if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
10656 fprintf (file, ";; NOTE_INSN_LOOP_CONT not in loop latch\n");
10660 /* Call this function from the debugger to dump LOOP. */
10662 void
10663 debug_loop (loop)
10664 const struct loop *loop;
10666 flow_loop_dump (loop, stderr, loop_dump_aux, 1);
10669 /* Call this function from the debugger to dump LOOPS. */
10671 void
10672 debug_loops (loops)
10673 const struct loops *loops;
10675 flow_loops_dump (loops, stderr, loop_dump_aux, 1);