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