OSDN Git Service

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