OSDN Git Service

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