home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / unroll.c < prev    next >
C/C++ Source or Header  |  1991-08-01  |  101KB  |  2,962 lines

  1. /* Try to unroll loops, and split induction variables.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Contributed by James E. Wilson, Cygnus Support/UC Berkeley.
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Try to unroll a loop, and split induction variables.
  22.  
  23.    Loops for which the number of iterations can be calculated exactly are
  24.    handled specially.  If the number of iterations times the insn_count is
  25.    less than MAX_UNROLLED_INSNS, then the loop is unrolled completely.
  26.    Otherwise, we try to unroll the loop a number of times modulo the number
  27.    of iterations, so that only one exit test will be needed.  It is unrolled
  28.    a number of times approximately equal to MAX_UNROLLED_INSNS divided by
  29.    the insn count.
  30.  
  31.    Otherwise, if the number of iterations can be calculated exactly at
  32.    run time, and the loop is always entered at the top, then we try to
  33.    precondition the loop.  That is, at run time, calculate how many times
  34.    the loop will execute, and then execute the loop body a few times so
  35.    that the remaining iterations will be some multiple of 4 (or 2 if the
  36.    loop is large).  Then fall through to a loop unrolled 4 (or 2) times,
  37.    with only one exit test needed at the end of the loop.
  38.  
  39.    Otherwise, if the number of iterations can not be calculated exactly,
  40.    not even at run time, then we still unroll the loop a number of times
  41.    approximately equal to MAX_UNROLLED_INSNS divided by the insn count,
  42.    but there must be an exit test after each copy of the loop body.
  43.  
  44.    For each induction variable, which is dead outside the loop (replaceable)
  45.    or for which we can easily calculate the final value, if we can easily
  46.    calculate its value at each place where it is set as a function of the
  47.    current loop unroll count and the variable's value at loop entry, then
  48.    the induction variable is split into `N' different variables, one for
  49.    each copy of the loop body.  One variable is live across the backward
  50.    branch, and the others are all calculated as a function of this variable.
  51.    This helps eliminate data dependencies, and leads to further opportunities
  52.    for cse.  */
  53.  
  54. /* Possible improvements follow:  */
  55.  
  56. /* ??? Add an extra pass somewhere to determine whether unrolling will
  57.    give any benefit.  E.g. after generating all unrolled insns, compute the
  58.    cost of all insns and compare against cost of insns in rolled loop.
  59.  
  60.    - On traditional architectures, unrolling a non-constant bound loop
  61.      is a win if there is a giv whose only use is in memory addresses, the
  62.      memory addresses can be split, and hence giv incremenets can be
  63.      eliminated.
  64.    - It is also a win if the loop is executed many times, and preconditioning
  65.      can be performed for the loop.
  66.    Add code to check for these and similar cases.  */
  67.  
  68. /* ??? Improve control of which loops get unrolled.  Could use profiling
  69.    info to only unroll the most commonly executed loops.  Perhaps have
  70.    a user specifyable option to control the amount of code expansion,
  71.    or the percent of loops to consider for unrolling.  Etc.  */
  72.  
  73. /* ??? Look at the register copies inside the loop to see if they form a
  74.    simple permutation.  If so, iterate the permutation until it gets back to
  75.    the start state.  This is how many times we should unroll the loop, for
  76.    best results, because then all register copies can be eliminated.
  77.    For example, the lips nreverse function should be unrolled 3 times
  78.    while (this)
  79.      {
  80.        next = this->cdr;
  81.        this->cdr = prev;
  82.        prev = this;
  83.        this = next;
  84.      }
  85.   */
  86.  
  87. /* ??? Add some simple linear equation solving capability so that we can
  88.    determine the number of loop iterations for more complex loops.
  89.    For example, consider this loop from gdb
  90.    #define SWAP_TARGET_AND_HOST(buffer,len)
  91.      {
  92.        char tmp;
  93.        char *p = (char *) buffer;
  94.        char *q = ((char *) buffer) + len - 1;
  95.        int iterations = (len + 1) >> 1;
  96.        int i;
  97.        for (p; p < q; p++, q--;)
  98.          {
  99.            tmp = *q;
  100.            *q = *p;
  101.            *p = tmp;
  102.          }
  103.      }
  104.    Note that:
  105.      start value = p = &buffer + current_iteration
  106.      end value   = q = &buffer + len - 1 - current_iteration
  107.    Given the loop exit test of "p < q", then there must be "q - p" iterations,
  108.    set equal to zero and solve for number of iterations:
  109.      q - p = len - 1 - 2*current_iteration = 0
  110.      current_iteration = (len - 1) / 2
  111.    Hence, there are (len - 1) / 2 (rounded up to the nearest integer)
  112.    iterations of this loop.  */
  113.  
  114. /* ??? Currently, no labels are marked as loop invariant when doing loop
  115.    unrolling.  This is because an insn inside the loop, that loads the address
  116.    of a label inside the loop into a register, could be moved outside the loop
  117.    by the invariant code motion pass if labels were invariant.  If the loop
  118.    is subsequently unrolled, the code will be wrong because each unrolled
  119.    body of the loop will use the same address, whereas each actually needs a
  120.    different address.  A case where this happens is when a loop containing
  121.    a switch statement is unrolled.
  122.  
  123.    It would be better to let labels be considered invariant.  When we
  124.    unroll loops here, check to see if any insns using a label local to the
  125.    loop were moved before the loop.  If so, then correct the problem, by
  126.    moving the insn back into the loop, or perhaps replicate the insn before
  127.    the loop, one copy for each time the loop is unrolled.  */
  128.  
  129. /* The prime factors looked for when trying to unroll a loop by some
  130.    number which is modulo the total number of iterations.  Just checking
  131.    for these 4 prime factors will find at least one factor for 75% of
  132.    all numbers theoretically.  Practically speaking, this will succeed
  133.    almost all of the time since loops are generally a multiple of 2
  134.    and/or 5.  */
  135.  
  136. #define NUM_FACTORS 4
  137.  
  138. struct _factor { int factor, count; } factors[NUM_FACTORS]
  139.   = { {2, 0}, {3, 0}, {5, 0}, {7, 0}};
  140.       
  141. /* Describes the different types of loop unrolling performed.  */
  142.  
  143. enum unroll_types { UNROLL_COMPLETELY, UNROLL_MODULO, UNROLL_NAIVE };
  144.  
  145. #include "config.h"
  146. #include "rtl.h"
  147. #include "insn-config.h"
  148. #include "integrate.h"
  149. #include "regs.h"
  150. #include "flags.h"
  151. #include "expr.h"
  152. #include <stdio.h>
  153. #include "loop.h"
  154.  
  155. /* This controls which loops are unrolled, and by how much we unroll
  156.    them.  */
  157.  
  158. #ifndef MAX_UNROLLED_INSNS
  159. #define MAX_UNROLLED_INSNS 100
  160. #endif
  161.  
  162. /* Indexed by register number, if non-zero, then it contains a pointer
  163.    to a struct induction for a DEST_REG giv which has been combined with
  164.    one of more address givs.  This is needed because whenever such a DEST_REG
  165.    giv is modified, we must modify the value of all split address givs
  166.    that were combined with this DEST_REG giv.  */
  167.  
  168. static struct induction **addr_combined_regs;
  169.  
  170. /* Indexed by register number, if this is a splittable induction variable,
  171.    then this will hold the current value of the register, which depends on the
  172.    iteration number.  */
  173.  
  174. static rtx *splittable_regs;
  175.  
  176. /* Indexed by register number, if this is a splittable induction variable,
  177.    then this will hold the number of instructions in the loop that modify
  178.    the induction variable.  Used to ensure that only the last insn modifying
  179.    a split iv will update the original iv of the dest.  */
  180.  
  181. static int *splittable_regs_updates;
  182.  
  183. /* Values describing the current loop's iteration variable.  These are set up
  184.    by loop_iterations, and used by precondition_loop_p.  */
  185.  
  186. static rtx loop_iteration_var;
  187. static rtx loop_initial_value;
  188. static rtx loop_increment;
  189. static rtx loop_final_value;
  190.  
  191. /* Forward declarations.  */
  192.  
  193. static void init_reg_map ();
  194. static int precondition_loop_p ();
  195. static void copy_loop_body ();
  196. static void iteration_info ();
  197. static rtx approx_final_value ();
  198. static int find_splittable_regs ();
  199. static int find_splittable_givs ();
  200. static rtx fold_rtx_mult_add ();
  201.  
  202. /* Try to unroll one loop and split induction variables in the loop.
  203.  
  204.    The loop is described by the arguments LOOP_END, INSN_COUNT, and
  205.    LOOP_START.  END_INSERT_BEDFORE indicates where insns should be added
  206.    which need to be executed when the loop falls through.  STRENGTH_REDUCTION_P
  207.    indicates whether information generated in the strength reduction pass
  208.    is available.
  209.  
  210.    This function is intended to be called from within `strength_reduce'
  211.    in loop.c.  */
  212.  
  213. void
  214. unroll_loop (loop_end, insn_count, loop_start, end_insert_before,
  215.          strength_reduce_p)
  216.      rtx loop_end;
  217.      int insn_count;
  218.      rtx loop_start;
  219.      rtx end_insert_before;
  220.      int strength_reduce_p;
  221. {
  222.   int i, j, temp;
  223.   int unroll_number = 1;
  224.   rtx copy_start, copy_end;
  225.   rtx insn, copy, sequence, pattern, tem;
  226.   int max_labelno, max_insnno;
  227.   rtx insert_before;
  228.   struct inline_remap *map;
  229.   char *local_label;
  230.   int maxregnum;
  231.   int new_maxregnum;
  232.   rtx exit_label = 0;
  233.   rtx start_label;
  234.   struct iv_class *bl;
  235.   struct induction *v;
  236.   int splitting_not_safe = 0;
  237.   enum unroll_types unroll_type;
  238.   int loop_preconditioned = 0;
  239.   rtx safety_label;
  240.  
  241.   /* Don't bother unrolling huge loops.  Since the minimum factor is
  242.      two, loops greater than one half of MAX_UNROLLED_INSNS will never
  243.      be unrolled.  */
  244.   if (insn_count > MAX_UNROLLED_INSNS / 2)
  245.     {
  246.       if (loop_dump_stream)
  247.     fprintf (loop_dump_stream, "Loop unrolling: Loop too big.\n");
  248.       return;
  249.     }
  250.  
  251.   /* When emitting debugger info, we can't unroll loops with unequal numbers
  252.      of block_beg and block_end notes, because that would unbalance the block
  253.      structure of the function.  This can happen as a result of the
  254.      "if (foo) bar; else break;" optimization in jump.c.  */
  255.  
  256.   if (write_symbols != NO_DEBUG)
  257.     {
  258.       int block_begins = 0;
  259.       int block_ends = 0;
  260.  
  261.       for (insn = loop_start; insn != loop_end; insn = NEXT_INSN (insn))
  262.     {
  263.       if (GET_CODE (insn) == NOTE)
  264.         {
  265.           if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
  266.         block_begins++;
  267.           else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
  268.         block_ends++;
  269.         }
  270.     }
  271.  
  272.       if (block_begins != block_ends)
  273.     {
  274.       if (loop_dump_stream)
  275.         fprintf (loop_dump_stream,
  276.              "Loop unrolling: Unbalanced block notes.\n");
  277.       return;
  278.     }
  279.     }
  280.  
  281.   /* Determine type of unroll to perform.  Depends on the number of iterations
  282.      and the size of the loop.  */
  283.  
  284.   /* If there is no strength reduce info, then set loop_n_iterations to zero.
  285.      This can happen if strength_reduce can't find any bivs in the loop.
  286.      A value of zero indicates that the number of iterations could not be
  287.      calculated.  */
  288.  
  289.   if (! strength_reduce_p)
  290.     loop_n_iterations = 0;
  291.  
  292.   if (loop_dump_stream && loop_n_iterations > 0)
  293.     fprintf (loop_dump_stream,
  294.          "Loop unrolling: %d iterations.\n", loop_n_iterations);
  295.  
  296.   /* Calculate how many times to unroll the loop.  Indicate whether or
  297.      not the loop is being completely unrolled.  */
  298.  
  299.   if (loop_n_iterations == 1)
  300.     {
  301.       /* If number of iterations is exactly 1, then eliminate the compare and
  302.      branch at the end of the loop since they will never be taken.
  303.      Then return, since no other action is needed here.  */
  304.  
  305.       delete_insn (PREV_INSN (loop_end));
  306.       delete_insn (PREV_INSN (loop_end));
  307.       return;
  308.     }
  309.   else if (loop_n_iterations > 0
  310.       && loop_n_iterations * insn_count < MAX_UNROLLED_INSNS)
  311.     {
  312.       unroll_number = loop_n_iterations;
  313.       unroll_type = UNROLL_COMPLETELY;
  314.     }
  315.   else if (loop_n_iterations > 0)
  316.     {
  317.       /* Try to factor the number of iterations.  Don't bother with the
  318.      general case, only using 2, 3, 5, and 7 will get 75% of all
  319.      numbers theoretically, and almost all in practice.  */
  320.  
  321.       for (i = 0; i < NUM_FACTORS; i++)
  322.     factors[i].count = 0;
  323.  
  324.       temp = loop_n_iterations;
  325.       for (i = NUM_FACTORS - 1; i >= 0; i--)
  326.     while (temp % factors[i].factor == 0)
  327.       {
  328.         factors[i].count++;
  329.         temp = temp / factors[i].factor;
  330.       }
  331.  
  332.       /* Start with the larger factors first so that we generally
  333.      get lots of unrolling.  */
  334.  
  335.       unroll_number = 1;
  336.       temp = insn_count;
  337.       for (i = 3; i >= 0; i--)
  338.     while (factors[i].count--)
  339.       {
  340.         if (temp * factors[i].factor < MAX_UNROLLED_INSNS)
  341.           {
  342.         unroll_number *= factors[i].factor;
  343.         temp *= factors[i].factor;
  344.           }
  345.         else
  346.           break;
  347.       }
  348.  
  349.       /* If we couldn't find any factors, then unroll as in the normal
  350.      case.  */
  351.       if (unroll_number == 1)
  352.     {
  353.       if (loop_dump_stream)
  354.         fprintf (loop_dump_stream,
  355.              "Loop unrolling: No factors found.\n");
  356.     }
  357.       else
  358.     unroll_type = UNROLL_MODULO;
  359.     }
  360.  
  361.  
  362.   /* Default case, calculate number of times to unroll loop based on its
  363.      size.  */
  364.   if (unroll_number == 1)
  365.     {
  366.       if (8 * insn_count < MAX_UNROLLED_INSNS)
  367.     unroll_number = 8;
  368.       else if (4 * insn_count < MAX_UNROLLED_INSNS)
  369.     unroll_number = 4;
  370.       else
  371.     unroll_number = 2;
  372.  
  373.       unroll_type = UNROLL_NAIVE;
  374.     }
  375.  
  376.   /* Now we know how many times to unroll the loop.  */
  377.  
  378.   if (loop_dump_stream)
  379.     fprintf (loop_dump_stream,
  380.          "Unrolling loop %d times.\n", unroll_number);
  381.  
  382.  
  383.   if (unroll_type == UNROLL_COMPLETELY || unroll_type == UNROLL_MODULO)
  384.     {
  385.       /* Loops of these types should never start with a jump down to
  386.      the exit condition test.  For now, check for this case just to
  387.      be sure.  UNROLL_NAIVE loops can be of this form, this case is
  388.      handled below.  */
  389.       insn = loop_start;
  390.       while (GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != JUMP_INSN)
  391.     insn = NEXT_INSN (insn);
  392.       if (GET_CODE (insn) == JUMP_INSN)
  393.     abort ();
  394.     }
  395.  
  396.   if (unroll_type == UNROLL_COMPLETELY)
  397.     {
  398.       /* Completely unrolling the loop:  Delete the compare and branch at
  399.      the end (the last two instructions).   This delete must done at the
  400.      very end of loop unrolling, to avoid problems with calls to
  401.      back_branch_in_range_p, which is called by find_splittable_regs.
  402.      All increments of splittable bivs/givs are changed to load constant
  403.      instructions.  */
  404.  
  405.       insert_before = loop_end;
  406.       copy_start = loop_start;
  407.  
  408.       /* First must find the jump insn at the end of the loop.  There may be
  409.      a line number note between the LOOP_END note and the jump insn.  */
  410.       copy_end = PREV_INSN (loop_end);
  411.       while (GET_CODE (copy_end) == NOTE)
  412.     copy_end = PREV_INSN (copy_end);
  413.       /* Set copy_end to the insn before the compare/jump at the end of the
  414.      loop.  */
  415.       copy_end = PREV_INSN (PREV_INSN (copy_end));
  416.     }
  417.   else if (unroll_type == UNROLL_MODULO)
  418.     {
  419.       /* Partially unrolling the loop:  The compare and branch at the end
  420.      (the last two instructions) must remain.  Don't copy the compare
  421.      and branch instructions at the end of the loop.  Insert the unrolled
  422.      code immediately before the compare/branch at the end so that the
  423.      code will fall through to them as before.  */
  424.  
  425.       copy_start = loop_start;
  426.  
  427.       /* First must find the jump insn at the end of the loop.  There may be
  428.      a line number note between the LOOP_END note and the jump insn.  */
  429.       insert_before = PREV_INSN (loop_end);
  430.       while (GET_CODE (insert_before) == NOTE)
  431.     insert_before = PREV_INSN (insert_before);
  432.       /* Set insert_before to the compare insn at the end of the loop.  */
  433.       insert_before = PREV_INSN (insert_before);
  434.       /* Set copy_end to before the compare insn at the end of the loop.  */
  435.       copy_end = PREV_INSN (insert_before);
  436.     }
  437.   else
  438.     {
  439.       /* Normal case: Must copy the compare and branch instructions at the
  440.      end of the loop.  */
  441.       insert_before = loop_end;
  442.       /* Find the first non-note insn searching backwards from the LOOP_END
  443.      note.  */
  444.       copy_end = PREV_INSN (loop_end);
  445.       while (GET_CODE (copy_end) == NOTE)
  446.     copy_end = PREV_INSN (copy_end);
  447.  
  448.       if (GET_CODE (copy_end) == BARRIER)
  449.     {
  450.       /* Loops end with an unconditional jump and a barrier.
  451.          Handle this like above, don't copy jump and barrier.  */
  452.       insert_before = PREV_INSN (PREV_INSN (loop_end));
  453.       copy_end = PREV_INSN (insert_before);
  454.     }
  455.  
  456.       /* If copying exit test branches because they can not be eliminated,
  457.      then must convert the fall through case of the branch to a jump past
  458.      the end of the loop.  Create a label to emit after the loop and save
  459.      it for later use.  Do not use the label after the loop, if any, since
  460.      it might be used by insns outside the loop, or there might be insns
  461.      added before it later by final_[bg]iv_value which must be after
  462.      the real exit label.  */
  463.       exit_label = gen_label_rtx ();
  464.  
  465.       insn = loop_start;
  466.       while (GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != JUMP_INSN)
  467.     insn = NEXT_INSN (insn);
  468.  
  469.       if (GET_CODE (insn) == JUMP_INSN)
  470.     {
  471.       /* The loop starts with a jump down to the exit condition test.
  472.          Start copying the loop after the barrier following this
  473.          jump insn.  */
  474.       copy_start = NEXT_INSN (insn);
  475.  
  476.       /* Splitting induction variables doesn't work when the loop is
  477.          entered via a jump to the bottom, because then we end up doing
  478.          a comparison against a new register for a split variable, but
  479.          we did not execute the set insn for the new register because
  480.          it was skipped over.  */
  481.       splitting_not_safe = 1;
  482.       if (loop_dump_stream)
  483.         fprintf (loop_dump_stream,
  484.              "Splitting not safe, because loop not entered at top.\n");
  485.     }
  486.       else
  487.     copy_start = loop_start;
  488.     }
  489.  
  490.   /* This should always be the first label in the loop.  */
  491.   start_label = NEXT_INSN (copy_start);
  492.   /* There may be a line number note and/or a loop continue note here.  */
  493.   while (GET_CODE (start_label) == NOTE)
  494.     start_label = NEXT_INSN (start_label);
  495.   if (GET_CODE (start_label) != CODE_LABEL)
  496.     {
  497.       /* This can happen as a result of jump threading.  If the first insns in
  498.      the loop test the same condition as the loop's backward jump, or the
  499.      opposite condition, then the backward jump will be modified to point
  500.      to elsewhere, and the loop's start label is deleted.
  501.  
  502.      This case currently can not be handled by the loop unrolling code.  */
  503.  
  504.       if (loop_dump_stream)
  505.     fprintf (loop_dump_stream,
  506.          "Unrolling: unknown insns between BEG note and loop label.\n");
  507.       return;
  508.     }
  509.  
  510.   /* Allocate a translation table for the labels and insn numbers.
  511.      They will be filled in as we copy the insns in the loop.  */
  512.  
  513.   max_labelno = max_label_num ();
  514.   max_insnno = get_max_uid ();
  515.  
  516.   map = (struct inline_remap *) alloca (sizeof (struct inline_remap));
  517.  
  518.   /* Allocate the label map.  */
  519.  
  520.   if (max_labelno > 0)
  521.     {
  522.       map->label_map = (rtx *) alloca (max_labelno * sizeof (rtx));
  523.  
  524.       local_label = (char *) alloca (max_labelno);
  525.       bzero (local_label, max_labelno);
  526.     }
  527.   else
  528.     map->label_map = 0;
  529.  
  530.   /* Search the loop and mark all local labels, i.e. the ones which have to
  531.      be distinct labels when copied.  For all labels which might be
  532.      non-local, set their label_map entries to point to themselves.
  533.      If they happen to be local their label_map entries will be overwritten
  534.      before the loop body is copied.  The label_map entries for local labels
  535.      will be set to a different value each time the loop body is copied.  */
  536.  
  537.   for (insn = copy_start; insn != loop_end; insn = NEXT_INSN (insn))
  538.     {
  539.       if (GET_CODE (insn) == CODE_LABEL)
  540.     local_label[CODE_LABEL_NUMBER (insn)] = 1;
  541.       else if (GET_CODE (insn) == JUMP_INSN)
  542.     {
  543.       if (JUMP_LABEL (insn))
  544.         map->label_map[CODE_LABEL_NUMBER (JUMP_LABEL (insn))]
  545.           = JUMP_LABEL (insn);
  546.       else if (GET_CODE (PATTERN (insn)) == ADDR_VEC
  547.            || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
  548.         {
  549.           rtx pat = PATTERN (insn);
  550.           int diff_vec_p = GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC;
  551.           int len = XVECLEN (pat, diff_vec_p);
  552.           rtx label;
  553.  
  554.           for (i = 0; i < len; i++)
  555.         {
  556.           label = XEXP (XVECEXP (pat, diff_vec_p, i), 0);
  557.           map->label_map[CODE_LABEL_NUMBER (label)] = label;
  558.         }
  559.         }
  560.     }
  561.     }
  562.  
  563.   /* Allocate space for the insn map.  */
  564.  
  565.   map->insn_map = (rtx *) alloca (max_insnno * sizeof (rtx));
  566.  
  567.   /* Set this to zero, to indicate that we are doing loop unrolling,
  568.      not function inlining.  */
  569.   map->inline_target = 0;
  570.  
  571.   /* The register and constant maps depend on the number of registers
  572.      present, so the final maps can't be created until after
  573.      find_splittable_regs is called.  However, they are needed for
  574.      preconditioning, so we create temporary maps when preconditioning
  575.      is performed.  */
  576.  
  577.   /* The preconditioning code may allocate two new pseudo registers.  */
  578.   maxregnum = max_reg_num ();
  579.  
  580.   /* Allocate and zero out the splittable_regs and addr_combined_regs
  581.      arrays.  These must be zeroed here because they will be used if
  582.      loop preconditioning is performed, and must be zero for that case.
  583.  
  584.      It is safe to do this here, since the extra registers created by the
  585.      preconditioning code and find_splittable_regs will never be used
  586.      to accees the splittable_regs[] and addr_combined_regs[] arrays.  */
  587.  
  588.   splittable_regs = (rtx *) alloca (maxregnum * sizeof (rtx));
  589.   bzero (splittable_regs, maxregnum * sizeof (rtx));
  590.   splittable_regs_updates = (int *) alloca (maxregnum * sizeof (int));
  591.   bzero (splittable_regs_updates, maxregnum * sizeof (int));
  592.   addr_combined_regs
  593.     = (struct induction **) alloca (maxregnum * sizeof (struct induction *));
  594.   bzero (addr_combined_regs, maxregnum * sizeof (struct induction *));
  595.  
  596.   /* If this loop requires exit tests when unrolled, check to see if we
  597.      can precondition the loop so as to make the exit tests unnecessary.
  598.      Just like variable splitting, this is not safe if the loop is entered
  599.      via a jump to the bottom.  Also, can not do this if no strength
  600.      reduce info, because precondition_loop_p uses this info.  */
  601.  
  602.   /* Must copy the loop body for preconditioning before the following
  603.      find_splittable_regs call since that will emit insns which need to
  604.      be after the preconditioned loop copies, but immediately before the
  605.      unrolled loop copies.  */
  606.  
  607.   /* Also, it is not safe to split induction variables for the preconditioned
  608.      copies of the loop body.  If we split induction variables, then the code
  609.      assumes that each induction variable can be represented as a function
  610.      of its initial value and the loop iteration number.  This is not true
  611.      in this case, because the last preconditioned copy of the loop body
  612.      could be any iteration from the first up to the `unroll_number-1'th,
  613.      depending on the initial value of the iteration variable.  Therefore
  614.      we can not split induction variables here, because we can not calculate
  615.      their value.  Hence, this code must occur before find_splittable_regs
  616.      is called.  */
  617.  
  618.   if (unroll_type == UNROLL_NAIVE && ! splitting_not_safe && strength_reduce_p)
  619.     {
  620.       rtx initial_value, final_value, increment;
  621.  
  622.       if (precondition_loop_p (&initial_value, &final_value, &increment,
  623.                    loop_start, loop_end))
  624.     {
  625.       register rtx diff, temp;
  626.       enum machine_mode mode;
  627.       rtx *labels;
  628.       int abs_inc, neg_inc;
  629.  
  630.       map->reg_map = (rtx *) alloca (maxregnum * sizeof (rtx));
  631.  
  632.       map->const_equiv_map = (rtx *) alloca (maxregnum * sizeof (rtx));
  633.       map->const_age_map = (unsigned *) alloca (maxregnum
  634.                             * sizeof (unsigned));
  635.       global_const_equiv_map = map->const_equiv_map;
  636.  
  637.       init_reg_map (map, maxregnum);
  638.  
  639.       /* Limit loop unrolling to 4, since this will make 7 copies of
  640.          the loop body.  */
  641.       if (unroll_number > 4)
  642.         unroll_number = 4;
  643.  
  644.       /* Save the absolute value of the increment, and also whether or
  645.          not it is negative.  */
  646.       neg_inc = 0;
  647.       abs_inc = INTVAL (increment);
  648.       if (abs_inc < 0)
  649.         {
  650.           abs_inc = - abs_inc;
  651.           neg_inc = 1;
  652.         }
  653.  
  654.       start_sequence ();
  655.  
  656.       /* Decide what mode to do these calculations in.  Choose the larger
  657.          of final_value's mode and initial_value's mode, or SImode if
  658.          both are constants.  */
  659.       mode = GET_MODE (final_value);
  660.       if (mode == VOIDmode)
  661.         {
  662.           mode = GET_MODE (initial_value);
  663.           if (mode == VOIDmode)
  664.         mode = SImode;
  665.         }
  666.       else if (mode != GET_MODE (initial_value)
  667.            && (GET_MODE_SIZE (mode)
  668.                < GET_MODE_SIZE (GET_MODE (initial_value))))
  669.         mode = GET_MODE (initial_value);
  670.  
  671.       /* Calculate the difference between the final and initial values.
  672.          Final value may be a (plus (reg x) (const_int 1)) rtx.
  673.          Let the following cse pass simplify this if initial value is
  674.          a constant.  */
  675.  
  676.       diff = expand_binop (mode, sub_optab, final_value, initial_value,
  677.                    0, 0, OPTAB_LIB_WIDEN);
  678.  
  679.       /* Now calculate (diff % (unroll * abs (increment))) by using an
  680.          and instruction.  */
  681.       diff = expand_binop (GET_MODE (diff), and_optab, diff,
  682.                    gen_rtx (CONST_INT, VOIDmode,
  683.                     unroll_number * abs_inc - 1),
  684.                    0, 0, OPTAB_LIB_WIDEN);
  685.  
  686.       /* Now emit a sequence of branches to jump to the proper precond
  687.          loop entry point.  */
  688.  
  689.       labels = (rtx *) alloca (sizeof (rtx) * unroll_number);
  690.       for (i = 0; i < unroll_number; i++)
  691.         labels[i] = gen_label_rtx ();
  692.  
  693.       /* Assuming the unroll_number is 4, and the increment is 2, then
  694.          for a negative increment:    for a positive increment:
  695.          diff = 0,1   precond 0    diff = 0,7   precond 0
  696.          diff = 2,3   precond 3     diff = 1,2   precond 1
  697.          diff = 4,5   precond 2     diff = 3,4   precond 2
  698.          diff = 6,7   precond 1     diff = 5,6   precond 3  */
  699.  
  700.       /* We only need to emit (unroll_number - 1) branches here, the
  701.          last case just falls through to the following code.  */
  702.  
  703.       /* ??? This would give better code if we emitted a tree of branches
  704.          instead of the current linear list of branches.  */
  705.  
  706.       for (i = 0; i < unroll_number - 1; i++)
  707.         {
  708.           int cmp_const;
  709.  
  710.           /* For negative increments, must invert the constant compared
  711.          against, except when comparing against zero.  */
  712.           if (i == 0)
  713.         cmp_const = 0;
  714.           else if (neg_inc)
  715.         cmp_const = unroll_number - i;
  716.           else
  717.         cmp_const = i;
  718.  
  719.           emit_cmp_insn (diff, gen_rtx (CONST_INT, VOIDmode,
  720.                         abs_inc * cmp_const),
  721.                  EQ, 0, 0, 0);
  722.  
  723.           if (i == 0)
  724.         emit_jump_insn (gen_beq (labels[i]));
  725.           else if (neg_inc)
  726.         emit_jump_insn (gen_bge (labels[i]));
  727.           else
  728.         emit_jump_insn (gen_ble (labels[i]));
  729.           JUMP_LABEL (get_last_insn ()) = labels[i];
  730.           LABEL_NUSES (labels[i])++;
  731.         }
  732.  
  733.       /* If the increment is greater than one, then we need another branch,
  734.          to handle other cases equivalent to 0.  */
  735.  
  736.       /* ??? This should be merged into the code above somehow to help
  737.          simplify the code here, and reduce the number of branches emitted.
  738.          For the negative increment case, the branch here could easily
  739.          be merged with the `0' case branch above.  For the positive
  740.          increment case, it is not clear how this can be simplified.  */
  741.          
  742.       if (abs_inc != 1)
  743.         {
  744.           int cmp_const;
  745.  
  746.           if (neg_inc)
  747.         cmp_const = abs_inc - 1;
  748.           else
  749.         cmp_const = abs_inc * (unroll_number - 1) + 1;
  750.  
  751.           emit_cmp_insn (diff, gen_rtx (CONST_INT, VOIDmode, cmp_const),
  752.                  EQ, 0, 0, 0);
  753.  
  754.           if (neg_inc)
  755.         emit_jump_insn (gen_ble (labels[0]));
  756.           else
  757.         emit_jump_insn (gen_bge (labels[0]));
  758.           JUMP_LABEL (get_last_insn ()) = labels[0];
  759.           LABEL_NUSES (labels[0])++;
  760.         }
  761.  
  762.       sequence = gen_sequence ();
  763.       end_sequence ();
  764.       emit_insn_before (sequence, loop_start);
  765.       
  766.       /* Only the last copy of the loop body here needs the exit
  767.          test, so set copy_end to exclude the compare/branch here,
  768.          and then reset it inside the loop when get to the last
  769.          copy.  */
  770.  
  771.       copy_end = PREV_INSN (PREV_INSN (PREV_INSN (loop_end)));
  772.  
  773.       for (i = 1; i < unroll_number; i++)
  774.         {
  775.           emit_label_after (labels[unroll_number - i],
  776.                 PREV_INSN (loop_start));
  777.  
  778.           bzero (map->insn_map, max_insnno * sizeof (rtx));
  779.           bzero (map->const_equiv_map, maxregnum * sizeof (rtx));
  780.           bzero (map->const_age_map, maxregnum * sizeof (unsigned));
  781.           map->const_age = 0;
  782.  
  783.           for (j = 0; j < max_labelno; j++)
  784.         if (local_label[j])
  785.           map->label_map[j] = gen_label_rtx ();
  786.  
  787.           /* The last copy needs the compare/branch insns at the end,
  788.          so reset copy_end here if the loop ends with a conditional
  789.          branch.  */
  790.  
  791.           if (i == unroll_number - 1
  792.           && GET_CODE (PREV_INSN (loop_end)) != BARRIER)
  793.         copy_end = PREV_INSN (loop_end);
  794.  
  795.           /* None of the copies are the `last_iteration', so just
  796.          pass zero for that parameter.  */
  797.           copy_loop_body (copy_start, copy_end, map, exit_label, 0,
  798.                   unroll_type, start_label, loop_end, loop_start);
  799.         }
  800.       emit_label_after (labels[0], PREV_INSN (loop_start));
  801.  
  802.       /* The loop exit tests are now unnecessary, so change the setup
  803.          to prevent them from being copied.  */
  804.       insert_before = PREV_INSN (PREV_INSN (loop_end));
  805.       copy_end = PREV_INSN (insert_before);
  806.  
  807.       /* Set unroll type to MODULO now.  */
  808.       unroll_type = UNROLL_MODULO;
  809.       loop_preconditioned = 1;
  810.     }
  811.     }
  812.  
  813.   /* If reach here, and the loop type is UNROLL_NAIVE, then don't unroll
  814.      the loop unless all loops are being unrolled.  */
  815.   if (unroll_type == UNROLL_NAIVE && ! flag_unroll_all_loops)
  816.     {
  817.       if (loop_dump_stream)
  818.     fprintf (loop_dump_stream, "Loop unrolling: Not unrolled.\n");
  819.       return;
  820.     }
  821.  
  822.   /* At this point, we are guaranteed to unroll the loop.  */
  823.  
  824.   /* For each biv and giv, determine whether it can be safely split into
  825.      a different variable for each unrolled copy of the loop body.
  826.      We precalculate and save this info here, since computing it is
  827.      expensive.
  828.  
  829.      Do this before deleting any instructions from the loop, so that
  830.      back_branch_in_range_p will work correctly.  */
  831.  
  832.   if (splitting_not_safe)
  833.     temp = 0;
  834.   else
  835.     temp = find_splittable_regs (unroll_type, loop_start, loop_end,
  836.                 end_insert_before, unroll_number);
  837.  
  838.   /* find_splittable_regs may have created some new registers, so must
  839.      reallocate the reg_map with the new larger size, and must realloc
  840.      the constant maps also.  */
  841.  
  842.   maxregnum = max_reg_num ();
  843.   map->reg_map = (rtx *) alloca (maxregnum * sizeof (rtx));
  844.  
  845.   init_reg_map (map, maxregnum);
  846.  
  847.   /* Space is needed in some of the map for new registers, so new_maxregnum
  848.      is an (over)estimate of how many registers will exist at the end.  */
  849.   new_maxregnum = maxregnum + (temp * unroll_number * 2);
  850.  
  851.   /* Must realloc space for the constant maps, because the number of registers
  852.      may have changed.  */
  853.  
  854.   map->const_equiv_map = (rtx *) alloca (new_maxregnum * sizeof (rtx));
  855.   map->const_age_map = (unsigned *) alloca (new_maxregnum * sizeof (unsigned));
  856.  
  857.   global_const_equiv_map = map->const_equiv_map;
  858.  
  859.   /* Search the list of bivs and givs to find ones which need to be remapped
  860.      when split, and set their reg_map entry appropriately.  */
  861.  
  862.   for (bl = loop_iv_list; bl; bl = bl->next)
  863.     {
  864.       if (REGNO (bl->biv->src_reg) != bl->regno)
  865.     map->reg_map[bl->regno] = bl->biv->src_reg;
  866. #if 0
  867.       /* Currently, non-reduced/final-value givs are never split.  */
  868.       for (v = bl->giv; v; v = v->next_iv)
  869.     if (REGNO (v->src_reg) != bl->regno)
  870.       map->reg_map[REGNO (v->dest_reg)] = v->src_reg;
  871. #endif
  872.     }
  873.  
  874.   /* If the loop is being partially unrolled, and the iteration variables
  875.      are being split, and are being renamed for the split, then must fix up
  876.      the compare instruction at the end of the loop to refer to the new
  877.      registers.  This compare isn't copied, so the registers used in it
  878.      will never be replaced if it isn't done here.  */
  879.  
  880.   if (unroll_type == UNROLL_MODULO)
  881.     {
  882.       insn = NEXT_INSN (copy_end);
  883.       if (GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SET)
  884.     {
  885. #if 0
  886.       /* If non-reduced/final-value givs were split, then this would also
  887.          have to remap those givs.  */
  888. #endif
  889.  
  890.       tem = SET_SRC (PATTERN (insn));
  891.       /* The set source is a register.  */
  892.       if (GET_CODE (tem) == REG)
  893.         {
  894.           if (REGNO (tem) < max_reg_before_loop
  895.           && reg_iv_type[REGNO (tem)] == BASIC_INDUCT)
  896.         SET_SRC (PATTERN (insn))
  897.           = reg_biv_class[REGNO (tem)]->biv->src_reg;
  898.         }
  899.       else
  900.         {
  901.           /* The set source is a compare of some sort.  */
  902.           tem = XEXP (SET_SRC (PATTERN (insn)), 0);
  903.           if (GET_CODE (tem) == REG
  904.           && REGNO (tem) < max_reg_before_loop
  905.           && reg_iv_type[REGNO (tem)] == BASIC_INDUCT)
  906.         XEXP (SET_SRC (PATTERN (insn)), 0)
  907.           = reg_biv_class[REGNO (tem)]->biv->src_reg;
  908.           
  909.           tem = XEXP (SET_SRC (PATTERN (insn)), 1);
  910.           if (GET_CODE (tem) == REG
  911.           && REGNO (tem) < max_reg_before_loop
  912.           && reg_iv_type[REGNO (tem)] == BASIC_INDUCT)
  913.         XEXP (SET_SRC (PATTERN (insn)), 1)
  914.           = reg_biv_class[REGNO (tem)]->biv->src_reg;
  915.         }
  916.     }
  917.     }
  918.  
  919.   /* For unroll_number - 1 times, make a copy of each instruction
  920.      between copy_start and copy_end, and insert these new instructions
  921.      before the end of the loop.  */
  922.  
  923.   for (i = 0; i < unroll_number; i++)
  924.     {
  925.       bzero (map->insn_map, max_insnno * sizeof (rtx));
  926.       bzero (map->const_equiv_map, new_maxregnum * sizeof (rtx));
  927.       bzero (map->const_age_map, new_maxregnum * sizeof (unsigned));
  928.       map->const_age = 0;
  929.  
  930.       for (j = 0; j < max_labelno; j++)
  931.     if (local_label[j])
  932.       map->label_map[j] = gen_label_rtx ();
  933.  
  934.       /* If loop starts with a branch to the test, then fix it so that
  935.      it points to the test of the first unrolled copy of the loop.  */
  936.       if (i == 0 && loop_start != copy_start)
  937.     {
  938.       insn = PREV_INSN (copy_start);
  939.       pattern = PATTERN (insn);
  940.       
  941.       tem = map->label_map[CODE_LABEL_NUMBER
  942.                    (XEXP (SET_SRC (pattern), 0))];
  943.       SET_SRC (pattern) = gen_rtx (LABEL_REF, VOIDmode, tem);
  944.  
  945.       /* Set the jump label so that it can be used by later loop unrolling
  946.          passes.  */
  947.       JUMP_LABEL (insn) = tem;
  948.       LABEL_NUSES (tem)++;
  949.     }
  950.  
  951.       copy_loop_body (copy_start, copy_end, map, exit_label,
  952.               i == unroll_number - 1, unroll_type, start_label,
  953.               loop_end, insert_before);
  954.     }
  955.  
  956.   /* Before deleting any insns, emit a CODE_LABEL immediately after the last
  957.      insn to be deleted.  This prevents any runaway delete_insn call from
  958.      more insns that it should, as it always stops at a CODE_LABEL.  */
  959.  
  960.   if (unroll_type == UNROLL_COMPLETELY)
  961.     safety_label = emit_label_after (gen_label_rtx (),
  962.                     NEXT_INSN (NEXT_INSN (copy_end)));
  963.   else
  964.     safety_label = emit_label_after (gen_label_rtx (), copy_end);
  965.  
  966.   if (unroll_type == UNROLL_COMPLETELY)
  967.     {
  968.       /* Delete the compare and branch at the end of the loop if completely
  969.      unrolling the loop.  Deleting the backward branch at the end also
  970.      deletes the code label at the start of the loop.  This is done at
  971.      the very end to avoid problems with back_branch_in_range_p.  */
  972.  
  973.       delete_insn (NEXT_INSN (copy_end));
  974.       delete_insn (NEXT_INSN (copy_end));
  975.     }
  976.  
  977.   /* Delete all of the original loop instructions.  Don't delete the 
  978.      LOOP_BEG note, or the first code label in the loop.  */
  979.  
  980.   insn = NEXT_INSN (copy_start);
  981.   copy_end = NEXT_INSN (copy_end);
  982.   while (insn != copy_end)
  983.     {
  984.       if (insn != start_label)
  985.     insn = delete_insn (insn);
  986.       else
  987.     insn = NEXT_INSN (insn);
  988.     }
  989.  
  990.   /* Can now delete the 'safety' label emitted to protect us from runaway
  991.      delete_insn calls.  */
  992.   if (INSN_DELETED_P (safety_label))
  993.     abort ();
  994.   delete_insn (safety_label);
  995.  
  996.   /* If exit_label exists, emit it after the loop.  Doing the emit here
  997.      forces it to have a higher INSN_UID than any insn in the unrolled loop.
  998.      This is needed so that mostly_true_jump in reorg.c will treat jumps
  999.      to this loop end label correctly, i.e. predict that they are usually
  1000.      not taken.  */
  1001.   if (exit_label)
  1002.     emit_label_after (exit_label, loop_end);
  1003.  
  1004.   /* If debugging, we must replicate the tree nodes corresponsing to the blocks
  1005.      inside the loop, so that the original one to one mapping will remain.  */
  1006.  
  1007.   if (write_symbols != NO_DEBUG)
  1008.     {
  1009.       int copies = unroll_number;
  1010.  
  1011.       if (loop_preconditioned)
  1012.     copies += unroll_number - 1;
  1013.  
  1014.       unroll_block_trees (uid_loop_num[INSN_UID (loop_start)], copies);
  1015.     }
  1016. }
  1017.  
  1018. /* Return true if the loop can be safely, and profitably, preconditioned
  1019.    so that the unrolled copies of the loop body don't need exit tests.
  1020.  
  1021.    This only works if final_value, initial_value and increment can be
  1022.    determined, and if increment is a constant power of 2.
  1023.    If increment is not a power of 2, then the preconditioning modulo
  1024.    operation would require a real modulo instead of a boolean AND, and this
  1025.    is not considered `profitable'.  */
  1026.  
  1027. /* ??? If the loop is known to be executed very many times, or the machine
  1028.    has a very cheap divide instruction, then preconditioning is a win even
  1029.    when the increment is not a power of 2.  Use RTX_COST to compute
  1030.    whether divide is cheap.  */
  1031.  
  1032. static int
  1033. precondition_loop_p (initial_value, final_value, increment, loop_start,
  1034.              loop_end)
  1035.      rtx *initial_value, *final_value, *increment;
  1036.      rtx loop_start, loop_end;
  1037. {
  1038.   int unsigned_compare, compare_dir;
  1039.  
  1040.   if (loop_n_iterations > 0)
  1041.     {
  1042.       *initial_value = const0_rtx;
  1043.       *increment = const1_rtx;
  1044.       *final_value = gen_rtx (CONST_INT, VOIDmode, loop_n_iterations);
  1045.  
  1046.       if (loop_dump_stream)
  1047.     fprintf (loop_dump_stream,
  1048.          "Preconditioning: Success, number of iterations known, %d.\n",
  1049.          loop_n_iterations);
  1050.       return 1;
  1051.     }
  1052.  
  1053.   if (loop_initial_value == 0)
  1054.     {
  1055.       if (loop_dump_stream)
  1056.     fprintf (loop_dump_stream,
  1057.          "Preconditioning: Could not find initial value.\n");
  1058.       return 0;
  1059.     }
  1060.   else if (loop_increment == 0)
  1061.     {
  1062.       if (loop_dump_stream)
  1063.     fprintf (loop_dump_stream,
  1064.          "Preconditioning: Could not find increment value.\n");
  1065.       return 0;
  1066.     }
  1067.   else if (GET_CODE (loop_increment) != CONST_INT)
  1068.     {
  1069.       if (loop_dump_stream)
  1070.     fprintf (loop_dump_stream,
  1071.          "Preconditioning: Increment not a constant.\n");
  1072.       return 0;
  1073.     }
  1074.   else if ((exact_log2 (INTVAL (loop_increment)) < 0)
  1075.        && (exact_log2 (- INTVAL (loop_increment)) < 0))
  1076.     {
  1077.       if (loop_dump_stream)
  1078.     fprintf (loop_dump_stream,
  1079.          "Preconditioning: Increment not a constant power of 2.\n");
  1080.       return 0;
  1081.     }
  1082.  
  1083.   /* Unsigned_compare and compare_dir can be ignored here, since they do
  1084.      not matter for preconditioning.  */
  1085.  
  1086.   if (loop_final_value == 0)
  1087.     {
  1088.       if (loop_dump_stream)
  1089.     fprintf (loop_dump_stream,
  1090.          "Preconditioning: EQ comparison loop.\n");
  1091.       return 0;
  1092.     }
  1093.  
  1094.   /* Must ensure that final_value is invariant, so call invariant_p to
  1095.      check.  Before doing so, must check regno against max_reg_before_loop
  1096.      to make sure that the register is in the range convered by invariant_p.
  1097.      If it isn't, then it is most likely a biv/giv which by definition are
  1098.      not invariant.  */
  1099.   if ((GET_CODE (loop_final_value) == REG
  1100.        && REGNO (loop_final_value) >= max_reg_before_loop)
  1101.       || (GET_CODE (loop_final_value) == PLUS
  1102.       && REGNO (XEXP (loop_final_value, 0)) >= max_reg_before_loop)
  1103.       || ! invariant_p (loop_final_value))
  1104.     {
  1105.       if (loop_dump_stream)
  1106.     fprintf (loop_dump_stream,
  1107.          "Preconditioning: Final value not invariant.\n");
  1108.       return 0;
  1109.     }
  1110.  
  1111.   /* Fail for floating point values, since the caller of this function
  1112.      does not have code to deal with them.  */
  1113.   if (GET_MODE_CLASS (GET_MODE (loop_final_value)) == MODE_FLOAT
  1114.       || GET_MODE_CLASS (GET_MODE (loop_initial_value) == MODE_FLOAT))
  1115.     {
  1116.       if (loop_dump_stream)
  1117.     fprintf (loop_dump_stream,
  1118.          "Preconditioning: Floating point final or initial value.\n");
  1119.       return 0;
  1120.     }
  1121.  
  1122.   /* Now set initial_value to be the iteration_var, since that may be a
  1123.      simpler expression, and is guaranteed to be correct if all of the
  1124.      above tests succeed.
  1125.  
  1126.      We can not use the initial_value as calculated, because it will be
  1127.      one too small for loops of the form "while (i-- > 0)".  We can not
  1128.      emit code before the loop_skip_over insns to fix this problem as this
  1129.      will then give a number one too large for loops of the form
  1130.      "while (--i > 0)".
  1131.  
  1132.      Note that all loops that reach here are entered at the top, because
  1133.      this function is not called if the loop starts with a jump.  */
  1134.  
  1135.   *initial_value = loop_iteration_var;
  1136.   *increment = loop_increment;
  1137.   *final_value = loop_final_value;
  1138.  
  1139.   /* Success! */
  1140.   if (loop_dump_stream)
  1141.     fprintf (loop_dump_stream, "Preconditioning: Successful.\n");
  1142.   return 1;
  1143. }
  1144.  
  1145.  
  1146. /* All pseudo-registers must be mapped to themselves.  Two hard registers
  1147.    must be mapped, VIRTUAL_STACK_VARS_REGNUM and VIRTUAL_INCOMING_ARGS_
  1148.    REGNUM, to avoid function-inlining specific conversions of these
  1149.    registers.  All other hard regs can not be mapped because they may be
  1150.    used with different
  1151.    modes.  */
  1152.  
  1153. static void
  1154. init_reg_map (map, maxregnum)
  1155.      struct inline_remap *map;
  1156.      int maxregnum;
  1157. {
  1158.   int i;
  1159.  
  1160.   for (i = maxregnum - 1; i > LAST_VIRTUAL_REGISTER; i--)
  1161.     map->reg_map[i] = regno_reg_rtx[i];
  1162.   /* Just clear the rest of the entries.  */
  1163.   for (i = LAST_VIRTUAL_REGISTER; i >= 0; i--)
  1164.     map->reg_map[i] = 0;
  1165.  
  1166.   map->reg_map[VIRTUAL_STACK_VARS_REGNUM]
  1167.     = regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM];
  1168.   map->reg_map[VIRTUAL_INCOMING_ARGS_REGNUM]
  1169.     = regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM];
  1170. }
  1171.  
  1172. /* Strength-reduction will often emit code for optimized biv/givs which
  1173.    calculates their value in a temporary register, and then copies the result
  1174.    to the iv.  This procedure reconstructs the pattern computing the iv;
  1175.    verifying that all operands are of the proper form.
  1176.  
  1177.    The return value is the amount that the giv is incremented by.  */
  1178.  
  1179. static rtx
  1180. calculate_giv_inc (pattern, src_insn, regno)
  1181.      rtx pattern, src_insn;
  1182.      int regno;
  1183. {
  1184.   rtx increment;
  1185.  
  1186.   /* Verify that we have an increment insn here.  First check for a plus
  1187.      as the set source.  */
  1188.   if (GET_CODE (SET_SRC (pattern)) != PLUS)
  1189.     {
  1190.       /* SR sometimes computes the new giv value in a temp, then copies it
  1191.      to the new_reg.  */
  1192.       src_insn = PREV_INSN (src_insn);
  1193.       pattern = PATTERN (src_insn);
  1194.       if (GET_CODE (SET_SRC (pattern)) != PLUS)
  1195.     abort ();
  1196.           
  1197.       /* The last insn emitted is not needed, so delete it to avoid confusing
  1198.      the second cse pass.  This insn sets the giv unnecessarily.  */
  1199.       delete_insn (get_last_insn ());
  1200.     }
  1201.  
  1202.   /* Verify that we have a constant as the second operand of the plus.  */
  1203.   increment = XEXP (SET_SRC (pattern), 1);
  1204.   if (GET_CODE (increment) != CONST_INT)
  1205.     {
  1206.       /* SR sometimes puts the constant in a register, especially if it is
  1207.      too big to be an add immed operand.  */
  1208.       increment = SET_SRC (PATTERN (PREV_INSN (src_insn)));
  1209.  
  1210.       /* SR may have used LO_SUM to compute the constant if it is too large
  1211.      for a load immed operand.  In this case, the constant is in operand
  1212.      one of the LO_SUM rtx.  */
  1213.       if (GET_CODE (increment) == LO_SUM)
  1214.     increment = XEXP (increment, 1);
  1215.  
  1216.       if (GET_CODE (increment) != CONST_INT)
  1217.     abort ();
  1218.           
  1219.       /* The insn loading the constant into a register is not longer needed,
  1220.      so delete it.  */
  1221.       delete_insn (get_last_insn ());
  1222.     }
  1223.  
  1224.   /* Check that the source register is the same as the dest register.  */
  1225.   if (GET_CODE (XEXP (SET_SRC (pattern), 0)) != REG
  1226.       || REGNO (XEXP (SET_SRC (pattern), 0)) != regno)
  1227.     abort ();
  1228.  
  1229.   return increment;
  1230. }
  1231.  
  1232.  
  1233. /* Copy each instruction in the loop, substituting from map as appropriate.
  1234.    This is very similar to a loop in expand_inline_function.  */
  1235.   
  1236. static void
  1237. copy_loop_body (copy_start, copy_end, map, exit_label, last_iteration,
  1238.         unroll_type, start_label, loop_end, insert_before)
  1239.      rtx copy_start, copy_end;
  1240.      struct inline_remap *map;
  1241.      int last_iteration;
  1242.      enum unroll_types unroll_type;
  1243.      rtx start_label, loop_end, insert_before;
  1244. {
  1245.   rtx insn, pattern;
  1246.   rtx tem, copy;
  1247.   int dest_reg_was_split, i;
  1248.   rtx cc0_insn = 0;
  1249.   rtx final_label = 0;
  1250.   rtx giv_inc, giv_dest_reg, giv_src_reg;
  1251.  
  1252.   /* If this isn't the last iteration, then map any references to the
  1253.      start_label to final_label.  Final label will then be emitted immediately
  1254.      after the end of this loop body if it was ever used.
  1255.  
  1256.      If this is the last iteration, then map references to the start_label
  1257.      to itself.  */
  1258.   if (! last_iteration)
  1259.     {
  1260.       final_label = gen_label_rtx ();
  1261.       map->label_map[CODE_LABEL_NUMBER (start_label)] = final_label;
  1262.     }
  1263.   else
  1264.     map->label_map[CODE_LABEL_NUMBER (start_label)] = start_label;
  1265.  
  1266.   start_sequence ();
  1267.   
  1268.   insn = copy_start;
  1269.   do
  1270.     {
  1271.       insn = NEXT_INSN (insn);
  1272.       
  1273.       map->orig_asm_operands_vector = 0;
  1274.       
  1275.       switch (GET_CODE (insn))
  1276.     {
  1277.     case INSN:
  1278.       pattern = PATTERN (insn);
  1279.       copy = 0;
  1280.       giv_inc = 0;
  1281.       
  1282.       /* Check to see if this is a giv that has been combined with
  1283.          some split address givs.  (Combined in the sense that 
  1284.          `combine_givs' in loop.c has put two givs in the same register.)
  1285.          In this case, we must search all givs based on the same biv to
  1286.          find the address givs.  Then split the address givs.
  1287.          Do this before splitting the giv, since that may map the
  1288.          SET_DEST to a new register.  */
  1289.       
  1290.       if (GET_CODE (pattern) == SET
  1291.           && GET_CODE (SET_DEST (pattern)) == REG
  1292.           && addr_combined_regs[REGNO (SET_DEST (pattern))])
  1293.         {
  1294.           struct iv_class *bl;
  1295.           struct induction *v, *tv;
  1296.           int regno = REGNO (SET_DEST (pattern));
  1297.           
  1298.           v = addr_combined_regs[REGNO (SET_DEST (pattern))];
  1299.           bl = reg_biv_class[REGNO (v->src_reg)];
  1300.           
  1301.           /* Although the giv_inc amount is not needed here, we must call
  1302.          calculate_giv_inc here since it might try to delete the
  1303.          last insn emitted.  If we wait until later to call it,
  1304.          we might accidentally delete insns generated immediately
  1305.          below by emit_unrolled_add.  */
  1306.  
  1307.           giv_inc = calculate_giv_inc (pattern, insn, regno);
  1308.  
  1309.           /* This assumes that two DEST_ADDR givs won't be combined
  1310.          unless they are exactly equal.  If they are equal, then
  1311.          we don't want to `split' the one that appears to have been
  1312.          combined with the other since that gives incorrect code.  */
  1313.           for (tv = bl->giv; tv; tv = tv->next_iv)
  1314.         if (tv->giv_type == DEST_ADDR && tv->same == v)
  1315.           {
  1316.             tv->dest_reg = plus_constant (tv->dest_reg,
  1317.                           INTVAL (giv_inc));
  1318.             *tv->location = tv->dest_reg;
  1319.             
  1320.             if (last_iteration && unroll_type != UNROLL_COMPLETELY)
  1321.               {
  1322.             /* Must emit an insn to increment the split address
  1323.                giv.  Add in the const_adjust field in case there
  1324.                was a constant eliminated from the address.  */
  1325.             rtx value, dest_reg;
  1326.             
  1327.             /* tv->dest_reg will be either a bare register,
  1328.                or else a register plus a constant.  */
  1329.             if (GET_CODE (tv->dest_reg) == REG)
  1330.               dest_reg = tv->dest_reg;
  1331.             else
  1332.               dest_reg = XEXP (tv->dest_reg, 0);
  1333.             
  1334.             /* tv->dest_reg may actually be a (PLUS (REG) (CONST))
  1335.                here, so we must call plus_constant to add
  1336.                the const_adjust amount before calling
  1337.                emit_unrolled_add below.  */
  1338.             value = plus_constant (tv->dest_reg, tv->const_adjust);
  1339.  
  1340.             /* The constant could be too large for an add
  1341.                immediate, so can't directly emit an insn here.  */
  1342.             emit_unrolled_add (dest_reg, XEXP (value, 0),
  1343.                        XEXP (value, 1));
  1344.             
  1345.             /* Reset the giv to be just the register again, in case
  1346.                it is used after the set we have just emitted.  */
  1347.             tv->dest_reg = dest_reg;
  1348.             *tv->location = tv->dest_reg;
  1349.               }
  1350.           }
  1351.         }
  1352.       
  1353.       /* If this is a setting of a splittable variable, then determine
  1354.          how to split the variable, create a new set based on this split,
  1355.          and set up the reg_map so that later uses of the variable will
  1356.          use the new split variable.  */
  1357.       
  1358.       dest_reg_was_split = 0;
  1359.       
  1360.       if (GET_CODE (pattern) == SET
  1361.           && GET_CODE (SET_DEST (pattern)) == REG
  1362.           && splittable_regs[REGNO (SET_DEST (pattern))])
  1363.         {
  1364.           int regno = REGNO (SET_DEST (pattern));
  1365.           
  1366.           dest_reg_was_split = 1;
  1367.           
  1368.           /* Compute the increment value for the giv, if it wasn't
  1369.          already computed above.  */
  1370.  
  1371.           if (giv_inc == 0)
  1372.         giv_inc = calculate_giv_inc (pattern, insn, regno);
  1373.           giv_dest_reg = SET_DEST (pattern);
  1374.           giv_src_reg = SET_DEST (pattern);
  1375.  
  1376.           if (unroll_type == UNROLL_COMPLETELY)
  1377.         {
  1378.           /* Completely unrolling the loop.  Set the induction
  1379.              variable to a known constant value.  */
  1380.           
  1381.           /* The value in splittable_regs may be an invariant
  1382.              value, so we must use plus_constant here.  */
  1383.           splittable_regs[regno]
  1384.             = plus_constant (splittable_regs[regno], INTVAL (giv_inc));
  1385.  
  1386.           if (GET_CODE (splittable_regs[regno]) == PLUS)
  1387.             {
  1388.               giv_src_reg = XEXP (splittable_regs[regno], 0);
  1389.               giv_inc = XEXP (splittable_regs[regno], 1);
  1390.             }
  1391.           else
  1392.             {
  1393.               /* The splittable_regs value must be a REG or a
  1394.              CONST_INT, so put the entire value in the giv_src_reg
  1395.              variable.  */
  1396.               giv_src_reg = splittable_regs[regno];
  1397.               giv_inc = const0_rtx;
  1398.             }
  1399.         }
  1400.           else
  1401.         {
  1402.           /* Partially unrolling loop.  Create a new pseudo
  1403.              register for the iteration variable, and set it to
  1404.              be a constant plus the original register.  Except
  1405.              on the last iteration, when the result has to
  1406.              go back into the original iteration var register.  */
  1407.           
  1408.           /* Handle bivs which must be mapped to a new register
  1409.              when split.  This happens for bivs which need their
  1410.              final value set before loop entry.  The new register
  1411.              for the biv was stored in the biv's first struct
  1412.              induction entry by find_splittable_regs.  */
  1413.  
  1414.           if (regno < max_reg_before_loop
  1415.               && reg_iv_type[regno] == BASIC_INDUCT)
  1416.             {
  1417.               giv_src_reg = reg_biv_class[regno]->biv->src_reg;
  1418.               giv_dest_reg = giv_src_reg;
  1419.             }
  1420.           
  1421. #if 0
  1422.           /* If non-reduced/final-value givs were split, then
  1423.              this would have to remap those givs also.  See
  1424.              find_splittable_regs.  */
  1425. #endif
  1426.           
  1427.           splittable_regs[regno]
  1428.             = gen_rtx (CONST_INT, VOIDmode,
  1429.                    INTVAL (giv_inc)
  1430.                    + INTVAL (splittable_regs[regno]));
  1431.           giv_inc = splittable_regs[regno];
  1432.           
  1433.           /* Now split the induction variable by changing the dest
  1434.              of this insn to a new register, and setting its
  1435.              reg_map entry to point to this new register.
  1436.  
  1437.              If this is the last iteration, and this is the last insn
  1438.              that will update the iv, then reuse the original dest,
  1439.              to ensure that the iv will have the proper value when
  1440.              the loop exits or repeats.
  1441.  
  1442.              Using splittable_regs_updates here like this is safe,
  1443.              because it can only be greater than one if all
  1444.              instructions modifying the iv are always executed in
  1445.              order.  */
  1446.  
  1447.           if (! last_iteration
  1448.               || (splittable_regs_updates[regno]-- != 1))
  1449.             {
  1450.               tem = gen_reg_rtx (GET_MODE (giv_src_reg));
  1451.               giv_dest_reg = tem;
  1452.               map->reg_map[regno] = tem;
  1453.             }
  1454.           else
  1455.             map->reg_map[regno] = giv_src_reg;
  1456.         }
  1457.  
  1458.           /* The constant being added could be too large for an add
  1459.          immediate, so can't directly emit an insn here.  */
  1460.           emit_unrolled_add (giv_dest_reg, giv_src_reg, giv_inc);
  1461.           copy = get_last_insn ();
  1462.           pattern = PATTERN (copy);
  1463.         }
  1464.       else
  1465.         {
  1466.           pattern = copy_rtx_and_substitute (pattern, map);
  1467.           copy = emit_insn (pattern);
  1468.         }
  1469.       /* REG_NOTES will be copied later.  */
  1470.       
  1471. #ifdef HAVE_cc0
  1472.       /* If this insn is setting CC0, it may need to look at
  1473.          the insn that uses CC0 to see what type of insn it is.
  1474.          In that case, the call to recog via validate_change will
  1475.          fail.  So don't substitute constants here.  Instead,
  1476.          do it when we emit the following insn.
  1477.  
  1478.          For example, see the pyr.md file.  That machine has signed and
  1479.          unsigned compares.  The compare patterns must check the
  1480.          following branch insn to see which what kind of compare to
  1481.          emit.
  1482.  
  1483.          If the previous insn set CC0, substitute constants on it as
  1484.          well.  */
  1485.       if (sets_cc0_p (copy) != 0)
  1486.         cc0_insn = copy;
  1487.       else
  1488.         {
  1489.           if (cc0_insn)
  1490.         try_constants (cc0_insn, map);
  1491.           cc0_insn = 0;
  1492.           try_constants (copy, map);
  1493.         }
  1494. #else
  1495.       try_constants (copy, map);
  1496. #endif
  1497.  
  1498.       /* Make split induction variable constants `permanent' since we
  1499.          know there are no backward branches across iteration variable
  1500.          settings which would invalidate this.  */
  1501.       if (dest_reg_was_split)
  1502.         {
  1503.           int regno = REGNO (SET_DEST (pattern));
  1504.  
  1505.           if (map->const_age_map[regno] == map->const_age)
  1506.         map->const_age_map[regno] = -1;
  1507.         }
  1508.       break;
  1509.       
  1510.     case JUMP_INSN:
  1511.       if (JUMP_LABEL (insn) == start_label && insn == copy_end
  1512.           && ! last_iteration)
  1513.         {
  1514.           /* This is a branch to the beginning of the loop; this is the
  1515.          last insn being copied; and this is not the last iteration.
  1516.          In this case, we want to change the original fall through
  1517.          case to be a branch past the end of the loop, and the
  1518.          original jump label case to fall_through.  */
  1519.  
  1520.           int fall_through;
  1521.  
  1522.           /* Never map the label in this case.  */
  1523.           pattern = copy_rtx (PATTERN (insn));
  1524.           
  1525.           /* Assume a conditional branch, since the code above
  1526.          does not let unconditional branches be copied.  */
  1527.           if (! condjump_p (insn))
  1528.         abort ();
  1529.           fall_through
  1530.         = (XEXP (SET_SRC (PATTERN (insn)), 2) == pc_rtx) + 1;
  1531.  
  1532.           /* Set the fall through case to the exit label.  Must
  1533.          create a new label_ref since they can't be shared.  */
  1534.           XEXP (SET_SRC (pattern), fall_through)
  1535.         = gen_rtx (LABEL_REF, VOIDmode, exit_label);
  1536.               
  1537.           /* Set the original branch case to fall through.  */
  1538.           XEXP (SET_SRC (pattern), 3 - fall_through)
  1539.         = pc_rtx;
  1540.         }
  1541.       else
  1542.         pattern = copy_rtx_and_substitute (PATTERN (insn), map);
  1543.       
  1544.       copy = emit_jump_insn (pattern);
  1545.       
  1546. #ifdef HAVE_cc0
  1547.       if (cc0_insn)
  1548.         try_constants (cc0_insn, map);
  1549.       cc0_insn = 0;
  1550. #endif
  1551.       try_constants (copy, map);
  1552.  
  1553.       /* Set the jump label of COPY correctly to avoid problems with
  1554.          later passes of unroll_loop, if INSN had jump label set.  */
  1555.       if (JUMP_LABEL (insn))
  1556.         {
  1557.           /* Can't use the label_map for every insn, since this may be
  1558.          the backward branch, and hence the label was not mapped.  */
  1559.           if (GET_CODE (pattern) == SET)
  1560.         {
  1561.           tem = SET_SRC (pattern);
  1562.           if (GET_CODE (tem) == LABEL_REF)
  1563.             JUMP_LABEL (copy) = XEXP (tem, 0);
  1564.           else if (GET_CODE (tem) == IF_THEN_ELSE)
  1565.             {
  1566.               if (XEXP (tem, 1) != pc_rtx)
  1567.             JUMP_LABEL (copy) = XEXP (XEXP (tem, 1), 0);
  1568.               else
  1569.             JUMP_LABEL (copy) = XEXP (XEXP (tem, 2), 0);
  1570.             }
  1571.           else
  1572.             abort ();
  1573.         }
  1574.           else
  1575.         {
  1576.           /* An unrecognizable jump insn, probably the entry jump
  1577.              for a switch statement.  This label must have been mapped,
  1578.              so just use the label_map to get the new jump label.  */
  1579.           JUMP_LABEL (copy) = map->label_map[CODE_LABEL_NUMBER
  1580.                              (JUMP_LABEL (insn))];
  1581.         }
  1582.       
  1583.           /* If this is a non-local jump, then must increase the label
  1584.          use count so that the label will not be deleted when the
  1585.          original jump is deleted.  */
  1586.           LABEL_NUSES (JUMP_LABEL (copy))++;
  1587.         }
  1588.       else if (GET_CODE (PATTERN (copy)) == ADDR_VEC
  1589.            || GET_CODE (PATTERN (copy)) == ADDR_DIFF_VEC)
  1590.         {
  1591.           rtx pat = PATTERN (copy);
  1592.           int diff_vec_p = GET_CODE (pat) == ADDR_DIFF_VEC;
  1593.           int len = XVECLEN (pat, diff_vec_p);
  1594.           int i;
  1595.  
  1596.           for (i = 0; i < len; i++)
  1597.         LABEL_NUSES (XEXP (XVECEXP (pat, diff_vec_p, i), 0))++;
  1598.         }
  1599.  
  1600.       /* If this used to be a conditional jump insn but whose branch
  1601.          direction is now known, we must do something special.  */
  1602.       if (condjump_p (insn) && !simplejump_p (insn) && map->last_pc_value)
  1603.         {
  1604. #ifdef HAVE_cc0
  1605.           /* The previous insn set cc0 for us.  So delete it.  */
  1606.           delete_insn (PREV_INSN (copy));
  1607. #endif
  1608.  
  1609.           /* If this is now a no-op, delete it.  */
  1610.           if (map->last_pc_value == pc_rtx)
  1611.         {
  1612.           delete_insn (copy);
  1613.           copy = 0;
  1614.         }
  1615.           else
  1616.         /* Otherwise, this is unconditional jump so we must put a
  1617.            BARRIER after it.  We could do some dead code elimination
  1618.            here, but jump.c will do it just as well.  */
  1619.         emit_barrier ();
  1620.         }
  1621.       break;
  1622.       
  1623.     case CALL_INSN:
  1624.       pattern = copy_rtx_and_substitute (PATTERN (insn), map);
  1625.       copy = emit_call_insn (pattern);
  1626.  
  1627. #ifdef HAVE_cc0
  1628.       if (cc0_insn)
  1629.         try_constants (cc0_insn, map);
  1630.       cc0_insn = 0;
  1631. #endif
  1632.       try_constants (copy, map);
  1633.  
  1634.       /* Be lazy and assume CALL_INSNs clobber all hard registers.  */
  1635.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1636.         map->const_equiv_map[i] = 0;
  1637.       break;
  1638.       
  1639.     case CODE_LABEL:
  1640.       /* If this is the loop start label, then we don't need to emit a
  1641.          copy of this label since no one will use it.  */
  1642.  
  1643.       if (insn != start_label)
  1644.         {
  1645.           copy = emit_label (map->label_map[CODE_LABEL_NUMBER (insn)]);
  1646.           map->const_age++;
  1647.         }
  1648.       break;
  1649.       
  1650.     case BARRIER:
  1651.       copy = emit_barrier ();
  1652.       break;
  1653.       
  1654.     case NOTE:
  1655.       if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED)
  1656.         copy = emit_note (NOTE_SOURCE_FILE (insn),
  1657.                   NOTE_LINE_NUMBER (insn));
  1658.       else
  1659.         copy = 0;
  1660.       break;
  1661.       
  1662.     default:
  1663.       abort ();
  1664.       break;
  1665.     }
  1666.       
  1667.       map->insn_map[INSN_UID (insn)] = copy;
  1668.     }
  1669.   while (insn != copy_end);
  1670.   
  1671.   /* Now copy the REG_NOTES.  */
  1672.   insn = copy_start;
  1673.   do
  1674.     {
  1675.       insn = NEXT_INSN (insn);
  1676.       if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  1677.        || GET_CODE (insn) == CALL_INSN)
  1678.       && map->insn_map[INSN_UID (insn)])
  1679.     REG_NOTES (map->insn_map[INSN_UID (insn)])
  1680.       = copy_rtx_and_substitute (REG_NOTES (insn), map);
  1681.     }
  1682.   while (insn != copy_end);
  1683.  
  1684.   /* There may be notes between insert_before and loop_end.  Emit a copy of
  1685.      each of these notes here, since there may be some important ones, such as
  1686.      NOTE_INSN_BLOCK_END notes, in this group.  We don't do this on the last
  1687.      iteration, because the original notes won't be deleted.  */
  1688.  
  1689.   if (! last_iteration)
  1690.     {
  1691.       for (insn = insert_before; insn != loop_end; insn = NEXT_INSN (insn))
  1692.     {
  1693.       if (GET_CODE (insn) == NOTE
  1694.           && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED)
  1695.         emit_note (NOTE_SOURCE_FILE (insn), NOTE_LINE_NUMBER (insn));
  1696.     }
  1697.     }
  1698.  
  1699.   if (final_label && LABEL_NUSES (final_label) > 0)
  1700.     emit_label (final_label);
  1701.  
  1702.   tem = gen_sequence ();
  1703.   end_sequence ();
  1704.   emit_insn_before (tem, insert_before);
  1705. }
  1706.  
  1707. /* Emit an insn, using the expand_binop to ensure that a valid insn is
  1708.    emitted.  This will correctly handle the case where the increment value
  1709.    won't fit in the immediate field of a PLUS insns.  */
  1710.  
  1711. void
  1712. emit_unrolled_add (dest_reg, src_reg, increment)
  1713.      rtx dest_reg, src_reg, increment;
  1714. {
  1715.   rtx result;
  1716.  
  1717.   result = expand_binop (GET_MODE (dest_reg), add_optab, src_reg, increment,
  1718.              dest_reg, 0, OPTAB_LIB_WIDEN);
  1719.  
  1720.   if (dest_reg != result)
  1721.     emit_move_insn (dest_reg, result);
  1722. }
  1723.  
  1724. /* Searches the insns between INSN and LOOP_END.  Returns 1 if there
  1725.    is a backward branch in that range that branches to to somewhere between
  1726.    LOOP_START and INSN.  Returns 0 otherwise.  */
  1727.  
  1728. /* ??? This is quadratic algorithm.  Could be rewriten to be linear.
  1729.    In practice, this is not a problem, because this function is seldom called,
  1730.    and uses a negligible amount of CPU time on average.  */
  1731.  
  1732. static int
  1733. back_branch_in_range_p (insn, loop_start, loop_end)
  1734.      rtx insn;
  1735.      rtx loop_start, loop_end;
  1736. {
  1737.   rtx p, q, target_insn;
  1738.  
  1739.   /* Stop before we get to the backward branch at the end of the loop.  */
  1740.   loop_end = PREV_INSN (loop_end);
  1741.  
  1742.   /* Check in case insn has been deleted, search forward for first non
  1743.      deleted insn following it.  */
  1744.   while (INSN_DELETED_P (insn))
  1745.     insn = NEXT_INSN (insn);
  1746.  
  1747.   /* Check for the case where insn is the last insn in the loop.  */
  1748.   if (insn == loop_end)
  1749.     return 0;
  1750.  
  1751.   for (p = NEXT_INSN (insn); p != loop_end; p = NEXT_INSN (p))
  1752.     {
  1753.       if (GET_CODE (p) == JUMP_INSN)
  1754.     {
  1755.       target_insn = JUMP_LABEL (p);
  1756.       
  1757.       /* Search from loop_start to insn, to see if one of them is
  1758.          the target_insn.  We can't use INSN_LUID comparisons here,
  1759.          since insn may not have an LUID entry.  */
  1760.       for (q = loop_start; q != insn; q = NEXT_INSN (q))
  1761.         if (q == target_insn)
  1762.           return 1;
  1763.     }
  1764.     }
  1765.  
  1766.   return 0;
  1767. }
  1768.  
  1769. /* Try to generate the simplest rtx for the expression
  1770.    (PLUS (MULT mult1 mult2) add1).  This is used to calculate the initial
  1771.    value of giv's.  */
  1772.  
  1773. static rtx
  1774. fold_rtx_mult_add (mult1, mult2, add1, mode)
  1775.      rtx mult1, mult2, add1;
  1776.      enum machine_mode mode;
  1777. {
  1778.   rtx temp;
  1779.  
  1780.   /* The modes must all be the same.  This should always be true.  For now,
  1781.      check to make sure.  */
  1782.   if ((GET_MODE (mult1) != mode && GET_MODE (mult1) != VOIDmode)
  1783.       || (GET_MODE (mult2) != mode && GET_MODE (mult2) != VOIDmode)
  1784.       || (GET_MODE (add1) != mode && GET_MODE (add1) != VOIDmode))
  1785.     abort ();
  1786.  
  1787.   /* Ensure that if at least one of mult1/mult2 are constant, then mult2
  1788.      will be a constant.  */
  1789.   if (GET_CODE (mult1) == CONST_INT)
  1790.     {
  1791.       temp = mult2;
  1792.       mult2 = mult1;
  1793.       mult1 = temp;
  1794.     }
  1795.  
  1796.   if (GET_CODE (mult2) == CONST_INT)
  1797.     {
  1798.       if (GET_CODE (mult1) == CONST_INT)
  1799.     return plus_constant (add1, INTVAL (mult1) * INTVAL (mult2));
  1800.       else if (INTVAL (mult2) == 0)
  1801.     return add1;
  1802.       else if (INTVAL (mult2) == 1)
  1803.     {
  1804.       if (GET_CODE (add1) == CONST_INT)
  1805.         return plus_constant (mult1, INTVAL (add1));
  1806.       else
  1807.         return gen_rtx (PLUS, GET_MODE (add1), add1, mult1);
  1808.     }
  1809.     }
  1810.   else if (GET_CODE (mult2) == CONST_DOUBLE
  1811.        || GET_CODE (mult1) == CONST_DOUBLE)
  1812.     /* Should simplify these if they can ever occur.  */
  1813.     abort ();
  1814.  
  1815.   mode = GET_MODE (add1);
  1816.   if (mode == VOIDmode)
  1817.     mode = GET_MODE (mult1);
  1818.   return gen_rtx (PLUS, mode, gen_rtx (MULT, GET_MODE (mult1), mult1, mult2),
  1819.           add1);
  1820. }
  1821.  
  1822. /* Searches the list of induction struct's for the biv BL, to try to calculate
  1823.    the total increment value for one iteration of the loop as a constant.
  1824.  
  1825.    Returns the increment value as an rtx, simplified as much as possible,
  1826.    if it can be calculated.  Otherwise, returns 0.  */
  1827.  
  1828. rtx 
  1829. biv_total_increment (bl, loop_start, loop_end)
  1830.      struct iv_class *bl;
  1831.      rtx loop_start, loop_end;
  1832. {
  1833.   struct induction *v;
  1834.   rtx result;
  1835.  
  1836.   /* For increment, must check every instruction that sets it.  Each
  1837.      instruction must be executed only once each time through the loop.
  1838.      To verify this, we check that the the insn is always executed, and that
  1839.      there are no backward branches after the insn that branch to before it.
  1840.      Also, the insn must have a mult_val of one (to make sure it really is
  1841.      an increment).  */
  1842.  
  1843.   result = const0_rtx;
  1844.   for (v = bl->biv; v; v = v->next_iv)
  1845.     {
  1846.       if (v->always_computable && v->mult_val == const1_rtx
  1847.       && ! back_branch_in_range_p (v->insn, loop_start, loop_end))
  1848.     result = fold_rtx_mult_add (result, const1_rtx, v->add_val, v->mode);
  1849.       else
  1850.     return 0;
  1851.     }
  1852.  
  1853.   return result;
  1854. }
  1855.  
  1856. /* Determine the initial value of the iteration variable, and the amount
  1857.    that it is incremented each loop.  Use the tables constructed by
  1858.    the strength reduction pass to calculate these values.
  1859.  
  1860.    Initial_value and/or increment are set to zero if their values could not
  1861.    be calculated.  */
  1862.  
  1863. static void
  1864. iteration_info (iteration_var, initial_value, increment, loop_start, loop_end)
  1865.      rtx iteration_var, *initial_value, *increment;
  1866.      rtx loop_start, loop_end;
  1867. {
  1868.   struct iv_class *bl;
  1869.   struct induction *v, *b;
  1870.  
  1871.   /* Clear the result values, in case no answer can be found.  */
  1872.   *initial_value = 0;
  1873.   *increment = 0;
  1874.  
  1875.   /* The iteration variable can be either a giv or a biv.  Check to see
  1876.      which it is, and compute the variable's initial value, and increment
  1877.      value if possible.  */
  1878.  
  1879.   /* If this is a new register, can't handle it since we don't have any
  1880.      reg_iv_type entry for it.  */
  1881.   if (REGNO (iteration_var) >= max_reg_before_loop)
  1882.     {
  1883.       if (loop_dump_stream)
  1884.     fprintf (loop_dump_stream,
  1885.          "Loop unrolling: No reg_iv_type entry for iteration var.\n");
  1886.       return;
  1887.     }
  1888.   /* Reject DImode iteration variables, since they could result in a number
  1889.      of iterations greater than the range of our `unsigned long' variable
  1890.      loop_n_iterations.  */
  1891.   else if (GET_MODE (iteration_var) == DImode)
  1892.     {
  1893.       if (loop_dump_stream)
  1894.     fprintf (loop_dump_stream,
  1895.          "Loop unrolling: Iteration var rejected because DImode.\n");
  1896.     }
  1897.   else if (reg_iv_type[REGNO (iteration_var)] == BASIC_INDUCT)
  1898.     {
  1899.       /* Grab initial value, only useful if it is a constant.  */
  1900.       bl = reg_biv_class[REGNO (iteration_var)];
  1901.       *initial_value = bl->initial_value;
  1902.  
  1903.       *increment = biv_total_increment (bl, loop_start, loop_end);
  1904.     }
  1905.   else if (reg_iv_type[REGNO (iteration_var)] == GENERAL_INDUCT)
  1906.     {
  1907.       /* Initial value is mult_val times the biv's initial value plus
  1908.      add_val.  Only useful if it is a constant.  */
  1909.       v = reg_iv_info[REGNO (iteration_var)];
  1910.       bl = reg_biv_class[REGNO (v->src_reg)];
  1911.       *initial_value = fold_rtx_mult_add (v->mult_val, bl->initial_value,
  1912.                       v->add_val, v->mode);
  1913.       
  1914.       /* Increment value is mult_val times the increment value of the biv.  */
  1915.  
  1916.       *increment = biv_total_increment (bl, loop_start, loop_end);
  1917.       if (*increment)
  1918.     *increment = fold_rtx_mult_add (v->mult_val, *increment, const0_rtx,
  1919.                     v->mode);
  1920.     }
  1921.   else
  1922.     {
  1923.       if (loop_dump_stream)
  1924.     fprintf (loop_dump_stream,
  1925.          "Loop unrolling: Not basic or general induction var.\n");
  1926.       return;
  1927.     }
  1928. }
  1929.  
  1930. /* Calculate the approximate final value of the iteration variable
  1931.    which has an loop exit test with code COMPARISON_CODE and comparison value
  1932.    of COMPARISON_VALUE.  Also returns an indication of whether the comparison
  1933.    was signed or unsigned, and the direction of the comparison.  This info is
  1934.    needed to calculate the number of loop iterations.  */
  1935.  
  1936. static rtx
  1937. approx_final_value (comparison_code, comparison_value, unsigned_p, compare_dir)
  1938.      enum rtx_code comparison_code;
  1939.      rtx comparison_value;
  1940.      int *unsigned_p;
  1941.      int *compare_dir;
  1942. {
  1943.   /* Calculate the final value of the induction variable.
  1944.      The exact final value depends on the branch operator, and increment sign.
  1945.      This is only an approximate value.  It will be wrong if the iteration
  1946.      variable is not incremented by one each time through the loop, and
  1947.      approx final value - start value % increment != 0.  */
  1948.  
  1949.   *unsigned_p = 0;
  1950.   switch (comparison_code)
  1951.     {
  1952.     case LEU:
  1953.       *unsigned_p = 1;
  1954.     case LE:
  1955.       *compare_dir = 1;
  1956.       return plus_constant (comparison_value, 1);
  1957.     case GEU:
  1958.       *unsigned_p = 1;
  1959.     case GE:
  1960.       *compare_dir = -1;
  1961.       return plus_constant (comparison_value, -1);
  1962.     case EQ:
  1963.       /* Can not calculate a final value for this case.  */
  1964.       *compare_dir = 0;
  1965.       return 0;
  1966.     case LTU:
  1967.       *unsigned_p = 1;
  1968.     case LT:
  1969.       *compare_dir = 1;
  1970.       return comparison_value;
  1971.       break;
  1972.     case GTU:
  1973.       *unsigned_p = 1;
  1974.     case GT:
  1975.       *compare_dir = -1;
  1976.       return comparison_value;
  1977.     case NE:
  1978.       *compare_dir = 0;
  1979.       return comparison_value;
  1980.     default:
  1981.       abort ();
  1982.     }
  1983. }
  1984.  
  1985. /* For each biv and giv, determine whether it can be safely split into
  1986.    a different variable for each unrolled copy of the loop body.  If it
  1987.    is safe to split, then indicate that by saving some useful info
  1988.    in the splittable_regs array.
  1989.  
  1990.    If the loop is being completely unrolled, then splittable_regs will hold
  1991.    the current value of the induction variable while the loop is unrolled.
  1992.    It must be set to the initial value of the induction variable here.
  1993.    Otherwise, splittable_regs will hold the difference between the current
  1994.    value of the induction variable and the value the induction variable had
  1995.    at the top of the loop.  It must be set to the value 0 here.  */
  1996.  
  1997. /* ?? If the loop is only unrolled twice, then most of the restrictions to
  1998.    constant values are unnecessary, since we can easily calculate increment
  1999.    values in this case even if nothing is constant.  The increment value
  2000.    should not involve a multiply however.  */
  2001.  
  2002. /* ?? Even if the biv/giv increment values aren't constant, it may still
  2003.    be beneficial to split the variable if the loop is only unrolled a few
  2004.    times, since multiplies by small integers (1,2,3,4) are very cheap.  */
  2005.  
  2006. static int
  2007. find_splittable_regs (unroll_type, loop_start, loop_end, end_insert_before,
  2008.              unroll_number)
  2009.      enum unroll_types unroll_type;
  2010.      rtx loop_start, loop_end;
  2011.      rtx end_insert_before;
  2012.      int unroll_number;
  2013. {
  2014.   struct iv_class *bl;
  2015.   rtx increment, tem;
  2016.   rtx biv_final_value;
  2017.   int biv_splittable;
  2018.   int result = 0;
  2019.  
  2020.   for (bl = loop_iv_list; bl; bl = bl->next)
  2021.     {
  2022.       /* Biv_total_increment must return a constant value,
  2023.      otherwise we can not calculate the split values.  */
  2024.  
  2025.       increment = biv_total_increment (bl, loop_start, loop_end);
  2026.       if (! increment || GET_CODE (increment) != CONST_INT)
  2027.     continue;
  2028.  
  2029.       /* The loop must be unrolled completely, or else have a known number
  2030.      of iterations and only one exit, or else the biv must be dead
  2031.      outside the loop, or else the final value must be known.  Otherwise,
  2032.      it is unsafe to split the biv since it may not have the proper
  2033.      value on loop exit.  */
  2034.  
  2035.       /* loop_number_exit_labels is non-zero if the loop has an exit other than
  2036.      a fall through at the end.  */
  2037.  
  2038.       biv_splittable = 1;
  2039.       biv_final_value = 0;
  2040.       if (unroll_type != UNROLL_COMPLETELY
  2041.       && (loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]]
  2042.           || unroll_type == UNROLL_NAIVE)
  2043.       && (uid_luid[regno_last_uid[bl->regno]] >= INSN_LUID (loop_end)
  2044.           || ! bl->init_insn
  2045.           || INSN_UID (bl->init_insn) >= max_uid_for_loop
  2046.           || (uid_luid[regno_first_uid[bl->regno]]
  2047.           < INSN_LUID (bl->init_insn))
  2048.           || reg_mentioned_p (bl->biv->dest_reg, SET_SRC (bl->init_set)))
  2049.       && ! (biv_final_value = final_biv_value (bl, loop_start, loop_end)))
  2050.     biv_splittable = 0;
  2051.  
  2052.       /* If final value is non-zero, then must emit an instruction which sets
  2053.      the value of the biv to the proper value.  This is done after
  2054.      handling all of the givs, since some of them may need to use the
  2055.      biv's value in their initialization code.  */
  2056.  
  2057.       /* This biv is splittable.  If completely unrolling the loop, save
  2058.      the biv's initial value.  Otherwise, save the constant zero.  */
  2059.  
  2060.       if (biv_splittable == 1)
  2061.     {
  2062.       if (unroll_type == UNROLL_COMPLETELY)
  2063.         {
  2064.           /* If the initial value of the biv is itself (i.e. it is too
  2065.          complicated for strength_reduce to compute), or is a hard
  2066.          register, then we must create a new psuedo reg to hold the
  2067.          initial value of the biv.  */
  2068.  
  2069.           if (GET_CODE (bl->initial_value) == REG
  2070.           && (REGNO (bl->initial_value) == bl->regno
  2071.               || REGNO (bl->initial_value) < FIRST_PSEUDO_REGISTER))
  2072.         {
  2073.           rtx tem = gen_reg_rtx (bl->biv->mode);
  2074.           
  2075.           emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
  2076.                     loop_start);
  2077.  
  2078.           if (loop_dump_stream)
  2079.             fprintf (loop_dump_stream, "Biv %d initial value remapped to %d.\n",
  2080.                  bl->regno, REGNO (tem));
  2081.  
  2082.           splittable_regs[bl->regno] = tem;
  2083.         }
  2084.           else
  2085.         splittable_regs[bl->regno] = bl->initial_value;
  2086.         }
  2087.       else
  2088.         splittable_regs[bl->regno] = const0_rtx;
  2089.  
  2090.       /* Save the number of instructions that modify the biv, so that
  2091.          we can treat the last one specially.  */
  2092.  
  2093.       splittable_regs_updates[bl->regno] = bl->biv_count;
  2094.  
  2095.       result++;
  2096.  
  2097.       if (loop_dump_stream)
  2098.         fprintf (loop_dump_stream,
  2099.              "Biv %d safe to split.\n", bl->regno);
  2100.     }
  2101.  
  2102.       /* Check every giv that depends on this biv to see whether it is
  2103.      splittable also.  Even if the biv isn't splittable, givs which
  2104.      depend on it may be splittable if the biv is live outside the
  2105.      loop, and the givs aren't.  */
  2106.  
  2107.       result = find_splittable_givs (bl, unroll_type, loop_start, loop_end,
  2108.                      increment, unroll_number, result);
  2109.  
  2110.       /* If final value is non-zero, then must emit an instruction which sets
  2111.      the value of the biv to the proper value.  This is done after
  2112.      handling all of the givs, since some of them may need to use the
  2113.      biv's value in their initialization code.  */
  2114.       if (biv_final_value)
  2115.     {
  2116.       /* If the loop has multiple exits, emit the insns before the
  2117.          loop to ensure that it will always be executed no matter
  2118.          how the loop exits.  Otherwise emit the insn after the loop,
  2119.          since this is slightly more efficient.  */
  2120.       if (! loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]])
  2121.         emit_insn_before (gen_move_insn (bl->biv->src_reg,
  2122.                          biv_final_value),
  2123.                   end_insert_before);
  2124.       else
  2125.         {
  2126.           /* Create a new register to hold the value of the biv, and then
  2127.          set the biv to its final value before the loop start.  The biv
  2128.          is set to its final value before loop start to ensure that
  2129.          this insn will always be executed, no matter how the loop
  2130.          exits.  */
  2131.           rtx tem = gen_reg_rtx (bl->biv->mode);
  2132.           emit_insn_before (gen_move_insn (tem, bl->biv->src_reg),
  2133.                 loop_start);
  2134.           emit_insn_before (gen_move_insn (bl->biv->src_reg,
  2135.                            biv_final_value),
  2136.                 loop_start);
  2137.  
  2138.           if (loop_dump_stream)
  2139.         fprintf (loop_dump_stream, "Biv %d mapped to %d for split.\n",
  2140.              REGNO (bl->biv->src_reg), REGNO (tem));
  2141.  
  2142.           /* Set up the mapping from the original biv register to the new
  2143.          register.  */
  2144.           bl->biv->src_reg = tem;
  2145.         }
  2146.     }
  2147.     }
  2148.   return result;
  2149. }
  2150.  
  2151. /* For every giv based on the biv BL, check to determine whether it is
  2152.    splittable.  This is a subroutine to find_splittable_regs ().  */
  2153.  
  2154. static int
  2155. find_splittable_givs (bl, unroll_type, loop_start, loop_end, increment,
  2156.               unroll_number, result)
  2157.      struct iv_class *bl;
  2158.      enum unroll_types unroll_type;
  2159.      rtx loop_start, loop_end;
  2160.      rtx increment;
  2161.      int unroll_number, result;
  2162. {
  2163.   struct induction *v;
  2164.   rtx final_value;
  2165.   rtx tem;
  2166.  
  2167.   for (v = bl->giv; v; v = v->next_iv)
  2168.     {
  2169.       rtx giv_inc, value;
  2170.  
  2171.       /* Only split the giv if it has already been reduced, or if the loop is
  2172.      being completely unrolled.  */
  2173.       if (unroll_type != UNROLL_COMPLETELY && v->ignore)
  2174.     continue;
  2175.  
  2176.       /* The giv can be split if the insn that sets the giv is executed once
  2177.      and only once on every iteration of the loop.  */
  2178.       /* An address giv can always be split.  v->insn is just a use not a set,
  2179.      and hence it does not matter whether it is always executed.  All that
  2180.      matters is that all the biv increments are always executed, and we
  2181.      won't reach here if they aren't.  */
  2182.       if (v->giv_type != DEST_ADDR
  2183.       && (! v->always_computable
  2184.           || back_branch_in_range_p (v->insn, loop_start, loop_end)))
  2185.     continue;
  2186.       
  2187.       /* The giv increment value must be a constant.  */
  2188.       giv_inc = fold_rtx_mult_add (v->mult_val, increment, const0_rtx,
  2189.                    v->mode);
  2190.       if (! giv_inc || GET_CODE (giv_inc) != CONST_INT)
  2191.     continue;
  2192.  
  2193.       /* The loop must be unrolled completely, or else have a known number of
  2194.      iterations and only one exit, or else the giv must be dead outside
  2195.      the loop, or else the final value of the giv must be known.
  2196.      Otherwise, it is not safe to split the giv since it may not have the
  2197.      proper value on loop exit.  */
  2198.       
  2199.       /* The used outside loop test will fail for DEST_ADDR givs.  They are
  2200.      never used outside the loop anyways, so it is always safe to split a
  2201.      DEST_ADDR giv.  */
  2202.  
  2203.       final_value = 0;
  2204.       if (unroll_type != UNROLL_COMPLETELY
  2205.       && (loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]]
  2206.           || unroll_type == UNROLL_NAIVE)
  2207.       && v->giv_type != DEST_ADDR
  2208.       && ((regno_first_uid[REGNO (v->dest_reg)] != INSN_UID (v->insn)
  2209.            /* Check for the case where the pseudo is set by a shift/add
  2210.           sequence, in which case the first insn setting the pseudo
  2211.           is the first insn of the shift/add sequence.  */
  2212.            && (! (tem = find_reg_note (v->insn, REG_RETVAL, 0))
  2213.            || (regno_first_uid[REGNO (v->dest_reg)]
  2214.                != INSN_UID (XEXP (tem, 0)))))
  2215.           /* Line above always fails if INSN was moved by loop opt.  */
  2216.           || (uid_luid[regno_last_uid[REGNO (v->dest_reg)]]
  2217.           >= INSN_LUID (loop_end)))
  2218.       && ! (final_value = v->final_value))
  2219.     continue;
  2220.  
  2221. #if 0
  2222.       /* Currently, non-reduced/final-value givs are never split.  */
  2223.       /* Should emit insns after the loop if possible, as the biv final value
  2224.      code below does.  */
  2225.  
  2226.       /* If the final value is non-zero, and the giv has not been reduced,
  2227.      then must emit an instruction to set the final value.  */
  2228.       if (final_value && !v->new_reg)
  2229.     {
  2230.       /* Create a new register to hold the value of the giv, and then set
  2231.          the giv to its final value before the loop start.  The giv is set
  2232.          to its final value before loop start to ensure that this insn
  2233.          will always be executed, no matter how we exit.  */
  2234.       tem = gen_reg_rtx (v->mode);
  2235.       emit_insn_before (gen_move_insn (tem, v->dest_reg), loop_start);
  2236.       emit_insn_before (gen_move_insn (v->dest_reg, final_value),
  2237.                 loop_start);
  2238.       
  2239.       if (loop_dump_stream)
  2240.         fprintf (loop_dump_stream, "Giv %d mapped to %d for split.\n",
  2241.              REGNO (v->dest_reg), REGNO (tem));
  2242.       
  2243.       v->src_reg = tem;
  2244.     }
  2245. #endif
  2246.  
  2247.       /* This giv is splittable.  If completely unrolling the loop, save the
  2248.      giv's initial value.  Otherwise, save the constant zero for it.  */
  2249.  
  2250.       if (unroll_type == UNROLL_COMPLETELY)
  2251.     value = fold_rtx_mult_add (v->mult_val, bl->initial_value, v->add_val,
  2252.                    v->mode);
  2253.       else
  2254.     value = const0_rtx;
  2255.  
  2256.       if (v->new_reg)
  2257.     {
  2258.       /* If the giv is an address destination, it could be something other
  2259.          than a simple register, these have to be treated differently.  */
  2260.       if (v->giv_type == DEST_REG)
  2261.         splittable_regs[REGNO (v->new_reg)] = value;
  2262.       else
  2263.         {
  2264.           /* Splitting address givs is useful since it will often allow us
  2265.          to eliminate some increment insns for the base giv as
  2266.          unnecessary.  */
  2267.  
  2268.           /* If the addr giv is combined with a dest_reg giv, then all
  2269.          references to that dest reg will be remapped, which is NOT
  2270.          what we want for split addr regs. We always create a new
  2271.          register for the split addr giv, just to be safe.  */
  2272.  
  2273.           /* ??? If there are multiple address givs which have been
  2274.          combined with the same dest_reg giv, then we may only need
  2275.          one new register for them.  Pulling out constants below will
  2276.          catch some of the common cases of this.  Currently, I leave
  2277.          the work of simplifying multiple address givs to the
  2278.          following cse pass.  */
  2279.           
  2280.           v->const_adjust = 0;
  2281.           if (unroll_type != UNROLL_COMPLETELY)
  2282.         {
  2283.           /* If not completely unrolling the loop, then create a new
  2284.              register to hold the split value of the DEST_ADDR giv.
  2285.              Emit insn to initialize its value before loop start.  */
  2286.           tem = gen_reg_rtx (v->mode);
  2287.  
  2288.           /* If the address giv has a constant in its new_reg value,
  2289.              then this constant can be pulled out and put in value,
  2290.              instead of being part of the initialization code.  */
  2291.           
  2292.           if (GET_CODE (v->new_reg) == PLUS
  2293.               && GET_CODE (XEXP (v->new_reg, 1)) == CONST_INT)
  2294.             {
  2295.               v->dest_reg
  2296.             = plus_constant (tem, INTVAL (XEXP (v->new_reg,1)));
  2297.               
  2298.               /* Only succeed if this will give valid addresses.
  2299.              Try to validate both the first and the last
  2300.              address resulting from loop unrolling, if
  2301.              one fails, then can't do const elim here.  */
  2302.               if (memory_address_p (v->mode, v->dest_reg)
  2303.               && memory_address_p (v->mode,
  2304.                        plus_constant (v->dest_reg,
  2305.                               INTVAL (giv_inc)
  2306.                               * (unroll_number - 1))))
  2307.             {
  2308.               /* Save the negative of the eliminated const, so
  2309.                  that we can calculate the dest_reg's increment
  2310.                  value later.  */
  2311.               v->const_adjust = - INTVAL (XEXP (v->new_reg, 1));
  2312.  
  2313.               v->new_reg = XEXP (v->new_reg, 0);
  2314.               if (loop_dump_stream)
  2315.                 fprintf (loop_dump_stream,
  2316.                      "Eliminating constant from giv %d\n",
  2317.                      REGNO (tem));
  2318.             }
  2319.               else
  2320.             v->dest_reg = tem;
  2321.             }
  2322.           else
  2323.             v->dest_reg = tem;
  2324.           
  2325.           /* If the address hasn't been checked for validity yet, do so
  2326.              now, and fail completely if either the first or the last
  2327.              unrolled copy of the address is not a valid address.  */
  2328.           if (v->dest_reg == tem
  2329.               && (! memory_address_p (v->mode, v->dest_reg)
  2330.               || ! memory_address_p (v->mode,
  2331.                  plus_constant (v->dest_reg,
  2332.                         INTVAL (giv_inc)
  2333.                         * (unroll_number -1)))))
  2334.             {
  2335.               if (loop_dump_stream)
  2336.             fprintf (loop_dump_stream,
  2337.                  "Illegal address for giv at insn %d\n",
  2338.                  INSN_UID (v->insn));
  2339.               continue;
  2340.             }
  2341.           
  2342.           /* To initialize the new register, just move the value of
  2343.              new_reg into it.  This is not guaranteed to give a valid
  2344.              instruction on machines with complex addressing modes.
  2345.              If we can't recognize it, then delete it and emit insns
  2346.              to calculate the value from scratch.  */
  2347.           emit_insn_before (gen_rtx (SET, VOIDmode, tem,
  2348.                          copy_rtx (v->new_reg)),
  2349.                     loop_start);
  2350.           if (! recog_memoized (PREV_INSN (loop_start)))
  2351.             {
  2352.               delete_insn (PREV_INSN (loop_start));
  2353.               emit_iv_add_mult (bl->initial_value, v->mult_val,
  2354.                     v->add_val, tem, loop_start);
  2355.               if (loop_dump_stream)
  2356.             fprintf (loop_dump_stream,
  2357.                  "Illegal init insn, rewritten.\n");
  2358.             }
  2359.         }
  2360.           else
  2361.         {
  2362.           v->dest_reg = value;
  2363.           
  2364.           /* Check the resulting address for validity, and fail
  2365.              if the resulting address would be illegal.  */
  2366.           if (! memory_address_p (v->mode, v->dest_reg)
  2367.               || ! memory_address_p (v->mode,
  2368.                      plus_constant (v->dest_reg,
  2369.                             INTVAL (giv_inc) *
  2370.                             (unroll_number -1))))
  2371.             {
  2372.               if (loop_dump_stream)
  2373.             fprintf (loop_dump_stream,
  2374.                  "Illegal address for giv at insn %d\n",
  2375.                  INSN_UID (v->insn));
  2376.               continue;
  2377.             }
  2378.         }
  2379.           
  2380.           /* Store the value of dest_reg into the insn.  This sharing
  2381.          will not be a problem as this insn will always be copied
  2382.          later.  */
  2383.           
  2384.           *v->location = v->dest_reg;
  2385.           
  2386.           /* If this address giv is combined with a dest reg giv, then
  2387.          save the base giv's induction pointer so that we will be
  2388.          able to handle this address giv properly.  The base giv
  2389.          itself does not have to be splittable.  */
  2390.           
  2391.           if (v->same && v->same->giv_type == DEST_REG)
  2392.         addr_combined_regs[REGNO (v->same->new_reg)] = v->same;
  2393.           
  2394.           if (GET_CODE (v->new_reg) == REG)
  2395.         {
  2396.           /* This giv maybe hasn't been combined with any others.
  2397.              Make sure that it's giv is marked as splittable here.  */
  2398.           
  2399.           splittable_regs[REGNO (v->new_reg)] = value;
  2400.           
  2401.           /* Make it appear to depend upon itself, so that the
  2402.              giv will be properly split in the main loop above.  */
  2403.           if (! v->same)
  2404.             {
  2405.               v->same = v;
  2406.               addr_combined_regs[REGNO (v->new_reg)] = v;
  2407.             }
  2408.         }
  2409.           
  2410.           if (loop_dump_stream)
  2411.         fprintf (loop_dump_stream, "DEST_ADDR giv being split.\n");
  2412.         }
  2413.     }
  2414.       else
  2415.     {
  2416. #if 0
  2417.       /* Currently, unreduced giv's can't be split.  This is not too much
  2418.          of a problem since unreduced giv's are not live across loop
  2419.          iterations anyways.  When unrolling a loop completely though,
  2420.          it makes sense to reduce&split givs when possible, as this will
  2421.          result in simpler instructions, and will not require that a reg
  2422.          be live across loop iterations.  */
  2423.       
  2424.       splittable_regs[REGNO (v->dest_reg)] = value;
  2425.       fprintf (stderr, "Giv %d at insn %d not reduced\n",
  2426.            REGNO (v->dest_reg), INSN_UID (v->insn));
  2427. #else
  2428.       continue;
  2429. #endif
  2430.     }
  2431.       
  2432.       /* Givs are only updated once by definition.  Mark it so if this is
  2433.      a splittable register.  Don't need to do anything for address givs
  2434.      where this may not be a register.  */
  2435.  
  2436.       if (GET_CODE (v->new_reg) == REG)
  2437.     splittable_regs_updates[REGNO (v->new_reg)] = 1;
  2438.  
  2439.       result++;
  2440.       
  2441.       if (loop_dump_stream)
  2442.     {
  2443.       int regnum;
  2444.       
  2445.       if (GET_CODE (v->dest_reg) == CONST_INT)
  2446.         regnum = -1;
  2447.       else if (GET_CODE (v->dest_reg) != REG)
  2448.         regnum = REGNO (XEXP (v->dest_reg, 0));
  2449.       else
  2450.         regnum = REGNO (v->dest_reg);
  2451.       fprintf (loop_dump_stream, "Giv %d at insn %d safe to split.\n",
  2452.            regnum, INSN_UID (v->insn));
  2453.     }
  2454.     }
  2455.  
  2456.   return result;
  2457. }
  2458.  
  2459. /* Try to prove that the register is dead after the loop exits.  Trace every
  2460.    loop exit looking for an insn that will always be executed, which sets
  2461.    the register to some value, and appears before the first use of the register
  2462.    is found.  If successful, then return 1, otherwise return 0.  */
  2463.  
  2464. /* ?? Could be made more intelligent in the handling of jumps, so that
  2465.    it can search past if statements and other similar structures.  */
  2466.  
  2467. static int
  2468. reg_dead_after_loop (reg, loop_start, loop_end)
  2469.      rtx reg, loop_start, loop_end;
  2470. {
  2471.   rtx insn, label;
  2472.   enum rtx_code code;
  2473.  
  2474.   /* HACK: Must also search the loop fall through exit, create a label_ref
  2475.      here which points to the loop_end, and append the loop_number_exit_labels
  2476.      list to it.  */
  2477.   label = gen_rtx (LABEL_REF, VOIDmode, loop_end);
  2478.   LABEL_NEXTREF (label)
  2479.     = loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]];
  2480.  
  2481.   for ( ; label; label = LABEL_NEXTREF (label))
  2482.     {
  2483.       /* Succeed if find an insn which sets the biv or if reach end of
  2484.      function.  Fail if find an insn that uses the biv, or if come to
  2485.      a conditional jump.  */
  2486.  
  2487.       insn = NEXT_INSN (XEXP (label, 0));
  2488.       while (1)
  2489.     {
  2490.       if (insn == 0)
  2491.         break;
  2492.  
  2493.       if ((code = GET_CODE (insn)) == INSN || code == JUMP_INSN
  2494.           || code == CALL_INSN)
  2495.         {
  2496.           if (GET_CODE (PATTERN (insn)) == SET)
  2497.         {
  2498.           if (reg_mentioned_p (reg, SET_SRC (PATTERN (insn))))
  2499.             return 0;
  2500.           if (SET_DEST (PATTERN (insn)) == reg)
  2501.             break;
  2502.           if (reg_mentioned_p (reg, SET_DEST (PATTERN (insn))))
  2503.             return 0;
  2504.         }
  2505.           else if (reg_mentioned_p (reg, PATTERN (insn)))
  2506.         return 0;
  2507.         }
  2508.       if (code == JUMP_INSN)
  2509.         {
  2510.           if (GET_CODE (PATTERN (insn)) == RETURN)
  2511.         break;
  2512.           else if (! simplejump_p (insn))
  2513.         return 0;
  2514.           else
  2515.         {
  2516.           insn = JUMP_LABEL (insn);
  2517.           /* If this branches to a code label after a LOOP_BEG or
  2518.              a LOOP_CONT note, then assume it is a loop back edge.
  2519.              Must fail in that case to prevent going into an infinite
  2520.              loop trying to trace infinite loops.
  2521.  
  2522.              In the presence of syntax errors, this may be a jump to
  2523.              a CODE_LABEL that was never emitted.  Fail in this case
  2524.              also.  */
  2525.  
  2526.           if (! PREV_INSN (insn)
  2527.               || (GET_CODE (PREV_INSN (insn)) == NOTE
  2528.               && ((NOTE_LINE_NUMBER (PREV_INSN (insn))
  2529.                    == NOTE_INSN_LOOP_BEG)
  2530.                   || (NOTE_LINE_NUMBER (PREV_INSN (insn))
  2531.                   == NOTE_INSN_LOOP_CONT))))
  2532.             return 0;
  2533.         }
  2534.         }
  2535.       
  2536.       insn = NEXT_INSN (insn);
  2537.     }
  2538.     }
  2539.  
  2540.   /* Success, the register is dead on all loop exits.  */
  2541.   return 1;
  2542. }
  2543.  
  2544. /* Try to calculate the final value of the biv, the value it will have at
  2545.    the end of the loop.  If we can do it, return that value.  */
  2546.   
  2547. rtx
  2548. final_biv_value (bl, loop_start, loop_end)
  2549.      struct iv_class *bl;
  2550.      rtx loop_start, loop_end;
  2551. {
  2552.   rtx increment, tem;
  2553.  
  2554.   /* The final value for reversed bivs must be calculated differently than
  2555.       for ordinary bivs.  In this case, there is already an insn after the
  2556.      loop which sets this biv's final value (if necessary), and there are
  2557.      no other loop exits, so we can return any value.  */
  2558.   if (bl->reversed)
  2559.     {
  2560.       if (loop_dump_stream)
  2561.     fprintf (loop_dump_stream,
  2562.          "Final biv value for %d, reversed biv.\n", bl->regno);
  2563.          
  2564.       return const0_rtx;
  2565.     }
  2566.  
  2567.   /* Try to calculate the final value as initial value + (number of iterations
  2568.      * increment).  For this to work, increment must be invariant, and the
  2569.      only exit from the loop must be the fall through at the bottom (otherwise
  2570.      it may not have its final value when the loop exits).  */
  2571.  
  2572.   if (loop_n_iterations != 0
  2573.       && ! loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]])
  2574.     {
  2575.       increment = biv_total_increment (bl, loop_start, loop_end);
  2576.       
  2577.       if (increment && invariant_p (increment))
  2578.     {
  2579.       /* Can calculate the loop exit value, emit insns after loop
  2580.          end to calculate this value into a temporary register in
  2581.          case it is needed later.  */
  2582.  
  2583.       tem = gen_reg_rtx (bl->biv->mode);
  2584.       emit_iv_add_mult (increment,
  2585.                 gen_rtx (CONST_INT, VOIDmode, loop_n_iterations),
  2586.                 bl->initial_value, tem, NEXT_INSN (loop_end));
  2587.  
  2588.       if (loop_dump_stream)
  2589.         fprintf (loop_dump_stream,
  2590.              "Final biv value for %d, calculated.\n", bl->regno);
  2591.       
  2592.       return tem;
  2593.     }
  2594.     }
  2595.  
  2596.   /* Check to see if the biv is dead at all loop exits.  */
  2597.   if (reg_dead_after_loop (bl->biv->src_reg, loop_start, loop_end))
  2598.     {
  2599.       if (loop_dump_stream)
  2600.     fprintf (loop_dump_stream,
  2601.          "Final biv value for %d, biv dead after loop exit.\n",
  2602.          bl->regno);
  2603.  
  2604.       return const0_rtx;
  2605.     }
  2606.  
  2607.   return 0;
  2608. }
  2609.  
  2610. /* Try to calculate the final value of the giv, the value it will have at
  2611.    the end of the loop.  If we can do it, return that value.  */
  2612.  
  2613. rtx
  2614. final_giv_value (v, loop_start, loop_end)
  2615.      struct induction *v;
  2616.      rtx loop_start, loop_end;
  2617. {
  2618.   struct iv_class *bl;
  2619.   rtx reg, insn, pattern;
  2620.   rtx increment, tem;
  2621.   enum rtx_code code;
  2622.   rtx insert_before;
  2623.  
  2624.   bl = reg_biv_class[REGNO (v->src_reg)];
  2625.  
  2626.   /* The final value for givs which depend on reversed bivs must be calculated
  2627.      differently than for ordinary givs.  In this case, there is already an
  2628.      insn after the loop which sets this giv's final value (if necessary),
  2629.      and there are no other loop exits, so we can return any value.  */
  2630.   if (bl->reversed)
  2631.     {
  2632.       if (loop_dump_stream)
  2633.     fprintf (loop_dump_stream,
  2634.          "Final giv value for %d, depends on reversed biv\n",
  2635.          REGNO (v->dest_reg));
  2636.       return const0_rtx;
  2637.     }
  2638.  
  2639.   /* Try to calculate the final value as a function of the biv it depends
  2640.      upon.  The only exit from the loop must be the fall through at the bottom
  2641.      (otherwise it may not have its final value when the loop exits).  */
  2642.       
  2643.   /* ??? Can calculate the final giv value by subtracting off the
  2644.      extra biv increments times the giv's mult_val.  The loop must have
  2645.      only one exit for this to work, but the loop iterations does not need
  2646.      to be known.  */
  2647.  
  2648.   if (loop_n_iterations != 0
  2649.       && ! loop_number_exit_labels[uid_loop_num[INSN_UID (loop_start)]])
  2650.     {
  2651.       /* ?? It is tempting to use the biv's value here since these insns will
  2652.      be put after the loop, and hence the biv will have its final value
  2653.      then.  However, this fails if the biv is subsequently eliminated.
  2654.      Perhaps determine whether biv's are eliminable before trying to
  2655.      determine whether giv's are replaceable so that we can use the
  2656.      biv value here if it is not eliminable.  */
  2657.  
  2658.       increment = biv_total_increment (bl, loop_start, loop_end);
  2659.  
  2660.       if (increment && invariant_p (increment))
  2661.     {
  2662.       /* Can calculate the loop exit value of its biv as
  2663.          (loop_n_iterations * increment) + initial_value */
  2664.           
  2665.       /* The loop exit value of the giv is then
  2666.          (final_biv_value - extra increments) * mult_val + add_val.
  2667.          The extra increments are any increments to the biv which
  2668.          occur in the loop after the giv's value is calculated.
  2669.          We must search from the insn that sets the giv to the end
  2670.          of the loop to calculate this value.  */
  2671.  
  2672.       insert_before = NEXT_INSN (loop_end);
  2673.  
  2674.       /* Put the final biv value in tem.  */
  2675.       tem = gen_reg_rtx (bl->biv->mode);
  2676.       emit_iv_add_mult (increment,
  2677.                 gen_rtx (CONST_INT, VOIDmode, loop_n_iterations),
  2678.                 bl->initial_value, tem, insert_before);
  2679.  
  2680.       /* Subtract off extra increments as we find them.  */
  2681.       for (insn = NEXT_INSN (v->insn); insn != loop_end;
  2682.            insn = NEXT_INSN (insn))
  2683.         {
  2684.           if (GET_CODE (insn) == INSN
  2685.           && GET_CODE (PATTERN (insn)) == SET
  2686.           && SET_DEST (PATTERN (insn)) == v->src_reg)
  2687.         {
  2688.           pattern = PATTERN (insn);
  2689.           if (GET_CODE (SET_SRC (pattern)) != PLUS)
  2690.             {
  2691.               /* Sometimes a biv is computed in a temp reg,
  2692.              and then copied into the biv reg.  */
  2693.               pattern = PATTERN (PREV_INSN (insn));
  2694.               if (GET_CODE (SET_SRC (pattern)) != PLUS)
  2695.             abort ();
  2696.             }
  2697.           if (GET_CODE (XEXP (SET_SRC (pattern), 0)) != REG
  2698.               || REGNO (XEXP (SET_SRC (pattern), 0)) != bl->regno)
  2699.             abort ();
  2700.           
  2701.           tem = expand_binop (GET_MODE (tem), sub_optab, tem,
  2702.                       XEXP (SET_SRC (pattern), 1), 0, 0,
  2703.                       OPTAB_LIB_WIDEN);
  2704.         }
  2705.         }
  2706.       
  2707.       /* Now calculate the giv's final value.  */
  2708.       emit_iv_add_mult (tem, v->mult_val, v->add_val, tem,
  2709.                 insert_before);
  2710.       
  2711.       if (loop_dump_stream)
  2712.         fprintf (loop_dump_stream,
  2713.              "Final giv value for %d, calc from biv's value.\n",
  2714.              REGNO (v->dest_reg));
  2715.  
  2716.       return tem;
  2717.     }
  2718.     }
  2719.  
  2720.   /* Replaceable giv's should never reach here.  */
  2721.   if (v->replaceable)
  2722.     abort ();
  2723.  
  2724.   /* Check to see if the biv is dead at all loop exits.  */
  2725.   if (reg_dead_after_loop (v->dest_reg, loop_start, loop_end))
  2726.     {
  2727.       if (loop_dump_stream)
  2728.     fprintf (loop_dump_stream,
  2729.          "Final giv value for %d, giv dead after loop exit.\n",
  2730.          REGNO (v->dest_reg));
  2731.  
  2732.       return const0_rtx;
  2733.     }
  2734.  
  2735.   return 0;
  2736. }
  2737.  
  2738.  
  2739. /* Calculate the number of loop iterations.  Returns the exact number of loop
  2740.    iterations if it can be calculated, otherwise retusns zero.  */
  2741.  
  2742. unsigned long
  2743. loop_iterations (loop_start, loop_end)
  2744.      rtx loop_start, loop_end;
  2745. {
  2746.   rtx comparison, comparison_value;
  2747.   rtx iteration_var, initial_value, increment, final_value;
  2748.   enum rtx_code comparison_code;
  2749.   int i, increment_dir;
  2750.   int unsigned_compare, compare_dir, final_larger;
  2751.   unsigned long tempu;
  2752.  
  2753.   /* First find the iteration variable.  If the last insn is a conditional
  2754.      branch, and the insn before tests a register value, make that the
  2755.      iteration variable.  */
  2756.   
  2757.   loop_initial_value = 0;
  2758.   loop_increment = 0;
  2759.   loop_final_value = 0;
  2760.   loop_iteration_var = 0;
  2761.  
  2762.   comparison = get_condition_for_loop (PREV_INSN (loop_end));
  2763.   if (comparison == 0)
  2764.     {
  2765.       if (loop_dump_stream)
  2766.     fprintf (loop_dump_stream,
  2767.          "Loop unrolling: No final conditional branch found.\n");
  2768.       return 0;
  2769.     }
  2770.  
  2771.   /* ??? Get_condition may switch position of induction variable and
  2772.      invariant register when it canonicalizes the comparison.  */
  2773.  
  2774.   comparison_code = GET_CODE (comparison);
  2775.   iteration_var = XEXP (comparison, 0);
  2776.   comparison_value = XEXP (comparison, 1);
  2777.  
  2778.   if (GET_CODE (iteration_var) != REG)
  2779.     {
  2780.       if (loop_dump_stream)
  2781.     fprintf (loop_dump_stream,
  2782.          "Loop unrolling: Comparison not against register.\n");
  2783.       return 0;
  2784.     }
  2785.  
  2786.   /* Loop iterations is always called before any registers now, so this
  2787.      should never occur.  */
  2788.  
  2789.   if (REGNO (iteration_var) >= max_reg_before_loop)
  2790.     abort ();
  2791.  
  2792.   iteration_info (iteration_var, &initial_value, &increment,
  2793.           loop_start, loop_end);
  2794.   if (initial_value == 0)
  2795.     /* iteration_info already printed a message.  */
  2796.     return 0;
  2797.  
  2798.   if (increment == 0)
  2799.     {
  2800.       if (loop_dump_stream)
  2801.     fprintf (loop_dump_stream,
  2802.          "Loop unrolling: Increment value can't be calculated.\n");
  2803.       return 0;
  2804.     }
  2805.   if (GET_CODE (increment) != CONST_INT)
  2806.     {
  2807.       if (loop_dump_stream)
  2808.     fprintf (loop_dump_stream,
  2809.          "Loop unrolling: Increment value not constant.\n");
  2810.       return 0;
  2811.     }
  2812.   if (GET_CODE (initial_value) != CONST_INT)
  2813.     {
  2814.       if (loop_dump_stream)
  2815.     fprintf (loop_dump_stream,
  2816.          "Loop unrolling: Initial value not constant.\n");
  2817.       return 0;
  2818.     }
  2819.  
  2820.   /* If the comparison value is an invariant register, then try to find
  2821.      its value from the insns before the start of the loop.  */
  2822.  
  2823.   if (GET_CODE (comparison_value) == REG && invariant_p (comparison_value))
  2824.     {
  2825.       rtx insn, set;
  2826.     
  2827.       for (insn = PREV_INSN (loop_start); insn ; insn = PREV_INSN (insn))
  2828.     {
  2829.       if (GET_CODE (insn) == CODE_LABEL)
  2830.         break;
  2831.  
  2832.       else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  2833.            && (set = single_set (insn))
  2834.            && (SET_DEST (set) == comparison_value))
  2835.         {
  2836.           rtx note = find_reg_note (insn, REG_EQUAL, 0);
  2837.  
  2838.           if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
  2839.         comparison_value = XEXP (note, 0);
  2840.  
  2841.           break;
  2842.         }
  2843.     }
  2844.     }
  2845.  
  2846.   final_value = approx_final_value (comparison_code, comparison_value,
  2847.                     &unsigned_compare, &compare_dir);
  2848.  
  2849.   /* Save the calculated values describing this loop's bounds, in case
  2850.      precondition_loop_p will need them later.  These values can not be
  2851.      recalculated inside precondition_loop_p because strength reduction
  2852.      optimizations may obscure the loop's structure.  */
  2853.  
  2854.   loop_iteration_var = iteration_var;
  2855.   loop_initial_value = initial_value;
  2856.   loop_increment = increment;
  2857.   loop_final_value = final_value;
  2858.  
  2859.   if (final_value == 0)
  2860.     {
  2861.       if (loop_dump_stream)
  2862.     fprintf (loop_dump_stream,
  2863.          "Loop unrolling: EQ comparison loop.\n");
  2864.       return 0;
  2865.     }
  2866.   else if (GET_CODE (final_value) != CONST_INT)
  2867.     {
  2868.       if (loop_dump_stream)
  2869.     fprintf (loop_dump_stream,
  2870.          "Loop unrolling: Final value not constant.\n");
  2871.       return 0;
  2872.     }
  2873.  
  2874.   /* ?? Final value and initial value do not have to be constants.
  2875.      Only their difference has to be constant.  When the iteration variable
  2876.      is an array address, the final value and initial value might both
  2877.      be addresses with the same base but different constant offsets.
  2878.      Final value must be invariant for this to work.
  2879.  
  2880.      To do this, need someway to find the values of registers which are
  2881.      invariant.  */
  2882.  
  2883.   /* Final_larger is 1 if final larger, 0 if they are equal, otherwise -1.  */
  2884.   if (unsigned_compare)
  2885.     final_larger
  2886.       = ((unsigned) INTVAL (final_value) > (unsigned) INTVAL (initial_value)) -
  2887.     ((unsigned) INTVAL (final_value) < (unsigned) INTVAL (initial_value));
  2888.   else
  2889.     final_larger = (INTVAL (final_value) > INTVAL (initial_value)) -
  2890.       (INTVAL (final_value) < INTVAL (initial_value));
  2891.  
  2892.   if (INTVAL (increment) > 0)
  2893.     increment_dir = 1;
  2894.   else if (INTVAL (increment) == 0)
  2895.     increment_dir = 0;
  2896.   else
  2897.     increment_dir = -1;
  2898.  
  2899.   /* There are 27 different cases: compare_dir = -1, 0, 1;
  2900.      final_larger = -1, 0, 1; increment_dir = -1, 0, 1.
  2901.      There are 4 normal cases, 4 reverse cases (where the iteration variable
  2902.      will overflow before the loop exits), 4 infinite loop cases, and 15
  2903.      immediate exit (0 or 1 iteration depending on loop type) cases.
  2904.      Only try to optimize the normal cases.  */
  2905.      
  2906.   /* (compare_dir/final_larger/increment_dir)
  2907.      Normal cases: (0/-1/-1), (0/1/1), (-1/-1/-1), (1/1/1)
  2908.      Reverse cases: (0/-1/1), (0/1/-1), (-1/-1/1), (1/1/-1)
  2909.      Infinite loops: (0/-1/0), (0/1/0), (-1/-1/0), (1/1/0)
  2910.      Immediate exit: (0/0/X), (-1/0/X), (-1/1/X), (1/0/X), (1/-1/X) */
  2911.  
  2912.   /* ?? If the meaning of reverse loops (where the iteration variable
  2913.      will overflow before the loop exits) is undefined, then could
  2914.      eliminate all of these special checks, and just always assume
  2915.      the loops are normal/immediate/infinite.  Note that this means
  2916.      the sign of increment_dir does not have to be known.  Also,
  2917.      since it does not really hurt if immediate exit loops or infinite loops
  2918.      are optimized, then that case could be ignored also, and hence all
  2919.      loops can be optimized.
  2920.  
  2921.      According to ANSI Spec, the reverse loop case result is undefined,
  2922.      because the action on overflow is undefined.
  2923.  
  2924.      See also the special test for NE loops below.  */
  2925.  
  2926.   if (final_larger == increment_dir && final_larger != 0
  2927.       && (final_larger == compare_dir || compare_dir == 0))
  2928.     /* Normal case.  */
  2929.     ;
  2930.   else
  2931.     {
  2932.       if (loop_dump_stream)
  2933.     fprintf (loop_dump_stream,
  2934.          "Loop unrolling: Not normal loop.\n");
  2935.       return 0;
  2936.     }
  2937.  
  2938.   /* Calculate the number of iterations, final_value is only an approximation,
  2939.      so correct for that.  Note that tempu and loop_n_iterations are
  2940.      unsigned, because they can be as large as 2^n - 1.  */
  2941.  
  2942.   i = INTVAL (increment);
  2943.   if (i > 0)
  2944.     tempu = INTVAL (final_value) - INTVAL (initial_value);
  2945.   else if (i < 0)
  2946.     {
  2947.       tempu = INTVAL (initial_value) - INTVAL (final_value);
  2948.       i = -i;
  2949.     }
  2950.   else
  2951.     abort ();
  2952.  
  2953.   /* For NE tests, make sure that the iteration variable won't miss the
  2954.      final value.  If tempu mod i is not zero, then the iteration variable
  2955.      will overflow before the loop exits, and we can not calculate the
  2956.      number of iterations.  */
  2957.   if (compare_dir == 0 && (tempu % i) != 0)
  2958.     return 0;
  2959.  
  2960.   return tempu / i + ((tempu % i) != 0);
  2961. }
  2962.