OSDN Git Service

* loop.c (check_final_value): Don't miss a biv increment in a
[pf3gnuchains/gcc-fork.git] / gcc / loop.c
1 /* Perform various loop optimizations, including strength reduction.
2    Copyright (C) 1987, 88, 89, 91-6, 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
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 <stdio.h>
38 #include "config.h"
39 #include "rtl.h"
40 #include "obstack.h"
41 #include "expr.h"
42 #include "insn-config.h"
43 #include "insn-flags.h"
44 #include "regs.h"
45 #include "hard-reg-set.h"
46 #include "recog.h"
47 #include "flags.h"
48 #include "real.h"
49 #include "loop.h"
50 #include "except.h"
51
52 /* Vector mapping INSN_UIDs to luids.
53    The luids are like uids but increase monotonically always.
54    We use them to see whether a jump comes from outside a given loop.  */
55
56 int *uid_luid;
57
58 /* Indexed by INSN_UID, contains the ordinal giving the (innermost) loop
59    number the insn is contained in.  */
60
61 int *uid_loop_num;
62
63 /* 1 + largest uid of any insn.  */
64
65 int max_uid_for_loop;
66
67 /* 1 + luid of last insn.  */
68
69 static int max_luid;
70
71 /* Number of loops detected in current function.  Used as index to the
72    next few tables.  */
73
74 static int max_loop_num;
75
76 /* Indexed by loop number, contains the first and last insn of each loop.  */
77
78 static rtx *loop_number_loop_starts, *loop_number_loop_ends;
79
80 /* For each loop, gives the containing loop number, -1 if none.  */
81
82 int *loop_outer_loop;
83
84 #ifdef HAIFA
85 /* The main output of analyze_loop_iterations is placed here */
86
87 int *loop_can_insert_bct;
88
89 /* For each loop, determines whether some of its inner loops has used
90    count register */
91
92 int *loop_used_count_register;
93
94 /* For each loop, remember its unrolling factor (if at all).
95    contents of the array:
96    0/1: not unrolled.
97    -1: completely unrolled - no further instrumentation is needed.
98    >1: holds the exact amount of unrolling.  */
99
100 int *loop_unroll_factor;
101 int *loop_unroll_iter;
102
103 /* loop parameters for arithmetic loops. These loops have a loop variable
104    which is initialized to loop_start_value, incremented in each iteration
105    by "loop_increment".  At the end of the iteration the loop variable is
106    compared to the loop_comparison_value (using loop_comparison_code).  */
107
108 rtx *loop_increment;
109 rtx *loop_comparison_value;
110 rtx *loop_start_value;
111 enum rtx_code *loop_comparison_code;
112
113 /* for debugging: selects sub-range of loops for which the bct optimization
114    is invoked.  The numbering is per compilation-unit.  */
115 int dbg_bct_min = -1;
116 int dbg_bct_max = -1;
117 #endif  /* HAIFA */
118
119
120 /* Indexed by loop number, contains a nonzero value if the "loop" isn't
121    really a loop (an insn outside the loop branches into it).  */
122
123 static char *loop_invalid;
124
125 /* Indexed by loop number, links together all LABEL_REFs which refer to
126    code labels outside the loop.  Used by routines that need to know all
127    loop exits, such as final_biv_value and final_giv_value.
128
129    This does not include loop exits due to return instructions.  This is
130    because all bivs and givs are pseudos, and hence must be dead after a
131    return, so the presense of a return does not affect any of the
132    optimizations that use this info.  It is simpler to just not include return
133    instructions on this list.  */
134
135 rtx *loop_number_exit_labels;
136
137 /* Indexed by loop number, counts the number of LABEL_REFs on
138    loop_number_exit_labels for this loop and all loops nested inside it.  */
139
140 int *loop_number_exit_count;
141
142 /* Holds the number of loop iterations.  It is zero if the number could not be
143    calculated.  Must be unsigned since the number of iterations can
144    be as high as 2^wordsize-1.  For loops with a wider iterator, this number
145    will will be zero if the number of loop iterations is too large for an
146    unsigned integer to hold.  */
147
148 unsigned HOST_WIDE_INT loop_n_iterations;
149
150 /* Nonzero if there is a subroutine call in the current loop.  */
151
152 static int loop_has_call;
153
154 /* Nonzero if there is a volatile memory reference in the current
155    loop.  */
156
157 static int loop_has_volatile;
158
159 /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
160    current loop.  A continue statement will generate a branch to
161    NEXT_INSN (loop_continue).  */
162
163 static rtx loop_continue;
164
165 /* Indexed by register number, contains the number of times the reg
166    is set during the loop being scanned.
167    During code motion, a negative value indicates a reg that has been
168    made a candidate; in particular -2 means that it is an candidate that
169    we know is equal to a constant and -1 means that it is an candidate
170    not known equal to a constant.
171    After code motion, regs moved have 0 (which is accurate now)
172    while the failed candidates have the original number of times set.
173
174    Therefore, at all times, == 0 indicates an invariant register;
175    < 0 a conditionally invariant one.  */
176
177 static int *n_times_set;
178
179 /* Original value of n_times_set; same except that this value
180    is not set negative for a reg whose sets have been made candidates
181    and not set to 0 for a reg that is moved.  */
182
183 static int *n_times_used;
184
185 /* Index by register number, 1 indicates that the register
186    cannot be moved or strength reduced.  */
187
188 static char *may_not_optimize;
189
190 /* Nonzero means reg N has already been moved out of one loop.
191    This reduces the desire to move it out of another.  */
192
193 static char *moved_once;
194
195 /* Array of MEMs that are stored in this loop. If there are too many to fit
196    here, we just turn on unknown_address_altered.  */
197
198 #define NUM_STORES 30
199 static rtx loop_store_mems[NUM_STORES];
200
201 /* Index of first available slot in above array.  */
202 static int loop_store_mems_idx;
203
204 /* Nonzero if we don't know what MEMs were changed in the current loop.
205    This happens if the loop contains a call (in which case `loop_has_call'
206    will also be set) or if we store into more than NUM_STORES MEMs.  */
207
208 static int unknown_address_altered;
209
210 /* Count of movable (i.e. invariant) instructions discovered in the loop.  */
211 static int num_movables;
212
213 /* Count of memory write instructions discovered in the loop.  */
214 static int num_mem_sets;
215
216 /* Number of loops contained within the current one, including itself.  */
217 static int loops_enclosed;
218
219 /* Bound on pseudo register number before loop optimization.
220    A pseudo has valid regscan info if its number is < max_reg_before_loop.  */
221 int max_reg_before_loop;
222
223 /* This obstack is used in product_cheap_p to allocate its rtl.  It
224    may call gen_reg_rtx which, in turn, may reallocate regno_reg_rtx.
225    If we used the same obstack that it did, we would be deallocating
226    that array.  */
227
228 static struct obstack temp_obstack;
229
230 /* This is where the pointer to the obstack being used for RTL is stored.  */
231
232 extern struct obstack *rtl_obstack;
233
234 #define obstack_chunk_alloc xmalloc
235 #define obstack_chunk_free free
236
237 extern char *oballoc ();
238 \f
239 /* During the analysis of a loop, a chain of `struct movable's
240    is made to record all the movable insns found.
241    Then the entire chain can be scanned to decide which to move.  */
242
243 struct movable
244 {
245   rtx insn;                     /* A movable insn */
246   rtx set_src;                  /* The expression this reg is set from.  */
247   rtx set_dest;                 /* The destination of this SET.  */
248   rtx dependencies;             /* When INSN is libcall, this is an EXPR_LIST
249                                    of any registers used within the LIBCALL.  */
250   int consec;                   /* Number of consecutive following insns 
251                                    that must be moved with this one.  */
252   int regno;                    /* The register it sets */
253   short lifetime;               /* lifetime of that register;
254                                    may be adjusted when matching movables
255                                    that load the same value are found.  */
256   short savings;                /* Number of insns we can move for this reg,
257                                    including other movables that force this
258                                    or match this one.  */
259   unsigned int cond : 1;        /* 1 if only conditionally movable */
260   unsigned int force : 1;       /* 1 means MUST move this insn */
261   unsigned int global : 1;      /* 1 means reg is live outside this loop */
262                 /* If PARTIAL is 1, GLOBAL means something different:
263                    that the reg is live outside the range from where it is set
264                    to the following label.  */
265   unsigned int done : 1;        /* 1 inhibits further processing of this */
266   
267   unsigned int partial : 1;     /* 1 means this reg is used for zero-extending.
268                                    In particular, moving it does not make it
269                                    invariant.  */
270   unsigned int move_insn : 1;   /* 1 means that we call emit_move_insn to
271                                    load SRC, rather than copying INSN.  */
272   unsigned int is_equiv : 1;    /* 1 means a REG_EQUIV is present on INSN.  */
273   enum machine_mode savemode;   /* Nonzero means it is a mode for a low part
274                                    that we should avoid changing when clearing
275                                    the rest of the reg.  */
276   struct movable *match;        /* First entry for same value */
277   struct movable *forces;       /* An insn that must be moved if this is */
278   struct movable *next;
279 };
280
281 FILE *loop_dump_stream;
282
283 /* Forward declarations.  */
284
285 static void find_and_verify_loops ();
286 static void mark_loop_jump ();
287 static void prescan_loop ();
288 static int reg_in_basic_block_p ();
289 static int consec_sets_invariant_p ();
290 static rtx libcall_other_reg ();
291 static int labels_in_range_p ();
292 static void count_loop_regs_set ();
293 static void note_addr_stored ();
294 static int loop_reg_used_before_p ();
295 static void scan_loop ();
296 static void replace_call_address ();
297 static rtx skip_consec_insns ();
298 static int libcall_benefit ();
299 static void ignore_some_movables ();
300 static void force_movables ();
301 static void combine_movables ();
302 static int rtx_equal_for_loop_p ();
303 static void move_movables ();
304 static void strength_reduce ();
305 static int valid_initial_value_p ();
306 static void find_mem_givs ();
307 static void record_biv ();
308 static void check_final_value ();
309 static void record_giv ();
310 static void update_giv_derive ();
311 static int basic_induction_var ();
312 static rtx simplify_giv_expr ();
313 static int general_induction_var ();
314 static int consec_sets_giv ();
315 static int check_dbra_loop ();
316 static rtx express_from ();
317 static int combine_givs_p ();
318 static void combine_givs ();
319 static int product_cheap_p ();
320 static int maybe_eliminate_biv ();
321 static int maybe_eliminate_biv_1 ();
322 static int last_use_this_basic_block ();
323 static void record_initial ();
324 static void update_reg_last_use ();
325
326 #ifdef HAIFA
327 /* This is extern from unroll.c */
328 void iteration_info ();
329
330 /* Two main functions for implementing bct:
331    first - to be called before loop unrolling, and the second - after */
332 static void analyze_loop_iterations ();
333 static void insert_bct ();
334
335 /* Auxiliary function that inserts the bct pattern into the loop */
336 static void instrument_loop_bct ();
337
338 /* Indirect_jump_in_function is computed once per function.  */
339 int indirect_jump_in_function = 0;
340 static int indirect_jump_in_function_p ();
341
342 int loop_number ();
343
344 /* Debugging functions.  */
345 int fix_bct_param ();
346 static int check_bct_param ();
347 #endif  /* HAIFA */
348
349 \f
350 /* Relative gain of eliminating various kinds of operations.  */
351 int add_cost;
352 #if 0
353 int shift_cost;
354 int mult_cost;
355 #endif
356
357 /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
358    copy the value of the strength reduced giv to its original register.  */
359 int copy_cost;
360
361 void
362 init_loop ()
363 {
364   char *free_point = (char *) oballoc (1);
365   rtx reg = gen_rtx (REG, word_mode, LAST_VIRTUAL_REGISTER + 1);
366
367   add_cost = rtx_cost (gen_rtx (PLUS, word_mode, reg, reg), SET);
368
369   /* We multiply by 2 to reconcile the difference in scale between
370      these two ways of computing costs.  Otherwise the cost of a copy
371      will be far less than the cost of an add.  */
372
373   copy_cost = 2 * 2;
374
375   /* Free the objects we just allocated.  */
376   obfree (free_point);
377
378   /* Initialize the obstack used for rtl in product_cheap_p.  */
379   gcc_obstack_init (&temp_obstack);
380 }
381 \f
382 /* Entry point of this file.  Perform loop optimization
383    on the current function.  F is the first insn of the function
384    and DUMPFILE is a stream for output of a trace of actions taken
385    (or 0 if none should be output).  */
386
387 void
388 loop_optimize (f, dumpfile)
389      /* f is the first instruction of a chain of insns for one function */
390      rtx f;
391      FILE *dumpfile;
392 {
393   register rtx insn;
394   register int i;
395   rtx last_insn;
396
397   loop_dump_stream = dumpfile;
398
399   init_recog_no_volatile ();
400   init_alias_analysis ();
401
402   max_reg_before_loop = max_reg_num ();
403
404   moved_once = (char *) alloca (max_reg_before_loop);
405   bzero (moved_once, max_reg_before_loop);
406
407   regs_may_share = 0;
408
409   /* Count the number of loops.  */
410
411   max_loop_num = 0;
412   for (insn = f; insn; insn = NEXT_INSN (insn))
413     {
414       if (GET_CODE (insn) == NOTE
415           && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
416         max_loop_num++;
417     }
418
419   /* Don't waste time if no loops.  */
420   if (max_loop_num == 0)
421     return;
422
423   /* Get size to use for tables indexed by uids.
424      Leave some space for labels allocated by find_and_verify_loops.  */
425   max_uid_for_loop = get_max_uid () + 1 + max_loop_num * 32;
426
427   uid_luid = (int *) alloca (max_uid_for_loop * sizeof (int));
428   uid_loop_num = (int *) alloca (max_uid_for_loop * sizeof (int));
429
430   bzero ((char *) uid_luid, max_uid_for_loop * sizeof (int));
431   bzero ((char *) uid_loop_num, max_uid_for_loop * sizeof (int));
432
433   /* Allocate tables for recording each loop.  We set each entry, so they need
434      not be zeroed.  */
435   loop_number_loop_starts = (rtx *) alloca (max_loop_num * sizeof (rtx));
436   loop_number_loop_ends = (rtx *) alloca (max_loop_num * sizeof (rtx));
437   loop_outer_loop = (int *) alloca (max_loop_num * sizeof (int));
438   loop_invalid = (char *) alloca (max_loop_num * sizeof (char));
439   loop_number_exit_labels = (rtx *) alloca (max_loop_num * sizeof (rtx));
440   loop_number_exit_count = (int *) alloca (max_loop_num * sizeof (int));
441
442 #ifdef HAIFA
443   /* Allocate for BCT optimization */
444   loop_can_insert_bct = (int *) alloca (max_loop_num * sizeof (int));
445   bzero ((char *) loop_can_insert_bct, max_loop_num * sizeof (int));
446
447   loop_used_count_register = (int *) alloca (max_loop_num * sizeof (int));
448   bzero ((char *) loop_used_count_register, max_loop_num * sizeof (int));
449
450   loop_unroll_factor = (int *) alloca (max_loop_num *sizeof (int));
451   bzero ((char *) loop_unroll_factor, max_loop_num * sizeof (int));
452
453   loop_unroll_iter = (int *) alloca (max_loop_num *sizeof (int));
454   bzero ((char *) loop_unroll_iter, max_loop_num * sizeof (int));
455
456   loop_increment = (rtx *) alloca (max_loop_num * sizeof (rtx));
457   loop_comparison_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
458   loop_start_value = (rtx *) alloca (max_loop_num * sizeof (rtx));
459   bzero ((char *) loop_increment, max_loop_num * sizeof (rtx));
460   bzero ((char *) loop_comparison_value, max_loop_num * sizeof (rtx));
461   bzero ((char *) loop_start_value, max_loop_num * sizeof (rtx));
462
463   loop_comparison_code 
464     = (enum rtx_code *) alloca (max_loop_num * sizeof (enum rtx_code));
465   bzero ((char *) loop_comparison_code, max_loop_num * sizeof (enum rtx_code));
466 #endif  /* HAIFA */
467
468   /* Find and process each loop.
469      First, find them, and record them in order of their beginnings.  */
470   find_and_verify_loops (f);
471
472   /* Now find all register lifetimes.  This must be done after
473      find_and_verify_loops, because it might reorder the insns in the
474      function.  */
475   reg_scan (f, max_reg_num (), 1);
476
477   /* See if we went too far.  */
478   if (get_max_uid () > max_uid_for_loop)
479     abort ();
480
481   /* Compute the mapping from uids to luids.
482      LUIDs are numbers assigned to insns, like uids,
483      except that luids increase monotonically through the code.
484      Don't assign luids to line-number NOTEs, so that the distance in luids
485      between two insns is not affected by -g.  */
486
487   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
488     {
489       last_insn = insn;
490       if (GET_CODE (insn) != NOTE
491           || NOTE_LINE_NUMBER (insn) <= 0)
492         uid_luid[INSN_UID (insn)] = ++i;
493       else
494         /* Give a line number note the same luid as preceding insn.  */
495         uid_luid[INSN_UID (insn)] = i;
496     }
497
498   max_luid = i + 1;
499
500   /* Don't leave gaps in uid_luid for insns that have been
501      deleted.  It is possible that the first or last insn
502      using some register has been deleted by cross-jumping.
503      Make sure that uid_luid for that former insn's uid
504      points to the general area where that insn used to be.  */
505   for (i = 0; i < max_uid_for_loop; i++)
506     {
507       uid_luid[0] = uid_luid[i];
508       if (uid_luid[0] != 0)
509         break;
510     }
511   for (i = 0; i < max_uid_for_loop; i++)
512     if (uid_luid[i] == 0)
513       uid_luid[i] = uid_luid[i - 1];
514
515   /* Create a mapping from loops to BLOCK tree nodes.  */
516   if (flag_unroll_loops && write_symbols != NO_DEBUG)
517     find_loop_tree_blocks ();
518
519 #ifdef HAIFA
520   /* determine if the function has indirect jump. If it does,
521      we cannot instrument loops in this function with bct */
522   indirect_jump_in_function = indirect_jump_in_function_p (f);
523 #endif  /* HAIFA */
524
525   /* Now scan the loops, last ones first, since this means inner ones are done
526      before outer ones.  */
527   for (i = max_loop_num-1; i >= 0; i--)
528     if (! loop_invalid[i] && loop_number_loop_ends[i])
529       scan_loop (loop_number_loop_starts[i], loop_number_loop_ends[i],
530                  max_reg_num ());
531
532   /* If debugging and unrolling loops, we must replicate the tree nodes
533      corresponding to the blocks inside the loop, so that the original one
534      to one mapping will remain.  */
535   if (flag_unroll_loops && write_symbols != NO_DEBUG)
536     unroll_block_trees ();
537 }
538 \f
539 /* Optimize one loop whose start is LOOP_START and end is END.
540    LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
541    NOTE_INSN_LOOP_END.  */
542
543 /* ??? Could also move memory writes out of loops if the destination address
544    is invariant, the source is invariant, the memory write is not volatile,
545    and if we can prove that no read inside the loop can read this address
546    before the write occurs.  If there is a read of this address after the
547    write, then we can also mark the memory read as invariant.  */
548
549 static void
550 scan_loop (loop_start, end, nregs)
551      rtx loop_start, end;
552      int nregs;
553 {
554   register int i;
555   register rtx p;
556   /* 1 if we are scanning insns that could be executed zero times.  */
557   int maybe_never = 0;
558   /* 1 if we are scanning insns that might never be executed
559      due to a subroutine call which might exit before they are reached.  */
560   int call_passed = 0;
561   /* For a rotated loop that is entered near the bottom,
562      this is the label at the top.  Otherwise it is zero.  */
563   rtx loop_top = 0;
564   /* Jump insn that enters the loop, or 0 if control drops in.  */
565   rtx loop_entry_jump = 0;
566   /* Place in the loop where control enters.  */
567   rtx scan_start;
568   /* Number of insns in the loop.  */
569   int insn_count;
570   int in_libcall = 0;
571   int tem;
572   rtx temp;
573   /* The SET from an insn, if it is the only SET in the insn.  */
574   rtx set, set1;
575   /* Chain describing insns movable in current loop.  */
576   struct movable *movables = 0;
577   /* Last element in `movables' -- so we can add elements at the end.  */
578   struct movable *last_movable = 0;
579   /* Ratio of extra register life span we can justify
580      for saving an instruction.  More if loop doesn't call subroutines
581      since in that case saving an insn makes more difference
582      and more registers are available.  */
583   int threshold;
584   /* If we have calls, contains the insn in which a register was used
585      if it was used exactly once; contains const0_rtx if it was used more
586      than once.  */
587   rtx *reg_single_usage = 0;
588   /* Nonzero if we are scanning instructions in a sub-loop.  */
589   int loop_depth = 0;
590
591   n_times_set = (int *) alloca (nregs * sizeof (int));
592   n_times_used = (int *) alloca (nregs * sizeof (int));
593   may_not_optimize = (char *) alloca (nregs);
594
595   /* Determine whether this loop starts with a jump down to a test at
596      the end.  This will occur for a small number of loops with a test
597      that is too complex to duplicate in front of the loop.
598
599      We search for the first insn or label in the loop, skipping NOTEs.
600      However, we must be careful not to skip past a NOTE_INSN_LOOP_BEG
601      (because we might have a loop executed only once that contains a
602      loop which starts with a jump to its exit test) or a NOTE_INSN_LOOP_END
603      (in case we have a degenerate loop).
604
605      Note that if we mistakenly think that a loop is entered at the top
606      when, in fact, it is entered at the exit test, the only effect will be
607      slightly poorer optimization.  Making the opposite error can generate
608      incorrect code.  Since very few loops now start with a jump to the 
609      exit test, the code here to detect that case is very conservative.  */
610
611   for (p = NEXT_INSN (loop_start);
612        p != end
613          && GET_CODE (p) != CODE_LABEL && GET_RTX_CLASS (GET_CODE (p)) != 'i'
614          && (GET_CODE (p) != NOTE
615              || (NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_BEG
616                  && NOTE_LINE_NUMBER (p) != NOTE_INSN_LOOP_END));
617        p = NEXT_INSN (p))
618     ;
619
620   scan_start = p;
621
622   /* Set up variables describing this loop.  */
623   prescan_loop (loop_start, end);
624   threshold = (loop_has_call ? 1 : 2) * (1 + n_non_fixed_regs);
625
626   /* If loop has a jump before the first label,
627      the true entry is the target of that jump.
628      Start scan from there.
629      But record in LOOP_TOP the place where the end-test jumps
630      back to so we can scan that after the end of the loop.  */
631   if (GET_CODE (p) == JUMP_INSN)
632     {
633       loop_entry_jump = p;
634
635       /* Loop entry must be unconditional jump (and not a RETURN)  */
636       if (simplejump_p (p)
637           && JUMP_LABEL (p) != 0
638           /* Check to see whether the jump actually
639              jumps out of the loop (meaning it's no loop).
640              This case can happen for things like
641              do {..} while (0).  If this label was generated previously
642              by loop, we can't tell anything about it and have to reject
643              the loop.  */
644           && INSN_UID (JUMP_LABEL (p)) < max_uid_for_loop
645           && INSN_LUID (JUMP_LABEL (p)) >= INSN_LUID (loop_start)
646           && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (end))
647         {
648           loop_top = next_label (scan_start);
649           scan_start = JUMP_LABEL (p);
650         }
651     }
652
653   /* If SCAN_START was an insn created by loop, we don't know its luid
654      as required by loop_reg_used_before_p.  So skip such loops.  (This
655      test may never be true, but it's best to play it safe.) 
656
657      Also, skip loops where we do not start scanning at a label.  This
658      test also rejects loops starting with a JUMP_INSN that failed the
659      test above.  */
660
661   if (INSN_UID (scan_start) >= max_uid_for_loop
662       || GET_CODE (scan_start) != CODE_LABEL)
663     {
664       if (loop_dump_stream)
665         fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
666                  INSN_UID (loop_start), INSN_UID (end));
667       return;
668     }
669
670   /* Count number of times each reg is set during this loop.
671      Set may_not_optimize[I] if it is not safe to move out
672      the setting of register I.  If this loop has calls, set
673      reg_single_usage[I].  */
674
675   bzero ((char *) n_times_set, nregs * sizeof (int));
676   bzero (may_not_optimize, nregs);
677
678   if (loop_has_call)
679     {
680       reg_single_usage = (rtx *) alloca (nregs * sizeof (rtx));
681       bzero ((char *) reg_single_usage, nregs * sizeof (rtx));
682     }
683
684   count_loop_regs_set (loop_top ? loop_top : loop_start, end,
685                        may_not_optimize, reg_single_usage, &insn_count, nregs);
686
687   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
688     may_not_optimize[i] = 1, n_times_set[i] = 1;
689   bcopy ((char *) n_times_set, (char *) n_times_used, nregs * sizeof (int));
690
691   if (loop_dump_stream)
692     {
693       fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
694                INSN_UID (loop_start), INSN_UID (end), insn_count);
695       if (loop_continue)
696         fprintf (loop_dump_stream, "Continue at insn %d.\n",
697                  INSN_UID (loop_continue));
698     }
699
700   /* Scan through the loop finding insns that are safe to move.
701      Set n_times_set negative for the reg being set, so that
702      this reg will be considered invariant for subsequent insns.
703      We consider whether subsequent insns use the reg
704      in deciding whether it is worth actually moving.
705
706      MAYBE_NEVER is nonzero if we have passed a conditional jump insn
707      and therefore it is possible that the insns we are scanning
708      would never be executed.  At such times, we must make sure
709      that it is safe to execute the insn once instead of zero times.
710      When MAYBE_NEVER is 0, all insns will be executed at least once
711      so that is not a problem.  */
712
713   p = scan_start;
714   while (1)
715     {
716       p = NEXT_INSN (p);
717       /* At end of a straight-in loop, we are done.
718          At end of a loop entered at the bottom, scan the top.  */
719       if (p == scan_start)
720         break;
721       if (p == end)
722         {
723           if (loop_top != 0)
724             p = loop_top;
725           else
726             break;
727           if (p == scan_start)
728             break;
729         }
730
731       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
732           && find_reg_note (p, REG_LIBCALL, NULL_RTX))
733         in_libcall = 1;
734       else if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
735                && find_reg_note (p, REG_RETVAL, NULL_RTX))
736         in_libcall = 0;
737
738       if (GET_CODE (p) == INSN
739           && (set = single_set (p))
740           && GET_CODE (SET_DEST (set)) == REG
741           && ! may_not_optimize[REGNO (SET_DEST (set))])
742         {
743           int tem1 = 0;
744           int tem2 = 0;
745           int move_insn = 0;
746           rtx src = SET_SRC (set);
747           rtx dependencies = 0;
748
749           /* Figure out what to use as a source of this insn.  If a REG_EQUIV
750              note is given or if a REG_EQUAL note with a constant operand is
751              specified, use it as the source and mark that we should move
752              this insn by calling emit_move_insn rather that duplicating the
753              insn.
754
755              Otherwise, only use the REG_EQUAL contents if a REG_RETVAL note
756              is present.  */
757           temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
758           if (temp)
759             src = XEXP (temp, 0), move_insn = 1;
760           else 
761             {
762               temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
763               if (temp && CONSTANT_P (XEXP (temp, 0)))
764                 src = XEXP (temp, 0), move_insn = 1;
765               if (temp && find_reg_note (p, REG_RETVAL, NULL_RTX))
766                 {
767                   src = XEXP (temp, 0);
768                   /* A libcall block can use regs that don't appear in
769                      the equivalent expression.  To move the libcall,
770                      we must move those regs too.  */
771                   dependencies = libcall_other_reg (p, src);
772                 }
773             }
774
775           /* Don't try to optimize a register that was made
776              by loop-optimization for an inner loop.
777              We don't know its life-span, so we can't compute the benefit.  */
778           if (REGNO (SET_DEST (set)) >= max_reg_before_loop)
779             ;
780           /* In order to move a register, we need to have one of three cases:
781              (1) it is used only in the same basic block as the set
782              (2) it is not a user variable and it is not used in the
783                  exit test (this can cause the variable to be used
784                  before it is set just like a user-variable).
785              (3) the set is guaranteed to be executed once the loop starts,
786                  and the reg is not used until after that.  */
787           else if (! ((! maybe_never
788                        && ! loop_reg_used_before_p (set, p, loop_start,
789                                                     scan_start, end))
790                       || (! REG_USERVAR_P (SET_DEST (set))
791                           && ! REG_LOOP_TEST_P (SET_DEST (set)))
792                       || reg_in_basic_block_p (p, SET_DEST (set))))
793             ;
794           else if ((tem = invariant_p (src))
795                    && (dependencies == 0
796                        || (tem2 = invariant_p (dependencies)) != 0)
797                    && (n_times_set[REGNO (SET_DEST (set))] == 1
798                        || (tem1
799                            = consec_sets_invariant_p (SET_DEST (set),
800                                                       n_times_set[REGNO (SET_DEST (set))],
801                                                       p)))
802                    /* If the insn can cause a trap (such as divide by zero),
803                       can't move it unless it's guaranteed to be executed
804                       once loop is entered.  Even a function call might
805                       prevent the trap insn from being reached
806                       (since it might exit!)  */
807                    && ! ((maybe_never || call_passed)
808                          && may_trap_p (src)))
809             {
810               register struct movable *m;
811               register int regno = REGNO (SET_DEST (set));
812
813               /* A potential lossage is where we have a case where two insns
814                  can be combined as long as they are both in the loop, but
815                  we move one of them outside the loop.  For large loops,
816                  this can lose.  The most common case of this is the address
817                  of a function being called.  
818
819                  Therefore, if this register is marked as being used exactly
820                  once if we are in a loop with calls (a "large loop"), see if
821                  we can replace the usage of this register with the source
822                  of this SET.  If we can, delete this insn. 
823
824                  Don't do this if P has a REG_RETVAL note or if we have
825                  SMALL_REGISTER_CLASSES and SET_SRC is a hard register.  */
826
827               if (reg_single_usage && reg_single_usage[regno] != 0
828                   && reg_single_usage[regno] != const0_rtx
829                   && REGNO_FIRST_UID (regno) == INSN_UID (p)
830                   && (REGNO_LAST_UID (regno)
831                       == INSN_UID (reg_single_usage[regno]))
832                   && n_times_set[REGNO (SET_DEST (set))] == 1
833                   && ! side_effects_p (SET_SRC (set))
834                   && ! find_reg_note (p, REG_RETVAL, NULL_RTX)
835 #ifdef SMALL_REGISTER_CLASSES
836                   && ! (SMALL_REGISTER_CLASSES
837                         && GET_CODE (SET_SRC (set)) == REG
838                         && REGNO (SET_SRC (set)) < FIRST_PSEUDO_REGISTER)
839 #endif
840                   /* This test is not redundant; SET_SRC (set) might be
841                      a call-clobbered register and the life of REGNO
842                      might span a call.  */
843                   && ! modified_between_p (SET_SRC (set), p,
844                                            reg_single_usage[regno])
845                   && no_labels_between_p (p, reg_single_usage[regno])
846                   && validate_replace_rtx (SET_DEST (set), SET_SRC (set),
847                                            reg_single_usage[regno]))
848                 {
849                   /* Replace any usage in a REG_EQUAL note.  Must copy the
850                      new source, so that we don't get rtx sharing between the
851                      SET_SOURCE and REG_NOTES of insn p.  */
852                   REG_NOTES (reg_single_usage[regno])
853                     = replace_rtx (REG_NOTES (reg_single_usage[regno]),
854                                    SET_DEST (set), copy_rtx (SET_SRC (set)));
855                                    
856                   PUT_CODE (p, NOTE);
857                   NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
858                   NOTE_SOURCE_FILE (p) = 0;
859                   n_times_set[regno] = 0;
860                   continue;
861                 }
862
863               m = (struct movable *) alloca (sizeof (struct movable));
864               m->next = 0;
865               m->insn = p;
866               m->set_src = src;
867               m->dependencies = dependencies;
868               m->set_dest = SET_DEST (set);
869               m->force = 0;
870               m->consec = n_times_set[REGNO (SET_DEST (set))] - 1;
871               m->done = 0;
872               m->forces = 0;
873               m->partial = 0;
874               m->move_insn = move_insn;
875               m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
876               m->savemode = VOIDmode;
877               m->regno = regno;
878               /* Set M->cond if either invariant_p or consec_sets_invariant_p
879                  returned 2 (only conditionally invariant).  */
880               m->cond = ((tem | tem1 | tem2) > 1);
881               m->global = (uid_luid[REGNO_LAST_UID (regno)] > INSN_LUID (end)
882                            || uid_luid[REGNO_FIRST_UID (regno)] < INSN_LUID (loop_start));
883               m->match = 0;
884               m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
885                              - uid_luid[REGNO_FIRST_UID (regno)]);
886               m->savings = n_times_used[regno];
887               if (find_reg_note (p, REG_RETVAL, NULL_RTX))
888                 m->savings += libcall_benefit (p);
889               n_times_set[regno] = move_insn ? -2 : -1;
890               /* Add M to the end of the chain MOVABLES.  */
891               if (movables == 0)
892                 movables = m;
893               else
894                 last_movable->next = m;
895               last_movable = m;
896
897               if (m->consec > 0)
898                 {
899                   /* Skip this insn, not checking REG_LIBCALL notes.  */
900                   p = next_nonnote_insn (p);
901                   /* Skip the consecutive insns, if there are any.  */
902                   p = skip_consec_insns (p, m->consec);
903                   /* Back up to the last insn of the consecutive group.  */
904                   p = prev_nonnote_insn (p);
905
906                   /* We must now reset m->move_insn, m->is_equiv, and possibly
907                      m->set_src to correspond to the effects of all the
908                      insns.  */
909                   temp = find_reg_note (p, REG_EQUIV, NULL_RTX);
910                   if (temp)
911                     m->set_src = XEXP (temp, 0), m->move_insn = 1;
912                   else
913                     {
914                       temp = find_reg_note (p, REG_EQUAL, NULL_RTX);
915                       if (temp && CONSTANT_P (XEXP (temp, 0)))
916                         m->set_src = XEXP (temp, 0), m->move_insn = 1;
917                       else
918                         m->move_insn = 0;
919
920                     }
921                   m->is_equiv = (find_reg_note (p, REG_EQUIV, NULL_RTX) != 0);
922                 }
923             }
924           /* If this register is always set within a STRICT_LOW_PART
925              or set to zero, then its high bytes are constant.
926              So clear them outside the loop and within the loop
927              just load the low bytes.
928              We must check that the machine has an instruction to do so.
929              Also, if the value loaded into the register
930              depends on the same register, this cannot be done.  */
931           else if (SET_SRC (set) == const0_rtx
932                    && GET_CODE (NEXT_INSN (p)) == INSN
933                    && (set1 = single_set (NEXT_INSN (p)))
934                    && GET_CODE (set1) == SET
935                    && (GET_CODE (SET_DEST (set1)) == STRICT_LOW_PART)
936                    && (GET_CODE (XEXP (SET_DEST (set1), 0)) == SUBREG)
937                    && (SUBREG_REG (XEXP (SET_DEST (set1), 0))
938                        == SET_DEST (set))
939                    && !reg_mentioned_p (SET_DEST (set), SET_SRC (set1)))
940             {
941               register int regno = REGNO (SET_DEST (set));
942               if (n_times_set[regno] == 2)
943                 {
944                   register struct movable *m;
945                   m = (struct movable *) alloca (sizeof (struct movable));
946                   m->next = 0;
947                   m->insn = p;
948                   m->set_dest = SET_DEST (set);
949                   m->dependencies = 0;
950                   m->force = 0;
951                   m->consec = 0;
952                   m->done = 0;
953                   m->forces = 0;
954                   m->move_insn = 0;
955                   m->partial = 1;
956                   /* If the insn may not be executed on some cycles,
957                      we can't clear the whole reg; clear just high part.
958                      Not even if the reg is used only within this loop.
959                      Consider this:
960                      while (1)
961                        while (s != t) {
962                          if (foo ()) x = *s;
963                          use (x);
964                        }
965                      Clearing x before the inner loop could clobber a value
966                      being saved from the last time around the outer loop.
967                      However, if the reg is not used outside this loop
968                      and all uses of the register are in the same
969                      basic block as the store, there is no problem.
970
971                      If this insn was made by loop, we don't know its
972                      INSN_LUID and hence must make a conservative
973                      assumption.  */
974                   m->global = (INSN_UID (p) >= max_uid_for_loop
975                                || (uid_luid[REGNO_LAST_UID (regno)]
976                                    > INSN_LUID (end))
977                                || (uid_luid[REGNO_FIRST_UID (regno)]
978                                    < INSN_LUID (p))
979                                || (labels_in_range_p
980                                    (p, uid_luid[REGNO_FIRST_UID (regno)])));
981                   if (maybe_never && m->global)
982                     m->savemode = GET_MODE (SET_SRC (set1));
983                   else
984                     m->savemode = VOIDmode;
985                   m->regno = regno;
986                   m->cond = 0;
987                   m->match = 0;
988                   m->lifetime = (uid_luid[REGNO_LAST_UID (regno)]
989                                  - uid_luid[REGNO_FIRST_UID (regno)]);
990                   m->savings = 1;
991                   n_times_set[regno] = -1;
992                   /* Add M to the end of the chain MOVABLES.  */
993                   if (movables == 0)
994                     movables = m;
995                   else
996                     last_movable->next = m;
997                   last_movable = m;
998                 }
999             }
1000         }
1001       /* Past a call insn, we get to insns which might not be executed
1002          because the call might exit.  This matters for insns that trap.
1003          Call insns inside a REG_LIBCALL/REG_RETVAL block always return,
1004          so they don't count.  */
1005       else if (GET_CODE (p) == CALL_INSN && ! in_libcall)
1006         call_passed = 1;
1007       /* Past a label or a jump, we get to insns for which we
1008          can't count on whether or how many times they will be
1009          executed during each iteration.  Therefore, we can
1010          only move out sets of trivial variables
1011          (those not used after the loop).  */
1012       /* Similar code appears twice in strength_reduce.  */
1013       else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
1014                /* If we enter the loop in the middle, and scan around to the
1015                   beginning, don't set maybe_never for that.  This must be an
1016                   unconditional jump, otherwise the code at the top of the
1017                   loop might never be executed.  Unconditional jumps are
1018                   followed a by barrier then loop end.  */
1019                && ! (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p) == loop_top
1020                      && NEXT_INSN (NEXT_INSN (p)) == end
1021                      && simplejump_p (p)))
1022         maybe_never = 1;
1023       else if (GET_CODE (p) == NOTE)
1024         {
1025           /* At the virtual top of a converted loop, insns are again known to
1026              be executed: logically, the loop begins here even though the exit
1027              code has been duplicated.  */
1028           if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
1029             maybe_never = call_passed = 0;
1030           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
1031             loop_depth++;
1032           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
1033             loop_depth--;
1034         }
1035     }
1036
1037   /* If one movable subsumes another, ignore that other.  */
1038
1039   ignore_some_movables (movables);
1040
1041   /* For each movable insn, see if the reg that it loads
1042      leads when it dies right into another conditionally movable insn.
1043      If so, record that the second insn "forces" the first one,
1044      since the second can be moved only if the first is.  */
1045
1046   force_movables (movables);
1047
1048   /* See if there are multiple movable insns that load the same value.
1049      If there are, make all but the first point at the first one
1050      through the `match' field, and add the priorities of them
1051      all together as the priority of the first.  */
1052
1053   combine_movables (movables, nregs);
1054         
1055   /* Now consider each movable insn to decide whether it is worth moving.
1056      Store 0 in n_times_set for each reg that is moved.  */
1057
1058   move_movables (movables, threshold,
1059                  insn_count, loop_start, end, nregs);
1060
1061   /* Now candidates that still are negative are those not moved.
1062      Change n_times_set to indicate that those are not actually invariant.  */
1063   for (i = 0; i < nregs; i++)
1064     if (n_times_set[i] < 0)
1065       n_times_set[i] = n_times_used[i];
1066
1067   if (flag_strength_reduce)
1068     strength_reduce (scan_start, end, loop_top,
1069                      insn_count, loop_start, end);
1070 }
1071 \f
1072 /* Add elements to *OUTPUT to record all the pseudo-regs
1073    mentioned in IN_THIS but not mentioned in NOT_IN_THIS.  */
1074
1075 void
1076 record_excess_regs (in_this, not_in_this, output)
1077      rtx in_this, not_in_this;
1078      rtx *output;
1079 {
1080   enum rtx_code code;
1081   char *fmt;
1082   int i;
1083
1084   code = GET_CODE (in_this);
1085
1086   switch (code)
1087     {
1088     case PC:
1089     case CC0:
1090     case CONST_INT:
1091     case CONST_DOUBLE:
1092     case CONST:
1093     case SYMBOL_REF:
1094     case LABEL_REF:
1095       return;
1096
1097     case REG:
1098       if (REGNO (in_this) >= FIRST_PSEUDO_REGISTER
1099           && ! reg_mentioned_p (in_this, not_in_this))
1100         *output = gen_rtx (EXPR_LIST, VOIDmode, in_this, *output);
1101       return;
1102     }
1103
1104   fmt = GET_RTX_FORMAT (code);
1105   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1106     {
1107       int j;
1108
1109       switch (fmt[i])
1110         {
1111         case 'E':
1112           for (j = 0; j < XVECLEN (in_this, i); j++)
1113             record_excess_regs (XVECEXP (in_this, i, j), not_in_this, output);
1114           break;
1115
1116         case 'e':
1117           record_excess_regs (XEXP (in_this, i), not_in_this, output);
1118           break;
1119         }
1120     }
1121 }
1122 \f
1123 /* Check what regs are referred to in the libcall block ending with INSN,
1124    aside from those mentioned in the equivalent value.
1125    If there are none, return 0.
1126    If there are one or more, return an EXPR_LIST containing all of them.  */
1127
1128 static rtx
1129 libcall_other_reg (insn, equiv)
1130      rtx insn, equiv;
1131 {
1132   rtx note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
1133   rtx p = XEXP (note, 0);
1134   rtx output = 0;
1135
1136   /* First, find all the regs used in the libcall block
1137      that are not mentioned as inputs to the result.  */
1138
1139   while (p != insn)
1140     {
1141       if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
1142           || GET_CODE (p) == CALL_INSN)
1143         record_excess_regs (PATTERN (p), equiv, &output);
1144       p = NEXT_INSN (p);
1145     }
1146
1147   return output;
1148 }
1149 \f
1150 /* Return 1 if all uses of REG
1151    are between INSN and the end of the basic block.  */
1152
1153 static int 
1154 reg_in_basic_block_p (insn, reg)
1155      rtx insn, reg;
1156 {
1157   int regno = REGNO (reg);
1158   rtx p;
1159
1160   if (REGNO_FIRST_UID (regno) != INSN_UID (insn))
1161     return 0;
1162
1163   /* Search this basic block for the already recorded last use of the reg.  */
1164   for (p = insn; p; p = NEXT_INSN (p))
1165     {
1166       switch (GET_CODE (p))
1167         {
1168         case NOTE:
1169           break;
1170
1171         case INSN:
1172         case CALL_INSN:
1173           /* Ordinary insn: if this is the last use, we win.  */
1174           if (REGNO_LAST_UID (regno) == INSN_UID (p))
1175             return 1;
1176           break;
1177
1178         case JUMP_INSN:
1179           /* Jump insn: if this is the last use, we win.  */
1180           if (REGNO_LAST_UID (regno) == INSN_UID (p))
1181             return 1;
1182           /* Otherwise, it's the end of the basic block, so we lose.  */
1183           return 0;
1184
1185         case CODE_LABEL:
1186         case BARRIER:
1187           /* It's the end of the basic block, so we lose.  */
1188           return 0;
1189         }
1190     }
1191
1192   /* The "last use" doesn't follow the "first use"??  */
1193   abort ();
1194 }
1195 \f
1196 /* Compute the benefit of eliminating the insns in the block whose
1197    last insn is LAST.  This may be a group of insns used to compute a
1198    value directly or can contain a library call.  */
1199
1200 static int
1201 libcall_benefit (last)
1202      rtx last;
1203 {
1204   rtx insn;
1205   int benefit = 0;
1206
1207   for (insn = XEXP (find_reg_note (last, REG_RETVAL, NULL_RTX), 0);
1208        insn != last; insn = NEXT_INSN (insn))
1209     {
1210       if (GET_CODE (insn) == CALL_INSN)
1211         benefit += 10;          /* Assume at least this many insns in a library
1212                                    routine.  */
1213       else if (GET_CODE (insn) == INSN
1214                && GET_CODE (PATTERN (insn)) != USE
1215                && GET_CODE (PATTERN (insn)) != CLOBBER)
1216         benefit++;
1217     }
1218
1219   return benefit;
1220 }
1221 \f
1222 /* Skip COUNT insns from INSN, counting library calls as 1 insn.  */
1223
1224 static rtx
1225 skip_consec_insns (insn, count)
1226      rtx insn;
1227      int count;
1228 {
1229   for (; count > 0; count--)
1230     {
1231       rtx temp;
1232
1233       /* If first insn of libcall sequence, skip to end.  */
1234       /* Do this at start of loop, since INSN is guaranteed to 
1235          be an insn here.  */
1236       if (GET_CODE (insn) != NOTE
1237           && (temp = find_reg_note (insn, REG_LIBCALL, NULL_RTX)))
1238         insn = XEXP (temp, 0);
1239
1240       do insn = NEXT_INSN (insn);
1241       while (GET_CODE (insn) == NOTE);
1242     }
1243
1244   return insn;
1245 }
1246
1247 /* Ignore any movable whose insn falls within a libcall
1248    which is part of another movable.
1249    We make use of the fact that the movable for the libcall value
1250    was made later and so appears later on the chain.  */
1251
1252 static void
1253 ignore_some_movables (movables)
1254      struct movable *movables;
1255 {
1256   register struct movable *m, *m1;
1257
1258   for (m = movables; m; m = m->next)
1259     {
1260       /* Is this a movable for the value of a libcall?  */
1261       rtx note = find_reg_note (m->insn, REG_RETVAL, NULL_RTX);
1262       if (note)
1263         {
1264           rtx insn;
1265           /* Check for earlier movables inside that range,
1266              and mark them invalid.  We cannot use LUIDs here because
1267              insns created by loop.c for prior loops don't have LUIDs.
1268              Rather than reject all such insns from movables, we just
1269              explicitly check each insn in the libcall (since invariant
1270              libcalls aren't that common).  */
1271           for (insn = XEXP (note, 0); insn != m->insn; insn = NEXT_INSN (insn))
1272             for (m1 = movables; m1 != m; m1 = m1->next)
1273               if (m1->insn == insn)
1274                 m1->done = 1;
1275         }
1276     }
1277 }         
1278
1279 /* For each movable insn, see if the reg that it loads
1280    leads when it dies right into another conditionally movable insn.
1281    If so, record that the second insn "forces" the first one,
1282    since the second can be moved only if the first is.  */
1283
1284 static void
1285 force_movables (movables)
1286      struct movable *movables;
1287 {
1288   register struct movable *m, *m1;
1289   for (m1 = movables; m1; m1 = m1->next)
1290     /* Omit this if moving just the (SET (REG) 0) of a zero-extend.  */
1291     if (!m1->partial && !m1->done)
1292       {
1293         int regno = m1->regno;
1294         for (m = m1->next; m; m = m->next)
1295           /* ??? Could this be a bug?  What if CSE caused the
1296              register of M1 to be used after this insn?
1297              Since CSE does not update regno_last_uid,
1298              this insn M->insn might not be where it dies.
1299              But very likely this doesn't matter; what matters is
1300              that M's reg is computed from M1's reg.  */
1301           if (INSN_UID (m->insn) == REGNO_LAST_UID (regno)
1302               && !m->done)
1303             break;
1304         if (m != 0 && m->set_src == m1->set_dest
1305             /* If m->consec, m->set_src isn't valid.  */
1306             && m->consec == 0)
1307           m = 0;
1308
1309         /* Increase the priority of the moving the first insn
1310            since it permits the second to be moved as well.  */
1311         if (m != 0)
1312           {
1313             m->forces = m1;
1314             m1->lifetime += m->lifetime;
1315             m1->savings += m1->savings;
1316           }
1317       }
1318 }
1319 \f
1320 /* Find invariant expressions that are equal and can be combined into
1321    one register.  */
1322
1323 static void
1324 combine_movables (movables, nregs)
1325      struct movable *movables;
1326      int nregs;
1327 {
1328   register struct movable *m;
1329   char *matched_regs = (char *) alloca (nregs);
1330   enum machine_mode mode;
1331
1332   /* Regs that are set more than once are not allowed to match
1333      or be matched.  I'm no longer sure why not.  */
1334   /* Perhaps testing m->consec_sets would be more appropriate here?  */
1335
1336   for (m = movables; m; m = m->next)
1337     if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
1338       {
1339         register struct movable *m1;
1340         int regno = m->regno;
1341
1342         bzero (matched_regs, nregs);
1343         matched_regs[regno] = 1;
1344
1345         for (m1 = movables; m1; m1 = m1->next)
1346           if (m != m1 && m1->match == 0 && n_times_used[m1->regno] == 1
1347               /* A reg used outside the loop mustn't be eliminated.  */
1348               && !m1->global
1349               /* A reg used for zero-extending mustn't be eliminated.  */
1350               && !m1->partial
1351               && (matched_regs[m1->regno]
1352                   ||
1353                   (
1354                    /* Can combine regs with different modes loaded from the
1355                       same constant only if the modes are the same or
1356                       if both are integer modes with M wider or the same
1357                       width as M1.  The check for integer is redundant, but
1358                       safe, since the only case of differing destination
1359                       modes with equal sources is when both sources are
1360                       VOIDmode, i.e., CONST_INT.  */
1361                    (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest)
1362                     || (GET_MODE_CLASS (GET_MODE (m->set_dest)) == MODE_INT
1363                         && GET_MODE_CLASS (GET_MODE (m1->set_dest)) == MODE_INT
1364                         && (GET_MODE_BITSIZE (GET_MODE (m->set_dest))
1365                             >= GET_MODE_BITSIZE (GET_MODE (m1->set_dest)))))
1366                    /* See if the source of M1 says it matches M.  */
1367                    && ((GET_CODE (m1->set_src) == REG
1368                         && matched_regs[REGNO (m1->set_src)])
1369                        || rtx_equal_for_loop_p (m->set_src, m1->set_src,
1370                                                 movables))))
1371               && ((m->dependencies == m1->dependencies)
1372                   || rtx_equal_p (m->dependencies, m1->dependencies)))
1373             {
1374               m->lifetime += m1->lifetime;
1375               m->savings += m1->savings;
1376               m1->done = 1;
1377               m1->match = m;
1378               matched_regs[m1->regno] = 1;
1379             }
1380       }
1381
1382   /* Now combine the regs used for zero-extension.
1383      This can be done for those not marked `global'
1384      provided their lives don't overlap.  */
1385
1386   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1387        mode = GET_MODE_WIDER_MODE (mode))
1388     {
1389       register struct movable *m0 = 0;
1390
1391       /* Combine all the registers for extension from mode MODE.
1392          Don't combine any that are used outside this loop.  */
1393       for (m = movables; m; m = m->next)
1394         if (m->partial && ! m->global
1395             && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
1396           {
1397             register struct movable *m1;
1398             int first = uid_luid[REGNO_FIRST_UID (m->regno)];
1399             int last = uid_luid[REGNO_LAST_UID (m->regno)];
1400
1401             if (m0 == 0)
1402               {
1403                 /* First one: don't check for overlap, just record it.  */
1404                 m0 = m;
1405                   continue;
1406               }
1407
1408             /* Make sure they extend to the same mode.
1409                (Almost always true.)  */
1410             if (GET_MODE (m->set_dest) != GET_MODE (m0->set_dest))
1411                 continue;
1412
1413             /* We already have one: check for overlap with those
1414                already combined together.  */
1415             for (m1 = movables; m1 != m; m1 = m1->next)
1416               if (m1 == m0 || (m1->partial && m1->match == m0))
1417                 if (! (uid_luid[REGNO_FIRST_UID (m1->regno)] > last
1418                        || uid_luid[REGNO_LAST_UID (m1->regno)] < first))
1419                   goto overlap;
1420
1421             /* No overlap: we can combine this with the others.  */
1422             m0->lifetime += m->lifetime;
1423             m0->savings += m->savings;
1424             m->done = 1;
1425             m->match = m0;
1426
1427           overlap: ;
1428           }
1429     }
1430 }
1431 \f
1432 /* Return 1 if regs X and Y will become the same if moved.  */
1433
1434 static int
1435 regs_match_p (x, y, movables)
1436      rtx x, y;
1437      struct movable *movables;
1438 {
1439   int xn = REGNO (x);
1440   int yn = REGNO (y);
1441   struct movable *mx, *my;
1442
1443   for (mx = movables; mx; mx = mx->next)
1444     if (mx->regno == xn)
1445       break;
1446
1447   for (my = movables; my; my = my->next)
1448     if (my->regno == yn)
1449       break;
1450
1451   return (mx && my
1452           && ((mx->match == my->match && mx->match != 0)
1453               || mx->match == my
1454               || mx == my->match));
1455 }
1456
1457 /* Return 1 if X and Y are identical-looking rtx's.
1458    This is the Lisp function EQUAL for rtx arguments.
1459
1460    If two registers are matching movables or a movable register and an
1461    equivalent constant, consider them equal.  */
1462
1463 static int
1464 rtx_equal_for_loop_p (x, y, movables)
1465      rtx x, y;
1466      struct movable *movables;
1467 {
1468   register int i;
1469   register int j;
1470   register struct movable *m;
1471   register enum rtx_code code;
1472   register char *fmt;
1473
1474   if (x == y)
1475     return 1;
1476   if (x == 0 || y == 0)
1477     return 0;
1478
1479   code = GET_CODE (x);
1480
1481   /* If we have a register and a constant, they may sometimes be
1482      equal.  */
1483   if (GET_CODE (x) == REG && n_times_set[REGNO (x)] == -2
1484       && CONSTANT_P (y))
1485     for (m = movables; m; m = m->next)
1486       if (m->move_insn && m->regno == REGNO (x)
1487           && rtx_equal_p (m->set_src, y))
1488         return 1;
1489
1490   else if (GET_CODE (y) == REG && n_times_set[REGNO (y)] == -2
1491            && CONSTANT_P (x))
1492     for (m = movables; m; m = m->next)
1493       if (m->move_insn && m->regno == REGNO (y)
1494           && rtx_equal_p (m->set_src, x))
1495         return 1;
1496
1497   /* Otherwise, rtx's of different codes cannot be equal.  */
1498   if (code != GET_CODE (y))
1499     return 0;
1500
1501   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1502      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
1503
1504   if (GET_MODE (x) != GET_MODE (y))
1505     return 0;
1506
1507   /* These three types of rtx's can be compared nonrecursively.  */
1508   if (code == REG)
1509     return (REGNO (x) == REGNO (y) || regs_match_p (x, y, movables));
1510
1511   if (code == LABEL_REF)
1512     return XEXP (x, 0) == XEXP (y, 0);
1513   if (code == SYMBOL_REF)
1514     return XSTR (x, 0) == XSTR (y, 0);
1515
1516   /* Compare the elements.  If any pair of corresponding elements
1517      fail to match, return 0 for the whole things.  */
1518
1519   fmt = GET_RTX_FORMAT (code);
1520   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1521     {
1522       switch (fmt[i])
1523         {
1524         case 'w':
1525           if (XWINT (x, i) != XWINT (y, i))
1526             return 0;
1527           break;
1528
1529         case 'i':
1530           if (XINT (x, i) != XINT (y, i))
1531             return 0;
1532           break;
1533
1534         case 'E':
1535           /* Two vectors must have the same length.  */
1536           if (XVECLEN (x, i) != XVECLEN (y, i))
1537             return 0;
1538
1539           /* And the corresponding elements must match.  */
1540           for (j = 0; j < XVECLEN (x, i); j++)
1541             if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
1542               return 0;
1543           break;
1544
1545         case 'e':
1546           if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
1547             return 0;
1548           break;
1549
1550         case 's':
1551           if (strcmp (XSTR (x, i), XSTR (y, i)))
1552             return 0;
1553           break;
1554
1555         case 'u':
1556           /* These are just backpointers, so they don't matter.  */
1557           break;
1558
1559         case '0':
1560           break;
1561
1562           /* It is believed that rtx's at this level will never
1563              contain anything but integers and other rtx's,
1564              except for within LABEL_REFs and SYMBOL_REFs.  */
1565         default:
1566           abort ();
1567         }
1568     }
1569   return 1;
1570 }
1571 \f
1572 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to all
1573   insns in INSNS which use thet reference.  */
1574
1575 static void
1576 add_label_notes (x, insns)
1577      rtx x;
1578      rtx insns;
1579 {
1580   enum rtx_code code = GET_CODE (x);
1581   int i, j;
1582   char *fmt;
1583   rtx insn;
1584
1585   if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
1586     {
1587       rtx next = next_real_insn (XEXP (x, 0));
1588
1589       /* Don't record labels that refer to dispatch tables.
1590          This is not necessary, since the tablejump references the same label.
1591          And if we did record them, flow.c would make worse code.  */
1592       if (next == 0
1593           || ! (GET_CODE (next) == JUMP_INSN
1594                 && (GET_CODE (PATTERN (next)) == ADDR_VEC
1595                     || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC)))
1596         {
1597           for (insn = insns; insn; insn = NEXT_INSN (insn))
1598             if (reg_mentioned_p (XEXP (x, 0), insn))
1599               REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, XEXP (x, 0),
1600                                           REG_NOTES (insn));
1601         }
1602       return;
1603     }
1604
1605   fmt = GET_RTX_FORMAT (code);
1606   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1607     {
1608       if (fmt[i] == 'e')
1609         add_label_notes (XEXP (x, i), insns);
1610       else if (fmt[i] == 'E')
1611         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1612           add_label_notes (XVECEXP (x, i, j), insns);
1613     }
1614 }
1615 \f
1616 /* Scan MOVABLES, and move the insns that deserve to be moved.
1617    If two matching movables are combined, replace one reg with the
1618    other throughout.  */
1619
1620 static void
1621 move_movables (movables, threshold, insn_count, loop_start, end, nregs)
1622      struct movable *movables;
1623      int threshold;
1624      int insn_count;
1625      rtx loop_start;
1626      rtx end;
1627      int nregs;
1628 {
1629   rtx new_start = 0;
1630   register struct movable *m;
1631   register rtx p;
1632   /* Map of pseudo-register replacements to handle combining
1633      when we move several insns that load the same value
1634      into different pseudo-registers.  */
1635   rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
1636   char *already_moved = (char *) alloca (nregs);
1637
1638   bzero (already_moved, nregs);
1639   bzero ((char *) reg_map, nregs * sizeof (rtx));
1640
1641   num_movables = 0;
1642
1643   for (m = movables; m; m = m->next)
1644     {
1645       /* Describe this movable insn.  */
1646
1647       if (loop_dump_stream)
1648         {
1649           fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
1650                    INSN_UID (m->insn), m->regno, m->lifetime);
1651           if (m->consec > 0)
1652             fprintf (loop_dump_stream, "consec %d, ", m->consec);
1653           if (m->cond)
1654             fprintf (loop_dump_stream, "cond ");
1655           if (m->force)
1656             fprintf (loop_dump_stream, "force ");
1657           if (m->global)
1658             fprintf (loop_dump_stream, "global ");
1659           if (m->done)
1660             fprintf (loop_dump_stream, "done ");
1661           if (m->move_insn)
1662             fprintf (loop_dump_stream, "move-insn ");
1663           if (m->match)
1664             fprintf (loop_dump_stream, "matches %d ",
1665                      INSN_UID (m->match->insn));
1666           if (m->forces)
1667             fprintf (loop_dump_stream, "forces %d ",
1668                      INSN_UID (m->forces->insn));
1669         }
1670
1671       /* Count movables.  Value used in heuristics in strength_reduce.  */
1672       num_movables++;
1673
1674       /* Ignore the insn if it's already done (it matched something else).
1675          Otherwise, see if it is now safe to move.  */
1676
1677       if (!m->done
1678           && (! m->cond
1679               || (1 == invariant_p (m->set_src)
1680                   && (m->dependencies == 0
1681                       || 1 == invariant_p (m->dependencies))
1682                   && (m->consec == 0
1683                       || 1 == consec_sets_invariant_p (m->set_dest,
1684                                                        m->consec + 1,
1685                                                        m->insn))))
1686           && (! m->forces || m->forces->done))
1687         {
1688           register int regno;
1689           register rtx p;
1690           int savings = m->savings;
1691
1692           /* We have an insn that is safe to move.
1693              Compute its desirability.  */
1694
1695           p = m->insn;
1696           regno = m->regno;
1697
1698           if (loop_dump_stream)
1699             fprintf (loop_dump_stream, "savings %d ", savings);
1700
1701           if (moved_once[regno])
1702             {
1703               insn_count *= 2;
1704
1705               if (loop_dump_stream)
1706                 fprintf (loop_dump_stream, "halved since already moved ");
1707             }
1708
1709           /* An insn MUST be moved if we already moved something else
1710              which is safe only if this one is moved too: that is,
1711              if already_moved[REGNO] is nonzero.  */
1712
1713           /* An insn is desirable to move if the new lifetime of the
1714              register is no more than THRESHOLD times the old lifetime.
1715              If it's not desirable, it means the loop is so big
1716              that moving won't speed things up much,
1717              and it is liable to make register usage worse.  */
1718
1719           /* It is also desirable to move if it can be moved at no
1720              extra cost because something else was already moved.  */
1721
1722           if (already_moved[regno]
1723               || (threshold * savings * m->lifetime) >= insn_count
1724               || (m->forces && m->forces->done
1725                   && n_times_used[m->forces->regno] == 1))
1726             {
1727               int count;
1728               register struct movable *m1;
1729               rtx first;
1730
1731               /* Now move the insns that set the reg.  */
1732
1733               if (m->partial && m->match)
1734                 {
1735                   rtx newpat, i1;
1736                   rtx r1, r2;
1737                   /* Find the end of this chain of matching regs.
1738                      Thus, we load each reg in the chain from that one reg.
1739                      And that reg is loaded with 0 directly,
1740                      since it has ->match == 0.  */
1741                   for (m1 = m; m1->match; m1 = m1->match);
1742                   newpat = gen_move_insn (SET_DEST (PATTERN (m->insn)),
1743                                           SET_DEST (PATTERN (m1->insn)));
1744                   i1 = emit_insn_before (newpat, loop_start);
1745
1746                   /* Mark the moved, invariant reg as being allowed to
1747                      share a hard reg with the other matching invariant.  */
1748                   REG_NOTES (i1) = REG_NOTES (m->insn);
1749                   r1 = SET_DEST (PATTERN (m->insn));
1750                   r2 = SET_DEST (PATTERN (m1->insn));
1751                   regs_may_share = gen_rtx (EXPR_LIST, VOIDmode, r1,
1752                                             gen_rtx (EXPR_LIST, VOIDmode, r2,
1753                                                      regs_may_share));
1754                   delete_insn (m->insn);
1755
1756                   if (new_start == 0)
1757                     new_start = i1;
1758
1759                   if (loop_dump_stream)
1760                     fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1761                 }
1762               /* If we are to re-generate the item being moved with a
1763                  new move insn, first delete what we have and then emit
1764                  the move insn before the loop.  */
1765               else if (m->move_insn)
1766                 {
1767                   rtx i1, temp;
1768
1769                   for (count = m->consec; count >= 0; count--)
1770                     {
1771                       /* If this is the first insn of a library call sequence,
1772                          skip to the end.  */
1773                       if (GET_CODE (p) != NOTE
1774                           && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1775                         p = XEXP (temp, 0);
1776
1777                       /* If this is the last insn of a libcall sequence, then
1778                          delete every insn in the sequence except the last.
1779                          The last insn is handled in the normal manner.  */
1780                       if (GET_CODE (p) != NOTE
1781                           && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1782                         {
1783                           temp = XEXP (temp, 0);
1784                           while (temp != p)
1785                             temp = delete_insn (temp);
1786                         }
1787
1788                       p = delete_insn (p);
1789                       while (p && GET_CODE (p) == NOTE)
1790                         p = NEXT_INSN (p);
1791                     }
1792
1793                   start_sequence ();
1794                   emit_move_insn (m->set_dest, m->set_src);
1795                   temp = get_insns ();
1796                   end_sequence ();
1797
1798                   add_label_notes (m->set_src, temp);
1799
1800                   i1 = emit_insns_before (temp, loop_start);
1801                   if (! find_reg_note (i1, REG_EQUAL, NULL_RTX))
1802                     REG_NOTES (i1)
1803                       = gen_rtx (EXPR_LIST,
1804                                  m->is_equiv ? REG_EQUIV : REG_EQUAL,
1805                                  m->set_src, REG_NOTES (i1));
1806
1807                   if (loop_dump_stream)
1808                     fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
1809
1810                   /* The more regs we move, the less we like moving them.  */
1811                   threshold -= 3;
1812                 }
1813               else
1814                 {
1815                   for (count = m->consec; count >= 0; count--)
1816                     {
1817                       rtx i1, temp;
1818
1819                       /* If first insn of libcall sequence, skip to end.  */
1820                       /* Do this at start of loop, since p is guaranteed to 
1821                          be an insn here.  */
1822                       if (GET_CODE (p) != NOTE
1823                           && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
1824                         p = XEXP (temp, 0);
1825
1826                       /* If last insn of libcall sequence, move all
1827                          insns except the last before the loop.  The last
1828                          insn is handled in the normal manner.  */
1829                       if (GET_CODE (p) != NOTE
1830                           && (temp = find_reg_note (p, REG_RETVAL, NULL_RTX)))
1831                         {
1832                           rtx fn_address = 0;
1833                           rtx fn_reg = 0;
1834                           rtx fn_address_insn = 0;
1835
1836                           first = 0;
1837                           for (temp = XEXP (temp, 0); temp != p;
1838                                temp = NEXT_INSN (temp))
1839                             {
1840                               rtx body;
1841                               rtx n;
1842                               rtx next;
1843
1844                               if (GET_CODE (temp) == NOTE)
1845                                 continue;
1846
1847                               body = PATTERN (temp);
1848
1849                               /* Find the next insn after TEMP,
1850                                  not counting USE or NOTE insns.  */
1851                               for (next = NEXT_INSN (temp); next != p;
1852                                    next = NEXT_INSN (next))
1853                                 if (! (GET_CODE (next) == INSN
1854                                        && GET_CODE (PATTERN (next)) == USE)
1855                                     && GET_CODE (next) != NOTE)
1856                                   break;
1857                               
1858                               /* If that is the call, this may be the insn
1859                                  that loads the function address.
1860
1861                                  Extract the function address from the insn
1862                                  that loads it into a register.
1863                                  If this insn was cse'd, we get incorrect code.
1864
1865                                  So emit a new move insn that copies the
1866                                  function address into the register that the
1867                                  call insn will use.  flow.c will delete any
1868                                  redundant stores that we have created.  */
1869                               if (GET_CODE (next) == CALL_INSN
1870                                   && GET_CODE (body) == SET
1871                                   && GET_CODE (SET_DEST (body)) == REG
1872                                   && (n = find_reg_note (temp, REG_EQUAL,
1873                                                          NULL_RTX)))
1874                                 {
1875                                   fn_reg = SET_SRC (body);
1876                                   if (GET_CODE (fn_reg) != REG)
1877                                     fn_reg = SET_DEST (body);
1878                                   fn_address = XEXP (n, 0);
1879                                   fn_address_insn = temp;
1880                                 }
1881                               /* We have the call insn.
1882                                  If it uses the register we suspect it might,
1883                                  load it with the correct address directly.  */
1884                               if (GET_CODE (temp) == CALL_INSN
1885                                   && fn_address != 0
1886                                   && reg_referenced_p (fn_reg, body))
1887                                 emit_insn_after (gen_move_insn (fn_reg,
1888                                                                 fn_address),
1889                                                  fn_address_insn);
1890
1891                               if (GET_CODE (temp) == CALL_INSN)
1892                                 {
1893                                   i1 = emit_call_insn_before (body, loop_start);
1894                                   /* Because the USAGE information potentially
1895                                      contains objects other than hard registers
1896                                      we need to copy it.  */
1897                                   if (CALL_INSN_FUNCTION_USAGE (temp))
1898                                     CALL_INSN_FUNCTION_USAGE (i1)
1899                                       = copy_rtx (CALL_INSN_FUNCTION_USAGE (temp));
1900                                 }
1901                               else
1902                                 i1 = emit_insn_before (body, loop_start);
1903                               if (first == 0)
1904                                 first = i1;
1905                               if (temp == fn_address_insn)
1906                                 fn_address_insn = i1;
1907                               REG_NOTES (i1) = REG_NOTES (temp);
1908                               delete_insn (temp);
1909                             }
1910                         }
1911                       if (m->savemode != VOIDmode)
1912                         {
1913                           /* P sets REG to zero; but we should clear only
1914                              the bits that are not covered by the mode
1915                              m->savemode.  */
1916                           rtx reg = m->set_dest;
1917                           rtx sequence;
1918                           rtx tem;
1919                       
1920                           start_sequence ();
1921                           tem = expand_binop
1922                             (GET_MODE (reg), and_optab, reg,
1923                              GEN_INT ((((HOST_WIDE_INT) 1
1924                                         << GET_MODE_BITSIZE (m->savemode)))
1925                                       - 1),
1926                              reg, 1, OPTAB_LIB_WIDEN);
1927                           if (tem == 0)
1928                             abort ();
1929                           if (tem != reg)
1930                             emit_move_insn (reg, tem);
1931                           sequence = gen_sequence ();
1932                           end_sequence ();
1933                           i1 = emit_insn_before (sequence, loop_start);
1934                         }
1935                       else if (GET_CODE (p) == CALL_INSN)
1936                         {
1937                           i1 = emit_call_insn_before (PATTERN (p), loop_start);
1938                           /* Because the USAGE information potentially
1939                              contains objects other than hard registers
1940                              we need to copy it.  */
1941                           if (CALL_INSN_FUNCTION_USAGE (p))
1942                             CALL_INSN_FUNCTION_USAGE (i1)
1943                               = copy_rtx (CALL_INSN_FUNCTION_USAGE (p));
1944                         }
1945                       else
1946                         i1 = emit_insn_before (PATTERN (p), loop_start);
1947
1948                       REG_NOTES (i1) = REG_NOTES (p);
1949
1950                       /* If there is a REG_EQUAL note present whose value is
1951                          not loop invariant, then delete it, since it may
1952                          cause problems with later optimization passes.
1953                          It is possible for cse to create such notes
1954                          like this as a result of record_jump_cond.  */
1955                       
1956                       if ((temp = find_reg_note (i1, REG_EQUAL, NULL_RTX))
1957                           && ! invariant_p (XEXP (temp, 0)))
1958                         remove_note (i1, temp);
1959
1960                       if (new_start == 0)
1961                         new_start = i1;
1962
1963                       if (loop_dump_stream)
1964                         fprintf (loop_dump_stream, " moved to %d",
1965                                  INSN_UID (i1));
1966
1967 #if 0
1968                       /* This isn't needed because REG_NOTES is copied
1969                          below and is wrong since P might be a PARALLEL.  */
1970                       if (REG_NOTES (i1) == 0
1971                           && ! m->partial /* But not if it's a zero-extend clr.  */
1972                           && ! m->global /* and not if used outside the loop
1973                                             (since it might get set outside).  */
1974                           && CONSTANT_P (SET_SRC (PATTERN (p))))
1975                         REG_NOTES (i1)
1976                           = gen_rtx (EXPR_LIST, REG_EQUAL,
1977                                      SET_SRC (PATTERN (p)), REG_NOTES (i1));
1978 #endif
1979
1980                       /* If library call, now fix the REG_NOTES that contain
1981                          insn pointers, namely REG_LIBCALL on FIRST
1982                          and REG_RETVAL on I1.  */
1983                       if (temp = find_reg_note (i1, REG_RETVAL, NULL_RTX))
1984                         {
1985                           XEXP (temp, 0) = first;
1986                           temp = find_reg_note (first, REG_LIBCALL, NULL_RTX);
1987                           XEXP (temp, 0) = i1;
1988                         }
1989
1990                       delete_insn (p);
1991                       do p = NEXT_INSN (p);
1992                       while (p && GET_CODE (p) == NOTE);
1993                     }
1994
1995                   /* The more regs we move, the less we like moving them.  */
1996                   threshold -= 3;
1997                 }
1998
1999               /* Any other movable that loads the same register
2000                  MUST be moved.  */
2001               already_moved[regno] = 1;
2002
2003               /* This reg has been moved out of one loop.  */
2004               moved_once[regno] = 1;
2005
2006               /* The reg set here is now invariant.  */
2007               if (! m->partial)
2008                 n_times_set[regno] = 0;
2009
2010               m->done = 1;
2011
2012               /* Change the length-of-life info for the register
2013                  to say it lives at least the full length of this loop.
2014                  This will help guide optimizations in outer loops.  */
2015
2016               if (uid_luid[REGNO_FIRST_UID (regno)] > INSN_LUID (loop_start))
2017                 /* This is the old insn before all the moved insns.
2018                    We can't use the moved insn because it is out of range
2019                    in uid_luid.  Only the old insns have luids.  */
2020                 REGNO_FIRST_UID (regno) = INSN_UID (loop_start);
2021               if (uid_luid[REGNO_LAST_UID (regno)] < INSN_LUID (end))
2022                 REGNO_LAST_UID (regno) = INSN_UID (end);
2023
2024               /* Combine with this moved insn any other matching movables.  */
2025
2026               if (! m->partial)
2027                 for (m1 = movables; m1; m1 = m1->next)
2028                   if (m1->match == m)
2029                     {
2030                       rtx temp;
2031
2032                       /* Schedule the reg loaded by M1
2033                          for replacement so that shares the reg of M.
2034                          If the modes differ (only possible in restricted
2035                          circumstances, make a SUBREG.  */
2036                       if (GET_MODE (m->set_dest) == GET_MODE (m1->set_dest))
2037                         reg_map[m1->regno] = m->set_dest;
2038                       else
2039                         reg_map[m1->regno]
2040                           = gen_lowpart_common (GET_MODE (m1->set_dest),
2041                                                 m->set_dest);
2042                     
2043                       /* Get rid of the matching insn
2044                          and prevent further processing of it.  */
2045                       m1->done = 1;
2046
2047                       /* if library call, delete all insn except last, which
2048                          is deleted below */
2049                       if (temp = find_reg_note (m1->insn, REG_RETVAL,
2050                                                 NULL_RTX))
2051                         {
2052                           for (temp = XEXP (temp, 0); temp != m1->insn;
2053                                temp = NEXT_INSN (temp))
2054                             delete_insn (temp);
2055                         }
2056                       delete_insn (m1->insn);
2057
2058                       /* Any other movable that loads the same register
2059                          MUST be moved.  */
2060                       already_moved[m1->regno] = 1;
2061
2062                       /* The reg merged here is now invariant,
2063                          if the reg it matches is invariant.  */
2064                       if (! m->partial)
2065                         n_times_set[m1->regno] = 0;
2066                     }
2067             }
2068           else if (loop_dump_stream)
2069             fprintf (loop_dump_stream, "not desirable");
2070         }
2071       else if (loop_dump_stream && !m->match)
2072         fprintf (loop_dump_stream, "not safe");
2073
2074       if (loop_dump_stream)
2075         fprintf (loop_dump_stream, "\n");
2076     }
2077
2078   if (new_start == 0)
2079     new_start = loop_start;
2080
2081   /* Go through all the instructions in the loop, making
2082      all the register substitutions scheduled in REG_MAP.  */
2083   for (p = new_start; p != end; p = NEXT_INSN (p))
2084     if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
2085         || GET_CODE (p) == CALL_INSN)
2086       {
2087         replace_regs (PATTERN (p), reg_map, nregs, 0);
2088         replace_regs (REG_NOTES (p), reg_map, nregs, 0);
2089         INSN_CODE (p) = -1;
2090       }
2091 }
2092 \f
2093 #if 0
2094 /* Scan X and replace the address of any MEM in it with ADDR.
2095    REG is the address that MEM should have before the replacement.  */
2096
2097 static void
2098 replace_call_address (x, reg, addr)
2099      rtx x, reg, addr;
2100 {
2101   register enum rtx_code code;
2102   register int i;
2103   register char *fmt;
2104
2105   if (x == 0)
2106     return;
2107   code = GET_CODE (x);
2108   switch (code)
2109     {
2110     case PC:
2111     case CC0:
2112     case CONST_INT:
2113     case CONST_DOUBLE:
2114     case CONST:
2115     case SYMBOL_REF:
2116     case LABEL_REF:
2117     case REG:
2118       return;
2119
2120     case SET:
2121       /* Short cut for very common case.  */
2122       replace_call_address (XEXP (x, 1), reg, addr);
2123       return;
2124
2125     case CALL:
2126       /* Short cut for very common case.  */
2127       replace_call_address (XEXP (x, 0), reg, addr);
2128       return;
2129
2130     case MEM:
2131       /* If this MEM uses a reg other than the one we expected,
2132          something is wrong.  */
2133       if (XEXP (x, 0) != reg)
2134         abort ();
2135       XEXP (x, 0) = addr;
2136       return;
2137     }
2138
2139   fmt = GET_RTX_FORMAT (code);
2140   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2141     {
2142       if (fmt[i] == 'e')
2143         replace_call_address (XEXP (x, i), reg, addr);
2144       if (fmt[i] == 'E')
2145         {
2146           register int j;
2147           for (j = 0; j < XVECLEN (x, i); j++)
2148             replace_call_address (XVECEXP (x, i, j), reg, addr);
2149         }
2150     }
2151 }
2152 #endif
2153 \f
2154 /* Return the number of memory refs to addresses that vary
2155    in the rtx X.  */
2156
2157 static int
2158 count_nonfixed_reads (x)
2159      rtx x;
2160 {
2161   register enum rtx_code code;
2162   register int i;
2163   register char *fmt;
2164   int value;
2165
2166   if (x == 0)
2167     return 0;
2168
2169   code = GET_CODE (x);
2170   switch (code)
2171     {
2172     case PC:
2173     case CC0:
2174     case CONST_INT:
2175     case CONST_DOUBLE:
2176     case CONST:
2177     case SYMBOL_REF:
2178     case LABEL_REF:
2179     case REG:
2180       return 0;
2181
2182     case MEM:
2183       return ((invariant_p (XEXP (x, 0)) != 1)
2184               + count_nonfixed_reads (XEXP (x, 0)));
2185     }
2186
2187   value = 0;
2188   fmt = GET_RTX_FORMAT (code);
2189   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2190     {
2191       if (fmt[i] == 'e')
2192         value += count_nonfixed_reads (XEXP (x, i));
2193       if (fmt[i] == 'E')
2194         {
2195           register int j;
2196           for (j = 0; j < XVECLEN (x, i); j++)
2197             value += count_nonfixed_reads (XVECEXP (x, i, j));
2198         }
2199     }
2200   return value;
2201 }
2202
2203 \f
2204 #if 0
2205 /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
2206    Replace it with an instruction to load just the low bytes
2207    if the machine supports such an instruction,
2208    and insert above LOOP_START an instruction to clear the register.  */
2209
2210 static void
2211 constant_high_bytes (p, loop_start)
2212      rtx p, loop_start;
2213 {
2214   register rtx new;
2215   register int insn_code_number;
2216
2217   /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
2218      to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...).  */
2219
2220   new = gen_rtx (SET, VOIDmode,
2221                  gen_rtx (STRICT_LOW_PART, VOIDmode,
2222                           gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
2223                                    SET_DEST (PATTERN (p)),
2224                                    0)),
2225                  XEXP (SET_SRC (PATTERN (p)), 0));
2226   insn_code_number = recog (new, p);
2227
2228   if (insn_code_number)
2229     {
2230       register int i;
2231
2232       /* Clear destination register before the loop.  */
2233       emit_insn_before (gen_rtx (SET, VOIDmode,
2234                                  SET_DEST (PATTERN (p)),
2235                                  const0_rtx),
2236                         loop_start);
2237
2238       /* Inside the loop, just load the low part.  */
2239       PATTERN (p) = new;
2240     }
2241 }
2242 #endif
2243 \f
2244 /* Scan a loop setting the variables `unknown_address_altered',
2245    `num_mem_sets', `loop_continue', loops_enclosed', `loop_has_call',
2246    and `loop_has_volatile'.
2247    Also, fill in the array `loop_store_mems'.  */
2248
2249 static void
2250 prescan_loop (start, end)
2251      rtx start, end;
2252 {
2253   register int level = 1;
2254   register rtx insn;
2255
2256   unknown_address_altered = 0;
2257   loop_has_call = 0;
2258   loop_has_volatile = 0;
2259   loop_store_mems_idx = 0;
2260
2261   num_mem_sets = 0;
2262   loops_enclosed = 1;
2263   loop_continue = 0;
2264
2265   for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2266        insn = NEXT_INSN (insn))
2267     {
2268       if (GET_CODE (insn) == NOTE)
2269         {
2270           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2271             {
2272               ++level;
2273               /* Count number of loops contained in this one.  */
2274               loops_enclosed++;
2275             }
2276           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
2277             {
2278               --level;
2279               if (level == 0)
2280                 {
2281                   end = insn;
2282                   break;
2283                 }
2284             }
2285           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
2286             {
2287               if (level == 1)
2288                 loop_continue = insn;
2289             }
2290         }
2291       else if (GET_CODE (insn) == CALL_INSN)
2292         {
2293           if (! CONST_CALL_P (insn))
2294             unknown_address_altered = 1;
2295           loop_has_call = 1;
2296         }
2297       else
2298         {
2299           if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
2300             {
2301               if (volatile_refs_p (PATTERN (insn)))
2302                 loop_has_volatile = 1;
2303
2304               note_stores (PATTERN (insn), note_addr_stored);
2305             }
2306         }
2307     }
2308 }
2309 \f
2310 /* Scan the function looking for loops.  Record the start and end of each loop.
2311    Also mark as invalid loops any loops that contain a setjmp or are branched
2312    to from outside the loop.  */
2313
2314 static void
2315 find_and_verify_loops (f)
2316      rtx f;
2317 {
2318   rtx insn, label;
2319   int current_loop = -1;
2320   int next_loop = -1;
2321   int loop;
2322
2323   /* If there are jumps to undefined labels,
2324      treat them as jumps out of any/all loops.
2325      This also avoids writing past end of tables when there are no loops.  */
2326   uid_loop_num[0] = -1;
2327
2328   /* Find boundaries of loops, mark which loops are contained within
2329      loops, and invalidate loops that have setjmp.  */
2330
2331   for (insn = f; insn; insn = NEXT_INSN (insn))
2332     {
2333       if (GET_CODE (insn) == NOTE)
2334         switch (NOTE_LINE_NUMBER (insn))
2335           {
2336           case NOTE_INSN_LOOP_BEG:
2337             loop_number_loop_starts[++next_loop] =  insn;
2338             loop_number_loop_ends[next_loop] = 0;
2339             loop_outer_loop[next_loop] = current_loop;
2340             loop_invalid[next_loop] = 0;
2341             loop_number_exit_labels[next_loop] = 0;
2342             loop_number_exit_count[next_loop] = 0;
2343             current_loop = next_loop;
2344             break;
2345
2346           case NOTE_INSN_SETJMP:
2347             /* In this case, we must invalidate our current loop and any
2348                enclosing loop.  */
2349             for (loop = current_loop; loop != -1; loop = loop_outer_loop[loop])
2350               {
2351                 loop_invalid[loop] = 1;
2352                 if (loop_dump_stream)
2353                   fprintf (loop_dump_stream,
2354                            "\nLoop at %d ignored due to setjmp.\n",
2355                            INSN_UID (loop_number_loop_starts[loop]));
2356               }
2357             break;
2358
2359           case NOTE_INSN_LOOP_END:
2360             if (current_loop == -1)
2361               abort ();
2362
2363             loop_number_loop_ends[current_loop] = insn;
2364             current_loop = loop_outer_loop[current_loop];
2365             break;
2366
2367           }
2368
2369       /* Note that this will mark the NOTE_INSN_LOOP_END note as being in the
2370          enclosing loop, but this doesn't matter.  */
2371       uid_loop_num[INSN_UID (insn)] = current_loop;
2372     }
2373
2374   /* Any loop containing a label used in an initializer must be invalidated,
2375      because it can be jumped into from anywhere.  */
2376
2377   for (label = forced_labels; label; label = XEXP (label, 1))
2378     {
2379       int loop_num;
2380
2381       for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2382            loop_num != -1;
2383            loop_num = loop_outer_loop[loop_num])
2384         loop_invalid[loop_num] = 1;
2385     }
2386
2387   /* Any loop containing a label used for an exception handler must be
2388      invalidated, because it can be jumped into from anywhere.  */
2389
2390   for (label = exception_handler_labels; label; label = XEXP (label, 1))
2391     {
2392       int loop_num;
2393
2394       for (loop_num = uid_loop_num[INSN_UID (XEXP (label, 0))];
2395            loop_num != -1;
2396            loop_num = loop_outer_loop[loop_num])
2397         loop_invalid[loop_num] = 1;
2398     }
2399
2400   /* Now scan all insn's in the function.  If any JUMP_INSN branches into a
2401      loop that it is not contained within, that loop is marked invalid.
2402      If any INSN or CALL_INSN uses a label's address, then the loop containing
2403      that label is marked invalid, because it could be jumped into from
2404      anywhere.
2405
2406      Also look for blocks of code ending in an unconditional branch that
2407      exits the loop.  If such a block is surrounded by a conditional 
2408      branch around the block, move the block elsewhere (see below) and
2409      invert the jump to point to the code block.  This may eliminate a
2410      label in our loop and will simplify processing by both us and a
2411      possible second cse pass.  */
2412
2413   for (insn = f; insn; insn = NEXT_INSN (insn))
2414     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2415       {
2416         int this_loop_num = uid_loop_num[INSN_UID (insn)];
2417
2418         if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
2419           {
2420             rtx note = find_reg_note (insn, REG_LABEL, NULL_RTX);
2421             if (note)
2422               {
2423                 int loop_num;
2424
2425                 for (loop_num = uid_loop_num[INSN_UID (XEXP (note, 0))];
2426                      loop_num != -1;
2427                      loop_num = loop_outer_loop[loop_num])
2428                   loop_invalid[loop_num] = 1;
2429               }
2430           }
2431
2432         if (GET_CODE (insn) != JUMP_INSN)
2433           continue;
2434
2435         mark_loop_jump (PATTERN (insn), this_loop_num);
2436
2437         /* See if this is an unconditional branch outside the loop.  */
2438         if (this_loop_num != -1
2439             && (GET_CODE (PATTERN (insn)) == RETURN
2440                 || (simplejump_p (insn)
2441                     && (uid_loop_num[INSN_UID (JUMP_LABEL (insn))]
2442                         != this_loop_num)))
2443             && get_max_uid () < max_uid_for_loop)
2444           {
2445             rtx p;
2446             rtx our_next = next_real_insn (insn);
2447             int dest_loop;
2448             int outer_loop = -1;
2449
2450             /* Go backwards until we reach the start of the loop, a label,
2451                or a JUMP_INSN.  */
2452             for (p = PREV_INSN (insn);
2453                  GET_CODE (p) != CODE_LABEL
2454                  && ! (GET_CODE (p) == NOTE
2455                        && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
2456                  && GET_CODE (p) != JUMP_INSN;
2457                  p = PREV_INSN (p))
2458               ;
2459
2460             /* Check for the case where we have a jump to an inner nested
2461                loop, and do not perform the optimization in that case.  */
2462
2463             if (JUMP_LABEL (insn))
2464               {
2465                 dest_loop = uid_loop_num[INSN_UID (JUMP_LABEL (insn))];
2466                 if (dest_loop != -1)
2467                   {
2468                     for (outer_loop = dest_loop; outer_loop != -1;
2469                          outer_loop = loop_outer_loop[outer_loop])
2470                       if (outer_loop == this_loop_num)
2471                         break;
2472                   }
2473               }
2474
2475             /* Make sure that the target of P is within the current loop.  */
2476
2477             if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
2478                 && uid_loop_num[INSN_UID (JUMP_LABEL (p))] != this_loop_num)
2479               outer_loop = this_loop_num;
2480
2481             /* If we stopped on a JUMP_INSN to the next insn after INSN,
2482                we have a block of code to try to move.
2483
2484                We look backward and then forward from the target of INSN
2485                to find a BARRIER at the same loop depth as the target.
2486                If we find such a BARRIER, we make a new label for the start
2487                of the block, invert the jump in P and point it to that label,
2488                and move the block of code to the spot we found.  */
2489
2490             if (outer_loop == -1
2491                 && GET_CODE (p) == JUMP_INSN
2492                 && JUMP_LABEL (p) != 0
2493                 /* Just ignore jumps to labels that were never emitted.
2494                    These always indicate compilation errors.  */
2495                 && INSN_UID (JUMP_LABEL (p)) != 0
2496                 && condjump_p (p)
2497                 && ! simplejump_p (p)
2498                 && next_real_insn (JUMP_LABEL (p)) == our_next)
2499               {
2500                 rtx target
2501                   = JUMP_LABEL (insn) ? JUMP_LABEL (insn) : get_last_insn ();
2502                 int target_loop_num = uid_loop_num[INSN_UID (target)];
2503                 rtx loc;
2504
2505                 for (loc = target; loc; loc = PREV_INSN (loc))
2506                   if (GET_CODE (loc) == BARRIER
2507                       && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2508                     break;
2509
2510                 if (loc == 0)
2511                   for (loc = target; loc; loc = NEXT_INSN (loc))
2512                     if (GET_CODE (loc) == BARRIER
2513                         && uid_loop_num[INSN_UID (loc)] == target_loop_num)
2514                       break;
2515
2516                 if (loc)
2517                   {
2518                     rtx cond_label = JUMP_LABEL (p);
2519                     rtx new_label = get_label_after (p);
2520
2521                     /* Ensure our label doesn't go away.  */
2522                     LABEL_NUSES (cond_label)++;
2523
2524                     /* Verify that uid_loop_num is large enough and that
2525                        we can invert P.  */
2526                    if (invert_jump (p, new_label))
2527                      {
2528                        rtx q, r;
2529
2530                        /* Include the BARRIER after INSN and copy the
2531                           block after LOC.  */
2532                        new_label = squeeze_notes (new_label, NEXT_INSN (insn));
2533                        reorder_insns (new_label, NEXT_INSN (insn), loc);
2534
2535                        /* All those insns are now in TARGET_LOOP_NUM.  */
2536                        for (q = new_label; q != NEXT_INSN (NEXT_INSN (insn));
2537                             q = NEXT_INSN (q))
2538                          uid_loop_num[INSN_UID (q)] = target_loop_num;
2539
2540                        /* The label jumped to by INSN is no longer a loop exit.
2541                           Unless INSN does not have a label (e.g., it is a
2542                           RETURN insn), search loop_number_exit_labels to find
2543                           its label_ref, and remove it.  Also turn off
2544                           LABEL_OUTSIDE_LOOP_P bit.  */
2545                        if (JUMP_LABEL (insn))
2546                          {
2547                            int loop_num;
2548
2549                            for (q = 0,
2550                                 r = loop_number_exit_labels[this_loop_num];
2551                                 r; q = r, r = LABEL_NEXTREF (r))
2552                              if (XEXP (r, 0) == JUMP_LABEL (insn))
2553                                {
2554                                  LABEL_OUTSIDE_LOOP_P (r) = 0;
2555                                  if (q)
2556                                    LABEL_NEXTREF (q) = LABEL_NEXTREF (r);
2557                                  else
2558                                    loop_number_exit_labels[this_loop_num]
2559                                      = LABEL_NEXTREF (r);
2560                                  break;
2561                                }
2562
2563                            for (loop_num = this_loop_num;
2564                                 loop_num != -1 && loop_num != target_loop_num;
2565                                 loop_num = loop_outer_loop[loop_num])
2566                              loop_number_exit_count[loop_num]--;
2567
2568                            /* If we didn't find it, then something is wrong.  */
2569                            if (! r)
2570                              abort ();
2571                          }
2572
2573                        /* P is now a jump outside the loop, so it must be put
2574                           in loop_number_exit_labels, and marked as such.
2575                           The easiest way to do this is to just call
2576                           mark_loop_jump again for P.  */
2577                        mark_loop_jump (PATTERN (p), this_loop_num);
2578
2579                        /* If INSN now jumps to the insn after it,
2580                           delete INSN.  */
2581                        if (JUMP_LABEL (insn) != 0
2582                            && (next_real_insn (JUMP_LABEL (insn))
2583                                == next_real_insn (insn)))
2584                          delete_insn (insn);
2585                      }
2586
2587                     /* Continue the loop after where the conditional
2588                        branch used to jump, since the only branch insn
2589                        in the block (if it still remains) is an inter-loop
2590                        branch and hence needs no processing.  */
2591                     insn = NEXT_INSN (cond_label);
2592
2593                     if (--LABEL_NUSES (cond_label) == 0)
2594                       delete_insn (cond_label);
2595
2596                     /* This loop will be continued with NEXT_INSN (insn).  */
2597                     insn = PREV_INSN (insn);
2598                   }
2599               }
2600           }
2601       }
2602 }
2603
2604 /* If any label in X jumps to a loop different from LOOP_NUM and any of the
2605    loops it is contained in, mark the target loop invalid.
2606
2607    For speed, we assume that X is part of a pattern of a JUMP_INSN.  */
2608
2609 static void
2610 mark_loop_jump (x, loop_num)
2611      rtx x;
2612      int loop_num;
2613 {
2614   int dest_loop;
2615   int outer_loop;
2616   int i;
2617
2618   switch (GET_CODE (x))
2619     {
2620     case PC:
2621     case USE:
2622     case CLOBBER:
2623     case REG:
2624     case MEM:
2625     case CONST_INT:
2626     case CONST_DOUBLE:
2627     case RETURN:
2628       return;
2629
2630     case CONST:
2631       /* There could be a label reference in here.  */
2632       mark_loop_jump (XEXP (x, 0), loop_num);
2633       return;
2634
2635     case PLUS:
2636     case MINUS:
2637     case MULT:
2638       mark_loop_jump (XEXP (x, 0), loop_num);
2639       mark_loop_jump (XEXP (x, 1), loop_num);
2640       return;
2641
2642     case SIGN_EXTEND:
2643     case ZERO_EXTEND:
2644       mark_loop_jump (XEXP (x, 0), loop_num);
2645       return;
2646
2647     case LABEL_REF:
2648       dest_loop = uid_loop_num[INSN_UID (XEXP (x, 0))];
2649
2650       /* Link together all labels that branch outside the loop.  This
2651          is used by final_[bg]iv_value and the loop unrolling code.  Also
2652          mark this LABEL_REF so we know that this branch should predict
2653          false.  */
2654
2655       /* A check to make sure the label is not in an inner nested loop,
2656          since this does not count as a loop exit.  */
2657       if (dest_loop != -1)
2658         {
2659           for (outer_loop = dest_loop; outer_loop != -1;
2660                outer_loop = loop_outer_loop[outer_loop])
2661             if (outer_loop == loop_num)
2662               break;
2663         }
2664       else
2665         outer_loop = -1;
2666
2667       if (loop_num != -1 && outer_loop == -1)
2668         {
2669           LABEL_OUTSIDE_LOOP_P (x) = 1;
2670           LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2671           loop_number_exit_labels[loop_num] = x;
2672
2673           for (outer_loop = loop_num;
2674                outer_loop != -1 && outer_loop != dest_loop;
2675                outer_loop = loop_outer_loop[outer_loop])
2676             loop_number_exit_count[outer_loop]++;
2677         }
2678
2679       /* If this is inside a loop, but not in the current loop or one enclosed
2680          by it, it invalidates at least one loop.  */
2681
2682       if (dest_loop == -1)
2683         return;
2684
2685       /* We must invalidate every nested loop containing the target of this
2686          label, except those that also contain the jump insn.  */
2687
2688       for (; dest_loop != -1; dest_loop = loop_outer_loop[dest_loop])
2689         {
2690           /* Stop when we reach a loop that also contains the jump insn.  */
2691           for (outer_loop = loop_num; outer_loop != -1;
2692                outer_loop = loop_outer_loop[outer_loop])
2693             if (dest_loop == outer_loop)
2694               return;
2695
2696           /* If we get here, we know we need to invalidate a loop.  */
2697           if (loop_dump_stream && ! loop_invalid[dest_loop])
2698             fprintf (loop_dump_stream,
2699                      "\nLoop at %d ignored due to multiple entry points.\n",
2700                      INSN_UID (loop_number_loop_starts[dest_loop]));
2701           
2702           loop_invalid[dest_loop] = 1;
2703         }
2704       return;
2705
2706     case SET:
2707       /* If this is not setting pc, ignore.  */
2708       if (SET_DEST (x) == pc_rtx)
2709         mark_loop_jump (SET_SRC (x), loop_num);
2710       return;
2711
2712     case IF_THEN_ELSE:
2713       mark_loop_jump (XEXP (x, 1), loop_num);
2714       mark_loop_jump (XEXP (x, 2), loop_num);
2715       return;
2716
2717     case PARALLEL:
2718     case ADDR_VEC:
2719       for (i = 0; i < XVECLEN (x, 0); i++)
2720         mark_loop_jump (XVECEXP (x, 0, i), loop_num);
2721       return;
2722
2723     case ADDR_DIFF_VEC:
2724       for (i = 0; i < XVECLEN (x, 1); i++)
2725         mark_loop_jump (XVECEXP (x, 1, i), loop_num);
2726       return;
2727
2728     default:
2729       /* Treat anything else (such as a symbol_ref)
2730          as a branch out of this loop, but not into any loop.  */
2731
2732       if (loop_num != -1)
2733         {
2734 #ifdef HAIFA
2735           LABEL_OUTSIDE_LOOP_P (x) = 1;
2736           LABEL_NEXTREF (x) = loop_number_exit_labels[loop_num];
2737 #endif  /* HAIFA */
2738
2739           loop_number_exit_labels[loop_num] = x;
2740
2741           for (outer_loop = loop_num; outer_loop != -1;
2742                outer_loop = loop_outer_loop[outer_loop])
2743             loop_number_exit_count[outer_loop]++;
2744         }
2745       return;
2746     }
2747 }
2748 \f
2749 /* Return nonzero if there is a label in the range from
2750    insn INSN to and including the insn whose luid is END
2751    INSN must have an assigned luid (i.e., it must not have
2752    been previously created by loop.c).  */
2753
2754 static int
2755 labels_in_range_p (insn, end)
2756      rtx insn;
2757      int end;
2758 {
2759   while (insn && INSN_LUID (insn) <= end)
2760     {
2761       if (GET_CODE (insn) == CODE_LABEL)
2762         return 1;
2763       insn = NEXT_INSN (insn);
2764     }
2765
2766   return 0;
2767 }
2768
2769 /* Record that a memory reference X is being set.  */
2770
2771 static void
2772 note_addr_stored (x)
2773      rtx x;
2774 {
2775   register int i;
2776
2777   if (x == 0 || GET_CODE (x) != MEM)
2778     return;
2779
2780   /* Count number of memory writes.
2781      This affects heuristics in strength_reduce.  */
2782   num_mem_sets++;
2783
2784   /* BLKmode MEM means all memory is clobbered.  */
2785   if (GET_MODE (x) == BLKmode)
2786     unknown_address_altered = 1;
2787
2788   if (unknown_address_altered)
2789     return;
2790
2791   for (i = 0; i < loop_store_mems_idx; i++)
2792     if (rtx_equal_p (XEXP (loop_store_mems[i], 0), XEXP (x, 0))
2793         && MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (loop_store_mems[i]))
2794       {
2795         /* We are storing at the same address as previously noted.  Save the
2796            wider reference.  */
2797         if (GET_MODE_SIZE (GET_MODE (x))
2798             > GET_MODE_SIZE (GET_MODE (loop_store_mems[i])))
2799           loop_store_mems[i] = x;
2800         break;
2801       }
2802
2803   if (i == NUM_STORES)
2804     unknown_address_altered = 1;
2805
2806   else if (i == loop_store_mems_idx)
2807     loop_store_mems[loop_store_mems_idx++] = x;
2808 }
2809 \f
2810 /* Return nonzero if the rtx X is invariant over the current loop.
2811
2812    The value is 2 if we refer to something only conditionally invariant.
2813
2814    If `unknown_address_altered' is nonzero, no memory ref is invariant.
2815    Otherwise, a memory ref is invariant if it does not conflict with
2816    anything stored in `loop_store_mems'.  */
2817
2818 int
2819 invariant_p (x)
2820      register rtx x;
2821 {
2822   register int i;
2823   register enum rtx_code code;
2824   register char *fmt;
2825   int conditional = 0;
2826
2827   if (x == 0)
2828     return 1;
2829   code = GET_CODE (x);
2830   switch (code)
2831     {
2832     case CONST_INT:
2833     case CONST_DOUBLE:
2834     case SYMBOL_REF:
2835     case CONST:
2836       return 1;
2837
2838     case LABEL_REF:
2839       /* A LABEL_REF is normally invariant, however, if we are unrolling
2840          loops, and this label is inside the loop, then it isn't invariant.
2841          This is because each unrolled copy of the loop body will have
2842          a copy of this label.  If this was invariant, then an insn loading
2843          the address of this label into a register might get moved outside
2844          the loop, and then each loop body would end up using the same label.
2845
2846          We don't know the loop bounds here though, so just fail for all
2847          labels.  */
2848       if (flag_unroll_loops)
2849         return 0;
2850       else
2851         return 1;
2852
2853     case PC:
2854     case CC0:
2855     case UNSPEC_VOLATILE:
2856       return 0;
2857
2858     case REG:
2859       /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
2860          since the reg might be set by initialization within the loop.  */
2861
2862       if ((x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2863            || x == arg_pointer_rtx)
2864           && ! current_function_has_nonlocal_goto)
2865         return 1;
2866
2867       if (loop_has_call
2868           && REGNO (x) < FIRST_PSEUDO_REGISTER && call_used_regs[REGNO (x)])
2869         return 0;
2870
2871       if (n_times_set[REGNO (x)] < 0)
2872         return 2;
2873
2874       return n_times_set[REGNO (x)] == 0;
2875
2876     case MEM:
2877       /* Volatile memory references must be rejected.  Do this before
2878          checking for read-only items, so that volatile read-only items
2879          will be rejected also.  */
2880       if (MEM_VOLATILE_P (x))
2881         return 0;
2882
2883       /* Read-only items (such as constants in a constant pool) are
2884          invariant if their address is.  */
2885       if (RTX_UNCHANGING_P (x))
2886         break;
2887
2888       /* If we filled the table (or had a subroutine call), any location
2889          in memory could have been clobbered.  */
2890       if (unknown_address_altered)
2891         return 0;
2892
2893       /* See if there is any dependence between a store and this load.  */
2894       for (i = loop_store_mems_idx - 1; i >= 0; i--)
2895         if (true_dependence (loop_store_mems[i], VOIDmode, x, rtx_varies_p))
2896           return 0;
2897
2898       /* It's not invalidated by a store in memory
2899          but we must still verify the address is invariant.  */
2900       break;
2901
2902     case ASM_OPERANDS:
2903       /* Don't mess with insns declared volatile.  */
2904       if (MEM_VOLATILE_P (x))
2905         return 0;
2906     }
2907
2908   fmt = GET_RTX_FORMAT (code);
2909   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2910     {
2911       if (fmt[i] == 'e')
2912         {
2913           int tem = invariant_p (XEXP (x, i));
2914           if (tem == 0)
2915             return 0;
2916           if (tem == 2)
2917             conditional = 1;
2918         }
2919       else if (fmt[i] == 'E')
2920         {
2921           register int j;
2922           for (j = 0; j < XVECLEN (x, i); j++)
2923             {
2924               int tem = invariant_p (XVECEXP (x, i, j));
2925               if (tem == 0)
2926                 return 0;
2927               if (tem == 2)
2928                 conditional = 1;
2929             }
2930
2931         }
2932     }
2933
2934   return 1 + conditional;
2935 }
2936
2937 \f
2938 /* Return nonzero if all the insns in the loop that set REG
2939    are INSN and the immediately following insns,
2940    and if each of those insns sets REG in an invariant way
2941    (not counting uses of REG in them).
2942
2943    The value is 2 if some of these insns are only conditionally invariant.
2944
2945    We assume that INSN itself is the first set of REG
2946    and that its source is invariant.  */
2947
2948 static int
2949 consec_sets_invariant_p (reg, n_sets, insn)
2950      int n_sets;
2951      rtx reg, insn;
2952 {
2953   register rtx p = insn;
2954   register int regno = REGNO (reg);
2955   rtx temp;
2956   /* Number of sets we have to insist on finding after INSN.  */
2957   int count = n_sets - 1;
2958   int old = n_times_set[regno];
2959   int value = 0;
2960   int this;
2961
2962   /* If N_SETS hit the limit, we can't rely on its value.  */
2963   if (n_sets == 127)
2964     return 0;
2965
2966   n_times_set[regno] = 0;
2967
2968   while (count > 0)
2969     {
2970       register enum rtx_code code;
2971       rtx set;
2972
2973       p = NEXT_INSN (p);
2974       code = GET_CODE (p);
2975
2976       /* If library call, skip to end of of it.  */
2977       if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
2978         p = XEXP (temp, 0);
2979
2980       this = 0;
2981       if (code == INSN
2982           && (set = single_set (p))
2983           && GET_CODE (SET_DEST (set)) == REG
2984           && REGNO (SET_DEST (set)) == regno)
2985         {
2986           this = invariant_p (SET_SRC (set));
2987           if (this != 0)
2988             value |= this;
2989           else if (temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
2990             {
2991               /* If this is a libcall, then any invariant REG_EQUAL note is OK.
2992                  If this is an ordinary insn, then only CONSTANT_P REG_EQUAL
2993                  notes are OK.  */
2994               this = (CONSTANT_P (XEXP (temp, 0))
2995                       || (find_reg_note (p, REG_RETVAL, NULL_RTX)
2996                           && invariant_p (XEXP (temp, 0))));
2997               if (this != 0)
2998                 value |= this;
2999             }
3000         }
3001       if (this != 0)
3002         count--;
3003       else if (code != NOTE)
3004         {
3005           n_times_set[regno] = old;
3006           return 0;
3007         }
3008     }
3009
3010   n_times_set[regno] = old;
3011   /* If invariant_p ever returned 2, we return 2.  */
3012   return 1 + (value & 2);
3013 }
3014
3015 #if 0
3016 /* I don't think this condition is sufficient to allow INSN
3017    to be moved, so we no longer test it.  */
3018
3019 /* Return 1 if all insns in the basic block of INSN and following INSN
3020    that set REG are invariant according to TABLE.  */
3021
3022 static int
3023 all_sets_invariant_p (reg, insn, table)
3024      rtx reg, insn;
3025      short *table;
3026 {
3027   register rtx p = insn;
3028   register int regno = REGNO (reg);
3029
3030   while (1)
3031     {
3032       register enum rtx_code code;
3033       p = NEXT_INSN (p);
3034       code = GET_CODE (p);
3035       if (code == CODE_LABEL || code == JUMP_INSN)
3036         return 1;
3037       if (code == INSN && GET_CODE (PATTERN (p)) == SET
3038           && GET_CODE (SET_DEST (PATTERN (p))) == REG
3039           && REGNO (SET_DEST (PATTERN (p))) == regno)
3040         {
3041           if (!invariant_p (SET_SRC (PATTERN (p)), table))
3042             return 0;
3043         }
3044     }
3045 }
3046 #endif /* 0 */
3047 \f
3048 /* Look at all uses (not sets) of registers in X.  For each, if it is
3049    the single use, set USAGE[REGNO] to INSN; if there was a previous use in
3050    a different insn, set USAGE[REGNO] to const0_rtx.  */
3051
3052 static void
3053 find_single_use_in_loop (insn, x, usage)
3054      rtx insn;
3055      rtx x;
3056      rtx *usage;
3057 {
3058   enum rtx_code code = GET_CODE (x);
3059   char *fmt = GET_RTX_FORMAT (code);
3060   int i, j;
3061
3062   if (code == REG)
3063     usage[REGNO (x)]
3064       = (usage[REGNO (x)] != 0 && usage[REGNO (x)] != insn)
3065         ? const0_rtx : insn;
3066
3067   else if (code == SET)
3068     {
3069       /* Don't count SET_DEST if it is a REG; otherwise count things
3070          in SET_DEST because if a register is partially modified, it won't
3071          show up as a potential movable so we don't care how USAGE is set 
3072          for it.  */
3073       if (GET_CODE (SET_DEST (x)) != REG)
3074         find_single_use_in_loop (insn, SET_DEST (x), usage);
3075       find_single_use_in_loop (insn, SET_SRC (x), usage);
3076     }
3077   else
3078     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3079       {
3080         if (fmt[i] == 'e' && XEXP (x, i) != 0)
3081           find_single_use_in_loop (insn, XEXP (x, i), usage);
3082         else if (fmt[i] == 'E')
3083           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3084             find_single_use_in_loop (insn, XVECEXP (x, i, j), usage);
3085       }
3086 }
3087 \f
3088 /* Increment N_TIMES_SET at the index of each register
3089    that is modified by an insn between FROM and TO.
3090    If the value of an element of N_TIMES_SET becomes 127 or more,
3091    stop incrementing it, to avoid overflow.
3092
3093    Store in SINGLE_USAGE[I] the single insn in which register I is
3094    used, if it is only used once.  Otherwise, it is set to 0 (for no
3095    uses) or const0_rtx for more than one use.  This parameter may be zero,
3096    in which case this processing is not done.
3097
3098    Store in *COUNT_PTR the number of actual instruction
3099    in the loop.  We use this to decide what is worth moving out.  */
3100
3101 /* last_set[n] is nonzero iff reg n has been set in the current basic block.
3102    In that case, it is the insn that last set reg n.  */
3103
3104 static void
3105 count_loop_regs_set (from, to, may_not_move, single_usage, count_ptr, nregs)
3106      register rtx from, to;
3107      char *may_not_move;
3108      rtx *single_usage;
3109      int *count_ptr;
3110      int nregs;
3111 {
3112   register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
3113   register rtx insn;
3114   register int count = 0;
3115   register rtx dest;
3116
3117   bzero ((char *) last_set, nregs * sizeof (rtx));
3118   for (insn = from; insn != to; insn = NEXT_INSN (insn))
3119     {
3120       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3121         {
3122           ++count;
3123
3124           /* If requested, record registers that have exactly one use.  */
3125           if (single_usage)
3126             {
3127               find_single_use_in_loop (insn, PATTERN (insn), single_usage);
3128
3129               /* Include uses in REG_EQUAL notes.  */
3130               if (REG_NOTES (insn))
3131                 find_single_use_in_loop (insn, REG_NOTES (insn), single_usage);
3132             }
3133
3134           if (GET_CODE (PATTERN (insn)) == CLOBBER
3135               && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
3136             /* Don't move a reg that has an explicit clobber.
3137                We might do so sometimes, but it's not worth the pain.  */
3138             may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
3139
3140           if (GET_CODE (PATTERN (insn)) == SET
3141               || GET_CODE (PATTERN (insn)) == CLOBBER)
3142             {
3143               dest = SET_DEST (PATTERN (insn));
3144               while (GET_CODE (dest) == SUBREG
3145                      || GET_CODE (dest) == ZERO_EXTRACT
3146                      || GET_CODE (dest) == SIGN_EXTRACT
3147                      || GET_CODE (dest) == STRICT_LOW_PART)
3148                 dest = XEXP (dest, 0);
3149               if (GET_CODE (dest) == REG)
3150                 {
3151                   register int regno = REGNO (dest);
3152                   /* If this is the first setting of this reg
3153                      in current basic block, and it was set before,
3154                      it must be set in two basic blocks, so it cannot
3155                      be moved out of the loop.  */
3156                   if (n_times_set[regno] > 0 && last_set[regno] == 0)
3157                     may_not_move[regno] = 1;
3158                   /* If this is not first setting in current basic block,
3159                      see if reg was used in between previous one and this.
3160                      If so, neither one can be moved.  */
3161                   if (last_set[regno] != 0
3162                       && reg_used_between_p (dest, last_set[regno], insn))
3163                     may_not_move[regno] = 1;
3164                   if (n_times_set[regno] < 127)
3165                     ++n_times_set[regno];
3166                   last_set[regno] = insn;
3167                 }
3168             }
3169           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3170             {
3171               register int i;
3172               for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3173                 {
3174                   register rtx x = XVECEXP (PATTERN (insn), 0, i);
3175                   if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
3176                     /* Don't move a reg that has an explicit clobber.
3177                        It's not worth the pain to try to do it correctly.  */
3178                     may_not_move[REGNO (XEXP (x, 0))] = 1;
3179
3180                   if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
3181                     {
3182                       dest = SET_DEST (x);
3183                       while (GET_CODE (dest) == SUBREG
3184                              || GET_CODE (dest) == ZERO_EXTRACT
3185                              || GET_CODE (dest) == SIGN_EXTRACT
3186                              || GET_CODE (dest) == STRICT_LOW_PART)
3187                         dest = XEXP (dest, 0);
3188                       if (GET_CODE (dest) == REG)
3189                         {
3190                           register int regno = REGNO (dest);
3191                           if (n_times_set[regno] > 0 && last_set[regno] == 0)
3192                             may_not_move[regno] = 1;
3193                           if (last_set[regno] != 0
3194                               && reg_used_between_p (dest, last_set[regno], insn))
3195                             may_not_move[regno] = 1;
3196                           if (n_times_set[regno] < 127)
3197                             ++n_times_set[regno];
3198                           last_set[regno] = insn;
3199                         }
3200                     }
3201                 }
3202             }
3203         }
3204
3205       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
3206         bzero ((char *) last_set, nregs * sizeof (rtx));
3207     }
3208   *count_ptr = count;
3209 }
3210 \f
3211 /* Given a loop that is bounded by LOOP_START and LOOP_END
3212    and that is entered at SCAN_START,
3213    return 1 if the register set in SET contained in insn INSN is used by
3214    any insn that precedes INSN in cyclic order starting
3215    from the loop entry point.
3216
3217    We don't want to use INSN_LUID here because if we restrict INSN to those
3218    that have a valid INSN_LUID, it means we cannot move an invariant out
3219    from an inner loop past two loops.  */
3220
3221 static int
3222 loop_reg_used_before_p (set, insn, loop_start, scan_start, loop_end)
3223      rtx set, insn, loop_start, scan_start, loop_end;
3224 {
3225   rtx reg = SET_DEST (set);
3226   rtx p;
3227
3228   /* Scan forward checking for register usage.  If we hit INSN, we
3229      are done.  Otherwise, if we hit LOOP_END, wrap around to LOOP_START.  */
3230   for (p = scan_start; p != insn; p = NEXT_INSN (p))
3231     {
3232       if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
3233           && reg_overlap_mentioned_p (reg, PATTERN (p)))
3234         return 1;
3235
3236       if (p == loop_end)
3237         p = loop_start;
3238     }
3239
3240   return 0;
3241 }
3242 \f
3243 /* A "basic induction variable" or biv is a pseudo reg that is set
3244    (within this loop) only by incrementing or decrementing it.  */
3245 /* A "general induction variable" or giv is a pseudo reg whose
3246    value is a linear function of a biv.  */
3247
3248 /* Bivs are recognized by `basic_induction_var';
3249    Givs by `general_induct_var'.  */
3250
3251 /* Indexed by register number, indicates whether or not register is an
3252    induction variable, and if so what type.  */
3253
3254 enum iv_mode *reg_iv_type;
3255
3256 /* Indexed by register number, contains pointer to `struct induction'
3257    if register is an induction variable.  This holds general info for
3258    all induction variables.  */
3259
3260 struct induction **reg_iv_info;
3261
3262 /* Indexed by register number, contains pointer to `struct iv_class'
3263    if register is a basic induction variable.  This holds info describing
3264    the class (a related group) of induction variables that the biv belongs
3265    to.  */
3266
3267 struct iv_class **reg_biv_class;
3268
3269 /* The head of a list which links together (via the next field)
3270    every iv class for the current loop.  */
3271
3272 struct iv_class *loop_iv_list;
3273
3274 /* Communication with routines called via `note_stores'.  */
3275
3276 static rtx note_insn;
3277
3278 /* Dummy register to have non-zero DEST_REG for DEST_ADDR type givs.  */
3279
3280 static rtx addr_placeholder;
3281
3282 /* ??? Unfinished optimizations, and possible future optimizations,
3283    for the strength reduction code.  */
3284
3285 /* ??? There is one more optimization you might be interested in doing: to
3286    allocate pseudo registers for frequently-accessed memory locations.
3287    If the same memory location is referenced each time around, it might
3288    be possible to copy it into a register before and out after.
3289    This is especially useful when the memory location is a variable which
3290    is in a stack slot because somewhere its address is taken.  If the
3291    loop doesn't contain a function call and the variable isn't volatile,
3292    it is safe to keep the value in a register for the duration of the
3293    loop. One tricky thing is that the copying of the value back from the
3294    register has to be done on all exits from the loop.  You need to check that
3295    all the exits from the loop go to the same place.  */
3296
3297 /* ??? The interaction of biv elimination, and recognition of 'constant'
3298    bivs, may cause problems.  */
3299
3300 /* ??? Add heuristics so that DEST_ADDR strength reduction does not cause
3301    performance problems.
3302
3303    Perhaps don't eliminate things that can be combined with an addressing
3304    mode.  Find all givs that have the same biv, mult_val, and add_val;
3305    then for each giv, check to see if its only use dies in a following
3306    memory address.  If so, generate a new memory address and check to see
3307    if it is valid.   If it is valid, then store the modified memory address,
3308    otherwise, mark the giv as not done so that it will get its own iv.  */
3309
3310 /* ??? Could try to optimize branches when it is known that a biv is always
3311    positive.  */
3312
3313 /* ??? When replace a biv in a compare insn, we should replace with closest
3314    giv so that an optimized branch can still be recognized by the combiner,
3315    e.g. the VAX acb insn.  */
3316
3317 /* ??? Many of the checks involving uid_luid could be simplified if regscan
3318    was rerun in loop_optimize whenever a register was added or moved.
3319    Also, some of the optimizations could be a little less conservative.  */
3320 \f
3321 /* Perform strength reduction and induction variable elimination.  */
3322
3323 /* Pseudo registers created during this function will be beyond the last
3324    valid index in several tables including n_times_set and regno_last_uid.
3325    This does not cause a problem here, because the added registers cannot be
3326    givs outside of their loop, and hence will never be reconsidered.
3327    But scan_loop must check regnos to make sure they are in bounds.  */
3328
3329 static void
3330 strength_reduce (scan_start, end, loop_top, insn_count,
3331                  loop_start, loop_end)
3332      rtx scan_start;
3333      rtx end;
3334      rtx loop_top;
3335      int insn_count;
3336      rtx loop_start;
3337      rtx loop_end;
3338 {
3339   rtx p;
3340   rtx set;
3341   rtx inc_val;
3342   rtx mult_val;
3343   rtx dest_reg;
3344   /* This is 1 if current insn is not executed at least once for every loop
3345      iteration.  */
3346   int not_every_iteration = 0;
3347   /* This is 1 if current insn may be executed more than once for every
3348      loop iteration.  */
3349   int maybe_multiple = 0;
3350   /* Temporary list pointers for traversing loop_iv_list.  */
3351   struct iv_class *bl, **backbl;
3352   /* Ratio of extra register life span we can justify
3353      for saving an instruction.  More if loop doesn't call subroutines
3354      since in that case saving an insn makes more difference
3355      and more registers are available.  */
3356   /* ??? could set this to last value of threshold in move_movables */
3357   int threshold = (loop_has_call ? 1 : 2) * (3 + n_non_fixed_regs);
3358   /* Map of pseudo-register replacements.  */
3359   rtx *reg_map;
3360   int call_seen;
3361   rtx test;
3362   rtx end_insert_before;
3363   int loop_depth = 0;
3364
3365   reg_iv_type = (enum iv_mode *) alloca (max_reg_before_loop
3366                                          * sizeof (enum iv_mode *));
3367   bzero ((char *) reg_iv_type, max_reg_before_loop * sizeof (enum iv_mode *));
3368   reg_iv_info = (struct induction **)
3369     alloca (max_reg_before_loop * sizeof (struct induction *));
3370   bzero ((char *) reg_iv_info, (max_reg_before_loop
3371                                 * sizeof (struct induction *)));
3372   reg_biv_class = (struct iv_class **)
3373     alloca (max_reg_before_loop * sizeof (struct iv_class *));
3374   bzero ((char *) reg_biv_class, (max_reg_before_loop
3375                                   * sizeof (struct iv_class *)));
3376
3377   loop_iv_list = 0;
3378   addr_placeholder = gen_reg_rtx (Pmode);
3379
3380   /* Save insn immediately after the loop_end.  Insns inserted after loop_end
3381      must be put before this insn, so that they will appear in the right
3382      order (i.e. loop order). 
3383
3384      If loop_end is the end of the current function, then emit a 
3385      NOTE_INSN_DELETED after loop_end and set end_insert_before to the
3386      dummy note insn.  */
3387   if (NEXT_INSN (loop_end) != 0)
3388     end_insert_before = NEXT_INSN (loop_end);
3389   else
3390     end_insert_before = emit_note_after (NOTE_INSN_DELETED, loop_end);
3391
3392   /* Scan through loop to find all possible bivs.  */
3393
3394   p = scan_start;
3395   while (1)
3396     {
3397       p = NEXT_INSN (p);
3398       /* At end of a straight-in loop, we are done.
3399          At end of a loop entered at the bottom, scan the top.  */
3400       if (p == scan_start)
3401         break;
3402       if (p == end)
3403         {
3404           if (loop_top != 0)
3405             p = loop_top;
3406           else
3407             break;
3408           if (p == scan_start)
3409             break;
3410         }
3411
3412       if (GET_CODE (p) == INSN
3413           && (set = single_set (p))
3414           && GET_CODE (SET_DEST (set)) == REG)
3415         {
3416           dest_reg = SET_DEST (set);
3417           if (REGNO (dest_reg) < max_reg_before_loop
3418               && REGNO (dest_reg) >= FIRST_PSEUDO_REGISTER
3419               && reg_iv_type[REGNO (dest_reg)] != NOT_BASIC_INDUCT)
3420             {
3421               if (basic_induction_var (SET_SRC (set), GET_MODE (SET_SRC (set)),
3422                                        dest_reg, p, &inc_val, &mult_val))
3423                 {
3424                   /* It is a possible basic induction variable.
3425                      Create and initialize an induction structure for it.  */
3426
3427                   struct induction *v
3428                     = (struct induction *) alloca (sizeof (struct induction));
3429
3430                   record_biv (v, p, dest_reg, inc_val, mult_val,
3431                               not_every_iteration, maybe_multiple);
3432                   reg_iv_type[REGNO (dest_reg)] = BASIC_INDUCT;
3433                 }
3434               else if (REGNO (dest_reg) < max_reg_before_loop)
3435                 reg_iv_type[REGNO (dest_reg)] = NOT_BASIC_INDUCT;
3436             }
3437         }
3438
3439       /* Past CODE_LABEL, we get to insns that may be executed multiple
3440          times.  The only way we can be sure that they can't is if every
3441          every jump insn between here and the end of the loop either
3442          returns, exits the loop, is a forward jump, or is a jump
3443          to the loop start.  */
3444
3445       if (GET_CODE (p) == CODE_LABEL)
3446         {
3447           rtx insn = p;
3448
3449           maybe_multiple = 0;
3450
3451           while (1)
3452             {
3453               insn = NEXT_INSN (insn);
3454               if (insn == scan_start)
3455                 break;
3456               if (insn == end)
3457                 {
3458                   if (loop_top != 0)
3459                     insn = loop_top;
3460                   else
3461                     break;
3462                   if (insn == scan_start)
3463                     break;
3464                 }
3465
3466               if (GET_CODE (insn) == JUMP_INSN
3467                   && GET_CODE (PATTERN (insn)) != RETURN
3468                   && (! condjump_p (insn)
3469                       || (JUMP_LABEL (insn) != 0
3470                           && JUMP_LABEL (insn) != scan_start
3471                           && (INSN_UID (JUMP_LABEL (insn)) >= max_uid_for_loop
3472                               || INSN_UID (insn) >= max_uid_for_loop
3473                               || (INSN_LUID (JUMP_LABEL (insn))
3474                                   < INSN_LUID (insn))))))
3475                 {
3476                   maybe_multiple = 1;
3477                   break;
3478                 }
3479             }
3480         }
3481
3482       /* Past a jump, we get to insns for which we can't count
3483          on whether they will be executed during each iteration.  */
3484       /* This code appears twice in strength_reduce.  There is also similar
3485          code in scan_loop.  */
3486       if (GET_CODE (p) == JUMP_INSN
3487           /* If we enter the loop in the middle, and scan around to the
3488              beginning, don't set not_every_iteration for that.
3489              This can be any kind of jump, since we want to know if insns
3490              will be executed if the loop is executed.  */
3491           && ! (JUMP_LABEL (p) == loop_top
3492                 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3493                     || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3494         {
3495           rtx label = 0;
3496
3497           /* If this is a jump outside the loop, then it also doesn't
3498              matter.  Check to see if the target of this branch is on the
3499              loop_number_exits_labels list.  */
3500              
3501           for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3502                label;
3503                label = LABEL_NEXTREF (label))
3504             if (XEXP (label, 0) == JUMP_LABEL (p))
3505               break;
3506
3507           if (! label)
3508             not_every_iteration = 1;
3509         }
3510
3511       else if (GET_CODE (p) == NOTE)
3512         {
3513           /* At the virtual top of a converted loop, insns are again known to
3514              be executed each iteration: logically, the loop begins here
3515              even though the exit code has been duplicated.  */
3516           if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3517             not_every_iteration = 0;
3518           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3519             loop_depth++;
3520           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3521             loop_depth--;
3522         }
3523
3524       /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3525          an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3526          or not an insn is known to be executed each iteration of the
3527          loop, whether or not any iterations are known to occur.
3528
3529          Therefore, if we have just passed a label and have no more labels
3530          between here and the test insn of the loop, we know these insns
3531          will be executed each iteration.  */
3532
3533       if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3534           && no_labels_between_p (p, loop_end))
3535         not_every_iteration = 0;
3536     }
3537
3538   /* Scan loop_iv_list to remove all regs that proved not to be bivs.
3539      Make a sanity check against n_times_set.  */
3540   for (backbl = &loop_iv_list, bl = *backbl; bl; bl = bl->next)
3541     {
3542       if (reg_iv_type[bl->regno] != BASIC_INDUCT
3543           /* Above happens if register modified by subreg, etc.  */
3544           /* Make sure it is not recognized as a basic induction var: */
3545           || n_times_set[bl->regno] != bl->biv_count
3546           /* If never incremented, it is invariant that we decided not to
3547              move.  So leave it alone.  */
3548           || ! bl->incremented)
3549         {
3550           if (loop_dump_stream)
3551             fprintf (loop_dump_stream, "Reg %d: biv discarded, %s\n",
3552                      bl->regno,
3553                      (reg_iv_type[bl->regno] != BASIC_INDUCT
3554                       ? "not induction variable"
3555                       : (! bl->incremented ? "never incremented"
3556                          : "count error")));
3557           
3558           reg_iv_type[bl->regno] = NOT_BASIC_INDUCT;
3559           *backbl = bl->next;
3560         }
3561       else
3562         {
3563           backbl = &bl->next;
3564
3565           if (loop_dump_stream)
3566             fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
3567         }
3568     }
3569
3570   /* Exit if there are no bivs.  */
3571   if (! loop_iv_list)
3572     {
3573       /* Can still unroll the loop anyways, but indicate that there is no
3574          strength reduction info available.  */
3575       if (flag_unroll_loops)
3576         unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 0);
3577
3578       return;
3579     }
3580
3581   /* Find initial value for each biv by searching backwards from loop_start,
3582      halting at first label.  Also record any test condition.  */
3583
3584   call_seen = 0;
3585   for (p = loop_start; p && GET_CODE (p) != CODE_LABEL; p = PREV_INSN (p))
3586     {
3587       note_insn = p;
3588
3589       if (GET_CODE (p) == CALL_INSN)
3590         call_seen = 1;
3591
3592       if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3593           || GET_CODE (p) == CALL_INSN)
3594         note_stores (PATTERN (p), record_initial);
3595
3596       /* Record any test of a biv that branches around the loop if no store
3597          between it and the start of loop.  We only care about tests with
3598          constants and registers and only certain of those.  */
3599       if (GET_CODE (p) == JUMP_INSN
3600           && JUMP_LABEL (p) != 0
3601           && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end)
3602           && (test = get_condition_for_loop (p)) != 0
3603           && GET_CODE (XEXP (test, 0)) == REG
3604           && REGNO (XEXP (test, 0)) < max_reg_before_loop
3605           && (bl = reg_biv_class[REGNO (XEXP (test, 0))]) != 0
3606           && valid_initial_value_p (XEXP (test, 1), p, call_seen, loop_start)
3607           && bl->init_insn == 0)
3608         {
3609           /* If an NE test, we have an initial value!  */
3610           if (GET_CODE (test) == NE)
3611             {
3612               bl->init_insn = p;
3613               bl->init_set = gen_rtx (SET, VOIDmode,
3614                                       XEXP (test, 0), XEXP (test, 1));
3615             }
3616           else
3617             bl->initial_test = test;
3618         }
3619     }
3620
3621   /* Look at the each biv and see if we can say anything better about its
3622      initial value from any initializing insns set up above.  (This is done
3623      in two passes to avoid missing SETs in a PARALLEL.)  */
3624   for (bl = loop_iv_list; bl; bl = bl->next)
3625     {
3626       rtx src;
3627
3628       if (! bl->init_insn)
3629         continue;
3630
3631       src = SET_SRC (bl->init_set);
3632
3633       if (loop_dump_stream)
3634         fprintf (loop_dump_stream,
3635                  "Biv %d initialized at insn %d: initial value ",
3636                  bl->regno, INSN_UID (bl->init_insn));
3637
3638       if ((GET_MODE (src) == GET_MODE (regno_reg_rtx[bl->regno])
3639            || GET_MODE (src) == VOIDmode)
3640           && valid_initial_value_p (src, bl->init_insn, call_seen, loop_start))
3641         {
3642           bl->initial_value = src;
3643
3644           if (loop_dump_stream)
3645             {
3646               if (GET_CODE (src) == CONST_INT)
3647                 fprintf (loop_dump_stream, "%d\n", INTVAL (src));
3648               else
3649                 {
3650                   print_rtl (loop_dump_stream, src);
3651                   fprintf (loop_dump_stream, "\n");
3652                 }
3653             }
3654         }
3655       else
3656         {
3657           /* Biv initial value is not simple move,
3658              so let it keep initial value of "itself".  */
3659
3660           if (loop_dump_stream)
3661             fprintf (loop_dump_stream, "is complex\n");
3662         }
3663     }
3664
3665   /* Search the loop for general induction variables.  */
3666
3667   /* A register is a giv if: it is only set once, it is a function of a
3668      biv and a constant (or invariant), and it is not a biv.  */
3669
3670   not_every_iteration = 0;
3671   loop_depth = 0;
3672   p = scan_start;
3673   while (1)
3674     {
3675       p = NEXT_INSN (p);
3676       /* At end of a straight-in loop, we are done.
3677          At end of a loop entered at the bottom, scan the top.  */
3678       if (p == scan_start)
3679         break;
3680       if (p == end)
3681         {
3682           if (loop_top != 0)
3683             p = loop_top;
3684           else
3685             break;
3686           if (p == scan_start)
3687             break;
3688         }
3689
3690       /* Look for a general induction variable in a register.  */
3691       if (GET_CODE (p) == INSN
3692           && (set = single_set (p))
3693           && GET_CODE (SET_DEST (set)) == REG
3694           && ! may_not_optimize[REGNO (SET_DEST (set))])
3695         {
3696           rtx src_reg;
3697           rtx add_val;
3698           rtx mult_val;
3699           int benefit;
3700           rtx regnote = 0;
3701
3702           dest_reg = SET_DEST (set);
3703           if (REGNO (dest_reg) < FIRST_PSEUDO_REGISTER)
3704             continue;
3705
3706           if (/* SET_SRC is a giv.  */
3707               ((benefit = general_induction_var (SET_SRC (set),
3708                                                  &src_reg, &add_val,
3709                                                  &mult_val))
3710                /* Equivalent expression is a giv.  */
3711                || ((regnote = find_reg_note (p, REG_EQUAL, NULL_RTX))
3712                    && (benefit = general_induction_var (XEXP (regnote, 0),
3713                                                         &src_reg,
3714                                                         &add_val, &mult_val))))
3715               /* Don't try to handle any regs made by loop optimization.
3716                  We have nothing on them in regno_first_uid, etc.  */
3717               && REGNO (dest_reg) < max_reg_before_loop
3718               /* Don't recognize a BASIC_INDUCT_VAR here.  */
3719               && dest_reg != src_reg
3720               /* This must be the only place where the register is set.  */
3721               && (n_times_set[REGNO (dest_reg)] == 1
3722                   /* or all sets must be consecutive and make a giv.  */
3723                   || (benefit = consec_sets_giv (benefit, p,
3724                                                  src_reg, dest_reg,
3725                                                  &add_val, &mult_val))))
3726             {
3727               int count;
3728               struct induction *v
3729                 = (struct induction *) alloca (sizeof (struct induction));
3730               rtx temp;
3731
3732               /* If this is a library call, increase benefit.  */
3733               if (find_reg_note (p, REG_RETVAL, NULL_RTX))
3734                 benefit += libcall_benefit (p);
3735
3736               /* Skip the consecutive insns, if there are any.  */
3737               for (count = n_times_set[REGNO (dest_reg)] - 1;
3738                    count > 0; count--)
3739                 {
3740                   /* If first insn of libcall sequence, skip to end.
3741                      Do this at start of loop, since INSN is guaranteed to
3742                      be an insn here.  */
3743                   if (GET_CODE (p) != NOTE
3744                       && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
3745                     p = XEXP (temp, 0);
3746
3747                   do p = NEXT_INSN (p);
3748                   while (GET_CODE (p) == NOTE);
3749                 }
3750
3751               record_giv (v, p, src_reg, dest_reg, mult_val, add_val, benefit,
3752                           DEST_REG, not_every_iteration, NULL_PTR, loop_start,
3753                           loop_end);
3754
3755             }
3756         }
3757
3758 #ifndef DONT_REDUCE_ADDR
3759       /* Look for givs which are memory addresses.  */
3760       /* This resulted in worse code on a VAX 8600.  I wonder if it
3761          still does.  */
3762       if (GET_CODE (p) == INSN)
3763         find_mem_givs (PATTERN (p), p, not_every_iteration, loop_start,
3764                        loop_end);
3765 #endif
3766
3767       /* Update the status of whether giv can derive other givs.  This can
3768          change when we pass a label or an insn that updates a biv.  */
3769       if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
3770         || GET_CODE (p) == CODE_LABEL)
3771         update_giv_derive (p);
3772
3773       /* Past a jump, we get to insns for which we can't count
3774          on whether they will be executed during each iteration.  */
3775       /* This code appears twice in strength_reduce.  There is also similar
3776          code in scan_loop.  */
3777       if (GET_CODE (p) == JUMP_INSN
3778           /* If we enter the loop in the middle, and scan around to the
3779              beginning, don't set not_every_iteration for that.
3780              This can be any kind of jump, since we want to know if insns
3781              will be executed if the loop is executed.  */
3782           && ! (JUMP_LABEL (p) == loop_top
3783                 && ((NEXT_INSN (NEXT_INSN (p)) == loop_end && simplejump_p (p))
3784                     || (NEXT_INSN (p) == loop_end && condjump_p (p)))))
3785         {
3786           rtx label = 0;
3787
3788           /* If this is a jump outside the loop, then it also doesn't
3789              matter.  Check to see if the target of this branch is on the
3790              loop_number_exits_labels list.  */
3791              
3792           for (label = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
3793                label;
3794                label = LABEL_NEXTREF (label))
3795             if (XEXP (label, 0) == JUMP_LABEL (p))
3796               break;
3797
3798           if (! label)
3799             not_every_iteration = 1;
3800         }
3801
3802       else if (GET_CODE (p) == NOTE)
3803         {
3804           /* At the virtual top of a converted loop, insns are again known to
3805              be executed each iteration: logically, the loop begins here
3806              even though the exit code has been duplicated.  */
3807           if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_VTOP && loop_depth == 0)
3808             not_every_iteration = 0;
3809           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_BEG)
3810             loop_depth++;
3811           else if (NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
3812             loop_depth--;
3813         }
3814
3815       /* Unlike in the code motion pass where MAYBE_NEVER indicates that
3816          an insn may never be executed, NOT_EVERY_ITERATION indicates whether
3817          or not an insn is known to be executed each iteration of the
3818          loop, whether or not any iterations are known to occur.
3819
3820          Therefore, if we have just passed a label and have no more labels
3821          between here and the test insn of the loop, we know these insns
3822          will be executed each iteration.  */
3823
3824       if (not_every_iteration && GET_CODE (p) == CODE_LABEL
3825           && no_labels_between_p (p, loop_end))
3826         not_every_iteration = 0;
3827     }
3828
3829   /* Try to calculate and save the number of loop iterations.  This is
3830      set to zero if the actual number can not be calculated.  This must
3831      be called after all giv's have been identified, since otherwise it may
3832      fail if the iteration variable is a giv.  */
3833
3834   loop_n_iterations = loop_iterations (loop_start, loop_end);
3835
3836   /* Now for each giv for which we still don't know whether or not it is
3837      replaceable, check to see if it is replaceable because its final value
3838      can be calculated.  This must be done after loop_iterations is called,
3839      so that final_giv_value will work correctly.  */
3840
3841   for (bl = loop_iv_list; bl; bl = bl->next)
3842     {
3843       struct induction *v;
3844
3845       for (v = bl->giv; v; v = v->next_iv)
3846         if (! v->replaceable && ! v->not_replaceable)
3847           check_final_value (v, loop_start, loop_end);
3848     }
3849
3850   /* Try to prove that the loop counter variable (if any) is always
3851      nonnegative; if so, record that fact with a REG_NONNEG note
3852      so that "decrement and branch until zero" insn can be used.  */
3853   check_dbra_loop (loop_end, insn_count, loop_start);
3854
3855 #ifdef HAIFA
3856   /* record loop-variables relevant for BCT optimization before unrolling
3857      the loop.  Unrolling may update part of this information, and the
3858      correct data will be used for generating the BCT.  */
3859 #ifdef HAVE_decrement_and_branch_on_count
3860   if (HAVE_decrement_and_branch_on_count)
3861     analyze_loop_iterations (loop_start, loop_end);
3862 #endif
3863 #endif  /* HAIFA */
3864
3865   /* Create reg_map to hold substitutions for replaceable giv regs.  */
3866   reg_map = (rtx *) alloca (max_reg_before_loop * sizeof (rtx));
3867   bzero ((char *) reg_map, max_reg_before_loop * sizeof (rtx));
3868
3869   /* Examine each iv class for feasibility of strength reduction/induction
3870      variable elimination.  */
3871
3872   for (bl = loop_iv_list; bl; bl = bl->next)
3873     {
3874       struct induction *v;
3875       int benefit;
3876       int all_reduced;
3877       rtx final_value = 0;
3878
3879       /* Test whether it will be possible to eliminate this biv
3880          provided all givs are reduced.  This is possible if either
3881          the reg is not used outside the loop, or we can compute
3882          what its final value will be.
3883
3884          For architectures with a decrement_and_branch_until_zero insn,
3885          don't do this if we put a REG_NONNEG note on the endtest for
3886          this biv.  */
3887
3888       /* Compare against bl->init_insn rather than loop_start.
3889          We aren't concerned with any uses of the biv between
3890          init_insn and loop_start since these won't be affected
3891          by the value of the biv elsewhere in the function, so
3892          long as init_insn doesn't use the biv itself.
3893          March 14, 1989 -- self@bayes.arc.nasa.gov */
3894
3895       if ((uid_luid[REGNO_LAST_UID (bl->regno)] < INSN_LUID (loop_end)
3896            && bl->init_insn
3897            && INSN_UID (bl->init_insn) < max_uid_for_loop
3898            && uid_luid[REGNO_FIRST_UID (bl->regno)] >= INSN_LUID (bl->init_insn)
3899 #ifdef HAVE_decrement_and_branch_until_zero
3900            && ! bl->nonneg
3901 #endif
3902            && ! reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
3903           || ((final_value = final_biv_value (bl, loop_start, loop_end))
3904 #ifdef HAVE_decrement_and_branch_until_zero
3905               && ! bl->nonneg
3906 #endif
3907               ))
3908         bl->eliminable = maybe_eliminate_biv (bl, loop_start, end, 0,
3909                                               threshold, insn_count);
3910       else
3911         {
3912           if (loop_dump_stream)
3913             {
3914               fprintf (loop_dump_stream,
3915                        "Cannot eliminate biv %d.\n",
3916                        bl->regno);
3917               fprintf (loop_dump_stream,
3918                        "First use: insn %d, last use: insn %d.\n",
3919                        REGNO_FIRST_UID (bl->regno),
3920                        REGNO_LAST_UID (bl->regno));
3921             }
3922         }
3923
3924       /* Combine all giv's for this iv_class.  */
3925       combine_givs (bl);
3926
3927       /* This will be true at the end, if all givs which depend on this
3928          biv have been strength reduced.
3929          We can't (currently) eliminate the biv unless this is so.  */
3930       all_reduced = 1;
3931
3932       /* Check each giv in this class to see if we will benefit by reducing
3933          it.  Skip giv's combined with others.  */
3934       for (v = bl->giv; v; v = v->next_iv)
3935         {
3936           struct induction *tv;
3937
3938           if (v->ignore || v->same)
3939             continue;
3940
3941           benefit = v->benefit;
3942
3943           /* Reduce benefit if not replaceable, since we will insert
3944              a move-insn to replace the insn that calculates this giv.
3945              Don't do this unless the giv is a user variable, since it
3946              will often be marked non-replaceable because of the duplication
3947              of the exit code outside the loop.  In such a case, the copies
3948              we insert are dead and will be deleted.  So they don't have
3949              a cost.  Similar situations exist.  */
3950           /* ??? The new final_[bg]iv_value code does a much better job
3951              of finding replaceable giv's, and hence this code may no longer
3952              be necessary.  */
3953           if (! v->replaceable && ! bl->eliminable
3954               && REG_USERVAR_P (v->dest_reg))
3955             benefit -= copy_cost;
3956
3957           /* Decrease the benefit to count the add-insns that we will
3958              insert to increment the reduced reg for the giv.  */
3959           benefit -= add_cost * bl->biv_count;
3960
3961           /* Decide whether to strength-reduce this giv or to leave the code
3962              unchanged (recompute it from the biv each time it is used).
3963              This decision can be made independently for each giv.  */
3964
3965 #ifdef AUTO_INC_DEC
3966           /* Attempt to guess whether autoincrement will handle some of the
3967              new add insns; if so, increase BENEFIT (undo the subtraction of
3968              add_cost that was done above).  */
3969           if (v->giv_type == DEST_ADDR
3970               && GET_CODE (v->mult_val) == CONST_INT)
3971             {
3972 #if defined (HAVE_POST_INCREMENT) || defined (HAVE_PRE_INCREMENT)
3973               if (INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3974                 benefit += add_cost * bl->biv_count;
3975 #endif
3976 #if defined (HAVE_POST_DECREMENT) || defined (HAVE_PRE_DECREMENT)
3977               if (-INTVAL (v->mult_val) == GET_MODE_SIZE (v->mem_mode))
3978                 benefit += add_cost * bl->biv_count;
3979 #endif
3980             }
3981 #endif
3982
3983           /* If an insn is not to be strength reduced, then set its ignore
3984              flag, and clear all_reduced.  */
3985
3986           /* A giv that depends on a reversed biv must be reduced if it is
3987              used after the loop exit, otherwise, it would have the wrong
3988              value after the loop exit.  To make it simple, just reduce all
3989              of such giv's whether or not we know they are used after the loop
3990              exit.  */
3991
3992           if (v->lifetime * threshold * benefit < insn_count
3993               && ! bl->reversed)
3994             {
3995               if (loop_dump_stream)
3996                 fprintf (loop_dump_stream,
3997                          "giv of insn %d not worth while, %d vs %d.\n",
3998                          INSN_UID (v->insn),
3999                          v->lifetime * threshold * benefit, insn_count);
4000               v->ignore = 1;
4001               all_reduced = 0;
4002             }
4003           else
4004             {
4005               /* Check that we can increment the reduced giv without a
4006                  multiply insn.  If not, reject it.  */
4007
4008               for (tv = bl->biv; tv; tv = tv->next_iv)
4009                 if (tv->mult_val == const1_rtx
4010                     && ! product_cheap_p (tv->add_val, v->mult_val))
4011                   {
4012                     if (loop_dump_stream)
4013                       fprintf (loop_dump_stream,
4014                                "giv of insn %d: would need a multiply.\n",
4015                                INSN_UID (v->insn));
4016                     v->ignore = 1;
4017                     all_reduced = 0;
4018                     break;
4019                   }
4020             }
4021         }
4022
4023       /* Reduce each giv that we decided to reduce.  */
4024
4025       for (v = bl->giv; v; v = v->next_iv)
4026         {
4027           struct induction *tv;
4028           if (! v->ignore && v->same == 0)
4029             {
4030               int auto_inc_opt = 0;
4031
4032               v->new_reg = gen_reg_rtx (v->mode);
4033
4034 #ifdef AUTO_INC_DEC
4035               /* If the target has auto-increment addressing modes, and
4036                  this is an address giv, then try to put the increment
4037                  immediately after its use, so that flow can create an
4038                  auto-increment addressing mode.  */
4039               if (v->giv_type == DEST_ADDR && bl->biv_count == 1
4040                   && bl->biv->always_executed && ! bl->biv->maybe_multiple
4041                   /* We don't handle reversed biv's because bl->biv->insn
4042                      does not have a valid INSN_LUID.  */
4043                   && ! bl->reversed
4044                   && v->always_executed && ! v->maybe_multiple)
4045                 {
4046                   /* If other giv's have been combined with this one, then
4047                      this will work only if all uses of the other giv's occur
4048                      before this giv's insn.  This is difficult to check.
4049
4050                      We simplify this by looking for the common case where
4051                      there is one DEST_REG giv, and this giv's insn is the
4052                      last use of the dest_reg of that DEST_REG giv.  If the
4053                      the increment occurs after the address giv, then we can
4054                      perform the optimization.  (Otherwise, the increment
4055                      would have to go before other_giv, and we would not be
4056                      able to combine it with the address giv to get an
4057                      auto-inc address.)  */
4058                   if (v->combined_with)
4059                     {
4060                       struct induction *other_giv = 0;
4061
4062                       for (tv = bl->giv; tv; tv = tv->next_iv)
4063                         if (tv->same == v)
4064                           {
4065                             if (other_giv)
4066                               break;
4067                             else
4068                               other_giv = tv;
4069                           }
4070                       if (! tv && other_giv
4071                           && REGNO (other_giv->dest_reg) <= max_reg_before_loop
4072                           && (REGNO_LAST_UID (REGNO (other_giv->dest_reg))
4073                               == INSN_UID (v->insn))
4074                           && INSN_LUID (v->insn) < INSN_LUID (bl->biv->insn))
4075                         auto_inc_opt = 1;
4076                     }
4077                   /* Check for case where increment is before the the address
4078                      giv.  */
4079                   else if (INSN_LUID (v->insn) > INSN_LUID (bl->biv->insn))
4080                     auto_inc_opt = -1;
4081                   else
4082                     auto_inc_opt = 1;
4083
4084 #ifdef HAVE_cc0
4085                   {
4086                     rtx prev;
4087
4088                     /* We can't put an insn immediately after one setting
4089                        cc0, or immediately before one using cc0.  */
4090                     if ((auto_inc_opt == 1 && sets_cc0_p (PATTERN (v->insn)))
4091                         || (auto_inc_opt == -1
4092                             && (prev = prev_nonnote_insn (v->insn)) != 0
4093                             && GET_RTX_CLASS (GET_CODE (prev)) == 'i'
4094                             && sets_cc0_p (PATTERN (prev))))
4095                       auto_inc_opt = 0;
4096                   }
4097 #endif
4098
4099                   if (auto_inc_opt)
4100                     v->auto_inc_opt = 1;
4101                 }
4102 #endif
4103
4104               /* For each place where the biv is incremented, add an insn
4105                  to increment the new, reduced reg for the giv.  */
4106               for (tv = bl->biv; tv; tv = tv->next_iv)
4107                 {
4108                   rtx insert_before;
4109
4110                   if (! auto_inc_opt)
4111                     insert_before = tv->insn;
4112                   else if (auto_inc_opt == 1)
4113                     insert_before = NEXT_INSN (v->insn);
4114                   else
4115                     insert_before = v->insn;
4116
4117                   if (tv->mult_val == const1_rtx)
4118                     emit_iv_add_mult (tv->add_val, v->mult_val,
4119                                       v->new_reg, v->new_reg, insert_before);
4120                   else /* tv->mult_val == const0_rtx */
4121                     /* A multiply is acceptable here
4122                        since this is presumed to be seldom executed.  */
4123                     emit_iv_add_mult (tv->add_val, v->mult_val,
4124                                       v->add_val, v->new_reg, insert_before);
4125                 }
4126
4127               /* Add code at loop start to initialize giv's reduced reg.  */
4128
4129               emit_iv_add_mult (bl->initial_value, v->mult_val,
4130                                 v->add_val, v->new_reg, loop_start);
4131             }
4132         }
4133
4134       /* Rescan all givs.  If a giv is the same as a giv not reduced, mark it
4135          as not reduced.
4136          
4137          For each giv register that can be reduced now: if replaceable,
4138          substitute reduced reg wherever the old giv occurs;
4139          else add new move insn "giv_reg = reduced_reg".
4140
4141          Also check for givs whose first use is their definition and whose
4142          last use is the definition of another giv.  If so, it is likely
4143          dead and should not be used to eliminate a biv.  */
4144       for (v = bl->giv; v; v = v->next_iv)
4145         {
4146           if (v->same && v->same->ignore)
4147             v->ignore = 1;
4148
4149           if (v->ignore)
4150             continue;
4151
4152           if (v->giv_type == DEST_REG
4153               && REGNO_FIRST_UID (REGNO (v->dest_reg)) == INSN_UID (v->insn))
4154             {
4155               struct induction *v1;
4156
4157               for (v1 = bl->giv; v1; v1 = v1->next_iv)
4158                 if (REGNO_LAST_UID (REGNO (v->dest_reg)) == INSN_UID (v1->insn))
4159                   v->maybe_dead = 1;
4160             }
4161
4162           /* Update expression if this was combined, in case other giv was
4163              replaced.  */
4164           if (v->same)
4165             v->new_reg = replace_rtx (v->new_reg,
4166                                       v->same->dest_reg, v->same->new_reg);
4167
4168           if (v->giv_type == DEST_ADDR)
4169             /* Store reduced reg as the address in the memref where we found
4170                this giv.  */
4171             validate_change (v->insn, v->location, v->new_reg, 0);
4172           else if (v->replaceable)
4173             {
4174               reg_map[REGNO (v->dest_reg)] = v->new_reg;
4175
4176 #if 0
4177               /* I can no longer duplicate the original problem.  Perhaps
4178                  this is unnecessary now?  */
4179
4180               /* Replaceable; it isn't strictly necessary to delete the old
4181                  insn and emit a new one, because v->dest_reg is now dead.
4182
4183                  However, especially when unrolling loops, the special
4184                  handling for (set REG0 REG1) in the second cse pass may
4185                  make v->dest_reg live again.  To avoid this problem, emit
4186                  an insn to set the original giv reg from the reduced giv.
4187                  We can not delete the original insn, since it may be part
4188                  of a LIBCALL, and the code in flow that eliminates dead
4189                  libcalls will fail if it is deleted.  */
4190               emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4191                                v->insn);
4192 #endif
4193             }
4194           else
4195             {
4196               /* Not replaceable; emit an insn to set the original giv reg from
4197                  the reduced giv, same as above.  */
4198               emit_insn_after (gen_move_insn (v->dest_reg, v->new_reg),
4199                                v->insn);
4200             }
4201
4202           /* When a loop is reversed, givs which depend on the reversed
4203              biv, and which are live outside the loop, must be set to their
4204              correct final value.  This insn is only needed if the giv is
4205              not replaceable.  The correct final value is the same as the
4206              value that the giv starts the reversed loop with.  */
4207           if (bl->reversed && ! v->replaceable)
4208             emit_iv_add_mult (bl->initial_value, v->mult_val,
4209                               v->add_val, v->dest_reg, end_insert_before);
4210           else if (v->final_value)
4211             {
4212               rtx insert_before;
4213
4214               /* If the loop has multiple exits, emit the insn before the
4215                  loop to ensure that it will always be executed no matter
4216                  how the loop exits.  Otherwise, emit the insn after the loop,
4217                  since this is slightly more efficient.  */
4218               if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4219                 insert_before = loop_start;
4220               else
4221                 insert_before = end_insert_before;
4222               emit_insn_before (gen_move_insn (v->dest_reg, v->final_value),
4223                                 insert_before);
4224
4225 #if 0
4226               /* If the insn to set the final value of the giv was emitted
4227                  before the loop, then we must delete the insn inside the loop
4228                  that sets it.  If this is a LIBCALL, then we must delete
4229                  every insn in the libcall.  Note, however, that
4230                  final_giv_value will only succeed when there are multiple
4231                  exits if the giv is dead at each exit, hence it does not
4232                  matter that the original insn remains because it is dead
4233                  anyways.  */
4234               /* Delete the insn inside the loop that sets the giv since
4235                  the giv is now set before (or after) the loop.  */
4236               delete_insn (v->insn);
4237 #endif
4238             }
4239
4240           if (loop_dump_stream)
4241             {
4242               fprintf (loop_dump_stream, "giv at %d reduced to ",
4243                        INSN_UID (v->insn));
4244               print_rtl (loop_dump_stream, v->new_reg);
4245               fprintf (loop_dump_stream, "\n");
4246             }
4247         }
4248
4249       /* All the givs based on the biv bl have been reduced if they
4250          merit it.  */
4251
4252       /* For each giv not marked as maybe dead that has been combined with a
4253          second giv, clear any "maybe dead" mark on that second giv.
4254          v->new_reg will either be or refer to the register of the giv it
4255          combined with.
4256
4257          Doing this clearing avoids problems in biv elimination where a
4258          giv's new_reg is a complex value that can't be put in the insn but
4259          the giv combined with (with a reg as new_reg) is marked maybe_dead.
4260          Since the register will be used in either case, we'd prefer it be
4261          used from the simpler giv.  */
4262
4263       for (v = bl->giv; v; v = v->next_iv)
4264         if (! v->maybe_dead && v->same)
4265           v->same->maybe_dead = 0;
4266
4267       /* Try to eliminate the biv, if it is a candidate.
4268          This won't work if ! all_reduced,
4269          since the givs we planned to use might not have been reduced.
4270
4271          We have to be careful that we didn't initially think we could eliminate
4272          this biv because of a giv that we now think may be dead and shouldn't
4273          be used as a biv replacement.  
4274
4275          Also, there is the possibility that we may have a giv that looks
4276          like it can be used to eliminate a biv, but the resulting insn
4277          isn't valid.  This can happen, for example, on the 88k, where a 
4278          JUMP_INSN can compare a register only with zero.  Attempts to
4279          replace it with a compare with a constant will fail.
4280
4281          Note that in cases where this call fails, we may have replaced some
4282          of the occurrences of the biv with a giv, but no harm was done in
4283          doing so in the rare cases where it can occur.  */
4284
4285       if (all_reduced == 1 && bl->eliminable
4286           && maybe_eliminate_biv (bl, loop_start, end, 1,
4287                                   threshold, insn_count))
4288
4289         {
4290           /* ?? If we created a new test to bypass the loop entirely,
4291              or otherwise drop straight in, based on this test, then
4292              we might want to rewrite it also.  This way some later
4293              pass has more hope of removing the initialization of this
4294              biv entirely.  */
4295
4296           /* If final_value != 0, then the biv may be used after loop end
4297              and we must emit an insn to set it just in case.
4298
4299              Reversed bivs already have an insn after the loop setting their
4300              value, so we don't need another one.  We can't calculate the
4301              proper final value for such a biv here anyways.  */
4302           if (final_value != 0 && ! bl->reversed)
4303             {
4304               rtx insert_before;
4305
4306               /* If the loop has multiple exits, emit the insn before the
4307                  loop to ensure that it will always be executed no matter
4308                  how the loop exits.  Otherwise, emit the insn after the
4309                  loop, since this is slightly more efficient.  */
4310               if (loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
4311                 insert_before = loop_start;
4312               else
4313                 insert_before = end_insert_before;
4314
4315               emit_insn_before (gen_move_insn (bl->biv->dest_reg, final_value),
4316                                 end_insert_before);
4317             }
4318
4319 #if 0
4320           /* Delete all of the instructions inside the loop which set
4321              the biv, as they are all dead.  If is safe to delete them,
4322              because an insn setting a biv will never be part of a libcall.  */
4323           /* However, deleting them will invalidate the regno_last_uid info,
4324              so keeping them around is more convenient.  Final_biv_value
4325              will only succeed when there are multiple exits if the biv
4326              is dead at each exit, hence it does not matter that the original
4327              insn remains, because it is dead anyways.  */
4328           for (v = bl->biv; v; v = v->next_iv)
4329             delete_insn (v->insn);
4330 #endif
4331
4332           if (loop_dump_stream)
4333             fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
4334                      bl->regno);
4335         }
4336     }
4337
4338   /* Go through all the instructions in the loop, making all the
4339      register substitutions scheduled in REG_MAP.  */
4340
4341   for (p = loop_start; p != end; p = NEXT_INSN (p))
4342     if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4343         || GET_CODE (p) == CALL_INSN)
4344       {
4345         replace_regs (PATTERN (p), reg_map, max_reg_before_loop, 0);
4346         replace_regs (REG_NOTES (p), reg_map, max_reg_before_loop, 0);
4347         INSN_CODE (p) = -1;
4348       }
4349
4350   /* Unroll loops from within strength reduction so that we can use the
4351      induction variable information that strength_reduce has already
4352      collected.  */
4353   
4354   if (flag_unroll_loops)
4355     unroll_loop (loop_end, insn_count, loop_start, end_insert_before, 1);
4356
4357 #ifdef HAIFA
4358   /* instrument the loop with bct insn */
4359 #ifdef HAVE_decrement_and_branch_on_count
4360   if (HAVE_decrement_and_branch_on_count)
4361     insert_bct (loop_start, loop_end);
4362 #endif
4363 #endif  /* HAIFA */
4364
4365   if (loop_dump_stream)
4366     fprintf (loop_dump_stream, "\n");
4367 }
4368 \f
4369 /* Return 1 if X is a valid source for an initial value (or as value being
4370    compared against in an initial test).
4371
4372    X must be either a register or constant and must not be clobbered between
4373    the current insn and the start of the loop.
4374
4375    INSN is the insn containing X.  */
4376
4377 static int
4378 valid_initial_value_p (x, insn, call_seen, loop_start)
4379      rtx x;
4380      rtx insn;
4381      int call_seen;
4382      rtx loop_start;
4383 {
4384   if (CONSTANT_P (x))
4385     return 1;
4386
4387   /* Only consider pseudos we know about initialized in insns whose luids
4388      we know.  */
4389   if (GET_CODE (x) != REG
4390       || REGNO (x) >= max_reg_before_loop)
4391     return 0;
4392
4393   /* Don't use call-clobbered registers across a call which clobbers it.  On
4394      some machines, don't use any hard registers at all.  */
4395   if (REGNO (x) < FIRST_PSEUDO_REGISTER
4396       && (
4397 #ifdef SMALL_REGISTER_CLASSES
4398           SMALL_REGISTER_CLASSES
4399 #else
4400           0
4401 #endif
4402             || (call_used_regs[REGNO (x)] && call_seen))
4403       )
4404     return 0;
4405
4406   /* Don't use registers that have been clobbered before the start of the
4407      loop.  */
4408   if (reg_set_between_p (x, insn, loop_start))
4409     return 0;
4410
4411   return 1;
4412 }
4413 \f
4414 /* Scan X for memory refs and check each memory address
4415    as a possible giv.  INSN is the insn whose pattern X comes from.
4416    NOT_EVERY_ITERATION is 1 if the insn might not be executed during
4417    every loop iteration.  */
4418
4419 static void
4420 find_mem_givs (x, insn, not_every_iteration, loop_start, loop_end)
4421      rtx x;
4422      rtx insn;
4423      int not_every_iteration;
4424      rtx loop_start, loop_end;
4425 {
4426   register int i, j;
4427   register enum rtx_code code;
4428   register char *fmt;
4429
4430   if (x == 0)
4431     return;
4432
4433   code = GET_CODE (x);
4434   switch (code)
4435     {
4436     case REG:
4437     case CONST_INT:
4438     case CONST:
4439     case CONST_DOUBLE:
4440     case SYMBOL_REF:
4441     case LABEL_REF:
4442     case PC:
4443     case CC0:
4444     case ADDR_VEC:
4445     case ADDR_DIFF_VEC:
4446     case USE:
4447     case CLOBBER:
4448       return;
4449
4450     case MEM:
4451       {
4452         rtx src_reg;
4453         rtx add_val;
4454         rtx mult_val;
4455         int benefit;
4456
4457         benefit = general_induction_var (XEXP (x, 0),
4458                                          &src_reg, &add_val, &mult_val);
4459
4460         /* Don't make a DEST_ADDR giv with mult_val == 1 && add_val == 0.
4461            Such a giv isn't useful.  */
4462         if (benefit > 0 && (mult_val != const1_rtx || add_val != const0_rtx))
4463           {
4464             /* Found one; record it.  */
4465             struct induction *v
4466               = (struct induction *) oballoc (sizeof (struct induction));
4467
4468             record_giv (v, insn, src_reg, addr_placeholder, mult_val,
4469                         add_val, benefit, DEST_ADDR, not_every_iteration,
4470                         &XEXP (x, 0), loop_start, loop_end);
4471
4472             v->mem_mode = GET_MODE (x);
4473           }
4474         return;
4475       }
4476     }
4477
4478   /* Recursively scan the subexpressions for other mem refs.  */
4479
4480   fmt = GET_RTX_FORMAT (code);
4481   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4482     if (fmt[i] == 'e')
4483       find_mem_givs (XEXP (x, i), insn, not_every_iteration, loop_start,
4484                      loop_end);
4485     else if (fmt[i] == 'E')
4486       for (j = 0; j < XVECLEN (x, i); j++)
4487         find_mem_givs (XVECEXP (x, i, j), insn, not_every_iteration,
4488                        loop_start, loop_end);
4489 }
4490 \f
4491 /* Fill in the data about one biv update.
4492    V is the `struct induction' in which we record the biv.  (It is
4493    allocated by the caller, with alloca.)
4494    INSN is the insn that sets it.
4495    DEST_REG is the biv's reg.
4496
4497    MULT_VAL is const1_rtx if the biv is being incremented here, in which case
4498    INC_VAL is the increment.  Otherwise, MULT_VAL is const0_rtx and the biv is
4499    being set to INC_VAL.
4500
4501    NOT_EVERY_ITERATION is nonzero if this biv update is not know to be
4502    executed every iteration; MAYBE_MULTIPLE is nonzero if this biv update
4503    can be executed more than once per iteration.  If MAYBE_MULTIPLE
4504    and NOT_EVERY_ITERATION are both zero, we know that the biv update is
4505    executed exactly once per iteration.  */
4506
4507 static void
4508 record_biv (v, insn, dest_reg, inc_val, mult_val,
4509             not_every_iteration, maybe_multiple)
4510      struct induction *v;
4511      rtx insn;
4512      rtx dest_reg;
4513      rtx inc_val;
4514      rtx mult_val;
4515      int not_every_iteration;
4516      int maybe_multiple;
4517 {
4518   struct iv_class *bl;
4519
4520   v->insn = insn;
4521   v->src_reg = dest_reg;
4522   v->dest_reg = dest_reg;
4523   v->mult_val = mult_val;
4524   v->add_val = inc_val;
4525   v->mode = GET_MODE (dest_reg);
4526   v->always_computable = ! not_every_iteration;
4527   v->always_executed = ! not_every_iteration;
4528   v->maybe_multiple = maybe_multiple;
4529
4530   /* Add this to the reg's iv_class, creating a class
4531      if this is the first incrementation of the reg.  */
4532
4533   bl = reg_biv_class[REGNO (dest_reg)];
4534   if (bl == 0)
4535     {
4536       /* Create and initialize new iv_class.  */
4537
4538       bl = (struct iv_class *) oballoc (sizeof (struct iv_class));
4539
4540       bl->regno = REGNO (dest_reg);
4541       bl->biv = 0;
4542       bl->giv = 0;
4543       bl->biv_count = 0;
4544       bl->giv_count = 0;
4545
4546       /* Set initial value to the reg itself.  */
4547       bl->initial_value = dest_reg;
4548       /* We haven't seen the initializing insn yet */
4549       bl->init_insn = 0;
4550       bl->init_set = 0;
4551       bl->initial_test = 0;
4552       bl->incremented = 0;
4553       bl->eliminable = 0;
4554       bl->nonneg = 0;
4555       bl->reversed = 0;
4556       bl->total_benefit = 0;
4557
4558       /* Add this class to loop_iv_list.  */
4559       bl->next = loop_iv_list;
4560       loop_iv_list = bl;
4561
4562       /* Put it in the array of biv register classes.  */
4563       reg_biv_class[REGNO (dest_reg)] = bl;
4564     }
4565
4566   /* Update IV_CLASS entry for this biv.  */
4567   v->next_iv = bl->biv;
4568   bl->biv = v;
4569   bl->biv_count++;
4570   if (mult_val == const1_rtx)
4571     bl->incremented = 1;
4572
4573   if (loop_dump_stream)
4574     {
4575       fprintf (loop_dump_stream,
4576                "Insn %d: possible biv, reg %d,",
4577                INSN_UID (insn), REGNO (dest_reg));
4578       if (GET_CODE (inc_val) == CONST_INT)
4579         fprintf (loop_dump_stream, " const = %d\n",
4580                  INTVAL (inc_val));
4581       else
4582         {
4583           fprintf (loop_dump_stream, " const = ");
4584           print_rtl (loop_dump_stream, inc_val);
4585           fprintf (loop_dump_stream, "\n");
4586         }
4587     }
4588 }
4589 \f
4590 /* Fill in the data about one giv.
4591    V is the `struct induction' in which we record the giv.  (It is
4592    allocated by the caller, with alloca.)
4593    INSN is the insn that sets it.
4594    BENEFIT estimates the savings from deleting this insn.
4595    TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
4596    into a register or is used as a memory address.
4597
4598    SRC_REG is the biv reg which the giv is computed from.
4599    DEST_REG is the giv's reg (if the giv is stored in a reg).
4600    MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
4601    LOCATION points to the place where this giv's value appears in INSN.  */
4602
4603 static void
4604 record_giv (v, insn, src_reg, dest_reg, mult_val, add_val, benefit,
4605             type, not_every_iteration, location, loop_start, loop_end)
4606      struct induction *v;
4607      rtx insn;
4608      rtx src_reg;
4609      rtx dest_reg;
4610      rtx mult_val, add_val;
4611      int benefit;
4612      enum g_types type;
4613      int not_every_iteration;
4614      rtx *location;
4615      rtx loop_start, loop_end;
4616 {
4617   struct induction *b;
4618   struct iv_class *bl;
4619   rtx set = single_set (insn);
4620   rtx p;
4621
4622   v->insn = insn;
4623   v->src_reg = src_reg;
4624   v->giv_type = type;
4625   v->dest_reg = dest_reg;
4626   v->mult_val = mult_val;
4627   v->add_val = add_val;
4628   v->benefit = benefit;
4629   v->location = location;
4630   v->cant_derive = 0;
4631   v->combined_with = 0;
4632   v->maybe_multiple = 0;
4633   v->maybe_dead = 0;
4634   v->derive_adjustment = 0;
4635   v->same = 0;
4636   v->ignore = 0;
4637   v->new_reg = 0;
4638   v->final_value = 0;
4639   v->same_insn = 0;
4640   v->auto_inc_opt = 0;
4641   v->unrolled = 0;
4642   v->shared = 0;
4643
4644   /* The v->always_computable field is used in update_giv_derive, to
4645      determine whether a giv can be used to derive another giv.  For a
4646      DEST_REG giv, INSN computes a new value for the giv, so its value
4647      isn't computable if INSN insn't executed every iteration.
4648      However, for a DEST_ADDR giv, INSN merely uses the value of the giv;
4649      it does not compute a new value.  Hence the value is always computable
4650      regardless of whether INSN is executed each iteration.  */
4651
4652   if (type == DEST_ADDR)
4653     v->always_computable = 1;
4654   else
4655     v->always_computable = ! not_every_iteration;
4656
4657   v->always_executed = ! not_every_iteration;
4658
4659   if (type == DEST_ADDR)
4660     {
4661       v->mode = GET_MODE (*location);
4662       v->lifetime = 1;
4663       v->times_used = 1;
4664     }
4665   else /* type == DEST_REG */
4666     {
4667       v->mode = GET_MODE (SET_DEST (set));
4668
4669       v->lifetime = (uid_luid[REGNO_LAST_UID (REGNO (dest_reg))]
4670                      - uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))]);
4671
4672       v->times_used = n_times_used[REGNO (dest_reg)];
4673
4674       /* If the lifetime is zero, it means that this register is
4675          really a dead store.  So mark this as a giv that can be
4676          ignored.  This will not prevent the biv from being eliminated.  */
4677       if (v->lifetime == 0)
4678         v->ignore = 1;
4679
4680       reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
4681       reg_iv_info[REGNO (dest_reg)] = v;
4682     }
4683
4684   /* Add the giv to the class of givs computed from one biv.  */
4685
4686   bl = reg_biv_class[REGNO (src_reg)];
4687   if (bl)
4688     {
4689       v->next_iv = bl->giv;
4690       bl->giv = v;
4691       /* Don't count DEST_ADDR.  This is supposed to count the number of
4692          insns that calculate givs.  */
4693       if (type == DEST_REG)
4694         bl->giv_count++;
4695       bl->total_benefit += benefit;
4696     }
4697   else
4698     /* Fatal error, biv missing for this giv?  */
4699     abort ();
4700
4701   if (type == DEST_ADDR)
4702     v->replaceable = 1;
4703   else
4704     {
4705       /* The giv can be replaced outright by the reduced register only if all
4706          of the following conditions are true:
4707          - the insn that sets the giv is always executed on any iteration
4708            on which the giv is used at all
4709            (there are two ways to deduce this:
4710             either the insn is executed on every iteration,
4711             or all uses follow that insn in the same basic block),
4712          - the giv is not used outside the loop
4713          - no assignments to the biv occur during the giv's lifetime.  */
4714
4715       if (REGNO_FIRST_UID (REGNO (dest_reg)) == INSN_UID (insn)
4716           /* Previous line always fails if INSN was moved by loop opt.  */
4717           && uid_luid[REGNO_LAST_UID (REGNO (dest_reg))] < INSN_LUID (loop_end)
4718           && (! not_every_iteration
4719               || last_use_this_basic_block (dest_reg, insn)))
4720         {
4721           /* Now check that there are no assignments to the biv within the
4722              giv's lifetime.  This requires two separate checks.  */
4723
4724           /* Check each biv update, and fail if any are between the first
4725              and last use of the giv.
4726              
4727              If this loop contains an inner loop that was unrolled, then
4728              the insn modifying the biv may have been emitted by the loop
4729              unrolling code, and hence does not have a valid luid.  Just
4730              mark the biv as not replaceable in this case.  It is not very
4731              useful as a biv, because it is used in two different loops.
4732              It is very unlikely that we would be able to optimize the giv
4733              using this biv anyways.  */
4734
4735           v->replaceable = 1;
4736           for (b = bl->biv; b; b = b->next_iv)
4737             {
4738               if (INSN_UID (b->insn) >= max_uid_for_loop
4739                   || ((uid_luid[INSN_UID (b->insn)]
4740                        >= uid_luid[REGNO_FIRST_UID (REGNO (dest_reg))])
4741                       && (uid_luid[INSN_UID (b->insn)]
4742                           <= uid_luid[REGNO_LAST_UID (REGNO (dest_reg))])))
4743                 {
4744                   v->replaceable = 0;
4745                   v->not_replaceable = 1;
4746                   break;
4747                 }
4748             }
4749
4750           /* If there are any backwards branches that go from after the
4751              biv update to before it, then this giv is not replaceable.  */
4752           if (v->replaceable)
4753             for (b = bl->biv; b; b = b->next_iv)
4754               if (back_branch_in_range_p (b->insn, loop_start, loop_end))
4755                 {
4756                   v->replaceable = 0;
4757                   v->not_replaceable = 1;
4758                   break;
4759                 }
4760         }
4761       else
4762         {
4763           /* May still be replaceable, we don't have enough info here to
4764              decide.  */
4765           v->replaceable = 0;
4766           v->not_replaceable = 0;
4767         }
4768     }
4769
4770   if (loop_dump_stream)
4771     {
4772       if (type == DEST_REG)
4773         fprintf (loop_dump_stream, "Insn %d: giv reg %d",
4774                  INSN_UID (insn), REGNO (dest_reg));
4775       else
4776         fprintf (loop_dump_stream, "Insn %d: dest address",
4777                  INSN_UID (insn));
4778
4779       fprintf (loop_dump_stream, " src reg %d benefit %d",
4780                REGNO (src_reg), v->benefit);
4781       fprintf (loop_dump_stream, " used %d lifetime %d",
4782                v->times_used, v->lifetime);
4783
4784       if (v->replaceable)
4785         fprintf (loop_dump_stream, " replaceable");
4786
4787       if (GET_CODE (mult_val) == CONST_INT)
4788         fprintf (loop_dump_stream, " mult %d",
4789                  INTVAL (mult_val));
4790       else
4791         {
4792           fprintf (loop_dump_stream, " mult ");
4793           print_rtl (loop_dump_stream, mult_val);
4794         }
4795
4796       if (GET_CODE (add_val) == CONST_INT)
4797         fprintf (loop_dump_stream, " add %d",
4798                  INTVAL (add_val));
4799       else
4800         {
4801           fprintf (loop_dump_stream, " add ");
4802           print_rtl (loop_dump_stream, add_val);
4803         }
4804     }
4805
4806   if (loop_dump_stream)
4807     fprintf (loop_dump_stream, "\n");
4808
4809 }
4810
4811
4812 /* All this does is determine whether a giv can be made replaceable because
4813    its final value can be calculated.  This code can not be part of record_giv
4814    above, because final_giv_value requires that the number of loop iterations
4815    be known, and that can not be accurately calculated until after all givs
4816    have been identified.  */
4817
4818 static void
4819 check_final_value (v, loop_start, loop_end)
4820      struct induction *v;
4821      rtx loop_start, loop_end;
4822 {
4823   struct iv_class *bl;
4824   rtx final_value = 0;
4825
4826   bl = reg_biv_class[REGNO (v->src_reg)];
4827
4828   /* DEST_ADDR givs will never reach here, because they are always marked
4829      replaceable above in record_giv.  */
4830
4831   /* The giv can be replaced outright by the reduced register only if all
4832      of the following conditions are true:
4833      - the insn that sets the giv is always executed on any iteration
4834        on which the giv is used at all
4835        (there are two ways to deduce this:
4836         either the insn is executed on every iteration,
4837         or all uses follow that insn in the same basic block),
4838      - its final value can be calculated (this condition is different
4839        than the one above in record_giv)
4840      - no assignments to the biv occur during the giv's lifetime.  */
4841
4842 #if 0
4843   /* This is only called now when replaceable is known to be false.  */
4844   /* Clear replaceable, so that it won't confuse final_giv_value.  */
4845   v->replaceable = 0;
4846 #endif
4847
4848   if ((final_value = final_giv_value (v, loop_start, loop_end))
4849       && (v->always_computable || last_use_this_basic_block (v->dest_reg, v->insn)))
4850     {
4851       int biv_increment_seen = 0;
4852       rtx p = v->insn;
4853       rtx last_giv_use;
4854
4855       v->replaceable = 1;
4856
4857       /* When trying to determine whether or not a biv increment occurs
4858          during the lifetime of the giv, we can ignore uses of the variable
4859          outside the loop because final_value is true.  Hence we can not
4860          use regno_last_uid and regno_first_uid as above in record_giv.  */
4861
4862       /* Search the loop to determine whether any assignments to the
4863          biv occur during the giv's lifetime.  Start with the insn
4864          that sets the giv, and search around the loop until we come
4865          back to that insn again.
4866
4867          Also fail if there is a jump within the giv's lifetime that jumps
4868          to somewhere outside the lifetime but still within the loop.  This
4869          catches spaghetti code where the execution order is not linear, and
4870          hence the above test fails.  Here we assume that the giv lifetime
4871          does not extend from one iteration of the loop to the next, so as
4872          to make the test easier.  Since the lifetime isn't known yet,
4873          this requires two loops.  See also record_giv above.  */
4874
4875       last_giv_use = v->insn;
4876
4877       while (1)
4878         {
4879           p = NEXT_INSN (p);
4880           if (p == loop_end)
4881             p = NEXT_INSN (loop_start);
4882           if (p == v->insn)
4883             break;
4884
4885           if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
4886               || GET_CODE (p) == CALL_INSN)
4887             {
4888               if (biv_increment_seen)
4889                 {
4890                   if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4891                     {
4892                       v->replaceable = 0;
4893                       v->not_replaceable = 1;
4894                       break;
4895                     }
4896                 }
4897               else if (reg_set_p (v->src_reg, PATTERN (p)))
4898                 biv_increment_seen = 1;
4899               else if (reg_mentioned_p (v->dest_reg, PATTERN (p)))
4900                 last_giv_use = p;
4901             }
4902         }
4903       
4904       /* Now that the lifetime of the giv is known, check for branches
4905          from within the lifetime to outside the lifetime if it is still
4906          replaceable.  */
4907
4908       if (v->replaceable)
4909         {
4910           p = v->insn;
4911           while (1)
4912             {
4913               p = NEXT_INSN (p);
4914               if (p == loop_end)
4915                 p = NEXT_INSN (loop_start);
4916               if (p == last_giv_use)
4917                 break;
4918
4919               if (GET_CODE (p) == JUMP_INSN && JUMP_LABEL (p)
4920                   && LABEL_NAME (JUMP_LABEL (p))
4921                   && ((INSN_UID (JUMP_LABEL (p)) >= max_uid_for_loop)
4922                       || (INSN_UID (v->insn) >= max_uid_for_loop)
4923                       || (INSN_UID (last_giv_use) >= max_uid_for_loop)
4924                       || (INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (v->insn)
4925                           && INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (loop_start))
4926                       || (INSN_LUID (JUMP_LABEL (p)) > INSN_LUID (last_giv_use)
4927                           && INSN_LUID (JUMP_LABEL (p)) < INSN_LUID (loop_end))))
4928                 {
4929                   v->replaceable = 0;
4930                   v->not_replaceable = 1;
4931
4932                   if (loop_dump_stream)
4933                     fprintf (loop_dump_stream,
4934                              "Found branch outside giv lifetime.\n");
4935
4936                   break;
4937                 }
4938             }
4939         }
4940
4941       /* If it is replaceable, then save the final value.  */
4942       if (v->replaceable)
4943         v->final_value = final_value;
4944     }
4945
4946   if (loop_dump_stream && v->replaceable)
4947     fprintf (loop_dump_stream, "Insn %d: giv reg %d final_value replaceable\n",
4948              INSN_UID (v->insn), REGNO (v->dest_reg));
4949 }
4950 \f
4951 /* Update the status of whether a giv can derive other givs.
4952
4953    We need to do something special if there is or may be an update to the biv
4954    between the time the giv is defined and the time it is used to derive
4955    another giv.
4956
4957    In addition, a giv that is only conditionally set is not allowed to
4958    derive another giv once a label has been passed.
4959
4960    The cases we look at are when a label or an update to a biv is passed.  */
4961
4962 static void
4963 update_giv_derive (p)
4964      rtx p;
4965 {
4966   struct iv_class *bl;
4967   struct induction *biv, *giv;
4968   rtx tem;
4969   int dummy;
4970
4971   /* Search all IV classes, then all bivs, and finally all givs.
4972
4973      There are three cases we are concerned with.  First we have the situation
4974      of a giv that is only updated conditionally.  In that case, it may not
4975      derive any givs after a label is passed.
4976
4977      The second case is when a biv update occurs, or may occur, after the
4978      definition of a giv.  For certain biv updates (see below) that are
4979      known to occur between the giv definition and use, we can adjust the
4980      giv definition.  For others, or when the biv update is conditional,
4981      we must prevent the giv from deriving any other givs.  There are two
4982      sub-cases within this case.
4983
4984      If this is a label, we are concerned with any biv update that is done
4985      conditionally, since it may be done after the giv is defined followed by
4986      a branch here (actually, we need to pass both a jump and a label, but
4987      this extra tracking doesn't seem worth it).
4988
4989      If this is a jump, we are concerned about any biv update that may be
4990      executed multiple times.  We are actually only concerned about
4991      backward jumps, but it is probably not worth performing the test
4992      on the jump again here.
4993
4994      If this is a biv update, we must adjust the giv status to show that a
4995      subsequent biv update was performed.  If this adjustment cannot be done,
4996      the giv cannot derive further givs.  */
4997
4998   for (bl = loop_iv_list; bl; bl = bl->next)
4999     for (biv = bl->biv; biv; biv = biv->next_iv)
5000       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN
5001           || biv->insn == p)
5002         {
5003           for (giv = bl->giv; giv; giv = giv->next_iv)
5004             {
5005               /* If cant_derive is already true, there is no point in
5006                  checking all of these conditions again.  */
5007               if (giv->cant_derive)
5008                 continue;
5009
5010               /* If this giv is conditionally set and we have passed a label,
5011                  it cannot derive anything.  */
5012               if (GET_CODE (p) == CODE_LABEL && ! giv->always_computable)
5013                 giv->cant_derive = 1;
5014
5015               /* Skip givs that have mult_val == 0, since
5016                  they are really invariants.  Also skip those that are
5017                  replaceable, since we know their lifetime doesn't contain
5018                  any biv update.  */
5019               else if (giv->mult_val == const0_rtx || giv->replaceable)
5020                 continue;
5021
5022               /* The only way we can allow this giv to derive another
5023                  is if this is a biv increment and we can form the product
5024                  of biv->add_val and giv->mult_val.  In this case, we will
5025                  be able to compute a compensation.  */
5026               else if (biv->insn == p)
5027                 {
5028                   tem = 0;
5029
5030                   if (biv->mult_val == const1_rtx)
5031                     tem = simplify_giv_expr (gen_rtx (MULT, giv->mode,
5032                                                       biv->add_val,
5033                                                       giv->mult_val),
5034                                              &dummy);
5035
5036                   if (tem && giv->derive_adjustment)
5037                     tem = simplify_giv_expr (gen_rtx (PLUS, giv->mode, tem,
5038                                                       giv->derive_adjustment),
5039                                              &dummy);
5040                   if (tem)
5041                     giv->derive_adjustment = tem;
5042                   else
5043                     giv->cant_derive = 1;
5044                 }
5045               else if ((GET_CODE (p) == CODE_LABEL && ! biv->always_computable)
5046                        || (GET_CODE (p) == JUMP_INSN && biv->maybe_multiple))
5047                 giv->cant_derive = 1;
5048             }
5049         }
5050 }
5051 \f
5052 /* Check whether an insn is an increment legitimate for a basic induction var.
5053    X is the source of insn P, or a part of it.
5054    MODE is the mode in which X should be interpreted.
5055
5056    DEST_REG is the putative biv, also the destination of the insn.
5057    We accept patterns of these forms:
5058      REG = REG + INVARIANT (includes REG = REG - CONSTANT)
5059      REG = INVARIANT + REG
5060
5061    If X is suitable, we return 1, set *MULT_VAL to CONST1_RTX,
5062    and store the additive term into *INC_VAL.
5063
5064    If X is an assignment of an invariant into DEST_REG, we set
5065    *MULT_VAL to CONST0_RTX, and store the invariant into *INC_VAL.
5066
5067    We also want to detect a BIV when it corresponds to a variable
5068    whose mode was promoted via PROMOTED_MODE.  In that case, an increment
5069    of the variable may be a PLUS that adds a SUBREG of that variable to
5070    an invariant and then sign- or zero-extends the result of the PLUS
5071    into the variable.
5072
5073    Most GIVs in such cases will be in the promoted mode, since that is the
5074    probably the natural computation mode (and almost certainly the mode
5075    used for addresses) on the machine.  So we view the pseudo-reg containing
5076    the variable as the BIV, as if it were simply incremented.
5077
5078    Note that treating the entire pseudo as a BIV will result in making
5079    simple increments to any GIVs based on it.  However, if the variable
5080    overflows in its declared mode but not its promoted mode, the result will
5081    be incorrect.  This is acceptable if the variable is signed, since 
5082    overflows in such cases are undefined, but not if it is unsigned, since
5083    those overflows are defined.  So we only check for SIGN_EXTEND and
5084    not ZERO_EXTEND.
5085
5086    If we cannot find a biv, we return 0.  */
5087
5088 static int
5089 basic_induction_var (x, mode, dest_reg, p, inc_val, mult_val)
5090      register rtx x;
5091      enum machine_mode mode;
5092      rtx p;
5093      rtx dest_reg;
5094      rtx *inc_val;
5095      rtx *mult_val;
5096 {
5097   register enum rtx_code code;
5098   rtx arg;
5099   rtx insn, set = 0;
5100
5101   code = GET_CODE (x);
5102   switch (code)
5103     {
5104     case PLUS:
5105       if (XEXP (x, 0) == dest_reg
5106           || (GET_CODE (XEXP (x, 0)) == SUBREG
5107               && SUBREG_PROMOTED_VAR_P (XEXP (x, 0))
5108               && SUBREG_REG (XEXP (x, 0)) == dest_reg))
5109         arg = XEXP (x, 1);
5110       else if (XEXP (x, 1) == dest_reg
5111                || (GET_CODE (XEXP (x, 1)) == SUBREG
5112                    && SUBREG_PROMOTED_VAR_P (XEXP (x, 1))
5113                    && SUBREG_REG (XEXP (x, 1)) == dest_reg))
5114         arg = XEXP (x, 0);
5115       else
5116         return 0;
5117
5118       if (invariant_p (arg) != 1)
5119         return 0;
5120
5121       *inc_val = convert_modes (GET_MODE (dest_reg), GET_MODE (x), arg, 0);
5122       *mult_val = const1_rtx;
5123       return 1;
5124
5125     case SUBREG:
5126       /* If this is a SUBREG for a promoted variable, check the inner
5127          value.  */
5128       if (SUBREG_PROMOTED_VAR_P (x))
5129         return basic_induction_var (SUBREG_REG (x), GET_MODE (SUBREG_REG (x)),
5130                                     dest_reg, p, inc_val, mult_val);
5131       return 0;
5132
5133     case REG:
5134       /* If this register is assigned in the previous insn, look at its
5135          source, but don't go outside the loop or past a label.  */
5136
5137       for (insn = PREV_INSN (p);
5138            (insn && GET_CODE (insn) == NOTE
5139             && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5140            insn = PREV_INSN (insn))
5141         ;
5142
5143       if (insn)
5144         set = single_set (insn);
5145
5146       if (set != 0
5147           && (SET_DEST (set) == x
5148               || (GET_CODE (SET_DEST (set)) == SUBREG
5149                   && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
5150                       <= UNITS_PER_WORD)
5151                   && SUBREG_REG (SET_DEST (set)) == x)))
5152         return basic_induction_var (SET_SRC (set),
5153                                     (GET_MODE (SET_SRC (set)) == VOIDmode
5154                                      ? GET_MODE (x)
5155                                      : GET_MODE (SET_SRC (set))),
5156                                     dest_reg, insn,
5157                                     inc_val, mult_val);
5158       /* ... fall through ...  */
5159
5160       /* Can accept constant setting of biv only when inside inner most loop.
5161          Otherwise, a biv of an inner loop may be incorrectly recognized
5162          as a biv of the outer loop,
5163          causing code to be moved INTO the inner loop.  */
5164     case MEM:
5165       if (invariant_p (x) != 1)
5166         return 0;
5167     case CONST_INT:
5168     case SYMBOL_REF:
5169     case CONST:
5170       if (loops_enclosed == 1)
5171         {
5172           /* Possible bug here?  Perhaps we don't know the mode of X.  */
5173           *inc_val = convert_modes (GET_MODE (dest_reg), mode, x, 0);
5174           *mult_val = const0_rtx;
5175           return 1;
5176         }
5177       else
5178         return 0;
5179
5180     case SIGN_EXTEND:
5181       return basic_induction_var (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5182                                   dest_reg, p, inc_val, mult_val);
5183     case ASHIFTRT:
5184       /* Similar, since this can be a sign extension.  */
5185       for (insn = PREV_INSN (p);
5186            (insn && GET_CODE (insn) == NOTE
5187             && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_BEG);
5188            insn = PREV_INSN (insn))
5189         ;
5190
5191       if (insn)
5192         set = single_set (insn);
5193
5194       if (set && SET_DEST (set) == XEXP (x, 0)
5195           && GET_CODE (XEXP (x, 1)) == CONST_INT
5196           && INTVAL (XEXP (x, 1)) >= 0
5197           && GET_CODE (SET_SRC (set)) == ASHIFT
5198           && XEXP (x, 1) == XEXP (SET_SRC (set), 1))
5199         return basic_induction_var (XEXP (SET_SRC (set), 0),
5200                                     GET_MODE (XEXP (x, 0)),
5201                                     dest_reg, insn, inc_val, mult_val);
5202       return 0;
5203
5204     default:
5205       return 0;
5206     }
5207 }
5208 \f
5209 /* A general induction variable (giv) is any quantity that is a linear
5210    function   of a basic induction variable,
5211    i.e. giv = biv * mult_val + add_val.
5212    The coefficients can be any loop invariant quantity.
5213    A giv need not be computed directly from the biv;
5214    it can be computed by way of other givs.  */
5215
5216 /* Determine whether X computes a giv.
5217    If it does, return a nonzero value
5218      which is the benefit from eliminating the computation of X;
5219    set *SRC_REG to the register of the biv that it is computed from;
5220    set *ADD_VAL and *MULT_VAL to the coefficients,
5221      such that the value of X is biv * mult + add;  */
5222
5223 static int
5224 general_induction_var (x, src_reg, add_val, mult_val)
5225      rtx x;
5226      rtx *src_reg;
5227      rtx *add_val;
5228      rtx *mult_val;
5229 {
5230   rtx orig_x = x;
5231   int benefit = 0;
5232   char *storage;
5233
5234   /* If this is an invariant, forget it, it isn't a giv.  */
5235   if (invariant_p (x) == 1)
5236     return 0;
5237
5238   /* See if the expression could be a giv and get its form.
5239      Mark our place on the obstack in case we don't find a giv.  */
5240   storage = (char *) oballoc (0);
5241   x = simplify_giv_expr (x, &benefit);
5242   if (x == 0)
5243     {
5244       obfree (storage);
5245       return 0;
5246     }
5247
5248   switch (GET_CODE (x))
5249     {
5250     case USE:
5251     case CONST_INT:
5252       /* Since this is now an invariant and wasn't before, it must be a giv
5253          with MULT_VAL == 0.  It doesn't matter which BIV we associate this
5254          with.  */
5255       *src_reg = loop_iv_list->biv->dest_reg;
5256       *mult_val = const0_rtx;
5257       *add_val = x;
5258       break;
5259
5260     case REG:
5261       /* This is equivalent to a BIV.  */
5262       *src_reg = x;
5263       *mult_val = const1_rtx;
5264       *add_val = const0_rtx;
5265       break;
5266
5267     case PLUS:
5268       /* Either (plus (biv) (invar)) or
5269          (plus (mult (biv) (invar_1)) (invar_2)).  */
5270       if (GET_CODE (XEXP (x, 0)) == MULT)
5271         {
5272           *src_reg = XEXP (XEXP (x, 0), 0);
5273           *mult_val = XEXP (XEXP (x, 0), 1);
5274         }
5275       else
5276         {
5277           *src_reg = XEXP (x, 0);
5278           *mult_val = const1_rtx;
5279         }
5280       *add_val = XEXP (x, 1);
5281       break;
5282
5283     case MULT:
5284       /* ADD_VAL is zero.  */
5285       *src_reg = XEXP (x, 0);
5286       *mult_val = XEXP (x, 1);
5287       *add_val = const0_rtx;
5288       break;
5289
5290     default:
5291       abort ();
5292     }
5293
5294   /* Remove any enclosing USE from ADD_VAL and MULT_VAL (there will be
5295      unless they are CONST_INT).  */
5296   if (GET_CODE (*add_val) == USE)
5297     *add_val = XEXP (*add_val, 0);
5298   if (GET_CODE (*mult_val) == USE)
5299     *mult_val = XEXP (*mult_val, 0);
5300
5301   benefit += rtx_cost (orig_x, SET);
5302
5303   /* Always return some benefit if this is a giv so it will be detected
5304      as such.  This allows elimination of bivs that might otherwise
5305      not be eliminated.  */
5306   return benefit == 0 ? 1 : benefit;
5307 }
5308 \f
5309 /* Given an expression, X, try to form it as a linear function of a biv.
5310    We will canonicalize it to be of the form
5311         (plus (mult (BIV) (invar_1))
5312               (invar_2))
5313    with possible degeneracies.
5314
5315    The invariant expressions must each be of a form that can be used as a
5316    machine operand.  We surround then with a USE rtx (a hack, but localized
5317    and certainly unambiguous!) if not a CONST_INT for simplicity in this
5318    routine; it is the caller's responsibility to strip them.
5319
5320    If no such canonicalization is possible (i.e., two biv's are used or an
5321    expression that is neither invariant nor a biv or giv), this routine
5322    returns 0.
5323
5324    For a non-zero return, the result will have a code of CONST_INT, USE,
5325    REG (for a BIV), PLUS, or MULT.  No other codes will occur.  
5326
5327    *BENEFIT will be incremented by the benefit of any sub-giv encountered.  */
5328
5329 static rtx
5330 simplify_giv_expr (x, benefit)
5331      rtx x;
5332      int *benefit;
5333 {
5334   enum machine_mode mode = GET_MODE (x);
5335   rtx arg0, arg1;
5336   rtx tem;
5337
5338   /* If this is not an integer mode, or if we cannot do arithmetic in this
5339      mode, this can't be a giv.  */
5340   if (mode != VOIDmode
5341       && (GET_MODE_CLASS (mode) != MODE_INT
5342           || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT))
5343     return 0;
5344
5345   switch (GET_CODE (x))
5346     {
5347     case PLUS:
5348       arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5349       arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5350       if (arg0 == 0 || arg1 == 0)
5351         return 0;
5352
5353       /* Put constant last, CONST_INT last if both constant.  */
5354       if ((GET_CODE (arg0) == USE
5355            || GET_CODE (arg0) == CONST_INT)
5356           && GET_CODE (arg1) != CONST_INT)
5357         tem = arg0, arg0 = arg1, arg1 = tem;
5358
5359       /* Handle addition of zero, then addition of an invariant.  */
5360       if (arg1 == const0_rtx)
5361         return arg0;
5362       else if (GET_CODE (arg1) == CONST_INT || GET_CODE (arg1) == USE)
5363         switch (GET_CODE (arg0))
5364           {
5365           case CONST_INT:
5366           case USE:
5367             /* Both invariant.  Only valid if sum is machine operand.
5368                First strip off possible USE on first operand.  */
5369             if (GET_CODE (arg0) == USE)
5370               arg0 = XEXP (arg0, 0);
5371
5372             tem = 0;
5373             if (CONSTANT_P (arg0) && GET_CODE (arg1) == CONST_INT)
5374               {
5375                 tem = plus_constant (arg0, INTVAL (arg1));
5376                 if (GET_CODE (tem) != CONST_INT)
5377                   tem = gen_rtx (USE, mode, tem);
5378               }
5379
5380             return tem;
5381
5382           case REG:
5383           case MULT:
5384             /* biv + invar or mult + invar.  Return sum.  */
5385             return gen_rtx (PLUS, mode, arg0, arg1);
5386
5387           case PLUS:
5388             /* (a + invar_1) + invar_2.  Associate.  */
5389             return simplify_giv_expr (gen_rtx (PLUS, mode,
5390                                                XEXP (arg0, 0),
5391                                                gen_rtx (PLUS, mode,
5392                                                         XEXP (arg0, 1), arg1)),
5393                                       benefit);
5394
5395           default:
5396             abort ();
5397           }
5398
5399       /* Each argument must be either REG, PLUS, or MULT.  Convert REG to
5400          MULT to reduce cases.  */
5401       if (GET_CODE (arg0) == REG)
5402         arg0 = gen_rtx (MULT, mode, arg0, const1_rtx);
5403       if (GET_CODE (arg1) == REG)
5404         arg1 = gen_rtx (MULT, mode, arg1, const1_rtx);
5405
5406       /* Now have PLUS + PLUS, PLUS + MULT, MULT + PLUS, or MULT + MULT.
5407          Put a MULT first, leaving PLUS + PLUS, MULT + PLUS, or MULT + MULT.
5408          Recurse to associate the second PLUS.  */
5409       if (GET_CODE (arg1) == MULT)
5410         tem = arg0, arg0 = arg1, arg1 = tem;
5411
5412       if (GET_CODE (arg1) == PLUS)
5413           return simplify_giv_expr (gen_rtx (PLUS, mode,
5414                                              gen_rtx (PLUS, mode,
5415                                                       arg0, XEXP (arg1, 0)),
5416                                              XEXP (arg1, 1)),
5417                                     benefit);
5418
5419       /* Now must have MULT + MULT.  Distribute if same biv, else not giv.  */
5420       if (GET_CODE (arg0) != MULT || GET_CODE (arg1) != MULT)
5421         abort ();
5422
5423       if (XEXP (arg0, 0) != XEXP (arg1, 0))
5424         return 0;
5425
5426       return simplify_giv_expr (gen_rtx (MULT, mode,
5427                                          XEXP (arg0, 0),
5428                                          gen_rtx (PLUS, mode,
5429                                                   XEXP (arg0, 1),
5430                                                   XEXP (arg1, 1))),
5431                                 benefit);
5432
5433     case MINUS:
5434       /* Handle "a - b" as "a + b * (-1)".  */
5435       return simplify_giv_expr (gen_rtx (PLUS, mode,
5436                                          XEXP (x, 0),
5437                                          gen_rtx (MULT, mode,
5438                                                   XEXP (x, 1), constm1_rtx)),
5439                                 benefit);
5440
5441     case MULT:
5442       arg0 = simplify_giv_expr (XEXP (x, 0), benefit);
5443       arg1 = simplify_giv_expr (XEXP (x, 1), benefit);
5444       if (arg0 == 0 || arg1 == 0)
5445         return 0;
5446
5447       /* Put constant last, CONST_INT last if both constant.  */
5448       if ((GET_CODE (arg0) == USE || GET_CODE (arg0) == CONST_INT)
5449           && GET_CODE (arg1) != CONST_INT)
5450         tem = arg0, arg0 = arg1, arg1 = tem;
5451
5452       /* If second argument is not now constant, not giv.  */
5453       if (GET_CODE (arg1) != USE && GET_CODE (arg1) != CONST_INT)
5454         return 0;
5455
5456       /* Handle multiply by 0 or 1.  */
5457       if (arg1 == const0_rtx)
5458         return const0_rtx;
5459
5460       else if (arg1 == const1_rtx)
5461         return arg0;
5462
5463       switch (GET_CODE (arg0))
5464         {
5465         case REG:
5466           /* biv * invar.  Done.  */
5467           return gen_rtx (MULT, mode, arg0, arg1);
5468
5469         case CONST_INT:
5470           /* Product of two constants.  */
5471           return GEN_INT (INTVAL (arg0) * INTVAL (arg1));
5472
5473         case USE:
5474           /* invar * invar.  Not giv.  */
5475           return 0;
5476
5477         case MULT:
5478           /* (a * invar_1) * invar_2.  Associate.  */
5479           return simplify_giv_expr (gen_rtx (MULT, mode,
5480                                              XEXP (arg0, 0),
5481                                              gen_rtx (MULT, mode,
5482                                                       XEXP (arg0, 1), arg1)),
5483                                     benefit);
5484
5485         case PLUS:
5486           /* (a + invar_1) * invar_2.  Distribute.  */
5487           return simplify_giv_expr (gen_rtx (PLUS, mode,
5488                                              gen_rtx (MULT, mode,
5489                                                       XEXP (arg0, 0), arg1),
5490                                              gen_rtx (MULT, mode,
5491                                                       XEXP (arg0, 1), arg1)),
5492                                     benefit);
5493
5494         default:
5495           abort ();
5496         }
5497
5498     case ASHIFT:
5499       /* Shift by constant is multiply by power of two.  */
5500       if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5501         return 0;
5502
5503       return simplify_giv_expr (gen_rtx (MULT, mode,
5504                                          XEXP (x, 0),
5505                                          GEN_INT ((HOST_WIDE_INT) 1
5506                                                   << INTVAL (XEXP (x, 1)))),
5507                                 benefit);
5508
5509     case NEG:
5510       /* "-a" is "a * (-1)" */
5511       return simplify_giv_expr (gen_rtx (MULT, mode, XEXP (x, 0), constm1_rtx),
5512                                 benefit);
5513
5514     case NOT:
5515       /* "~a" is "-a - 1". Silly, but easy.  */
5516       return simplify_giv_expr (gen_rtx (MINUS, mode,
5517                                          gen_rtx (NEG, mode, XEXP (x, 0)),
5518                                          const1_rtx),
5519                                 benefit);
5520
5521     case USE:
5522       /* Already in proper form for invariant.  */
5523       return x;
5524
5525     case REG:
5526       /* If this is a new register, we can't deal with it.  */
5527       if (REGNO (x) >= max_reg_before_loop)
5528         return 0;
5529
5530       /* Check for biv or giv.  */
5531       switch (reg_iv_type[REGNO (x)])
5532         {
5533         case BASIC_INDUCT:
5534           return x;
5535         case GENERAL_INDUCT:
5536           {
5537             struct induction *v = reg_iv_info[REGNO (x)];
5538
5539             /* Form expression from giv and add benefit.  Ensure this giv
5540                can derive another and subtract any needed adjustment if so.  */
5541             *benefit += v->benefit;
5542             if (v->cant_derive)
5543               return 0;
5544
5545             tem = gen_rtx (PLUS, mode, gen_rtx (MULT, mode,
5546                                                 v->src_reg, v->mult_val),
5547                            v->add_val);
5548             if (v->derive_adjustment)
5549               tem = gen_rtx (MINUS, mode, tem, v->derive_adjustment);
5550             return simplify_giv_expr (tem, benefit);
5551           }
5552         }
5553
5554       /* Fall through to general case.  */
5555     default:
5556       /* If invariant, return as USE (unless CONST_INT).
5557          Otherwise, not giv.  */
5558       if (GET_CODE (x) == USE)
5559         x = XEXP (x, 0);
5560
5561       if (invariant_p (x) == 1)
5562         {
5563           if (GET_CODE (x) == CONST_INT)
5564             return x;
5565           else
5566             return gen_rtx (USE, mode, x);
5567         }
5568       else
5569         return 0;
5570     }
5571 }
5572 \f
5573 /* Help detect a giv that is calculated by several consecutive insns;
5574    for example,
5575       giv = biv * M
5576       giv = giv + A
5577    The caller has already identified the first insn P as having a giv as dest;
5578    we check that all other insns that set the same register follow
5579    immediately after P, that they alter nothing else,
5580    and that the result of the last is still a giv.
5581
5582    The value is 0 if the reg set in P is not really a giv.
5583    Otherwise, the value is the amount gained by eliminating
5584    all the consecutive insns that compute the value.
5585
5586    FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
5587    SRC_REG is the reg of the biv; DEST_REG is the reg of the giv.
5588
5589    The coefficients of the ultimate giv value are stored in
5590    *MULT_VAL and *ADD_VAL.  */
5591
5592 static int
5593 consec_sets_giv (first_benefit, p, src_reg, dest_reg,
5594                  add_val, mult_val)
5595      int first_benefit;
5596      rtx p;
5597      rtx src_reg;
5598      rtx dest_reg;
5599      rtx *add_val;
5600      rtx *mult_val;
5601 {
5602   int count;
5603   enum rtx_code code;
5604   int benefit;
5605   rtx temp;
5606   rtx set;
5607
5608   /* Indicate that this is a giv so that we can update the value produced in
5609      each insn of the multi-insn sequence. 
5610
5611      This induction structure will be used only by the call to
5612      general_induction_var below, so we can allocate it on our stack.
5613      If this is a giv, our caller will replace the induct var entry with
5614      a new induction structure.  */
5615   struct induction *v
5616     = (struct induction *) alloca (sizeof (struct induction));
5617   v->src_reg = src_reg;
5618   v->mult_val = *mult_val;
5619   v->add_val = *add_val;
5620   v->benefit = first_benefit;
5621   v->cant_derive = 0;
5622   v->derive_adjustment = 0;
5623
5624   reg_iv_type[REGNO (dest_reg)] = GENERAL_INDUCT;
5625   reg_iv_info[REGNO (dest_reg)] = v;
5626
5627   count = n_times_set[REGNO (dest_reg)] - 1;
5628
5629   while (count > 0)
5630     {
5631       p = NEXT_INSN (p);
5632       code = GET_CODE (p);
5633
5634       /* If libcall, skip to end of call sequence.  */
5635       if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, NULL_RTX)))
5636         p = XEXP (temp, 0);
5637
5638       if (code == INSN
5639           && (set = single_set (p))
5640           && GET_CODE (SET_DEST (set)) == REG
5641           && SET_DEST (set) == dest_reg
5642           && ((benefit = general_induction_var (SET_SRC (set), &src_reg,
5643                                                 add_val, mult_val))
5644               /* Giv created by equivalent expression.  */
5645               || ((temp = find_reg_note (p, REG_EQUAL, NULL_RTX))
5646                   && (benefit = general_induction_var (XEXP (temp, 0), &src_reg,
5647                                                        add_val, mult_val))))
5648           && src_reg == v->src_reg)
5649         {
5650           if (find_reg_note (p, REG_RETVAL, NULL_RTX))
5651             benefit += libcall_benefit (p);
5652
5653           count--;
5654           v->mult_val = *mult_val;
5655           v->add_val = *add_val;
5656           v->benefit = benefit;
5657         }
5658       else if (code != NOTE)
5659         {
5660           /* Allow insns that set something other than this giv to a
5661              constant.  Such insns are needed on machines which cannot
5662              include long constants and should not disqualify a giv.  */
5663           if (code == INSN
5664               && (set = single_set (p))
5665               && SET_DEST (set) != dest_reg
5666               && CONSTANT_P (SET_SRC (set)))
5667             continue;
5668
5669           reg_iv_type[REGNO (dest_reg)] = UNKNOWN_INDUCT;
5670           return 0;
5671         }
5672     }
5673
5674   return v->benefit;
5675 }
5676 \f
5677 /* Return an rtx, if any, that expresses giv G2 as a function of the register
5678    represented by G1.  If no such expression can be found, or it is clear that
5679    it cannot possibly be a valid address, 0 is returned. 
5680
5681    To perform the computation, we note that
5682         G1 = a * v + b          and
5683         G2 = c * v + d
5684    where `v' is the biv.
5685
5686    So G2 = (c/a) * G1 + (d - b*c/a)  */
5687
5688 #ifdef ADDRESS_COST
5689 static rtx
5690 express_from (g1, g2)
5691      struct induction *g1, *g2;
5692 {
5693   rtx mult, add;
5694
5695   /* The value that G1 will be multiplied by must be a constant integer.  Also,
5696      the only chance we have of getting a valid address is if b*c/a (see above
5697      for notation) is also an integer.  */
5698   if (GET_CODE (g1->mult_val) != CONST_INT
5699       || GET_CODE (g2->mult_val) != CONST_INT
5700       || GET_CODE (g1->add_val) != CONST_INT
5701       || g1->mult_val == const0_rtx
5702       || INTVAL (g2->mult_val) % INTVAL (g1->mult_val) != 0)
5703     return 0;
5704
5705   mult = GEN_INT (INTVAL (g2->mult_val) / INTVAL (g1->mult_val));
5706   add = plus_constant (g2->add_val, - INTVAL (g1->add_val) * INTVAL (mult));
5707
5708   /* Form simplified final result.  */
5709   if (mult == const0_rtx)
5710     return add;
5711   else if (mult == const1_rtx)
5712     mult = g1->dest_reg;
5713   else
5714     mult = gen_rtx (MULT, g2->mode, g1->dest_reg, mult);
5715
5716   if (add == const0_rtx)
5717     return mult;
5718   else
5719     return gen_rtx (PLUS, g2->mode, mult, add);
5720 }
5721 #endif
5722 \f
5723 /* Return 1 if giv G2 can be combined with G1.  This means that G2 can use
5724    (either directly or via an address expression) a register used to represent
5725    G1.  Set g2->new_reg to a represtation of G1 (normally just
5726    g1->dest_reg).  */
5727
5728 static int
5729 combine_givs_p (g1, g2)
5730      struct induction *g1, *g2;
5731 {
5732   rtx tem;
5733
5734   /* If these givs are identical, they can be combined.  */
5735   if (rtx_equal_p (g1->mult_val, g2->mult_val)
5736       && rtx_equal_p (g1->add_val, g2->add_val))
5737     {
5738       g2->new_reg = g1->dest_reg;
5739       return 1;
5740     }
5741
5742 #ifdef ADDRESS_COST
5743   /* If G2 can be expressed as a function of G1 and that function is valid
5744      as an address and no more expensive than using a register for G2,
5745      the expression of G2 in terms of G1 can be used.  */
5746   if (g2->giv_type == DEST_ADDR
5747       && (tem = express_from (g1, g2)) != 0
5748       && memory_address_p (g2->mem_mode, tem)
5749       && ADDRESS_COST (tem) <= ADDRESS_COST (*g2->location))
5750     {
5751       g2->new_reg = tem;
5752       return 1;
5753     }
5754 #endif
5755
5756   return 0;
5757 }
5758 \f
5759 #ifdef GIV_SORT_CRITERION
5760 /* Compare two givs and sort the most desirable one for combinations first.
5761    This is used only in one qsort call below.  */
5762
5763 static int
5764 giv_sort (x, y)
5765      struct induction **x, **y;
5766 {
5767   GIV_SORT_CRITERION (*x, *y);
5768
5769   return 0;
5770 }
5771 #endif
5772
5773 /* Check all pairs of givs for iv_class BL and see if any can be combined with
5774    any other.  If so, point SAME to the giv combined with and set NEW_REG to
5775    be an expression (in terms of the other giv's DEST_REG) equivalent to the
5776    giv.  Also, update BENEFIT and related fields for cost/benefit analysis.  */
5777
5778 static void
5779 combine_givs (bl)
5780      struct iv_class *bl;
5781 {
5782   struct induction *g1, *g2, **giv_array, *temp_iv;
5783   int i, j, giv_count, pass;
5784
5785   /* Count givs, because bl->giv_count is incorrect here.  */
5786   giv_count = 0;
5787   for (g1 = bl->giv; g1; g1 = g1->next_iv)
5788     giv_count++;
5789
5790   giv_array
5791     = (struct induction **) alloca (giv_count * sizeof (struct induction *));
5792   i = 0;
5793   for (g1 = bl->giv; g1; g1 = g1->next_iv)
5794     giv_array[i++] = g1;
5795
5796 #ifdef GIV_SORT_CRITERION
5797   /* Sort the givs if GIV_SORT_CRITERION is defined.
5798      This is usually defined for processors which lack
5799      negative register offsets so more givs may be combined.  */
5800
5801   if (loop_dump_stream)
5802     fprintf (loop_dump_stream, "%d givs counted, sorting...\n", giv_count);
5803
5804   qsort (giv_array, giv_count, sizeof (struct induction *), giv_sort);
5805 #endif
5806
5807   for (i = 0; i < giv_count; i++)
5808     {
5809       g1 = giv_array[i];
5810       for (pass = 0; pass <= 1; pass++)
5811         for (j = 0; j < giv_count; j++)
5812           {
5813             g2 = giv_array[j];
5814             if (g1 != g2
5815                 /* First try to combine with replaceable givs, then all givs.  */
5816                 && (g1->replaceable || pass == 1)
5817                 /* If either has already been combined or is to be ignored, can't
5818                    combine.  */
5819                 && ! g1->ignore && ! g2->ignore && ! g1->same && ! g2->same
5820                 /* If something has been based on G2, G2 cannot itself be based
5821                    on something else.  */
5822                 && ! g2->combined_with
5823                 && combine_givs_p (g1, g2))
5824               {
5825                 /* g2->new_reg set by `combine_givs_p'  */
5826                 g2->same = g1;
5827                 g1->combined_with = 1;
5828
5829                 /* If one of these givs is a DEST_REG that was only used
5830                    once, by the other giv, this is actually a single use.
5831                    The DEST_REG has the correct cost, while the other giv
5832                    counts the REG use too often.  */
5833                 if (g2->giv_type == DEST_REG
5834                     && n_times_used[REGNO (g2->dest_reg)] == 1
5835                     && reg_mentioned_p (g2->dest_reg, PATTERN (g1->insn)))
5836                   g1->benefit = g2->benefit;
5837                 else if (g1->giv_type != DEST_REG
5838                          || n_times_used[REGNO (g1->dest_reg)] != 1
5839                          || ! reg_mentioned_p (g1->dest_reg,
5840                                                PATTERN (g2->insn)))
5841                   {
5842                     g1->benefit += g2->benefit;
5843                     g1->times_used += g2->times_used;
5844                   }
5845                 /* ??? The new final_[bg]iv_value code does a much better job
5846                    of finding replaceable giv's, and hence this code may no
5847                    longer be necessary.  */
5848                 if (! g2->replaceable && REG_USERVAR_P (g2->dest_reg))
5849                   g1->benefit -= copy_cost;
5850                 g1->lifetime += g2->lifetime;
5851                 
5852                 if (loop_dump_stream)
5853                   fprintf (loop_dump_stream, "giv at %d combined with giv at %d\n",
5854                            INSN_UID (g2->insn), INSN_UID (g1->insn));
5855               }
5856           }
5857     }
5858 }
5859 \f
5860 /* EMIT code before INSERT_BEFORE to set REG = B * M + A.  */
5861
5862 void
5863 emit_iv_add_mult (b, m, a, reg, insert_before)
5864      rtx b;          /* initial value of basic induction variable */
5865      rtx m;          /* multiplicative constant */
5866      rtx a;          /* additive constant */
5867      rtx reg;        /* destination register */
5868      rtx insert_before;
5869 {
5870   rtx seq;
5871   rtx result;
5872
5873   /* Prevent unexpected sharing of these rtx.  */
5874   a = copy_rtx (a);
5875   b = copy_rtx (b);
5876
5877   /* Increase the lifetime of any invariants moved further in code.  */
5878   update_reg_last_use (a, insert_before);
5879   update_reg_last_use (b, insert_before);
5880   update_reg_last_use (m, insert_before);
5881
5882   start_sequence ();
5883   result = expand_mult_add (b, reg, m, a, GET_MODE (reg), 0);
5884   if (reg != result)
5885     emit_move_insn (reg, result);
5886   seq = gen_sequence ();
5887   end_sequence ();
5888
5889   emit_insn_before (seq, insert_before);
5890
5891   record_base_value (REGNO (reg), b);
5892 }
5893 \f
5894 /* Test whether A * B can be computed without
5895    an actual multiply insn.  Value is 1 if so.  */
5896
5897 static int
5898 product_cheap_p (a, b)
5899      rtx a;
5900      rtx b;
5901 {
5902   int i;
5903   rtx tmp;
5904   struct obstack *old_rtl_obstack = rtl_obstack;
5905   char *storage = (char *) obstack_alloc (&temp_obstack, 0);
5906   int win = 1;
5907
5908   /* If only one is constant, make it B.  */
5909   if (GET_CODE (a) == CONST_INT)
5910     tmp = a, a = b, b = tmp;
5911
5912   /* If first constant, both constant, so don't need multiply.  */
5913   if (GET_CODE (a) == CONST_INT)
5914     return 1;
5915
5916   /* If second not constant, neither is constant, so would need multiply.  */
5917   if (GET_CODE (b) != CONST_INT)
5918     return 0;
5919
5920   /* One operand is constant, so might not need multiply insn.  Generate the
5921      code for the multiply and see if a call or multiply, or long sequence
5922      of insns is generated.  */
5923
5924   rtl_obstack = &temp_obstack;
5925   start_sequence ();
5926   expand_mult (GET_MODE (a), a, b, NULL_RTX, 0);
5927   tmp = gen_sequence ();
5928   end_sequence ();
5929
5930   if (GET_CODE (tmp) == SEQUENCE)
5931     {
5932       if (XVEC (tmp, 0) == 0)
5933         win = 1;
5934       else if (XVECLEN (tmp, 0) > 3)
5935         win = 0;
5936       else
5937         for (i = 0; i < XVECLEN (tmp, 0); i++)
5938           {
5939             rtx insn = XVECEXP (tmp, 0, i);
5940
5941             if (GET_CODE (insn) != INSN
5942                 || (GET_CODE (PATTERN (insn)) == SET
5943                     && GET_CODE (SET_SRC (PATTERN (insn))) == MULT)
5944                 || (GET_CODE (PATTERN (insn)) == PARALLEL
5945                     && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET
5946                     && GET_CODE (SET_SRC (XVECEXP (PATTERN (insn), 0, 0))) == MULT))
5947               {
5948                 win = 0;
5949                 break;
5950               }
5951           }
5952     }
5953   else if (GET_CODE (tmp) == SET
5954            && GET_CODE (SET_SRC (tmp)) == MULT)
5955     win = 0;
5956   else if (GET_CODE (tmp) == PARALLEL
5957            && GET_CODE (XVECEXP (tmp, 0, 0)) == SET
5958            && GET_CODE (SET_SRC (XVECEXP (tmp, 0, 0))) == MULT)
5959     win = 0;
5960
5961   /* Free any storage we obtained in generating this multiply and restore rtl
5962      allocation to its normal obstack.  */
5963   obstack_free (&temp_obstack, storage);
5964   rtl_obstack = old_rtl_obstack;
5965
5966   return win;
5967 }
5968 \f
5969 /* Check to see if loop can be terminated by a "decrement and branch until
5970    zero" instruction.  If so, add a REG_NONNEG note to the branch insn if so.
5971    Also try reversing an increment loop to a decrement loop
5972    to see if the optimization can be performed.
5973    Value is nonzero if optimization was performed.  */
5974
5975 /* This is useful even if the architecture doesn't have such an insn,
5976    because it might change a loops which increments from 0 to n to a loop
5977    which decrements from n to 0.  A loop that decrements to zero is usually
5978    faster than one that increments from zero.  */
5979
5980 /* ??? This could be rewritten to use some of the loop unrolling procedures,
5981    such as approx_final_value, biv_total_increment, loop_iterations, and
5982    final_[bg]iv_value.  */
5983
5984 static int
5985 check_dbra_loop (loop_end, insn_count, loop_start)
5986      rtx loop_end;
5987      int insn_count;
5988      rtx loop_start;
5989 {
5990   struct iv_class *bl;
5991   rtx reg;
5992   rtx jump_label;
5993   rtx final_value;
5994   rtx start_value;
5995   rtx new_add_val;
5996   rtx comparison;
5997   rtx before_comparison;
5998   rtx p;
5999
6000   /* If last insn is a conditional branch, and the insn before tests a
6001      register value, try to optimize it.  Otherwise, we can't do anything.  */
6002
6003   comparison = get_condition_for_loop (PREV_INSN (loop_end));
6004   if (comparison == 0)
6005     return 0;
6006
6007   /* Check all of the bivs to see if the compare uses one of them.
6008      Skip biv's set more than once because we can't guarantee that
6009      it will be zero on the last iteration.  Also skip if the biv is
6010      used between its update and the test insn.  */
6011
6012   for (bl = loop_iv_list; bl; bl = bl->next)
6013     {
6014       if (bl->biv_count == 1
6015           && bl->biv->dest_reg == XEXP (comparison, 0)
6016           && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
6017                                    PREV_INSN (PREV_INSN (loop_end))))
6018         break;
6019     }
6020
6021   if (! bl)
6022     return 0;
6023
6024   /* Look for the case where the basic induction variable is always
6025      nonnegative, and equals zero on the last iteration.
6026      In this case, add a reg_note REG_NONNEG, which allows the
6027      m68k DBRA instruction to be used.  */
6028
6029   if (((GET_CODE (comparison) == GT
6030         && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6031         && INTVAL (XEXP (comparison, 1)) == -1)
6032        || (GET_CODE (comparison) == NE && XEXP (comparison, 1) == const0_rtx))
6033       && GET_CODE (bl->biv->add_val) == CONST_INT
6034       && INTVAL (bl->biv->add_val) < 0)
6035     {
6036       /* Initial value must be greater than 0,
6037          init_val % -dec_value == 0 to ensure that it equals zero on
6038          the last iteration */
6039
6040       if (GET_CODE (bl->initial_value) == CONST_INT
6041           && INTVAL (bl->initial_value) > 0
6042           && (INTVAL (bl->initial_value)
6043               % (-INTVAL (bl->biv->add_val))) == 0)
6044         {
6045           /* register always nonnegative, add REG_NOTE to branch */
6046           REG_NOTES (PREV_INSN (loop_end))
6047             = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6048                        REG_NOTES (PREV_INSN (loop_end)));
6049           bl->nonneg = 1;
6050
6051           return 1;
6052         }
6053
6054       /* If the decrement is 1 and the value was tested as >= 0 before
6055          the loop, then we can safely optimize.  */
6056       for (p = loop_start; p; p = PREV_INSN (p))
6057         {
6058           if (GET_CODE (p) == CODE_LABEL)
6059             break;
6060           if (GET_CODE (p) != JUMP_INSN)
6061             continue;
6062
6063           before_comparison = get_condition_for_loop (p);
6064           if (before_comparison
6065               && XEXP (before_comparison, 0) == bl->biv->dest_reg
6066               && GET_CODE (before_comparison) == LT
6067               && XEXP (before_comparison, 1) == const0_rtx
6068               && ! reg_set_between_p (bl->biv->dest_reg, p, loop_start)
6069               && INTVAL (bl->biv->add_val) == -1)
6070             {
6071               REG_NOTES (PREV_INSN (loop_end))
6072                 = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6073                            REG_NOTES (PREV_INSN (loop_end)));
6074               bl->nonneg = 1;
6075
6076               return 1;
6077             }
6078         }
6079     }
6080   else if (num_mem_sets <= 1)
6081     {
6082       /* Try to change inc to dec, so can apply above optimization.  */
6083       /* Can do this if:
6084          all registers modified are induction variables or invariant,
6085          all memory references have non-overlapping addresses
6086          (obviously true if only one write)
6087          allow 2 insns for the compare/jump at the end of the loop.  */
6088       /* Also, we must avoid any instructions which use both the reversed
6089          biv and another biv.  Such instructions will fail if the loop is
6090          reversed.  We meet this condition by requiring that either
6091          no_use_except_counting is true, or else that there is only
6092          one biv.  */
6093       int num_nonfixed_reads = 0;
6094       /* 1 if the iteration var is used only to count iterations.  */
6095       int no_use_except_counting = 0;
6096       /* 1 if the loop has no memory store, or it has a single memory store
6097          which is reversible.  */
6098       int reversible_mem_store = 1;
6099
6100       for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6101         if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6102           num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
6103
6104       if (bl->giv_count == 0
6105           && ! loop_number_exit_count[uid_loop_num[INSN_UID (loop_start)]])
6106         {
6107           rtx bivreg = regno_reg_rtx[bl->regno];
6108
6109           /* If there are no givs for this biv, and the only exit is the
6110              fall through at the end of the the loop, then
6111              see if perhaps there are no uses except to count.  */
6112           no_use_except_counting = 1;
6113           for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
6114             if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
6115               {
6116                 rtx set = single_set (p);
6117
6118                 if (set && GET_CODE (SET_DEST (set)) == REG
6119                     && REGNO (SET_DEST (set)) == bl->regno)
6120                   /* An insn that sets the biv is okay.  */
6121                   ;
6122                 else if (p == prev_nonnote_insn (prev_nonnote_insn (loop_end))
6123                          || p == prev_nonnote_insn (loop_end))
6124                   /* Don't bother about the end test.  */
6125                   ;
6126                 else if (reg_mentioned_p (bivreg, PATTERN (p)))
6127                   /* Any other use of the biv is no good.  */
6128                   {
6129                     no_use_except_counting = 0;
6130                     break;
6131                   }
6132               }
6133         }
6134
6135       /* If the loop has a single store, and the destination address is
6136          invariant, then we can't reverse the loop, because this address
6137          might then have the wrong value at loop exit.
6138          This would work if the source was invariant also, however, in that
6139          case, the insn should have been moved out of the loop.  */
6140
6141       if (num_mem_sets == 1)
6142         reversible_mem_store
6143           = (! unknown_address_altered
6144              && ! invariant_p (XEXP (loop_store_mems[0], 0)));
6145
6146       /* This code only acts for innermost loops.  Also it simplifies
6147          the memory address check by only reversing loops with
6148          zero or one memory access.
6149          Two memory accesses could involve parts of the same array,
6150          and that can't be reversed.  */
6151
6152       if (num_nonfixed_reads <= 1
6153           && !loop_has_call
6154           && !loop_has_volatile
6155           && reversible_mem_store
6156           && (no_use_except_counting
6157               || ((bl->giv_count + bl->biv_count + num_mem_sets
6158                    + num_movables + 2 == insn_count)
6159                   && (bl == loop_iv_list && bl->next == 0))))
6160         {
6161           rtx tem;
6162
6163           /* Loop can be reversed.  */
6164           if (loop_dump_stream)
6165             fprintf (loop_dump_stream, "Can reverse loop\n");
6166
6167           /* Now check other conditions:
6168              initial_value must be zero,
6169              final_value % add_val == 0, so that when reversed, the
6170              biv will be zero on the last iteration.
6171
6172              This test can probably be improved since +/- 1 in the constant
6173              can be obtained by changing LT to LE and vice versa; this is
6174              confusing.  */
6175
6176           if (comparison && bl->initial_value == const0_rtx
6177               && GET_CODE (XEXP (comparison, 1)) == CONST_INT
6178               /* LE gets turned into LT */
6179               && GET_CODE (comparison) == LT
6180               && (INTVAL (XEXP (comparison, 1))
6181                   % INTVAL (bl->biv->add_val)) == 0)
6182             {
6183               /* Register will always be nonnegative, with value
6184                  0 on last iteration if loop reversed */
6185
6186               /* Save some info needed to produce the new insns.  */
6187               reg = bl->biv->dest_reg;
6188               jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
6189               if (jump_label == pc_rtx)
6190                 jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 2);
6191               new_add_val = GEN_INT (- INTVAL (bl->biv->add_val));
6192
6193               final_value = XEXP (comparison, 1);
6194               start_value = GEN_INT (INTVAL (XEXP (comparison, 1))
6195                                      - INTVAL (bl->biv->add_val));
6196
6197               /* Initialize biv to start_value before loop start.
6198                  The old initializing insn will be deleted as a
6199                  dead store by flow.c.  */
6200               emit_insn_before (gen_move_insn (reg, start_value), loop_start);
6201
6202               /* Add insn to decrement register, and delete insn
6203                  that incremented the register.  */
6204               p = emit_insn_before (gen_add2_insn (reg, new_add_val),
6205                                     bl->biv->insn);
6206               delete_insn (bl->biv->insn);
6207                       
6208               /* Update biv info to reflect its new status.  */
6209               bl->biv->insn = p;
6210               bl->initial_value = start_value;
6211               bl->biv->add_val = new_add_val;
6212
6213               /* Inc LABEL_NUSES so that delete_insn will
6214                  not delete the label.  */
6215               LABEL_NUSES (XEXP (jump_label, 0)) ++;
6216
6217               /* Emit an insn after the end of the loop to set the biv's
6218                  proper exit value if it is used anywhere outside the loop.  */
6219               if ((REGNO_LAST_UID (bl->regno)
6220                    != INSN_UID (PREV_INSN (PREV_INSN (loop_end))))
6221                   || ! bl->init_insn
6222                   || REGNO_FIRST_UID (bl->regno) != INSN_UID (bl->init_insn))
6223                 emit_insn_after (gen_move_insn (reg, final_value),
6224                                  loop_end);
6225
6226               /* Delete compare/branch at end of loop.  */
6227               delete_insn (PREV_INSN (loop_end));
6228               delete_insn (PREV_INSN (loop_end));
6229
6230               /* Add new compare/branch insn at end of loop.  */
6231               start_sequence ();
6232               emit_cmp_insn (reg, const0_rtx, GE, NULL_RTX,
6233                              GET_MODE (reg), 0, 0);
6234               emit_jump_insn (gen_bge (XEXP (jump_label, 0)));
6235               tem = gen_sequence ();
6236               end_sequence ();
6237               emit_jump_insn_before (tem, loop_end);
6238
6239               for (tem = PREV_INSN (loop_end);
6240                    tem && GET_CODE (tem) != JUMP_INSN; tem = PREV_INSN (tem))
6241                 ;
6242               if (tem)
6243                 {
6244                   JUMP_LABEL (tem) = XEXP (jump_label, 0);
6245
6246                   /* Increment of LABEL_NUSES done above.  */
6247                   /* Register is now always nonnegative,
6248                      so add REG_NONNEG note to the branch.  */
6249                   REG_NOTES (tem) = gen_rtx (EXPR_LIST, REG_NONNEG, NULL_RTX,
6250                                              REG_NOTES (tem));
6251                 }
6252
6253               bl->nonneg = 1;
6254
6255               /* Mark that this biv has been reversed.  Each giv which depends
6256                  on this biv, and which is also live past the end of the loop
6257                  will have to be fixed up.  */
6258
6259               bl->reversed = 1;
6260
6261               if (loop_dump_stream)
6262                 fprintf (loop_dump_stream,
6263                          "Reversed loop and added reg_nonneg\n");
6264
6265               return 1;
6266             }
6267         }
6268     }
6269
6270   return 0;
6271 }
6272 \f
6273 /* Verify whether the biv BL appears to be eliminable,
6274    based on the insns in the loop that refer to it.
6275    LOOP_START is the first insn of the loop, and END is the end insn.
6276
6277    If ELIMINATE_P is non-zero, actually do the elimination.
6278
6279    THRESHOLD and INSN_COUNT are from loop_optimize and are used to
6280    determine whether invariant insns should be placed inside or at the
6281    start of the loop.  */
6282
6283 static int
6284 maybe_eliminate_biv (bl, loop_start, end, eliminate_p, threshold, insn_count)
6285      struct iv_class *bl;
6286      rtx loop_start;
6287      rtx end;
6288      int eliminate_p;
6289      int threshold, insn_count;
6290 {
6291   rtx reg = bl->biv->dest_reg;
6292   rtx p;
6293
6294   /* Scan all insns in the loop, stopping if we find one that uses the
6295      biv in a way that we cannot eliminate.  */
6296
6297   for (p = loop_start; p != end; p = NEXT_INSN (p))
6298     {
6299       enum rtx_code code = GET_CODE (p);
6300       rtx where = threshold >= insn_count ? loop_start : p;
6301
6302       if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
6303           && reg_mentioned_p (reg, PATTERN (p))
6304           && ! maybe_eliminate_biv_1 (PATTERN (p), p, bl, eliminate_p, where))
6305         {
6306           if (loop_dump_stream)
6307             fprintf (loop_dump_stream,
6308                      "Cannot eliminate biv %d: biv used in insn %d.\n",
6309                      bl->regno, INSN_UID (p));
6310           break;
6311         }
6312     }
6313
6314   if (p == end)
6315     {
6316       if (loop_dump_stream)
6317         fprintf (loop_dump_stream, "biv %d %s eliminated.\n",
6318                  bl->regno, eliminate_p ? "was" : "can be");
6319       return 1;
6320     }
6321
6322   return 0;
6323 }
6324 \f
6325 /* If BL appears in X (part of the pattern of INSN), see if we can
6326    eliminate its use.  If so, return 1.  If not, return 0.
6327
6328    If BIV does not appear in X, return 1.
6329
6330    If ELIMINATE_P is non-zero, actually do the elimination.  WHERE indicates
6331    where extra insns should be added.  Depending on how many items have been
6332    moved out of the loop, it will either be before INSN or at the start of
6333    the loop.  */
6334
6335 static int
6336 maybe_eliminate_biv_1 (x, insn, bl, eliminate_p, where)
6337      rtx x, insn;
6338      struct iv_class *bl;
6339      int eliminate_p;
6340      rtx where;
6341 {
6342   enum rtx_code code = GET_CODE (x);
6343   rtx reg = bl->biv->dest_reg;
6344   enum machine_mode mode = GET_MODE (reg);
6345   struct induction *v;
6346   rtx arg, new, tem;
6347   int arg_operand;
6348   char *fmt;
6349   int i, j;
6350
6351   switch (code)
6352     {
6353     case REG:
6354       /* If we haven't already been able to do something with this BIV,
6355          we can't eliminate it.  */
6356       if (x == reg)
6357         return 0;
6358       return 1;
6359
6360     case SET:
6361       /* If this sets the BIV, it is not a problem.  */
6362       if (SET_DEST (x) == reg)
6363         return 1;
6364
6365       /* If this is an insn that defines a giv, it is also ok because
6366          it will go away when the giv is reduced.  */
6367       for (v = bl->giv; v; v = v->next_iv)
6368         if (v->giv_type == DEST_REG && SET_DEST (x) == v->dest_reg)
6369           return 1;
6370
6371 #ifdef HAVE_cc0
6372       if (SET_DEST (x) == cc0_rtx && SET_SRC (x) == reg)
6373         {
6374           /* Can replace with any giv that was reduced and
6375              that has (MULT_VAL != 0) and (ADD_VAL == 0).
6376              Require a constant for MULT_VAL, so we know it's nonzero.
6377              ??? We disable this optimization to avoid potential
6378              overflows.  */
6379
6380           for (v = bl->giv; v; v = v->next_iv)
6381             if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6382                 && v->add_val == const0_rtx
6383                 && ! v->ignore && ! v->maybe_dead && v->always_computable
6384                 && v->mode == mode
6385                 && 0)
6386               {
6387                 /* If the giv V had the auto-inc address optimization applied
6388                    to it, and INSN occurs between the giv insn and the biv
6389                    insn, then we must adjust the value used here.
6390                    This is rare, so we don't bother to do so.  */
6391                 if (v->auto_inc_opt
6392                     && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6393                          && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6394                         || (INSN_LUID (v->insn) > INSN_LUID (insn)
6395                             && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6396                   continue;
6397
6398                 if (! eliminate_p)
6399                   return 1;
6400
6401                 /* If the giv has the opposite direction of change,
6402                    then reverse the comparison.  */
6403                 if (INTVAL (v->mult_val) < 0)
6404                   new = gen_rtx (COMPARE, GET_MODE (v->new_reg),
6405                                  const0_rtx, v->new_reg);
6406                 else
6407                   new = v->new_reg;
6408
6409                 /* We can probably test that giv's reduced reg.  */
6410                 if (validate_change (insn, &SET_SRC (x), new, 0))
6411                   return 1;
6412               }
6413
6414           /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0);
6415              replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
6416              Require a constant for MULT_VAL, so we know it's nonzero.
6417              ??? Do this only if ADD_VAL is a pointer to avoid a potential
6418              overflow problem.  */
6419
6420           for (v = bl->giv; v; v = v->next_iv)
6421             if (CONSTANT_P (v->mult_val) && v->mult_val != const0_rtx
6422                 && ! v->ignore && ! v->maybe_dead && v->always_computable
6423                 && v->mode == mode
6424                 && (GET_CODE (v->add_val) == SYMBOL_REF
6425                     || GET_CODE (v->add_val) == LABEL_REF
6426                     || GET_CODE (v->add_val) == CONST
6427                     || (GET_CODE (v->add_val) == REG
6428                         && REGNO_POINTER_FLAG (REGNO (v->add_val)))))
6429               {
6430                 /* If the giv V had the auto-inc address optimization applied
6431                    to it, and INSN occurs between the giv insn and the biv
6432                    insn, then we must adjust the value used here.
6433                    This is rare, so we don't bother to do so.  */
6434                 if (v->auto_inc_opt
6435                     && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6436                          && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6437                         || (INSN_LUID (v->insn) > INSN_LUID (insn)
6438                             && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6439                   continue;
6440
6441                 if (! eliminate_p)
6442                   return 1;
6443
6444                 /* If the giv has the opposite direction of change,
6445                    then reverse the comparison.  */
6446                 if (INTVAL (v->mult_val) < 0)
6447                   new = gen_rtx (COMPARE, VOIDmode, copy_rtx (v->add_val),
6448                                  v->new_reg);
6449                 else
6450                   new = gen_rtx (COMPARE, VOIDmode, v->new_reg,
6451                                  copy_rtx (v->add_val));
6452
6453                 /* Replace biv with the giv's reduced register.  */
6454                 update_reg_last_use (v->add_val, insn);
6455                 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6456                   return 1;
6457
6458                 /* Insn doesn't support that constant or invariant.  Copy it
6459                    into a register (it will be a loop invariant.)  */
6460                 tem = gen_reg_rtx (GET_MODE (v->new_reg));
6461
6462                 emit_insn_before (gen_move_insn (tem, copy_rtx (v->add_val)),
6463                                   where);
6464
6465                 /* Substitute the new register for its invariant value in
6466                    the compare expression. */
6467                 XEXP (new, (INTVAL (v->mult_val) < 0) ? 0 : 1) = tem;
6468                 if (validate_change (insn, &SET_SRC (PATTERN (insn)), new, 0))
6469                   return 1;
6470               }
6471         }
6472 #endif
6473       break;
6474
6475     case COMPARE:
6476     case EQ:  case NE:
6477     case GT:  case GE:  case GTU:  case GEU:
6478     case LT:  case LE:  case LTU:  case LEU:
6479       /* See if either argument is the biv.  */
6480       if (XEXP (x, 0) == reg)
6481         arg = XEXP (x, 1), arg_operand = 1;
6482       else if (XEXP (x, 1) == reg)
6483         arg = XEXP (x, 0), arg_operand = 0;
6484       else
6485         break;
6486
6487       if (CONSTANT_P (arg))
6488         {
6489           /* First try to replace with any giv that has constant positive
6490              mult_val and constant add_val.  We might be able to support
6491              negative mult_val, but it seems complex to do it in general.  */
6492
6493           for (v = bl->giv; v; v = v->next_iv)
6494             if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6495                 && (GET_CODE (v->add_val) == SYMBOL_REF
6496                     || GET_CODE (v->add_val) == LABEL_REF
6497                     || GET_CODE (v->add_val) == CONST
6498                     || (GET_CODE (v->add_val) == REG
6499                         && REGNO_POINTER_FLAG (REGNO (v->add_val))))
6500                 && ! v->ignore && ! v->maybe_dead && v->always_computable
6501                 && v->mode == mode)
6502               {
6503                 /* If the giv V had the auto-inc address optimization applied
6504                    to it, and INSN occurs between the giv insn and the biv
6505                    insn, then we must adjust the value used here.
6506                    This is rare, so we don't bother to do so.  */
6507                 if (v->auto_inc_opt
6508                     && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6509                          && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6510                         || (INSN_LUID (v->insn) > INSN_LUID (insn)
6511                             && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6512                   continue;
6513
6514                 if (! eliminate_p)
6515                   return 1;
6516
6517                 /* Replace biv with the giv's reduced reg.  */
6518                 XEXP (x, 1-arg_operand) = v->new_reg;
6519
6520                 /* If all constants are actually constant integers and
6521                    the derived constant can be directly placed in the COMPARE,
6522                    do so.  */
6523                 if (GET_CODE (arg) == CONST_INT
6524                     && GET_CODE (v->mult_val) == CONST_INT
6525                     && GET_CODE (v->add_val) == CONST_INT
6526                     && validate_change (insn, &XEXP (x, arg_operand),
6527                                         GEN_INT (INTVAL (arg)
6528                                                  * INTVAL (v->mult_val)
6529                                                  + INTVAL (v->add_val)), 0))
6530                   return 1;
6531
6532                 /* Otherwise, load it into a register.  */
6533                 tem = gen_reg_rtx (mode);
6534                 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6535                 if (validate_change (insn, &XEXP (x, arg_operand), tem, 0))
6536                   return 1;
6537
6538                 /* If that failed, put back the change we made above.  */
6539                 XEXP (x, 1-arg_operand) = reg;
6540               }
6541           
6542           /* Look for giv with positive constant mult_val and nonconst add_val.
6543              Insert insns to calculate new compare value.  
6544              ??? Turn this off due to possible overflow.  */
6545
6546           for (v = bl->giv; v; v = v->next_iv)
6547             if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6548                 && ! v->ignore && ! v->maybe_dead && v->always_computable
6549                 && v->mode == mode
6550                 && 0)
6551               {
6552                 rtx tem;
6553
6554                 /* If the giv V had the auto-inc address optimization applied
6555                    to it, and INSN occurs between the giv insn and the biv
6556                    insn, then we must adjust the value used here.
6557                    This is rare, so we don't bother to do so.  */
6558                 if (v->auto_inc_opt
6559                     && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6560                          && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6561                         || (INSN_LUID (v->insn) > INSN_LUID (insn)
6562                             && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6563                   continue;
6564
6565                 if (! eliminate_p)
6566                   return 1;
6567
6568                 tem = gen_reg_rtx (mode);
6569
6570                 /* Replace biv with giv's reduced register.  */
6571                 validate_change (insn, &XEXP (x, 1 - arg_operand),
6572                                  v->new_reg, 1);
6573
6574                 /* Compute value to compare against.  */
6575                 emit_iv_add_mult (arg, v->mult_val, v->add_val, tem, where);
6576                 /* Use it in this insn.  */
6577                 validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6578                 if (apply_change_group ())
6579                   return 1;
6580               }
6581         }
6582       else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
6583         {
6584           if (invariant_p (arg) == 1)
6585             {
6586               /* Look for giv with constant positive mult_val and nonconst
6587                  add_val. Insert insns to compute new compare value. 
6588                  ??? Turn this off due to possible overflow.  */
6589
6590               for (v = bl->giv; v; v = v->next_iv)
6591                 if (CONSTANT_P (v->mult_val) && INTVAL (v->mult_val) > 0
6592                     && ! v->ignore && ! v->maybe_dead && v->always_computable
6593                     && v->mode == mode
6594                     && 0)
6595                   {
6596                     rtx tem;
6597
6598                     /* If the giv V had the auto-inc address optimization applied
6599                        to it, and INSN occurs between the giv insn and the biv
6600                        insn, then we must adjust the value used here.
6601                        This is rare, so we don't bother to do so.  */
6602                     if (v->auto_inc_opt
6603                         && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6604                              && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6605                             || (INSN_LUID (v->insn) > INSN_LUID (insn)
6606                                 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6607                       continue;
6608
6609                     if (! eliminate_p)
6610                       return 1;
6611
6612                     tem = gen_reg_rtx (mode);
6613
6614                     /* Replace biv with giv's reduced register.  */
6615                     validate_change (insn, &XEXP (x, 1 - arg_operand),
6616                                      v->new_reg, 1);
6617
6618                     /* Compute value to compare against.  */
6619                     emit_iv_add_mult (arg, v->mult_val, v->add_val,
6620                                       tem, where);
6621                     validate_change (insn, &XEXP (x, arg_operand), tem, 1);
6622                     if (apply_change_group ())
6623                       return 1;
6624                   }
6625             }
6626
6627           /* This code has problems.  Basically, you can't know when
6628              seeing if we will eliminate BL, whether a particular giv
6629              of ARG will be reduced.  If it isn't going to be reduced,
6630              we can't eliminate BL.  We can try forcing it to be reduced,
6631              but that can generate poor code.
6632
6633              The problem is that the benefit of reducing TV, below should
6634              be increased if BL can actually be eliminated, but this means
6635              we might have to do a topological sort of the order in which
6636              we try to process biv.  It doesn't seem worthwhile to do
6637              this sort of thing now.  */
6638
6639 #if 0
6640           /* Otherwise the reg compared with had better be a biv.  */
6641           if (GET_CODE (arg) != REG
6642               || reg_iv_type[REGNO (arg)] != BASIC_INDUCT)
6643             return 0;
6644
6645           /* Look for a pair of givs, one for each biv,
6646              with identical coefficients.  */
6647           for (v = bl->giv; v; v = v->next_iv)
6648             {
6649               struct induction *tv;
6650
6651               if (v->ignore || v->maybe_dead || v->mode != mode)
6652                 continue;
6653
6654               for (tv = reg_biv_class[REGNO (arg)]->giv; tv; tv = tv->next_iv)
6655                 if (! tv->ignore && ! tv->maybe_dead
6656                     && rtx_equal_p (tv->mult_val, v->mult_val)
6657                     && rtx_equal_p (tv->add_val, v->add_val)
6658                     && tv->mode == mode)
6659                   {
6660                     /* If the giv V had the auto-inc address optimization applied
6661                        to it, and INSN occurs between the giv insn and the biv
6662                        insn, then we must adjust the value used here.
6663                        This is rare, so we don't bother to do so.  */
6664                     if (v->auto_inc_opt
6665                         && ((INSN_LUID (v->insn) < INSN_LUID (insn)
6666                              && INSN_LUID (insn) < INSN_LUID (bl->biv->insn))
6667                             || (INSN_LUID (v->insn) > INSN_LUID (insn)
6668                                 && INSN_LUID (insn) > INSN_LUID (bl->biv->insn))))
6669                       continue;
6670
6671                     if (! eliminate_p)
6672                       return 1;
6673
6674                     /* Replace biv with its giv's reduced reg.  */
6675                     XEXP (x, 1-arg_operand) = v->new_reg;
6676                     /* Replace other operand with the other giv's
6677                        reduced reg.  */
6678                     XEXP (x, arg_operand) = tv->new_reg;
6679                     return 1;
6680                   }
6681             }
6682 #endif
6683         }
6684
6685       /* If we get here, the biv can't be eliminated.  */
6686       return 0;
6687
6688     case MEM:
6689       /* If this address is a DEST_ADDR giv, it doesn't matter if the
6690          biv is used in it, since it will be replaced.  */
6691       for (v = bl->giv; v; v = v->next_iv)
6692         if (v->giv_type == DEST_ADDR && v->location == &XEXP (x, 0))
6693           return 1;
6694       break;
6695     }
6696
6697   /* See if any subexpression fails elimination.  */
6698   fmt = GET_RTX_FORMAT (code);
6699   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6700     {
6701       switch (fmt[i])
6702         {
6703         case 'e':
6704           if (! maybe_eliminate_biv_1 (XEXP (x, i), insn, bl, 
6705                                        eliminate_p, where))
6706             return 0;
6707           break;
6708
6709         case 'E':
6710           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6711             if (! maybe_eliminate_biv_1 (XVECEXP (x, i, j), insn, bl,
6712                                          eliminate_p, where))
6713               return 0;
6714           break;
6715         }
6716     }
6717
6718   return 1;
6719 }  
6720 \f
6721 /* Return nonzero if the last use of REG
6722    is in an insn following INSN in the same basic block.  */
6723
6724 static int
6725 last_use_this_basic_block (reg, insn)
6726      rtx reg;
6727      rtx insn;
6728 {
6729   rtx n;
6730   for (n = insn;
6731        n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
6732        n = NEXT_INSN (n))
6733     {
6734       if (REGNO_LAST_UID (REGNO (reg)) == INSN_UID (n))
6735         return 1;
6736     }
6737   return 0;
6738 }
6739 \f
6740 /* Called via `note_stores' to record the initial value of a biv.  Here we
6741    just record the location of the set and process it later.  */
6742
6743 static void
6744 record_initial (dest, set)
6745      rtx dest;
6746      rtx set;
6747 {
6748   struct iv_class *bl;
6749
6750   if (GET_CODE (dest) != REG
6751       || REGNO (dest) >= max_reg_before_loop
6752       || reg_iv_type[REGNO (dest)] != BASIC_INDUCT)
6753     return;
6754
6755   bl = reg_biv_class[REGNO (dest)];
6756
6757   /* If this is the first set found, record it.  */
6758   if (bl->init_insn == 0)
6759     {
6760       bl->init_insn = note_insn;
6761       bl->init_set = set;
6762     }
6763 }
6764 \f
6765 /* If any of the registers in X are "old" and currently have a last use earlier
6766    than INSN, update them to have a last use of INSN.  Their actual last use
6767    will be the previous insn but it will not have a valid uid_luid so we can't
6768    use it.  */
6769
6770 static void
6771 update_reg_last_use (x, insn)
6772      rtx x;
6773      rtx insn;
6774 {
6775   /* Check for the case where INSN does not have a valid luid.  In this case,
6776      there is no need to modify the regno_last_uid, as this can only happen
6777      when code is inserted after the loop_end to set a pseudo's final value,
6778      and hence this insn will never be the last use of x.  */
6779   if (GET_CODE (x) == REG && REGNO (x) < max_reg_before_loop
6780       && INSN_UID (insn) < max_uid_for_loop
6781       && uid_luid[REGNO_LAST_UID (REGNO (x))] < uid_luid[INSN_UID (insn)])
6782     REGNO_LAST_UID (REGNO (x)) = INSN_UID (insn);
6783   else
6784     {
6785       register int i, j;
6786       register char *fmt = GET_RTX_FORMAT (GET_CODE (x));
6787       for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6788         {
6789           if (fmt[i] == 'e')
6790             update_reg_last_use (XEXP (x, i), insn);
6791           else if (fmt[i] == 'E')
6792             for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6793               update_reg_last_use (XVECEXP (x, i, j), insn);
6794         }
6795     }
6796 }
6797 \f
6798 /* Given a jump insn JUMP, return the condition that will cause it to branch
6799    to its JUMP_LABEL.  If the condition cannot be understood, or is an
6800    inequality floating-point comparison which needs to be reversed, 0 will
6801    be returned.
6802
6803    If EARLIEST is non-zero, it is a pointer to a place where the earliest
6804    insn used in locating the condition was found.  If a replacement test
6805    of the condition is desired, it should be placed in front of that
6806    insn and we will be sure that the inputs are still valid.
6807
6808    The condition will be returned in a canonical form to simplify testing by
6809    callers.  Specifically:
6810
6811    (1) The code will always be a comparison operation (EQ, NE, GT, etc.).
6812    (2) Both operands will be machine operands; (cc0) will have been replaced.
6813    (3) If an operand is a constant, it will be the second operand.
6814    (4) (LE x const) will be replaced with (LT x <const+1>) and similarly
6815        for GE, GEU, and LEU.  */
6816
6817 rtx
6818 get_condition (jump, earliest)
6819      rtx jump;
6820      rtx *earliest;
6821 {
6822   enum rtx_code code;
6823   rtx prev = jump;
6824   rtx set;
6825   rtx tem;
6826   rtx op0, op1;
6827   int reverse_code = 0;
6828   int did_reverse_condition = 0;
6829
6830   /* If this is not a standard conditional jump, we can't parse it.  */
6831   if (GET_CODE (jump) != JUMP_INSN
6832       || ! condjump_p (jump) || simplejump_p (jump))
6833     return 0;
6834
6835   code = GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 0));
6836   op0 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 0);
6837   op1 = XEXP (XEXP (SET_SRC (PATTERN (jump)), 0), 1);
6838
6839   if (earliest)
6840     *earliest = jump;
6841
6842   /* If this branches to JUMP_LABEL when the condition is false, reverse
6843      the condition.  */
6844   if (GET_CODE (XEXP (SET_SRC (PATTERN (jump)), 2)) == LABEL_REF
6845       && XEXP (XEXP (SET_SRC (PATTERN (jump)), 2), 0) == JUMP_LABEL (jump))
6846     code = reverse_condition (code), did_reverse_condition ^= 1;
6847
6848   /* If we are comparing a register with zero, see if the register is set
6849      in the previous insn to a COMPARE or a comparison operation.  Perform
6850      the same tests as a function of STORE_FLAG_VALUE as find_comparison_args
6851      in cse.c  */
6852
6853   while (GET_RTX_CLASS (code) == '<' && op1 == CONST0_RTX (GET_MODE (op0)))
6854     {
6855       /* Set non-zero when we find something of interest.  */
6856       rtx x = 0;
6857
6858 #ifdef HAVE_cc0
6859       /* If comparison with cc0, import actual comparison from compare
6860          insn.  */
6861       if (op0 == cc0_rtx)
6862         {
6863           if ((prev = prev_nonnote_insn (prev)) == 0
6864               || GET_CODE (prev) != INSN
6865               || (set = single_set (prev)) == 0
6866               || SET_DEST (set) != cc0_rtx)
6867             return 0;
6868
6869           op0 = SET_SRC (set);
6870           op1 = CONST0_RTX (GET_MODE (op0));
6871           if (earliest)
6872             *earliest = prev;
6873         }
6874 #endif
6875
6876       /* If this is a COMPARE, pick up the two things being compared.  */
6877       if (GET_CODE (op0) == COMPARE)
6878         {
6879           op1 = XEXP (op0, 1);
6880           op0 = XEXP (op0, 0);
6881           continue;
6882         }
6883       else if (GET_CODE (op0) != REG)
6884         break;
6885
6886       /* Go back to the previous insn.  Stop if it is not an INSN.  We also
6887          stop if it isn't a single set or if it has a REG_INC note because
6888          we don't want to bother dealing with it.  */
6889
6890       if ((prev = prev_nonnote_insn (prev)) == 0
6891           || GET_CODE (prev) != INSN
6892           || FIND_REG_INC_NOTE (prev, 0)
6893           || (set = single_set (prev)) == 0)
6894         break;
6895
6896       /* If this is setting OP0, get what it sets it to if it looks
6897          relevant.  */
6898       if (rtx_equal_p (SET_DEST (set), op0))
6899         {
6900           enum machine_mode inner_mode = GET_MODE (SET_SRC (set));
6901
6902           if ((GET_CODE (SET_SRC (set)) == COMPARE
6903                || (((code == NE
6904                      || (code == LT
6905                          && GET_MODE_CLASS (inner_mode) == MODE_INT
6906                          && (GET_MODE_BITSIZE (inner_mode)
6907                              <= HOST_BITS_PER_WIDE_INT)
6908                          && (STORE_FLAG_VALUE
6909                              & ((HOST_WIDE_INT) 1
6910                                 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6911 #ifdef FLOAT_STORE_FLAG_VALUE
6912                      || (code == LT
6913                          && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6914                          && FLOAT_STORE_FLAG_VALUE < 0)
6915 #endif
6916                      ))
6917                    && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')))
6918             x = SET_SRC (set);
6919           else if (((code == EQ
6920                      || (code == GE
6921                          && (GET_MODE_BITSIZE (inner_mode)
6922                              <= HOST_BITS_PER_WIDE_INT)
6923                          && GET_MODE_CLASS (inner_mode) == MODE_INT
6924                          && (STORE_FLAG_VALUE
6925                              & ((HOST_WIDE_INT) 1
6926                                 << (GET_MODE_BITSIZE (inner_mode) - 1))))
6927 #ifdef FLOAT_STORE_FLAG_VALUE
6928                      || (code == GE
6929                          && GET_MODE_CLASS (inner_mode) == MODE_FLOAT
6930                          && FLOAT_STORE_FLAG_VALUE < 0)
6931 #endif
6932                      ))
6933                    && GET_RTX_CLASS (GET_CODE (SET_SRC (set))) == '<')
6934             {
6935               /* We might have reversed a LT to get a GE here.  But this wasn't
6936                  actually the comparison of data, so we don't flag that we
6937                  have had to reverse the condition.  */
6938               did_reverse_condition ^= 1;
6939               reverse_code = 1;
6940               x = SET_SRC (set);
6941             }
6942           else
6943             break;
6944         }
6945
6946       else if (reg_set_p (op0, prev))
6947         /* If this sets OP0, but not directly, we have to give up.  */
6948         break;
6949
6950       if (x)
6951         {
6952           if (GET_RTX_CLASS (GET_CODE (x)) == '<')
6953             code = GET_CODE (x);
6954           if (reverse_code)
6955             {
6956               code = reverse_condition (code);
6957               did_reverse_condition ^= 1;
6958               reverse_code = 0;
6959             }
6960
6961           op0 = XEXP (x, 0), op1 = XEXP (x, 1);
6962           if (earliest)
6963             *earliest = prev;
6964         }
6965     }
6966
6967   /* If constant is first, put it last.  */
6968   if (CONSTANT_P (op0))
6969     code = swap_condition (code), tem = op0, op0 = op1, op1 = tem;
6970
6971   /* If OP0 is the result of a comparison, we weren't able to find what
6972      was really being compared, so fail.  */
6973   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6974     return 0;
6975
6976   /* Canonicalize any ordered comparison with integers involving equality
6977      if we can do computations in the relevant mode and we do not
6978      overflow.  */
6979
6980   if (GET_CODE (op1) == CONST_INT
6981       && GET_MODE (op0) != VOIDmode
6982       && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT)
6983     {
6984       HOST_WIDE_INT const_val = INTVAL (op1);
6985       unsigned HOST_WIDE_INT uconst_val = const_val;
6986       unsigned HOST_WIDE_INT max_val
6987         = (unsigned HOST_WIDE_INT) GET_MODE_MASK (GET_MODE (op0));
6988
6989       switch (code)
6990         {
6991         case LE:
6992           if (const_val != max_val >> 1)
6993             code = LT,  op1 = GEN_INT (const_val + 1);
6994           break;
6995
6996         case GE:
6997           if (const_val
6998               != (((HOST_WIDE_INT) 1
6999                    << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
7000             code = GT, op1 = GEN_INT (const_val - 1);
7001           break;
7002
7003         case LEU:
7004           if (uconst_val != max_val)
7005             code = LTU, op1 = GEN_INT (uconst_val + 1);
7006           break;
7007
7008         case GEU:
7009           if (uconst_val != 0)
7010             code = GTU, op1 = GEN_INT (uconst_val - 1);
7011           break;
7012         }
7013     }
7014
7015   /* If this was floating-point and we reversed anything other than an
7016      EQ or NE, return zero.  */
7017   if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
7018       && did_reverse_condition && code != NE && code != EQ
7019       && ! flag_fast_math
7020       && GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
7021     return 0;
7022
7023 #ifdef HAVE_cc0
7024   /* Never return CC0; return zero instead.  */
7025   if (op0 == cc0_rtx)
7026     return 0;
7027 #endif
7028
7029   return gen_rtx (code, VOIDmode, op0, op1);
7030 }
7031
7032 /* Similar to above routine, except that we also put an invariant last
7033    unless both operands are invariants.  */
7034
7035 rtx
7036 get_condition_for_loop (x)
7037      rtx x;
7038 {
7039   rtx comparison = get_condition (x, NULL_PTR);
7040
7041   if (comparison == 0
7042       || ! invariant_p (XEXP (comparison, 0))
7043       || invariant_p (XEXP (comparison, 1)))
7044     return comparison;
7045
7046   return gen_rtx (swap_condition (GET_CODE (comparison)), VOIDmode,
7047                   XEXP (comparison, 1), XEXP (comparison, 0));
7048 }
7049
7050 #ifdef HAIFA
7051 /* Analyze a loop in order to instrument it with the use of count register.
7052    loop_start and loop_end are the first and last insns of the loop.
7053    This function works in cooperation with insert_bct ().
7054    loop_can_insert_bct[loop_num] is set according to whether the optimization
7055    is applicable to the loop.  When it is applicable, the following variables
7056    are also set:
7057     loop_start_value[loop_num]
7058     loop_comparison_value[loop_num]
7059     loop_increment[loop_num]
7060     loop_comparison_code[loop_num] */
7061
7062 static
7063 void analyze_loop_iterations (loop_start, loop_end)
7064   rtx loop_start, loop_end;
7065 {
7066   rtx comparison, comparison_value;
7067   rtx iteration_var, initial_value, increment;
7068   enum rtx_code comparison_code;
7069
7070   rtx last_loop_insn;
7071   rtx insn;
7072   int i;
7073
7074   /* loop_variable mode */
7075   enum machine_mode original_mode;
7076
7077   /* find the number of the loop */
7078   int loop_num = loop_number (loop_start, loop_end);
7079
7080   /* we change our mind only when we are sure that loop will be instrumented */
7081   loop_can_insert_bct[loop_num] = 0;
7082
7083   /* debugging: do we wish to instrument this loop? */
7084   if ( !check_bct_param () )
7085     return;
7086
7087   /* is the optimization suppressed.  */
7088   if ( !flag_branch_on_count_reg )
7089     return;
7090
7091   /* make sure that count-reg is not in use */
7092   if (loop_used_count_register[loop_num]){
7093     if (loop_dump_stream)
7094       fprintf (loop_dump_stream,
7095               "analyze_loop_iterations %d: BCT instrumentation failed: count register already in use\n",
7096               loop_num);
7097     return;
7098   }
7099
7100   /* make sure that the function has no indirect jumps.  */
7101   if (indirect_jump_in_function){
7102     if (loop_dump_stream)
7103       fprintf (loop_dump_stream,
7104               "analyze_loop_iterations %d: BCT instrumentation failed: indirect jump in function\n",
7105               loop_num);
7106     return;
7107   }
7108
7109   /* make sure that the last loop insn is a conditional jump */
7110   last_loop_insn = PREV_INSN (loop_end);
7111   if (GET_CODE (last_loop_insn) != JUMP_INSN || !condjump_p (last_loop_insn)) {
7112     if (loop_dump_stream)
7113       fprintf (loop_dump_stream,
7114               "analyze_loop_iterations %d: BCT instrumentation failed: invalid jump at loop end\n",
7115               loop_num);
7116     return;
7117   }
7118
7119   /* First find the iteration variable.  If the last insn is a conditional
7120      branch, and the insn preceding it tests a register value, make that
7121      register the iteration variable.  */
7122
7123   /* We used to use prev_nonnote_insn here, but that fails because it might
7124      accidentally get the branch for a contained loop if the branch for this
7125      loop was deleted.  We can only trust branches immediately before the
7126      loop_end.  */
7127
7128   comparison = get_condition_for_loop (last_loop_insn);
7129   /* ??? Get_condition may switch position of induction variable and
7130      invariant register when it canonicalizes the comparison.  */
7131
7132   if (comparison == 0) {
7133     if (loop_dump_stream)
7134       fprintf (loop_dump_stream,
7135               "analyze_loop_iterations %d: BCT instrumentation failed: comparison not found\n",
7136               loop_num);
7137     return;
7138   }
7139
7140   comparison_code = GET_CODE (comparison);
7141   iteration_var = XEXP (comparison, 0);
7142   comparison_value = XEXP (comparison, 1);
7143
7144   original_mode = GET_MODE (iteration_var);
7145   if (GET_MODE_CLASS (original_mode) != MODE_INT
7146       || GET_MODE_SIZE (original_mode) != UNITS_PER_WORD) {
7147     if (loop_dump_stream)
7148       fprintf (loop_dump_stream,
7149               "analyze_loop_iterations %d: BCT Instrumentation failed: loop variable not integer\n",
7150               loop_num);
7151     return;
7152   }
7153
7154   /* get info about loop bounds and increment */
7155   iteration_info (iteration_var, &initial_value, &increment,
7156                   loop_start, loop_end);
7157
7158   /* make sure that all required loop data were found */
7159   if (!(initial_value && increment && comparison_value
7160         && invariant_p (comparison_value) && invariant_p (increment)
7161         && ! indirect_jump_in_function))
7162     {
7163       if (loop_dump_stream) {
7164         fprintf (loop_dump_stream,
7165                 "analyze_loop_iterations %d: BCT instrumentation failed because of wrong loop: ", loop_num);
7166         if (!(initial_value && increment && comparison_value)) {
7167           fprintf (loop_dump_stream, "\tbounds not available: ");
7168           if ( ! initial_value )
7169             fprintf (loop_dump_stream, "initial ");
7170           if ( ! increment )
7171             fprintf (loop_dump_stream, "increment ");
7172           if ( ! comparison_value )
7173             fprintf (loop_dump_stream, "comparison ");
7174           fprintf (loop_dump_stream, "\n");
7175         }
7176         if (!invariant_p (comparison_value) || !invariant_p (increment))
7177           fprintf (loop_dump_stream, "\tloop bounds not invariant\n");
7178       }
7179       return;
7180     }
7181
7182   /* make sure that the increment is constant */
7183   if (GET_CODE (increment) != CONST_INT) {
7184     if (loop_dump_stream)
7185       fprintf (loop_dump_stream,
7186               "analyze_loop_iterations %d: instrumentation failed: not arithmetic loop\n",
7187               loop_num);
7188     return;
7189   }
7190
7191   /* make sure that the loop contains neither function call, nor jump on table.
7192      (the count register might be altered by the called function, and might
7193      be used for a branch on table).  */
7194   for (insn = loop_start; insn && insn != loop_end; insn = NEXT_INSN (insn)) {
7195     if (GET_CODE (insn) == CALL_INSN){
7196       if (loop_dump_stream)
7197         fprintf (loop_dump_stream,
7198                 "analyze_loop_iterations %d: BCT instrumentation failed: function call in the loop\n",
7199                 loop_num);
7200       return;
7201     }
7202
7203     if (GET_CODE (insn) == JUMP_INSN
7204        && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
7205            || GET_CODE (PATTERN (insn)) == ADDR_VEC)){
7206       if (loop_dump_stream)
7207         fprintf (loop_dump_stream,
7208                 "analyze_loop_iterations %d: BCT instrumentation failed: computed branch in the loop\n",
7209                 loop_num);
7210       return;
7211     }
7212   }
7213
7214   /* At this point, we are sure that the loop can be instrumented with BCT.
7215      Some of the loops, however, will not be instrumented - the final decision
7216      is taken by insert_bct () */
7217   if (loop_dump_stream)
7218     fprintf (loop_dump_stream,
7219             "analyze_loop_iterations: loop (luid =%d) can be BCT instrumented.\n",
7220             loop_num);
7221
7222   /* mark all enclosing loops that they cannot use count register */
7223   /* ???: In fact, since insert_bct may decide not to instrument this loop,
7224      marking here may prevent instrumenting an enclosing loop that could
7225     actually be instrumented.  But since this is rare, it is safer to mark
7226     here in case the order of calling  (analyze/insert)_bct would be changed.  */
7227   for (i=loop_num; i != -1; i = loop_outer_loop[i])
7228     loop_used_count_register[i] = 1;
7229
7230   /* Set data structures which will be used by the instrumentation phase */
7231   loop_start_value[loop_num] = initial_value;
7232   loop_comparison_value[loop_num] = comparison_value;
7233   loop_increment[loop_num] = increment;
7234   loop_comparison_code[loop_num] = comparison_code;
7235   loop_can_insert_bct[loop_num] = 1;
7236 }
7237
7238
7239 /* instrument loop for insertion of bct instruction.  We distinguish between
7240  loops with compile-time bounds, to those with run-time bounds.  The loop
7241  behaviour is analized according to the following characteristics/variables:
7242  ; Input variables:
7243  ;   comparison-value: the value to which the iteration counter is compared.
7244  ;   initial-value: iteration-counter initial value.
7245  ;   increment: iteration-counter increment.
7246  ; Computed variables:
7247  ;   increment-direction: the sign of the increment.
7248  ;   compare-direction: '1' for GT, GTE, '-1' for LT, LTE, '0' for NE.
7249  ;   range-direction: sign (comparison-value - initial-value)
7250  We give up on the following cases:
7251  ; loop variable overflow.
7252  ; run-time loop bounds with comparison code NE.
7253  */
7254
7255 static void
7256 insert_bct (loop_start, loop_end)
7257      rtx loop_start, loop_end;
7258 {
7259   rtx initial_value, comparison_value, increment;
7260   enum rtx_code comparison_code;
7261
7262   int increment_direction, compare_direction;
7263   int unsigned_p = 0;
7264
7265   /* if the loop condition is <= or >=, the number of iteration
7266       is 1 more than the range of the bounds of the loop */
7267   int add_iteration = 0;
7268
7269   /* the only machine mode we work with - is the integer of the size that the
7270      machine has */
7271   enum machine_mode loop_var_mode = SImode;
7272
7273   int loop_num = loop_number (loop_start, loop_end);
7274
7275   /* get loop-variables. No need to check that these are valid - already
7276      checked in analyze_loop_iterations ().  */
7277   comparison_code = loop_comparison_code[loop_num];
7278   initial_value = loop_start_value[loop_num];
7279   comparison_value = loop_comparison_value[loop_num];
7280   increment = loop_increment[loop_num];
7281
7282   /* check analyze_loop_iterations decision for this loop.  */
7283   if (! loop_can_insert_bct[loop_num]){
7284     if (loop_dump_stream)
7285       fprintf (loop_dump_stream,
7286               "insert_bct: [%d] - was decided not to instrument by analyze_loop_iterations ()\n",
7287               loop_num);
7288     return;
7289   }
7290
7291   /* make sure that the loop was not fully unrolled.  */
7292   if (loop_unroll_factor[loop_num] == -1){
7293     if (loop_dump_stream)
7294       fprintf (loop_dump_stream, "insert_bct %d: was completely unrolled\n", loop_num);
7295     return;
7296   }
7297
7298   /* make sure that the last loop insn is a conditional jump .
7299      This check is repeated from analyze_loop_iterations (),
7300      because unrolling might have changed that.  */
7301   if (GET_CODE (PREV_INSN (loop_end)) != JUMP_INSN
7302       || !condjump_p (PREV_INSN (loop_end))) {
7303     if (loop_dump_stream)
7304       fprintf (loop_dump_stream,
7305               "insert_bct: not instrumenting BCT because of invalid branch\n");
7306     return;
7307   }
7308
7309   /* fix increment in case loop was unrolled.  */
7310   if (loop_unroll_factor[loop_num] > 1)
7311     increment = GEN_INT ( INTVAL (increment) * loop_unroll_factor[loop_num] );
7312
7313   /* determine properties and directions of the loop */
7314   increment_direction = (INTVAL (increment) > 0) ? 1:-1;
7315   switch ( comparison_code ) {
7316   case LEU:
7317     unsigned_p = 1;
7318     /* fallthrough */
7319   case LE:
7320     compare_direction = 1;
7321     add_iteration = 1;
7322     break;
7323   case GEU:
7324     unsigned_p = 1;
7325     /* fallthrough */
7326   case GE:
7327     compare_direction = -1;
7328     add_iteration = 1;
7329     break;
7330   case EQ:
7331     /* in this case we cannot know the number of iterations */
7332     if (loop_dump_stream)
7333       fprintf (loop_dump_stream,
7334               "insert_bct: %d: loop cannot be instrumented: == in condition\n",
7335               loop_num);
7336     return;
7337   case LTU:
7338     unsigned_p = 1;
7339     /* fallthrough */
7340   case LT:
7341     compare_direction = 1;
7342     break;
7343   case GTU:
7344     unsigned_p = 1;
7345     /* fallthrough */
7346   case GT:
7347     compare_direction = -1;
7348     break;
7349   case NE:
7350     compare_direction = 0;
7351     break;
7352   default:
7353     abort ();
7354   }
7355
7356
7357   /* make sure that the loop does not end by an overflow */
7358   if (compare_direction != increment_direction) {
7359     if (loop_dump_stream)
7360       fprintf (loop_dump_stream,
7361               "insert_bct: %d: loop cannot be instrumented: terminated by overflow\n",
7362               loop_num);
7363     return;
7364   }
7365
7366   /* try to instrument the loop.  */
7367
7368   /* Handle the simpler case, where the bounds are known at compile time.  */
7369   if (GET_CODE (initial_value) == CONST_INT && GET_CODE (comparison_value) == CONST_INT)
7370     {
7371       int n_iterations;
7372       int increment_value_abs = INTVAL (increment) * increment_direction;
7373
7374       /* check the relation between compare-val and initial-val */
7375       int difference = INTVAL (comparison_value) - INTVAL (initial_value);
7376       int range_direction = (difference > 0) ? 1 : -1;
7377
7378       /* make sure the loop executes enough iterations to gain from BCT */
7379       if (difference > -3 && difference < 3) {
7380         if (loop_dump_stream)
7381           fprintf (loop_dump_stream,
7382                   "insert_bct: loop %d not BCT instrumented: too small iteration count.\n",
7383                   loop_num);
7384         return;
7385       }
7386
7387       /* make sure that the loop executes at least once */
7388       if ((range_direction ==  1 && compare_direction == -1)
7389           || (range_direction == -1 && compare_direction ==  1))
7390         {
7391           if (loop_dump_stream)
7392             fprintf (loop_dump_stream,
7393                     "insert_bct: loop %d: does not iterate even once. Not instrumenting.\n",
7394                     loop_num);
7395           return;
7396         }
7397
7398       /* make sure that the loop does not end by an overflow (in compile time
7399          bounds we must have an additional check for overflow, because here
7400          we also support the compare code of 'NE'.  */
7401       if (comparison_code == NE
7402           && increment_direction != range_direction) {
7403         if (loop_dump_stream)
7404           fprintf (loop_dump_stream,
7405                   "insert_bct (compile time bounds): %d: loop not instrumented: terminated by overflow\n",
7406                   loop_num);
7407         return;
7408       }
7409
7410       /* Determine the number of iterations by:
7411          ;
7412          ;                  compare-val - initial-val + (increment -1) + additional-iteration
7413          ; num_iterations = -----------------------------------------------------------------
7414          ;                                           increment
7415          */
7416       difference = (range_direction > 0) ? difference : -difference;
7417 #if 0
7418       fprintf (stderr, "difference is: %d\n", difference); /* @*/
7419       fprintf (stderr, "increment_value_abs is: %d\n", increment_value_abs); /* @*/
7420       fprintf (stderr, "add_iteration is: %d\n", add_iteration); /* @*/
7421       fprintf (stderr, "INTVAL (comparison_value) is: %d\n", INTVAL (comparison_value)); /* @*/
7422       fprintf (stderr, "INTVAL (initial_value) is: %d\n", INTVAL (initial_value)); /* @*/
7423 #endif
7424
7425       if (increment_value_abs == 0) {
7426         fprintf (stderr, "insert_bct: error: increment == 0 !!!\n");
7427         abort ();
7428       }
7429       n_iterations = (difference + increment_value_abs - 1 + add_iteration)
7430         / increment_value_abs;
7431
7432 #if 0
7433       fprintf (stderr, "number of iterations is: %d\n", n_iterations); /* @*/
7434 #endif
7435       instrument_loop_bct (loop_start, loop_end, GEN_INT (n_iterations));
7436
7437       /* Done with this loop.  */
7438       return;
7439     }
7440
7441   /* Handle the more complex case, that the bounds are NOT known at compile time.  */
7442   /* In this case we generate run_time calculation of the number of iterations */
7443
7444   /* With runtime bounds, if the compare is of the form '!=' we give up */
7445   if (comparison_code == NE) {
7446     if (loop_dump_stream)
7447       fprintf (loop_dump_stream,
7448               "insert_bct: fail for loop %d: runtime bounds with != comparison\n",
7449               loop_num);
7450     return;
7451   }
7452
7453   else {
7454     /* We rely on the existence of run-time guard to ensure that the
7455        loop executes at least once.  */
7456     rtx sequence;
7457     rtx iterations_num_reg;
7458
7459     int increment_value_abs = INTVAL (increment) * increment_direction;
7460
7461     /* make sure that the increment is a power of two, otherwise (an
7462        expensive) divide is needed.  */
7463     if (exact_log2 (increment_value_abs) == -1)
7464       {
7465         if (loop_dump_stream)
7466           fprintf (loop_dump_stream,
7467                   "insert_bct: not instrumenting BCT because the increment is not power of 2\n");
7468         return;
7469       }
7470
7471     /* compute the number of iterations */
7472     start_sequence ();
7473     {
7474       /* CYGNUS LOCAL: HAIFA bug fix */
7475       rtx temp_reg;
7476
7477       /* Again, the number of iterations is calculated by:
7478          ;
7479          ;                  compare-val - initial-val + (increment -1) + additional-iteration
7480          ; num_iterations = -----------------------------------------------------------------
7481          ;                                           increment
7482          */
7483       /* ??? Do we have to call copy_rtx here before passing rtx to
7484          expand_binop?  */
7485       if (compare_direction > 0) {
7486         /* <, <= :the loop variable is increasing */
7487         temp_reg = expand_binop (loop_var_mode, sub_optab, comparison_value,
7488                                  initial_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7489       }
7490       else {
7491         temp_reg = expand_binop (loop_var_mode, sub_optab, initial_value,
7492                                  comparison_value, NULL_RTX, 0, OPTAB_LIB_WIDEN);
7493       }
7494
7495       if (increment_value_abs - 1 + add_iteration != 0)
7496         temp_reg = expand_binop (loop_var_mode, add_optab, temp_reg,
7497                                  GEN_INT (increment_value_abs - 1 + add_iteration),
7498                                  NULL_RTX, 0, OPTAB_LIB_WIDEN);
7499
7500       if (increment_value_abs != 1)
7501         {
7502           /* ??? This will generate an expensive divide instruction for
7503              most targets.  The original authors apparently expected this
7504              to be a shift, since they test for power-of-2 divisors above,
7505              but just naively generating a divide instruction will not give 
7506              a shift.  It happens to work for the PowerPC target because
7507              the rs6000.md file has a divide pattern that emits shifts.
7508              It will probably not work for any other target.  */
7509           iterations_num_reg = expand_binop (loop_var_mode, sdiv_optab,
7510                                              temp_reg,
7511                                              GEN_INT (increment_value_abs),
7512                                              NULL_RTX, 0, OPTAB_LIB_WIDEN);
7513         }
7514       else
7515         iterations_num_reg = temp_reg;
7516       /* END CYGNUS LOCAL: HAIFA bug fix */
7517     }
7518     sequence = gen_sequence ();
7519     end_sequence ();
7520     emit_insn_before (sequence, loop_start);
7521     instrument_loop_bct (loop_start, loop_end, iterations_num_reg);
7522   }
7523 }
7524
7525 /* instrument loop by inserting a bct in it. This is done in the following way:
7526    1. A new register is created and assigned the hard register number of the count
7527     register.
7528    2. In the head of the loop the new variable is initialized by the value passed in the
7529     loop_num_iterations parameter.
7530    3. At the end of the loop, comparison of the register with 0 is generated.
7531     The created comparison follows the pattern defined for the
7532     decrement_and_branch_on_count insn, so this insn will be generated in assembly
7533     generation phase.
7534    4. The compare&branch on the old variable is deleted. So, if the loop-variable was
7535     not used elsewhere, it will be eliminated by data-flow analisys.  */
7536
7537 static void
7538 instrument_loop_bct (loop_start, loop_end, loop_num_iterations)
7539      rtx loop_start, loop_end;
7540      rtx loop_num_iterations;
7541 {
7542   rtx temp_reg1, temp_reg2;
7543   rtx start_label;
7544
7545   rtx sequence;
7546   enum machine_mode loop_var_mode = SImode;
7547
7548 #ifdef HAVE_decrement_and_branch_on_count
7549   if (HAVE_decrement_and_branch_on_count)
7550     {
7551       if (loop_dump_stream)
7552         fprintf (loop_dump_stream, "Loop: Inserting BCT\n");
7553
7554       /* eliminate the check on the old variable */
7555       delete_insn (PREV_INSN (loop_end));
7556       delete_insn (PREV_INSN (loop_end));
7557
7558       /* insert the label which will delimit the start of the loop */
7559       start_label = gen_label_rtx ();
7560       emit_label_after (start_label, loop_start);
7561
7562       /* insert initialization of the count register into the loop header */
7563       start_sequence ();
7564       temp_reg1 = gen_reg_rtx (loop_var_mode);
7565       emit_insn (gen_move_insn (temp_reg1, loop_num_iterations));
7566
7567       /* this will be count register */
7568       temp_reg2 = gen_rtx (REG, loop_var_mode, COUNT_REGISTER_REGNUM);
7569       /* we have to move the value to the count register from an GPR
7570          because rtx pointed to by loop_num_iterations could contain
7571          expression which cannot be moved into count register */
7572       emit_insn (gen_move_insn (temp_reg2, temp_reg1));
7573
7574       sequence = gen_sequence ();
7575       end_sequence ();
7576       emit_insn_after (sequence, loop_start);
7577
7578       /* insert new comparison on the count register instead of the
7579          old one, generating the needed BCT pattern (that will be
7580          later recognized by assembly generation phase).  */
7581       emit_jump_insn_before (gen_decrement_and_branch_on_count (temp_reg2, start_label),
7582                              loop_end);
7583       LABEL_NUSES (start_label)++;
7584     }
7585
7586 #endif /* HAVE_decrement_and_branch_on_count */
7587 }
7588
7589 /* calculate the uid of the given loop */
7590 int
7591 loop_number (loop_start, loop_end)
7592      rtx loop_start, loop_end;
7593 {
7594   int loop_num = -1;
7595
7596   /* assume that this insn contains the LOOP_START
7597      note, so it will not be changed by the loop unrolling */
7598   loop_num = uid_loop_num[INSN_UID (loop_start)];
7599   /* sanity check - should never happen */
7600   if (loop_num == -1)
7601     abort ();
7602
7603   return loop_num;
7604 }
7605
7606 /* scan the function and determine whether it has indirect (computed) jump */
7607 static int
7608 indirect_jump_in_function_p (start)
7609      rtx start;
7610 {
7611   rtx insn;
7612   int is_indirect_jump = 0;
7613
7614   for (insn = start; insn; insn = NEXT_INSN (insn)) {
7615     if (GET_CODE (insn) == JUMP_INSN) {
7616       if (GET_CODE (PATTERN (insn)) == SET) {
7617         rtx insn_work_code = XEXP (PATTERN (insn), 1);
7618
7619         if (GET_CODE (insn_work_code) == LABEL_REF)
7620           continue;
7621         if (GET_CODE (insn_work_code) == IF_THEN_ELSE) {
7622           rtx jump_target = XEXP (insn_work_code, 1);
7623
7624           if (jump_target == pc_rtx
7625              || (GET_CODE (jump_target) == (enum rtx_code)LABEL_REF))
7626             continue;
7627         }
7628       }
7629       is_indirect_jump = 1;
7630     }
7631   }
7632   return is_indirect_jump;
7633 }
7634
7635 /* debugging: fix_bct_param () is called from toplev.c upon detection
7636    of the -fbct-***-N options.  */
7637 int
7638 fix_bct_param (param, val)
7639      char *param, *val;
7640 {
7641   if ( !strcmp (param, "max") )
7642     dbg_bct_max = atoi (val);
7643   else if ( !strcmp (param, "min") )
7644     dbg_bct_min = atoi (val);
7645 }
7646
7647 /* debugging: return 1 if the loop should be instrumented,
7648    according to bct-min/max.  */
7649 static int
7650 check_bct_param ()
7651 {
7652   static int dbg_bct_num = 0;
7653
7654   dbg_bct_num++;
7655   if (dbg_bct_num > dbg_bct_min || dbg_bct_min == -1)
7656     if (dbg_bct_num <= dbg_bct_max || dbg_bct_max == -1)
7657       return 1;
7658   return 0;
7659 }
7660 #endif  /* HAIFA */
7661 /* END CYGNUS LOCAL haifa */