OSDN Git Service

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