OSDN Git Service

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