OSDN Git Service

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