OSDN Git Service

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