OSDN Git Service

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