home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 321 / compsrc3 / reload1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  67.1 KB  |  1,979 lines

  1. /* Reload pseudo regs into hard regs for insns that require hard regs.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "rtl.h"
  24. #include "flags.h"
  25. #include "regs.h"
  26. #include "hard-reg-set.h"
  27. #include "reload.h"
  28. #include "insn-config.h"
  29. #include "basic-block.h"
  30. #include <stdio.h>
  31.  
  32. #define min(A,B) ((A) < (B) ? (A) : (B))
  33.  
  34. /* This file contains the reload pass of the compiler, which is
  35.    run after register allocation has been done.  It checks that
  36.    each insn is valid (operands required to be in registers really
  37.    are in registers of the proper class) and fixes up invalid ones
  38.    by copying values temporarily into registers for the insns
  39.    that need them.
  40.  
  41.    The results of register allocation are described by the vector
  42.    reg_renumber; the insns still contain pseudo regs, but reg_renumber
  43.    can be used to find which hard reg, if any, a pseudo reg is in.
  44.  
  45.    The technique we always use is to free up a few hard regs that are
  46.    called ``reload regs'', and for each place where a pseudo reg
  47.    must be in a hard reg, copy it temporarily into one of the reload regs.
  48.  
  49.    All the pseudos that were formerly allocated to the hard regs that
  50.    are now in use as reload regs must be ``spilled''.  This means
  51.    that they go to other hard regs, or to stack slots if no other
  52.    available hard regs can be found.  Spilling can invalidate more
  53.    insns, requiring additional need for reloads, so we must keep checking
  54.    until the process stabilizes.
  55.  
  56.    For machines with different classes of registers, we must keep track
  57.    of the register class needed for each reload, and make sure that
  58.    we allocate enough reload registers of each class.
  59.  
  60.    The file reload.c contains the code that checks one insn for
  61.    validity and reports the reloads that it needs.  This file
  62.    is in charge of scanning the entire rtl code, accumulating the
  63.    reload needs, spilling, assigning reload registers to use for
  64.    fixing up each insn, and generating the new insns to copy values
  65.    into the reload registers.  */
  66.  
  67. /* During reload_as_needed, element N contains a REG rtx for the hard reg
  68.    into which pseudo reg N has been reloaded (perhaps for a previous insn). */
  69. static rtx *reg_last_reload_reg;
  70.  
  71. /* Element N is the constant value to which pseudo reg N is equivalent,
  72.    or zero if pseudo reg N is not equivalent to a constant.
  73.    find_reloads looks at this in order to replace pseudo reg N
  74.    with the constant it stands for.  */
  75. rtx *reg_equiv_constant;
  76.  
  77. /* Element N is the address of stack slot to which pseudo reg N is equivalent.
  78.    This is used when the address is not valid as a memory address
  79.    (because its displacement is too big for the machine.)  */
  80. rtx *reg_equiv_address;
  81.  
  82. /* Element N is the memory slot to which pseudo reg N is equivalent,
  83.    or zero if pseudo reg N is not equivalent to a memory slot.  */
  84. static rtx *reg_equiv_mem;
  85.  
  86. /* Element N is the insn that initialized reg N from its equivalent
  87.    constant or memory slot.  */
  88. static rtx *reg_equiv_init;
  89.  
  90. /* During reload_as_needed, element N contains the last pseudo regno
  91.    reloaded into the Nth reload register.  This vector is in parallel
  92.    with spill_regs.  */
  93. static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
  94.  
  95. /* Number of spill-regs so far; number of valid elements of spill_regs.  */
  96. static int n_spills;
  97.  
  98. /* In parallel with spill_regs, contains REG rtx's for those regs.
  99.    Holds the last rtx used for any given reg, or 0 if it has never
  100.    been used for spilling yet.  This rtx is reused, provided it has
  101.    the proper mode.  */
  102. static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
  103.  
  104. /* In parallel with spill_regs, contains nonzero for a spill reg
  105.    that was stored after the last time it was used.
  106.    The precise value is the insn generated to do the store.  */
  107. static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
  108.  
  109. /* This table is the inverse mapping of spill_regs:
  110.    indexed by hard reg number,
  111.    it contains the position of that reg in spill_regs,
  112.    or -1 for something that is not in spill_regs.  */
  113. static short spill_reg_order[FIRST_PSEUDO_REGISTER];
  114.  
  115. /* This table contains 1 for a register that may not be used
  116.    for retrying global allocation, or -1 for a register that may be used.
  117.    The registers that may not be used include all spill registers
  118.    and the frame pointer (if we are using one).  */
  119. static short forbidden_regs[FIRST_PSEUDO_REGISTER];
  120.  
  121. /* Describes order of use of registers for reloading
  122.    of spilled pseudo-registers.  `spills' is the number of
  123.    elements that are actually valid; new ones are added at the end.  */
  124. static char spill_regs[FIRST_PSEUDO_REGISTER];
  125.  
  126. /* Describes order of preference for putting regs into spill_regs.
  127.    Contains the numbers of all the hard regs, in order most preferred first.
  128.    This order is different for each function.
  129.    It is set up by order_regs_for_reload.
  130.    Empty elements at the end contain -1.  */
  131. static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
  132.  
  133. /* 1 for a hard register that appears explicitly in the rtl
  134.    (for example, function value registers, special registers
  135.    used by insns, structure value pointer registers).  */
  136. static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
  137.  
  138. /* Nonzero if spilling (REG n) does not require reloading it into
  139.    a register in order to do (MEM (REG n)).  */
  140.  
  141. static char spill_indirect_ok;
  142.  
  143. /* Indexed by basic block number, nonzero if there is any need
  144.    for a spill register in that basic block.
  145.    The pointer is 0 if we did stupid allocation and don't know
  146.    the structure of basic blocks.  */
  147.  
  148. char *basic_block_needs;
  149.  
  150. /* First uid used by insns created by reload in this function.
  151.    Used in find_equiv_reg.  */
  152. int reload_first_uid;
  153.  
  154. void mark_home_live ();
  155. static void reload_as_needed ();
  156. static rtx alter_frame_pointer_addresses ();
  157. static void alter_reg ();
  158. static int new_spill_reg();
  159. static int spill_hard_reg ();
  160. static void choose_reload_targets ();
  161. static void forget_old_reloads ();
  162. static void order_regs_for_reload ();
  163. static void eliminate_frame_pointer ();
  164.  
  165. extern void remove_death ();
  166. extern rtx adj_offsetable_operand ();
  167.  
  168. /* Main entry point for the reload pass, and only entry point
  169.    in this file.
  170.  
  171.    FIRST is the first insn of the function being compiled.
  172.  
  173.    GLOBAL nonzero means we were called from global_alloc
  174.    and should attempt to reallocate any pseudoregs that we
  175.    displace from hard regs we will use for reloads.
  176.    If GLOBAL is zero, we do not have enough information to do that,
  177.    so any pseudo reg that is spilled must go to the stack.
  178.  
  179.    DUMPFILE is the global-reg debugging dump file stream, or 0.
  180.    If it is nonzero, messages are written to it to describe
  181.    which registers are seized as reload regs, which pseudo regs
  182.    are spilled from them, and where the pseudo regs are reallocated to.  */
  183.  
  184. void
  185. reload (first, global, dumpfile)
  186.      rtx first;
  187.      int global;
  188.      FILE *dumpfile;
  189. {
  190.   register int class;
  191.   register int i;
  192.   register rtx insn;
  193.  
  194.   int something_changed;
  195.   int something_needs_reloads;
  196.   int new_basic_block_needs;
  197.  
  198.   /* The basic block number currently being processed for INSN.  */
  199.   int this_block;
  200.  
  201.   /* Enable find_equiv_reg to distinguish insns made by reload.  */
  202.   reload_first_uid = get_max_uid ();
  203.  
  204.   basic_block_needs = 0;
  205.  
  206.   /* Remember which hard regs appear explicitly
  207.      before we merge into `regs_ever_live' the ones in which
  208.      pseudo regs have been allocated.  */
  209.   bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
  210.  
  211.   /* Compute which hard registers are now in use
  212.      as homes for pseudo registers.
  213.      This is done here rather than (eg) in global_alloc
  214.      because this point is reached even if not optimizing.  */
  215.  
  216.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  217.     mark_home_live (i);
  218.  
  219.   /* Find all the pseudo registers that didn't get hard regs
  220.      but do have known equivalent constants or memory slots.
  221.      These include parameters (known equivalent to parameter slots)
  222.      and cse'd or loop-moved constant memory addresses.
  223.  
  224.      Record constant equivalents in reg_equiv_constant
  225.      so they will be substituted by find_reloads.
  226.      Record memory equivalents in reg_mem_equiv so they can
  227.      be substituted eventually by altering the REG-rtx's.  */
  228.  
  229.   reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
  230.   bzero (reg_equiv_constant, max_regno * sizeof (rtx));
  231.   reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
  232.   bzero (reg_equiv_mem, max_regno * sizeof (rtx));
  233.   reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
  234.   bzero (reg_equiv_init, max_regno * sizeof (rtx));
  235.   reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
  236.   bzero (reg_equiv_address, max_regno * sizeof (rtx));
  237.  
  238.   for (insn = first; insn; insn = NEXT_INSN (insn))
  239.     if (GET_CODE (insn) == INSN
  240.     && GET_CODE (PATTERN (insn)) == SET
  241.     && GET_CODE (SET_DEST (PATTERN (insn))) == REG)
  242.       {
  243.     rtx note = find_reg_note (insn, REG_EQUIV, 0);
  244.     if (note)
  245.       {
  246.         rtx x = XEXP (note, 0);
  247.         i = REGNO (SET_DEST (PATTERN (insn)));
  248.         if (GET_CODE (x) == MEM)
  249.           reg_equiv_mem[i] = x;
  250.         else if (immediate_operand (x))
  251.           reg_equiv_constant[i] = x;
  252.         else
  253.           continue;
  254.  
  255.         reg_equiv_init[i] = insn;
  256.       }
  257.       }
  258.  
  259.   /* Does this function require a frame pointer?  */
  260.  
  261.   frame_pointer_needed
  262.     |= (! global || FRAME_POINTER_REQUIRED);
  263.  
  264.   if (! frame_pointer_needed)
  265.     frame_pointer_needed
  266.       = check_frame_pointer_required (reg_equiv_constant, reg_equiv_mem);
  267.  
  268.   /* Alter each pseudo-reg rtx to contain its hard reg number.
  269.      Delete initializations of pseudos that don't have hard regs
  270.      and do have equivalents.
  271.      Assign stack slots to the pseudos that lack hard regs or equivalents.  */
  272.  
  273.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  274.     alter_reg (i);
  275.  
  276. #ifndef REGISTER_CONSTRAINTS
  277.   /* If all the pseudo regs have hard regs,
  278.      except for those that are never referenced,
  279.      we know that no reloads are needed.  */
  280.   /* But that is not true if there are register constraints, since
  281.      in that case some pseudos might be in the wrong kind of hard reg.  */
  282.  
  283.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  284.     if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
  285.       break;
  286.  
  287.   if (i == max_regno && frame_pointer_needed)
  288.     return;
  289. #endif
  290.  
  291.   /* Compute the order of preference for hard registers to spill.
  292.      Store them by decreasing preference in potential_reload_regs.  */
  293.  
  294.   order_regs_for_reload ();
  295.  
  296.   /* So far, no hard regs have been spilled.  */
  297.   n_spills = 0;
  298.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  299.     {
  300.       spill_reg_order[i] = -1;
  301.       forbidden_regs[i] = -1;
  302.     }
  303.  
  304.   if (frame_pointer_needed)
  305.     {
  306.       forbidden_regs[FRAME_POINTER_REGNUM] = 1;
  307.       spill_hard_reg (FRAME_POINTER_REGNUM, global, dumpfile);
  308.     }
  309.  
  310.   if (global)
  311.     {
  312.       basic_block_needs = (char *)alloca (n_basic_blocks);
  313.       bzero (basic_block_needs, n_basic_blocks);
  314.     }
  315.  
  316.   /* This loop scans the entire function each go-round
  317.      and repeats until one repetition spills no additional hard regs.  */
  318.  
  319.   /* This flag is set when a psuedo reg is spilled,
  320.      to require another pass.  Note that getting an additional reload
  321.      reg does not necessarily imply any pseudo reg was spilled;
  322.      sometimes we find a reload reg that no pseudo reg was allocated in.  */
  323.   something_changed = 1;
  324.   /* This flag is set if there are any insns that require reloading.  */
  325.   something_needs_reloads = 0;
  326.   while (something_changed)
  327.     {
  328.       /* For each class, number of reload regs needed in that class.
  329.      This is the maximum over all insns of the needs in that class
  330.      of the individual insn.  */
  331.       int max_needs[N_REG_CLASSES];
  332.       /* For each class, size of group of consecutive regs
  333.      that is needed for the reloads of this class.  */
  334.       int group_size[N_REG_CLASSES];
  335.       /* For each class, max number of consecutive groups needed.
  336.      (Each group contains max_needs_size[CLASS] consecutive registers.)  */
  337.       int max_groups[N_REG_CLASSES];
  338.       /* For each class, max number needed of regs that don't belong
  339.      to any of the groups.  */
  340.       int max_nongroups[N_REG_CLASSES];
  341.       /* For each class, the machine mode which requires consecutive
  342.      groups of regs of that class.
  343.      If two different modes ever require groups of one class,
  344.      they must be tieable and the same number of regs;
  345.      otherwise we can't handle the complexity.  */
  346.       enum machine_mode group_mode[N_REG_CLASSES];
  347.       /* For each register, 1 if it was counted against the need for
  348.      groups.  0 means it can count against max_nongroup instead.  */
  349.       char counted_for_groups[FIRST_PSEUDO_REGISTER];
  350.       /* For each register, 1 if it was counted against the need for
  351.      non-groups.  0 means it can become part of a new group.  */
  352.       char counted_for_nongroups[FIRST_PSEUDO_REGISTER];
  353.  
  354.       something_changed = 0;
  355.       bzero (max_needs, sizeof max_needs);
  356.       bzero (max_groups, sizeof max_groups);
  357.       bzero (max_nongroups, sizeof max_nongroups);
  358.       bzero (group_size, sizeof group_size);
  359.       for (i = 0; i < N_REG_CLASSES; i++)
  360.     group_mode[i] = VOIDmode;
  361.  
  362.       /* Keep track of which basic blocks are needing the reloads.  */
  363.       this_block = 0;
  364.  
  365.       /* Remember whether any element of basic_block_needs
  366.      changes from 0 to 1 in this pass.  */
  367.       new_basic_block_needs = 0;
  368.  
  369.       /* Compute the most additional registers needed by any instruction.
  370.      Collect information separately for each class of regs.  */
  371.  
  372.       for (insn = first; insn; insn = NEXT_INSN (insn))
  373.     {
  374.       if (global && insn == basic_block_head[this_block+1])
  375.         ++this_block;
  376.  
  377.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  378.           || GET_CODE (insn) == CALL_INSN)
  379.         {
  380.           int insn_needs[N_REG_CLASSES];
  381.           int insn_groups[N_REG_CLASSES];
  382.           int insn_total_groups = 0;
  383.  
  384.           for (i = 0; i < N_REG_CLASSES; i++)
  385.         insn_needs[i] = 0, insn_groups[i] = 0;
  386.  
  387. #if 0
  388.           /* Optimization: a bit-field instruction whose field
  389.          happens to be a byte or halfword in memory
  390.          can be changed to a move instruction.  */
  391.  
  392.           if (GET_CODE (PATTERN (insn)) == SET)
  393.         {
  394.           rtx dest = SET_DEST (PATTERN (insn));
  395.           rtx src = SET_SRC (PATTERN (insn));
  396.  
  397.           if (GET_CODE (dest) == ZERO_EXTRACT
  398.               || GET_CODE (dest) == SIGN_EXTRACT)
  399.             optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
  400.           if (GET_CODE (src) == ZERO_EXTRACT
  401.               || GET_CODE (src) == SIGN_EXTRACT)
  402.             optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
  403.         }
  404. #endif
  405.  
  406.           /* Analyze the instruction.  */
  407.  
  408.           find_reloads (insn, 0, spill_indirect_ok, global, spill_reg_order);
  409.  
  410.           if (n_reloads == 0)
  411.         continue;
  412.  
  413.           something_needs_reloads = 1;
  414.  
  415.           /* Count each reload once in every class
  416.          containing the reload's own class.  */
  417.  
  418.           for (i = 0; i < n_reloads; i++)
  419.         {
  420.           register enum reg_class *p;
  421.           int size;
  422.           enum machine_mode mode;
  423.  
  424.           /* Don't count the dummy reloads, for which one of the
  425.              regs mentioned in the insn can be used for reloading.
  426.              Don't count optional reloads.
  427.              Don't count reloads that got combined with others.  */
  428.           if (reload_reg_rtx[i] != 0
  429.               || reload_optional[i] != 0
  430.               || (reload_out[i] == 0 && reload_in[i] == 0))
  431.             continue;
  432.  
  433.           mode = reload_inmode[i];
  434.           if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
  435.             mode = reload_outmode[i];
  436.           size = CLASS_MAX_NREGS (reload_reg_class[i], mode);
  437.           if (size > 1)
  438.             {
  439.               /* Count number of groups needed separately from
  440.              number of individual regs needed.  */
  441.               insn_groups[(int) reload_reg_class[i]]++;
  442.               p = reg_class_superclasses[(int) reload_reg_class[i]];
  443.               while (*p != LIM_REG_CLASSES)
  444.             insn_groups[(int) *p++]++;
  445.               insn_total_groups++;
  446.  
  447.               /* If a group of consecutive regs are needed,
  448.              record which machine mode needs them.
  449.              Crash if two dissimilar machine modes both need
  450.              groups of consecutive regs of the same class.  */
  451.  
  452.               if (group_mode[(int) reload_reg_class[i]] != VOIDmode
  453.               &&
  454.               (!MODES_TIEABLE_P (group_mode[(int) reload_reg_class[i]], mode)
  455.                ||
  456.                group_size[(int) reload_reg_class[i]] != size))
  457.             abort ();
  458.  
  459.               /* Record size and mode of a group of this class.  */
  460.               group_size[(int) reload_reg_class[i]] = size;
  461.               group_mode[(int) reload_reg_class[i]] = mode;
  462.             }
  463.           else if (size == 1)
  464.             {
  465.               insn_needs[(int) reload_reg_class[i]] += 1;
  466.               p = reg_class_superclasses[(int) reload_reg_class[i]];
  467.               while (*p != LIM_REG_CLASSES)
  468.             insn_needs[(int) *p++] += 1;
  469.             }
  470.           else
  471.             abort ();
  472.  
  473.           if (global)
  474.             {
  475.               if (! basic_block_needs[this_block])
  476.             new_basic_block_needs = 1;
  477.               basic_block_needs[this_block] = 1;
  478.             }
  479.         }
  480.  
  481.           /* Remember for later shortcuts which insns had any reloads.  */
  482.  
  483.           PUT_MODE (insn, n_reloads ? QImode : VOIDmode);
  484.  
  485.           /* For each class, collect maximum need of any insn */
  486.  
  487.           for (i = 0; i < N_REG_CLASSES; i++)
  488.         {
  489.           if (max_needs[i] < insn_needs[i])
  490.             max_needs[i] = insn_needs[i];
  491.           if (max_groups[i] < insn_groups[i])
  492.             max_groups[i] = insn_groups[i];
  493.           if (insn_total_groups > 0)
  494.             if (max_nongroups[i] < insn_needs[i])
  495.               max_nongroups[i] = insn_needs[i];
  496.         }
  497.         }
  498.       /* Note that there is a continue statement above.  */
  499.     }
  500.  
  501.       /* Now deduct from the needs for the registers already
  502.      available (already spilled).  */
  503.  
  504.       bzero (counted_for_groups, sizeof counted_for_groups);
  505.       bzero (counted_for_nongroups, sizeof counted_for_nongroups);
  506.  
  507.       /* Find all consecutive groups of spilled registers
  508.      and mark each group off against the need for such groups.  */
  509.  
  510.       for (i = 0; i < N_REG_CLASSES; i++)
  511.     if (group_size[i] > 1)
  512.       {
  513.         char regmask[FIRST_PSEUDO_REGISTER];
  514.         int j;
  515.  
  516.         bzero (regmask, sizeof regmask);
  517.         /* Make a mask of all the regs that are spill regs in class I.  */
  518.         for (j = 0; j < n_spills; j++)
  519.           if (TEST_HARD_REG_BIT (reg_class_contents[i], spill_regs[j])
  520.           && !counted_for_groups[spill_regs[i]])
  521.         regmask[spill_regs[j]] = 1;
  522.         /* Find each consecutive group of them.  */
  523.         for (j = 0; j < FIRST_PSEUDO_REGISTER && max_groups[i] > 0; j++)
  524.           if (regmask[j] && j + group_size[i] <= FIRST_PSEUDO_REGISTER
  525.           /* Next line in case group-mode for this class
  526.              demands an even-odd pair.  */
  527.           && HARD_REGNO_MODE_OK (j, group_mode[i]))
  528.         {
  529.           int k;
  530.           for (k = 1; k < group_size[i]; k++)
  531.             if (! regmask[j + k])
  532.               break;
  533.           if (k == group_size[i])
  534.             {
  535.               /* We found a group.  Mark it off against this class's
  536.              need for groups, and against each superclass too.  */
  537.               register enum reg_class *p;
  538.               max_groups[i]--;
  539.               p = reg_class_superclasses[i];
  540.               while (*p != LIM_REG_CLASSES)
  541.             max_groups[(int) *p++]--;
  542.               /* Don't count these registers again.  */ 
  543.               counted_for_groups[j] = 1;
  544.               for (k = 1; k < group_size[i]; k++)
  545.             counted_for_groups[j + k] = 1;
  546.             }
  547.           j += k;
  548.         }
  549.       }
  550.  
  551.       /* Now count all remaining spill regs against the individual need.
  552.      Those that weren't counted_for_groups in groups can also count against
  553.      the not-in-group need.  */
  554.  
  555.       for (i = 0; i < n_spills; i++)
  556.     {
  557.       register enum reg_class *p;
  558.       class = (int) REGNO_REG_CLASS (spill_regs[i]);
  559.  
  560.       max_needs[class]--;
  561.       p = reg_class_superclasses[class];
  562.       while (*p != LIM_REG_CLASSES)
  563.         max_needs[(int) *p++]--;
  564.  
  565.       if (! counted_for_groups[spill_regs[i]])
  566.         {
  567.           if (max_nongroups[class] > 0)
  568.         counted_for_nongroups[spill_regs[i]] = 1;
  569.           max_nongroups[class]--;
  570.           p = reg_class_superclasses[class];
  571.           while (*p != LIM_REG_CLASSES)
  572.         {
  573.           if (max_nongroups[(int) *p] > 0)
  574.             counted_for_nongroups[spill_regs[i]] = 1;
  575.           max_nongroups[(int) *p++]--;
  576.         }
  577.         }
  578.     }
  579.  
  580.       /* If all needs are met, we win.  */
  581.  
  582.       for (i = 0; i < N_REG_CLASSES; i++)
  583.     if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
  584.       break;
  585.       if (i == N_REG_CLASSES && !new_basic_block_needs)
  586.     break;
  587.  
  588.       /* Not all needs are met; must spill more hard regs.  */
  589.  
  590.       /* If any element of basic_block_needs changed from 0 to 1,
  591.      re-spill all the regs already spilled.  This may spill
  592.      additional pseudos that didn't spill before.  */
  593.  
  594.       if (new_basic_block_needs)
  595.     for (i = 0; i < n_spills; i++)
  596.       something_changed
  597.         |= spill_hard_reg (spill_regs[i], global, dumpfile);
  598.  
  599.       /* Now find more reload regs to satisfy the remaining need
  600.      First satisfy all need for groups of registers.
  601.      Count them in `spills', and add entries to
  602.      `spill_regs' and `spill_reg_order'.  */
  603.  
  604.       for (class = 0; class < N_REG_CLASSES; class++)
  605.     while (max_groups[class] > 0)
  606.       {
  607.         register enum reg_class *p;
  608.  
  609.         /* First, if we need more groups of consecutive regs, get them.
  610.            Either get a spill register that completes a group
  611.            or, if that cannot be done, get one that starts a group.
  612.            Here we do not yet handle groups of size > 2.  */
  613.         if (max_groups[class] > 0)
  614.           {
  615.         if (group_size[class] > 2)
  616.           abort ();
  617.  
  618.         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  619.           {
  620.             int j = potential_reload_regs[i];
  621.             if (j >= 0
  622.             &&
  623.             ((j > 0 && spill_reg_order[j - 1] >= 0
  624.               && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  625.               && TEST_HARD_REG_BIT (reg_class_contents[class], j - 1)
  626.               && HARD_REGNO_MODE_OK (j - 1, group_mode[class])
  627.               && ! counted_for_nongroups[j - 1])
  628.              ||
  629.              (j < FIRST_PSEUDO_REGISTER - 1
  630.               && spill_reg_order[j + 1] >= 0
  631.               && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  632.               && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
  633.               && HARD_REGNO_MODE_OK (j, group_mode[class])
  634.               && ! counted_for_nongroups[j + 1])))
  635.               {
  636.             /* We have found one that will complete a group,
  637.                so count off one group as provided.  */
  638.             max_groups[class]--;
  639.             p = reg_class_superclasses[class];
  640.             while (*p != LIM_REG_CLASSES)
  641.               max_groups[(int) *p++]--;
  642.             break;
  643.               }
  644.           }
  645.         /* We can't complete any group, so start one.  */
  646.         if (i == FIRST_PSEUDO_REGISTER)
  647.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  648.             {
  649.               int j = potential_reload_regs[i];
  650.               if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
  651.               && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
  652.               && TEST_HARD_REG_BIT (reg_class_contents[class], j)
  653.               && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
  654.               && HARD_REGNO_MODE_OK (j, group_mode[class])
  655.               && ! counted_for_nongroups[j + 1])
  656.             break;
  657.             }
  658.           }
  659.  
  660.         /* I should be the index in potential_reload_regs
  661.            of the new reload reg we have found.  */
  662.  
  663.         something_changed
  664.           |= new_spill_reg (i, class, max_needs, 0,
  665.                 global, dumpfile);
  666.     }
  667.  
  668.       /* Now similarly satisfy all need for single registers.  */
  669.  
  670.       for (class = 0; class < N_REG_CLASSES; class++)
  671.     while (max_needs[class] > 0 || max_nongroups[class] > 0)
  672.       {
  673.         /* Consider the potential reload regs that aren't
  674.            yet in use as reload regs, in order of preference.
  675.            Find the most preferred one that's in this class.  */
  676.  
  677.         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  678.           if (potential_reload_regs[i] >= 0
  679.           && TEST_HARD_REG_BIT (reg_class_contents[class],
  680.                     potential_reload_regs[i]))
  681.         break;
  682.  
  683.         /* I should be the index in potential_reload_regs
  684.            of the new reload reg we have found.  */
  685.  
  686.         something_changed
  687.           |= new_spill_reg (i, class, max_needs, max_nongroups,
  688.                 global, dumpfile);
  689.     }
  690.     }
  691.  
  692.   /* Now we know for certain whether we have a frame pointer.
  693.      If not, correct all references to go through the stack pointer.
  694.      This must be done before reloading, since reloading could generate
  695.      insns where sp+const cannot validly replace the frame pointer.
  696.      *This will lose if an insn might need more spill regs after
  697.      frame pointer elimination than it needed before.*  */
  698.  
  699.   if (! frame_pointer_needed)
  700.     eliminate_frame_pointer (first);
  701.  
  702.   /* Use the reload registers where necessary
  703.      by generating move instructions to move the must-be-register
  704.      values into or out of the reload registers.  */
  705.  
  706.   if (something_needs_reloads)
  707.     reload_as_needed (first, global);
  708.  
  709.   /* Now eliminate all pseudo regs by modifying them into
  710.      their equivalent memory references.
  711.      The REG-rtx's for the pseudos are modified in place,
  712.      so all insns that used to refer to them now refer to memory.
  713.  
  714.      For a reg that has a reg_equiv_address, all those insns
  715.      were changed by reloading so that no insns refer to it any longer;
  716.      but the DECL_RTL of a variable decl may refer to it,
  717.      and if so this causes the debugging info to mention the variable.  */
  718.  
  719.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  720.     {
  721.       rtx addr = 0;
  722.       if (reg_equiv_mem[i])
  723.     addr = XEXP (reg_equiv_mem[i], 0);
  724.       if (reg_equiv_address[i])
  725.     addr = reg_equiv_address[i];
  726.       if (addr)
  727.     {
  728.       if (reg_renumber[i] < 0)
  729.         {
  730.           rtx reg = regno_reg_rtx[i];
  731.           XEXP (reg, 0) = addr;
  732.           PUT_CODE (reg, MEM);
  733.         }
  734.       else if (reg_equiv_mem[i])
  735.         {
  736.           if (! frame_pointer_needed)
  737.         FIX_FRAME_POINTER_ADDRESS (addr, 0);
  738.           XEXP (reg_equiv_mem[i], 0) = addr;
  739.         }
  740.     }
  741.     }
  742. }
  743.  
  744. /* Add a new register REGNO to the tables of available spill-registers
  745.     (as well as spilling all pseudos allocated to the register).
  746.    CLASS is the regclass whose need is being satisfied.
  747.    MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
  748.     so that this register can count off against them.
  749.     MAX_NONGROUPS is 0 if this register is part of a group.
  750.    GLOBAL and DUMPFILE are the same as the args that `reload' got.  */
  751.  
  752. static int
  753. new_spill_reg (regno, class, max_needs, max_nongroups, global, dumpfile)
  754.      int regno;
  755.      int class;
  756.      int *max_needs;
  757.      int *max_nongroups;
  758.      int global;
  759.      FILE *dumpfile;
  760. {
  761.   register enum reg_class *p;
  762.   int val;
  763.  
  764.   if (regno >= FIRST_PSEUDO_REGISTER)
  765.     abort ();    /* Caller failed to find any register.  */
  766.  
  767.   /* Make potential_reload_regs[REGNO] an additional reload reg.  */
  768.  
  769.   spill_regs[n_spills] = potential_reload_regs[regno];
  770.   spill_reg_order[potential_reload_regs[regno]] = n_spills;
  771.   forbidden_regs[potential_reload_regs[regno]] = 1;
  772.   potential_reload_regs[regno] = -1;
  773.   if (dumpfile)
  774.     fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
  775.  
  776.   /* Clear off the needs we just satisfied.  */
  777.  
  778.   max_needs[class]--;
  779.   p = reg_class_superclasses[class];
  780.   while (*p != LIM_REG_CLASSES)
  781.     max_needs[(int) *p++]--;
  782.  
  783.   if (max_nongroups)
  784.     {
  785.       max_nongroups[class]--;
  786.       p = reg_class_superclasses[class];
  787.       while (*p != LIM_REG_CLASSES)
  788.     max_nongroups[(int) *p++]--;
  789.     }
  790.  
  791.   /* Spill every pseudo reg that was allocated to this reg
  792.      or to something that overlaps this reg.  */
  793.  
  794.   val = spill_hard_reg (spill_regs[n_spills], global, dumpfile);
  795.  
  796.   regs_ever_live[spill_regs[n_spills]] = 1;
  797.   n_spills++;
  798.  
  799.   return val;
  800. }
  801.  
  802. /* Scan all insns, computing the stack depth, and convert all
  803.    frame-pointer-relative references to stack-pointer-relative references.  */
  804.  
  805. static void
  806. eliminate_frame_pointer (first)
  807.      rtx first;
  808. {
  809.   int depth = 0;
  810.   int max_uid = get_max_uid ();
  811.   int *label_depth = (int *) alloca ((max_uid + 1) * sizeof (int));
  812.   int i;
  813.   rtx insn;
  814.  
  815.   for (i = 0; i <= max_uid; i++)
  816.     label_depth[i] = -1;
  817.  
  818.   /* In this loop, for each forward branch we record the stack
  819.      depth of the label it jumps to.  We take advantage of the fact
  820.      that the stack depth at a label reached by a backward branch
  821.      is always, in GCC output, equal to the stack depth of the preceding
  822.      unconditional jump, because it was either a loop statement or
  823.      statement label.  */
  824.  
  825.   for (insn = first; insn; insn = NEXT_INSN (insn))
  826.     {
  827.       rtx pattern = PATTERN (insn);
  828.       switch (GET_CODE (insn))
  829.     {
  830.     case INSN:
  831.       alter_frame_pointer_addresses (pattern, depth);
  832. #ifdef PUSH_ROUNDING
  833.       /* Notice pushes and pops; update DEPTH.  */
  834.       if (GET_CODE (pattern) == SET)
  835.         {
  836.           if (push_operand (SET_DEST (pattern),
  837.                 GET_MODE (SET_DEST (pattern))))
  838.         depth += PUSH_ROUNDING (GET_MODE_SIZE (GET_MODE (SET_DEST (pattern))));
  839.           if (GET_CODE (SET_DEST (pattern)) == REG
  840.           && REGNO (SET_DEST (pattern)) == STACK_POINTER_REGNUM)
  841.         {
  842.           int delta;
  843.           if (GET_CODE (SET_SRC (pattern)) == PLUS
  844.               && GET_CODE (XEXP (SET_SRC (pattern), 0)) == REG
  845.               && REGNO (XEXP (SET_SRC (pattern), 0)) == STACK_POINTER_REGNUM)
  846.             delta = INTVAL (XEXP (SET_SRC (pattern), 1));
  847.           else if (GET_CODE (SET_SRC (pattern)) == MINUS
  848.                && GET_CODE (XEXP (SET_SRC (pattern), 0)) == REG
  849.                && REGNO (XEXP (SET_SRC (pattern), 0)) == STACK_POINTER_REGNUM)
  850.             delta = -INTVAL (XEXP (SET_SRC (pattern), 1));
  851.           else abort ();
  852. #ifdef STACK_GROWS_DOWNWARD
  853.           depth -= delta;
  854. #else
  855.           depth += delta;
  856. #endif
  857.         }
  858.         }
  859. #endif
  860.       break;
  861.  
  862.     case JUMP_INSN:
  863.       alter_frame_pointer_addresses (pattern, depth);
  864.       if (GET_CODE (pattern) == ADDR_VEC)
  865.         for (i = 0; i < XVECLEN (pattern, 0); i++)
  866.           label_depth[INSN_UID (XEXP (XVECEXP (pattern, 0, i), 0))] = depth;
  867.       else if (GET_CODE (pattern) == ADDR_DIFF_VEC)
  868.         {
  869.           label_depth[INSN_UID (XEXP (XEXP (pattern, 0), 0))] = depth;
  870.           for (i = 0; i < XVECLEN (pattern, 1); i++)
  871.         label_depth[INSN_UID (XEXP (XVECEXP (pattern, 1, i), 0))] = depth;
  872.         }
  873.       else if (JUMP_LABEL (insn))
  874.         label_depth[INSN_UID (JUMP_LABEL (insn))] = depth;
  875.       else
  876.       break;
  877.  
  878.     case CODE_LABEL:
  879.       if (label_depth [INSN_UID (insn)] >= 0)
  880.         depth = label_depth [INSN_UID (insn)];
  881.       break;
  882.  
  883.     case CALL_INSN:
  884.       alter_frame_pointer_addresses (pattern, depth);
  885.       break;
  886.     }
  887.     }
  888. }
  889.  
  890. /* Walk the rtx X, converting all frame-pointer refs to stack-pointer refs
  891.    on the assumption that the current temporary stack depth is DEPTH.
  892.    (The size of saved registers must be added to DEPTH
  893.    to get the actual offset between the logical frame-pointer and the
  894.    stack pointer.  FIX_FRAME_POINTER_ADDRESS takes care of that.)  */
  895.  
  896. static rtx
  897. alter_frame_pointer_addresses (x, depth)
  898.      register rtx x;
  899.      int depth;
  900. {
  901.   register int i;
  902.   register char *fmt;
  903.   register enum rtx_code code = GET_CODE (x);
  904.  
  905.   switch (code)
  906.     {
  907.     case CONST_INT:
  908.     case CONST:
  909.     case SYMBOL_REF:
  910.     case LABEL_REF:
  911.     case CONST_DOUBLE:
  912.     case CC0:
  913.     case PC:
  914.       return x;
  915.  
  916.     case REG:
  917.       /* Frame ptr can occur outside a PLUS if a stack slot
  918.      can occur with offset 0.  */
  919.       if (x == frame_pointer_rtx)
  920.     {
  921.       FIX_FRAME_POINTER_ADDRESS (x, depth);
  922.     }
  923.       return x;
  924.  
  925.     case MEM:
  926.       {
  927.     rtx addr = XEXP (x, 0);
  928.     FIX_FRAME_POINTER_ADDRESS (addr, depth);
  929.     XEXP (x, 0) = addr;
  930.       }
  931.       break;
  932.  
  933.     case PLUS:
  934.       /* Handle addresses being loaded or pushed, etc.,
  935.      rather than referenced.  */
  936.       FIX_FRAME_POINTER_ADDRESS (x, depth);
  937.       break;
  938.     }
  939.  
  940.   fmt = GET_RTX_FORMAT (code);
  941.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  942.     {
  943.       if (fmt[i] == 'e')
  944.     XEXP (x, i) = alter_frame_pointer_addresses (XEXP (x, i), depth);
  945.       else if (fmt[i] == 'E')
  946.     {
  947.       register int j;
  948.       for (j = XVECLEN (x, i) - 1; j >=0; j--)
  949.         XVECEXP (x, i, j)
  950.           = alter_frame_pointer_addresses (XVECEXP (x, i, j), depth);
  951.     }
  952.     }
  953.   return x;
  954. }
  955.  
  956. static void
  957. alter_reg (i)
  958.      register int i;
  959. {
  960.   /* If the reg got changed to a MEM at rtl-generation time,
  961.      ignore it.  */
  962.   if (GET_CODE (regno_reg_rtx[i]) != REG)
  963.     return;
  964.  
  965.   /* Modify the reg-rtx to contain the new hard reg
  966.      number or else to contain its pseudo reg number.  */
  967.   REGNO (regno_reg_rtx[i])
  968.     = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
  969.  
  970.   if (reg_renumber[i] < 0 && reg_equiv_init[i])
  971.     {
  972.       /* Delete the insn that loads the pseudo register.  */
  973.       PUT_CODE (reg_equiv_init[i], NOTE);
  974.       NOTE_LINE_NUMBER (reg_equiv_init[i])
  975.     = NOTE_INSN_DELETED;
  976.       NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
  977.     }
  978.  
  979.   /* If we have a pseudo that is needed but has no hard reg or equivalent,
  980.      allocate a stack slot for it.  */
  981.  
  982.   if (reg_renumber[i] < 0
  983.       && reg_n_refs[i] > 0
  984.       && reg_equiv_constant[i] == 0
  985.       && reg_equiv_mem[i] == 0)
  986.     {
  987.       register rtx x = assign_stack_local (GET_MODE (regno_reg_rtx[i]),
  988.                        PSEUDO_REGNO_BYTES (i));
  989.       register rtx addr = XEXP (x, 0);
  990.       /* If the stack slot is directly addressable, substitute
  991.      the MEM we just got directly for the old REG.
  992.      Otherwise, record the address; we will generate hairy code
  993.      to compute the address in a register each time it is needed.  */
  994.       if (memory_address_p (GET_MODE (regno_reg_rtx[i]), addr))
  995.     reg_equiv_mem[i] = x;
  996.       else
  997.     reg_equiv_address[i] = XEXP (x, 0);
  998.     }
  999. }
  1000.  
  1001. /* Mark the slots in regs_ever_live for the hard regs
  1002.    used by pseudo-reg number REGNO.  */
  1003.  
  1004. void
  1005. mark_home_live (regno)
  1006.      int regno;
  1007. {
  1008.   register int i, lim;
  1009.   i = reg_renumber[regno];
  1010.   if (i < 0)
  1011.     return;
  1012.   lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
  1013.   while (i < lim)
  1014.     regs_ever_live[i++] = 1;
  1015. }
  1016.  
  1017. /* Kick all pseudos out of hard register REGNO.
  1018.    If GLOBAL is nonzero, try to find someplace else to put them.
  1019.    If DUMPFILE is nonzero, log actions taken on that file.
  1020.  
  1021.    Return nonzero if any pseudos needed to be kicked out.  */
  1022.  
  1023. static int
  1024. spill_hard_reg (regno, global, dumpfile)
  1025.      register int regno;
  1026.      int global;
  1027.      FILE *dumpfile;
  1028. {
  1029.   int something_changed = 0;
  1030.   register int i;
  1031.  
  1032.   /* Spill every pseudo reg that was allocated to this reg
  1033.      or to something that overlaps this reg.  */
  1034.  
  1035.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1036.     if (reg_renumber[i] >= 0
  1037.     && reg_renumber[i] <= regno
  1038.     && (reg_renumber[i] 
  1039.         + HARD_REGNO_NREGS (reg_renumber[i],
  1040.                 PSEUDO_REGNO_MODE (i))
  1041.         > regno))
  1042.       {
  1043. #if 1
  1044.     /* If this register belongs solely to a basic block
  1045.        which needed no spilling, leave it be.  */
  1046.     if (basic_block_needs
  1047.         && reg_basic_block[i] >= 0
  1048.         && basic_block_needs[reg_basic_block[i]] == 0)
  1049.       continue;
  1050. #endif
  1051.  
  1052.     /* Mark it as no longer having a hard register home.  */
  1053.     reg_renumber[i] = -1;
  1054.     /* We will need to scan everything again.  */
  1055.     something_changed = 1;
  1056.     if (global)
  1057.       {
  1058.         retry_global_alloc (i, forbidden_regs);
  1059.         /* Update regs_ever_live for new home (if any).  */
  1060.         mark_home_live (i);
  1061.         /* If something gets spilled to the stack,
  1062.            we must have a frame pointer, so spill the frame pointer.  */
  1063.         if (reg_renumber[i] == -1 && ! frame_pointer_needed)
  1064.           {
  1065.         frame_pointer_needed = 1;
  1066.         forbidden_regs[FRAME_POINTER_REGNUM] = 1;
  1067.         spill_hard_reg (FRAME_POINTER_REGNUM, global, dumpfile);
  1068.           }
  1069.       }
  1070.     alter_reg (i);
  1071.     if (dumpfile)
  1072.       {
  1073.         if (reg_renumber[i] == -1)
  1074.           fprintf (dumpfile, " Register %d now on stack.\n\n", i);
  1075.         else
  1076.           fprintf (dumpfile, " Register %d now in %d.\n\n",
  1077.                i, reg_renumber[i]);
  1078.       }
  1079.       }
  1080.  
  1081.   return something_changed;
  1082. }
  1083.  
  1084. struct hard_reg_n_uses { int regno; int uses; };
  1085.  
  1086. static int
  1087. hard_reg_use_compare (p1, p2)
  1088.      struct hard_reg_n_uses *p1, *p2;
  1089. {
  1090.   return p1->uses - p2->uses;
  1091. }
  1092.  
  1093. /* Choose the order to consider regs for use as reload registers
  1094.    based on how much trouble would be caused by spilling one.
  1095.    Store them in order of decreasing preference in potential_reload_regs.  */
  1096.  
  1097. static void
  1098. order_regs_for_reload ()
  1099. {
  1100.   register int i;
  1101.   register int o = 0;
  1102.   int large = 0;
  1103.  
  1104.   struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
  1105.  
  1106.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1107.     potential_reload_regs[i] = -1;
  1108.  
  1109.   /* Count number of uses of each hard reg by pseudo regs allocated to it
  1110.      and then order them by decreasing use.  */
  1111.  
  1112.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1113.     {
  1114.       hard_reg_n_uses[i].uses = 0;
  1115.       hard_reg_n_uses[i].regno = i;
  1116.     }
  1117.  
  1118.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1119.     {
  1120.       if (reg_renumber[i] >= 0)
  1121.     hard_reg_n_uses[reg_renumber[i]].uses += reg_n_refs[i];
  1122.       large += reg_n_refs[i];
  1123.     }
  1124.  
  1125.   /* Now fixed registers (which cannot safely be used for reloading)
  1126.      get a very high use count so they will be considered least desirable.
  1127.      Likewise registers used explicitly in the rtl code.  */
  1128.  
  1129.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1130.     if (fixed_regs[i] || regs_explicitly_used[i])
  1131.       hard_reg_n_uses[i].uses = large;
  1132.   hard_reg_n_uses[FRAME_POINTER_REGNUM].uses = large;
  1133.  
  1134.   qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
  1135.      sizeof hard_reg_n_uses[0], hard_reg_use_compare);
  1136.  
  1137.   /* Prefer registers not so far used, for use in temporary loading.
  1138.      Among them, prefer registers not preserved by calls.  */
  1139.  
  1140.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1141.     if (regs_ever_live[i] == 0 && call_used_regs[i]
  1142.     && ! fixed_regs[i])
  1143.       potential_reload_regs[o++] = i;
  1144.  
  1145.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1146.     if (regs_ever_live[i] == 0 && ! call_used_regs[i]
  1147.     && i != FRAME_POINTER_REGNUM)
  1148.       potential_reload_regs[o++] = i;
  1149.  
  1150.   /* Now add the regs that are already used,
  1151.      preferring those used less often.  */
  1152.  
  1153.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1154.     if (regs_ever_live[hard_reg_n_uses[i].regno] != 0)
  1155.       potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
  1156.  
  1157. #if 0
  1158.   /* For regs that are used, don't prefer those not preserved by calls
  1159.      because those are likely to contain high priority things
  1160.      that are live for short periods of time.  */
  1161.  
  1162.   for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
  1163.     if (regs_ever_live[i] != 0 && ! call_used_regs[i])
  1164.       potential_reload_regs[o++] = i;
  1165. #endif
  1166. }
  1167.  
  1168. /* Reload pseudo-registers into hard regs around each insn as needed.
  1169.    Additional register load insns are output before the insn that needs it
  1170.    and perhaps store insns after insns that modify the reloaded pseudo reg.
  1171.  
  1172.    reg_last_reload_reg and reg_reloaded_contents keep track of
  1173.    which pseudo-registers are already available in reload registers.
  1174.    We update these for the reloads that we perform,
  1175.    as the insns are scanned.  */
  1176.  
  1177. static void
  1178. reload_as_needed (first, live_known)
  1179.      rtx first;
  1180.      int live_known;
  1181. {
  1182.   register rtx insn;
  1183.   register int i;
  1184.  
  1185.   /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
  1186.      Set spill_indirect_ok if so.  */
  1187.   register rtx tem
  1188.     = gen_rtx (MEM, SImode,
  1189.            gen_rtx (PLUS, Pmode,
  1190.             gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM),
  1191.             gen_rtx (CONST_INT, VOIDmode, 4)));
  1192.  
  1193.   spill_indirect_ok = memory_address_p (QImode, tem);
  1194.  
  1195.   bzero (spill_reg_rtx, sizeof spill_reg_rtx);
  1196.   reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
  1197.   bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
  1198.   for (i = 0; i < n_spills; i++)
  1199.     reg_reloaded_contents[i] = -1;
  1200.  
  1201.   for (insn = first; insn;)
  1202.     {
  1203.       register rtx next = NEXT_INSN (insn);
  1204.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
  1205.       || GET_CODE (insn) == CALL_INSN)
  1206.     {
  1207.       if (GET_MODE (insn) == VOIDmode)
  1208.         n_reloads = 0;
  1209.       /* First find the pseudo regs that must be reloaded for this insn.
  1210.          This info is returned in the tables reload_... (see reload.h).
  1211.          Also modify the body of INSN by substituting RELOAD
  1212.          rtx's for those pseudo regs.  */
  1213.       else
  1214.         find_reloads (insn, 1, spill_indirect_ok, live_known, spill_reg_order);
  1215.  
  1216.       if (n_reloads > 0)
  1217.         {
  1218.           /* Now compute which reload regs to reload them into.  Perhaps
  1219.          reusing reload regs from previous insns, or else output
  1220.          load insns to reload them.  Maybe output store insns too.
  1221.          Record the choices of reload reg in reload_reg_rtx.  */
  1222.           choose_reload_targets (insn, n_spills);
  1223.           /* Substitute the chosen reload regs from reload_reg_rtx
  1224.          into the insn's body (or perhaps into the bodies of other
  1225.          load and store insn that we just made for reloading
  1226.          and that we moved the structure into).  */
  1227.           subst_reloads ();
  1228.         }
  1229.       /* Any previously reloaded spilled pseudo reg, stored in this insn,
  1230.          is no longer validly lying around to save a future reload.
  1231.          Note that this does not detect pseudos that were reloaded
  1232.          for this insn in order to be stored in
  1233.          (obeying register constraints).  That is correct; such reload
  1234.          registers ARE still valid.  */
  1235.       forget_old_reloads (PATTERN (insn));
  1236.     }
  1237.       /* A reload reg's contents are unknown after a label.  */
  1238.       if (GET_CODE (insn) == CODE_LABEL)
  1239.     for (i = 0; i < n_spills; i++)
  1240.       reg_reloaded_contents[i] = -1;
  1241.  
  1242.       /* Don't assume a reload reg is still good after a call insn
  1243.      if it is a call-used reg.  */
  1244.       if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
  1245.     for (i = 0; i < n_spills; i++)
  1246.       if (call_used_regs[spill_regs[i]])
  1247.         reg_reloaded_contents[i] = -1;
  1248.  
  1249.       /* In case registers overlap, allow certain insns to invalidate
  1250.      particular hard registers.  */
  1251.  
  1252. #ifdef INSN_CLOBBERS_REGNO_P
  1253.       for (i = 0 ; i < n_spills ; i++)
  1254.     if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
  1255.       reg_reloaded_contents[i] = -1;
  1256. #endif
  1257.  
  1258.       insn = next;
  1259.     }
  1260. }
  1261.  
  1262. /* If we see a pseudo-reg being stored into,
  1263.    don't try to reuse an old reload reg
  1264.    which previously contained a copy of it.  */
  1265.  
  1266. static void
  1267. forget_old_reloads (x)
  1268.      rtx x;
  1269. {
  1270.   if (GET_CODE (x) == SET && GET_CODE (SET_DEST (x)) == REG)
  1271.     {
  1272.       register int regno = REGNO (SET_DEST (x));
  1273.       int nr;
  1274.  
  1275.       if (regno >= FIRST_PSEUDO_REGISTER)
  1276.     nr = 1;
  1277.       else
  1278.     {
  1279.       int i;
  1280.       nr = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  1281.       /* Storing into a spilled-reg invalidates its contents.
  1282.          This can happen if a block-local pseudo is allocated to that reg
  1283.          and it wasn't spilled because this block's total need is 0.
  1284.          Then some insn might have an optional reload and use this reg.  */
  1285.       for (i = 0; i < nr; i++)
  1286.         if (spill_reg_order[regno + i] >= 0)
  1287.           reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
  1288.     }
  1289.       
  1290.       while (nr-- > 0) reg_last_reload_reg[regno + nr] = 0;
  1291.     }
  1292.   else if (GET_CODE (x) == PARALLEL)
  1293.     {
  1294.       register int i;
  1295.       for (i = 0; i < XVECLEN (x, 0); i++)
  1296.     {
  1297.       register rtx y = XVECEXP (x, 0, i);
  1298.       if (GET_CODE (y) == SET && GET_CODE (SET_DEST (y)) == REG)
  1299.         forget_old_reloads (y);
  1300.     }
  1301.     }
  1302. }
  1303.  
  1304. /* Comparison function for qsort to decide which of two reloads
  1305.    should be handled first.  *P1 and *P2 are the reload numbers.  */
  1306.  
  1307. static int
  1308. reload_reg_class_lower (p1, p2)
  1309.      short *p1, *p2;
  1310. {
  1311.   register int r1 = *p1, r2 = *p2;
  1312.   register int t;
  1313.   register enum machine_mode mode1, mode2;
  1314.   
  1315.   /* Consider required reloads before optional ones.  */
  1316.   t = reload_optional[r1] - reload_optional[r2];
  1317.   if (t != 0)
  1318.     return t;
  1319.   /* Consider all multi-reg groups first.
  1320.      This is safe because `reload' fills all group-need before
  1321.      filling all non-group need.  */
  1322.   mode1 = (reload_inmode[r1] == VOIDmode ? reload_outmode[r1] : reload_inmode[r1]);
  1323.   mode2 = (reload_inmode[r2] == VOIDmode ? reload_outmode[r2] : reload_inmode[r2]);
  1324.   t = (CLASS_MAX_NREGS (reload_reg_class[r2], mode2)
  1325.        - CLASS_MAX_NREGS (reload_reg_class[r1], mode1));
  1326.   if (t != 0)
  1327.     return t;
  1328.   /* Consider reloads in order of increasing reg-class number.  */
  1329.   t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
  1330.   return t;
  1331. }
  1332.  
  1333. /* Assign hard reg targets for the pseudo-registers we must reload
  1334.    into hard regs for this insn.
  1335.    Also output the instructions to copy them in and out of the hard regs.
  1336.  
  1337.    For machines with register classes, we are responsible for
  1338.    finding a reload reg in the proper class.  */
  1339.  
  1340. static void
  1341. choose_reload_targets (insn)
  1342.      rtx insn;
  1343. {
  1344.   register int j;
  1345.   char reload_reg_in_use[FIRST_PSEUDO_REGISTER];
  1346.   short reload_order[FIRST_PSEUDO_REGISTER];
  1347.   char reload_inherited[FIRST_PSEUDO_REGISTER];
  1348.   /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
  1349.      for an output reload that stores into reg N.  */
  1350.   char *reg_has_output_reload;
  1351.   int have_groups = 0;
  1352.  
  1353.   /* For each reload, the index in spill_regs of the spill register used,
  1354.      or -1 if we did not need one of the spill registers for this reload.  */
  1355.   int reload_spill_index[FIRST_PSEUDO_REGISTER];
  1356.  
  1357.   bzero (reload_inherited, FIRST_PSEUDO_REGISTER);
  1358.   bzero (reload_reg_in_use, FIRST_PSEUDO_REGISTER);
  1359.  
  1360.   reg_has_output_reload = (char *) alloca (max_regno);
  1361.   bzero (reg_has_output_reload, max_regno);
  1362.  
  1363.   /* In order to be certain of getting the registers we need,
  1364.      we must sort the reloads into order of increasing register class.
  1365.      Then our grabbing of reload registers will parallel the process
  1366.      that provided the reload registers.  */
  1367.  
  1368.   /* Also note whether any of the reloads wants a consecutive group of regs.
  1369.      When that happens, we must when processing the non-group reloads
  1370.      avoid (when possible) using a reload reg that would break up a group.  */
  1371.  
  1372.   /* This used to look for an existing reloaded home for all
  1373.      of the reloads, and only then perform any new reloads.
  1374.      But that could lose if the reloads were done out of reg-class order
  1375.      because a later reload with a looser constraint might have an old
  1376.      home in a register needed by an earlier reload with a tighter constraint.
  1377.      It would be possible with even hairier code to detect such cases
  1378.      and handle them, but it doesn't seem worth while yet.  */
  1379.  
  1380.   for (j = 0; j < n_reloads; j++)
  1381.     {
  1382.       enum machine_mode mode;
  1383.       reload_order[j] = j;
  1384.       reload_spill_index[j] = -1;
  1385.       mode = (reload_inmode[j] == VOIDmode ? reload_outmode[j] : reload_inmode[j]);
  1386.       if (CLASS_MAX_NREGS (reload_reg_class[j], mode) > 1)
  1387.     have_groups = 1;
  1388.     }
  1389.  
  1390.   if (n_reloads > 1)
  1391.     qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
  1392.  
  1393.   for (j = 0; j < n_reloads; j++)
  1394.     {
  1395.       register int r = reload_order[j];
  1396.       register int i;
  1397.       register rtx new;
  1398.       enum machine_mode reload_mode = reload_inmode[r];
  1399.  
  1400.       if (GET_MODE_SIZE (reload_outmode[r]) > GET_MODE_SIZE (reload_mode))
  1401.     reload_mode = reload_outmode[r];
  1402.       if (reload_strict_low[r])
  1403.     reload_mode = GET_MODE (SUBREG_REG (reload_out[r]));
  1404.  
  1405.       /* Ignore reloads that got marked inoperative.  */
  1406.       if (reload_out[r] == 0 && reload_in[r] == 0)
  1407.     continue;
  1408.  
  1409.       /* No need to find a reload-register if find_reloads chose one.  */
  1410.  
  1411.       if (reload_reg_rtx[r] != 0)
  1412.     {
  1413. #if 0
  1414.       /* But do see if the chosen reload-reg already contains
  1415.          a copy of the desired value.  */
  1416.       if (reload_in[r] != 0)
  1417.         {
  1418.           register rtx equiv
  1419.         = find_equiv_reg (reload_in[r], insn, 0,
  1420.                   REGNO (reload_reg_rtx[r]), 0, 0,
  1421.                   reload_mode);
  1422.           if (equiv != 0)
  1423.         reload_inherited[r] = 1;
  1424.         }
  1425. #endif
  1426.       continue;
  1427.     }
  1428.  
  1429.       /* First see if this pseudo is already available as reloaded
  1430.      for a previous insn.
  1431.      This feature is disabled for multi-register groups
  1432.      because we haven't yet any way to tell whether the entire
  1433.      value is properly preserved.
  1434.      It is also disabled when there are other reloads for mult-register
  1435.      groups, lest the inherited reload reg break up a needed group.  */
  1436.  
  1437.       {
  1438.     register int regno = -1;
  1439.  
  1440.     if (reload_in[r] == 0)
  1441.       ;
  1442.     else if (GET_CODE (reload_in[r]) == REG)
  1443.       regno = REGNO (reload_in[r]);
  1444. #if 0
  1445.     /* This won't work, since REGNO can be a pseudo reg number.
  1446.        Also, it takes much more hair to keep track of all the things
  1447.        that can invalidate an inherited reload of part of a pseudoreg.  */
  1448.     else if (GET_CODE (reload_in[r]) == SUBREG
  1449.          && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
  1450.       regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
  1451. #endif
  1452.  
  1453.     if (regno >= 0
  1454.         && GET_MODE_SIZE (reload_mode) <= UNITS_PER_WORD
  1455.         && reg_last_reload_reg[regno] != 0
  1456.         && ! have_groups)
  1457.       {
  1458.         i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
  1459.  
  1460.         if (reg_reloaded_contents[i] == regno
  1461.         && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
  1462.                       spill_regs[i])
  1463.         && ! reload_reg_in_use[spill_regs[i]])
  1464.           {
  1465.         /* Mark the reload register as in use for this insn.  */
  1466.         reload_reg_rtx[r] = reg_last_reload_reg[regno];
  1467.         reload_reg_in_use[spill_regs[i]] = 1;
  1468.         reload_inherited[r] = 1;
  1469.         reload_spill_index[r] = i;
  1470.           }
  1471.       }
  1472.       }
  1473.  
  1474.       /* If this is not a pseudo, here's a different way to see
  1475.      if it is already lying around.  */
  1476.       if (reload_in[r] != 0
  1477.       && reload_out[r] == 0
  1478.       && (CONSTANT_P (reload_in[r])
  1479.           || GET_CODE (reload_in[r]) == PLUS
  1480.           || GET_CODE (reload_in[r]) == MEM)
  1481.       && ! have_groups)
  1482.     {
  1483.       register rtx equiv
  1484.         = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
  1485.                   -1, (short *)1, 0, reload_mode);
  1486.       /* If we found an equivalent reg, say no code need be generated
  1487.          to load it, and use it as our reload reg.  */
  1488.       if (equiv != 0
  1489.           && REGNO (equiv) != FRAME_POINTER_REGNUM)
  1490.         {
  1491.           reload_reg_rtx[r] = equiv;
  1492.           reload_inherited[r] = 1;
  1493.           /* If it is a spill reg,
  1494.          mark the spill reg as in use for this insn.  */
  1495.           i = spill_reg_order[REGNO (equiv)];
  1496.           if (i >= 0)
  1497.         {
  1498.           int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1499.           while (nr > 0)
  1500.             reload_reg_in_use[REGNO (equiv) + --nr] = 1;
  1501.         }
  1502.         }
  1503.     }
  1504.  
  1505.       /* If it isn't lying around, and isn't optional,
  1506.      find a place to reload it into.  */
  1507.       if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
  1508.     continue;
  1509.  
  1510.       /* Value not lying around; find a register to reload it into.
  1511.      Here I is not a regno, it is an index into spill_regs.  */
  1512.       i = n_spills;
  1513.  
  1514. #if 0
  1515.       /* The following is no longer needed now that all multi-register
  1516.      (group) reloads are processed before all solitary register reloads
  1517.      (due to changes in `reg_class_lower_p' and `reload'.  */
  1518.       /* The following also fails to test HARD_REGNO_MODE_OK appropriately,
  1519.      which was hard to fix because we don't know the mode that the
  1520.      group might have that would want this register.  */
  1521.  
  1522.       /* If we want just one reg, and other reloads want groups,
  1523.      first try to find a reg that can't be part of a group.  */
  1524.       if (have_groups
  1525.       && CLASS_MAX_NREGS (reload_reg_class[r], reload_mode) == 1)
  1526.     for (i = 0; i < n_spills; i++)
  1527.       {
  1528.         int regno = spill_regs[i];
  1529.         int class = (int) reload_reg_class[r];
  1530.         if (reload_reg_in_use[regno] == 0
  1531.         && TEST_HARD_REG_BIT (reg_class_contents[class],
  1532.                       regno)
  1533.         && !(regno + 1 < FIRST_PSEUDO_REGISTER
  1534.              && spill_reg_order[regno + 1] >= 0
  1535.              && reload_reg_in_use[regno + 1] == 0
  1536.              && TEST_HARD_REG_BIT (reg_class_contents[class],
  1537.                        regno + 1))
  1538.         && !(regno > 0
  1539.              && spill_reg_order[regno - 1] >= 0
  1540.              && reload_reg_in_use[regno - 1] == 0
  1541.              && TEST_HARD_REG_BIT (reg_class_contents[class],
  1542.                        regno - 1)))
  1543.           break;
  1544.       }
  1545.  
  1546.       /* If that didn't work, try to find a register that has only one
  1547.      neighbor that could make a group with it.  That way, if the
  1548.      available registers are three consecutive ones, we avoid taking
  1549.      the middle one (which would leave us with no possible groups).  */
  1550.  
  1551.       if (have_groups
  1552.       && CLASS_MAX_NREGS (reload_reg_class[r], reload_mode) == 1
  1553.       && i == n_spills)
  1554.     for (i = 0; i < n_spills; i++)
  1555.       {
  1556.         int regno = spill_regs[i];
  1557.         int class = (int) reload_reg_class[r];
  1558.         if (reload_reg_in_use[regno] == 0
  1559.         && TEST_HARD_REG_BIT (reg_class_contents[class],
  1560.                       regno)
  1561.         && (!(regno + 1 < FIRST_PSEUDO_REGISTER
  1562.               && spill_reg_order[regno + 1] >= 0
  1563.               && reload_reg_in_use[regno + 1] == 0
  1564.               && TEST_HARD_REG_BIT (reg_class_contents[class],
  1565.                         regno + 1))
  1566.             || !(regno > 0
  1567.              && spill_reg_order[regno - 1] >= 0
  1568.              && reload_reg_in_use[regno - 1] == 0
  1569.              && TEST_HARD_REG_BIT (reg_class_contents[class],
  1570.                            regno - 1))))
  1571.           break;
  1572.       }
  1573. #endif
  1574.  
  1575.       /* Now, if we want a single register and haven't yet found one,
  1576.      take any reg in the right class and not in use.
  1577.      If we want a consecutive group, here is where we look for it.  */
  1578.       if (i == n_spills)
  1579.     for (i = 0; i < n_spills; i++)
  1580.       {
  1581.         int class = (int) reload_reg_class[r];
  1582.         if (reload_reg_in_use[spill_regs[i]] == 0
  1583.         && TEST_HARD_REG_BIT (reg_class_contents[class],
  1584.                       spill_regs[i]))
  1585.           {
  1586.         int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1587.         /* If we need only one reg, we have already won.  */
  1588.         if (nr == 1)
  1589.           break;
  1590.         /* Otherwise check that as many consecutive regs as we need
  1591.            are available here.  */
  1592.         if (HARD_REGNO_MODE_OK (spill_regs[i], reload_mode))
  1593.           while (nr > 1)
  1594.             {
  1595.               if (!(TEST_HARD_REG_BIT (reg_class_contents[class],
  1596.                            spill_regs[i] + nr - 1)
  1597.                 && spill_reg_order[spill_regs[i] + nr - 1] >= 0
  1598.                 && reload_reg_in_use[spill_regs[i] + nr - 1] == 0))
  1599.             break;
  1600.               nr--;
  1601.             }
  1602.         if (nr == 1)
  1603.           break;
  1604.           }
  1605.       }
  1606.  
  1607.       /* We should have found a spill register by now.  */
  1608.       if (i == n_spills)
  1609.     abort ();
  1610.  
  1611.       /* Mark as in use for this insn the reload regs we use for this.  */
  1612.       {
  1613.     int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode);
  1614.     while (nr > 0)
  1615.       reload_reg_in_use[spill_regs[i] + --nr] = 1;
  1616.       }
  1617.  
  1618.       new = spill_reg_rtx[i];
  1619.  
  1620.       if (new == 0 || GET_MODE (new) != reload_mode)
  1621.     spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode, spill_regs[i]);
  1622.  
  1623.       reload_reg_rtx[r] = new;
  1624.       reload_spill_index[r] = i;
  1625.       reg_reloaded_contents[i] = -1;
  1626.  
  1627.       /* Detect when the reload reg can't hold the reload mode.  */
  1628.       if (! HARD_REGNO_MODE_OK (REGNO (reload_reg_rtx[r]), reload_mode))
  1629.     {
  1630.       if (! asm_noperands (PATTERN (insn)))
  1631.         /* It's the compiler's fault.  */
  1632.         abort ();
  1633.       /* It's the user's fault; the operand's mode and constraint
  1634.          don't match.  Disable this reload so we don't crash in final.
  1635.          Maybe we should print an error message too??  */
  1636.       reload_in[r] = 0;
  1637.       reload_out[r] = 0;
  1638.       reload_reg_rtx[r] = 0;
  1639.       reload_optional[r] = 1;
  1640.     }
  1641.     }
  1642.  
  1643.   /* For all the spill regs newly reloaded in this instruction,
  1644.      record what they were reloaded from, so subsequent instructions
  1645.      can inherit the reloads.  */
  1646.  
  1647.   for (j = 0; j < n_reloads; j++)
  1648.     {
  1649.       register int r = reload_order[j];
  1650.       register int i = reload_spill_index[r];
  1651.  
  1652.       /* I is nonneg if this reload used one of the spill regs.
  1653.      If reload_reg_rtx[r] is 0, this is an optional reload
  1654.      that we opted to ignore.  */
  1655.       if (i >= 0 && reload_reg_rtx[r] != 0)
  1656.     {
  1657.       /* Maybe the spill reg contains a copy of reload_out.  */
  1658.       if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
  1659.         {
  1660.           register int nregno = REGNO (reload_out[r]);
  1661.           reg_last_reload_reg[nregno] = reload_reg_rtx[r];
  1662.           reg_reloaded_contents[i] = nregno;
  1663.           reg_has_output_reload[nregno] = 1;
  1664.         }
  1665.       /* Maybe the spill reg contains a copy of reload_in.  */
  1666.       else if (reload_out[r] == 0 && GET_CODE (reload_in[r]) == REG)
  1667.         {
  1668.           register int nregno = REGNO (reload_in[r]);
  1669.           /* If there are two separate reloads (one in and one out)
  1670.          for the same (hard or pseudo) reg, set reg_last_reload_reg
  1671.          based on the output reload.  */
  1672.           if (!reg_has_output_reload[nregno])
  1673.         {
  1674.           reg_last_reload_reg[nregno] = reload_reg_rtx[r];
  1675.           reg_reloaded_contents[i] = nregno;
  1676.         }
  1677.         }
  1678.       /* Otherwise, the spill reg doesn't contain a copy of any reg.
  1679.          Clear out its records, lest it be taken for a copy
  1680.          of reload_in when that is no longer true.  */
  1681.       else
  1682.         reg_reloaded_contents[i] = -1;
  1683.     }
  1684.     }
  1685.  
  1686.   /* Now output the instructions to copy the data into and out of the
  1687.      reload registers.  Do these in the order that the reloads were reported,
  1688.      since reloads of base and index registers precede reloads of operands
  1689.      and the operands may need the base and index registers reloaded.  */
  1690.  
  1691.   for (j = 0; j < n_reloads; j++)
  1692.     {
  1693.       register rtx old;
  1694.       rtx store_insn;
  1695.  
  1696.       old = reload_in[j];
  1697.       if (old != 0 && ! reload_inherited[j]
  1698.       && reload_reg_rtx[j] != old
  1699.       && reload_reg_rtx[j] != 0)
  1700.     {
  1701.       register rtx reloadreg = reload_reg_rtx[j];
  1702.       rtx oldequiv = 0;
  1703.       enum machine_mode mode;
  1704.  
  1705.       /* Strip off of OLD any size-increasing SUBREGs such as
  1706.          (SUBREG:SI foo:QI 0).  */
  1707.  
  1708.       while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
  1709.          && (GET_MODE_SIZE (GET_MODE (old))
  1710.              > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
  1711.         old = SUBREG_REG (old);
  1712.  
  1713.       /* Determine the mode to reload in.
  1714.          This is very tricky because we have three to choose from.
  1715.          There is the mode the insn operand wants (reload_inmode[J]).
  1716.          There is the mode of the reload register RELOADREG.
  1717.          There is the intrinsic mode of the operand, revealed now
  1718.          in OLD because we have stripped SUBREGs.
  1719.          It turns out that RELOADREG's mode is irrelevant:
  1720.          we can change that arbitrarily.
  1721.  
  1722.          Neither of the other two is always right.  For example, consider
  1723.          (SUBREG:SI foo:QI)) appearing as an operand that must be SImode;
  1724.          then suppose foo is in memory.  This must be loaded in QImode
  1725.          because we cannot fetch a byte as a word.  In this case OLD's
  1726.          mode is correct.
  1727.  
  1728.          Then consider a one-word union which has SImode and one of its
  1729.          members is a float, being fetched as (SUBREG:SF union:SI).
  1730.          We must fetch that as SFmode because we could be loading into
  1731.          a float-only register.  In this case OLD's mode is also correct.
  1732.  
  1733.          Consider an immediate integer: it has VOIDmode.  Here we need
  1734.          to get a mode from something else.
  1735.  
  1736.          In some cases, there is a fourth mode, the operand's
  1737.          containing mode.  If the insn specifies a containing mode for
  1738.          this operand, it overrides all others.
  1739.  
  1740.          I am not sure whether the algorithm here is always right,
  1741.          but it does the right things in those cases.  */
  1742.  
  1743.       mode = GET_MODE (old);
  1744.       if (mode == VOIDmode)
  1745.         mode = reload_inmode[j];
  1746.       if (reload_strict_low[j])
  1747.         mode = GET_MODE (SUBREG_REG (reload_in[j]));
  1748.  
  1749.       /* If reloading from memory, see if there is a register
  1750.          that already holds the same value.  If so, reload from there.
  1751.          We can pass 0 as the reload_reg_p argument because
  1752.          any other reload has either already been emitted,
  1753.          in which case find_equiv_reg will see the reload-insn,
  1754.          or has yet to be emitted, in which case it doesn't matter
  1755.          because we will use this equiv reg right away.  */
  1756.  
  1757.       if (GET_CODE (old) == MEM
  1758.           || (GET_CODE (old) == REG
  1759.           && REGNO (old) >= FIRST_PSEUDO_REGISTER
  1760.           && reg_renumber[REGNO (old)] < 0))
  1761.         oldequiv = find_equiv_reg (old, insn, GENERAL_REGS,
  1762.                        -1, 0, 0, mode);
  1763.  
  1764.       if (oldequiv == 0)
  1765.         oldequiv = old;
  1766.  
  1767.       /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
  1768.          then load RELOADREG from OLDEQUIV.  */
  1769.  
  1770.       if (GET_MODE (reloadreg) != mode)
  1771.         reloadreg = gen_rtx (SUBREG, mode, reloadreg, 0);
  1772.       if (GET_MODE (oldequiv) != VOIDmode
  1773.           && mode != GET_MODE (oldequiv))
  1774.         oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
  1775.  
  1776.       /* If we are reloading a pseudo-register that was set by the previous
  1777.          insn, see if we can get rid of that pseudo-register entirely
  1778.          by redirecting the previous insn into our reload register.  */
  1779.  
  1780.       if (optimize && GET_CODE (old) == REG
  1781.           && REGNO (old) >= FIRST_PSEUDO_REGISTER
  1782.           && PREV_INSN (insn) && GET_CODE (PREV_INSN (insn)) == INSN
  1783.           && GET_CODE (PATTERN (PREV_INSN (insn))) == SET
  1784.           && SET_DEST (PATTERN (PREV_INSN (insn))) == old
  1785.           && dead_or_set_p (insn, old)
  1786.           && reg_n_deaths[REGNO (old)] == 1
  1787.           && reg_n_sets[REGNO (old)] == 1)
  1788.         {
  1789.           /* For the debugging info,
  1790.          say the pseudo lives in this reload reg.  */
  1791.           reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
  1792.           alter_reg (REGNO (old));
  1793.           /* Store into the reload register instead of the pseudo.  */
  1794.           SET_DEST (PATTERN (PREV_INSN (insn))) = reloadreg;
  1795.         }
  1796.       else
  1797.         /* We can't do that, so output an insn to load RELOADREG.  */
  1798.         emit_insn_before (gen_move_insn (reloadreg, oldequiv), insn);
  1799.  
  1800.       /* For some registers it is important to keep the REG_DEATH
  1801.          notes accurate for the final pass.
  1802.          If we are inheriting an old output-reload out of such a reg,
  1803.          the reg no longer dies there, so remove the death note.  */
  1804.       
  1805. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  1806.       if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
  1807.         {
  1808.           int was_dead = 0;
  1809.           if (REG_P (oldequiv)
  1810.           && regno_dead_p (REGNO (oldequiv), insn))
  1811.         {
  1812.           was_dead = 1;
  1813.           remove_death (REGNO (oldequiv), insn);
  1814.         }
  1815.  
  1816.           /* Add a death note to this insn, for an input reload.  */
  1817.  
  1818.           if (! dead_or_set_p (insn, reloadreg))
  1819.         REG_NOTES (insn)
  1820.           = gen_rtx (EXPR_LIST, REG_DEAD,
  1821.                  reloadreg, REG_NOTES (insn));
  1822.         }
  1823. #endif
  1824.  
  1825.       /* If this reload wants reload_in[j] incremented by a constant,
  1826.          output code to get this done before the insn reloaded for.  */
  1827.  
  1828.       if (reload_inc[j] != 0)
  1829.         {
  1830.           /* If reload_in[j] is a register, assume we can
  1831.          output an insn to increment it directly.  */
  1832.           if (GET_CODE (old) == REG &&
  1833.           (REGNO (old) < FIRST_PSEUDO_REGISTER
  1834.            || reg_renumber[REGNO (old)] >= 0))
  1835.         emit_insn_before (gen_add2_insn (old,
  1836.                          gen_rtx (CONST_INT, VOIDmode,
  1837.                               reload_inc[j])),
  1838.                   insn);
  1839.           else
  1840.         /* Else we must not assume we can increment reload_in[j]
  1841.            (even though on many target machines we can);
  1842.            increment the copy in the reload register,
  1843.            save that back, then decrement the reload register
  1844.            so it has its original contents.  */
  1845.         {
  1846.           rtx oldreal = old;
  1847.           /* OLDREAL is OLDEQUIV encapsulated in that mode,
  1848.              in case we need to write back to it.
  1849.              OLDEQUIV is good only for reading.  */
  1850.           if (GET_MODE (old) != VOIDmode
  1851.               && mode != GET_MODE (old))
  1852.             oldreal = gen_rtx (SUBREG, mode, oldreal, 0);
  1853.  
  1854.           emit_insn_before (gen_add2_insn (reloadreg,
  1855.                            gen_rtx (CONST_INT, VOIDmode,
  1856.                                 reload_inc[j])),
  1857.                     insn);
  1858.           emit_insn_before (gen_move_insn (oldreal, reloadreg), insn);
  1859.           emit_insn_before (gen_sub2_insn (reloadreg,
  1860.                            gen_rtx (CONST_INT, VOIDmode,
  1861.                                 reload_inc[j])),
  1862.                     insn);
  1863.         }
  1864.         }
  1865.     }
  1866.  
  1867.       /* If we are reloading a register that was recently stored in with an
  1868.      output-reload, see if we can prove there was
  1869.      actually no need to store the old value in it.  */
  1870.  
  1871.       if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
  1872.       && GET_CODE (reload_in[j]) == REG
  1873.       && spill_reg_store[reload_spill_index[j]] != 0
  1874.       && dead_or_set_p (insn, reload_in[j]))
  1875.     {
  1876.       register rtx i1;
  1877.       /* If the pseudo-reg we are reloading is no longer referenced
  1878.          anywhere between the store into it and here,
  1879.          and no jumps or labels intervene, then the value can get
  1880.          here through the reload reg alone.  */
  1881.       for (i1 = NEXT_INSN (spill_reg_store[reload_spill_index[j]]);
  1882.            i1 != insn; i1 = NEXT_INSN (i1))
  1883.         {
  1884.           if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
  1885.         break;
  1886.           if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
  1887.           && reg_mentioned_p (reload_in[j], PATTERN (i1)))
  1888.         break;
  1889.         }
  1890.       if (i1 == insn)
  1891.         {
  1892.           /* If this insn will store in the pseudo again,
  1893.          the previous store can be removed.  */
  1894.           if (reload_out[j] == reload_in[j])
  1895.         delete_insn (spill_reg_store[reload_spill_index[j]]);
  1896.  
  1897.           /* See if the pseudo reg has been completely replaced
  1898.          with reload regs.  If so, delete the store insn
  1899.          and forget we had a stack slot for the pseudo.  */
  1900.           if (reg_n_deaths[REGNO (reload_in[j])] == 1
  1901.           && reg_basic_block[REGNO (reload_in[j])] >= 0)
  1902.         {
  1903.           /* We know that it was used only between here
  1904.              and the beginning of the current basic block.
  1905.              Search that range; see if any ref remains.  */
  1906.           for (i1 = PREV_INSN (insn); i1; i1 = PREV_INSN (i1))
  1907.             {
  1908.               if (GET_CODE (i1) == CODE_LABEL
  1909.               || GET_CODE (i1) == JUMP_INSN)
  1910.             break;
  1911.               if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
  1912.               && reg_mentioned_p (reload_in[j], PATTERN (i1)))
  1913.             goto still_used;
  1914.             }
  1915.           /* For the debugging info,
  1916.              say the pseudo lives in this reload reg.  */
  1917.           reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
  1918.           alter_reg (REGNO (old));
  1919.           delete_insn (spill_reg_store[reload_spill_index[j]]);
  1920.         still_used: ;
  1921.         }
  1922.         }
  1923.     }
  1924.  
  1925.       /* Input-reloading is done.  Now do output-reloading,
  1926.      storing the value from the reload-register after the main insn
  1927.      if reload_out[j] is nonzero.  */
  1928.       old = reload_out[j];
  1929.       if (old != 0
  1930.       && reload_reg_rtx[j] != old
  1931.       && reload_reg_rtx[j] != 0)
  1932.     {
  1933.       register rtx reloadreg = reload_reg_rtx[j];
  1934.       enum machine_mode mode;
  1935.  
  1936.       /* Strip off of OLD any size-increasing SUBREGs such as
  1937.          (SUBREG:SI foo:QI 0).  */
  1938.  
  1939.       while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
  1940.          && (GET_MODE_SIZE (GET_MODE (old))
  1941.              > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
  1942.         old = SUBREG_REG (old);
  1943.  
  1944.       /* Determine the mode to reload in.
  1945.          See comments above (for input reloading).  */
  1946.  
  1947.       mode = GET_MODE (old);
  1948.       if (mode == VOIDmode)
  1949.         abort ();        /* Should never happen for an output.  */
  1950. #if 0
  1951.         mode = reload_inmode[j];
  1952. #endif
  1953.       if (reload_strict_low[j])
  1954.         mode = GET_MODE (SUBREG_REG (reload_out[j]));
  1955.  
  1956.       /* Encapsulate both RELOADREG and OLD into that mode,
  1957.          then load RELOADREG from OLD.  */
  1958.       if (GET_MODE (reloadreg) != mode)
  1959.         reloadreg = gen_rtx (SUBREG, mode, reloadreg, 0);
  1960.       if (GET_MODE (old) != VOIDmode
  1961.           && mode != GET_MODE (old))
  1962.         old = gen_rtx (SUBREG, mode, old, 0);
  1963.       store_insn = emit_insn_after (gen_move_insn (old, reloadreg), insn);
  1964.       /* If final will look at death notes for this reg,
  1965.          put one on each output-reload insn.  */
  1966. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  1967.       if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
  1968.         REG_NOTES (store_insn)
  1969.         = gen_rtx (EXPR_LIST, REG_DEAD,
  1970.                reloadreg, REG_NOTES (store_insn));
  1971. #endif
  1972.     }
  1973.       else store_insn = 0;
  1974.  
  1975.       if (reload_spill_index[j] >= 0)
  1976.     spill_reg_store[reload_spill_index[j]] = store_insn;
  1977.     }
  1978. }
  1979.