OSDN Git Service

* Makefile.in (INSTALL_CPP, UNINSTALL_CPP): Remove.
[pf3gnuchains/gcc-fork.git] / gcc / loop.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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.
28
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.
33
34    Most of the complexity is in heuristics to decide when it is worth
35    while to do these things.  */
36
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"
58
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
71
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
85
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.  */
89
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
94
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
100
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
105
106 /* Define what we mean by a "low" iteration count.  */
107 #ifndef PREFETCH_LOW_LOOPCNT
108 #define PREFETCH_LOW_LOOPCNT 32
109 #endif
110
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
116
117 /* Do not prefetch accesses with an extreme stride.  */
118 #ifndef PREFETCH_NO_EXTREME_STRIDE
119 #define PREFETCH_NO_EXTREME_STRIDE 1
120 #endif
121
122 /* Define what we mean by an "extreme" stride.  */
123 #ifndef PREFETCH_EXTREME_STRIDE
124 #define PREFETCH_EXTREME_STRIDE 4096
125 #endif
126
127 /* Define a limit to how far apart indices can be and still be merged
128    into a single prefetch.  */
129 #ifndef PREFETCH_EXTREME_DIFFERENCE
130 #define PREFETCH_EXTREME_DIFFERENCE 4096
131 #endif
132
133 /* Issue prefetch instructions before the loop to fetch data to be used
134    in the first few loop iterations.  */
135 #ifndef PREFETCH_BEFORE_LOOP
136 #define PREFETCH_BEFORE_LOOP 1
137 #endif
138
139 /* Do not handle reversed order prefetches (negative stride).  */
140 #ifndef PREFETCH_NO_REVERSE_ORDER
141 #define PREFETCH_NO_REVERSE_ORDER 1
142 #endif
143
144 /* Prefetch even if the GIV is in conditional code.  */
145 #ifndef PREFETCH_CONDITIONAL
146 #define PREFETCH_CONDITIONAL 1
147 #endif
148
149 /* If the loop requires more prefetches than the target can process in
150    parallel then don't prefetch anything in that loop.  */
151 #ifndef PREFETCH_LIMIT_TO_SIMULTANEOUS
152 #define PREFETCH_LIMIT_TO_SIMULTANEOUS 1
153 #endif
154
155 #define LOOP_REG_LIFETIME(LOOP, REGNO) \
156 ((REGNO_LAST_LUID (REGNO) - REGNO_FIRST_LUID (REGNO)))
157
158 #define LOOP_REG_GLOBAL_P(LOOP, REGNO) \
159 ((REGNO_LAST_LUID (REGNO) > INSN_LUID ((LOOP)->end) \
160  || REGNO_FIRST_LUID (REGNO) < INSN_LUID ((LOOP)->start)))
161
162 #define LOOP_REGNO_NREGS(REGNO, SET_DEST) \
163 ((REGNO) < FIRST_PSEUDO_REGISTER \
164  ? HARD_REGNO_NREGS ((REGNO), GET_MODE (SET_DEST)) : 1)
165
166
167 /* Vector mapping INSN_UIDs to luids.
168    The luids are like uids but increase monotonically always.
169    We use them to see whether a jump comes from outside a given loop.  */
170
171 int *uid_luid;
172
173 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
174    number the insn is contained in.  */
175
176 struct loop **uid_loop;
177
178 /* 1 + largest uid of any insn.  */
179
180 int max_uid_for_loop;
181
182 /* 1 + luid of last insn.  */
183
184 static int max_luid;
185
186 /* Number of loops detected in current function.  Used as index to the
187    next few tables.  */
188
189 static int max_loop_num;
190
191 /* Bound on pseudo register number before loop optimization.
192    A pseudo has valid regscan info if its number is < max_reg_before_loop.  */
193 unsigned int max_reg_before_loop;
194
195 /* The value to pass to the next call of reg_scan_update.  */
196 static int loop_max_reg;
197
198 #define obstack_chunk_alloc xmalloc
199 #define obstack_chunk_free free
200 \f
201 /* During the analysis of a loop, a chain of `struct movable's
202    is made to record all the movable insns found.
203    Then the entire chain can be scanned to decide which to move.  */
204
205 struct movable
206 {
207   rtx insn;                     /* A movable insn */
208   rtx set_src;                  /* The expression this reg is set from.  */
209   rtx set_dest;                 /* The destination of this SET.  */
210   rtx dependencies;             /* When INSN is libcall, this is an EXPR_LIST
211                                    of any registers used within the LIBCALL.  */
212   int consec;                   /* Number of consecutive following insns
213                                    that must be moved with this one.  */
214   unsigned int regno;           /* The register it sets */
215   short lifetime;               /* lifetime of that register;
216                                    may be adjusted when matching movables
217                                    that load the same value are found.  */
218   short savings;                /* Number of insns we can move for this reg,
219                                    including other movables that force this
220                                    or match this one.  */
221   unsigned int cond : 1;        /* 1 if only conditionally movable */
222   unsigned int force : 1;       /* 1 means MUST move this insn */
223   unsigned int global : 1;      /* 1 means reg is live outside this loop */
224                 /* If PARTIAL is 1, GLOBAL means something different:
225                    that the reg is live outside the range from where it is set
226                    to the following label.  */
227   unsigned int done : 1;        /* 1 inhibits further processing of this */
228
229   unsigned int partial : 1;     /* 1 means this reg is used for zero-extending.
230                                    In particular, moving it does not make it
231                                    invariant.  */
232   unsigned int move_insn : 1;   /* 1 means that we call emit_move_insn to
233                                    load SRC, rather than copying INSN.  */
234   unsigned int move_insn_first:1;/* Same as above, if this is necessary for the
235                                     first insn of a consecutive sets group.  */
236   unsigned int is_equiv : 1;    /* 1 means a REG_EQUIV is present on INSN.  */
237   enum machine_mode savemode;   /* Nonzero means it is a mode for a low part
238                                    that we should avoid changing when clearing
239                                    the rest of the reg.  */
240   struct movable *match;        /* First entry for same value */
241   struct movable *forces;       /* An insn that must be moved if this is */
242   struct movable *next;
243 };
244
245
246 FILE *loop_dump_stream;
247
248 /* Forward declarations.  */
249
250 static void invalidate_loops_containing_label PARAMS ((rtx));
251 static void find_and_verify_loops PARAMS ((rtx, struct loops *));
252 static void mark_loop_jump PARAMS ((rtx, struct loop *));
253 static void prescan_loop PARAMS ((struct loop *));
254 static int reg_in_basic_block_p PARAMS ((rtx, rtx));
255 static int consec_sets_invariant_p PARAMS ((const struct loop *,
256                                             rtx, int, rtx));
257 static int labels_in_range_p PARAMS ((rtx, int));
258 static void count_one_set PARAMS ((struct loop_regs *, rtx, rtx, rtx *));
259 static void note_addr_stored PARAMS ((rtx, rtx, void *));
260 static void note_set_pseudo_multiple_uses PARAMS ((rtx, rtx, void *));
261 static int loop_reg_used_before_p PARAMS ((const struct loop *, rtx, rtx));
262 static void scan_loop PARAMS ((struct loop*, int));
263 #if 0
264 static void replace_call_address PARAMS ((rtx, rtx, rtx));
265 #endif
266 static rtx skip_consec_insns PARAMS ((rtx, int));
267 static int libcall_benefit PARAMS ((rtx));
268 static void ignore_some_movables PARAMS ((struct loop_movables *));
269 static void force_movables PARAMS ((struct loop_movables *));
270 static void combine_movables PARAMS ((struct loop_movables *,
271                                       struct loop_regs *));
272 static int num_unmoved_movables PARAMS ((const struct loop *));
273 static int regs_match_p PARAMS ((rtx, rtx, struct loop_movables *));
274 static int rtx_equal_for_loop_p PARAMS ((rtx, rtx, struct loop_movables *,
275                                          struct loop_regs *));
276 static void add_label_notes PARAMS ((rtx, rtx));
277 static void move_movables PARAMS ((struct loop *loop, struct loop_movables *,
278                                    int, int));
279 static void loop_movables_add PARAMS((struct loop_movables *,
280                                       struct movable *));
281 static void loop_movables_free PARAMS((struct loop_movables *));
282 static int count_nonfixed_reads PARAMS ((const struct loop *, rtx));
283 static void loop_bivs_find PARAMS((struct loop *));
284 static void loop_bivs_init_find PARAMS((struct loop *));
285 static void loop_bivs_check PARAMS((struct loop *));
286 static void loop_givs_find PARAMS((struct loop *));
287 static void loop_givs_check PARAMS((struct loop *));
288 static int loop_biv_eliminable_p PARAMS((struct loop *, struct iv_class *,
289                                          int, int));
290 static int loop_giv_reduce_benefit PARAMS((struct loop *, struct iv_class *,
291                                            struct induction *, rtx));
292 static void loop_givs_dead_check PARAMS((struct loop *, struct iv_class *));
293 static void loop_givs_reduce PARAMS((struct loop *, struct iv_class *));
294 static void loop_givs_rescan PARAMS((struct loop *, struct iv_class *,
295                                      rtx *));
296 static void loop_ivs_free PARAMS((struct loop *));
297 static void strength_reduce PARAMS ((struct loop *, int));
298 static void find_single_use_in_loop PARAMS ((struct loop_regs *, rtx, rtx));
299 static int valid_initial_value_p PARAMS ((rtx, rtx, int, rtx));
300 static void find_mem_givs PARAMS ((const struct loop *, rtx, rtx, int, int));
301 static void record_biv PARAMS ((struct loop *, struct induction *,
302                                 rtx, rtx, rtx, rtx, rtx *,
303                                 int, int));
304 static void check_final_value PARAMS ((const struct loop *,
305                                        struct induction *));
306 static void loop_ivs_dump PARAMS((const struct loop *, FILE *, int));
307 static void loop_iv_class_dump PARAMS((const struct iv_class *, FILE *, int));
308 static void loop_biv_dump PARAMS((const struct induction *, FILE *, int));
309 static void loop_giv_dump PARAMS((const struct induction *, FILE *, int));
310 static void record_giv PARAMS ((const struct loop *, struct induction *,
311                                 rtx, rtx, rtx, rtx, rtx, rtx, int,
312                                 enum g_types, int, int, rtx *));
313 static void update_giv_derive PARAMS ((const struct loop *, rtx));
314 static void check_ext_dependent_givs PARAMS ((struct iv_class *,
315                                               struct loop_info *));
316 static int basic_induction_var PARAMS ((const struct loop *, rtx,
317                                         enum machine_mode, rtx, rtx,
318                                         rtx *, rtx *, rtx **));
319 static rtx simplify_giv_expr PARAMS ((const struct loop *, rtx, rtx *, int *));
320 static int general_induction_var PARAMS ((const struct loop *loop, rtx, rtx *,
321                                           rtx *, rtx *, rtx *, int, int *,
322                                           enum machine_mode));
323 static int consec_sets_giv PARAMS ((const struct loop *, int, rtx,
324                                     rtx, rtx, rtx *, rtx *, rtx *, rtx *));
325 static int check_dbra_loop PARAMS ((struct loop *, int));
326 static rtx express_from_1 PARAMS ((rtx, rtx, rtx));
327 static rtx combine_givs_p PARAMS ((struct induction *, struct induction *));
328 static int cmp_combine_givs_stats PARAMS ((const PTR, const PTR));
329 static void combine_givs PARAMS ((struct loop_regs *, struct iv_class *));
330 static int product_cheap_p PARAMS ((rtx, rtx));
331 static int maybe_eliminate_biv PARAMS ((const struct loop *, struct iv_class *,
332                                         int, int, int));
333 static int maybe_eliminate_biv_1 PARAMS ((const struct loop *, rtx, rtx,
334                                           struct iv_class *, int,
335                                           basic_block, rtx));
336 static int last_use_this_basic_block PARAMS ((rtx, rtx));
337 static void record_initial PARAMS ((rtx, rtx, void *));
338 static void update_reg_last_use PARAMS ((rtx, rtx));
339 static rtx next_insn_in_loop PARAMS ((const struct loop *, rtx));
340 static void loop_regs_scan PARAMS ((const struct loop *, int));
341 static int count_insns_in_loop PARAMS ((const struct loop *));
342 static void load_mems PARAMS ((const struct loop *));
343 static int insert_loop_mem PARAMS ((rtx *, void *));
344 static int replace_loop_mem PARAMS ((rtx *, void *));
345 static void replace_loop_mems PARAMS ((rtx, rtx, rtx));
346 static int replace_loop_reg PARAMS ((rtx *, void *));
347 static void replace_loop_regs PARAMS ((rtx insn, rtx, rtx));
348 static void note_reg_stored PARAMS ((rtx, rtx, void *));
349 static void try_copy_prop PARAMS ((const struct loop *, rtx, unsigned int));
350 static void try_swap_copy_prop PARAMS ((const struct loop *, rtx,
351                                          unsigned int));
352 static int replace_label PARAMS ((rtx *, void *));
353 static rtx check_insn_for_givs PARAMS((struct loop *, rtx, int, int));
354 static rtx check_insn_for_bivs PARAMS((struct loop *, rtx, int, int));
355 static rtx gen_add_mult PARAMS ((rtx, rtx, rtx, rtx));
356 static void loop_regs_update PARAMS ((const struct loop *, rtx));
357 static int iv_add_mult_cost PARAMS ((rtx, rtx, rtx, rtx));
358
359 static rtx loop_insn_emit_after PARAMS((const struct loop *, basic_block,
360                                         rtx, rtx));
361 static rtx loop_call_insn_emit_before PARAMS((const struct loop *,
362                                               basic_block, rtx, rtx));
363 static rtx loop_call_insn_hoist PARAMS((const struct loop *, rtx));
364 static rtx loop_insn_sink_or_swim PARAMS((const struct loop *, rtx));
365
366 static void loop_dump_aux PARAMS ((const struct loop *, FILE *, int));
367 static void loop_delete_insns PARAMS ((rtx, rtx));
368 static HOST_WIDE_INT remove_constant_addition PARAMS ((rtx *));
369 static rtx gen_load_of_final_value PARAMS ((rtx, rtx));
370 void debug_ivs PARAMS ((const struct loop *));
371 void debug_iv_class PARAMS ((const struct iv_class *));
372 void debug_biv PARAMS ((const struct induction *));
373 void debug_giv PARAMS ((const struct induction *));
374 void debug_loop PARAMS ((const struct loop *));
375 void debug_loops PARAMS ((const struct loops *));
376
377 typedef struct rtx_pair
378 {
379   rtx r1;
380   rtx r2;
381 } rtx_pair;
382
383 typedef struct loop_replace_args
384 {
385   rtx match;
386   rtx replacement;
387   rtx insn;
388 } loop_replace_args;
389
390 /* Nonzero iff INSN is between START and END, inclusive.  */
391 #define INSN_IN_RANGE_P(INSN, START, END)       \
392   (INSN_UID (INSN) < max_uid_for_loop           \
393    && INSN_LUID (INSN) >= INSN_LUID (START)     \
394    && INSN_LUID (INSN) <= INSN_LUID (END))
395
396 /* Indirect_jump_in_function is computed once per function.  */
397 static int indirect_jump_in_function;
398 static int indirect_jump_in_function_p PARAMS ((rtx));
399
400 static int compute_luids PARAMS ((rtx, rtx, int));
401
402 static int biv_elimination_giv_has_0_offset PARAMS ((struct induction *,
403                                                      struct induction *,
404                                                      rtx));
405 \f
406 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
407    copy the value of the strength reduced giv to its original register.  */
408 static int copy_cost;
409
410 /* Cost of using a register, to normalize the benefits of a giv.  */
411 static int reg_address_cost;
412
413 void
414 init_loop ()
415 {
416   rtx reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
417
418   reg_address_cost = address_cost (reg, SImode);
419
420   copy_cost = COSTS_N_INSNS (1);
421 }
422 \f
423 /* Compute the mapping from uids to luids.
424    LUIDs are numbers assigned to insns, like uids,
425    except that luids increase monotonically through the code.
426    Start at insn START and stop just before END.  Assign LUIDs
427    starting with PREV_LUID + 1.  Return the last assigned LUID + 1.  */
428 static int
429 compute_luids (start, end, prev_luid)
430      rtx start, end;
431      int prev_luid;
432 {
433   int i;
434   rtx insn;
435
436   for (insn = start, i = prev_luid; insn != end; insn = NEXT_INSN (insn))
437     {
438       if (INSN_UID (insn) >= max_uid_for_loop)
439         continue;
440       /* Don't assign luids to line-number NOTEs, so that the distance in
441          luids between two insns is not affected by -g.  */
442       if (GET_CODE (insn) != NOTE
443           || NOTE_LINE_NUMBER (insn) <= 0)
444         uid_luid[INSN_UID (insn)] = ++i;
445       else
446         /* Give a line number note the same luid as preceding insn.  */
447         uid_luid[INSN_UID (insn)] = i;
448     }
449   return i + 1;
450 }
451 \f
452 /* Entry point of this file.  Perform loop optimization
453    on the current function.  F is the first insn of the function
454    and DUMPFILE is a stream for output of a trace of actions taken
455    (or 0 if none should be output).  */
456
457 void
458 loop_optimize (f, dumpfile, flags)
459      /* f is the first instruction of a chain of insns for one function */
460      rtx f;
461      FILE *dumpfile;
462      int flags;
463 {
464   rtx insn;
465   int i;
466   struct loops loops_data;
467   struct loops *loops = &loops_data;
468   struct loop_info *loops_info;
469
470   loop_dump_stream = dumpfile;
471
472   init_recog_no_volatile ();
473
474   max_reg_before_loop = max_reg_num ();
475   loop_max_reg = max_reg_before_loop;
476
477   regs_may_share = 0;
478
479   /* Count the number of loops.  */
480
481   max_loop_num = 0;
482   for (insn = f; insn; insn = NEXT_INSN (insn))
483     {
484       if (GET_CODE (insn) == NOTE
485           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
486         max_loop_num++;
487     }
488
489   /* Don't waste time if no loops.  */
490   if (max_loop_num == 0)
491     return;
492
493   loops->num = max_loop_num;
494
495   /* Get size to use for tables indexed by uids.
496      Leave some space for labels allocated by find_and_verify_loops.  */
497   max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
498
499   uid_luid = (int *) xcalloc (max_uid_for_loop, sizeof (int));
500   uid_loop = (struct loop **) xcalloc (max_uid_for_loop,
501                                        sizeof (struct loop *));
502
503   /* Allocate storage for array of loops.  */
504   loops->array = (struct loop *)
505     xcalloc (loops->num, sizeof (struct loop));
506
507   /* Find and process each loop.
508      First, find them, and record them in order of their beginnings.  */
509   find_and_verify_loops (f, loops);
510
511   /* Allocate and initialize auxiliary loop information.  */
512   loops_info = xcalloc (loops->num, sizeof (struct loop_info));
513   for (i = 0; i < loops->num; i++)
514     loops->array[i].aux = loops_info + i;
515
516   /* Now find all register lifetimes.  This must be done after
517      find_and_verify_loops, because it might reorder the insns in the
518      function.  */
519   reg_scan (f, max_reg_before_loop, 1);
520
521   /* This must occur after reg_scan so that registers created by gcse
522      will have entries in the register tables.
523
524      We could have added a call to reg_scan after gcse_main in toplev.c,
525      but moving this call to init_alias_analysis is more efficient.  */
526   init_alias_analysis ();
527
528   /* See if we went too far.  Note that get_max_uid already returns
529      one more that the maximum uid of all insn.  */
530   if (get_max_uid () > max_uid_for_loop)
531     abort ();
532   /* Now reset it to the actual size we need.  See above.  */
533   max_uid_for_loop = get_max_uid ();
534
535   /* find_and_verify_loops has already called compute_luids, but it
536      might have rearranged code afterwards, so we need to recompute
537      the luids now.  */
538   max_luid = compute_luids (f, NULL_RTX, 0);
539
540   /* Don't leave gaps in uid_luid for insns that have been
541      deleted.  It is possible that the first or last insn
542      using some register has been deleted by cross-jumping.
543      Make sure that uid_luid for that former insn's uid
544      points to the general area where that insn used to be.  */
545   for (i = 0; i < max_uid_for_loop; i++)
546     {
547       uid_luid[0] = uid_luid[i];
548       if (uid_luid[0] != 0)
549         break;
550     }
551   for (i = 0; i < max_uid_for_loop; i++)
552     if (uid_luid[i] == 0)
553       uid_luid[i] = uid_luid[i - 1];
554
555   /* Determine if the function has indirect jump.  On some systems
556      this prevents low overhead loop instructions from being used.  */
557   indirect_jump_in_function = indirect_jump_in_function_p (f);
558
559   /* Now scan the loops, last ones first, since this means inner ones are done
560      before outer ones.  */
561   for (i = max_loop_num - 1; i >= 0; i--)
562     {
563       struct loop *loop = &loops->array[i];
564
565       if (! loop->invalid && loop->end)
566         scan_loop (loop, flags);
567     }
568
569   /* If there were lexical blocks inside the loop, they have been
570      replicated.  We will now have more than one NOTE_INSN_BLOCK_BEG
571      and NOTE_INSN_BLOCK_END for each such block.  We must duplicate
572      the BLOCKs as well.  */
573   if (write_symbols != NO_DEBUG)
574     reorder_blocks ();
575
576   end_alias_analysis ();
577
578   /* Clean up.  */
579   free (uid_luid);
580   free (uid_loop);
581   free (loops_info);
582   free (loops->array);
583 }
584 \f
585 /* Returns the next insn, in execution order, after INSN.  START and
586    END are the NOTE_INSN_LOOP_BEG and NOTE_INSN_LOOP_END for the loop,
587    respectively.  LOOP->TOP, if non-NULL, is the top of the loop in the
588    insn-stream; it is used with loops that are entered near the
589    bottom.  */
590
591 static rtx
592 next_insn_in_loop (loop, insn)
593      const struct loop *loop;
594      rtx insn;
595 {
596   insn = NEXT_INSN (insn);
597
598   if (insn == loop->end)
599     {
600       if (loop->top)
601         /* Go to the top of the loop, and continue there.  */
602         insn = loop->top;
603       else
604         /* We're done.  */
605         insn = NULL_RTX;
606     }
607
608   if (insn == loop->scan_start)
609     /* We're done.  */
610     insn = NULL_RTX;
611
612   return insn;
613 }
614
615 /* Optimize one loop described by LOOP.  */
616
617 /* ??? Could also move memory writes out of loops if the destination address
618    is invariant, the source is invariant, the memory write is not volatile,
619    and if we can prove that no read inside the loop can read this address
620    before the write occurs.  If there is a read of this address after the
621    write, then we can also mark the memory read as invariant.  */
622
623 static void
624 scan_loop (loop, flags)
625      struct loop *loop;
626      int flags;
627 {
628   struct loop_info *loop_info = LOOP_INFO (loop);
629   struct loop_regs *regs = LOOP_REGS (loop);
630   int i;
631   rtx loop_start = loop->start;
632   rtx loop_end = loop->end;
633   rtx p;
634   /* 1 if we are scanning insns that could be executed zero times.  */
635   int maybe_never = 0;
636   /* 1 if we are scanning insns that might never be executed
637      due to a subroutine call which might exit before they are reached.  */
638   int call_passed = 0;
639   /* Jump insn that enters the loop, or 0 if control drops in.  */
640   rtx loop_entry_jump = 0;
641   /* Number of insns in the loop.  */
642   int insn_count;
643   int tem;
644   rtx temp, update_start, update_end;
645   /* The SET from an insn, if it is the only SET in the insn.  */
646   rtx set, set1;
647   /* Chain describing insns movable in current loop.  */
648   struct loop_movables *movables = LOOP_MOVABLES (loop);
649   /* Ratio of extra register life span we can justify
650      for saving an instruction.  More if loop doesn't call subroutines
651      since in that case saving an insn makes more difference
652      and more registers are available.  */
653   int threshold;
654   /* Nonzero if we are scanning instructions in a sub-loop.  */
655   int loop_depth = 0;
656
657   loop->top = 0;
658
659   movables->head = 0;
660   movables->last = 0;
661
662   /* Determine whether this loop starts with a jump down to a test at
663      the end.  This will occur for a small number of loops with a test
664      that is too complex to duplicate in front of the loop.
665
666      We search for the first insn or label in the loop, skipping NOTEs.
667      However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
668      (because we might have a loop executed only once that contains a
669      loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
670      (in case we have a degenerate loop).
671
672      Note that if we mistakenly think that a loop is entered at the top
673      when, in fact, it is entered at the exit test, the only effect will be
674      slightly poorer optimization.  Making the opposite error can generate
675      incorrect code.  Since very few loops now start with a jump to the
676      exit test, the code here to detect that case is very conservative.  */
677
678   for (p = NEXT_INSN (loop_start);
679        p != loop_end
680          && GET_CODE (p) != CODE_LABEL && ! INSN_P (p)
681          && (GET_CODE (p) != NOTE
682              || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
683                  && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
684        p = NEXT_INSN (p))
685     ;
686
687   loop->scan_start = p;
688
689   /* If loop end is the end of the current function, then emit a
690      NOTE_INSN_DELETED after loop_end and set loop->sink to the dummy
691      note insn.  This is the position we use when sinking insns out of
692      the loop.  */
693   if (NEXT_INSN (loop->end) != 0)
694     loop->sink = NEXT_INSN (loop->end);
695   else
696     loop->sink = emit_note_after (NOTE_INSN_DELETED, loop->end);
697
698   /* Set up variables describing this loop.  */
699   prescan_loop (loop);
700   threshold = (loop_info->has_call ? 1 : 2) * (1 + n_non_fixed_regs);
701
702   /* If loop has a jump before the first label,
703      the true entry is the target of that jump.
704      Start scan from there.
705      But record in LOOP->TOP the place where the end-test jumps
706      back to so we can scan that after the end of the loop.  */
707   if (GET_CODE (p) == JUMP_INSN)
708     {
709       loop_entry_jump = p;
710
711       /* Loop entry must be unconditional jump (and not a RETURN)  */
712       if (any_uncondjump_p (p)
713           && JUMP_LABEL (p) != 0
714           /* Check to see whether the jump actually
715              jumps out of the loop (meaning it's no loop).
716              This case can happen for things like
717              do {..} while (0).  If this label was generated previously
718              by loop, we can't tell anything about it and have to reject
719              the loop.  */
720           && INSN_IN_RANGE_P (JUMP_LABEL (p), loop_start, loop_end))
721         {
722           loop->top = next_label (loop->scan_start);
723           loop->scan_start = JUMP_LABEL (p);
724         }
725     }
726
727   /* If LOOP->SCAN_START was an insn created by loop, we don't know its luid
728      as required by loop_reg_used_before_p.  So skip such loops.  (This
729      test may never be true, but it's best to play it safe.)
730
731      Also, skip loops where we do not start scanning at a label.  This
732      test also rejects loops starting with a JUMP_INSN that failed the
733      test above.  */
734
735   if (INSN_UID (loop->scan_start) >= max_uid_for_loop
736       || GET_CODE (loop->scan_start) != CODE_LABEL)
737     {
738       if (loop_dump_stream)
739         fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
740                  INSN_UID (loop_start), INSN_UID (loop_end));
741       return;
742     }
743
744   /* Allocate extra space for REGs that might be created by load_mems.
745      We allocate a little extra slop as well, in the hopes that we
746      won't have to reallocate the regs array.  */
747   loop_regs_scan (loop, loop_info->mems_idx + 16);
748   insn_count = count_insns_in_loop (loop);
749
750   if (loop_dump_stream)
751     {
752       fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
753                INSN_UID (loop_start), INSN_UID (loop_end), insn_count);
754       if (loop->cont)
755         fprintf (loop_dump_stream, "Continue at insn %d.\n",
756                  INSN_UID (loop->cont));
757     }
758
759   /* Scan through the loop finding insns that are safe to move.
760      Set REGS->ARRAY[I].SET_IN_LOOP negative for the reg I being set, so that
761      this reg will be considered invariant for subsequent insns.
762      We consider whether subsequent insns use the reg
763      in deciding whether it is worth actually moving.
764
765      MAYBE_NEVER is nonzero if we have passed a conditional jump insn
766      and therefore it is possible that the insns we are scanning
767      would never be executed.  At such times, we must make sure
768      that it is safe to execute the insn once instead of zero times.
769      When MAYBE_NEVER is 0, all insns will be executed at least once
770      so that is not a problem.  */
771
772   for (p = next_insn_in_loop (loop, loop->scan_start);
773        p != NULL_RTX;
774        p = next_insn_in_loop (loop, p))
775     {
776       if (GET_CODE (p) == INSN
777           && (set = single_set (p))
778           && GET_CODE (SET_DEST (set)) == REG
779 #ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
780           && SET_DEST (set) != pic_offset_table_rtx
781 #endif
782           && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
783         {
784           int tem1 = 0;
785           int tem2 = 0;
786           int move_insn = 0;
787           rtx src = SET_SRC (set);
788           rtx dependencies = 0;
789
790           /* Figure out what to use as a source of this insn.  If a REG_EQUIV
791              note is given or if a REG_EQUAL note with a constant operand is
792              specified, use it as the source and mark that we should move
793              this insn by calling emit_move_insn rather that duplicating the
794              insn.
795
796              Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
797              is present.  */
798           temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
799           if (temp)
800             src = XEXP (temp, 0), move_insn = 1;
801           else
802             {
803               temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
804               if (temp && CONSTANT_P (XEXP (temp, 0)))
805                 src = XEXP (temp, 0), move_insn = 1;
806               if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
807                 {
808                   src = XEXP (temp, 0);
809                   /* A libcall block can use regs that don't appear in
810                      the equivalent expression.  To move the libcall,
811                      we must move those regs too.  */
812                   dependencies = libcall_other_reg (p, src);
813                 }
814             }
815
816           /* For parallels, add any possible uses to the depencies, as we can't move
817              the insn without resolving them first.  */
818           if (GET_CODE (PATTERN (p)) == PARALLEL)
819             {
820               for (i = 0; i < XVECLEN (PATTERN (p), 0); i++)
821                 {
822                   rtx x = XVECEXP (PATTERN (p), 0, i);
823                   if (GET_CODE (x) == USE)
824                     dependencies = gen_rtx_EXPR_LIST (VOIDmode, XEXP (x, 0), dependencies);
825                 }
826             }
827
828           /* Don't try to optimize a register that was made
829              by loop-optimization for an inner loop.
830              We don't know its life-span, so we can't compute the benefit.  */
831           if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
832             ;
833           else if (/* The register is used in basic blocks other
834                       than the one where it is set (meaning that
835                       something after this point in the loop might
836                       depend on its value before the set).  */
837                    ! reg_in_basic_block_p (p, SET_DEST (set))
838                    /* And the set is not guaranteed to be executed once
839                       the loop starts, or the value before the set is
840                       needed before the set occurs...
841
842                       ??? Note we have quadratic behaviour here, mitigated
843                       by the fact that the previous test will often fail for
844                       large loops.  Rather than re-scanning the entire loop
845                       each time for register usage, we should build tables
846                       of the register usage and use them here instead.  */
847                    && (maybe_never
848                        || loop_reg_used_before_p (loop, set, p)))
849             /* It is unsafe to move the set.
850
851                This code used to consider it OK to move a set of a variable
852                which was not created by the user and not used in an exit test.
853                That behavior is incorrect and was removed.  */
854             ;
855           else if ((tem = loop_invariant_p (loop, src))
856                    && (dependencies == 0
857                        || (tem2 = loop_invariant_p (loop, dependencies)) != 0)
858                    && (regs->array[REGNO (SET_DEST (set))].set_in_loop == 1
859                        || (tem1
860                            = consec_sets_invariant_p
861                            (loop, SET_DEST (set),
862                             regs->array[REGNO (SET_DEST (set))].set_in_loop,
863                             p)))
864                    /* If the insn can cause a trap (such as divide by zero),
865                       can't move it unless it's guaranteed to be executed
866                       once loop is entered.  Even a function call might
867                       prevent the trap insn from being reached
868                       (since it might exit!)  */
869                    && ! ((maybe_never || call_passed)
870                          && may_trap_p (src)))
871             {
872               struct movable *m;
873               int regno = REGNO (SET_DEST (set));
874
875               /* A potential lossage is where we have a case where two insns
876                  can be combined as long as they are both in the loop, but
877                  we move one of them outside the loop.  For large loops,
878                  this can lose.  The most common case of this is the address
879                  of a function being called.
880
881                  Therefore, if this register is marked as being used exactly
882                  once if we are in a loop with calls (a "large loop"), see if
883                  we can replace the usage of this register with the source
884                  of this SET.  If we can, delete this insn.
885
886                  Don't do this if P has a REG_RETVAL note or if we have
887                  SMALL_REGISTER_CLASSES and SET_SRC is a hard register.  */
888
889               if (loop_info->has_call
890                   && regs->array[regno].single_usage != 0
891                   && regs->array[regno].single_usage != const0_rtx
892                   && REGNO_FIRST_UID (regno) == INSN_UID (p)
893                   && (REGNO_LAST_UID (regno)
894                       == INSN_UID (regs->array[regno].single_usage))
895                   && regs->array[regno].set_in_loop == 1
896                   && GET_CODE (SET_SRC (set)) != ASM_OPERANDS
897                   && ! side_effects_p (SET_SRC (set))
898                   && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
899                   && (! SMALL_REGISTER_CLASSES
900                       || (! (GET_CODE (SET_SRC (set)) == REG
901                              && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)))
902                   /* This test is not redundant; SET_SRC (set) might be
903                      a call-clobbered register and the life of REGNO
904                      might span a call.  */
905                   && ! modified_between_p (SET_SRC (set), p,
906                                            regs->array[regno].single_usage)
907                   && no_labels_between_p (p, regs->array[regno].single_usage)
908                   && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
909                                            regs->array[regno].single_usage))
910                 {
911                   /* Replace any usage in a REG_EQUAL note.  Must copy the
912                      new source, so that we don't get rtx sharing between the
913                      SET_SOURCE and REG_NOTES of insn p.  */
914                   REG_NOTES (regs->array[regno].single_usage)
915                     = replace_rtx (REG_NOTES (regs->array[regno].single_usage),
916                                    SET_DEST (set), copy_rtx (SET_SRC (set)));
917
918                   delete_insn (p);
919                   for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
920                     regs->array[regno+i].set_in_loop = 0;
921                   continue;
922                 }
923
924               m = (struct movable *) xmalloc (sizeof (struct movable));
925               m->next = 0;
926               m->insn = p;
927               m->set_src = src;
928               m->dependencies = dependencies;
929               m->set_dest = SET_DEST (set);
930               m->force = 0;
931               m->consec = regs->array[REGNO (SET_DEST (set))].set_in_loop - 1;
932               m->done = 0;
933               m->forces = 0;
934               m->partial = 0;
935               m->move_insn = move_insn;
936               m->move_insn_first = 0;
937               m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
938               m->savemode = VOIDmode;
939               m->regno = regno;
940               /* Set M->cond if either loop_invariant_p
941                  or consec_sets_invariant_p returned 2
942                  (only conditionally invariant).  */
943               m->cond = ((tem | tem1 | tem2) > 1);
944               m->global =  LOOP_REG_GLOBAL_P (loop, regno);
945               m->match = 0;
946               m->lifetime = LOOP_REG_LIFETIME (loop, regno);
947               m->savings = regs->array[regno].n_times_set;
948               if (find_reg_note (p, REG_RETVAL, NULL_RTX))
949                 m->savings += libcall_benefit (p);
950               for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
951                 regs->array[regno+i].set_in_loop = move_insn ? -2 : -1;
952               /* Add M to the end of the chain MOVABLES.  */
953               loop_movables_add (movables, m);
954
955               if (m->consec > 0)
956                 {
957                   /* It is possible for the first instruction to have a
958                      REG_EQUAL note but a non-invariant SET_SRC, so we must
959                      remember the status of the first instruction in case
960                      the last instruction doesn't have a REG_EQUAL note.  */
961                   m->move_insn_first = m->move_insn;
962
963                   /* Skip this insn, not checking REG_LIBCALL notes.  */
964                   p = next_nonnote_insn (p);
965                   /* Skip the consecutive insns, if there are any.  */
966                   p = skip_consec_insns (p, m->consec);
967                   /* Back up to the last insn of the consecutive group.  */
968                   p = prev_nonnote_insn (p);
969
970                   /* We must now reset m->move_insn, m->is_equiv, and possibly
971                      m->set_src to correspond to the effects of all the
972                      insns.  */
973                   temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
974                   if (temp)
975                     m->set_src = XEXP (temp, 0), m->move_insn = 1;
976                   else
977                     {
978                       temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
979                       if (temp && CONSTANT_P (XEXP (temp, 0)))
980                         m->set_src = XEXP (temp, 0), m->move_insn = 1;
981                       else
982                         m->move_insn = 0;
983
984                     }
985                   m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
986                 }
987             }
988           /* If this register is always set within a STRICT_LOW_PART
989              or set to zero, then its high bytes are constant.
990              So clear them outside the loop and within the loop
991              just load the low bytes.
992              We must check that the machine has an instruction to do so.
993              Also, if the value loaded into the register
994              depends on the same register, this cannot be done.  */
995           else if (SET_SRC (set) == const0_rtx
996                    && GET_CODE (NEXT_INSN (p)) == INSN
997                    && (set1 = single_set (NEXT_INSN (p)))
998                    && GET_CODE (set1) == SET
999                    && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
1000                    && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
1001                    && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
1002                        == SET_DEST (set))
1003                    && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
1004             {
1005               int regno = REGNO (SET_DEST (set));
1006               if (regs->array[regno].set_in_loop == 2)
1007                 {
1008                   struct movable *m;
1009                   m = (struct movable *) xmalloc (sizeof (struct movable));
1010                   m->next = 0;
1011                   m->insn = p;
1012                   m->set_dest = SET_DEST (set);
1013                   m->dependencies = 0;
1014                   m->force = 0;
1015                   m->consec = 0;
1016                   m->done = 0;
1017                   m->forces = 0;
1018                   m->move_insn = 0;
1019                   m->move_insn_first = 0;
1020                   m->partial = 1;
1021                   /* If the insn may not be executed on some cycles,
1022                      we can't clear the whole reg; clear just high part.
1023                      Not even if the reg is used only within this loop.
1024                      Consider this:
1025                      while (1)
1026                        while (s != t) {
1027                          if (foo ()) x = *s;
1028                          use (x);
1029                        }
1030                      Clearing x before the inner loop could clobber a value
1031                      being saved from the last time around the outer loop.
1032                      However, if the reg is not used outside this loop
1033                      and all uses of the register are in the same
1034                      basic block as the store, there is no problem.
1035
1036                      If this insn was made by loop, we don't know its
1037                      INSN_LUID and hence must make a conservative
1038                      assumption.  */
1039                   m->global = (INSN_UID (p) >= max_uid_for_loop
1040                                || LOOP_REG_GLOBAL_P (loop, regno)
1041                                || (labels_in_range_p
1042                                    (p, REGNO_FIRST_LUID (regno))));
1043                   if (maybe_never && m->global)
1044                     m->savemode = GET_MODE (SET_SRC (set1));
1045                   else
1046                     m->savemode = VOIDmode;
1047                   m->regno = regno;
1048                   m->cond = 0;
1049                   m->match = 0;
1050                   m->lifetime = LOOP_REG_LIFETIME (loop, regno);
1051                   m->savings = 1;
1052                   for (i = 0; i < LOOP_REGNO_NREGS (regno, SET_DEST (set)); i++)
1053                     regs->array[regno+i].set_in_loop = -1;
1054                   /* Add M to the end of the chain MOVABLES.  */
1055                   loop_movables_add (movables, m);
1056                 }
1057             }
1058         }
1059       /* Past a call insn, we get to insns which might not be executed
1060          because the call might exit.  This matters for insns that trap.
1061          Constant and pure call insns always return, so they don't count.  */
1062       else if (GET_CODE (p) == CALL_INSN && ! CONST_OR_PURE_CALL_P (p))
1063         call_passed = 1;
1064       /* Past a label or a jump, we get to insns for which we
1065          can't count on whether or how many times they will be
1066          executed during each iteration.  Therefore, we can
1067          only move out sets of trivial variables
1068          (those not used after the loop).  */
1069       /* Similar code appears twice in strength_reduce.  */
1070       else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1071                /* If we enter the loop in the middle, and scan around to the
1072                   beginning, don't set maybe_never for that.  This must be an
1073                   unconditional jump, otherwise the code at the top of the
1074                   loop might never be executed.  Unconditional jumps are
1075                   followed by a barrier then the loop_end.  */
1076                && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop->top
1077                      && NEXT_INSN (NEXT_INSN (p)) == loop_end
1078                      && any_uncondjump_p (p)))
1079         maybe_never = 1;
1080       else if (GET_CODE (p) == NOTE)
1081         {
1082           /* At the virtual top of a converted loop, insns are again known to
1083              be executed: logically, the loop begins here even though the exit
1084              code has been duplicated.  */
1085           if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1086             maybe_never = call_passed = 0;
1087           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1088             loop_depth++;
1089           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1090             loop_depth--;
1091         }
1092     }
1093
1094   /* If one movable subsumes another, ignore that other.  */
1095
1096   ignore_some_movables (movables);
1097
1098   /* For each movable insn, see if the reg that it loads
1099      leads when it dies right into another conditionally movable insn.
1100      If so, record that the second insn "forces" the first one,
1101      since the second can be moved only if the first is.  */
1102
1103   force_movables (movables);
1104
1105   /* See if there are multiple movable insns that load the same value.
1106      If there are, make all but the first point at the first one
1107      through the `match' field, and add the priorities of them
1108      all together as the priority of the first.  */
1109
1110   combine_movables (movables, regs);
1111
1112   /* Now consider each movable insn to decide whether it is worth moving.
1113      Store 0 in regs->array[I].set_in_loop for each reg I that is moved.
1114
1115      Generally this increases code size, so do not move moveables when
1116      optimizing for code size.  */
1117
1118   if (! optimize_size)
1119     {
1120       move_movables (loop, movables, threshold, insn_count);
1121
1122       /* Recalculate regs->array if move_movables has created new
1123          registers.  */
1124       if (max_reg_num () > regs->num)
1125         {
1126           loop_regs_scan (loop, 0);
1127           for (update_start = loop_start;
1128                PREV_INSN (update_start)
1129                && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1130                update_start = PREV_INSN (update_start))
1131             ;
1132           update_end = NEXT_INSN (loop_end);
1133
1134           reg_scan_update (update_start, update_end, loop_max_reg);
1135           loop_max_reg = max_reg_num ();
1136         }
1137     }
1138
1139   /* Now candidates that still are negative are those not moved.
1140      Change regs->array[I].set_in_loop to indicate that those are not actually
1141      invariant.  */
1142   for (i = 0; i < regs->num; i++)
1143     if (regs->array[i].set_in_loop < 0)
1144       regs->array[i].set_in_loop = regs->array[i].n_times_set;
1145
1146   /* Now that we've moved some things out of the loop, we might be able to
1147      hoist even more memory references.  */
1148   load_mems (loop);
1149
1150   /* Recalculate regs->array if load_mems has created new registers.  */
1151   if (max_reg_num () > regs->num)
1152     loop_regs_scan (loop, 0);
1153
1154   for (update_start = loop_start;
1155        PREV_INSN (update_start)
1156          && GET_CODE (PREV_INSN (update_start)) != CODE_LABEL;
1157        update_start = PREV_INSN (update_start))
1158     ;
1159   update_end = NEXT_INSN (loop_end);
1160
1161   reg_scan_update (update_start, update_end, loop_max_reg);
1162   loop_max_reg = max_reg_num ();
1163
1164   if (flag_strength_reduce)
1165     {
1166       if (update_end && GET_CODE (update_end) == CODE_LABEL)
1167         /* Ensure our label doesn't go away.  */
1168         LABEL_NUSES (update_end)++;
1169
1170       strength_reduce (loop, flags);
1171
1172       reg_scan_update (update_start, update_end, loop_max_reg);
1173       loop_max_reg = max_reg_num ();
1174
1175       if (update_end && GET_CODE (update_end) == CODE_LABEL
1176           && --LABEL_NUSES (update_end) == 0)
1177         delete_related_insns (update_end);
1178     }
1179
1180
1181   /* The movable information is required for strength reduction.  */
1182   loop_movables_free (movables);
1183
1184   free (regs->array);
1185   regs->array = 0;
1186   regs->num = 0;
1187 }
1188 \f
1189 /* Add elements to *OUTPUT to record all the pseudo-regs
1190    mentioned in IN_THIS but not mentioned in NOT_IN_THIS.  */
1191
1192 void
1193 record_excess_regs (in_this, not_in_this, output)
1194      rtx in_this, not_in_this;
1195      rtx *output;
1196 {
1197   enum rtx_code code;
1198   const char *fmt;
1199   int i;
1200
1201   code = GET_CODE (in_this);
1202
1203   switch (code)
1204     {
1205     case PC:
1206     case CC0:
1207     case CONST_INT:
1208     case CONST_DOUBLE:
1209     case CONST:
1210     case SYMBOL_REF:
1211     case LABEL_REF:
1212       return;
1213
1214     case REG:
1215       if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1216           && ! reg_mentioned_p (in_this, not_in_this))
1217         *output = gen_rtx_EXPR_LIST (VOIDmode, in_this, *output);
1218       return;
1219
1220     default:
1221       break;
1222     }
1223
1224   fmt = GET_RTX_FORMAT (code);
1225   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1226     {
1227       int j;
1228
1229       switch (fmt[i])
1230         {
1231         case 'E':
1232           for (j = 0; j < XVECLEN (in_this, i); j++)
1233             record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1234           break;
1235
1236         case 'e':
1237           record_excess_regs (XEXP (in_this, i), not_in_this, output);
1238           break;
1239         }
1240     }
1241 }
1242 \f
1243 /* Check what regs are referred to in the libcall block ending with INSN,
1244    aside from those mentioned in the equivalent value.
1245    If there are none, return 0.
1246    If there are one or more, return an EXPR_LIST containing all of them.  */
1247
1248 rtx
1249 libcall_other_reg (insn, equiv)
1250      rtx insn, equiv;
1251 {
1252   rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1253   rtx p = XEXP (note, 0);
1254   rtx output = 0;
1255
1256   /* First, find all the regs used in the libcall block
1257      that are not mentioned as inputs to the result.  */
1258
1259   while (p != insn)
1260     {
1261       if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1262           || GET_CODE (p) == CALL_INSN)
1263         record_excess_regs (PATTERN (p), equiv, &output);
1264       p = NEXT_INSN (p);
1265     }
1266
1267   return output;
1268 }
1269 \f
1270 /* Return 1 if all uses of REG
1271    are between INSN and the end of the basic block.  */
1272
1273 static int
1274 reg_in_basic_block_p (insn, reg)
1275      rtx insn, reg;
1276 {
1277   int regno = REGNO (reg);
1278   rtx p;
1279
1280   if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1281     return 0;
1282
1283   /* Search this basic block for the already recorded last use of the reg.  */
1284   for (p = insn; p; p = NEXT_INSN (p))
1285     {
1286       switch (GET_CODE (p))
1287         {
1288         case NOTE:
1289           break;
1290
1291         case INSN:
1292         case CALL_INSN:
1293           /* Ordinary insn: if this is the last use, we win.  */
1294           if (REGNO_LAST_UID (regno) == INSN_UID (p))
1295             return 1;
1296           break;
1297
1298         case JUMP_INSN:
1299           /* Jump insn: if this is the last use, we win.  */
1300           if (REGNO_LAST_UID (regno) == INSN_UID (p))
1301             return 1;
1302           /* Otherwise, it's the end of the basic block, so we lose.  */
1303           return 0;
1304
1305         case CODE_LABEL:
1306         case BARRIER:
1307           /* It's the end of the basic block, so we lose.  */
1308           return 0;
1309
1310         default:
1311           break;
1312         }
1313     }
1314
1315   /* The "last use" that was recorded can't be found after the first
1316      use.  This can happen when the last use was deleted while
1317      processing an inner loop, this inner loop was then completely
1318      unrolled, and the outer loop is always exited after the inner loop,
1319      so that everything after the first use becomes a single basic block.  */
1320   return 1;
1321 }
1322 \f
1323 /* Compute the benefit of eliminating the insns in the block whose
1324    last insn is LAST.  This may be a group of insns used to compute a
1325    value directly or can contain a library call.  */
1326
1327 static int
1328 libcall_benefit (last)
1329      rtx last;
1330 {
1331   rtx insn;
1332   int benefit = 0;
1333
1334   for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1335        insn != last; insn = NEXT_INSN (insn))
1336     {
1337       if (GET_CODE (insn) == CALL_INSN)
1338         benefit += 10;          /* Assume at least this many insns in a library
1339                                    routine.  */
1340       else if (GET_CODE (insn) == INSN
1341                && GET_CODE (PATTERN (insn)) != USE
1342                && GET_CODE (PATTERN (insn)) != CLOBBER)
1343         benefit++;
1344     }
1345
1346   return benefit;
1347 }
1348 \f
1349 /* Skip COUNT insns from INSN, counting library calls as 1 insn.  */
1350
1351 static rtx
1352 skip_consec_insns (insn, count)
1353      rtx insn;
1354      int count;
1355 {
1356   for (; count > 0; count--)
1357     {
1358       rtx temp;
1359
1360       /* If first insn of libcall sequence, skip to end.  */
1361       /* Do this at start of loop, since INSN is guaranteed to
1362          be an insn here.  */
1363       if (GET_CODE (insn) != NOTE
1364           && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1365         insn = XEXP (temp, 0);
1366
1367       do
1368         insn = NEXT_INSN (insn);
1369       while (GET_CODE (insn) == NOTE);
1370     }
1371
1372   return insn;
1373 }
1374
1375 /* Ignore any movable whose insn falls within a libcall
1376    which is part of another movable.
1377    We make use of the fact that the movable for the libcall value
1378    was made later and so appears later on the chain.  */
1379
1380 static void
1381 ignore_some_movables (movables)
1382      struct loop_movables *movables;
1383 {
1384   struct movable *m, *m1;
1385
1386   for (m = movables->head; m; m = m->next)
1387     {
1388       /* Is this a movable for the value of a libcall?  */
1389       rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1390       if (note)
1391         {
1392           rtx insn;
1393           /* Check for earlier movables inside that range,
1394              and mark them invalid.  We cannot use LUIDs here because
1395              insns created by loop.c for prior loops don't have LUIDs.
1396              Rather than reject all such insns from movables, we just
1397              explicitly check each insn in the libcall (since invariant
1398              libcalls aren't that common).  */
1399           for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1400             for (m1 = movables->head; m1 != m; m1 = m1->next)
1401               if (m1->insn == insn)
1402                 m1->done = 1;
1403         }
1404     }
1405 }
1406
1407 /* For each movable insn, see if the reg that it loads
1408    leads when it dies right into another conditionally movable insn.
1409    If so, record that the second insn "forces" the first one,
1410    since the second can be moved only if the first is.  */
1411
1412 static void
1413 force_movables (movables)
1414      struct loop_movables *movables;
1415 {
1416   struct movable *m, *m1;
1417
1418   for (m1 = movables->head; m1; m1 = m1->next)
1419     /* Omit this if moving just the (SET (REG) 0) of a zero-extend.  */
1420     if (!m1->partial && !m1->done)
1421       {
1422         int regno = m1->regno;
1423         for (m = m1->next; m; m = m->next)
1424           /* ??? Could this be a bug?  What if CSE caused the
1425              register of M1 to be used after this insn?
1426              Since CSE does not update regno_last_uid,
1427              this insn M->insn might not be where it dies.
1428              But very likely this doesn't matter; what matters is
1429              that M's reg is computed from M1's reg.  */
1430           if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1431               && !m->done)
1432             break;
1433         if (m != 0 && m->set_src == m1->set_dest
1434             /* If m->consec, m->set_src isn't valid.  */
1435             && m->consec == 0)
1436           m = 0;
1437
1438         /* Increase the priority of the moving the first insn
1439            since it permits the second to be moved as well.  */
1440         if (m != 0)
1441           {
1442             m->forces = m1;
1443             m1->lifetime += m->lifetime;
1444             m1->savings += m->savings;
1445           }
1446       }
1447 }
1448 \f
1449 /* Find invariant expressions that are equal and can be combined into
1450    one register.  */
1451
1452 static void
1453 combine_movables (movables, regs)
1454      struct loop_movables *movables;
1455      struct loop_regs *regs;
1456 {
1457   struct movable *m;
1458   char *matched_regs = (char *) xmalloc (regs->num);
1459   enum machine_mode mode;
1460
1461   /* Regs that are set more than once are not allowed to match
1462      or be matched.  I'm no longer sure why not.  */
1463   /* Only pseudo registers are allowed to match or be matched,
1464      since move_movables does not validate the change.  */
1465   /* Perhaps testing m->consec_sets would be more appropriate here?  */
1466
1467   for (m = movables->head; m; m = m->next)
1468     if (m->match == 0 && regs->array[m->regno].n_times_set == 1
1469         && m->regno >= FIRST_PSEUDO_REGISTER
1470         && !m->partial)
1471       {
1472         struct movable *m1;
1473         int regno = m->regno;
1474
1475         memset (matched_regs, 0, regs->num);
1476         matched_regs[regno] = 1;
1477
1478         /* We want later insns to match the first one.  Don't make the first
1479            one match any later ones.  So start this loop at m->next.  */
1480         for (m1 = m->next; m1; m1 = m1->next)
1481           if (m != m1 && m1->match == 0
1482               && regs->array[m1->regno].n_times_set == 1
1483               && m1->regno >= FIRST_PSEUDO_REGISTER
1484               /* A reg used outside the loop mustn't be eliminated.  */
1485               && !m1->global
1486               /* A reg used for zero-extending mustn't be eliminated.  */
1487               && !m1->partial
1488               && (matched_regs[m1->regno]
1489                   ||
1490                   (
1491                    /* Can combine regs with different modes loaded from the
1492                       same constant only if the modes are the same or
1493                       if both are integer modes with M wider or the same
1494                       width as M1.  The check for integer is redundant, but
1495                       safe, since the only case of differing destination
1496                       modes with equal sources is when both sources are
1497                       VOIDmode, i.e., CONST_INT.  */
1498                    (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1499                     || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1500                         && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1501                         && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1502                             >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1503                    /* See if the source of M1 says it matches M.  */
1504                    && ((GET_CODE (m1->set_src) == REG
1505                         && matched_regs[REGNO (m1->set_src)])
1506                        || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1507                                                 movables, regs))))
1508               && ((m->dependencies == m1->dependencies)
1509                   || rtx_equal_p (m->dependencies, m1->dependencies)))
1510             {
1511               m->lifetime += m1->lifetime;
1512               m->savings += m1->savings;
1513               m1->done = 1;
1514               m1->match = m;
1515               matched_regs[m1->regno] = 1;
1516             }
1517       }
1518
1519   /* Now combine the regs used for zero-extension.
1520      This can be done for those not marked `global'
1521      provided their lives don't overlap.  */
1522
1523   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1524        mode = GET_MODE_WIDER_MODE (mode))
1525     {
1526       struct movable *m0 = 0;
1527
1528       /* Combine all the registers for extension from mode MODE.
1529          Don't combine any that are used outside this loop.  */
1530       for (m = movables->head; m; m = m->next)
1531         if (m->partial && ! m->global
1532             && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1533           {
1534             struct movable *m1;
1535
1536             int first = REGNO_FIRST_LUID (m->regno);
1537             int last = REGNO_LAST_LUID (m->regno);
1538
1539             if (m0 == 0)
1540               {
1541                 /* First one: don't check for overlap, just record it.  */
1542                 m0 = m;
1543                 continue;
1544               }
1545
1546             /* Make sure they extend to the same mode.
1547                (Almost always true.)  */
1548             if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1549               continue;
1550
1551             /* We already have one: check for overlap with those
1552                already combined together.  */
1553             for (m1 = movables->head; m1 != m; m1 = m1->next)
1554               if (m1 == m0 || (m1->partial && m1->match == m0))
1555                 if (! (REGNO_FIRST_LUID (m1->regno) > last
1556                        || REGNO_LAST_LUID (m1->regno) < first))
1557                   goto overlap;
1558
1559             /* No overlap: we can combine this with the others.  */
1560             m0->lifetime += m->lifetime;
1561             m0->savings += m->savings;
1562             m->done = 1;
1563             m->match = m0;
1564
1565           overlap:
1566             ;
1567           }
1568     }
1569
1570   /* Clean up.  */
1571   free (matched_regs);
1572 }
1573
1574 /* Returns the number of movable instructions in LOOP that were not
1575    moved outside the loop.  */
1576
1577 static int
1578 num_unmoved_movables (loop)
1579      const struct loop *loop;
1580 {
1581   int num = 0;
1582   struct movable *m;
1583
1584   for (m = LOOP_MOVABLES (loop)->head; m; m = m->next)
1585     if (!m->done)
1586       ++num;
1587
1588   return num;
1589 }
1590
1591 \f
1592 /* Return 1 if regs X and Y will become the same if moved.  */
1593
1594 static int
1595 regs_match_p (x, y, movables)
1596      rtx x, y;
1597      struct loop_movables *movables;
1598 {
1599   unsigned int xn = REGNO (x);
1600   unsigned int yn = REGNO (y);
1601   struct movable *mx, *my;
1602
1603   for (mx = movables->head; mx; mx = mx->next)
1604     if (mx->regno == xn)
1605       break;
1606
1607   for (my = movables->head; my; my = my->next)
1608     if (my->regno == yn)
1609       break;
1610
1611   return (mx && my
1612           && ((mx->match == my->match && mx->match != 0)
1613               || mx->match == my
1614               || mx == my->match));
1615 }
1616
1617 /* Return 1 if X and Y are identical-looking rtx's.
1618    This is the Lisp function EQUAL for rtx arguments.
1619
1620    If two registers are matching movables or a movable register and an
1621    equivalent constant, consider them equal.  */
1622
1623 static int
1624 rtx_equal_for_loop_p (x, y, movables, regs)
1625      rtx x, y;
1626      struct loop_movables *movables;
1627      struct loop_regs *regs;
1628 {
1629   int i;
1630   int j;
1631   struct movable *m;
1632   enum rtx_code code;
1633   const char *fmt;
1634
1635   if (x == y)
1636     return 1;
1637   if (x == 0 || y == 0)
1638     return 0;
1639
1640   code = GET_CODE (x);
1641
1642   /* If we have a register and a constant, they may sometimes be
1643      equal.  */
1644   if (GET_CODE (x) == REG && regs->array[REGNO (x)].set_in_loop == -2
1645       && CONSTANT_P (y))
1646     {
1647       for (m = movables->head; m; m = m->next)
1648         if (m->move_insn && m->regno == REGNO (x)
1649             && rtx_equal_p (m->set_src, y))
1650           return 1;
1651     }
1652   else if (GET_CODE (y) == REG && regs->array[REGNO (y)].set_in_loop == -2
1653            && CONSTANT_P (x))
1654     {
1655       for (m = movables->head; m; m = m->next)
1656         if (m->move_insn && m->regno == REGNO (y)
1657             && rtx_equal_p (m->set_src, x))
1658           return 1;
1659     }
1660
1661   /* Otherwise, rtx's of different codes cannot be equal.  */
1662   if (code != GET_CODE (y))
1663     return 0;
1664
1665   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1666      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
1667
1668   if (GET_MODE (x) != GET_MODE (y))
1669     return 0;
1670
1671   /* These three types of rtx's can be compared nonrecursively.  */
1672   if (code == REG)
1673     return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1674
1675   if (code == LABEL_REF)
1676     return XEXP (x, 0) == XEXP (y, 0);
1677   if (code == SYMBOL_REF)
1678     return XSTR (x, 0) == XSTR (y, 0);
1679
1680   /* Compare the elements.  If any pair of corresponding elements
1681      fail to match, return 0 for the whole things.  */
1682
1683   fmt = GET_RTX_FORMAT (code);
1684   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1685     {
1686       switch (fmt[i])
1687         {
1688         case 'w':
1689           if (XWINT (x, i) != XWINT (y, i))
1690             return 0;
1691           break;
1692
1693         case 'i':
1694           if (XINT (x, i) != XINT (y, i))
1695             return 0;
1696           break;
1697
1698         case 'E':
1699           /* Two vectors must have the same length.  */
1700           if (XVECLEN (x, i) != XVECLEN (y, i))
1701             return 0;
1702
1703           /* And the corresponding elements must match.  */
1704           for (j = 0; j < XVECLEN (x, i); j++)
1705             if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j),
1706                                       movables, regs) == 0)
1707               return 0;
1708           break;
1709
1710         case 'e':
1711           if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables, regs)
1712               == 0)
1713             return 0;
1714           break;
1715
1716         case 's':
1717           if (strcmp (XSTR (x, i), XSTR (y, i)))
1718             return 0;
1719           break;
1720
1721         case 'u':
1722           /* These are just backpointers, so they don't matter.  */
1723           break;
1724
1725         case '0':
1726           break;
1727
1728           /* It is believed that rtx's at this level will never
1729              contain anything but integers and other rtx's,
1730              except for within LABEL_REFs and SYMBOL_REFs.  */
1731         default:
1732           abort ();
1733         }
1734     }
1735   return 1;
1736 }
1737 \f
1738 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1739    insns in INSNS which use the reference.  LABEL_NUSES for CODE_LABEL
1740    references is incremented once for each added note.  */
1741
1742 static void
1743 add_label_notes (x, insns)
1744      rtx x;
1745      rtx insns;
1746 {
1747   enum rtx_code code = GET_CODE (x);
1748   int i, j;
1749   const char *fmt;
1750   rtx insn;
1751
1752   if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1753     {
1754       /* This code used to ignore labels that referred to dispatch tables to
1755          avoid flow generating (slighly) worse code.
1756
1757          We no longer ignore such label references (see LABEL_REF handling in
1758          mark_jump_label for additional information).  */
1759       for (insn = insns; insn; insn = NEXT_INSN (insn))
1760         if (reg_mentioned_p (XEXP (x, 0), insn))
1761           {
1762             REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
1763                                                   REG_NOTES (insn));
1764             if (LABEL_P (XEXP (x, 0)))
1765               LABEL_NUSES (XEXP (x, 0))++;
1766           }
1767     }
1768
1769   fmt = GET_RTX_FORMAT (code);
1770   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1771     {
1772       if (fmt[i] == 'e')
1773         add_label_notes (XEXP (x, i), insns);
1774       else if (fmt[i] == 'E')
1775         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1776           add_label_notes (XVECEXP (x, i, j), insns);
1777     }
1778 }
1779 \f
1780 /* Scan MOVABLES, and move the insns that deserve to be moved.
1781    If two matching movables are combined, replace one reg with the
1782    other throughout.  */
1783
1784 static void
1785 move_movables (loop, movables, threshold, insn_count)
1786      struct loop *loop;
1787      struct loop_movables *movables;
1788      int threshold;
1789      int insn_count;
1790 {
1791   struct loop_regs *regs = LOOP_REGS (loop);
1792   int nregs = regs->num;
1793   rtx new_start = 0;
1794   struct movable *m;
1795   rtx p;
1796   rtx loop_start = loop->start;
1797   rtx loop_end = loop->end;
1798   /* Map of pseudo-register replacements to handle combining
1799      when we move several insns that load the same value
1800      into different pseudo-registers.  */
1801   rtx *reg_map = (rtx *) xcalloc (nregs, sizeof (rtx));
1802   char *already_moved = (char *) xcalloc (nregs, sizeof (char));
1803
1804   for (m = movables->head; m; m = m->next)
1805     {
1806       /* Describe this movable insn.  */
1807
1808       if (loop_dump_stream)
1809         {
1810           fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1811                    INSN_UID (m->insn), m->regno, m->lifetime);
1812           if (m->consec > 0)
1813             fprintf (loop_dump_stream, "consec %d, ", m->consec);
1814           if (m->cond)
1815             fprintf (loop_dump_stream, "cond ");
1816           if (m->force)
1817             fprintf (loop_dump_stream, "force ");
1818           if (m->global)
1819             fprintf (loop_dump_stream, "global ");
1820           if (m->done)
1821             fprintf (loop_dump_stream, "done ");
1822           if (m->move_insn)
1823             fprintf (loop_dump_stream, "move-insn ");
1824           if (m->match)
1825             fprintf (loop_dump_stream, "matches %d ",
1826                      INSN_UID (m->match->insn));
1827           if (m->forces)
1828             fprintf (loop_dump_stream, "forces %d ",
1829                      INSN_UID (m->forces->insn));
1830         }
1831
1832       /* Ignore the insn if it's already done (it matched something else).
1833          Otherwise, see if it is now safe to move.  */
1834
1835       if (!m->done
1836           && (! m->cond
1837               || (1 == loop_invariant_p (loop, m->set_src)
1838                   && (m->dependencies == 0
1839                       || 1 == loop_invariant_p (loop, m->dependencies))
1840                   && (m->consec == 0
1841                       || 1 == consec_sets_invariant_p (loop, m->set_dest,
1842                                                        m->consec + 1,
1843                                                        m->insn))))
1844           && (! m->forces || m->forces->done))
1845         {
1846           int regno;
1847           rtx p;
1848           int savings = m->savings;
1849
1850           /* We have an insn that is safe to move.
1851              Compute its desirability.  */
1852
1853           p = m->insn;
1854           regno = m->regno;
1855
1856           if (loop_dump_stream)
1857             fprintf (loop_dump_stream, "savings %d ", savings);
1858
1859           if (regs->array[regno].moved_once && loop_dump_stream)
1860             fprintf (loop_dump_stream, "halved since already moved ");
1861
1862           /* An insn MUST be moved if we already moved something else
1863              which is safe only if this one is moved too: that is,
1864              if already_moved[REGNO] is nonzero.  */
1865
1866           /* An insn is desirable to move if the new lifetime of the
1867              register is no more than THRESHOLD times the old lifetime.
1868              If it's not desirable, it means the loop is so big
1869              that moving won't speed things up much,
1870              and it is liable to make register usage worse.  */
1871
1872           /* It is also desirable to move if it can be moved at no
1873              extra cost because something else was already moved.  */
1874
1875           if (already_moved[regno]
1876               || flag_move_all_movables
1877               || (threshold * savings * m->lifetime) >=
1878                  (regs->array[regno].moved_once ? insn_count * 2 : insn_count)
1879               || (m->forces && m->forces->done
1880                   && regs->array[m->forces->regno].n_times_set == 1))
1881             {
1882               int count;
1883               struct movable *m1;
1884               rtx first = NULL_RTX;
1885
1886               /* Now move the insns that set the reg.  */
1887
1888               if (m->partial && m->match)
1889                 {
1890                   rtx newpat, i1;
1891                   rtx r1, r2;
1892                   /* Find the end of this chain of matching regs.
1893                      Thus, we load each reg in the chain from that one reg.
1894                      And that reg is loaded with 0 directly,
1895                      since it has ->match == 0.  */
1896                   for (m1 = m; m1->match; m1 = m1->match);
1897                   newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1898                                           SET_DEST (PATTERN (m1->insn)));
1899                   i1 = loop_insn_hoist (loop, newpat);
1900
1901                   /* Mark the moved, invariant reg as being allowed to
1902                      share a hard reg with the other matching invariant.  */
1903                   REG_NOTES (i1) = REG_NOTES (m->insn);
1904                   r1 = SET_DEST (PATTERN (m->insn));
1905                   r2 = SET_DEST (PATTERN (m1->insn));
1906                   regs_may_share
1907                     = gen_rtx_EXPR_LIST (VOIDmode, r1,
1908                                          gen_rtx_EXPR_LIST (VOIDmode, r2,
1909                                                             regs_may_share));
1910                   delete_insn (m->insn);
1911
1912                   if (new_start == 0)
1913                     new_start = i1;
1914
1915                   if (loop_dump_stream)
1916                     fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1917                 }
1918               /* If we are to re-generate the item being moved with a
1919                  new move insn, first delete what we have and then emit
1920                  the move insn before the loop.  */
1921               else if (m->move_insn)
1922                 {
1923                   rtx i1, temp, seq;
1924
1925                   for (count = m->consec; count >= 0; count--)
1926                     {
1927                       /* If this is the first insn of a library call sequence,
1928                          skip to the end.  */
1929                       if (GET_CODE (p) != NOTE
1930                           && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1931                         p = XEXP (temp, 0);
1932
1933                       /* If this is the last insn of a libcall sequence, then
1934                          delete every insn in the sequence except the last.
1935                          The last insn is handled in the normal manner.  */
1936                       if (GET_CODE (p) != NOTE
1937                           && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1938                         {
1939                           temp = XEXP (temp, 0);
1940                           while (temp != p)
1941                             temp = delete_insn (temp);
1942                         }
1943
1944                       temp = p;
1945                       p = delete_insn (p);
1946
1947                       /* simplify_giv_expr expects that it can walk the insns
1948                          at m->insn forwards and see this old sequence we are
1949                          tossing here.  delete_insn does preserve the next
1950                          pointers, but when we skip over a NOTE we must fix
1951                          it up.  Otherwise that code walks into the non-deleted
1952                          insn stream.  */
1953                       while (p && GET_CODE (p) == NOTE)
1954                         p = NEXT_INSN (temp) = NEXT_INSN (p);
1955                     }
1956
1957                   start_sequence ();
1958                   emit_move_insn (m->set_dest, m->set_src);
1959                   temp = get_insns ();
1960                   seq = gen_sequence ();
1961                   end_sequence ();
1962
1963                   add_label_notes (m->set_src, temp);
1964
1965                   i1 = loop_insn_hoist (loop, seq);
1966                   if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1967                     set_unique_reg_note (i1,
1968                                          m->is_equiv ? REG_EQUIV : REG_EQUAL,
1969                                          m->set_src);
1970
1971                   if (loop_dump_stream)
1972                     fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1973
1974                   /* The more regs we move, the less we like moving them.  */
1975                   threshold -= 3;
1976                 }
1977               else
1978                 {
1979                   for (count = m->consec; count >= 0; count--)
1980                     {
1981                       rtx i1, temp;
1982
1983                       /* If first insn of libcall sequence, skip to end.  */
1984                       /* Do this at start of loop, since p is guaranteed to
1985                          be an insn here.  */
1986                       if (GET_CODE (p) != NOTE
1987                           && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1988                         p = XEXP (temp, 0);
1989
1990                       /* If last insn of libcall sequence, move all
1991                          insns except the last before the loop.  The last
1992                          insn is handled in the normal manner.  */
1993                       if (GET_CODE (p) != NOTE
1994                           && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1995                         {
1996                           rtx fn_address = 0;
1997                           rtx fn_reg = 0;
1998                           rtx fn_address_insn = 0;
1999
2000                           first = 0;
2001                           for (temp = XEXP (temp, 0); temp != p;
2002                                temp = NEXT_INSN (temp))
2003                             {
2004                               rtx body;
2005                               rtx n;
2006                               rtx next;
2007
2008                               if (GET_CODE (temp) == NOTE)
2009                                 continue;
2010
2011                               body = PATTERN (temp);
2012
2013                               /* Find the next insn after TEMP,
2014                                  not counting USE or NOTE insns.  */
2015                               for (next = NEXT_INSN (temp); next != p;
2016                                    next = NEXT_INSN (next))
2017                                 if (! (GET_CODE (next) == INSN
2018                                        && GET_CODE (PATTERN (next)) == USE)
2019                                     && GET_CODE (next) != NOTE)
2020                                   break;
2021
2022                               /* If that is the call, this may be the insn
2023                                  that loads the function address.
2024
2025                                  Extract the function address from the insn
2026                                  that loads it into a register.
2027                                  If this insn was cse'd, we get incorrect code.
2028
2029                                  So emit a new move insn that copies the
2030                                  function address into the register that the
2031                                  call insn will use.  flow.c will delete any
2032                                  redundant stores that we have created.  */
2033                               if (GET_CODE (next) == CALL_INSN
2034                                   && GET_CODE (body) == SET
2035                                   && GET_CODE (SET_DEST (body)) == REG
2036                                   && (n = find_reg_note (temp, REG_EQUAL,
2037                                                          NULL_RTX)))
2038                                 {
2039                                   fn_reg = SET_SRC (body);
2040                                   if (GET_CODE (fn_reg) != REG)
2041                                     fn_reg = SET_DEST (body);
2042                                   fn_address = XEXP (n, 0);
2043                                   fn_address_insn = temp;
2044                                 }
2045                               /* We have the call insn.
2046                                  If it uses the register we suspect it might,
2047                                  load it with the correct address directly.  */
2048                               if (GET_CODE (temp) == CALL_INSN
2049                                   && fn_address != 0
2050                                   && reg_referenced_p (fn_reg, body))
2051                                 loop_insn_emit_after (loop, 0, fn_address_insn,
2052                                                       gen_move_insn
2053                                                       (fn_reg, fn_address));
2054
2055                               if (GET_CODE (temp) == CALL_INSN)
2056                                 {
2057                                   i1 = loop_call_insn_hoist (loop, body);
2058                                   /* Because the USAGE information potentially
2059                                      contains objects other than hard registers
2060                                      we need to copy it.  */
2061                                   if (CALL_INSN_FUNCTION_USAGE (temp))
2062                                     CALL_INSN_FUNCTION_USAGE (i1)
2063                                       = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
2064                                 }
2065                               else
2066                                 i1 = loop_insn_hoist (loop, body);
2067                               if (first == 0)
2068                                 first = i1;
2069                               if (temp == fn_address_insn)
2070                                 fn_address_insn = i1;
2071                               REG_NOTES (i1) = REG_NOTES (temp);
2072                               REG_NOTES (temp) = NULL;
2073                               delete_insn (temp);
2074                             }
2075                           if (new_start == 0)
2076                             new_start = first;
2077                         }
2078                       if (m->savemode != VOIDmode)
2079                         {
2080                           /* P sets REG to zero; but we should clear only
2081                              the bits that are not covered by the mode
2082                              m->savemode.  */
2083                           rtx reg = m->set_dest;
2084                           rtx sequence;
2085                           rtx tem;
2086
2087                           start_sequence ();
2088                           tem = expand_simple_binop
2089                             (GET_MODE (reg), AND, reg,
2090                              GEN_INT ((((HOST_WIDE_INT) 1
2091                                         << GET_MODE_BITSIZE (m->savemode)))
2092                                       - 1),
2093                              reg, 1, OPTAB_LIB_WIDEN);
2094                           if (tem == 0)
2095                             abort ();
2096                           if (tem != reg)
2097                             emit_move_insn (reg, tem);
2098                           sequence = gen_sequence ();
2099                           end_sequence ();
2100                           i1 = loop_insn_hoist (loop, sequence);
2101                         }
2102                       else if (GET_CODE (p) == CALL_INSN)
2103                         {
2104                           i1 = loop_call_insn_hoist (loop, PATTERN (p));
2105                           /* Because the USAGE information potentially
2106                              contains objects other than hard registers
2107                              we need to copy it.  */
2108                           if (CALL_INSN_FUNCTION_USAGE (p))
2109                             CALL_INSN_FUNCTION_USAGE (i1)
2110                               = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
2111                         }
2112                       else if (count == m->consec && m->move_insn_first)
2113                         {
2114                           rtx seq;
2115                           /* The SET_SRC might not be invariant, so we must
2116                              use the REG_EQUAL note.  */
2117                           start_sequence ();
2118                           emit_move_insn (m->set_dest, m->set_src);
2119                           temp = get_insns ();
2120                           seq = gen_sequence ();
2121                           end_sequence ();
2122
2123                           add_label_notes (m->set_src, temp);
2124
2125                           i1 = loop_insn_hoist (loop, seq);
2126                           if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
2127                             set_unique_reg_note (i1, m->is_equiv ? REG_EQUIV
2128                                                      : REG_EQUAL, m->set_src);
2129                         }
2130                       else
2131                         i1 = loop_insn_hoist (loop, PATTERN (p));
2132
2133                       if (REG_NOTES (i1) == 0)
2134                         {
2135                           REG_NOTES (i1) = REG_NOTES (p);
2136                           REG_NOTES (p) = NULL;
2137
2138                           /* If there is a REG_EQUAL note present whose value
2139                              is not loop invariant, then delete it, since it
2140                              may cause problems with later optimization passes.
2141                              It is possible for cse to create such notes
2142                              like this as a result of record_jump_cond.  */
2143
2144                           if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
2145                               && ! loop_invariant_p (loop, XEXP (temp, 0)))
2146                             remove_note (i1, temp);
2147                         }
2148
2149                       if (new_start == 0)
2150                         new_start = i1;
2151
2152                       if (loop_dump_stream)
2153                         fprintf (loop_dump_stream, " moved to %d",
2154                                  INSN_UID (i1));
2155
2156                       /* If library call, now fix the REG_NOTES that contain
2157                          insn pointers, namely REG_LIBCALL on FIRST
2158                          and REG_RETVAL on I1.  */
2159                       if ((temp = find_reg_note (i1, REG_RETVAL, NULL_RTX)))
2160                         {
2161                           XEXP (temp, 0) = first;
2162                           temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
2163                           XEXP (temp, 0) = i1;
2164                         }
2165
2166                       temp = p;
2167                       delete_insn (p);
2168                       p = NEXT_INSN (p);
2169
2170                       /* simplify_giv_expr expects that it can walk the insns
2171                          at m->insn forwards and see this old sequence we are
2172                          tossing here.  delete_insn does preserve the next
2173                          pointers, but when we skip over a NOTE we must fix
2174                          it up.  Otherwise that code walks into the non-deleted
2175                          insn stream.  */
2176                       while (p && GET_CODE (p) == NOTE)
2177                         p = NEXT_INSN (temp) = NEXT_INSN (p);
2178                     }
2179
2180                   /* The more regs we move, the less we like moving them.  */
2181                   threshold -= 3;
2182                 }
2183
2184               /* Any other movable that loads the same register
2185                  MUST be moved.  */
2186               already_moved[regno] = 1;
2187
2188               /* This reg has been moved out of one loop.  */
2189               regs->array[regno].moved_once = 1;
2190
2191               /* The reg set here is now invariant.  */
2192               if (! m->partial)
2193                 {
2194                   int i;
2195                   for (i = 0; i < LOOP_REGNO_NREGS (regno, m->set_dest); i++)
2196                     regs->array[regno+i].set_in_loop = 0;
2197                 }
2198
2199               m->done = 1;
2200
2201               /* Change the length-of-life info for the register
2202                  to say it lives at least the full length of this loop.
2203                  This will help guide optimizations in outer loops.  */
2204
2205               if (REGNO_FIRST_LUID (regno) > INSN_LUID (loop_start))
2206                 /* This is the old insn before all the moved insns.
2207                    We can't use the moved insn because it is out of range
2208                    in uid_luid.  Only the old insns have luids.  */
2209                 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2210               if (REGNO_LAST_LUID (regno) < INSN_LUID (loop_end))
2211                 REGNO_LAST_UID (regno) = INSN_UID (loop_end);
2212
2213               /* Combine with this moved insn any other matching movables.  */
2214
2215               if (! m->partial)
2216                 for (m1 = movables->head; m1; m1 = m1->next)
2217                   if (m1->match == m)
2218                     {
2219                       rtx temp;
2220
2221                       /* Schedule the reg loaded by M1
2222                          for replacement so that shares the reg of M.
2223                          If the modes differ (only possible in restricted
2224                          circumstances, make a SUBREG.
2225
2226                          Note this assumes that the target dependent files
2227                          treat REG and SUBREG equally, including within
2228                          GO_IF_LEGITIMATE_ADDRESS and in all the
2229                          predicates since we never verify that replacing the
2230                          original register with a SUBREG results in a
2231                          recognizable insn.  */
2232                       if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2233                         reg_map[m1->regno] = m->set_dest;
2234                       else
2235                         reg_map[m1->regno]
2236                           = gen_lowpart_common (GET_MODE (m1->set_dest),
2237                                                 m->set_dest);
2238
2239                       /* Get rid of the matching insn
2240                          and prevent further processing of it.  */
2241                       m1->done = 1;
2242
2243                       /* if library call, delete all insns.  */
2244                       if ((temp = find_reg_note (m1->insn, REG_RETVAL,
2245                                                  NULL_RTX)))
2246                         delete_insn_chain (XEXP (temp, 0), m1->insn);
2247                       else
2248                         delete_insn (m1->insn);
2249
2250                       /* Any other movable that loads the same register
2251                          MUST be moved.  */
2252                       already_moved[m1->regno] = 1;
2253
2254                       /* The reg merged here is now invariant,
2255                          if the reg it matches is invariant.  */
2256                       if (! m->partial)
2257                         {
2258                           int i;
2259                           for (i = 0;
2260                                i < LOOP_REGNO_NREGS (regno, m1->set_dest);
2261                                i++)
2262                             regs->array[m1->regno+i].set_in_loop = 0;
2263                         }
2264                     }
2265             }
2266           else if (loop_dump_stream)
2267             fprintf (loop_dump_stream, "not desirable");
2268         }
2269       else if (loop_dump_stream && !m->match)
2270         fprintf (loop_dump_stream, "not safe");
2271
2272       if (loop_dump_stream)
2273         fprintf (loop_dump_stream, "\n");
2274     }
2275
2276   if (new_start == 0)
2277     new_start = loop_start;
2278
2279   /* Go through all the instructions in the loop, making
2280      all the register substitutions scheduled in REG_MAP.  */
2281   for (p = new_start; p != loop_end; p = NEXT_INSN (p))
2282     if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2283         || GET_CODE (p) == CALL_INSN)
2284       {
2285         replace_regs (PATTERN (p), reg_map, nregs, 0);
2286         replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2287         INSN_CODE (p) = -1;
2288       }
2289
2290   /* Clean up.  */
2291   free (reg_map);
2292   free (already_moved);
2293 }
2294
2295
2296 static void
2297 loop_movables_add (movables, m)
2298      struct loop_movables *movables;
2299      struct movable *m;
2300 {
2301   if (movables->head == 0)
2302     movables->head = m;
2303   else
2304     movables->last->next = m;
2305   movables->last = m;
2306 }
2307
2308
2309 static void
2310 loop_movables_free (movables)
2311      struct loop_movables *movables;
2312 {
2313   struct movable *m;
2314   struct movable *m_next;
2315
2316   for (m = movables->head; m; m = m_next)
2317     {
2318       m_next = m->next;
2319       free (m);
2320     }
2321 }
2322 \f
2323 #if 0
2324 /* Scan X and replace the address of any MEM in it with ADDR.
2325    REG is the address that MEM should have before the replacement.  */
2326
2327 static void
2328 replace_call_address (x, reg, addr)
2329      rtx x, reg, addr;
2330 {
2331   enum rtx_code code;
2332   int i;
2333   const char *fmt;
2334
2335   if (x == 0)
2336     return;
2337   code = GET_CODE (x);
2338   switch (code)
2339     {
2340     case PC:
2341     case CC0:
2342     case CONST_INT:
2343     case CONST_DOUBLE:
2344     case CONST:
2345     case SYMBOL_REF:
2346     case LABEL_REF:
2347     case REG:
2348       return;
2349
2350     case SET:
2351       /* Short cut for very common case.  */
2352       replace_call_address (XEXP (x, 1), reg, addr);
2353       return;
2354
2355     case CALL:
2356       /* Short cut for very common case.  */
2357       replace_call_address (XEXP (x, 0), reg, addr);
2358       return;
2359
2360     case MEM:
2361       /* If this MEM uses a reg other than the one we expected,
2362          something is wrong.  */
2363       if (XEXP (x, 0) != reg)
2364         abort ();
2365       XEXP (x, 0) = addr;
2366       return;
2367
2368     default:
2369       break;
2370     }
2371
2372   fmt = GET_RTX_FORMAT (code);
2373   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2374     {
2375       if (fmt[i] == 'e')
2376         replace_call_address (XEXP (x, i), reg, addr);
2377       else if (fmt[i] == 'E')
2378         {
2379           int j;
2380           for (j = 0; j < XVECLEN (x, i); j++)
2381             replace_call_address (XVECEXP (x, i, j), reg, addr);
2382         }
2383     }
2384 }
2385 #endif
2386 \f
2387 /* Return the number of memory refs to addresses that vary
2388    in the rtx X.  */
2389
2390 static int
2391 count_nonfixed_reads (loop, x)
2392      const struct loop *loop;
2393      rtx x;
2394 {
2395   enum rtx_code code;
2396   int i;
2397   const char *fmt;
2398   int value;
2399
2400   if (x == 0)
2401     return 0;
2402
2403   code = GET_CODE (x);
2404   switch (code)
2405     {
2406     case PC:
2407     case CC0:
2408     case CONST_INT:
2409     case CONST_DOUBLE:
2410     case CONST:
2411     case SYMBOL_REF:
2412     case LABEL_REF:
2413     case REG:
2414       return 0;
2415
2416     case MEM:
2417       return ((loop_invariant_p (loop, XEXP (x, 0)) != 1)
2418               + count_nonfixed_reads (loop, XEXP (x, 0)));
2419
2420     default:
2421       break;
2422     }
2423
2424   value = 0;
2425   fmt = GET_RTX_FORMAT (code);
2426   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2427     {
2428       if (fmt[i] == 'e')
2429         value += count_nonfixed_reads (loop, XEXP (x, i));
2430       if (fmt[i] == 'E')
2431         {
2432           int j;
2433           for (j = 0; j < XVECLEN (x, i); j++)
2434             value += count_nonfixed_reads (loop, XVECEXP (x, i, j));
2435         }
2436     }
2437   return value;
2438 }
2439 \f
2440 /* Scan a loop setting the elements `cont', `vtop', `loops_enclosed',
2441    `has_call', `has_nonconst_call', `has_volatile', `has_tablejump',
2442    `unknown_address_altered', `unknown_constant_address_altered', and
2443    `num_mem_sets' in LOOP.  Also, fill in the array `mems' and the
2444    list `store_mems' in LOOP.  */
2445
2446 static void
2447 prescan_loop (loop)
2448      struct loop *loop;
2449 {
2450   int level = 1;
2451   rtx insn;
2452   struct loop_info *loop_info = LOOP_INFO (loop);
2453   rtx start = loop->start;
2454   rtx end = loop->end;
2455   /* The label after END.  Jumping here is just like falling off the
2456      end of the loop.  We use next_nonnote_insn instead of next_label
2457      as a hedge against the (pathological) case where some actual insn
2458      might end up between the two.  */
2459   rtx exit_target = next_nonnote_insn (end);
2460
2461   loop_info->has_indirect_jump = indirect_jump_in_function;
2462   loop_info->pre_header_has_call = 0;
2463   loop_info->has_call = 0;
2464   loop_info->has_nonconst_call = 0;
2465   loop_info->has_prefetch = 0;
2466   loop_info->has_volatile = 0;
2467   loop_info->has_tablejump = 0;
2468   loop_info->has_multiple_exit_targets = 0;
2469   loop->level = 1;
2470
2471   loop_info->unknown_address_altered = 0;
2472   loop_info->unknown_constant_address_altered = 0;
2473   loop_info->store_mems = NULL_RTX;
2474   loop_info->first_loop_store_insn = NULL_RTX;
2475   loop_info->mems_idx = 0;
2476   loop_info->num_mem_sets = 0;
2477
2478
2479   for (insn = start; insn && GET_CODE (insn) != CODE_LABEL;
2480        insn = PREV_INSN (insn))
2481     {
2482       if (GET_CODE (insn) == CALL_INSN)
2483         {
2484           loop_info->pre_header_has_call = 1;
2485           break;
2486         }
2487     }
2488
2489   for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2490        insn = NEXT_INSN (insn))
2491     {
2492       switch (GET_CODE (insn))
2493         {
2494         case NOTE:
2495           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2496             {
2497               ++level;
2498               /* Count number of loops contained in this one.  */
2499               loop->level++;
2500             }
2501           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2502             --level;
2503           break;
2504
2505         case CALL_INSN:
2506           if (! CONST_OR_PURE_CALL_P (insn))
2507             {
2508               loop_info->unknown_address_altered = 1;
2509               loop_info->has_nonconst_call = 1;
2510             }
2511           else if (pure_call_p (insn))
2512             loop_info->has_nonconst_call = 1;
2513           loop_info->has_call = 1;
2514           if (can_throw_internal (insn))
2515             loop_info->has_multiple_exit_targets = 1;
2516           break;
2517
2518         case JUMP_INSN:
2519           if (! loop_info->has_multiple_exit_targets)
2520             {
2521               rtx set = pc_set (insn);
2522
2523               if (set)
2524                 {
2525                   rtx src = SET_SRC (set);
2526                   rtx label1, label2;
2527
2528                   if (GET_CODE (src) == IF_THEN_ELSE)
2529                     {
2530                       label1 = XEXP (src, 1);
2531                       label2 = XEXP (src, 2);
2532                     }
2533                   else
2534                     {
2535                       label1 = src;
2536                       label2 = NULL_RTX;
2537                     }
2538
2539                   do
2540                     {
2541                       if (label1 && label1 != pc_rtx)
2542                         {
2543                           if (GET_CODE (label1) != LABEL_REF)
2544                             {
2545                               /* Something tricky.  */
2546                               loop_info->has_multiple_exit_targets = 1;
2547                               break;
2548                             }
2549                           else if (XEXP (label1, 0) != exit_target
2550                                    && LABEL_OUTSIDE_LOOP_P (label1))
2551                             {
2552                               /* A jump outside the current loop.  */
2553                               loop_info->has_multiple_exit_targets = 1;
2554                               break;
2555                             }
2556                         }
2557
2558                       label1 = label2;
2559                       label2 = NULL_RTX;
2560                     }
2561                   while (label1);
2562                 }
2563               else
2564                 {
2565                   /* A return, or something tricky.  */
2566                   loop_info->has_multiple_exit_targets = 1;
2567                 }
2568             }
2569           /* FALLTHRU */
2570
2571         case INSN:
2572           if (volatile_refs_p (PATTERN (insn)))
2573             loop_info->has_volatile = 1;
2574
2575           if (GET_CODE (insn) == JUMP_INSN
2576               && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2577                   || GET_CODE (PATTERN (insn)) == ADDR_VEC))
2578             loop_info->has_tablejump = 1;
2579
2580           note_stores (PATTERN (insn), note_addr_stored, loop_info);
2581           if (! loop_info->first_loop_store_insn && loop_info->store_mems)
2582             loop_info->first_loop_store_insn = insn;
2583
2584           if (flag_non_call_exceptions && can_throw_internal (insn))
2585             loop_info->has_multiple_exit_targets = 1;
2586           break;
2587
2588         default:
2589           break;
2590         }
2591     }
2592
2593   /* Now, rescan the loop, setting up the LOOP_MEMS array.  */
2594   if (/* An exception thrown by a called function might land us
2595          anywhere.  */
2596       ! loop_info->has_nonconst_call
2597       /* We don't want loads for MEMs moved to a location before the
2598          one at which their stack memory becomes allocated.  (Note
2599          that this is not a problem for malloc, etc., since those
2600          require actual function calls.  */
2601       && ! current_function_calls_alloca
2602       /* There are ways to leave the loop other than falling off the
2603          end.  */
2604       && ! loop_info->has_multiple_exit_targets)
2605     for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2606          insn = NEXT_INSN (insn))
2607       for_each_rtx (&insn, insert_loop_mem, loop_info);
2608
2609   /* BLKmode MEMs are added to LOOP_STORE_MEM as necessary so
2610      that loop_invariant_p and load_mems can use true_dependence
2611      to determine what is really clobbered.  */
2612   if (loop_info->unknown_address_altered)
2613     {
2614       rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2615
2616       loop_info->store_mems
2617         = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2618     }
2619   if (loop_info->unknown_constant_address_altered)
2620     {
2621       rtx mem = gen_rtx_MEM (BLKmode, const0_rtx);
2622
2623       RTX_UNCHANGING_P (mem) = 1;
2624       loop_info->store_mems
2625         = gen_rtx_EXPR_LIST (VOIDmode, mem, loop_info->store_mems);
2626     }
2627 }
2628 \f
2629 /* Invalidate all loops containing LABEL.  */
2630
2631 static void
2632 invalidate_loops_containing_label (label)
2633      rtx label;
2634 {
2635   struct loop *loop;
2636   for (loop = uid_loop[INSN_UID (label)]; loop; loop = loop->outer)
2637     loop->invalid = 1;
2638 }
2639
2640 /* Scan the function looking for loops.  Record the start and end of each loop.
2641    Also mark as invalid loops any loops that contain a setjmp or are branched
2642    to from outside the loop.  */
2643
2644 static void
2645 find_and_verify_loops (f, loops)
2646      rtx f;
2647      struct loops *loops;
2648 {
2649   rtx insn;
2650   rtx label;
2651   int num_loops;
2652   struct loop *current_loop;
2653   struct loop *next_loop;
2654   struct loop *loop;
2655
2656   num_loops = loops->num;
2657
2658   compute_luids (f, NULL_RTX, 0);
2659
2660   /* If there are jumps to undefined labels,
2661      treat them as jumps out of any/all loops.
2662      This also avoids writing past end of tables when there are no loops.  */
2663   uid_loop[0] = NULL;
2664
2665   /* Find boundaries of loops, mark which loops are contained within
2666      loops, and invalidate loops that have setjmp.  */
2667
2668   num_loops = 0;
2669   current_loop = NULL;
2670   for (insn = f; insn; insn = NEXT_INSN (insn))
2671     {
2672       if (GET_CODE (insn) == NOTE)
2673         switch (NOTE_LINE_NUMBER (insn))
2674           {
2675           case NOTE_INSN_LOOP_BEG:
2676             next_loop = loops->array + num_loops;
2677             next_loop->num = num_loops;
2678             num_loops++;
2679             next_loop->start = insn;
2680             next_loop->outer = current_loop;
2681             current_loop = next_loop;
2682             break;
2683
2684           case NOTE_INSN_LOOP_CONT:
2685             current_loop->cont = insn;
2686             break;
2687
2688           case NOTE_INSN_LOOP_VTOP:
2689             current_loop->vtop = insn;
2690             break;
2691
2692           case NOTE_INSN_LOOP_END:
2693             if (! current_loop)
2694               abort ();
2695
2696             current_loop->end = insn;
2697             current_loop = current_loop->outer;
2698             break;
2699
2700           default:
2701             break;
2702           }
2703
2704       if (GET_CODE (insn) == CALL_INSN
2705           && find_reg_note (insn, REG_SETJMP, NULL))
2706         {
2707           /* In this case, we must invalidate our current loop and any
2708              enclosing loop.  */
2709           for (loop = current_loop; loop; loop = loop->outer)
2710             {
2711               loop->invalid = 1;
2712               if (loop_dump_stream)
2713                 fprintf (loop_dump_stream,
2714                          "\nLoop at %d ignored due to setjmp.\n",
2715                          INSN_UID (loop->start));
2716             }
2717         }
2718
2719       /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2720          enclosing loop, but this doesn't matter.  */
2721       uid_loop[INSN_UID (insn)] = current_loop;
2722     }
2723
2724   /* Any loop containing a label used in an initializer must be invalidated,
2725      because it can be jumped into from anywhere.  */
2726   for (label = forced_labels; label; label = XEXP (label, 1))
2727     invalidate_loops_containing_label (XEXP (label, 0));
2728
2729   /* Any loop containing a label used for an exception handler must be
2730      invalidated, because it can be jumped into from anywhere.  */
2731   for_each_eh_label (invalidate_loops_containing_label);
2732
2733   /* Now scan all insn's in the function.  If any JUMP_INSN branches into a
2734      loop that it is not contained within, that loop is marked invalid.
2735      If any INSN or CALL_INSN uses a label's address, then the loop containing
2736      that label is marked invalid, because it could be jumped into from
2737      anywhere.
2738
2739      Also look for blocks of code ending in an unconditional branch that
2740      exits the loop.  If such a block is surrounded by a conditional
2741      branch around the block, move the block elsewhere (see below) and
2742      invert the jump to point to the code block.  This may eliminate a
2743      label in our loop and will simplify processing by both us and a
2744      possible second cse pass.  */
2745
2746   for (insn = f; insn; insn = NEXT_INSN (insn))
2747     if (INSN_P (insn))
2748       {
2749         struct loop *this_loop = uid_loop[INSN_UID (insn)];
2750
2751         if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2752           {
2753             rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2754             if (note)
2755               invalidate_loops_containing_label (XEXP (note, 0));
2756           }
2757
2758         if (GET_CODE (insn) != JUMP_INSN)
2759           continue;
2760
2761         mark_loop_jump (PATTERN (insn), this_loop);
2762
2763         /* See if this is an unconditional branch outside the loop.  */
2764         if (this_loop
2765             && (GET_CODE (PATTERN (insn)) == RETURN
2766                 || (any_uncondjump_p (insn)
2767                     && onlyjump_p (insn)
2768                     && (uid_loop[INSN_UID (JUMP_LABEL (insn))]
2769                         != this_loop)))
2770             && get_max_uid () < max_uid_for_loop)
2771           {
2772             rtx p;
2773             rtx our_next = next_real_insn (insn);
2774             rtx last_insn_to_move = NEXT_INSN (insn);
2775             struct loop *dest_loop;
2776             struct loop *outer_loop = NULL;
2777
2778             /* Go backwards until we reach the start of the loop, a label,
2779                or a JUMP_INSN.  */
2780             for (p = PREV_INSN (insn);
2781                  GET_CODE (p) != CODE_LABEL
2782                  && ! (GET_CODE (p) == NOTE
2783                        && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2784                  && GET_CODE (p) != JUMP_INSN;
2785                  p = PREV_INSN (p))
2786               ;
2787
2788             /* Check for the case where we have a jump to an inner nested
2789                loop, and do not perform the optimization in that case.  */
2790
2791             if (JUMP_LABEL (insn))
2792               {
2793                 dest_loop = uid_loop[INSN_UID (JUMP_LABEL (insn))];
2794                 if (dest_loop)
2795                   {
2796                     for (outer_loop = dest_loop; outer_loop;
2797                          outer_loop = outer_loop->outer)
2798                       if (outer_loop == this_loop)
2799                         break;
2800                   }
2801               }
2802
2803             /* Make sure that the target of P is within the current loop.  */
2804
2805             if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2806                 && uid_loop[INSN_UID (JUMP_LABEL (p))] != this_loop)
2807               outer_loop = this_loop;
2808
2809             /* If we stopped on a JUMP_INSN to the next insn after INSN,
2810                we have a block of code to try to move.
2811
2812                We look backward and then forward from the target of INSN
2813                to find a BARRIER at the same loop depth as the target.
2814                If we find such a BARRIER, we make a new label for the start
2815                of the block, invert the jump in P and point it to that label,
2816                and move the block of code to the spot we found.  */
2817
2818             if (! outer_loop
2819                 && GET_CODE (p) == JUMP_INSN
2820                 && JUMP_LABEL (p) != 0
2821                 /* Just ignore jumps to labels that were never emitted.
2822                    These always indicate compilation errors.  */
2823                 && INSN_UID (JUMP_LABEL (p)) != 0
2824                 && any_condjump_p (p) && onlyjump_p (p)
2825                 && next_real_insn (JUMP_LABEL (p)) == our_next
2826                 /* If it's not safe to move the sequence, then we
2827                    mustn't try.  */
2828                 && insns_safe_to_move_p (p, NEXT_INSN (insn),
2829                                          &last_insn_to_move))
2830               {
2831                 rtx target
2832                   = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2833                 struct loop *target_loop = uid_loop[INSN_UID (target)];
2834                 rtx loc, loc2;
2835                 rtx tmp;
2836
2837                 /* Search for possible garbage past the conditional jumps
2838                    and look for the last barrier.  */
2839                 for (tmp = last_insn_to_move;
2840                      tmp && GET_CODE (tmp) != CODE_LABEL; tmp = NEXT_INSN (tmp))
2841                   if (GET_CODE (tmp) == BARRIER)
2842                     last_insn_to_move = tmp;
2843
2844                 for (loc = target; loc; loc = PREV_INSN (loc))
2845                   if (GET_CODE (loc) == BARRIER
2846                       /* Don't move things inside a tablejump.  */
2847                       && ((loc2 = next_nonnote_insn (loc)) == 0
2848                           || GET_CODE (loc2) != CODE_LABEL
2849                           || (loc2 = next_nonnote_insn (loc2)) == 0
2850                           || GET_CODE (loc2) != JUMP_INSN
2851                           || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2852                               && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2853                       && uid_loop[INSN_UID (loc)] == target_loop)
2854                     break;
2855
2856                 if (loc == 0)
2857                   for (loc = target; loc; loc = NEXT_INSN (loc))
2858                     if (GET_CODE (loc) == BARRIER
2859                         /* Don't move things inside a tablejump.  */
2860                         && ((loc2 = next_nonnote_insn (loc)) == 0
2861                             || GET_CODE (loc2) != CODE_LABEL
2862                             || (loc2 = next_nonnote_insn (loc2)) == 0
2863                             || GET_CODE (loc2) != JUMP_INSN
2864                             || (GET_CODE (PATTERN (loc2)) != ADDR_VEC
2865                                 && GET_CODE (PATTERN (loc2)) != ADDR_DIFF_VEC))
2866                         && uid_loop[INSN_UID (loc)] == target_loop)
2867                       break;
2868
2869                 if (loc)
2870                   {
2871                     rtx cond_label = JUMP_LABEL (p);
2872                     rtx new_label = get_label_after (p);
2873
2874                     /* Ensure our label doesn't go away.  */
2875                     LABEL_NUSES (cond_label)++;
2876
2877                     /* Verify that uid_loop is large enough and that
2878                        we can invert P.  */
2879                     if (invert_jump (p, new_label, 1))
2880                       {
2881                         rtx q, r;
2882
2883                         /* If no suitable BARRIER was found, create a suitable
2884                            one before TARGET.  Since TARGET is a fall through
2885                            path, we'll need to insert an jump around our block
2886                            and add a BARRIER before TARGET.
2887
2888                            This creates an extra unconditional jump outside
2889                            the loop.  However, the benefits of removing rarely
2890                            executed instructions from inside the loop usually
2891                            outweighs the cost of the extra unconditional jump
2892                            outside the loop.  */
2893                         if (loc == 0)
2894                           {
2895                             rtx temp;
2896
2897                             temp = gen_jump (JUMP_LABEL (insn));
2898                             temp = emit_jump_insn_before (temp, target);
2899                             JUMP_LABEL (temp) = JUMP_LABEL (insn);
2900                             LABEL_NUSES (JUMP_LABEL (insn))++;
2901                             loc = emit_barrier_before (target);
2902                           }
2903
2904                         /* Include the BARRIER after INSN and copy the
2905                            block after LOC.  */
2906                         if (squeeze_notes (&new_label, &last_insn_to_move))
2907                           abort ();
2908                         reorder_insns (new_label, last_insn_to_move, loc);
2909
2910                         /* All those insns are now in TARGET_LOOP.  */
2911                         for (q = new_label;
2912                              q != NEXT_INSN (last_insn_to_move);
2913                              q = NEXT_INSN (q))
2914                           uid_loop[INSN_UID (q)] = target_loop;
2915
2916                         /* The label jumped to by INSN is no longer a loop
2917                            exit.  Unless INSN does not have a label (e.g.,
2918                            it is a RETURN insn), search loop->exit_labels
2919                            to find its label_ref, and remove it.  Also turn
2920                            off LABEL_OUTSIDE_LOOP_P bit.  */
2921                         if (JUMP_LABEL (insn))
2922                           {
2923                             for (q = 0, r = this_loop->exit_labels;
2924                                  r;
2925                                  q = r, r = LABEL_NEXTREF (r))
2926                               if (XEXP (r, 0) == JUMP_LABEL (insn))
2927                                 {
2928                                   LABEL_OUTSIDE_LOOP_P (r) = 0;
2929                                   if (q)
2930                                     LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2931                                   else
2932                                     this_loop->exit_labels = LABEL_NEXTREF (r);
2933                                   break;
2934                                 }
2935
2936                             for (loop = this_loop; loop && loop != target_loop;
2937                                  loop = loop->outer)
2938                               loop->exit_count--;
2939
2940                             /* If we didn't find it, then something is
2941                                wrong.  */
2942                             if (! r)
2943                               abort ();
2944                           }
2945
2946                         /* P is now a jump outside the loop, so it must be put
2947                            in loop->exit_labels, and marked as such.
2948                            The easiest way to do this is to just call
2949                            mark_loop_jump again for P.  */
2950                         mark_loop_jump (PATTERN (p), this_loop);
2951
2952                         /* If INSN now jumps to the insn after it,
2953                            delete INSN.  */
2954                         if (JUMP_LABEL (insn) != 0
2955                             && (next_real_insn (JUMP_LABEL (insn))
2956                                 == next_real_insn (insn)))
2957                           delete_related_insns (insn);
2958                       }
2959
2960                     /* Continue the loop after where the conditional
2961                        branch used to jump, since the only branch insn
2962                        in the block (if it still remains) is an inter-loop
2963                        branch and hence needs no processing.  */
2964                     insn = NEXT_INSN (cond_label);
2965
2966                     if (--LABEL_NUSES (cond_label) == 0)
2967                       delete_related_insns (cond_label);
2968
2969                     /* This loop will be continued with NEXT_INSN (insn).  */
2970                     insn = PREV_INSN (insn);
2971                   }
2972               }
2973           }
2974       }
2975 }
2976
2977 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2978    loops it is contained in, mark the target loop invalid.
2979
2980    For speed, we assume that X is part of a pattern of a JUMP_INSN.  */
2981
2982 static void
2983 mark_loop_jump (x, loop)
2984      rtx x;
2985      struct loop *loop;
2986 {
2987   struct loop *dest_loop;
2988   struct loop *outer_loop;
2989   int i;
2990
2991   switch (GET_CODE (x))
2992     {
2993     case PC:
2994     case USE:
2995     case CLOBBER:
2996     case REG:
2997     case MEM:
2998     case CONST_INT:
2999     case CONST_DOUBLE:
3000     case RETURN:
3001       return;
3002
3003     case CONST:
3004       /* There could be a label reference in here.  */
3005       mark_loop_jump (XEXP (x, 0), loop);
3006       return;
3007
3008     case PLUS:
3009     case MINUS:
3010     case MULT:
3011       mark_loop_jump (XEXP (x, 0), loop);
3012       mark_loop_jump (XEXP (x, 1), loop);
3013       return;
3014
3015     case LO_SUM:
3016       /* This may refer to a LABEL_REF or SYMBOL_REF.  */
3017       mark_loop_jump (XEXP (x, 1), loop);
3018       return;
3019
3020     case SIGN_EXTEND:
3021     case ZERO_EXTEND:
3022       mark_loop_jump (XEXP (x, 0), loop);
3023       return;
3024
3025     case LABEL_REF:
3026       dest_loop = uid_loop[INSN_UID (XEXP (x, 0))];
3027
3028       /* Link together all labels that branch outside the loop.  This
3029          is used by final_[bg]iv_value and the loop unrolling code.  Also
3030          mark this LABEL_REF so we know that this branch should predict
3031          false.  */
3032
3033       /* A check to make sure the label is not in an inner nested loop,
3034          since this does not count as a loop exit.  */
3035       if (dest_loop)
3036         {
3037           for (outer_loop = dest_loop; outer_loop;
3038                outer_loop = outer_loop->outer)
3039             if (outer_loop == loop)
3040               break;
3041         }
3042       else
3043         outer_loop = NULL;
3044
3045       if (loop && ! outer_loop)
3046         {
3047           LABEL_OUTSIDE_LOOP_P (x) = 1;
3048           LABEL_NEXTREF (x) = loop->exit_labels;
3049           loop->exit_labels = x;
3050
3051           for (outer_loop = loop;
3052                outer_loop && outer_loop != dest_loop;
3053                outer_loop = outer_loop->outer)
3054             outer_loop->exit_count++;
3055         }
3056
3057       /* If this is inside a loop, but not in the current loop or one enclosed
3058          by it, it invalidates at least one loop.  */
3059
3060       if (! dest_loop)
3061         return;
3062
3063       /* We must invalidate every nested loop containing the target of this
3064          label, except those that also contain the jump insn.  */
3065
3066       for (; dest_loop; dest_loop = dest_loop->outer)
3067         {
3068           /* Stop when we reach a loop that also contains the jump insn.  */
3069           for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3070             if (dest_loop == outer_loop)
3071               return;
3072
3073           /* If we get here, we know we need to invalidate a loop.  */
3074           if (loop_dump_stream && ! dest_loop->invalid)
3075             fprintf (loop_dump_stream,
3076                      "\nLoop at %d ignored due to multiple entry points.\n",
3077                      INSN_UID (dest_loop->start));
3078
3079           dest_loop->invalid = 1;
3080         }
3081       return;
3082
3083     case SET:
3084       /* If this is not setting pc, ignore.  */
3085       if (SET_DEST (x) == pc_rtx)
3086         mark_loop_jump (SET_SRC (x), loop);
3087       return;
3088
3089     case IF_THEN_ELSE:
3090       mark_loop_jump (XEXP (x, 1), loop);
3091       mark_loop_jump (XEXP (x, 2), loop);
3092       return;
3093
3094     case PARALLEL:
3095     case ADDR_VEC:
3096       for (i = 0; i < XVECLEN (x, 0); i++)
3097         mark_loop_jump (XVECEXP (x, 0, i), loop);
3098       return;
3099
3100     case ADDR_DIFF_VEC:
3101       for (i = 0; i < XVECLEN (x, 1); i++)
3102         mark_loop_jump (XVECEXP (x, 1, i), loop);
3103       return;
3104
3105     default:
3106       /* Strictly speaking this is not a jump into the loop, only a possible
3107          jump out of the loop.  However, we have no way to link the destination
3108          of this jump onto the list of exit labels.  To be safe we mark this
3109          loop and any containing loops as invalid.  */
3110       if (loop)
3111         {
3112           for (outer_loop = loop; outer_loop; outer_loop = outer_loop->outer)
3113             {
3114               if (loop_dump_stream && ! outer_loop->invalid)
3115                 fprintf (loop_dump_stream,
3116                          "\nLoop at %d ignored due to unknown exit jump.\n",
3117                          INSN_UID (outer_loop->start));
3118               outer_loop->invalid = 1;
3119             }
3120         }
3121       return;
3122     }
3123 }
3124 \f
3125 /* Return nonzero if there is a label in the range from
3126    insn INSN to and including the insn whose luid is END
3127    INSN must have an assigned luid (i.e., it must not have
3128    been previously created by loop.c).  */
3129
3130 static int
3131 labels_in_range_p (insn, end)
3132      rtx insn;
3133      int end;
3134 {
3135   while (insn && INSN_LUID (insn) <= end)
3136     {
3137       if (GET_CODE (insn) == CODE_LABEL)
3138         return 1;
3139       insn = NEXT_INSN (insn);
3140     }
3141
3142   return 0;
3143 }
3144
3145 /* Record that a memory reference X is being set.  */
3146
3147 static void
3148 note_addr_stored (x, y, data)
3149      rtx x;
3150      rtx y ATTRIBUTE_UNUSED;
3151      void *data ATTRIBUTE_UNUSED;
3152 {
3153   struct loop_info *loop_info = data;
3154
3155   if (x == 0 || GET_CODE (x) != MEM)
3156     return;
3157
3158   /* Count number of memory writes.
3159      This affects heuristics in strength_reduce.  */
3160   loop_info->num_mem_sets++;
3161
3162   /* BLKmode MEM means all memory is clobbered.  */
3163   if (GET_MODE (x) == BLKmode)
3164     {
3165       if (RTX_UNCHANGING_P (x))
3166         loop_info->unknown_constant_address_altered = 1;
3167       else
3168         loop_info->unknown_address_altered = 1;
3169
3170       return;
3171     }
3172
3173   loop_info->store_mems = gen_rtx_EXPR_LIST (VOIDmode, x,
3174                                              loop_info->store_mems);
3175 }
3176
3177 /* X is a value modified by an INSN that references a biv inside a loop
3178    exit test (ie, X is somehow related to the value of the biv).  If X
3179    is a pseudo that is used more than once, then the biv is (effectively)
3180    used more than once.  DATA is a pointer to a loop_regs structure.  */
3181
3182 static void
3183 note_set_pseudo_multiple_uses (x, y, data)
3184      rtx x;
3185      rtx y ATTRIBUTE_UNUSED;
3186      void *data;
3187 {
3188   struct loop_regs *regs = (struct loop_regs *) data;
3189
3190   if (x == 0)
3191     return;
3192
3193   while (GET_CODE (x) == STRICT_LOW_PART
3194          || GET_CODE (x) == SIGN_EXTRACT
3195          || GET_CODE (x) == ZERO_EXTRACT
3196          || GET_CODE (x) == SUBREG)
3197     x = XEXP (x, 0);
3198
3199   if (GET_CODE (x) != REG || REGNO (x) < FIRST_PSEUDO_REGISTER)
3200     return;
3201
3202   /* If we do not have usage information, or if we know the register
3203      is used more than once, note that fact for check_dbra_loop.  */
3204   if (REGNO (x) >= max_reg_before_loop
3205       || ! regs->array[REGNO (x)].single_usage
3206       || regs->array[REGNO (x)].single_usage == const0_rtx)
3207     regs->multiple_uses = 1;
3208 }
3209 \f
3210 /* Return nonzero if the rtx X is invariant over the current loop.
3211
3212    The value is 2 if we refer to something only conditionally invariant.
3213
3214    A memory ref is invariant if it is not volatile and does not conflict
3215    with anything stored in `loop_info->store_mems'.  */
3216
3217 int
3218 loop_invariant_p (loop, x)
3219      const struct loop *loop;
3220      rtx x;
3221 {
3222   struct loop_info *loop_info = LOOP_INFO (loop);
3223   struct loop_regs *regs = LOOP_REGS (loop);
3224   int i;
3225   enum rtx_code code;
3226   const char *fmt;
3227   int conditional = 0;
3228   rtx mem_list_entry;
3229
3230   if (x == 0)
3231     return 1;
3232   code = GET_CODE (x);
3233   switch (code)
3234     {
3235     case CONST_INT:
3236     case CONST_DOUBLE:
3237     case SYMBOL_REF:
3238     case CONST:
3239       return 1;
3240
3241     case LABEL_REF:
3242       /* A LABEL_REF is normally invariant, however, if we are unrolling
3243          loops, and this label is inside the loop, then it isn't invariant.
3244          This is because each unrolled copy of the loop body will have
3245          a copy of this label.  If this was invariant, then an insn loading
3246          the address of this label into a register might get moved outside
3247          the loop, and then each loop body would end up using the same label.
3248
3249          We don't know the loop bounds here though, so just fail for all
3250          labels.  */
3251       if (flag_unroll_loops)
3252         return 0;
3253       else
3254         return 1;
3255
3256     case PC:
3257     case CC0:
3258     case UNSPEC_VOLATILE:
3259       return 0;
3260
3261     case REG:
3262       /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
3263          since the reg might be set by initialization within the loop.  */
3264
3265       if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3266            || x == arg_pointer_rtx || x == pic_offset_table_rtx)
3267           && ! current_function_has_nonlocal_goto)
3268         return 1;
3269
3270       if (LOOP_INFO (loop)->has_call
3271           && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
3272         return 0;
3273
3274       if (regs->array[REGNO (x)].set_in_loop < 0)
3275         return 2;
3276
3277       return regs->array[REGNO (x)].set_in_loop == 0;
3278
3279     case MEM:
3280       /* Volatile memory references must be rejected.  Do this before
3281          checking for read-only items, so that volatile read-only items
3282          will be rejected also.  */
3283       if (MEM_VOLATILE_P (x))
3284         return 0;
3285
3286       /* See if there is any dependence between a store and this load.  */
3287       mem_list_entry = loop_info->store_mems;
3288       while (mem_list_entry)
3289         {
3290           if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
3291                                x, rtx_varies_p))
3292             return 0;
3293
3294           mem_list_entry = XEXP (mem_list_entry, 1);
3295         }
3296
3297       /* It's not invalidated by a store in memory
3298          but we must still verify the address is invariant.  */
3299       break;
3300
3301     case ASM_OPERANDS:
3302       /* Don't mess with insns declared volatile.  */
3303       if (MEM_VOLATILE_P (x))
3304         return 0;
3305       break;
3306
3307     default:
3308       break;
3309     }
3310
3311   fmt = GET_RTX_FORMAT (code);
3312   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3313     {
3314       if (fmt[i] == 'e')
3315         {
3316           int tem = loop_invariant_p (loop, XEXP (x, i));
3317           if (tem == 0)
3318             return 0;
3319           if (tem == 2)
3320             conditional = 1;
3321         }
3322       else if (fmt[i] == 'E')
3323         {
3324           int j;
3325           for (j = 0; j < XVECLEN (x, i); j++)
3326             {
3327               int tem = loop_invariant_p (loop, XVECEXP (x, i, j));
3328               if (tem == 0)
3329                 return 0;
3330               if (tem == 2)
3331                 conditional = 1;
3332             }
3333
3334         }
3335     }
3336
3337   return 1 + conditional;
3338 }
3339 \f
3340 /* Return nonzero if all the insns in the loop that set REG
3341    are INSN and the immediately following insns,
3342    and if each of those insns sets REG in an invariant way
3343    (not counting uses of REG in them).
3344
3345    The value is 2 if some of these insns are only conditionally invariant.
3346
3347    We assume that INSN itself is the first set of REG
3348    and that its source is invariant.  */
3349
3350 static int
3351 consec_sets_invariant_p (loop, reg, n_sets, insn)
3352      const struct loop *loop;
3353      int n_sets;
3354      rtx reg, insn;
3355 {
3356   struct loop_regs *regs = LOOP_REGS (loop);
3357   rtx p = insn;
3358   unsigned int regno = REGNO (reg);
3359   rtx temp;
3360   /* Number of sets we have to insist on finding after INSN.  */
3361   int count = n_sets - 1;
3362   int old = regs->array[regno].set_in_loop;
3363   int value = 0;
3364   int this;
3365
3366   /* If N_SETS hit the limit, we can't rely on its value.  */
3367   if (n_sets == 127)
3368     return 0;
3369
3370   regs->array[regno].set_in_loop = 0;
3371
3372   while (count > 0)
3373     {
3374       enum rtx_code code;
3375       rtx set;
3376
3377       p = NEXT_INSN (p);
3378       code = GET_CODE (p);
3379
3380       /* If library call, skip to end of it.  */
3381       if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3382         p = XEXP (temp, 0);
3383
3384       this = 0;
3385       if (code == INSN
3386           && (set = single_set (p))
3387           && GET_CODE (SET_DEST (set)) == REG
3388           && REGNO (SET_DEST (set)) == regno)
3389         {
3390           this = loop_invariant_p (loop, SET_SRC (set));
3391           if (this != 0)
3392             value |= this;
3393           else if ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX)))
3394             {
3395               /* If this is a libcall, then any invariant REG_EQUAL note is OK.
3396                  If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
3397                  notes are OK.  */
3398               this = (CONSTANT_P (XEXP (temp, 0))
3399                       || (find_reg_note (p, REG_RETVAL, NULL_RTX)
3400                           && loop_invariant_p (loop, XEXP (temp, 0))));
3401               if (this != 0)
3402                 value |= this;
3403             }
3404         }
3405       if (this != 0)
3406         count--;
3407       else if (code != NOTE)
3408         {
3409           regs->array[regno].set_in_loop = old;
3410           return 0;
3411         }
3412     }
3413
3414   regs->array[regno].set_in_loop = old;
3415   /* If loop_invariant_p ever returned 2, we return 2.  */
3416   return 1 + (value & 2);
3417 }
3418
3419 #if 0
3420 /* I don't think this condition is sufficient to allow INSN
3421    to be moved, so we no longer test it.  */
3422
3423 /* Return 1 if all insns in the basic block of INSN and following INSN
3424    that set REG are invariant according to TABLE.  */
3425
3426 static int
3427 all_sets_invariant_p (reg, insn, table)
3428      rtx reg, insn;
3429      short *table;
3430 {
3431   rtx p = insn;
3432   int regno = REGNO (reg);
3433
3434   while (1)
3435     {
3436       enum rtx_code code;
3437       p = NEXT_INSN (p);
3438       code = GET_CODE (p);
3439       if (code == CODE_LABEL || code == JUMP_INSN)
3440         return 1;
3441       if (code == INSN && GET_CODE (PATTERN (p)) == SET
3442           && GET_CODE (SET_DEST (PATTERN (p))) == REG
3443           && REGNO (SET_DEST (PATTERN (p))) == regno)
3444         {
3445           if (! loop_invariant_p (loop, SET_SRC (PATTERN (p)), table))
3446             return 0;
3447         }
3448     }
3449 }
3450 #endif /* 0 */
3451 \f
3452 /* Look at all uses (not sets) of registers in X.  For each, if it is
3453    the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3454    a different insn, set USAGE[REGNO] to const0_rtx.  */
3455
3456 static void
3457 find_single_use_in_loop (regs, insn, x)
3458      struct loop_regs *regs;
3459      rtx insn;
3460      rtx x;
3461 {
3462   enum rtx_code code = GET_CODE (x);
3463   const char *fmt = GET_RTX_FORMAT (code);
3464   int i, j;
3465
3466   if (code == REG)
3467     regs->array[REGNO (x)].single_usage
3468       = (regs->array[REGNO (x)].single_usage != 0
3469          && regs->array[REGNO (x)].single_usage != insn)
3470         ? const0_rtx : insn;
3471
3472   else if (code == SET)
3473     {
3474       /* Don't count SET_DEST if it is a REG; otherwise count things
3475          in SET_DEST because if a register is partially modified, it won't
3476          show up as a potential movable so we don't care how USAGE is set
3477          for it.  */
3478       if (GET_CODE (SET_DEST (x)) != REG)
3479         find_single_use_in_loop (regs, insn, SET_DEST (x));
3480       find_single_use_in_loop (regs, insn, SET_SRC (x));
3481     }
3482   else
3483     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3484       {
3485         if (fmt[i] == 'e' && XEXP (x, i) != 0)
3486           find_single_use_in_loop (regs, insn, XEXP (x, i));
3487         else if (fmt[i] == 'E')
3488           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3489             find_single_use_in_loop (regs, insn, XVECEXP (x, i, j));
3490       }
3491 }
3492 \f
3493 /* Count and record any set in X which is contained in INSN.  Update
3494    REGS->array[I].MAY_NOT_OPTIMIZE and LAST_SET for any register I set
3495    in X.  */
3496
3497 static void
3498 count_one_set (regs, insn, x, last_set)
3499      struct loop_regs *regs;
3500      rtx insn, x;
3501      rtx *last_set;
3502 {
3503   if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3504     /* Don't move a reg that has an explicit clobber.
3505        It's not worth the pain to try to do it correctly.  */
3506     regs->array[REGNO (XEXP (x, 0))].may_not_optimize = 1;
3507
3508   if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3509     {
3510       rtx dest = SET_DEST (x);
3511       while (GET_CODE (dest) == SUBREG
3512              || GET_CODE (dest) == ZERO_EXTRACT
3513              || GET_CODE (dest) == SIGN_EXTRACT
3514              || GET_CODE (dest) == STRICT_LOW_PART)
3515         dest = XEXP (dest, 0);
3516       if (GET_CODE (dest) == REG)
3517         {
3518           int i;
3519           int regno = REGNO (dest);
3520           for (i = 0; i < LOOP_REGNO_NREGS (regno, dest); i++)
3521             {
3522               /* If this is the first setting of this reg
3523                  in current basic block, and it was set before,
3524                  it must be set in two basic blocks, so it cannot
3525                  be moved out of the loop.  */
3526               if (regs->array[regno].set_in_loop > 0
3527                   && last_set == 0)
3528                 regs->array[regno+i].may_not_optimize = 1;
3529               /* If this is not first setting in current basic block,
3530                  see if reg was used in between previous one and this.
3531                  If so, neither one can be moved.  */
3532               if (last_set[regno] != 0
3533                   && reg_used_between_p (dest, last_set[regno], insn))
3534                 regs->array[regno+i].may_not_optimize = 1;
3535               if (regs->array[regno+i].set_in_loop < 127)
3536                 ++regs->array[regno+i].set_in_loop;
3537               last_set[regno+i] = insn;
3538             }
3539         }
3540     }
3541 }
3542 \f
3543 /* Given a loop that is bounded by LOOP->START and LOOP->END and that
3544    is entered at LOOP->SCAN_START, return 1 if the register set in SET
3545    contained in insn INSN is used by any insn that precedes INSN in
3546    cyclic order starting from the loop entry point.
3547
3548    We don't want to use INSN_LUID here because if we restrict INSN to those
3549    that have a valid INSN_LUID, it means we cannot move an invariant out
3550    from an inner loop past two loops.  */
3551
3552 static int
3553 loop_reg_used_before_p (loop, set, insn)
3554      const struct loop *loop;
3555      rtx set, insn;
3556 {
3557   rtx reg = SET_DEST (set);
3558   rtx p;
3559
3560   /* Scan forward checking for register usage.  If we hit INSN, we
3561      are done.  Otherwise, if we hit LOOP->END, wrap around to LOOP->START.  */
3562   for (p = loop->scan_start; p != insn; p = NEXT_INSN (p))
3563     {
3564       if (INSN_P (p) && reg_overlap_mentioned_p (reg, PATTERN (p)))
3565         return 1;
3566
3567       if (p == loop->end)
3568         p = loop->start;
3569     }
3570
3571   return 0;
3572 }
3573 \f
3574
3575 /* Information we collect about arrays that we might want to prefetch.  */
3576 struct prefetch_info
3577 {
3578   struct iv_class *class;       /* Class this prefetch is based on.  */
3579   struct induction *giv;        /* GIV this prefetch is based on.  */
3580   rtx base_address;             /* Start prefetching from this address plus
3581                                    index.  */
3582   HOST_WIDE_INT index;
3583   HOST_WIDE_INT stride;         /* Prefetch stride in bytes in each
3584                                    iteration.  */
3585   unsigned int bytes_accessed;  /* Sum of sizes of all acceses to this
3586                                    prefetch area in one iteration.  */
3587   unsigned int total_bytes;     /* Total bytes loop will access in this block.
3588                                    This is set only for loops with known
3589                                    iteration counts and is 0xffffffff
3590                                    otherwise.  */
3591   int prefetch_in_loop;         /* Number of prefetch insns in loop.  */
3592   int prefetch_before_loop;     /* Number of prefetch insns before loop.  */
3593   unsigned int write : 1;       /* 1 for read/write prefetches.  */
3594 };
3595
3596 /* Data used by check_store function.  */
3597 struct check_store_data
3598 {
3599   rtx mem_address;
3600   int mem_write;
3601 };
3602
3603 static void check_store PARAMS ((rtx, rtx, void *));
3604 static void emit_prefetch_instructions PARAMS ((struct loop *));
3605 static int rtx_equal_for_prefetch_p PARAMS ((rtx, rtx));
3606
3607 /* Set mem_write when mem_address is found.  Used as callback to
3608    note_stores.  */
3609 static void
3610 check_store (x, pat, data)
3611      rtx x, pat ATTRIBUTE_UNUSED;
3612      void *data;
3613 {
3614   struct check_store_data *d = (struct check_store_data *) data;
3615
3616   if ((GET_CODE (x) == MEM) && rtx_equal_p (d->mem_address, XEXP (x, 0)))
3617     d->mem_write = 1;
3618 }
3619 \f
3620 /* Like rtx_equal_p, but attempts to swap commutative operands.  This is
3621    important to get some addresses combined.  Later more sophisticated
3622    transformations can be added when necesary.
3623
3624    ??? Same trick with swapping operand is done at several other places.
3625    It can be nice to develop some common way to handle this.  */
3626
3627 static int
3628 rtx_equal_for_prefetch_p (x, y)
3629      rtx x, y;
3630 {
3631   int i;
3632   int j;
3633   enum rtx_code code = GET_CODE (x);
3634   const char *fmt;
3635
3636   if (x == y)
3637     return 1;
3638   if (code != GET_CODE (y))
3639     return 0;
3640
3641   code = GET_CODE (x);
3642
3643   if (GET_RTX_CLASS (code) == 'c')
3644     {
3645       return ((rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 0))
3646                && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 1)))
3647               || (rtx_equal_for_prefetch_p (XEXP (x, 0), XEXP (y, 1))
3648                   && rtx_equal_for_prefetch_p (XEXP (x, 1), XEXP (y, 0))));
3649     }
3650   /* Compare the elements.  If any pair of corresponding elements fails to
3651      match, return 0 for the whole thing.  */
3652
3653   fmt = GET_RTX_FORMAT (code);
3654   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3655     {
3656       switch (fmt[i])
3657         {
3658         case 'w':
3659           if (XWINT (x, i) != XWINT (y, i))
3660             return 0;
3661           break;
3662
3663         case 'i':
3664           if (XINT (x, i) != XINT (y, i))
3665             return 0;
3666           break;
3667
3668         case 'E':
3669           /* Two vectors must have the same length.  */
3670           if (XVECLEN (x, i) != XVECLEN (y, i))
3671             return 0;
3672
3673           /* And the corresponding elements must match.  */
3674           for (j = 0; j < XVECLEN (x, i); j++)
3675             if (rtx_equal_for_prefetch_p (XVECEXP (x, i, j),
3676                                           XVECEXP (y, i, j)) == 0)
3677               return 0;
3678           break;
3679
3680         case 'e':
3681           if (rtx_equal_for_prefetch_p (XEXP (x, i), XEXP (y, i)) == 0)
3682             return 0;
3683           break;
3684
3685         case 's':
3686           if (strcmp (XSTR (x, i), XSTR (y, i)))
3687             return 0;
3688           break;
3689
3690         case 'u':
3691           /* These are just backpointers, so they don't matter.  */
3692           break;
3693
3694         case '0':
3695           break;
3696
3697           /* It is believed that rtx's at this level will never
3698              contain anything but integers and other rtx's,
3699              except for within LABEL_REFs and SYMBOL_REFs.  */
3700         default:
3701           abort ();
3702         }
3703     }
3704   return 1;
3705 }
3706 \f
3707 /* Remove constant addition value from the expression X (when present)
3708    and return it.  */
3709
3710 static HOST_WIDE_INT
3711 remove_constant_addition (x)
3712      rtx *x;
3713 {
3714   HOST_WIDE_INT addval = 0;
3715   rtx exp = *x;
3716
3717   /* Avoid clobbering a shared CONST expression.  */
3718   if (GET_CODE (exp) == CONST)
3719     {
3720       if (GET_CODE (XEXP (exp, 0)) == PLUS
3721           && GET_CODE (XEXP (XEXP (exp, 0), 0)) == SYMBOL_REF
3722           && GET_CODE (XEXP (XEXP (exp, 0), 1)) == CONST_INT)
3723         {
3724           *x = XEXP (XEXP (exp, 0), 0);
3725           return INTVAL (XEXP (XEXP (exp, 0), 1));
3726         }
3727       return 0;
3728     }
3729
3730   if (GET_CODE (exp) == CONST_INT)
3731     {
3732       addval = INTVAL (exp);
3733       *x = const0_rtx;
3734     }
3735
3736   /* For plus expression recurse on ourself.  */
3737   else if (GET_CODE (exp) == PLUS)
3738     {
3739       addval += remove_constant_addition (&XEXP (exp, 0));
3740       addval += remove_constant_addition (&XEXP (exp, 1));
3741
3742       /* In case our parameter was constant, remove extra zero from the
3743          expression.  */
3744       if (XEXP (exp, 0) == const0_rtx)
3745         *x = XEXP (exp, 1);
3746       else if (XEXP (exp, 1) == const0_rtx)
3747         *x = XEXP (exp, 0);
3748     }
3749
3750   return addval;
3751 }
3752
3753 /* Attempt to identify accesses to arrays that are most likely to cause cache
3754    misses, and emit prefetch instructions a few prefetch blocks forward.
3755
3756    To detect the arrays we use the GIV information that was collected by the
3757    strength reduction pass.
3758
3759    The prefetch instructions are generated after the GIV information is done
3760    and before the strength reduction process. The new GIVs are injected into
3761    the strength reduction tables, so the prefetch addresses are optimized as
3762    well.
3763
3764    GIVs are split into base address, stride, and constant addition values.
3765    GIVs with the same address, stride and close addition values are combined
3766    into a single prefetch.  Also writes to GIVs are detected, so that prefetch
3767    for write instructions can be used for the block we write to, on machines
3768    that support write prefetches.
3769
3770    Several heuristics are used to determine when to prefetch.  They are
3771    controlled by defined symbols that can be overridden for each target.  */
3772
3773 static void
3774 emit_prefetch_instructions (loop)
3775      struct loop *loop;
3776 {
3777   int num_prefetches = 0;
3778   int num_real_prefetches = 0;
3779   int num_real_write_prefetches = 0;
3780   int num_prefetches_before = 0;
3781   int num_write_prefetches_before = 0;
3782   int ahead = 0;
3783   int i;
3784   struct iv_class *bl;
3785   struct induction *iv;
3786   struct prefetch_info info[MAX_PREFETCHES];
3787   struct loop_ivs *ivs = LOOP_IVS (loop);
3788
3789   if (!HAVE_prefetch)
3790     return;
3791
3792   /* Consider only loops w/o calls.  When a call is done, the loop is probably
3793      slow enough to read the memory.  */
3794   if (PREFETCH_NO_CALL && LOOP_INFO (loop)->has_call)
3795     {
3796       if (loop_dump_stream)
3797         fprintf (loop_dump_stream, "Prefetch: ignoring loop: has call.\n");
3798
3799       return;
3800     }
3801
3802   /* Don't prefetch in loops known to have few iterations.  */
3803   if (PREFETCH_NO_LOW_LOOPCNT
3804       && LOOP_INFO (loop)->n_iterations
3805       && LOOP_INFO (loop)->n_iterations <= PREFETCH_LOW_LOOPCNT)
3806     {
3807       if (loop_dump_stream)
3808         fprintf (loop_dump_stream,
3809                  "Prefetch: ignoring loop: not enough iterations.\n");
3810       return;
3811     }
3812
3813   /* Search all induction variables and pick those interesting for the prefetch
3814      machinery.  */
3815   for (bl = ivs->list; bl; bl = bl->next)
3816     {
3817       struct induction *biv = bl->biv, *biv1;
3818       int basestride = 0;
3819
3820       biv1 = biv;
3821
3822       /* Expect all BIVs to be executed in each iteration.  This makes our
3823          analysis more conservative.  */
3824       while (biv1)
3825         {
3826           /* Discard non-constant additions that we can't handle well yet, and
3827              BIVs that are executed multiple times; such BIVs ought to be
3828              handled in the nested loop.  We accept not_every_iteration BIVs,
3829              since these only result in larger strides and make our
3830              heuristics more conservative.  */
3831           if (GET_CODE (biv->add_val) != CONST_INT)
3832             {
3833               if (loop_dump_stream)
3834                 {
3835                   fprintf (loop_dump_stream,
3836                     "Prefetch: ignoring biv %d: non-constant addition at insn %d:",
3837                            REGNO (biv->src_reg), INSN_UID (biv->insn));
3838                   print_rtl (loop_dump_stream, biv->add_val);
3839                   fprintf (loop_dump_stream, "\n");
3840                 }
3841               break;
3842             }
3843
3844           if (biv->maybe_multiple)
3845             {
3846               if (loop_dump_stream)
3847                 {
3848                   fprintf (loop_dump_stream,
3849                            "Prefetch: ignoring biv %d: maybe_multiple at insn %i:",
3850                            REGNO (biv->src_reg), INSN_UID (biv->insn));
3851                   print_rtl (loop_dump_stream, biv->add_val);
3852                   fprintf (loop_dump_stream, "\n");
3853                 }
3854               break;
3855             }
3856
3857           basestride += INTVAL (biv1->add_val);
3858           biv1 = biv1->next_iv;
3859         }
3860
3861       if (biv1 || !basestride)
3862         continue;
3863
3864       for (iv = bl->giv; iv; iv = iv->next_iv)
3865         {
3866           rtx address;
3867           rtx temp;
3868           HOST_WIDE_INT index = 0;
3869           int add = 1;
3870           HOST_WIDE_INT stride = 0;
3871           int stride_sign = 1;
3872           struct check_store_data d;
3873           const char *ignore_reason = NULL;
3874           int size = GET_MODE_SIZE (GET_MODE (iv));
3875
3876           /* See whether an induction variable is interesting to us and if
3877              not, report the reason.  */
3878           if (iv->giv_type != DEST_ADDR)
3879             ignore_reason = "giv is not a destination address";
3880
3881           /* We are interested only in constant stride memory references
3882              in order to be able to compute density easily.  */
3883           else if (GET_CODE (iv->mult_val) != CONST_INT)
3884             ignore_reason = "stride is not constant";
3885
3886           else
3887             {
3888               stride = INTVAL (iv->mult_val) * basestride;
3889               if (stride < 0)
3890                 {
3891                   stride = -stride;
3892                   stride_sign = -1;
3893                 }
3894
3895               /* On some targets, reversed order prefetches are not
3896                  worthwhile.  */
3897               if (PREFETCH_NO_REVERSE_ORDER && stride_sign < 0)
3898                 ignore_reason = "reversed order stride";
3899
3900               /* Prefetch of accesses with an extreme stride might not be
3901                  worthwhile, either.  */
3902               else if (PREFETCH_NO_EXTREME_STRIDE
3903                        && stride > PREFETCH_EXTREME_STRIDE)
3904                 ignore_reason = "extreme stride";
3905
3906               /* Ignore GIVs with varying add values; we can't predict the
3907                  value for the next iteration.  */
3908               else if (!loop_invariant_p (loop, iv->add_val))
3909                 ignore_reason = "giv has varying add value";
3910
3911               /* Ignore GIVs in the nested loops; they ought to have been
3912                  handled already.  */
3913               else if (iv->maybe_multiple)
3914                 ignore_reason = "giv is in nested loop";
3915             }
3916
3917           if (ignore_reason != NULL)
3918             {
3919               if (loop_dump_stream)
3920                 fprintf (loop_dump_stream,
3921                          "Prefetch: ignoring giv at %d: %s.\n",
3922                          INSN_UID (iv->insn), ignore_reason);
3923               continue;
3924             }
3925
3926           /* Determine the pointer to the basic array we are examining.  It is
3927              the sum of the BIV's initial value and the GIV's add_val.  */
3928           address = copy_rtx (iv->add_val);
3929           temp = copy_rtx (bl->initial_value);
3930
3931           address = simplify_gen_binary (PLUS, Pmode, temp, address);
3932           index = remove_constant_addition (&address);
3933
3934           d.mem_write = 0;
3935           d.mem_address = *iv->location;
3936
3937           /* When the GIV is not always executed, we might be better off by
3938              not dirtying the cache pages.  */
3939           if (PREFETCH_CONDITIONAL || iv->always_executed)
3940             note_stores (PATTERN (iv->insn), check_store, &d);
3941           else
3942             {
3943               if (loop_dump_stream)
3944                 fprintf (loop_dump_stream, "Prefetch: Ignoring giv at %d: %s\n",
3945                          INSN_UID (iv->insn), "in conditional code.");
3946               continue;
3947             }
3948
3949           /* Attempt to find another prefetch to the same array and see if we
3950              can merge this one.  */
3951           for (i = 0; i < num_prefetches; i++)
3952             if (rtx_equal_for_prefetch_p (address, info[i].base_address)
3953                 && stride == info[i].stride)
3954               {
3955                 /* In case both access same array (same location
3956                    just with small difference in constant indexes), merge
3957                    the prefetches.  Just do the later and the earlier will
3958                    get prefetched from previous iteration.
3959                    The artificial threshold should not be too small,
3960                    but also not bigger than small portion of memory usually
3961                    traversed by single loop.  */
3962                 if (index >= info[i].index
3963                     && index - info[i].index < PREFETCH_EXTREME_DIFFERENCE)
3964                   {
3965                     info[i].write |= d.mem_write;
3966                     info[i].bytes_accessed += size;
3967                     info[i].index = index;
3968                     info[i].giv = iv;
3969                     info[i].class = bl;
3970                     info[num_prefetches].base_address = address;
3971                     add = 0;
3972                     break;
3973                   }
3974
3975                 if (index < info[i].index
3976                     && info[i].index - index < PREFETCH_EXTREME_DIFFERENCE)
3977                   {
3978                     info[i].write |= d.mem_write;
3979                     info[i].bytes_accessed += size;
3980                     add = 0;
3981                     break;
3982                   }
3983               }
3984
3985           /* Merging failed.  */
3986           if (add)
3987             {
3988               info[num_prefetches].giv = iv;
3989               info[num_prefetches].class = bl;
3990               info[num_prefetches].index = index;
3991               info[num_prefetches].stride = stride;
3992               info[num_prefetches].base_address = address;
3993               info[num_prefetches].write = d.mem_write;
3994               info[num_prefetches].bytes_accessed = size;
3995               num_prefetches++;
3996               if (num_prefetches >= MAX_PREFETCHES)
3997                 {
3998                   if (loop_dump_stream)
3999                     fprintf (loop_dump_stream,
4000                              "Maximal number of prefetches exceeded.\n");
4001                   return;
4002                 }
4003             }
4004         }
4005     }
4006
4007   for (i = 0; i < num_prefetches; i++)
4008     {
4009       int density;
4010
4011       /* Attempt to calculate the total number of bytes fetched by all
4012          iterations of the loop.  Avoid overflow.  */
4013       if (LOOP_INFO (loop)->n_iterations
4014           && ((unsigned HOST_WIDE_INT) (0xffffffff / info[i].stride)
4015               >= LOOP_INFO (loop)->n_iterations))
4016         info[i].total_bytes = info[i].stride * LOOP_INFO (loop)->n_iterations;
4017       else
4018         info[i].total_bytes = 0xffffffff;
4019
4020       density = info[i].bytes_accessed * 100 / info[i].stride;
4021
4022       /* Prefetch might be worthwhile only when the loads/stores are dense.  */
4023       if (PREFETCH_ONLY_DENSE_MEM)
4024         if (density * 256 > PREFETCH_DENSE_MEM * 100
4025             && (info[i].total_bytes / PREFETCH_BLOCK
4026                 >= PREFETCH_BLOCKS_BEFORE_LOOP_MIN))
4027           {
4028             info[i].prefetch_before_loop = 1;
4029             info[i].prefetch_in_loop
4030               = (info[i].total_bytes / PREFETCH_BLOCK
4031                  > PREFETCH_BLOCKS_BEFORE_LOOP_MAX);
4032           }
4033         else
4034           {
4035             info[i].prefetch_in_loop = 0, info[i].prefetch_before_loop = 0;
4036             if (loop_dump_stream)
4037               fprintf (loop_dump_stream,
4038                   "Prefetch: ignoring giv at %d: %d%% density is too low.\n",
4039                        INSN_UID (info[i].giv->insn), density);
4040           }
4041       else
4042         info[i].prefetch_in_loop = 1, info[i].prefetch_before_loop = 1;
4043
4044       /* Find how many prefetch instructions we'll use within the loop.  */
4045       if (info[i].prefetch_in_loop != 0)
4046         {
4047           info[i].prefetch_in_loop = ((info[i].stride + PREFETCH_BLOCK - 1)
4048                                   / PREFETCH_BLOCK);
4049           num_real_prefetches += info[i].prefetch_in_loop;
4050           if (info[i].write)
4051             num_real_write_prefetches += info[i].prefetch_in_loop;
4052         }
4053     }
4054
4055   /* Determine how many iterations ahead to prefetch within the loop, based
4056      on how many prefetches we currently expect to do within the loop.  */
4057   if (num_real_prefetches != 0)
4058     {
4059       if ((ahead = SIMULTANEOUS_PREFETCHES / num_real_prefetches) == 0)
4060         {
4061           if (loop_dump_stream)
4062             fprintf (loop_dump_stream,
4063                      "Prefetch: ignoring prefetches within loop: ahead is zero; %d < %d\n",
4064                      SIMULTANEOUS_PREFETCHES, num_real_prefetches);
4065           num_real_prefetches = 0, num_real_write_prefetches = 0;
4066         }
4067     }
4068   /* We'll also use AHEAD to determine how many prefetch instructions to
4069      emit before a loop, so don't leave it zero.  */
4070   if (ahead == 0)
4071     ahead = PREFETCH_BLOCKS_BEFORE_LOOP_MAX;
4072
4073   for (i = 0; i < num_prefetches; i++)
4074     {
4075       /* Update if we've decided not to prefetch anything within the loop.  */
4076       if (num_real_prefetches == 0)
4077         info[i].prefetch_in_loop = 0;
4078
4079       /* Find how many prefetch instructions we'll use before the loop.  */
4080       if (info[i].prefetch_before_loop != 0)
4081         {
4082           int n = info[i].total_bytes / PREFETCH_BLOCK;
4083           if (n > ahead)
4084             n = ahead;
4085           info[i].prefetch_before_loop = n;
4086           num_prefetches_before += n;
4087           if (info[i].write)
4088             num_write_prefetches_before += n;
4089         }
4090
4091       if (loop_dump_stream)
4092         {
4093           if (info[i].prefetch_in_loop == 0
4094               && info[i].prefetch_before_loop == 0)
4095             continue;
4096           fprintf (loop_dump_stream, "Prefetch insn: %d",
4097                    INSN_UID (info[i].giv->insn));
4098           fprintf (loop_dump_stream,
4099                    "; in loop: %d; before: %d; %s\n",
4100                    info[i].prefetch_in_loop,
4101                    info[i].prefetch_before_loop,
4102                    info[i].write ? "read/write" : "read only");
4103           fprintf (loop_dump_stream,
4104                    " density: %d%%; bytes_accessed: %u; total_bytes: %u\n",
4105                    (int) (info[i].bytes_accessed * 100 / info[i].stride),
4106                    info[i].bytes_accessed, info[i].total_bytes);
4107           fprintf (loop_dump_stream, " index: ");
4108           fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].index);
4109           fprintf (loop_dump_stream, "; stride: ");
4110           fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC, info[i].stride);
4111           fprintf (loop_dump_stream, "; address: ");
4112           print_rtl (loop_dump_stream, info[i].base_address);
4113           fprintf (loop_dump_stream, "\n");
4114         }
4115     }
4116
4117   if (num_real_prefetches + num_prefetches_before > 0)
4118     {
4119       /* Record that this loop uses prefetch instructions.  */
4120       LOOP_INFO (loop)->has_prefetch = 1;
4121
4122       if (loop_dump_stream)
4123         {
4124           fprintf (loop_dump_stream, "Real prefetches needed within loop: %d (write: %d)\n",
4125                    num_real_prefetches, num_real_write_prefetches);
4126           fprintf (loop_dump_stream, "Real prefetches needed before loop: %d (write: %d)\n",
4127                    num_prefetches_before, num_write_prefetches_before);
4128         }
4129     }
4130
4131   for (i = 0; i < num_prefetches; i++)
4132     {
4133       int y;
4134
4135       for (y = 0; y < info[i].prefetch_in_loop; y++)
4136         {
4137           rtx loc = copy_rtx (*info[i].giv->location);
4138           rtx insn;
4139           int bytes_ahead = PREFETCH_BLOCK * (ahead + y);
4140           rtx before_insn = info[i].giv->insn;
4141           rtx prev_insn = PREV_INSN (info[i].giv->insn);
4142
4143           /* We can save some effort by offsetting the address on
4144              architectures with offsettable memory references.  */
4145           if (offsettable_address_p (0, VOIDmode, loc))
4146             loc = plus_constant (loc, bytes_ahead);
4147           else
4148             {
4149               rtx reg = gen_reg_rtx (Pmode);
4150               loop_iv_add_mult_emit_before (loop, loc, const1_rtx,
4151                                             GEN_INT (bytes_ahead), reg,
4152                                             0, before_insn);
4153               loc = reg;
4154             }
4155
4156           /* Make sure the address operand is valid for prefetch.  */
4157           if (! (*insn_data[(int)CODE_FOR_prefetch].operand[0].predicate)
4158                   (loc, insn_data[(int)CODE_FOR_prefetch].operand[0].mode))
4159             loc = force_reg (Pmode, loc);
4160           emit_insn_before (gen_prefetch (loc, GEN_INT (info[i].write),
4161                                           GEN_INT (3)),
4162                             before_insn);
4163
4164           /* Check all insns emitted and record the new GIV
4165              information.  */
4166           insn = NEXT_INSN (prev_insn);
4167           while (insn != before_insn)
4168             {
4169               insn = check_insn_for_givs (loop, insn,
4170                                           info[i].giv->always_executed,
4171                                           info[i].giv->maybe_multiple);
4172               insn = NEXT_INSN (insn);
4173             }
4174         }
4175
4176       if (PREFETCH_BEFORE_LOOP)
4177         {
4178           /* Emit insns before the loop to fetch the first cache lines or,
4179              if we're not prefetching within the loop, everything we expect
4180              to need.  */
4181           for (y = 0; y < info[i].prefetch_before_loop; y++)
4182             {
4183               rtx reg = gen_reg_rtx (Pmode);
4184               rtx loop_start = loop->start;
4185               rtx init_val = info[i].class->initial_value;
4186               rtx add_val = simplify_gen_binary (PLUS, Pmode,
4187                                                  info[i].giv->add_val,
4188                                                  GEN_INT (y * PREFETCH_BLOCK));
4189
4190               /* Functions called by LOOP_IV_ADD_EMIT_BEFORE expect a
4191                  non-constant INIT_VAL to have the same mode as REG, which
4192                  in this case we know to be Pmode.  */
4193               if (GET_MODE (init_val) != Pmode && !CONSTANT_P (init_val))
4194                 init_val = convert_to_mode (Pmode, init_val, 0);
4195               loop_iv_add_mult_emit_before (loop, init_val,
4196                                             info[i].giv->mult_val,
4197                                             add_val, reg, 0, loop_start);
4198               emit_insn_before (gen_prefetch (reg, GEN_INT (info[i].write),
4199                                               GEN_INT (3)),
4200                                 loop_start);
4201             }
4202         }
4203     }
4204
4205   return;
4206 }
4207 \f
4208 /* A "basic induction variable" or biv is a pseudo reg that is set
4209    (within this loop) only by incrementing or decrementing it.  */
4210 /* A "general induction variable" or giv is a pseudo reg whose
4211    value is a linear function of a biv.  */
4212
4213 /* Bivs are recognized by `basic_induction_var';
4214    Givs by `general_induction_var'.  */
4215
4216 /* Communication with routines called via `note_stores'.  */
4217
4218 static rtx note_insn;
4219
4220 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs.  */
4221
4222 static rtx addr_placeholder;
4223
4224 /* ??? Unfinished optimizations, and possible future optimizations,
4225    for the strength reduction code.  */
4226
4227 /* ??? The interaction of biv elimination, and recognition of 'constant'
4228    bivs, may cause problems.  */
4229
4230 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
4231    performance problems.
4232
4233    Perhaps don't eliminate things that can be combined with an addressing
4234    mode.  Find all givs that have the same biv, mult_val, and add_val;
4235    then for each giv, check to see if its only use dies in a following
4236    memory address.  If so, generate a new memory address and check to see
4237    if it is valid.   If it is valid, then store the modified memory address,
4238    otherwise, mark the giv as not done so that it will get its own iv.  */
4239
4240 /* ??? Could try to optimize branches when it is known that a biv is always
4241    positive.  */
4242
4243 /* ??? When replace a biv in a compare insn, we should replace with closest
4244    giv so that an optimized branch can still be recognized by the combiner,
4245    e.g. the VAX acb insn.  */
4246
4247 /* ??? Many of the checks involving uid_luid could be simplified if regscan
4248    was rerun in loop_optimize whenever a register was added or moved.
4249    Also, some of the optimizations could be a little less conservative.  */
4250 \f
4251 /* Scan the loop body and call FNCALL for each insn.  In the addition to the
4252    LOOP and INSN parameters pass MAYBE_MULTIPLE and NOT_EVERY_ITERATION to the
4253    callback.
4254
4255    NOT_EVERY_ITERATION if current insn is not executed at least once for every
4256    loop iteration except for the last one.
4257
4258    MAYBE_MULTIPLE is 1 if current insn may be executed more than once for every
4259    loop iteration.
4260  */
4261 void
4262 for_each_insn_in_loop (loop, fncall)
4263      struct loop *loop;
4264      loop_insn_callback fncall;
4265 {
4266   /* This is 1 if current insn is not executed at least once for every loop
4267      iteration.  */
4268   int not_every_iteration = 0;
4269   int maybe_multiple = 0;
4270   int past_loop_latch = 0;
4271   int loop_depth = 0;
4272   rtx p;
4273
4274   /* If loop_scan_start points to the loop exit test, we have to be wary of
4275      subversive use of gotos inside expression statements.  */
4276   if (prev_nonnote_insn (loop->scan_start) != prev_nonnote_insn (loop->start))
4277     maybe_multiple = back_branch_in_range_p (loop, loop->scan_start);
4278
4279   /* Scan through loop to find all possible bivs.  */
4280
4281   for (p = next_insn_in_loop (loop, loop->scan_start);
4282        p != NULL_RTX;
4283        p = next_insn_in_loop (loop, p))
4284     {
4285       p = fncall (loop, p, not_every_iteration, maybe_multiple);
4286
4287       /* Past CODE_LABEL, we get to insns that may be executed multiple
4288          times.  The only way we can be sure that they can't is if every
4289          jump insn between here and the end of the loop either
4290          returns, exits the loop, is a jump to a location that is still
4291          behind the label, or is a jump to the loop start.  */
4292
4293       if (GET_CODE (p) == CODE_LABEL)
4294         {
4295           rtx insn = p;
4296
4297           maybe_multiple = 0;
4298
4299           while (1)
4300             {
4301               insn = NEXT_INSN (insn);
4302               if (insn == loop->scan_start)
4303                 break;
4304               if (insn == loop->end)
4305                 {
4306                   if (loop->top != 0)
4307                     insn = loop->top;
4308                   else
4309                     break;
4310                   if (insn == loop->scan_start)
4311                     break;
4312                 }
4313
4314               if (GET_CODE (insn) == JUMP_INSN
4315                   && GET_CODE (PATTERN (insn)) != RETURN
4316                   && (!any_condjump_p (insn)
4317                       || (JUMP_LABEL (insn) != 0
4318                           && JUMP_LABEL (insn) != loop->scan_start
4319                           && !loop_insn_first_p (p, JUMP_LABEL (insn)))))
4320                 {
4321                   maybe_multiple = 1;
4322                   break;
4323                 }
4324             }
4325         }
4326
4327       /* Past a jump, we get to insns for which we can't count
4328          on whether they will be executed during each iteration.  */
4329       /* This code appears twice in strength_reduce.  There is also similar
4330          code in scan_loop.  */
4331       if (GET_CODE (p) == JUMP_INSN
4332       /* If we enter the loop in the middle, and scan around to the
4333          beginning, don't set not_every_iteration for that.
4334          This can be any kind of jump, since we want to know if insns
4335          will be executed if the loop is executed.  */
4336           && !(JUMP_LABEL (p) == loop->top
4337              && ((NEXT_INSN (NEXT_INSN (p)) == loop->end
4338                   && any_uncondjump_p (p))
4339                  || (NEXT_INSN (p) == loop->end && any_condjump_p (p)))))
4340         {
4341           rtx label = 0;
4342
4343           /* If this is a jump outside the loop, then it also doesn't
4344              matter.  Check to see if the target of this branch is on the
4345              loop->exits_labels list.  */
4346
4347           for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
4348             if (XEXP (label, 0) == JUMP_LABEL (p))
4349               break;
4350
4351           if (!label)
4352             not_every_iteration = 1;
4353         }
4354
4355       else if (GET_CODE (p) == NOTE)
4356         {
4357           /* At the virtual top of a converted loop, insns are again known to
4358              be executed each iteration: logically, the loop begins here
4359              even though the exit code has been duplicated.
4360
4361              Insns are also again known to be executed each iteration at
4362              the LOOP_CONT note.  */
4363           if ((NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP
4364                || NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_CONT)
4365               && loop_depth == 0)
4366             not_every_iteration = 0;
4367           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
4368             loop_depth++;
4369           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
4370             loop_depth--;
4371         }
4372
4373       /* Note if we pass a loop latch.  If we do, then we can not clear
4374          NOT_EVERY_ITERATION below when we pass the last CODE_LABEL in
4375          a loop since a jump before the last CODE_LABEL may have started
4376          a new loop iteration.
4377
4378          Note that LOOP_TOP is only set for rotated loops and we need
4379          this check for all loops, so compare against the CODE_LABEL
4380          which immediately follows LOOP_START.  */
4381       if (GET_CODE (p) == JUMP_INSN
4382           && JUMP_LABEL (p) == NEXT_INSN (loop->start))
4383         past_loop_latch = 1;
4384
4385       /* Unlike in the code motion pass where MAYBE_NEVER indicates that
4386          an insn may never be executed, NOT_EVERY_ITERATION indicates whether
4387          or not an insn is known to be executed each iteration of the
4388          loop, whether or not any iterations are known to occur.
4389
4390          Therefore, if we have just passed a label and have no more labels
4391          between here and the test insn of the loop, and we have not passed
4392          a jump to the top of the loop, then we know these insns will be
4393          executed each iteration.  */
4394
4395       if (not_every_iteration
4396           && !past_loop_latch
4397           && GET_CODE (p) == CODE_LABEL
4398           && no_labels_between_p (p, loop->end)
4399           && loop_insn_first_p (p, loop->cont))
4400         not_every_iteration = 0;
4401     }
4402 }
4403 \f
4404 static void
4405 loop_bivs_find (loop)
4406      struct loop *loop;
4407 {
4408   struct loop_regs *regs = LOOP_REGS (loop);
4409   struct loop_ivs *ivs = LOOP_IVS (loop);
4410   /* Temporary list pointers for traversing ivs->list.  */
4411   struct iv_class *bl, **backbl;
4412
4413   ivs->list = 0;
4414
4415   for_each_insn_in_loop (loop, check_insn_for_bivs);
4416
4417   /* Scan ivs->list to remove all regs that proved not to be bivs.
4418      Make a sanity check against regs->n_times_set.  */
4419   for (backbl = &ivs->list, bl = *backbl; bl; bl = bl->next)
4420     {
4421       if (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4422           /* Above happens if register modified by subreg, etc.  */
4423           /* Make sure it is not recognized as a basic induction var: */
4424           || regs->array[bl->regno].n_times_set != bl->biv_count
4425           /* If never incremented, it is invariant that we decided not to
4426              move.  So leave it alone.  */
4427           || ! bl->incremented)
4428         {
4429           if (loop_dump_stream)
4430             fprintf (loop_dump_stream, "Biv %d: discarded, %s\n",
4431                      bl->regno,
4432                      (REG_IV_TYPE (ivs, bl->regno) != BASIC_INDUCT
4433                       ? "not induction variable"
4434                       : (! bl->incremented ? "never incremented"
4435                          : "count error")));
4436
4437           REG_IV_TYPE (ivs, bl->regno) = NOT_BASIC_INDUCT;
4438           *backbl = bl->next;
4439         }
4440       else
4441         {
4442           backbl = &bl->next;
4443
4444           if (loop_dump_stream)
4445             fprintf (loop_dump_stream, "Biv %d: verified\n", bl->regno);
4446         }
4447     }
4448 }
4449
4450
4451 /* Determine how BIVS are initialised by looking through pre-header
4452    extended basic block.  */
4453 static void
4454 loop_bivs_init_find (loop)
4455      struct loop *loop;
4456 {
4457   struct loop_ivs *ivs = LOOP_IVS (loop);
4458   /* Temporary list pointers for traversing ivs->list.  */
4459   struct iv_class *bl;
4460   int call_seen;
4461   rtx p;
4462
4463   /* Find initial value for each biv by searching backwards from loop_start,
4464      halting at first label.  Also record any test condition.  */
4465
4466   call_seen = 0;
4467   for (p = loop->start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
4468     {
4469       rtx test;
4470
4471       note_insn = p;
4472
4473       if (GET_CODE (p) == CALL_INSN)
4474         call_seen = 1;
4475
4476       if (INSN_P (p))
4477         note_stores (PATTERN (p), record_initial, ivs);
4478
4479       /* Record any test of a biv that branches around the loop if no store
4480          between it and the start of loop.  We only care about tests with
4481          constants and registers and only certain of those.  */
4482       if (GET_CODE (p) == JUMP_INSN
4483           && JUMP_LABEL (p) != 0
4484           && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop->end)
4485           && (test = get_condition_for_loop (loop, p)) != 0
4486           && GET_CODE (XEXP (test, 0)) == REG
4487           && REGNO (XEXP (test, 0)) < max_reg_before_loop
4488           && (bl = REG_IV_CLASS (ivs, REGNO (XEXP (test, 0)))) != 0
4489           && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop->start)
4490           && bl->init_insn == 0)
4491         {
4492           /* If an NE test, we have an initial value!  */
4493           if (GET_CODE (test) == NE)
4494             {
4495               bl->init_insn = p;
4496               bl->init_set = gen_rtx_SET (VOIDmode,
4497                                           XEXP (test, 0), XEXP (test, 1));
4498             }
4499           else
4500             bl->initial_test = test;
4501         }
4502     }
4503 }
4504
4505
4506 /* Look at the each biv and see if we can say anything better about its
4507    initial value from any initializing insns set up above.  (This is done
4508    in two passes to avoid missing SETs in a PARALLEL.)  */
4509 static void
4510 loop_bivs_check (loop)
4511      struct loop *loop;
4512 {
4513   struct loop_ivs *ivs = LOOP_IVS (loop);
4514   /* Temporary list pointers for traversing ivs->list.  */
4515   struct iv_class *bl;
4516   struct iv_class **backbl;
4517
4518   for (backbl = &ivs->list; (bl = *backbl); backbl = &bl->next)
4519     {
4520       rtx src;
4521       rtx note;
4522
4523       if (! bl->init_insn)
4524         continue;
4525
4526       /* IF INIT_INSN has a REG_EQUAL or REG_EQUIV note and the value
4527          is a constant, use the value of that.  */
4528       if (((note = find_reg_note (bl->init_insn, REG_EQUAL, 0)) != NULL
4529            && CONSTANT_P (XEXP (note, 0)))
4530           || ((note = find_reg_note (bl->init_insn, REG_EQUIV, 0)) != NULL
4531               && CONSTANT_P (XEXP (note, 0))))
4532         src = XEXP (note, 0);
4533       else
4534         src = SET_SRC (bl->init_set);
4535
4536       if (loop_dump_stream)
4537         fprintf (loop_dump_stream,
4538                  "Biv %d: initialized at insn %d: initial value ",
4539                  bl->regno, INSN_UID (bl->init_insn));
4540
4541       if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
4542            || GET_MODE (src) == VOIDmode)
4543           && valid_initial_value_p (src, bl->init_insn,
4544                                     LOOP_INFO (loop)->pre_header_has_call,
4545                                     loop->start))
4546         {
4547           bl->initial_value = src;
4548
4549           if (loop_dump_stream)
4550             {
4551               print_simple_rtl (loop_dump_stream, src);
4552               fputc ('\n', loop_dump_stream);
4553             }
4554         }
4555       /* If we can't make it a giv,
4556          let biv keep initial value of "itself".  */
4557       else if (loop_dump_stream)
4558         fprintf (loop_dump_stream, "is complex\n");
4559     }
4560 }
4561
4562
4563 /* Search the loop for general induction variables.  */
4564
4565 static void
4566 loop_givs_find (loop)
4567      struct loop* loop;
4568 {
4569   for_each_insn_in_loop (loop, check_insn_for_givs);
4570 }
4571
4572
4573 /* For each giv for which we still don't know whether or not it is
4574    replaceable, check to see if it is replaceable because its final value
4575    can be calculated.  */
4576
4577 static void
4578 loop_givs_check (loop)
4579      struct loop *loop;
4580 {
4581   struct loop_ivs *ivs = LOOP_IVS (loop);
4582   struct iv_class *bl;
4583
4584   for (bl = ivs->list; bl; bl = bl->next)
4585     {
4586       struct induction *v;
4587
4588       for (v = bl->giv; v; v = v->next_iv)
4589         if (! v->replaceable && ! v->not_replaceable)
4590           check_final_value (loop, v);
4591     }
4592 }
4593
4594
4595 /* Return non-zero if it is possible to eliminate the biv BL provided
4596    all givs are reduced.  This is possible if either the reg is not
4597    used outside the loop, or we can compute what its final value will
4598    be.  */
4599
4600 static int
4601 loop_biv_eliminable_p (loop, bl, threshold, insn_count)
4602      struct loop *loop;
4603      struct iv_class *bl;
4604      int threshold;
4605      int insn_count;
4606 {
4607   /* For architectures with a decrement_and_branch_until_zero insn,
4608      don't do this if we put a REG_NONNEG note on the endtest for this
4609      biv.  */
4610
4611 #ifdef HAVE_decrement_and_branch_until_zero
4612   if (bl->nonneg)
4613     {
4614       if (loop_dump_stream)
4615         fprintf (loop_dump_stream,
4616                  "Cannot eliminate nonneg biv %d.\n", bl->regno);
4617       return 0;
4618     }
4619 #endif
4620
4621   /* Check that biv is used outside loop or if it has a final value.
4622      Compare against bl->init_insn rather than loop->start.  We aren't
4623      concerned with any uses of the biv between init_insn and
4624      loop->start since these won't be affected by the value of the biv
4625      elsewhere in the function, so long as init_insn doesn't use the
4626      biv itself.  */
4627
4628   if ((REGNO_LAST_LUID (bl->regno) < INSN_LUID (loop->end)
4629        && bl->init_insn
4630        && INSN_UID (bl->init_insn) < max_uid_for_loop
4631        && REGNO_FIRST_LUID (bl->regno) >= INSN_LUID (bl->init_insn)
4632        && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
4633       || (bl->final_value = final_biv_value (loop, bl)))
4634     return maybe_eliminate_biv (loop, bl, 0, threshold, insn_count);
4635
4636   if (loop_dump_stream)
4637     {
4638       fprintf (loop_dump_stream,
4639                "Cannot eliminate biv %d.\n",
4640                bl->regno);
4641       fprintf (loop_dump_stream,
4642                "First use: insn %d, last use: insn %d.\n",
4643                REGNO_FIRST_UID (bl->regno),
4644                REGNO_LAST_UID (bl->regno));
4645     }
4646   return 0;
4647 }
4648
4649
4650 /* Reduce each giv of BL that we have decided to reduce.  */
4651
4652 static void
4653 loop_givs_reduce (loop, bl)
4654      struct loop *loop;
4655      struct iv_class *bl;
4656 {
4657   struct induction *v;
4658
4659   for (v = bl->giv; v; v = v->next_iv)
4660     {
4661       struct induction *tv;
4662       if (! v->ignore && v->same == 0)
4663         {
4664           int auto_inc_opt = 0;
4665
4666           /* If the code for derived givs immediately below has already
4667              allocated a new_reg, we must keep it.  */
4668           if (! v->new_reg)
4669             v->new_reg = gen_reg_rtx (v->mode);
4670
4671 #ifdef AUTO_INC_DEC
4672           /* If the target has auto-increment addressing modes, and
4673              this is an address giv, then try to put the increment
4674              immediately after its use, so that flow can create an
4675              auto-increment addressing mode.  */
4676           if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4677               && bl->biv->always_executed && ! bl->biv->maybe_multiple
4678               /* We don't handle reversed biv's because bl->biv->insn
4679                  does not have a valid INSN_LUID.  */
4680               && ! bl->reversed
4681               && v->always_executed && ! v->maybe_multiple
4682               && INSN_UID (v->insn) < max_uid_for_loop)
4683             {
4684               /* If other giv's have been combined with this one, then
4685                  this will work only if all uses of the other giv's occur
4686                  before this giv's insn.  This is difficult to check.
4687
4688                  We simplify this by looking for the common case where
4689                  there is one DEST_REG giv, and this giv's insn is the
4690                  last use of the dest_reg of that DEST_REG giv.  If the
4691                  increment occurs after the address giv, then we can
4692                  perform the optimization.  (Otherwise, the increment
4693                  would have to go before other_giv, and we would not be
4694                  able to combine it with the address giv to get an
4695                  auto-inc address.)  */
4696               if (v->combined_with)
4697                 {
4698                   struct induction *other_giv = 0;
4699
4700                   for (tv = bl->giv; tv; tv = tv->next_iv)
4701                     if (tv->same == v)
4702                       {
4703                         if (other_giv)
4704                           break;
4705                         else
4706                           other_giv = tv;
4707                       }
4708                   if (! tv && other_giv
4709                       && REGNO (other_giv->dest_reg) < max_reg_before_loop
4710                       && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4711                           == INSN_UID (v->insn))
4712                       && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4713                     auto_inc_opt = 1;
4714                 }
4715               /* Check for case where increment is before the address
4716                  giv.  Do this test in "loop order".  */
4717               else if ((INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn)
4718                         && (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4719                             || (INSN_LUID (bl->biv->insn)
4720                                 > INSN_LUID (loop->scan_start))))
4721                        || (INSN_LUID (v->insn) < INSN_LUID (loop->scan_start)
4722                            && (INSN_LUID (loop->scan_start)
4723                                < INSN_LUID (bl->biv->insn))))
4724                 auto_inc_opt = -1;
4725               else
4726                 auto_inc_opt = 1;
4727
4728 #ifdef HAVE_cc0
4729               {
4730                 rtx prev;
4731
4732                 /* We can't put an insn immediately after one setting
4733                    cc0, or immediately before one using cc0.  */
4734                 if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4735                     || (auto_inc_opt == -1
4736                         && (prev = prev_nonnote_insn (v->insn)) != 0
4737                         && INSN_P (prev)
4738                         && sets_cc0_p (PATTERN (prev))))
4739                   auto_inc_opt = 0;
4740               }
4741 #endif
4742
4743               if (auto_inc_opt)
4744                 v->auto_inc_opt = 1;
4745             }
4746 #endif
4747
4748           /* For each place where the biv is incremented, add an insn
4749              to increment the new, reduced reg for the giv.  */
4750           for (tv = bl->biv; tv; tv = tv->next_iv)
4751             {
4752               rtx insert_before;
4753
4754               if (! auto_inc_opt)
4755                 insert_before = tv->insn;
4756               else if (auto_inc_opt == 1)
4757                 insert_before = NEXT_INSN (v->insn);
4758               else
4759                 insert_before = v->insn;
4760
4761               if (tv->mult_val == const1_rtx)
4762                 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4763                                               v->new_reg, v->new_reg,
4764                                               0, insert_before);
4765               else /* tv->mult_val == const0_rtx */
4766                 /* A multiply is acceptable here
4767                    since this is presumed to be seldom executed.  */
4768                 loop_iv_add_mult_emit_before (loop, tv->add_val, v->mult_val,
4769                                               v->add_val, v->new_reg,
4770                                               0, insert_before);
4771             }
4772
4773           /* Add code at loop start to initialize giv's reduced reg.  */
4774
4775           loop_iv_add_mult_hoist (loop,
4776                                   extend_value_for_giv (v, bl->initial_value),
4777                                   v->mult_val, v->add_val, v->new_reg);
4778         }
4779     }
4780 }
4781
4782
4783 /* Check for givs whose first use is their definition and whose
4784    last use is the definition of another giv.  If so, it is likely
4785    dead and should not be used to derive another giv nor to
4786    eliminate a biv.  */
4787
4788 static void
4789 loop_givs_dead_check (loop, bl)
4790      struct loop *loop ATTRIBUTE_UNUSED;
4791      struct iv_class *bl;
4792 {
4793   struct induction *v;
4794
4795   for (v = bl->giv; v; v = v->next_iv)
4796     {
4797       if (v->ignore
4798           || (v->same && v->same->ignore))
4799         continue;
4800
4801       if (v->giv_type == DEST_REG
4802           && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4803         {
4804           struct induction *v1;
4805
4806           for (v1 = bl->giv; v1; v1 = v1->next_iv)
4807             if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4808               v->maybe_dead = 1;
4809         }
4810     }
4811 }
4812
4813
4814 static void
4815 loop_givs_rescan (loop, bl, reg_map)
4816      struct loop *loop;
4817      struct iv_class *bl;
4818      rtx *reg_map;
4819 {
4820   struct induction *v;
4821
4822   for (v = bl->giv; v; v = v->next_iv)
4823     {
4824       if (v->same && v->same->ignore)
4825         v->ignore = 1;
4826
4827       if (v->ignore)
4828         continue;
4829
4830       /* Update expression if this was combined, in case other giv was
4831          replaced.  */
4832       if (v->same)
4833         v->new_reg = replace_rtx (v->new_reg,
4834                                   v->same->dest_reg, v->same->new_reg);
4835
4836       /* See if this register is known to be a pointer to something.  If
4837          so, see if we can find the alignment.  First see if there is a
4838          destination register that is a pointer.  If so, this shares the
4839          alignment too.  Next see if we can deduce anything from the
4840          computational information.  If not, and this is a DEST_ADDR
4841          giv, at least we know that it's a pointer, though we don't know
4842          the alignment.  */
4843       if (GET_CODE (v->new_reg) == REG
4844           && v->giv_type == DEST_REG
4845           && REG_POINTER (v->dest_reg))
4846         mark_reg_pointer (v->new_reg,
4847                           REGNO_POINTER_ALIGN (REGNO (v->dest_reg)));
4848       else if (GET_CODE (v->new_reg) == REG
4849                && REG_POINTER (v->src_reg))
4850         {
4851           unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->src_reg));
4852
4853           if (align == 0
4854               || GET_CODE (v->add_val) != CONST_INT
4855               || INTVAL (v->add_val) % (align / BITS_PER_UNIT) != 0)
4856             align = 0;
4857
4858           mark_reg_pointer (v->new_reg, align);
4859         }
4860       else if (GET_CODE (v->new_reg) == REG
4861                && GET_CODE (v->add_val) == REG
4862                && REG_POINTER (v->add_val))
4863         {
4864           unsigned int align = REGNO_POINTER_ALIGN (REGNO (v->add_val));
4865
4866           if (align == 0 || GET_CODE (v->mult_val) != CONST_INT
4867               || INTVAL (v->mult_val) % (align / BITS_PER_UNIT) != 0)
4868             align = 0;
4869
4870           mark_reg_pointer (v->new_reg, align);
4871         }
4872       else if (GET_CODE (v->new_reg) == REG && v->giv_type == DEST_ADDR)
4873         mark_reg_pointer (v->new_reg, 0);
4874
4875       if (v->giv_type == DEST_ADDR)
4876         /* Store reduced reg as the address in the memref where we found
4877            this giv.  */
4878         validate_change (v->insn, v->location, v->new_reg, 0);
4879       else if (v->replaceable)
4880         {
4881           reg_map[REGNO (v->dest_reg)] = v->new_reg;
4882         }
4883       else
4884         {
4885           /* Not replaceable; emit an insn to set the original giv reg from
4886              the reduced giv, same as above.  */
4887           loop_insn_emit_after (loop, 0, v->insn,
4888                                 gen_move_insn (v->dest_reg, v->new_reg));
4889         }
4890
4891       /* When a loop is reversed, givs which depend on the reversed
4892          biv, and which are live outside the loop, must be set to their
4893          correct final value.  This insn is only needed if the giv is
4894          not replaceable.  The correct final value is the same as the
4895          value that the giv starts the reversed loop with.  */
4896       if (bl->reversed && ! v->replaceable)
4897         loop_iv_add_mult_sink (loop,
4898                                extend_value_for_giv (v, bl->initial_value),
4899                                v->mult_val, v->add_val, v->dest_reg);
4900       else if (v->final_value)
4901         loop_insn_sink_or_swim (loop,
4902                                 gen_load_of_final_value (v->dest_reg,
4903                                                          v->final_value));
4904
4905       if (loop_dump_stream)
4906         {
4907           fprintf (loop_dump_stream, "giv at %d reduced to ",
4908                    INSN_UID (v->insn));
4909           print_simple_rtl (loop_dump_stream, v->new_reg);
4910           fprintf (loop_dump_stream, "\n");
4911         }
4912     }
4913 }
4914
4915
4916 static int
4917 loop_giv_reduce_benefit (loop, bl, v, test_reg)
4918      struct loop *loop ATTRIBUTE_UNUSED;
4919      struct iv_class *bl;
4920      struct induction *v;
4921      rtx test_reg;
4922 {
4923   int add_cost;
4924   int benefit;
4925
4926   benefit = v->benefit;
4927   PUT_MODE (test_reg, v->mode);
4928   add_cost = iv_add_mult_cost (bl->biv->add_val, v->mult_val,
4929                                test_reg, test_reg);
4930
4931   /* Reduce benefit if not replaceable, since we will insert a
4932      move-insn to replace the insn that calculates this giv.  Don't do
4933      this unless the giv is a user variable, since it will often be
4934      marked non-replaceable because of the duplication of the exit
4935      code outside the loop.  In such a case, the copies we insert are
4936      dead and will be deleted.  So they don't have a cost.  Similar
4937      situations exist.  */
4938   /* ??? The new final_[bg]iv_value code does a much better job of
4939      finding replaceable giv's, and hence this code may no longer be
4940      necessary.  */
4941   if (! v->replaceable && ! bl->eliminable
4942       && REG_USERVAR_P (v->dest_reg))
4943     benefit -= copy_cost;
4944
4945   /* Decrease the benefit to count the add-insns that we will insert
4946      to increment the reduced reg for the giv.  ??? This can
4947      overestimate the run-time cost of the additional insns, e.g. if
4948      there are multiple basic blocks that increment the biv, but only
4949      one of these blocks is executed during each iteration.  There is
4950      no good way to detect cases like this with the current structure
4951      of the loop optimizer.  This code is more accurate for
4952      determining code size than run-time benefits.  */
4953   benefit -= add_cost * bl->biv_count;
4954
4955   /* Decide whether to strength-reduce this giv or to leave the code
4956      unchanged (recompute it from the biv each time it is used).  This
4957      decision can be made independently for each giv.  */
4958
4959 #ifdef AUTO_INC_DEC
4960   /* Attempt to guess whether autoincrement will handle some of the
4961      new add insns; if so, increase BENEFIT (undo the subtraction of
4962      add_cost that was done above).  */
4963   if (v->giv_type == DEST_ADDR
4964       /* Increasing the benefit is risky, since this is only a guess.
4965          Avoid increasing register pressure in cases where there would
4966          be no other benefit from reducing this giv.  */
4967       && benefit > 0
4968       && GET_CODE (v->mult_val) == CONST_INT)
4969     {
4970       int size = GET_MODE_SIZE (GET_MODE (v->mem));
4971
4972       if (HAVE_POST_INCREMENT
4973           && INTVAL (v->mult_val) == size)
4974         benefit += add_cost * bl->biv_count;
4975       else if (HAVE_PRE_INCREMENT
4976                && INTVAL (v->mult_val) == size)
4977         benefit += add_cost * bl->biv_count;
4978       else if (HAVE_POST_DECREMENT
4979                && -INTVAL (v->mult_val) == size)
4980         benefit += add_cost * bl->biv_count;
4981       else if (HAVE_PRE_DECREMENT
4982                && -INTVAL (v->mult_val) == size)
4983         benefit += add_cost * bl->biv_count;
4984     }
4985 #endif
4986
4987   return benefit;
4988 }
4989
4990
4991 /* Free IV structures for LOOP.  */
4992
4993 static void
4994 loop_ivs_free (loop)
4995      struct loop *loop;
4996 {
4997   struct loop_ivs *ivs = LOOP_IVS (loop);
4998   struct iv_class *iv = ivs->list;
4999
5000   free (ivs->regs);
5001
5002   while (iv)
5003     {
5004       struct iv_class *next = iv->next;
5005       struct induction *induction;
5006       struct induction *next_induction;
5007
5008       for (induction = iv->biv; induction; induction = next_induction)
5009         {
5010           next_induction = induction->next_iv;
5011           free (induction);
5012         }
5013       for (induction = iv->giv; induction; induction = next_induction)
5014         {
5015           next_induction = induction->next_iv;
5016           free (induction);
5017         }
5018
5019       free (iv);
5020       iv = next;
5021     }
5022 }
5023
5024
5025 /* Perform strength reduction and induction variable elimination.
5026
5027    Pseudo registers created during this function will be beyond the
5028    last valid index in several tables including
5029    REGS->ARRAY[I].N_TIMES_SET and REGNO_LAST_UID.  This does not cause a
5030    problem here, because the added registers cannot be givs outside of
5031    their loop, and hence will never be reconsidered.  But scan_loop
5032    must check regnos to make sure they are in bounds.  */
5033
5034 static void
5035 strength_reduce (loop, flags)
5036      struct loop *loop;
5037      int flags;
5038 {
5039   struct loop_info *loop_info = LOOP_INFO (loop);
5040   struct loop_regs *regs = LOOP_REGS (loop);
5041   struct loop_ivs *ivs = LOOP_IVS (loop);
5042   rtx p;
5043   /* Temporary list pointer for traversing ivs->list.  */
5044   struct iv_class *bl;
5045   /* Ratio of extra register life span we can justify
5046      for saving an instruction.  More if loop doesn't call subroutines
5047      since in that case saving an insn makes more difference
5048      and more registers are available.  */
5049   /* ??? could set this to last value of threshold in move_movables */
5050   int threshold = (loop_info->has_call ? 1 : 2) * (3 + n_non_fixed_regs);
5051   /* Map of pseudo-register replacements.  */
5052   rtx *reg_map = NULL;
5053   int reg_map_size;
5054   int unrolled_insn_copies = 0;
5055   rtx test_reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
5056   int insn_count = count_insns_in_loop (loop);
5057
5058   addr_placeholder = gen_reg_rtx (Pmode);
5059
5060   ivs->n_regs = max_reg_before_loop;
5061   ivs->regs = (struct iv *) xcalloc (ivs->n_regs, sizeof (struct iv));
5062
5063   /* Find all BIVs in loop.  */
5064   loop_bivs_find (loop);
5065
5066   /* Exit if there are no bivs.  */
5067   if (! ivs->list)
5068     {
5069       /* Can still unroll the loop anyways, but indicate that there is no
5070          strength reduction info available.  */
5071       if (flags & LOOP_UNROLL)
5072         unroll_loop (loop, insn_count, 0);
5073
5074       loop_ivs_free (loop);
5075       return;
5076     }
5077
5078   /* Determine how BIVS are initialised by looking through pre-header
5079      extended basic block.  */
5080   loop_bivs_init_find (loop);
5081
5082   /* Look at the each biv and see if we can say anything better about its
5083      initial value from any initializing insns set up above.  */
5084   loop_bivs_check (loop);
5085
5086   /* Search the loop for general induction variables.  */
5087   loop_givs_find (loop);
5088
5089   /* Try to calculate and save the number of loop iterations.  This is
5090      set to zero if the actual number can not be calculated.  This must
5091      be called after all giv's have been identified, since otherwise it may
5092      fail if the iteration variable is a giv.  */
5093   loop_iterations (loop);
5094
5095 #ifdef HAVE_prefetch
5096   if (flags & LOOP_PREFETCH)
5097     emit_prefetch_instructions (loop);
5098 #endif
5099
5100   /* Now for each giv for which we still don't know whether or not it is
5101      replaceable, check to see if it is replaceable because its final value
5102      can be calculated.  This must be done after loop_iterations is called,
5103      so that final_giv_value will work correctly.  */
5104   loop_givs_check (loop);
5105
5106   /* Try to prove that the loop counter variable (if any) is always
5107      nonnegative; if so, record that fact with a REG_NONNEG note
5108      so that "decrement and branch until zero" insn can be used.  */
5109   check_dbra_loop (loop, insn_count);
5110
5111   /* Create reg_map to hold substitutions for replaceable giv regs.
5112      Some givs might have been made from biv increments, so look at
5113      ivs->reg_iv_type for a suitable size.  */
5114   reg_map_size = ivs->n_regs;
5115   reg_map = (rtx *) xcalloc (reg_map_size, sizeof (rtx));
5116
5117   /* Examine each iv class for feasibility of strength reduction/induction
5118      variable elimination.  */
5119
5120   for (bl = ivs->list; bl; bl = bl->next)
5121     {
5122       struct induction *v;
5123       int benefit;
5124
5125       /* Test whether it will be possible to eliminate this biv
5126          provided all givs are reduced.  */
5127       bl->eliminable = loop_biv_eliminable_p (loop, bl, threshold, insn_count);
5128
5129       /* This will be true at the end, if all givs which depend on this
5130          biv have been strength reduced.
5131          We can't (currently) eliminate the biv unless this is so.  */
5132       bl->all_reduced = 1;
5133
5134       /* Check each extension dependent giv in this class to see if its
5135          root biv is safe from wrapping in the interior mode.  */
5136       check_ext_dependent_givs (bl, loop_info);
5137
5138       /* Combine all giv's for this iv_class.  */
5139       combine_givs (regs, bl);
5140
5141       for (v = bl->giv; v; v = v->next_iv)
5142         {
5143           struct induction *tv;
5144
5145           if (v->ignore || v->same)
5146             continue;
5147
5148           benefit = loop_giv_reduce_benefit (loop, bl, v, test_reg);
5149
5150           /* If an insn is not to be strength reduced, then set its ignore
5151              flag, and clear bl->all_reduced.  */
5152
5153           /* A giv that depends on a reversed biv must be reduced if it is
5154              used after the loop exit, otherwise, it would have the wrong
5155              value after the loop exit.  To make it simple, just reduce all
5156              of such giv's whether or not we know they are used after the loop
5157              exit.  */
5158
5159           if (! flag_reduce_all_givs
5160               && v->lifetime * threshold * benefit < insn_count
5161               && ! bl->reversed)
5162             {
5163               if (loop_dump_stream)
5164                 fprintf (loop_dump_stream,
5165                          "giv of insn %d not worth while, %d vs %d.\n",
5166                          INSN_UID (v->insn),
5167                          v->lifetime * threshold * benefit, insn_count);
5168               v->ignore = 1;
5169               bl->all_reduced = 0;
5170             }
5171           else
5172             {
5173               /* Check that we can increment the reduced giv without a
5174                  multiply insn.  If not, reject it.  */
5175
5176               for (tv = bl->biv; tv; tv = tv->next_iv)
5177                 if (tv->mult_val == const1_rtx
5178                     && ! product_cheap_p (tv->add_val, v->mult_val))
5179                   {
5180                     if (loop_dump_stream)
5181                       fprintf (loop_dump_stream,
5182                                "giv of insn %d: would need a multiply.\n",
5183                                INSN_UID (v->insn));
5184                     v->ignore = 1;
5185                     bl->all_reduced = 0;
5186                     break;
5187                   }
5188             }
5189         }
5190
5191       /* Check for givs whose first use is their definition and whose
5192          last use is the definition of another giv.  If so, it is likely
5193          dead and should not be used to derive another giv nor to
5194          eliminate a biv.  */
5195       loop_givs_dead_check (loop, bl);
5196
5197       /* Reduce each giv that we decided to reduce.  */
5198       loop_givs_reduce (loop, bl);
5199
5200       /* Rescan all givs.  If a giv is the same as a giv not reduced, mark it
5201          as not reduced.
5202
5203          For each giv register that can be reduced now: if replaceable,
5204          substitute reduced reg wherever the old giv occurs;
5205          else add new move insn "giv_reg = reduced_reg".  */
5206       loop_givs_rescan (loop, bl, reg_map);
5207
5208       /* All the givs based on the biv bl have been reduced if they
5209          merit it.  */
5210
5211       /* For each giv not marked as maybe dead that has been combined with a
5212          second giv, clear any "maybe dead" mark on that second giv.
5213          v->new_reg will either be or refer to the register of the giv it
5214          combined with.
5215
5216          Doing this clearing avoids problems in biv elimination where
5217          a giv's new_reg is a complex value that can't be put in the
5218          insn but the giv combined with (with a reg as new_reg) is
5219          marked maybe_dead.  Since the register will be used in either
5220          case, we'd prefer it be used from the simpler giv.  */
5221
5222       for (v = bl->giv; v; v = v->next_iv)
5223         if (! v->maybe_dead && v->same)
5224           v->same->maybe_dead = 0;
5225
5226       /* Try to eliminate the biv, if it is a candidate.
5227          This won't work if ! bl->all_reduced,
5228          since the givs we planned to use might not have been reduced.
5229
5230          We have to be careful that we didn't initially think we could
5231          eliminate this biv because of a giv that we now think may be
5232          dead and shouldn't be used as a biv replacement.
5233
5234          Also, there is the possibility that we may have a giv that looks
5235          like it can be used to eliminate a biv, but the resulting insn
5236          isn't valid.  This can happen, for example, on the 88k, where a
5237          JUMP_INSN can compare a register only with zero.  Attempts to
5238          replace it with a compare with a constant will fail.
5239
5240          Note that in cases where this call fails, we may have replaced some
5241          of the occurrences of the biv with a giv, but no harm was done in
5242          doing so in the rare cases where it can occur.  */
5243
5244       if (bl->all_reduced == 1 && bl->eliminable
5245           && maybe_eliminate_biv (loop, bl, 1, threshold, insn_count))
5246         {
5247           /* ?? If we created a new test to bypass the loop entirely,
5248              or otherwise drop straight in, based on this test, then
5249              we might want to rewrite it also.  This way some later
5250              pass has more hope of removing the initialization of this
5251              biv entirely.  */
5252
5253           /* If final_value != 0, then the biv may be used after loop end
5254              and we must emit an insn to set it just in case.
5255
5256              Reversed bivs already have an insn after the loop setting their
5257              value, so we don't need another one.  We can't calculate the
5258              proper final value for such a biv here anyways.  */
5259           if (bl->final_value && ! bl->reversed)
5260               loop_insn_sink_or_swim (loop,
5261                                       gen_load_of_final_value (bl->biv->dest_reg,
5262                                                                bl->final_value));
5263
5264           if (loop_dump_stream)
5265             fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
5266                      bl->regno);
5267         }
5268       /* See above note wrt final_value.  But since we couldn't eliminate
5269          the biv, we must set the value after the loop instead of before.  */
5270       else if (bl->final_value && ! bl->reversed)
5271         loop_insn_sink (loop, gen_load_of_final_value (bl->biv->dest_reg,
5272                                                        bl->final_value));
5273     }
5274
5275   /* Go through all the instructions in the loop, making all the
5276      register substitutions scheduled in REG_MAP.  */
5277
5278   for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
5279     if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5280         || GET_CODE (p) == CALL_INSN)
5281       {
5282         replace_regs (PATTERN (p), reg_map, reg_map_size, 0);
5283         replace_regs (REG_NOTES (p), reg_map, reg_map_size, 0);
5284         INSN_CODE (p) = -1;
5285       }
5286
5287   if (loop_info->n_iterations > 0)
5288     {
5289       /* When we completely unroll a loop we will likely not need the increment
5290          of the loop BIV and we will not need the conditional branch at the
5291          end of the loop.  */
5292       unrolled_insn_copies = insn_count - 2;
5293
5294 #ifdef HAVE_cc0
5295       /* When we completely unroll a loop on a HAVE_cc0 machine we will not
5296          need the comparison before the conditional branch at the end of the
5297          loop.  */
5298       unrolled_insn_copies -= 1;
5299 #endif
5300
5301       /* We'll need one copy for each loop iteration.  */
5302       unrolled_insn_copies *= loop_info->n_iterations;
5303
5304       /* A little slop to account for the ability to remove initialization
5305          code, better CSE, and other secondary benefits of completely
5306          unrolling some loops.  */
5307       unrolled_insn_copies -= 1;
5308
5309       /* Clamp the value.  */
5310       if (unrolled_insn_copies < 0)
5311         unrolled_insn_copies = 0;
5312     }
5313
5314   /* Unroll loops from within strength reduction so that we can use the
5315      induction variable information that strength_reduce has already
5316      collected.  Always unroll loops that would be as small or smaller
5317      unrolled than when rolled.  */
5318   if ((flags & LOOP_UNROLL)
5319       || (!(flags & LOOP_FIRST_PASS)
5320           && loop_info->n_iterations > 0
5321           && unrolled_insn_copies <= insn_count))
5322     unroll_loop (loop, insn_count, 1);
5323
5324 #ifdef HAVE_doloop_end
5325   if (HAVE_doloop_end && (flags & LOOP_BCT) && flag_branch_on_count_reg)
5326     doloop_optimize (loop);
5327 #endif  /* HAVE_doloop_end  */
5328
5329   /* In case number of iterations is known, drop branch prediction note
5330      in the branch.  Do that only in second loop pass, as loop unrolling
5331      may change the number of iterations performed.  */
5332   if (flags & LOOP_BCT)
5333     {
5334       unsigned HOST_WIDE_INT n
5335         = loop_info->n_iterations / loop_info->unroll_number;
5336       if (n > 1)
5337         predict_insn (PREV_INSN (loop->end), PRED_LOOP_ITERATIONS,
5338                       REG_BR_PROB_BASE - REG_BR_PROB_BASE / n);
5339     }
5340
5341   if (loop_dump_stream)
5342     fprintf (loop_dump_stream, "\n");
5343
5344   loop_ivs_free (loop);
5345   if (reg_map)
5346     free (reg_map);
5347 }
5348 \f
5349 /*Record all basic induction variables calculated in the insn.  */
5350 static rtx
5351 check_insn_for_bivs (loop, p, not_every_iteration, maybe_multiple)
5352      struct loop *loop;
5353      rtx p;
5354      int not_every_iteration;
5355      int maybe_multiple;
5356 {
5357   struct loop_ivs *ivs = LOOP_IVS (loop);
5358   rtx set;
5359   rtx dest_reg;
5360   rtx inc_val;
5361   rtx mult_val;
5362   rtx *location;
5363
5364   if (GET_CODE (p) == INSN
5365       && (set = single_set (p))
5366       && GET_CODE (SET_DEST (set)) == REG)
5367     {
5368       dest_reg = SET_DEST (set);
5369       if (REGNO (dest_reg) < max_reg_before_loop
5370           && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
5371           && REG_IV_TYPE (ivs, REGNO (dest_reg)) != NOT_BASIC_INDUCT)
5372         {
5373           if (basic_induction_var (loop, SET_SRC (set),
5374                                    GET_MODE (SET_SRC (set)),
5375                                    dest_reg, p, &inc_val, &mult_val,
5376                                    &location))
5377             {
5378               /* It is a possible basic induction variable.
5379                  Create and initialize an induction structure for it.  */
5380
5381               struct induction *v
5382                 = (struct induction *) xmalloc (sizeof (struct induction));
5383
5384               record_biv (loop, v, p, dest_reg, inc_val, mult_val, location,
5385                           not_every_iteration, maybe_multiple);
5386               REG_IV_TYPE (ivs, REGNO (dest_reg)) = BASIC_INDUCT;
5387             }
5388           else if (REGNO (dest_reg) < ivs->n_regs)
5389             REG_IV_TYPE (ivs, REGNO (dest_reg)) = NOT_BASIC_INDUCT;
5390         }
5391     }
5392   return p;
5393 }
5394 \f
5395 /* Record all givs calculated in the insn.
5396    A register is a giv if: it is only set once, it is a function of a
5397    biv and a constant (or invariant), and it is not a biv.  */
5398 static rtx
5399 check_insn_for_givs (loop, p, not_every_iteration, maybe_multiple)
5400      struct loop *loop;
5401      rtx p;
5402      int not_every_iteration;
5403      int maybe_multiple;
5404 {
5405   struct loop_regs *regs = LOOP_REGS (loop);
5406
5407   rtx set;
5408   /* Look for a general induction variable in a register.  */
5409   if (GET_CODE (p) == INSN
5410       && (set = single_set (p))
5411       && GET_CODE (SET_DEST (set)) == REG
5412       && ! regs->array[REGNO (SET_DEST (set))].may_not_optimize)
5413     {
5414       rtx src_reg;
5415       rtx dest_reg;
5416       rtx add_val;
5417       rtx mult_val;
5418       rtx ext_val;
5419       int benefit;
5420       rtx regnote = 0;
5421       rtx last_consec_insn;
5422
5423       dest_reg = SET_DEST (set);
5424       if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
5425         return p;
5426
5427       if (/* SET_SRC is a giv.  */
5428           (general_induction_var (loop, SET_SRC (set), &src_reg, &add_val,
5429                                   &mult_val, &ext_val, 0, &benefit, VOIDmode)
5430            /* Equivalent expression is a giv.  */
5431            || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
5432                && general_induction_var (loop, XEXP (regnote, 0), &src_reg,
5433                                          &add_val, &mult_val, &ext_val, 0,
5434                                          &benefit, VOIDmode)))
5435           /* Don't try to handle any regs made by loop optimization.
5436              We have nothing on them in regno_first_uid, etc.  */
5437           && REGNO (dest_reg) < max_reg_before_loop
5438           /* Don't recognize a BASIC_INDUCT_VAR here.  */
5439           && dest_reg != src_reg
5440           /* This must be the only place where the register is set.  */
5441           && (regs->array[REGNO (dest_reg)].n_times_set == 1
5442               /* or all sets must be consecutive and make a giv.  */
5443               || (benefit = consec_sets_giv (loop, benefit, p,
5444                                              src_reg, dest_reg,
5445                                              &add_val, &mult_val, &ext_val,
5446                                              &last_consec_insn))))
5447         {
5448           struct induction *v
5449             = (struct induction *) xmalloc (sizeof (struct induction));
5450
5451           /* If this is a library call, increase benefit.  */
5452           if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5453             benefit += libcall_benefit (p);
5454
5455           /* Skip the consecutive insns, if there are any.  */
5456           if (regs->array[REGNO (dest_reg)].n_times_set != 1)
5457             p = last_consec_insn;
5458
5459           record_giv (loop, v, p, src_reg, dest_reg, mult_val, add_val,
5460                       ext_val, benefit, DEST_REG, not_every_iteration,
5461                       maybe_multiple, (rtx*) 0);
5462
5463         }
5464     }
5465
5466 #ifndef DONT_REDUCE_ADDR
5467   /* Look for givs which are memory addresses.  */
5468   /* This resulted in worse code on a VAX 8600.  I wonder if it
5469      still does.  */
5470   if (GET_CODE (p) == INSN)
5471     find_mem_givs (loop, PATTERN (p), p, not_every_iteration,
5472                    maybe_multiple);
5473 #endif
5474
5475   /* Update the status of whether giv can derive other givs.  This can
5476      change when we pass a label or an insn that updates a biv.  */
5477   if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5478       || GET_CODE (p) == CODE_LABEL)
5479     update_giv_derive (loop, p);
5480   return p;
5481 }
5482 \f
5483 /* Return 1 if X is a valid source for an initial value (or as value being
5484    compared against in an initial test).
5485
5486    X must be either a register or constant and must not be clobbered between
5487    the current insn and the start of the loop.
5488
5489    INSN is the insn containing X.  */
5490
5491 static int
5492 valid_initial_value_p (x, insn, call_seen, loop_start)
5493      rtx x;
5494      rtx insn;
5495      int call_seen;
5496      rtx loop_start;
5497 {
5498   if (CONSTANT_P (x))
5499     return 1;
5500
5501   /* Only consider pseudos we know about initialized in insns whose luids
5502      we know.  */
5503   if (GET_CODE (x) != REG
5504       || REGNO (x) >= max_reg_before_loop)
5505     return 0;
5506
5507   /* Don't use call-clobbered registers across a call which clobbers it.  On
5508      some machines, don't use any hard registers at all.  */
5509   if (REGNO (x) < FIRST_PSEUDO_REGISTER
5510       && (SMALL_REGISTER_CLASSES
5511           || (call_used_regs[REGNO (x)] && call_seen)))
5512     return 0;
5513
5514   /* Don't use registers that have been clobbered before the start of the
5515      loop.  */
5516   if (reg_set_between_p (x, insn, loop_start))
5517     return 0;
5518
5519   return 1;
5520 }
5521 \f
5522 /* Scan X for memory refs and check each memory address
5523    as a possible giv.  INSN is the insn whose pattern X comes from.
5524    NOT_EVERY_ITERATION is 1 if the insn might not be executed during
5525    every loop iteration.  MAYBE_MULTIPLE is 1 if the insn might be executed
5526    more thanonce in each loop iteration.  */
5527
5528 static void
5529 find_mem_givs (loop, x, insn, not_every_iteration, maybe_multiple)
5530      const struct loop *loop;
5531      rtx x;
5532      rtx insn;
5533      int not_every_iteration, maybe_multiple;
5534 {
5535   int i, j;
5536   enum rtx_code code;
5537   const char *fmt;
5538
5539   if (x == 0)
5540     return;
5541
5542   code = GET_CODE (x);
5543   switch (code)
5544     {
5545     case REG:
5546     case CONST_INT:
5547     case CONST:
5548     case CONST_DOUBLE:
5549     case SYMBOL_REF:
5550     case LABEL_REF:
5551     case PC:
5552     case CC0:
5553     case ADDR_VEC:
5554     case ADDR_DIFF_VEC:
5555     case USE:
5556     case CLOBBER:
5557       return;
5558
5559     case MEM:
5560       {
5561         rtx src_reg;
5562         rtx add_val;
5563         rtx mult_val;
5564         rtx ext_val;
5565         int benefit;
5566
5567         /* This code used to disable creating GIVs with mult_val == 1 and
5568            add_val == 0.  However, this leads to lost optimizations when
5569            it comes time to combine a set of related DEST_ADDR GIVs, since
5570            this one would not be seen.  */
5571
5572         if (general_induction_var (loop, XEXP (x, 0), &src_reg, &add_val,
5573                                    &mult_val, &ext_val, 1, &benefit,
5574                                    GET_MODE (x)))
5575           {
5576             /* Found one; record it.  */
5577             struct induction *v
5578               = (struct induction *) xmalloc (sizeof (struct induction));
5579
5580             record_giv (loop, v, insn, src_reg, addr_placeholder, mult_val,
5581                         add_val, ext_val, benefit, DEST_ADDR,
5582                         not_every_iteration, maybe_multiple, &XEXP (x, 0));
5583
5584             v->mem = x;
5585           }
5586       }
5587       return;
5588
5589     default:
5590       break;
5591     }
5592
5593   /* Recursively scan the subexpressions for other mem refs.  */
5594
5595   fmt = GET_RTX_FORMAT (code);
5596   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5597     if (fmt[i] == 'e')
5598       find_mem_givs (loop, XEXP (x, i), insn, not_every_iteration,
5599                      maybe_multiple);
5600     else if (fmt[i] == 'E')
5601       for (j = 0; j < XVECLEN (x, i); j++)
5602         find_mem_givs (loop, XVECEXP (x, i, j), insn, not_every_iteration,
5603                        maybe_multiple);
5604 }
5605 \f
5606 /* Fill in the data about one biv update.
5607    V is the `struct induction' in which we record the biv.  (It is
5608    allocated by the caller, with alloca.)
5609    INSN is the insn that sets it.
5610    DEST_REG is the biv's reg.
5611
5612    MULT_VAL is const1_rtx if the biv is being incremented here, in which case
5613    INC_VAL is the increment.  Otherwise, MULT_VAL is const0_rtx and the biv is
5614    being set to INC_VAL.
5615
5616    NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
5617    executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
5618    can be executed more than once per iteration.  If MAYBE_MULTIPLE
5619    and NOT_EVERY_ITERATION are both zero, we know that the biv update is
5620    executed exactly once per iteration.  */
5621
5622 static void
5623 record_biv (loop, v, insn, dest_reg, inc_val, mult_val, location,
5624             not_every_iteration, maybe_multiple)
5625      struct loop *loop;
5626      struct induction *v;
5627      rtx insn;
5628      rtx dest_reg;
5629      rtx inc_val;
5630      rtx mult_val;
5631      rtx *location;
5632      int not_every_iteration;
5633      int maybe_multiple;
5634 {
5635   struct loop_ivs *ivs = LOOP_IVS (loop);
5636   struct iv_class *bl;
5637
5638   v->insn = insn;
5639   v->src_reg = dest_reg;
5640   v->dest_reg = dest_reg;
5641   v->mult_val = mult_val;
5642   v->add_val = inc_val;
5643   v->ext_dependent = NULL_RTX;
5644   v->location = location;
5645   v->mode = GET_MODE (dest_reg);
5646   v->always_computable = ! not_every_iteration;
5647   v->always_executed = ! not_every_iteration;
5648   v->maybe_multiple = maybe_multiple;
5649
5650   /* Add this to the reg's iv_class, creating a class
5651      if this is the first incrementation of the reg.  */
5652
5653   bl = REG_IV_CLASS (ivs, REGNO (dest_reg));
5654   if (bl == 0)
5655     {
5656       /* Create and initialize new iv_class.  */
5657
5658       bl = (struct iv_class *) xmalloc (sizeof (struct iv_class));
5659
5660       bl->regno = REGNO (dest_reg);
5661       bl->biv = 0;
5662       bl->giv = 0;
5663       bl->biv_count = 0;
5664       bl->giv_count = 0;
5665
5666       /* Set initial value to the reg itself.  */
5667       bl->initial_value = dest_reg;
5668       bl->final_value = 0;
5669       /* We haven't seen the initializing insn yet */
5670       bl->init_insn = 0;
5671       bl->init_set = 0;
5672       bl->initial_test = 0;
5673       bl->incremented = 0;
5674       bl->eliminable = 0;
5675       bl->nonneg = 0;
5676       bl->reversed = 0;
5677       bl->total_benefit = 0;
5678
5679       /* Add this class to ivs->list.  */
5680       bl->next = ivs->list;
5681       ivs->list = bl;
5682
5683       /* Put it in the array of biv register classes.  */
5684       REG_IV_CLASS (ivs, REGNO (dest_reg)) = bl;
5685     }
5686
5687   /* Update IV_CLASS entry for this biv.  */
5688   v->next_iv = bl->biv;
5689   bl->biv = v;
5690   bl->biv_count++;
5691   if (mult_val == const1_rtx)
5692     bl->incremented = 1;
5693
5694   if (loop_dump_stream)
5695     loop_biv_dump (v, loop_dump_stream, 0);
5696 }
5697 \f
5698 /* Fill in the data about one giv.
5699    V is the `struct induction' in which we record the giv.  (It is
5700    allocated by the caller, with alloca.)
5701    INSN is the insn that sets it.
5702    BENEFIT estimates the savings from deleting this insn.
5703    TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
5704    into a register or is used as a memory address.
5705
5706    SRC_REG is the biv reg which the giv is computed from.
5707    DEST_REG is the giv's reg (if the giv is stored in a reg).
5708    MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
5709    LOCATION points to the place where this giv's value appears in INSN.  */
5710
5711 static void
5712 record_giv (loop, v, insn, src_reg, dest_reg, mult_val, add_val, ext_val,
5713             benefit, type, not_every_iteration, maybe_multiple, location)
5714      const struct loop *loop;
5715      struct induction *v;
5716      rtx insn;
5717      rtx src_reg;
5718      rtx dest_reg;
5719      rtx mult_val, add_val, ext_val;
5720      int benefit;
5721      enum g_types type;
5722      int not_every_iteration, maybe_multiple;
5723      rtx *location;
5724 {
5725   struct loop_ivs *ivs = LOOP_IVS (loop);
5726   struct induction *b;
5727   struct iv_class *bl;
5728   rtx set = single_set (insn);
5729   rtx temp;
5730
5731   /* Attempt to prove constantness of the values.  Don't let simplity_rtx
5732      undo the MULT canonicalization that we performed earlier.  */
5733   temp = simplify_rtx (add_val);
5734   if (temp
5735       && ! (GET_CODE (add_val) == MULT
5736             && GET_CODE (temp) == ASHIFT))
5737     add_val = temp;
5738
5739   v->insn = insn;
5740   v->src_reg = src_reg;
5741   v->giv_type = type;
5742   v->dest_reg = dest_reg;
5743   v->mult_val = mult_val;
5744   v->add_val = add_val;
5745   v->ext_dependent = ext_val;
5746   v->benefit = benefit;
5747   v->location = location;
5748   v->cant_derive = 0;
5749   v->combined_with = 0;
5750   v->maybe_multiple = maybe_multiple;
5751   v->maybe_dead = 0;
5752   v->derive_adjustment = 0;
5753   v->same = 0;
5754   v->ignore = 0;
5755   v->new_reg = 0;
5756   v->final_value = 0;
5757   v->same_insn = 0;
5758   v->auto_inc_opt = 0;
5759   v->unrolled = 0;
5760   v->shared = 0;
5761
5762   /* The v->always_computable field is used in update_giv_derive, to
5763      determine whether a giv can be used to derive another giv.  For a
5764      DEST_REG giv, INSN computes a new value for the giv, so its value
5765      isn't computable if INSN insn't executed every iteration.
5766      However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
5767      it does not compute a new value.  Hence the value is always computable
5768      regardless of whether INSN is executed each iteration.  */
5769
5770   if (type == DEST_ADDR)
5771     v->always_computable = 1;
5772   else
5773     v->always_computable = ! not_every_iteration;
5774
5775   v->always_executed = ! not_every_iteration;
5776
5777   if (type == DEST_ADDR)
5778     {
5779       v->mode = GET_MODE (*location);
5780       v->lifetime = 1;
5781     }
5782   else /* type == DEST_REG */
5783     {
5784       v->mode = GET_MODE (SET_DEST (set));
5785
5786       v->lifetime = LOOP_REG_LIFETIME (loop, REGNO (dest_reg));
5787
5788       /* If the lifetime is zero, it means that this register is
5789          really a dead store.  So mark this as a giv that can be
5790          ignored.  This will not prevent the biv from being eliminated.  */
5791       if (v->lifetime == 0)
5792         v->ignore = 1;
5793
5794       REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
5795       REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
5796     }
5797
5798   /* Add the giv to the class of givs computed from one biv.  */
5799
5800   bl = REG_IV_CLASS (ivs, REGNO (src_reg));
5801   if (bl)
5802     {
5803       v->next_iv = bl->giv;
5804       bl->giv = v;
5805       /* Don't count DEST_ADDR.  This is supposed to count the number of
5806          insns that calculate givs.  */
5807       if (type == DEST_REG)
5808         bl->giv_count++;
5809       bl->total_benefit += benefit;
5810     }
5811   else
5812     /* Fatal error, biv missing for this giv?  */
5813     abort ();
5814
5815   if (type == DEST_ADDR)
5816     v->replaceable = 1;
5817   else
5818     {
5819       /* The giv can be replaced outright by the reduced register only if all
5820          of the following conditions are true:
5821          - the insn that sets the giv is always executed on any iteration
5822            on which the giv is used at all
5823            (there are two ways to deduce this:
5824             either the insn is executed on every iteration,
5825             or all uses follow that insn in the same basic block),
5826          - the giv is not used outside the loop
5827          - no assignments to the biv occur during the giv's lifetime.  */
5828
5829       if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
5830           /* Previous line always fails if INSN was moved by loop opt.  */
5831           && REGNO_LAST_LUID (REGNO (dest_reg))
5832           < INSN_LUID (loop->end)
5833           && (! not_every_iteration
5834               || last_use_this_basic_block (dest_reg, insn)))
5835         {
5836           /* Now check that there are no assignments to the biv within the
5837              giv's lifetime.  This requires two separate checks.  */
5838
5839           /* Check each biv update, and fail if any are between the first
5840              and last use of the giv.
5841
5842              If this loop contains an inner loop that was unrolled, then
5843              the insn modifying the biv may have been emitted by the loop
5844              unrolling code, and hence does not have a valid luid.  Just
5845              mark the biv as not replaceable in this case.  It is not very
5846              useful as a biv, because it is used in two different loops.
5847              It is very unlikely that we would be able to optimize the giv
5848              using this biv anyways.  */
5849
5850           v->replaceable = 1;
5851           for (b = bl->biv; b; b = b->next_iv)
5852             {
5853               if (INSN_UID (b->insn) >= max_uid_for_loop
5854                   || ((INSN_LUID (b->insn)
5855                        >= REGNO_FIRST_LUID (REGNO (dest_reg)))
5856                       && (INSN_LUID (b->insn)
5857                           <= REGNO_LAST_LUID (REGNO (dest_reg)))))
5858                 {
5859                   v->replaceable = 0;
5860                   v->not_replaceable = 1;
5861                   break;
5862                 }
5863             }
5864
5865           /* If there are any backwards branches that go from after the
5866              biv update to before it, then this giv is not replaceable.  */
5867           if (v->replaceable)
5868             for (b = bl->biv; b; b = b->next_iv)
5869               if (back_branch_in_range_p (loop, b->insn))
5870                 {
5871                   v->replaceable = 0;
5872                   v->not_replaceable = 1;
5873                   break;
5874                 }
5875         }
5876       else
5877         {
5878           /* May still be replaceable, we don't have enough info here to
5879              decide.  */
5880           v->replaceable = 0;
5881           v->not_replaceable = 0;
5882         }
5883     }
5884
5885   /* Record whether the add_val contains a const_int, for later use by
5886      combine_givs.  */
5887   {
5888     rtx tem = add_val;
5889
5890     v->no_const_addval = 1;
5891     if (tem == const0_rtx)
5892       ;
5893     else if (CONSTANT_P (add_val))
5894       v->no_const_addval = 0;
5895     if (GET_CODE (tem) == PLUS)
5896       {
5897         while (1)
5898           {
5899             if (GET_CODE (XEXP (tem, 0)) == PLUS)
5900               tem = XEXP (tem, 0);
5901             else if (GET_CODE (XEXP (tem, 1)) == PLUS)
5902               tem = XEXP (tem, 1);
5903             else
5904               break;
5905           }
5906         if (CONSTANT_P (XEXP (tem, 1)))
5907           v->no_const_addval = 0;
5908       }
5909   }
5910
5911   if (loop_dump_stream)
5912     loop_giv_dump (v, loop_dump_stream, 0);
5913 }
5914
5915 /* All this does is determine whether a giv can be made replaceable because
5916    its final value can be calculated.  This code can not be part of record_giv
5917    above, because final_giv_value requires that the number of loop iterations
5918    be known, and that can not be accurately calculated until after all givs
5919    have been identified.  */
5920
5921 static void
5922 check_final_value (loop, v)
5923      const struct loop *loop;
5924      struct induction *v;
5925 {
5926   struct loop_ivs *ivs = LOOP_IVS (loop);
5927   struct iv_class *bl;
5928   rtx final_value = 0;
5929
5930   bl = REG_IV_CLASS (ivs, REGNO (v->src_reg));
5931
5932   /* DEST_ADDR givs will never reach here, because they are always marked
5933      replaceable above in record_giv.  */
5934
5935   /* The giv can be replaced outright by the reduced register only if all
5936      of the following conditions are true:
5937      - the insn that sets the giv is always executed on any iteration
5938        on which the giv is used at all
5939        (there are two ways to deduce this:
5940         either the insn is executed on every iteration,
5941         or all uses follow that insn in the same basic block),
5942      - its final value can be calculated (this condition is different
5943        than the one above in record_giv)
5944      - it's not used before the it's set
5945      - no assignments to the biv occur during the giv's lifetime.  */
5946
5947 #if 0
5948   /* This is only called now when replaceable is known to be false.  */
5949   /* Clear replaceable, so that it won't confuse final_giv_value.  */
5950   v->replaceable = 0;
5951 #endif
5952
5953   if ((final_value = final_giv_value (loop, v))
5954       && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
5955     {
5956       int biv_increment_seen = 0, before_giv_insn = 0;
5957       rtx p = v->insn;
5958       rtx last_giv_use;
5959
5960       v->replaceable = 1;
5961
5962       /* When trying to determine whether or not a biv increment occurs
5963          during the lifetime of the giv, we can ignore uses of the variable
5964          outside the loop because final_value is true.  Hence we can not
5965          use regno_last_uid and regno_first_uid as above in record_giv.  */
5966
5967       /* Search the loop to determine whether any assignments to the
5968          biv occur during the giv's lifetime.  Start with the insn
5969          that sets the giv, and search around the loop until we come
5970          back to that insn again.
5971
5972          Also fail if there is a jump within the giv's lifetime that jumps
5973          to somewhere outside the lifetime but still within the loop.  This
5974          catches spaghetti code where the execution order is not linear, and
5975          hence the above test fails.  Here we assume that the giv lifetime
5976          does not extend from one iteration of the loop to the next, so as
5977          to make the test easier.  Since the lifetime isn't known yet,
5978          this requires two loops.  See also record_giv above.  */
5979
5980       last_giv_use = v->insn;
5981
5982       while (1)
5983         {
5984           p = NEXT_INSN (p);
5985           if (p == loop->end)
5986             {
5987               before_giv_insn = 1;
5988               p = NEXT_INSN (loop->start);
5989             }
5990           if (p == v->insn)
5991             break;
5992
5993           if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
5994               || GET_CODE (p) == CALL_INSN)
5995             {
5996               /* It is possible for the BIV increment to use the GIV if we
5997                  have a cycle.  Thus we must be sure to check each insn for
5998                  both BIV and GIV uses, and we must check for BIV uses
5999                  first.  */
6000
6001               if (! biv_increment_seen
6002                   && reg_set_p (v->src_reg, PATTERN (p)))
6003                 biv_increment_seen = 1;
6004
6005               if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
6006                 {
6007                   if (biv_increment_seen || before_giv_insn)
6008                     {
6009                       v->replaceable = 0;
6010                       v->not_replaceable = 1;
6011                       break;
6012                     }
6013                   last_giv_use = p;
6014                 }
6015             }
6016         }
6017
6018       /* Now that the lifetime of the giv is known, check for branches
6019          from within the lifetime to outside the lifetime if it is still
6020          replaceable.  */
6021
6022       if (v->replaceable)
6023         {
6024           p = v->insn;
6025           while (1)
6026             {
6027               p = NEXT_INSN (p);
6028               if (p == loop->end)
6029                 p = NEXT_INSN (loop->start);
6030               if (p == last_giv_use)
6031                 break;
6032
6033               if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
6034                   && LABEL_NAME (JUMP_LABEL (p))
6035                   && ((loop_insn_first_p (JUMP_LABEL (p), v->insn)
6036                        && loop_insn_first_p (loop->start, JUMP_LABEL (p)))
6037                       || (loop_insn_first_p (last_giv_use, JUMP_LABEL (p))
6038                           && loop_insn_first_p (JUMP_LABEL (p), loop->end))))
6039                 {
6040                   v->replaceable = 0;
6041                   v->not_replaceable = 1;
6042
6043                   if (loop_dump_stream)
6044                     fprintf (loop_dump_stream,
6045                              "Found branch outside giv lifetime.\n");
6046
6047                   break;
6048                 }
6049             }
6050         }
6051
6052       /* If it is replaceable, then save the final value.  */
6053       if (v->replaceable)
6054         v->final_value = final_value;
6055     }
6056
6057   if (loop_dump_stream && v->replaceable)
6058     fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
6059              INSN_UID (v->insn), REGNO (v->dest_reg));
6060 }
6061 \f
6062 /* Update the status of whether a giv can derive other givs.
6063
6064    We need to do something special if there is or may be an update to the biv
6065    between the time the giv is defined and the time it is used to derive
6066    another giv.
6067
6068    In addition, a giv that is only conditionally set is not allowed to
6069    derive another giv once a label has been passed.
6070
6071    The cases we look at are when a label or an update to a biv is passed.  */
6072
6073 static void
6074 update_giv_derive (loop, p)
6075      const struct loop *loop;
6076      rtx p;
6077 {
6078   struct loop_ivs *ivs = LOOP_IVS (loop);
6079   struct iv_class *bl;
6080   struct induction *biv, *giv;
6081   rtx tem;
6082   int dummy;
6083
6084   /* Search all IV classes, then all bivs, and finally all givs.
6085
6086      There are three cases we are concerned with.  First we have the situation
6087      of a giv that is only updated conditionally.  In that case, it may not
6088      derive any givs after a label is passed.
6089
6090      The second case is when a biv update occurs, or may occur, after the
6091      definition of a giv.  For certain biv updates (see below) that are
6092      known to occur between the giv definition and use, we can adjust the
6093      giv definition.  For others, or when the biv update is conditional,
6094      we must prevent the giv from deriving any other givs.  There are two
6095      sub-cases within this case.
6096
6097      If this is a label, we are concerned with any biv update that is done
6098      conditionally, since it may be done after the giv is defined followed by
6099      a branch here (actually, we need to pass both a jump and a label, but
6100      this extra tracking doesn't seem worth it).
6101
6102      If this is a jump, we are concerned about any biv update that may be
6103      executed multiple times.  We are actually only concerned about
6104      backward jumps, but it is probably not worth performing the test
6105      on the jump again here.
6106
6107      If this is a biv update, we must adjust the giv status to show that a
6108      subsequent biv update was performed.  If this adjustment cannot be done,
6109      the giv cannot derive further givs.  */
6110
6111   for (bl = ivs->list; bl; bl = bl->next)
6112     for (biv = bl->biv; biv; biv = biv->next_iv)
6113       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
6114           || biv->insn == p)
6115         {
6116           for (giv = bl->giv; giv; giv = giv->next_iv)
6117             {
6118               /* If cant_derive is already true, there is no point in
6119                  checking all of these conditions again.  */
6120               if (giv->cant_derive)
6121                 continue;
6122
6123               /* If this giv is conditionally set and we have passed a label,
6124                  it cannot derive anything.  */
6125               if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
6126                 giv->cant_derive = 1;
6127
6128               /* Skip givs that have mult_val == 0, since
6129                  they are really invariants.  Also skip those that are
6130                  replaceable, since we know their lifetime doesn't contain
6131                  any biv update.  */
6132               else if (giv->mult_val == const0_rtx || giv->replaceable)
6133                 continue;
6134
6135               /* The only way we can allow this giv to derive another
6136                  is if this is a biv increment and we can form the product
6137                  of biv->add_val and giv->mult_val.  In this case, we will
6138                  be able to compute a compensation.  */
6139               else if (biv->insn == p)
6140                 {
6141                   rtx ext_val_dummy;
6142
6143                   tem = 0;
6144                   if (biv->mult_val == const1_rtx)
6145                     tem = simplify_giv_expr (loop,
6146                                              gen_rtx_MULT (giv->mode,
6147                                                            biv->add_val,
6148                                                            giv->mult_val),
6149                                              &ext_val_dummy, &dummy);
6150
6151                   if (tem && giv->derive_adjustment)
6152                     tem = simplify_giv_expr
6153                       (loop,
6154                        gen_rtx_PLUS (giv->mode, tem, giv->derive_adjustment),
6155                        &ext_val_dummy, &dummy);
6156
6157                   if (tem)
6158                     giv->derive_adjustment = tem;
6159                   else
6160                     giv->cant_derive = 1;
6161                 }
6162               else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
6163                        || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
6164                 giv->cant_derive = 1;
6165             }
6166         }
6167 }
6168 \f
6169 /* Check whether an insn is an increment legitimate for a basic induction var.
6170    X is the source of insn P, or a part of it.
6171    MODE is the mode in which X should be interpreted.
6172
6173    DEST_REG is the putative biv, also the destination of the insn.
6174    We accept patterns of these forms:
6175      REG = REG + INVARIANT (includes REG = REG - CONSTANT)
6176      REG = INVARIANT + REG
6177
6178    If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
6179    store the additive term into *INC_VAL, and store the place where
6180    we found the additive term into *LOCATION.
6181
6182    If X is an assignment of an invariant into DEST_REG, we set
6183    *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
6184
6185    We also want to detect a BIV when it corresponds to a variable
6186    whose mode was promoted via PROMOTED_MODE.  In that case, an increment
6187    of the variable may be a PLUS that adds a SUBREG of that variable to
6188    an invariant and then sign- or zero-extends the result of the PLUS
6189    into the variable.
6190
6191    Most GIVs in such cases will be in the promoted mode, since that is the
6192    probably the natural computation mode (and almost certainly the mode
6193    used for addresses) on the machine.  So we view the pseudo-reg containing
6194    the variable as the BIV, as if it were simply incremented.
6195
6196    Note that treating the entire pseudo as a BIV will result in making
6197    simple increments to any GIVs based on it.  However, if the variable
6198    overflows in its declared mode but not its promoted mode, the result will
6199    be incorrect.  This is acceptable if the variable is signed, since
6200    overflows in such cases are undefined, but not if it is unsigned, since
6201    those overflows are defined.  So we only check for SIGN_EXTEND and
6202    not ZERO_EXTEND.
6203
6204    If we cannot find a biv, we return 0.  */
6205
6206 static int
6207 basic_induction_var (loop, x, mode, dest_reg, p, inc_val, mult_val, location)
6208      const struct loop *loop;
6209      rtx x;
6210      enum machine_mode mode;
6211      rtx dest_reg;
6212      rtx p;
6213      rtx *inc_val;
6214      rtx *mult_val;
6215      rtx **location;
6216 {
6217   enum rtx_code code;
6218   rtx *argp, arg;
6219   rtx insn, set = 0;
6220
6221   code = GET_CODE (x);
6222   *location = NULL;
6223   switch (code)
6224     {
6225     case PLUS:
6226       if (rtx_equal_p (XEXP (x, 0), dest_reg)
6227           || (GET_CODE (XEXP (x, 0)) == SUBREG
6228               && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
6229               && SUBREG_REG (XEXP (x, 0)) == dest_reg))
6230         {
6231           argp = &XEXP (x, 1);
6232         }
6233       else if (rtx_equal_p (XEXP (x, 1), dest_reg)
6234                || (GET_CODE (XEXP (x, 1)) == SUBREG
6235                    && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
6236                    && SUBREG_REG (XEXP (x, 1)) == dest_reg))
6237         {
6238           argp = &XEXP (x, 0);
6239         }
6240       else
6241         return 0;
6242
6243       arg = *argp;
6244       if (loop_invariant_p (loop, arg) != 1)
6245         return 0;
6246
6247       *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
6248       *mult_val = const1_rtx;
6249       *location = argp;
6250       return 1;
6251
6252     case SUBREG:
6253       /* If what's inside the SUBREG is a BIV, then the SUBREG.  This will
6254          handle addition of promoted variables.
6255          ??? The comment at the start of this function is wrong: promoted
6256          variable increments don't look like it says they do.  */
6257       return basic_induction_var (loop, SUBREG_REG (x),
6258                                   GET_MODE (SUBREG_REG (x)),
6259                                   dest_reg, p, inc_val, mult_val, location);
6260
6261     case REG:
6262       /* If this register is assigned in a previous insn, look at its
6263          source, but don't go outside the loop or past a label.  */
6264
6265       /* If this sets a register to itself, we would repeat any previous
6266          biv increment if we applied this strategy blindly.  */
6267       if (rtx_equal_p (dest_reg, x))
6268         return 0;
6269
6270       insn = p;
6271       while (1)
6272         {
6273           rtx dest;
6274           do
6275             {
6276               insn = PREV_INSN (insn);
6277             }
6278           while (insn && GET_CODE (insn) == NOTE
6279                  && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6280
6281           if (!insn)
6282             break;
6283           set = single_set (insn);
6284           if (set == 0)
6285             break;
6286           dest = SET_DEST (set);
6287           if (dest == x
6288               || (GET_CODE (dest) == SUBREG
6289                   && (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
6290                   && (GET_MODE_CLASS (GET_MODE (dest)) == MODE_INT)
6291                   && SUBREG_REG (dest) == x))
6292             return basic_induction_var (loop, SET_SRC (set),
6293                                         (GET_MODE (SET_SRC (set)) == VOIDmode
6294                                          ? GET_MODE (x)
6295                                          : GET_MODE (SET_SRC (set))),
6296                                         dest_reg, insn,
6297                                         inc_val, mult_val, location);
6298
6299           while (GET_CODE (dest) == SIGN_EXTRACT
6300                  || GET_CODE (dest) == ZERO_EXTRACT
6301                  || GET_CODE (dest) == SUBREG
6302                  || GET_CODE (dest) == STRICT_LOW_PART)
6303             dest = XEXP (dest, 0);
6304           if (dest == x)
6305             break;
6306         }
6307       /* Fall through.  */
6308
6309       /* Can accept constant setting of biv only when inside inner most loop.
6310          Otherwise, a biv of an inner loop may be incorrectly recognized
6311          as a biv of the outer loop,
6312          causing code to be moved INTO the inner loop.  */
6313     case MEM:
6314       if (loop_invariant_p (loop, x) != 1)
6315         return 0;
6316     case CONST_INT:
6317     case SYMBOL_REF:
6318     case CONST:
6319       /* convert_modes aborts if we try to convert to or from CCmode, so just
6320          exclude that case.  It is very unlikely that a condition code value
6321          would be a useful iterator anyways.  convert_modes aborts if we try to
6322          convert a float mode to non-float or vice versa too.  */
6323       if (loop->level == 1
6324           && GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (dest_reg))
6325           && GET_MODE_CLASS (mode) != MODE_CC)
6326         {
6327           /* Possible bug here?  Perhaps we don't know the mode of X.  */
6328           *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
6329           *mult_val = const0_rtx;
6330           return 1;
6331         }
6332       else
6333         return 0;
6334
6335     case SIGN_EXTEND:
6336       return basic_induction_var (loop, XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6337                                   dest_reg, p, inc_val, mult_val, location);
6338
6339     case ASHIFTRT:
6340       /* Similar, since this can be a sign extension.  */
6341       for (insn = PREV_INSN (p);
6342            (insn && GET_CODE (insn) == NOTE
6343             && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
6344            insn = PREV_INSN (insn))
6345         ;
6346
6347       if (insn)
6348         set = single_set (insn);
6349
6350       if (! rtx_equal_p (dest_reg, XEXP (x, 0))
6351           && set && SET_DEST (set) == XEXP (x, 0)
6352           && GET_CODE (XEXP (x, 1)) == CONST_INT
6353           && INTVAL (XEXP (x, 1)) >= 0
6354           && GET_CODE (SET_SRC (set)) == ASHIFT
6355           && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
6356         return basic_induction_var (loop, XEXP (SET_SRC (set), 0),
6357                                     GET_MODE (XEXP (x, 0)),
6358                                     dest_reg, insn, inc_val, mult_val,
6359                                     location);
6360       return 0;
6361
6362     default:
6363       return 0;
6364     }
6365 }
6366 \f
6367 /* A general induction variable (giv) is any quantity that is a linear
6368    function   of a basic induction variable,
6369    i.e. giv = biv * mult_val + add_val.
6370    The coefficients can be any loop invariant quantity.
6371    A giv need not be computed directly from the biv;
6372    it can be computed by way of other givs.  */
6373
6374 /* Determine whether X computes a giv.
6375    If it does, return a nonzero value
6376      which is the benefit from eliminating the computation of X;
6377    set *SRC_REG to the register of the biv that it is computed from;
6378    set *ADD_VAL and *MULT_VAL to the coefficients,
6379      such that the value of X is biv * mult + add;  */
6380
6381 static int
6382 general_induction_var (loop, x, src_reg, add_val, mult_val, ext_val,
6383                        is_addr, pbenefit, addr_mode)
6384      const struct loop *loop;
6385      rtx x;
6386      rtx *src_reg;
6387      rtx *add_val;
6388      rtx *mult_val;
6389      rtx *ext_val;
6390      int is_addr;
6391      int *pbenefit;
6392      enum machine_mode addr_mode;
6393 {
6394   struct loop_ivs *ivs = LOOP_IVS (loop);
6395   rtx orig_x = x;
6396
6397   /* If this is an invariant, forget it, it isn't a giv.  */
6398   if (loop_invariant_p (loop, x) == 1)
6399     return 0;
6400
6401   *pbenefit = 0;
6402   *ext_val = NULL_RTX;
6403   x = simplify_giv_expr (loop, x, ext_val, pbenefit);
6404   if (x == 0)
6405     return 0;
6406
6407   switch (GET_CODE (x))
6408     {
6409     case USE:
6410     case CONST_INT:
6411       /* Since this is now an invariant and wasn't before, it must be a giv
6412          with MULT_VAL == 0.  It doesn't matter which BIV we associate this
6413          with.  */
6414       *src_reg = ivs->list->biv->dest_reg;
6415       *mult_val = const0_rtx;
6416       *add_val = x;
6417       break;
6418
6419     case REG:
6420       /* This is equivalent to a BIV.  */
6421       *src_reg = x;
6422       *mult_val = const1_rtx;
6423       *add_val = const0_rtx;
6424       break;
6425
6426     case PLUS:
6427       /* Either (plus (biv) (invar)) or
6428          (plus (mult (biv) (invar_1)) (invar_2)).  */
6429       if (GET_CODE (XEXP (x, 0)) == MULT)
6430         {
6431           *src_reg = XEXP (XEXP (x, 0), 0);
6432           *mult_val = XEXP (XEXP (x, 0), 1);
6433         }
6434       else
6435         {
6436           *src_reg = XEXP (x, 0);
6437           *mult_val = const1_rtx;
6438         }
6439       *add_val = XEXP (x, 1);
6440       break;
6441
6442     case MULT:
6443       /* ADD_VAL is zero.  */
6444       *src_reg = XEXP (x, 0);
6445       *mult_val = XEXP (x, 1);
6446       *add_val = const0_rtx;
6447       break;
6448
6449     default:
6450       abort ();
6451     }
6452
6453   /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
6454      unless they are CONST_INT).  */
6455   if (GET_CODE (*add_val) == USE)
6456     *add_val = XEXP (*add_val, 0);
6457   if (GET_CODE (*mult_val) == USE)
6458     *mult_val = XEXP (*mult_val, 0);
6459
6460   if (is_addr)
6461     *pbenefit += address_cost (orig_x, addr_mode) - reg_address_cost;
6462   else
6463     *pbenefit += rtx_cost (orig_x, SET);
6464
6465   /* Always return true if this is a giv so it will be detected as such,
6466      even if the benefit is zero or negative.  This allows elimination
6467      of bivs that might otherwise not be eliminated.  */
6468   return 1;
6469 }
6470 \f
6471 /* Given an expression, X, try to form it as a linear function of a biv.
6472    We will canonicalize it to be of the form
6473         (plus (mult (BIV) (invar_1))
6474               (invar_2))
6475    with possible degeneracies.
6476
6477    The invariant expressions must each be of a form that can be used as a
6478    machine operand.  We surround then with a USE rtx (a hack, but localized
6479    and certainly unambiguous!) if not a CONST_INT for simplicity in this
6480    routine; it is the caller's responsibility to strip them.
6481
6482    If no such canonicalization is possible (i.e., two biv's are used or an
6483    expression that is neither invariant nor a biv or giv), this routine
6484    returns 0.
6485
6486    For a non-zero return, the result will have a code of CONST_INT, USE,
6487    REG (for a BIV), PLUS, or MULT.  No other codes will occur.
6488
6489    *BENEFIT will be incremented by the benefit of any sub-giv encountered.  */
6490
6491 static rtx sge_plus PARAMS ((enum machine_mode, rtx, rtx));
6492 static rtx sge_plus_constant PARAMS ((rtx, rtx));
6493
6494 static rtx
6495 simplify_giv_expr (loop, x, ext_val, benefit)
6496      const struct loop *loop;
6497      rtx x;
6498      rtx *ext_val;
6499      int *benefit;
6500 {
6501   struct loop_ivs *ivs = LOOP_IVS (loop);
6502   struct loop_regs *regs = LOOP_REGS (loop);
6503   enum machine_mode mode = GET_MODE (x);
6504   rtx arg0, arg1;
6505   rtx tem;
6506
6507   /* If this is not an integer mode, or if we cannot do arithmetic in this
6508      mode, this can't be a giv.  */
6509   if (mode != VOIDmode
6510       && (GET_MODE_CLASS (mode) != MODE_INT
6511           || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
6512     return NULL_RTX;
6513
6514   switch (GET_CODE (x))
6515     {
6516     case PLUS:
6517       arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6518       arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6519       if (arg0 == 0 || arg1 == 0)
6520         return NULL_RTX;
6521
6522       /* Put constant last, CONST_INT last if both constant.  */
6523       if ((GET_CODE (arg0) == USE
6524            || GET_CODE (arg0) == CONST_INT)
6525           && ! ((GET_CODE (arg0) == USE
6526                  && GET_CODE (arg1) == USE)
6527                 || GET_CODE (arg1) == CONST_INT))
6528         tem = arg0, arg0 = arg1, arg1 = tem;
6529
6530       /* Handle addition of zero, then addition of an invariant.  */
6531       if (arg1 == const0_rtx)
6532         return arg0;
6533       else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
6534         switch (GET_CODE (arg0))
6535           {
6536           case CONST_INT:
6537           case USE:
6538             /* Adding two invariants must result in an invariant, so enclose
6539                addition operation inside a USE and return it.  */
6540             if (GET_CODE (arg0) == USE)
6541               arg0 = XEXP (arg0, 0);
6542             if (GET_CODE (arg1) == USE)
6543               arg1 = XEXP (arg1, 0);
6544
6545             if (GET_CODE (arg0) == CONST_INT)
6546               tem = arg0, arg0 = arg1, arg1 = tem;
6547             if (GET_CODE (arg1) == CONST_INT)
6548               tem = sge_plus_constant (arg0, arg1);
6549             else
6550               tem = sge_plus (mode, arg0, arg1);
6551
6552             if (GET_CODE (tem) != CONST_INT)
6553               tem = gen_rtx_USE (mode, tem);
6554             return tem;
6555
6556           case REG:
6557           case MULT:
6558             /* biv + invar or mult + invar.  Return sum.  */
6559             return gen_rtx_PLUS (mode, arg0, arg1);
6560
6561           case PLUS:
6562             /* (a + invar_1) + invar_2.  Associate.  */
6563             return
6564               simplify_giv_expr (loop,
6565                                  gen_rtx_PLUS (mode,
6566                                                XEXP (arg0, 0),
6567                                                gen_rtx_PLUS (mode,
6568                                                              XEXP (arg0, 1),
6569                                                              arg1)),
6570                                  ext_val, benefit);
6571
6572           default:
6573             abort ();
6574           }
6575
6576       /* Each argument must be either REG, PLUS, or MULT.  Convert REG to
6577          MULT to reduce cases.  */
6578       if (GET_CODE (arg0) == REG)
6579         arg0 = gen_rtx_MULT (mode, arg0, const1_rtx);
6580       if (GET_CODE (arg1) == REG)
6581         arg1 = gen_rtx_MULT (mode, arg1, const1_rtx);
6582
6583       /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
6584          Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
6585          Recurse to associate the second PLUS.  */
6586       if (GET_CODE (arg1) == MULT)
6587         tem = arg0, arg0 = arg1, arg1 = tem;
6588
6589       if (GET_CODE (arg1) == PLUS)
6590         return
6591           simplify_giv_expr (loop,
6592                              gen_rtx_PLUS (mode,
6593                                            gen_rtx_PLUS (mode, arg0,
6594                                                          XEXP (arg1, 0)),
6595                                            XEXP (arg1, 1)),
6596                              ext_val, benefit);
6597
6598       /* Now must have MULT + MULT.  Distribute if same biv, else not giv.  */
6599       if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
6600         return NULL_RTX;
6601
6602       if (!rtx_equal_p (arg0, arg1))
6603         return NULL_RTX;
6604
6605       return simplify_giv_expr (loop,
6606                                 gen_rtx_MULT (mode,
6607                                               XEXP (arg0, 0),
6608                                               gen_rtx_PLUS (mode,
6609                                                             XEXP (arg0, 1),
6610                                                             XEXP (arg1, 1))),
6611                                 ext_val, benefit);
6612
6613     case MINUS:
6614       /* Handle "a - b" as "a + b * (-1)".  */
6615       return simplify_giv_expr (loop,
6616                                 gen_rtx_PLUS (mode,
6617                                               XEXP (x, 0),
6618                                               gen_rtx_MULT (mode,
6619                                                             XEXP (x, 1),
6620                                                             constm1_rtx)),
6621                                 ext_val, benefit);
6622
6623     case MULT:
6624       arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6625       arg1 = simplify_giv_expr (loop, XEXP (x, 1), ext_val, benefit);
6626       if (arg0 == 0 || arg1 == 0)
6627         return NULL_RTX;
6628
6629       /* Put constant last, CONST_INT last if both constant.  */
6630       if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
6631           && GET_CODE (arg1) != CONST_INT)
6632         tem = arg0, arg0 = arg1, arg1 = tem;
6633
6634       /* If second argument is not now constant, not giv.  */
6635       if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
6636         return NULL_RTX;
6637
6638       /* Handle multiply by 0 or 1.  */
6639       if (arg1 == const0_rtx)
6640         return const0_rtx;
6641
6642       else if (arg1 == const1_rtx)
6643         return arg0;
6644
6645       switch (GET_CODE (arg0))
6646         {
6647         case REG:
6648           /* biv * invar.  Done.  */
6649           return gen_rtx_MULT (mode, arg0, arg1);
6650
6651         case CONST_INT:
6652           /* Product of two constants.  */
6653           return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
6654
6655         case USE:
6656           /* invar * invar is a giv, but attempt to simplify it somehow.  */
6657           if (GET_CODE (arg1) != CONST_INT)
6658             return NULL_RTX;
6659
6660           arg0 = XEXP (arg0, 0);
6661           if (GET_CODE (arg0) == MULT)
6662             {
6663               /* (invar_0 * invar_1) * invar_2.  Associate.  */
6664               return simplify_giv_expr (loop,
6665                                         gen_rtx_MULT (mode,
6666                                                       XEXP (arg0, 0),
6667                                                       gen_rtx_MULT (mode,
6668                                                                     XEXP (arg0,
6669                                                                           1),
6670                                                                     arg1)),
6671                                         ext_val, benefit);
6672             }
6673           /* Porpagate the MULT expressions to the intermost nodes.  */
6674           else if (GET_CODE (arg0) == PLUS)
6675             {
6676               /* (invar_0 + invar_1) * invar_2.  Distribute.  */
6677               return simplify_giv_expr (loop,
6678                                         gen_rtx_PLUS (mode,
6679                                                       gen_rtx_MULT (mode,
6680                                                                     XEXP (arg0,
6681                                                                           0),
6682                                                                     arg1),
6683                                                       gen_rtx_MULT (mode,
6684                                                                     XEXP (arg0,
6685                                                                           1),
6686                                                                     arg1)),
6687                                         ext_val, benefit);
6688             }
6689           return gen_rtx_USE (mode, gen_rtx_MULT (mode, arg0, arg1));
6690
6691         case MULT:
6692           /* (a * invar_1) * invar_2.  Associate.  */
6693           return simplify_giv_expr (loop,
6694                                     gen_rtx_MULT (mode,
6695                                                   XEXP (arg0, 0),
6696                                                   gen_rtx_MULT (mode,
6697                                                                 XEXP (arg0, 1),
6698                                                                 arg1)),
6699                                     ext_val, benefit);
6700
6701         case PLUS:
6702           /* (a + invar_1) * invar_2.  Distribute.  */
6703           return simplify_giv_expr (loop,
6704                                     gen_rtx_PLUS (mode,
6705                                                   gen_rtx_MULT (mode,
6706                                                                 XEXP (arg0, 0),
6707                                                                 arg1),
6708                                                   gen_rtx_MULT (mode,
6709                                                                 XEXP (arg0, 1),
6710                                                                 arg1)),
6711                                     ext_val, benefit);
6712
6713         default:
6714           abort ();
6715         }
6716
6717     case ASHIFT:
6718       /* Shift by constant is multiply by power of two.  */
6719       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6720         return 0;
6721
6722       return
6723         simplify_giv_expr (loop,
6724                            gen_rtx_MULT (mode,
6725                                          XEXP (x, 0),
6726                                          GEN_INT ((HOST_WIDE_INT) 1
6727                                                   << INTVAL (XEXP (x, 1)))),
6728                            ext_val, benefit);
6729
6730     case NEG:
6731       /* "-a" is "a * (-1)" */
6732       return simplify_giv_expr (loop,
6733                                 gen_rtx_MULT (mode, XEXP (x, 0), constm1_rtx),
6734                                 ext_val, benefit);
6735
6736     case NOT:
6737       /* "~a" is "-a - 1". Silly, but easy.  */
6738       return simplify_giv_expr (loop,
6739                                 gen_rtx_MINUS (mode,
6740                                                gen_rtx_NEG (mode, XEXP (x, 0)),
6741                                                const1_rtx),
6742                                 ext_val, benefit);
6743
6744     case USE:
6745       /* Already in proper form for invariant.  */
6746       return x;
6747
6748     case SIGN_EXTEND:
6749     case ZERO_EXTEND:
6750     case TRUNCATE:
6751       /* Conditionally recognize extensions of simple IVs.  After we've
6752          computed loop traversal counts and verified the range of the
6753          source IV, we'll reevaluate this as a GIV.  */
6754       if (*ext_val == NULL_RTX)
6755         {
6756           arg0 = simplify_giv_expr (loop, XEXP (x, 0), ext_val, benefit);
6757           if (arg0 && *ext_val == NULL_RTX && GET_CODE (arg0) == REG)
6758             {
6759               *ext_val = gen_rtx_fmt_e (GET_CODE (x), mode, arg0);
6760               return arg0;
6761             }
6762         }
6763       goto do_default;
6764
6765     case REG:
6766       /* If this is a new register, we can't deal with it.  */
6767       if (REGNO (x) >= max_reg_before_loop)
6768         return 0;
6769
6770       /* Check for biv or giv.  */
6771       switch (REG_IV_TYPE (ivs, REGNO (x)))
6772         {
6773         case BASIC_INDUCT:
6774           return x;
6775         case GENERAL_INDUCT:
6776           {
6777             struct induction *v = REG_IV_INFO (ivs, REGNO (x));
6778
6779             /* Form expression from giv and add benefit.  Ensure this giv
6780                can derive another and subtract any needed adjustment if so.  */
6781
6782             /* Increasing the benefit here is risky.  The only case in which it
6783                is arguably correct is if this is the only use of V.  In other
6784                cases, this will artificially inflate the benefit of the current
6785                giv, and lead to suboptimal code.  Thus, it is disabled, since
6786                potentially not reducing an only marginally beneficial giv is
6787                less harmful than reducing many givs that are not really
6788                beneficial.  */
6789             {
6790               rtx single_use = regs->array[REGNO (x)].single_usage;
6791               if (single_use && single_use != const0_rtx)
6792                 *benefit += v->benefit;
6793             }
6794
6795             if (v->cant_derive)
6796               return 0;
6797
6798             tem = gen_rtx_PLUS (mode, gen_rtx_MULT (mode,
6799                                                     v->src_reg, v->mult_val),
6800                                 v->add_val);
6801
6802             if (v->derive_adjustment)
6803               tem = gen_rtx_MINUS (mode, tem, v->derive_adjustment);
6804             arg0 = simplify_giv_expr (loop, tem, ext_val, benefit);
6805             if (*ext_val)
6806               {
6807                 if (!v->ext_dependent)
6808                   return arg0;
6809               }
6810             else
6811               {
6812                 *ext_val = v->ext_dependent;
6813                 return arg0;
6814               }
6815             return 0;
6816           }
6817
6818         default:
6819         do_default:
6820           /* If it isn't an induction variable, and it is invariant, we
6821              may be able to simplify things further by looking through
6822              the bits we just moved outside the loop.  */
6823           if (loop_invariant_p (loop, x) == 1)
6824             {
6825               struct movable *m;
6826               struct loop_movables *movables = LOOP_MOVABLES (loop);
6827
6828               for (m = movables->head; m; m = m->next)
6829                 if (rtx_equal_p (x, m->set_dest))
6830                   {
6831                     /* Ok, we found a match.  Substitute and simplify.  */
6832
6833                     /* If we match another movable, we must use that, as
6834                        this one is going away.  */
6835                     if (m->match)
6836                       return simplify_giv_expr (loop, m->match->set_dest,
6837                                                 ext_val, benefit);
6838
6839                     /* If consec is non-zero, this is a member of a group of
6840                        instructions that were moved together.  We handle this
6841                        case only to the point of seeking to the last insn and
6842                        looking for a REG_EQUAL.  Fail if we don't find one.  */
6843                     if (m->consec != 0)
6844                       {
6845                         int i = m->consec;
6846                         tem = m->insn;
6847                         do
6848                           {
6849                             tem = NEXT_INSN (tem);
6850                           }
6851                         while (--i > 0);
6852
6853                         tem = find_reg_note (tem, REG_EQUAL, NULL_RTX);
6854                         if (tem)
6855                           tem = XEXP (tem, 0);
6856                       }
6857                     else
6858                       {
6859                         tem = single_set (m->insn);
6860                         if (tem)
6861                           tem = SET_SRC (tem);
6862                       }
6863
6864                     if (tem)
6865                       {
6866                         /* What we are most interested in is pointer
6867                            arithmetic on invariants -- only take
6868                            patterns we may be able to do something with.  */
6869                         if (GET_CODE (tem) == PLUS
6870                             || GET_CODE (tem) == MULT
6871                             || GET_CODE (tem) == ASHIFT
6872                             || GET_CODE (tem) == CONST_INT
6873                             || GET_CODE (tem) == SYMBOL_REF)
6874                           {
6875                             tem = simplify_giv_expr (loop, tem, ext_val,
6876                                                      benefit);
6877                             if (tem)
6878                               return tem;
6879                           }
6880                         else if (GET_CODE (tem) == CONST
6881                                  && GET_CODE (XEXP (tem, 0)) == PLUS
6882                                  && GET_CODE (XEXP (XEXP (tem, 0), 0)) == SYMBOL_REF
6883                                  && GET_CODE (XEXP (XEXP (tem, 0), 1)) == CONST_INT)
6884                           {
6885                             tem = simplify_giv_expr (loop, XEXP (tem, 0),
6886                                                      ext_val, benefit);
6887                             if (tem)
6888                               return tem;
6889                           }
6890                       }
6891                     break;
6892                   }
6893             }
6894           break;
6895         }
6896
6897       /* Fall through to general case.  */
6898     default:
6899       /* If invariant, return as USE (unless CONST_INT).
6900          Otherwise, not giv.  */
6901       if (GET_CODE (x) == USE)
6902         x = XEXP (x, 0);
6903
6904       if (loop_invariant_p (loop, x) == 1)
6905         {
6906           if (GET_CODE (x) == CONST_INT)
6907             return x;
6908           if (GET_CODE (x) == CONST
6909               && GET_CODE (XEXP (x, 0)) == PLUS
6910               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6911               && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
6912             x = XEXP (x, 0);
6913           return gen_rtx_USE (mode, x);
6914         }
6915       else
6916         return 0;
6917     }
6918 }
6919
6920 /* This routine folds invariants such that there is only ever one
6921    CONST_INT in the summation.  It is only used by simplify_giv_expr.  */
6922
6923 static rtx
6924 sge_plus_constant (x, c)
6925      rtx x, c;
6926 {
6927   if (GET_CODE (x) == CONST_INT)
6928     return GEN_INT (INTVAL (x) + INTVAL (c));
6929   else if (GET_CODE (x) != PLUS)
6930     return gen_rtx_PLUS (GET_MODE (x), x, c);
6931   else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6932     {
6933       return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0),
6934                            GEN_INT (INTVAL (XEXP (x, 1)) + INTVAL (c)));
6935     }
6936   else if (GET_CODE (XEXP (x, 0)) == PLUS
6937            || GET_CODE (XEXP (x, 1)) != PLUS)
6938     {
6939       return gen_rtx_PLUS (GET_MODE (x),
6940                            sge_plus_constant (XEXP (x, 0), c), XEXP (x, 1));
6941     }
6942   else
6943     {
6944       return gen_rtx_PLUS (GET_MODE (x),
6945                            sge_plus_constant (XEXP (x, 1), c), XEXP (x, 0));
6946     }
6947 }
6948
6949 static rtx
6950 sge_plus (mode, x, y)
6951      enum machine_mode mode;
6952      rtx x, y;
6953 {
6954   while (GET_CODE (y) == PLUS)
6955     {
6956       rtx a = XEXP (y, 0);
6957       if (GET_CODE (a) == CONST_INT)
6958         x = sge_plus_constant (x, a);
6959       else
6960         x = gen_rtx_PLUS (mode, x, a);
6961       y = XEXP (y, 1);
6962     }
6963   if (GET_CODE (y) == CONST_INT)
6964     x = sge_plus_constant (x, y);
6965   else
6966     x = gen_rtx_PLUS (mode, x, y);
6967   return x;
6968 }
6969 \f
6970 /* Help detect a giv that is calculated by several consecutive insns;
6971    for example,
6972       giv = biv * M
6973       giv = giv + A
6974    The caller has already identified the first insn P as having a giv as dest;
6975    we check that all other insns that set the same register follow
6976    immediately after P, that they alter nothing else,
6977    and that the result of the last is still a giv.
6978
6979    The value is 0 if the reg set in P is not really a giv.
6980    Otherwise, the value is the amount gained by eliminating
6981    all the consecutive insns that compute the value.
6982
6983    FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
6984    SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
6985
6986    The coefficients of the ultimate giv value are stored in
6987    *MULT_VAL and *ADD_VAL.  */
6988
6989 static int
6990 consec_sets_giv (loop, first_benefit, p, src_reg, dest_reg,
6991                  add_val, mult_val, ext_val, last_consec_insn)
6992      const struct loop *loop;
6993      int first_benefit;
6994      rtx p;
6995      rtx src_reg;
6996      rtx dest_reg;
6997      rtx *add_val;
6998      rtx *mult_val;
6999      rtx *ext_val;
7000      rtx *last_consec_insn;
7001 {
7002   struct loop_ivs *ivs = LOOP_IVS (loop);
7003   struct loop_regs *regs = LOOP_REGS (loop);
7004   int count;
7005   enum rtx_code code;
7006   int benefit;
7007   rtx temp;
7008   rtx set;
7009
7010   /* Indicate that this is a giv so that we can update the value produced in
7011      each insn of the multi-insn sequence.
7012
7013      This induction structure will be used only by the call to
7014      general_induction_var below, so we can allocate it on our stack.
7015      If this is a giv, our caller will replace the induct var entry with
7016      a new induction structure.  */
7017   struct induction *v;
7018
7019   if (REG_IV_TYPE (ivs, REGNO (dest_reg)) != UNKNOWN_INDUCT)
7020     return 0;
7021
7022   v = (struct induction *) alloca (sizeof (struct induction));
7023   v->src_reg = src_reg;
7024   v->mult_val = *mult_val;
7025   v->add_val = *add_val;
7026   v->benefit = first_benefit;
7027   v->cant_derive = 0;
7028   v->derive_adjustment = 0;
7029   v->ext_dependent = NULL_RTX;
7030
7031   REG_IV_TYPE (ivs, REGNO (dest_reg)) = GENERAL_INDUCT;
7032   REG_IV_INFO (ivs, REGNO (dest_reg)) = v;
7033
7034   count = regs->array[REGNO (dest_reg)].n_times_set - 1;
7035
7036   while (count > 0)
7037     {
7038       p = NEXT_INSN (p);
7039       code = GET_CODE (p);
7040
7041       /* If libcall, skip to end of call sequence.  */
7042       if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
7043         p = XEXP (temp, 0);
7044
7045       if (code == INSN
7046           && (set = single_set (p))
7047           && GET_CODE (SET_DEST (set)) == REG
7048           && SET_DEST (set) == dest_reg
7049           && (general_induction_var (loop, SET_SRC (set), &src_reg,
7050                                      add_val, mult_val, ext_val, 0,
7051                                      &benefit, VOIDmode)
7052               /* Giv created by equivalent expression.  */
7053               || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
7054                   && general_induction_var (loop, XEXP (temp, 0), &src_reg,
7055                                             add_val, mult_val, ext_val, 0,
7056                                             &benefit, VOIDmode)))
7057           && src_reg == v->src_reg)
7058         {
7059           if (find_reg_note (p, REG_RETVAL, NULL_RTX))
7060             benefit += libcall_benefit (p);
7061
7062           count--;
7063           v->mult_val = *mult_val;
7064           v->add_val = *add_val;
7065           v->benefit += benefit;
7066         }
7067       else if (code != NOTE)
7068         {
7069           /* Allow insns that set something other than this giv to a
7070              constant.  Such insns are needed on machines which cannot
7071              include long constants and should not disqualify a giv.  */
7072           if (code == INSN
7073               && (set = single_set (p))
7074               && SET_DEST (set) != dest_reg
7075               && CONSTANT_P (SET_SRC (set)))
7076             continue;
7077
7078           REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7079           return 0;
7080         }
7081     }
7082
7083   REG_IV_TYPE (ivs, REGNO (dest_reg)) = UNKNOWN_INDUCT;
7084   *last_consec_insn = p;
7085   return v->benefit;
7086 }
7087 \f
7088 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7089    represented by G1.  If no such expression can be found, or it is clear that
7090    it cannot possibly be a valid address, 0 is returned.
7091
7092    To perform the computation, we note that
7093         G1 = x * v + a          and
7094         G2 = y * v + b
7095    where `v' is the biv.
7096
7097    So G2 = (y/b) * G1 + (b - a*y/x).
7098
7099    Note that MULT = y/x.
7100
7101    Update: A and B are now allowed to be additive expressions such that
7102    B contains all variables in A.  That is, computing B-A will not require
7103    subtracting variables.  */
7104
7105 static rtx
7106 express_from_1 (a, b, mult)
7107      rtx a, b, mult;
7108 {
7109   /* If MULT is zero, then A*MULT is zero, and our expression is B.  */
7110
7111   if (mult == const0_rtx)
7112     return b;
7113
7114   /* If MULT is not 1, we cannot handle A with non-constants, since we
7115      would then be required to subtract multiples of the registers in A.
7116      This is theoretically possible, and may even apply to some Fortran
7117      constructs, but it is a lot of work and we do not attempt it here.  */
7118
7119   if (mult != const1_rtx && GET_CODE (a) != CONST_INT)
7120     return NULL_RTX;
7121
7122   /* In general these structures are sorted top to bottom (down the PLUS
7123      chain), but not left to right across the PLUS.  If B is a higher
7124      order giv than A, we can strip one level and recurse.  If A is higher
7125      order, we'll eventually bail out, but won't know that until the end.
7126      If they are the same, we'll strip one level around this loop.  */
7127
7128   while (GET_CODE (a) == PLUS && GET_CODE (b) == PLUS)
7129     {
7130       rtx ra, rb, oa, ob, tmp;
7131
7132       ra = XEXP (a, 0), oa = XEXP (a, 1);
7133       if (GET_CODE (ra) == PLUS)
7134         tmp = ra, ra = oa, oa = tmp;
7135
7136       rb = XEXP (b, 0), ob = XEXP (b, 1);
7137       if (GET_CODE (rb) == PLUS)
7138         tmp = rb, rb = ob, ob = tmp;
7139
7140       if (rtx_equal_p (ra, rb))
7141         /* We matched: remove one reg completely.  */
7142         a = oa, b = ob;
7143       else if (GET_CODE (ob) != PLUS && rtx_equal_p (ra, ob))
7144         /* An alternate match.  */
7145         a = oa, b = rb;
7146       else if (GET_CODE (oa) != PLUS && rtx_equal_p (oa, rb))
7147         /* An alternate match.  */
7148         a = ra, b = ob;
7149       else
7150         {
7151           /* Indicates an extra register in B.  Strip one level from B and
7152              recurse, hoping B was the higher order expression.  */
7153           ob = express_from_1 (a, ob, mult);
7154           if (ob == NULL_RTX)
7155             return NULL_RTX;
7156           return gen_rtx_PLUS (GET_MODE (b), rb, ob);
7157         }
7158     }
7159
7160   /* Here we are at the last level of A, go through the cases hoping to
7161      get rid of everything but a constant.  */
7162
7163   if (GET_CODE (a) == PLUS)
7164     {
7165       rtx ra, oa;
7166
7167       ra = XEXP (a, 0), oa = XEXP (a, 1);
7168       if (rtx_equal_p (oa, b))
7169         oa = ra;
7170       else if (!rtx_equal_p (ra, b))
7171         return NULL_RTX;
7172
7173       if (GET_CODE (oa) != CONST_INT)
7174         return NULL_RTX;
7175
7176       return GEN_INT (-INTVAL (oa) * INTVAL (mult));
7177     }
7178   else if (GET_CODE (a) == CONST_INT)
7179     {
7180       return plus_constant (b, -INTVAL (a) * INTVAL (mult));
7181     }
7182   else if (CONSTANT_P (a))
7183     {
7184       enum machine_mode mode_a = GET_MODE (a);
7185       enum machine_mode mode_b = GET_MODE (b);
7186       enum machine_mode mode = mode_b == VOIDmode ? mode_a : mode_b;
7187       return simplify_gen_binary (MINUS, mode, b, a);
7188     }
7189   else if (GET_CODE (b) == PLUS)
7190     {
7191       if (rtx_equal_p (a, XEXP (b, 0)))
7192         return XEXP (b, 1);
7193       else if (rtx_equal_p (a, XEXP (b, 1)))
7194         return XEXP (b, 0);
7195       else
7196         return NULL_RTX;
7197     }
7198   else if (rtx_equal_p (a, b))
7199     return const0_rtx;
7200
7201   return NULL_RTX;
7202 }
7203
7204 rtx
7205 express_from (g1, g2)
7206      struct induction *g1, *g2;
7207 {
7208   rtx mult, add;
7209
7210   /* The value that G1 will be multiplied by must be a constant integer.  Also,
7211      the only chance we have of getting a valid address is if b*c/a (see above
7212      for notation) is also an integer.  */
7213   if (GET_CODE (g1->mult_val) == CONST_INT
7214       && GET_CODE (g2->mult_val) == CONST_INT)
7215     {
7216       if (g1->mult_val == const0_rtx
7217           || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
7218         return NULL_RTX;
7219       mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
7220     }
7221   else if (rtx_equal_p (g1->mult_val, g2->mult_val))
7222     mult = const1_rtx;
7223   else
7224     {
7225       /* ??? Find out if the one is a multiple of the other?  */
7226       return NULL_RTX;
7227     }
7228
7229   add = express_from_1 (g1->add_val, g2->add_val, mult);
7230   if (add == NULL_RTX)
7231     {
7232       /* Failed.  If we've got a multiplication factor between G1 and G2,
7233          scale G1's addend and try again.  */
7234       if (INTVAL (mult) > 1)
7235         {
7236           rtx g1_add_val = g1->add_val;
7237           if (GET_CODE (g1_add_val) == MULT
7238               && GET_CODE (XEXP (g1_add_val, 1)) == CONST_INT)
7239             {
7240               HOST_WIDE_INT m;
7241               m = INTVAL (mult) * INTVAL (XEXP (g1_add_val, 1));
7242               g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val),
7243                                          XEXP (g1_add_val, 0), GEN_INT (m));
7244             }
7245           else
7246             {
7247               g1_add_val = gen_rtx_MULT (GET_MODE (g1_add_val), g1_add_val,
7248                                          mult);
7249             }
7250
7251           add = express_from_1 (g1_add_val, g2->add_val, const1_rtx);
7252         }
7253     }
7254   if (add == NULL_RTX)
7255     return NULL_RTX;
7256
7257   /* Form simplified final result.  */
7258   if (mult == const0_rtx)
7259     return add;
7260   else if (mult == const1_rtx)
7261     mult = g1->dest_reg;
7262   else
7263     mult = gen_rtx_MULT (g2->mode, g1->dest_reg, mult);
7264
7265   if (add == const0_rtx)
7266     return mult;
7267   else
7268     {
7269       if (GET_CODE (add) == PLUS
7270           && CONSTANT_P (XEXP (add, 1)))
7271         {
7272           rtx tem = XEXP (add, 1);
7273           mult = gen_rtx_PLUS (g2->mode, mult, XEXP (add, 0));
7274           add = tem;
7275         }
7276
7277       return gen_rtx_PLUS (g2->mode, mult, add);
7278     }
7279 }
7280 \f
7281 /* Return an rtx, if any, that expresses giv G2 as a function of the register
7282    represented by G1.  This indicates that G2 should be combined with G1 and
7283    that G2 can use (either directly or via an address expression) a register
7284    used to represent G1.  */
7285
7286 static rtx
7287 combine_givs_p (g1, g2)
7288      struct induction *g1, *g2;
7289 {
7290   rtx comb, ret;
7291
7292   /* With the introduction of ext dependent givs, we must care for modes.
7293      G2 must not use a wider mode than G1.  */
7294   if (GET_MODE_SIZE (g1->mode) < GET_MODE_SIZE (g2->mode))
7295     return NULL_RTX;
7296
7297   ret = comb = express_from (g1, g2);
7298   if (comb == NULL_RTX)
7299     return NULL_RTX;
7300   if (g1->mode != g2->mode)
7301     ret = gen_lowpart (g2->mode, comb);
7302
7303   /* If these givs are identical, they can be combined.  We use the results
7304      of express_from because the addends are not in a canonical form, so
7305      rtx_equal_p is a weaker test.  */
7306   /* But don't combine a DEST_REG giv with a DEST_ADDR giv; we want the
7307      combination to be the other way round.  */
7308   if (comb == g1->dest_reg
7309       && (g1->giv_type == DEST_REG || g2->giv_type == DEST_ADDR))
7310     {
7311       return ret;
7312     }
7313
7314   /* If G2 can be expressed as a function of G1 and that function is valid
7315      as an address and no more expensive than using a register for G2,
7316      the expression of G2 in terms of G1 can be used.  */
7317   if (ret != NULL_RTX
7318       && g2->giv_type == DEST_ADDR
7319       && memory_address_p (GET_MODE (g2->mem), ret)
7320       /* ??? Looses, especially with -fforce-addr, where *g2->location
7321          will always be a register, and so anything more complicated
7322          gets discarded.  */
7323 #if 0
7324 #ifdef ADDRESS_COST
7325       && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location)
7326 #else
7327       && rtx_cost (tem, MEM) <= rtx_cost (*g2->location, MEM)
7328 #endif
7329 #endif
7330       )
7331     {
7332       return ret;
7333     }
7334
7335   return NULL_RTX;
7336 }
7337 \f
7338 /* Check each extension dependent giv in this class to see if its
7339    root biv is safe from wrapping in the interior mode, which would
7340    make the giv illegal.  */
7341
7342 static void
7343 check_ext_dependent_givs (bl, loop_info)
7344      struct iv_class *bl;
7345      struct loop_info *loop_info;
7346 {
7347   int ze_ok = 0, se_ok = 0, info_ok = 0;
7348   enum machine_mode biv_mode = GET_MODE (bl->biv->src_reg);
7349   HOST_WIDE_INT start_val;
7350   unsigned HOST_WIDE_INT u_end_val = 0;
7351   unsigned HOST_WIDE_INT u_start_val = 0;
7352   rtx incr = pc_rtx;
7353   struct induction *v;
7354
7355   /* Make sure the iteration data is available.  We must have
7356      constants in order to be certain of no overflow.  */
7357   /* ??? An unknown iteration count with an increment of +-1
7358      combined with friendly exit tests of against an invariant
7359      value is also ameanable to optimization.  Not implemented.  */
7360   if (loop_info->n_iterations > 0
7361       && bl->initial_value
7362       && GET_CODE (bl->initial_value) == CONST_INT
7363       && (incr = biv_total_increment (bl))
7364       && GET_CODE (incr) == CONST_INT
7365       /* Make sure the host can represent the arithmetic.  */
7366       && HOST_BITS_PER_WIDE_INT >= GET_MODE_BITSIZE (biv_mode))
7367     {
7368       unsigned HOST_WIDE_INT abs_incr, total_incr;
7369       HOST_WIDE_INT s_end_val;
7370       int neg_incr;
7371
7372       info_ok = 1;
7373       start_val = INTVAL (bl->initial_value);
7374       u_start_val = start_val;
7375
7376       neg_incr = 0, abs_incr = INTVAL (incr);
7377       if (INTVAL (incr) < 0)
7378         neg_incr = 1, abs_incr = -abs_incr;
7379       total_incr = abs_incr * loop_info->n_iterations;
7380
7381       /* Check for host arithmatic overflow.  */
7382       if (total_incr / loop_info->n_iterations == abs_incr)
7383         {
7384           unsigned HOST_WIDE_INT u_max;
7385           HOST_WIDE_INT s_max;
7386
7387           u_end_val = start_val + (neg_incr ? -total_incr : total_incr);
7388           s_end_val = u_end_val;
7389           u_max = GET_MODE_MASK (biv_mode);
7390           s_max = u_max >> 1;
7391
7392           /* Check zero extension of biv ok.  */
7393           if (start_val >= 0
7394               /* Check for host arithmatic overflow.  */
7395               && (neg_incr
7396                   ? u_end_val < u_start_val
7397                   : u_end_val > u_start_val)
7398               /* Check for target arithmetic overflow.  */
7399               && (neg_incr
7400                   ? 1 /* taken care of with host overflow */
7401                   : u_end_val <= u_max))
7402             {
7403               ze_ok = 1;
7404             }
7405
7406           /* Check sign extension of biv ok.  */
7407           /* ??? While it is true that overflow with signed and pointer
7408              arithmetic is undefined, I fear too many programmers don't
7409              keep this fact in mind -- myself included on occasion.
7410              So leave alone with the signed overflow optimizations.  */
7411           if (start_val >= -s_max - 1
7412               /* Check for host arithmatic overflow.  */
7413               && (neg_incr
7414                   ? s_end_val < start_val
7415                   : s_end_val > start_val)
7416               /* Check for target arithmetic overflow.  */
7417               && (neg_incr
7418                   ? s_end_val >= -s_max - 1
7419                   : s_end_val <= s_max))
7420             {
7421               se_ok = 1;
7422             }
7423         }
7424     }
7425
7426   /* Invalidate givs that fail the tests.  */
7427   for (v = bl->giv; v; v = v->next_iv)
7428     if (v->ext_dependent)
7429       {
7430         enum rtx_code code = GET_CODE (v->ext_dependent);
7431         int ok = 0;
7432
7433         switch (code)
7434           {
7435           case SIGN_EXTEND:
7436             ok = se_ok;
7437             break;
7438           case ZERO_EXTEND:
7439             ok = ze_ok;
7440             break;
7441
7442           case TRUNCATE:
7443             /* We don't know whether this value is being used as either
7444                signed or unsigned, so to safely truncate we must satisfy
7445                both.  The initial check here verifies the BIV itself;
7446                once that is successful we may check its range wrt the
7447                derived GIV.  */
7448             if (se_ok && ze_ok)
7449               {
7450                 enum machine_mode outer_mode = GET_MODE (v->ext_dependent);
7451                 unsigned HOST_WIDE_INT max = GET_MODE_MASK (outer_mode) >> 1;
7452
7453                 /* We know from the above that both endpoints are nonnegative,
7454                    and that there is no wrapping.  Verify that both endpoints
7455                    are within the (signed) range of the outer mode.  */
7456                 if (u_start_val <= max && u_end_val <= max)
7457                   ok = 1;
7458               }
7459             break;
7460
7461           default:
7462             abort ();
7463           }
7464
7465         if (ok)
7466           {
7467             if (loop_dump_stream)
7468               {
7469                 fprintf (loop_dump_stream,
7470                          "Verified ext dependent giv at %d of reg %d\n",
7471                          INSN_UID (v->insn), bl->regno);
7472               }
7473           }
7474         else
7475           {
7476             if (loop_dump_stream)
7477               {
7478                 const char *why;
7479
7480                 if (info_ok)
7481                   why = "biv iteration values overflowed";
7482                 else
7483                   {
7484                     if (incr == pc_rtx)
7485                       incr = biv_total_increment (bl);
7486                     if (incr == const1_rtx)
7487                       why = "biv iteration info incomplete; incr by 1";
7488                     else
7489                       why = "biv iteration info incomplete";
7490                   }
7491
7492                 fprintf (loop_dump_stream,
7493                          "Failed ext dependent giv at %d, %s\n",
7494                          INSN_UID (v->insn), why);
7495               }
7496             v->ignore = 1;
7497             bl->all_reduced = 0;
7498           }
7499       }
7500 }
7501
7502 /* Generate a version of VALUE in a mode appropriate for initializing V.  */
7503
7504 rtx
7505 extend_value_for_giv (v, value)
7506      struct induction *v;
7507      rtx value;
7508 {
7509   rtx ext_dep = v->ext_dependent;
7510
7511   if (! ext_dep)
7512     return value;
7513
7514   /* Recall that check_ext_dependent_givs verified that the known bounds
7515      of a biv did not overflow or wrap with respect to the extension for
7516      the giv.  Therefore, constants need no additional adjustment.  */
7517   if (CONSTANT_P (value) && GET_MODE (value) == VOIDmode)
7518     return value;
7519
7520   /* Otherwise, we must adjust the value to compensate for the
7521      differing modes of the biv and the giv.  */
7522   return gen_rtx_fmt_e (GET_CODE (ext_dep), GET_MODE (ext_dep), value);
7523 }
7524 \f
7525 struct combine_givs_stats
7526 {
7527   int giv_number;
7528   int total_benefit;
7529 };
7530
7531 static int
7532 cmp_combine_givs_stats (xp, yp)
7533      const PTR xp;
7534      const PTR yp;
7535 {
7536   const struct combine_givs_stats * const x =
7537     (const struct combine_givs_stats *) xp;
7538   const struct combine_givs_stats * const y =
7539     (const struct combine_givs_stats *) yp;
7540   int d;
7541   d = y->total_benefit - x->total_benefit;
7542   /* Stabilize the sort.  */
7543   if (!d)
7544     d = x->giv_number - y->giv_number;
7545   return d;
7546 }
7547
7548 /* Check all pairs of givs for iv_class BL and see if any can be combined with
7549    any other.  If so, point SAME to the giv combined with and set NEW_REG to
7550    be an expression (in terms of the other giv's DEST_REG) equivalent to the
7551    giv.  Also, update BENEFIT and related fields for cost/benefit analysis.  */
7552
7553 static void
7554 combine_givs (regs, bl)
7555      struct loop_regs *regs;
7556      struct iv_class *bl;
7557 {
7558   /* Additional benefit to add for being combined multiple times.  */
7559   const int extra_benefit = 3;
7560
7561   struct induction *g1, *g2, **giv_array;
7562   int i, j, k, giv_count;
7563   struct combine_givs_stats *stats;
7564   rtx *can_combine;
7565
7566   /* Count givs, because bl->giv_count is incorrect here.  */
7567   giv_count = 0;
7568   for (g1 = bl->giv; g1; g1 = g1->next_iv)
7569     if (!g1->ignore)
7570       giv_count++;
7571
7572   giv_array
7573     = (struct induction **) alloca (giv_count * sizeof (struct induction *));
7574   i = 0;
7575   for (g1 = bl->giv; g1; g1 = g1->next_iv)
7576     if (!g1->ignore)
7577       giv_array[i++] = g1;
7578
7579   stats = (struct combine_givs_stats *) xcalloc (giv_count, sizeof (*stats));
7580   can_combine = (rtx *) xcalloc (giv_count, giv_count * sizeof (rtx));
7581
7582   for (i = 0; i < giv_count; i++)
7583     {
7584       int this_benefit;
7585       rtx single_use;
7586
7587       g1 = giv_array[i];
7588       stats[i].giv_number = i;
7589
7590       /* If a DEST_REG GIV is used only once, do not allow it to combine
7591          with anything, for in doing so we will gain nothing that cannot
7592          be had by simply letting the GIV with which we would have combined
7593          to be reduced on its own.  The losage shows up in particular with
7594          DEST_ADDR targets on hosts with reg+reg addressing, though it can
7595          be seen elsewhere as well.  */
7596       if (g1->giv_type == DEST_REG
7597           && (single_use = regs->array[REGNO (g1->dest_reg)].single_usage)
7598           && single_use != const0_rtx)
7599         continue;
7600
7601       this_benefit = g1->benefit;
7602       /* Add an additional weight for zero addends.  */
7603       if (g1->no_const_addval)
7604         this_benefit += 1;
7605
7606       for (j = 0; j < giv_count; j++)
7607         {
7608           rtx this_combine;
7609
7610           g2 = giv_array[j];
7611           if (g1 != g2
7612               && (this_combine = combine_givs_p (g1, g2)) != NULL_RTX)
7613             {
7614               can_combine[i * giv_count + j] = this_combine;
7615               this_benefit += g2->benefit + extra_benefit;
7616             }
7617         }
7618       stats[i].total_benefit = this_benefit;
7619     }
7620
7621   /* Iterate, combining until we can't.  */
7622 restart:
7623   qsort (stats, giv_count, sizeof (*stats), cmp_combine_givs_stats);
7624
7625   if (loop_dump_stream)
7626     {
7627       fprintf (loop_dump_stream, "Sorted combine statistics:\n");
7628       for (k = 0; k < giv_count; k++)
7629         {
7630           g1 = giv_array[stats[k].giv_number];
7631           if (!g1->combined_with && !g1->same)
7632             fprintf (loop_dump_stream, " {%d, %d}",
7633                      INSN_UID (giv_array[stats[k].giv_number]->insn),
7634                      stats[k].total_benefit);
7635         }
7636       putc ('\n', loop_dump_stream);
7637     }
7638
7639   for (k = 0; k < giv_count; k++)
7640     {
7641       int g1_add_benefit = 0;
7642
7643       i = stats[k].giv_number;
7644       g1 = giv_array[i];
7645
7646       /* If it has already been combined, skip.  */
7647       if (g1->combined_with || g1->same)
7648         continue;
7649
7650       for (j = 0; j < giv_count; j++)
7651         {
7652           g2 = giv_array[j];
7653           if (g1 != g2 && can_combine[i * giv_count + j]
7654               /* If it has already been combined, skip.  */
7655               && ! g2->same && ! g2->combined_with)
7656             {
7657               int l;
7658
7659               g2->new_reg = can_combine[i * giv_count + j];
7660               g2->same = g1;
7661               /* For destination, we now may replace by mem expression instead
7662                  of register.  This changes the costs considerably, so add the
7663                  compensation.  */
7664               if (g2->giv_type == DEST_ADDR)
7665                 g2->benefit = (g2->benefit + reg_address_cost
7666                                - address_cost (g2->new_reg,
7667                                GET_MODE (g2->mem)));
7668               g1->combined_with++;
7669               g1->lifetime += g2->lifetime;
7670
7671               g1_add_benefit += g2->benefit;
7672
7673               /* ??? The new final_[bg]iv_value code does a much better job
7674                  of finding replaceable giv's, and hence this code may no
7675                  longer be necessary.  */
7676               if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
7677                 g1_add_benefit -= copy_cost;
7678
7679               /* To help optimize the next set of combinations, remove
7680                  this giv from the benefits of other potential mates.  */
7681               for (l = 0; l < giv_count; ++l)
7682                 {
7683                   int m = stats[l].giv_number;
7684                   if (can_combine[m * giv_count + j])
7685                     stats[l].total_benefit -= g2->benefit + extra_benefit;
7686                 }
7687
7688               if (loop_dump_stream)
7689                 fprintf (loop_dump_stream,
7690                          "giv at %d combined with giv at %d; new benefit %d + %d, lifetime %d\n",
7691                          INSN_UID (g2->insn), INSN_UID (g1->insn),
7692                          g1->benefit, g1_add_benefit, g1->lifetime);
7693             }
7694         }
7695
7696       /* To help optimize the next set of combinations, remove
7697          this giv from the benefits of other potential mates.  */
7698       if (g1->combined_with)
7699         {
7700           for (j = 0; j < giv_count; ++j)
7701             {
7702               int m = stats[j].giv_number;
7703               if (can_combine[m * giv_count + i])
7704                 stats[j].total_benefit -= g1->benefit + extra_benefit;
7705             }
7706
7707           g1->benefit += g1_add_benefit;
7708
7709           /* We've finished with this giv, and everything it touched.
7710              Restart the combination so that proper weights for the
7711              rest of the givs are properly taken into account.  */
7712           /* ??? Ideally we would compact the arrays at this point, so
7713              as to not cover old ground.  But sanely compacting
7714              can_combine is tricky.  */
7715           goto restart;
7716         }
7717     }
7718
7719   /* Clean up.  */
7720   free (stats);
7721   free (can_combine);
7722 }
7723 \f
7724 /* Generate sequence for REG = B * M + A.  */
7725
7726 static rtx
7727 gen_add_mult (b, m, a, reg)
7728      rtx b;          /* initial value of basic induction variable */
7729      rtx m;          /* multiplicative constant */
7730      rtx a;          /* additive constant */
7731      rtx reg;        /* destination register */
7732 {
7733   rtx seq;
7734   rtx result;
7735
7736   start_sequence ();
7737   /* Use unsigned arithmetic.  */
7738   result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7739   if (reg != result)
7740     emit_move_insn (reg, result);
7741   seq = gen_sequence ();
7742   end_sequence ();
7743
7744   return seq;
7745 }
7746
7747
7748 /* Update registers created in insn sequence SEQ.  */
7749
7750 static void
7751 loop_regs_update (loop, seq)
7752      const struct loop *loop ATTRIBUTE_UNUSED;
7753      rtx seq;
7754 {
7755   /* Update register info for alias analysis.  */
7756
7757   if (GET_CODE (seq) == SEQUENCE)
7758     {
7759       int i;
7760       for (i = 0; i < XVECLEN (seq, 0); ++i)
7761         {
7762           rtx set = single_set (XVECEXP (seq, 0, i));
7763           if (set && GET_CODE (SET_DEST (set)) == REG)
7764             record_base_value (REGNO (SET_DEST (set)), SET_SRC (set), 0);
7765         }
7766     }
7767   else
7768     {
7769       if (GET_CODE (seq) == SET
7770           && GET_CODE (SET_DEST (seq)) == REG)
7771         record_base_value (REGNO (SET_DEST (seq)), SET_SRC (seq), 0);
7772     }
7773 }
7774
7775
7776 /* EMIT code before BEFORE_BB/BEFORE_INSN to set REG = B * M + A.  */
7777
7778 void
7779 loop_iv_add_mult_emit_before (loop, b, m, a, reg, before_bb, before_insn)
7780      const struct loop *loop;
7781      rtx b;          /* initial value of basic induction variable */
7782      rtx m;          /* multiplicative constant */
7783      rtx a;          /* additive constant */
7784      rtx reg;        /* destination register */
7785      basic_block before_bb;
7786      rtx before_insn;
7787 {
7788   rtx seq;
7789
7790   if (! before_insn)
7791     {
7792       loop_iv_add_mult_hoist (loop, b, m, a, reg);
7793       return;
7794     }
7795
7796   /* Use copy_rtx to prevent unexpected sharing of these rtx.  */
7797   seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7798
7799   /* Increase the lifetime of any invariants moved further in code.  */
7800   update_reg_last_use (a, before_insn);
7801   update_reg_last_use (b, before_insn);
7802   update_reg_last_use (m, before_insn);
7803
7804   loop_insn_emit_before (loop, before_bb, before_insn, seq);
7805
7806   /* It is possible that the expansion created lots of new registers.
7807      Iterate over the sequence we just created and record them all.  */
7808   loop_regs_update (loop, seq);
7809 }
7810
7811
7812 /* Emit insns in loop pre-header to set REG = B * M + A.  */
7813
7814 void
7815 loop_iv_add_mult_sink (loop, b, m, a, reg)
7816      const struct loop *loop;
7817      rtx b;          /* initial value of basic induction variable */
7818      rtx m;          /* multiplicative constant */
7819      rtx a;          /* additive constant */
7820      rtx reg;        /* destination register */
7821 {
7822   rtx seq;
7823
7824   /* Use copy_rtx to prevent unexpected sharing of these rtx.  */
7825   seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7826
7827   /* Increase the lifetime of any invariants moved further in code.
7828      ???? Is this really necessary?  */
7829   update_reg_last_use (a, loop->sink);
7830   update_reg_last_use (b, loop->sink);
7831   update_reg_last_use (m, loop->sink);
7832
7833   loop_insn_sink (loop, seq);
7834
7835   /* It is possible that the expansion created lots of new registers.
7836      Iterate over the sequence we just created and record them all.  */
7837   loop_regs_update (loop, seq);
7838 }
7839
7840
7841 /* Emit insns after loop to set REG = B * M + A.  */
7842
7843 void
7844 loop_iv_add_mult_hoist (loop, b, m, a, reg)
7845      const struct loop *loop;
7846      rtx b;          /* initial value of basic induction variable */
7847      rtx m;          /* multiplicative constant */
7848      rtx a;          /* additive constant */
7849      rtx reg;        /* destination register */
7850 {
7851   rtx seq;
7852
7853   /* Use copy_rtx to prevent unexpected sharing of these rtx.  */
7854   seq = gen_add_mult (copy_rtx (b), copy_rtx (m), copy_rtx (a), reg);
7855
7856   loop_insn_hoist (loop, seq);
7857
7858   /* It is possible that the expansion created lots of new registers.
7859      Iterate over the sequence we just created and record them all.  */
7860   loop_regs_update (loop, seq);
7861 }
7862
7863
7864
7865 /* Similar to gen_add_mult, but compute cost rather than generating
7866    sequence.  */
7867
7868 static int
7869 iv_add_mult_cost (b, m, a, reg)
7870      rtx b;          /* initial value of basic induction variable */
7871      rtx m;          /* multiplicative constant */
7872      rtx a;          /* additive constant */
7873      rtx reg;        /* destination register */
7874 {
7875   int cost = 0;
7876   rtx last, result;
7877
7878   start_sequence ();
7879   result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 1);
7880   if (reg != result)
7881     emit_move_insn (reg, result);
7882   last = get_last_insn ();
7883   while (last)
7884     {
7885       rtx t = single_set (last);
7886       if (t)
7887         cost += rtx_cost (SET_SRC (t), SET);
7888       last = PREV_INSN (last);
7889     }
7890   end_sequence ();
7891   return cost;
7892 }
7893 \f
7894 /* Test whether A * B can be computed without
7895    an actual multiply insn.  Value is 1 if so.  */
7896
7897 static int
7898 product_cheap_p (a, b)
7899      rtx a;
7900      rtx b;
7901 {
7902   int i;
7903   rtx tmp;
7904   int win = 1;
7905
7906   /* If only one is constant, make it B.  */
7907   if (GET_CODE (a) == CONST_INT)
7908     tmp = a, a = b, b = tmp;
7909
7910   /* If first constant, both constant, so don't need multiply.  */
7911   if (GET_CODE (a) == CONST_INT)
7912     return 1;
7913
7914   /* If second not constant, neither is constant, so would need multiply.  */
7915   if (GET_CODE (b) != CONST_INT)
7916     return 0;
7917
7918   /* One operand is constant, so might not need multiply insn.  Generate the
7919      code for the multiply and see if a call or multiply, or long sequence
7920      of insns is generated.  */
7921
7922   start_sequence ();
7923   expand_mult (GET_MODE (a), a, b, NULL_RTX, 1);
7924   tmp = gen_sequence ();
7925   end_sequence ();
7926
7927   if (GET_CODE (tmp) == SEQUENCE)
7928     {
7929       if (XVEC (tmp, 0) == 0)
7930         win = 1;
7931       else if (XVECLEN (tmp, 0) > 3)
7932         win = 0;
7933       else
7934         for (i = 0; i < XVECLEN (tmp, 0); i++)
7935           {
7936             rtx insn = XVECEXP (tmp, 0, i);
7937
7938             if (GET_CODE (insn) != INSN
7939                 || (GET_CODE (PATTERN (insn)) == SET
7940                     && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
7941                 || (GET_CODE (PATTERN (insn)) == PARALLEL
7942                     && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
7943                     && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
7944               {
7945                 win = 0;
7946                 break;
7947               }
7948           }
7949     }
7950   else if (GET_CODE (tmp) == SET
7951            && GET_CODE (SET_SRC (tmp)) == MULT)
7952     win = 0;
7953   else if (GET_CODE (tmp) == PARALLEL
7954            && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
7955            && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
7956     win = 0;
7957
7958   return win;
7959 }
7960 \f
7961 /* Check to see if loop can be terminated by a "decrement and branch until
7962    zero" instruction.  If so, add a REG_NONNEG note to the branch insn if so.
7963    Also try reversing an increment loop to a decrement loop
7964    to see if the optimization can be performed.
7965    Value is nonzero if optimization was performed.  */
7966
7967 /* This is useful even if the architecture doesn't have such an insn,
7968    because it might change a loops which increments from 0 to n to a loop
7969    which decrements from n to 0.  A loop that decrements to zero is usually
7970    faster than one that increments from zero.  */
7971
7972 /* ??? This could be rewritten to use some of the loop unrolling procedures,
7973    such as approx_final_value, biv_total_increment, loop_iterations, and
7974    final_[bg]iv_value.  */
7975
7976 static int
7977 check_dbra_loop (loop, insn_count)
7978      struct loop *loop;
7979      int insn_count;
7980 {
7981   struct loop_info *loop_info = LOOP_INFO (loop);
7982   struct loop_regs *regs = LOOP_REGS (loop);
7983   struct loop_ivs *ivs = LOOP_IVS (loop);
7984   struct iv_class *bl;
7985   rtx reg;
7986   rtx jump_label;
7987   rtx final_value;
7988   rtx start_value;
7989   rtx new_add_val;
7990   rtx comparison;
7991   rtx before_comparison;
7992   rtx p;
7993   rtx jump;
7994   rtx first_compare;
7995   int compare_and_branch;
7996   rtx loop_start = loop->start;
7997   rtx loop_end = loop->end;
7998
7999   /* If last insn is a conditional branch, and the insn before tests a
8000      register value, try to optimize it.  Otherwise, we can't do anything.  */
8001
8002   jump = PREV_INSN (loop_end);
8003   comparison = get_condition_for_loop (loop, jump);
8004   if (comparison == 0)
8005     return 0;
8006   if (!onlyjump_p (jump))
8007     return 0;
8008
8009   /* Try to compute whether the compare/branch at the loop end is one or
8010      two instructions.  */
8011   get_condition (jump, &first_compare);
8012   if (first_compare == jump)
8013     compare_and_branch = 1;
8014   else if (first_compare == prev_nonnote_insn (jump))
8015     compare_and_branch = 2;
8016   else
8017     return 0;
8018
8019   {
8020     /* If more than one condition is present to control the loop, then
8021        do not proceed, as this function does not know how to rewrite
8022        loop tests with more than one condition.
8023
8024        Look backwards from the first insn in the last comparison
8025        sequence and see if we've got another comparison sequence.  */
8026
8027     rtx jump1;
8028     if ((jump1 = prev_nonnote_insn (first_compare)) != loop->cont)
8029       if (GET_CODE (jump1) == JUMP_INSN)
8030         return 0;
8031   }
8032
8033   /* Check all of the bivs to see if the compare uses one of them.
8034      Skip biv's set more than once because we can't guarantee that
8035      it will be zero on the last iteration.  Also skip if the biv is
8036      used between its update and the test insn.  */
8037
8038   for (bl = ivs->list; bl; bl = bl->next)
8039     {
8040       if (bl->biv_count == 1
8041           && ! bl->biv->maybe_multiple
8042           && bl->biv->dest_reg == XEXP (comparison, 0)
8043           && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
8044                                    first_compare))
8045         break;
8046     }
8047
8048   if (! bl)
8049     return 0;
8050
8051   /* Look for the case where the basic induction variable is always
8052      nonnegative, and equals zero on the last iteration.
8053      In this case, add a reg_note REG_NONNEG, which allows the
8054      m68k DBRA instruction to be used.  */
8055
8056   if (((GET_CODE (comparison) == GT
8057         && GET_CODE (XEXP (comparison, 1)) == CONST_INT
8058         && INTVAL (XEXP (comparison, 1)) == -1)
8059        || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
8060       && GET_CODE (bl->biv->add_val) == CONST_INT
8061       && INTVAL (bl->biv->add_val) < 0)
8062     {
8063       /* Initial value must be greater than 0,
8064          init_val % -dec_value == 0 to ensure that it equals zero on
8065          the last iteration */
8066
8067       if (GET_CODE (bl->initial_value) == CONST_INT
8068           && INTVAL (bl->initial_value) > 0
8069           && (INTVAL (bl->initial_value)
8070               % (-INTVAL (bl->biv->add_val))) == 0)
8071         {
8072           /* register always nonnegative, add REG_NOTE to branch */
8073           if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8074             REG_NOTES (jump)
8075               = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8076                                    REG_NOTES (jump));
8077           bl->nonneg = 1;
8078
8079           return 1;
8080         }
8081
8082       /* If the decrement is 1 and the value was tested as >= 0 before
8083          the loop, then we can safely optimize.  */
8084       for (p = loop_start; p; p = PREV_INSN (p))
8085         {
8086           if (GET_CODE (p) == CODE_LABEL)
8087             break;
8088           if (GET_CODE (p) != JUMP_INSN)
8089             continue;
8090
8091           before_comparison = get_condition_for_loop (loop, p);
8092           if (before_comparison
8093               && XEXP (before_comparison, 0) == bl->biv->dest_reg
8094               && GET_CODE (before_comparison) == LT
8095               && XEXP (before_comparison, 1) == const0_rtx
8096               && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
8097               && INTVAL (bl->biv->add_val) == -1)
8098             {
8099               if (! find_reg_note (jump, REG_NONNEG, NULL_RTX))
8100                 REG_NOTES (jump)
8101                   = gen_rtx_EXPR_LIST (REG_NONNEG, bl->biv->dest_reg,
8102                                        REG_NOTES (jump));
8103               bl->nonneg = 1;
8104
8105               return 1;
8106             }
8107         }
8108     }
8109   else if (GET_CODE (bl->biv->add_val) == CONST_INT
8110            && INTVAL (bl->biv->add_val) > 0)
8111     {
8112       /* Try to change inc to dec, so can apply above optimization.  */
8113       /* Can do this if:
8114          all registers modified are induction variables or invariant,
8115          all memory references have non-overlapping addresses
8116          (obviously true if only one write)
8117          allow 2 insns for the compare/jump at the end of the loop.  */
8118       /* Also, we must avoid any instructions which use both the reversed
8119          biv and another biv.  Such instructions will fail if the loop is
8120          reversed.  We meet this condition by requiring that either
8121          no_use_except_counting is true, or else that there is only
8122          one biv.  */
8123       int num_nonfixed_reads = 0;
8124       /* 1 if the iteration var is used only to count iterations.  */
8125       int no_use_except_counting = 0;
8126       /* 1 if the loop has no memory store, or it has a single memory store
8127          which is reversible.  */
8128       int reversible_mem_store = 1;
8129
8130       if (bl->giv_count == 0
8131           && !loop->exit_count
8132           && !loop_info->has_multiple_exit_targets)
8133         {
8134           rtx bivreg = regno_reg_rtx[bl->regno];
8135           struct iv_class *blt;
8136
8137           /* If there are no givs for this biv, and the only exit is the
8138              fall through at the end of the loop, then
8139              see if perhaps there are no uses except to count.  */
8140           no_use_except_counting = 1;
8141           for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8142             if (INSN_P (p))
8143               {
8144                 rtx set = single_set (p);
8145
8146                 if (set && GET_CODE (SET_DEST (set)) == REG
8147                     && REGNO (SET_DEST (set)) == bl->regno)
8148                   /* An insn that sets the biv is okay.  */
8149                   ;
8150                 else if ((p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
8151                           || p == prev_nonnote_insn (loop_end))
8152                          && reg_mentioned_p (bivreg, PATTERN (p)))
8153                   {
8154                     /* If either of these insns uses the biv and sets a pseudo
8155                        that has more than one usage, then the biv has uses
8156                        other than counting since it's used to derive a value
8157                        that is used more than one time.  */
8158                     note_stores (PATTERN (p), note_set_pseudo_multiple_uses,
8159                                  regs);
8160                     if (regs->multiple_uses)
8161                       {
8162                         no_use_except_counting = 0;
8163                         break;
8164                       }
8165                   }
8166                 else if (reg_mentioned_p (bivreg, PATTERN (p)))
8167                   {
8168                     no_use_except_counting = 0;
8169                     break;
8170                   }
8171               }
8172
8173           /* A biv has uses besides counting if it is used to set
8174              another biv.  */
8175           for (blt = ivs->list; blt; blt = blt->next)
8176             if (blt->init_set
8177                 && reg_mentioned_p (bivreg, SET_SRC (blt->init_set)))
8178               {
8179                 no_use_except_counting = 0;
8180                 break;
8181               }
8182         }
8183
8184       if (no_use_except_counting)
8185         /* No need to worry about MEMs.  */
8186         ;
8187       else if (loop_info->num_mem_sets <= 1)
8188         {
8189           for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8190             if (INSN_P (p))
8191               num_nonfixed_reads += count_nonfixed_reads (loop, PATTERN (p));
8192
8193           /* If the loop has a single store, and the destination address is
8194              invariant, then we can't reverse the loop, because this address
8195              might then have the wrong value at loop exit.
8196              This would work if the source was invariant also, however, in that
8197              case, the insn should have been moved out of the loop.  */
8198
8199           if (loop_info->num_mem_sets == 1)
8200             {
8201               struct induction *v;
8202
8203               /* If we could prove that each of the memory locations
8204                  written to was different, then we could reverse the
8205                  store -- but we don't presently have any way of
8206                  knowing that.  */
8207               reversible_mem_store = 0;
8208
8209               /* If the store depends on a register that is set after the
8210                  store, it depends on the initial value, and is thus not
8211                  reversible.  */
8212               for (v = bl->giv; reversible_mem_store && v; v = v->next_iv)
8213                 {
8214                   if (v->giv_type == DEST_REG
8215                       && reg_mentioned_p (v->dest_reg,
8216                                           PATTERN (loop_info->first_loop_store_insn))
8217                       && loop_insn_first_p (loop_info->first_loop_store_insn,
8218                                             v->insn))
8219                     reversible_mem_store = 0;
8220                 }
8221             }
8222         }
8223       else
8224         return 0;
8225
8226       /* This code only acts for innermost loops.  Also it simplifies
8227          the memory address check by only reversing loops with
8228          zero or one memory access.
8229          Two memory accesses could involve parts of the same array,
8230          and that can't be reversed.
8231          If the biv is used only for counting, than we don't need to worry
8232          about all these things.  */
8233
8234       if ((num_nonfixed_reads <= 1
8235            && ! loop_info->has_nonconst_call
8236            && ! loop_info->has_prefetch
8237            && ! loop_info->has_volatile
8238            && reversible_mem_store
8239            && (bl->giv_count + bl->biv_count + loop_info->num_mem_sets
8240                + num_unmoved_movables (loop) + compare_and_branch == insn_count)
8241            && (bl == ivs->list && bl->next == 0))
8242           || (no_use_except_counting && ! loop_info->has_prefetch))
8243         {
8244           rtx tem;
8245
8246           /* Loop can be reversed.  */
8247           if (loop_dump_stream)
8248             fprintf (loop_dump_stream, "Can reverse loop\n");
8249
8250           /* Now check other conditions:
8251
8252              The increment must be a constant, as must the initial value,
8253              and the comparison code must be LT.
8254
8255              This test can probably be improved since +/- 1 in the constant
8256              can be obtained by changing LT to LE and vice versa; this is
8257              confusing.  */
8258
8259           if (comparison
8260               /* for constants, LE gets turned into LT */
8261               && (GET_CODE (comparison) == LT
8262                   || (GET_CODE (comparison) == LE
8263                       && no_use_except_counting)))
8264             {
8265               HOST_WIDE_INT add_val, add_adjust, comparison_val = 0;
8266               rtx initial_value, comparison_value;
8267               int nonneg = 0;
8268               enum rtx_code cmp_code;
8269               int comparison_const_width;
8270               unsigned HOST_WIDE_INT comparison_sign_mask;
8271
8272               add_val = INTVAL (bl->biv->add_val);
8273               comparison_value = XEXP (comparison, 1);
8274               if (GET_MODE (comparison_value) == VOIDmode)
8275                 comparison_const_width
8276                   = GET_MODE_BITSIZE (GET_MODE (XEXP (comparison, 0)));
8277               else
8278                 comparison_const_width
8279                   = GET_MODE_BITSIZE (GET_MODE (comparison_value));
8280               if (comparison_const_width > HOST_BITS_PER_WIDE_INT)
8281                 comparison_const_width = HOST_BITS_PER_WIDE_INT;
8282               comparison_sign_mask
8283                 = (unsigned HOST_WIDE_INT) 1 << (comparison_const_width - 1);
8284
8285               /* If the comparison value is not a loop invariant, then we
8286                  can not reverse this loop.
8287
8288                  ??? If the insns which initialize the comparison value as
8289                  a whole compute an invariant result, then we could move
8290                  them out of the loop and proceed with loop reversal.  */
8291               if (! loop_invariant_p (loop, comparison_value))
8292                 return 0;
8293
8294               if (GET_CODE (comparison_value) == CONST_INT)
8295                 comparison_val = INTVAL (comparison_value);
8296               initial_value = bl->initial_value;
8297
8298               /* Normalize the initial value if it is an integer and
8299                  has no other use except as a counter.  This will allow
8300                  a few more loops to be reversed.  */
8301               if (no_use_except_counting
8302                   && GET_CODE (comparison_value) == CONST_INT
8303                   && GET_CODE (initial_value) == CONST_INT)
8304                 {
8305                   comparison_val = comparison_val - INTVAL (bl->initial_value);
8306                   /* The code below requires comparison_val to be a multiple
8307                      of add_val in order to do the loop reversal, so
8308                      round up comparison_val to a multiple of add_val.
8309                      Since comparison_value is constant, we know that the
8310                      current comparison code is LT.  */
8311                   comparison_val = comparison_val + add_val - 1;
8312                   comparison_val
8313                     -= (unsigned HOST_WIDE_INT) comparison_val % add_val;
8314                   /* We postpone overflow checks for COMPARISON_VAL here;
8315                      even if there is an overflow, we might still be able to
8316                      reverse the loop, if converting the loop exit test to
8317                      NE is possible.  */
8318                   initial_value = const0_rtx;
8319                 }
8320
8321               /* First check if we can do a vanilla loop reversal.  */
8322               if (initial_value == const0_rtx
8323                   /* If we have a decrement_and_branch_on_count,
8324                      prefer the NE test, since this will allow that
8325                      instruction to be generated.  Note that we must
8326                      use a vanilla loop reversal if the biv is used to
8327                      calculate a giv or has a non-counting use.  */
8328 #if ! defined (HAVE_decrement_and_branch_until_zero) \
8329 && defined (HAVE_decrement_and_branch_on_count)
8330                   && (! (add_val == 1 && loop->vtop
8331                          && (bl->biv_count == 0
8332                              || no_use_except_counting)))
8333 #endif
8334                   && GET_CODE (comparison_value) == CONST_INT
8335                      /* Now do postponed overflow checks on COMPARISON_VAL.  */
8336                   && ! (((comparison_val - add_val) ^ INTVAL (comparison_value))
8337                         & comparison_sign_mask))
8338                 {
8339                   /* Register will always be nonnegative, with value
8340                      0 on last iteration */
8341                   add_adjust = add_val;
8342                   nonneg = 1;
8343                   cmp_code = GE;
8344                 }
8345               else if (add_val == 1 && loop->vtop
8346                        && (bl->biv_count == 0
8347                            || no_use_except_counting))
8348                 {
8349                   add_adjust = 0;
8350                   cmp_code = NE;
8351                 }
8352               else
8353                 return 0;
8354
8355               if (GET_CODE (comparison) == LE)
8356                 add_adjust -= add_val;
8357
8358               /* If the initial value is not zero, or if the comparison
8359                  value is not an exact multiple of the increment, then we
8360                  can not reverse this loop.  */
8361               if (initial_value == const0_rtx
8362                   && GET_CODE (comparison_value) == CONST_INT)
8363                 {
8364                   if (((unsigned HOST_WIDE_INT) comparison_val % add_val) != 0)
8365                     return 0;
8366                 }
8367               else
8368                 {
8369                   if (! no_use_except_counting || add_val != 1)
8370                     return 0;
8371                 }
8372
8373               final_value = comparison_value;
8374
8375               /* Reset these in case we normalized the initial value
8376                  and comparison value above.  */
8377               if (GET_CODE (comparison_value) == CONST_INT
8378                   && GET_CODE (initial_value) == CONST_INT)
8379                 {
8380                   comparison_value = GEN_INT (comparison_val);
8381                   final_value
8382                     = GEN_INT (comparison_val + INTVAL (bl->initial_value));
8383                 }
8384               bl->initial_value = initial_value;
8385
8386               /* Save some info needed to produce the new insns.  */
8387               reg = bl->biv->dest_reg;
8388               jump_label = condjump_label (PREV_INSN (loop_end));
8389               new_add_val = GEN_INT (-INTVAL (bl->biv->add_val));
8390
8391               /* Set start_value; if this is not a CONST_INT, we need
8392                  to generate a SUB.
8393                  Initialize biv to start_value before loop start.
8394                  The old initializing insn will be deleted as a
8395                  dead store by flow.c.  */
8396               if (initial_value == const0_rtx
8397                   && GET_CODE (comparison_value) == CONST_INT)
8398                 {
8399                   start_value = GEN_INT (comparison_val - add_adjust);
8400                   loop_insn_hoist (loop, gen_move_insn (reg, start_value));
8401                 }
8402               else if (GET_CODE (initial_value) == CONST_INT)
8403                 {
8404                   enum machine_mode mode = GET_MODE (reg);
8405                   rtx offset = GEN_INT (-INTVAL (initial_value) - add_adjust);
8406                   rtx add_insn = gen_add3_insn (reg, comparison_value, offset);
8407
8408                   if (add_insn == 0)
8409                     return 0;
8410
8411                   start_value
8412                     = gen_rtx_PLUS (mode, comparison_value, offset);
8413                   loop_insn_hoist (loop, add_insn);
8414                   if (GET_CODE (comparison) == LE)
8415                     final_value = gen_rtx_PLUS (mode, comparison_value,
8416                                                 GEN_INT (add_val));
8417                 }
8418               else if (! add_adjust)
8419                 {
8420                   enum machine_mode mode = GET_MODE (reg);
8421                   rtx sub_insn = gen_sub3_insn (reg, comparison_value,
8422                                                 initial_value);
8423
8424                   if (sub_insn == 0)
8425                     return 0;
8426                   start_value
8427                     = gen_rtx_MINUS (mode, comparison_value, initial_value);
8428                   loop_insn_hoist (loop, sub_insn);
8429                 }
8430               else
8431                 /* We could handle the other cases too, but it'll be
8432                    better to have a testcase first.  */
8433                 return 0;
8434
8435               /* We may not have a single insn which can increment a reg, so
8436                  create a sequence to hold all the insns from expand_inc.  */
8437               start_sequence ();
8438               expand_inc (reg, new_add_val);
8439               tem = gen_sequence ();
8440               end_sequence ();
8441
8442               p = loop_insn_emit_before (loop, 0, bl->biv->insn, tem);
8443               delete_insn (bl->biv->insn);
8444
8445               /* Update biv info to reflect its new status.  */
8446               bl->biv->insn = p;
8447               bl->initial_value = start_value;
8448               bl->biv->add_val = new_add_val;
8449
8450               /* Update loop info.  */
8451               loop_info->initial_value = reg;
8452               loop_info->initial_equiv_value = reg;
8453               loop_info->final_value = const0_rtx;
8454               loop_info->final_equiv_value = const0_rtx;
8455               loop_info->comparison_value = const0_rtx;
8456               loop_info->comparison_code = cmp_code;
8457               loop_info->increment = new_add_val;
8458
8459               /* Inc LABEL_NUSES so that delete_insn will
8460                  not delete the label.  */
8461               LABEL_NUSES (XEXP (jump_label, 0))++;
8462
8463               /* Emit an insn after the end of the loop to set the biv's
8464                  proper exit value if it is used anywhere outside the loop.  */
8465               if ((REGNO_LAST_UID (bl->regno) != INSN_UID (first_compare))
8466                   || ! bl->init_insn
8467                   || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
8468                 loop_insn_sink (loop, gen_load_of_final_value (reg, final_value));
8469
8470               /* Delete compare/branch at end of loop.  */
8471               delete_related_insns (PREV_INSN (loop_end));
8472               if (compare_and_branch == 2)
8473                 delete_related_insns (first_compare);
8474
8475               /* Add new compare/branch insn at end of loop.  */
8476               start_sequence ();
8477               emit_cmp_and_jump_insns (reg, const0_rtx, cmp_code, NULL_RTX,
8478                                        GET_MODE (reg), 0,
8479                                        XEXP (jump_label, 0));
8480               tem = gen_sequence ();
8481               end_sequence ();
8482               emit_jump_insn_before (tem, loop_end);
8483
8484               for (tem = PREV_INSN (loop_end);
8485                    tem && GET_CODE (tem) != JUMP_INSN;
8486                    tem = PREV_INSN (tem))
8487                 ;
8488
8489               if (tem)
8490                 JUMP_LABEL (tem) = XEXP (jump_label, 0);
8491
8492               if (nonneg)
8493                 {
8494                   if (tem)
8495                     {
8496                       /* Increment of LABEL_NUSES done above.  */
8497                       /* Register is now always nonnegative,
8498                          so add REG_NONNEG note to the branch.  */
8499                       REG_NOTES (tem) = gen_rtx_EXPR_LIST (REG_NONNEG, reg,
8500                                                            REG_NOTES (tem));
8501                     }
8502                   bl->nonneg = 1;
8503                 }
8504
8505               /* No insn may reference both the reversed and another biv or it
8506                  will fail (see comment near the top of the loop reversal
8507                  code).
8508                  Earlier on, we have verified that the biv has no use except
8509                  counting, or it is the only biv in this function.
8510                  However, the code that computes no_use_except_counting does
8511                  not verify reg notes.  It's possible to have an insn that
8512                  references another biv, and has a REG_EQUAL note with an
8513                  expression based on the reversed biv.  To avoid this case,
8514                  remove all REG_EQUAL notes based on the reversed biv
8515                  here.  */
8516               for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
8517                 if (INSN_P (p))
8518                   {
8519                     rtx *pnote;
8520                     rtx set = single_set (p);
8521                     /* If this is a set of a GIV based on the reversed biv, any
8522                        REG_EQUAL notes should still be correct.  */
8523                     if (! set
8524                         || GET_CODE (SET_DEST (set)) != REG
8525                         || (size_t) REGNO (SET_DEST (set)) >= ivs->n_regs
8526                         || REG_IV_TYPE (ivs, REGNO (SET_DEST (set))) != GENERAL_INDUCT
8527                         || REG_IV_INFO (ivs, REGNO (SET_DEST (set)))->src_reg != bl->biv->src_reg)
8528                       for (pnote = &REG_NOTES (p); *pnote;)
8529                         {
8530                           if (REG_NOTE_KIND (*pnote) == REG_EQUAL
8531                               && reg_mentioned_p (regno_reg_rtx[bl->regno],
8532                                                   XEXP (*pnote, 0)))
8533                             *pnote = XEXP (*pnote, 1);
8534                           else
8535                             pnote = &XEXP (*pnote, 1);
8536                         }
8537                   }
8538
8539               /* Mark that this biv has been reversed.  Each giv which depends
8540                  on this biv, and which is also live past the end of the loop
8541                  will have to be fixed up.  */
8542
8543               bl->reversed = 1;
8544
8545               if (loop_dump_stream)
8546                 {
8547                   fprintf (loop_dump_stream, "Reversed loop");
8548                   if (bl->nonneg)
8549                     fprintf (loop_dump_stream, " and added reg_nonneg\n");
8550                   else
8551                     fprintf (loop_dump_stream, "\n");
8552                 }
8553
8554               return 1;
8555             }
8556         }
8557     }
8558
8559   return 0;
8560 }
8561 \f
8562 /* Verify whether the biv BL appears to be eliminable,
8563    based on the insns in the loop that refer to it.
8564
8565    If ELIMINATE_P is non-zero, actually do the elimination.
8566
8567    THRESHOLD and INSN_COUNT are from loop_optimize and are used to
8568    determine whether invariant insns should be placed inside or at the
8569    start of the loop.  */
8570
8571 static int
8572 maybe_eliminate_biv (loop, bl, eliminate_p, threshold, insn_count)
8573      const struct loop *loop;
8574      struct iv_class *bl;
8575      int eliminate_p;
8576      int threshold, insn_count;
8577 {
8578   struct loop_ivs *ivs = LOOP_IVS (loop);
8579   rtx reg = bl->biv->dest_reg;
8580   rtx p;
8581
8582   /* Scan all insns in the loop, stopping if we find one that uses the
8583      biv in a way that we cannot eliminate.  */
8584
8585   for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
8586     {
8587       enum rtx_code code = GET_CODE (p);
8588       basic_block where_bb = 0;
8589       rtx where_insn = threshold >= insn_count ? 0 : p;
8590
8591       /* If this is a libcall that sets a giv, skip ahead to its end.  */
8592       if (GET_RTX_CLASS (code) == 'i')
8593         {
8594           rtx note = find_reg_note (p, REG_LIBCALL, NULL_RTX);
8595
8596           if (note)
8597             {
8598               rtx last = XEXP (note, 0);
8599               rtx set = single_set (last);
8600
8601               if (set && GET_CODE (SET_DEST (set)) == REG)
8602                 {
8603                   unsigned int regno = REGNO (SET_DEST (set));
8604
8605                   if (regno < ivs->n_regs
8606                       && REG_IV_TYPE (ivs, regno) == GENERAL_INDUCT
8607                       && REG_IV_INFO (ivs, regno)->src_reg == bl->biv->src_reg)
8608                     p = last;
8609                 }
8610             }
8611         }
8612       if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
8613           && reg_mentioned_p (reg, PATTERN (p))
8614           && ! maybe_eliminate_biv_1 (loop, PATTERN (p), p, bl,
8615                                       eliminate_p, where_bb, where_insn))
8616         {
8617           if (loop_dump_stream)
8618             fprintf (loop_dump_stream,
8619                      "Cannot eliminate biv %d: biv used in insn %d.\n",
8620                      bl->regno, INSN_UID (p));
8621           break;
8622         }
8623     }
8624
8625   if (p == loop->end)
8626     {
8627       if (loop_dump_stream)
8628         fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
8629                  bl->regno, eliminate_p ? "was" : "can be");
8630       return 1;
8631     }
8632
8633   return 0;
8634 }
8635 \f
8636 /* INSN and REFERENCE are instructions in the same insn chain.
8637    Return non-zero if INSN is first.  */
8638
8639 int
8640 loop_insn_first_p (insn, reference)
8641      rtx insn, reference;
8642 {
8643   rtx p, q;
8644
8645   for (p = insn, q = reference;;)
8646     {
8647       /* Start with test for not first so that INSN == REFERENCE yields not
8648          first.  */
8649       if (q == insn || ! p)
8650         return 0;
8651       if (p == reference || ! q)
8652         return 1;
8653
8654       /* Either of P or Q might be a NOTE.  Notes have the same LUID as the
8655          previous insn, hence the <= comparison below does not work if
8656          P is a note.  */
8657       if (INSN_UID (p) < max_uid_for_loop
8658           && INSN_UID (q) < max_uid_for_loop
8659           && GET_CODE (p) != NOTE)
8660         return INSN_LUID (p) <= INSN_LUID (q);
8661
8662       if (INSN_UID (p) >= max_uid_for_loop
8663           || GET_CODE (p) == NOTE)
8664         p = NEXT_INSN (p);
8665       if (INSN_UID (q) >= max_uid_for_loop)
8666         q = NEXT_INSN (q);
8667     }
8668 }
8669
8670 /* We are trying to eliminate BIV in INSN using GIV.  Return non-zero if
8671    the offset that we have to take into account due to auto-increment /
8672    div derivation is zero.  */
8673 static int
8674 biv_elimination_giv_has_0_offset (biv, giv, insn)
8675      struct induction *biv, *giv;
8676      rtx insn;
8677 {
8678   /* If the giv V had the auto-inc address optimization applied
8679      to it, and INSN occurs between the giv insn and the biv
8680      insn, then we'd have to adjust the value used here.
8681      This is rare, so we don't bother to make this possible.  */
8682   if (giv->auto_inc_opt
8683       && ((loop_insn_first_p (giv->insn, insn)
8684            && loop_insn_first_p (insn, biv->insn))
8685           || (loop_insn_first_p (biv->insn, insn)
8686               && loop_insn_first_p (insn, giv->insn))))
8687     return 0;
8688
8689   return 1;
8690 }
8691
8692 /* If BL appears in X (part of the pattern of INSN), see if we can
8693    eliminate its use.  If so, return 1.  If not, return 0.
8694
8695    If BIV does not appear in X, return 1.
8696
8697    If ELIMINATE_P is non-zero, actually do the elimination.
8698    WHERE_INSN/WHERE_BB indicate where extra insns should be added.
8699    Depending on how many items have been moved out of the loop, it
8700    will either be before INSN (when WHERE_INSN is non-zero) or at the
8701    start of the loop (when WHERE_INSN is zero).  */
8702
8703 static int
8704 maybe_eliminate_biv_1 (loop, x, insn, bl, eliminate_p, where_bb, where_insn)
8705      const struct loop *loop;
8706      rtx x, insn;
8707      struct iv_class *bl;
8708      int eliminate_p;
8709      basic_block where_bb;
8710      rtx where_insn;
8711 {
8712   enum rtx_code code = GET_CODE (x);
8713   rtx reg = bl->biv->dest_reg;
8714   enum machine_mode mode = GET_MODE (reg);
8715   struct induction *v;
8716   rtx arg, tem;
8717 #ifdef HAVE_cc0
8718   rtx new;
8719 #endif
8720   int arg_operand;
8721   const char *fmt;
8722   int i, j;
8723
8724   switch (code)
8725     {
8726     case REG:
8727       /* If we haven't already been able to do something with this BIV,
8728          we can't eliminate it.  */
8729       if (x == reg)
8730         return 0;
8731       return 1;
8732
8733     case SET:
8734       /* If this sets the BIV, it is not a problem.  */
8735       if (SET_DEST (x) == reg)
8736         return 1;
8737
8738       /* If this is an insn that defines a giv, it is also ok because
8739          it will go away when the giv is reduced.  */
8740       for (v = bl->giv; v; v = v->next_iv)
8741         if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
8742           return 1;
8743
8744 #ifdef HAVE_cc0
8745       if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
8746         {
8747           /* Can replace with any giv that was reduced and
8748              that has (MULT_VAL != 0) and (ADD_VAL == 0).
8749              Require a constant for MULT_VAL, so we know it's nonzero.
8750              ??? We disable this optimization to avoid potential
8751              overflows.  */
8752
8753           for (v = bl->giv; v; v = v->next_iv)
8754             if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
8755                 && v->add_val == const0_rtx
8756                 && ! v->ignore && ! v->maybe_dead && v->always_computable
8757                 && v->mode == mode
8758                 && 0)
8759               {
8760                 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8761                   continue;
8762
8763                 if (! eliminate_p)
8764                   return 1;
8765
8766                 /* If the giv has the opposite direction of change,
8767                    then reverse the comparison.  */
8768                 if (INTVAL (v->mult_val) < 0)
8769                   new = gen_rtx_COMPARE (GET_MODE (v->new_reg),
8770                                          const0_rtx, v->new_reg);
8771                 else
8772                   new = v->new_reg;
8773
8774                 /* We can probably test that giv's reduced reg.  */
8775                 if (validate_change (insn, &SET_SRC (x), new, 0))
8776                   return 1;
8777               }
8778
8779           /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
8780              replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
8781              Require a constant for MULT_VAL, so we know it's nonzero.
8782              ??? Do this only if ADD_VAL is a pointer to avoid a potential
8783              overflow problem.  */
8784
8785           for (v = bl->giv; v; v = v->next_iv)
8786             if (GET_CODE (v->mult_val) == CONST_INT
8787                 && v->mult_val != const0_rtx
8788                 && ! v->ignore && ! v->maybe_dead && v->always_computable
8789                 && v->mode == mode
8790                 && (GET_CODE (v->add_val) == SYMBOL_REF
8791                     || GET_CODE (v->add_val) == LABEL_REF
8792                     || GET_CODE (v->add_val) == CONST
8793                     || (GET_CODE (v->add_val) == REG
8794                         && REG_POINTER (v->add_val))))
8795               {
8796                 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8797                   continue;
8798
8799                 if (! eliminate_p)
8800                   return 1;
8801
8802                 /* If the giv has the opposite direction of change,
8803                    then reverse the comparison.  */
8804                 if (INTVAL (v->mult_val) < 0)
8805                   new = gen_rtx_COMPARE (VOIDmode, copy_rtx (v->add_val),
8806                                          v->new_reg);
8807                 else
8808                   new = gen_rtx_COMPARE (VOIDmode, v->new_reg,
8809                                          copy_rtx (v->add_val));
8810
8811                 /* Replace biv with the giv's reduced register.  */
8812                 update_reg_last_use (v->add_val, insn);
8813                 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8814                   return 1;
8815
8816                 /* Insn doesn't support that constant or invariant.  Copy it
8817                    into a register (it will be a loop invariant.)  */
8818                 tem = gen_reg_rtx (GET_MODE (v->new_reg));
8819
8820                 loop_insn_emit_before (loop, 0, where_insn,
8821                                        gen_move_insn (tem,
8822                                                       copy_rtx (v->add_val)));
8823
8824                 /* Substitute the new register for its invariant value in
8825                    the compare expression.  */
8826                 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
8827                 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
8828                   return 1;
8829               }
8830         }
8831 #endif
8832       break;
8833
8834     case COMPARE:
8835     case EQ:  case NE:
8836     case GT:  case GE:  case GTU:  case GEU:
8837     case LT:  case LE:  case LTU:  case LEU:
8838       /* See if either argument is the biv.  */
8839       if (XEXP (x, 0) == reg)
8840         arg = XEXP (x, 1), arg_operand = 1;
8841       else if (XEXP (x, 1) == reg)
8842         arg = XEXP (x, 0), arg_operand = 0;
8843       else
8844         break;
8845
8846       if (CONSTANT_P (arg))
8847         {
8848           /* First try to replace with any giv that has constant positive
8849              mult_val and constant add_val.  We might be able to support
8850              negative mult_val, but it seems complex to do it in general.  */
8851
8852           for (v = bl->giv; v; v = v->next_iv)
8853             if (GET_CODE (v->mult_val) == CONST_INT
8854                 && INTVAL (v->mult_val) > 0
8855                 && (GET_CODE (v->add_val) == SYMBOL_REF
8856                     || GET_CODE (v->add_val) == LABEL_REF
8857                     || GET_CODE (v->add_val) == CONST
8858                     || (GET_CODE (v->add_val) == REG
8859                         && REG_POINTER (v->add_val)))
8860                 && ! v->ignore && ! v->maybe_dead && v->always_computable
8861                 && v->mode == mode)
8862               {
8863                 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8864                   continue;
8865
8866                 /* Don't eliminate if the linear combination that makes up
8867                    the giv overflows when it is applied to ARG.  */
8868                 if (GET_CODE (arg) == CONST_INT)
8869                   {
8870                     rtx add_val;
8871
8872                     if (GET_CODE (v->add_val) == CONST_INT)
8873                       add_val = v->add_val;
8874                     else
8875                       add_val = const0_rtx;
8876
8877                     if (const_mult_add_overflow_p (arg, v->mult_val,
8878                                                    add_val, mode, 1))
8879                       continue;
8880                   }
8881
8882                 if (! eliminate_p)
8883                   return 1;
8884
8885                 /* Replace biv with the giv's reduced reg.  */
8886                 validate_change (insn, &XEXP (x, 1 - arg_operand), v->new_reg, 1);
8887
8888                 /* If all constants are actually constant integers and
8889                    the derived constant can be directly placed in the COMPARE,
8890                    do so.  */
8891                 if (GET_CODE (arg) == CONST_INT
8892                     && GET_CODE (v->add_val) == CONST_INT)
8893                   {
8894                     tem = expand_mult_add (arg, NULL_RTX, v->mult_val,
8895                                            v->add_val, mode, 1);
8896                   }
8897                 else
8898                   {
8899                     /* Otherwise, load it into a register.  */
8900                     tem = gen_reg_rtx (mode);
8901                     loop_iv_add_mult_emit_before (loop, arg,
8902                                                   v->mult_val, v->add_val,
8903                                                   tem, where_bb, where_insn);
8904                   }
8905
8906                 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8907
8908                 if (apply_change_group ())
8909                   return 1;
8910               }
8911
8912           /* Look for giv with positive constant mult_val and nonconst add_val.
8913              Insert insns to calculate new compare value.
8914              ??? Turn this off due to possible overflow.  */
8915
8916           for (v = bl->giv; v; v = v->next_iv)
8917             if (GET_CODE (v->mult_val) == CONST_INT
8918                 && INTVAL (v->mult_val) > 0
8919                 && ! v->ignore && ! v->maybe_dead && v->always_computable
8920                 && v->mode == mode
8921                 && 0)
8922               {
8923                 rtx tem;
8924
8925                 if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8926                   continue;
8927
8928                 if (! eliminate_p)
8929                   return 1;
8930
8931                 tem = gen_reg_rtx (mode);
8932
8933                 /* Replace biv with giv's reduced register.  */
8934                 validate_change (insn, &XEXP (x, 1 - arg_operand),
8935                                  v->new_reg, 1);
8936
8937                 /* Compute value to compare against.  */
8938                 loop_iv_add_mult_emit_before (loop, arg,
8939                                               v->mult_val, v->add_val,
8940                                               tem, where_bb, where_insn);
8941                 /* Use it in this insn.  */
8942                 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8943                 if (apply_change_group ())
8944                   return 1;
8945               }
8946         }
8947       else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
8948         {
8949           if (loop_invariant_p (loop, arg) == 1)
8950             {
8951               /* Look for giv with constant positive mult_val and nonconst
8952                  add_val. Insert insns to compute new compare value.
8953                  ??? Turn this off due to possible overflow.  */
8954
8955               for (v = bl->giv; v; v = v->next_iv)
8956                 if (GET_CODE (v->mult_val) == CONST_INT && INTVAL (v->mult_val) > 0
8957                     && ! v->ignore && ! v->maybe_dead && v->always_computable
8958                     && v->mode == mode
8959                     && 0)
8960                   {
8961                     rtx tem;
8962
8963                     if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
8964                       continue;
8965
8966                     if (! eliminate_p)
8967                       return 1;
8968
8969                     tem = gen_reg_rtx (mode);
8970
8971                     /* Replace biv with giv's reduced register.  */
8972                     validate_change (insn, &XEXP (x, 1 - arg_operand),
8973                                      v->new_reg, 1);
8974
8975                     /* Compute value to compare against.  */
8976                     loop_iv_add_mult_emit_before (loop, arg,
8977                                                   v->mult_val, v->add_val,
8978                                                   tem, where_bb, where_insn);
8979                     validate_change (insn, &XEXP (x, arg_operand), tem, 1);
8980                     if (apply_change_group ())
8981                       return 1;
8982                   }
8983             }
8984
8985           /* This code has problems.  Basically, you can't know when
8986              seeing if we will eliminate BL, whether a particular giv
8987              of ARG will be reduced.  If it isn't going to be reduced,
8988              we can't eliminate BL.  We can try forcing it to be reduced,
8989              but that can generate poor code.
8990
8991              The problem is that the benefit of reducing TV, below should
8992              be increased if BL can actually be eliminated, but this means
8993              we might have to do a topological sort of the order in which
8994              we try to process biv.  It doesn't seem worthwhile to do
8995              this sort of thing now.  */
8996
8997 #if 0
8998           /* Otherwise the reg compared with had better be a biv.  */
8999           if (GET_CODE (arg) != REG
9000               || REG_IV_TYPE (ivs, REGNO (arg)) != BASIC_INDUCT)
9001             return 0;
9002
9003           /* Look for a pair of givs, one for each biv,
9004              with identical coefficients.  */
9005           for (v = bl->giv; v; v = v->next_iv)
9006             {
9007               struct induction *tv;
9008
9009               if (v->ignore || v->maybe_dead || v->mode != mode)
9010                 continue;
9011
9012               for (tv = REG_IV_CLASS (ivs, REGNO (arg))->giv; tv;
9013                    tv = tv->next_iv)
9014                 if (! tv->ignore && ! tv->maybe_dead
9015                     && rtx_equal_p (tv->mult_val, v->mult_val)
9016                     && rtx_equal_p (tv->add_val, v->add_val)
9017                     && tv->mode == mode)
9018                   {
9019                     if (! biv_elimination_giv_has_0_offset (bl->biv, v, insn))
9020                       continue;
9021
9022                     if (! eliminate_p)
9023                       return 1;
9024
9025                     /* Replace biv with its giv's reduced reg.  */
9026                     XEXP (x, 1 - arg_operand) = v->new_reg;
9027                     /* Replace other operand with the other giv's
9028                        reduced reg.  */
9029                     XEXP (x, arg_operand) = tv->new_reg;
9030                     return 1;
9031                   }
9032             }
9033 #endif
9034         }
9035
9036       /* If we get here, the biv can't be eliminated.  */
9037       return 0;
9038
9039     case MEM:
9040       /* If this address is a DEST_ADDR giv, it doesn't matter if the
9041          biv is used in it, since it will be replaced.  */
9042       for (v = bl->giv; v; v = v->next_iv)
9043         if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
9044           return 1;
9045       break;
9046
9047     default:
9048       break;
9049     }
9050
9051   /* See if any subexpression fails elimination.  */
9052   fmt = GET_RTX_FORMAT (code);
9053   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9054     {
9055       switch (fmt[i])
9056         {
9057         case 'e':
9058           if (! maybe_eliminate_biv_1 (loop, XEXP (x, i), insn, bl,
9059                                        eliminate_p, where_bb, where_insn))
9060             return 0;
9061           break;
9062
9063         case 'E':
9064           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9065             if (! maybe_eliminate_biv_1 (loop, XVECEXP (x, i, j), insn, bl,
9066                                          eliminate_p, where_bb, where_insn))
9067               return 0;
9068           break;
9069         }
9070     }
9071
9072   return 1;
9073 }
9074 \f
9075 /* Return nonzero if the last use of REG
9076    is in an insn following INSN in the same basic block.  */
9077
9078 static int
9079 last_use_this_basic_block (reg, insn)
9080      rtx reg;
9081      rtx insn;
9082 {
9083   rtx n;
9084   for (n = insn;
9085        n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
9086        n = NEXT_INSN (n))
9087     {
9088       if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
9089         return 1;
9090     }
9091   return 0;
9092 }
9093 \f
9094 /* Called via `note_stores' to record the initial value of a biv.  Here we
9095    just record the location of the set and process it later.  */
9096
9097 static void
9098 record_initial (dest, set, data)
9099      rtx dest;
9100      rtx set;
9101      void *data ATTRIBUTE_UNUSED;
9102 {
9103   struct loop_ivs *ivs = (struct loop_ivs *) data;
9104   struct iv_class *bl;
9105
9106   if (GET_CODE (dest) != REG
9107       || REGNO (dest) >= ivs->n_regs
9108       || REG_IV_TYPE (ivs, REGNO (dest)) != BASIC_INDUCT)
9109     return;
9110
9111   bl = REG_IV_CLASS (ivs, REGNO (dest));
9112
9113   /* If this is the first set found, record it.  */
9114   if (bl->init_insn == 0)
9115     {
9116       bl->init_insn = note_insn;
9117       bl->init_set = set;
9118     }
9119 }
9120 \f
9121 /* If any of the registers in X are "old" and currently have a last use earlier
9122    than INSN, update them to have a last use of INSN.  Their actual last use
9123    will be the previous insn but it will not have a valid uid_luid so we can't
9124    use it.  X must be a source expression only.  */
9125
9126 static void
9127 update_reg_last_use (x, insn)
9128      rtx x;
9129      rtx insn;
9130 {
9131   /* Check for the case where INSN does not have a valid luid.  In this case,
9132      there is no need to modify the regno_last_uid, as this can only happen
9133      when code is inserted after the loop_end to set a pseudo's final value,
9134      and hence this insn will never be the last use of x.
9135      ???? This comment is not correct.  See for example loop_givs_reduce.
9136      This may insert an insn before another new insn.  */
9137   if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
9138       && INSN_UID (insn) < max_uid_for_loop
9139       && REGNO_LAST_LUID (REGNO (x)) < INSN_LUID (insn))
9140     {
9141       REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
9142     }
9143   else
9144     {
9145       int i, j;
9146       const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
9147       for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9148         {
9149           if (fmt[i] == 'e')
9150             update_reg_last_use (XEXP (x, i), insn);
9151           else if (fmt[i] == 'E')
9152             for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9153               update_reg_last_use (XVECEXP (x, i, j), insn);
9154         }
9155     }
9156 }
9157 \f
9158 /* Given an insn INSN and condition COND, return the condition in a
9159    canonical form to simplify testing by callers.  Specifically:
9160
9161    (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
9162    (2) Both operands will be machine operands; (cc0) will have been replaced.
9163    (3) If an operand is a constant, it will be the second operand.
9164    (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
9165        for GE, GEU, and LEU.
9166
9167    If the condition cannot be understood, or is an inequality floating-point
9168    comparison which needs to be reversed, 0 will be returned.
9169
9170    If REVERSE is non-zero, then reverse the condition prior to canonizing it.
9171
9172    If EARLIEST is non-zero, it is a pointer to a place where the earliest
9173    insn used in locating the condition was found.  If a replacement test
9174    of the condition is desired, it should be placed in front of that
9175    insn and we will be sure that the inputs are still valid.
9176
9177    If WANT_REG is non-zero, we wish the condition to be relative to that
9178    register, if possible.  Therefore, do not canonicalize the condition
9179    further.  */
9180
9181 rtx
9182 canonicalize_condition (insn, cond, reverse, earliest, want_reg)
9183      rtx insn;
9184      rtx cond;
9185      int reverse;
9186      rtx *earliest;
9187      rtx want_reg;
9188 {
9189   enum rtx_code code;
9190   rtx prev = insn;
9191   rtx set;
9192   rtx tem;
9193   rtx op0, op1;
9194   int reverse_code = 0;
9195   enum machine_mode mode;
9196
9197   code = GET_CODE (cond);
9198   mode = GET_MODE (cond);
9199   op0 = XEXP (cond, 0);
9200   op1 = XEXP (cond, 1);
9201
9202   if (reverse)
9203     code = reversed_comparison_code (cond, insn);
9204   if (code == UNKNOWN)
9205     return 0;
9206
9207   if (earliest)
9208     *earliest = insn;
9209
9210   /* If we are comparing a register with zero, see if the register is set
9211      in the previous insn to a COMPARE or a comparison operation.  Perform
9212      the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
9213      in cse.c  */
9214
9215   while (GET_RTX_CLASS (code) == '<'
9216          && op1 == CONST0_RTX (GET_MODE (op0))
9217          && op0 != want_reg)
9218     {
9219       /* Set non-zero when we find something of interest.  */
9220       rtx x = 0;
9221
9222 #ifdef HAVE_cc0
9223       /* If comparison with cc0, import actual comparison from compare
9224          insn.  */
9225       if (op0 == cc0_rtx)
9226         {
9227           if ((prev = prev_nonnote_insn (prev)) == 0
9228               || GET_CODE (prev) != INSN
9229               || (set = single_set (prev)) == 0
9230               || SET_DEST (set) != cc0_rtx)
9231             return 0;
9232
9233           op0 = SET_SRC (set);
9234           op1 = CONST0_RTX (GET_MODE (op0));
9235           if (earliest)
9236             *earliest = prev;
9237         }
9238 #endif
9239
9240       /* If this is a COMPARE, pick up the two things being compared.  */
9241       if (GET_CODE (op0) == COMPARE)
9242         {
9243           op1 = XEXP (op0, 1);
9244           op0 = XEXP (op0, 0);
9245           continue;
9246         }
9247       else if (GET_CODE (op0) != REG)
9248         break;
9249
9250       /* Go back to the previous insn.  Stop if it is not an INSN.  We also
9251          stop if it isn't a single set or if it has a REG_INC note because
9252          we don't want to bother dealing with it.  */
9253
9254       if ((prev = prev_nonnote_insn (prev)) == 0
9255           || GET_CODE (prev) != INSN
9256           || FIND_REG_INC_NOTE (prev, NULL_RTX))
9257         break;
9258
9259       set = set_of (op0, prev);
9260
9261       if (set
9262           && (GET_CODE (set) != SET
9263               || !rtx_equal_p (SET_DEST (set), op0)))
9264         break;
9265
9266       /* If this is setting OP0, get what it sets it to if it looks
9267          relevant.  */
9268       if (set)
9269         {
9270           enum machine_mode inner_mode = GET_MODE (SET_DEST (set));
9271
9272           /* ??? We may not combine comparisons done in a CCmode with
9273              comparisons not done in a CCmode.  This is to aid targets
9274              like Alpha that have an IEEE compliant EQ instruction, and
9275              a non-IEEE compliant BEQ instruction.  The use of CCmode is
9276              actually artificial, simply to prevent the combination, but
9277              should not affect other platforms.
9278
9279              However, we must allow VOIDmode comparisons to match either
9280              CCmode or non-CCmode comparison, because some ports have
9281              modeless comparisons inside branch patterns.
9282
9283              ??? This mode check should perhaps look more like the mode check
9284              in simplify_comparison in combine.  */
9285
9286           if ((GET_CODE (SET_SRC (set)) == COMPARE
9287                || (((code == NE
9288                      || (code == LT
9289                          && GET_MODE_CLASS (inner_mode) == MODE_INT
9290                          && (GET_MODE_BITSIZE (inner_mode)
9291                              <= HOST_BITS_PER_WIDE_INT)
9292                          && (STORE_FLAG_VALUE
9293                              & ((HOST_WIDE_INT) 1
9294                                 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9295 #ifdef FLOAT_STORE_FLAG_VALUE
9296                      || (code == LT
9297                          && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9298                          && (REAL_VALUE_NEGATIVE
9299                              (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9300 #endif
9301                      ))
9302                    && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'))
9303               && (((GET_MODE_CLASS (mode) == MODE_CC)
9304                    == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9305                   || mode == VOIDmode || inner_mode == VOIDmode))
9306             x = SET_SRC (set);
9307           else if (((code == EQ
9308                      || (code == GE
9309                          && (GET_MODE_BITSIZE (inner_mode)
9310                              <= HOST_BITS_PER_WIDE_INT)
9311                          && GET_MODE_CLASS (inner_mode) == MODE_INT
9312                          && (STORE_FLAG_VALUE
9313                              & ((HOST_WIDE_INT) 1
9314                                 << (GET_MODE_BITSIZE (inner_mode) - 1))))
9315 #ifdef FLOAT_STORE_FLAG_VALUE
9316                      || (code == GE
9317                          && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
9318                          && (REAL_VALUE_NEGATIVE
9319                              (FLOAT_STORE_FLAG_VALUE (inner_mode))))
9320 #endif
9321                      ))
9322                    && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<'
9323                    && (((GET_MODE_CLASS (mode) == MODE_CC)
9324                         == (GET_MODE_CLASS (inner_mode) == MODE_CC))
9325                        || mode == VOIDmode || inner_mode == VOIDmode))
9326
9327             {
9328               reverse_code = 1;
9329               x = SET_SRC (set);
9330             }
9331           else
9332             break;
9333         }
9334
9335       else if (reg_set_p (op0, prev))
9336         /* If this sets OP0, but not directly, we have to give up.  */
9337         break;
9338
9339       if (x)
9340         {
9341           if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9342             code = GET_CODE (x);
9343           if (reverse_code)
9344             {
9345               code = reversed_comparison_code (x, prev);
9346               if (code == UNKNOWN)
9347                 return 0;
9348               reverse_code = 0;
9349             }
9350
9351           op0 = XEXP (x, 0), op1 = XEXP (x, 1);
9352           if (earliest)
9353             *earliest = prev;
9354         }
9355     }
9356
9357   /* If constant is first, put it last.  */
9358   if (CONSTANT_P (op0))
9359     code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
9360
9361   /* If OP0 is the result of a comparison, we weren't able to find what
9362      was really being compared, so fail.  */
9363   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
9364     return 0;
9365
9366   /* Canonicalize any ordered comparison with integers involving equality
9367      if we can do computations in the relevant mode and we do not
9368      overflow.  */
9369
9370   if (GET_CODE (op1) == CONST_INT
9371       && GET_MODE (op0) != VOIDmode
9372       && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
9373     {
9374       HOST_WIDE_INT const_val = INTVAL (op1);
9375       unsigned HOST_WIDE_INT uconst_val = const_val;
9376       unsigned HOST_WIDE_INT max_val
9377         = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
9378
9379       switch (code)
9380         {
9381         case LE:
9382           if ((unsigned HOST_WIDE_INT) const_val != max_val >> 1)
9383             code = LT, op1 = gen_int_mode (const_val + 1, GET_MODE (op0));
9384           break;
9385
9386         /* When cross-compiling, const_val might be sign-extended from
9387            BITS_PER_WORD to HOST_BITS_PER_WIDE_INT */
9388         case GE:
9389           if ((HOST_WIDE_INT) (const_val & max_val)
9390               != (((HOST_WIDE_INT) 1
9391                    << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9392             code = GT, op1 = gen_int_mode (const_val - 1, GET_MODE (op0));
9393           break;
9394
9395         case LEU:
9396           if (uconst_val < max_val)
9397             code = LTU, op1 = gen_int_mode (uconst_val + 1, GET_MODE (op0));
9398           break;
9399
9400         case GEU:
9401           if (uconst_val != 0)
9402             code = GTU, op1 = gen_int_mode (uconst_val - 1, GET_MODE (op0));
9403           break;
9404
9405         default:
9406           break;
9407         }
9408     }
9409
9410 #ifdef HAVE_cc0
9411   /* Never return CC0; return zero instead.  */
9412   if (op0 == cc0_rtx)
9413     return 0;
9414 #endif
9415
9416   return gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
9417 }
9418
9419 /* Given a jump insn JUMP, return the condition that will cause it to branch
9420    to its JUMP_LABEL.  If the condition cannot be understood, or is an
9421    inequality floating-point comparison which needs to be reversed, 0 will
9422    be returned.
9423
9424    If EARLIEST is non-zero, it is a pointer to a place where the earliest
9425    insn used in locating the condition was found.  If a replacement test
9426    of the condition is desired, it should be placed in front of that
9427    insn and we will be sure that the inputs are still valid.  */
9428
9429 rtx
9430 get_condition (jump, earliest)
9431      rtx jump;
9432      rtx *earliest;
9433 {
9434   rtx cond;
9435   int reverse;
9436   rtx set;
9437
9438   /* If this is not a standard conditional jump, we can't parse it.  */
9439   if (GET_CODE (jump) != JUMP_INSN
9440       || ! any_condjump_p (jump))
9441     return 0;
9442   set = pc_set (jump);
9443
9444   cond = XEXP (SET_SRC (set), 0);
9445
9446   /* If this branches to JUMP_LABEL when the condition is false, reverse
9447      the condition.  */
9448   reverse
9449     = GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
9450       && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump);
9451
9452   return canonicalize_condition (jump, cond, reverse, earliest, NULL_RTX);
9453 }
9454
9455 /* Similar to above routine, except that we also put an invariant last
9456    unless both operands are invariants.  */
9457
9458 rtx
9459 get_condition_for_loop (loop, x)
9460      const struct loop *loop;
9461      rtx x;
9462 {
9463   rtx comparison = get_condition (x, (rtx*) 0);
9464
9465   if (comparison == 0
9466       || ! loop_invariant_p (loop, XEXP (comparison, 0))
9467       || loop_invariant_p (loop, XEXP (comparison, 1)))
9468     return comparison;
9469
9470   return gen_rtx_fmt_ee (swap_condition (GET_CODE (comparison)), VOIDmode,
9471                          XEXP (comparison, 1), XEXP (comparison, 0));
9472 }
9473
9474 /* Scan the function and determine whether it has indirect (computed) jumps.
9475
9476    This is taken mostly from flow.c; similar code exists elsewhere
9477    in the compiler.  It may be useful to put this into rtlanal.c.  */
9478 static int
9479 indirect_jump_in_function_p (start)
9480      rtx start;
9481 {
9482   rtx insn;
9483
9484   for (insn = start; insn; insn = NEXT_INSN (insn))
9485     if (computed_jump_p (insn))
9486       return 1;
9487
9488   return 0;
9489 }
9490
9491 /* Add MEM to the LOOP_MEMS array, if appropriate.  See the
9492    documentation for LOOP_MEMS for the definition of `appropriate'.
9493    This function is called from prescan_loop via for_each_rtx.  */
9494
9495 static int
9496 insert_loop_mem (mem, data)
9497      rtx *mem;
9498      void *data ATTRIBUTE_UNUSED;
9499 {
9500   struct loop_info *loop_info = data;
9501   int i;
9502   rtx m = *mem;
9503
9504   if (m == NULL_RTX)
9505     return 0;
9506
9507   switch (GET_CODE (m))
9508     {
9509     case MEM:
9510       break;
9511
9512     case CLOBBER:
9513       /* We're not interested in MEMs that are only clobbered.  */
9514       return -1;
9515
9516     case CONST_DOUBLE:
9517       /* We're not interested in the MEM associated with a
9518          CONST_DOUBLE, so there's no need to traverse into this.  */
9519       return -1;
9520
9521     case EXPR_LIST:
9522       /* We're not interested in any MEMs that only appear in notes.  */
9523       return -1;
9524
9525     default:
9526       /* This is not a MEM.  */
9527       return 0;
9528     }
9529
9530   /* See if we've already seen this MEM.  */
9531   for (i = 0; i < loop_info->mems_idx; ++i)
9532     if (rtx_equal_p (m, loop_info->mems[i].mem))
9533       {
9534         if (GET_MODE (m) != GET_MODE (loop_info->mems[i].mem))
9535           /* The modes of the two memory accesses are different.  If
9536              this happens, something tricky is going on, and we just
9537              don't optimize accesses to this MEM.  */
9538           loop_info->mems[i].optimize = 0;
9539
9540         return 0;
9541       }
9542
9543   /* Resize the array, if necessary.  */
9544   if (loop_info->mems_idx == loop_info->mems_allocated)
9545     {
9546       if (loop_info->mems_allocated != 0)
9547         loop_info->mems_allocated *= 2;
9548       else
9549         loop_info->mems_allocated = 32;
9550
9551       loop_info->mems = (loop_mem_info *)
9552         xrealloc (loop_info->mems,
9553                   loop_info->mems_allocated * sizeof (loop_mem_info));
9554     }
9555
9556   /* Actually insert the MEM.  */
9557   loop_info->mems[loop_info->mems_idx].mem = m;
9558   /* We can't hoist this MEM out of the loop if it's a BLKmode MEM
9559      because we can't put it in a register.  We still store it in the
9560      table, though, so that if we see the same address later, but in a
9561      non-BLK mode, we'll not think we can optimize it at that point.  */
9562   loop_info->mems[loop_info->mems_idx].optimize = (GET_MODE (m) != BLKmode);
9563   loop_info->mems[loop_info->mems_idx].reg = NULL_RTX;
9564   ++loop_info->mems_idx;
9565
9566   return 0;
9567 }
9568
9569
9570 /* Allocate REGS->ARRAY or reallocate it if it is too small.
9571
9572    Increment REGS->ARRAY[I].SET_IN_LOOP at the index I of each
9573    register that is modified by an insn between FROM and TO.  If the
9574    value of an element of REGS->array[I].SET_IN_LOOP becomes 127 or
9575    more, stop incrementing it, to avoid overflow.
9576
9577    Store in REGS->ARRAY[I].SINGLE_USAGE the single insn in which
9578    register I is used, if it is only used once.  Otherwise, it is set
9579    to 0 (for no uses) or const0_rtx for more than one use.  This
9580    parameter may be zero, in which case this processing is not done.
9581
9582    Set REGS->ARRAY[I].MAY_NOT_OPTIMIZE nonzero if we should not
9583    optimize register I.  */
9584
9585 static void
9586 loop_regs_scan (loop, extra_size)
9587      const struct loop *loop;
9588      int extra_size;
9589 {
9590   struct loop_regs *regs = LOOP_REGS (loop);
9591   int old_nregs;
9592   /* last_set[n] is nonzero iff reg n has been set in the current
9593    basic block.  In that case, it is the insn that last set reg n.  */
9594   rtx *last_set;
9595   rtx insn;
9596   int i;
9597
9598   old_nregs = regs->num;
9599   regs->num = max_reg_num ();
9600
9601   /* Grow the regs array if not allocated or too small.  */
9602   if (regs->num >= regs->size)
9603     {
9604       regs->size = regs->num + extra_size;
9605
9606       regs->array = (struct loop_reg *)
9607         xrealloc (regs->array, regs->size * sizeof (*regs->array));
9608
9609       /* Zero the new elements.  */
9610       memset (regs->array + old_nregs, 0,
9611               (regs->size - old_nregs) * sizeof (*regs->array));
9612     }
9613
9614   /* Clear previously scanned fields but do not clear n_times_set.  */
9615   for (i = 0; i < old_nregs; i++)
9616     {
9617       regs->array[i].set_in_loop = 0;
9618       regs->array[i].may_not_optimize = 0;
9619       regs->array[i].single_usage = NULL_RTX;
9620     }
9621
9622   last_set = (rtx *) xcalloc (regs->num, sizeof (rtx));
9623
9624   /* Scan the loop, recording register usage.  */
9625   for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9626        insn = NEXT_INSN (insn))
9627     {
9628       if (INSN_P (insn))
9629         {
9630           /* Record registers that have exactly one use.  */
9631           find_single_use_in_loop (regs, insn, PATTERN (insn));
9632
9633           /* Include uses in REG_EQUAL notes.  */
9634           if (REG_NOTES (insn))
9635             find_single_use_in_loop (regs, insn, REG_NOTES (insn));
9636
9637           if (GET_CODE (PATTERN (insn)) == SET
9638               || GET_CODE (PATTERN (insn)) == CLOBBER)
9639             count_one_set (regs, insn, PATTERN (insn), last_set);
9640           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
9641             {
9642               int i;
9643               for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
9644                 count_one_set (regs, insn, XVECEXP (PATTERN (insn), 0, i),
9645                                last_set);
9646             }
9647         }
9648
9649       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
9650         memset (last_set, 0, regs->num * sizeof (rtx));
9651     }
9652
9653   /* Invalidate all hard registers clobbered by calls.  With one exception:
9654      a call-clobbered PIC register is still function-invariant for our
9655      purposes, since we can hoist any PIC calculations out of the loop.
9656      Thus the call to rtx_varies_p.  */
9657   if (LOOP_INFO (loop)->has_call)
9658     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
9659       if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i)
9660           && rtx_varies_p (gen_rtx_REG (Pmode, i), /*for_alias=*/1))
9661         {
9662           regs->array[i].may_not_optimize = 1;
9663           regs->array[i].set_in_loop = 1;
9664         }
9665
9666 #ifdef AVOID_CCMODE_COPIES
9667   /* Don't try to move insns which set CC registers if we should not
9668      create CCmode register copies.  */
9669   for (i = regs->num - 1; i >= FIRST_PSEUDO_REGISTER; i--)
9670     if (GET_MODE_CLASS (GET_MODE (regno_reg_rtx[i])) == MODE_CC)
9671       regs->array[i].may_not_optimize = 1;
9672 #endif
9673
9674   /* Set regs->array[I].n_times_set for the new registers.  */
9675   for (i = old_nregs; i < regs->num; i++)
9676     regs->array[i].n_times_set = regs->array[i].set_in_loop;
9677
9678   free (last_set);
9679 }
9680
9681 /* Returns the number of real INSNs in the LOOP.  */
9682
9683 static int
9684 count_insns_in_loop (loop)
9685      const struct loop *loop;
9686 {
9687   int count = 0;
9688   rtx insn;
9689
9690   for (insn = loop->top ? loop->top : loop->start; insn != loop->end;
9691        insn = NEXT_INSN (insn))
9692     if (INSN_P (insn))
9693       ++count;
9694
9695   return count;
9696 }
9697
9698 /* Move MEMs into registers for the duration of the loop.  */
9699
9700 static void
9701 load_mems (loop)
9702      const struct loop *loop;
9703 {
9704   struct loop_info *loop_info = LOOP_INFO (loop);
9705   struct loop_regs *regs = LOOP_REGS (loop);
9706   int maybe_never = 0;
9707   int i;
9708   rtx p, prev_ebb_head;
9709   rtx label = NULL_RTX;
9710   rtx end_label;
9711   /* Nonzero if the next instruction may never be executed.  */
9712   int next_maybe_never = 0;
9713   unsigned int last_max_reg = max_reg_num ();
9714
9715   if (loop_info->mems_idx == 0)
9716     return;
9717
9718   /* We cannot use next_label here because it skips over normal insns.  */
9719   end_label = next_nonnote_insn (loop->end);
9720   if (end_label && GET_CODE (end_label) != CODE_LABEL)
9721     end_label = NULL_RTX;
9722
9723   /* Check to see if it's possible that some instructions in the loop are
9724      never executed.  Also check if there is a goto out of the loop other
9725      than right after the end of the loop.  */
9726   for (p = next_insn_in_loop (loop, loop->scan_start);
9727        p != NULL_RTX;
9728        p = next_insn_in_loop (loop, p))
9729     {
9730       if (GET_CODE (p) == CODE_LABEL)
9731         maybe_never = 1;
9732       else if (GET_CODE (p) == JUMP_INSN
9733                /* If we enter the loop in the middle, and scan
9734                   around to the beginning, don't set maybe_never
9735                   for that.  This must be an unconditional jump,
9736                   otherwise the code at the top of the loop might
9737                   never be executed.  Unconditional jumps are
9738                   followed a by barrier then loop end.  */
9739                && ! (GET_CODE (p) == JUMP_INSN
9740                      && JUMP_LABEL (p) == loop->top
9741                      && NEXT_INSN (NEXT_INSN (p)) == loop->end
9742                      && any_uncondjump_p (p)))
9743         {
9744           /* If this is a jump outside of the loop but not right
9745              after the end of the loop, we would have to emit new fixup
9746              sequences for each such label.  */
9747           if (/* If we can't tell where control might go when this
9748                  JUMP_INSN is executed, we must be conservative.  */
9749               !JUMP_LABEL (p)
9750               || (JUMP_LABEL (p) != end_label
9751                   && (INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop
9752                       || INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop->start)
9753                       || INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop->end))))
9754             return;
9755
9756           if (!any_condjump_p (p))
9757             /* Something complicated.  */
9758             maybe_never = 1;
9759           else
9760             /* If there are any more instructions in the loop, they
9761                might not be reached.  */
9762             next_maybe_never = 1;
9763         }
9764       else if (next_maybe_never)
9765         maybe_never = 1;
9766     }
9767
9768   /* Find start of the extended basic block that enters the loop.  */
9769   for (p = loop->start;
9770        PREV_INSN (p) && GET_CODE (p) != CODE_LABEL;
9771        p = PREV_INSN (p))
9772     ;
9773   prev_ebb_head = p;
9774
9775   cselib_init ();
9776
9777   /* Build table of mems that get set to constant values before the
9778      loop.  */
9779   for (; p != loop->start; p = NEXT_INSN (p))
9780     cselib_process_insn (p);
9781
9782   /* Actually move the MEMs.  */
9783   for (i = 0; i < loop_info->mems_idx; ++i)
9784     {
9785       regset_head load_copies;
9786       regset_head store_copies;
9787       int written = 0;
9788       rtx reg;
9789       rtx mem = loop_info->mems[i].mem;
9790       rtx mem_list_entry;
9791
9792       if (MEM_VOLATILE_P (mem)
9793           || loop_invariant_p (loop, XEXP (mem, 0)) != 1)
9794         /* There's no telling whether or not MEM is modified.  */
9795         loop_info->mems[i].optimize = 0;
9796
9797       /* Go through the MEMs written to in the loop to see if this
9798          one is aliased by one of them.  */
9799       mem_list_entry = loop_info->store_mems;
9800       while (mem_list_entry)
9801         {
9802           if (rtx_equal_p (mem, XEXP (mem_list_entry, 0)))
9803             written = 1;
9804           else if (true_dependence (XEXP (mem_list_entry, 0), VOIDmode,
9805                                     mem, rtx_varies_p))
9806             {
9807               /* MEM is indeed aliased by this store.  */
9808               loop_info->mems[i].optimize = 0;
9809               break;
9810             }
9811           mem_list_entry = XEXP (mem_list_entry, 1);
9812         }
9813
9814       if (flag_float_store && written
9815           && GET_MODE_CLASS (GET_MODE (mem)) == MODE_FLOAT)
9816         loop_info->mems[i].optimize = 0;
9817
9818       /* If this MEM is written to, we must be sure that there
9819          are no reads from another MEM that aliases this one.  */
9820       if (loop_info->mems[i].optimize && written)
9821         {
9822           int j;
9823
9824           for (j = 0; j < loop_info->mems_idx; ++j)
9825             {
9826               if (j == i)
9827                 continue;
9828               else if (true_dependence (mem,
9829                                         VOIDmode,
9830                                         loop_info->mems[j].mem,
9831                                         rtx_varies_p))
9832                 {
9833                   /* It's not safe to hoist loop_info->mems[i] out of
9834                      the loop because writes to it might not be
9835                      seen by reads from loop_info->mems[j].  */
9836                   loop_info->mems[i].optimize = 0;
9837                   break;
9838                 }
9839             }
9840         }
9841
9842       if (maybe_never && may_trap_p (mem))
9843         /* We can't access the MEM outside the loop; it might
9844            cause a trap that wouldn't have happened otherwise.  */
9845         loop_info->mems[i].optimize = 0;
9846
9847       if (!loop_info->mems[i].optimize)
9848         /* We thought we were going to lift this MEM out of the
9849            loop, but later discovered that we could not.  */
9850         continue;
9851
9852       INIT_REG_SET (&load_copies);
9853       INIT_REG_SET (&store_copies);
9854
9855       /* Allocate a pseudo for this MEM.  We set REG_USERVAR_P in
9856          order to keep scan_loop from moving stores to this MEM
9857          out of the loop just because this REG is neither a
9858          user-variable nor used in the loop test.  */
9859       reg = gen_reg_rtx (GET_MODE (mem));
9860       REG_USERVAR_P (reg) = 1;
9861       loop_info->mems[i].reg = reg;
9862
9863       /* Now, replace all references to the MEM with the
9864          corresponding pseudos.  */
9865       maybe_never = 0;
9866       for (p = next_insn_in_loop (loop, loop->scan_start);
9867            p != NULL_RTX;
9868            p = next_insn_in_loop (loop, p))
9869         {
9870           if (INSN_P (p))
9871             {
9872               rtx set;
9873
9874               set = single_set (p);
9875
9876               /* See if this copies the mem into a register that isn't
9877                  modified afterwards.  We'll try to do copy propagation
9878                  a little further on.  */
9879               if (set
9880                   /* @@@ This test is _way_ too conservative.  */
9881                   && ! maybe_never
9882                   && GET_CODE (SET_DEST (set)) == REG
9883                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
9884                   && REGNO (SET_DEST (set)) < last_max_reg
9885                   && regs->array[REGNO (SET_DEST (set))].n_times_set == 1
9886                   && rtx_equal_p (SET_SRC (set), mem))
9887                 SET_REGNO_REG_SET (&load_copies, REGNO (SET_DEST (set)));
9888
9889               /* See if this copies the mem from a register that isn't
9890                  modified afterwards.  We'll try to remove the
9891                  redundant copy later on by doing a little register
9892                  renaming and copy propagation.   This will help
9893                  to untangle things for the BIV detection code.  */
9894               if (set
9895                   && ! maybe_never
9896                   && GET_CODE (SET_SRC (set)) == REG
9897                   && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER
9898                   && REGNO (SET_SRC (set)) < last_max_reg
9899                   && regs->array[REGNO (SET_SRC (set))].n_times_set == 1
9900                   && rtx_equal_p (SET_DEST (set), mem))
9901                 SET_REGNO_REG_SET (&store_copies, REGNO (SET_SRC (set)));
9902
9903               /* If this is a call which uses / clobbers this memory
9904                  location, we must not change the interface here.  */
9905               if (GET_CODE (p) == CALL_INSN
9906                   && reg_mentioned_p (loop_info->mems[i].mem,
9907                                       CALL_INSN_FUNCTION_USAGE (p)))
9908                 {
9909                   cancel_changes (0);
9910                   loop_info->mems[i].optimize = 0;
9911                   break;
9912                 }
9913               else
9914                 /* Replace the memory reference with the shadow register.  */
9915                 replace_loop_mems (p, loop_info->mems[i].mem,
9916                                    loop_info->mems[i].reg);
9917             }
9918
9919           if (GET_CODE (p) == CODE_LABEL
9920               || GET_CODE (p) == JUMP_INSN)
9921             maybe_never = 1;
9922         }
9923
9924       if (! loop_info->mems[i].optimize)
9925         ; /* We found we couldn't do the replacement, so do nothing.  */
9926       else if (! apply_change_group ())
9927         /* We couldn't replace all occurrences of the MEM.  */
9928         loop_info->mems[i].optimize = 0;
9929       else
9930         {
9931           /* Load the memory immediately before LOOP->START, which is
9932              the NOTE_LOOP_BEG.  */
9933           cselib_val *e = cselib_lookup (mem, VOIDmode, 0);
9934           rtx set;
9935           rtx best = mem;
9936           int j;
9937           struct elt_loc_list *const_equiv = 0;
9938
9939           if (e)
9940             {
9941               struct elt_loc_list *equiv;
9942               struct elt_loc_list *best_equiv = 0;
9943               for (equiv = e->locs; equiv; equiv = equiv->next)
9944                 {
9945                   if (CONSTANT_P (equiv->loc))
9946                     const_equiv = equiv;
9947                   else if (GET_CODE (equiv->loc) == REG
9948                            /* Extending hard register lifetimes causes crash
9949                               on SRC targets.  Doing so on non-SRC is
9950                               probably also not good idea, since we most
9951                               probably have pseudoregister equivalence as
9952                               well.  */
9953                            && REGNO (equiv->loc) >= FIRST_PSEUDO_REGISTER)
9954                     best_equiv = equiv;
9955                 }
9956               /* Use the constant equivalence if that is cheap enough.  */
9957               if (! best_equiv)
9958                 best_equiv = const_equiv;
9959               else if (const_equiv
9960                        && (rtx_cost (const_equiv->loc, SET)
9961                            <= rtx_cost (best_equiv->loc, SET)))
9962                 {
9963                   best_equiv = const_equiv;
9964                   const_equiv = 0;
9965                 }
9966
9967               /* If best_equiv is nonzero, we know that MEM is set to a
9968                  constant or register before the loop.  We will use this
9969                  knowledge to initialize the shadow register with that
9970                  constant or reg rather than by loading from MEM.  */
9971               if (best_equiv)
9972                 best = copy_rtx (best_equiv->loc);
9973             }
9974
9975           set = gen_move_insn (reg, best);
9976           set = loop_insn_hoist (loop, set);
9977           if (REG_P (best))
9978             {
9979               for (p = prev_ebb_head; p != loop->start; p = NEXT_INSN (p))
9980                 if (REGNO_LAST_UID (REGNO (best)) == INSN_UID (p))
9981                   {
9982                     REGNO_LAST_UID (REGNO (best)) = INSN_UID (set);
9983                     break;
9984                   }
9985             }
9986
9987           if (const_equiv)
9988             set_unique_reg_note (set, REG_EQUAL, copy_rtx (const_equiv->loc));
9989
9990           if (written)
9991             {
9992               if (label == NULL_RTX)
9993                 {
9994                   label = gen_label_rtx ();
9995                   emit_label_after (label, loop->end);
9996                 }
9997
9998               /* Store the memory immediately after END, which is
9999                  the NOTE_LOOP_END.  */
10000               set = gen_move_insn (copy_rtx (mem), reg);
10001               loop_insn_emit_after (loop, 0, label, set);
10002             }
10003
10004           if (loop_dump_stream)
10005             {
10006               fprintf (loop_dump_stream, "Hoisted regno %d %s from ",
10007                        REGNO (reg), (written ? "r/w" : "r/o"));
10008               print_rtl (loop_dump_stream, mem);
10009               fputc ('\n', loop_dump_stream);
10010             }
10011
10012           /* Attempt a bit of copy propagation.  This helps untangle the
10013              data flow, and enables {basic,general}_induction_var to find
10014              more bivs/givs.  */
10015           EXECUTE_IF_SET_IN_REG_SET
10016             (&load_copies, FIRST_PSEUDO_REGISTER, j,
10017              {
10018                try_copy_prop (loop, reg, j);
10019              });
10020           CLEAR_REG_SET (&load_copies);
10021
10022           EXECUTE_IF_SET_IN_REG_SET
10023             (&store_copies, FIRST_PSEUDO_REGISTER, j,
10024              {
10025                try_swap_copy_prop (loop, reg, j);
10026              });
10027           CLEAR_REG_SET (&store_copies);
10028         }
10029     }
10030
10031   if (label != NULL_RTX && end_label != NULL_RTX)
10032     {
10033       /* Now, we need to replace all references to the previous exit
10034          label with the new one.  */
10035       rtx_pair rr;
10036       rr.r1 = end_label;
10037       rr.r2 = label;
10038
10039       for (p = loop->start; p != loop->end; p = NEXT_INSN (p))
10040         {
10041           for_each_rtx (&p, replace_label, &rr);
10042
10043           /* If this is a JUMP_INSN, then we also need to fix the JUMP_LABEL
10044              field.  This is not handled by for_each_rtx because it doesn't
10045              handle unprinted ('0') fields.  We need to update JUMP_LABEL
10046              because the immediately following unroll pass will use it.
10047              replace_label would not work anyways, because that only handles
10048              LABEL_REFs.  */
10049           if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == end_label)
10050             JUMP_LABEL (p) = label;
10051         }
10052     }
10053
10054   cselib_finish ();
10055 }
10056
10057 /* For communication between note_reg_stored and its caller.  */
10058 struct note_reg_stored_arg
10059 {
10060   int set_seen;
10061   rtx reg;
10062 };
10063
10064 /* Called via note_stores, record in SET_SEEN whether X, which is written,
10065    is equal to ARG.  */
10066 static void
10067 note_reg_stored (x, setter, arg)
10068      rtx x, setter ATTRIBUTE_UNUSED;
10069      void *arg;
10070 {
10071   struct note_reg_stored_arg *t = (struct note_reg_stored_arg *) arg;
10072   if (t->reg == x)
10073     t->set_seen = 1;
10074 }
10075
10076 /* Try to replace every occurrence of pseudo REGNO with REPLACEMENT.
10077    There must be exactly one insn that sets this pseudo; it will be
10078    deleted if all replacements succeed and we can prove that the register
10079    is not used after the loop.  */
10080
10081 static void
10082 try_copy_prop (loop, replacement, regno)
10083      const struct loop *loop;
10084      rtx replacement;
10085      unsigned int regno;
10086 {
10087   /* This is the reg that we are copying from.  */
10088   rtx reg_rtx = regno_reg_rtx[regno];
10089   rtx init_insn = 0;
10090   rtx insn;
10091   /* These help keep track of whether we replaced all uses of the reg.  */
10092   int replaced_last = 0;
10093   int store_is_first = 0;
10094
10095   for (insn = next_insn_in_loop (loop, loop->scan_start);
10096        insn != NULL_RTX;
10097        insn = next_insn_in_loop (loop, insn))
10098     {
10099       rtx set;
10100
10101       /* Only substitute within one extended basic block from the initializing
10102          insn.  */
10103       if (GET_CODE (insn) == CODE_LABEL && init_insn)
10104         break;
10105
10106       if (! INSN_P (insn))
10107         continue;
10108
10109       /* Is this the initializing insn?  */
10110       set = single_set (insn);
10111       if (set
10112           && GET_CODE (SET_DEST (set)) == REG
10113           && REGNO (SET_DEST (set)) == regno)
10114         {
10115           if (init_insn)
10116             abort ();
10117
10118           init_insn = insn;
10119           if (REGNO_FIRST_UID (regno) == INSN_UID (insn))
10120             store_is_first = 1;
10121         }
10122
10123       /* Only substitute after seeing the initializing insn.  */
10124       if (init_insn && insn != init_insn)
10125         {
10126           struct note_reg_stored_arg arg;
10127
10128           replace_loop_regs (insn, reg_rtx, replacement);
10129           if (REGNO_LAST_UID (regno) == INSN_UID (insn))
10130             replaced_last = 1;
10131
10132           /* Stop replacing when REPLACEMENT is modified.  */
10133           arg.reg = replacement;
10134           arg.set_seen = 0;
10135           note_stores (PATTERN (insn), note_reg_stored, &arg);
10136           if (arg.set_seen)
10137             {
10138               rtx note = find_reg_note (insn, REG_EQUAL, NULL);
10139
10140               /* It is possible that we've turned previously valid REG_EQUAL to
10141                  invalid, as we change the REGNO to REPLACEMENT and unlike REGNO,
10142                  REPLACEMENT is modified, we get different meaning.  */
10143               if (note && reg_mentioned_p (replacement, XEXP (note, 0)))
10144                 remove_note (insn, note);
10145               break;
10146             }
10147         }
10148     }
10149   if (! init_insn)
10150     abort ();
10151   if (apply_change_group ())
10152     {
10153       if (loop_dump_stream)
10154         fprintf (loop_dump_stream, "  Replaced reg %d", regno);
10155       if (store_is_first && replaced_last)
10156         {
10157           rtx first;
10158           rtx retval_note;
10159
10160           /* Assume we're just deleting INIT_INSN.  */
10161           first = init_insn;
10162           /* Look for REG_RETVAL note.  If we're deleting the end of
10163              the libcall sequence, the whole sequence can go.  */
10164           retval_note = find_reg_note (init_insn, REG_RETVAL, NULL_RTX);
10165           /* If we found a REG_RETVAL note, find the first instruction
10166              in the sequence.  */
10167           if (retval_note)
10168             first = XEXP (retval_note, 0);
10169
10170           /* Delete the instructions.  */
10171           loop_delete_insns (first, init_insn);
10172         }
10173       if (loop_dump_stream)
10174         fprintf (loop_dump_stream, ".\n");
10175     }
10176 }
10177
10178 /* Replace all the instructions from FIRST up to and including LAST
10179    with NOTE_INSN_DELETED notes.  */
10180
10181 static void
10182 loop_delete_insns (first, last)
10183      rtx first;
10184      rtx last;
10185 {
10186   while (1)
10187     {
10188       if (loop_dump_stream)
10189         fprintf (loop_dump_stream, ", deleting init_insn (%d)",
10190                  INSN_UID (first));
10191       delete_insn (first);
10192
10193       /* If this was the LAST instructions we're supposed to delete,
10194          we're done.  */
10195       if (first == last)
10196         break;
10197
10198       first = NEXT_INSN (first);
10199     }
10200 }
10201
10202 /* Try to replace occurrences of pseudo REGNO with REPLACEMENT within
10203    loop LOOP if the order of the sets of these registers can be
10204    swapped.  There must be exactly one insn within the loop that sets
10205    this pseudo followed immediately by a move insn that sets
10206    REPLACEMENT with REGNO.  */
10207 static void
10208 try_swap_copy_prop (loop, replacement, regno)
10209      const struct loop *loop;
10210      rtx replacement;
10211      unsigned int regno;
10212 {
10213   rtx insn;
10214   rtx set = NULL_RTX;
10215   unsigned int new_regno;
10216
10217   new_regno = REGNO (replacement);
10218
10219   for (insn = next_insn_in_loop (loop, loop->scan_start);
10220        insn != NULL_RTX;
10221        insn = next_insn_in_loop (loop, insn))
10222     {
10223       /* Search for the insn that copies REGNO to NEW_REGNO?  */
10224       if (INSN_P (insn)
10225           && (set = single_set (insn))
10226           && GET_CODE (SET_DEST (set)) == REG
10227           && REGNO (SET_DEST (set)) == new_regno
10228           && GET_CODE (SET_SRC (set)) == REG
10229           && REGNO (SET_SRC (set)) == regno)
10230         break;
10231     }
10232
10233   if (insn != NULL_RTX)
10234     {
10235       rtx prev_insn;
10236       rtx prev_set;
10237
10238       /* Some DEF-USE info would come in handy here to make this
10239          function more general.  For now, just check the previous insn
10240          which is the most likely candidate for setting REGNO.  */
10241
10242       prev_insn = PREV_INSN (insn);
10243
10244       if (INSN_P (insn)
10245           && (prev_set = single_set (prev_insn))
10246           && GET_CODE (SET_DEST (prev_set)) == REG
10247           && REGNO (SET_DEST (prev_set)) == regno)
10248         {
10249           /* We have:
10250              (set (reg regno) (expr))
10251              (set (reg new_regno) (reg regno))
10252
10253              so try converting this to:
10254              (set (reg new_regno) (expr))
10255              (set (reg regno) (reg new_regno))
10256
10257              The former construct is often generated when a global
10258              variable used for an induction variable is shadowed by a
10259              register (NEW_REGNO).  The latter construct improves the
10260              chances of GIV replacement and BIV elimination.  */
10261
10262           validate_change (prev_insn, &SET_DEST (prev_set),
10263                            replacement, 1);
10264           validate_change (insn, &SET_DEST (set),
10265                            SET_SRC (set), 1);
10266           validate_change (insn, &SET_SRC (set),
10267                            replacement, 1);
10268
10269           if (apply_change_group ())
10270             {
10271               if (loop_dump_stream)
10272                 fprintf (loop_dump_stream,
10273                          "  Swapped set of reg %d at %d with reg %d at %d.\n",
10274                          regno, INSN_UID (insn),
10275                          new_regno, INSN_UID (prev_insn));
10276
10277               /* Update first use of REGNO.  */
10278               if (REGNO_FIRST_UID (regno) == INSN_UID (prev_insn))
10279                 REGNO_FIRST_UID (regno) = INSN_UID (insn);
10280
10281               /* Now perform copy propagation to hopefully
10282                  remove all uses of REGNO within the loop.  */
10283               try_copy_prop (loop, replacement, regno);
10284             }
10285         }
10286     }
10287 }
10288
10289 /* Replace MEM with its associated pseudo register.  This function is
10290    called from load_mems via for_each_rtx.  DATA is actually a pointer
10291    to a structure describing the instruction currently being scanned
10292    and the MEM we are currently replacing.  */
10293
10294 static int
10295 replace_loop_mem (mem, data)
10296      rtx *mem;
10297      void *data;
10298 {
10299   loop_replace_args *args = (loop_replace_args *) data;
10300   rtx m = *mem;
10301
10302   if (m == NULL_RTX)
10303     return 0;
10304
10305   switch (GET_CODE (m))
10306     {
10307     case MEM:
10308       break;
10309
10310     case CONST_DOUBLE:
10311       /* We're not interested in the MEM associated with a
10312          CONST_DOUBLE, so there's no need to traverse into one.  */
10313       return -1;
10314
10315     default:
10316       /* This is not a MEM.  */
10317       return 0;
10318     }
10319
10320   if (!rtx_equal_p (args->match, m))
10321     /* This is not the MEM we are currently replacing.  */
10322     return 0;
10323
10324   /* Actually replace the MEM.  */
10325   validate_change (args->insn, mem, args->replacement, 1);
10326
10327   return 0;
10328 }
10329
10330 static void
10331 replace_loop_mems (insn, mem, reg)
10332      rtx insn;
10333      rtx mem;
10334      rtx reg;
10335 {
10336   loop_replace_args args;
10337
10338   args.insn = insn;
10339   args.match = mem;
10340   args.replacement = reg;
10341
10342   for_each_rtx (&insn, replace_loop_mem, &args);
10343 }
10344
10345 /* Replace one register with another.  Called through for_each_rtx; PX points
10346    to the rtx being scanned.  DATA is actually a pointer to
10347    a structure of arguments.  */
10348
10349 static int
10350 replace_loop_reg (px, data)
10351      rtx *px;
10352      void *data;
10353 {
10354   rtx x = *px;
10355   loop_replace_args *args = (loop_replace_args *) data;
10356
10357   if (x == NULL_RTX)
10358     return 0;
10359
10360   if (x == args->match)
10361     validate_change (args->insn, px, args->replacement, 1);
10362
10363   return 0;
10364 }
10365
10366 static void
10367 replace_loop_regs (insn, reg, replacement)
10368      rtx insn;
10369      rtx reg;
10370      rtx replacement;
10371 {
10372   loop_replace_args args;
10373
10374   args.insn = insn;
10375   args.match = reg;
10376   args.replacement = replacement;
10377
10378   for_each_rtx (&insn, replace_loop_reg, &args);
10379 }
10380
10381 /* Replace occurrences of the old exit label for the loop with the new
10382    one.  DATA is an rtx_pair containing the old and new labels,
10383    respectively.  */
10384
10385 static int
10386 replace_label (x, data)
10387      rtx *x;
10388      void *data;
10389 {
10390   rtx l = *x;
10391   rtx old_label = ((rtx_pair *) data)->r1;
10392   rtx new_label = ((rtx_pair *) data)->r2;
10393
10394   if (l == NULL_RTX)
10395     return 0;
10396
10397   if (GET_CODE (l) != LABEL_REF)
10398     return 0;
10399
10400   if (XEXP (l, 0) != old_label)
10401     return 0;
10402
10403   XEXP (l, 0) = new_label;
10404   ++LABEL_NUSES (new_label);
10405   --LABEL_NUSES (old_label);
10406
10407   return 0;
10408 }
10409 \f
10410 /* Emit insn for PATTERN after WHERE_INSN in basic block WHERE_BB
10411    (ignored in the interim).  */
10412
10413 static rtx
10414 loop_insn_emit_after (loop, where_bb, where_insn, pattern)
10415      const struct loop *loop ATTRIBUTE_UNUSED;
10416      basic_block where_bb ATTRIBUTE_UNUSED;
10417      rtx where_insn;
10418      rtx pattern;
10419 {
10420   return emit_insn_after (pattern, where_insn);
10421 }
10422
10423
10424 /* If WHERE_INSN is non-zero emit insn for PATTERN before WHERE_INSN
10425    in basic block WHERE_BB (ignored in the interim) within the loop
10426    otherwise hoist PATTERN into the loop pre-header.  */
10427
10428 rtx
10429 loop_insn_emit_before (loop, where_bb, where_insn, pattern)
10430      const struct loop *loop;
10431      basic_block where_bb ATTRIBUTE_UNUSED;
10432      rtx where_insn;
10433      rtx pattern;
10434 {
10435   if (! where_insn)
10436     return loop_insn_hoist (loop, pattern);
10437   return emit_insn_before (pattern, where_insn);
10438 }
10439
10440
10441 /* Emit call insn for PATTERN before WHERE_INSN in basic block
10442    WHERE_BB (ignored in the interim) within the loop.  */
10443
10444 static rtx
10445 loop_call_insn_emit_before (loop, where_bb, where_insn, pattern)
10446      const struct loop *loop ATTRIBUTE_UNUSED;
10447      basic_block where_bb ATTRIBUTE_UNUSED;
10448      rtx where_insn;
10449      rtx pattern;
10450 {
10451   return emit_call_insn_before (pattern, where_insn);
10452 }
10453
10454
10455 /* Hoist insn for PATTERN into the loop pre-header.  */
10456
10457 rtx
10458 loop_insn_hoist (loop, pattern)
10459      const struct loop *loop;
10460      rtx pattern;
10461 {
10462   return loop_insn_emit_before (loop, 0, loop->start, pattern);
10463 }
10464
10465
10466 /* Hoist call insn for PATTERN into the loop pre-header.  */
10467
10468 static rtx
10469 loop_call_insn_hoist (loop, pattern)
10470      const struct loop *loop;
10471      rtx pattern;
10472 {
10473   return loop_call_insn_emit_before (loop, 0, loop->start, pattern);
10474 }
10475
10476
10477 /* Sink insn for PATTERN after the loop end.  */
10478
10479 rtx
10480 loop_insn_sink (loop, pattern)
10481      const struct loop *loop;
10482      rtx pattern;
10483 {
10484   return loop_insn_emit_before (loop, 0, loop->sink, pattern);
10485 }
10486
10487 /* bl->final_value can be eighter general_operand or PLUS of general_operand
10488    and constant.  Emit sequence of intructions to load it into REG  */
10489 static rtx
10490 gen_load_of_final_value (reg, final_value)
10491      rtx reg, final_value;
10492 {
10493   rtx seq;
10494   start_sequence ();
10495   final_value = force_operand (final_value, reg);
10496   if (final_value != reg)
10497     emit_move_insn (reg, final_value);
10498   seq = gen_sequence ();
10499   end_sequence ();
10500   return seq;
10501 }
10502
10503 /* If the loop has multiple exits, emit insn for PATTERN before the
10504    loop to ensure that it will always be executed no matter how the
10505    loop exits.  Otherwise, emit the insn for PATTERN after the loop,
10506    since this is slightly more efficient.  */
10507
10508 static rtx
10509 loop_insn_sink_or_swim (loop, pattern)
10510      const struct loop *loop;
10511      rtx pattern;
10512 {
10513   if (loop->exit_count)
10514     return loop_insn_hoist (loop, pattern);
10515   else
10516     return loop_insn_sink (loop, pattern);
10517 }
10518 \f
10519 static void
10520 loop_ivs_dump (loop, file, verbose)
10521      const struct loop *loop;
10522      FILE *file;
10523      int verbose;
10524 {
10525   struct iv_class *bl;
10526   int iv_num = 0;
10527
10528   if (! loop || ! file)
10529     return;
10530
10531   for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10532     iv_num++;
10533
10534   fprintf (file, "Loop %d: %d IV classes\n", loop->num, iv_num);
10535
10536   for (bl = LOOP_IVS (loop)->list; bl; bl = bl->next)
10537     {
10538       loop_iv_class_dump (bl, file, verbose);
10539       fputc ('\n', file);
10540     }
10541 }
10542
10543
10544 static void
10545 loop_iv_class_dump (bl, file, verbose)
10546      const struct iv_class *bl;
10547      FILE *file;
10548      int verbose ATTRIBUTE_UNUSED;
10549 {
10550   struct induction *v;
10551   rtx incr;
10552   int i;
10553
10554   if (! bl || ! file)
10555     return;
10556
10557   fprintf (file, "IV class for reg %d, benefit %d\n",
10558            bl->regno, bl->total_benefit);
10559
10560   fprintf (file, " Init insn %d", INSN_UID (bl->init_insn));
10561   if (bl->initial_value)
10562     {
10563       fprintf (file, ", init val: ");
10564       print_simple_rtl (file, bl->initial_value);
10565     }
10566   if (bl->initial_test)
10567     {
10568       fprintf (file, ", init test: ");
10569       print_simple_rtl (file, bl->initial_test);
10570     }
10571   fputc ('\n', file);
10572
10573   if (bl->final_value)
10574     {
10575       fprintf (file, " Final val: ");
10576       print_simple_rtl (file, bl->final_value);
10577       fputc ('\n', file);
10578     }
10579
10580   if ((incr = biv_total_increment (bl)))
10581     {
10582       fprintf (file, " Total increment: ");
10583       print_simple_rtl (file, incr);
10584       fputc ('\n', file);
10585     }
10586
10587   /* List the increments.  */
10588   for (i = 0, v = bl->biv; v; v = v->next_iv, i++)
10589     {
10590       fprintf (file, " Inc%d: insn %d, incr: ", i, INSN_UID (v->insn));
10591       print_simple_rtl (file, v->add_val);
10592       fputc ('\n', file);
10593     }
10594
10595   /* List the givs.  */
10596   for (i = 0, v = bl->giv; v; v = v->next_iv, i++)
10597     {
10598       fprintf (file, " Giv%d: insn %d, benefit %d, ",
10599                i, INSN_UID (v->insn), v->benefit);
10600       if (v->giv_type == DEST_ADDR)
10601         print_simple_rtl (file, v->mem);
10602       else
10603         print_simple_rtl (file, single_set (v->insn));
10604       fputc ('\n', file);
10605     }
10606 }
10607
10608
10609 static void
10610 loop_biv_dump (v, file, verbose)
10611      const struct induction *v;
10612      FILE *file;
10613      int verbose;
10614 {
10615   if (! v || ! file)
10616     return;
10617
10618   fprintf (file,
10619            "Biv %d: insn %d",
10620            REGNO (v->dest_reg), INSN_UID (v->insn));
10621   fprintf (file, " const ");
10622   print_simple_rtl (file, v->add_val);
10623
10624   if (verbose && v->final_value)
10625     {
10626       fputc ('\n', file);
10627       fprintf (file, " final ");
10628       print_simple_rtl (file, v->final_value);
10629     }
10630
10631   fputc ('\n', file);
10632 }
10633
10634
10635 static void
10636 loop_giv_dump (v, file, verbose)
10637      const struct induction *v;
10638      FILE *file;
10639      int verbose;
10640 {
10641   if (! v || ! file)
10642     return;
10643
10644   if (v->giv_type == DEST_REG)
10645     fprintf (file, "Giv %d: insn %d",
10646              REGNO (v->dest_reg), INSN_UID (v->insn));
10647   else
10648     fprintf (file, "Dest address: insn %d",
10649              INSN_UID (v->insn));
10650
10651   fprintf (file, " src reg %d benefit %d",
10652            REGNO (v->src_reg), v->benefit);
10653   fprintf (file, " lifetime %d",
10654            v->lifetime);
10655
10656   if (v->replaceable)
10657     fprintf (file, " replaceable");
10658
10659   if (v->no_const_addval)
10660     fprintf (file, " ncav");
10661
10662   if (v->ext_dependent)
10663     {
10664       switch (GET_CODE (v->ext_dependent))
10665         {
10666         case SIGN_EXTEND:
10667           fprintf (file, " ext se");
10668           break;
10669         case ZERO_EXTEND:
10670           fprintf (file, " ext ze");
10671           break;
10672         case TRUNCATE:
10673           fprintf (file, " ext tr");
10674           break;
10675         default:
10676           abort ();
10677         }
10678     }
10679
10680   fputc ('\n', file);
10681   fprintf (file, " mult ");
10682   print_simple_rtl (file, v->mult_val);
10683
10684   fputc ('\n', file);
10685   fprintf (file, " add  ");
10686   print_simple_rtl (file, v->add_val);
10687
10688   if (verbose && v->final_value)
10689     {
10690       fputc ('\n', file);
10691       fprintf (file, " final ");
10692       print_simple_rtl (file, v->final_value);
10693     }
10694
10695   fputc ('\n', file);
10696 }
10697
10698
10699 void
10700 debug_ivs (loop)
10701      const struct loop *loop;
10702 {
10703   loop_ivs_dump (loop, stderr, 1);
10704 }
10705
10706
10707 void
10708 debug_iv_class (bl)
10709      const struct iv_class *bl;
10710 {
10711   loop_iv_class_dump (bl, stderr, 1);
10712 }
10713
10714
10715 void
10716 debug_biv (v)
10717      const struct induction *v;
10718 {
10719   loop_biv_dump (v, stderr, 1);
10720 }
10721
10722
10723 void
10724 debug_giv (v)
10725      const struct induction *v;
10726 {
10727   loop_giv_dump (v, stderr, 1);
10728 }
10729
10730
10731 #define LOOP_BLOCK_NUM_1(INSN) \
10732 ((INSN) ? (BLOCK_FOR_INSN (INSN) ? BLOCK_NUM (INSN) : - 1) : -1)
10733
10734 /* The notes do not have an assigned block, so look at the next insn.  */
10735 #define LOOP_BLOCK_NUM(INSN) \
10736 ((INSN) ? (GET_CODE (INSN) == NOTE \
10737             ? LOOP_BLOCK_NUM_1 (next_nonnote_insn (INSN)) \
10738             : LOOP_BLOCK_NUM_1 (INSN)) \
10739         : -1)
10740
10741 #define LOOP_INSN_UID(INSN) ((INSN) ? INSN_UID (INSN) : -1)
10742
10743 static void
10744 loop_dump_aux (loop, file, verbose)
10745      const struct loop *loop;
10746      FILE *file;
10747      int verbose ATTRIBUTE_UNUSED;
10748 {
10749   rtx label;
10750
10751   if (! loop || ! file)
10752     return;
10753
10754   /* Print diagnostics to compare our concept of a loop with
10755      what the loop notes say.  */
10756   if (! PREV_INSN (loop->first->head)
10757       || GET_CODE (PREV_INSN (loop->first->head)) != NOTE
10758       || NOTE_LINE_NUMBER (PREV_INSN (loop->first->head))
10759       != NOTE_INSN_LOOP_BEG)
10760     fprintf (file, ";;  No NOTE_INSN_LOOP_BEG at %d\n",
10761              INSN_UID (PREV_INSN (loop->first->head)));
10762   if (! NEXT_INSN (loop->last->end)
10763       || GET_CODE (NEXT_INSN (loop->last->end)) != NOTE
10764       || NOTE_LINE_NUMBER (NEXT_INSN (loop->last->end))
10765       != NOTE_INSN_LOOP_END)
10766     fprintf (file, ";;  No NOTE_INSN_LOOP_END at %d\n",
10767              INSN_UID (NEXT_INSN (loop->last->end)));
10768
10769   if (loop->start)
10770     {
10771       fprintf (file,
10772                ";;  start %d (%d), cont dom %d (%d), cont %d (%d), vtop %d (%d), end %d (%d)\n",
10773                LOOP_BLOCK_NUM (loop->start),
10774                LOOP_INSN_UID (loop->start),
10775                LOOP_BLOCK_NUM (loop->cont),
10776                LOOP_INSN_UID (loop->cont),
10777                LOOP_BLOCK_NUM (loop->cont),
10778                LOOP_INSN_UID (loop->cont),
10779                LOOP_BLOCK_NUM (loop->vtop),
10780                LOOP_INSN_UID (loop->vtop),
10781                LOOP_BLOCK_NUM (loop->end),
10782                LOOP_INSN_UID (loop->end));
10783       fprintf (file, ";;  top %d (%d), scan start %d (%d)\n",
10784                LOOP_BLOCK_NUM (loop->top),
10785                LOOP_INSN_UID (loop->top),
10786                LOOP_BLOCK_NUM (loop->scan_start),
10787                LOOP_INSN_UID (loop->scan_start));
10788       fprintf (file, ";;  exit_count %d", loop->exit_count);
10789       if (loop->exit_count)
10790         {
10791           fputs (", labels:", file);
10792           for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
10793             {
10794               fprintf (file, " %d ",
10795                        LOOP_INSN_UID (XEXP (label, 0)));
10796             }
10797         }
10798       fputs ("\n", file);
10799
10800       /* This can happen when a marked loop appears as two nested loops,
10801          say from while (a || b) {}.  The inner loop won't match
10802          the loop markers but the outer one will.  */
10803       if (LOOP_BLOCK_NUM (loop->cont) != loop->latch->index)
10804         fprintf (file, ";;  NOTE_INSN_LOOP_CONT not in loop latch\n");
10805     }
10806 }
10807
10808 /* Call this function from the debugger to dump LOOP.  */
10809
10810 void
10811 debug_loop (loop)
10812      const struct loop *loop;
10813 {
10814   flow_loop_dump (loop, stderr, loop_dump_aux, 1);
10815 }
10816
10817 /* Call this function from the debugger to dump LOOPS.  */
10818
10819 void
10820 debug_loops (loops)
10821      const struct loops *loops;
10822 {
10823   flow_loops_dump (loops, stderr, loop_dump_aux, 1);
10824 }