home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / GCC 1.37.1r14 / usr / gcc-1.37.1r14 / (gcc-1.37.π) / loop.c < prev    next >
Encoding:
Text File  |  1993-07-01  |  153.0 KB  |  5,224 lines  |  [TEXT/KAHL]

  1. plied warranty of
  2. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  3. GNU General Public License for more details.
  4.  
  5. You should have received a copy of the GNU General Public License
  6. along with GNU CC; see the file COPYING.  If not, write to
  7. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  8.  
  9.  
  10. /* This is the loop optimization pass of the compiler.
  11.    It finds invariant computations within loops and moves them
  12.    to the beginning of the loop.  Then it identifies basic and 
  13.    general induction variables.  Strength reduction is applied to the general
  14.    induction variables, and induction variable elimination is applied to
  15.    the basic induction variables.
  16.  
  17.    It also finds cases where
  18.    a register is set within the loop by zero-extending a narrower value
  19.    and changes these to zero the entire register once before the loop
  20.    and merely copy the low part within the loop.
  21.  
  22.    Most of the complexity is in heuristics to decide when it is worth
  23.    while to do these things.  */
  24.  
  25. /* ??? verify_loop would run faster if we made one table
  26.    of the minimum and maximum luids from which each label is reached.
  27.    Also, it would be faster if loop_store_addrs were a hash table.  */
  28.  
  29. #include "config.h"
  30. #include "rtl.h"
  31. #include "expr.h"
  32. #include "insn-config.h"
  33. #include "regs.h"
  34. #include "hard-reg-set.h"
  35. #include "recog.h"
  36. #include "flags.h"
  37. #include <stdio.h>
  38.  
  39. /* Vector mapping INSN_UIDs to luids.
  40.    The luids are like uids but increase monononically always.
  41.    We use them to see whether a jump comes from outside a given loop.  */
  42.  
  43. static int *uid_luid;
  44.  
  45. /* Get the luid of an insn.  */
  46.  
  47. #define INSN_LUID(INSN) (uid_luid[INSN_UID (INSN)])
  48.  
  49. /* 1 + largest uid of any insn.  */
  50.  
  51. static int max_uid;
  52.  
  53. /* 1 + luid of last insn.  */
  54.  
  55. static int max_luid;
  56.  
  57. /* Nonzero if somewhere in the current loop
  58.    there is either a subroutine call,
  59.    or a store into a memory address that is not fixed,
  60.    or a store in a BLKmode memory operand,
  61.    or too many different fixed addresses stored in
  62.    to record them all in `loop_store_addrs'.
  63.  
  64.    In any of these cases, no memory location can be regarded
  65.    as invariant.  */
  66.  
  67. static int unknown_address_altered;
  68.  
  69. /* Nonzero if somewhere in the current loop there is a store
  70.    into a memory address that is not fixed but is known to be
  71.    part of an aggregate.
  72.  
  73.    In this case, no memory reference in an aggregate may be
  74.    considered invariant.  */
  75.  
  76. static int unknown_aggregate_altered;
  77.  
  78. /* Nonzero if somewhere in the current loop there is a store
  79.    into a memory address other than a fixed address not in an aggregate.
  80.  
  81.    In this case, no memory reference in an aggregate at a varying address
  82.    may be considered invariant.  */
  83.  
  84. static int fixed_aggregate_altered;
  85.  
  86. /* Nonzero if there is a subroutine call in the current loop.
  87.    (unknown_address_altered is also nonzero in this case.)  */
  88.  
  89. static int loop_has_call;
  90.  
  91. /* Added loop_continue which is the NOTE_INSN_LOOP_CONT of the
  92.    current loop.  A continue statement will generate a branch to
  93.    NEXT_INSN (loop_continue).  */
  94.  
  95. static rtx loop_continue;
  96.  
  97. /* Indexed by register number, contains the number of times the reg
  98.    is set during the loop being scanned.
  99.    During code motion, -1 indicates a reg that has been made a candidate.
  100.    After code motion, regs moved have 0 (which is accurate now)
  101.    while the failed candidates have the original number of times set.
  102.  
  103.    Therefore, at all times, 0 indicates an invariant register;
  104.    -1 a conditionally invariant one.  */
  105.  
  106. static short *n_times_set;
  107.  
  108. /* Original value of n_times_set; same except that this value
  109.    is not set to -1 for a reg whose sets have been made candidates
  110.    and not set to 0 for a reg that is moved.  */
  111.  
  112. static short *n_times_used;
  113.  
  114. /* Nonzero means reg N has already been moved out of one loop.
  115.    This reduces the desire to move it out of another.  */
  116.  
  117. static char *moved_once;
  118.  
  119. /* Array of fixed memory addresses that are stored in this loop.
  120.    If there are too many to fit here,
  121.    we just turn on unknown_address_altered.  */
  122.  
  123. #define NUM_STORES 10
  124. static rtx loop_store_addrs[NUM_STORES];
  125. static int loop_store_widths[NUM_STORES];
  126.  
  127. /* Index of first available slot in above array.  */
  128. static int loop_store_addrs_idx;
  129.  
  130. /* Count of movable (i.e. invariant) instructions discovered in the loop.  */
  131. static int num_movables;
  132.  
  133. /* Count of memory write instructions discovered in the loop.  */
  134. static int num_mem_sets;
  135.  
  136. /* Number of loops contained within the current one, including itself.  */
  137. static int loops_enclosed;
  138.  
  139. /* Bound on pseudo register number before loop optimization.
  140.    A pseudo has valid regscan info if its number is < old_max_reg.  */
  141. static int old_max_reg;
  142.  
  143. /* During the analysis of a loop, a chain of `struct movable's
  144.    is made to record all the movable insns found.
  145.    Then the entire chain can be scanned to decide which to move.  */
  146.  
  147. struct movable
  148. {
  149.   rtx insn;            /* A movable insn */
  150.   rtx set_src;                  /* The expression this reg is set from.
  151.                    Either SET_SRC (body) or a REG_EQUAL.  */
  152.   int consec;            /* Number of consecutive following insns 
  153.                    that must be moved with this one.  */
  154.   int regno;            /* The register it sets */
  155.   short lifetime;        /* lifetime of that register;
  156.                    may be adjusted when matching movables
  157.                    that load the same value are found.  */
  158.   short savings;        /* Number of insns we can move for this reg,
  159.                    including other movables that force this
  160.                    or match this one.  */
  161.   unsigned int cond : 1;    /* 1 if only conditionally movable */
  162.   unsigned int force : 1;    /* 1 means MUST move this insn */
  163.   unsigned int global : 1;    /* 1 means reg is live outside this loop */
  164.         /* If PARTIAL is 1, GLOBAL means something different:
  165.            that the reg is live outside the range from where it is set
  166.            to the following label.  */
  167.   unsigned int done : 1;    /* 1 inhibits further processing of this */
  168.   /* 1 in PARTIAL means this reg is used for zero-extending.
  169.      In particular, moving it does not make it invariant.  */
  170.   unsigned int partial : 1;
  171.   enum machine_mode savemode;   /* Nonzero means it is a mode for a low part
  172.                    that we should avoid changing when clearing
  173.                    the rest of the reg.  */
  174.   struct movable *match;    /* First entry for same value */
  175.   struct movable *forces;    /* An insn that must be moved if this is */
  176.   struct movable *next;
  177. };
  178.  
  179. static FILE *loop_dump_stream;
  180.  
  181. /* Forward declarations.  */
  182.  
  183. struct induction;
  184. struct iv_class;
  185.  
  186. static rtx verify_loop ();
  187. static int invariant_p ();
  188. static int consec_sets_invariant_p ();
  189. static int can_jump_into_range_p ();
  190. static int labels_in_range_p ();
  191. static void count_loop_regs_set ();
  192. static void note_addr_stored ();
  193. static int loop_reg_used_before_p ();
  194. static void constant_high_bytes ();
  195. static void scan_loop ();
  196. static rtx replace_regs ();
  197. static void replace_call_address ();
  198. static rtx skip_consec_insns ();
  199. static void ignore_some_movables ();
  200. static void force_movables ();
  201. static void combine_movables ();
  202. static int rtx_equal_for_loop_p ();
  203. static void move_movables ();
  204. static void strength_reduce ();
  205. static void find_mem_givs ();
  206. static void record_giv ();
  207. static void delete_insn_forces ();
  208. static int basic_induction_var ();
  209. static int general_induction_var ();
  210. static int consec_sets_giv ();
  211. static int check_dbra_loop ();
  212. static void emit_iv_init_code ();
  213. static int product_cheap_p ();
  214. static void emit_iv_inc ();
  215. static void check_eliminate_biv ();
  216. static int can_eliminate_biv_p ();
  217. static void eliminate_biv ();
  218. static rtx final_biv_value ();
  219. static int last_use_this_basic_block ();
  220.  
  221. /* Entry point of this file.  Perform loop optimization
  222.    on the current function.  F is the first insn of the function
  223.    and DUMPFILE is a stream for output of a trace of actions taken
  224.    (or 0 if none should be output).  */
  225.  
  226. void
  227. loop_optimize (f, dumpfile)
  228.      /* f is the first instruction of a chain of insns for one function */
  229.      rtx f;
  230.      FILE *dumpfile;
  231. {
  232.   register rtx insn;
  233.   register int i;
  234.   rtx end;
  235.   rtx last_insn;
  236.  
  237.   loop_dump_stream = dumpfile;
  238.  
  239. #if 0 /* we hate it */
  240.   /* MPW C -m can't deal with this code at all... */
  241.   init_recog ();
  242.  
  243.   old_max_reg = max_reg_num ();
  244.  
  245.   moved_once = (char *) alloca (old_max_reg);
  246.   bzero (moved_once, old_max_reg);
  247.  
  248.   /* First find the last real insn, and count the number of insns,
  249.      and assign insns their luids.  */
  250.  
  251.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  252.     if (INSN_UID (insn) > i)
  253.       i = INSN_UID (insn);
  254.  
  255.   max_uid = i + 1;
  256.   uid_luid = (int *) alloca ((i + 1) * sizeof (int));
  257.   bzero (uid_luid, (i + 1) * sizeof (int));
  258.  
  259.   /* Compute the mapping from uids to luids.
  260.      LUIDs are numbers assigned to insns, like uids,
  261.      except that luids increase monotonically through the code.
  262.      Don't assign luids to line-number NOTEs, so that the distance in luids
  263.      between two insns is not affected by -g.  */
  264.  
  265.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  266.     {
  267.       last_insn = insn;
  268.       if (GET_CODE (insn) != NOTE
  269.       || NOTE_LINE_NUMBER (insn) < 0)
  270.     INSN_LUID (insn) = ++i;
  271.       else
  272.     /* Give a line number note the same luid as preceding insn.  */
  273.     INSN_LUID (insn) = i;
  274.     }
  275.  
  276.   max_luid = i;
  277.  
  278.   /* Don't leave gaps in uid_luid for insns that have been
  279.      deleted.  It is possible that the first or last insn
  280.      using some register has been deleted by cross-jumping.
  281.      Make sure that uid_luid for that former insn's uid
  282.      points to the general area where that insn used to be.  */
  283.   for (i = 0; i < max_uid; i++)
  284.     {
  285.       uid_luid[0] = uid_luid[i];
  286.       if (uid_luid[0] != 0)
  287.     break;
  288.     }
  289.   for (i = 0; i < max_uid; i++)
  290.     if (uid_luid[i] == 0)
  291.       uid_luid[i] = uid_luid[i - 1];
  292.  
  293.   /* Find and process each loop.
  294.      We scan from the end, and process each loop when its start is seen,
  295.      so we process innermost loops first.  */
  296.  
  297.   for (insn = last_insn; insn; insn = PREV_INSN (insn))
  298.     if (GET_CODE (insn) == NOTE
  299.     && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  300.       {
  301.     /* Make sure it really is a loop -- no jumps in from outside.  */
  302.     end = verify_loop (f, insn);
  303.     if (end != 0)
  304.       /* If so, optimize this loop.  */
  305.       scan_loop (insn, end, max_reg_num ());
  306.     else if (loop_dump_stream)
  307.       fprintf (loop_dump_stream,
  308.            "\nLoop at %d ignored due to multiple entry points.\n",
  309.            INSN_UID (insn));
  310.       }
  311. }
  312.  
  313. /* Optimize one loop whose start is LOOP_START and end is END.
  314.    LOOP_START is the NOTE_INSN_LOOP_BEG and END is the matching
  315.    NOTE_INSN_LOOP_END.  */
  316.  
  317. /* ??? can also move memory writes out of loop if destination
  318.    address is invariant? */
  319.  
  320. static void
  321. scan_loop (loop_start, end, nregs)
  322.      rtx loop_start, end;
  323.      int nregs;
  324. {
  325.   register int i;
  326.   register rtx p = NEXT_INSN (loop_start);
  327.   /* 1 if we are scanning insns that could be executed zero times.  */
  328.   int maybe_never = 0;
  329.   /* 1 if we are scanning insns that might never be executed
  330.      due to a subroutine call which might exit before they are reached.  */
  331.   int call_passed = 0;
  332.   /* For a rotated loop that is entered near the bottom,
  333.      this is the label at the top.  Otherwise it is zero.  */
  334.   rtx loop_top = 0;
  335.   /* Jump insn that enters the loop, or 0 if control drops in.  */
  336.   rtx loop_entry_jump = 0;
  337.   /* Place in the loop where control enters.  */
  338.   rtx scan_start;
  339.   /* Number of insns in the loop.  */
  340.   int insn_count;
  341.   int tem;
  342.   rtx temp;
  343.   /* Indexed by register number, contains 1 for a register whose
  344.      assignments may not be moved out of the loop.  */
  345.   char *may_not_move;
  346.   /* Chain describing insns movable in current loop.  */
  347.   struct movable *movables = 0;
  348.   /* Last element in `movables' -- so we can add elements at the end.  */
  349.   struct movable *last_movable = 0;
  350.   /* Ratio of extra register life span we can justify
  351.      for saving an instruction.  More if loop doesn't call subroutines
  352.      since in that case saving an insn makes more difference
  353.      and more registers are available.  */
  354.   int threshold = loop_has_call ? 15 : 30;
  355.   /* Nonzero if the insn that jumps into the real loop
  356.      is not the very first thing after the loop-beginning note.  */
  357.   int something_before_entry_jump = 0;
  358.  
  359.   n_times_set = (short *) alloca (nregs * sizeof (short));
  360.   n_times_used = (short *) alloca (nregs * sizeof (short));
  361.   may_not_move = (char *) alloca (nregs);
  362.  
  363.   /* Determine whether this loop starts with a jump down
  364.      to a test at the end.  */
  365.   while (p != end
  366.      && GET_CODE (p) != CODE_LABEL && GET_CODE (p) != JUMP_INSN)
  367.     {
  368.       if (GET_CODE (p) == CALL_INSN || GET_CODE (p) == INSN)
  369.     something_before_entry_jump = 1;
  370.       p = NEXT_INSN (p);
  371.     }
  372.  
  373.   /* "Loop" contains neither jumps nor labels;
  374.      it must have been a dummy.  Think no more about it.  */
  375.   if (p == end)
  376.     return;
  377.  
  378.   scan_start = p;
  379.  
  380.   /* If loop has a jump before the first label,
  381.      the true entry is the target of that jump.
  382.      Start scan from there.
  383.      But record in LOOP_TOP the place where the end-test jumps
  384.      back to so we can scan that after the end of the loop.  */
  385.   if (GET_CODE (p) == JUMP_INSN)
  386.     {
  387.       loop_entry_jump = p;
  388.       loop_top = NEXT_INSN (p);
  389.       /* Loop entry will never be a conditional jump.
  390.      If we see one, this must not be a real loop.
  391.      Also, a return-insn isn't a jump to enter the loop.  */
  392.       if (GET_CODE (loop_top) != BARRIER
  393.       || GET_CODE (PATTERN (p)) != SET)
  394.     return;
  395.       /* Get the label at which the loop is entered.  */
  396.       p = XEXP (SET_SRC (PATTERN (p)), 0);
  397.       /* Check to see whether the jump actually
  398.      jumps out of the loop (meaning it's no loop).
  399.      This case can happen for things like
  400.      do {..} while (0).  */
  401.       if (p == 0
  402.       || INSN_LUID (p) < INSN_LUID (loop_start)
  403.       || INSN_LUID (p) >= INSN_LUID (end))
  404.     {
  405.       if (loop_dump_stream)
  406.         fprintf (loop_dump_stream, "\nLoop from %d to %d is phony.\n\n",
  407.              INSN_UID (loop_start), INSN_UID (end));
  408.       return;
  409.     }
  410.  
  411.       /* Find the first label after the entry-jump.  */
  412.       while (GET_CODE (loop_top) != CODE_LABEL)
  413.     {
  414.       loop_top = NEXT_INSN (loop_top);
  415.       if (loop_top == 0)
  416.         abort ();
  417.     }
  418.  
  419.       /* Maybe rearrange the loop to drop straight in
  420.      with a new test to jump around it entirely.
  421.      (The latter is considered outside the loop.)
  422.      If this is done, we no longer enter with a jump.  */
  423.       if (! something_before_entry_jump
  424.       && loop_skip_over (loop_start, end, loop_entry_jump))
  425.     {
  426.       scan_start = loop_top;
  427.       loop_top = 0;
  428.     }
  429.       else
  430.     /* We really do enter with a jump;
  431.        scan the loop from the place where the jump jumps to.  */
  432.     scan_start = p;
  433.     }
  434.  
  435.   /* Count number of times each reg is set during this loop.
  436.      Set MAY_NOT_MOVE[I] if it is not safe to move out
  437.      the setting of register I.  */
  438.  
  439.   bzero (n_times_set, nregs * sizeof (short));
  440.   bzero (may_not_move, nregs);
  441.   count_loop_regs_set (loop_top ? loop_top : loop_start, end,
  442.                may_not_move, &insn_count, nregs);
  443.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  444.     may_not_move[i] = 1, n_times_set[i] = 1;
  445.   bcopy (n_times_set, n_times_used, nregs * sizeof (short));
  446.  
  447.   if (loop_dump_stream)
  448.     {
  449.       fprintf (loop_dump_stream, "\nLoop from %d to %d: %d real insns.\n",
  450.            INSN_UID (loop_start), INSN_UID (end), insn_count);
  451.       if (loop_continue)
  452.     fprintf (loop_dump_stream, "Continue at insn %d.\n",
  453.          INSN_UID (loop_continue));
  454.     }
  455.  
  456.   /* Scan through the loop finding insns that are safe to move.
  457.      In each such insn, store QImode as the mode, to mark it.
  458.      Then set n_times_set to -1 for the reg being set, so that
  459.      this reg will be considered invariant for subsequent insns.
  460.      We consider whether subsequent insns use the reg
  461.      in deciding whether it is worth actually moving.
  462.  
  463.      MAYBE_NEVER is nonzero if we have passed a conditional jump insn
  464.      and therefore it is possible that the insns we are scanning
  465.      would never be executed.  At such times, we must make sure
  466.      that it is safe to execute the insn once instead of zero times.
  467.      When MAYBE_NEVER is 0, all insns will be executed at least once
  468.      so that is not a problem.  */
  469.  
  470.   p = scan_start;
  471.   while (1)
  472.     {
  473.       p = NEXT_INSN (p);
  474.       /* At end of a straight-in loop, we are done.
  475.      At end of a loop entered at the bottom, scan the top.  */
  476.       if (p == scan_start)
  477.     break;
  478.       if (p == end)
  479.     {
  480.       if (loop_top != 0)
  481.         p = NEXT_INSN (loop_top);
  482.       else
  483.         break;
  484.       if (p == scan_start)
  485.         break;
  486.     }
  487.       if (GET_CODE (p) == INSN
  488.       && GET_CODE (PATTERN (p)) == SET
  489.       && GET_CODE (SET_DEST (PATTERN (p))) == REG
  490.       && ! may_not_move[REGNO (SET_DEST (PATTERN (p)))])
  491.     {
  492.       int tem1 = 0;
  493.       /* Don't try to optimize a register that was made
  494.          by loop-optimization for an inner loop.
  495.          We don't know its life-span, so we can't compute the benefit.  */
  496.       if (REGNO (SET_DEST (PATTERN (p))) >= old_max_reg)
  497.         ;
  498.       /* If this register is used or set outside the loop,
  499.          then we can move it only if we know this insn is
  500.          executed exactly once per iteration,
  501.          and we can check all the insns executed before it
  502.          to make sure none of them used the value that
  503.          was lying around at entry to the loop.  */
  504.       else if ((uid_luid[regno_last_uid[REGNO (SET_DEST (PATTERN (p)))]] > INSN_LUID (end)
  505.             || uid_luid[regno_first_uid[REGNO (SET_DEST (PATTERN (p)))]] < INSN_LUID (loop_start))
  506.            && (maybe_never
  507.                || loop_reg_used_before_p (p, loop_start, scan_start, end)))
  508.         ;
  509.       else if (((tem = invariant_p (SET_SRC (PATTERN (p))))
  510.             || ((temp = find_reg_note (p, REG_EQUAL, 0)) 
  511.             && (tem = invariant_p (XEXP (temp, 0)))))
  512.            && (n_times_set[REGNO (SET_DEST (PATTERN (p)))] == 1
  513.                || (tem1
  514.                = consec_sets_invariant_p (SET_DEST (PATTERN (p)),
  515.                               n_times_set[REGNO (SET_DEST (PATTERN (p)))],
  516.                               p)))
  517.            /* If the insn can cause a trap (such as divide by zero),
  518.               can't move it unless it's guaranteed to be executed
  519.               once loop is entered.  Even a function call might
  520.               prevent the trap insn from being reached
  521.               (since it might exit!)  */
  522.            && ! ((maybe_never || call_passed)
  523.              && (may_trap_p (SET_SRC (PATTERN (p)))
  524.                  || ((temp = find_reg_note (p, REG_EQUAL, 0))
  525.                  && may_trap_p (XEXP (temp, 0))))))
  526.         {
  527.           register struct movable *m;
  528.           register int regno = REGNO (SET_DEST (PATTERN (p)));
  529.           int count;
  530.           m = (struct movable *) alloca (sizeof (struct movable));
  531.           m->next = 0;
  532.           m->insn = p;
  533.           temp = find_reg_note (p, REG_EQUAL, 0);
  534.           if (temp)
  535.         m->set_src = XEXP (temp, 0);
  536.           else
  537.         m->set_src = SET_SRC (PATTERN (p));
  538.           m->force = 0;
  539.           m->consec = n_times_set[REGNO (SET_DEST (PATTERN (p)))] - 1;
  540.           m->done = 0;
  541.           m->forces = 0;
  542.           m->partial = 0;
  543.           m->savemode = VOIDmode;
  544.           m->regno = regno;
  545.           /* Set M->cond if either invariant_p or consec_sets_invariant_p
  546.          returned 2 (only conditionally invariant).  */
  547.           m->cond = ((tem | tem1) > 1);
  548.           m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
  549.                || uid_luid[regno_first_uid[regno]] < INSN_LUID (loop_start));
  550.           m->match = 0;
  551.           m->lifetime = (uid_luid[regno_last_uid[regno]]
  552.                  - uid_luid[regno_first_uid[regno]]);
  553.           m->savings = n_times_used[regno];
  554.           n_times_set[regno] = -1;
  555.           /* Add M to the end of the chain MOVABLES.  */
  556.           if (movables == 0)
  557.         movables = m;
  558.           else
  559.         last_movable->next = m;
  560.           last_movable = m;
  561.           if (m->consec > 0)
  562.         {
  563.           /* Skip this insn, not checking REG_LIBCALL notes.  */
  564.           p = NEXT_INSN (p);
  565.           /* Skip the consecutive insns, if there are any.  */
  566.           p = skip_consec_insns (p, m->consec);
  567.           /* Back up, so the main loop will advance to the right place.  */
  568.           p = PREV_INSN (p);
  569.         }
  570.         }
  571.       /* If this register is always set within a STRICT_LOW_PART
  572.          or set to zero, then its high bytes are constant.
  573.          So clear them outside the loop and within the loop
  574.          just load the low bytes.
  575.          We must check that the machine has an instruction to do so.
  576.          Also, if the value loaded into the register
  577.          depends on the same register, this cannot be done.  */
  578.       else if (SET_SRC (PATTERN (p)) == const0_rtx
  579.            && GET_CODE (NEXT_INSN (p)) == INSN
  580.            && GET_CODE (PATTERN (NEXT_INSN (p))) == SET
  581.            && (GET_CODE (SET_DEST (PATTERN (NEXT_INSN (p))))
  582.                == STRICT_LOW_PART)
  583.            && (GET_CODE (XEXP (SET_DEST (PATTERN (NEXT_INSN (p))), 0))
  584.                == SUBREG)
  585.            && (SUBREG_REG (XEXP (SET_DEST (PATTERN (NEXT_INSN (p))), 0))
  586.                == SET_DEST (PATTERN (p)))
  587.            && !reg_mentioned_p (SET_DEST (PATTERN (p)),
  588.                     SET_SRC (PATTERN (NEXT_INSN (p)))))
  589.         {
  590.           register int regno = REGNO (SET_DEST (PATTERN (p)));
  591.           if (n_times_set[regno] == 2)
  592.         {
  593.           register struct movable *m;
  594.           int count;
  595.           m = (struct movable *) alloca (sizeof (struct movable));
  596.           m->next = 0;
  597.           m->insn = p;
  598.           m->force = 0;
  599.           m->consec = 0;
  600.           m->done = 0;
  601.           m->forces = 0;
  602.           m->partial = 1;
  603.           /* If the insn may not be executed on some cycles,
  604.              we can't clear the whole reg; clear just high part.
  605.              Not even if the reg is used only within this loop.
  606.              Consider this:
  607.              while (1)
  608.                while (s != t) {
  609.                  if (foo ()) x = *s;
  610.              use (x);
  611.                }
  612.              Clearing x before the inner loop could clobber a value
  613.              being saved from the last time around the outer loop.
  614.              However, if the reg is not used outside this loop
  615.              and all uses of the register are in the same
  616.              basic block as the store, there is no problem.  */
  617.           m->global = (uid_luid[regno_last_uid[regno]] > INSN_LUID (end)
  618.                    || uid_luid[regno_first_uid[regno]] < INSN_LUID (p)
  619.                    || (labels_in_range_p
  620.                    (p, uid_luid[regno_first_uid[regno]])));
  621.           if (maybe_never && m->global)
  622.             m->savemode = GET_MODE (SET_SRC (PATTERN (NEXT_INSN (p))));
  623.           else
  624.             m->savemode = VOIDmode;
  625.           m->regno = regno;
  626.           m->cond = 0;
  627.           m->match = 0;
  628.           m->lifetime = (uid_luid[regno_last_uid[regno]]
  629.                  - uid_luid[regno_first_uid[regno]]);
  630.           m->savings = 1;
  631.           n_times_set[regno] = -1;
  632.           /* Add M to the end of the chain MOVABLES.  */
  633.           if (movables == 0)
  634.             movables = m;
  635.           else
  636.             last_movable->next = m;
  637.           last_movable = m;
  638.         }
  639.         }
  640.     }
  641.       /* Past a call insn, we get to insns which might not be executed
  642.      because the call might exit.  This matters for insns that trap.  */
  643.       else if (GET_CODE (p) == CALL_INSN)
  644.     call_passed = 1;
  645.       /* Past a label or a jump, we get to insns for which we
  646.      can't count on whether or how many times they will be
  647.      executed during each iteration.  Therefore, we can
  648.      only move out sets of trivial variables
  649.      (those not used after the loop).  */
  650.       else if ((GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
  651.            /* If we enter the loop in the middle, and scan around
  652.           to the beginning, don't set maybe_never for that.  */
  653.                && ! (NEXT_INSN (p) == end && GET_CODE (p) == JUMP_INSN
  654.                      && simplejump_p (p)))
  655.     maybe_never = 1;
  656.     }
  657.  
  658.   /* If one movable subsumes another, ignore that other.  */
  659.  
  660.   ignore_some_movables (movables);
  661.  
  662.   /* For each movable insn, see if the reg that it loads
  663.      leads when it dies right into another conditionally movable insn.
  664.      If so, record that the second insn "forces" the first one,
  665.      since the second can be moved only if the first is.  */
  666.  
  667.   force_movables (movables);
  668.  
  669.   /* See if there are multiple movable insns that load the same value.
  670.      If there are, make all but the first point at the first one
  671.      through the `match' field, and add the priorities of them
  672.      all together as the priority of the first.  */
  673.  
  674.   combine_movables (movables, nregs);
  675.     
  676.   /* Now consider each movable insn to decide whether it is worth moving.
  677.      Store 0 in n_times_set for each reg that is moved.  */
  678.  
  679.   move_movables (movables, threshold,
  680.          insn_count, loop_start, end, nregs);
  681.  
  682.   /* Now candidates that still have -1 are those not moved.
  683.      Change n_times_set to indicate that those are not actually invariant.  */
  684.   for (i = 0; i < nregs; i++)
  685.     if (n_times_set[i] < 0)
  686.       n_times_set[i] = n_times_used[i];
  687.  
  688.   if (flag_strength_reduce)
  689.     strength_reduce (scan_start, end, loop_top,
  690.              insn_count, loop_start, end, nregs);
  691. }
  692.  
  693. /* Skip COUNT insns from INSN, counting library calls as 1 insn.  */
  694.  
  695. static rtx
  696. skip_consec_insns (insn, count)
  697.      rtx insn;
  698.      int count;
  699. {
  700.   for (; count > 0; count--)
  701.     {
  702.       if (GET_CODE (insn) == NOTE)
  703.     insn = NEXT_INSN (insn);
  704.       else if (GET_CODE (insn) == BARRIER || GET_CODE (insn) == CODE_LABEL)
  705.     abort ();
  706.       else
  707.     {
  708.       rtx i1, temp;
  709.  
  710.       /* If first insn of gnulib call sequence, skip to end.  */
  711.       /* Do this at start of loop, since INSN is guaranteed to 
  712.          be an insn here.  */
  713.       if (temp = find_reg_note (insn, REG_LIBCALL, 0))
  714.         insn = XEXP (temp, 0);
  715.  
  716.       do insn = NEXT_INSN (insn);
  717.       while (GET_CODE (insn) == NOTE);
  718.     }
  719.     }
  720.  
  721.   return insn;
  722. }
  723.  
  724. /* Ignore any movable whose insn falls within a libcall
  725.    which is part of another movable.
  726.    We make use of the fact that the movable for the libcall value
  727.    was made later and so appears later on the chain.  */
  728.  
  729. static void
  730. ignore_some_movables (movables)
  731.      struct movable *movables;
  732. {
  733.   register struct movable *m, *m1;
  734.  
  735.   for (m = movables; m; m = m->next)
  736.     {
  737.       /* Is this a movable for the value of a libcall?  */
  738.       rtx note = find_reg_note (m->insn, REG_RETVAL, 0);
  739.       if (note)
  740.     {
  741.       /* Find the beginning of that libcall.  */
  742.       rtx first_insn = XEXP (note, 0);
  743.       /* Check for earlier movables inside that range,
  744.          and mark them invalid.  */
  745.       for (m1 = movables; m1 != m; m1 = m1->next)
  746.         if (INSN_LUID (m1->insn) >= INSN_LUID (first_insn)
  747.         && INSN_LUID (m1->insn) < INSN_LUID (m->insn))
  748.           m1->done = 1;
  749.     }
  750.     }
  751. }      
  752.  
  753. /* For each movable insn, see if the reg that it loads
  754.    leads when it dies right into another conditionally movable insn.
  755.    If so, record that the second insn "forces" the first one,
  756.    since the second can be moved only if the first is.  */
  757.  
  758. static void
  759. force_movables (movables)
  760.      struct movable *movables;
  761. {
  762.   register struct movable *m, *m1;
  763.   for (m1 = movables; m1; m1 = m1->next)
  764.     /* Omit this if moving just the (SET (REG) 0) of a zero-extend.  */
  765.     if (!m1->partial && !m1->done)
  766.       {
  767.     int regno = m1->regno;
  768.     for (m = m1->next; m; m = m->next)
  769.       /* ??? Could this be a bug?  What if CSE caused the
  770.          register of M1 to be used after this insn?
  771.          Since CSE does not update regno_last_uid,
  772.          this insn M->insn might not be where it dies.
  773.          But very likely this doesn't matter; what matters is
  774.          that M's reg is computed from M1's reg.  */
  775.       if (INSN_UID (m->insn) == regno_last_uid[regno]
  776.           && !m->done)
  777.         break;
  778.     if (m != 0 && m->set_src == SET_DEST (PATTERN (m1->insn)))
  779.       m = 0;
  780.  
  781.     /* Increase the priority of the moving the first insn
  782.        since it permits the second to be moved as well.  */
  783.     if (m != 0)
  784.       {
  785.         m->forces = m1;
  786.         m1->lifetime += m->lifetime;
  787.         m1->savings += m1->savings;
  788.       }
  789.       }
  790. }
  791.  
  792. /* Find invariant expressions that are equal and can be combined into
  793.    one register.  */
  794.  
  795. static void
  796. combine_movables (movables, nregs)
  797.      struct movable *movables;
  798.      int nregs;
  799. {
  800.   register struct movable *m;
  801.   char *matched_regs = (char *) alloca (nregs);
  802.   enum machine_mode mode;
  803.  
  804.   /* Regs that are set more than once are not allowed to match
  805.      or be matched.  I'm no longer sure why not.  */
  806.   /* Perhaps testing m->consec_sets would be more appropriate here?  */
  807.  
  808.   for (m = movables; m; m = m->next)
  809.     if (m->match == 0 && n_times_used[m->regno] == 1 && !m->partial)
  810.       {
  811.     register struct movable *m1;
  812.     int regno = m->regno;
  813.  
  814.     bzero (matched_regs, nregs);
  815.     matched_regs[regno] = 1;
  816.  
  817.     for (m1 = m->next; m1; m1 = m1->next)
  818.       if (m1->match == 0 && n_times_used[m1->regno] == 1
  819.           /* A reg used outside the loop mustn't be eliminated.  */
  820.           && !m1->global
  821.           /* A reg used for zero-extending mustn't be eliminated.  */
  822.           && !m1->partial
  823.           && (matched_regs[m1->regno]
  824.           ||
  825.           (
  826.            /* Can't combine regs with different modes
  827.               even if loaded from the same constant.  */
  828.            (GET_MODE (SET_DEST (PATTERN (m->insn)))
  829.             == GET_MODE (SET_DEST (PATTERN (m1->insn))))
  830.            /* See if the source of M1 says it matches M.  */
  831.            && ((GET_CODE (m1->set_src) == REG
  832.             && matched_regs[REGNO (m1->set_src)])
  833.                || rtx_equal_for_loop_p (m->set_src, m1->set_src,
  834.                         movables)
  835.                || (REG_NOTES (m->insn) && REG_NOTES (m1->insn)
  836.                && REG_NOTE_KIND (REG_NOTES (m->insn)) == REG_EQUIV
  837.                && REG_NOTE_KIND (REG_NOTES (m1->insn)) == REG_EQUIV
  838.                && rtx_equal_p (XEXP (REG_NOTES (m->insn), 0),
  839.                        XEXP (REG_NOTES (m1->insn), 0)))))))
  840.         {
  841.           m->lifetime += m1->lifetime;
  842.           m->savings += m1->savings;
  843.           m1->match = m;
  844.           matched_regs[m1->regno] = 1;
  845.         }
  846.       }
  847.  
  848.   /* Now combine the regs used for zero-extension.
  849.      This can be done for those not marked `global'
  850.      provided their lives don't overlap.  */
  851.  
  852.   for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
  853.        mode = (enum machine_mode) ((int) mode + 1))
  854.     if (GET_MODE_CLASS (mode) == MODE_INT)
  855.       {
  856.     register struct movable *m0 = 0;
  857.  
  858.     /* Combine all the registers for extension from mode MODE.
  859.        Don't combine any that are used outside this loop.  */
  860.     for (m = movables; m; m = m->next)
  861.       if (m->partial && ! m->global
  862.           && mode == GET_MODE (SET_SRC (PATTERN (NEXT_INSN (m->insn)))))
  863.         {
  864.           register struct movable *m1;
  865.           int first = uid_luid[regno_first_uid[m->regno]];
  866.           int last = uid_luid[regno_last_uid[m->regno]];
  867.  
  868.           if (m0 == 0)
  869.         {
  870.           /* First one: don't check for overlap, just record it.  */
  871.           m0 = m;
  872.           continue;
  873.         }
  874.  
  875.           /* Make sure they extend to the same mode.
  876.          (Almost always true.)  */
  877.           if (GET_MODE (SET_DEST (PATTERN (m->insn)))
  878.           != GET_MODE (SET_DEST (PATTERN (m0->insn))))
  879.         continue;
  880.  
  881.           /* We already have one: check for overlap with those
  882.          already combined together.  */
  883.           for (m1 = movables; m1 != m; m1 = m1->next)
  884.         if (m1 == m0 || (m1->partial && m1->match == m0))
  885.           if (! (uid_luid[regno_first_uid[m1->regno]] > last
  886.              || uid_luid[regno_last_uid[m1->regno]] < first))
  887.             goto overlap;
  888.  
  889.           /* No overlap: we can combine this with the others.  */
  890.           m0->lifetime += m->lifetime;
  891.           m0->savings += m->savings;
  892.           m->match = m0;
  893.  
  894.         overlap: ;
  895.         }
  896.       }
  897. }
  898.  
  899. /* Return 1 if regs X and Y will become the same if moved.  */
  900.  
  901. static int
  902. regs_match_p (x, y, movables)
  903.      rtx x, y;
  904.      struct movable *movables;
  905. {
  906.   int xn = REGNO (x);
  907.   int yn = REGNO (y);
  908.   struct movable *mx, *my;
  909.  
  910.   for (mx = movables; mx; mx = mx->next)
  911.     if (mx->regno == xn)
  912.       break;
  913.  
  914.   for (my = movables; my; my = my->next)
  915.     if (my->regno == yn)
  916.       break;
  917.  
  918.   return (mx && my
  919.       && ((mx->match == my->match && mx->match != 0)
  920.           || mx->match == my
  921.           || mx == my->match));
  922. }
  923.  
  924. /* Return 1 if X and Y are identical-looking rtx's.
  925.    This is the Lisp function EQUAL for rtx arguments.  */
  926.  
  927. static int
  928. rtx_equal_for_loop_p (x, y, movables)
  929.      rtx x, y;
  930.      struct movable *movables;
  931. {
  932.   register int i;
  933.   register int j;
  934.   register enum rtx_code code;
  935.   register char *fmt;
  936.  
  937.   if (x == y)
  938.     return 1;
  939.   if (x == 0 || y == 0)
  940.     return 0;
  941.  
  942.   code = GET_CODE (x);
  943.   /* Rtx's of different codes cannot be equal.  */
  944.   if (code != GET_CODE (y))
  945.     return 0;
  946.  
  947.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
  948.      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
  949.  
  950.   if (GET_MODE (x) != GET_MODE (y))
  951.     return 0;
  952.  
  953.   /* These three types of rtx's can be compared nonrecursively.  */
  954.   /* Until the end of reload,
  955.      don't consider the a reference to the return register of the current
  956.      function the same as the return from a called function.  This eases
  957.      the job of function integration.  Once the distinction no longer
  958.      matters, the insn will be deleted.  */
  959.   if (code == REG)
  960.     return ((REGNO (x) == REGNO (y)
  961.          && REG_FUNCTION_VALUE_P (x) == REG_FUNCTION_VALUE_P (y))
  962.         || regs_match_p (x, y, movables));
  963.  
  964.   if (code == LABEL_REF)
  965.     return XEXP (x, 0) == XEXP (y, 0);
  966.   if (code == SYMBOL_REF)
  967.     return XSTR (x, 0) == XSTR (y, 0);
  968.  
  969.   /* Compare the elements.  If any pair of corresponding elements
  970.      fail to match, return 0 for the whole things.  */
  971.  
  972.   fmt = GET_RTX_FORMAT (code);
  973.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  974.     {
  975.       switch (fmt[i])
  976.     {
  977.     case 'i':
  978.       if (XINT (x, i) != XINT (y, i))
  979.         return 0;
  980.       break;
  981.  
  982.     case 'E':
  983.       /* Two vectors must have the same length.  */
  984.       if (XVECLEN (x, i) != XVECLEN (y, i))
  985.         return 0;
  986.  
  987.       /* And the corresponding elements must match.  */
  988.       for (j = 0; j < XVECLEN (x, i); j++)
  989.         if (rtx_equal_for_loop_p (XVECEXP (x, i, j), XVECEXP (y, i, j), movables) == 0)
  990.           return 0;
  991.       break;
  992.  
  993.     case 'e':
  994.       if (rtx_equal_for_loop_p (XEXP (x, i), XEXP (y, i), movables) == 0)
  995.         return 0;
  996.       break;
  997.  
  998.     case 's':
  999.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  1000.         return 0;
  1001.       break;
  1002.  
  1003.     case 'u':
  1004.       /* These are just backpointers, so they don't matter.  */
  1005.       break;
  1006.  
  1007.     case '0':
  1008.       break;
  1009.  
  1010.       /* It is believed that rtx's at this level will never
  1011.          contain anything but integers and other rtx's,
  1012.          except for within LABEL_REFs and SYMBOL_REFs.  */
  1013.     default:
  1014.       abort ();
  1015.     }
  1016.     }
  1017.   return 1;
  1018. }
  1019.  
  1020. /* Scan MOVABLES, and move the insns that deserve to be moved.
  1021.    If two matching movables are combined, replace one reg with the
  1022.    other throughout.  */
  1023.  
  1024. static void
  1025. move_movables (movables, threshold, insn_count, loop_start, end, nregs)
  1026.      struct movable *movables;
  1027.      int threshold;
  1028.      int insn_count;
  1029.      rtx loop_start;
  1030.      rtx end;
  1031.      int nregs;
  1032. {
  1033.   rtx new_start = 0;
  1034.   register struct movable *m;
  1035.   register rtx p;
  1036.   /* Map of pseudo-register replacements to handle combining
  1037.      when we move several insns that load the same value
  1038.      into different pseudo-registers.  */
  1039.   rtx *reg_map = (rtx *) alloca (nregs * sizeof (rtx));
  1040.   char *already_moved = (char *) alloca (nregs);
  1041.  
  1042.   bzero (already_moved, nregs);
  1043.   bzero (reg_map, nregs * sizeof (rtx));
  1044.  
  1045.   num_movables = 0;
  1046.  
  1047.   for (m = movables; m; m = m->next)
  1048.     {
  1049.       /* Describe this movable insn.  */
  1050.  
  1051.       if (loop_dump_stream)
  1052.     {
  1053.       fprintf (loop_dump_stream, "Insn %d: regno %d (life %d), ",
  1054.            INSN_UID (m->insn), m->regno, m->lifetime);
  1055.       if (m->consec > 0)
  1056.         fprintf (loop_dump_stream, "consec %d, ", m->consec);
  1057.       if (m->cond)
  1058.         fprintf (loop_dump_stream, "cond ");
  1059.       if (m->force)
  1060.         fprintf (loop_dump_stream, "force ");
  1061.       if (m->global)
  1062.         fprintf (loop_dump_stream, "global ");
  1063.       if (m->done)
  1064.         fprintf (loop_dump_stream, "done ");
  1065.       if (m->match)
  1066.         fprintf (loop_dump_stream, "matches %d ",
  1067.              INSN_UID (m->match->insn));
  1068.       if (m->forces)
  1069.         fprintf (loop_dump_stream, "forces %d ",
  1070.              INSN_UID (m->forces->insn));
  1071.     }
  1072.  
  1073.       /* Count movables.  Value used in heuristics in strength_reduce.  */
  1074.       num_movables++;
  1075.  
  1076.       /* Ignore the insn if it's already done (it matched something else).
  1077.      Otherwise, see if it is now safe to move.  */
  1078.  
  1079.       if (!m->done
  1080.       && (! m->cond
  1081.           || (1 == invariant_p (m->set_src)
  1082.           && (m->consec == 0
  1083.               || 1 == consec_sets_invariant_p (SET_DEST (PATTERN (m->insn)),
  1084.                                m->consec + 1,
  1085.                                m->insn))))
  1086.       && (! m->forces || m->forces->done))
  1087.     {
  1088.       register int regno;
  1089.       register rtx p;
  1090.       int savings = m->savings;
  1091.  
  1092.       /* We have an insn that is safe to move.
  1093.          Compute its desirability.  */
  1094.  
  1095.       p = m->insn;
  1096.       regno = m->regno;
  1097.  
  1098.       if (loop_dump_stream)
  1099.         fprintf (loop_dump_stream, "savings %d ", savings);
  1100.  
  1101.       if (moved_once[regno])
  1102.         {
  1103.           insn_count *= 2;
  1104.  
  1105.           if (loop_dump_stream)
  1106.         fprintf (loop_dump_stream, "halved since already moved ");
  1107.         }
  1108.  
  1109.       /* An insn MUST be moved if we already moved something else
  1110.          which is safe only if this one is moved too: that is,
  1111.          if already_moved[REGNO] is nonzero.  */
  1112.  
  1113.       /* An insn is desirable to move if the new lifetime of the
  1114.          register is no more than THRESHOLD times the old lifetime.
  1115.          If it's not desirable, it means the loop is so big
  1116.          that moving won't speed things up much,
  1117.          and it is liable to make register usage worse.  */
  1118.  
  1119.       /* It is also desirable to move if it can be moved at no
  1120.          extra cost because something else was already moved.  */
  1121.  
  1122.       if (already_moved[regno]
  1123.           || (threshold * savings * m->lifetime) >= insn_count
  1124.           || (m->forces && m->forces->done
  1125.           && n_times_used[m->forces->regno] == 1))
  1126.         {
  1127.           int count;
  1128.           register struct movable *m1;
  1129.           rtx first;
  1130.  
  1131.           /* Now move the insns that set the reg.  */
  1132.  
  1133.           for (count = m->consec; count >= 0; count--)
  1134.         {
  1135.           rtx i1, temp;
  1136.  
  1137.           /* If first insn of gnulib call sequence, skip to end.  */
  1138.           /* Do this at start of loop, since p is guaranteed to 
  1139.              be an insn here.  */
  1140.           if (temp = find_reg_note (p, REG_LIBCALL, 0))
  1141.             p = XEXP (temp, 0);
  1142.           
  1143.           /* If last insn of gnulib call sequence, move all
  1144.              insns except the last before the loop.  The last insn is
  1145.              handled in the normal manner.  */
  1146.           if (temp = find_reg_note (p, REG_RETVAL
  1147.                         , 0))
  1148.             {
  1149.               rtx fn_address = 0;
  1150.               rtx fn_reg = 0;
  1151.               first = 0;
  1152.               for (temp = XEXP (temp, 0); temp != p;
  1153.                temp = NEXT_INSN (temp))
  1154.             {
  1155.               rtx body = PATTERN (temp);
  1156.               rtx n;
  1157.               /* Extract the function address from the insn
  1158.                  that loads it into a register.
  1159.                  If this insn was cse'd, we get incorrect code.
  1160.                  So delete it and stick the fn address right
  1161.                  into the call insn.  Since the moved insns
  1162.                  won't be cse'd, that does no harm.  */
  1163.               if (GET_CODE (NEXT_INSN (temp)) == CALL_INSN
  1164.                   && GET_CODE (body) == SET
  1165.                   && GET_CODE (SET_DEST (body)) == REG
  1166.                   && (n = find_reg_note (temp, REG_EQUIV, 0)))
  1167.                 {
  1168.                   fn_reg = SET_SRC (body);
  1169.                   if (GET_CODE (fn_reg) != REG)
  1170.                 fn_reg = SET_DEST (body);
  1171.                   fn_address = XEXP (n, 0);
  1172.                   continue;
  1173.                 }
  1174.               /* We have the call insn.
  1175.                  Substitute the fn address for the reg
  1176.                  that we believe this insn will use.  */
  1177.               if (GET_CODE (temp) == CALL_INSN
  1178.                   && fn_address != 0)
  1179.                 replace_call_address (body, fn_reg, fn_address);
  1180.               if (GET_CODE (temp) == CALL_INSN)
  1181.                 i1 = emit_call_insn_before (body, loop_start);
  1182.               else
  1183.                 i1 = emit_insn_before (body, loop_start);
  1184.               if (first == 0)
  1185.                 first = i1;
  1186.               REG_NOTES (i1) = REG_NOTES (temp);
  1187.               delete_insn (temp);
  1188.             }
  1189.             }
  1190.           if (m->savemode != VOIDmode)
  1191.             {
  1192.               /* P sets REG to zero; but we should clear only the bits
  1193.              that are not covered by the mode m->savemode.  */
  1194.               rtx reg = SET_DEST (PATTERN (p));
  1195.               i1 = emit_insn_before
  1196.             (gen_rtx (SET, VOIDmode, reg,
  1197.                   gen_rtx (AND, GET_MODE (reg),
  1198.                        reg,
  1199.                        gen_rtx (CONST_INT, VOIDmode,
  1200.                             (1 << GET_MODE_BITSIZE (m->savemode)) - 1))),
  1201.              loop_start);
  1202.             }
  1203.           else if (GET_CODE (PATTERN (p)) == CALL_INSN)
  1204.             i1 = emit_call_insn_before (PATTERN (p), loop_start);
  1205.           else
  1206.             i1 = emit_insn_before (PATTERN (p), loop_start);
  1207.  
  1208.           if (new_start == 0)
  1209.             new_start = i1;
  1210.  
  1211.           if (loop_dump_stream)
  1212.             fprintf (loop_dump_stream, " moved to %d", INSN_UID (i1));
  1213.  
  1214.           /* Mark the moved, invariant reg as being equivalent to
  1215.              its constant value.  */
  1216.           REG_NOTES (i1) = REG_NOTES (p);
  1217.           if (REG_NOTES (i1) == 0
  1218.               && ! m->partial /* But not if it's a zero-extend clr. */
  1219.               && ! m->global /* and not if used outside the loop
  1220.                     (since it might get set outside).  */
  1221.               && CONSTANT_P (SET_SRC (PATTERN (p))))
  1222.             REG_NOTES (i1)
  1223.               = gen_rtx (EXPR_LIST, REG_EQUIV,
  1224.                  SET_SRC (PATTERN (p)), REG_NOTES (i1));
  1225.  
  1226.           /* If library call, now fix the REG_NOTES that contain
  1227.              insn pointers, namely REG_LIBCALL on FIRST
  1228.              and REG_RETVAL on I1.  */
  1229.           if (temp = find_reg_note (i1, REG_RETVAL, 0))
  1230.             {
  1231.               XEXP (temp, 0) = first;
  1232.               temp = find_reg_note (first, REG_LIBCALL, 0);
  1233.               XEXP (temp, 0) = i1;
  1234.             }
  1235.  
  1236.           delete_insn (p);
  1237.           do p = NEXT_INSN (p);
  1238.           while (GET_CODE (p) == NOTE);
  1239.         }
  1240.  
  1241.           /* The more regs we move, the less we like moving them.  */
  1242.           threshold -= 3;
  1243.  
  1244.           /* Any other movable that loads the same register
  1245.          MUST be moved.  */
  1246.           already_moved[regno] = 1;
  1247.  
  1248.           /* This reg has been moved out of one loop.  */
  1249.           moved_once[regno] = 1;
  1250.  
  1251.           /* The reg set here is now invariant.  */
  1252.           if (! m->partial)
  1253.         n_times_set[regno] = 0;
  1254.  
  1255.           m->done = 1;
  1256.  
  1257.           /* Change the length-of-life info for the register
  1258.          to say it lives at least the full length of this loop.
  1259.          This will help guide optimizations in outer loops.  */
  1260.  
  1261.           if (uid_luid[regno_first_uid[regno]] > INSN_LUID (loop_start))
  1262.         /* This is the old insn before all the moved insns.
  1263.            We can't use the moved insn because it is out of range
  1264.            in uid_luid.  Only the old insns have luids.  */
  1265.         regno_first_uid[regno] = INSN_UID (loop_start);
  1266.           if (uid_luid[regno_last_uid[regno]] < INSN_LUID (end))
  1267.         regno_last_uid[regno] = INSN_UID (end);
  1268.  
  1269.           /* Combine with this moved insn any other matching movables.  */
  1270.  
  1271.           for (m1 = m->next; m1; m1 = m1->next)
  1272.         if (m1->match == m)
  1273.           {
  1274.             rtx temp;
  1275.  
  1276.             /* Schedule the reg loaded by M1
  1277.                for replacement so that shares the reg of M.  */
  1278.             reg_map[m1->regno] = SET_DEST (PATTERN (m->insn));
  1279.             /* Get rid of the matching insn
  1280.                and prevent further processing of it.  */
  1281.             m1->done = 1;
  1282.  
  1283.             /* if library call, delete all insn except last, which
  1284.                is deleted below */
  1285.             if (temp = find_reg_note (m1->insn, REG_RETVAL, 0))
  1286.               {
  1287.             for (temp = XEXP (temp, 0); temp != m1->insn;
  1288.                  temp = NEXT_INSN (temp))
  1289.                 delete_insn (temp);
  1290.               }
  1291.             delete_insn (m1->insn);
  1292.  
  1293.             /* Any other movable that loads the same register
  1294.                MUST be moved.  */
  1295.             already_moved[m1->regno] = 1;
  1296.  
  1297.             /* The reg merged here is now invariant,
  1298.                if the reg it matches is invariant.  */
  1299.             if (! m->partial)
  1300.               n_times_set[m1->regno] = 0;
  1301.           }
  1302.         }
  1303.       else if (loop_dump_stream)
  1304.         fprintf (loop_dump_stream, "not desirable");
  1305.     }
  1306.       else if (loop_dump_stream && !m->match)
  1307.     fprintf (loop_dump_stream, "not safe");
  1308.  
  1309.       if (loop_dump_stream)
  1310.     fprintf (loop_dump_stream, "\n");
  1311.     }
  1312.  
  1313.   if (new_start == 0)
  1314.     new_start = loop_start;
  1315.  
  1316.   /* Go through all the instructions in the loop, making
  1317.      all the register substitutions scheduled in REG_MAP.  */
  1318.   for (p = new_start; p != end; p = NEXT_INSN (p))
  1319.     if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
  1320.     || GET_CODE (p) == CALL_INSN)
  1321.       replace_regs (PATTERN (p), reg_map, nregs);
  1322. }
  1323.  
  1324. /* Optionally change a loop which enters just before the endtest
  1325.    to one which falls straight in
  1326.    after skipping around the entire loop if the endtest would drop out.
  1327.    Returns 1 if the change was made, 0 if the loop was not really suitable.  */
  1328.  
  1329. int
  1330. loop_skip_over (start, end, loop_entry_jump)
  1331.      rtx start;
  1332.      rtx end;
  1333.      rtx loop_entry_jump;
  1334. {
  1335.   rtx entry_insn;
  1336.   rtx endtest;
  1337.   rtx endtestjump;
  1338.   register rtx p = JUMP_LABEL (loop_entry_jump);
  1339.  
  1340.   while (GET_CODE (p) != INSN && GET_CODE (p) != JUMP_INSN
  1341.      && GET_CODE (p) != CALL_INSN)
  1342.     p = NEXT_INSN (p);
  1343.   entry_insn = p;
  1344.  
  1345.   /* Skip any ordinary arithmetic insns to find the compare.  */
  1346.   for (; p != 0; p = NEXT_INSN (p))
  1347.     if (GET_CODE (p) != NOTE)
  1348.       if (GET_CODE (p) != INSN || sets_cc0_p (PATTERN (p)))
  1349.     break;
  1350.   if (p == 0 || GET_CODE (p) != INSN)
  1351.     return 0;
  1352.   endtest = p;
  1353.   endtestjump = next_real_insn (p);
  1354.  
  1355.   /* Check that (1) we have reached a compare insn and (2)
  1356.      the insn (presumably a jump) following that compare
  1357.      is the last in the loop and jumps back to the loop beginning.  */
  1358.  
  1359.   if (sets_cc0_p (PATTERN (endtest)) > 0
  1360.       && endtestjump == prev_real_insn (end)
  1361.       && prev_real_insn (JUMP_LABEL (endtestjump)) == loop_entry_jump)
  1362.     {
  1363.       rtx newlab;
  1364.       /* This is the jump that we insert.  */
  1365.       rtx new_jump;
  1366.  
  1367.       /* Duplicate the ordinary arith insns before the compare.  */
  1368.       for (p = entry_insn; p != endtest; p = NEXT_INSN (p))
  1369.     if (GET_CODE (p) == INSN)
  1370.       {
  1371.         rtx new = emit_insn_before (copy_rtx (PATTERN (p)), start);
  1372.         if (REG_NOTES (p))
  1373.           REG_NOTES (new) = copy_rtx (REG_NOTES (p));
  1374.       }
  1375.     
  1376.       /* Ok, duplicate that test before start of loop.  */
  1377.       emit_insn_before (copy_rtx (PATTERN (endtest)), start);
  1378.       /* Make a new entry-jump (before the original one)
  1379.      whose condition is opposite to the loop-around endtest
  1380.      and which jumps around the loop (to just after the ending NOTE).  */
  1381.       newlab = gen_label_rtx ();
  1382.       emit_label_after (newlab, end);
  1383.       emit_jump_insn_before (copy_rtx (PATTERN (endtestjump)), start);
  1384.       new_jump = PREV_INSN (start);
  1385.       JUMP_LABEL (new_jump) = JUMP_LABEL (endtestjump);
  1386.       LABEL_NUSES (JUMP_LABEL (endtestjump))++;
  1387.       invert_jump (new_jump, newlab);
  1388.       /* Delete the original entry-jump.  */
  1389.       delete_insn (loop_entry_jump);
  1390.  
  1391.       return 1;
  1392.     }
  1393.  
  1394.   return 0;
  1395. }
  1396.  
  1397. /* Throughout the rtx X, replace many registers according to REG_MAP.
  1398.    Return the replacement for X (which may be X with altered contents).
  1399.    REG_MAP[R] is the replacement for register R, or 0 for don't replace.
  1400.    NREGS is the length of REG_MAP; regs >= NREGS are not mapped.  */
  1401.  
  1402. static rtx
  1403. replace_regs (x, reg_map, nregs)
  1404.      rtx x;
  1405.      rtx *reg_map;
  1406.      int nregs;
  1407. {
  1408.   register enum rtx_code code;
  1409.   register int i;
  1410.   register char *fmt;
  1411.  
  1412.   if (x == 0)
  1413.     return x;
  1414.  
  1415.   code = GET_CODE (x);
  1416.   switch (code)
  1417.     {
  1418.     case PC:
  1419.     case CC0:
  1420.     case CONST_INT:
  1421.     case CONST_DOUBLE:
  1422.     case CONST:
  1423.     case SYMBOL_REF:
  1424.     case LABEL_REF:
  1425.       return x;
  1426.  
  1427.     case REG:
  1428.       /* Verify that the register has an entry before trying to access it.  */
  1429.       if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
  1430.     return reg_map[REGNO (x)];
  1431.       return x;
  1432.     }
  1433.  
  1434.   fmt = GET_RTX_FORMAT (code);
  1435.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1436.     {
  1437.       if (fmt[i] == 'e')
  1438.     XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs);
  1439.       if (fmt[i] == 'E')
  1440.     {
  1441.       register int j;
  1442.       for (j = 0; j < XVECLEN (x, i); j++)
  1443.         XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map, nregs);
  1444.     }
  1445.     }
  1446.   return x;
  1447. }
  1448.  
  1449. /* Scan X and replace the address of any MEM in it with ADDR.
  1450.    REG is the address that MEM should have before the replacement.  */
  1451.  
  1452. static void
  1453. replace_call_address (x, reg, addr)
  1454.      rtx x, reg, addr;
  1455. {
  1456.   register enum rtx_code code;
  1457.   register int i;
  1458.   register char *fmt;
  1459.  
  1460.   if (x == 0)
  1461.     return;
  1462.   code = GET_CODE (x);
  1463.   switch (code)
  1464.     {
  1465.     case PC:
  1466.     case CC0:
  1467.     case CONST_INT:
  1468.     case CONST_DOUBLE:
  1469.     case CONST:
  1470.     case SYMBOL_REF:
  1471.     case LABEL_REF:
  1472.     case REG:
  1473.       return;
  1474.  
  1475.     case SET:
  1476.       /* Short cut for very common case.  */
  1477.       replace_call_address (XEXP (x, 1), reg, addr);
  1478.       return;
  1479.  
  1480.     case CALL:
  1481.       /* Short cut for very common case.  */
  1482.       replace_call_address (XEXP (x, 0), reg, addr);
  1483.       return;
  1484.  
  1485.     case MEM:
  1486.       /* If this MEM uses a reg other than the one we expected,
  1487.      something is wrong.  */
  1488.       if (XEXP (x, 0) != reg)
  1489.     abort ();
  1490.       XEXP (x, 0) = addr;
  1491.       return;
  1492.     }
  1493.  
  1494.   fmt = GET_RTX_FORMAT (code);
  1495.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1496.     {
  1497.       if (fmt[i] == 'e')
  1498.     replace_call_address (XEXP (x, i), reg, addr);
  1499.       if (fmt[i] == 'E')
  1500.     {
  1501.       register int j;
  1502.       for (j = 0; j < XVECLEN (x, i); j++)
  1503.         replace_call_address (XVECEXP (x, i, j), reg, addr);
  1504.     }
  1505.     }
  1506. }
  1507.  
  1508. /* Return the number of memory refs to addresses that vary
  1509.    in the rtx X.  */
  1510.  
  1511. static int
  1512. count_nonfixed_reads (x)
  1513.      rtx x;
  1514. {
  1515.   register enum rtx_code code;
  1516.   register int i;
  1517.   register char *fmt;
  1518.   int value;
  1519.  
  1520.   if (x == 0)
  1521.     return 0;
  1522.  
  1523.   code = GET_CODE (x);
  1524.   switch (code)
  1525.     {
  1526.     case PC:
  1527.     case CC0:
  1528.     case CONST_INT:
  1529.     case CONST_DOUBLE:
  1530.     case CONST:
  1531.     case SYMBOL_REF:
  1532.     case LABEL_REF:
  1533.     case REG:
  1534.       return 0;
  1535.  
  1536.     case MEM:
  1537.       return rtx_varies_p (XEXP (x, 0)) + count_nonfixed_reads (XEXP (x, 0));
  1538.     }
  1539.  
  1540.   value = 0;
  1541.   fmt = GET_RTX_FORMAT (code);
  1542.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1543.     {
  1544.       if (fmt[i] == 'e')
  1545.     value += count_nonfixed_reads (XEXP (x, i));
  1546.       if (fmt[i] == 'E')
  1547.     {
  1548.       register int j;
  1549.       for (j = 0; j < XVECLEN (x, i); j++)
  1550.         value += count_nonfixed_reads (XVECEXP (x, i, j));
  1551.     }
  1552.     }
  1553.   return value;
  1554. }
  1555.  
  1556.  
  1557. #if 0
  1558. /* P is an instruction that sets a register to the result of a ZERO_EXTEND.
  1559.    Replace it with an instruction to load just the low bytes
  1560.    if the machine supports such an instruction,
  1561.    and insert above LOOP_START an instruction to clear the register.  */
  1562.  
  1563. static void
  1564. constant_high_bytes (p, loop_start)
  1565.      rtx p, loop_start;
  1566. {
  1567.   register rtx new;
  1568.   register int insn_code_number;
  1569.  
  1570.   /* Try to change (SET (REG ...) (ZERO_EXTEND (..:B ...)))
  1571.      to (SET (STRICT_LOW_PART (SUBREG:B (REG...))) ...).  */
  1572.  
  1573.   new = gen_rtx (SET, VOIDmode,
  1574.          gen_rtx (STRICT_LOW_PART, VOIDmode,
  1575.               gen_rtx (SUBREG, GET_MODE (XEXP (SET_SRC (PATTERN (p)), 0)),
  1576.                    SET_DEST (PATTERN (p)),
  1577.                    0)),
  1578.          XEXP (SET_SRC (PATTERN (p)), 0));
  1579.   insn_code_number = recog (new, p);
  1580.  
  1581.   if (insn_code_number)
  1582.     {
  1583.       register int i;
  1584.  
  1585.       /* Clear destination register before the loop.  */
  1586.       emit_insn_before (gen_rtx (SET, VOIDmode,
  1587.                  SET_DEST (PATTERN (p)),
  1588.                  const0_rtx),
  1589.             loop_start);
  1590.  
  1591.       /* Inside the loop, just load the low part.  */
  1592.       PATTERN (p) = new;
  1593.     }
  1594. }
  1595. #endif
  1596.  
  1597. /* Verify that the ostensible loop starting at START
  1598.    really is a loop: nothing jumps into it from outside.
  1599.    Return the marker for the end of the loop, or zero if not a real loop.
  1600.  
  1601.    Also set the variables `unknown_*_altered' and `loop_has_call',
  1602.    and fill in the array `loop_store_addrs'.  */
  1603.  
  1604. static rtx
  1605. verify_loop (f, start)
  1606.      rtx f, start;
  1607. {
  1608.   register int level = 1;
  1609.   register rtx insn, end;
  1610.  
  1611.   /* First find the LOOP_END that matches.
  1612.      Also check each insn for storing in memory and record where.  */
  1613.  
  1614.   unknown_address_altered = 0;
  1615.   unknown_aggregate_altered = 0;
  1616.   fixed_aggregate_altered = 0;
  1617.   loop_has_call = 0;
  1618.   loop_store_addrs_idx = 0;
  1619.  
  1620.   num_mem_sets = 0;
  1621.   loops_enclosed = 1;
  1622.   loop_continue = 0;
  1623.  
  1624.   for (insn = NEXT_INSN (start); level > 0; insn = NEXT_INSN (insn))
  1625.     {
  1626.       if (insn == 0)
  1627.     /* Parse errors can cause a loop-beg with no loop-end.  */
  1628.     return 0;
  1629.       if (GET_CODE (insn) == NOTE)
  1630.     {
  1631.       if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
  1632.         {
  1633.           ++level;
  1634.           /* Count number of loops contained in this one.  */
  1635.           loops_enclosed++;
  1636.         }
  1637.       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  1638.         {
  1639.           --level;
  1640.           if (level == 0)
  1641.         {
  1642.           end = insn;
  1643.           break;
  1644.         }
  1645.         }
  1646.       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_CONT)
  1647.         {
  1648.           if (level == 1)
  1649.         loop_continue = insn;
  1650.         }
  1651.  
  1652.       /* Don't optimize loops containing setjmps.
  1653.          On some machines, longjmp does not restore the reg
  1654.          values as of the time of the setjmp.  */
  1655.       else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
  1656.         return 0;
  1657.     }
  1658.       else if (GET_CODE (insn) == CALL_INSN)
  1659.     {
  1660.       unknown_address_altered = 1;
  1661.       loop_has_call = 1;
  1662.     }
  1663. /* ???
  1664.       else if (! unknown_address_altered) */
  1665.       else
  1666.     {
  1667.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  1668.         note_stores (PATTERN (insn), note_addr_stored);
  1669.     }
  1670.     }
  1671.  
  1672.   /* Now scan all jumps in the function and see if any of them can
  1673.      reach a label within the range of the loop.  */
  1674.  
  1675.   for (insn = f; insn; insn = NEXT_INSN (insn))
  1676.     if (GET_CODE (insn) == JUMP_INSN
  1677.     /* Don't get fooled by jumps inserted by loop-optimize.
  1678.        They don't have valid LUIDs, and they never jump into loops.  */
  1679.     && INSN_UID (insn) < max_uid
  1680.     && (INSN_LUID (insn) < INSN_LUID (start)
  1681.         || INSN_LUID (insn) > INSN_LUID (end))
  1682.     /* We have a jump that is outside the loop.
  1683.        Does it jump into the loop?  */
  1684.     && can_jump_into_range_p (PATTERN (insn),
  1685.                   INSN_LUID (start), INSN_LUID (end)))
  1686.       return 0;
  1687.  
  1688. #if 0      
  1689.   /* Now scan all labels between them and check for any jumps from outside.
  1690.      This uses the ref-chains set up by find_basic_blocks.
  1691.      This code is not used because it's more convenient for other reasons
  1692.      to do the loop optimization before find_basic_blocks.  */
  1693.  
  1694.   for (insn = start; insn != end; insn = NEXT_INSN (insn))
  1695.     if (GET_CODE (insn) == CODE_LABEL)
  1696.       {
  1697.     register rtx y;
  1698.     for (y = LABEL_REFS (insn); y != insn; y = LABEL_NEXTREF (y))
  1699.       if (INSN_LUID (CONTAINING_INSN (y)) < INSN_LUID (start)
  1700.           || INSN_LUID (CONTAINING_INSN (y)) > INSN_LUID (end))
  1701.         return 0;
  1702.       }
  1703. #endif
  1704.  
  1705.   return end;
  1706. }
  1707.  
  1708. /* Return 1 if somewhere in X is a LABEL_REF to a label
  1709.    located between BEG and END.  */
  1710.  
  1711. static int
  1712. can_jump_into_range_p (x, beg, end)
  1713.      rtx x;
  1714.      int beg, end;
  1715. {
  1716.   register enum rtx_code code = GET_CODE (x);
  1717.   register int i;
  1718.   register char *fmt;
  1719.  
  1720.   if (code == LABEL_REF)
  1721.     {
  1722.       register int luid = INSN_LUID (XEXP (x, 0));
  1723.       return luid > beg && luid < end;
  1724.     }
  1725.  
  1726.   fmt = GET_RTX_FORMAT (code);
  1727.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1728.     {
  1729.       if (fmt[i] == 'e')
  1730.     {
  1731.       if (can_jump_into_range_p (XEXP (x, i), beg, end))
  1732.         return 1;
  1733.     }
  1734.       else if (fmt[i] == 'E')
  1735.     {
  1736.       register int j;
  1737.       for (j = 0; j < XVECLEN (x, i); j++)
  1738.         if (can_jump_into_range_p (XVECEXP (x, i, j), beg, end))
  1739.           return 1;
  1740.     }
  1741.     }
  1742.  
  1743.   return 0;
  1744. }
  1745.  
  1746. /* Return nonzero if there is a label in the range from
  1747.    insn INSN to the insn whose luid is END.  */
  1748.  
  1749. static int
  1750. labels_in_range_p (insn, end)
  1751.      rtx insn;
  1752.      int end;
  1753. {
  1754.   while (insn && INSN_LUID (insn) <= end)
  1755.     {
  1756.       if (GET_CODE (insn) == CODE_LABEL)
  1757.     return 0;
  1758.       insn = NEXT_INSN (insn);
  1759.     }
  1760.  
  1761.   return 0;
  1762. }
  1763.  
  1764. /* Record that a memory reference X is being set.  */
  1765.  
  1766. static void
  1767. note_addr_stored (x)
  1768.      rtx x;
  1769. {
  1770.   if (x == 0 || GET_CODE (x) != MEM)
  1771.     return;
  1772.  
  1773.   /* Count number of memory writes.
  1774.      This affects heuristics in strength_reduce.  */
  1775.   num_mem_sets++;
  1776.   if (unknown_address_altered)
  1777.     return;
  1778.  
  1779.   if (GET_MODE (x) == BLKmode)
  1780.     unknown_address_altered = 1;
  1781.   else if (rtx_addr_varies_p (x))
  1782.     {
  1783.       if (GET_CODE (XEXP (x, 0)) == PLUS)
  1784.     unknown_aggregate_altered = 1;
  1785.       else
  1786.     unknown_address_altered = 1;
  1787.     }
  1788.   else
  1789.     {
  1790.       register int i;
  1791.       register rtx addr = XEXP (x, 0);
  1792.  
  1793.       if (MEM_IN_STRUCT_P (x))
  1794.     fixed_aggregate_altered = 1;
  1795.       for (i = 0; i < loop_store_addrs_idx; i++)
  1796.     if (rtx_equal_p (loop_store_addrs[i], addr))
  1797.       {
  1798.         if (loop_store_widths[i] < GET_MODE_SIZE (GET_MODE (x)))
  1799.           loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
  1800.         break;
  1801.       }
  1802.       if (i == NUM_STORES)
  1803.     unknown_address_altered = 1;
  1804.       else if (i == loop_store_addrs_idx)
  1805.     {
  1806.       loop_store_widths[i] = GET_MODE_SIZE (GET_MODE (x));
  1807.       loop_store_addrs[loop_store_addrs_idx++] = addr;
  1808.     }
  1809.     }
  1810. }
  1811.  
  1812. /* Return nonzero if the rtx X is invariant over the current loop.
  1813.  
  1814.    The value is 2 if we refer to something only conditionally invariant.
  1815.  
  1816.    If `unknown_address_altered' is nonzero, no memory ref is invariant.
  1817.    Otherwise if `unknown_aggregate_altered' is nonzero,
  1818.    a memory ref is invariant if it is not part of an aggregate
  1819.    and its address is fixed and not in `loop_store_addrs'.
  1820.    Otherwise if `fixed_aggregate_altered' is nonzero,
  1821.    a memory ref is invariant
  1822.    if its address is fixed and not in `loop_store_addrs'.
  1823.    Otherwise, a memory ref is invariant if its address is fixed and not in
  1824.    `loop_store_addrs' or if it is not an aggregate.  */
  1825.  
  1826. static int
  1827. invariant_p (x)
  1828.      register rtx x;
  1829. {
  1830.   register int i;
  1831.   register enum rtx_code code;
  1832.   register char *fmt;
  1833.   int conditional = 0;
  1834.  
  1835.   if (x == 0)
  1836.     return 1;
  1837.   code = GET_CODE (x);
  1838.   switch (code)
  1839.     {
  1840.     case CONST_INT:
  1841.     case CONST_DOUBLE:
  1842.     case SYMBOL_REF:
  1843.     case LABEL_REF:
  1844.     case CONST:
  1845.       return 1;
  1846.  
  1847.     case PC:
  1848.     case CC0:
  1849.       return 0;
  1850.  
  1851.     case REG:
  1852.       /* We used to check RTX_UNCHANGING_P (x) here, but that is invalid
  1853.      since the reg might be set by initialization within the loop.  */
  1854.       if (x == frame_pointer_rtx || x == arg_pointer_rtx)
  1855.     return 1;
  1856.       if (n_times_set[REGNO (x)] == -1)
  1857.     return 2;
  1858.       return n_times_set[REGNO (x)] == 0;
  1859.  
  1860.     case MEM:
  1861.       /* Constants in the constant pool are invariant.
  1862.      ?? Really we should detect any constant address in the
  1863.      text section.  */
  1864.       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
  1865.       && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
  1866.     return 1;
  1867.       /* A store in a varying-address scalar (or a subroutine call)
  1868.      could clobber anything in memory.  */
  1869.       if (unknown_address_altered)
  1870.     return 0;
  1871.       /* Don't mess with volatile memory references.  */
  1872.       if (MEM_VOLATILE_P (x))
  1873.     return 0;
  1874. #if 0
  1875.       /* If it's declared read-only, it is invariant
  1876.      if its address is invariant.  */
  1877.       if (RTX_UNCHANGING_P (x))
  1878.     return invariant_p (XEXP (x, 0));
  1879. #endif
  1880.       /* A store in a varying-address aggregate component
  1881.      could clobber anything except a scalar with a fixed address.  */
  1882.       if (unknown_aggregate_altered
  1883.       && ((MEM_IN_STRUCT_P (x) || GET_CODE (XEXP (x, 0)) == PLUS)
  1884.           || rtx_addr_varies_p (x)))
  1885.     return 0;
  1886.       /* A store in a fixed-address aggregate component
  1887.      could clobber anything whose address is not fixed,
  1888.      even an aggregate component.  */
  1889.       if (fixed_aggregate_altered
  1890.       && rtx_addr_varies_p (x))
  1891.     return 0;
  1892.       /* Any store could clobber a varying-address scalar.  */
  1893.       if (loop_store_addrs_idx
  1894.       && !(MEM_IN_STRUCT_P (x) || GET_CODE (XEXP (x, 0)) == PLUS)
  1895.       && rtx_addr_varies_p (x))
  1896.     return 0;
  1897.       /* A store in a fixed address clobbers overlapping references.  */
  1898.       for (i = loop_store_addrs_idx - 1; i >= 0; i--)
  1899.     if (addr_overlap_p (x, loop_store_addrs[i], loop_store_widths[i]))
  1900.       return 0;
  1901.       /* It's not invalidated by a store in memory
  1902.      but we must still verify the address is invariant.  */
  1903.       break;
  1904.  
  1905.     case ASM_OPERANDS:
  1906.       /* Don't mess with insns declared volatile.  */
  1907.       if (MEM_VOLATILE_P (x))
  1908.     return 0;
  1909.     }
  1910.  
  1911.   fmt = GET_RTX_FORMAT (code);
  1912.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1913.     {
  1914.       if (fmt[i] == 'e')
  1915.     {
  1916.       int tem = invariant_p (XEXP (x, i));
  1917.       if (tem == 0)
  1918.         return 0;
  1919.       if (tem == 2)
  1920.         conditional = 1;
  1921.     }
  1922.       else if (fmt[i] == 'E')
  1923.     {
  1924.       register int j;
  1925.       for (j = 0; j < XVECLEN (x, i); j++)
  1926.         {
  1927.           int tem = invariant_p (XVECEXP (x, i, j));
  1928.           if (tem == 0)
  1929.         return 0;
  1930.           if (tem == 2)
  1931.         conditional = 1;
  1932.         }
  1933.  
  1934.     }
  1935.     }
  1936.  
  1937.   return 1 + conditional;
  1938. }
  1939.  
  1940. /* Return 1 if OTHER (a mem ref) overlaps the area of memory
  1941.    which is SIZE bytes starting at BASE.  */
  1942.  
  1943. int
  1944. addr_overlap_p (other, base, size)
  1945.      rtx other;
  1946.      rtx base;
  1947.      int size;
  1948. {
  1949.   int start = 0, end;
  1950.  
  1951.   if (GET_CODE (base) == CONST)
  1952.     base = XEXP (base, 0);
  1953.   if (GET_CODE (base) == PLUS
  1954.       && GET_CODE (XEXP (base, 1)) == CONST_INT)
  1955.     {
  1956.       start = INTVAL (XEXP (base, 1));
  1957.       base = XEXP (base, 0);
  1958.     }
  1959.  
  1960.   end = start + size;
  1961.   return refers_to_mem_p (other, base, start, end);
  1962. }
  1963.  
  1964. /* Return nonzero if all the insns in the loop that set REG
  1965.    are INSN and the immediately following insns,
  1966.    and if each of those insns sets REG in an invariant way
  1967.    (not counting uses of REG in them).
  1968.  
  1969.    The value is 2 if some of these insns are only conditionally invariant.
  1970.  
  1971.    We assume that INSN itself is the first set of REG
  1972.    and that its source is invariant.  */
  1973.  
  1974. static int
  1975. consec_sets_invariant_p (reg, n_sets, insn)
  1976.      int n_sets;
  1977.      rtx reg, insn;
  1978. {
  1979.   register rtx p = insn;
  1980.   register int regno = REGNO (reg);
  1981.   rtx temp;
  1982.   /* Number of sets we have to insist on finding after INSN.  */
  1983.   int count = n_sets - 1;
  1984.   int old = n_times_set[regno];
  1985.   int value = 0;
  1986.   int this;
  1987.  
  1988.   /* If N_SETS hit the limit, we can't rely on its value.  */
  1989.   if (n_sets == 127)
  1990.     return 0;
  1991.  
  1992.   n_times_set[regno] = 0;
  1993.  
  1994.   while (count > 0)
  1995.     {
  1996.       register enum rtx_code code;
  1997.       p = NEXT_INSN (p);
  1998.       code = GET_CODE (p);
  1999.  
  2000.       /* If library call, skip to end of of it.  */
  2001.       if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, 0)))
  2002.     p = XEXP (temp, 0);
  2003.  
  2004.       this = 0;
  2005.       if (code == INSN && GET_CODE (PATTERN (p)) == SET
  2006.       && GET_CODE (SET_DEST (PATTERN (p))) == REG
  2007.       && REGNO (SET_DEST (PATTERN (p))) == regno)
  2008.     {
  2009.       this = invariant_p (SET_SRC (PATTERN (p)));
  2010.       if (this != 0)
  2011.         value |= this;
  2012.       else if (temp = find_reg_note (p, REG_EQUAL, 0))
  2013.         {
  2014.           this = invariant_p (XEXP (temp, 0));
  2015.           if (this != 0)
  2016.         value |= this;
  2017.         }
  2018.     }
  2019.       if (this != 0)
  2020.     count--;
  2021.       else if (code != NOTE)
  2022.     {
  2023.       n_times_set[regno] = old;
  2024.       return 0;
  2025.     }
  2026.     }
  2027.  
  2028.   n_times_set[regno] = old;
  2029.   /* If invariant_p ever returned 2, we return 2.  */
  2030.   return 1 + (value & 2);
  2031. }
  2032.  
  2033. #if 0
  2034. /* I don't think this condition is sufficient to allow INSN
  2035.    to be moved, so we no longer test it.  */
  2036.  
  2037. /* Return 1 if all insns in the basic block of INSN and following INSN
  2038.    that set REG are invariant according to TABLE.  */
  2039.  
  2040. static int
  2041. all_sets_invariant_p (reg, insn, table)
  2042.      rtx reg, insn;
  2043.      short *table;
  2044. {
  2045.   register rtx p = insn;
  2046.   register int regno = REGNO (reg);
  2047.  
  2048.   while (1)
  2049.     {
  2050.       register enum rtx_code code;
  2051.       p = NEXT_INSN (p);
  2052.       code = GET_CODE (p);
  2053.       if (code == CODE_LABEL || code == JUMP_INSN)
  2054.     return 1;
  2055.       if (code == INSN && GET_CODE (PATTERN (p)) == SET
  2056.       && GET_CODE (SET_DEST (PATTERN (p))) == REG
  2057.       && REGNO (SET_DEST (PATTERN (p))) == regno)
  2058.     {
  2059.       if (!invariant_p (SET_SRC (PATTERN (p)), table))
  2060.         return 0;
  2061.     }
  2062.     }
  2063. }
  2064. #endif /* 0 */
  2065.  
  2066. /* Increment N_TIMES_SET at the index of each register
  2067.    that is modified by an insn between FROM and TO.
  2068.    If the value of an element of N_TIMES_SET becomes 127 or more,
  2069.    stop incrementing it, to avoid overflow.
  2070.  
  2071.    Store in *COUNT_PTR the number of actual instruction
  2072.    in the loop.  We use this to decide what is worth moving out.  */
  2073.  
  2074. /* last_set[n] is nonzero iff reg n has been set in the current basic block.
  2075.    In that case, it is the insn that last set reg n.  */
  2076.  
  2077. static void
  2078. count_loop_regs_set (from, to, may_not_move, count_ptr, nregs)
  2079.      register rtx from, to;
  2080.      char *may_not_move;
  2081.      int *count_ptr;
  2082.      int nregs;
  2083. {
  2084.   register rtx *last_set = (rtx *) alloca (nregs * sizeof (rtx));
  2085.   register rtx insn;
  2086.   register int count = 0;
  2087.   register rtx dest;
  2088.  
  2089.   bzero (last_set, nregs * sizeof (rtx));
  2090.   for (insn = from; insn != to; insn = NEXT_INSN (insn))
  2091.     {
  2092.       if (GET_CODE (insn) == CALL_INSN)
  2093.     {
  2094.       /* If a register is used as a subroutine address,
  2095.          don't allow this register's setting to be moved out of the loop.
  2096.          This condition is not at all logically correct
  2097.          but it averts a very common lossage pattern
  2098.          and creates lossage much less often.  */
  2099.       if (GET_CODE (PATTERN (insn)) == CALL
  2100.           && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM
  2101.           && GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) == REG)
  2102.         {
  2103.           register int regno
  2104.         = REGNO (XEXP (XEXP (PATTERN (insn), 0), 0));
  2105.           may_not_move[regno] = 1;
  2106.         }
  2107.       else if (GET_CODE (PATTERN (insn)) == SET
  2108.           && GET_CODE (SET_SRC (PATTERN (insn))) == CALL
  2109.           && GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 0)) == MEM
  2110.           && GET_CODE (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0)) == REG)
  2111.         {
  2112.           register int regno
  2113.         = REGNO (XEXP (XEXP (SET_SRC (PATTERN (insn)), 0), 0));
  2114.           may_not_move[regno] = 1;
  2115.           /* The call insn itself sets a reg, which cannot be moved.  */
  2116.           may_not_move[REGNO (SET_DEST (PATTERN (insn)))] = 1;
  2117.           if (n_times_set[REGNO (SET_DEST (PATTERN (insn)))] < 127)
  2118.         n_times_set[REGNO (SET_DEST (PATTERN (insn)))]++;
  2119.         }
  2120.     }
  2121.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN) 
  2122.     {
  2123.       ++count;
  2124.       if (GET_CODE (PATTERN (insn)) == CLOBBER
  2125.           && GET_CODE (XEXP (PATTERN (insn), 0)) == REG)
  2126.         /* Don't move a reg that has an explicit clobber.
  2127.            We might do so sometimes, but it's not worth the pain.  */
  2128.         may_not_move[REGNO (XEXP (PATTERN (insn), 0))] = 1;
  2129.       else if (GET_CODE (PATTERN (insn)) == SET)
  2130.         {
  2131.           dest = SET_DEST (PATTERN (insn));
  2132.           while (GET_CODE (dest) == SUBREG
  2133.              || GET_CODE (dest) == ZERO_EXTRACT
  2134.              || GET_CODE (dest) == SIGN_EXTRACT
  2135.              || GET_CODE (dest) == STRICT_LOW_PART)
  2136.         dest = XEXP (dest, 0);
  2137.           if (GET_CODE (dest) == REG)
  2138.         {
  2139.           register int regno = REGNO (dest);
  2140.           /* If this is the first setting of this reg
  2141.              in current basic block, and it was set before,
  2142.              it must be set in two basic blocks, so it cannot
  2143.              be moved out of the loop.  */
  2144.           if (n_times_set[regno] > 0 && last_set[regno] == 0)
  2145.             may_not_move[regno] = 1;
  2146.           /* If this is not first setting in current basic block,
  2147.              see if reg was used in between previous one and this.
  2148.              If so, neither one can be moved.  */
  2149.           if (last_set[regno] != 0
  2150.               && reg_used_between_p (dest, last_set[regno], insn))
  2151.             may_not_move[regno] = 1;
  2152.           if (n_times_set[regno] < 127)
  2153.             ++n_times_set[regno];
  2154.           last_set[regno] = insn;
  2155.         }
  2156.         }
  2157.       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  2158.         {
  2159.           register int i;
  2160.           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  2161.         {
  2162.           register rtx x = XVECEXP (PATTERN (insn), 0, i);
  2163.           if (GET_CODE (x) == CLOBBER && GET_CODE (XEXP (x, 0)) == REG)
  2164.             /* Don't move a reg that has an explicit clobber.
  2165.                It's not worth the pain to try to do it correctly.  */
  2166.             may_not_move[REGNO (XEXP (x, 0))] = 1;
  2167.           if (GET_CODE (x) == SET)
  2168.             {
  2169.               dest = SET_DEST (x);
  2170.               while (GET_CODE (dest) == SUBREG
  2171.                  || GET_CODE (dest) == ZERO_EXTRACT
  2172.                  || GET_CODE (dest) == SIGN_EXTRACT
  2173.                  || GET_CODE (dest) == STRICT_LOW_PART)
  2174.             dest = XEXP (dest, 0);
  2175.               if (GET_CODE (dest) == REG)
  2176.             {
  2177.               register int regno = REGNO (dest);
  2178.               if (n_times_set[regno] < 127)
  2179.                 ++n_times_set[regno];
  2180.               may_not_move[regno] = 1;
  2181.               last_set[regno] = insn;
  2182.             }
  2183.             }
  2184.         }
  2185.         }
  2186.     }
  2187.       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN)
  2188.     bzero (last_set, nregs * sizeof (rtx));
  2189.     }
  2190.   *count_ptr = count;
  2191. }
  2192.  
  2193. /* Given a loop that is bounded by LOOP_START and LOOP_END
  2194.    and that is entered at SCAN_START,
  2195.    return 1 if the register set by insn INSN is used by
  2196.    any insn that precedes INSN in cyclic order starting
  2197.    from the loop entry point.  */
  2198.  
  2199. static int
  2200. loop_reg_used_before_p (insn, loop_start, scan_start, loop_end)
  2201.      rtx insn, loop_start, scan_start, loop_end;
  2202. {
  2203.   rtx reg = SET_DEST (PATTERN (insn));
  2204.   if (INSN_LUID (scan_start) > INSN_LUID (insn))
  2205.     return (reg_used_between_p (reg, scan_start, loop_end)
  2206.         || reg_used_between_p (reg, loop_start, insn));
  2207.   else
  2208.     return reg_used_between_p (reg, scan_start, insn);
  2209. }
  2210.  
  2211. /* A "basic induction variable" or biv is a pseudo reg that is set
  2212.    (within this loop) only by incrementing or decrementing it.  */
  2213. /* A "general induction variable" or giv is a pseudo reg whose
  2214.    value is a linear function of a biv.  */
  2215.  
  2216. /* Bivs are recognized by `basic_induction_var';
  2217.    Givs by `general_induct_var'.  */
  2218.  
  2219. /* An enum for the two different types of givs, those that are used
  2220.    as memory addresses and those that are calculated into registers.  */
  2221. enum g_types { DEST_ADDR, DEST_REG };
  2222.  
  2223. /* A `struct induction' is created for every instruction that sets
  2224.    an induction variable (either a biv or a giv).  */
  2225.  
  2226. struct induction
  2227. {
  2228.   rtx insn;               /* The insn that sets a biv or giv */
  2229.   rtx new_reg;               /* New register, containing strength reduced
  2230.                   version of this giv.  */
  2231.   int src_regno;           /* Biv from which this giv is computed.
  2232.                   (If this is a biv, then this is the biv.)  */
  2233.   enum g_types giv_type;       /* Indicate whether DEST_ADDR or DEST_REG giv */
  2234.   int dest_regno;           /* Destination register for insn: this is the
  2235.                   register which was the biv or giv.
  2236.                   For a biv, this equals src_reg.
  2237.                   For a DEST_ADDR type giv, this is 0.  */
  2238.   rtx *location;           /* Place in the insn where this giv occurs.
  2239.                   If GIV_TYPE is DEST_REG, this is 0.  */
  2240.   enum machine_mode mode;      /* The mode of this biv or giv */
  2241.   rtx mult_val;               /* Multiplicative factor for src_reg.  */
  2242.   rtx add_val;               /* Additive constant for that product.  */
  2243.   int benefit;               /* Gain from eliminating this insn.  */
  2244.   int consec;               /* The number of consecutive insn that set this
  2245.                   register; they are all eliminated if this
  2246.                   one is.  */
  2247.   char replaceable;           /* 1 if we can substitute the strength-reduced
  2248.                   variable for the original variable.
  2249.                   0 means they must be kept separate and the
  2250.                   new one must be copied into the old pseudo
  2251.                   reg each time the old one is set.  */
  2252.   char ignore;               /* 1 prohibits further processing of this giv */
  2253.   int lifetime;               /* Length of life of this giv */
  2254.   int times_used;           /* # times this giv is used. */
  2255.   struct induction *family;    /* Links together all induction variables that
  2256.                   have the same src register.  */
  2257.   struct induction *forces;    /* Points to an induction variable insn which
  2258.                   is used only once, to compute this giv,
  2259.                   and hence can be deleted if this insn is
  2260.                   strength reduced.  */
  2261.   struct induction *forces2;   /* Likewise.  */
  2262.   struct induction *same;      /* Links together all induction variables that
  2263.                   have the same tuple (src, mult, add).  */
  2264. };
  2265.  
  2266. /* A `struct iv_class' is created for each biv.  */
  2267.  
  2268. struct iv_class {
  2269.   int regno;                   /* Pseudo reg which is the biv.  */
  2270.   int biv_count;               /* Number of insns setting this reg.  */
  2271.   struct induction *biv;       /* List of all insns that set this reg.  */
  2272.   int giv_count;               /* Number of DEST_REG givs computed from this
  2273.                   biv.  The resulting count is only used in
  2274.                    check_dbra_loop.  */
  2275.   struct induction *giv;       /* List of all insns that compute a giv
  2276.                   from this reg.  */
  2277.   int total_benefit;           /* Sum of BENEFITs of all those givs */
  2278.   rtx initial_value;           /* Value of reg at loop start */
  2279.   struct iv_class *next;       /* Links all class structures together */
  2280.   rtx init_insn;           /* insn which intializes biv, 0 if none seen. */
  2281.   char eliminable;           /* 1 if plausible candidate for elimination.  */
  2282.   char nonneg;               /* 1 if we added a REG_NONNEG note for this.  */
  2283. };
  2284.  
  2285. /* Definitions used by the basic induction variable discovery code.  */
  2286. enum iv_mode { UNKNOWN_INDUCT, BASIC_INDUCT, NOT_BASIC_INDUCT,
  2287.          GENERAL_INDUCT };
  2288.  
  2289. /* Relative gain of eliminating various kinds of operations.  */
  2290. #define NO_BENEFIT    0
  2291. #define ADD_BENEFIT   1
  2292. #define SHIFT_BENEFIT 2
  2293. #define MULT_BENEFIT  4
  2294. #define LIBCALL_BENEFIT 15
  2295. /* Benefit penalty, if a giv is not replaceable, i.e. must emit an insn to
  2296.    copy the value of the strength reduced giv to its original register.  */
  2297. #define COPY_PENALTY  2
  2298.  
  2299. /* Indexed by register number, indicates whether or not register is an
  2300.    induction variable, and if so what type.  */
  2301.  
  2302. static enum iv_mode *induct_var;
  2303.  
  2304. /* Indexed by register number, contains pointer to `struct induction'
  2305.    if register is a general induction variable.  */
  2306.  
  2307. static struct induction **induct_struct;
  2308.  
  2309. /* Indexed by register number, contains pointer to `struct iv_class'
  2310.    if register is a basic induction variable.  */
  2311.  
  2312. static struct iv_class **class_struct;
  2313.  
  2314. /*********************************/
  2315.  
  2316. /* ??? Unfinished optimizations, wilson@ji.Berkeley.EDU */
  2317.  
  2318. /* strength reduce addresses found in sources (set () (mem ())*/
  2319.  
  2320. /* There is one more optimization you might be interested in doing: to
  2321.    allocate pseudo registers for frequently-accessed memory locations.
  2322.    If the same memory location is referenced each time around, it might
  2323.    be possible to copy it into a register before and out after.
  2324.    This is especially useful when the memory location is a variable which
  2325.    is in a stack slot because somewhere its address is taken.  If the
  2326.    loop doesn't contain a function call and the variable isn't volatile,
  2327.    it is safe to keep the value in a register for the duration of the
  2328.    loop. One tricky thing is that the copying of the value back from the
  2329.    register has to be done on all exits from the loop.  You need to check that
  2330.    all the exits from the loop go to the same place. */
  2331.  
  2332. /* WARNING: the interaction of biv elimination, and recognizing 'constant'
  2333.    bivs may cause problems */
  2334.  
  2335. /* add heuristic so that DEST_ADDR strength reduction does not cause
  2336.    performance problems */
  2337.  
  2338. /* don't eliminate things that can be combined with an addressing mode?
  2339.    find all giv that have same biv and mult_val (now must also have
  2340.    same add_val), then for each giv, check to see if its only use
  2341.    dies in a following memory address, generate a new memory address
  2342.    and check to see if valid, if valid then store modified mem addr,
  2343.    else if not valid addr mark giv as not done so that it will get its
  2344.    own iv */
  2345.  
  2346. /* consec_sets_giv does not calculate replaceable and forces correctly,
  2347.    forces should be a more general linked list instead of two entries */
  2348.  
  2349. /* try to optimize branches when it is known that a biv is always positive */
  2350.  
  2351. /* when replace biv in compare insn, should replace with closest giv so that
  2352.    an optimized branch can still be recognized by combiner, i.e. VAXen acb */
  2353.  
  2354. /* should merge final_value calculation in check_dbra_loop with the 
  2355.    new final_biv_value function */
  2356.  
  2357. /* many of the checks involving uid_luid could be simplified if regscan
  2358.    was rerun in loop_optimize() whenever a register was added or moved,
  2359.    also some of the optimizations could be a little less conservative */
  2360.  
  2361. /* Perform strength reduction and induction variable elimination.  */
  2362.  
  2363. /* Pseudo registers created during this function will be beyond the last
  2364.    valid index in several tables including n_times_set and regno_last_uid.
  2365.    This does not cause a problem here, because the added registers cannot be
  2366.    givs outside of their loop, and hence will never be reconsidered.
  2367.    But scan_loop must check regnos to make sure they are in bounds.  */
  2368.  
  2369. static void
  2370. strength_reduce (scan_start, end, loop_top, insn_count,
  2371.          loop_start, loop_end, nregs)
  2372.      rtx scan_start;
  2373.      rtx end;
  2374.      rtx loop_top;
  2375.      int insn_count;
  2376.      rtx loop_start;
  2377.      rtx loop_end;
  2378.      int nregs;
  2379. {
  2380.   rtx p;
  2381.   rtx inc_val;
  2382.   rtx mult_val;
  2383.   int dest_regno;
  2384.   int biv_found;
  2385.   /* This is 1 if current insn could be executed zero times in the loop.  */
  2386.   int maybe_never = 0;
  2387.   /* List of all possible basic induction variables.  */
  2388.   struct iv_class *iv_list = 0;
  2389.   /* Temporary list pointers for traversing iv_list.  */
  2390.   struct iv_class *bl, *backbl;
  2391.   /* Ratio of extra register life span we can justify
  2392.      for saving an instruction.  More if loop doesn't call subroutines
  2393.      since in that case saving an insn makes more difference
  2394.      and more registers are available.  */
  2395.   /* ??? could set this to last value of threshold in move_movables */
  2396.   int threshold = loop_has_call ? 17 : 34;
  2397.   /* Map of pseudo-register replacements.  */
  2398.   rtx *reg_map;
  2399.   int call_seen;
  2400.  
  2401.   induct_var = (enum iv_mode *) alloca (nregs * sizeof (induct_var[0]));
  2402.   bzero ((char *)induct_var, nregs * sizeof (induct_var[0]));
  2403.   induct_struct = (struct induction **)
  2404.     alloca (nregs * sizeof (struct induction *));
  2405.   bzero ((char *)induct_struct, nregs * sizeof (struct induction *));
  2406.   class_struct = (struct iv_class **)
  2407.     alloca (nregs * sizeof (struct iv_class *));
  2408.   bzero ((char *)class_struct, nregs * sizeof (struct iv_class *));
  2409.  
  2410.   /* Scan through loop to find all possible bivs.  */
  2411.  
  2412.   for (p = NEXT_INSN (loop_start); p != end; p = NEXT_INSN (p))
  2413.     {
  2414.       if (GET_CODE (p) == INSN
  2415.       && GET_CODE (PATTERN (p)) == SET
  2416.       && GET_CODE (SET_DEST (PATTERN (p))) == REG)
  2417.     {
  2418.       dest_regno = REGNO (SET_DEST (PATTERN (p)));
  2419.       if (induct_var[dest_regno] != NOT_BASIC_INDUCT
  2420.           && dest_regno >= FIRST_PSEUDO_REGISTER)
  2421.         {
  2422.           if (basic_induction_var (SET_SRC (PATTERN (p)), dest_regno,
  2423.                       &inc_val, &mult_val))
  2424.         {
  2425.           /* It is a possible basic induction variable.
  2426.              Create and initialize an induction structure for it.  */
  2427.  
  2428.           struct induction *v =
  2429.             (struct induction *) alloca (sizeof (struct induction));
  2430.  
  2431.           v->insn = p;
  2432.           v->src_regno = dest_regno;
  2433.           v->dest_regno = dest_regno;
  2434.           v->mult_val = mult_val;
  2435.           v->add_val = inc_val;
  2436.           v->mode = GET_MODE (SET_DEST (PATTERN (p)));
  2437.  
  2438.           /* Add this to the reg's iv_class, creating a class
  2439.              if this is the first incrementation of the reg.  */
  2440.  
  2441.           bl = class_struct[dest_regno];
  2442.           if (bl)
  2443.             {
  2444.               v->family = bl->biv;
  2445.               bl->biv = v;
  2446.               bl->biv_count++;
  2447.             }
  2448.           else
  2449.             {
  2450.               /* Create and initialize new iv_class.  */
  2451.  
  2452.               bl = (struct iv_class *) alloca (sizeof (struct iv_class));
  2453.  
  2454.               bl->regno = dest_regno;
  2455.               bl->biv = v;
  2456.               v->family = 0;
  2457.               bl->giv = 0;
  2458.               bl->biv_count = 1;
  2459.               bl->giv_count = 0;
  2460.  
  2461.               /* Set initial value to the reg itself.  */
  2462.               bl->initial_value = SET_DEST (PATTERN (p));
  2463.               /* We haven't seen the intializing insn yet */
  2464.               bl->init_insn = 0;
  2465.               bl->eliminable = 0;
  2466.               bl->nonneg = 0;
  2467.  
  2468.               /* Add this insn to iv_list.  */
  2469.               bl->next = iv_list;
  2470.               iv_list = bl;
  2471.  
  2472.               /* Put it in the array of iv_lists.  */
  2473.               class_struct[dest_regno] = bl;
  2474.             }
  2475.  
  2476.           induct_var[dest_regno] = BASIC_INDUCT;
  2477.  
  2478.           if (loop_dump_stream)
  2479.             {
  2480.               fprintf (loop_dump_stream,
  2481.                    "Insn %d: possible biv, reg %d,",
  2482.                    INSN_UID (p), dest_regno);
  2483.               if (GET_CODE (inc_val) == CONST_INT)
  2484.             fprintf (loop_dump_stream, " const = %d\n",
  2485.                  INTVAL (inc_val));
  2486.               else
  2487.             {
  2488.               fprintf (loop_dump_stream, " const = ");
  2489.               print_rtl (loop_dump_stream, inc_val);
  2490.               fprintf (loop_dump_stream, "\n");
  2491.             }
  2492.             }
  2493.         }
  2494.           else
  2495.         induct_var[dest_regno] = NOT_BASIC_INDUCT;
  2496.         }
  2497.     }
  2498.     }
  2499.  
  2500.   /* Scan iv_list to remove all regs that proved not to be bivs.
  2501.      Make a sanity check against n_times_set.  */
  2502.  
  2503.   biv_found = 0;
  2504.  
  2505.   for (backbl = bl = iv_list; bl; backbl = bl, bl = bl->next)
  2506.     {
  2507.       if (induct_var[bl->regno] != BASIC_INDUCT)
  2508.     {
  2509.       /* Not a basic induction variable, remove this iv_class.  */
  2510.  
  2511.       if (backbl == bl)
  2512.         iv_list = bl->next;
  2513.       else
  2514.         backbl->next = bl->next;
  2515.  
  2516.       if (loop_dump_stream)
  2517.         fprintf (loop_dump_stream, "Reg %d: biv discarded, not induct\n",
  2518.             bl->regno);
  2519.     }
  2520.       else if (n_times_set[bl->regno] != bl->biv_count)
  2521.     {
  2522.       /* This happens if register modified by subreg, etc.  */
  2523.       /* Make sure it is not recognized as a basic induction var: */
  2524.       /* remove this iv_class from iv_list.  */
  2525.  
  2526.       induct_var[bl->regno] = NOT_BASIC_INDUCT;
  2527.  
  2528.       if (backbl == bl)
  2529.         iv_list = bl->next;
  2530.       else
  2531.         backbl->next = bl->next;
  2532.  
  2533.       if (loop_dump_stream)
  2534.         fprintf (loop_dump_stream, "Reg %d: biv discarded, count error\n",
  2535.             bl->regno);
  2536.     }
  2537.       else
  2538.     {
  2539.       /* This is a valid basic induction variable.  */
  2540.  
  2541.       biv_found++;
  2542.  
  2543.       if (loop_dump_stream)
  2544.         fprintf (loop_dump_stream, "Reg %d: biv verified\n", bl->regno);
  2545.     }
  2546.     }
  2547.  
  2548.   /* Exit if there are no bivs.  */
  2549.   if (!iv_list)
  2550.     return;
  2551.  
  2552.   /* Find initial value for each biv.  */
  2553.   /* Search backwards from loop_start, halting at first label
  2554.      or when all bivs have been seen.  */
  2555.  
  2556.   call_seen = 0;
  2557.   p = loop_start;
  2558.   while (biv_found)
  2559.     {
  2560.       p = PREV_INSN (p);
  2561.       if (p == 0)
  2562.     break;
  2563.  
  2564.       if (GET_CODE (p) == CALL_INSN)
  2565.     call_seen = 1;
  2566.  
  2567.       if (GET_CODE (p) == INSN
  2568.       && GET_CODE (PATTERN (p)) == SET)
  2569.     {
  2570.       rtx dest = SET_DEST (PATTERN (p));
  2571.  
  2572.       while (GET_CODE (dest) == SUBREG
  2573.          || GET_CODE (dest) == ZERO_EXTRACT
  2574.          || GET_CODE (dest) == SIGN_EXTRACT
  2575.          || GET_CODE (dest) == STRICT_LOW_PART)
  2576.         dest = XEXP (dest, 0);
  2577.  
  2578.       if (GET_CODE (dest) == REG)
  2579.         {
  2580.           int dest_regno = REGNO (dest);
  2581.           if (induct_var[dest_regno] == BASIC_INDUCT
  2582.           && class_struct[dest_regno]->init_insn == 0)
  2583.         {
  2584.           /* This is the first modification found for this reg.  */
  2585.  
  2586.           rtx src = SET_SRC (PATTERN (p));
  2587.  
  2588.           /* Record the intializing INSN */
  2589.  
  2590.           class_struct[dest_regno]->init_insn = p;
  2591.  
  2592.           if (loop_dump_stream)
  2593.             fprintf (loop_dump_stream, "Biv %d initialized at insn %d: ",
  2594.                  dest_regno, INSN_UID (p));
  2595.  
  2596.           /* Save value if it is a constant or register.  */
  2597.           if (CONSTANT_P (src)
  2598.               || (GET_CODE (src) == REG
  2599.               /* Don't try to use a value in a hard reg
  2600.                  across a call which clobbers it.  */
  2601.               && ! (REGNO (src) < FIRST_PSEUDO_REGISTER
  2602.                 && call_used_regs[REGNO (src)]
  2603.                 && call_seen)
  2604.               && ! reg_set_between_p (src, p, loop_start)))
  2605.             {
  2606.               class_struct[dest_regno]->initial_value = src;
  2607.  
  2608.               if (loop_dump_stream)
  2609.             fprintf (loop_dump_stream, "initial value ");
  2610.               if (loop_dump_stream)
  2611.             {
  2612.               if (GET_CODE (src) == CONST_INT)
  2613.                 fprintf (loop_dump_stream, "%d\n", INTVAL (src));
  2614.               else
  2615.                 {
  2616.                   print_rtl (loop_dump_stream, src);
  2617.                   fprintf (loop_dump_stream, "\n");
  2618.                 }
  2619.             }
  2620.             }
  2621.           else
  2622.             {
  2623.               /* Biv initial value is not simple move,
  2624.              so let it keep intial value of "itself".  */
  2625.  
  2626.               if (loop_dump_stream)
  2627.             fprintf (loop_dump_stream, "complex initial value\n");
  2628.             }
  2629.  
  2630.           biv_found--;
  2631.         }
  2632.         }
  2633.     }
  2634.       else if (GET_CODE (p) == CODE_LABEL)
  2635.     break;
  2636.     }
  2637.  
  2638.   /* Search the loop for general induction variables.  */
  2639.  
  2640.   /* A register is a giv if: it is only set once, it is a function of a
  2641.      biv and a constant (or invariant), and it is not a biv.  */
  2642.  
  2643.   p = scan_start;
  2644.   while (1)
  2645.     {
  2646.       p = NEXT_INSN (p);
  2647.       /* At end of a straight-in loop, we are done.
  2648.      At end of a loop entered at the bottom, scan the top.  */
  2649.       if (p == scan_start)
  2650.     break;
  2651.       if (p == end)
  2652.     {
  2653.       if (loop_top != 0)
  2654.         p = NEXT_INSN (loop_top);
  2655.       else
  2656.         break;
  2657.       if (p == scan_start)
  2658.         break;
  2659.     }
  2660.  
  2661.       /* Look for a general induction variable in a register.  */
  2662.       if (GET_CODE (p) == INSN
  2663.       && GET_CODE (PATTERN (p)) == SET
  2664.       && GET_CODE (SET_DEST (PATTERN (p))) == REG)
  2665.     {
  2666.       int src_regno;
  2667.       rtx add_val;
  2668.       rtx mult_val;
  2669.       int benefit;
  2670.       rtx regnote = 0;
  2671.       struct induction *forces = 0;
  2672.       struct induction *forces2 = 0;
  2673.  
  2674.       dest_regno = REGNO (SET_DEST (PATTERN (p)));
  2675.       if (dest_regno < FIRST_PSEUDO_REGISTER)
  2676.         continue;
  2677.  
  2678.       if (/* Normal giv.  */
  2679.           ((benefit = general_induction_var (SET_SRC (PATTERN (p)),
  2680.                          &src_regno, &add_val,
  2681.                          &mult_val,
  2682.                          &forces, &forces2))
  2683.            /* Giv set with call to a library routine.  */
  2684.            || ((regnote = find_reg_note (p, REG_EQUAL, 0))
  2685.            &&
  2686.            (benefit = general_induction_var (XEXP (regnote, 0),
  2687.                              &src_regno,
  2688.                              &add_val, &mult_val,
  2689.                              &forces, &forces2))))
  2690.           /* Don't try to handle any regs made by loop optimization.
  2691.          We have nothing on them in regno_first_uid, etc.  */
  2692.           && dest_regno < old_max_reg
  2693.           /* Don't recognize a BASIC_INDUCT_VAR here.  */
  2694.           && dest_regno != src_regno
  2695.           /* This must be the only place where the register is set.  */
  2696.           && (n_times_set[dest_regno] == 1
  2697.           || (benefit = consec_sets_giv (benefit, p,
  2698.                          src_regno, dest_regno,
  2699.                          &add_val, &mult_val))))
  2700.         {
  2701.           int count;
  2702.           struct induction *v =
  2703.         (struct induction *) alloca (sizeof (struct induction));
  2704.           rtx temp;
  2705.  
  2706.           record_giv (v, p, src_regno, dest_regno, mult_val, add_val, benefit,
  2707.               forces, forces2, DEST_REG, maybe_never, 0, loop_end);
  2708.  
  2709.           /* Skip the consecutive insns, if there are any.  */
  2710.           for (count = v->consec - 1; count >= 0; count--)
  2711.         {
  2712.           /* If first insn of libcall sequence, skip to end.  */
  2713.           /* Do this at start of loop, since INSN is guaranteed to
  2714.              be an insn here.  */
  2715.           if (temp = find_reg_note (p, REG_LIBCALL, 0))
  2716.             {
  2717.               /* Eliminating a libcall does more good than
  2718.              eliminating a single insn to do the same job.  */
  2719.               benefit += LIBCALL_BENEFIT;
  2720.               p = XEXP (temp, 0);
  2721.             }
  2722.  
  2723.           do p = NEXT_INSN (p);
  2724.           while (GET_CODE (p) == NOTE);
  2725.         }
  2726.         }
  2727.     }
  2728.  
  2729. #ifndef DONT_REDUCE_ADDR
  2730.       /* Look for givs which are memory addresses.  */
  2731.       /* This resulted in worse code on a VAX 8600.  I wonder if it
  2732.      still does.  */
  2733.       if (GET_CODE (p) == INSN)
  2734.     find_mem_givs (PATTERN (p), p, maybe_never, loop_end);
  2735. #endif
  2736.  
  2737.       /* Past a label or a jump, we get to insns for which we can't count
  2738.      on whether or how many times they will be executed during each
  2739.      iteration.  Givs found afterwards cannot be marked replaceable.  */
  2740.       if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == JUMP_INSN)
  2741.     maybe_never = 1;
  2742.     }
  2743.  
  2744.   /* Try to prove that the loop counter variable (if any) is always
  2745.      nonnegative; if so, record that fact with a REG_NONNEG note
  2746.      so that "decrement and branch until zero" insn can be used.  */
  2747.   check_dbra_loop (loop_end, iv_list, insn_count, loop_start);
  2748.  
  2749.   /* Create reg_map to hold substitutions for replaceable giv regs.  */
  2750.   reg_map = (rtx *) alloca (nregs * sizeof (rtx));
  2751.   bzero ((char *)reg_map, nregs * sizeof (rtx));
  2752.  
  2753.   /* Examine each iv class for feasibility of strength reduction/induction
  2754.      variable elimination.  */
  2755.  
  2756.   for (bl = iv_list; bl; bl = bl->next)
  2757.     {
  2758.       struct induction *v;
  2759.       int benefit;
  2760.       int replaceable;
  2761.       int all_reduced;
  2762.       rtx final_value = 0;
  2763.  
  2764.       /* Test whether it will be possible to eliminate this biv
  2765.      provided all givs are reduced.  This is possible if either
  2766.      the reg is not used outside the loop, or we can compute
  2767.      what its final value will be.
  2768.  
  2769.      Don't try if we put a REG_NONNEG note on the endtest for this biv.
  2770.      ??? That should be only on machines that have dbra insns.  */
  2771.  
  2772.       /* Compare against bl->init_insn rather than loop_start.
  2773.      We aren't concerned with any uses of the biv between
  2774.      init_insn and loop_start since these won't be affected
  2775.      by the value of the biv elsewhere in the function, so
  2776.      long as init_insn doesn't use the biv itself.
  2777.      March 14, 1989 -- self@bayes.arc.nasa.gov */
  2778.  
  2779.       if ((uid_luid[regno_last_uid[bl->regno]] < INSN_LUID (loop_end)
  2780.        && bl->init_insn
  2781.        && INSN_UID (bl->init_insn) < max_uid
  2782.        && uid_luid[regno_first_uid[bl->regno]] >= INSN_LUID (bl->init_insn)
  2783.        && ! reg_mentioned_p (SET_DEST (PATTERN (bl->biv->insn)),
  2784.                  SET_SRC (PATTERN (bl->init_insn)))
  2785.        && ! bl->nonneg)
  2786.       || (final_value = final_biv_value (bl, loop_end)))
  2787.     check_eliminate_biv (bl, loop_start, end);
  2788.       else
  2789.     {
  2790.       if (loop_dump_stream)
  2791.         {
  2792.           fprintf (loop_dump_stream,
  2793.                "Cannot eliminate biv %d.\n",
  2794.                bl->regno);
  2795.           fprintf (loop_dump_stream,
  2796.                "First use: insn %d, last use: insn %d.\n",
  2797.                regno_first_uid[bl->regno],
  2798.                regno_last_uid[bl->regno]);
  2799.         }
  2800.     }
  2801.  
  2802.       /* This will be true at the end, if all givs which depend on this
  2803.      biv have been strength reduced.
  2804.      We can't (currently) eliminate the biv unless this is so.  */
  2805.       all_reduced = 1;
  2806.  
  2807.       /* Check each giv in this class.  */
  2808.  
  2809.       for (v = bl->giv; v; v = v->family)
  2810.     {
  2811.       struct induction *tv;
  2812.  
  2813.       if (v->ignore)
  2814.         continue;
  2815.  
  2816.       benefit = v->benefit;
  2817.       replaceable = v->replaceable;
  2818.  
  2819.       /* Reduce benefit if not replaceable, since we will insert
  2820.          a move-insn to replace the insn that calculates this giv.  */
  2821.       if (!replaceable && ! bl->eliminable)
  2822.         benefit -= COPY_PENALTY;
  2823.  
  2824.       /* Decrease the benefit to count the add-insns that we will
  2825.          insert to increment the reduced reg for the giv.  */
  2826.       benefit -= ADD_BENEFIT * bl->biv_count;
  2827.  
  2828.       /* Find all equivalent givs (that bear same relation to the biv).
  2829.          Link them via the `same' field and add their benefits together.
  2830.          They can be replaced with a single register.  */
  2831.  
  2832.       for (tv = v->family; tv; tv = tv->family)
  2833.         {
  2834.           if (tv->ignore == 0
  2835.           && tv->src_regno == v->src_regno
  2836.           && rtx_equal_p (tv->mult_val, v->mult_val)
  2837.           && rtx_equal_p (tv->add_val, v->add_val))
  2838.         {
  2839.           benefit += tv->benefit;
  2840.           if (! tv->replaceable)
  2841.             benefit -= COPY_PENALTY;
  2842.           v->lifetime += tv->lifetime;
  2843.           v->times_used += tv->times_used;
  2844.           tv->ignore = 1;
  2845.  
  2846.           /* Link them together via `same' field.  */
  2847.           tv->same = v->same;
  2848.           v->same = tv;
  2849.  
  2850.           if (loop_dump_stream)
  2851.             fprintf (loop_dump_stream,
  2852.                  "giv of insn %d combined with that of %d.\n",
  2853.                  INSN_UID (v->insn), INSN_UID (tv->insn));
  2854.         }
  2855.         }
  2856.  
  2857.       /* Decide whether to strength-reduce this giv
  2858.          or to leave the code unchanged
  2859.          (recompute it from the biv each time it is used).
  2860.          This decision can be made independently for each giv.  */
  2861.  
  2862.       /* ??? Perhaps attempt to guess whether autoincrement will handle
  2863.          some of the new add insns; if so, can increase BENEFIT
  2864.          (undo the subtraction of ADD_BENEFIT that was done above).  */
  2865.  
  2866.       /* If an insn is not to be strength reduced, then set its ignore
  2867.          flag, and clear all_reduced.  */
  2868.  
  2869.       /* Is it right to consider times_used?  */
  2870.  
  2871.       /* ??? What about the insns that are 'forced' by this one?
  2872.          Although this insn is not worthwhile to reduce, it may be
  2873.          worthwhile to reduce the simpler givs used to compute this 
  2874.          complex giv.  */
  2875.  
  2876.       /* ??? Hey! If a giv has its forces field set, then that means
  2877.          it is not computed directly from the biv, it is instead computed
  2878.          from a simpler giv.  If we define UNFORCE_INSNS, then the simpler
  2879.          giv will be considered for strength reduction, and this giv should
  2880.          not cause all_reduced to be cleared because it DOESN'T use the
  2881.          biv!!!  If the simpler giv can not be reduced, then that simpler
  2882.          biv will still cause all_reduced to be cleared.  */
  2883.  
  2884.       if (benefit <= 0)
  2885.         {
  2886.           if (loop_dump_stream)
  2887.         fprintf (loop_dump_stream, "giv of insn %d, no benefit\n",
  2888.              INSN_UID (v->insn));
  2889.           v->ignore = 1;
  2890.           all_reduced = 0;
  2891.         }
  2892.  
  2893.       if (v->lifetime * threshold * benefit < insn_count)
  2894.         {
  2895.           if (loop_dump_stream)
  2896.         fprintf (loop_dump_stream,
  2897.              "giv of insn %d not worth while, %d vs %d.\n",
  2898.              INSN_UID (v->insn),
  2899.              v->lifetime * threshold * benefit, insn_count);
  2900.           v->ignore = 1;
  2901.           all_reduced = 0;
  2902.         }
  2903.  
  2904.       /* Now check that we can increment the reduced giv
  2905.          without needing a multiply insn.  If not, reject it.  */
  2906.  
  2907.       if (! v->ignore)
  2908.         {
  2909.           int success = 1;
  2910.  
  2911.           for (tv = bl->biv; tv; tv = tv->family)
  2912.         if (tv->mult_val == const1_rtx)
  2913.           success &= product_cheap_p (tv->add_val, v->mult_val);
  2914.  
  2915.           if (! success)
  2916.         {
  2917.           if (loop_dump_stream)
  2918.             fprintf (loop_dump_stream,
  2919.                  "giv of insn %d: would need a multiply.\n",
  2920.                  INSN_UID (v->insn));
  2921.           v->ignore = 1;
  2922.           all_reduced = 0;
  2923.         }
  2924.         }
  2925.     }
  2926.  
  2927.       /* Reduce each giv that we decided to reduce.  */
  2928.  
  2929.       for (v = bl->giv; v; v = v->family)
  2930.     {
  2931.       struct induction *tv;
  2932.       if (! v->ignore)
  2933.         {
  2934.           rtx new_reg;
  2935.  
  2936.           /* Note Iris compiler dies if ?: is used inside gen_reg_rtx. */
  2937.           if (v->giv_type == DEST_ADDR)
  2938.             new_reg = gen_reg_rtx (Pmode);
  2939.           else
  2940.             new_reg = gen_reg_rtx (GET_MODE (SET_DEST (PATTERN (v->insn))));
  2941.  
  2942.           /* For each place where the biv is incremented,
  2943.          add an insn to increment the new, reduced reg for the giv.
  2944.          Insert it before the insn that sets the biv,
  2945.          so that the biv increment remains last before the endtest,
  2946.          so that dbra will still be recognized.  */
  2947.  
  2948.           for (tv = bl->biv; tv; tv = tv->family)
  2949.         {
  2950.           struct induction *iv;
  2951.           rtx before_insn = tv->insn;
  2952.  
  2953.           /* If this increment is between the setting of the giv and
  2954.              its use, don't increment until after the use.  */
  2955.           for (iv = v; iv; iv = iv->same)
  2956.             {
  2957.               if (INSN_LUID (tv->insn) <= INSN_LUID (iv->insn)
  2958.               && ((iv->forces
  2959.                    && (INSN_LUID (tv->insn)
  2960.                    >= INSN_LUID (iv->forces->insn))
  2961.                   || (iv->forces2
  2962.                   && (INSN_LUID (tv->insn)
  2963.                       >= INSN_LUID (iv->forces2->insn))))))
  2964.             {
  2965.               before_insn = NEXT_INSN (iv->insn);
  2966.               break;
  2967.             }
  2968.             }
  2969.  
  2970.           if (tv->mult_val == const1_rtx)
  2971.             emit_iv_inc (tv->add_val, v->mult_val,
  2972.                  new_reg, before_insn);
  2973.           else /* tv->mult_val == const0_rtx */
  2974.             /* A multiply is acceptable here
  2975.                since this is presumed to be seldom executed.  */
  2976.             emit_iv_init_code (tv->add_val, v->mult_val,
  2977.                        v->add_val, new_reg, before_insn);
  2978.         }
  2979.  
  2980.           /* Add code at loop start to initialize giv's reduced reg.  */
  2981.  
  2982.           emit_iv_init_code (bl->initial_value, v->mult_val,
  2983.                  v->add_val, new_reg, loop_start);
  2984.           /* If the initial value uses a register,
  2985.          then we may have just extended its range of appearance.
  2986.          Update this conservatively for the sake of outer loops.  */
  2987.           if (GET_CODE (bl->initial_value) == REG
  2988.           && (uid_luid[regno_last_uid[REGNO (bl->initial_value)]]
  2989.               < INSN_LUID (loop_start)))
  2990.         uid_luid[regno_last_uid[REGNO (bl->initial_value)]]
  2991.           = INSN_LUID (loop_start);
  2992.  
  2993.           /* For each giv register that can be reduced now:
  2994.          delete old insn that modifies the giv,
  2995.          if replaceable, substitute reduced reg
  2996.            wherever the old giv occurs;
  2997.          else add new move insn "giv_reg = reduced_reg".  */
  2998.  
  2999.           for (tv = v; tv; tv = tv->same)
  3000.         {
  3001.           /* Record the identity of the reduced reg.  */
  3002.           tv->new_reg = new_reg;
  3003.  
  3004.           if (tv->giv_type == DEST_ADDR)
  3005.             {
  3006.               /* Store reduced reg as the address in the memref
  3007.              where we found this giv.  */
  3008.               * tv->location = new_reg;
  3009.             }
  3010.           else if (tv->replaceable)
  3011.             {
  3012.               reg_map[tv->dest_regno] = new_reg;
  3013.               /* If giv lives after end of loop,
  3014.              emit insn to copy reduced reg into old reg,
  3015.              at the end of the loop.
  3016.              ?? insufficient; used before loop could
  3017.              mean live after loop, due to surrounding loop.  */
  3018.               /* Currently a giv used outside
  3019.              the loop will not be marked replaceable,
  3020.              so these deficiencies don't really hurt.  */
  3021.               if (uid_luid[regno_last_uid[tv->dest_regno]]
  3022.               > uid_luid[INSN_UID (loop_end)])
  3023.             {
  3024.               /* ?? This won't work.  We need to do this at
  3025.                  ALL exits.  */
  3026.               emit_insn_after (gen_rtx (SET, VOIDmode,
  3027.                             SET_DEST (PATTERN (tv->insn)),
  3028.                             new_reg),
  3029.                        loop_end);
  3030.               abort ();
  3031.             }
  3032.             }
  3033.           else
  3034.             {
  3035.               /* Not replaceable; emit an insn to set the
  3036.              original giv reg from the reduced giv.  */
  3037.  
  3038.               int count;
  3039.               rtx after_insn = tv->insn;
  3040.  
  3041.               for (count = tv->consec; count > 0; count--)
  3042.             after_insn = next_real_insn (after_insn);
  3043.  
  3044.               /* Put new insn after, not before, in case
  3045.              after_insn is the end of a libcall.  */
  3046.               emit_insn_after (gen_rtx (SET, VOIDmode,
  3047.                         SET_DEST (PATTERN (tv->insn)),
  3048.                         new_reg),
  3049.                        after_insn);
  3050.             }
  3051.  
  3052.           /* Delete the insn that used to set the old giv reg,
  3053.              unless we modified an address in it.
  3054.              In any case, delete the other insns used for this one.  */
  3055.           delete_insn_forces (tv, tv->giv_type != DEST_ADDR);
  3056.  
  3057.           if (loop_dump_stream)
  3058.             fprintf (loop_dump_stream, "giv at %d reduced to reg %d\n",
  3059.                  INSN_UID (tv->insn), REGNO (new_reg));
  3060.         }
  3061.           /* One set of equivalent givs has been strength-reduced.  */
  3062.         }
  3063. #if 0
  3064.       else if (v->new_reg == 0)
  3065.         {
  3066.           /* This giv wasn't reduced and is not worth reducing.  */
  3067.  
  3068.           for (tv = v; tv; tv = tv->same)
  3069.         if (loop_dump_stream)
  3070.           fprintf (loop_dump_stream, "giv at %d not reduced\n",
  3071.                INSN_UID (tv->insn));
  3072.  
  3073.           all_reduced = 0;
  3074.         }
  3075. #endif
  3076.     }
  3077.  
  3078.       /* All the givs in this family have been reduced if they merit it.  */
  3079.  
  3080.       /* Try to eliminate the biv, if it is a candidate.
  3081.      This won't work if ! all_reduced,
  3082.      since the givs we planned to use might not have been reduced.  */
  3083.  
  3084.       if (all_reduced == 1 && bl->eliminable)
  3085.     {
  3086.       /* Get the REG rtx for the biv.  */
  3087.       rtx reg = SET_DEST (PATTERN (bl->biv->insn));
  3088.  
  3089.       for (p = loop_start; p != end; p = NEXT_INSN (p))
  3090.         {
  3091.           enum rtx_code code = GET_CODE (p);
  3092.           if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
  3093.           && reg_mentioned_p (reg, PATTERN (p))
  3094.           && SET_DEST (PATTERN (p)) == cc0_rtx)
  3095.         /* Found a compare instruction using this biv;
  3096.            rewrite it to use a related giv.  */
  3097.         {
  3098.           struct induction *v1;
  3099.           /* If this is an insn which uses the biv ONLY in the
  3100.              calculation of a giv which is in the family of this
  3101.              biv, it's ok becuase it will go away when the giv is
  3102.              reduced.  */
  3103.           for (v1 = bl->giv; v1; v1 = v1->family)
  3104.             if (v1->insn == p)
  3105.               {
  3106.             if (v1->giv_type == DEST_REG
  3107.                 || (v1->giv_type == DEST_ADDR
  3108.                 /* Test was backwards - rms, 5 Dec 89 */
  3109.                 && only_reg_use_p (reg, *(v1->location),
  3110.                            PATTERN (p))))
  3111.               break;
  3112.               }
  3113.           if (!v1)
  3114.             eliminate_biv (p, bl, loop_start);
  3115.         }
  3116.         }
  3117.  
  3118.       /* Biv is no longer really needed inside the loop,
  3119.          so delete all insns that set the biv.  */
  3120.  
  3121.       for (v = bl->biv; v; v = v->family)
  3122.         delete_insn (v->insn);
  3123.  
  3124.       /* ?? If we created a new test to bypass the loop entirely,
  3125.          or otherwise drop straight in, based on this test, then
  3126.          we might want to rewrite it also.  This way some later
  3127.          pass has more hope of removing the intialization of this
  3128.          biv entirely. */
  3129.  
  3130.       /* If final_value != 0, then biv may be used after loop end
  3131.          and we must emit an insn to set it just in case.  */
  3132.       if (final_value != 0)
  3133.         emit_insn_after (gen_rtx (SET, VOIDmode, reg, final_value),
  3134.                  loop_end);
  3135.  
  3136.       if (loop_dump_stream)
  3137.         fprintf (loop_dump_stream, "Reg %d: biv eliminated\n",
  3138.              bl->regno);
  3139.     }
  3140.     }
  3141.  
  3142.   /* Go through all the instructions in the loop, making all the
  3143.      register substitutions scheduled in REG_MAP.  */
  3144.  
  3145.   for (p = loop_start; p != end; p = NEXT_INSN (p))
  3146.     if (GET_CODE (p) == INSN || GET_CODE (p) == JUMP_INSN
  3147.      || GET_CODE (p) == CALL_INSN)
  3148.       replace_regs (PATTERN (p), reg_map, nregs);
  3149.  
  3150.   if (loop_dump_stream)
  3151.     fprintf (loop_dump_stream, "\n");
  3152. }
  3153.  
  3154. /* Nonzero if register REG appears somewhere within IN, except in
  3155.    subexpressions EQ to EXPR.  This is a modification of reg_mentioned_p.  */
  3156.  
  3157. int
  3158. only_reg_use_p (reg, expr, in)
  3159.      register rtx reg, expr, in;
  3160. {
  3161.   register char *fmt;
  3162.   register int i;
  3163.   register enum rtx_code code;
  3164.  
  3165.   if (in == 0)
  3166.     return 0;
  3167.  
  3168.   if (reg == expr)
  3169.     return 0;
  3170.  
  3171.   if (reg == in)
  3172.     return 1;
  3173.  
  3174.   code = GET_CODE (in);
  3175.  
  3176.   switch (code)
  3177.     {
  3178.       /* Compare registers by number.  */
  3179.     case REG:
  3180.       return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
  3181.  
  3182.       /* These codes have no constituent expressions
  3183.      and are unique.  */
  3184.     case CC0:
  3185.     case PC:
  3186.     case CONST_INT:
  3187.     case CONST_DOUBLE:
  3188.     case SYMBOL_REF:
  3189.     case CODE_LABEL:
  3190.       return 0;
  3191.     }
  3192.  
  3193.   fmt = GET_RTX_FORMAT (code);
  3194.  
  3195.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  3196.     {
  3197.       if (fmt[i] == 'E')
  3198.     {
  3199.       register int j;
  3200.       for (j = XVECLEN (in, i) - 1; j >= 0; j--)
  3201.         if (only_reg_use_p (reg, expr, XVECEXP (in, i, j)))
  3202.           return 1;
  3203.     }
  3204.       else if (fmt[i] == 'e'
  3205.            && only_reg_use_p (reg, expr, XEXP (in, i)))
  3206.     return 1;
  3207.     }
  3208.   return 0;
  3209. }
  3210.  
  3211. /* Scan X for memory refs and check each memory address
  3212.    as a possible giv.  INSN is the insn whose pattern X comes from.
  3213.    MAYBE_NEVER is 1 if the loop might execute INSN zero times.  */
  3214.  
  3215. static void
  3216. find_mem_givs (x, insn, maybe_never, loop_end)
  3217.      rtx x;
  3218.      rtx insn;
  3219.      int maybe_never;
  3220.      rtx loop_end;
  3221. {
  3222.   register int i, j;
  3223.   register enum rtx_code code;
  3224.   register char *fmt;
  3225.  
  3226.   if (x == 0)
  3227.     return;
  3228.  
  3229.   code = GET_CODE (x);
  3230.   switch (code)
  3231.     {
  3232.     case REG:
  3233.     case CONST_INT:
  3234.     case CONST:
  3235.     case CONST_DOUBLE:
  3236.     case SYMBOL_REF:
  3237.     case LABEL_REF:
  3238.     case PC:
  3239.     case CC0:
  3240.     case ADDR_VEC:
  3241.     case ADDR_DIFF_VEC:
  3242.     case USE:
  3243.     case CLOBBER:
  3244.       return;
  3245.  
  3246.     case MEM:
  3247.       {
  3248.     int src_regno;
  3249.     rtx add_val;
  3250.     rtx mult_val;
  3251.     int benefit;
  3252.     struct induction *forces = 0;
  3253.     struct induction *forces2 = 0;
  3254.  
  3255.     benefit = general_induction_var (XEXP (x, 0),
  3256.                      &src_regno, &add_val, &mult_val,
  3257.                      &forces, &forces2);
  3258.     if (benefit > 0)
  3259.       {
  3260.         /* Found one; record it.  */
  3261.         struct induction *v =
  3262.           (struct induction *) oballoc (sizeof (struct induction));
  3263.  
  3264.         record_giv (v, insn, src_regno, 0, mult_val, add_val, benefit,
  3265.             forces, forces2, DEST_ADDR, maybe_never, &XEXP (x, 0),
  3266.             loop_end);
  3267.       }
  3268.     return;
  3269.       }
  3270.     }
  3271.  
  3272.   /* Recursively scan the subexpressions for other mem refs.  */
  3273.  
  3274.   fmt = GET_RTX_FORMAT (code);
  3275.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  3276.     if (fmt[i] == 'e')
  3277.       find_mem_givs (XEXP (x, i), insn, maybe_never, loop_end);
  3278.     else if (fmt[i] == 'E')
  3279.       for (j = 0; j < XVECLEN (x, i); j++)
  3280.     find_mem_givs (XVECEXP (x, i, j), insn, maybe_never, loop_end);
  3281. }
  3282.  
  3283. /* Fill in the data about one giv.
  3284.    V is the `struct induction' in which we record the giv.  (It is
  3285.    allocated by the caller, with alloca.)
  3286.    INSN is the insn that sets it.
  3287.    BENEFIT estimates the savings from deleting this insn.
  3288.    TYPE is DEST_REG or DEST_ADDR; it says whether the giv is computed
  3289.    into a register or is used as a memory address.
  3290.  
  3291.    SRC_REGNO is the biv reg number which the giv is computed from.
  3292.    DEST_REGNO is the giv's reg number (if the giv is stored in a reg).
  3293.    MULT_VAL and ADD_VAL are the coefficients used to compute the giv.
  3294.    FORCES and FORCES2, if nonzero, are other `struct induction's for
  3295.    other givs which are used to compute this giv indirectly.
  3296.    LOCATION points to the place where this giv's value appears in INSN.  */
  3297.  
  3298. static void
  3299. record_giv (v, insn, src_regno, dest_regno, mult_val, add_val, benefit,
  3300.         forces, forces2, type, maybe_never, location, loop_end)
  3301.      struct induction *v;
  3302.      rtx insn;
  3303.      int src_regno, dest_regno;
  3304.      rtx mult_val, add_val;
  3305.      int benefit;
  3306.      struct induction *forces, *forces2;
  3307.      enum g_types type;
  3308.      int maybe_never;
  3309.      rtx *location;
  3310.      rtx loop_end;
  3311. {
  3312.   struct induction *b;
  3313.   struct iv_class *bl;
  3314.  
  3315.   v->insn = insn;
  3316.   v->src_regno = src_regno;
  3317.   v->giv_type = type;
  3318.   v->dest_regno = dest_regno;
  3319.   v->mult_val = mult_val;
  3320.   v->add_val = add_val;
  3321.   v->benefit = benefit;
  3322.   v->location = location;
  3323.  
  3324.   if (type == DEST_ADDR)
  3325.     {
  3326.       v->mode = GET_MODE (*location);
  3327.       v->consec = 0;
  3328.       v->lifetime = 1;
  3329.       v->times_used = 1;
  3330.     }
  3331.   else /* type == DEST_REG */
  3332.     {
  3333.       v->mode = GET_MODE (SET_DEST (PATTERN (insn)));
  3334.       v->consec = n_times_set[dest_regno] - 1;
  3335.       v->lifetime = (uid_luid[regno_last_uid[dest_regno]]
  3336.              - uid_luid[regno_first_uid[dest_regno]]);
  3337.       v->times_used = n_times_used[dest_regno];
  3338.     }
  3339.  
  3340.   v->same = 0;
  3341.   v->forces = 0;
  3342.   v->forces2 = 0;
  3343.   v->ignore = 0;
  3344.   v->new_reg = 0;
  3345.  
  3346.   /* Mark giv as forced if it is only used to compute another giv.  */
  3347.  
  3348.   /* This check is not sufficient as INSN may have been moved giving
  3349.      it a new uid, so make another check by calculating lifetimes.
  3350.      This is overconservative but seems to be correct.  */
  3351.  
  3352.   if (forces)
  3353.     {
  3354.       v->benefit += forces->benefit;
  3355.       if ((regno_last_uid[forces->dest_regno] == INSN_UID (insn)
  3356.        ||
  3357.        ((uid_luid[regno_last_uid[forces->dest_regno]]
  3358.          - uid_luid[regno_first_uid[forces->dest_regno]])
  3359.         == (INSN_LUID (insn) - INSN_LUID (forces->insn))))
  3360.       && !reg_used_between_p (SET_DEST (PATTERN (forces->insn)),
  3361.                   forces->insn, insn))
  3362.      {
  3363.       v->forces = forces;
  3364.       forces->ignore = 1;
  3365.      }
  3366.     }
  3367.  
  3368.   if (forces2)
  3369.     {
  3370.       v->benefit += forces2->benefit;
  3371.       if ((regno_last_uid[forces2->dest_regno] == INSN_UID (insn)
  3372.        ||
  3373.        ((uid_luid[regno_last_uid[forces2->dest_regno]]
  3374.          - uid_luid[regno_first_uid[forces2->dest_regno]])
  3375.         == (INSN_LUID (insn) - INSN_LUID (forces2->insn))))
  3376.       && !reg_used_between_p (SET_DEST (PATTERN (forces2->insn)),
  3377.                   forces2->insn, insn))
  3378.      {
  3379.       if (v->forces)
  3380.         v->forces2 = forces2;
  3381.       else
  3382.         v->forces = forces2;
  3383.       forces2->ignore = 1;
  3384.     }
  3385.     }
  3386.  
  3387.   if (type == DEST_REG)
  3388.     {
  3389.       induct_var[dest_regno] = GENERAL_INDUCT;
  3390.       induct_struct[dest_regno] = v;
  3391.     }
  3392.  
  3393.   /* Add the giv to the class of givs computed from one biv.  */
  3394.  
  3395.   bl = class_struct[src_regno];
  3396.   if (bl)
  3397.     {
  3398.       v->family = bl->giv;
  3399.       bl->giv = v;
  3400.       /* Don't count DEST_ADDR.  This is supposed to count the number of
  3401.      insns that calculate givs.  */
  3402.       if (type == DEST_REG)
  3403.     bl->giv_count++;
  3404.       bl->total_benefit += benefit;
  3405.     }
  3406.   else
  3407.     /* Fatal error, biv missing for this giv?  */
  3408.     abort ();
  3409.  
  3410.   if (type == DEST_ADDR)
  3411.     v->replaceable = 1;
  3412.   else
  3413.     {
  3414.       /* The giv can be replaced outright by the reduced register if
  3415.       - the insn that sets the giv is always executed on any iteration
  3416.        on which the giv is used at all
  3417.        (there are two ways to deduce this:
  3418.         either the insn is executed on every iteration,
  3419.         or all uses follow that insn in the same basic block),
  3420.       - the giv is not used before the insn that sets it,
  3421.          i.e. no definition outside loop reaches into loop
  3422.      - no assignments to the biv occur during the giv's lifetime.  */
  3423.  
  3424.       /* Is this right?  Don't we need to make sure the giv is not used
  3425.      outside the loop.  Someday we will know where all the loop exits
  3426.      are so we can do better, but until then....
  3427.      March 18, 1989 -- self@bayes.arc.nasa.gov */
  3428.  
  3429.       if (regno_first_uid[dest_regno] == INSN_UID (insn)
  3430.       /* Previous line always fails if INSN was moved by loop opt.  */
  3431.       && uid_luid[regno_last_uid[dest_regno]] < INSN_LUID (loop_end)
  3432.       && (!maybe_never || last_use_this_basic_block (dest_regno, insn)))
  3433.      {
  3434.       v->replaceable = 1;
  3435.       for (b = bl->biv; b; b = b->family)
  3436.         {
  3437.           if ((uid_luid[INSN_UID (b->insn)] >= uid_luid[regno_first_uid[dest_regno]])
  3438.           &&
  3439.           (uid_luid[INSN_UID (b->insn)]
  3440.            <= uid_luid[regno_last_uid[dest_regno]]))
  3441.         {
  3442.           v->replaceable = 0;
  3443.           break;
  3444.          }
  3445.         }
  3446.     }
  3447.       else
  3448.      v->replaceable = 0;
  3449.     }
  3450.  
  3451.   if (loop_dump_stream)
  3452.     {
  3453.       if (type == DEST_REG)
  3454.      fprintf (loop_dump_stream, "Insn %d: giv reg %d",
  3455.          INSN_UID (insn), dest_regno);
  3456.       else
  3457.      fprintf (loop_dump_stream, "Insn %d: dest address",
  3458.           INSN_UID (insn));
  3459.  
  3460.       fprintf (loop_dump_stream, " src reg %d benefit %d",
  3461.            src_regno, v->benefit);
  3462.       fprintf (loop_dump_stream, " used %d lifetime %d",
  3463.            v->times_used, v->lifetime);
  3464.  
  3465.       if (v->replaceable)
  3466.      fprintf (loop_dump_stream, " replaceable");
  3467.  
  3468.       if (GET_CODE (mult_val) == CONST_INT)
  3469.     fprintf (loop_dump_stream, " mult %d",
  3470.           INTVAL (mult_val));
  3471.       else
  3472.     {
  3473.       fprintf (loop_dump_stream, " mult ");
  3474.       print_rtl (loop_dump_stream, mult_val);
  3475.     }
  3476.  
  3477.       if (GET_CODE (add_val) == CONST_INT)
  3478.     fprintf (loop_dump_stream, " add %d",
  3479.          INTVAL (add_val));
  3480.       else
  3481.     {
  3482.       fprintf (loop_dump_stream, " add ");
  3483.       print_rtl (loop_dump_stream, add_val);
  3484.     }
  3485.     }
  3486.  
  3487.   if (loop_dump_stream && v->forces)
  3488.     fprintf (loop_dump_stream, " forces insn %d", INSN_UID (v->forces->insn));
  3489.   if (loop_dump_stream && v->forces2)
  3490.     fprintf (loop_dump_stream, " forces insn %d", INSN_UID (v->forces2->insn));
  3491.   if (loop_dump_stream && v->consec)
  3492.     fprintf (loop_dump_stream, " consec %d", v->consec);
  3493.   if (loop_dump_stream)
  3494.     fprintf (loop_dump_stream, "\n");
  3495. }
  3496.  
  3497. /* Delete the insns forced by the insn described by V.
  3498.    If THIS_TOO is nonzero, delete that insn itself as well.  */
  3499.  
  3500. static void
  3501. delete_insn_forces (v, this_too)
  3502.      struct induction *v;
  3503.      int this_too;
  3504. {
  3505.   rtx x, p;
  3506.   int count;
  3507.   rtx insn;
  3508.  
  3509.   if (this_too)
  3510.     {
  3511.       insn = v->insn;
  3512.       for (count = v->consec; count >= 0; count--)
  3513.     {
  3514.       /* If first insn of libcall sequence, skip to end.  */
  3515.       /* Do this at start of loop, since p is guaranteed to
  3516.          be an insn here.  */
  3517.       if (x = find_reg_note (insn, REG_LIBCALL, 0))
  3518.         insn = XEXP (x, 0);
  3519.  
  3520.       if (x = find_reg_note (insn, REG_RETVAL, 0))
  3521.         {
  3522.           /* This is a library call; delete all insns backward until get to
  3523.          first insn in this group.  */
  3524.           rtx first = XEXP (x, 0);
  3525.           for (p = insn; p != first; p = PREV_INSN (p))
  3526.         delete_insn (p);
  3527.           /* Delete first insn also.  */
  3528.           delete_insn (p);
  3529.         }
  3530.       else
  3531.         delete_insn (insn);
  3532.  
  3533.       do insn = NEXT_INSN (insn);
  3534.       while (GET_CODE (insn) == NOTE);
  3535.     }
  3536.     }
  3537.  
  3538.   if (v->forces)
  3539.     delete_insn_forces (v->forces, 1);
  3540.   if (v->forces2)
  3541.     delete_insn_forces (v->forces2, 1);
  3542. }
  3543.  
  3544. /* Check whether an insn is an increment legitimate for a basic induction var.
  3545.    X is the source of the insn.
  3546.    DEST_REG is the putative biv, also the destination of the insn.
  3547.    We accept patterns of these forms:
  3548.      REG = REG + INVARIANT
  3549.      REG = INVARIANT + REG
  3550.      REG = REG - CONSTANT
  3551.  
  3552.    If X is suitable, we return 1,
  3553.    and store the factor multiplying REF in X into *MULT_VAL
  3554.    and the additive term into *INC_VAL.
  3555.    Otherwise we return 0.  */
  3556.  
  3557. static int
  3558. basic_induction_var (x, dest_regno, inc_val, mult_val)
  3559.      register rtx x;
  3560.      int dest_regno;
  3561.      rtx *inc_val;
  3562.      rtx *mult_val;
  3563. {
  3564.   register enum rtx_code code;
  3565.   rtx arg;
  3566.  
  3567.   if (x == 0)
  3568.     return 0;
  3569.   code = GET_CODE (x);
  3570.   switch (code)
  3571.     {
  3572.     case PLUS:
  3573.       if (GET_CODE (XEXP (x, 0)) == REG
  3574.       && REGNO (XEXP (x, 0)) == dest_regno)
  3575.      arg = XEXP (x, 1);
  3576.       else if (GET_CODE (XEXP (x, 1)) == REG
  3577.            && REGNO (XEXP (x, 1)) == dest_regno)
  3578.     arg = XEXP (x, 0);
  3579.       else
  3580.      return 0;
  3581.  
  3582.       if (invariant_p (arg) == 1)
  3583.     *inc_val = arg;
  3584.       else
  3585.     return 0;
  3586.  
  3587.       *mult_val = const1_rtx;
  3588.       return 1;
  3589.  
  3590.     case MINUS:
  3591.       if (GET_CODE (XEXP (x, 0)) == REG
  3592.        && REGNO (XEXP (x, 0)) == dest_regno
  3593.        && GET_CODE (XEXP (x, 1)) == CONST_INT)
  3594.      *inc_val = gen_rtx (CONST_INT, VOIDmode,
  3595.                 - INTVAL (XEXP (x, 1)));
  3596.       else
  3597.      return 0;
  3598.       *mult_val = const1_rtx;
  3599.       return 1;
  3600.  
  3601.       /* Can accept constant setting of biv only when inside inner most loop.
  3602.        Otherwise, a biv of an inner loop may be incorrectly recognized
  3603.      as a biv of the outer loop,
  3604.      causing code to be moved INTO the inner loop.  */
  3605.     case REG:
  3606.       if (!invariant_p (x))
  3607.     return 0;
  3608.     case CONST_INT:
  3609.     case SYMBOL_REF:
  3610.     case CONST:
  3611.       if (loops_enclosed == 1)
  3612.      {
  3613.       *inc_val = x;
  3614.        *mult_val = const0_rtx;
  3615.        return 1;
  3616.      }
  3617.       else
  3618.      return 0;
  3619.  
  3620.     default:
  3621.       return 0;
  3622.     }
  3623. }
  3624.  
  3625. /* A general induction variable (giv) is any quantity that is a linear function
  3626.    of a basic induction variable, i.e. giv = biv * mult_val + add_val.
  3627.    The coefficients can be any loop invariant quantity.
  3628.    A giv need not be computed directly from the biv;
  3629.    it can be computed by way of other givs.  */
  3630.  
  3631. /* Determine whether X computes a giv.
  3632.    If it does, return a nonzero value
  3633.      which is the benefit from eliminating the computation of X;
  3634.    set *SRC_REGNO to the register number of the biv that it is computed from;
  3635.    set *ADD_VAL and *MULT_VAL to the coefficients,
  3636.      such that the value of X is biv * mult + add;
  3637.    set forces (and forces2) to identify any other givs that are used
  3638.      solely to compute this one.  */
  3639.  
  3640. /* This routine recognizes four types of patterns that generate givs:
  3641.    - giv = biv op invariant             v = 0,    g = 0
  3642.    - giv1 = giv2 op invariant           v = 0,    g = giv2
  3643.        where giv1 and giv2 are functions of the same biv
  3644.    - giv1 = biv op giv2                 v = giv2, g = 0
  3645.        where giv2 is a function of biv
  3646.    - giv1 = giv2 op giv3                v = giv3, g = giv2
  3647.        where giv2 and giv3 are functions of the save biv  */
  3648.  
  3649. static int
  3650. general_induction_var (x, src_regno, add_val, mult_val, forces, forces2)
  3651.      rtx x;
  3652.      int *src_regno;
  3653.      rtx *add_val;
  3654.      rtx *mult_val;
  3655.      struct induction **forces;
  3656.      struct induction **forces2;
  3657. {
  3658.   register enum rtx_code code;
  3659.   rtx arg;
  3660.   struct induction *g = 0;
  3661.   struct induction *v = 0;
  3662.   int subexp = 0;
  3663.   int tem;
  3664.  
  3665.   if (x == 0)
  3666.     return 0;
  3667.  
  3668.   code = GET_CODE (x);
  3669.   switch (code)
  3670.     {
  3671.     case NEG:
  3672.       /* This can generate givs also, but it is not handled.  */
  3673.       return 0;
  3674.  
  3675.     case MULT:
  3676.     case UMULT:
  3677.       /* Reject widening multiply in version 1.
  3678.      That is safer than trying to handle it.  */
  3679.       {
  3680.     enum machine_mode m0 = GET_MODE (XEXP (x, 0));
  3681.     enum machine_mode m1 = GET_MODE (XEXP (x, 1));
  3682.     if (m0 != VOIDmode && m0 != GET_MODE (x))
  3683.       return 0;
  3684.     if (m1 != VOIDmode && m1 != GET_MODE (x))
  3685.       return 0;
  3686.       }
  3687.     case PLUS:
  3688.     case MINUS:
  3689.       /* Result is linear in both operands.  */
  3690.       /* Determine which operand is the biv, and put the other in ARG.  */
  3691.       if (GET_CODE (XEXP (x, 0)) == REG
  3692.       && induct_var[REGNO (XEXP (x, 0))] == BASIC_INDUCT)
  3693.     {
  3694.       *src_regno = REGNO (XEXP (x, 0));
  3695.       arg = XEXP (x, 1);
  3696.  
  3697.     }
  3698.       else if (GET_CODE (XEXP (x, 1)) == REG
  3699.            && induct_var[REGNO (XEXP (x, 1))] == BASIC_INDUCT)
  3700.     {
  3701.       *src_regno = REGNO (XEXP (x, 1));
  3702.       arg = XEXP (x, 0);
  3703.  
  3704.     }
  3705.       /* Check for an rtl subexpression that is a giv.  Memory address
  3706.      givs often look like (plus (reg) (mult (biv) (const))).  */
  3707.       /* Do this before checking for a giv operand, as this function will
  3708.      fail if this special operand is not recognized.  */
  3709. #ifndef DONT_REDUCE_ADDR
  3710.       else if (tem = general_induction_var (XEXP (x, 1), src_regno,
  3711.                         add_val, mult_val,
  3712.                         forces, forces2)
  3713.            && code != MINUS)
  3714.     {
  3715.       /* Set subexp true so that this can be handled a little
  3716.          differently from the normal case of g set.  */
  3717.       /* Note that SRC_REGNO is already set.  */
  3718.       subexp = TRUE;
  3719.       g = (struct induction *) alloca (sizeof (struct induction));
  3720.       g->mult_val = *mult_val;
  3721.       g->add_val = *add_val;
  3722.       /* Fake out the test below.  */
  3723.       g->replaceable = 1;
  3724.       /* Count this multiply as a shift, since that's what it
  3725.          really will do.  */
  3726.       if (tem == MULT_BENEFIT)
  3727.         g->benefit = SHIFT_BENEFIT;
  3728.       else
  3729.         g->benefit = tem;
  3730.       arg = XEXP (x, 0);
  3731.     }
  3732.       else if (tem = general_induction_var (XEXP (x, 0), src_regno,
  3733.                         add_val, mult_val,
  3734.                         forces, forces2))
  3735.     {
  3736.       /* Set subexp true so that this can be handled a little
  3737.          differently from the normal case of g set.  */
  3738.       /* Note that SRC_REGNO is already set.  */
  3739.       subexp = TRUE;
  3740.       g = (struct induction *) alloca (sizeof (struct induction));
  3741.       g->mult_val = *mult_val;
  3742.       g->add_val = *add_val;
  3743.       /* Fake out the test below.  */
  3744.       g->replaceable = 1;
  3745.       /* Count this multiply as a shift, since that's what it
  3746.          really will do.  */
  3747.       if (tem == MULT_BENEFIT)
  3748.         g->benefit = SHIFT_BENEFIT;
  3749.       else
  3750.         g->benefit = tem;
  3751.       arg = XEXP (x, 1);
  3752.     }
  3753. #endif
  3754.       /* Also allow general induction variables.
  3755.      Could have a mult followed by an add (i.e. an address calculation),
  3756.      thereby generating two related general induction variables
  3757.      of which only the second is actually used.  */
  3758.       /* Do this after checking both args for basic induction variables.  */
  3759.       else if (GET_CODE (XEXP (x, 0)) == REG
  3760.            && induct_var[REGNO (XEXP (x, 0))] == GENERAL_INDUCT)
  3761.     {
  3762.       g = induct_struct[REGNO (XEXP (x, 0))];
  3763.       *src_regno = g->src_regno;
  3764.       arg = XEXP (x, 1);
  3765.     }
  3766.       else if (GET_CODE (XEXP (x, 1)) == REG
  3767.            && induct_var[REGNO (XEXP (x, 1))] == GENERAL_INDUCT
  3768.            && code != MINUS)
  3769.     {
  3770.       g = induct_struct[REGNO (XEXP (x, 1))];
  3771.       *src_regno = g->src_regno;
  3772.       arg = XEXP (x, 0);
  3773.     }
  3774.       else
  3775.     return 0;
  3776.  
  3777.       /* Overall form of expression looks good.  */
  3778.       break;
  3779.  
  3780.       /* Could handle these also.  */
  3781.     case DIV:
  3782.     case UDIV:
  3783.       /* For a 68020 could handle these? */
  3784.     case LSHIFT:
  3785.     case ASHIFT:
  3786.     case ASHIFTRT:
  3787.     case LSHIFTRT:
  3788.       /* These operations are linear only in first operand.
  3789.      Check for a biv or giv there; if found, put other operand in ARG.  */
  3790.       if (GET_CODE (XEXP (x, 0)) == REG
  3791.       && induct_var[REGNO (XEXP (x, 0))] == BASIC_INDUCT)
  3792.     {
  3793.       *src_regno = REGNO (XEXP (x, 0));
  3794.       arg = XEXP (x, 1);
  3795.     }
  3796.       /* Also allow general induction variable.  */
  3797.       else if (GET_CODE (XEXP (x, 0)) == REG
  3798.            && induct_var[REGNO (XEXP (x, 0))] == GENERAL_INDUCT)
  3799.     {
  3800.       g = induct_struct[REGNO (XEXP (x, 0))];
  3801.       *src_regno = g->src_regno;
  3802.       arg = XEXP (x, 1);
  3803.     }
  3804.       else
  3805.     return 0;
  3806.  
  3807.       /* Overall form of expression looks good.  */
  3808.       break;
  3809.  
  3810.     default:
  3811.       return 0;
  3812.     }
  3813.  
  3814.   /* ARG is the operand that is NOT a biv or giv.
  3815.      Test it for superficial validity.  */
  3816.  
  3817.   /* This is just a special case of invariant values,
  3818.      it is not really needed, but it's a shortcut.  */
  3819.   if (GET_CODE (arg) == CONST_INT)
  3820.     ;
  3821.  
  3822.   /* Depends on previous general induction variable, which has
  3823.      the same basic induction variable */
  3824.   /* This code detects mults that have been generated as shift and add.  */
  3825.   else if (GET_CODE (arg) == REG
  3826.        && induct_var[REGNO (arg)] == GENERAL_INDUCT
  3827.        && induct_struct[REGNO (arg)]->src_regno == *src_regno)
  3828.     {
  3829.       v = induct_struct[REGNO (arg)];
  3830.       /* Dependence indicated by forces, sort of kludgey.  */
  3831.     }
  3832.  
  3833.   /* Invariant expression, could be a constant-valued register. */
  3834.   else if (invariant_p (arg) == 1)
  3835.     ;
  3836.  
  3837.   /* Failure */
  3838.   else
  3839.     return 0;
  3840.     
  3841.   /* Until we can do the correct thing, suppress use of nonreplaceable givs
  3842.      as sources for other givs.  */
  3843.   if ((g && ! g->replaceable)
  3844.       || (v && ! v->replaceable))
  3845.     return 0;
  3846.  
  3847.   /* Now we know looks like a giv; extract the coefficients.
  3848.      We can still fail if the coefficients are not what we can handle.  */
  3849.  
  3850.   /* Only succeed if result mult_val and add_val are only one level of rtl,
  3851.      for example, (NEG:SI (REG:SI 34)) is not accepted.
  3852.      This mainly causes problems with the MINUS code.  */
  3853.  
  3854.   switch (code)
  3855.     {
  3856.     case PLUS:
  3857.       if (v && g)
  3858.     {
  3859.       if (GET_CODE (g->mult_val) == CONST_INT)
  3860.         {
  3861.           if (g->mult_val == const0_rtx)
  3862.         *mult_val = v->mult_val;
  3863.           else if (GET_CODE (v->mult_val) == CONST_INT)
  3864.         *mult_val = gen_rtx (CONST_INT, VOIDmode,
  3865.                        INTVAL (g->mult_val)
  3866.                        + INTVAL (v->mult_val));
  3867.           else
  3868.         return 0;
  3869.         }
  3870.       else if (v->mult_val == const0_rtx)
  3871.         *mult_val = g->mult_val;
  3872.       else
  3873.         return 0;
  3874.  
  3875.       if (GET_CODE (g->add_val) == CONST_INT)
  3876.         {
  3877.           if (g->add_val == const0_rtx)
  3878.         *add_val = v->add_val;
  3879.           else if (GET_CODE (v->add_val) == CONST_INT)
  3880.         *add_val = gen_rtx (CONST_INT, VOIDmode,
  3881.                        INTVAL (g->add_val)
  3882.                        + INTVAL (v->add_val));
  3883.           else
  3884.         return 0;
  3885.         }
  3886.       else if (v->add_val == const0_rtx)
  3887.         *add_val = g->add_val;
  3888.       else
  3889.         return 0;
  3890.  
  3891.       if (subexp)
  3892.         {
  3893.           /* g deleted when return, can't return pointer to it */
  3894.           if (*forces2 == 0)
  3895.         *forces2 = v;
  3896.           return ADD_BENEFIT + g->benefit;
  3897.         }
  3898.       else
  3899.         {
  3900.           *forces = g;
  3901.           *forces2 = v;
  3902.           return ADD_BENEFIT;
  3903.         }
  3904.     }
  3905.       else if (v)
  3906.     {
  3907.       if (GET_CODE (v->mult_val) == CONST_INT)
  3908.         *mult_val = gen_rtx (CONST_INT, VOIDmode,
  3909.                    INTVAL (v->mult_val) + 1);
  3910.       else
  3911.         return 0;
  3912.       *add_val = v->add_val;
  3913.       *forces = v;
  3914.       return ADD_BENEFIT;
  3915.     }
  3916.       else if (g)
  3917.     {
  3918.       *mult_val = g->mult_val;
  3919.       if (GET_CODE (g->add_val) == CONST_INT)
  3920.         *add_val = plus_constant (arg, INTVAL (g->add_val));
  3921.       else if (GET_CODE (arg) == CONST_INT)
  3922.         *add_val = plus_constant (g->add_val, INTVAL (arg));
  3923.       else
  3924.         /* Could succeed if arg == 0, but that will never occur.  */
  3925.         return 0;
  3926.  
  3927.       if (subexp)
  3928.         /* g deleted when return, can't return pointer to it */
  3929.         return ADD_BENEFIT + g->benefit;
  3930.       else
  3931.         {
  3932.           *forces = g;
  3933.           return ADD_BENEFIT;
  3934.         }
  3935.     }
  3936.       else
  3937.     {
  3938.       *mult_val = const1_rtx;
  3939.       *add_val = arg;
  3940.       return ADD_BENEFIT;
  3941.     }
  3942.  
  3943.       /* Takes a lot of code and will rarely succeed.  */
  3944.     case MINUS:
  3945.       if (v && g)
  3946.     {
  3947.       /* G is the first argument of MINUS.  */
  3948.  
  3949.       if (GET_CODE (g->mult_val) == CONST_INT)
  3950.         {
  3951.           if (g->mult_val == const0_rtx)
  3952. #if 0 /* Should not have to fail here */
  3953.         *mult_val = gen_rtx (NEG, SImode, v->mult_val);
  3954. #endif
  3955.         return 0;
  3956.           else if (GET_CODE (v->mult_val) == CONST_INT)
  3957.         *mult_val = gen_rtx (CONST_INT, VOIDmode,
  3958.                        INTVAL (g->mult_val)
  3959.                        - INTVAL (v->mult_val));
  3960.           else
  3961.         return 0;
  3962.         }
  3963.       else if (v->mult_val == const0_rtx)
  3964.         *mult_val = g->mult_val;
  3965.       else
  3966.         return 0;
  3967.  
  3968.       if (GET_CODE (g->add_val) == CONST_INT)
  3969.         {
  3970.           if (g->add_val == const0_rtx)
  3971. #if 0 /* should not have to fail here */
  3972.         *add_val = v->add_val;
  3973. #endif
  3974.         return 0;
  3975.           else if (GET_CODE (v->add_val) == CONST_INT)
  3976.         *add_val = gen_rtx (CONST_INT, VOIDmode,
  3977.                        INTVAL (g->add_val)
  3978.                        - INTVAL (v->add_val));
  3979.           else
  3980.         return 0;
  3981.         }
  3982.       else if (v->add_val == const0_rtx)
  3983.         *add_val = g->add_val;
  3984.       else
  3985.         return 0;
  3986.  
  3987.       if (subexp)
  3988.         {
  3989.           /* G deleted when return, can't return pointer to it */
  3990.           if (*forces2 == 0)
  3991.         *forces2 = v;
  3992.           return ADD_BENEFIT + g->benefit;
  3993.         }
  3994.       else
  3995.         {
  3996.           *forces = g;
  3997.           *forces2 = v;
  3998.           return ADD_BENEFIT;
  3999.         }
  4000.     }
  4001.       else if (v)
  4002.     {
  4003.       if (GET_CODE (v->mult_val) != CONST_INT)
  4004.         return 0;
  4005.       if (arg == XEXP (x, 0))             /* giv1 = giv2 - biv */
  4006.         {
  4007.           *mult_val = gen_rtx (CONST_INT, VOIDmode,
  4008.                      INTVAL (v->mult_val) - 1);
  4009.           *add_val = v->add_val;
  4010.         }
  4011.       else                                /* giv1 = biv - giv2 */
  4012.         {
  4013.           *mult_val = gen_rtx (CONST_INT, VOIDmode,
  4014.                      1 - INTVAL (v->mult_val));
  4015.           if (GET_CODE (v->add_val) == CONST_INT)
  4016.         *add_val = gen_rtx (CONST_INT, VOIDmode,
  4017.                       - INTVAL (v->add_val));
  4018.           else
  4019.         return 0;
  4020.         }
  4021.       *forces = v;
  4022.       return ADD_BENEFIT;
  4023.     }
  4024.       else if (g)
  4025.     {
  4026.       if (arg == XEXP (x, 1))
  4027.         *mult_val = g->mult_val;
  4028.       else
  4029.         {
  4030.           if (GET_CODE (g->mult_val) == CONST_INT)
  4031.         *mult_val = gen_rtx (CONST_INT, VOIDmode,
  4032.                        - INTVAL (g->mult_val));
  4033.           else
  4034.         return 0;
  4035.         }
  4036.       if (GET_CODE (g->add_val) == CONST_INT)
  4037.         {
  4038.           if (g->add_val == const0_rtx)
  4039.         {
  4040.           if (arg == XEXP (x, 1))    /* giv1 = giv2 - arg */
  4041.             {
  4042.               /* Fail unless arg is a constant.  */
  4043.               if (GET_CODE (arg) == CONST_INT)
  4044.             *add_val = gen_rtx (CONST_INT, VOIDmode,
  4045.                           -INTVAL (arg));
  4046.               else
  4047.             return 0;
  4048.             }
  4049.           else                       /* giv1 = arg - giv2 */
  4050.             *add_val = arg;
  4051.         }
  4052.           else if (GET_CODE (arg) == CONST_INT)
  4053.         {
  4054.           if (arg == XEXP (x, 1))   /* giv1 = giv2 - arg */
  4055.             *add_val = gen_rtx (CONST_INT, VOIDmode,
  4056.                       INTVAL (g->add_val)
  4057.                       - INTVAL (arg));
  4058.           else                      /* giv1 = arg - giv2 */
  4059.             *add_val = gen_rtx (CONST_INT, VOIDmode,
  4060.                       INTVAL (arg),
  4061.                       - INTVAL (g->add_val));
  4062.         }
  4063.           else
  4064.         return 0;
  4065.         }
  4066.       else
  4067.         /* Could succeed if arg == 0, but that will never occur.  */
  4068.         return 0;
  4069.  
  4070.       if (subexp)
  4071.         /* G deleted when return, can't return pointer to it.  */
  4072.         return ADD_BENEFIT + g->benefit;
  4073.       else
  4074.         {
  4075.           *forces = g;
  4076.           return ADD_BENEFIT;
  4077.         }
  4078.     }
  4079.       else if (GET_CODE (arg) == CONST_INT)
  4080.     {
  4081.       if (arg == XEXP (x, 1))
  4082.         {
  4083.           *add_val = gen_rtx (CONST_INT, VOIDmode, - INTVAL (arg));
  4084.           *mult_val = const1_rtx;
  4085.         }
  4086.       else
  4087.         {
  4088.           *add_val = arg;
  4089.           *mult_val = gen_rtx (CONST_INT, VOIDmode, -1);
  4090.         }
  4091.       return ADD_BENEFIT;
  4092.     }
  4093.       else
  4094.       return 0;
  4095.  
  4096.       /* UMULT can be handled like MULT since C ignores overflows.  */
  4097.     case MULT:
  4098.     case UMULT:
  4099.       if (v && g)
  4100.     {
  4101.       /* Quadratic term, just fail.  */
  4102.       return 0;
  4103.     }
  4104.       else if (v)
  4105.     {
  4106.       /* Quadratic term, just fail.  */
  4107.       return 0;
  4108.     }
  4109.       else if (g)
  4110.     {
  4111.       /* Takes a lot of code and will rarely succeed.  */
  4112.       /* dest = m * arg * b + a * arg */
  4113.       if (GET_CODE (g->mult_val) == CONST_INT)
  4114.         {
  4115.           if (g->mult_val == const0_rtx)
  4116.         *mult_val = const0_rtx;
  4117.           else if (g->mult_val == const1_rtx)
  4118.         *mult_val = arg;
  4119.           else if (GET_CODE (arg) == CONST_INT)
  4120.         *mult_val = gen_rtx (CONST_INT, VOIDmode,
  4121.                        INTVAL (g->mult_val) * INTVAL (arg));
  4122.           else
  4123.         return 0;
  4124.         }
  4125.       else
  4126.         /* Could suceed if arg == 1 or 0, but this will never occur.  */
  4127.         return 0;
  4128.  
  4129.       if (GET_CODE (g->add_val) == CONST_INT)
  4130.         {
  4131.           if (g->add_val == const0_rtx)
  4132.         *add_val = const0_rtx;
  4133.           else if (g->add_val == const1_rtx)
  4134.         *add_val = arg;
  4135.           else if (GET_CODE (arg) == CONST_INT)
  4136.         *add_val = gen_rtx (CONST_INT, VOIDmode,
  4137.                        INTVAL (g->add_val) * INTVAL (arg));
  4138.           else
  4139.         return 0;
  4140.         }
  4141.       else
  4142.         /* Could suceed if arg == 1 or 0, but this will never occur.  */
  4143.         return 0;
  4144.  
  4145.       if (subexp)
  4146.         /* G deleted when return, can't return pointer to it.  */
  4147.         return MULT_BENEFIT + g->benefit;
  4148.       else
  4149.         {
  4150.           *forces = g;
  4151.           return MULT_BENEFIT;
  4152.         }
  4153.     }
  4154.       else
  4155.     {
  4156.       *mult_val = arg;
  4157.       *add_val = const0_rtx;
  4158.       return MULT_BENEFIT;
  4159.     }
  4160.  
  4161.       /* These are not worth the trouble.  */
  4162.     case DIV:
  4163.     case UDIV:
  4164.       return 0;
  4165.  
  4166.       /* Handle these, but only for left shift.  */
  4167.     case LSHIFT:
  4168.     case ASHIFT:
  4169.       if (v && g)
  4170.     {
  4171.       /* Quadratic term, just fail.  */
  4172.       return 0;
  4173.     }
  4174.       else if (v)
  4175.     {
  4176.       /* Quadratic term, just fail.  */
  4177.       return 0;
  4178.     }
  4179.       else if (g)
  4180.     {
  4181.       /* Takes a lot of code and will rarely succeed.  */
  4182.       /* dest = ((m * b) << arg) + (a << arg) */
  4183.       if (GET_CODE (g->mult_val) == CONST_INT)
  4184.         {
  4185.           if (g->mult_val == const0_rtx)
  4186.         *mult_val = const0_rtx;
  4187.           else if (GET_CODE (arg) == CONST_INT && INTVAL (arg) >= 0)
  4188.         *mult_val = gen_rtx (CONST_INT, VOIDmode,
  4189.                        INTVAL (g->mult_val)
  4190.                        * (1 << INTVAL (arg)));
  4191.           else
  4192.         return 0;
  4193.         }
  4194.       else
  4195.         /* Could suceed if arg == 0, but this will never occur.  */
  4196.         return 0;
  4197.  
  4198.       if (GET_CODE (g->add_val) == CONST_INT)
  4199.         {
  4200.           if (g->add_val == const0_rtx)
  4201.         *add_val = const0_rtx;
  4202.           else if (GET_CODE (arg) == CONST_INT)
  4203.         *add_val = gen_rtx (CONST_INT, VOIDmode,
  4204.                        INTVAL (g->add_val)
  4205.                        * (1 << INTVAL (arg)));
  4206.           else
  4207.         return 0;
  4208.         }
  4209.       else
  4210.         /* Could suceed if arg == 0, but this will never occur.  */
  4211.         return 0;
  4212.  
  4213.       if (subexp)
  4214.         /* G deleted when return, can't return pointer to it.  */
  4215.         return SHIFT_BENEFIT + g->benefit;
  4216.       else
  4217.         {
  4218.           *forces = g;
  4219.           return SHIFT_BENEFIT;
  4220.         }
  4221.     }
  4222.  
  4223.       if (GET_CODE (arg) == CONST_INT && INTVAL (arg) >= 0)
  4224.     *mult_val = gen_rtx (CONST_INT, VOIDmode, 1 << INTVAL (arg));
  4225.       else
  4226.     return 0;
  4227.       *add_val = const0_rtx;
  4228.       return SHIFT_BENEFIT;
  4229.  
  4230.       /* These are not worth the trouble.  */
  4231.     case ASHIFTRT:
  4232.     case LSHIFTRT:
  4233.       return 0;
  4234.  
  4235.       /* should never reach here */
  4236.     default:
  4237.       abort ();
  4238.       return 0;
  4239.     }
  4240. }
  4241.  
  4242. /* Help detect a giv that is calculated by several consecutive insns;
  4243.    for example,
  4244.       giv = biv * M
  4245.       giv = giv + A
  4246.    The caller has already identified the first insn P as having a giv as dest;
  4247.    we check that all other insns that set the same register follow
  4248.    immediately after P, that they alter nothing else,
  4249.    and that the result of the last is still a giv.
  4250.  
  4251.    The value is 0 if the reg set in P is not really a giv.
  4252.    Otherwise, the value is the amount gained by eliminating
  4253.    all the consecutive insns that compute the value.
  4254.  
  4255.    FIRST_BENEFIT is the amount gained by eliminating the first insn, P.
  4256.    SRC_REGNO is the regno of the biv; DEST_REGNO is that of the giv.
  4257.  
  4258.    The coefficients of the ultimate giv value are stored in
  4259.    *MULT_VAL and *ADD_VAL.  */
  4260.  
  4261. static int
  4262. consec_sets_giv (first_benefit, p, src_regno, dest_regno,
  4263.          add_val, mult_val)
  4264.      int first_benefit;
  4265.      rtx p;
  4266.      int src_regno;
  4267.      int dest_regno;
  4268.      rtx *add_val;
  4269.      rtx *mult_val;
  4270. {
  4271.   int count;
  4272.   int benefit = first_benefit;
  4273.   enum rtx_code code;
  4274.   struct induction forces, forces2;
  4275.   rtx temp;
  4276.   int tem;
  4277.  
  4278.   /* Initialize info used by general_induction_var.  */
  4279.   struct induction *v =
  4280.     (struct induction *) oballoc (sizeof (struct induction));
  4281.   v->src_regno = src_regno;
  4282.   v->mult_val = *mult_val;
  4283.   v->add_val = *add_val;
  4284.  
  4285.   induct_var[dest_regno] = GENERAL_INDUCT;
  4286.   induct_struct[dest_regno] = v;
  4287.  
  4288.   count = n_times_set[dest_regno] - 1;
  4289.  
  4290.   while (count > 0)
  4291.     {
  4292.       p = NEXT_INSN (p);
  4293.       code = GET_CODE (p);
  4294.  
  4295.       /* If libcall, skip to end of call sequence.  */
  4296.       if (code == INSN && (temp = find_reg_note (p, REG_LIBCALL, 0)))
  4297.     p = XEXP (temp, 0);
  4298.  
  4299.       if (code == INSN && GET_CODE (PATTERN (p)) == SET
  4300.       && GET_CODE (SET_DEST (PATTERN (p))) == REG
  4301.       && REGNO (SET_DEST (PATTERN (p))) == dest_regno
  4302.       && ((tem = general_induction_var (SET_SRC (PATTERN (p)), &src_regno,
  4303.                         add_val, mult_val,
  4304.                         &forces, &forces2))
  4305.           /* Giv created by call to library routine.  */
  4306.           || ((temp = find_reg_note (p, REG_EQUAL, 0)) &&
  4307.           (tem = general_induction_var (XEXP (temp, 0), &src_regno,
  4308.                         add_val, mult_val,
  4309.                         &forces, &forces2))))
  4310.       && src_regno == v->src_regno)
  4311.     {
  4312.       count--;
  4313.       benefit += tem;
  4314.       v->mult_val = *mult_val;
  4315.       v->add_val = *add_val;
  4316.     }
  4317.       else if (code != NOTE)
  4318.     {
  4319.       induct_var[dest_regno] = UNKNOWN_INDUCT;
  4320.       return 0;
  4321.     }
  4322.     }
  4323.  
  4324.   return benefit;
  4325. }
  4326.  
  4327. /* Generate a SEQUENCE to multiply OP0 and OP1 with result in TARGET.
  4328.    Use expand_mult to "optimally" do the multiply.
  4329.    This also works for machines that do not have multiply insns.
  4330.    If one of the operands is a constant, it must be the second.  */
  4331.  
  4332. static rtx
  4333. gen_iv_mult (mode, op0, op1, target)
  4334.      enum machine_mode mode;
  4335.      register rtx op0, op1, target;
  4336. {
  4337.   extern rtx gen_sequence ();
  4338.   extern rtx start_sequence ();
  4339.   rtx saved, result, temp;
  4340.  
  4341.   saved = start_sequence ();
  4342.  
  4343.   /* ??? It is very unmodular to use expand_mult here!
  4344.      This should be redesigned.  */
  4345.  
  4346.   /* UNSIGNEDP arg can be zero since operands/target always same width.  */
  4347.   temp = expand_mult (mode, op0, op1, target, 0);
  4348.  
  4349.   /* Move to target register, if expand_mult did not put it there.  */
  4350.   if (target != 0 && temp != target)
  4351.     emit_move_insn (target, temp);
  4352.  
  4353.   result = gen_sequence ();
  4354.   end_sequence (saved);
  4355.  
  4356.   return result;
  4357. }
  4358.  
  4359. /* Emit code to initialize an induction variable created by strength
  4360.    reduction.
  4361.    More precisely, emit code before INSERT_BEFORE
  4362.    to set REG = B * M + A.  */
  4363.  
  4364. static void
  4365. emit_iv_init_code (b, m, a, reg, insert_before)
  4366.      rtx b;          /* initial value of basic induction variable */
  4367.      rtx m;          /* multiplicative constant */
  4368.      rtx a;          /* additive constant */
  4369.      rtx reg;        /* destination register */
  4370.      rtx insert_before;
  4371. {
  4372.   rtx seq;
  4373.   rtx result;
  4374.  
  4375.   /* Prevent unexpected sharing of these rtx.  */
  4376.   a = copy_rtx (a);
  4377.   b = copy_rtx (b);
  4378.  
  4379.   start_sequence ();
  4380.   result = expand_mult_add (b, m, a, GET_MODE (reg), 0);
  4381.   if (reg != result)
  4382.     emit_move_insn (reg, result);
  4383.   seq = gen_sequence ();
  4384.   end_sequence ();
  4385.  
  4386.   emit_insn_before (seq, insert_before);
  4387. }
  4388.  
  4389. /* Emit code to increment the induction variable inside the loop.
  4390.    Try to emit optimal code for the expression
  4391.    REG = REG + BIV_ADD * GIV_MULT.  */
  4392.  
  4393. static void
  4394. emit_iv_inc (biv_add, giv_mult, reg, insn)
  4395.      rtx biv_add;                   /* increment value for biv */
  4396.      rtx giv_mult;                  /* multiply value of giv */
  4397.      rtx reg;                       /* create insn to set this reg */
  4398.      rtx insn;                      /* where to insert the new insn */
  4399. {
  4400.   emit_iv_init_code (biv_add, giv_mult, reg, reg, insn);
  4401. }
  4402.  
  4403. /* Test whethen BIV_ADD * GIV_MULT can be computed without
  4404.    an actual multiply insn.  Value is 1 if so.  */
  4405.  
  4406. static int
  4407. product_cheap_p (biv_add, giv_mult)
  4408.      rtx biv_add;
  4409.      rtx giv_mult;
  4410. {
  4411.   /* Indicates which of MULT/ADD are constants.  */
  4412.   int status = 0;
  4413.   int const_val;
  4414.   rtx tmp;
  4415.  
  4416.   if (GET_CODE (biv_add) == CONST_INT)
  4417.     status |= 0x1;
  4418.   if (GET_CODE (giv_mult) == CONST_INT)
  4419.     status |= 0x2;
  4420.  
  4421.   switch (status)
  4422.     {
  4423.     case 0:
  4424.       /* Neither is constant: would need a multiply insn, so fail.  */
  4425.       return 0;
  4426.  
  4427.     case 1:
  4428.       /* BIV_ADD value is constant */
  4429.       /* Equivalent to state 2, just switch values of BIV_ADD and GIV_MULT
  4430.      and fall through.  */
  4431.       tmp = biv_add;
  4432.       biv_add = giv_mult;
  4433.       giv_mult = tmp;
  4434.  
  4435.     case 2:
  4436.       /* GIV_MULT value is constant.
  4437.      If it is 1, 0 or -1 then we win.  */
  4438.       const_val = INTVAL (giv_mult);
  4439.       if (const_val < -1 || const_val > 1)
  4440.     {
  4441.       tmp = gen_iv_mult (GET_MODE (biv_add), biv_add, giv_mult, 0);
  4442.       /* Don't emit a multiply insn, just fail instead.  */
  4443.       if ((GET_CODE (tmp) == SET && GET_CODE (SET_SRC (tmp)) == MULT)
  4444.              /* Also fail if library call (which generates more
  4445.             then two insn) is needed.  */
  4446.           || (GET_CODE (tmp) == SEQUENCE && XVECLEN (tmp, 0) > 2))
  4447.         return 0;
  4448.     }
  4449.       return 1;
  4450.  
  4451.     case 3:
  4452.       /* Both BIV_ADD and GIV_MULT are constant;
  4453.      can compute the product at compile time.  */
  4454.       return 1;
  4455.  
  4456.     default:
  4457.       abort ();
  4458.     }
  4459. }
  4460.  
  4461.  
  4462. /* Check to see if loop can be terminated by a "decrement and branch until
  4463.    zero" instruction.  If so, add a REG_NONNEG note to the branch insn if so.
  4464.    Also try reversing an increment loop to a decrement loop
  4465.    to see if the optimization can be performed.
  4466.    Value is nonzero if optimization was performed.  */
  4467.  
  4468. static int
  4469. check_dbra_loop (loop_end, iv_list, insn_count, loop_start)
  4470.      rtx loop_end;
  4471.      struct iv_class *iv_list;
  4472.      int insn_count;
  4473.      rtx loop_start;
  4474. {
  4475.   struct iv_class *bl;
  4476.   rtx reg;
  4477.   rtx jump_label;
  4478.   rtx final_value;
  4479.   rtx start_value;
  4480.   enum rtx_code branch_code;
  4481.   rtx new_add_val;
  4482.   rtx tested_before_loop = 0;
  4483.   rtx p;
  4484.  
  4485.   /* See if the loop is contained in  `if (X >= 0)' for some reg X.
  4486.      If so, then we know X is initially nonnegative even though
  4487.      we don't know its initial value.
  4488.      Record X in TESTED_BEFORE_LOOP.  */
  4489.  
  4490.   for (p = loop_start; p != 0; p = PREV_INSN (p))
  4491.     if (GET_CODE (p) != NOTE)
  4492.       break;
  4493.  
  4494.   /* See if a conditional branch preceeds the loop.
  4495.      There may be no other insns or labels between it and
  4496.      the beginning of the loop.  */
  4497.   if (p != 0 && GET_CODE (p) == JUMP_INSN && condjump_p (p)
  4498.       && SET_SRC (PATTERN (p)) != pc_rtx
  4499.       && ((GET_CODE (XEXP (SET_SRC (PATTERN (p)), 0)) == LT
  4500.        && XEXP (SET_SRC (PATTERN (p)), 2) == pc_rtx)
  4501.       ||
  4502.       (GET_CODE (XEXP (SET_SRC (PATTERN (p)), 0)) == GE
  4503.        && XEXP (SET_SRC (PATTERN (p)), 1) == pc_rtx))
  4504.       && next_real_insn (JUMP_LABEL (p)) == next_real_insn (loop_end))
  4505.     {
  4506.       /* Before the branch should be a test or compare.
  4507.      See if we are comparing something against zero.  */
  4508.       p = PREV_INSN (p);
  4509.       if (GET_CODE (p) == INSN && GET_CODE (PATTERN (p)) == SET
  4510.       && SET_DEST (PATTERN (p)) == cc0_rtx)
  4511.     {
  4512.       if (GET_CODE (SET_SRC (PATTERN (p))) == REG)
  4513.         tested_before_loop = SET_SRC (PATTERN (p));
  4514.       else if (GET_CODE (SET_SRC (PATTERN (p))) == COMPARE
  4515.            && GET_CODE (XEXP (SET_SRC (PATTERN (p)), 0)) == REG
  4516.            && XEXP (SET_SRC (PATTERN (p)), 1) == const0_rtx)
  4517.         tested_before_loop = XEXP (SET_SRC (PATTERN (p)), 0);
  4518.       else if (GET_CODE (SET_SRC (PATTERN (p))) == COMPARE
  4519.            && GET_CODE (XEXP (SET_SRC (PATTERN (p)), 1)) == REG
  4520.            && XEXP (SET_SRC (PATTERN (p)), 0) == const0_rtx)
  4521.         tested_before_loop = XEXP (SET_SRC (PATTERN (p)), 1);
  4522.     }
  4523.     }
  4524.  
  4525.   /* If last insn is a conditional branch, and the insn before tests a register
  4526.      value, then try to optimize it.  */
  4527.  
  4528.   if (GET_CODE (PREV_INSN (loop_end)) == JUMP_INSN
  4529.       && GET_CODE (PATTERN (PREV_INSN (loop_end))) == SET
  4530.       && GET_CODE (SET_SRC (PATTERN (PREV_INSN (loop_end)))) == IF_THEN_ELSE
  4531.       && GET_CODE (PREV_INSN (PREV_INSN (loop_end))) == INSN
  4532.       && GET_CODE (PATTERN (PREV_INSN (PREV_INSN (loop_end)))) == SET
  4533.       && (GET_CODE (SET_DEST (PATTERN (PREV_INSN (PREV_INSN (loop_end))))) ==
  4534.       CC0))
  4535.     {
  4536.       /* Check all of the bivs to see if the compare uses one of them.  */
  4537.  
  4538.       for (bl = iv_list; bl; bl = bl->next)
  4539.     {
  4540.       if (reg_mentioned_p (SET_DEST (PATTERN (bl->biv->insn)),
  4541.                    PREV_INSN (PREV_INSN (loop_end))))
  4542.         break;
  4543.     }
  4544.  
  4545.       /* If biv set more than once, then give up.
  4546.      We can't guarantee that it will be zero on the last iteration.
  4547.      Also give up if the biv is used between its update and the test
  4548.      insn.  */
  4549.       if (bl && bl->biv_count == 1
  4550.       && ! reg_used_between_p (regno_reg_rtx[bl->regno], bl->biv->insn,
  4551.                    PREV_INSN (PREV_INSN (loop_end))))
  4552.     {
  4553.       /* Look for the case where the basic induction variable is always
  4554.          nonnegative, and equals zero on the last iteration.
  4555.          In this case, add a reg_note REG_NONNEG, which allows the
  4556.          m68k DBRA instruction to be used.  */
  4557.  
  4558.       /* the decrement case */
  4559.  
  4560.       if (GET_CODE (bl->biv->add_val) == CONST_INT
  4561.           && INTVAL (bl->biv->add_val) < 0)
  4562.         {
  4563.           /* Initial value must be greater than 0,
  4564.          init_val % -dec_value == 0 to ensure that it equals zero on
  4565.             the last iteration */
  4566.  
  4567.           if (GET_CODE (bl->initial_value) == CONST_INT
  4568.           && INTVAL (bl->initial_value) > 0
  4569.           && (INTVAL (bl->initial_value) %
  4570.               (-INTVAL (bl->biv->add_val))) == 0)
  4571.         {
  4572.           /* register always nonnegative, add REG_NOTE to branch */
  4573.           REG_NOTES (PREV_INSN (loop_end))
  4574.             = gen_rtx (EXPR_LIST, REG_NONNEG, 0,
  4575.                    REG_NOTES (PREV_INSN (loop_end)));
  4576.           bl->nonneg = 1;
  4577.  
  4578.           return 1;
  4579.         }
  4580.  
  4581.           /* If the decrement is 1 and the value was tested as >= 0 before
  4582.          the loop, then we can safely optimize.  */
  4583.           if (SET_DEST (PATTERN (bl->biv->insn)) == tested_before_loop
  4584.           && INTVAL (bl->biv->add_val) == -1)
  4585.         {
  4586.           REG_NOTES (PREV_INSN (loop_end))
  4587.             = gen_rtx (EXPR_LIST, REG_NONNEG, 0,
  4588.                    REG_NOTES (PREV_INSN (loop_end)));
  4589.           bl->nonneg = 1;
  4590.  
  4591.           return 1;
  4592.         }
  4593.         }
  4594.       else if (num_mem_sets <= 1)
  4595.         {
  4596.           /* Try to change inc to dec, so can apply above optimization.  */
  4597.           /* Can do this if:
  4598.          all registers modified are induction variables or invariant,
  4599.          all memory references have non-overlapping addresses
  4600.                        (obviously true if only one write)
  4601.              allow 2 insns for the compare/jump at the end of the loop.  */
  4602.           int num_nonfixed_reads = 0;
  4603.           rtx p;
  4604.  
  4605.           for (p = loop_start; p != loop_end; p = NEXT_INSN (p))
  4606.         if (GET_CODE (p) == INSN || GET_CODE (p) == CALL_INSN
  4607.             || GET_CODE (p) == JUMP_INSN)
  4608.           num_nonfixed_reads += count_nonfixed_reads (PATTERN (p));
  4609.  
  4610.           /* This code only acts for innermost loops.  Also it simplifies
  4611.          the memory address check by only reversing loops with
  4612.          zero or one memory access.
  4613.          Two memory accesses could involve parts of the same array,
  4614.          and that can't be reversed.  */
  4615.  
  4616.           if (num_nonfixed_reads <= 1
  4617.           && !loop_has_call
  4618.           && (bl->giv_count + bl->biv_count + num_mem_sets
  4619.               + num_movables + 2 == insn_count))
  4620.         {
  4621.           rtx src_two_before_end;
  4622.           int constant;
  4623.           int win;
  4624.  
  4625.           /* Loop can be reversed.  */
  4626.           if (loop_dump_stream)
  4627.             fprintf (loop_dump_stream, "Can reverse loop\n");
  4628.  
  4629.           /* Now check other conditions:
  4630.              initial_value must be zero,
  4631.              final_value % add_val == 0, so that when reversed, the
  4632.              biv will be zero on the last iteration.  */
  4633.  
  4634.           /* Calculating the final value non trivial.
  4635.              If branch is (LT (CC0) (CONST 0),
  4636.              then value in compare is final value.
  4637.              If branch is (LE (CC0) (CONST 0),
  4638.              then value in compare is final_value - add_val */
  4639.  
  4640.           branch_code
  4641.             = GET_CODE (XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 0));
  4642.           src_two_before_end
  4643.             = SET_SRC (PATTERN (PREV_INSN (PREV_INSN (loop_end))));
  4644.  
  4645.           win = 1;
  4646.           if (GET_CODE (src_two_before_end) == REG)
  4647.             constant = 0;
  4648.           else if (GET_CODE (src_two_before_end) == COMPARE
  4649.                && GET_CODE (XEXP (src_two_before_end, 1)) == CONST_INT)
  4650.             constant = INTVAL (XEXP (src_two_before_end, 1));
  4651.           else
  4652.             win = 0;
  4653.  
  4654.           if (win && bl->initial_value == const0_rtx
  4655.               && (branch_code == LT || branch_code == LE)
  4656.               && XEXP (XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 0), 1) == const0_rtx
  4657.               && (constant % INTVAL (bl->biv->add_val)) == 0)
  4658.             {
  4659.               /* Register will always be nonnegative, with value
  4660.              0 on last iteration if loop reversed */
  4661.  
  4662.               /* Save some info needed to produce the new insns.  */
  4663.               reg = SET_DEST (PATTERN (bl->biv->insn));
  4664.               jump_label = XEXP (SET_SRC (PATTERN (PREV_INSN (loop_end))), 1);
  4665.               new_add_val = gen_rtx (CONST_INT, VOIDmode,
  4666.                          - INTVAL (bl->biv->add_val));
  4667.  
  4668.  
  4669.               if (branch_code == LT)
  4670.             {
  4671.               final_value
  4672.                 = gen_rtx (CONST_INT, VOIDmode, constant);
  4673.               start_value
  4674.                 = gen_rtx (CONST_INT, VOIDmode,
  4675.                        (constant - INTVAL (bl->biv->add_val)));
  4676.             }
  4677.               else /* branch_code == LE */
  4678.             {
  4679.               start_value
  4680.                 = gen_rtx (CONST_INT, VOIDmode, constant);
  4681.               final_value
  4682.                 = gen_rtx (CONST_INT, VOIDmode,
  4683.                        (constant + INTVAL (bl->biv->add_val)));
  4684.             }
  4685.  
  4686.               /* Initialize biv to start_value before loop start.
  4687.              The old initializing insn will be deleted as a
  4688.              dead store by flow.c.  */
  4689.               emit_insn_before (gen_rtx (SET, VOIDmode, reg,
  4690.                          start_value),
  4691.                     loop_start);
  4692.  
  4693.               /* Add insn to decrement register, and delete insn
  4694.              that incremented the register.  */
  4695.               emit_insn_before (gen_rtx (SET, VOIDmode, reg,
  4696.                       gen_rtx (PLUS, GET_MODE (reg), reg,
  4697.                            new_add_val)),
  4698.                     bl->biv->insn);
  4699.               /* Update biv info to reflect its new status.  */
  4700.               bl->biv->insn = PREV_INSN (bl->biv->insn);
  4701.               delete_insn (NEXT_INSN (bl->biv->insn));
  4702.  
  4703.               /* Inc LABEL_NUSES so that delete_insn will
  4704.              not delete the label.  */
  4705.               LABEL_NUSES (XEXP (jump_label, 0)) ++;
  4706.  
  4707.               if (regno_last_uid[bl->regno] != INSN_UID (PREV_INSN (loop_end)))
  4708.             emit_insn_after (gen_rtx (SET, VOIDmode, reg,
  4709.                           final_value),
  4710.                      loop_end);
  4711.  
  4712.               /* Delete compare/branch at end of loop.  */
  4713.               delete_insn (PREV_INSN (loop_end));
  4714.               delete_insn (PREV_INSN (loop_end));
  4715.  
  4716.               /* Add new compare/branch insn at end of loop.  */
  4717.               emit_insn_before (gen_rtx (SET, VOIDmode, cc0_rtx, reg),
  4718.                     loop_end);
  4719.               emit_jump_insn_before (gen_rtx (SET, VOIDmode, pc_rtx,
  4720.                      gen_rtx (IF_THEN_ELSE, VOIDmode,
  4721.                          gen_rtx (GE, VOIDmode, cc0_rtx,
  4722.                               const0_rtx),
  4723.                          jump_label,
  4724.                          pc_rtx)),
  4725.                       loop_end);
  4726.  
  4727.               JUMP_LABEL (PREV_INSN (loop_end)) = XEXP (jump_label, 0);
  4728.               /* Increment of LABEL_NUSES done above. */
  4729.  
  4730.               /* Register is now always nonnegative,
  4731.              so add REG_NONNEG note to the branch.  */
  4732.               REG_NOTES (PREV_INSN (loop_end))
  4733.             = gen_rtx (EXPR_LIST, REG_NONNEG, 0,
  4734.                    REG_NOTES (PREV_INSN (loop_end)));
  4735.               bl->nonneg = 1;
  4736.  
  4737.               /* Update rest of biv info.  */
  4738.               bl->initial_value = start_value;
  4739.               bl->biv->add_val = new_add_val;
  4740.  
  4741.               if (loop_dump_stream)
  4742.             fprintf (loop_dump_stream, "Reversed loop and added reg_nonneg\n");
  4743.  
  4744.               return 1;
  4745.             }
  4746.         }
  4747.         }
  4748.     }
  4749.     }
  4750.   return 0;
  4751. }
  4752.  
  4753. /* Verify whether the biv BL appears to be eliminable,
  4754.    based on the insns in the loop that refer to it.
  4755.    LOOP_START is the first insn of the loop, and END is the end insn.  */
  4756.  
  4757. static void
  4758. check_eliminate_biv (bl, loop_start, end)
  4759.      struct iv_class *bl;
  4760.      rtx loop_start;
  4761.      rtx end;
  4762. {
  4763.   /* Get the REG rtx for the biv.  */
  4764.   rtx reg = SET_DEST (PATTERN (bl->biv->insn));
  4765.   rtx p;
  4766.   struct induction *v;
  4767.  
  4768.   bl->eliminable = 0;
  4769.  
  4770.   for (p = loop_start; p != end; p = NEXT_INSN (p))
  4771.     {
  4772.       enum rtx_code code = GET_CODE (p);
  4773.       if ((code == INSN || code == JUMP_INSN || code == CALL_INSN)
  4774.       && reg_mentioned_p (reg, PATTERN (p)))
  4775.     {
  4776.       /* This insn uses the biv.  If we can't understand it,
  4777.          then we can't eliminate the biv.  */
  4778.       if (GET_CODE (PATTERN (p)) != SET)
  4779.         {
  4780.           if (loop_dump_stream)
  4781.         fprintf (loop_dump_stream,
  4782.              "Cannot eliminate biv %d: cannot understand insn %d.\n",
  4783.              bl->regno, INSN_UID (p));
  4784.           break;
  4785.         }
  4786.  
  4787.       /* The insns that increment the biv are no problem.  */
  4788.       if (SET_DEST (PATTERN (p)) == reg)
  4789.         continue;
  4790.  
  4791.       /* If this is an insn which uses the biv ONLY in the
  4792.          calculation of a giv which is in the family of this
  4793.          biv, it's ok becuase it will go away when the giv is
  4794.          reduced.  March 14, 1989 -- self@bayes.arc.nasa.gov */
  4795.       for (v = bl->giv; v; v = v->family)
  4796.         if (v->insn == p)
  4797.           {
  4798.         if (v->giv_type == DEST_REG
  4799.             || (v->giv_type == DEST_ADDR
  4800.             /* Test was backwards - rms, 5 Dec 89 */
  4801.             && only_reg_use_p (reg, *(v->location),
  4802.                        PATTERN (p))))
  4803.           break;
  4804.           }
  4805.       if (v)
  4806.         continue;
  4807.  
  4808.       /* If can rewrite this insn not to use the biv, it's ok.  */
  4809.       if (can_eliminate_biv_p (p, bl))
  4810.         continue;
  4811.  
  4812.       /* Biv is used in a way we cannot eliminate.  */
  4813.       if (loop_dump_stream)
  4814.         fprintf (loop_dump_stream,
  4815.              "Cannot eliminate biv %d: biv used in insn %d.\n",
  4816.              bl->regno, INSN_UID (p));
  4817.       break;
  4818.     }
  4819.     }
  4820.  
  4821.   if (p == end)
  4822.     {
  4823.       bl->eliminable = 1;
  4824.       if (loop_dump_stream)
  4825.     fprintf (loop_dump_stream, "Can eliminate biv %d.\n",
  4826.          bl->regno);
  4827.     }
  4828. }
  4829.  
  4830. /* Return 1 if INSN, a compare insn which tests the biv described by BL,
  4831.    can be rewritten to use instead some reduced giv related to that biv.
  4832.    Does not change the rtl.
  4833.  
  4834.    We make the assumption that all the givs depending on this biv
  4835.    will be reduced, since only in that case will an attempt to eliminate
  4836.    the biv actually be made.
  4837.  
  4838.    The following function is very parallel to this one.  */
  4839.  
  4840. static int
  4841. can_eliminate_biv_p (insn, bl)
  4842.      rtx insn;
  4843.      struct iv_class *bl;
  4844. {
  4845.   rtx src;
  4846.   enum rtx_code code;
  4847.   struct induction *v, *tv;
  4848.   rtx arg;
  4849.   int arg_operand;
  4850.   /* Mode of this biv.  */
  4851.   enum machine_mode mode = bl->biv->mode;
  4852.  
  4853.   if (SET_DEST (PATTERN (insn)) != cc0_rtx)
  4854.     return 0;
  4855.  
  4856.   src = SET_SRC (PATTERN (insn));
  4857.   code = GET_CODE (src);
  4858.  
  4859.   switch (code)
  4860.     {
  4861.       /* a test insn */
  4862.     case REG:
  4863.       /* Can replace with any giv that has (MULT_VAL != 0) and (ADD_VAL == 0)
  4864.      Require a constant integer for MULT_VAL, so we know it's nonzero.  */
  4865.  
  4866.       for (v = bl->giv; v; v = v->family)
  4867.     if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
  4868.         && v->add_val == const0_rtx
  4869.         && ! v->ignore
  4870.         && v->mode == mode)
  4871.       return 1;
  4872.  
  4873.       /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0)
  4874.      where ADD_VAL is a constant or a register;
  4875.      can replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
  4876.      Require a constant integer for MULT_VAL, so we know it's nonzero.  */
  4877.  
  4878.       for (v = bl->giv; v; v = v->family)
  4879.     if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
  4880.         && (GET_CODE (v->add_val) == REG || GET_CODE (v->add_val) == CONST_INT)
  4881.         && ! v->ignore
  4882.         && v->mode == mode)
  4883.       return 1;
  4884.  
  4885.       if (loop_dump_stream)
  4886.     fprintf (loop_dump_stream, "Cannot eliminate biv %d in test insn %d: no appropriate giv.\n",
  4887.          bl->regno, INSN_UID (insn));
  4888.  
  4889.       return 0;
  4890.  
  4891.       /* a compare insn */
  4892.     case COMPARE:
  4893.       /* Figure out which operand is the biv.  */
  4894.       if ((GET_CODE (XEXP (src, 0)) == REG)
  4895.       && (REGNO (XEXP (src, 0)) == bl->regno))
  4896.     {
  4897.       arg = XEXP (src, 1);
  4898.       arg_operand = 1;
  4899.     }
  4900.       else if ((GET_CODE (XEXP (src, 1)) == REG)
  4901.            && (REGNO (XEXP (src, 1)) == bl->regno))
  4902.     {
  4903.       arg = XEXP (src, 0);
  4904.       arg_operand = 0;
  4905.     }
  4906.       else
  4907.     return 0;
  4908.  
  4909.       if (GET_CODE (arg) == CONST_INT)
  4910.     {
  4911.       /* Can replace with any giv that has constant coefficients.  */
  4912.  
  4913.       for (v = bl->giv; v; v = v->family)
  4914.         if (GET_CODE (v->mult_val) == CONST_INT
  4915.         && GET_CODE (v->add_val) == CONST_INT
  4916.         && ! v->ignore
  4917.         && v->mode == mode)
  4918.           return 1;
  4919.  
  4920.       /* Look for giv with constant mult_val and nonconst add_val,
  4921.          since we can insert add insn before loop
  4922.          to calculate new compare value.  */
  4923.  
  4924.       for (v = bl->giv; v; v = v->family)
  4925.         if (GET_CODE (v->mult_val) == CONST_INT
  4926.         && ! v->ignore
  4927.         && v->mode == mode)
  4928.           return 1;
  4929.     }
  4930.       else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
  4931.     {
  4932.       /* Comparing against invariant register or memref can be handled.  */
  4933.  
  4934.       if (invariant_p (arg))
  4935.         {
  4936.           /* Look for giv with constant mult_val and nonconst add_val.
  4937.          Insert add-insn before loop to compute new compare value.  */
  4938.  
  4939.           for (v = bl->giv; v; v = v->family)
  4940.         if ((GET_CODE (v->mult_val) == CONST_INT)
  4941.             && ! v->ignore
  4942.             && v->mode == mode)
  4943.           return 1;
  4944.         }
  4945.  
  4946.       /* Otherwise, only comparing against a biv can be handled.  */
  4947.       if (GET_CODE (arg) != REG
  4948.           || induct_var[REGNO (arg)] != BASIC_INDUCT)
  4949.         return 0;
  4950.  
  4951.       /* Look for a giv for each biv that have identical
  4952.          values for mult_val and add_val.  */
  4953.       for (v = bl->giv; v; v = v->family)
  4954.         if (v->mode == mode
  4955.         && ! v->ignore)
  4956.           {
  4957.         for (tv = class_struct[REGNO (arg)]->giv; tv; tv = tv->family)
  4958.           if ((tv->new_reg != 0)
  4959.               && rtx_equal_p (tv->mult_val, v->mult_val)
  4960.               && rtx_equal_p (tv->mult_val, v->mult_val)
  4961.               && ! tv->ignore
  4962.               && tv->mode == mode)
  4963.             return 1;
  4964.           }
  4965.     }
  4966.       return 0;
  4967.  
  4968.     default:
  4969.       return 0;
  4970.     }
  4971. }
  4972.  
  4973. /* Rewrite a compare insn INSN which uses the biv described by BL
  4974.    so that it doesn't use that biv any more.
  4975.    Instead it will use some reduced giv related to that biv.
  4976.  
  4977.    The preceding function is very parallel to this one.  */
  4978.  
  4979. static void
  4980. eliminate_biv (insn, bl, loop_start)
  4981.      rtx insn;
  4982.      struct iv_class *bl;
  4983.      rtx loop_start;
  4984. {
  4985.   rtx src = SET_SRC (PATTERN (insn));
  4986.   enum rtx_code code = GET_CODE (src);
  4987.   struct induction *v, *tv;
  4988.   rtx arg;
  4989.   int arg_operand;
  4990.   /* Mode of this biv.  */
  4991.   enum machine_mode mode = bl->biv->mode;
  4992.  
  4993.   switch (code)
  4994.     {
  4995.       /* a test insn */
  4996.     case REG:
  4997.       /* Can replace with any giv that was reduced and
  4998.      that has (MULT_VAL != 0) and (ADD_VAL == 0).
  4999.      Require a constant integer for MULT_VAL, so we know it's nonzero.  */
  5000.  
  5001.       for (v = bl->giv; v; v = v->family)
  5002.     if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
  5003.         && v->add_val == const0_rtx
  5004.         && v->new_reg != 0
  5005.         && v->mode == mode)
  5006.       break;
  5007.       if (v)
  5008.     {
  5009.       /* We can test the sign of that giv's reduced reg.  */
  5010.       SET_SRC (PATTERN (insn)) = v->new_reg;
  5011.       return;
  5012.     }
  5013.  
  5014.       /* Look for a giv with (MULT_VAL != 0) and (ADD_VAL != 0)
  5015.      where ADD_VAL is a constant or a register;
  5016.      replace test insn with a compare insn (cmp REDUCED_GIV ADD_VAL).
  5017.      Require a constant integer for MULT_VAL, so we know it's nonzero.  */
  5018.  
  5019.       for (v = bl->giv; v; v = v->family)
  5020.     if (GET_CODE (v->mult_val) == CONST_INT && v->mult_val != const0_rtx
  5021.         && (GET_CODE (v->add_val) == REG || GET_CODE (v->add_val) == CONST_INT)
  5022.         && v->new_reg != 0
  5023.         && v->mode == mode)
  5024.       break;
  5025.       if (v)
  5026.     {
  5027.       /* Replace biv with the giv's reduced register.  */
  5028.       SET_SRC (PATTERN (insn)) = gen_rtx (COMPARE, GET_MODE (v->new_reg),
  5029.                           v->new_reg,
  5030.                           copy_rtx (v->add_val));
  5031.  
  5032. #if 0
  5033.       /* add_val must be invariant, so don't bother storing in a register */
  5034.       /* calculate the appropriate constant to compare against */
  5035.       emit_insn_before (gen_rtx (SET, VOIDmode, compare_value,
  5036.                      copy_rtx (v->add_val)),
  5037.                 loop_start);
  5038. #endif
  5039.       return;
  5040.     }
  5041.       abort ();
  5042.       break;
  5043.  
  5044.       /* a compare insn */
  5045.     case COMPARE:
  5046.       /* Figure out which operand is the biv.  */
  5047.       if (GET_CODE (XEXP (src, 0)) == REG
  5048.       && REGNO (XEXP (src, 0)) == bl->regno)
  5049.     {
  5050.       arg = XEXP (src, 1);
  5051.       arg_operand = 1;
  5052.     }
  5053.       else if (GET_CODE (XEXP (src, 1)) == REG
  5054.            && REGNO (XEXP (src, 1)) == bl->regno)
  5055.     {
  5056.       arg = XEXP (src, 0);
  5057.       arg_operand = 0;
  5058.     }
  5059.       else
  5060.     abort ();
  5061.  
  5062.       if (GET_CODE (arg) == CONST_INT)
  5063.     {
  5064.       /* Can replace with any giv that has constant mult_val and add_val.
  5065.          Make sure it was strength reduced by checking new_reg != 0.  */
  5066.  
  5067.       for (v = bl->giv; v; v = v->family)
  5068.         if (GET_CODE (v->mult_val) == CONST_INT
  5069.         && GET_CODE (v->add_val) == CONST_INT
  5070.         && v->new_reg
  5071.         && v->mode == mode)
  5072.           break;
  5073.       if (v)
  5074.         {
  5075.           rtx newval;
  5076.           /* Replace biv with the giv's reduced reg.  */
  5077.           XEXP (src, 1-arg_operand) = v->new_reg;
  5078.           /* Calculate the appropriate constant to compare against.  */
  5079.           newval = gen_rtx (CONST_INT, VOIDmode,
  5080.                 (INTVAL (arg) * INTVAL (v->mult_val)
  5081.                  + INTVAL (v->add_val)));
  5082.           XEXP (src, arg_operand) = newval;
  5083.           /* If that constant is no good in a compare,
  5084.          put it in a register.  */
  5085.           if (recog (PATTERN (insn), insn) < 0)
  5086.         {
  5087.           rtx temp = gen_reg_rtx (mode);
  5088.           emit_iv_init_code (arg, v->mult_val, v->add_val,
  5089.                      temp, loop_start);
  5090.           XEXP (src, arg_operand) = temp;
  5091.         }
  5092.           return;
  5093.         }
  5094.  
  5095.       /* Look for giv with constant mult_val and nonconst add_val.
  5096.          Insert add insn before loop to calculate new compare value.  */
  5097.  
  5098.       for (v = bl->giv; v; v = v->family)
  5099.         if (GET_CODE (v->mult_val) == CONST_INT
  5100.         && v->new_reg
  5101.         && v->mode == mode)
  5102.           break;
  5103.       if (v)
  5104.         {
  5105.           rtx compare_value = gen_reg_rtx (mode);
  5106.  
  5107.           /* Replace biv with giv's reduced register.  */
  5108.           XEXP (src, 1-arg_operand) = v->new_reg;
  5109.  
  5110.           /* At start of loop, compute value to compare against.  */
  5111.           emit_iv_init_code (arg, v->mult_val, v->add_val,
  5112.                  compare_value, loop_start);
  5113.           /* Use it in this insn.  */
  5114.           XEXP (src, arg_operand) = compare_value;
  5115.           return;
  5116.         }
  5117.       abort ();
  5118.     }
  5119.       else if (GET_CODE (arg) == REG || GET_CODE (arg) == MEM)
  5120.     {
  5121.       if (invariant_p (arg))
  5122.         {
  5123.           /* Look for giv with constant mult_val and nonconst add_val.
  5124.          Insert add-insn before loop to compute new compare value.  */
  5125.  
  5126.           for (v = bl->giv; v; v = v->family)
  5127.         if (GET_CODE (v->mult_val) == CONST_INT
  5128.             && v->new_reg
  5129.             && v->mode == mode)
  5130.           break;
  5131.           if (v)
  5132.         {
  5133.           rtx compare_value = gen_reg_rtx (mode);
  5134.  
  5135.           /* Replace biv with giv's reduced register.  */
  5136.           XEXP (src, 1-arg_operand) = v->new_reg;
  5137.  
  5138.           /* At start of loop, compute value to compare against.  */
  5139.           emit_iv_init_code (arg, v->mult_val, v->add_val,
  5140.                      compare_value, loop_start);
  5141.           XEXP (src, arg_operand) = compare_value;
  5142.           return;
  5143.         }
  5144.         }
  5145.  
  5146.       /* Otherwise the reg compared with had better be a biv.  */
  5147.       if (GET_CODE (arg) != REG
  5148.           || induct_var[REGNO (arg)] != BASIC_INDUCT)
  5149.         abort ();
  5150.  
  5151.       /* Look for a pair of givs, one for each biv,
  5152.          with identical coefficients.  */
  5153.       for (v = bl->giv; v; v = v->family)
  5154.         {
  5155.           if (!v->new_reg && v->mode == mode)
  5156.         continue;
  5157.           for (tv = class_struct[REGNO (arg)]->giv; tv; tv = tv->family)
  5158.         if ((tv->new_reg != 0)
  5159.             && rtx_equal_p (tv->mult_val, v->mult_val)
  5160.             && rtx_equal_p (tv->add_val, v->add_val)
  5161.             && tv->mode == mode)
  5162.           break;
  5163.           if (tv)
  5164.         break;
  5165.         }
  5166.       if (v)
  5167.         {
  5168.           /* Replace biv with its giv's reduced reg.  */
  5169.           XEXP (src, 1-arg_operand) = v->new_reg;
  5170.           /* Replace other operand with the other giv's reduced reg.  */
  5171.           XEXP (src, arg_operand) = tv->new_reg;
  5172.           return;
  5173.         }
  5174.     }
  5175.       abort ();
  5176.  
  5177.     default:
  5178.       abort ();
  5179.     }
  5180. }
  5181.  
  5182. /* Try to calculate the final value of the biv,
  5183.    the value it will have at the end of the loop.
  5184.    If we can do it, return that value.  */
  5185.  
  5186. /* ??? One case that should be simple to handle
  5187.    is when the biv is incremented by an invariant
  5188.    exactly once each time around the loop,
  5189.    and when the number of iterations can be determined
  5190.    (as the value of some invariant).
  5191.    Then the final value would be BIV + (INCREMENT * NUM_ITERATIONS).
  5192.  
  5193.    Once that case is handled, it would become desirable to detect empty
  5194.    loops and delete them, since this optimization could make empty loops.  */
  5195.  
  5196. static rtx
  5197. final_biv_value (bl, loop_end)
  5198.      struct iv_class *bl;
  5199.      rtx loop_end;
  5200. {
  5201.   /* wimpy, but guaranteed to work */
  5202.   return 0;
  5203. }
  5204.  
  5205. /* Return nonzero if the last use of reg REGNO
  5206.    is in an insn following INSN in the same basic block.  */
  5207.  
  5208. static int
  5209. last_use_this_basic_block (regno, insn)
  5210.      int regno;
  5211.      rtx insn;
  5212. {
  5213.   rtx n;
  5214.   for (n = insn;
  5215.        n && GET_CODE (n) != CODE_LABEL && GET_CODE (n) != JUMP_INSN;
  5216.        n = NEXT_INSN (n))
  5217.     {
  5218.       if (regno_last_uid[regno] == INSN_UID (n))
  5219.     return 1;
  5220.     }
  5221.   return 0;
  5222. #endif /* we really hate it */
  5223. }
  5224.