OSDN Git Service

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