OSDN Git Service

* Makefile.in (local-distclean): Also remove fastjar.
[pf3gnuchains/gcc-fork.git] / gcc / unroll.c
1 /* Try to unroll loops, and split induction variables.
2    Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 1999, 2000
3    Free Software Foundation, Inc.
4    Contributed by James E. Wilson, Cygnus Support/UC Berkeley.
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Try to unroll a loop, and split induction variables.
24
25    Loops for which the number of iterations can be calculated exactly are
26    handled specially.  If the number of iterations times the insn_count is
27    less than MAX_UNROLLED_INSNS, then the loop is unrolled completely.
28    Otherwise, we try to unroll the loop a number of times modulo the number
29    of iterations, so that only one exit test will be needed.  It is unrolled
30    a number of times approximately equal to MAX_UNROLLED_INSNS divided by
31    the insn count.
32
33    Otherwise, if the number of iterations can be calculated exactly at
34    run time, and the loop is always entered at the top, then we try to
35    precondition the loop.  That is, at run time, calculate how many times
36    the loop will execute, and then execute the loop body a few times so
37    that the remaining iterations will be some multiple of 4 (or 2 if the
38    loop is large).  Then fall through to a loop unrolled 4 (or 2) times,
39    with only one exit test needed at the end of the loop.
40
41    Otherwise, if the number of iterations can not be calculated exactly,
42    not even at run time, then we still unroll the loop a number of times
43    approximately equal to MAX_UNROLLED_INSNS divided by the insn count,
44    but there must be an exit test after each copy of the loop body.
45
46    For each induction variable, which is dead outside the loop (replaceable)
47    or for which we can easily calculate the final value, if we can easily
48    calculate its value at each place where it is set as a function of the
49    current loop unroll count and the variable's value at loop entry, then
50    the induction variable is split into `N' different variables, one for
51    each copy of the loop body.  One variable is live across the backward
52    branch, and the others are all calculated as a function of this variable.
53    This helps eliminate data dependencies, and leads to further opportunities
54    for cse.  */
55
56 /* Possible improvements follow:  */
57
58 /* ??? Add an extra pass somewhere to determine whether unrolling will
59    give any benefit.  E.g. after generating all unrolled insns, compute the
60    cost of all insns and compare against cost of insns in rolled loop.
61
62    - On traditional architectures, unrolling a non-constant bound loop
63      is a win if there is a giv whose only use is in memory addresses, the
64      memory addresses can be split, and hence giv increments can be
65      eliminated.
66    - It is also a win if the loop is executed many times, and preconditioning
67      can be performed for the loop.
68    Add code to check for these and similar cases.  */
69
70 /* ??? Improve control of which loops get unrolled.  Could use profiling
71    info to only unroll the most commonly executed loops.  Perhaps have
72    a user specifyable option to control the amount of code expansion,
73    or the percent of loops to consider for unrolling.  Etc.  */
74
75 /* ??? Look at the register copies inside the loop to see if they form a
76    simple permutation.  If so, iterate the permutation until it gets back to
77    the start state.  This is how many times we should unroll the loop, for
78    best results, because then all register copies can be eliminated.
79    For example, the lisp nreverse function should be unrolled 3 times
80    while (this)
81      {
82        next = this->cdr;
83        this->cdr = prev;
84        prev = this;
85        this = next;
86      }
87
88    ??? The number of times to unroll the loop may also be based on data
89    references in the loop.  For example, if we have a loop that references
90    x[i-1], x[i], and x[i+1], we should unroll it a multiple of 3 times.  */
91
92 /* ??? Add some simple linear equation solving capability so that we can
93    determine the number of loop iterations for more complex loops.
94    For example, consider this loop from gdb
95    #define SWAP_TARGET_AND_HOST(buffer,len)
96      {
97        char tmp;
98        char *p = (char *) buffer;
99        char *q = ((char *) buffer) + len - 1;
100        int iterations = (len + 1) >> 1;
101        int i;
102        for (p; p < q; p++, q--;)
103          {
104            tmp = *q;
105            *q = *p;
106            *p = tmp;
107          }
108      }
109    Note that:
110      start value = p = &buffer + current_iteration
111      end value   = q = &buffer + len - 1 - current_iteration
112    Given the loop exit test of "p < q", then there must be "q - p" iterations,
113    set equal to zero and solve for number of iterations:
114      q - p = len - 1 - 2*current_iteration = 0
115      current_iteration = (len - 1) / 2
116    Hence, there are (len - 1) / 2 (rounded up to the nearest integer)
117    iterations of this loop.  */
118
119 /* ??? Currently, no labels are marked as loop invariant when doing loop
120    unrolling.  This is because an insn inside the loop, that loads the address
121    of a label inside the loop into a register, could be moved outside the loop
122    by the invariant code motion pass if labels were invariant.  If the loop
123    is subsequently unrolled, the code will be wrong because each unrolled
124    body of the loop will use the same address, whereas each actually needs a
125    different address.  A case where this happens is when a loop containing
126    a switch statement is unrolled.
127
128    It would be better to let labels be considered invariant.  When we
129    unroll loops here, check to see if any insns using a label local to the
130    loop were moved before the loop.  If so, then correct the problem, by
131    moving the insn back into the loop, or perhaps replicate the insn before
132    the loop, one copy for each time the loop is unrolled.  */
133
134 /* The prime factors looked for when trying to unroll a loop by some
135    number which is modulo the total number of iterations.  Just checking
136    for these 4 prime factors will find at least one factor for 75% of
137    all numbers theoretically.  Practically speaking, this will succeed
138    almost all of the time since loops are generally a multiple of 2
139    and/or 5.  */
140
141 #define NUM_FACTORS 4
142
143 struct _factor { int factor, count; }
144 factors[NUM_FACTORS] = { {2, 0}, {3, 0}, {5, 0}, {7, 0}};
145
146 /* Describes the different types of loop unrolling performed.  */
147
148 enum unroll_types
149 {
150   UNROLL_COMPLETELY,
151   UNROLL_MODULO,
152   UNROLL_NAIVE
153 };
154
155 #include "config.h"
156 #include "system.h"
157 #include "rtl.h"
158 #include "tm_p.h"
159 #include "insn-config.h"
160 #include "integrate.h"
161 #include "regs.h"
162 #include "recog.h"
163 #include "flags.h"
164 #include "function.h"
165 #include "expr.h"
166 #include "loop.h"
167 #include "toplev.h"
168 #include "hard-reg-set.h"
169 #include "basic-block.h"
170
171 /* This controls which loops are unrolled, and by how much we unroll
172    them.  */
173
174 #ifndef MAX_UNROLLED_INSNS
175 #define MAX_UNROLLED_INSNS 100
176 #endif
177
178 /* Indexed by register number, if non-zero, then it contains a pointer
179    to a struct induction for a DEST_REG giv which has been combined with
180    one of more address givs.  This is needed because whenever such a DEST_REG
181    giv is modified, we must modify the value of all split address givs
182    that were combined with this DEST_REG giv.  */
183
184 static struct induction **addr_combined_regs;
185
186 /* Indexed by register number, if this is a splittable induction variable,
187    then this will hold the current value of the register, which depends on the
188    iteration number.  */
189
190 static rtx *splittable_regs;
191
192 /* Indexed by register number, if this is a splittable induction variable,
193    then this will hold the number of instructions in the loop that modify
194    the induction variable.  Used to ensure that only the last insn modifying
195    a split iv will update the original iv of the dest.  */
196
197 static int *splittable_regs_updates;
198
199 /* Forward declarations.  */
200
201 static void init_reg_map PARAMS ((struct inline_remap *, int));
202 static rtx calculate_giv_inc PARAMS ((rtx, rtx, unsigned int));
203 static rtx initial_reg_note_copy PARAMS ((rtx, struct inline_remap *));
204 static void final_reg_note_copy PARAMS ((rtx, struct inline_remap *));
205 static void copy_loop_body PARAMS ((struct loop *, rtx, rtx,
206                                     struct inline_remap *, rtx, int,
207                                     enum unroll_types, rtx, rtx, rtx, rtx));
208 static int find_splittable_regs PARAMS ((const struct loop *,
209                                          enum unroll_types, rtx, int));
210 static int find_splittable_givs PARAMS ((const struct loop *,
211                                          struct iv_class *, enum unroll_types,
212                                          rtx, int));
213 static int reg_dead_after_loop PARAMS ((const struct loop *, rtx));
214 static rtx fold_rtx_mult_add PARAMS ((rtx, rtx, rtx, enum machine_mode));
215 static int verify_addresses PARAMS ((struct induction *, rtx, int));
216 static rtx remap_split_bivs PARAMS ((struct loop *, rtx));
217 static rtx find_common_reg_term PARAMS ((rtx, rtx));
218 static rtx subtract_reg_term PARAMS ((rtx, rtx));
219 static rtx loop_find_equiv_value PARAMS ((const struct loop *, rtx));
220 static rtx ujump_to_loop_cont PARAMS ((rtx, rtx));
221
222 /* Try to unroll one loop and split induction variables in the loop.
223
224    The loop is described by the arguments LOOP and INSN_COUNT.
225    END_INSERT_BEFORE indicates where insns should be added which need
226    to be executed when the loop falls through.  STRENGTH_REDUCTION_P
227    indicates whether information generated in the strength reduction
228    pass is available.
229
230    This function is intended to be called from within `strength_reduce'
231    in loop.c.  */
232
233 void
234 unroll_loop (loop, insn_count, end_insert_before, strength_reduce_p)
235      struct loop *loop;
236      int insn_count;
237      rtx end_insert_before;
238      int strength_reduce_p;
239 {
240   struct loop_info *loop_info = LOOP_INFO (loop);
241   struct loop_ivs *ivs = LOOP_IVS (loop);
242   int i, j;
243   unsigned int r;
244   unsigned HOST_WIDE_INT temp;
245   int unroll_number = 1;
246   rtx copy_start, copy_end;
247   rtx insn, sequence, pattern, tem;
248   int max_labelno, max_insnno;
249   rtx insert_before;
250   struct inline_remap *map;
251   char *local_label = NULL;
252   char *local_regno;
253   unsigned int max_local_regnum;
254   unsigned int maxregnum;
255   rtx exit_label = 0;
256   rtx start_label;
257   struct iv_class *bl;
258   int splitting_not_safe = 0;
259   enum unroll_types unroll_type = UNROLL_NAIVE;
260   int loop_preconditioned = 0;
261   rtx safety_label;
262   /* This points to the last real insn in the loop, which should be either
263      a JUMP_INSN (for conditional jumps) or a BARRIER (for unconditional
264      jumps).  */
265   rtx last_loop_insn;
266   rtx loop_start = loop->start;
267   rtx loop_end = loop->end;
268
269   /* Don't bother unrolling huge loops.  Since the minimum factor is
270      two, loops greater than one half of MAX_UNROLLED_INSNS will never
271      be unrolled.  */
272   if (insn_count > MAX_UNROLLED_INSNS / 2)
273     {
274       if (loop_dump_stream)
275         fprintf (loop_dump_stream, "Unrolling failure: Loop too big.\n");
276       return;
277     }
278
279   /* When emitting debugger info, we can't unroll loops with unequal numbers
280      of block_beg and block_end notes, because that would unbalance the block
281      structure of the function.  This can happen as a result of the
282      "if (foo) bar; else break;" optimization in jump.c.  */
283   /* ??? Gcc has a general policy that -g is never supposed to change the code
284      that the compiler emits, so we must disable this optimization always,
285      even if debug info is not being output.  This is rare, so this should
286      not be a significant performance problem.  */
287
288   if (1 /* write_symbols != NO_DEBUG */)
289     {
290       int block_begins = 0;
291       int block_ends = 0;
292
293       for (insn = loop_start; insn != loop_end; insn = NEXT_INSN (insn))
294         {
295           if (GET_CODE (insn) == NOTE)
296             {
297               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
298                 block_begins++;
299               else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
300                 block_ends++;
301               if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG
302                   || NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
303                 {
304                   /* Note, would be nice to add code to unroll EH
305                      regions, but until that time, we punt (don't
306                      unroll).  For the proper way of doing it, see
307                      expand_inline_function.  */
308
309                   if (loop_dump_stream)
310                     fprintf (loop_dump_stream,
311                              "Unrolling failure: cannot unroll EH regions.\n");
312                   return;
313                 }
314             }
315         }
316
317       if (block_begins != block_ends)
318         {
319           if (loop_dump_stream)
320             fprintf (loop_dump_stream,
321                      "Unrolling failure: Unbalanced block notes.\n");
322           return;
323         }
324     }
325
326   /* Determine type of unroll to perform.  Depends on the number of iterations
327      and the size of the loop.  */
328
329   /* If there is no strength reduce info, then set
330      loop_info->n_iterations to zero.  This can happen if
331      strength_reduce can't find any bivs in the loop.  A value of zero
332      indicates that the number of iterations could not be calculated.  */
333
334   if (! strength_reduce_p)
335     loop_info->n_iterations = 0;
336
337   if (loop_dump_stream && loop_info->n_iterations > 0)
338     {
339       fputs ("Loop unrolling: ", loop_dump_stream);
340       fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
341                loop_info->n_iterations);
342       fputs (" iterations.\n", loop_dump_stream);
343     }
344
345   /* Find and save a pointer to the last nonnote insn in the loop.  */
346
347   last_loop_insn = prev_nonnote_insn (loop_end);
348
349   /* Calculate how many times to unroll the loop.  Indicate whether or
350      not the loop is being completely unrolled.  */
351
352   if (loop_info->n_iterations == 1)
353     {
354       /* Handle the case where the loop begins with an unconditional
355          jump to the loop condition.  Make sure to delete the jump
356          insn, otherwise the loop body will never execute.  */
357
358       rtx ujump = ujump_to_loop_cont (loop->start, loop->cont);
359       if (ujump)
360         delete_insn (ujump);
361
362       /* If number of iterations is exactly 1, then eliminate the compare and
363          branch at the end of the loop since they will never be taken.
364          Then return, since no other action is needed here.  */
365
366       /* If the last instruction is not a BARRIER or a JUMP_INSN, then
367          don't do anything.  */
368
369       if (GET_CODE (last_loop_insn) == BARRIER)
370         {
371           /* Delete the jump insn.  This will delete the barrier also.  */
372           delete_insn (PREV_INSN (last_loop_insn));
373         }
374       else if (GET_CODE (last_loop_insn) == JUMP_INSN)
375         {
376 #ifdef HAVE_cc0
377           rtx prev = PREV_INSN (last_loop_insn);
378 #endif
379           delete_insn (last_loop_insn);
380 #ifdef HAVE_cc0
381           /* The immediately preceding insn may be a compare which must be
382              deleted.  */
383           if (sets_cc0_p (prev))
384             delete_insn (prev);
385 #endif
386         }
387
388       /* Remove the loop notes since this is no longer a loop.  */
389       if (loop->vtop)
390         delete_insn (loop->vtop);
391       if (loop->cont)
392         delete_insn (loop->cont);
393       if (loop_start)
394         delete_insn (loop_start);
395       if (loop_end)
396         delete_insn (loop_end);
397
398       return;
399     }
400   else if (loop_info->n_iterations > 0
401            /* Avoid overflow in the next expression.  */
402            && loop_info->n_iterations < MAX_UNROLLED_INSNS
403            && loop_info->n_iterations * insn_count < MAX_UNROLLED_INSNS)
404     {
405       unroll_number = loop_info->n_iterations;
406       unroll_type = UNROLL_COMPLETELY;
407     }
408   else if (loop_info->n_iterations > 0)
409     {
410       /* Try to factor the number of iterations.  Don't bother with the
411          general case, only using 2, 3, 5, and 7 will get 75% of all
412          numbers theoretically, and almost all in practice.  */
413
414       for (i = 0; i < NUM_FACTORS; i++)
415         factors[i].count = 0;
416
417       temp = loop_info->n_iterations;
418       for (i = NUM_FACTORS - 1; i >= 0; i--)
419         while (temp % factors[i].factor == 0)
420           {
421             factors[i].count++;
422             temp = temp / factors[i].factor;
423           }
424
425       /* Start with the larger factors first so that we generally
426          get lots of unrolling.  */
427
428       unroll_number = 1;
429       temp = insn_count;
430       for (i = 3; i >= 0; i--)
431         while (factors[i].count--)
432           {
433             if (temp * factors[i].factor < MAX_UNROLLED_INSNS)
434               {
435                 unroll_number *= factors[i].factor;
436                 temp *= factors[i].factor;
437               }
438             else
439               break;
440           }
441
442       /* If we couldn't find any factors, then unroll as in the normal
443          case.  */
444       if (unroll_number == 1)
445         {
446           if (loop_dump_stream)
447             fprintf (loop_dump_stream, "Loop unrolling: No factors found.\n");
448         }
449       else
450         unroll_type = UNROLL_MODULO;
451     }
452
453   /* Default case, calculate number of times to unroll loop based on its
454      size.  */
455   if (unroll_type == UNROLL_NAIVE)
456     {
457       if (8 * insn_count < MAX_UNROLLED_INSNS)
458         unroll_number = 8;
459       else if (4 * insn_count < MAX_UNROLLED_INSNS)
460         unroll_number = 4;
461       else
462         unroll_number = 2;
463     }
464
465   /* Now we know how many times to unroll the loop.  */
466
467   if (loop_dump_stream)
468     fprintf (loop_dump_stream, "Unrolling loop %d times.\n", unroll_number);
469
470   if (unroll_type == UNROLL_COMPLETELY || unroll_type == UNROLL_MODULO)
471     {
472       /* Loops of these types can start with jump down to the exit condition
473          in rare circumstances.
474
475          Consider a pair of nested loops where the inner loop is part
476          of the exit code for the outer loop.
477
478          In this case jump.c will not duplicate the exit test for the outer
479          loop, so it will start with a jump to the exit code.
480
481          Then consider if the inner loop turns out to iterate once and
482          only once.  We will end up deleting the jumps associated with
483          the inner loop.  However, the loop notes are not removed from
484          the instruction stream.
485
486          And finally assume that we can compute the number of iterations
487          for the outer loop.
488
489          In this case unroll may want to unroll the outer loop even though
490          it starts with a jump to the outer loop's exit code.
491
492          We could try to optimize this case, but it hardly seems worth it.
493          Just return without unrolling the loop in such cases.  */
494
495       insn = loop_start;
496       while (GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != JUMP_INSN)
497         insn = NEXT_INSN (insn);
498       if (GET_CODE (insn) == JUMP_INSN)
499         return;
500     }
501
502   if (unroll_type == UNROLL_COMPLETELY)
503     {
504       /* Completely unrolling the loop:  Delete the compare and branch at
505          the end (the last two instructions).   This delete must done at the
506          very end of loop unrolling, to avoid problems with calls to
507          back_branch_in_range_p, which is called by find_splittable_regs.
508          All increments of splittable bivs/givs are changed to load constant
509          instructions.  */
510
511       copy_start = loop_start;
512
513       /* Set insert_before to the instruction immediately after the JUMP_INSN
514          (or BARRIER), so that any NOTEs between the JUMP_INSN and the end of
515          the loop will be correctly handled by copy_loop_body.  */
516       insert_before = NEXT_INSN (last_loop_insn);
517
518       /* Set copy_end to the insn before the jump at the end of the loop.  */
519       if (GET_CODE (last_loop_insn) == BARRIER)
520         copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
521       else if (GET_CODE (last_loop_insn) == JUMP_INSN)
522         {
523           copy_end = PREV_INSN (last_loop_insn);
524 #ifdef HAVE_cc0
525           /* The instruction immediately before the JUMP_INSN may be a compare
526              instruction which we do not want to copy.  */
527           if (sets_cc0_p (PREV_INSN (copy_end)))
528             copy_end = PREV_INSN (copy_end);
529 #endif
530         }
531       else
532         {
533           /* We currently can't unroll a loop if it doesn't end with a
534              JUMP_INSN.  There would need to be a mechanism that recognizes
535              this case, and then inserts a jump after each loop body, which
536              jumps to after the last loop body.  */
537           if (loop_dump_stream)
538             fprintf (loop_dump_stream,
539                      "Unrolling failure: loop does not end with a JUMP_INSN.\n");
540           return;
541         }
542     }
543   else if (unroll_type == UNROLL_MODULO)
544     {
545       /* Partially unrolling the loop:  The compare and branch at the end
546          (the last two instructions) must remain.  Don't copy the compare
547          and branch instructions at the end of the loop.  Insert the unrolled
548          code immediately before the compare/branch at the end so that the
549          code will fall through to them as before.  */
550
551       copy_start = loop_start;
552
553       /* Set insert_before to the jump insn at the end of the loop.
554          Set copy_end to before the jump insn at the end of the loop.  */
555       if (GET_CODE (last_loop_insn) == BARRIER)
556         {
557           insert_before = PREV_INSN (last_loop_insn);
558           copy_end = PREV_INSN (insert_before);
559         }
560       else if (GET_CODE (last_loop_insn) == JUMP_INSN)
561         {
562           insert_before = last_loop_insn;
563 #ifdef HAVE_cc0
564           /* The instruction immediately before the JUMP_INSN may be a compare
565              instruction which we do not want to copy or delete.  */
566           if (sets_cc0_p (PREV_INSN (insert_before)))
567             insert_before = PREV_INSN (insert_before);
568 #endif
569           copy_end = PREV_INSN (insert_before);
570         }
571       else
572         {
573           /* We currently can't unroll a loop if it doesn't end with a
574              JUMP_INSN.  There would need to be a mechanism that recognizes
575              this case, and then inserts a jump after each loop body, which
576              jumps to after the last loop body.  */
577           if (loop_dump_stream)
578             fprintf (loop_dump_stream,
579                      "Unrolling failure: loop does not end with a JUMP_INSN.\n");
580           return;
581         }
582     }
583   else
584     {
585       /* Normal case: Must copy the compare and branch instructions at the
586          end of the loop.  */
587
588       if (GET_CODE (last_loop_insn) == BARRIER)
589         {
590           /* Loop ends with an unconditional jump and a barrier.
591              Handle this like above, don't copy jump and barrier.
592              This is not strictly necessary, but doing so prevents generating
593              unconditional jumps to an immediately following label.
594
595              This will be corrected below if the target of this jump is
596              not the start_label.  */
597
598           insert_before = PREV_INSN (last_loop_insn);
599           copy_end = PREV_INSN (insert_before);
600         }
601       else if (GET_CODE (last_loop_insn) == JUMP_INSN)
602         {
603           /* Set insert_before to immediately after the JUMP_INSN, so that
604              NOTEs at the end of the loop will be correctly handled by
605              copy_loop_body.  */
606           insert_before = NEXT_INSN (last_loop_insn);
607           copy_end = last_loop_insn;
608         }
609       else
610         {
611           /* We currently can't unroll a loop if it doesn't end with a
612              JUMP_INSN.  There would need to be a mechanism that recognizes
613              this case, and then inserts a jump after each loop body, which
614              jumps to after the last loop body.  */
615           if (loop_dump_stream)
616             fprintf (loop_dump_stream,
617                      "Unrolling failure: loop does not end with a JUMP_INSN.\n");
618           return;
619         }
620
621       /* If copying exit test branches because they can not be eliminated,
622          then must convert the fall through case of the branch to a jump past
623          the end of the loop.  Create a label to emit after the loop and save
624          it for later use.  Do not use the label after the loop, if any, since
625          it might be used by insns outside the loop, or there might be insns
626          added before it later by final_[bg]iv_value which must be after
627          the real exit label.  */
628       exit_label = gen_label_rtx ();
629
630       insn = loop_start;
631       while (GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != JUMP_INSN)
632         insn = NEXT_INSN (insn);
633
634       if (GET_CODE (insn) == JUMP_INSN)
635         {
636           /* The loop starts with a jump down to the exit condition test.
637              Start copying the loop after the barrier following this
638              jump insn.  */
639           copy_start = NEXT_INSN (insn);
640
641           /* Splitting induction variables doesn't work when the loop is
642              entered via a jump to the bottom, because then we end up doing
643              a comparison against a new register for a split variable, but
644              we did not execute the set insn for the new register because
645              it was skipped over.  */
646           splitting_not_safe = 1;
647           if (loop_dump_stream)
648             fprintf (loop_dump_stream,
649                      "Splitting not safe, because loop not entered at top.\n");
650         }
651       else
652         copy_start = loop_start;
653     }
654
655   /* This should always be the first label in the loop.  */
656   start_label = NEXT_INSN (copy_start);
657   /* There may be a line number note and/or a loop continue note here.  */
658   while (GET_CODE (start_label) == NOTE)
659     start_label = NEXT_INSN (start_label);
660   if (GET_CODE (start_label) != CODE_LABEL)
661     {
662       /* This can happen as a result of jump threading.  If the first insns in
663          the loop test the same condition as the loop's backward jump, or the
664          opposite condition, then the backward jump will be modified to point
665          to elsewhere, and the loop's start label is deleted.
666
667          This case currently can not be handled by the loop unrolling code.  */
668
669       if (loop_dump_stream)
670         fprintf (loop_dump_stream,
671                  "Unrolling failure: unknown insns between BEG note and loop label.\n");
672       return;
673     }
674   if (LABEL_NAME (start_label))
675     {
676       /* The jump optimization pass must have combined the original start label
677          with a named label for a goto.  We can't unroll this case because
678          jumps which go to the named label must be handled differently than
679          jumps to the loop start, and it is impossible to differentiate them
680          in this case.  */
681       if (loop_dump_stream)
682         fprintf (loop_dump_stream,
683                  "Unrolling failure: loop start label is gone\n");
684       return;
685     }
686
687   if (unroll_type == UNROLL_NAIVE
688       && GET_CODE (last_loop_insn) == BARRIER
689       && GET_CODE (PREV_INSN (last_loop_insn)) == JUMP_INSN
690       && start_label != JUMP_LABEL (PREV_INSN (last_loop_insn)))
691     {
692       /* In this case, we must copy the jump and barrier, because they will
693          not be converted to jumps to an immediately following label.  */
694
695       insert_before = NEXT_INSN (last_loop_insn);
696       copy_end = last_loop_insn;
697     }
698
699   if (unroll_type == UNROLL_NAIVE
700       && GET_CODE (last_loop_insn) == JUMP_INSN
701       && start_label != JUMP_LABEL (last_loop_insn))
702     {
703       /* ??? The loop ends with a conditional branch that does not branch back
704          to the loop start label.  In this case, we must emit an unconditional
705          branch to the loop exit after emitting the final branch.
706          copy_loop_body does not have support for this currently, so we
707          give up.  It doesn't seem worthwhile to unroll anyways since
708          unrolling would increase the number of branch instructions
709          executed.  */
710       if (loop_dump_stream)
711         fprintf (loop_dump_stream,
712                  "Unrolling failure: final conditional branch not to loop start\n");
713       return;
714     }
715
716   /* Allocate a translation table for the labels and insn numbers.
717      They will be filled in as we copy the insns in the loop.  */
718
719   max_labelno = max_label_num ();
720   max_insnno = get_max_uid ();
721
722   /* Various paths through the unroll code may reach the "egress" label
723      without initializing fields within the map structure.
724
725      To be safe, we use xcalloc to zero the memory.  */
726   map = (struct inline_remap *) xcalloc (1, sizeof (struct inline_remap));
727
728   /* Allocate the label map.  */
729
730   if (max_labelno > 0)
731     {
732       map->label_map = (rtx *) xmalloc (max_labelno * sizeof (rtx));
733
734       local_label = (char *) xcalloc (max_labelno, sizeof (char));
735     }
736
737   /* Search the loop and mark all local labels, i.e. the ones which have to
738      be distinct labels when copied.  For all labels which might be
739      non-local, set their label_map entries to point to themselves.
740      If they happen to be local their label_map entries will be overwritten
741      before the loop body is copied.  The label_map entries for local labels
742      will be set to a different value each time the loop body is copied.  */
743
744   for (insn = copy_start; insn != loop_end; insn = NEXT_INSN (insn))
745     {
746       rtx note;
747
748       if (GET_CODE (insn) == CODE_LABEL)
749         local_label[CODE_LABEL_NUMBER (insn)] = 1;
750       else if (GET_CODE (insn) == JUMP_INSN)
751         {
752           if (JUMP_LABEL (insn))
753             set_label_in_map (map,
754                               CODE_LABEL_NUMBER (JUMP_LABEL (insn)),
755                               JUMP_LABEL (insn));
756           else if (GET_CODE (PATTERN (insn)) == ADDR_VEC
757                    || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
758             {
759               rtx pat = PATTERN (insn);
760               int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
761               int len = XVECLEN (pat, diff_vec_p);
762               rtx label;
763
764               for (i = 0; i < len; i++)
765                 {
766                   label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
767                   set_label_in_map (map, CODE_LABEL_NUMBER (label), label);
768                 }
769             }
770         }
771       else if ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)))
772         set_label_in_map (map, CODE_LABEL_NUMBER (XEXP (note, 0)),
773                           XEXP (note, 0));
774     }
775
776   /* Allocate space for the insn map.  */
777
778   map->insn_map = (rtx *) xmalloc (max_insnno * sizeof (rtx));
779
780   /* Set this to zero, to indicate that we are doing loop unrolling,
781      not function inlining.  */
782   map->inline_target = 0;
783
784   /* The register and constant maps depend on the number of registers
785      present, so the final maps can't be created until after
786      find_splittable_regs is called.  However, they are needed for
787      preconditioning, so we create temporary maps when preconditioning
788      is performed.  */
789
790   /* The preconditioning code may allocate two new pseudo registers.  */
791   maxregnum = max_reg_num ();
792
793   /* local_regno is only valid for regnos < max_local_regnum.  */
794   max_local_regnum = maxregnum;
795
796   /* Allocate and zero out the splittable_regs and addr_combined_regs
797      arrays.  These must be zeroed here because they will be used if
798      loop preconditioning is performed, and must be zero for that case.
799
800      It is safe to do this here, since the extra registers created by the
801      preconditioning code and find_splittable_regs will never be used
802      to access the splittable_regs[] and addr_combined_regs[] arrays.  */
803
804   splittable_regs = (rtx *) xcalloc (maxregnum, sizeof (rtx));
805   splittable_regs_updates = (int *) xcalloc (maxregnum, sizeof (int));
806   addr_combined_regs
807     = (struct induction **) xcalloc (maxregnum, sizeof (struct induction *));
808   local_regno = (char *) xcalloc (maxregnum, sizeof (char));
809
810   /* Mark all local registers, i.e. the ones which are referenced only
811      inside the loop.  */
812   if (INSN_UID (copy_end) < max_uid_for_loop)
813     {
814       int copy_start_luid = INSN_LUID (copy_start);
815       int copy_end_luid = INSN_LUID (copy_end);
816
817       /* If a register is used in the jump insn, we must not duplicate it
818          since it will also be used outside the loop.  */
819       if (GET_CODE (copy_end) == JUMP_INSN)
820         copy_end_luid--;
821
822       /* If we have a target that uses cc0, then we also must not duplicate
823          the insn that sets cc0 before the jump insn, if one is present.  */
824 #ifdef HAVE_cc0
825       if (GET_CODE (copy_end) == JUMP_INSN
826           && sets_cc0_p (PREV_INSN (copy_end)))
827         copy_end_luid--;
828 #endif
829
830       /* If copy_start points to the NOTE that starts the loop, then we must
831          use the next luid, because invariant pseudo-regs moved out of the loop
832          have their lifetimes modified to start here, but they are not safe
833          to duplicate.  */
834       if (copy_start == loop_start)
835         copy_start_luid++;
836
837       /* If a pseudo's lifetime is entirely contained within this loop, then we
838          can use a different pseudo in each unrolled copy of the loop.  This
839          results in better code.  */
840       /* We must limit the generic test to max_reg_before_loop, because only
841          these pseudo registers have valid regno_first_uid info.  */
842       for (r = FIRST_PSEUDO_REGISTER; r < max_reg_before_loop; ++r)
843         if (REGNO_FIRST_UID (r) > 0 && REGNO_FIRST_UID (r) <= max_uid_for_loop
844             && uid_luid[REGNO_FIRST_UID (r)] >= copy_start_luid
845             && REGNO_LAST_UID (r) > 0 && REGNO_LAST_UID (r) <= max_uid_for_loop
846             && uid_luid[REGNO_LAST_UID (r)] <= copy_end_luid)
847           {
848             /* However, we must also check for loop-carried dependencies.
849                If the value the pseudo has at the end of iteration X is
850                used by iteration X+1, then we can not use a different pseudo
851                for each unrolled copy of the loop.  */
852             /* A pseudo is safe if regno_first_uid is a set, and this
853                set dominates all instructions from regno_first_uid to
854                regno_last_uid.  */
855             /* ??? This check is simplistic.  We would get better code if
856                this check was more sophisticated.  */
857             if (set_dominates_use (r, REGNO_FIRST_UID (r), REGNO_LAST_UID (r),
858                                    copy_start, copy_end))
859               local_regno[r] = 1;
860
861             if (loop_dump_stream)
862               {
863                 if (local_regno[r])
864                   fprintf (loop_dump_stream, "Marked reg %d as local\n", r);
865                 else
866                   fprintf (loop_dump_stream, "Did not mark reg %d as local\n",
867                            r);
868               }
869           }
870     }
871
872   /* If this loop requires exit tests when unrolled, check to see if we
873      can precondition the loop so as to make the exit tests unnecessary.
874      Just like variable splitting, this is not safe if the loop is entered
875      via a jump to the bottom.  Also, can not do this if no strength
876      reduce info, because precondition_loop_p uses this info.  */
877
878   /* Must copy the loop body for preconditioning before the following
879      find_splittable_regs call since that will emit insns which need to
880      be after the preconditioned loop copies, but immediately before the
881      unrolled loop copies.  */
882
883   /* Also, it is not safe to split induction variables for the preconditioned
884      copies of the loop body.  If we split induction variables, then the code
885      assumes that each induction variable can be represented as a function
886      of its initial value and the loop iteration number.  This is not true
887      in this case, because the last preconditioned copy of the loop body
888      could be any iteration from the first up to the `unroll_number-1'th,
889      depending on the initial value of the iteration variable.  Therefore
890      we can not split induction variables here, because we can not calculate
891      their value.  Hence, this code must occur before find_splittable_regs
892      is called.  */
893
894   if (unroll_type == UNROLL_NAIVE && ! splitting_not_safe && strength_reduce_p)
895     {
896       rtx initial_value, final_value, increment;
897       enum machine_mode mode;
898
899       if (precondition_loop_p (loop,
900                                &initial_value, &final_value, &increment,
901                                &mode))
902         {
903           register rtx diff;
904           rtx *labels;
905           int abs_inc, neg_inc;
906
907           map->reg_map = (rtx *) xmalloc (maxregnum * sizeof (rtx));
908
909           VARRAY_CONST_EQUIV_INIT (map->const_equiv_varray, maxregnum,
910                                    "unroll_loop_precondition");
911           global_const_equiv_varray = map->const_equiv_varray;
912
913           init_reg_map (map, maxregnum);
914
915           /* Limit loop unrolling to 4, since this will make 7 copies of
916              the loop body.  */
917           if (unroll_number > 4)
918             unroll_number = 4;
919
920           /* Save the absolute value of the increment, and also whether or
921              not it is negative.  */
922           neg_inc = 0;
923           abs_inc = INTVAL (increment);
924           if (abs_inc < 0)
925             {
926               abs_inc = -abs_inc;
927               neg_inc = 1;
928             }
929
930           start_sequence ();
931
932           /* Calculate the difference between the final and initial values.
933              Final value may be a (plus (reg x) (const_int 1)) rtx.
934              Let the following cse pass simplify this if initial value is
935              a constant.
936
937              We must copy the final and initial values here to avoid
938              improperly shared rtl.  */
939
940           diff = expand_binop (mode, sub_optab, copy_rtx (final_value),
941                                copy_rtx (initial_value), NULL_RTX, 0,
942                                OPTAB_LIB_WIDEN);
943
944           /* Now calculate (diff % (unroll * abs (increment))) by using an
945              and instruction.  */
946           diff = expand_binop (GET_MODE (diff), and_optab, diff,
947                                GEN_INT (unroll_number * abs_inc - 1),
948                                NULL_RTX, 0, OPTAB_LIB_WIDEN);
949
950           /* Now emit a sequence of branches to jump to the proper precond
951              loop entry point.  */
952
953           labels = (rtx *) xmalloc (sizeof (rtx) * unroll_number);
954           for (i = 0; i < unroll_number; i++)
955             labels[i] = gen_label_rtx ();
956
957           /* Check for the case where the initial value is greater than or
958              equal to the final value.  In that case, we want to execute
959              exactly one loop iteration.  The code below will fail for this
960              case.  This check does not apply if the loop has a NE
961              comparison at the end.  */
962
963           if (loop_info->comparison_code != NE)
964             {
965               emit_cmp_and_jump_insns (initial_value, final_value,
966                                        neg_inc ? LE : GE,
967                                        NULL_RTX, mode, 0, 0, labels[1]);
968               JUMP_LABEL (get_last_insn ()) = labels[1];
969               LABEL_NUSES (labels[1])++;
970             }
971
972           /* Assuming the unroll_number is 4, and the increment is 2, then
973              for a negative increment:  for a positive increment:
974              diff = 0,1   precond 0     diff = 0,7   precond 0
975              diff = 2,3   precond 3     diff = 1,2   precond 1
976              diff = 4,5   precond 2     diff = 3,4   precond 2
977              diff = 6,7   precond 1     diff = 5,6   precond 3  */
978
979           /* We only need to emit (unroll_number - 1) branches here, the
980              last case just falls through to the following code.  */
981
982           /* ??? This would give better code if we emitted a tree of branches
983              instead of the current linear list of branches.  */
984
985           for (i = 0; i < unroll_number - 1; i++)
986             {
987               int cmp_const;
988               enum rtx_code cmp_code;
989
990               /* For negative increments, must invert the constant compared
991                  against, except when comparing against zero.  */
992               if (i == 0)
993                 {
994                   cmp_const = 0;
995                   cmp_code = EQ;
996                 }
997               else if (neg_inc)
998                 {
999                   cmp_const = unroll_number - i;
1000                   cmp_code = GE;
1001                 }
1002               else
1003                 {
1004                   cmp_const = i;
1005                   cmp_code = LE;
1006                 }
1007
1008               emit_cmp_and_jump_insns (diff, GEN_INT (abs_inc * cmp_const),
1009                                        cmp_code, NULL_RTX, mode, 0, 0,
1010                                        labels[i]);
1011               JUMP_LABEL (get_last_insn ()) = labels[i];
1012               LABEL_NUSES (labels[i])++;
1013             }
1014
1015           /* If the increment is greater than one, then we need another branch,
1016              to handle other cases equivalent to 0.  */
1017
1018           /* ??? This should be merged into the code above somehow to help
1019              simplify the code here, and reduce the number of branches emitted.
1020              For the negative increment case, the branch here could easily
1021              be merged with the `0' case branch above.  For the positive
1022              increment case, it is not clear how this can be simplified.  */
1023
1024           if (abs_inc != 1)
1025             {
1026               int cmp_const;
1027               enum rtx_code cmp_code;
1028
1029               if (neg_inc)
1030                 {
1031                   cmp_const = abs_inc - 1;
1032                   cmp_code = LE;
1033                 }
1034               else
1035                 {
1036                   cmp_const = abs_inc * (unroll_number - 1) + 1;
1037                   cmp_code = GE;
1038                 }
1039
1040               emit_cmp_and_jump_insns (diff, GEN_INT (cmp_const), cmp_code,
1041                                        NULL_RTX, mode, 0, 0, labels[0]);
1042               JUMP_LABEL (get_last_insn ()) = labels[0];
1043               LABEL_NUSES (labels[0])++;
1044             }
1045
1046           sequence = gen_sequence ();
1047           end_sequence ();
1048           emit_insn_before (sequence, loop_start);
1049
1050           /* Only the last copy of the loop body here needs the exit
1051              test, so set copy_end to exclude the compare/branch here,
1052              and then reset it inside the loop when get to the last
1053              copy.  */
1054
1055           if (GET_CODE (last_loop_insn) == BARRIER)
1056             copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
1057           else if (GET_CODE (last_loop_insn) == JUMP_INSN)
1058             {
1059               copy_end = PREV_INSN (last_loop_insn);
1060 #ifdef HAVE_cc0
1061               /* The immediately preceding insn may be a compare which
1062                  we do not want to copy.  */
1063               if (sets_cc0_p (PREV_INSN (copy_end)))
1064                 copy_end = PREV_INSN (copy_end);
1065 #endif
1066             }
1067           else
1068             abort ();
1069
1070           for (i = 1; i < unroll_number; i++)
1071             {
1072               emit_label_after (labels[unroll_number - i],
1073                                 PREV_INSN (loop_start));
1074
1075               memset ((char *) map->insn_map, 0, max_insnno * sizeof (rtx));
1076               memset ((char *) &VARRAY_CONST_EQUIV (map->const_equiv_varray, 0),
1077                       0, (VARRAY_SIZE (map->const_equiv_varray)
1078                           * sizeof (struct const_equiv_data)));
1079               map->const_age = 0;
1080
1081               for (j = 0; j < max_labelno; j++)
1082                 if (local_label[j])
1083                   set_label_in_map (map, j, gen_label_rtx ());
1084
1085               for (r = FIRST_PSEUDO_REGISTER; r < max_local_regnum; r++)
1086                 if (local_regno[r])
1087                   {
1088                     map->reg_map[r]
1089                       = gen_reg_rtx (GET_MODE (regno_reg_rtx[r]));
1090                     record_base_value (REGNO (map->reg_map[r]),
1091                                        regno_reg_rtx[r], 0);
1092                   }
1093               /* The last copy needs the compare/branch insns at the end,
1094                  so reset copy_end here if the loop ends with a conditional
1095                  branch.  */
1096
1097               if (i == unroll_number - 1)
1098                 {
1099                   if (GET_CODE (last_loop_insn) == BARRIER)
1100                     copy_end = PREV_INSN (PREV_INSN (last_loop_insn));
1101                   else
1102                     copy_end = last_loop_insn;
1103                 }
1104
1105               /* None of the copies are the `last_iteration', so just
1106                  pass zero for that parameter.  */
1107               copy_loop_body (loop, copy_start, copy_end, map, exit_label, 0,
1108                               unroll_type, start_label, loop_end,
1109                               loop_start, copy_end);
1110             }
1111           emit_label_after (labels[0], PREV_INSN (loop_start));
1112
1113           if (GET_CODE (last_loop_insn) == BARRIER)
1114             {
1115               insert_before = PREV_INSN (last_loop_insn);
1116               copy_end = PREV_INSN (insert_before);
1117             }
1118           else
1119             {
1120               insert_before = last_loop_insn;
1121 #ifdef HAVE_cc0
1122               /* The instruction immediately before the JUMP_INSN may
1123                  be a compare instruction which we do not want to copy
1124                  or delete.  */
1125               if (sets_cc0_p (PREV_INSN (insert_before)))
1126                 insert_before = PREV_INSN (insert_before);
1127 #endif
1128               copy_end = PREV_INSN (insert_before);
1129             }
1130
1131           /* Set unroll type to MODULO now.  */
1132           unroll_type = UNROLL_MODULO;
1133           loop_preconditioned = 1;
1134
1135           /* Clean up.  */
1136           free (labels);
1137         }
1138     }
1139
1140   /* If reach here, and the loop type is UNROLL_NAIVE, then don't unroll
1141      the loop unless all loops are being unrolled.  */
1142   if (unroll_type == UNROLL_NAIVE && ! flag_unroll_all_loops)
1143     {
1144       if (loop_dump_stream)
1145         fprintf (loop_dump_stream,
1146                  "Unrolling failure: Naive unrolling not being done.\n");
1147       goto egress;
1148     }
1149
1150   /* At this point, we are guaranteed to unroll the loop.  */
1151
1152   /* Keep track of the unroll factor for the loop.  */
1153   loop_info->unroll_number = unroll_number;
1154
1155   /* For each biv and giv, determine whether it can be safely split into
1156      a different variable for each unrolled copy of the loop body.
1157      We precalculate and save this info here, since computing it is
1158      expensive.
1159
1160      Do this before deleting any instructions from the loop, so that
1161      back_branch_in_range_p will work correctly.  */
1162
1163   if (splitting_not_safe)
1164     temp = 0;
1165   else
1166     temp = find_splittable_regs (loop, unroll_type,
1167                                  end_insert_before, unroll_number);
1168
1169   /* find_splittable_regs may have created some new registers, so must
1170      reallocate the reg_map with the new larger size, and must realloc
1171      the constant maps also.  */
1172
1173   maxregnum = max_reg_num ();
1174   map->reg_map = (rtx *) xmalloc (maxregnum * sizeof (rtx));
1175
1176   init_reg_map (map, maxregnum);
1177
1178   if (map->const_equiv_varray == 0)
1179     VARRAY_CONST_EQUIV_INIT (map->const_equiv_varray,
1180                              maxregnum + temp * unroll_number * 2,
1181                              "unroll_loop");
1182   global_const_equiv_varray = map->const_equiv_varray;
1183
1184   /* Search the list of bivs and givs to find ones which need to be remapped
1185      when split, and set their reg_map entry appropriately.  */
1186
1187   for (bl = ivs->loop_iv_list; bl; bl = bl->next)
1188     {
1189       if (REGNO (bl->biv->src_reg) != bl->regno)
1190         map->reg_map[bl->regno] = bl->biv->src_reg;
1191 #if 0
1192       /* Currently, non-reduced/final-value givs are never split.  */
1193       for (v = bl->giv; v; v = v->next_iv)
1194         if (REGNO (v->src_reg) != bl->regno)
1195           map->reg_map[REGNO (v->dest_reg)] = v->src_reg;
1196 #endif
1197     }
1198
1199   /* Use our current register alignment and pointer flags.  */
1200   map->regno_pointer_align = cfun->emit->regno_pointer_align;
1201   map->x_regno_reg_rtx = cfun->emit->x_regno_reg_rtx;
1202
1203   /* If the loop is being partially unrolled, and the iteration variables
1204      are being split, and are being renamed for the split, then must fix up
1205      the compare/jump instruction at the end of the loop to refer to the new
1206      registers.  This compare isn't copied, so the registers used in it
1207      will never be replaced if it isn't done here.  */
1208
1209   if (unroll_type == UNROLL_MODULO)
1210     {
1211       insn = NEXT_INSN (copy_end);
1212       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
1213         PATTERN (insn) = remap_split_bivs (loop, PATTERN (insn));
1214     }
1215
1216   /* For unroll_number times, make a copy of each instruction
1217      between copy_start and copy_end, and insert these new instructions
1218      before the end of the loop.  */
1219
1220   for (i = 0; i < unroll_number; i++)
1221     {
1222       memset ((char *) map->insn_map, 0, max_insnno * sizeof (rtx));
1223       memset ((char *) &VARRAY_CONST_EQUIV (map->const_equiv_varray, 0), 0,
1224               VARRAY_SIZE (map->const_equiv_varray) * sizeof (struct const_equiv_data));
1225       map->const_age = 0;
1226
1227       for (j = 0; j < max_labelno; j++)
1228         if (local_label[j])
1229           set_label_in_map (map, j, gen_label_rtx ());
1230
1231       for (r = FIRST_PSEUDO_REGISTER; r < max_local_regnum; r++)
1232         if (local_regno[r])
1233           {
1234             map->reg_map[r] = gen_reg_rtx (GET_MODE (regno_reg_rtx[r]));
1235             record_base_value (REGNO (map->reg_map[r]),
1236                                regno_reg_rtx[r], 0);
1237           }
1238
1239       /* If loop starts with a branch to the test, then fix it so that
1240          it points to the test of the first unrolled copy of the loop.  */
1241       if (i == 0 && loop_start != copy_start)
1242         {
1243           insn = PREV_INSN (copy_start);
1244           pattern = PATTERN (insn);
1245
1246           tem = get_label_from_map (map,
1247                                     CODE_LABEL_NUMBER
1248                                     (XEXP (SET_SRC (pattern), 0)));
1249           SET_SRC (pattern) = gen_rtx_LABEL_REF (VOIDmode, tem);
1250
1251           /* Set the jump label so that it can be used by later loop unrolling
1252              passes.  */
1253           JUMP_LABEL (insn) = tem;
1254           LABEL_NUSES (tem)++;
1255         }
1256
1257       copy_loop_body (loop, copy_start, copy_end, map, exit_label,
1258                       i == unroll_number - 1, unroll_type, start_label,
1259                       loop_end, insert_before, insert_before);
1260     }
1261
1262   /* Before deleting any insns, emit a CODE_LABEL immediately after the last
1263      insn to be deleted.  This prevents any runaway delete_insn call from
1264      more insns that it should, as it always stops at a CODE_LABEL.  */
1265
1266   /* Delete the compare and branch at the end of the loop if completely
1267      unrolling the loop.  Deleting the backward branch at the end also
1268      deletes the code label at the start of the loop.  This is done at
1269      the very end to avoid problems with back_branch_in_range_p.  */
1270
1271   if (unroll_type == UNROLL_COMPLETELY)
1272     safety_label = emit_label_after (gen_label_rtx (), last_loop_insn);
1273   else
1274     safety_label = emit_label_after (gen_label_rtx (), copy_end);
1275
1276   /* Delete all of the original loop instructions.  Don't delete the
1277      LOOP_BEG note, or the first code label in the loop.  */
1278
1279   insn = NEXT_INSN (copy_start);
1280   while (insn != safety_label)
1281     {
1282       /* ??? Don't delete named code labels.  They will be deleted when the
1283          jump that references them is deleted.  Otherwise, we end up deleting
1284          them twice, which causes them to completely disappear instead of turn
1285          into NOTE_INSN_DELETED_LABEL notes.  This in turn causes aborts in
1286          dwarfout.c/dwarf2out.c.  We could perhaps fix the dwarf*out.c files
1287          to handle deleted labels instead.  Or perhaps fix DECL_RTL of the
1288          associated LABEL_DECL to point to one of the new label instances.  */
1289       /* ??? Likewise, we can't delete a NOTE_INSN_DELETED_LABEL note.  */
1290       if (insn != start_label
1291           && ! (GET_CODE (insn) == CODE_LABEL && LABEL_NAME (insn))
1292           && ! (GET_CODE (insn) == NOTE
1293                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL))
1294         insn = delete_insn (insn);
1295       else
1296         insn = NEXT_INSN (insn);
1297     }
1298
1299   /* Can now delete the 'safety' label emitted to protect us from runaway
1300      delete_insn calls.  */
1301   if (INSN_DELETED_P (safety_label))
1302     abort ();
1303   delete_insn (safety_label);
1304
1305   /* If exit_label exists, emit it after the loop.  Doing the emit here
1306      forces it to have a higher INSN_UID than any insn in the unrolled loop.
1307      This is needed so that mostly_true_jump in reorg.c will treat jumps
1308      to this loop end label correctly, i.e. predict that they are usually
1309      not taken.  */
1310   if (exit_label)
1311     emit_label_after (exit_label, loop_end);
1312
1313  egress:
1314   if (unroll_type == UNROLL_COMPLETELY)
1315     {
1316       /* Remove the loop notes since this is no longer a loop.  */
1317       if (loop->vtop)
1318         delete_insn (loop->vtop);
1319       if (loop->cont)
1320         delete_insn (loop->cont);
1321       if (loop_start)
1322         delete_insn (loop_start);
1323       if (loop_end)
1324         delete_insn (loop_end);
1325     }
1326
1327   if (map->const_equiv_varray)
1328     VARRAY_FREE (map->const_equiv_varray);
1329   if (map->label_map)
1330     {
1331       free (map->label_map);
1332       free (local_label);
1333     }
1334   free (map->insn_map);
1335   free (splittable_regs);
1336   free (splittable_regs_updates);
1337   free (addr_combined_regs);
1338   free (local_regno);
1339   if (map->reg_map)
1340     free (map->reg_map);
1341   free (map);
1342 }
1343 \f
1344 /* Return true if the loop can be safely, and profitably, preconditioned
1345    so that the unrolled copies of the loop body don't need exit tests.
1346
1347    This only works if final_value, initial_value and increment can be
1348    determined, and if increment is a constant power of 2.
1349    If increment is not a power of 2, then the preconditioning modulo
1350    operation would require a real modulo instead of a boolean AND, and this
1351    is not considered `profitable'.  */
1352
1353 /* ??? If the loop is known to be executed very many times, or the machine
1354    has a very cheap divide instruction, then preconditioning is a win even
1355    when the increment is not a power of 2.  Use RTX_COST to compute
1356    whether divide is cheap.
1357    ??? A divide by constant doesn't actually need a divide, look at
1358    expand_divmod.  The reduced cost of this optimized modulo is not
1359    reflected in RTX_COST.  */
1360
1361 int
1362 precondition_loop_p (loop, initial_value, final_value, increment, mode)
1363      const struct loop *loop;
1364      rtx *initial_value, *final_value, *increment;
1365      enum machine_mode *mode;
1366 {
1367   rtx loop_start = loop->start;
1368   struct loop_info *loop_info = LOOP_INFO (loop);
1369
1370   if (loop_info->n_iterations > 0)
1371     {
1372       *initial_value = const0_rtx;
1373       *increment = const1_rtx;
1374       *final_value = GEN_INT (loop_info->n_iterations);
1375       *mode = word_mode;
1376
1377       if (loop_dump_stream)
1378         {
1379           fputs ("Preconditioning: Success, number of iterations known, ",
1380                  loop_dump_stream);
1381           fprintf (loop_dump_stream, HOST_WIDE_INT_PRINT_DEC,
1382                    loop_info->n_iterations);
1383           fputs (".\n", loop_dump_stream);
1384         }
1385       return 1;
1386     }
1387
1388   if (loop_info->initial_value == 0)
1389     {
1390       if (loop_dump_stream)
1391         fprintf (loop_dump_stream,
1392                  "Preconditioning: Could not find initial value.\n");
1393       return 0;
1394     }
1395   else if (loop_info->increment == 0)
1396     {
1397       if (loop_dump_stream)
1398         fprintf (loop_dump_stream,
1399                  "Preconditioning: Could not find increment value.\n");
1400       return 0;
1401     }
1402   else if (GET_CODE (loop_info->increment) != CONST_INT)
1403     {
1404       if (loop_dump_stream)
1405         fprintf (loop_dump_stream,
1406                  "Preconditioning: Increment not a constant.\n");
1407       return 0;
1408     }
1409   else if ((exact_log2 (INTVAL (loop_info->increment)) < 0)
1410            && (exact_log2 (-INTVAL (loop_info->increment)) < 0))
1411     {
1412       if (loop_dump_stream)
1413         fprintf (loop_dump_stream,
1414                  "Preconditioning: Increment not a constant power of 2.\n");
1415       return 0;
1416     }
1417
1418   /* Unsigned_compare and compare_dir can be ignored here, since they do
1419      not matter for preconditioning.  */
1420
1421   if (loop_info->final_value == 0)
1422     {
1423       if (loop_dump_stream)
1424         fprintf (loop_dump_stream,
1425                  "Preconditioning: EQ comparison loop.\n");
1426       return 0;
1427     }
1428
1429   /* Must ensure that final_value is invariant, so call
1430      loop_invariant_p to check.  Before doing so, must check regno
1431      against max_reg_before_loop to make sure that the register is in
1432      the range covered by loop_invariant_p.  If it isn't, then it is
1433      most likely a biv/giv which by definition are not invariant.  */
1434   if ((GET_CODE (loop_info->final_value) == REG
1435        && REGNO (loop_info->final_value) >= max_reg_before_loop)
1436       || (GET_CODE (loop_info->final_value) == PLUS
1437           && REGNO (XEXP (loop_info->final_value, 0)) >= max_reg_before_loop)
1438       || ! loop_invariant_p (loop, loop_info->final_value))
1439     {
1440       if (loop_dump_stream)
1441         fprintf (loop_dump_stream,
1442                  "Preconditioning: Final value not invariant.\n");
1443       return 0;
1444     }
1445
1446   /* Fail for floating point values, since the caller of this function
1447      does not have code to deal with them.  */
1448   if (GET_MODE_CLASS (GET_MODE (loop_info->final_value)) == MODE_FLOAT
1449       || GET_MODE_CLASS (GET_MODE (loop_info->initial_value)) == MODE_FLOAT)
1450     {
1451       if (loop_dump_stream)
1452         fprintf (loop_dump_stream,
1453                  "Preconditioning: Floating point final or initial value.\n");
1454       return 0;
1455     }
1456
1457   /* Fail if loop_info->iteration_var is not live before loop_start,
1458      since we need to test its value in the preconditioning code.  */
1459
1460   if (uid_luid[REGNO_FIRST_UID (REGNO (loop_info->iteration_var))]
1461       > INSN_LUID (loop_start))
1462     {
1463       if (loop_dump_stream)
1464         fprintf (loop_dump_stream,
1465                  "Preconditioning: Iteration var not live before loop start.\n");
1466       return 0;
1467     }
1468
1469   /* Note that loop_iterations biases the initial value for GIV iterators
1470      such as "while (i-- > 0)" so that we can calculate the number of
1471      iterations just like for BIV iterators.
1472
1473      Also note that the absolute values of initial_value and
1474      final_value are unimportant as only their difference is used for
1475      calculating the number of loop iterations.  */
1476   *initial_value = loop_info->initial_value;
1477   *increment = loop_info->increment;
1478   *final_value = loop_info->final_value;
1479
1480   /* Decide what mode to do these calculations in.  Choose the larger
1481      of final_value's mode and initial_value's mode, or a full-word if
1482      both are constants.  */
1483   *mode = GET_MODE (*final_value);
1484   if (*mode == VOIDmode)
1485     {
1486       *mode = GET_MODE (*initial_value);
1487       if (*mode == VOIDmode)
1488         *mode = word_mode;
1489     }
1490   else if (*mode != GET_MODE (*initial_value)
1491            && (GET_MODE_SIZE (*mode)
1492                < GET_MODE_SIZE (GET_MODE (*initial_value))))
1493     *mode = GET_MODE (*initial_value);
1494
1495   /* Success!  */
1496   if (loop_dump_stream)
1497     fprintf (loop_dump_stream, "Preconditioning: Successful.\n");
1498   return 1;
1499 }
1500
1501 /* All pseudo-registers must be mapped to themselves.  Two hard registers
1502    must be mapped, VIRTUAL_STACK_VARS_REGNUM and VIRTUAL_INCOMING_ARGS_
1503    REGNUM, to avoid function-inlining specific conversions of these
1504    registers.  All other hard regs can not be mapped because they may be
1505    used with different
1506    modes.  */
1507
1508 static void
1509 init_reg_map (map, maxregnum)
1510      struct inline_remap *map;
1511      int maxregnum;
1512 {
1513   int i;
1514
1515   for (i = maxregnum - 1; i > LAST_VIRTUAL_REGISTER; i--)
1516     map->reg_map[i] = regno_reg_rtx[i];
1517   /* Just clear the rest of the entries.  */
1518   for (i = LAST_VIRTUAL_REGISTER; i >= 0; i--)
1519     map->reg_map[i] = 0;
1520
1521   map->reg_map[VIRTUAL_STACK_VARS_REGNUM]
1522     = regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM];
1523   map->reg_map[VIRTUAL_INCOMING_ARGS_REGNUM]
1524     = regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM];
1525 }
1526 \f
1527 /* Strength-reduction will often emit code for optimized biv/givs which
1528    calculates their value in a temporary register, and then copies the result
1529    to the iv.  This procedure reconstructs the pattern computing the iv;
1530    verifying that all operands are of the proper form.
1531
1532    PATTERN must be the result of single_set.
1533    The return value is the amount that the giv is incremented by.  */
1534
1535 static rtx
1536 calculate_giv_inc (pattern, src_insn, regno)
1537      rtx pattern, src_insn;
1538      unsigned int regno;
1539 {
1540   rtx increment;
1541   rtx increment_total = 0;
1542   int tries = 0;
1543
1544  retry:
1545   /* Verify that we have an increment insn here.  First check for a plus
1546      as the set source.  */
1547   if (GET_CODE (SET_SRC (pattern)) != PLUS)
1548     {
1549       /* SR sometimes computes the new giv value in a temp, then copies it
1550          to the new_reg.  */
1551       src_insn = PREV_INSN (src_insn);
1552       pattern = PATTERN (src_insn);
1553       if (GET_CODE (SET_SRC (pattern)) != PLUS)
1554         abort ();
1555
1556       /* The last insn emitted is not needed, so delete it to avoid confusing
1557          the second cse pass.  This insn sets the giv unnecessarily.  */
1558       delete_insn (get_last_insn ());
1559     }
1560
1561   /* Verify that we have a constant as the second operand of the plus.  */
1562   increment = XEXP (SET_SRC (pattern), 1);
1563   if (GET_CODE (increment) != CONST_INT)
1564     {
1565       /* SR sometimes puts the constant in a register, especially if it is
1566          too big to be an add immed operand.  */
1567       src_insn = PREV_INSN (src_insn);
1568       increment = SET_SRC (PATTERN (src_insn));
1569
1570       /* SR may have used LO_SUM to compute the constant if it is too large
1571          for a load immed operand.  In this case, the constant is in operand
1572          one of the LO_SUM rtx.  */
1573       if (GET_CODE (increment) == LO_SUM)
1574         increment = XEXP (increment, 1);
1575
1576       /* Some ports store large constants in memory and add a REG_EQUAL
1577          note to the store insn.  */
1578       else if (GET_CODE (increment) == MEM)
1579         {
1580           rtx note = find_reg_note (src_insn, REG_EQUAL, 0);
1581           if (note)
1582             increment = XEXP (note, 0);
1583         }
1584
1585       else if (GET_CODE (increment) == IOR
1586                || GET_CODE (increment) == ASHIFT
1587                || GET_CODE (increment) == PLUS)
1588         {
1589           /* The rs6000 port loads some constants with IOR.
1590              The alpha port loads some constants with ASHIFT and PLUS.  */
1591           rtx second_part = XEXP (increment, 1);
1592           enum rtx_code code = GET_CODE (increment);
1593
1594           src_insn = PREV_INSN (src_insn);
1595           increment = SET_SRC (PATTERN (src_insn));
1596           /* Don't need the last insn anymore.  */
1597           delete_insn (get_last_insn ());
1598
1599           if (GET_CODE (second_part) != CONST_INT
1600               || GET_CODE (increment) != CONST_INT)
1601             abort ();
1602
1603           if (code == IOR)
1604             increment = GEN_INT (INTVAL (increment) | INTVAL (second_part));
1605           else if (code == PLUS)
1606             increment = GEN_INT (INTVAL (increment) + INTVAL (second_part));
1607           else
1608             increment = GEN_INT (INTVAL (increment) << INTVAL (second_part));
1609         }
1610
1611       if (GET_CODE (increment) != CONST_INT)
1612         abort ();
1613
1614       /* The insn loading the constant into a register is no longer needed,
1615          so delete it.  */
1616       delete_insn (get_last_insn ());
1617     }
1618
1619   if (increment_total)
1620     increment_total = GEN_INT (INTVAL (increment_total) + INTVAL (increment));
1621   else
1622     increment_total = increment;
1623
1624   /* Check that the source register is the same as the register we expected
1625      to see as the source.  If not, something is seriously wrong.  */
1626   if (GET_CODE (XEXP (SET_SRC (pattern), 0)) != REG
1627       || REGNO (XEXP (SET_SRC (pattern), 0)) != regno)
1628     {
1629       /* Some machines (e.g. the romp), may emit two add instructions for
1630          certain constants, so lets try looking for another add immediately
1631          before this one if we have only seen one add insn so far.  */
1632
1633       if (tries == 0)
1634         {
1635           tries++;
1636
1637           src_insn = PREV_INSN (src_insn);
1638           pattern = PATTERN (src_insn);
1639
1640           delete_insn (get_last_insn ());
1641
1642           goto retry;
1643         }
1644
1645       abort ();
1646     }
1647
1648   return increment_total;
1649 }
1650
1651 /* Copy REG_NOTES, except for insn references, because not all insn_map
1652    entries are valid yet.  We do need to copy registers now though, because
1653    the reg_map entries can change during copying.  */
1654
1655 static rtx
1656 initial_reg_note_copy (notes, map)
1657      rtx notes;
1658      struct inline_remap *map;
1659 {
1660   rtx copy;
1661
1662   if (notes == 0)
1663     return 0;
1664
1665   copy = rtx_alloc (GET_CODE (notes));
1666   PUT_MODE (copy, GET_MODE (notes));
1667
1668   if (GET_CODE (notes) == EXPR_LIST)
1669     XEXP (copy, 0) = copy_rtx_and_substitute (XEXP (notes, 0), map, 0);
1670   else if (GET_CODE (notes) == INSN_LIST)
1671     /* Don't substitute for these yet.  */
1672     XEXP (copy, 0) = XEXP (notes, 0);
1673   else
1674     abort ();
1675
1676   XEXP (copy, 1) = initial_reg_note_copy (XEXP (notes, 1), map);
1677
1678   return copy;
1679 }
1680
1681 /* Fixup insn references in copied REG_NOTES.  */
1682
1683 static void
1684 final_reg_note_copy (notes, map)
1685      rtx notes;
1686      struct inline_remap *map;
1687 {
1688   rtx note;
1689
1690   for (note = notes; note; note = XEXP (note, 1))
1691     if (GET_CODE (note) == INSN_LIST)
1692       XEXP (note, 0) = map->insn_map[INSN_UID (XEXP (note, 0))];
1693 }
1694
1695 /* Copy each instruction in the loop, substituting from map as appropriate.
1696    This is very similar to a loop in expand_inline_function.  */
1697
1698 static void
1699 copy_loop_body (loop, copy_start, copy_end, map, exit_label, last_iteration,
1700                 unroll_type, start_label, loop_end, insert_before,
1701                 copy_notes_from)
1702      struct loop *loop;
1703      rtx copy_start, copy_end;
1704      struct inline_remap *map;
1705      rtx exit_label;
1706      int last_iteration;
1707      enum unroll_types unroll_type;
1708      rtx start_label, loop_end, insert_before, copy_notes_from;
1709 {
1710   struct loop_ivs *ivs = LOOP_IVS (loop);
1711   rtx insn, pattern;
1712   rtx set, tem, copy = NULL_RTX;
1713   int dest_reg_was_split, i;
1714 #ifdef HAVE_cc0
1715   rtx cc0_insn = 0;
1716 #endif
1717   rtx final_label = 0;
1718   rtx giv_inc, giv_dest_reg, giv_src_reg;
1719
1720   /* If this isn't the last iteration, then map any references to the
1721      start_label to final_label.  Final label will then be emitted immediately
1722      after the end of this loop body if it was ever used.
1723
1724      If this is the last iteration, then map references to the start_label
1725      to itself.  */
1726   if (! last_iteration)
1727     {
1728       final_label = gen_label_rtx ();
1729       set_label_in_map (map, CODE_LABEL_NUMBER (start_label), final_label);
1730     }
1731   else
1732     set_label_in_map (map, CODE_LABEL_NUMBER (start_label), start_label);
1733
1734   start_sequence ();
1735
1736   /* Emit a NOTE_INSN_DELETED to force at least two insns onto the sequence.
1737      Else gen_sequence could return a raw pattern for a jump which we pass
1738      off to emit_insn_before (instead of emit_jump_insn_before) which causes
1739      a variety of losing behaviors later.  */
1740   emit_note (0, NOTE_INSN_DELETED);
1741
1742   insn = copy_start;
1743   do
1744     {
1745       insn = NEXT_INSN (insn);
1746
1747       map->orig_asm_operands_vector = 0;
1748
1749       switch (GET_CODE (insn))
1750         {
1751         case INSN:
1752           pattern = PATTERN (insn);
1753           copy = 0;
1754           giv_inc = 0;
1755
1756           /* Check to see if this is a giv that has been combined with
1757              some split address givs.  (Combined in the sense that
1758              `combine_givs' in loop.c has put two givs in the same register.)
1759              In this case, we must search all givs based on the same biv to
1760              find the address givs.  Then split the address givs.
1761              Do this before splitting the giv, since that may map the
1762              SET_DEST to a new register.  */
1763
1764           if ((set = single_set (insn))
1765               && GET_CODE (SET_DEST (set)) == REG
1766               && addr_combined_regs[REGNO (SET_DEST (set))])
1767             {
1768               struct iv_class *bl;
1769               struct induction *v, *tv;
1770               unsigned int regno = REGNO (SET_DEST (set));
1771
1772               v = addr_combined_regs[REGNO (SET_DEST (set))];
1773               bl = ivs->reg_biv_class[REGNO (v->src_reg)];
1774
1775               /* Although the giv_inc amount is not needed here, we must call
1776                  calculate_giv_inc here since it might try to delete the
1777                  last insn emitted.  If we wait until later to call it,
1778                  we might accidentally delete insns generated immediately
1779                  below by emit_unrolled_add.  */
1780
1781               giv_inc = calculate_giv_inc (set, insn, regno);
1782
1783               /* Now find all address giv's that were combined with this
1784                  giv 'v'.  */
1785               for (tv = bl->giv; tv; tv = tv->next_iv)
1786                 if (tv->giv_type == DEST_ADDR && tv->same == v)
1787                   {
1788                     int this_giv_inc;
1789
1790                     /* If this DEST_ADDR giv was not split, then ignore it.  */
1791                     if (*tv->location != tv->dest_reg)
1792                       continue;
1793
1794                     /* Scale this_giv_inc if the multiplicative factors of
1795                        the two givs are different.  */
1796                     this_giv_inc = INTVAL (giv_inc);
1797                     if (tv->mult_val != v->mult_val)
1798                       this_giv_inc = (this_giv_inc / INTVAL (v->mult_val)
1799                                       * INTVAL (tv->mult_val));
1800
1801                     tv->dest_reg = plus_constant (tv->dest_reg, this_giv_inc);
1802                     *tv->location = tv->dest_reg;
1803
1804                     if (last_iteration && unroll_type != UNROLL_COMPLETELY)
1805                       {
1806                         /* Must emit an insn to increment the split address
1807                            giv.  Add in the const_adjust field in case there
1808                            was a constant eliminated from the address.  */
1809                         rtx value, dest_reg;
1810
1811                         /* tv->dest_reg will be either a bare register,
1812                            or else a register plus a constant.  */
1813                         if (GET_CODE (tv->dest_reg) == REG)
1814                           dest_reg = tv->dest_reg;
1815                         else
1816                           dest_reg = XEXP (tv->dest_reg, 0);
1817
1818                         /* Check for shared address givs, and avoid
1819                            incrementing the shared pseudo reg more than
1820                            once.  */
1821                         if (! tv->same_insn && ! tv->shared)
1822                           {
1823                             /* tv->dest_reg may actually be a (PLUS (REG)
1824                                (CONST)) here, so we must call plus_constant
1825                                to add the const_adjust amount before calling
1826                                emit_unrolled_add below.  */
1827                             value = plus_constant (tv->dest_reg,
1828                                                    tv->const_adjust);
1829
1830                             if (GET_CODE (value) == PLUS)
1831                               {
1832                                 /* The constant could be too large for an add
1833                                    immediate, so can't directly emit an insn
1834                                    here.  */
1835                                 emit_unrolled_add (dest_reg, XEXP (value, 0),
1836                                                    XEXP (value, 1));
1837                               }
1838                           }
1839
1840                         /* Reset the giv to be just the register again, in case
1841                            it is used after the set we have just emitted.
1842                            We must subtract the const_adjust factor added in
1843                            above.  */
1844                         tv->dest_reg = plus_constant (dest_reg,
1845                                                       -tv->const_adjust);
1846                         *tv->location = tv->dest_reg;
1847                       }
1848                   }
1849             }
1850
1851           /* If this is a setting of a splittable variable, then determine
1852              how to split the variable, create a new set based on this split,
1853              and set up the reg_map so that later uses of the variable will
1854              use the new split variable.  */
1855
1856           dest_reg_was_split = 0;
1857
1858           if ((set = single_set (insn))
1859               && GET_CODE (SET_DEST (set)) == REG
1860               && splittable_regs[REGNO (SET_DEST (set))])
1861             {
1862               unsigned int regno = REGNO (SET_DEST (set));
1863               unsigned int src_regno;
1864
1865               dest_reg_was_split = 1;
1866
1867               giv_dest_reg = SET_DEST (set);
1868               giv_src_reg = giv_dest_reg;
1869               /* Compute the increment value for the giv, if it wasn't
1870                  already computed above.  */
1871               if (giv_inc == 0)
1872                 giv_inc = calculate_giv_inc (set, insn, regno);
1873
1874               src_regno = REGNO (giv_src_reg);
1875
1876               if (unroll_type == UNROLL_COMPLETELY)
1877                 {
1878                   /* Completely unrolling the loop.  Set the induction
1879                      variable to a known constant value.  */
1880
1881                   /* The value in splittable_regs may be an invariant
1882                      value, so we must use plus_constant here.  */
1883                   splittable_regs[regno]
1884                     = plus_constant (splittable_regs[src_regno],
1885                                      INTVAL (giv_inc));
1886
1887                   if (GET_CODE (splittable_regs[regno]) == PLUS)
1888                     {
1889                       giv_src_reg = XEXP (splittable_regs[regno], 0);
1890                       giv_inc = XEXP (splittable_regs[regno], 1);
1891                     }
1892                   else
1893                     {
1894                       /* The splittable_regs value must be a REG or a
1895                          CONST_INT, so put the entire value in the giv_src_reg
1896                          variable.  */
1897                       giv_src_reg = splittable_regs[regno];
1898                       giv_inc = const0_rtx;
1899                     }
1900                 }
1901               else
1902                 {
1903                   /* Partially unrolling loop.  Create a new pseudo
1904                      register for the iteration variable, and set it to
1905                      be a constant plus the original register.  Except
1906                      on the last iteration, when the result has to
1907                      go back into the original iteration var register.  */
1908
1909                   /* Handle bivs which must be mapped to a new register
1910                      when split.  This happens for bivs which need their
1911                      final value set before loop entry.  The new register
1912                      for the biv was stored in the biv's first struct
1913                      induction entry by find_splittable_regs.  */
1914
1915                   if (regno < max_reg_before_loop
1916                       && REG_IV_TYPE (ivs, regno) == BASIC_INDUCT)
1917                     {
1918                       giv_src_reg = ivs->reg_biv_class[regno]->biv->src_reg;
1919                       giv_dest_reg = giv_src_reg;
1920                     }
1921
1922 #if 0
1923                   /* If non-reduced/final-value givs were split, then
1924                      this would have to remap those givs also.  See
1925                      find_splittable_regs.  */
1926 #endif
1927
1928                   splittable_regs[regno]
1929                     = simplify_gen_binary (PLUS, GET_MODE (giv_src_reg),
1930                                            giv_inc,
1931                                            splittable_regs[src_regno]);
1932                   giv_inc = splittable_regs[regno];
1933
1934                   /* Now split the induction variable by changing the dest
1935                      of this insn to a new register, and setting its
1936                      reg_map entry to point to this new register.
1937
1938                      If this is the last iteration, and this is the last insn
1939                      that will update the iv, then reuse the original dest,
1940                      to ensure that the iv will have the proper value when
1941                      the loop exits or repeats.
1942
1943                      Using splittable_regs_updates here like this is safe,
1944                      because it can only be greater than one if all
1945                      instructions modifying the iv are always executed in
1946                      order.  */
1947
1948                   if (! last_iteration
1949                       || (splittable_regs_updates[regno]-- != 1))
1950                     {
1951                       tem = gen_reg_rtx (GET_MODE (giv_src_reg));
1952                       giv_dest_reg = tem;
1953                       map->reg_map[regno] = tem;
1954                       record_base_value (REGNO (tem),
1955                                          giv_inc == const0_rtx
1956                                          ? giv_src_reg
1957                                          : gen_rtx_PLUS (GET_MODE (giv_src_reg),
1958                                                          giv_src_reg, giv_inc),
1959                                          1);
1960                     }
1961                   else
1962                     map->reg_map[regno] = giv_src_reg;
1963                 }
1964
1965               /* The constant being added could be too large for an add
1966                  immediate, so can't directly emit an insn here.  */
1967               emit_unrolled_add (giv_dest_reg, giv_src_reg, giv_inc);
1968               copy = get_last_insn ();
1969               pattern = PATTERN (copy);
1970             }
1971           else
1972             {
1973               pattern = copy_rtx_and_substitute (pattern, map, 0);
1974               copy = emit_insn (pattern);
1975             }
1976           REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
1977
1978 #ifdef HAVE_cc0
1979           /* If this insn is setting CC0, it may need to look at
1980              the insn that uses CC0 to see what type of insn it is.
1981              In that case, the call to recog via validate_change will
1982              fail.  So don't substitute constants here.  Instead,
1983              do it when we emit the following insn.
1984
1985              For example, see the pyr.md file.  That machine has signed and
1986              unsigned compares.  The compare patterns must check the
1987              following branch insn to see which what kind of compare to
1988              emit.
1989
1990              If the previous insn set CC0, substitute constants on it as
1991              well.  */
1992           if (sets_cc0_p (PATTERN (copy)) != 0)
1993             cc0_insn = copy;
1994           else
1995             {
1996               if (cc0_insn)
1997                 try_constants (cc0_insn, map);
1998               cc0_insn = 0;
1999               try_constants (copy, map);
2000             }
2001 #else
2002           try_constants (copy, map);
2003 #endif
2004
2005           /* Make split induction variable constants `permanent' since we
2006              know there are no backward branches across iteration variable
2007              settings which would invalidate this.  */
2008           if (dest_reg_was_split)
2009             {
2010               int regno = REGNO (SET_DEST (set));
2011
2012               if ((size_t) regno < VARRAY_SIZE (map->const_equiv_varray)
2013                   && (VARRAY_CONST_EQUIV (map->const_equiv_varray, regno).age
2014                       == map->const_age))
2015                 VARRAY_CONST_EQUIV (map->const_equiv_varray, regno).age = -1;
2016             }
2017           break;
2018
2019         case JUMP_INSN:
2020           pattern = copy_rtx_and_substitute (PATTERN (insn), map, 0);
2021           copy = emit_jump_insn (pattern);
2022           REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
2023
2024           if (JUMP_LABEL (insn) == start_label && insn == copy_end
2025               && ! last_iteration)
2026             {
2027               /* Update JUMP_LABEL make invert_jump work correctly.  */
2028               JUMP_LABEL (copy) = get_label_from_map (map,
2029                                                       CODE_LABEL_NUMBER
2030                                                       (JUMP_LABEL (insn)));
2031               LABEL_NUSES (JUMP_LABEL (copy))++;
2032
2033               /* This is a branch to the beginning of the loop; this is the
2034                  last insn being copied; and this is not the last iteration.
2035                  In this case, we want to change the original fall through
2036                  case to be a branch past the end of the loop, and the
2037                  original jump label case to fall_through.  */
2038
2039               if (!invert_jump (copy, exit_label, 0))
2040                 {
2041                   rtx jmp;
2042                   rtx lab = gen_label_rtx ();
2043                   /* Can't do it by reversing the jump (probably because we
2044                      couldn't reverse the conditions), so emit a new
2045                      jump_insn after COPY, and redirect the jump around
2046                      that.  */
2047                   jmp = emit_jump_insn_after (gen_jump (exit_label), copy);
2048                   jmp = emit_barrier_after (jmp);
2049                   emit_label_after (lab, jmp);
2050                   LABEL_NUSES (lab) = 0;
2051                   if (!redirect_jump (copy, lab, 0))
2052                     abort ();
2053                 }
2054             }
2055
2056 #ifdef HAVE_cc0
2057           if (cc0_insn)
2058             try_constants (cc0_insn, map);
2059           cc0_insn = 0;
2060 #endif
2061           try_constants (copy, map);
2062
2063           /* Set the jump label of COPY correctly to avoid problems with
2064              later passes of unroll_loop, if INSN had jump label set.  */
2065           if (JUMP_LABEL (insn))
2066             {
2067               rtx label = 0;
2068
2069               /* Can't use the label_map for every insn, since this may be
2070                  the backward branch, and hence the label was not mapped.  */
2071               if ((set = single_set (copy)))
2072                 {
2073                   tem = SET_SRC (set);
2074                   if (GET_CODE (tem) == LABEL_REF)
2075                     label = XEXP (tem, 0);
2076                   else if (GET_CODE (tem) == IF_THEN_ELSE)
2077                     {
2078                       if (XEXP (tem, 1) != pc_rtx)
2079                         label = XEXP (XEXP (tem, 1), 0);
2080                       else
2081                         label = XEXP (XEXP (tem, 2), 0);
2082                     }
2083                 }
2084
2085               if (label && GET_CODE (label) == CODE_LABEL)
2086                 JUMP_LABEL (copy) = label;
2087               else
2088                 {
2089                   /* An unrecognizable jump insn, probably the entry jump
2090                      for a switch statement.  This label must have been mapped,
2091                      so just use the label_map to get the new jump label.  */
2092                   JUMP_LABEL (copy)
2093                     = get_label_from_map (map,
2094                                           CODE_LABEL_NUMBER (JUMP_LABEL (insn)));
2095                 }
2096
2097               /* If this is a non-local jump, then must increase the label
2098                  use count so that the label will not be deleted when the
2099                  original jump is deleted.  */
2100               LABEL_NUSES (JUMP_LABEL (copy))++;
2101             }
2102           else if (GET_CODE (PATTERN (copy)) == ADDR_VEC
2103                    || GET_CODE (PATTERN (copy)) == ADDR_DIFF_VEC)
2104             {
2105               rtx pat = PATTERN (copy);
2106               int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
2107               int len = XVECLEN (pat, diff_vec_p);
2108               int i;
2109
2110               for (i = 0; i < len; i++)
2111                 LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))++;
2112             }
2113
2114           /* If this used to be a conditional jump insn but whose branch
2115              direction is now known, we must do something special.  */
2116           if (any_condjump_p (insn) && onlyjump_p (insn) && map->last_pc_value)
2117             {
2118 #ifdef HAVE_cc0
2119               /* If the previous insn set cc0 for us, delete it.  */
2120               if (sets_cc0_p (PREV_INSN (copy)))
2121                 delete_insn (PREV_INSN (copy));
2122 #endif
2123
2124               /* If this is now a no-op, delete it.  */
2125               if (map->last_pc_value == pc_rtx)
2126                 {
2127                   /* Don't let delete_insn delete the label referenced here,
2128                      because we might possibly need it later for some other
2129                      instruction in the loop.  */
2130                   if (JUMP_LABEL (copy))
2131                     LABEL_NUSES (JUMP_LABEL (copy))++;
2132                   delete_insn (copy);
2133                   if (JUMP_LABEL (copy))
2134                     LABEL_NUSES (JUMP_LABEL (copy))--;
2135                   copy = 0;
2136                 }
2137               else
2138                 /* Otherwise, this is unconditional jump so we must put a
2139                    BARRIER after it.  We could do some dead code elimination
2140                    here, but jump.c will do it just as well.  */
2141                 emit_barrier ();
2142             }
2143           break;
2144
2145         case CALL_INSN:
2146           pattern = copy_rtx_and_substitute (PATTERN (insn), map, 0);
2147           copy = emit_call_insn (pattern);
2148           REG_NOTES (copy) = initial_reg_note_copy (REG_NOTES (insn), map);
2149
2150           /* Because the USAGE information potentially contains objects other
2151              than hard registers, we need to copy it.  */
2152           CALL_INSN_FUNCTION_USAGE (copy)
2153             = copy_rtx_and_substitute (CALL_INSN_FUNCTION_USAGE (insn),
2154                                        map, 0);
2155
2156 #ifdef HAVE_cc0
2157           if (cc0_insn)
2158             try_constants (cc0_insn, map);
2159           cc0_insn = 0;
2160 #endif
2161           try_constants (copy, map);
2162
2163           /* Be lazy and assume CALL_INSNs clobber all hard registers.  */
2164           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2165             VARRAY_CONST_EQUIV (map->const_equiv_varray, i).rtx = 0;
2166           break;
2167
2168         case CODE_LABEL:
2169           /* If this is the loop start label, then we don't need to emit a
2170              copy of this label since no one will use it.  */
2171
2172           if (insn != start_label)
2173             {
2174               copy = emit_label (get_label_from_map (map,
2175                                                      CODE_LABEL_NUMBER (insn)));
2176               map->const_age++;
2177             }
2178           break;
2179
2180         case BARRIER:
2181           copy = emit_barrier ();
2182           break;
2183
2184         case NOTE:
2185           /* VTOP and CONT notes are valid only before the loop exit test.
2186              If placed anywhere else, loop may generate bad code.  */
2187           /* BASIC_BLOCK notes exist to stabilize basic block structures with
2188              the associated rtl.  We do not want to share the structure in
2189              this new block.  */
2190
2191           if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED
2192               && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL
2193               && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK
2194               && ((NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_VTOP
2195                    && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_CONT)
2196                   || (last_iteration && unroll_type != UNROLL_COMPLETELY)))
2197             copy = emit_note (NOTE_SOURCE_FILE (insn),
2198                               NOTE_LINE_NUMBER (insn));
2199           else
2200             copy = 0;
2201           break;
2202
2203         default:
2204           abort ();
2205         }
2206
2207       map->insn_map[INSN_UID (insn)] = copy;
2208     }
2209   while (insn != copy_end);
2210
2211   /* Now finish coping the REG_NOTES.  */
2212   insn = copy_start;
2213   do
2214     {
2215       insn = NEXT_INSN (insn);
2216       if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2217            || GET_CODE (insn) == CALL_INSN)
2218           && map->insn_map[INSN_UID (insn)])
2219         final_reg_note_copy (REG_NOTES (map->insn_map[INSN_UID (insn)]), map);
2220     }
2221   while (insn != copy_end);
2222
2223   /* There may be notes between copy_notes_from and loop_end.  Emit a copy of
2224      each of these notes here, since there may be some important ones, such as
2225      NOTE_INSN_BLOCK_END notes, in this group.  We don't do this on the last
2226      iteration, because the original notes won't be deleted.
2227
2228      We can't use insert_before here, because when from preconditioning,
2229      insert_before points before the loop.  We can't use copy_end, because
2230      there may be insns already inserted after it (which we don't want to
2231      copy) when not from preconditioning code.  */
2232
2233   if (! last_iteration)
2234     {
2235       for (insn = copy_notes_from; insn != loop_end; insn = NEXT_INSN (insn))
2236         {
2237           /* VTOP notes are valid only before the loop exit test.
2238              If placed anywhere else, loop may generate bad code.
2239              There is no need to test for NOTE_INSN_LOOP_CONT notes
2240              here, since COPY_NOTES_FROM will be at most one or two (for cc0)
2241              instructions before the last insn in the loop, and if the
2242              end test is that short, there will be a VTOP note between
2243              the CONT note and the test.  */
2244           if (GET_CODE (insn) == NOTE
2245               && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED
2246               && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK
2247               && NOTE_LINE_NUMBER (insn) != NOTE_INSN_LOOP_VTOP)
2248             emit_note (NOTE_SOURCE_FILE (insn), NOTE_LINE_NUMBER (insn));
2249         }
2250     }
2251
2252   if (final_label && LABEL_NUSES (final_label) > 0)
2253     emit_label (final_label);
2254
2255   tem = gen_sequence ();
2256   end_sequence ();
2257   emit_insn_before (tem, insert_before);
2258 }
2259 \f
2260 /* Emit an insn, using the expand_binop to ensure that a valid insn is
2261    emitted.  This will correctly handle the case where the increment value
2262    won't fit in the immediate field of a PLUS insns.  */
2263
2264 void
2265 emit_unrolled_add (dest_reg, src_reg, increment)
2266      rtx dest_reg, src_reg, increment;
2267 {
2268   rtx result;
2269
2270   result = expand_binop (GET_MODE (dest_reg), add_optab, src_reg, increment,
2271                          dest_reg, 0, OPTAB_LIB_WIDEN);
2272
2273   if (dest_reg != result)
2274     emit_move_insn (dest_reg, result);
2275 }
2276 \f
2277 /* Searches the insns between INSN and LOOP->END.  Returns 1 if there
2278    is a backward branch in that range that branches to somewhere between
2279    LOOP->START and INSN.  Returns 0 otherwise.  */
2280
2281 /* ??? This is quadratic algorithm.  Could be rewritten to be linear.
2282    In practice, this is not a problem, because this function is seldom called,
2283    and uses a negligible amount of CPU time on average.  */
2284
2285 int
2286 back_branch_in_range_p (loop, insn)
2287      const struct loop *loop;
2288      rtx insn;
2289 {
2290   rtx p, q, target_insn;
2291   rtx loop_start = loop->start;
2292   rtx loop_end = loop->end;
2293   rtx orig_loop_end = loop->end;
2294
2295   /* Stop before we get to the backward branch at the end of the loop.  */
2296   loop_end = prev_nonnote_insn (loop_end);
2297   if (GET_CODE (loop_end) == BARRIER)
2298     loop_end = PREV_INSN (loop_end);
2299
2300   /* Check in case insn has been deleted, search forward for first non
2301      deleted insn following it.  */
2302   while (INSN_DELETED_P (insn))
2303     insn = NEXT_INSN (insn);
2304
2305   /* Check for the case where insn is the last insn in the loop.  Deal
2306      with the case where INSN was a deleted loop test insn, in which case
2307      it will now be the NOTE_LOOP_END.  */
2308   if (insn == loop_end || insn == orig_loop_end)
2309     return 0;
2310
2311   for (p = NEXT_INSN (insn); p != loop_end; p = NEXT_INSN (p))
2312     {
2313       if (GET_CODE (p) == JUMP_INSN)
2314         {
2315           target_insn = JUMP_LABEL (p);
2316
2317           /* Search from loop_start to insn, to see if one of them is
2318              the target_insn.  We can't use INSN_LUID comparisons here,
2319              since insn may not have an LUID entry.  */
2320           for (q = loop_start; q != insn; q = NEXT_INSN (q))
2321             if (q == target_insn)
2322               return 1;
2323         }
2324     }
2325
2326   return 0;
2327 }
2328
2329 /* Try to generate the simplest rtx for the expression
2330    (PLUS (MULT mult1 mult2) add1).  This is used to calculate the initial
2331    value of giv's.  */
2332
2333 static rtx
2334 fold_rtx_mult_add (mult1, mult2, add1, mode)
2335      rtx mult1, mult2, add1;
2336      enum machine_mode mode;
2337 {
2338   rtx temp, mult_res;
2339   rtx result;
2340
2341   /* The modes must all be the same.  This should always be true.  For now,
2342      check to make sure.  */
2343   if ((GET_MODE (mult1) != mode && GET_MODE (mult1) != VOIDmode)
2344       || (GET_MODE (mult2) != mode && GET_MODE (mult2) != VOIDmode)
2345       || (GET_MODE (add1) != mode && GET_MODE (add1) != VOIDmode))
2346     abort ();
2347
2348   /* Ensure that if at least one of mult1/mult2 are constant, then mult2
2349      will be a constant.  */
2350   if (GET_CODE (mult1) == CONST_INT)
2351     {
2352       temp = mult2;
2353       mult2 = mult1;
2354       mult1 = temp;
2355     }
2356
2357   mult_res = simplify_binary_operation (MULT, mode, mult1, mult2);
2358   if (! mult_res)
2359     mult_res = gen_rtx_MULT (mode, mult1, mult2);
2360
2361   /* Again, put the constant second.  */
2362   if (GET_CODE (add1) == CONST_INT)
2363     {
2364       temp = add1;
2365       add1 = mult_res;
2366       mult_res = temp;
2367     }
2368
2369   result = simplify_binary_operation (PLUS, mode, add1, mult_res);
2370   if (! result)
2371     result = gen_rtx_PLUS (mode, add1, mult_res);
2372
2373   return result;
2374 }
2375
2376 /* Searches the list of induction struct's for the biv BL, to try to calculate
2377    the total increment value for one iteration of the loop as a constant.
2378
2379    Returns the increment value as an rtx, simplified as much as possible,
2380    if it can be calculated.  Otherwise, returns 0.  */
2381
2382 rtx
2383 biv_total_increment (bl)
2384      struct iv_class *bl;
2385 {
2386   struct induction *v;
2387   rtx result;
2388
2389   /* For increment, must check every instruction that sets it.  Each
2390      instruction must be executed only once each time through the loop.
2391      To verify this, we check that the insn is always executed, and that
2392      there are no backward branches after the insn that branch to before it.
2393      Also, the insn must have a mult_val of one (to make sure it really is
2394      an increment).  */
2395
2396   result = const0_rtx;
2397   for (v = bl->biv; v; v = v->next_iv)
2398     {
2399       if (v->always_computable && v->mult_val == const1_rtx
2400           && ! v->maybe_multiple)
2401         result = fold_rtx_mult_add (result, const1_rtx, v->add_val, v->mode);
2402       else
2403         return 0;
2404     }
2405
2406   return result;
2407 }
2408
2409 /* For each biv and giv, determine whether it can be safely split into
2410    a different variable for each unrolled copy of the loop body.  If it
2411    is safe to split, then indicate that by saving some useful info
2412    in the splittable_regs array.
2413
2414    If the loop is being completely unrolled, then splittable_regs will hold
2415    the current value of the induction variable while the loop is unrolled.
2416    It must be set to the initial value of the induction variable here.
2417    Otherwise, splittable_regs will hold the difference between the current
2418    value of the induction variable and the value the induction variable had
2419    at the top of the loop.  It must be set to the value 0 here.
2420
2421    Returns the total number of instructions that set registers that are
2422    splittable.  */
2423
2424 /* ?? If the loop is only unrolled twice, then most of the restrictions to
2425    constant values are unnecessary, since we can easily calculate increment
2426    values in this case even if nothing is constant.  The increment value
2427    should not involve a multiply however.  */
2428
2429 /* ?? Even if the biv/giv increment values aren't constant, it may still
2430    be beneficial to split the variable if the loop is only unrolled a few
2431    times, since multiplies by small integers (1,2,3,4) are very cheap.  */
2432
2433 static int
2434 find_splittable_regs (loop, unroll_type, end_insert_before, unroll_number)
2435      const struct loop *loop;
2436      enum unroll_types unroll_type;
2437      rtx end_insert_before;
2438      int unroll_number;
2439 {
2440   struct loop_ivs *ivs = LOOP_IVS (loop);
2441   struct iv_class *bl;
2442   struct induction *v;
2443   rtx increment, tem;
2444   rtx biv_final_value;
2445   int biv_splittable;
2446   int result = 0;
2447   rtx loop_start = loop->start;
2448   rtx loop_end = loop->end;
2449
2450   for (bl = ivs->loop_iv_list; bl; bl = bl->next)
2451     {
2452       /* Biv_total_increment must return a constant value,
2453          otherwise we can not calculate the split values.  */
2454
2455       increment = biv_total_increment (bl);
2456       if (! increment || GET_CODE (increment) != CONST_INT)
2457         continue;
2458
2459       /* The loop must be unrolled completely, or else have a known number
2460          of iterations and only one exit, or else the biv must be dead
2461          outside the loop, or else the final value must be known.  Otherwise,
2462          it is unsafe to split the biv since it may not have the proper
2463          value on loop exit.  */
2464
2465       /* loop_number_exit_count is non-zero if the loop has an exit other than
2466          a fall through at the end.  */
2467
2468       biv_splittable = 1;
2469       biv_final_value = 0;
2470       if (unroll_type != UNROLL_COMPLETELY
2471           && (loop->exit_count || unroll_type == UNROLL_NAIVE)
2472           && (uid_luid[REGNO_LAST_UID (bl->regno)] >= INSN_LUID (loop_end)
2473               || ! bl->init_insn
2474               || INSN_UID (bl->init_insn) >= max_uid_for_loop
2475               || (uid_luid[REGNO_FIRST_UID (bl->regno)]
2476                   < INSN_LUID (bl->init_insn))
2477               || reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
2478           && ! (biv_final_value = final_biv_value (loop, bl)))
2479         biv_splittable = 0;
2480
2481       /* If any of the insns setting the BIV don't do so with a simple
2482          PLUS, we don't know how to split it.  */
2483       for (v = bl->biv; biv_splittable && v; v = v->next_iv)
2484         if ((tem = single_set (v->insn)) == 0
2485             || GET_CODE (SET_DEST (tem)) != REG
2486             || REGNO (SET_DEST (tem)) != bl->regno
2487             || GET_CODE (SET_SRC (tem)) != PLUS)
2488           biv_splittable = 0;
2489
2490       /* If final value is non-zero, then must emit an instruction which sets
2491          the value of the biv to the proper value.  This is done after
2492          handling all of the givs, since some of them may need to use the
2493          biv's value in their initialization code.  */
2494
2495       /* This biv is splittable.  If completely unrolling the loop, save
2496          the biv's initial value.  Otherwise, save the constant zero.  */
2497
2498       if (biv_splittable == 1)
2499         {
2500           if (unroll_type == UNROLL_COMPLETELY)
2501             {
2502               /* If the initial value of the biv is itself (i.e. it is too
2503                  complicated for strength_reduce to compute), or is a hard
2504                  register, or it isn't invariant, then we must create a new
2505                  pseudo reg to hold the initial value of the biv.  */
2506
2507               if (GET_CODE (bl->initial_value) == REG
2508                   && (REGNO (bl->initial_value) == bl->regno
2509                       || REGNO (bl->initial_value) < FIRST_PSEUDO_REGISTER
2510                       || ! loop_invariant_p (loop, bl->initial_value)))
2511                 {
2512                   rtx tem = gen_reg_rtx (bl->biv->mode);
2513
2514                   record_base_value (REGNO (tem), bl->biv->add_val, 0);
2515                   emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
2516                                     loop_start);
2517
2518                   if (loop_dump_stream)
2519                     fprintf (loop_dump_stream,
2520                              "Biv %d initial value remapped to %d.\n",
2521                              bl->regno, REGNO (tem));
2522
2523                   splittable_regs[bl->regno] = tem;
2524                 }
2525               else
2526                 splittable_regs[bl->regno] = bl->initial_value;
2527             }
2528           else
2529             splittable_regs[bl->regno] = const0_rtx;
2530
2531           /* Save the number of instructions that modify the biv, so that
2532              we can treat the last one specially.  */
2533
2534           splittable_regs_updates[bl->regno] = bl->biv_count;
2535           result += bl->biv_count;
2536
2537           if (loop_dump_stream)
2538             fprintf (loop_dump_stream,
2539                      "Biv %d safe to split.\n", bl->regno);
2540         }
2541
2542       /* Check every giv that depends on this biv to see whether it is
2543          splittable also.  Even if the biv isn't splittable, givs which
2544          depend on it may be splittable if the biv is live outside the
2545          loop, and the givs aren't.  */
2546
2547       result += find_splittable_givs (loop, bl, unroll_type, increment,
2548                                       unroll_number);
2549
2550       /* If final value is non-zero, then must emit an instruction which sets
2551          the value of the biv to the proper value.  This is done after
2552          handling all of the givs, since some of them may need to use the
2553          biv's value in their initialization code.  */
2554       if (biv_final_value)
2555         {
2556           /* If the loop has multiple exits, emit the insns before the
2557              loop to ensure that it will always be executed no matter
2558              how the loop exits.  Otherwise emit the insn after the loop,
2559              since this is slightly more efficient.  */
2560           if (! loop->exit_count)
2561             emit_insn_before (gen_move_insn (bl->biv->src_reg,
2562                                              biv_final_value),
2563                               end_insert_before);
2564           else
2565             {
2566               /* Create a new register to hold the value of the biv, and then
2567                  set the biv to its final value before the loop start.  The biv
2568                  is set to its final value before loop start to ensure that
2569                  this insn will always be executed, no matter how the loop
2570                  exits.  */
2571               rtx tem = gen_reg_rtx (bl->biv->mode);
2572               record_base_value (REGNO (tem), bl->biv->add_val, 0);
2573
2574               emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
2575                                 loop_start);
2576               emit_insn_before (gen_move_insn (bl->biv->src_reg,
2577                                                biv_final_value),
2578                                 loop_start);
2579
2580               if (loop_dump_stream)
2581                 fprintf (loop_dump_stream, "Biv %d mapped to %d for split.\n",
2582                          REGNO (bl->biv->src_reg), REGNO (tem));
2583
2584               /* Set up the mapping from the original biv register to the new
2585                  register.  */
2586               bl->biv->src_reg = tem;
2587             }
2588         }
2589     }
2590   return result;
2591 }
2592
2593 /* Return 1 if the first and last unrolled copy of the address giv V is valid
2594    for the instruction that is using it.  Do not make any changes to that
2595    instruction.  */
2596
2597 static int
2598 verify_addresses (v, giv_inc, unroll_number)
2599      struct induction *v;
2600      rtx giv_inc;
2601      int unroll_number;
2602 {
2603   int ret = 1;
2604   rtx orig_addr = *v->location;
2605   rtx last_addr = plus_constant (v->dest_reg,
2606                                  INTVAL (giv_inc) * (unroll_number - 1));
2607
2608   /* First check to see if either address would fail.   Handle the fact
2609      that we have may have a match_dup.  */
2610   if (! validate_replace_rtx (*v->location, v->dest_reg, v->insn)
2611       || ! validate_replace_rtx (*v->location, last_addr, v->insn))
2612     ret = 0;
2613
2614   /* Now put things back the way they were before.  This should always
2615    succeed.  */
2616   if (! validate_replace_rtx (*v->location, orig_addr, v->insn))
2617     abort ();
2618
2619   return ret;
2620 }
2621
2622 /* For every giv based on the biv BL, check to determine whether it is
2623    splittable.  This is a subroutine to find_splittable_regs ().
2624
2625    Return the number of instructions that set splittable registers.  */
2626
2627 static int
2628 find_splittable_givs (loop, bl, unroll_type, increment, unroll_number)
2629      const struct loop *loop;
2630      struct iv_class *bl;
2631      enum unroll_types unroll_type;
2632      rtx increment;
2633      int unroll_number;
2634 {
2635   struct loop_ivs *ivs = LOOP_IVS (loop);
2636   struct induction *v, *v2;
2637   rtx final_value;
2638   rtx tem;
2639   int result = 0;
2640
2641   /* Scan the list of givs, and set the same_insn field when there are
2642      multiple identical givs in the same insn.  */
2643   for (v = bl->giv; v; v = v->next_iv)
2644     for (v2 = v->next_iv; v2; v2 = v2->next_iv)
2645       if (v->insn == v2->insn && rtx_equal_p (v->new_reg, v2->new_reg)
2646           && ! v2->same_insn)
2647         v2->same_insn = v;
2648
2649   for (v = bl->giv; v; v = v->next_iv)
2650     {
2651       rtx giv_inc, value;
2652
2653       /* Only split the giv if it has already been reduced, or if the loop is
2654          being completely unrolled.  */
2655       if (unroll_type != UNROLL_COMPLETELY && v->ignore)
2656         continue;
2657
2658       /* The giv can be split if the insn that sets the giv is executed once
2659          and only once on every iteration of the loop.  */
2660       /* An address giv can always be split.  v->insn is just a use not a set,
2661          and hence it does not matter whether it is always executed.  All that
2662          matters is that all the biv increments are always executed, and we
2663          won't reach here if they aren't.  */
2664       if (v->giv_type != DEST_ADDR
2665           && (! v->always_computable
2666               || back_branch_in_range_p (loop, v->insn)))
2667         continue;
2668
2669       /* The giv increment value must be a constant.  */
2670       giv_inc = fold_rtx_mult_add (v->mult_val, increment, const0_rtx,
2671                                    v->mode);
2672       if (! giv_inc || GET_CODE (giv_inc) != CONST_INT)
2673         continue;
2674
2675       /* The loop must be unrolled completely, or else have a known number of
2676          iterations and only one exit, or else the giv must be dead outside
2677          the loop, or else the final value of the giv must be known.
2678          Otherwise, it is not safe to split the giv since it may not have the
2679          proper value on loop exit.  */
2680
2681       /* The used outside loop test will fail for DEST_ADDR givs.  They are
2682          never used outside the loop anyways, so it is always safe to split a
2683          DEST_ADDR giv.  */
2684
2685       final_value = 0;
2686       if (unroll_type != UNROLL_COMPLETELY
2687           && (loop->exit_count || unroll_type == UNROLL_NAIVE)
2688           && v->giv_type != DEST_ADDR
2689           /* The next part is true if the pseudo is used outside the loop.
2690              We assume that this is true for any pseudo created after loop
2691              starts, because we don't have a reg_n_info entry for them.  */
2692           && (REGNO (v->dest_reg) >= max_reg_before_loop
2693               || (REGNO_FIRST_UID (REGNO (v->dest_reg)) != INSN_UID (v->insn)
2694                   /* Check for the case where the pseudo is set by a shift/add
2695                      sequence, in which case the first insn setting the pseudo
2696                      is the first insn of the shift/add sequence.  */
2697                   && (! (tem = find_reg_note (v->insn, REG_RETVAL, NULL_RTX))
2698                       || (REGNO_FIRST_UID (REGNO (v->dest_reg))
2699                           != INSN_UID (XEXP (tem, 0)))))
2700               /* Line above always fails if INSN was moved by loop opt.  */
2701               || (uid_luid[REGNO_LAST_UID (REGNO (v->dest_reg))]
2702                   >= INSN_LUID (loop->end)))
2703           && ! (final_value = v->final_value))
2704         continue;
2705
2706 #if 0
2707       /* Currently, non-reduced/final-value givs are never split.  */
2708       /* Should emit insns after the loop if possible, as the biv final value
2709          code below does.  */
2710
2711       /* If the final value is non-zero, and the giv has not been reduced,
2712          then must emit an instruction to set the final value.  */
2713       if (final_value && !v->new_reg)
2714         {
2715           /* Create a new register to hold the value of the giv, and then set
2716              the giv to its final value before the loop start.  The giv is set
2717              to its final value before loop start to ensure that this insn
2718              will always be executed, no matter how we exit.  */
2719           tem = gen_reg_rtx (v->mode);
2720           emit_insn_before (gen_move_insn (tem, v->dest_reg), loop_start);
2721           emit_insn_before (gen_move_insn (v->dest_reg, final_value),
2722                             loop_start);
2723
2724           if (loop_dump_stream)
2725             fprintf (loop_dump_stream, "Giv %d mapped to %d for split.\n",
2726                      REGNO (v->dest_reg), REGNO (tem));
2727
2728           v->src_reg = tem;
2729         }
2730 #endif
2731
2732       /* This giv is splittable.  If completely unrolling the loop, save the
2733          giv's initial value.  Otherwise, save the constant zero for it.  */
2734
2735       if (unroll_type == UNROLL_COMPLETELY)
2736         {
2737           /* It is not safe to use bl->initial_value here, because it may not
2738              be invariant.  It is safe to use the initial value stored in
2739              the splittable_regs array if it is set.  In rare cases, it won't
2740              be set, so then we do exactly the same thing as
2741              find_splittable_regs does to get a safe value.  */
2742           rtx biv_initial_value;
2743
2744           if (splittable_regs[bl->regno])
2745             biv_initial_value = splittable_regs[bl->regno];
2746           else if (GET_CODE (bl->initial_value) != REG
2747                    || (REGNO (bl->initial_value) != bl->regno
2748                        && REGNO (bl->initial_value) >= FIRST_PSEUDO_REGISTER))
2749             biv_initial_value = bl->initial_value;
2750           else
2751             {
2752               rtx tem = gen_reg_rtx (bl->biv->mode);
2753
2754               record_base_value (REGNO (tem), bl->biv->add_val, 0);
2755               emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
2756                                 loop->start);
2757               biv_initial_value = tem;
2758             }
2759           biv_initial_value = extend_value_for_giv (v, biv_initial_value);
2760           value = fold_rtx_mult_add (v->mult_val, biv_initial_value,
2761                                      v->add_val, v->mode);
2762         }
2763       else
2764         value = const0_rtx;
2765
2766       if (v->new_reg)
2767         {
2768           /* If a giv was combined with another giv, then we can only split
2769              this giv if the giv it was combined with was reduced.  This
2770              is because the value of v->new_reg is meaningless in this
2771              case.  */
2772           if (v->same && ! v->same->new_reg)
2773             {
2774               if (loop_dump_stream)
2775                 fprintf (loop_dump_stream,
2776                          "giv combined with unreduced giv not split.\n");
2777               continue;
2778             }
2779           /* If the giv is an address destination, it could be something other
2780              than a simple register, these have to be treated differently.  */
2781           else if (v->giv_type == DEST_REG)
2782             {
2783               /* If value is not a constant, register, or register plus
2784                  constant, then compute its value into a register before
2785                  loop start.  This prevents invalid rtx sharing, and should
2786                  generate better code.  We can use bl->initial_value here
2787                  instead of splittable_regs[bl->regno] because this code
2788                  is going before the loop start.  */
2789               if (unroll_type == UNROLL_COMPLETELY
2790                   && GET_CODE (value) != CONST_INT
2791                   && GET_CODE (value) != REG
2792                   && (GET_CODE (value) != PLUS
2793                       || GET_CODE (XEXP (value, 0)) != REG
2794                       || GET_CODE (XEXP (value, 1)) != CONST_INT))
2795                 {
2796                   rtx tem = gen_reg_rtx (v->mode);
2797                   record_base_value (REGNO (tem), v->add_val, 0);
2798                   emit_iv_add_mult (bl->initial_value, v->mult_val,
2799                                     v->add_val, tem, loop->start);
2800                   value = tem;
2801                 }
2802
2803               splittable_regs[REGNO (v->new_reg)] = value;
2804             }
2805           else
2806             {
2807               /* Splitting address givs is useful since it will often allow us
2808                  to eliminate some increment insns for the base giv as
2809                  unnecessary.  */
2810
2811               /* If the addr giv is combined with a dest_reg giv, then all
2812                  references to that dest reg will be remapped, which is NOT
2813                  what we want for split addr regs. We always create a new
2814                  register for the split addr giv, just to be safe.  */
2815
2816               /* If we have multiple identical address givs within a
2817                  single instruction, then use a single pseudo reg for
2818                  both.  This is necessary in case one is a match_dup
2819                  of the other.  */
2820
2821               v->const_adjust = 0;
2822
2823               if (v->same_insn)
2824                 {
2825                   v->dest_reg = v->same_insn->dest_reg;
2826                   if (loop_dump_stream)
2827                     fprintf (loop_dump_stream,
2828                              "Sharing address givs in insn %d\n",
2829                              INSN_UID (v->insn));
2830                 }
2831               /* If multiple address GIVs have been combined with the
2832                  same dest_reg GIV, do not create a new register for
2833                  each.  */
2834               else if (unroll_type != UNROLL_COMPLETELY
2835                        && v->giv_type == DEST_ADDR
2836                        && v->same && v->same->giv_type == DEST_ADDR
2837                        && v->same->unrolled
2838                        /* combine_givs_p may return true for some cases
2839                           where the add and mult values are not equal.
2840                           To share a register here, the values must be
2841                           equal.  */
2842                        && rtx_equal_p (v->same->mult_val, v->mult_val)
2843                        && rtx_equal_p (v->same->add_val, v->add_val)
2844                        /* If the memory references have different modes,
2845                           then the address may not be valid and we must
2846                           not share registers.  */
2847                        && verify_addresses (v, giv_inc, unroll_number))
2848                 {
2849                   v->dest_reg = v->same->dest_reg;
2850                   v->shared = 1;
2851                 }
2852               else if (unroll_type != UNROLL_COMPLETELY)
2853                 {
2854                   /* If not completely unrolling the loop, then create a new
2855                      register to hold the split value of the DEST_ADDR giv.
2856                      Emit insn to initialize its value before loop start.  */
2857
2858                   rtx tem = gen_reg_rtx (v->mode);
2859                   struct induction *same = v->same;
2860                   rtx new_reg = v->new_reg;
2861                   record_base_value (REGNO (tem), v->add_val, 0);
2862
2863                   /* If the address giv has a constant in its new_reg value,
2864                      then this constant can be pulled out and put in value,
2865                      instead of being part of the initialization code.  */
2866
2867                   if (GET_CODE (new_reg) == PLUS
2868                       && GET_CODE (XEXP (new_reg, 1)) == CONST_INT)
2869                     {
2870                       v->dest_reg
2871                         = plus_constant (tem, INTVAL (XEXP (new_reg, 1)));
2872
2873                       /* Only succeed if this will give valid addresses.
2874                          Try to validate both the first and the last
2875                          address resulting from loop unrolling, if
2876                          one fails, then can't do const elim here.  */
2877                       if (verify_addresses (v, giv_inc, unroll_number))
2878                         {
2879                           /* Save the negative of the eliminated const, so
2880                              that we can calculate the dest_reg's increment
2881                              value later.  */
2882                           v->const_adjust = -INTVAL (XEXP (new_reg, 1));
2883
2884                           new_reg = XEXP (new_reg, 0);
2885                           if (loop_dump_stream)
2886                             fprintf (loop_dump_stream,
2887                                      "Eliminating constant from giv %d\n",
2888                                      REGNO (tem));
2889                         }
2890                       else
2891                         v->dest_reg = tem;
2892                     }
2893                   else
2894                     v->dest_reg = tem;
2895
2896                   /* If the address hasn't been checked for validity yet, do so
2897                      now, and fail completely if either the first or the last
2898                      unrolled copy of the address is not a valid address
2899                      for the instruction that uses it.  */
2900                   if (v->dest_reg == tem
2901                       && ! verify_addresses (v, giv_inc, unroll_number))
2902                     {
2903                       for (v2 = v->next_iv; v2; v2 = v2->next_iv)
2904                         if (v2->same_insn == v)
2905                           v2->same_insn = 0;
2906
2907                       if (loop_dump_stream)
2908                         fprintf (loop_dump_stream,
2909                                  "Invalid address for giv at insn %d\n",
2910                                  INSN_UID (v->insn));
2911                       continue;
2912                     }
2913
2914                   v->new_reg = new_reg;
2915                   v->same = same;
2916
2917                   /* We set this after the address check, to guarantee that
2918                      the register will be initialized.  */
2919                   v->unrolled = 1;
2920
2921                   /* To initialize the new register, just move the value of
2922                      new_reg into it.  This is not guaranteed to give a valid
2923                      instruction on machines with complex addressing modes.
2924                      If we can't recognize it, then delete it and emit insns
2925                      to calculate the value from scratch.  */
2926                   emit_insn_before (gen_rtx_SET (VOIDmode, tem,
2927                                                  copy_rtx (v->new_reg)),
2928                                     loop->start);
2929                   if (recog_memoized (PREV_INSN (loop->start)) < 0)
2930                     {
2931                       rtx sequence, ret;
2932
2933                       /* We can't use bl->initial_value to compute the initial
2934                          value, because the loop may have been preconditioned.
2935                          We must calculate it from NEW_REG.  Try using
2936                          force_operand instead of emit_iv_add_mult.  */
2937                       delete_insn (PREV_INSN (loop->start));
2938
2939                       start_sequence ();
2940                       ret = force_operand (v->new_reg, tem);
2941                       if (ret != tem)
2942                         emit_move_insn (tem, ret);
2943                       sequence = gen_sequence ();
2944                       end_sequence ();
2945                       emit_insn_before (sequence, loop->start);
2946
2947                       if (loop_dump_stream)
2948                         fprintf (loop_dump_stream,
2949                                  "Invalid init insn, rewritten.\n");
2950                     }
2951                 }
2952               else
2953                 {
2954                   v->dest_reg = value;
2955
2956                   /* Check the resulting address for validity, and fail
2957                      if the resulting address would be invalid.  */
2958                   if (! verify_addresses (v, giv_inc, unroll_number))
2959                     {
2960                       for (v2 = v->next_iv; v2; v2 = v2->next_iv)
2961                         if (v2->same_insn == v)
2962                           v2->same_insn = 0;
2963
2964                       if (loop_dump_stream)
2965                         fprintf (loop_dump_stream,
2966                                  "Invalid address for giv at insn %d\n",
2967                                  INSN_UID (v->insn));
2968                       continue;
2969                     }
2970                 }
2971
2972               /* Store the value of dest_reg into the insn.  This sharing
2973                  will not be a problem as this insn will always be copied
2974                  later.  */
2975
2976               *v->location = v->dest_reg;
2977
2978               /* If this address giv is combined with a dest reg giv, then
2979                  save the base giv's induction pointer so that we will be
2980                  able to handle this address giv properly.  The base giv
2981                  itself does not have to be splittable.  */
2982
2983               if (v->same && v->same->giv_type == DEST_REG)
2984                 addr_combined_regs[REGNO (v->same->new_reg)] = v->same;
2985
2986               if (GET_CODE (v->new_reg) == REG)
2987                 {
2988                   /* This giv maybe hasn't been combined with any others.
2989                      Make sure that it's giv is marked as splittable here.  */
2990
2991                   splittable_regs[REGNO (v->new_reg)] = value;
2992
2993                   /* Make it appear to depend upon itself, so that the
2994                      giv will be properly split in the main loop above.  */
2995                   if (! v->same)
2996                     {
2997                       v->same = v;
2998                       addr_combined_regs[REGNO (v->new_reg)] = v;
2999                     }
3000                 }
3001
3002               if (loop_dump_stream)
3003                 fprintf (loop_dump_stream, "DEST_ADDR giv being split.\n");
3004             }
3005         }
3006       else
3007         {
3008 #if 0
3009           /* Currently, unreduced giv's can't be split.  This is not too much
3010              of a problem since unreduced giv's are not live across loop
3011              iterations anyways.  When unrolling a loop completely though,
3012              it makes sense to reduce&split givs when possible, as this will
3013              result in simpler instructions, and will not require that a reg
3014              be live across loop iterations.  */
3015
3016           splittable_regs[REGNO (v->dest_reg)] = value;
3017           fprintf (stderr, "Giv %d at insn %d not reduced\n",
3018                    REGNO (v->dest_reg), INSN_UID (v->insn));
3019 #else
3020           continue;
3021 #endif
3022         }
3023
3024       /* Unreduced givs are only updated once by definition.  Reduced givs
3025          are updated as many times as their biv is.  Mark it so if this is
3026          a splittable register.  Don't need to do anything for address givs
3027          where this may not be a register.  */
3028
3029       if (GET_CODE (v->new_reg) == REG)
3030         {
3031           int count = 1;
3032           if (! v->ignore)
3033             count = ivs->reg_biv_class[REGNO (v->src_reg)]->biv_count;
3034
3035           splittable_regs_updates[REGNO (v->new_reg)] = count;
3036         }
3037
3038       result++;
3039
3040       if (loop_dump_stream)
3041         {
3042           int regnum;
3043
3044           if (GET_CODE (v->dest_reg) == CONST_INT)
3045             regnum = -1;
3046           else if (GET_CODE (v->dest_reg) != REG)
3047             regnum = REGNO (XEXP (v->dest_reg, 0));
3048           else
3049             regnum = REGNO (v->dest_reg);
3050           fprintf (loop_dump_stream, "Giv %d at insn %d safe to split.\n",
3051                    regnum, INSN_UID (v->insn));
3052         }
3053     }
3054
3055   return result;
3056 }
3057 \f
3058 /* Try to prove that the register is dead after the loop exits.  Trace every
3059    loop exit looking for an insn that will always be executed, which sets
3060    the register to some value, and appears before the first use of the register
3061    is found.  If successful, then return 1, otherwise return 0.  */
3062
3063 /* ?? Could be made more intelligent in the handling of jumps, so that
3064    it can search past if statements and other similar structures.  */
3065
3066 static int
3067 reg_dead_after_loop (loop, reg)
3068      const struct loop *loop;
3069      rtx reg;
3070 {
3071   rtx insn, label;
3072   enum rtx_code code;
3073   int jump_count = 0;
3074   int label_count = 0;
3075
3076   /* In addition to checking all exits of this loop, we must also check
3077      all exits of inner nested loops that would exit this loop.  We don't
3078      have any way to identify those, so we just give up if there are any
3079      such inner loop exits.  */
3080
3081   for (label = loop->exit_labels; label; label = LABEL_NEXTREF (label))
3082     label_count++;
3083
3084   if (label_count != loop->exit_count)
3085     return 0;
3086
3087   /* HACK: Must also search the loop fall through exit, create a label_ref
3088      here which points to the loop->end, and append the loop_number_exit_labels
3089      list to it.  */
3090   label = gen_rtx_LABEL_REF (VOIDmode, loop->end);
3091   LABEL_NEXTREF (label) = loop->exit_labels;
3092
3093   for (; label; label = LABEL_NEXTREF (label))
3094     {
3095       /* Succeed if find an insn which sets the biv or if reach end of
3096          function.  Fail if find an insn that uses the biv, or if come to
3097          a conditional jump.  */
3098
3099       insn = NEXT_INSN (XEXP (label, 0));
3100       while (insn)
3101         {
3102           code = GET_CODE (insn);
3103           if (GET_RTX_CLASS (code) == 'i')
3104             {
3105               rtx set;
3106
3107               if (reg_referenced_p (reg, PATTERN (insn)))
3108                 return 0;
3109
3110               set = single_set (insn);
3111               if (set && rtx_equal_p (SET_DEST (set), reg))
3112                 break;
3113             }
3114
3115           if (code == JUMP_INSN)
3116             {
3117               if (GET_CODE (PATTERN (insn)) == RETURN)
3118                 break;
3119               else if (!any_uncondjump_p (insn)
3120                        /* Prevent infinite loop following infinite loops.  */
3121                        || jump_count++ > 20)
3122                 return 0;
3123               else
3124                 insn = JUMP_LABEL (insn);
3125             }
3126
3127           insn = NEXT_INSN (insn);
3128         }
3129     }
3130
3131   /* Success, the register is dead on all loop exits.  */
3132   return 1;
3133 }
3134
3135 /* Try to calculate the final value of the biv, the value it will have at
3136    the end of the loop.  If we can do it, return that value.  */
3137
3138 rtx
3139 final_biv_value (loop, bl)
3140      const struct loop *loop;
3141      struct iv_class *bl;
3142 {
3143   rtx loop_end = loop->end;
3144   unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
3145   rtx increment, tem;
3146
3147   /* ??? This only works for MODE_INT biv's.  Reject all others for now.  */
3148
3149   if (GET_MODE_CLASS (bl->biv->mode) != MODE_INT)
3150     return 0;
3151
3152   /* The final value for reversed bivs must be calculated differently than
3153      for ordinary bivs.  In this case, there is already an insn after the
3154      loop which sets this biv's final value (if necessary), and there are
3155      no other loop exits, so we can return any value.  */
3156   if (bl->reversed)
3157     {
3158       if (loop_dump_stream)
3159         fprintf (loop_dump_stream,
3160                  "Final biv value for %d, reversed biv.\n", bl->regno);
3161
3162       return const0_rtx;
3163     }
3164
3165   /* Try to calculate the final value as initial value + (number of iterations
3166      * increment).  For this to work, increment must be invariant, the only
3167      exit from the loop must be the fall through at the bottom (otherwise
3168      it may not have its final value when the loop exits), and the initial
3169      value of the biv must be invariant.  */
3170
3171   if (n_iterations != 0
3172       && ! loop->exit_count
3173       && loop_invariant_p (loop, bl->initial_value))
3174     {
3175       increment = biv_total_increment (bl);
3176
3177       if (increment && loop_invariant_p (loop, increment))
3178         {
3179           /* Can calculate the loop exit value, emit insns after loop
3180              end to calculate this value into a temporary register in
3181              case it is needed later.  */
3182
3183           tem = gen_reg_rtx (bl->biv->mode);
3184           record_base_value (REGNO (tem), bl->biv->add_val, 0);
3185           /* Make sure loop_end is not the last insn.  */
3186           if (NEXT_INSN (loop_end) == 0)
3187             emit_note_after (NOTE_INSN_DELETED, loop_end);
3188           emit_iv_add_mult (increment, GEN_INT (n_iterations),
3189                             bl->initial_value, tem, NEXT_INSN (loop_end));
3190
3191           if (loop_dump_stream)
3192             fprintf (loop_dump_stream,
3193                      "Final biv value for %d, calculated.\n", bl->regno);
3194
3195           return tem;
3196         }
3197     }
3198
3199   /* Check to see if the biv is dead at all loop exits.  */
3200   if (reg_dead_after_loop (loop, bl->biv->src_reg))
3201     {
3202       if (loop_dump_stream)
3203         fprintf (loop_dump_stream,
3204                  "Final biv value for %d, biv dead after loop exit.\n",
3205                  bl->regno);
3206
3207       return const0_rtx;
3208     }
3209
3210   return 0;
3211 }
3212
3213 /* Try to calculate the final value of the giv, the value it will have at
3214    the end of the loop.  If we can do it, return that value.  */
3215
3216 rtx
3217 final_giv_value (loop, v)
3218      const struct loop *loop;
3219      struct induction *v;
3220 {
3221   struct loop_ivs *ivs = LOOP_IVS (loop);
3222   struct iv_class *bl;
3223   rtx insn;
3224   rtx increment, tem;
3225   rtx insert_before, seq;
3226   rtx loop_end = loop->end;
3227   unsigned HOST_WIDE_INT n_iterations = LOOP_INFO (loop)->n_iterations;
3228
3229   bl = ivs->reg_biv_class[REGNO (v->src_reg)];
3230
3231   /* The final value for givs which depend on reversed bivs must be calculated
3232      differently than for ordinary givs.  In this case, there is already an
3233      insn after the loop which sets this giv's final value (if necessary),
3234      and there are no other loop exits, so we can return any value.  */
3235   if (bl->reversed)
3236     {
3237       if (loop_dump_stream)
3238         fprintf (loop_dump_stream,
3239                  "Final giv value for %d, depends on reversed biv\n",
3240                  REGNO (v->dest_reg));
3241       return const0_rtx;
3242     }
3243
3244   /* Try to calculate the final value as a function of the biv it depends
3245      upon.  The only exit from the loop must be the fall through at the bottom
3246      (otherwise it may not have its final value when the loop exits).  */
3247
3248   /* ??? Can calculate the final giv value by subtracting off the
3249      extra biv increments times the giv's mult_val.  The loop must have
3250      only one exit for this to work, but the loop iterations does not need
3251      to be known.  */
3252
3253   if (n_iterations != 0
3254       && ! loop->exit_count)
3255     {
3256       /* ?? It is tempting to use the biv's value here since these insns will
3257          be put after the loop, and hence the biv will have its final value
3258          then.  However, this fails if the biv is subsequently eliminated.
3259          Perhaps determine whether biv's are eliminable before trying to
3260          determine whether giv's are replaceable so that we can use the
3261          biv value here if it is not eliminable.  */
3262
3263       /* We are emitting code after the end of the loop, so we must make
3264          sure that bl->initial_value is still valid then.  It will still
3265          be valid if it is invariant.  */
3266
3267       increment = biv_total_increment (bl);
3268
3269       if (increment && loop_invariant_p (loop, increment)
3270           && loop_invariant_p (loop, bl->initial_value))
3271         {
3272           /* Can calculate the loop exit value of its biv as
3273              (n_iterations * increment) + initial_value */
3274
3275           /* The loop exit value of the giv is then
3276              (final_biv_value - extra increments) * mult_val + add_val.
3277              The extra increments are any increments to the biv which
3278              occur in the loop after the giv's value is calculated.
3279              We must search from the insn that sets the giv to the end
3280              of the loop to calculate this value.  */
3281
3282           insert_before = NEXT_INSN (loop_end);
3283
3284           /* Put the final biv value in tem.  */
3285           tem = gen_reg_rtx (v->mode);
3286           record_base_value (REGNO (tem), bl->biv->add_val, 0);
3287           emit_iv_add_mult (extend_value_for_giv (v, increment),
3288                             GEN_INT (n_iterations),
3289                             extend_value_for_giv (v, bl->initial_value),
3290                             tem, insert_before);
3291
3292           /* Subtract off extra increments as we find them.  */
3293           for (insn = NEXT_INSN (v->insn); insn != loop_end;
3294                insn = NEXT_INSN (insn))
3295             {
3296               struct induction *biv;
3297
3298               for (biv = bl->biv; biv; biv = biv->next_iv)
3299                 if (biv->insn == insn)
3300                   {
3301                     start_sequence ();
3302                     tem = expand_binop (GET_MODE (tem), sub_optab, tem,
3303                                         biv->add_val, NULL_RTX, 0,
3304                                         OPTAB_LIB_WIDEN);
3305                     seq = gen_sequence ();
3306                     end_sequence ();
3307                     emit_insn_before (seq, insert_before);
3308                   }
3309             }
3310
3311           /* Now calculate the giv's final value.  */
3312           emit_iv_add_mult (tem, v->mult_val, v->add_val, tem, insert_before);
3313
3314           if (loop_dump_stream)
3315             fprintf (loop_dump_stream,
3316                      "Final giv value for %d, calc from biv's value.\n",
3317                      REGNO (v->dest_reg));
3318
3319           return tem;
3320         }
3321     }
3322
3323   /* Replaceable giv's should never reach here.  */
3324   if (v->replaceable)
3325     abort ();
3326
3327   /* Check to see if the biv is dead at all loop exits.  */
3328   if (reg_dead_after_loop (loop, v->dest_reg))
3329     {
3330       if (loop_dump_stream)
3331         fprintf (loop_dump_stream,
3332                  "Final giv value for %d, giv dead after loop exit.\n",
3333                  REGNO (v->dest_reg));
3334
3335       return const0_rtx;
3336     }
3337
3338   return 0;
3339 }
3340
3341 /* Look back before LOOP->START for then insn that sets REG and return
3342    the equivalent constant if there is a REG_EQUAL note otherwise just
3343    the SET_SRC of REG.  */
3344
3345 static rtx
3346 loop_find_equiv_value (loop, reg)
3347      const struct loop *loop;
3348      rtx reg;
3349 {
3350   rtx loop_start = loop->start;
3351   rtx insn, set;
3352   rtx ret;
3353
3354   ret = reg;
3355   for (insn = PREV_INSN (loop_start); insn; insn = PREV_INSN (insn))
3356     {
3357       if (GET_CODE (insn) == CODE_LABEL)
3358         break;
3359
3360       else if (INSN_P (insn) && reg_set_p (reg, insn))
3361         {
3362           /* We found the last insn before the loop that sets the register.
3363              If it sets the entire register, and has a REG_EQUAL note,
3364              then use the value of the REG_EQUAL note.  */
3365           if ((set = single_set (insn))
3366               && (SET_DEST (set) == reg))
3367             {
3368               rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3369
3370               /* Only use the REG_EQUAL note if it is a constant.
3371                  Other things, divide in particular, will cause
3372                  problems later if we use them.  */
3373               if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3374                   && CONSTANT_P (XEXP (note, 0)))
3375                 ret = XEXP (note, 0);
3376               else
3377                 ret = SET_SRC (set);
3378
3379               /* We cannot do this if it changes between the
3380                  assignment and loop start though.  */
3381               if (modified_between_p (ret, insn, loop_start))
3382                 ret = reg;
3383             }
3384           break;
3385         }
3386     }
3387   return ret;
3388 }
3389
3390 /* Return a simplified rtx for the expression OP - REG.
3391
3392    REG must appear in OP, and OP must be a register or the sum of a register
3393    and a second term.
3394
3395    Thus, the return value must be const0_rtx or the second term.
3396
3397    The caller is responsible for verifying that REG appears in OP and OP has
3398    the proper form.  */
3399
3400 static rtx
3401 subtract_reg_term (op, reg)
3402      rtx op, reg;
3403 {
3404   if (op == reg)
3405     return const0_rtx;
3406   if (GET_CODE (op) == PLUS)
3407     {
3408       if (XEXP (op, 0) == reg)
3409         return XEXP (op, 1);
3410       else if (XEXP (op, 1) == reg)
3411         return XEXP (op, 0);
3412     }
3413   /* OP does not contain REG as a term.  */
3414   abort ();
3415 }
3416
3417 /* Find and return register term common to both expressions OP0 and
3418    OP1 or NULL_RTX if no such term exists.  Each expression must be a
3419    REG or a PLUS of a REG.  */
3420
3421 static rtx
3422 find_common_reg_term (op0, op1)
3423      rtx op0, op1;
3424 {
3425   if ((GET_CODE (op0) == REG || GET_CODE (op0) == PLUS)
3426       && (GET_CODE (op1) == REG || GET_CODE (op1) == PLUS))
3427     {
3428       rtx op00;
3429       rtx op01;
3430       rtx op10;
3431       rtx op11;
3432
3433       if (GET_CODE (op0) == PLUS)
3434         op01 = XEXP (op0, 1), op00 = XEXP (op0, 0);
3435       else
3436         op01 = const0_rtx, op00 = op0;
3437
3438       if (GET_CODE (op1) == PLUS)
3439         op11 = XEXP (op1, 1), op10 = XEXP (op1, 0);
3440       else
3441         op11 = const0_rtx, op10 = op1;
3442
3443       /* Find and return common register term if present.  */
3444       if (REG_P (op00) && (op00 == op10 || op00 == op11))
3445         return op00;
3446       else if (REG_P (op01) && (op01 == op10 || op01 == op11))
3447         return op01;
3448     }
3449
3450   /* No common register term found.  */
3451   return NULL_RTX;
3452 }
3453
3454 /* Determine the loop iterator and calculate the number of loop
3455    iterations.  Returns the exact number of loop iterations if it can
3456    be calculated, otherwise returns zero.  */
3457
3458 unsigned HOST_WIDE_INT
3459 loop_iterations (loop)
3460      struct loop *loop;
3461 {
3462   struct loop_info *loop_info = LOOP_INFO (loop);
3463   struct loop_ivs *ivs = LOOP_IVS (loop);
3464   rtx comparison, comparison_value;
3465   rtx iteration_var, initial_value, increment, final_value;
3466   enum rtx_code comparison_code;
3467   HOST_WIDE_INT abs_inc;
3468   unsigned HOST_WIDE_INT abs_diff;
3469   int off_by_one;
3470   int increment_dir;
3471   int unsigned_p, compare_dir, final_larger;
3472   rtx last_loop_insn;
3473   rtx reg_term;
3474   struct iv_class *bl;
3475
3476   loop_info->n_iterations = 0;
3477   loop_info->initial_value = 0;
3478   loop_info->initial_equiv_value = 0;
3479   loop_info->comparison_value = 0;
3480   loop_info->final_value = 0;
3481   loop_info->final_equiv_value = 0;
3482   loop_info->increment = 0;
3483   loop_info->iteration_var = 0;
3484   loop_info->unroll_number = 1;
3485   loop_info->iv = 0;
3486
3487   /* We used to use prev_nonnote_insn here, but that fails because it might
3488      accidentally get the branch for a contained loop if the branch for this
3489      loop was deleted.  We can only trust branches immediately before the
3490      loop_end.  */
3491   last_loop_insn = PREV_INSN (loop->end);
3492
3493   /* ??? We should probably try harder to find the jump insn
3494      at the end of the loop.  The following code assumes that
3495      the last loop insn is a jump to the top of the loop.  */
3496   if (GET_CODE (last_loop_insn) != JUMP_INSN)
3497     {
3498       if (loop_dump_stream)
3499         fprintf (loop_dump_stream,
3500                  "Loop iterations: No final conditional branch found.\n");
3501       return 0;
3502     }
3503
3504   /* If there is a more than a single jump to the top of the loop
3505      we cannot (easily) determine the iteration count.  */
3506   if (LABEL_NUSES (JUMP_LABEL (last_loop_insn)) > 1)
3507     {
3508       if (loop_dump_stream)
3509         fprintf (loop_dump_stream,
3510                  "Loop iterations: Loop has multiple back edges.\n");
3511       return 0;
3512     }
3513
3514   /* Find the iteration variable.  If the last insn is a conditional
3515      branch, and the insn before tests a register value, make that the
3516      iteration variable.  */
3517
3518   comparison = get_condition_for_loop (loop, last_loop_insn);
3519   if (comparison == 0)
3520     {
3521       if (loop_dump_stream)
3522         fprintf (loop_dump_stream,
3523                  "Loop iterations: No final comparison found.\n");
3524       return 0;
3525     }
3526
3527   /* ??? Get_condition may switch position of induction variable and
3528      invariant register when it canonicalizes the comparison.  */
3529
3530   comparison_code = GET_CODE (comparison);
3531   iteration_var = XEXP (comparison, 0);
3532   comparison_value = XEXP (comparison, 1);
3533
3534   if (GET_CODE (iteration_var) != REG)
3535     {
3536       if (loop_dump_stream)
3537         fprintf (loop_dump_stream,
3538                  "Loop iterations: Comparison not against register.\n");
3539       return 0;
3540     }
3541
3542   /* The only new registers that are created before loop iterations
3543      are givs made from biv increments or registers created by
3544      load_mems.  In the latter case, it is possible that try_copy_prop
3545      will propagate a new pseudo into the old iteration register but
3546      this will be marked by having the REG_USERVAR_P bit set.  */
3547
3548   if ((unsigned) REGNO (iteration_var) >= ivs->reg_iv_type->num_elements
3549       && ! REG_USERVAR_P (iteration_var))
3550     abort ();
3551
3552   /* Determine the initial value of the iteration variable, and the amount
3553      that it is incremented each loop.  Use the tables constructed by
3554      the strength reduction pass to calculate these values.  */
3555
3556   /* Clear the result values, in case no answer can be found.  */
3557   initial_value = 0;
3558   increment = 0;
3559
3560   /* The iteration variable can be either a giv or a biv.  Check to see
3561      which it is, and compute the variable's initial value, and increment
3562      value if possible.  */
3563
3564   /* If this is a new register, can't handle it since we don't have any
3565      reg_iv_type entry for it.  */
3566   if ((unsigned) REGNO (iteration_var) >= ivs->reg_iv_type->num_elements)
3567     {
3568       if (loop_dump_stream)
3569         fprintf (loop_dump_stream,
3570                  "Loop iterations: No reg_iv_type entry for iteration var.\n");
3571       return 0;
3572     }
3573
3574   /* Reject iteration variables larger than the host wide int size, since they
3575      could result in a number of iterations greater than the range of our
3576      `unsigned HOST_WIDE_INT' variable loop_info->n_iterations.  */
3577   else if ((GET_MODE_BITSIZE (GET_MODE (iteration_var))
3578             > HOST_BITS_PER_WIDE_INT))
3579     {
3580       if (loop_dump_stream)
3581         fprintf (loop_dump_stream,
3582                  "Loop iterations: Iteration var rejected because mode too large.\n");
3583       return 0;
3584     }
3585   else if (GET_MODE_CLASS (GET_MODE (iteration_var)) != MODE_INT)
3586     {
3587       if (loop_dump_stream)
3588         fprintf (loop_dump_stream,
3589                  "Loop iterations: Iteration var not an integer.\n");
3590       return 0;
3591     }
3592   else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == BASIC_INDUCT)
3593     {
3594       /* When reg_iv_type / reg_iv_info is resized for biv increments
3595          that are turned into givs, reg_biv_class is not resized.
3596          So check here that we don't make an out-of-bounds access.  */
3597       if (REGNO (iteration_var) >= max_reg_before_loop)
3598         abort ();
3599
3600       /* Grab initial value, only useful if it is a constant.  */
3601       bl = ivs->reg_biv_class[REGNO (iteration_var)];
3602       initial_value = bl->initial_value;
3603
3604       increment = biv_total_increment (bl);
3605     }
3606   else if (REG_IV_TYPE (ivs, REGNO (iteration_var)) == GENERAL_INDUCT)
3607     {
3608       HOST_WIDE_INT offset = 0;
3609       struct induction *v = REG_IV_INFO (ivs, REGNO (iteration_var));
3610       rtx biv_initial_value;
3611
3612       if (REGNO (v->src_reg) >= max_reg_before_loop)
3613         abort ();
3614
3615       bl = ivs->reg_biv_class[REGNO (v->src_reg)];
3616
3617       /* Increment value is mult_val times the increment value of the biv.  */
3618
3619       increment = biv_total_increment (bl);
3620       if (increment)
3621         {
3622           struct induction *biv_inc;
3623
3624           increment = fold_rtx_mult_add (v->mult_val,
3625                                          extend_value_for_giv (v, increment),
3626                                          const0_rtx, v->mode);
3627           /* The caller assumes that one full increment has occured at the
3628              first loop test.  But that's not true when the biv is incremented
3629              after the giv is set (which is the usual case), e.g.:
3630              i = 6; do {;} while (i++ < 9) .
3631              Therefore, we bias the initial value by subtracting the amount of
3632              the increment that occurs between the giv set and the giv test.  */
3633           for (biv_inc = bl->biv; biv_inc; biv_inc = biv_inc->next_iv)
3634             {
3635               if (loop_insn_first_p (v->insn, biv_inc->insn))
3636                 offset -= INTVAL (biv_inc->add_val);
3637             }
3638           offset *= INTVAL (v->mult_val);
3639         }
3640       if (loop_dump_stream)
3641         fprintf (loop_dump_stream,
3642                  "Loop iterations: Giv iterator, initial value bias %ld.\n",
3643                  (long) offset);
3644
3645       /* Initial value is mult_val times the biv's initial value plus
3646          add_val.  Only useful if it is a constant.  */
3647       biv_initial_value = extend_value_for_giv (v, bl->initial_value);
3648       initial_value
3649         = fold_rtx_mult_add (v->mult_val,
3650                              plus_constant (biv_initial_value, offset),
3651                              v->add_val, v->mode);
3652     }
3653   else
3654     {
3655       if (loop_dump_stream)
3656         fprintf (loop_dump_stream,
3657                  "Loop iterations: Not basic or general induction var.\n");
3658       return 0;
3659     }
3660
3661   if (initial_value == 0)
3662     return 0;
3663
3664   unsigned_p = 0;
3665   off_by_one = 0;
3666   switch (comparison_code)
3667     {
3668     case LEU:
3669       unsigned_p = 1;
3670     case LE:
3671       compare_dir = 1;
3672       off_by_one = 1;
3673       break;
3674     case GEU:
3675       unsigned_p = 1;
3676     case GE:
3677       compare_dir = -1;
3678       off_by_one = -1;
3679       break;
3680     case EQ:
3681       /* Cannot determine loop iterations with this case.  */
3682       compare_dir = 0;
3683       break;
3684     case LTU:
3685       unsigned_p = 1;
3686     case LT:
3687       compare_dir = 1;
3688       break;
3689     case GTU:
3690       unsigned_p = 1;
3691     case GT:
3692       compare_dir = -1;
3693     case NE:
3694       compare_dir = 0;
3695       break;
3696     default:
3697       abort ();
3698     }
3699
3700   /* If the comparison value is an invariant register, then try to find
3701      its value from the insns before the start of the loop.  */
3702
3703   final_value = comparison_value;
3704   if (GET_CODE (comparison_value) == REG
3705       && loop_invariant_p (loop, comparison_value))
3706     {
3707       final_value = loop_find_equiv_value (loop, comparison_value);
3708
3709       /* If we don't get an invariant final value, we are better
3710          off with the original register.  */
3711       if (! loop_invariant_p (loop, final_value))
3712         final_value = comparison_value;
3713     }
3714
3715   /* Calculate the approximate final value of the induction variable
3716      (on the last successful iteration).  The exact final value
3717      depends on the branch operator, and increment sign.  It will be
3718      wrong if the iteration variable is not incremented by one each
3719      time through the loop and (comparison_value + off_by_one -
3720      initial_value) % increment != 0.
3721      ??? Note that the final_value may overflow and thus final_larger
3722      will be bogus.  A potentially infinite loop will be classified
3723      as immediate, e.g. for (i = 0x7ffffff0; i <= 0x7fffffff; i++)  */
3724   if (off_by_one)
3725     final_value = plus_constant (final_value, off_by_one);
3726
3727   /* Save the calculated values describing this loop's bounds, in case
3728      precondition_loop_p will need them later.  These values can not be
3729      recalculated inside precondition_loop_p because strength reduction
3730      optimizations may obscure the loop's structure.
3731
3732      These values are only required by precondition_loop_p and insert_bct
3733      whenever the number of iterations cannot be computed at compile time.
3734      Only the difference between final_value and initial_value is
3735      important.  Note that final_value is only approximate.  */
3736   loop_info->initial_value = initial_value;
3737   loop_info->comparison_value = comparison_value;
3738   loop_info->final_value = plus_constant (comparison_value, off_by_one);
3739   loop_info->increment = increment;
3740   loop_info->iteration_var = iteration_var;
3741   loop_info->comparison_code = comparison_code;
3742   loop_info->iv = bl;
3743
3744   /* Try to determine the iteration count for loops such
3745      as (for i = init; i < init + const; i++).  When running the
3746      loop optimization twice, the first pass often converts simple
3747      loops into this form.  */
3748
3749   if (REG_P (initial_value))
3750     {
3751       rtx reg1;
3752       rtx reg2;
3753       rtx const2;
3754
3755       reg1 = initial_value;
3756       if (GET_CODE (final_value) == PLUS)
3757         reg2 = XEXP (final_value, 0), const2 = XEXP (final_value, 1);
3758       else
3759         reg2 = final_value, const2 = const0_rtx;
3760
3761       /* Check for initial_value = reg1, final_value = reg2 + const2,
3762          where reg1 != reg2.  */
3763       if (REG_P (reg2) && reg2 != reg1)
3764         {
3765           rtx temp;
3766
3767           /* Find what reg1 is equivalent to.  Hopefully it will
3768              either be reg2 or reg2 plus a constant.  */
3769           temp = loop_find_equiv_value (loop, reg1);
3770
3771           if (find_common_reg_term (temp, reg2))
3772             initial_value = temp;
3773           else
3774             {
3775               /* Find what reg2 is equivalent to.  Hopefully it will
3776                  either be reg1 or reg1 plus a constant.  Let's ignore
3777                  the latter case for now since it is not so common.  */
3778               temp = loop_find_equiv_value (loop, reg2);
3779
3780               if (temp == loop_info->iteration_var)
3781                 temp = initial_value;
3782               if (temp == reg1)
3783                 final_value = (const2 == const0_rtx)
3784                   ? reg1 : gen_rtx_PLUS (GET_MODE (reg1), reg1, const2);
3785             }
3786         }
3787       else if (loop->vtop && GET_CODE (reg2) == CONST_INT)
3788         {
3789           rtx temp;
3790
3791           /* When running the loop optimizer twice, check_dbra_loop
3792              further obfuscates reversible loops of the form:
3793              for (i = init; i < init + const; i++).  We often end up with
3794              final_value = 0, initial_value = temp, temp = temp2 - init,
3795              where temp2 = init + const.  If the loop has a vtop we
3796              can replace initial_value with const.  */
3797
3798           temp = loop_find_equiv_value (loop, reg1);
3799
3800           if (GET_CODE (temp) == MINUS && REG_P (XEXP (temp, 0)))
3801             {
3802               rtx temp2 = loop_find_equiv_value (loop, XEXP (temp, 0));
3803
3804               if (GET_CODE (temp2) == PLUS
3805                   && XEXP (temp2, 0) == XEXP (temp, 1))
3806                 initial_value = XEXP (temp2, 1);
3807             }
3808         }
3809     }
3810
3811   /* If have initial_value = reg + const1 and final_value = reg +
3812      const2, then replace initial_value with const1 and final_value
3813      with const2.  This should be safe since we are protected by the
3814      initial comparison before entering the loop if we have a vtop.
3815      For example, a + b < a + c is not equivalent to b < c for all a
3816      when using modulo arithmetic.
3817
3818      ??? Without a vtop we could still perform the optimization if we check
3819      the initial and final values carefully.  */
3820   if (loop->vtop
3821       && (reg_term = find_common_reg_term (initial_value, final_value)))
3822     {
3823       initial_value = subtract_reg_term (initial_value, reg_term);
3824       final_value = subtract_reg_term (final_value, reg_term);
3825     }
3826
3827   loop_info->initial_equiv_value = initial_value;
3828   loop_info->final_equiv_value = final_value;
3829
3830   /* For EQ comparison loops, we don't have a valid final value.
3831      Check this now so that we won't leave an invalid value if we
3832      return early for any other reason.  */
3833   if (comparison_code == EQ)
3834     loop_info->final_equiv_value = loop_info->final_value = 0;
3835
3836   if (increment == 0)
3837     {
3838       if (loop_dump_stream)
3839         fprintf (loop_dump_stream,
3840                  "Loop iterations: Increment value can't be calculated.\n");
3841       return 0;
3842     }
3843
3844   if (GET_CODE (increment) != CONST_INT)
3845     {
3846       /* If we have a REG, check to see if REG holds a constant value.  */
3847       /* ??? Other RTL, such as (neg (reg)) is possible here, but it isn't
3848          clear if it is worthwhile to try to handle such RTL.  */
3849       if (GET_CODE (increment) == REG || GET_CODE (increment) == SUBREG)
3850         increment = loop_find_equiv_value (loop, increment);
3851
3852       if (GET_CODE (increment) != CONST_INT)
3853         {
3854           if (loop_dump_stream)
3855             {
3856               fprintf (loop_dump_stream,
3857                        "Loop iterations: Increment value not constant ");
3858               print_rtl (loop_dump_stream, increment);
3859               fprintf (loop_dump_stream, ".\n");
3860             }
3861           return 0;
3862         }
3863       loop_info->increment = increment;
3864     }
3865
3866   if (GET_CODE (initial_value) != CONST_INT)
3867     {
3868       if (loop_dump_stream)
3869         {
3870           fprintf (loop_dump_stream,
3871                    "Loop iterations: Initial value not constant ");
3872           print_rtl (loop_dump_stream, initial_value);
3873           fprintf (loop_dump_stream, ".\n");
3874         }
3875       return 0;
3876     }
3877   else if (comparison_code == EQ)
3878     {
3879       if (loop_dump_stream)
3880         fprintf (loop_dump_stream, "Loop iterations: EQ comparison loop.\n");
3881       return 0;
3882     }
3883   else if (GET_CODE (final_value) != CONST_INT)
3884     {
3885       if (loop_dump_stream)
3886         {
3887           fprintf (loop_dump_stream,
3888                    "Loop iterations: Final value not constant ");
3889           print_rtl (loop_dump_stream, final_value);
3890           fprintf (loop_dump_stream, ".\n");
3891         }
3892       return 0;
3893     }
3894
3895   /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1.  */
3896   if (unsigned_p)
3897     final_larger
3898       = ((unsigned HOST_WIDE_INT) INTVAL (final_value)
3899          > (unsigned HOST_WIDE_INT) INTVAL (initial_value))
3900         - ((unsigned HOST_WIDE_INT) INTVAL (final_value)
3901            < (unsigned HOST_WIDE_INT) INTVAL (initial_value));
3902   else
3903     final_larger = (INTVAL (final_value) > INTVAL (initial_value))
3904       - (INTVAL (final_value) < INTVAL (initial_value));
3905
3906   if (INTVAL (increment) > 0)
3907     increment_dir = 1;
3908   else if (INTVAL (increment) == 0)
3909     increment_dir = 0;
3910   else
3911     increment_dir = -1;
3912
3913   /* There are 27 different cases: compare_dir = -1, 0, 1;
3914      final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
3915      There are 4 normal cases, 4 reverse cases (where the iteration variable
3916      will overflow before the loop exits), 4 infinite loop cases, and 15
3917      immediate exit (0 or 1 iteration depending on loop type) cases.
3918      Only try to optimize the normal cases.  */
3919
3920   /* (compare_dir/final_larger/increment_dir)
3921      Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
3922      Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
3923      Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
3924      Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
3925
3926   /* ?? If the meaning of reverse loops (where the iteration variable
3927      will overflow before the loop exits) is undefined, then could
3928      eliminate all of these special checks, and just always assume
3929      the loops are normal/immediate/infinite.  Note that this means
3930      the sign of increment_dir does not have to be known.  Also,
3931      since it does not really hurt if immediate exit loops or infinite loops
3932      are optimized, then that case could be ignored also, and hence all
3933      loops can be optimized.
3934
3935      According to ANSI Spec, the reverse loop case result is undefined,
3936      because the action on overflow is undefined.
3937
3938      See also the special test for NE loops below.  */
3939
3940   if (final_larger == increment_dir && final_larger != 0
3941       && (final_larger == compare_dir || compare_dir == 0))
3942     /* Normal case.  */
3943     ;
3944   else
3945     {
3946       if (loop_dump_stream)
3947         fprintf (loop_dump_stream, "Loop iterations: Not normal loop.\n");
3948       return 0;
3949     }
3950
3951   /* Calculate the number of iterations, final_value is only an approximation,
3952      so correct for that.  Note that abs_diff and n_iterations are
3953      unsigned, because they can be as large as 2^n - 1.  */
3954
3955   abs_inc = INTVAL (increment);
3956   if (abs_inc > 0)
3957     abs_diff = INTVAL (final_value) - INTVAL (initial_value);
3958   else if (abs_inc < 0)
3959     {
3960       abs_diff = INTVAL (initial_value) - INTVAL (final_value);
3961       abs_inc = -abs_inc;
3962     }
3963   else
3964     abort ();
3965
3966   /* For NE tests, make sure that the iteration variable won't miss
3967      the final value.  If abs_diff mod abs_incr is not zero, then the
3968      iteration variable will overflow before the loop exits, and we
3969      can not calculate the number of iterations.  */
3970   if (compare_dir == 0 && (abs_diff % abs_inc) != 0)
3971     return 0;
3972
3973   /* Note that the number of iterations could be calculated using
3974      (abs_diff + abs_inc - 1) / abs_inc, provided care was taken to
3975      handle potential overflow of the summation.  */
3976   loop_info->n_iterations = abs_diff / abs_inc + ((abs_diff % abs_inc) != 0);
3977   return loop_info->n_iterations;
3978 }
3979
3980 /* Replace uses of split bivs with their split pseudo register.  This is
3981    for original instructions which remain after loop unrolling without
3982    copying.  */
3983
3984 static rtx
3985 remap_split_bivs (loop, x)
3986      struct loop *loop;
3987      rtx x;
3988 {
3989   struct loop_ivs *ivs = LOOP_IVS (loop);
3990   register enum rtx_code code;
3991   register int i;
3992   register const char *fmt;
3993
3994   if (x == 0)
3995     return x;
3996
3997   code = GET_CODE (x);
3998   switch (code)
3999     {
4000     case SCRATCH:
4001     case PC:
4002     case CC0:
4003     case CONST_INT:
4004     case CONST_DOUBLE:
4005     case CONST:
4006     case SYMBOL_REF:
4007     case LABEL_REF:
4008       return x;
4009
4010     case REG:
4011 #if 0
4012       /* If non-reduced/final-value givs were split, then this would also
4013          have to remap those givs also.  */
4014 #endif
4015       if (REGNO (x) < max_reg_before_loop
4016           && REG_IV_TYPE (ivs, REGNO (x)) == BASIC_INDUCT)
4017         return ivs->reg_biv_class[REGNO (x)]->biv->src_reg;
4018       break;
4019
4020     default:
4021       break;
4022     }
4023
4024   fmt = GET_RTX_FORMAT (code);
4025   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4026     {
4027       if (fmt[i] == 'e')
4028         XEXP (x, i) = remap_split_bivs (loop, XEXP (x, i));
4029       else if (fmt[i] == 'E')
4030         {
4031           register int j;
4032           for (j = 0; j < XVECLEN (x, i); j++)
4033             XVECEXP (x, i, j) = remap_split_bivs (loop, XVECEXP (x, i, j));
4034         }
4035     }
4036   return x;
4037 }
4038
4039 /* If FIRST_UID is a set of REGNO, and FIRST_UID dominates LAST_UID (e.g.
4040    FIST_UID is always executed if LAST_UID is), then return 1.  Otherwise
4041    return 0.  COPY_START is where we can start looking for the insns
4042    FIRST_UID and LAST_UID.  COPY_END is where we stop looking for these
4043    insns.
4044
4045    If there is no JUMP_INSN between LOOP_START and FIRST_UID, then FIRST_UID
4046    must dominate LAST_UID.
4047
4048    If there is a CODE_LABEL between FIRST_UID and LAST_UID, then FIRST_UID
4049    may not dominate LAST_UID.
4050
4051    If there is no CODE_LABEL between FIRST_UID and LAST_UID, then FIRST_UID
4052    must dominate LAST_UID.  */
4053
4054 int
4055 set_dominates_use (regno, first_uid, last_uid, copy_start, copy_end)
4056      int regno;
4057      int first_uid;
4058      int last_uid;
4059      rtx copy_start;
4060      rtx copy_end;
4061 {
4062   int passed_jump = 0;
4063   rtx p = NEXT_INSN (copy_start);
4064
4065   while (INSN_UID (p) != first_uid)
4066     {
4067       if (GET_CODE (p) == JUMP_INSN)
4068         passed_jump = 1;
4069       /* Could not find FIRST_UID.  */
4070       if (p == copy_end)
4071         return 0;
4072       p = NEXT_INSN (p);
4073     }
4074
4075   /* Verify that FIRST_UID is an insn that entirely sets REGNO.  */
4076   if (! INSN_P (p) || ! dead_or_set_regno_p (p, regno))
4077     return 0;
4078
4079   /* FIRST_UID is always executed.  */
4080   if (passed_jump == 0)
4081     return 1;
4082
4083   while (INSN_UID (p) != last_uid)
4084     {
4085       /* If we see a CODE_LABEL between FIRST_UID and LAST_UID, then we
4086          can not be sure that FIRST_UID dominates LAST_UID.  */
4087       if (GET_CODE (p) == CODE_LABEL)
4088         return 0;
4089       /* Could not find LAST_UID, but we reached the end of the loop, so
4090          it must be safe.  */
4091       else if (p == copy_end)
4092         return 1;
4093       p = NEXT_INSN (p);
4094     }
4095
4096   /* FIRST_UID is always executed if LAST_UID is executed.  */
4097   return 1;
4098 }
4099
4100 /* This routine is called when the number of iterations for the unrolled
4101    loop is one.   The goal is to identify a loop that begins with an
4102    unconditional branch to the loop continuation note (or a label just after).
4103    In this case, the unconditional branch that starts the loop needs to be
4104    deleted so that we execute the single iteration.  */
4105
4106 static rtx
4107 ujump_to_loop_cont (loop_start, loop_cont)
4108      rtx loop_start;
4109      rtx loop_cont;
4110 {
4111   rtx x, label, label_ref;
4112
4113   /* See if loop start, or the next insn is an unconditional jump.  */
4114   loop_start = next_nonnote_insn (loop_start);
4115
4116   x = pc_set (loop_start);
4117   if (!x)
4118     return NULL_RTX;
4119
4120   label_ref = SET_SRC (x);
4121   if (!label_ref)
4122     return NULL_RTX;
4123
4124   /* Examine insn after loop continuation note.  Return if not a label.  */
4125   label = next_nonnote_insn (loop_cont);
4126   if (label == 0 || GET_CODE (label) != CODE_LABEL)
4127     return NULL_RTX;
4128
4129   /* Return the loop start if the branch label matches the code label.  */
4130   if (CODE_LABEL_NUMBER (label) == CODE_LABEL_NUMBER (XEXP (label_ref, 0)))
4131     return loop_start;
4132   else
4133     return NULL_RTX;
4134 }