OSDN Git Service

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